Skip to content

Commit 22fcb4a

Browse files
committed
bellman_ford: update weight of each node
1 parent dd84205 commit 22fcb4a

File tree

1 file changed

+4
-3
lines changed
  • algorithm/graph_search/bellman_ford/shortest_path

1 file changed

+4
-3
lines changed

algorithm/graph_search/bellman_ford/shortest_path/code.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ function BELLMAN_FORD (src, dest) {
33

44
for (var i = 0; i < G.length; i++) {
55
weights [i] = MAX_VALUE;
6+
tracer._weight(i, weights[i]);
67
}
78
weights [src] = 0;
9+
tracer._weight(src, 0);
810

911
tracer._print ('Initializing weights to: [' + weights + ']');
1012
tracer._print ('');
@@ -19,14 +21,13 @@ function BELLMAN_FORD (src, dest) {
1921
for (var currentNodeNeighbor = 0; currentNodeNeighbor <= G.length; currentNodeNeighbor++) {
2022
if (G [currentNode] [currentNodeNeighbor]) { //proceed to relax Edges only if a particular weight != 0 (0 represents no edge)
2123
tracer._print ('Exploring edge from ' + currentNode + ' to ' + currentNodeNeighbor + ', weight = ' + G [currentNode] [currentNodeNeighbor]);
22-
tracer._visit (currentNodeNeighbor, currentNode, undefined);
2324

2425
if ( weights [currentNodeNeighbor] > (weights [currentNode] + G [currentNode] [currentNodeNeighbor]) ) {
2526
weights [currentNodeNeighbor] = weights [currentNode] + G [currentNode] [currentNodeNeighbor];
2627
tracer._print ('weights [' + currentNodeNeighbor + '] = weights [' + currentNode + '] + ' + G [currentNode] [currentNodeNeighbor]);
2728
}
28-
29-
tracer._leave (currentNodeNeighbor, currentNode, undefined);
29+
tracer._visit (currentNodeNeighbor, currentNode, weights [currentNodeNeighbor]);
30+
tracer._leave (currentNodeNeighbor, currentNode);
3031
}
3132
}
3233
}

0 commit comments

Comments
 (0)