@@ -1187,11 +1187,11 @@ console.log('\nSection 24.1 - Bellman-Ford Algorithm (Single-Source Shortest Pat
1187
1187
1188
1188
G = new WeightedGraph ( ) ;
1189
1189
G . addDirectedEdge ( new GraphVertexSSSP ( 's' ) , new GraphVertexSSSP ( 'y' ) , 7 ) ; // cf. Figure 24.4, p. 652
1190
- G . addDirectedEdge ( G . V [ 's' ] , new GraphVertexSSSP ( 't' ) , 6 ) ;
1191
- G . addDirectedEdge ( G . V [ 't ' ] , new GraphVertexSSSP ( 'x' ) , 5 ) ;
1190
+ G . addDirectedEdge ( new GraphVertexSSSP ( 'x' ) , new GraphVertexSSSP ( 't' ) , - 2 ) ;
1191
+ G . addDirectedEdge ( G . V [ 's ' ] , G . V [ 't' ] , 6 ) ;
1192
1192
G . addDirectedEdge ( G . V [ 't' ] , G . V [ 'y' ] , 8 ) ;
1193
1193
G . addDirectedEdge ( G . V [ 't' ] , new GraphVertexSSSP ( 'z' ) , - 4 ) ;
1194
- G . addDirectedEdge ( G . V [ 'x ' ] , G . V [ 't ' ] , - 2 ) ;
1194
+ G . addDirectedEdge ( G . V [ 't ' ] , G . V [ 'x ' ] , 5 ) ;
1195
1195
G . addDirectedEdge ( G . V [ 'y' ] , G . V [ 'x' ] , - 3 ) ;
1196
1196
G . addDirectedEdge ( G . V [ 'y' ] , G . V [ 'z' ] , 9 ) ;
1197
1197
G . addDirectedEdge ( G . V [ 'z' ] , G . V [ 'x' ] , 7 ) ;
@@ -1209,6 +1209,14 @@ console.log(
1209
1209
1210
1210
let isReachable = bellmanFord ( G , G . V [ 's' ] ) ;
1211
1211
console . log ( '\nSingle-source shortest path:\n' ) ;
1212
+ console . log ( 'v | v.pi | v.d ' ) ;
1213
+ console . log ( '---|------|-----' ) ;
1214
+ for ( let v in G . V ) {
1215
+ ( v !== 's' )
1216
+ ? console . log ( `${ G . V [ v ] . key } ${ G . V [ v ] . pi . key } ${ G . V [ v ] . d } ` )
1217
+ : console . log ( `${ G . V [ v ] . key } ${ ' ' } ${ G . V [ v ] . d } ` ) ;
1218
+ }
1219
+ console . log ( ) ;
1212
1220
console . log (
1213
1221
` t,${ G . V [ 't' ] . d } ${ arrows . l } -2${ arrows . l } x,${ G . V [ 'x' ] . d } `
1214
1222
+ `\n ${ arrows . dr } ${ arrows . ur } `
@@ -1247,6 +1255,14 @@ console.log(
1247
1255
1248
1256
dijkstra ( G , G . V [ 's' ] ) ;
1249
1257
console . log ( '\nSingle-source shortest path:\n' ) ;
1258
+ console . log ( 'v | v.pi | v.d ' ) ;
1259
+ console . log ( '---|------|-----' ) ;
1260
+ for ( let v in G . V ) {
1261
+ ( v !== 's' )
1262
+ ? console . log ( `${ G . V [ v ] . key } ${ G . V [ v ] . pi . key } ${ G . V [ v ] . d } ` )
1263
+ : console . log ( `${ G . V [ v ] . key } ${ ' ' } ${ G . V [ v ] . d } ` ) ;
1264
+ }
1265
+ console . log ( ) ;
1250
1266
console . log (
1251
1267
` t,${ G . V [ 't' ] . d } ${ arrows . r } 1${ arrows . r } x,${ G . V [ 'x' ] . d } `
1252
1268
+ `\n ${ arrows . u } `
0 commit comments