mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 16:47:55 +00:00
31 lines
924 B
JavaScript
31 lines
924 B
JavaScript
async function findShortestPath(graph, start, end) {
|
|
const { default: PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
|
|
|
|
const distances = Object.keys(graph).reduce((acc, node) => ({ ...acc, [node]: Infinity }), {});
|
|
distances[start] = 0;
|
|
|
|
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
|
|
pq.queue({ node: start, dist: 0 });
|
|
|
|
const visited = new Set();
|
|
|
|
while (pq.length) {
|
|
const { node, dist } = pq.dequeue();
|
|
|
|
if (visited.has(node)) continue;
|
|
visited.add(node);
|
|
|
|
if (node === end) return dist;
|
|
|
|
for (const [neighbor, weight] of Object.entries(graph[node] || {})) {
|
|
const newDist = dist + weight;
|
|
if (newDist < distances[neighbor]) {
|
|
distances[neighbor] = newDist;
|
|
pq.queue({ node: neighbor, dist: newDist });
|
|
}
|
|
}
|
|
}
|
|
|
|
return distances[end];
|
|
}
|
|
export default findShortestPath; |