DDS-Heaps 1
Heaps - basics l Heaps an abstract structure where each object has a key value (the priority), and the operations are: insert an object, find the object of minimum key (find min), and delete the object of minimum key (delete min). l So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible. l Of course, we can replace everywhere the minimum by maximum. l The heap structure invented for a special application of sorting, when lower bound of Ω(n log n) on comparisonbased sorting of n objects. l Heaps have many other applications.
Balanced Search Trees as Heaps l Balanced search trees support the heap operations. It is a double-ended heap: find max, delete max, as well as all additional operations supported by the search tree: l Theorem. The heap structure can be realized using any balanced search tree with time O (log n) for insert and delete min and O (1) for find min operations. min Search Tree Used as Heap
Balanced Search Trees as Heaps l Balanced search-tree structure supports splitting, leaves arranged in a linked list, pointer current min to current minimum, invalid nodes structure that allows us to add an entire subtree: min invalid Search Tree Used as Heap with Lazy Deletion: Invalid Elements Are Already Deleted from Heap
Balanced Search Trees as Heaps l The array elements establish a key ordering called heap order: l The entries used by heap are beginning interval of array; if the heap currently contains n elements, it uses the array positions 0 to n 1. l For all I 0 heapkey[i] < heapkey[2i + 1] if 2i + 1 < n and heapkey[i] < heapkey[2i + 2] if 2i + 2 < n. heap[15] heap[16] heap[17] heap[7] heap[8] heap[9] heap[10] heap[11] heap[12] heap[13] heap[14] heap[3] heap[4] heap[5] heap[6] heap[1] heap[2] heap[0] Array-Based Heap with Order Relation among Array Elements
Balanced Search Trees Heaps: insert per insert operation. 34 44 5 34 44 20 10 20 30 15 11 19 24 33 10 5 30 15 11 19 24 33 9 14 6 8 9 14 6 8 7 4 7 4 3 3 34 44 20 34 44 20 10 9 30 15 11 19 24 33 10 9 30 15 11 19 24 33 5 14 6 8 7 14 6 8 7 4 5 4 3 Array-Based Heap: Insertion of New Element with Key 5. The New Element Is Placed in the Last Position and Moves Down 3
Balanced Search Trees Heaps: delete 34 44 21 10 20 30 15 24 33 11 19 9 14 6 8 7 4 34 44 10 20 30 15 24 33 11 19 9 14 6 8 7 4 21 34 44 10 20 30 15 24 33 11 19 9 14 6 8 7 21 34 44 10 20 30 15 24 33 11 19 9 14 21 8 7 6 4 Array-Based Heap: Deletion of Minimum Element, Classical Method. The Last Element Is Placed into the Gap and Moves Up 4
Heap-Ordered Trees and Half-Ordered Trees l Each node contains a key and two pointers to other nodes, which itself are roots of some subheaps. l But the key does not separate keys in subheaps; instead, it is smaller than either of them. l So we are free to choose either of them. This order condition is called a heap-ordered tree, and it is different from the search-tree order. l Define a heap-ordered tree recursively: the heapordered tree is either empty or contains in root node a key, an object, and two pointers, each of which might be either NULL or point to another heap-ordered tree in which all keys are larger than the key in the root node.
Heap-Ordered Trees 14 99 obj4 13 obj15 12 6 obj2 11 obj7 72 10 obj5 5 obj1 15 obj8 42 obj3 50 obj6 16 89 obj10 obj11 obj12 obj13 22 92 78 obj9 91 obj16 80 obj14 obj18 obj19 obj20 obj21 Heap-Ordered Tree 97 81 obj17
Heap-Ordered Trees generic insert 3 5 6 8 11 28 10 13 9 30 32 29 23 21 15 27 22 35 40 31 16 42 33 37 41 7 8 7 13 9 21 15 27 22 31 16 42 33 3 5 6 11 28 10 30 32 29 23 35 40 37 41 Heap-Ordered Tree: Simple Generic Insertion Method. The Entire Subtree at the Inserted Node Is Moved Down
Heap-Ordered Trees alternative insert + 3 5 6 8 11 28 10 13 9 30 32 29 23 21 15 27 22 35 40 31 16 42 33 37 41 7 31 3 5 6 7 11 28 10 8 9 30 32 29 23 13 15 27 22 35 40 21 16 42 33 37 41 Heap-Ordered Tree: Alternative Generic Insertion Method. The Elements along the Arbitrary Chosen Path Are Pushed Down
Heap-Ordered Trees generic delete 5 6 8 11 28 10 13 9 30 32 29 23 21 15 27 22 35 40 31 16 42 33 37 41 5 8 6 11 28 10 13 9 30 32 29 23 21 15 27 22 35 40 31 16 42 33 37 41 5 8 6 9 11 28 10 13 22 30 32 29 23 21 15 27 35 40 31 16 42 33 37 41 8 11 28 10 13 9 30 32 29 23 21 15 27 22 35 40 31 16 42 33 37 41 5 5 6 8 6 9 11 28 10 13 30 32 29 23 21 15 27 22 35 40 31 16 42 33 37 41 5 8 6 9 11 28 10 13 22 30 32 29 23 21 15 27 35 40 31 16 42 33 37 41 Heap-Ordered Tree: Generic Deletion Method. The Root Is Deleted, and the Hole Moves Down to a Leaf
Heap-Ordered Trees and Half-Ordered Trees l The rotations cannot be applied to heap order, because other node receives a new lower neighbor and that lower neighbor might violate the heap-order condition l An alternative with a weaker order condition is halfordered trees. These are the same trees as before, but we demand only that for each node, every key in its right subtree should be larger. For the left subtree, there is no condition. x x no >x >x restriction >x Order Conditions below a Node: Heap Order and Half Order
Heap-Ordered Trees and Half-Ordered Trees g f >f e >e d >d Leftmost Path in Half-Ordered Tree: Maximum Must Be on Leftmost Path by Implied Order Conditions c >c b >b a >a
Leftist Heaps l Leftist heaps are heap-ordered trees that use the distance to the nearest leaf, called rank, as balancing information. This is different from the height, which is the distance to the farthest leaf. Each node contains an additional field, the rank, which is defined by l { n->rank = 1 if n->left = NULL or n->right = NULL. l { n->rank = 1 + min (n->left->rank, n->right->rank) if l n->left à= NULL and n->right à= NULL. l If we have this additional rank field, we can also use it to identify the root of an empty heap by rank = 0.
Leftist Heaps l Leftist heap is characterized by the property that in each node shortest path on left side is at least as long as that on right side: l { n->left->rank n->right->rank if both are defined; and { if they are not both defined, then if one of them exists, it is the left one: + l n->left = NULL only if n->right = NULL. 3 2 2 3 1 1 1 2 2 1 1 1 2 1 1 1 1 1 1 1 Tree Underlying a Leftist Heap with Nodes Labeled by Rank
Leftist Heaps l For an insertion, follow rightmost path down to correct place for new node and insert node there, moving the rest of rightmost path to left below the new node. 2 3 2 3 1 1 2 3 2 3 4 2 6 2 5 2 7 1 8 1 10 1 1 1 4 2 6 2 5 2 7 1 8 1 10 1 4 2 6 2 5 2 7 1 8 1 10 1 3 1 4 2 3 1 5 2 7 1 6 2 14 1 15 1 11 1 14 1 15 1 11 1 14 1 15 1 11 1 14 1 15 1 8 1 10 1 11 1 2 3 2 3 2 3 2 3 4 2 6 2 5 2 7 1 8 1 10 1 9 1 4 2 6 2 5 2 7 1 8 1 9 1 4 2 6 2 5 2 7 1 8 1 10 1 12 1 4 2 6 2 5 2 7 1 10 2 8 1 14 1 15 1 11 1 14 1 15 1 10 1 14 1 15 1 11 1 14 1 15 1 11 1 12 1 11 1 Insertion in a Leftist Heap: Four Examples
Leftist Heaps l The key idea of leftist heaps is merging; it is easy to reduce the delete min to merging: l just delete the root and merge left and right subtrees. l For the merge, one just merges the right paths of both trees and then does the same cleanup as in the insert: l recomputing the rank and restoring leftist heap property by exchanging left and right pointers wherever necessary.
Leftist Heaps l Merge: 4 3 2 3 12 3 7 2 5 2 8 2 merge 30 2 15 2 9 1 10 1 13 1 6 1 14 2 11 1 40 1 33 1 16 1 28 1 20 1 29 1 42 1 19 1 41 1 17 1 2 2 3 5 2 13 1 6 1 12 3 4 7 rebalance 12 3 4 3 7 2 13 1 5 2 6 1 29 1 30 2 15 2 9 1 8 30 2 15 2 8 2 9 1 29 1 40 1 33 1 16 1 28 1 20 1 14 2 10 40 1 33 1 16 1 28 1 14 2 10 1 20 1 41 1 17 1 42 1 19 1 11 41 1 17 1 42 1 19 1 11 1 Merging Two Leftist Heaps: Merge the Right Paths and Recompute Ranks and Restore Leftist Property along Merged Path
Skew Heaps l The skew heaps were introduced as an analog of leftist heaps, but without balancing information. l The interesting property here is that, as in splay trees, one can do without balancing information if one accepts amortized bounds instead of worst-case bounds. l By omitting balancing information, in principle the structure becomes simpler; we just always perform same sequence of operations. l The memory advantage of doing without balancing information is insignificant; memory is never a problem, and in the bottom-up variant of skew heaps, we actually need several additional pointers per node.
Skew Heaps l Without balancing information, one cannot decide whether rank on left or on right is larger, so whether to exchange left and right subtree to restore the leftist heap property. l In skew heaps, the strategy is just to exchange always. l Skew heaps lead to simpler code. l Other operations are same as before, references to rank field must be removed. We must use object field as mark for an empty heap. l Theorem. The skew heap structure supports operations find min in O (1) time and insert, merge, and delete min in amortized O (log n) time on a heap with n elements.
Binomial Heaps l Binomial heaps - classical, somewhat complicated, method to achieve all heap operations including merge in O (log n) time. In contrast to the previous structure, the find min operation also needs Θ (log n) time.. l Binomial heaps can be written as binary trees with keys and objects in each node, but they are not heap-ordered trees, but only half-ordered trees: l If node w is in right subtree of node v, then v->key < w- >key. l This is a weaker condition than heap order: keys get larger to the right, but on a left path keys might appear in any order.
Binomial Heaps l The minimum key itself might occur anywhere along path from root to the left. This weaker order condition is coupled with a stronger balance condition: l If v is a node on path from root to left, then v->right is root of a complete binary tree. The height of these trees is strictly decreasing along path from root to left. l Binomial heap consists of blocks, which are put together on left path: a node n i on path, whose right pointer points to a complete binary tree of height h i, where this h i is decreasing along path. The complete binary tree of height h contains 2 h+1 1 nodes, so together with node on the leftmost path block has 2 hi +1 nodes.
Binomial Heaps l Structure of binomial heap: Binomial Heap Structure, with Blocks of Size 2 0, 2 1, 2 2, 2 4,and2 6
Binomial Heaps l Combining heights of binomial heap: x y h >x >y y x x y h+1 or h+1 >y >x >x >y Combining Two Blocks of Size 2 h into One Block of Size 2 h+1
Binomial Heaps l Decreasing heights of binomial heap: from the deletion. One Block Minus its Root Is Again a Binomial Heap l Theorem. The binomial heap structure supports insert, merge, find min, and delete min in O(log n) time. l The amortized complexity of insert operation is O (1); any sequence of a insert and b delete min or merge operations on a set of heaps of initial total size n, with k heaps remaining at the end, takes O(a + b log n + k log n) time.
Fibonacci Heaps l Decrease-key operations motivated development of a number of heap structures with a decrease key operation aim to be constant time instead of O(log n). l Fibonacci heap is a half-ordered tree, and consists of blocks arranged on the leftmost path, but the structure of blocks is weaker and they are not necessarily of distinct size, and in decreasing order, as they were in binomial heaps. l The structure in a Fibonacci heap is as: each node n carries an integer field n->rank, and state n->state, which can be either complete or deficient. Then the defining properties are:
Fibonacci Heaps l For any node n with n->rank > 1, or n->rank = 1 and n- >state = complete, holds n->right à= NULL, and l If n->state = complete, then on left path below n->right there are n->rank nodes, which have rank at least n- >rank 1, n->rank 2,..., 0, in some sequence. l If n->state = deficient, then on left path below n->right there are n->rank 1 nodes, which have rank at least n- >rank 2, n->rank 3,..., 0, in some sequence. l For any node n with n->rank = 0, or n->rank = 1 and n- >state = deficient, holds n->right = NULL.
Fibonacci Heaps 1def 1 0 2 0 1def 0 2 0 2def 0 3 5def 1 0 2def 0 0 1 4 1def 0 l Structure: = = 2 2def 2def 3 0 1 2 0 0 1 0 1 0 0 1def 1def 0 0 0 Fibonacci Heap Structure: The Nodes Are Labeled by Rank and Deficiency Status If we allow no deficient nodes, demand strictly decreasing rank along the
Fibonacci Heaps l If we allow no deficient nodes, demand strictly decreasing rank along the leftmost path, and strengthen to the following property: l For any node n of with n->rank>0,holds n->right NULL and on the left path below n->right, there are n->rank nodes, which have rank exactly n->rank 1, n->rank 2,..., 0, in decreasing sequence. l Fibonacci heap is a structural relaxation of binomial heap. l A block again consists of a node n and the subtree below n->right; then we can add two blocks of rank k to one block of rank k + 1 in O(1) time.
Fibonacci Heaps l A block of rank k consists of top node n and at least k 2 further blocks, or k 1, if n->state = complete that are arranged on left path below n->right and have rank at least 0, 1,..., k 2. l So the minimum number f (k) of nodes in a block of rank k satisfies the recursion l f (k) = f (k 2) + f (k 3) + + f (1) + f (0) + 1. l Using f (k 1) = f (k 3) + + f (1) + f (0) + 1, we can rewrite this recursion as f(k)=f(k 1)+f(k 2) l The starting values are f (0) = f (1) = 1, so f is the classical sequence of Fibonacci numbers, which gives the name to this heap:
Fibonacci Heaps f (k) = 1 5 ( 1 + 5 2 ) k+1 1 5 ( 1 5 2 ) k+1 l The key elements of the Fibonacci heap are methods by which we maintain this structure. l Each node needs two further fields: a normal back pointer up and another pointer upward in the tree structure, r up, which for any node n not on the leftmost path points to that node m for which n is on the left path below m->right. l If n is on the leftmost path, we set n->r up to NULL.
Fibonacci Heaps Pointers: Fibonacci Heap: r up Pointers and Normal Tree Pointers
Fibonacci Heaps l The insert operation is simple: create a new node of rank 0, with the new key and object, and place it on top of the leftmost path, check whether the new key is smaller than the previous minimum and adjust the minimum pointer, if necessary. l The merge operation by just concatenating leftmost paths: left end pointer to end of the leftmost path. l Theorem. The Fibonacci heap structure supports operations find min, insert, merge, delete min, and decrease key, with find min, insert, and merge in O (1) time, decrease key in amortized O (1) time, and delete min in amortized O (log n) time.