@@ -1197,14 +1197,31 @@ G.addDirectedEdge(G.V['y'], G.V['x'], -3);
1197
1197
G . addDirectedEdge ( G . V [ 'y' ] , G . V [ 'z' ] , 9 ) ;
1198
1198
G . addDirectedEdge ( G . V [ 'z' ] , G . V [ 'x' ] , 7 ) ;
1199
1199
G . addDirectedEdge ( G . V [ 'z' ] , G . V [ 's' ] , 2 ) ;
1200
- // console.log(G);
1201
- let isReachable = bellmanFord ( G , G . V [ 's' ] ) ;
1202
1200
1203
- console . log ( `There is a negative-weight cycle that is reachable from the source: ${ isReachable } ` ) ;
1201
+ console . log ( '\nInitial directed weighted graph:\n' ) ;
1202
+ console . log (
1203
+ ` t -2${ arrows . l . repeat ( 2 ) } |${ arrows . r . repeat ( 2 ) } 5 x`
1204
+ + `\n 6${ arrows . ur } ${ arrows . d } ${ arrows . dr } ${ arrows . ur } ${ arrows . u } `
1205
+ + `\ns 8 -4 -3 7`
1206
+ + `\n 7${ arrows . dr } ${ arrows . d } ${ arrows . ur } ${ arrows . dr } ${ arrows . u } `
1207
+ + `\n y ${ arrows . r } 9${ arrows . r } z`
1208
+ + `\n(not pictured: s2${ arrows . ul } z)`
1209
+ ) ;
1204
1210
1205
- for ( let v in G . V ) {
1206
- console . log ( G . V [ v ] ) ;
1207
- }
1211
+ let isReachable = bellmanFord ( G , G . V [ 's' ] ) ;
1212
+ console . log ( '\nSingle-source shortest path:\n' ) ;
1213
+ console . log (
1214
+ ` t,${ G . V [ 't' ] . d } ${ arrows . l } -2${ arrows . l } x,${ G . V [ 'x' ] . d } `
1215
+ + `\n ${ arrows . dr } ${ arrows . ur } `
1216
+ + `\ns,${ G . V [ 's' ] . d } -4 -3`
1217
+ + `\n 7${ arrows . dr } ${ arrows . ur } ${ arrows . dr } `
1218
+ + `\n y,${ G . V [ 'y' ] . d } z,${ G . V [ 'z' ] . d } `
1219
+ ) ;
1220
+ console . log ( `\nThere is a negative-weight cycle that is reachable from the source: ${ isReachable } ` ) ;
1221
+
1222
+ // for(let v in G.V) {
1223
+ // console.log(G.V[v]);
1224
+ // }
1208
1225
1209
1226
// TO-DO: tweak for Dijkstra
1210
1227
// G.addDirectedEdge(new GraphVertexSSSP('s'), new GraphVertexSSSP('y'), 5); // cf. Figure 24.2
0 commit comments