Skip to content

Commit f05d2e2

Browse files
author
duaraghav8@gmail
committed
Fixed bug in Bellman Ford data.js
1 parent 230140d commit f05d2e2

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

algorithm/graph_search/bellman_ford/shortest_path/code.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,19 @@ function BELLMAN_FORD (src, dest) {
3838
}
3939

4040
//check for cycle
41+
tracer._print ('checking for cycle');
4142
for (currentNode = 0; currentNode < G.length; currentNode++) {
4243
for (currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
4344
if (G [currentNode] [currentNodeNeighbor]) {
4445
if ( weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor]) ) {
46+
tracer._print ('A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
4547
return (MAX_VALUE);
4648
}
4749
}
4850
}
4951
}
5052

51-
tracer._print ('Final weights for the source ' + src + ' are: [' + weights + ']');
53+
tracer._print ('No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']');
5254

5355
return weights [dest];
5456
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var tracer = new DirectedGraphTracer();
2-
//var G = WeightedGraph.random( (Math.random () * 10) | 0, .3, -9, 9);
32
var G = [
43
[0,-1,4,0,0],
54
[0,0,3,2,2],
@@ -8,4 +7,4 @@ var G = [
87
[0,0,0,-3,0]
98
];
109

11-
tracer._setData(G);
10+
tracer._setData(G);

0 commit comments

Comments
 (0)