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
+ 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
+ return ( MAX_VALUE ) ;
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ tracer . _print ( 'Final weights for the source ' + src + ' are: [' + weights + ']' ) ;
52
+
53
+ return weights [ dest ] ;
54
+ }
55
+
56
+ var src = Math . random ( ) * G . length | 0 , dest ;
57
+ var MAX_VALUE = Infinity ;
58
+ var minWeight ;
59
+
60
+ /*
61
+ src = start node
62
+ dest = start node (but will eventually at as the end node)
63
+ */
64
+
65
+ do {
66
+ dest = Math . random ( ) * G . length | 0 ;
67
+ }
68
+ while ( src === dest ) ;
69
+
70
+ tracer . _pace ( 100 ) ;
71
+ tracer . _print ( 'finding the shortest path from ' + src + ' to ' + dest ) ;
72
+ tracer . _sleep ( 1000 ) ;
73
+
74
+ minWeight = BELLMAN_FORD ( src , dest ) ;
75
+
76
+ if ( minWeight === MAX_VALUE ) {
77
+ tracer . _print ( 'there is no path from ' + src + ' to ' + dest ) ;
78
+ } else {
79
+ tracer . _print ( 'the shortest path from ' + src + ' to ' + dest + ' is ' + minWeight ) ;
80
+ }
0 commit comments