1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater than or equal to the key of its parent (if there is a parent). - Fibonacci heaps is a collection of trees with the heap property; these trees are notnecessarily binomial). -Tree roots are connected in a circular doubly linked list. -The FH is accessible by the min pointer (H) -The order of the trees is arbitrary
2 Theorem. Starting from empty Fibonacci heap, any sequence of a 1 insert, a 2 delete-min, and a 3 decrease-key operations takes O(a 1 + a 2 log n + a 3 ) time. make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary Heap log n 1 n 1 Binomial Heap log n 1 Fibonacci Heap † 1 1 log n Relaxed Heap 1 1 log n Linked List 1 n n 1 n n is-empty11111 Priority Queues Performance Cost Summary † amortizedn = number of elements in priority queue
3 Hopeless challenge. O(1) insert, delete-min and decrease-key. Why? Priority Queues Performance Cost Summary make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary Heap log n 1 n 1 Binomial Heap log n 1 Fibonacci Heap † 1 1 log n Relaxed Heap 1 1 log n Linked List 1 n n 1 n n is-empty11111 † amortizedn = number of elements in priority queue
4 Fibonacci Heaps History. [Fredman and Tarjan, 1986] n Ingenious data structure and analysis. Original motivation: improve Dijkstra's shortest path algorithm from O(E log V ) to O(E + V log V ). Basic idea. n Similar to binomial heaps, but less rigid structure. Binomial heap: eagerly consolidate trees after each insert. Fibonacci heap: lazily defer consolidation until next delete-min. V insert, V delete-min, E decrease-key
Heap H Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. roots heap-ordered tree each parent larger than its children
Heap H Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. min find-min takes O(1) time
Heap H Fibonacci Heaps: Structure Fibonacci heap. n Set of heap-ordered trees. n Maintain pointer to minimum element. n Set of marked nodes. min use to keep heaps flat (stay tuned) marked
8 Fibonacci Heaps: Notation Notation. n = number of nodes in heap. rank(x) = number of children of node x. rank(H) = max rank of any node in heap H. trees(H) = number of trees in heap H. marks(H) = number of marked nodes in heap H rank = 3 min Heap H trees(H) = 5 marks(H) = 3 marked n = 14
9 Fibonacci Heaps: Potential Function (H) = 3 = min Heap H (H) = trees(H) + 2 marks(H) potential of heap H trees(H) = 5 marks(H) = 3 marked
10 Insert
11 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to root list; update min pointer (if necessary) insert 21 min Heap H
12 Fibonacci Heaps: Insert Insert. n Create a new singleton tree. n Add to root list; update min pointer (if necessary) min Heap H insert 21
13 Fibonacci Heaps: Insert Analysis Actual cost. O(1) Change in potential. +1 Amortized cost. O(1) min Heap H (H) = trees(H) + 2 marks(H) potential of heap H
14 Delete Min
15 Linking Operation Linking operation. Make larger root be a child of smaller root tree T 1 tree T tree T' smaller root larger rootstill heap-ordered
16 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min
17 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min
18 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current
19 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank current min rank
20 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank
21 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank
22 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank link 23 into 17
23 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank link 17 into 7
24 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank current min rank link 24 into 7
25 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank
26 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank
27 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank
28 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank link 41 into 18
29 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min current rank
30 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min rank current
31 Fibonacci Heaps: Delete Min Delete min. n Delete min; meld its children into root list; update min. n Consolidate trees so that no two roots have same rank min stop
32 Fibonacci Heaps: Delete Min Analysis Delete min. Actual cost. O(rank(H)) + O(trees(H)) O(rank(H)) to meld min's children into root list. O(rank(H)) + O(trees(H)) to update min. O(rank(H)) + O(trees(H)) to consolidate trees. Change in potential. O(rank(H)) - trees(H) trees(H' ) rank(H) + 1 since no two trees have same rank. Amortized cost. O(rank(H)) (H) = trees(H) + 2 marks(H) potential function
33 Decrease Key
34 Intuition for decreasing the key of node x. If heap-order is not violated, just decrease the key of x. Otherwise, cut tree rooted at x and meld into root list. n To keep trees flat: as soon as a node has its second child cut, cut it off and meld into root list (and unmark it) Fibonacci Heaps: Decrease Key 35 min marked node: one child already cut
35 Case 1. [heap order not violated] Decrease key of x. n Change heap min pointer (if necessary) Fibonacci Heaps: Decrease Key min x decrease-key of x from 46 to 29
36 Case 1. [heap order not violated] Decrease key of x. n Change heap min pointer (if necessary) Fibonacci Heaps: Decrease Key 35 min x decrease-key of x from 46 to 29
37 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key min decrease-key of x from 29 to 15 p x
38 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p x
39 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p x
40 Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 35 min decrease-key of x from 29 to 15 p x mark parent 24
41 35 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key 5 min x p decrease-key of x from 35 to 5
42 5 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key min x p decrease-key of x from 35 to 5
43 Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 x p min Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).
44 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 x p second child cut min
45 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 xp min
46 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 xp p' second child cut min
47 Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child) Fibonacci Heaps: Decrease Key decrease-key of x from 35 to 5 xpp' min don't mark parent if it's a root p''
48 Decrease-key. Actual cost. O(c) n O(1) time for changing the key. O(1) time for each of c cuts, plus melding into root list. Change in potential. O(1) - c trees(H') = trees(H) + c. marks(H') marks(H) - c + 2. Amortized cost. O(1) Fibonacci Heaps: Decrease Key Analysis (H) = trees(H) + 2 marks(H) potential function
49 Analysis
50 Union
51 Fibonacci Heaps: Union Union. Combine two Fibonacci heaps. Representation. Root lists are circular, doubly linked lists min Heap H'Heap H''
52 Fibonacci Heaps: Union Union. Combine two Fibonacci heaps. Representation. Root lists are circular, doubly linked lists min Heap H
53 Fibonacci Heaps: Union Actual cost. O(1) Change in potential. 0 Amortized cost. O(1) (H) = trees(H) + 2 marks(H) potential function min Heap H
54 Delete
55 Delete node x. decrease-key of x to - . delete-min element in heap. Amortized cost. O(rank(H)) O(1) amortized for decrease-key. O(rank(H)) amortized for delete-min. Fibonacci Heaps: Delete (H) = trees(H) + 2 marks(H) potential function
56 make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary Heap log n 1 n 1 Binomial Heap log n 1 Fibonacci Heap † 1 1 log n Relaxed Heap 1 1 log n Linked List 1 n n 1 n n is-empty11111 Priority Queues Performance Cost Summary † amortizedn = number of elements in priority queue