Dijkstra Shortest Path With Priority Queue
Compute weighted shortest paths from one source with a binary min-heap, reject negative edges and reconstruct the full node path.
Code
export type WeightedGraph = ReadonlyMap<string, ReadonlyArray<readonly [string, number]>>;
interface HeapEntry {
readonly node: string;
readonly distance: number;
}
export interface ShortestPaths {
readonly distances: ReadonlyMap<string, number>;
pathTo(target: string): string[];
}
export function shortestPaths(graph: WeightedGraph, source: string): ShortestPaths {
if (!(graph instanceof Map)) throw new TypeError("graph must be a Map");
if (!graph.has(source)) throw new RangeError(`unknown source "${source}"`);
const distances = new Map<string, number>([[source, 0]]);
const previous = new Map<string, string>();
const heap: HeapEntry[] = [{ node: source, distance: 0 }];
const settled = new Set<string>();
while (heap.length > 0) {
const current = pop(heap);
if (current === undefined) break;
if (settled.has(current.node)) continue;
settled.add(current.node);
const edges = graph.get(current.node);
if (edges === undefined) continue;
for (const [neighbor, weight] of edges) {
if (!Number.isFinite(weight) || weight < 0) {
throw new RangeError(`edge "${current.node}" -> "${neighbor}" has an invalid weight`);
}
const candidate = current.distance + weight;
const known = distances.get(neighbor);
if (known === undefined || candidate < known) {
distances.set(neighbor, candidate);
previous.set(neighbor, current.node);
push(heap, { node: neighbor, distance: candidate });
}
}
}
return {
distances,
pathTo(target: string): string[] {
if (!distances.has(target)) return [];
const path: string[] = [];
let cursor: string | undefined = target;
while (cursor !== undefined) {
path.push(cursor);
cursor = previous.get(cursor);
}
return path.reverse();
},
};
}
function push(heap: HeapEntry[], entry: HeapEntry): void {
let index = heap.length;
heap.push(entry);
while (index > 0) {
const parent = (index - 1) >> 1;
const parentEntry = heap[parent];
if (parentEntry === undefined || parentEntry.distance <= entry.distance) break;
heap[index] = parentEntry;
index = parent;
}
heap[index] = entry;
}
function pop(heap: HeapEntry[]): HeapEntry | undefined {
const first = heap[0];
const last = heap.pop();
if (first === undefined || last === undefined || heap.length === 0) return first;
let index = 0;
while (true) {
const left = index * 2 + 1;
const right = left + 1;
let smallest = index;
let smallestEntry = last;
const leftEntry = heap[left];
if (leftEntry !== undefined && leftEntry.distance < smallestEntry.distance) {
smallest = left;
smallestEntry = leftEntry;
}
const rightEntry = heap[right];
if (rightEntry !== undefined && rightEntry.distance < smallestEntry.distance) {
smallest = right;
smallestEntry = rightEntry;
}
if (smallest === index) break;
heap[index] = smallestEntry;
index = smallest;
}
heap[index] = last;
return first;
}
const graph: WeightedGraph = new Map([
["a", [["b", 2], ["c", 5]]],
["b", [["c", 1], ["d", 7]]],
["c", [["d", 2]]],
["d", []],
]);
const result = shortestPaths(graph, "a");
console.log([...result.distances.entries()]);
console.log(result.pathTo("d"));
console.log(result.pathTo("missing"));How the snippet works
Count the functions parent, shortestPaths, push and pop as the entire surface area of Dijkstra Shortest Path With Priority Queue.
The core work runs in nested loops — outer while (heap.length > 0), inner for (const [neighbor, weight] of edges). Failure is loud: TypeError and RangeError aborts the current operation instead of degrading it.
The input heap is mutated in place — callers that need the original must pass a copy. The return value is newly constructed — inputs are never handed back.
What to watch for
- The guard
current === undefinedshort-circuits the flow with an earlybreakwhen its condition holds. heapis mutated in place — the value you pass will not be the same afterward.
Applying it
The main signature reads export function shortestPaths(graph: WeightedGraph, source: string)
Callers only need shortestPaths — everything else stays internal.
Statements that matter
throw new RangeError(`edge "${current.node}" -> "${neighbor}" has an invalid weight`);
if (first === undefined || last === undefined || heap.length === 0) return first;
if (!graph.has(source)) throw new RangeError(`unknown source "${source}"`);
Interactive Tools for This Topic
FAQ
- Does `shortestPaths` modify its input?
- Yes — `heap` is mutated in place, and the return value is a new object rather than the mutated input. Pass a copy if the caller needs the original preserved.
- What does the snippet do with malformed data?
- It throws `TypeError` and `RangeError` before any real work runs, via the guard `current === undefined` — handle it at the call site or let it propagate deliberately.
- Does this require external packages?
- No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything. The file defines `parent`, `shortestPaths`, `push` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.