mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-13 16:17:54 +00:00
21 lines
681 B
JavaScript
21 lines
681 B
JavaScript
async function findShortestPath(graph, start, end) {
|
|
const { PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
|
|
const pq = new PriorityQueue({ comparator: (a, b) => a.d - b.d });
|
|
const dist = {};
|
|
dist[start] = 0;
|
|
pq.queue({ node: start, d: 0 });
|
|
while (!pq.isEmpty()) {
|
|
const { node, d } = pq.dequeue();
|
|
if (d > dist[node]) continue;
|
|
if (node === end) return d;
|
|
for (const [nei, w] of Object.entries(graph[node] || {})) {
|
|
const alt = d + w;
|
|
if (alt < (dist[nei] ?? Infinity)) {
|
|
dist[nei] = alt;
|
|
pq.queue({ node: nei, d: alt });
|
|
}
|
|
}
|
|
}
|
|
return Infinity;
|
|
}
|
|
export default findShortestPath; |