Skip to content

Commit d49cf9f

Browse files
authored
chore: Added BellmanFord (#679)
* Added BellmanFord * Add References for BellmanFord * Style code using standard.js * Add tests and modify code * Fixed BellmanFord test file * Add BellmanFord and tests
1 parent 8d72872 commit d49cf9f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Graphs/BellmanFord.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
The Bellman–Ford algorithm is an algorithm that computes shortest paths
3+
from a single source vertex to all of the other vertices in a weighted digraph.
4+
It also detects negative weight cycle.
5+
6+
Complexity:
7+
Worst-case performance O(VE)
8+
Best-case performance O(E)
9+
Worst-case space complexity O(V)
10+
11+
Reference:
12+
https://en.wikipedia.org/wiki/Bellman–Ford_algorithm
13+
https://cp-algorithms.com/graph/bellman_ford.html
14+
15+
*/
16+
17+
/**
18+
*
19+
* @param graph Graph in the format (u, v, w) where
20+
* the edge is from vertex u to v. And weight
21+
* of the edge is w.
22+
* @param V Number of vertices in graph
23+
* @param E Number of edges in graph
24+
* @param src Starting node
25+
* @param dest Destination node
26+
* @returns Shortest distance from source to destination
27+
*/
28+
function BellmanFord (graph, V, E, src, dest) {
29+
// Initialize distance of all vertices as infinite.
30+
const dis = Array(V).fill(Infinity)
31+
// initialize distance of source as 0
32+
dis[src] = 0
33+
34+
// Relax all edges |V| - 1 times. A simple
35+
// shortest path from src to any other
36+
// vertex can have at-most |V| - 1 edges
37+
for (let i = 0; i < V - 1; i++) {
38+
for (let j = 0; j < E; j++) {
39+
if ((dis[graph[j][0]] + graph[j][2]) < dis[graph[j][1]]) { dis[graph[j][1]] = dis[graph[j][0]] + graph[j][2] }
40+
}
41+
}
42+
// check for negative-weight cycles.
43+
for (let i = 0; i < E; i++) {
44+
const x = graph[i][0]
45+
const y = graph[i][1]
46+
const weight = graph[i][2]
47+
if ((dis[x] !== Infinity) && (dis[x] + weight < dis[y])) {
48+
return null
49+
}
50+
}
51+
for (let i = 0; i < V; i++) {
52+
if (i === dest) return dis[i]
53+
}
54+
}
55+
56+
export { BellmanFord }

Graphs/test/BellmanFord.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { BellmanFord } from '../BellmanFord.js'
2+
3+
test('Test Case 1', () => {
4+
const V = 5
5+
const E = 8
6+
const destination = 3
7+
const graph = [[0, 1, -1], [0, 2, 4],
8+
[1, 2, 3], [1, 3, 2],
9+
[1, 4, 2], [3, 2, 5],
10+
[3, 1, 1], [4, 3, -3]]
11+
const dist = BellmanFord(graph, V, E, 0, destination)
12+
expect(dist).toBe(-2)
13+
})
14+
test('Test Case 2', () => {
15+
const V = 6
16+
const E = 9
17+
const destination = 4
18+
const graph = [[0, 1, 3], [0, 3, 6],
19+
[0, 5, -1], [1, 2, -3],
20+
[1, 4, -2], [5, 2, 5],
21+
[2, 3, 1], [4, 3, 5], [5, 4, 2]]
22+
const dist = BellmanFord(graph, V, E, 0, destination)
23+
expect(dist).toBe(1)
24+
})
25+
test('Test Case 3', () => {
26+
const V = 4
27+
const E = 5
28+
const destination = 1
29+
const graph = [[0, 3, -1], [0, 2, 4],
30+
[3, 2, 2], [3, 1, 5],
31+
[2, 1, -1]]
32+
const dist = BellmanFord(graph, V, E, 0, destination)
33+
expect(dist).toBe(0)
34+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy