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

Lines changed: 4 additions & 3 deletions
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)
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