Files
lynchmark/tests/1_dijkstra/outputs/anthropic_claude-sonnet-4.5 TEMP_0.7.js
2025-11-07 22:07:45 +00:00

25 lines
730 B
JavaScript

async function findShortestPath(graph, start, end) {
const { default: PQ } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/+esm');
const dist = { [start]: 0 };
const pq = new PQ({ comparator: (a, b) => a[1] - b[1] });
pq.queue([start, 0]);
while (pq.length) {
const [node, d] = pq.dequeue();
if (node === end) return d;
if (d > (dist[node] ?? Infinity)) continue;
for (const [neighbor, weight] of Object.entries(graph[node] || {})) {
const newDist = d + weight;
if (newDist < (dist[neighbor] ?? Infinity)) {
dist[neighbor] = newDist;
pq.queue([neighbor, newDist]);
}
}
}
return Infinity;
}
export default findShortestPath;