CS711008Z Algorithm Design and Analysis Lecture 7. Binary heap, binomial heap, and Fibonacci heap Dongbo Bu
Texte intégral
(2) Outline. Introduction to priority queue Various implementations of priority queue: Linked list: a list having n items is too long to support efficient ExtractMin and Insert operations simultaneously; Binary heap: using a tree rather than a linked list; Binomial heap: allowing multiple trees rather than a single tree to support efficient Union operation Fibonacci heap: implement DecreaseKey via simply cutting an edge rather than exchanging nodes, and control a “bushy” tree shape via allowing at most one child losing for any node.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 2 / 124. ..
(3) Priority queue. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 3 / 124. ..
(4) Priority queue: motivation. Motivation: It is usually a case to extract the minimum from a set S of n numbers, dynamically. Here, the word “dynamically” means that on S, we might perform Insertion, Deletion and DecreaseKey operations. The question is how to organize the data to efficiently support these operations.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 4 / 124. ..
(5) Priority queue. Priority queue is an abstract data type similar to stack or queue, but each element has a priority associated with its name. A min-oriented priority queue must support the following core operations: 1 2. 3. 4 5. H=MakeHeap(): to create a new heap H; Insert(H, x): to insert into H an element x together with its priority ExtractMin(H): to extract the element with the highest priority; DecreaseKey(H, x, k): to decrease the priority of element x; Union(H1 , H2 ): return a new heap containing all elements of heaps H1 and H2 , and destroy the input heaps. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 5 / 124. ..
(6) Priority queue is very useful. Priority queue has extensive applications, such as: Dijkstra’s shortest path algorithm Prim’s MST algorithm Huffman coding A∗ searching algorithm HeapSort ....... .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 6 / 124. ..
(7) An example: Dijkstra’s algorithm. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 7 / 124. ..
(8) Dijkstra’s algorithm [1959] Dijkstra(G, s, t) 1: key(s) = 0; //key(u) stores an upper bound of the shortest distance from s to u; 2: PQ. Insert (s); 3: for all node v ̸= s do 4: key(v) = +∞ 5: PQ. Insert (v) //n times 6: end for 7: S = {}; // Let S be the set of explored nodes; 8: while S ̸= V do 9: v∗ = PQ. ExtractMin(); //n times 10: S = S ∪ {v∗ }; 11: for all v ∈ / S and < v∗ , v >∈ E do 12: if key(v∗ ) + d(v∗ , v) < key(v) then 13: PQ.DecreaseKey(v, key(v∗ ) + d(v∗ , v)); //m times 14: end if 15: end for 16: end while. Here PQ denotes a min-priority queue.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 8 / 124. ..
(9) Dijkstra’s algorithm: an example. s. 9. 24. a. 18. 14 15. 2. b 20. c. d. 30. 11. e 44. 19. f. 6. 16. t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 9 / 124. ..
(10) Initialization. S = {} PQ = {s(0), a(∞), b(∞), c(∞), d(∞), e(∞), f(∞), t(∞)} 0 s. 9. ∞ a. 18. 14 15. ∞ c. ∞ d. 24. ∞ b 20. 2 30. 19. ∞ ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 10 / 124. ..
(11) Step 1: ExtractMin. S = {} PQ = {s(0), a(∞), b(∞), c(∞), d(∞), e(∞), f(∞), t(∞)} 0 s. 9. ∞ a. ∞ c. ExtractMin returns s. 18. 14 15. ∞ d. 24. ∞ b 20. 2 30. 19. ∞ ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 11 / 124. ..
(12) Step 1: DecreaseKey. S = {s} PQ = {a(∞), b(∞), c(∞), d(∞), e(∞), f(∞), t(∞)} 0 s. 9. ∞ a. 18. 14 15. ∞ c. ∞ d. 24. ∞ b 20. 2 30. DecreaseKey(a, 9) DecreaseKey(b, 14) 19 DecreaseKey(c, 15). ∞ ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 12 / 124. ..
(13) Step 2: ExtractMin. S = {s} PQ = {a(9), b(14), c(15), d(∞), e(∞), f(∞), t(∞)} 0 s. 9. 9 a. 18. 14 15. 15 c. ∞ d. 24. 14 b 20. 2. 19. ∞ 30 ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. ..
(14) Step 2: ExtractMin. S = {s} PQ = {a(9), b(14), c(15), d(∞), e(∞), f(∞), t(∞)} 0 s. 9. 9 a. 15 c. ExtractMin returns a. 18. 14 15. ∞ d. 24. 14 b 20. 2. 19. ∞ 30 ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 13 / 124. ..
(15) Step 2: DecreaseKey S = {s, a} PQ = {b(14), c(15), d(∞), e(∞), f(∞), t(∞)} 0 s. 9. 9 a. 15 c. DecreaseKey(d, 33). 18. 14 15. ∞ d. 24. 14 b 20. 2 30. 19. ∞ ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 14 / 124. ..
(16) Step 3: ExtractMin S = {s, a} PQ = {b(14), c(15), d(33), e(∞), f(∞), t(∞)} 0 s. 9. 9 a. 18. 14 15. 15 c. 33 d. 24. 14 b 20. 2 30. 19. ∞ ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. ..
(17) Step 3: ExtractMin S = {s, a} PQ = {b(14), c(15), d(33), e(∞), f(∞), t(∞)} 0 s. 9. 9 a. 15 c. ExtractMin returns b. 18. 14 15. 33 d. 24. 14 b 20. 2 30. 19. ∞ ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 15 / 124. ..
(18) Step 3: DecreaseKey. S = {s, a, b} PQ = {c(15), d(33), e(∞), f(∞), t(∞)} 0 s. 9. 9 a. 15 c. DecreaseKey(d, 32) DecreaseKey(e, 44). 18. 14 15. 33 d. 24. 14 b 20. 2. 19. ∞ 30 ∞ 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 16 / 124. ..
(19) Step 4: ExtractMin. S = {s, a, b} PQ = {c(15), d(32), e(44), f(∞), t(∞)} 0 s. 9. 9 a. 18. 14 15. 15 c. 32 d. 24. 14 b 20. 2. 19. ∞ 30 44 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. ..
(20) Step 4: ExtractMin. S = {s, a, b} PQ = {c(15), d(32), e(44), f(∞), t(∞)} 0 s. 9. 9 a. 15 c. ExtractMin returns c. 18. 14 15. 32 d. 24. 14 b 20. 2. 19. ∞ 30 44 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 17 / 124. ..
(21) Step 4: DecreaseKey. S = {s, a, b, c} PQ = {d(32), e(44), f(∞), t(∞)} 0 s. 9. 9 a. 15 c. DecreaseKey(e, 35) DecreaseKey(t, 59). 18. 14 15. 32 d. 24. 14 b 20. 2. 19. ∞ 30 44 11 f 6 e 16 ∞ 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 18 / 124. ..
(22) Step 5: ExtractMin. S = {s, a, b, c} PQ = {d(32), e(35), t(59), f(∞)} 0 s. 9. 9 a. 18. 14 15. 15 c. 32 d. 24. 14 b 20. 2. 19. ∞ 30 35 11 f 6 e 16 59 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. ..
(23) Step 5: ExtractMin. S = {s, a, b, c} PQ = {d(32), e(35), t(59), f(∞)} 0 s. 9. 9 a. 15 c. ExtractMin returns d. 18. 14 15. 32 d. 24. 14 b 20. 2. 19. ∞ 30 35 11 f 6 e 16 59 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 19 / 124. ..
(24) Step 5: DecreaseKey. S = {s, a, b, c, d} PQ = {e(35), t(59), f(∞)} 0 s. 9. 9 a. 15 c. DecreaseKey(t, 51) DecreaseKey(e, 34). 18. 14 15. 32 d. 24. 14 b 20. 2. 19. ∞ 30 35 11 f 6 e 16 59 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 20 / 124. ..
(25) Step 6: ExtractMin. S = {s, a, b, c, d} PQ = {e(34), t(51), f(∞)} 0 s. 9. 9 a. 18. 14 15. 15 c. 32 d. 24. 14 b 20. 2. 19. ∞ 30 34 11 f 6 e 16 51 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. ..
(26) Step 6: ExtractMin. S = {s, a, b, c, d} PQ = {e(34), t(51), f(∞)} 0 s. 9. 9 a. 15 c. ExtractMin returns e. 18. 14 15. 32 d. 24. 14 b 20. 2. 19. ∞ 30 34 11 f 6 e 16 51 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 21 / 124. ..
(27) Step 6: DecreaseKey. S = {s, a, b, c, d, e} PQ = {f(45), t(50)} 0 s. 9. 9 a. 15 c. DecreaseKey(f, 45) DecreaseKey(t, 50). 18. 14 15. 32 d. 24. 14 b 20. 2. 19. ∞ 30 34 11 f 6 e 16 51 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 22 / 124. ..
(28) Step 7: ExtractMin. S = {s, a, b, c, d, e} PQ = {f(45), t(50)} 0 s. 9. 9 a. 18. 14 15. 15 c. 32 d. 24. 14 b 20. 2. 19. 45 30 34 11 f 6 e 16 50 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. ..
(29) Step 7: ExtractMin. S = {s, a, b, c, d, e} PQ = {f(45), t(50)} 0 s. 9. 9 a. 15 c. ExtractMin returns f. 18. 14 15. 32 d. 24. 14 b 20. 2. 19. 45 30 34 11 f 6 e 16 50 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 23 / 124. ..
(30) Step 7: DecreaseKey. S = {s, a, b, c, d, e, f} PQ = {t(50)} 0 s. 9. 9 a. 18. 14 15. 15 c. 32 d. 24. 14 b 20. 2. 19. 45 30 34 11 f 6 e 16 50 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 24 / 124. ..
(31) Step 8: ExtractMin. S = {s, a, b, c, d, e, f, t} PQ = {} 0 s. 9. 9 a. 18. 14 15. 15 c. 32 d. 24. 14 b 20. 2. 19. 45 30 34 11 f 6 e 16 50 44 t. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 25 / 124. ..
(32) Time complexity of Dijkstra algorithm. Operation MakeHeap Insert ExtractMin DecreaseKey Delete Union FindMin Dijkstra. Linked list 1 1 n 1 n 1 n O(n2 ). Binary heap 1 log n log n log n log n n 1 O(m log n). Binomial heap 1 log n log n log n log n log n log n O(m log n). Fibonacci heap 1 1 log n 1 log n 1 1 O(m + n log n). Dijkstra algorithm: n Insert, n ExtractMin, and m DecreaseKey. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 26 / 124. ..
(33) Implementing priority queue: array or linked list. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 27 / 124. ..
(34) Implementing priority queue: unsorted array. Unsorted array: 8 1 6 2 4 Unsorted linked list: head 8. 1. 6. 2. 4. Operations: Insert: O(1) ExtractMin: O(n). Note: a list containing n elements is too long to find the minimum efficiently.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 28 / 124. ..
(35) Implementing priority queue: sorted array. Sorted array: 1 2 4 6 8 Sorted linked list: head 1. 2. 4. 6. 8. Operations: Insert: O(n) ExtractMin: O(1). Note: a list containing n elements is too long to maintain the order among elements.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 29 / 124. ..
(36) Implementing priority queue: array or linked list. Operation. Linked List Insert O(1) ExtractMin O(n) DecreaseKey O(1) Union O(1). .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 30 / 124. ..
(37) Binary heap: from a linked list to a tree. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 31 / 124. ..
(38) Binary heap. Figure 1: R. W. Floyd [1964]. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 32 / 124. ..
(39) Binary heap: a complete binary tree Basic idea: loosing the structure: Recall that the objective is to find the minimum. To achieve this objective, it is not necessary to sort all elements. but don’t loose it too much: we still need order between some elements. 6. 10. 18. 12. 21. 8. 11. 25. 17. Binary heap: elements are stored in a complete binary tree, i.e., a tree that is perfectly balanced except for the bottom level. Heap order is required, i.e., any parent has a key smaller than his children; Advantage: any path has a short length of O(log2 n) rather than n in linked list, making it efficient to maintain heap .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. .. .. .. . .. .. .. .. .. 33 / 124. ..
(40) Binary heap: an explicit implementation Pointer representation: each node has pointers to its parent and two children; The following information are maintained: the number of elements n; the pointer to the root node;. root 6. 12. lptr. rptr. 10. 8. 18. 25. 11. 21. Note: the last node can be found in O(log n) time. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 34 / 124. ..
(41) Binary heap: an implicit implementation Array representation: one-one correspondence between a binary tree and an array. Binary tree ⇒ array:. the indices starting from 1 for the sake of simplicity; the indices record the order that the binary tree is traversed level by level.. Array ⇒ binary tree: the k-th item has two children located at 2k and 2k + 1; the parent of the k-th item is located at ⌊ 2k ⌋; 1 6 2. 3. 10. 8. 4. 5. 6. 7. 12. 18. 11. 25. ⇔. 6. 10. 8. 12. 18. 11. 25. 21. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 8 21 .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 35 / 124. ..
(42) Sorted array vs. binary heap. Sorted array: an array containing n elements in an increasing order; 6. 8. 10. 11. 12. 18. 21. 25. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. Binary heap: heap order means that only the order among nodes in short paths (length is less than log n) are maintained. Note that some inverse pairs exist in the array. 6. 10. 8. 12. 18. 11. 25. 21. 1. 2. 3. 4. 5. 6. 7. 8. 9. .. 10. .. .. .. .. 11. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 36 / 124. ..
(43) Binary heap: primitive and other operations. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 37 / 124. ..
(44) Primitive: exchanging nodes to restore heap order Primitive operation: when heap order is violated, i.e. a parent has a value larger than only one of its children, we simply exchange them to resolve the conflict. 6. 6. 10 15. 18. 21. 8. 13. 27. 11. 13. 25. 8. 18. 21. 15. 25. 11. 27. Figure 2: Heap order is violated: 15 > 13. Exchange them to resolve the conflict.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 38 / 124. ..
(45) Primitive: exchanging nodes to restore heap order Primitive operation: when heap order is violated, i.e. a parent has a value larger than both of its children, we exchange the parent with its smaller child to resolve the conflict. 6. 6. 10 20. 18. 12. 21. 8. 27. 11. 8. 12. 25. 20. 21. 18. 25. 11. 27. Figure 3: Heap order is violated: 20 > 12, and 20 > 18. Exchange 20 with its smaller child (12) to resolve the conflicts.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 39 / 124. ..
(46) Binary heap: Insert operation. Insert operation: the element is added as a new node at the end. Since the heap order might be violated, the node is repeatedly exchanged with its parent until heap order is restored. For example, Insert( 7 ): 6. 6. 10. 18. 12. 21. 8. 7. 11. 8. 7. 25. 10. 21. 18. 25. 11. 12. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 40 / 124. ..
(47) Binary heap: ExtractMin operation ExtractMin operation: exchange element in root with the last node; repeatedly exchange the element in root with its smaller child until heap order is restored. For example, ExtractMin(): 6. 21. 10. 12. 8. 18. 10. 25. 11. 8. 18. 12. 25. 11. 6. 21 8. 10. 12. 11. 18. 21. 25.. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 41 / 124. ..
(48) Binary heap: DecreaseKey operation. DecreaseKey operation: given a handle to a node, repeatedly exchange the node with its parent until heap order is restored. For example, DecreaseKey( ptr, 7 ): 6. 6. 10. 12. 21. 8. 18. ← ptr. 11. 8. 7. 25. 10. 18. 25. 11. 12. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 42 / 124. ..
(49) Binary heap: analysis Theorem In an implicit binary heap, any sequence of m Insert, . DecreaseKey, and ExtractMin operations with n Insert operations takes O(m log n) time. Note: Each operation touches at most log n nodes on a path from the root to a leaf. Theorem In an explicit binary heap with n nodes, the Insert, . DecreaseKey, and ExtractMin operations take O(m log n) time in the worst case. Note: If using array representation, a dynamic array expanding/contracting is needed. However, the total cost of array expanding/contracting is O(n) (see TableInsert). .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. .. . .. . .. .. .. .. .. 43 / 124. ..
(50) Binary heap: heapify a set of items Question: Given a set of n elements, how to construct a binary heap containing them? Solutions: 1. 2. Simply Insert the elements one by one. Takes O(n log n) time. Bottom-up heapifying. Takes O(n) time. For i = n to 1, we repeatedly exchange the element in node i with its smaller child until the subtree rooted at node i is heap-ordered. 1 21 2. 3. 10. 11. 21 10 11 25 1. 4. 5. 6. 7. 25. 8. 18. 12. 2. 3. 4. 8. 18 12. 5. 6. 7. 6 8. 9. 10 11. 8 6. (see a demo). .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 44 / 124. ..
(51) Binary heap: heapify Theorem Given n elements, a binary heap can be constructed using O(n) time. Proof. n There are at most ⌈ 2h+1 ⌉ nodes of height h; It takes O(h) time to sink a node of height h; The total time is: ⌊log2 n⌋. ∑ h=0. ⌈. n 2h+1. ⌊log2 n⌋. ⌉h ≤. ∑. n. h 2h. .. .. .. .. .. h=0. ≤ 2n. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 45 / 124. ..
(52) Implementing priority queue: binary heap. Operation. Linked List Insert O(1) ExtractMin O(n) DecreaseKey O(1) Union O(1). Binary Heap O(log n) O(log n) O(log n) O(n). .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 46 / 124. ..
(53) Binary heap: Union operation. Union operation: Given two binary heaps H1 and H2 , to merge them into one binary heap.. 25. H1. H2. 21. 6. 27. 8. 11. 29. O(n) time is needed if using heapify. Question: Is there a quicker way to union two heaps?. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 47 / 124. ..
(54) Binomial heap: using multiple trees rather than a single tree to support efficient Union operation. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 48 / 124. ..
(55) Binomial heap. Figure 4: Jean Vuillenmin [1978]. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 49 / 124. ..
(56) Binomial heap: efficient Union Basic idea: loosing the structure: if multiple trees are allowed to represent a heap, Union can be efficiently implemented via simply putting trees together. but don’t loose it too much: there should not be too many trees; otherwise, it will take a long time to find the minimum among all root nodes.. 25. H1. H2. 21. 6. 27. 8. 11. 29. ExtractMin: simply finding the minimum element of the root nodes. Note that a root node holds the minimum of the tree due to the heap order. .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. .. .. .. . .. .. .. .. .. 50 / 124. ..
(57) Why we can’t loose the structure too much? An extreme case of multiple trees: each node is itself a tree. Then it will take O(n) time to find the minimum. 6. 11. 8. 29. Solution: consolidating, i.e., two trees (with the same size) are merged into one — the larger root is linked to the smaller one to keep the heap order. Note that after consolidating, at most log n trees will be left. 6. 11. link. 6. 11. 6. 8. 11. 9. link. 6. 8. 11. .. .. .. .. .. 9. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 51 / 124. ..
(58) Binomial tree. Definition (Binomial tree) The binomial tree is defined recursively: a single node is itself a B0 tree, and two Bk trees are linked into a Bk+1 tree. B0. Bk+1 Bk Bk. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 52 / 124. ..
(59) Binomial tree examples: B0 , B1 , B2 B0 6. B1 6. 8. B2 6. 8. 9. 10 .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 53 / 124. ..
(60) Binomial tree example: B3. B3 6. 8. 9. 7. 10. 11. 12. 14. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 54 / 124. ..
(61) Binomial tree example: B4. B4 0. 8. 9. 7. 10. 11. 5. 12. 14. 11. 12. 13. 14. 15. 16. 17. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 55 / 124. ..
(62) Binomial tree example: B5. B5 0. 8. 9. 7. 10. 11. 5. 6. B4 12. 14. 11. 12. 13. 14. 15. 16. 17. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 56 / 124. ..
(63) Binomial tree: property Properties: 1 |B | = 2k . k 2 height(B ) = k. k 3 degree(B ) = k. k 4 The i-th child of a node has a degree of i − 1. 5 The deletion of the root yields trees B , B , ..., B 0 1 k−1 . 6 Binomial tree is named after the fact that the node number of all levels are binomial coefficients. B3 Bk+1. 6. 8. 9. 7. ...... 10. 11. 12. B0. B1. B2. 14 .. .. .. .. .. .. . . . . . . . .. Bk . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 57 / 124. ..
(64) Binomial heap: a forest Definition (Binomial forest) A binomial heap is a collection of several binomial trees: Each tree is heap ordered; There is either 0 or 1 Bk for any k. Example: B0. B1. B2. B3. 3. 4. 5. 6. 8. 8. 9. 8. 10. 9. 7. 10. 11. 12. 14. Note that the roots are organized using doubly-linked circular list, and the minimum of them is recorded using a pointer. .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. .. .. .. . .. .. .. .. .. 58 / 124. ..
(65) Binomial heap: properties Properties: 1 A binomial heap with n nodes contains the binomial tree B iff i bi = 1, where bk bk−1 ...b1 b0 is binary representation of n. 2 It has at most ⌊log n⌋ + 1 trees. 2 3 Its height is at most ⌊log n⌋. 2 Thus, it takes O(log n) time to find the minimum element via checking the roots. B0. B1. B2. B3. 3. 4. 5. 6. 8. 8. 9. 10. 8. 9. 7. 10. 11. 12. 14 .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 59 / 124. ..
(66) Union is efficient: example 1 B0. B2. B1. B3. 3. 5. 4. 6. 8. 8. + 8. 9. 10. =. B0. B1. B2. B3. 3. 4. 5. 6. 8. 8. 9. 10. 8. 9. 7. 10. 11. 12. 14. 9. 7. 10. 11. 12. 14 . . . . . . Figure 5: An easy case: no consolidating is.. needed . . . . . . . . . .. .. . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 60 / 124. ..
(67) Union is efficient: example 2 I B1. B2. 3. 5. B1. B3. 4. 6. 8. 8. + 7. 8. 9. 10. =. B2. B2. B3. 3. 5. 6. 7. 4. 8. 8. 9. 10. 8. 9. 7. 10. 11. 12. 14. 9. 7. 10. 11. 12. 14. Figure 6: Consolidating two B1 trees into a B2 tree .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 61 / 124. ..
(68) Union is efficient: example 2 II B1. B2. B1. B3. 3. 5. 4. 6. 7. 8. 8. 8. + 9. 10. =. B3. B3. 3. 6. 7. 4. 5. 8. 8. 8. 9. 9. 7. 10. 11. 12. 14. 9. 7. 10. 11. 12. 10. 14. Figure 7: Consolidating two B2 trees into a B3 tree. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 62 / 124. ..
(69) Union is efficient: example 2 III B1 B2 3. B1. B3. 4. 6. 8. 8. 5. + 7. 8. 9. 10. B4. 9. 7. 10. 11. =3 7. 12. 14. 4. 5. 8. 8. 6. 9. 10. 8. 9. 7. 10. 11. 12. 14. Time complexity: O(log n) since there are at most O(log n) trees. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 63 / 124. ..
(70) Binomial heap: Insert operation. Insert(x) 1: Create a B0 tree for x; 2: Change the pointer to the minimum root node if necessary; 3: while there are two Bk trees for some k do 4: Link them together into one Bk+1 tree; 5: Change the pointer to the minimum root node if necessary; 6: end while. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 64 / 124. ..
(71) Insert operation: an example. B0. B4. 9. 2. 3. 4. 5. 8. 8. 6. 9. 8. 10. 9. 7. 10. 11. 12. 14. Figure 8: An easy case: no consolidating is needed. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 65 / 124. ..
(72) Insert operation: example 2 I. B0. B0. B1. B2. B3. 2. 3. 4. 5. 6. 8. 8. 9. 8. 10. 9. 7. 10. 11. 12. 14. Figure 9: Consolidating two B0. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 66 / 124. ..
(73) Insert operation: example 2 II. B1. B1. B2. B3. 2. 4. 5. 6. 3. 8. 8. 9. 10. 8. 9. 7. 10. 11. 12. 14. Figure 10: Consolidating two B1. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 67 / 124. ..
(74) Insert operation: example 2 III. B2. B2. B3. 2. 5. 6. 3. 4. 8. 8. 9. 10. 8. 9. 7. 10. 11. 12. 14. Figure 11: Consolidating two B2. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 68 / 124. ..
(75) Insert operation: example 2 IV. B3. B3. 2. 6. 3. 4. 5. 8. 8. 8. 9. 9. 7. 10. 11. 12. 10. 14. Figure 12: Consolidating two B3. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 69 / 124. ..
(76) Insert operation: example 2 V B4 2. 3. 4. 5. 8. 8. 6. 9. 10. 8. 9. 7. 10. 11. 12. 14. Time complexity: O(log n) (worst case) since there are at most log n trees.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 70 / 124. ..
(77) Binomial heap: ExtractMin operation. ExtractMin() 1: Remove the min node, and insert its children into the root list; 2: Change the pointer to the minimum root node if necessary; 3: while there are two Bk trees for some k do 4: Link them together into one Bk+1 tree; 5: Change the pointer to the minimum root node if necessary; 6: end while. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 71 / 124. ..
(78) ExtractMin operation: an example I. B0 B4 9. 2. 3. 4. 5. 8. 8. 6. 9. 8. 10. 9. 7. 10. 11. 12. 14. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 72 / 124. ..
(79) ExtractMin operation: an example II. B0 B0 9. 3. B1. B2. B3. 4. 5. 6. 8. 8. 9. 8. 10. 9. 7. 10. 11. 12. 14. Figure 13: The four children become trees. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 73 / 124. ..
(80) ExtractMin operation: an example III. B1. B1. B2. B3. 3. 4. 5. 6. 9. 8. 8. 9. 10. 8. 9. 7. 10. 11. 12. 14. Figure 14: Consolidating two B1 trees. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 74 / 124. ..
(81) ExtractMin operation: an example IV. B2. B2. B3. 3. 5. 6. 9. 4. 8. 8. 9. 10. 8. 9. 7. 10. 11. 12. 14. Figure 15: Consolidating two B2 trees. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 75 / 124. ..
(82) ExtractMin operation: an example V. B3. B3. 3. 6. 9. 4. 5. 8. 8. 8. 9. 9. 7. 10. 11. 12. 10. 14. Figure 16: Consolidating two B2 trees. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 76 / 124. ..
(83) ExtractMin operation: an example VI B4 3. 9. 4. 5. 8. 8. 6. 9. 10. 8. 9. 7. 10. 11. 12. 14. Time complexity: O(log n). .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 77 / 124. ..
(84) Implementing priority queue: Binomial heap. Operation. Linked List Insert O(1) ExtractMin O(n) DecreaseKey O(1) Union O(1). Binary Heap O(log n) O(log n) O(log n) O(n). .. Binomial Heap O(log n) O(log n) O(log n) O(log n). .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 78 / 124. ..
(85) Binomial heap: more accurate analysis using the amortized technique. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 79 / 124. ..
(86) Amortized analysis of Insert Motivation: If an Insert takes a long time (say log n), the subsequent Insert operations shouldn’t take long!. Thus, it will be more accurate to examine a sequence of operations rather than each operation individually. .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . .. .. . .. .. .. .. .. 80 / 124. ..
(87) Amortized analysis of Insert operation Insert(x) 1: Create a B0 tree for x; 2: Change the pointer to the minimum root node if necessary; 3: while there are two Bk trees for some k do 4: Link them together into one Bk+1 tree; 5: Change the pointer to the minimum root node if necessary; 6: end while. Analysis: A single Insert operation takes time 1 + w, where w = #WHILE. For the sake of calculating the total running time of a sequence of operations, we represent the running time of a single operation as decrease of a potential function. Consider a quantity Φ = #trees (called potential function). The changes of Φ during an operation are: Φ increase: 1. Φ decrease: w. Thus the running time of Insert can be rewritten in terms of Φ as 1 + w = 1+ decrease in Φ. Note that this representation makes it . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . convenient to sum running time of a sequence of. .Insert operations. 81 / 124.
(88) Amortized analysis of ExtractMin ExtractMin() 1: Remove the min node, and insert its children to the root list; 2: Change the pointer to the minimum root node if necessary; 3: while there are two Bk trees for some k do 4: Link them together into one Bk+1 tree; 5: Change the pointer to the minimum root node if necessary; 6: end while. Analysis: A single ExtractMin operation takes d + w time, where d denotes degree of the removed root node, and w = #WHILE. For the sake of calculating the total running time of a sequence of operations, we represent the running time of a single operation as decrease of a potential function. Consider a potential function Φ = #trees. The changes during an operation are: Φ increase: d. Φ decrease: w. Similarly, the running time is rewritten in terms of Φ as d + w = d+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . decrease in #trees. Note that d ≤ log n. 82 / 124.
(89) Amortized analysis Let’s consider any sequence of n Insert and m ExtractMin operations. The total running time is at most n + m log n+ total decrease in #trees. Note: total decrease in #trees ≤ total increase in #trees (why?), which is at most n + m log n. Thus the total time is at most 2n + 2m log n. We say Insert takes O(1) amortized time, and ExtractMin takes O(log n) amortized time. Definition (Amortized time) For any sequence of n1 operation 1, n2 operation 2..., if the total time is O(n1 T1 + n2 T2 ...), we say that operation 1 takes T1 amortized time, operation 2 takes T2 amortized time .... .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 83 / 124. ..
(90) Intuition of the amortized analysis. The actual running time of an Insert operation is 1 + w. A large w means that the Insert operation takes a long time. Note that the w time was spent on “decreasing trees”; thus, if the w time was amortized over the operations “creating trees”, the “amortized time” of Insert operation will be only O(1). The actual running time of an ExtractMin operation is at most log n + w. Note that at most log n new trees are created during an ExtractMin operation; thus, the amortized time is still O(log n) even if some costs have been amortized to it from other operations due to “tree creating”.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 84 / 124. ..
(91) Implementing priority queue: Binomial heap. Operation. Linked List Insert O(1) ExtractMin O(n) DecreaseKey O(1) Union O(1). Binary Heap O(log n) O(log n) O(log n) O(n). Binomial Heap O(log n) O(log n) O(log n) O(log n). Binomial Heap* O(1) O(log n) O(log n) O(1). *amortized cost. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 85 / 124. ..
(92) Binomial heap: DecreaseKey operation B4 0. 8. 9. 7. 10. 11. 5. 12. 14. 11. 12. 13. 14. 15. 16. 17. Figure 17: DecreaseKey: 17 to 1. Time: O(log n) since in the worst case, we need to perform node exchanging up to the root. Question: is there a quicker way for decrease key? .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. .. . .. . .. .. .. .. .. 86 / 124. ..
(93) Fibonacci heap: an efficient implementation of DecreaseKey via simply cutting an edge rather than exchanging nodes. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 87 / 124. ..
(94) Fibonacci heap. Figure 18: Robert Tarjan [1986]. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 88 / 124. ..
(95) Fibonacci heap: an efficient DecreaseKey operation Basic idea: loosing the structure: Binomial heap requires trees to be in perfect shape. Now we loose this restriction — when heap order is violated, a simple solution is to “cut off a node, and insert it into the root list”. but don’t loose it too much: the “cutting off” operation makes a tree not “binomial” any more; however, it should not deviate from a binomial tree too much. A technique to achieve this objective is allowing any non-root node to lose “at most one child”.. .. .. .. . . . .. . . . .. . . . .. . . . . . . . . . . . . . . . . Figure 19: Heap order is violated when DescreaseKey 13 to 2.. . .. . .. .. .. .. .. 89 / 124. ..
(96) Fibonacci heap: DescreaseKey DecreaseKey(v, x) 1: key(v) = x; 2: if heap order is violated then 3: u = v′ s parent; 4: Cut subtree rooted at node v, and insert it into the root list; 5: Change the pointer to the minimum root node if necessary; 6: while u is marked do 7: Cut subtree rooted at node u, and insert it into the root list; 8: Change the pointer to the minimum root node if necessary; 9: Unmark u; 10: u = u′ s parent; 11: end while 12: Mark u; 13: end if .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 90 / 124. ..
(97) DecreaseKey: an example I. 0. 8. 9. 7. 10. 11. 5. 12. 14. 18. 19. 13. 20. 15. 16. 17. Figure 20: A Fibonacci heap. To DecreaseKey: 19 to 3.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 91 / 124. ..
(98) DecreaseKey: an example II. 0. 8. 3. 9. 7. 10. 11. 5. 12. 20. 18. 13. 15. 14. 16. 17. Figure 21: After DecreaseKey: 19 to 3. To DecreaseKey: 15 to 2.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 92 / 124. ..
(99) DecreaseKey: an example III. 0. 8. 3. 9. 7. 10. 11. 5. 12. 2. 20. 18. 13. 16. 14. 17. Figure 22: After DecreaseKey: 15 to 2. To DecreaseKey: 12 to 8.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 93 / 124. ..
(100) DecreaseKey: an example IV. 0. 8. 3. 9. 7. 10. 11. 5. 8. 2. 20. 18. 13. 16. 14. 17. Figure 23: After DecreaseKey: 12 to 8. To DecreaseKey: 14 to 1.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 94 / 124. ..
(101) DecreaseKey: an example V. 1. 0. 8. 3. 9. 7. 10. 11. 5. 8. 2. 20. 18. 13. 16. 17. Figure 24: After DecreaseKey: 14 to 1. To DecreaseKey: 16 to 9.. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 95 / 124. ..
(102) DecreaseKey: an example VI. 1. 0. 8. 9. 7. 10. 11. 5. 3. 18. 20. 13. 2. 9. 17. 8. Figure 25: After DecreaseKey: 16 to 9. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 96 / 124. ..
(103) Fibonacci heap: Insert Insert(x) 1: Create a tree for x, and insert it into the root list; 2: Change the pointer to the minimum root node if necessary; Note: Being lazy! Consolidating trees when extracting minimum. 0. 1. 8. 1. 9. 7. 10. 11. 9. 7. 10. 11. 3. 18. 20. 13. 2. 9. 17. 8. 0. 8. 5. 5. 3. 18. 20. 13. 2. 9. 6. 17. 8. Figure 26: Insert(6): creating a new tree, and insert . . . it . . into . . . the . . . .root . . . list . .. .. .. . . . .. . . . .. . . . .. .. .. . .. .. .. .. .. 97 / 124. ..
(104) Fibonacci heap: ExtractMin. ExtractMin() 1: Remove the min node, and insert its children into the root list; 2: Change the pointer to the minimum root node if necessary; 3: while there are two roots u and v of the same degree do 4: Consolidate the two trees together; 5: Change the pointer to the minimum root node if necessary; 6: end while. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 98 / 124. ..
(105) ExtractMin: an example I. 1. 0. 8. 1. 8. 9. 7. 10. 11. 9. 7. 10. 11. 5. 3. 18. 20. 5. 3. 18. 20. 13. 2. 9. 17. 8. 8. 13. 2. 9. 17. Figure 27: ExtractMin: removing the min node, and adding 3 trees. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 99 / 124. ..
(106) ExtractMin: an example II 1. 9. 7. 8. 10. 11. 8. 5. 3. 18. 20. 13. 2. 9. 17. Figure 28: ExtractMin: after consolidating two trees rooted at node 1 and 8. 7. 1. 8. 9. 11. 8. 5. 3. 18. 20. 13. 2. 9. 17. 10. Figure 29: ExtractMin: after consolidating two trees rooted at node 1 and 9. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 100 / 124. ..
(107) ExtractMin: an example III 1. 8. 9. 7. 10. 11. 5. 3. 18. 20. 13. 2. 9. 17. 8. Figure 30: ExtractMin: after consolidating two trees rooted at node 1 and 7. 3. 1. 8. 9. 7. 10. 11. 20. 8. 13. 2. 5. 9. 17. 18. Figure 31: ExtractMin: after consolidating two trees rooted at node 3 and 5 . . . . . . . . . . . . . . . . .. .. .. . . . .. . . . .. . . . .. .. .. . .. .. .. .. .. 101 / 124. ..
(108) ExtractMin: an example IV 3. 1. 8. 9. 7. 10. 11. 20. 8. 5. 2. 9. 13. 17. 18. Figure 32: ExtractMin: after consolidating two trees rooted at node 2 and 13. 3. 1. 8. 9. 7. 10. 11. 20. 8. 2. 5. 13. 9. 18. 17. Figure 33: ExtractMin: after consolidating two trees rooted at node 2 and 9 . . . . . . . . . . . . . . . . .. .. .. . . . .. . . . .. . . . .. .. .. . .. .. .. .. .. 102 / 124. ..
(109) ExtractMin: an example V. 1. 8. 2. 5. 7. 10. 11. 13. 8. 9. 3. 17. 20. 9. 18. Figure 34: ExtractMin: after consolidating two trees rooted at node 2 and 3. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 103 / 124. ..
(110) ExtractMin: an example VI 1. 8. 5. 7. 10. 11. 2. 8. 13. 9. 3. 17. 20. 9. 18. Figure 35: ExtractMin: after consolidating two trees rooted at node 1 and 2. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 104 / 124. ..
(111) Fibonacci heap: an amortized analysis. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 105 / 124. ..
(112) Fibonacci heap: DecreaseKey DecreaseKey(v, x) 1: key(v) = x; 2: if heap order is violated then 3: u = v′ s parent; 4: Cut subtree rooted at node v, and insert it into the root list; 5: Change the pointer to the minimum root node if necessary; 6: while u is marked do 7: Cut subtree rooted at node u, and insert it into the root list; 8: Change the pointer to the minimum root node if necessary; 9: Unmark u; 10: u = u′ s parent; 11: end while 12: Mark u; 13: end if .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 106 / 124. ..
(113) DecreaseKey: analysis Analysis: The actual running time of a single operation is 1 + w, where w = #WHILE. To calculate the total running time of a sequence of operations, we represent the running time of a single operation as decrease of a potential function. Consider a potential function Φ = #trees + 2#marks. The changes of Φ during an operation are: Φ increase: 1 + 2 = 3. Φ decrease: (−1 + 2 ∗ 1) ∗ w = w.. Thus we can rewrite the running time in terms of Φ as 1 + w = 1 + Φ decrease. Intuition: a large w means that DecreaseKey takes a long time; however, if we can “amortize” w over other operations, a DecreaseKey operation takes only O(1) “amortized time”. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 107 / 124. ..
(114) Fibonacci heap: ExtractMin. ExtractMin() 1: Remove the min node, and insert its children into the root list; 2: Change the pointer to the minimum root node if necessary; 3: while there are two roots u and v of the same degree do 4: Consolidate the two trees together; 5: Change the pointer to the minimum root node if necessary; 6: end while. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 108 / 124. ..
(115) ExtractMin: analysis Analysis: The actual running time of a single operation is d + w, where d denotes degree of the removed node, and w = #WHILE. To calculate the total running time of a sequence of operations, we represent the running time of a single operation as decrease of a potential function. Consider a potential function Φ = #trees + 2#marks. The changes of Φ during an operation are: Φ increase: d. Φ decrease: w.. Thus the running time can be rewritten in terms of Φ as d + w = d+ decrease in Φ. Note: d ≤ dmax , where dmax denotes the maximum root node degree. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 109 / 124. ..
(116) Fibonacci heap: Insert Insert(x) 1: Create a tree for x, and insert it into the root list; 2: Change the pointer to the minimum root node if necessary; Analysis: The actual running time is 1, and the changes of Φ during this operation are: Φ increase: 1. Φ decrease: 0.. Note: Recall that a binomial heap consolidates trees in both Insert and ExtractMin operations. In contrast, the Fibonacci heap adopts the strategy of “being lazy” — tree consolidating is removed from Insert operation for the sake of efficiency, and there is no tree consolidating until an ExtractMin operation. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 110 / 124. ..
(117) Fibonacci heap: amortized analysis Consider any sequence of n Insert, m ExtractMin, and r DecreaseKey operations. The total running time is at most: n + mdmax + r+ total decrease in Φ. Note: total decrease in Φ ≤ total increase in Φ = n + mdmax + 3r. Thus the total running time is at most: n + mdmax + r + n + mdmax + 3r = 2n + 2mdmax + 4r. Thus Insert takes O(1) amortized time, DecreaseKey takes O(1) amortized time, and ExtractMin takes O(dmax ) amortized time. In fact, ExtractMin takes O(log n) amortized time since dmax can be upper-bounded by log n (why?). .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 111 / 124. ..
(118) Fibonacci heap: bounding dmax. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 112 / 124. ..
(119) Fibonacci heap: bounding dmax Recall that for a binomial tree having n nodes, the root degree d is exactly log2 n, i.e. d = log2 n. 6 8. 9. 7. 10. 11. 12 14. In contrast, a tree in a Fibonacci heap might have several subtrees cutting off, leading to d ≥ log2 n. 6 8. 9. 7 11. However, the “marking technique” guarantees that any node can lose at most one child, thus limiting the deviation from the original binomial tree, i.e. logϕ n ≥ d ≥ log2 n, where √ 1+ 5. .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. .. . .. . .. .. .. .. .. 113 / 124. ..
(120) Fibonacci heap: a property of node degree Recall that for a binomial tree, the i-th child of each node has a degree of exactly i − 1. 6 8. 9. 7. 10. 11. 12 14. For a tree in a Fibonacci heap , we will show that the i-th child of each node has degree ≥ i − 2. 6 8. 9. 7 11. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 114 / 124. ..
(121) Lemma For any node in a Fibonacci heap, the i-th child has a degree ≥ i − 2. 6. w. 8. 9. 7. u. 11. Proof. Suppose u is the current i-th child of w; If w is not a root node, it has at most 1 child lost; otherwise, it might have multiple children lost; Consider the time when u is linked to w. At that time, degree(w) ≥ i − 1, so degree(u) = degree(w) ≥ i − 1; Subsequently, degree(u) decreases by at most 1 (Otherwise, u will be cut off and no longer a child of w). Thus, degree(u) ≥ i − 2. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 115 / 124. ..
(122) The smallest tree with root degree k in a Fibonacci heap. Let Fk be the smallest tree with root degree of k, and for any node of Fk , the i-th child has degree ≥ i − 2; B1 6. F0 6. 8. Figure 36: |B1 | = 21 and |F0 | = 1 ≥ ϕ0. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 116 / 124. ..
(123) Example: B2 versus F1. Let Fk be the smallest tree with root degree of k, and for any node of Fk , the i-th child has degree ≥ i − 2; B2 F1. 6. 6 8. 9 8 10. Figure 37: |B2 | = 22 and |F1 | = 2 ≥ ϕ1. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 117 / 124. ..
(124) Example: B3 versus F2 Let Fk be the smallest tree with root degree of k, and for any node of Fk , the i-th child has degree ≥ i − 2; B3 6. F2 8. 9. 7. 10. 11. 6. 12. 8. 9. 14. Figure 38: |B3 | = 23 and |F2 | = 3 ≥ ϕ2. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 118 / 124. ..
(125) Example: B4 versus F3. B4. F3. 0. 6 8. 9. 7. 10. 11. 5 12 14. 11. 12. 13. 14. 15. 8. 9. 7. 16 11 17. Figure 39: |B4 | = 24 and |F3 | = 5 ≥ ϕ3. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 119 / 124. ..
(126) Example: B5 versus F4. B5. F4. 0. 0 8. 9. 7. 10. 11. 5. 6. B4 12 14. 11. 12. 13. 14. 15. 8. 9. 7. 5. 11. 11. 16 12. 17. Figure 40: |B5 | = 25 and |F4 | = 8 ≥ ϕ4. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 120 / 124. ..
(127) General case of trees in Fibonacci heap Recall that a binomial tree Bk+1 is a combination of two Bk trees. Bk+1 B0 Bk Bk In contrast, Fk+1 is the combination of an Fk tree and an Fk−1 tree. F0. Fk+1 Fk Fk−1. We will show that though Fk is smaller than Bk , the difference is not too much. In fact, |Fk | ≥ 1.618k . .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 121 / 124. ..
(128) Fibonacci numbers and Fibonacci heap Definition (Fibonacci numbers) The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.... It can be if k = 0 0 defined by the recursion relation: fk = 1 if k = 1 fk−1 + fk−2 if k ≥ 2 √. Recall that fk+2 ≥ ϕk , where ϕ = 1+2 5 = 1.618.... Note that |Fk | = fk+2 , say |F0 | = f2 = 1, |F1 | = f3 = 2, |F2 | = f4 = 3. Consider a Fibonacci heap H having n nodes. Let T denote a tree in H with root degree d. We have n ≥ |T| ≥ |Fd | = fd+2 ≥ ϕd . Thus d = O(logϕ n) = O(log n). So, dmax = O(log n). Therefore, ExtractMin operation takes O(log n) amortized time. .. .. .. .. .. .. . . . .. . . . .. . . . .. . . . .. . . . .. . . . .. . .. .. . .. .. .. .. .. 122 / 124. ..
(129) Implementing priority queue: Fibonacci heap. Operation. Linked List Insert O(1) ExtractMin O(n) DecreaseKey O(1) Union O(1). Binary Heap O(log n) O(log n) O(log n) O(n). Binomial Heap O(log n) O(log n) O(log n) O(log n). Binomial Heap* O(1) O(log n) O(log n) O(1). Fibonacci Heap* O(1) O(log n) O(1) O(1). *amortized cost. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 123 / 124. ..
(130) Time complexity of Dijkstra algorithm. Operation MakeHeap Insert ExtractMin DecreaseKey Delete Union FindMin Dijkstra. Linked list 1 1 n 1 n 1 n O(n2 ). Binary heap 1 log n log n log n log n n 1 O(m log n). Binomial heap 1 log n log n log n log n log n log n O(m log n). Fibonacci heap 1 1 log n 1 log n 1 1 O(m + n log n). Dijkstra algorithm: n Insert, n ExtractMin, and m DecreaseKey. .. .. .. .. .. .. . . . . . . . .. . . . . . . . .. . . . . . . . .. . .. .. . .. .. .. .. .. 124 / 124. ..
(131)
Documents relatifs
To extend our approximate solution of Section 3 to such phase-type distributions of time between arrivals, we note that in our level-by-level approach we are in
To extend our approximate solution of Section 3 to such phase-type distributions of time between arrivals, we note that in our level-by-level approach we are in
Such a regime appears for flows in a thin channel and for high flow rate : the flow occurs atop a nearly static heap whose angle is stabilized by the flowing layer at its top and
Figure 3(b) depicts a solution using a self-made priority queue based on a heap and a separate array for tracking the location of graph vertices in the heap (the location information
Part A: Methodology to develop the list of priority devices in the context of COVID-19 Considering the WHO COVID-19 clinical management guideline (8) and the rational use of PPE
Then, we present singleton abstraction, a new heap abstraction based on a given partition-based address abstraction, which preserves the refinement relationship of its
The instruction new n allocates n consecutive cells of memory in the heap and returns the address of the first one and free frees the memory cell in the heap at the given
Autre solution pour que la View communique avec le