Advanced algorithms binary heap, d-ary heap, binomial heap, amortized analysis, Fibonacci heap Jiří Vyskočil, Radek Mařík 2013 Heaps [haldy] heap a heap is a specialized data structure (usually tree-based) that satisfies the heap property: If B is a child node of A, then key(B) ≥ key(A). The heap is one the most efficient implementation of an abstract data type called a priority queue. The operations commonly performed with a heap are: Insert (x) adds a new key x to the heap. AccessMin finds and returns the minimum item of the heap. DeleteMin removes the minimum node of the heap (usually, the minimum node is the root of a heap). DecreaseKey (x,d) decreases x key within the heap by d. Merge (H , H ) 1 2 joins two heaps H and H to form a valid new heap containing all the elements of both. 1 2 Delete (x) removes a key x of a heap. Advanced algorithms 2 / 38 Binary Heap [binární halda] binary heap A binary heap is a binary tree with two additional constraints: It is a complete binary tree except the last level; that is, all levels of 1) the tree, except possibly the last one (deepest) are fully filled. If the last level of the tree is not complete, the nodes of that level are filled from left to right. Each node is less than or equal to each of its children according to 2) a comparison predicate ≤ over keys. Advanced algorithms 3 / 38 Binary Heap - Insert Insert (x) 1. Add a node x at the end of the heap; 2. while ( key(parent(x)†) > key(x) ) { 3. Swap a location of the node x with the node parent(x); 4. } †parent(x) returns the parent of a node x. It returns x in the case where x has no parent. 3 3 3 Advanced algorithms 4 / 38 Binary Heap AccessMin Returns the root of the heap’s binary tree. DeleteMin 1. &x = a location of the root of the heap; 2. key(x) = +∞; 3. &y = a location of the last node of the heap; 4. do { 5. Swap a location of the node x with a location of the node y; 6. &x = &y; 7. for each z ∈ descendants(x) do 8. if ( key(y) > key(z) ) then &y = &z; 9. } while ( &x ≠ &y ); 10. Remove the last node of the heap. DecreaseKey (x, d) First, decrease the key of x by d and then apply the similar algorithm as in Insert case. Advanced algorithms 5 / 38 Binary Heap - Delete Delete (&x) 1. key(x) = +∞; &y = a location of the last node of the heap; 2. do { 3. Swap a location of the node x with a location of the node y; 4. &x = &y; 5. for each z ∈ descendants(x) do 6. if ( key(y) > key(z) ) then &y = &z; 7. } while (&x ≠ &y ); 8. while ( key(parent(x)) > key(x) ) { 9. Swap a location of the node x with the node parent(x); 10. } 11. Remove the last node of the heap. +∞ Advanced algorithms 6 / 38 Binary Heap - Representation data representation bidirectional tree structure 2nd child (using pointers) (2*n)+1 or equivalently (n << 1) + 1 array (the root is placed at index 1) … parent node 1st child n div 2 n 2*n or equivalently n >> 1 or equivalently n << 1 Advanced algorithms 7 / 38 Binary Heap - BuildHeap BuildHeap ( array A ) length(𝐴) 1. for i = downto 1 do { 2 2. Heapify(A,i); 3. } Heapify ( array A, index i ) 1. min = i; 2. do { 3. left = 2∙i; 4. right = 2∙i + 1; 5. if (left ≤ length(A)) and (A[left] < A[min]) then min = left; 6. if (right ≤ length(A)) and (A[right] < A[min]) then min = right; 7. if min = i then break; 8. swap A[i] ↔ A[min]; 9. i = min; 10. } while true; Advanced algorithms 8 / 38 Binary Heap – Time Complexity Insert O(log(n)) Delete O(log(n)) AccessMin O(1) DeleteMin O(log(n)) DecreaseKey O(log(n)) BuildHeap log 𝑛 number of nodes at heigh ℎ ∙ Ο(ℎ) ≤ log 𝑛 𝑛 ∙ Ο(ℎ) ≤Ο(𝑛 ∙ ∞ ℎ ) = ℎ=0 ℎ=0 2ℎ+1 ℎ=02ℎ O(n) Merge O(n) by building a new heap. Advanced algorithms 9 / 38 d-ary heap [d-regulární halda] A d-ary heap is a generalization of the binary heap in which the nodes have dchildren instead of 2. Operations for d-ary heap are analogical to the operations for binary heap. Asymptotic time complexity of d-ary heap operations is the same as binary heap operations. Exact complexity differs because of a different logarithm base (the base is d). For Delete operation it is needed to check d instead of 2 descendants in every loop. For an efficient implementation it is convenient to choose d as powers of 2. In this case, bit shifts can be used for traversing the array representation. A d-ary heap typically runs much faster than a binary heap for heap sizes that exceed the size of the computer's cache memory. Advanced algorithms 10 / 38
Description: