mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-06-07 20:12:13 +00:00
24 lines
783 B
JavaScript
24 lines
783 B
JavaScript
async function findShortestPath(graph, start, end) {
|
|
const { default: PriorityQueue } = await import("https://esm.sh/js-priority-queue");
|
|
const dist = {};
|
|
for (const node in graph) dist[node] = Infinity;
|
|
dist[start] = 0;
|
|
const pq = new PriorityQueue({ comparator: (a, b) => a.cost - b.cost });
|
|
pq.queue({ node: start, cost: 0 });
|
|
while (pq.length) {
|
|
const { node, cost } = pq.dequeue();
|
|
if (node === end) return cost;
|
|
if (cost > dist[node]) continue;
|
|
for (const next in graph[node]) {
|
|
const nc = cost + graph[node][next];
|
|
if (nc < dist[next]) {
|
|
dist[next] = nc;
|
|
pq.queue({ node: next, cost: nc });
|
|
}
|
|
}
|
|
}
|
|
return dist[end] ?? Infinity;
|
|
}
|
|
export default findShortestPath;
|
|
// Generation time: 5.506s
|
|
// Result: PASS
|