Dijkstra's Algorithm [00NL]
- September 28, 2026
The basic ideas behind Dijkstra's algorithm are first introduced via a POGIL activity.
Single-Source Shortest Paths [00NW]
- October 3, 2026
Dijkstra's Algorithm solves the single-source shortest path (SSSP) problem in a directed, weighted graph: given a vertex s, find the shortest paths from s to every other vertex in the graph.
Note that this seems like a harder problem than just finding the shortest path from s to some particular target vertex t, but it's really not. Intuitively, you can’t find the shortest path from s to t without exploring the rest of the graph, thus discovering shortest paths from s to other vertices along the way.
Dijkstra's Algorithm intuition [00NX]
- October 3, 2026
In an unweighted graph, we can use BFS to find shortest paths, but now we need to take edge weights into account. Intuitively, BFS searches outwards one layer at a time, by increasing distance. We’ll do the same thing: search outward by increasing distance. Imagine turning on a source of water at vertex s, and watching the water flood the whole graph. Each edge is a (directed) pipe, and the weight of an edge says how long the water takes to traverse that pipe. We can keep track of "where the water has reached" so far, and note at each step the next vertex reached by the water. Since the water floods in every direction possible, it is guaranteed that the time the water first reaches a vertex corresponds to the shortest path to that vertex from s.
Note that this only makes sense when the edge weights are positive—if water can “travel back in time” it invalidates this whole scheme; we can’t necessarily find the next vertex to be reached by the water with a greedy approach. If we want to find shortest paths when edge weights can be negative, we need a different algorithm (which we will study later).
Algorithm Dijkstra's Algorithm (basic version) [00NY]
- October 3, 2026
Here’s the basic algorithm, which we can think of as a generalization of BFS to weighted graphs. Recall that BFS keeps track of:
- which vertices have been visited so far,
- a layer map, giving the layer of each visited vertex, and
- a parent map recording the parent of each vertex in the BFS tree.
Theorem Correctness of Dijkstra's Algorithm [00NZ]
- October 3, 2026
Dijkstra’s algorithm correctly solves the SSSP problem for a weighted graph with nonnegative edge weights.
Proof.
- October 3, 2026
- At the start of the algorithm, s is the only visited vertex, and d[s] = 0 is correct since there are no negative-weight edges (and hence no better way to get from s to itself than just staying there).
- Now suppose the invariant holds and the loop executes one more time. Let u, v be the vertices picked by the algorithm. We will show that any other path from s to v must be at least as long as the path from s to v via u. Any other path from s to v must cross from a visited vertex to an unvisited vertex at some point, say it does this on the edge x \to y. But by the way u and v were chosen, we know that the shortest path from s to y via x is at least as long as the path from s to v (since by the invariant we know the shortest path from s to x), plus there may be extra distance from y to v as well. (Notice how the assumption of nonnegative edge weights is important here—if the distance from y to v could be negative it invalidates this proof!)
Hence, the loop updates the set of visited vertices and d appropriately and the invariant still holds.
Running time of Dijkstra's Algorithm (basic) [00O0]
- October 3, 2026
How long does the basic version of Dijkstra's Algorithm take? It depends a lot on how we choose vertices u and v on each iteration. A brute-force approach is to simply iterate over all edges (u,v), keep only those where u is visited and v unvisited, and remember the one with the smallest value of d[u] + w_{uv}. Since the while loop marks one vertex visited on each iteration, it executes |V| times, making the whole algorithm \Theta (VE) (which could be O(V^3) if the edges are dense). However, it turns out we can do better than this if we use a more clever scheme for quickly finding the best edge at each iteration. (Note that the best algorithms for solving the SSSP problem where negative weights are allowed run in \Theta (V^3) or \Theta (VE); we will learn about one such algorithm later in the semester.)
More efficient Dijkstra's algorithm [00O2]
- October 5, 2026
Can we do better than \Theta (VE)? The key, as usual, is to use some data structures to keep track of enough information so that we can pick u and v quickly without having to search through the whole graph every time.
Priority queue for Dijkstra [00O3]
- October 5, 2026
- We will expand d to keep track of not just the shortest distances to vertices, but the current shortest known distances to other vertices as well. So d[v] will always be an upper bound on the shortest distance from s to v. We may have to update d[v] every time we add a vertex u to S with an edge (u,v).
- We will expand the \mathit {parent} map similarly: \mathit {parent}[v] is the predecessor of v along the currently shortest known path from s.
The idea is that at each iteration we want to pick the unvisited vertex v with the smallest d[v]. We will store the unvisited vertices in some kind of data structure so that we can quickly remove the one with the smallest d[v]. We then need to be able to update d and \mathit {parent} appropriately.
So we need a data structure that allows us to:
- Add new items with a given value
- Remove the item with the smallest value
- Update the value of an existing item
Algorithm Dijkstra's Algorithm [00O4]
- October 5, 2026
Here is the updated version of Dijkstra's Algorithm using a priority queue for tracking unvisited vertices.
Remark Dijkstra in practice [00O5]
- October 7, 2026
Most standard libraries I am aware of do not have a priority queue implementation capable of doing a fast \mathit {updateKey} operation. One can simply remove an element and re-insert it, but this takes \Theta (n) since there is no way to find an element other than scanning over the entire heap. An implementation supporting a fast \mathit {updateKey} could, for example, keep a hash table on the side which keeps track of the location of each item within the heap. Given a sorted set data structure, it is also possible to keep a sorted set of (distance, vertex) pairs. Removing the vertex with the minimum distance is easy since it is just the first pair in the set; updating the distance of a vertex can be done by first deleting the (old distance, vertex) pair from the set (which can be done in O(\lg n) since the set is sorted) and then inserting (new distance, vertex).
Dijkstra runtime analysis [00O6]
- October 7, 2026
What’s the time complexity of this better implementation of Dijkstra's Algorithm? Again, we can’t just simplistically look at loops and so on. Instead, we count the total time taken by various operations. Let’s say our priority queue takes time T_{\mathit {rem}}(n) to do a remove-min operation on a priority queue of size n, and T_{\mathit {upd}}(n) to do an update-key operation.
- For any reasonable implementation of Q, creating it in the first place takes \Theta (V) time since we are inserting vertices with known keys all at once.
- We end up calling \mathit {removeMin} once for each vertex, and Q has size at most |V|, which contributes \Theta (V \cdot T_{\mathit {rem}}(V)) (the priority queue decreases each time, but half the calls are on a priority queue of size at least V/2, so we are still justified in saying it takes \Theta (V \cdot T_{\mathit {rem}}(V))).
- We also call \mathit {updateKey} at most once for each edge, which contributes O(E \cdot T_{\mathit {upd}}(V)).
- All other operations (e.g. setting values in d and \mathit {parent}) take \Theta (1), which adds \Theta (V + E) overall.
So in total, the algorithm takes \Theta (V \cdot T_{\mathit {rem}}(V) + E \cdot T_{\mathit {upd}}(V)). We’ll generally assume that |E| \geq |V| (otherwise the graph is either a tree, in which case we don’t need this algorithm, or there are degree-zero vertices which we can throw away). The total running time depends on the implementation of Q:
- We could simply use an array of vertices. Finding and removing the vertex with the smallest d value thus takes \Theta (n) (via linear search); updating the distance value also takes \Theta (n) since we have to do another linear search to find the required vertex. Substituting, this yields an overall time of \Theta (V \cdot V + E \cdot V) = \Theta (EV) (since we are assuming |E| \geq |V|).
- We could keep the array sorted by d value. Assuming we keep the vertex with the smallest d value at the end, removing it can be done in \Theta (1). To update the distance of a vertex requires a linear search to find the vertex and then linear time to insert it into its new sorted position. Thus overall this is \Theta (V \cdot 1 + E \cdot V) = \Theta (EV), no better than an unsorted array.
- We could use a dictionary (implemented as an array or hash table) mapping each vertex to its distance value. Updating is now \Theta (1), since we can just immediately access the value for a given vertex. However, finding and removing the vertex with minimum distance is \Theta (n) since we have to just scan through all the vertices to find the minimum. Overall, this takes \Theta (V \cdot V + E \cdot 1) = \Theta (V^2) (since E is O(V^2)).
- If we use a binary min-heap to store vertices by distance, we can remove the smallest-distance vertex in \Theta (\lg n). Updating a specific vertex is a bit tricker: we can keep some kind of hash table on the side so we know where to find a given vertex in the heap; then moving it up or down the heap to restore the heap properties after changing the distance value takes \Theta (\lg n). Hence, overall, Dijkstra with a binary heap takes \Theta (V \lg V + E \lg V) = \Theta (E \lg V).
- Finally, there is a data structure called a Fibonacci heap which allows updating in \Theta (1) and removing the minimum in \Theta (\lg n), in which case Dijkstra takes \Theta (V \lg V + E) overall. We won't learn more about Fibonacci heaps but you can read about them if you wish.
Technically, using a Fibonacci heap yields the fastest known implementation of Dijkstra's Algorithm, but Fibonacci heaps are complex to implement and rarely make a big difference in practice. \Theta (V \lg V + E) is better than \Theta (E \lg V) only if E is large relative to V, that is, if the graph is fairly dense. Using a binary heap tends to be the best all-around implementation.