Skip to content

Commit 122aed6

Browse files
committed
Merge branch 'duaraghav8-master'
2 parents c091633 + 1a6e32c commit 122aed6

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

algorithm/category.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"list": {
55
"dfs": "DFS",
66
"bfs": "BFS",
7-
"dijkstra": "Dijkstra"
7+
"dijkstra": "Dijkstra",
8+
"bellman_ford": "Bellman-Ford"
89
}
910
},
1011
"search": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"BELLMAN_FORD": "The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph. It is different from Dijksstra's Shortest Path Algorithm becuase it allows NEGATIVE weights unlike Dijkstra's.",
3+
"Applications": [
4+
"Packet Routing - A variation of BF is used in the Distance vector Routing Protocol"
5+
],
6+
"Complexity": {
7+
"time": "worst O(|V|.|E|)",
8+
"space": "worst O(|V|)"
9+
},
10+
"References": [
11+
"<a href='https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm'>Wikipedia</a>"
12+
],
13+
"files": {
14+
"shortest_path": "Finding the shortest path"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
function BELLMAN_FORD (src, dest) {
2+
var weights = new Array (G.length);
3+
4+
for (var i = 0; i < G.length; i++) {
5+
weights [i] = MAX_VALUE;
6+
tracer._weight (i, MAX_VALUE);
7+
}
8+
weights [src] = 0;
9+
tracer._weight (src, 0);
10+
11+
tracer._print ('Initializing weights to: [' + weights + ']');
12+
tracer._print ('');
13+
14+
//begin BF algorithm execution
15+
i = G.length - 1;
16+
while (i--) {
17+
tracer._print ('Iteration: ' + (G.length - i - 1));
18+
tracer._print ('------------------------------------------------------------------');
19+
20+
for (var currentNode = 0; currentNode < G.length; currentNode++) {
21+
for (var currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
22+
if (G [currentNode] [currentNodeNeighbor]) { //proceed to relax Edges only if a particular weight != 0 (0 represents no edge)
23+
tracer._print ('Exploring edge from ' + currentNode + ' to ' + currentNodeNeighbor + ', weight = ' + G [currentNode] [currentNodeNeighbor]);
24+
tracer._visit (currentNodeNeighbor, currentNode, undefined);
25+
26+
if ( weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor]) ) {
27+
weights [currentNodeNeighbor] = weights [currentNode] + G [currentNode] [currentNodeNeighbor];
28+
tracer._print ('weights [' + currentNodeNeighbor + '] = weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
29+
}
30+
31+
tracer._leave (currentNodeNeighbor, currentNode, undefined);
32+
}
33+
}
34+
}
35+
36+
tracer._print ('updated weights: [' + weights + ']');
37+
tracer._print ('');
38+
}
39+
40+
//check for cycle
41+
tracer._print ('checking for cycle');
42+
for (currentNode = 0; currentNode < G.length; currentNode++) {
43+
for (currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
44+
if (G [currentNode] [currentNodeNeighbor]) {
45+
if ( weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor]) ) {
46+
tracer._print ('A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
47+
return (MAX_VALUE);
48+
}
49+
}
50+
}
51+
}
52+
53+
tracer._print ('No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']');
54+
55+
return weights [dest];
56+
}
57+
58+
var src = Math.random() * G.length | 0, dest;
59+
var MAX_VALUE = Infinity;
60+
var minWeight;
61+
62+
/*
63+
src = start node
64+
dest = start node (but will eventually at as the end node)
65+
*/
66+
67+
do {
68+
dest = Math.random() * G.length | 0;
69+
}
70+
while (src === dest);
71+
72+
tracer._pace(100);
73+
tracer._print('finding the shortest path from ' + src + ' to ' + dest);
74+
tracer._sleep(1000);
75+
76+
minWeight = BELLMAN_FORD (src, dest);
77+
78+
if (minWeight === MAX_VALUE) {
79+
tracer._print('there is no path from ' + src + ' to ' + dest);
80+
} else {
81+
tracer._print('the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight);
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var tracer = new DirectedGraphTracer();
2+
var G = [
3+
[0,-1,4,0,0],
4+
[0,0,3,2,2],
5+
[0,0,0,0,0],
6+
[0, 1,5,0,0],
7+
[0,0,0,-3,0]
8+
];
9+
10+
tracer._setData(G);

0 commit comments

Comments
 (0)