Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 20 Binomial Heaps

Similar presentations


Presentation on theme: "Chapter 20 Binomial Heaps"— Presentation transcript:

1 Chapter 20 Binomial Heaps
컴퓨터 알고리즘 인공지능연구실 민병국

2 Preface Binomial Heap introduced in 1978 by Jean Vuillemin
“A data structure for manipulating priority queues” communications of the ACM, 21(4): studied their properties in detail by Mark R. Brown “The Analysis of a Practical and Nearly Optimal Priority Queue” “Implementation and analysis of binomial queue algorithms”

3 Preface Mergeable Heaps +  Binomial coefficients binomial expansion :
Binomial coefficients binomial expansion : x=y=1 : ex) n=4  24 = ( ) Bk-1 Bk-1 Bk-1 Bk-1 Bk

4 Mergeable Heaps Five operations Make-Heap() Insert(H,x) Minimum(H)
Heap H에 Node x 삽입 Minimum(H) Heap H의 가장 작은 Key의 Pointer 반환 Extract-Min(H) 가장 작은 Key의 Node를 빼 냄 Union(H1, H2) Heap H1, H2를 병합, 새로운 Heap 생성

5 Mergeable Heaps Additional two operation in Binomial heaps
Decrease-Key(H, x, k) Heap H의 Node x의 키값을 그 이하의 값 k로 대입 Delete(H,x) Heap H로부터 Node x를 제거

6 Running times for operations
Binary Binomial Fibonacci Procedure heap heap heap Make-Heap Insert Minimum Extract-Min Union Decrease-Key Delete

7 Figure 20.2 Binomial trees (a) B0 (b) B0 B1 B2 B3 B4 (c) Depth 1 2 3 4
1 2 3 4 B0 Bk-1 Bk Bk-1 (b) B0 B1 B2 B3 B4 (c) B0 B1 B2 Bk Bk-2 Bk-1

8 Binomial trees Lemma 20.1(Properties of binomial trees)
For the binomial tree Bk, 1.There are 2k nodes Binomial tree Bk consists of two copies of Bk-1, so Bk has 2k-1+ 2k-1 = 2k noes 2.The height of the tree is k Because the maximum depth of a node in Bk is one greater than the maximum depth in Bk-1, this maximum depth is (k-1)+1=k

9 Binomial trees 3.There are exactly nodes at depth i for i=0,1,…,k
Let D(k,i) be the number of nodes at depth i of binomial tree Bk D(k,i) = D(k-1,i) + D(k-1,i-1) = ※ k-combinations of an n-set

10 Binomial trees 4.The root has degree k, which is greater than that of any other node; moreover if the children of the root are numbered from left to right by k-1,k-2,…,0, child i is the root of a subtree Bi The only node with greater degree in Bk than in Bk-1 is the root, which has one more child than in Bk-1 Bk = Bk-1 + Bk-1 Bk = Bk-1 + (Bk-2 + Bk-3 + … + B0 ) Therefore, the root of Bk has Bk-1, Bk-2, … , B1, B0.

11 Figure 20.3 Binomial heap (a) head[H] 10 1 25 12 18 6 29 14 8 38 17 11
27

12 Figure 20.3 Binomial heap (b) head[H] p key degree child 10 1 2 25 12
1 2 25 12 18 6 3 29 38 14 8 17 11 27 head[H] sibling (b)

13 Binomial heaps Binomial heap H is a set of binomial trees that satisfies the following properties. Binomial-heap properties 1. Each binomial tree in H is heap-ordered: the key of a node is greater than or equal to the key of its parent. The root of a heap-ordered tree contains the smallest key in the tree

14 Binomial Heaps 2. There is at most one binomial tree in H whose root has a given degree. An n-node binomial heap H consists of at most binomial trees. The binary representation of n has bits, say , so that ex) the binary representation of 13 is <1101>  H consists of heap-ordered binomial trees B3, B2, and B0, having 8, 4, and 1 nodes respectively, for a total of 13 nodes

15 Operations on binomial heaps
Creating a new binomial heap Finding the minimum key Uniting two binomial heaps Inserting a node Extracting the node with minimum key Decreasing a key Deleting a key

16 Creating a new binomial heap
Allocates and returns an object H head[H] = Nil running time is

17 Finding the minimum key
Binomial-Heap-Minimum(H) y  Nil x  head[H] min  ∞ while x ≠ Nil do if key[x] < min then min  key[x] y  x x  sibling[x] return y running time is Since a binomial heap is heap-ordered, the minimum key must reside in a root node: at most

