Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 583 Analysis of Algorithms

Similar presentations


Presentation on theme: "CS 583 Analysis of Algorithms"— Presentation transcript:

1 CS 583 Analysis of Algorithms
Binomial Heaps CS 583 Analysis of Algorithms 1/17/2019 CS583 Fall'06: Binomial Heaps

2 CS583 Fall'06: Binomial Heaps
Outline Definitions Binomial trees Binomial heaps Operations on Binomial Heaps Creating a new heap Finding the minimum key Union Self-testing 19.1-1, 1/17/2019 CS583 Fall'06: Binomial Heaps

3 CS583 Fall'06: Binomial Heaps
Introduction Heap is a data structure that has enormous utility. We saw how a binary heap was used to design an efficient heapsort algorithm. Heaps are used to implement priority queues. Priority queue maintains elements according to their keys. Min-priority queue supports the following operations: Insert(Q,x) – insert an element x Minimum(Q) – returns the element with the smallest key Extract-Min(Q) – removes and return the min element Decrease-Key(Q,x,k) – decrease the value of the key of x to k. For example, priority queues are used to schedule jobs on shared computer resources. 1/17/2019 CS583 Fall'06: Binomial Heaps

4 CS583 Fall'06: Binomial Heaps
Introduction (cont.) We study data structures known as mergeable heaps, which support the following operations: Make-Heap() creates and returns an empty heap. Insert(H,x) inserts a node x with a key into a heap H. Minimum(H) returns a pointer to the node whose key is minimum. Extract-Min(H) deletes the minimum node and returns its pointer. Union(H1, H2) creates and returns a new heap that contains all nodes from H1 and H2. Binary heaps work well if we don’t need the Union operation that takes (n) time. The Union on binomial heaps takes O(lg n) time. 1/17/2019 CS583 Fall'06: Binomial Heaps

5 CS583 Fall'06: Binomial Heaps
Binomial Trees The ordered tree is a rooted tree, where the order of children nodes matters. The binomial tree Bk is an ordered tree defined recursively: B0 consists of a single node. Bk consists of two binomial trees Bk-1 that are linked together: The root of one tree is the leftmost child of the root of another tree. 1/17/2019 CS583 Fall'06: Binomial Heaps

6 Binomial Trees: Properties
Lemma 19.1. For the binomial tree Bk, there are 2k nodes, the height of the tree is k, there are exactly (ki) nodes at depth i for i=0,1,...,k the root has degree k, which is greater than any other node; moreover if children of the root are numbered left to right by k-1,k-2,...,0, the child i is the root of subtree Bi. Proof. The proof is by induction. Verifying all properties for B0 is trivial. Assume the lemma holds for Bk-1. 1. Binomial tree Bk consists of two copies of Bk-1, hence Bk has 2k-1+2k-1 = 2k nodes. 1/17/2019 CS583 Fall'06: Binomial Heaps

7 Binomial Trees: Properties (cont.)
2. The way two copies of Bk-1 are linked together, the height of Bk is increased by one compared to Bk-1. Hence its maximum depth is 1 + (k-1) = k. 3. Let D(k,i) be the number of nodes at depth i of Bk. Bk is composed of two Bk-1 with one tree a one level below another one. Hence the number of nodes at level i of Bk is all nodes at level i of the upper Bk-1 and all nodes at level (i-1) of the lower Bk-1: D(k,i) = D(k-1, i) + D(k-1, i-1) = (by inductive hypothesis) (k-1i) + (k-1i-1) = (see Exercise C.1-7 & HW2) (ki). 1/17/2019 CS583 Fall'06: Binomial Heaps

8 Binomial Trees: Properties (cont.)
4. The only node with the greater degree in Bk than Bk-1 is the root, which has one more child. Hence the root of Bk has degree (k-1) + 1 = k. By inductive hypothesis children of Bk-1 are roots of Bk-2, Bk-3, ... , B0. Once Bk-1 is linked to Bk-1 from the left, the children of the resulting root are Bk-1, Bk-2, ... , B0.  Corollary 19.2 The maximum degree of any node in an n-node binomial tree is lg n. Proof. Follows from properties 1 and 4. The root has the maximum degree k, where k = lg n.  1/17/2019 CS583 Fall'06: Binomial Heaps

9 CS583 Fall'06: Binomial Heaps
A binomial heap H is a set of binomial trees that satisfies the following properties: Each binomial tree in H obeys the min-heap property: the key of a node >= the key of its parent this ensures that the root of the min-heap-ordered tree contains the smallest key. For any k >= 0, there is at most one binomial tree in H whose root has degree k. this implies that an n-node binomial heap H consists of at most (lg n + 1) binomial trees. it can be observed by a binary representation of n as (lg n + 1) bits. A bit i = 1 only if a tree Bi is in the heap. 1/17/2019 CS583 Fall'06: Binomial Heaps

