File tree Expand file tree Collapse file tree
algorithm/graph_search/bellman_ford/shortest_path Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,17 +38,19 @@ function BELLMAN_FORD (src, dest) {
3838 }
3939
4040 //check for cycle
41+ tracer . _print ( 'checking for cycle' ) ;
4142 for ( currentNode = 0 ; currentNode < G . length ; currentNode ++ ) {
4243 for ( currentNodeNeighbor = 0 ; currentNodeNeighbor <= G . length ; currentNodeNeighbor ++ ) {
4344 if ( G [ currentNode ] [ currentNodeNeighbor ] ) {
4445 if ( weights [ currentNodeNeighbor ] > ( weights [ currentNode ] + G [ currentNode ] [ currentNodeNeighbor ] ) ) {
46+ tracer . _print ( 'A cycle was detected: weights [' + currentNodeNeighbor + '] > weights [' + currentNode + '] + ' + G [ currentNode ] [ currentNodeNeighbor ] ) ;
4547 return ( MAX_VALUE ) ;
4648 }
4749 }
4850 }
4951 }
5052
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 + ']' ) ;
5254
5355 return weights [ dest ] ;
5456}
Original file line number Diff line number Diff line change 11var tracer = new DirectedGraphTracer ( ) ;
2- //var G = WeightedGraph.random( (Math.random () * 10) | 0, .3, -9, 9);
32var G = [
43 [ 0 , - 1 , 4 , 0 , 0 ] ,
54 [ 0 , 0 , 3 , 2 , 2 ] ,
@@ -8,4 +7,4 @@ var G = [
87 [ 0 , 0 , 0 , - 3 , 0 ]
98] ;
109
11- tracer . _setData ( G ) ;
10+ tracer . _setData ( G ) ;
You can’t perform that action at this time.
0 commit comments