Skip to content

Commit ccb0223

Browse files
committed
Lint and add edgeWeight
1 parent 1e4d5d2 commit ccb0223

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

src/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export function Graph(serialized?: Serialized) {
178178
function depthFirstSearch(
179179
sourceNodes?: NodeId[],
180180
includeSourceNodes: boolean = true,
181-
errorOnCycle: boolean = false
181+
errorOnCycle: boolean = false,
182182
) {
183183
if (!sourceNodes) {
184184
sourceNodes = nodes();
@@ -284,7 +284,7 @@ export function Graph(serialized?: Serialized) {
284284
// Cormen et al. "Introduction to Algorithms" 3rd Ed. p. 613
285285
function topologicalSort(
286286
sourceNodes?: NodeId[],
287-
includeSourceNodes: boolean = true
287+
includeSourceNodes: boolean = true,
288288
) {
289289
return depthFirstSearch(sourceNodes, includeSourceNodes, true).reverse();
290290
}
@@ -394,17 +394,18 @@ export function Graph(serialized?: Serialized) {
394394
function shortestPaths(source: NodeId, destination: NodeId) {
395395
let path = shortestPath(source, destination);
396396
const paths = [path],
397-
removedEdges = [],
397+
removedEdges: any = [],
398398
weight = path.weight;
399399
while (weight) {
400-
if (hasEdge(path[0], path[1])) {
401-
removeEdge(path[0], path[1]);
402-
removedEdges.push([path[0], path[1]]);
403-
}
404-
if (hasEdge(path[1], path[0])) {
405-
removeEdge(path[1], path[0]);
406-
removedEdges.push([path[1], path[0]]);
407-
}
400+
[
401+
[path[0], path[1]],
402+
[path[1], path[0]],
403+
].forEach(([u, v]) => {
404+
if (hasEdge(u, v)) {
405+
removedEdges.push({ u, v, weight: getEdgeWeight(u, v) });
406+
removeEdge(u, v);
407+
}
408+
});
408409
try {
409410
path = shortestPath(source, destination);
410411
if (!path.weight || weight < path.weight) break;
@@ -413,7 +414,7 @@ export function Graph(serialized?: Serialized) {
413414
break;
414415
}
415416
}
416-
for (const [u, v] of removedEdges) addEdge(u, v);
417+
for (const { u, v, weight } of removedEdges) addEdge(u, v, weight);
417418
return paths;
418419
}
419420

test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ describe("Graph", function () {
378378
var graph = Graph().addEdge("a", "b").addEdge("b", "c");
379379
assert.deepEqual(
380380
graph.shortestPath("a", "c"),
381-
withWeight(["a", "b", "c"], 2)
381+
withWeight(["a", "b", "c"], 2),
382382
);
383383
});
384384

@@ -396,11 +396,11 @@ describe("Graph", function () {
396396

397397
assert.deepEqual(
398398
graph.shortestPath("s", "z"),
399-
withWeight(["s", "y", "z"], 5 + 2)
399+
withWeight(["s", "y", "z"], 5 + 2),
400400
);
401401
assert.deepEqual(
402402
graph.shortestPath("s", "x"),
403-
withWeight(["s", "y", "t", "x"], 5 + 3 + 1)
403+
withWeight(["s", "y", "t", "x"], 5 + 3 + 1),
404404
);
405405
});
406406

@@ -423,7 +423,7 @@ describe("Graph", function () {
423423
var graph = Graph().addEdge("a", "b").addEdge("b", "c").addEdge("d", "e");
424424
assert.deepEqual(
425425
graph.shortestPath("a", "c"),
426-
withWeight(["a", "b", "c"], 2)
426+
withWeight(["a", "b", "c"], 2),
427427
);
428428
});
429429

0 commit comments

Comments
 (0)