Skip to content

Commit 78555c8

Browse files
committed
Add js bellman-ford output
1 parent 0070c3b commit 78555c8

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

javascript/index.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,14 +1197,31 @@ G.addDirectedEdge(G.V['y'], G.V['x'], -3);
11971197
G.addDirectedEdge(G.V['y'], G.V['z'], 9);
11981198
G.addDirectedEdge(G.V['z'], G.V['x'], 7);
11991199
G.addDirectedEdge(G.V['z'], G.V['s'], 2);
1200-
// console.log(G);
1201-
let isReachable = bellmanFord(G, G.V['s']);
12021200

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+
);
12041210

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+
// }
12081225

12091226
// TO-DO: tweak for Dijkstra
12101227
// G.addDirectedEdge(new GraphVertexSSSP('s'), new GraphVertexSSSP('y'), 5); // cf. Figure 24.2

0 commit comments

Comments
 (0)