Files
lynchmark/tests/1_dijkstra/outputs/google_gemini-2.5-pro TEMP_0.7.js
2025-10-13 18:37:08 +00:00

27 lines
894 B
JavaScript

async function findShortestPath(graph, startNode, endNode) {
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
const distances = { [startNode]: 0 };
const pq = new PriorityQueue({ comparator: (a, b) => a.priority - b.priority });
pq.queue({ value: startNode, priority: 0 });
while (pq.length) {
const { value: currentNode } = pq.dequeue();
if (currentNode === endNode) break;
const neighbors = graph[currentNode] ?? {};
for (const [neighbor, weight] of Object.entries(neighbors)) {
const newDist = distances[currentNode] + weight;
if (newDist < (distances[neighbor] ?? Infinity)) {
distances[neighbor] = newDist;
pq.queue({ value: neighbor, priority: newDist });
}
}
}
return distances[endNode] ?? Infinity;
}
export default findShortestPath;