-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellman-ford.js
76 lines (65 loc) · 2.21 KB
/
bellman-ford.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class Edge {
constructor(source, destination, weight) {
this.source = source;
this.weight = weight;
this.destination = destination;
}
}
class Graph {
constructor(vertices, edges) {
this.edges = edges;
this.vertices = vertices;
}
addEdge(source, destination, weight) {
this.edges.push(new Edge(source, destination, weight));
}
bellmanFord(startVertex) {
let distance = {};
let predecessor = {};
this.vertices.forEach(vertex => {
distance[vertex] = Infinity;
predecessor[vertex] = null;
});
distance[startVertex] = 0;
for (let i = 0; i < this.vertices.length - 1; i++) {
this.edges.forEach(edge => {
let { source, destination, weight } = edge;
if (distance[source] + weight < distance[destination]) {
distance[destination] = distance[source] + weight;
predecessor[destination] = source;
}
});
}
this.edges.forEach(edge => {
let { source, destination, weight } = edge;
if (distance[source] + weight < distance[destination]) {
throw new Error("Graph contains a negative weight cycle");
}
});
return { distance, predecessor };
}
printShortestPath(startVertex, endVertex, predecessor) {
let path = [];
let currentVertex = endVertex;
while (currentVertex !== startVertex) {
path.unshift(currentVertex);
currentVertex = predecessor[currentVertex];
}
path.unshift(startVertex);
console.log("Shortest Path:", path.join(" -> "));
}
}
const graph = new Graph(["A", "B", "C", "D", "E"], []);
graph.addEdge("A", "B", 6);
graph.addEdge("A", "D", 1);
graph.addEdge("B", "C", 5);
graph.addEdge("B", "D", 2);
graph.addEdge("D", "B", 2);
graph.addEdge("D", "E", 1);
graph.addEdge("E", "B", -1);
graph.addEdge("E", "C", 5);
const startVertex = "A";
const { distance, predecessor } = graph.bellmanFord(startVertex);
console.log("Distance:", distance);
console.log("Predecessor:", predecessor);
graph.printShortestPath(startVertex, "C", predecessor);