mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-04-27 19:52:14 +00:00
24 lines
743 B
JavaScript
24 lines
743 B
JavaScript
async function findShortestPath(graph, start, end) {
|
|
const { default: PriorityQueue } = await import('https://esm.sh/js-priority-queue');
|
|
const dist = {};
|
|
for (const n in graph) dist[n] = Infinity;
|
|
dist[start] = 0;
|
|
const pq = new PriorityQueue({ comparator: (a, b) => a[0] - b[0] });
|
|
pq.queue([0, start]);
|
|
while (pq.length) {
|
|
const [d, node] = pq.dequeue();
|
|
if (node === end) return d;
|
|
if (d > dist[node]) continue;
|
|
for (const [next, w] of Object.entries(graph[node] || {})) {
|
|
const nd = d + w;
|
|
if (nd < (dist[next] ?? Infinity)) {
|
|
dist[next] = nd;
|
|
pq.queue([nd, next]);
|
|
}
|
|
}
|
|
}
|
|
return Infinity;
|
|
}
|
|
export default findShortestPath;
|
|
// Generation time: 4.505s
|
|
// Result: PASS
|