Fibonacci Heaps
Total Page:16
File Type:pdf, Size:1020Kb
Fibonacci Heaps Outline for Today ● Recap from Last Time ● Quick refresher on binomial heaps and lazy binomial heaps. ● The Need for decrease-key ● An important operation in many graph algorithms. ● Fibonacci Heaps ● A data structure efficiently supporting decrease- key. ● Representational Issues ● Some of the challenges in Fibonacci heaps. Recap from Last Time (Lazy) Binomial Heaps ● Last time, we covered the binomial heap and a variant called the lazy binomial heap. ● These are priority queue structures designed to support efficient melding. ● Elements are stored in a collection of binomial trees. 2 1 2 0 7 4 5 5 1 3 8 7 6 9 8 Eager Binomial Heap 1 9 5 2 3 7 6 4 8 Lazy Binomial Heap 1 2 3 4 5 6 7 8 9 Draw what happens if we enqueue the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9 into each heap. Eager Binomial Heap 2 5 9 3 7 6 4 8 Lazy Binomial Heap 2 5 9 3 7 6 4 8 Draw what happens after performing an extract-min in each binomial heap. Eager Binomial Heap 2 10 12 5 9 3 11 7 6 4 8 Lazy Binomial Heap 2 10 11 12 5 9 3 7 6 4 8 Let’s enqueue 10, 11, and 12 into both heaps. Eager Binomial Heap 3 9 5 4 10 12 7 6 11 8 Lazy Binomial Heap 3 9 5 4 10 12 7 6 11 8 Draw what happens after we do a extract-min from both heaps. Operation Costs ● Eager Binomial Heap: ● Lazy Binomial Heap: ● enqueue: O(log n) ● enqueue: O(1) ● meld: O(log n) ● meld: O(1) ● find-min: O(log n) ● find-min: O(1) ● extract-min: O(log n) ● extract-min: O(log n)* (Nothing to see here.) ● *amortized Intuition:Intuition: Each Each extract-min extract-min hashas to to do do a a bunch bunch of of cleanup cleanup forfor the the earlier earlier enqueue enqueue operations,operations, but but then then leaves leaves us us withwith few few trees. trees. New Stuff! The Need for decrease-key The decrease-key Operation ● Some priority queues support the operation decrease-key(v, k), which works as follows: Given a pointer to an element v, lower its key (priority) to k. It is assumed that k is less than the current priority of v. ● This operation is crucial in efficient implementations of Dijkstra's algorithm and Prim's MST algorithm. Dijkstra and decrease-key ● Dijkstra's algorithm can be implemented with a priority queue using ● O(n) total enqueues, ● O(n) total extract-mins, and ● O(m) total decrease-keys. 2 5? ∞? 5 1 start 0 4 10 1 10? ∞? 4 Dijkstra and decrease-key ● Dijkstra's algorithm can be implemented with a priority queue using ● O(n) total enqueues, ● O(n) total extract-mins, and ● O(m) total decrease-keys. ● Dijkstra's algorithm runtime is O(n Tenq + n Text + m Tdec) Prim and decrease-key ● Prim's algorithm can be implemented with a priority queue using ● O(n) total enqueues, ● O(n) total extract-mins, and ● O(m) total decrease-keys. 7 4 ∞? 1? 1 10 10? ∞? 4 3 9 9 6 8 ∞? 5? 5 2 2? ∞? Prim and decrease-key ● Prim's algorithm can be implemented with a priority queue using ● O(n) total enqueues, ● O(n) total extract-mins, and ● O(m) total decrease-keys. ● Prim's algorithm runtime is O(n Tenq + n Text + m Tdec) Standard Approaches ● In a binary heap, enqueue, extract-min, and decrease-key can be made to work in time O(log n) time each. ● Cost of Dijkstra's / Prim's algorithm: = O(n Tenq + n Text + m Tdec) = O(n log n + n log n + m log n) = O(m log n) Standard Approaches ● In a lazy binomial heap, enqueue takes amortized time O(1), and extract-min and decrease-key take amortized time O(log n). ● Cost of Dijkstra's / Prim's algorithm: = O(n Tenq + n Text + m Tdec) = O(n + n log n + m log n) = O(m log n) Where We're Going ● The Fibonacci heap has these amortized runtimes: ● enqueue: O(1) ● extract-min: O(log n). ● decrease-key: O(1). ● Cost of Prim's or Dijkstra's algorithm: = O(n Tenq + n Text + m Tdec) = O(n + n log n + m) = O(m + n log n) ● This is theoretically optimal for a comparison-based priority queue in Dijkstra's or Prim's algorithms. The Challenge of decrease-key 5 1 5 7 9 6 7 2 3 6 10 10 8 4 7 How might we implement decrease-key in a lazy binomial heap? 5 1 5 7 7 6 7 2 3 6 9 10 8 4 10 How might we implement decrease-key in a lazy binomial heap? IfIf our our lazy lazy binomialbinomial heap heap hashas n n nodes, nodes, how how talltall can can the the tallesttallest tree tree be? be? 5 1 5 7 7 6 7 2 3 6 9 10 8 4 SupposeSuppose the the biggest biggest k 10 tree has 2 knodes in it. tree has 2 nodes in it. k Then 2 k≤ n. Then 2 ≤ n. SoSo k k = = O(log O(log n n).). How might we implement decrease-key in a lazy binomial heap? IfIf our our lazy lazy binomialbinomial heap heap hashas n n nodes, nodes, how how talltall can can the the tallesttallest tree tree be? be? 5 1 5 7 7 6 7 2 3 6 9 10 8 4 SupposeSuppose the the biggest biggest k 10 tree has 2 knodes in it. tree has 2 nodes in it. k Then 2 k≤ n. Then 2 ≤ n. SoSo k k = = O(log O(log n n).). Challenge: Support decrease-key in (amortized) time O(1). 5 1 5 7 7 6 7 2 3 6 9 10 8 4 WeWe cannot cannot have have all all three three of of these these 3 nice properties at once: nice properties at once: 1.1. decrease-key decrease-key takes takes time time O(1). O(1). 2.2. Our Our trees trees are are heap-ordered. heap-ordered. 3.3. Our Our trees trees are are binomial binomial trees. trees. Challenge: Support decrease-key in (amortized) time O(1). 5 1 5 7 7 6 7 2 3 6 9 10 8 4 WeWe cannot cannot have have all all three three of of these these 3 nice properties at once: nice properties at once: 1.1. decrease-key decrease-key takes takes time time O(1). O(1). 2.2. Our Our trees trees are are heap-ordered. heap-ordered. 3.3. Our Our trees trees are are binomial binomial trees. trees. Challenge: Support decrease-key in (amortized) time O(1). 3 5 1 5 7 7 6 7 2 3 6 9 10 8 4 Challenge: Support decrease-key in (amortized) time O(1). 3 5 2 1 0 5 7 7 6 8 2 6 9 10 4 Problem: What do we do in an extract-min? 3 5 2 1 5 7 7 6 8 2 6 9 10 4 Order 2 Order 1 Order 0 5 3 9 10 11 12 7 6 4 8 What We Used to Do Problem: What do we do in an extract-min? 3 5 2 1 5 7 7 6 8 2 6 9 10 4 3 11 ThisThis system system assumes assumes we we can can assign an “order” to each tree. assign an “order” to each tree. That’s easy with binomial trees. 5 9 4 12 That’s easy with binomial trees. That’s harder with our new trees. That’s harder with our new trees. 7 6 10 WhatWhat should should we we do do here? here? 8 What We Used to Do Problem: What do we do in an extract-min? 3 5 2 1 5 7 7 6 8 2 6 9 10 4 Order 2 Order 0 Order 1 Order 0 Order 0 Order 0 5 9 3 10 11 12 7 6 4 Idea 1: A tree has order k Idea 1: Ak tree has order k if it has 2 knodes. if it has 2 nodes. 8 IdeaIdea 2: 2: A A tree tree has has order order k k ifif its its root root has has k k children. children. What We Used to Do Problem: What do we do in an extract-min? Order 0 Order 2 Order 1 Order 1 Order 1 Order 0 3 5 2 1 5 7 7 6 8 2 6 9 10 4 Idea 1: A tree has order k Idea 1: Ak tree has order k if it has 2 knodes. if it has 2 nodes. IdeaIdea 2: 2: A A tree tree has has order order k k ifif its its root root has has k k children. children. Problem: What do we do in an extract-min? Order 2 Order 1 Order 0 5 2 1 5 3 7 7 6 8 2 6 9 10 4 Problem: What do we do in an extract-min? Question:Question: How How efficientefficient is is this?this? 3 1 5 5 7 2 2 7 6 6 8 4 9 10 (1) To do a decrease-key, cut the node from its parent. (2) Do extract-min as usual, using child count as order. k 1. enqueue 2 k+ 1 nodes. 1. enqueue 2 + 1 nodes.