Skip to content

Commit e3cc716

Browse files
committed
Optimize performance and add proper types
1 parent ccb0223 commit e3cc716

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

src/index.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,22 @@ export function Graph(serialized?: Serialized) {
394394
function shortestPaths(source: NodeId, destination: NodeId) {
395395
let path = shortestPath(source, destination);
396396
const paths = [path],
397-
removedEdges: any = [],
397+
removedEdges: { u: NodeId; v: NodeId; weight: EdgeWeight }[] = [],
398398
weight = path.weight;
399399
while (weight) {
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-
});
400+
const u = path[0];
401+
const v = path[1];
402+
403+
if (hasEdge(u, v)) {
404+
removedEdges.push({ u, v, weight: getEdgeWeight(u, v) });
405+
removeEdge(u, v);
406+
}
407+
408+
if (hasEdge(v, u)) {
409+
removedEdges.push({ u: v, v: u, weight: getEdgeWeight(v, u) });
410+
removeEdge(v, u);
411+
}
412+
409413
try {
410414
path = shortestPath(source, destination);
411415
if (!path.weight || weight < path.weight) break;

0 commit comments

Comments
 (0)