1- function BELLMAN_FORD ( src , dest ) {
2- var weights = new Array ( G . length ) ;
1+ function BELLMAN_FORD ( src , dest ) {
2+ var weights = new Array ( G . length ) ;
33
4- for ( var i = 0 ; i < G . length ; i ++ ) {
5- weights [ i ] = MAX_VALUE ;
4+ for ( var i = 0 ; i < G . length ; i ++ ) {
5+ weights [ i ] = MAX_VALUE ;
66 tracer . _weight ( i , weights [ i ] ) ;
7- }
8- weights [ src ] = 0 ;
7+ }
8+ weights [ src ] = 0 ;
99 tracer . _weight ( src , 0 ) ;
1010
11- tracer . _print ( 'Initializing weights to: [' + weights + ']' ) ;
12- tracer . _print ( '' ) ;
11+ logger . _print ( 'Initializing weights to: [' + weights + ']' ) ;
12+ logger . _print ( '' ) ;
1313
14- //begin BF algorithm execution
15- i = G . length - 1 ;
16- while ( i -- ) {
17- tracer . _print ( 'Iteration: ' + ( G . length - i - 1 ) ) ;
18- tracer . _print ( '------------------------------------------------------------------' ) ;
14+ //begin BF algorithm execution
15+ i = G . length - 1 ;
16+ while ( i -- ) {
17+ logger . _print ( 'Iteration: ' + ( G . length - i - 1 ) ) ;
18+ logger . _print ( '------------------------------------------------------------------' ) ;
1919
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 ] ) ;
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+ logger . _next ( ) . _print ( 'Exploring edge from ' + currentNode + ' to ' + currentNodeNeighbor + ', weight = ' + G [ currentNode ] [ currentNodeNeighbor ] ) ;
2424
25- if ( weights [ currentNodeNeighbor ] > ( weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ) ) {
26- weights [ currentNodeNeighbor ] = weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ;
27- tracer . _print ( 'weights [' + currentNodeNeighbor + '] = weights [' + currentNode + '] + ' + G [ currentNode ] [ currentNodeNeighbor ] ) ;
28- }
29- tracer . _visit ( currentNodeNeighbor , currentNode , weights [ currentNodeNeighbor ] ) ;
30- tracer . _leave ( currentNodeNeighbor , currentNode ) ;
31- }
32- }
33- }
25+ if ( weights [ currentNodeNeighbor ] > ( weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ) ) {
26+ weights [ currentNodeNeighbor ] = weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ;
27+ logger . _print ( 'weights [' + currentNodeNeighbor + '] = weights [' + currentNode + '] + ' + G [ currentNode ] [ currentNodeNeighbor ] ) ;
28+ }
29+ tracer . _visit ( currentNodeNeighbor , currentNode , weights [ currentNodeNeighbor ] ) ;
30+ tracer . _next ( ) . _leave ( currentNodeNeighbor , currentNode ) ;
31+ }
32+ }
33+ }
3434
35- tracer . _print ( 'updated weights: [' + weights + ']' ) ;
36- tracer . _print ( '' ) ;
37- }
35+ logger . _print ( 'updated weights: [' + weights + ']' ) ;
36+ logger . _print ( '' ) ;
37+ }
3838
39- //check for cycle
40- tracer . _print ( 'checking for cycle' ) ;
41- for ( currentNode = 0 ; currentNode < G . length ; currentNode ++ ) {
42- for ( currentNodeNeighbor = 0 ; currentNodeNeighbor <= G . length ; currentNodeNeighbor ++ ) {
43- if ( G [ currentNode ] [ currentNodeNeighbor ] ) {
44- if ( weights [ currentNodeNeighbor ] > ( weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ) ) {
45- tracer . _print ( 'A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [ currentNode ] [ currentNodeNeighbor ] ) ;
46- return ( MAX_VALUE ) ;
47- }
48- }
49- }
50- }
39+ //check for cycle
40+ logger . _print ( 'checking for cycle' ) ;
41+ for ( currentNode = 0 ; currentNode < G . length ; currentNode ++ ) {
42+ for ( currentNodeNeighbor = 0 ; currentNodeNeighbor <= G . length ; currentNodeNeighbor ++ ) {
43+ if ( G [ currentNode ] [ currentNodeNeighbor ] ) {
44+ if ( weights [ currentNodeNeighbor ] > ( weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ) ) {
45+ logger . _print ( 'A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [ currentNode ] [ currentNodeNeighbor ] ) ;
46+ return ( MAX_VALUE ) ;
47+ }
48+ }
49+ }
50+ }
5151
52- tracer . _print ( 'No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']' ) ;
52+ logger . _print ( 'No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']' ) ;
5353
54- return weights [ dest ] ;
54+ return weights [ dest ] ;
5555}
5656
5757var src = Math . random ( ) * G . length | 0 , dest ;
5858var MAX_VALUE = Infinity ;
5959var minWeight ;
6060
6161/*
62- src = start node
63- dest = start node (but will eventually at as the end node)
64- */
62+ src = start node
63+ dest = start node (but will eventually at as the end node)
64+ */
6565
6666do {
6767 dest = Math . random ( ) * G . length | 0 ;
6868}
6969while ( src === dest ) ;
7070
71- tracer . _print ( 'finding the shortest path from ' + src + ' to ' + dest ) ;
72- tracer . _sleep ( 1000 ) ;
71+ logger . _print ( 'finding the shortest path from ' + src + ' to ' + dest ) ;
7372
74- minWeight = BELLMAN_FORD ( src , dest ) ;
73+ minWeight = BELLMAN_FORD ( src , dest ) ;
7574
7675if ( minWeight === MAX_VALUE ) {
77- tracer . _print ( 'there is no path from ' + src + ' to ' + dest ) ;
76+ logger . _print ( 'there is no path from ' + src + ' to ' + dest ) ;
7877} else {
79- tracer . _print ( 'the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight ) ;
78+ logger . _print ( 'the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight ) ;
8079}
0 commit comments