Sub topic 13
Decrease-Key, Lazy Deletion, and Fibonacci Heaps
This chapter assumes you have read the heap-based implementation and the running-time chapter. You should be comfortable with heapq, Big-O notation, and the idea that Dijkstra's algorithm pushes a vertex again whenever its tentative distance improves.
Two ways to handle an improved distance
When relaxation lowers dist[v], the priority queue has to reflect the new, smaller key. Implementations handle this in one of two ways.
- Decrease-key finds the existing entry for
vinside the heap and lowers its key in place. The heap never holds more than one entry per vertex, so its size stays at most V. - Lazy deletion leaves the old entry where it is and pushes a new
(nd, v)pair. Old entries become stale. When one is popped later, thevisitedcheck throws it away.
Python's heapq has no decrease-key operation. Supporting one means keeping a dictionary from each vertex to its index in the heap array and updating that dictionary on every swap. That code is easy to get wrong, which is the main reason most Python code uses lazy deletion.
Watching stale entries pile up
The version below adds counters to the lazy-deletion loop so you can see how much extra work the stale entries cause.
import heapq
def dijkstra_lazy_stats(graph, source):
dist = {source: 0}
heap = [(0, source)]
visited = set()
pushes, stale, peak = 1, 0, 1
while heap:
d, u = heapq.heappop(heap)
if u in visited:
stale += 1
continue
visited.add(u)
for v, w in graph.get(u, []):
nd = d + w
if v not in dist or nd < dist[v]:
dist[v] = nd
heapq.heappush(heap, (nd, v))
pushes += 1
peak = max(peak, len(heap))
return dist, pushes, stale, peak
graph = {
"A": [("B", 10), ("C", 1), ("D", 2)],
"C": [("B", 5)],
"D": [("B", 1)],
"B": [],
}
dist, pushes, stale, peak = dijkstra_lazy_stats(graph, "A")
print(dist) # {'A': 0, 'B': 3, 'C': 1, 'D': 2}
print(pushes, stale, peak) # 6 2 3B is pushed three times, with keys 10, 6, and 3. Only the entry with key 3 does useful work. The other two are popped later and thrown away. With decrease-key, B would have had a single entry whose key dropped twice.
Comparing the costs
| Priority queue | Decrease-key cost | Extract-min cost | Total time | Heap space |
|---|---|---|---|---|
| Binary heap, lazy deletion | O(log E) (a push) | O(log E) | O(E log V) | O(E) |
| Binary heap, decrease-key | O(log V) | O(log V) | O((V + E) log V) | O(V) |
| Fibonacci heap | O(1) amortized | O(log V) amortized | O(E + V log V) | O(V) |
Lazy deletion's O(E log E) equals O(E log V), because E ≤ V² gives log E ≤ 2 log V. In asymptotic terms, the two binary-heap styles cost the same. The real cost of lazy deletion is memory. On a dense graph where many edges improve distances, the heap can grow to hold about E entries.
Fibonacci heaps
A Fibonacci heap does each decrease-key in O(1) amortized time. It achieves this by cutting the node out and postponing the cleanup until the next extract-min. The total running time drops to O(E + V log V), which is better than a binary heap when E is much larger than V.
Choosing a strategy
- Default to lazy deletion with
heapq. The code is short and hard to get wrong. - Use an indexed heap with decrease-key when memory is tight and the graph is dense.
- Treat Fibonacci heaps mainly as a tool for proving bounds. Pairing heaps or radix heaps are more practical options when profiling shows the priority queue is the bottleneck.
Exercise
On a graph with V = 1,000 vertices and E = 200,000 edges, what is the largest number of entries a lazy-deletion heap might hold, and how does that compare with a decrease-key heap?
Show answer
Each successful relaxation pushes one entry, so the lazy heap can hold up to about E = 200,000 entries, plus the source. A decrease-key heap holds at most V = 1,000 entries. Both run in O(E log V) time, but the lazy version can use up to 200 times more heap memory.
Exercise
Why does lazy deletion stay correct even though the heap contains outdated keys for some vertices?
Show answer
A vertex's smallest key is always popped before its stale, larger keys. The first pop finalizes the vertex and adds it to visited. Every later pop of the same vertex fails the visited check and is skipped without relaxing any edges.