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