18 Uniting two binomial heaps
Binomial-Link(y,z) : running time is O(1) p[y]  z sibling[y]  child[z] child[z]  y degree[z]  degree[z] + 1 Binomial-Heap-Merge(H1,H2) merges the root lists of H1 and H2 into a single linked list that is sorted by degree into monotonically increasing order. If the root lists of H1 and H2 have m roots altogether, running time is O(m) The running time of Binomial-Heap-Union is

19 Figure 20.6 Four cases In each case, x is the root of a Bk-tree and l > k (a) Case 1: degree[x]≠degree[next-x] pointers move one position further down the root list (b) Case 2: degree[x]=degree[next-x]=degree[sibling[next-x]] (c) Case 3: degree[x]=degree[next-x]≠ degree[sibling[next-x]] and key[x]≤key[next-x] remove next-x from the root list and link it to x, creating a Bk+1-tree (d) Case 4: degree[x]=degree[next-x]≠ degree[sibling[next-x]] and key[next-x]≤key[x] remove x from the root list and link it to next-x,

20 Figure 20.5 Binomial-Heap-Union
(a) Binomial heaps H1 and H2 (b) case 3: both x and next-x have degree 0 and key[x] < key[next-x] (c) case 2: x is the first of three roots with the same degree (d) case 4: x is the first of two roots of equal degree (e) case 3: both x and next-x have degree 2 (f) case 1: x has degree 3 and next-x has degree 4

21 Binomial-Heap-Union (1/2)
Binomial-Heap-Union(H1,H2) H  Make-Binomial-Heap() head[H]  Binomial-Heap-Merge(H1,H2) free the objects H1 and H2 but not the lists they point to if head[H] = Nil then return H prev-x  Nil x  head[H] next-x  sibling[x] while next-x ≠ Nil

22 Binomial-Heap-Union (2/2)
do if (degree[x] ≠ degree[next-x]) or (sibling[next-x] ≠ Nil and degree[sibling[next-x]] = degree[x]) then prev-x  x ▶ Case 1 and 2 x  next-x ▶ Case 1 and 2 else if key[x] ≤ key[next-x] then sibling[x]  sibling[next-x] ▶ Case 3 Binomial-Link(next-x,x) ▶ Case 3 else if prev-x = Nil ▶ Case 4 then head[H]  next-x ▶ Case 4 else sibling[prev-x]  next-x ▶ Case 4 Binomial-Link(x,next-x) ▶ Case 4 x  next-x ▶ Case 4 next-x  sibling[x] return H

23 Inserting a node Binomial-Heap-Insert(H,x) Running time is
H’  Make-Binomial-Heap() p[x]  Nil child[x]  Nill sibling[x]  Nil degree[x]  0 head[H’]  x H  Binomial-Heap-Union(H,H’) Running time is unites the new heap H’ with the n-node binomial heap H in

24 Extracting the node with minimum key
Binomial-Heap-Extract-Min(H) find the root x with the minimum key in the root list of H, and remove x from the root list of H H’  Make-Binomial-Heap() reverse the order of the linked list of x’s children, and set head[H’] to point to the head of the resulting list H  Binomial-Heap-Union(H,H’) return x Running Time is

25 Figure 20.7 Binomial-Heap-Extract-Min
A binomial heap H The root x with minimum key is removed from the root list of H The linked list of x’s children is reversed, giving another binomial heap H’ The result of uniting H and H’

26 Decreasing a key Binomial-Heap-Decrease-Key(H,x,k) Running Time is
if k > key[x] then error “new key is greater than current key” key[x]  k y  x z  p[y] while z ≠ Nil and key[y] < key[z] do exchange key[y] ↔ key[z] ▷If y and z have satellite fields, exchange them, too. y  z Running Time is becase following the root path, the while loop iterates at most times

27 Figure 20.8 Binomial-Heap-Decrease-Key
The first iteration of the while loop. Node y has had its key decreased to 7, which is less than the key of y’s parent z The keys of the two nodes are exchanged, and pointers y and z have moved up one level in the tree, but heap order is still violated After another exchange and moving pointers y and z up one more level, the while loop terminates.

28 Deleting a key Binomial-Heap-Delete(H,x) Running Time is
Binomial-Heap-Decrease-Key(H,x,-∞) Binomial-Heap-Extract-Min(H) Running Time is Binomial-Heap-Decrease-Key : Binomial-Heap-Extract-Min :


Download ppt "Chapter 20 Binomial Heaps"

Similar presentations


Ads by Google