File tree 2 files changed +4
-3
lines changed
algorithm/graph_search/bellman_ford/shortest_path
2 files changed +4
-3
lines changed Original file line number Diff line number Diff line change @@ -38,17 +38,19 @@ function BELLMAN_FORD (src, dest) {
38
38
}
39
39
40
40
//check for cycle
41
+ tracer . _print ( 'checking for cycle' ) ;
41
42
for ( currentNode = 0 ; currentNode < G . length ; currentNode ++ ) {
42
43
for ( currentNodeNeighbor = 0 ; currentNodeNeighbor <= G . length ; currentNodeNeighbor ++ ) {
43
44
if ( G [ currentNode ] [ currentNodeNeighbor ] ) {
44
45
if ( weights [ currentNodeNeighbor ] > ( weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ) ) {
46
+ tracer . _print ( 'A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [ currentNode ] [ currentNodeNeighbor ] ) ;
45
47
return ( MAX_VALUE ) ;
46
48
}
47
49
}
48
50
}
49
51
}
50
52
51
- tracer . _print ( 'Final weights for the source ' + src + ' are: [' + weights + ']' ) ;
53
+ tracer . _print ( 'No cycles detected. Final weights for the source ' + src + ' are: [' + weights + ']' ) ;
52
54
53
55
return weights [ dest ] ;
54
56
}
Original file line number Diff line number Diff line change 1
1
var tracer = new DirectedGraphTracer ( ) ;
2
- //var G = WeightedGraph.random( (Math.random () * 10) | 0, .3, -9, 9);
3
2
var G = [
4
3
[ 0 , - 1 , 4 , 0 , 0 ] ,
5
4
[ 0 , 0 , 3 , 2 , 2 ] ,
@@ -8,4 +7,4 @@ var G = [
8
7
[ 0 , 0 , 0 , - 3 , 0 ]
9
8
] ;
10
9
11
- tracer . _setData ( G ) ;
10
+ tracer . _setData ( G ) ;
You can’t perform that action at this time.
0 commit comments