10 Binomial Heaps: Representation
Each binomial tree within a binomial heap is stored in the left-child, right-sibling representation. Each node has the following fields: p[x] pointer to the parent. child[x] pointer to the leftmost child. sibling[x] pointer to the sibling immediately to the right. degree[x] – the number of children of x. The roots of the binomial trees in the heap are organized in a linked list, -- root list. The degrees of roots increase when traversing to the right. A heap H is accessed by the field head[H], which is the pointer to the first root in the root list. 1/17/2019 CS583 Fall'06: Binomial Heaps

11 Operations: Finding the Minimum
This procedure returns a node with the minimum key in an n-node heap H. Note that in the min-heap-ordered heap, the minimum key must reside in a root node. Binomial-Heap-Minimum(H) 1 y = NIL 2 x = head[H] 3 min =  4 while x <> NIL 5 if key[x] < min min = key[x] y = x 8 x = sibling[x] // next root 9 return y Because there are at most (lg n + 1) roots to check, the running time of this algorithm is O(lg n). 1/17/2019 CS583 Fall'06: Binomial Heaps

12 CS583 Fall'06: Binomial Heaps
Uniting Two Heaps The operation of uniting two heaps is used as a subroutine by most of other operations. The procedure is implemented by repeatedly linking binomial trees whose roots have the same degree. It uses an auxiliary Binomial-Link procedure to link two trees. It also uses an auxiliary Binomial-Heap-Merge procedure to merge two root lists into a single linked list sorted by degree. Uniting two heaps H1 and H2 returns a new heap and destroys H1 and H2 in the process. 1/17/2019 CS583 Fall'06: Binomial Heaps

13 CS583 Fall'06: Binomial Heaps
Binomial Link This procedure links the Bk-1 tree rooted at node y to the Bk-1 tree rooted at node z. Node z becomes the root of a Bk tree. The procedure is straightforward because of the left-child, right-sibling representation of trees. Binomial-Link(y,z) 1 p[y] = z 2 sibling[y] = child[z] 3 child[z] = y 4 degree[z]++ The procedure simply updates pointers in constant time (1). 1/17/2019 CS583 Fall'06: Binomial Heaps

14 CS583 Fall'06: Binomial Heaps
Heap Union Algorithm Binomial-Heap-Union(H1, H2) 1 H = Make-Binomial-Heap // create an empty heap // Merge the root lists of H1, H2 sorted by degree 2 head[H] = Binomial-Heap-Merge(H1,H2) 3 <free objects H1,H2, but not its lists> 4 if head[H] = NIL 5 return H 6 prev-x = NIL 7 x = head[H] // next tree in the root list next-x = sibling[x] In the first phase of the algorithm two root lists are merged in a single one. In the second phase, two trees in the root list are linked if they have the same degree. 1/17/2019 CS583 Fall'06: Binomial Heaps

15 Heap Union Algorithm (cont.)
9 while next-x <> NIL // traverse the root list 10 if (degree[x] <> degree[next-x]) or (sibling[next-x] <> NIL and degree[sibling[next-x]] = degree[x])) prev-x = x // skip to the next node x = next-x 13 else // retain min-heap property if key[x] <= key[next-x] // link into x sibling[x] = sibling[next-x] Binomial-Link(next-x,x) 1/17/2019 CS583 Fall'06: Binomial Heaps

16 Heap Union Algorithm (cont.)
else // link into next-x if prev-x = NIL head[H] = next-x else sibling[prev-x] = next-x Binomial-Link(x, next-x) x = next-x next-x = sibling[x] 25 return H 1/17/2019 CS583 Fall'06: Binomial Heaps

17 Heap Union: Performance
The running time of Binomial-Heap-Union is O(lg n), where n is the total number of nodes in binomial heaps H1 and H2. Let H1 contain n1 nodes and H2 contain n2 nodes: n=n1+n2. After merging two heaps, H contains at most lg n1 + lg n2 + 2 <= 2 lg n + 2 = O(lg n) roots. Hence the time to perform Binomial-Heap-Merge is O(lg n). Each iteration of the while loop takes (1) time, and there are at most lg n1 + lg n2 + 2 = O(lg n) iterations. Each iteration either advances the pointers one position down the root list, or removes a root from the root list. 1/17/2019 CS583 Fall'06: Binomial Heaps


Download ppt "CS 583 Analysis of Algorithms"

Similar presentations


Ads by Google