mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-04-06 20:32:14 +00:00
25 lines
941 B
JavaScript
25 lines
941 B
JavaScript
const findShortestPath = async (graph, start, end) => {
|
|
const mod = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/lib/priority-queue.js');
|
|
const PriorityQueue = mod.default ?? mod.PriorityQueue ?? mod;
|
|
const dist = {};
|
|
for (const v of Object.keys(graph)) dist[v] = Infinity;
|
|
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
|
|
pq.push({ node: start, dist: 0 });
|
|
dist[start] = 0;
|
|
while (pq.length) {
|
|
const { node, dist: d } = pq.pop();
|
|
if (node === end) return d;
|
|
if (d > dist[node]) continue;
|
|
for (const [n, w] of Object.entries(graph[node] ?? {})) {
|
|
const nd = d + w;
|
|
if (nd < (dist[n] ?? Infinity)) {
|
|
dist[n] = nd;
|
|
pq.push({ node: n, dist: nd });
|
|
}
|
|
}
|
|
}
|
|
return Infinity;
|
|
};
|
|
export default findShortestPath;
|
|
// Generation time: 66.576s
|
|
// Result: FAIL
|