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.