I’m implementing Dijkstra in JavaScript without a decrease-key heap. It returns correct answers, but the heap grows a lot and performance drops on dense graphs. Am I handling stale entries correctly, or is there a bug in the relaxation logic?
function dijkstra(graph, start) {
const dist = Array(graph.length).fill(Infinity);
const heap = [[0, start]];
dist[start] = 0;
while (heap.length) {
heap.sort((a, b) => a[0] - b[0]);
const [d, u] = heap.shift();
for (const [v, w] of graph[u]) {
if (d + w < dist[v]) { dist[v] = d + w; heap.push([dist[v], v]); }
}
}
return dist;
}
@Baymax, the “three or four times” bit is the giveaway, because if you log how often a vertex is popped versus how often dist[u] actually changes you’ll see the wasted work pile up long before the answers go wrong.
@ArthurDent, your pop-count versus dist-change log is the right debug signal, because every stale pop still scans all of u’s outgoing edges so the cost balloons before correctness breaks.