Week 14, Wednesday — Dijkstra’s
April 10, 2026
Given a start vertex (source) \(s\), find the path of least total cost from \(s\) to every vertex in the graph.

Dijkstra’s Algorithm (1959)
Note:

Given a source vertex \(s\), we wish to find the shortest path from \(s\) to every other vertex in the graph.
Initialize structure:
d:p:Repeat these steps:
Initialize structure:
v: d[v] = INF, p[v] = nulld[s] = 0d[v]Repeat n times:
d[] unlabelled vertex: vvw of v:
d[w])
d[w] = ________________p[w] = vReturn p

When a node becomes labeled, its shortest distance is final. It will never improve again.
d[] values only decrease, never increase.
The predecessors p[] form a shortest-path tree.
Execute Dijkstra’s algorithm on this graph:
Initialize structure:
v: d[v] = INF, p[v] = nulld[s] = 0d[v] ⭐Repeat n times:
d[] unlabelled vertex: v ⭐vw of v: 💜
wt[(v,w)] < d[w])
d[w] = wt[(v,w)] ⭐p[w] = vReturn p
⭐ \(n\) removeMins + \(\leq m\) decreaseKeys
💜 \(O(m)\) total (adj list) | \(O(n^2)\) total (adj matrix)
| Adj matrix | Adj list | |
|---|---|---|
| heap | \(O(n^2 + m \log n)\) | \(O(m \log n)\) |
| unsorted array | \(O(n^2)\) | \(O(n^2)\) |
Which is best? Depends on graph density:
Thanks for a fun year!