ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. ·...

36
Binomial Heaps Manoj Kumar DTU, Delhi

Transcript of ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. ·...

Page 1: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Binomial Heaps

Manoj Kumar

DTU, Delhi

Page 2: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Binomial Heaps

• Known as mergeable heaps.

• Supports following five operations:

1. MAKE-HEAP(): creates and returns a new heap with

no elements.

2. INSERT(H, x): Inserts node x, into heap H.2. INSERT(H, x): Inserts node x, into heap H.

3. MINIMUM(H): returns a pointer to the node in heap H

whose key is minimum.

4. EXTRACT-MIN(H): deletes the node from heap H

whose key is minimum, returning pointer o the node.

5. UNION(H1,H2): creates and returns new heap that

contains all the elements of heaps H1 and H2. H1 and

H2 are destroyed in this process.

Page 3: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Binomial Heaps: Definitions

Binomial Heap: Collection of binomial trees

(satisfying some properties).

Binomial Trees

• Definition is inductive.

• These are ordered trees, i.e., order of children is

important.

1

32

1

23Different Trees

54 54

Page 4: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Binomial Trees

• Base Case: B0 = single node is a binomial tree.

• Inductive Step:

BBk = is a binomial tree.Bk-1

Bk-1

Page 5: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

ExamplesB0

B1

B2

B3

B4

depth # nodes

0 1

1 4

2 6

3 4

4 1

Page 6: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Another Way to Look at Bk

L

BB0

Bk-1

Bk-2

B2

B1

Bk

Page 7: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Properties of Binomial Trees

Lemma 1: For the binomial tree Bk,

1. There are 2k nodes.

2. Tree height is k.

3. nodes at depth i, i = 0, 1, …, k [binomial coefficients].

i

k3. nodes at depth i, i = 0, 1, …, k [binomial coefficients].

4. Root has degree k, other nodes have smaller degree. ith

child of root is root of subtree Bi, where i = k–1, k–2, …,

0 [Bk–1 is Left Most, B0 is Right Most].

i

Page 8: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Proof: Inductive

1. Binomial tree Bk consists of two copies of Bk-1, so

Bk has 2k-1 + 2k-1 = 2k nodes.

2. The way in which two copies of are connected,

height of Bk is one greater than height of Bk-1. So the

height of B is (k-1)+1= k.height of Bk is (k-1)+1= k.

Bk-1

Bk-1

Bk

Page 9: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Proof: Inductive

3. Let D(k,i) be the number of nodes at depth I of binomial tree

Bk. Since Bk is composed of two copies of Bk-1 linked

together, a node at depth i in Bk-1 appears in Bk once at depth

i and once at depth i+1.

Thus number of nodes in Bk at depth i is the number of nodes at

depth i in Bk-1 plus the number of nodes at depth i-1 in Bk-1. depth i in Bk-1 plus the number of nodes at depth i-1 in Bk-1.

Thus

D(k,i) = D(k-1,i) + D(k-1,i-1)

= + =

i

1-k

1-i

1-k

i

k

Bk-1

Bk-1

Bkdepth i in Bk

depth i in this Bk-1

depth i–1 in this Bk-1

Page 10: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

proof

4. Root degree of Bk = 1 + root degree of Bk-1

= 1 + k–1 , induction hypothesis

= k

Corollary : The maximum degree in an n-node binomial tree is lg n.

Page 11: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Binomial Heaps

• Binomial heap H is a set of binomial trees satisfying following

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 it’s parent.

iImplies root of a binomial tree has the smallest key in that tree.tree.

2 There is at most one binomial tree in H whose root has a

given degree.

iImplies B.H. with n nodes has at most lg n + 1 B.T.’s.

Think of n in binary: ⟨blg n, …, b0⟩, i.e.,

B.H contains Bi iff bi is 1.

∑=

=

n lg

1i

i

i 2bn

Page 12: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Representing Binomial Heaps

10

25

18

12

1

27

17 3811

29148

6

parentEach node is

head[H]

parent

key

degree

child sibling

Each node isrepresented by astructure like this

10

25

18

12

1

27

17 3811

29148

6head[H]

Page 13: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Operations on Binomial Heaps

MAKE-BINOMIAL-HEAP(): simply allocates and

return an object H, where head[H] = NIL.

Running time is Θ(1)

MAKE-BINOMIAL-HEAP()1. Create new head node H2. head[H]�NIL3. return H

Page 14: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Operations on Binomial Heaps

BINOMIAL-HEAP-MINIMUM(H)1. y � NIL;2. x � head[H];3. min � ∞;4. while x ≠ NIL do5. if key[x] < min then5. if key[x] < min then6. min � key[x];7. y � x8. x � sibling[x]9. return y

Time is O(lg n).

Page 15: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Linking Two Binomial Trees

BINOMIAL-LINK(y,z)1. p[y] � z;2. sibling[y] � child[z];

Linking two binomial trees whose roots have same degree.

2. sibling[y] � child[z];3. child[z] �y;4. degree[z] � degree[z] + 1

Bk-1

Bk-1Bk-1 Bk-1

yy

zz

Link

Page 16: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION

UnionH1, H2 H1 ∪ H2

H1 = H2 =

First, simply merge the two root lists by root degree (like merge sort).

Remaining Problem: Can have two trees with the same root degree.

Page 17: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION…

prev-x x next-x

Union traverses the new root list like this:

Depending on what x, next-x, and sibling[next-x] point to, Unionlinks trees with the same root degree.

Note: We may temporarily create three trees with the same rootdegree.

Page 18: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION…

12

33

41

28

15

25

7head[H1]18

37

3

50

31 1748

441029

6

32 2445

222330

8

head[H2]

Union

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7

head[H]

18

12

prev-x x

55

Union

Page 19: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION:Example

12

33

41

28

15

25

7head[H1] 18

37

3

50

31 1748

441029

6

55

32 2445

222330

8

head[H2]

55

18

37

3

50

31 1748

441029

6

55

32 2445

222330

8

12

33

41

28

15

25

7head[H]

Merge

x next-x

Page 20: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION:Example…

18

37

3

50

31 1748

441029

6

32 2445

222330

8

12

33

41

28

15

25

7head[H]x next-x

55

Case 3

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7head[H]

18

12

x next-x

Page 21: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION:Example…

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7head[H]

18

12

x next-x

Case 255

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7head[H]

18

12

prev-x x next-x

Page 22: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION:Example…

Case 4

37

3

50

31 1748

441029

6

32 2445

222330

8

33

41

28

15

25

7head[H]

18

12

prev-x x next-x

Binomial Heaps - 22

Case 455

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7

head[H]

18

12

prev-x x next-x

Page 23: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION:Example…

37

3

50

31 1748

441029

6

32 2445

222330

8

33

41

28

15

25

7

head[H]

18

12

prev-x x next-x

Case 3 55

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7

head[H]

18

12

prev-x x next-x

Page 24: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

UNION:Example…

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7

head[H]

18

12

prev-x x next-x

Case 1

37

3

50

31 1748

441029

6

55

32 2445

222330

8

33

41

28

15

25

7

head[H]

18

12

prev-x x next-x = NIL⇒⇒⇒⇒ terminates

55Case 1

Note: Union isO(lg n).

Page 25: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Code for UNIONBINOMIAL-HEAP-UNION(H1, H2)1. H � MAKE-BINOMIAL-HEAP();2. head[H] � BINOMIAL-HEAP-MERGE(H1, H2); /* simple merge of root lists */3. Free the objects H1 and H2, but not the list they point to4. if head[H] = NIL5. then return H 6. prev-x � NIL;7. x � head[H];8. next-x � sibling[x];9. while next-x ≠ NIL9. while next-x ≠ NIL10. do if (degree[x] ≠ degree[next-x]) or

(sibling[next-x] ≠ NIL and degree[sibling[next-x]] = degree[x])11. then prev-x � x;12. x � next-x;13. else if key[x] ≤ key[next-x] 14. then sibling[x] � sibling[next-x];15. BINOMIAL-LINK(next-x, x)16. else if prev-x = NIL 17. then head[H] � next-x 18. else sibling[prev-x] � next-x 19. BINOMIAL-LINK(x, next-x);20. x � next-x21. next-x � sibling[x]22. return H

Cases 1 & 2

Cases 1 & 2

Case 3

Case 3Case 4Case 4

Case 4

Case 4

Case 4

Page 26: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

a b c dprev-x x next-x sibling[next-x]

a b c dprev-x x next-x sibling[next-x]

prev-x x next-x sibling[next-x]

BkBkBk

Bk Bl

Case 2

Case 1 a b c dprev-x x next-x

Bk Bl

a b c dprev-x x next-x

BkBkBk

prev-x x next-xa b c d

prev-x x next-x sibling[next-x]

a b c dprev-x x next-x sibling[next-x]

Bk Bk

BkBkBl

Bl

key[x] > key[next[x]]

key[x] ≤≤≤≤ key[next[x]]

Case 3

Case 4

a

c

b dprev-x x next-x

Bk

Bk

Bl

Bk+1

a

b

c d

Bk

Bk

Bl

prev-x x next-x

Page 27: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

INSERT

BINOMIAL-HEAP-INSERT(H, x)1. H′ � MAKE-BINOMIAL-HEAP();2. p[x] � NIL;

Inserts node x into binomial heap H, assuming that node x already been allocated and key[x] has already been filled.

parent

key

degree

x

2. p[x] � NIL;3. child[x] � NIL;4. sibling[x] � NIL;5. degree[x] � 0;6. head(H′) � x;7. H �BINOMIAL-HAP-UNION(H, H′)

Time O(lg n).

degree

child sibling

Page 28: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

INSERT:Example

12

33

41

28

15

25

7head[H]2head[H’]

12 157head[H] 2

2

33

41

28

15

25

7head[H]

12

33

41

28

15

25

head[H]

Page 29: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

INSERT: Example…

7 33

41

28

15

12

2head[H]

25

7

33

41

28

1512

2head[H]

25

Page 30: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Extract-Minimum

• Extracts the node with minimum key from binomial

heap H, and returns a pointer to the extracted node

BINOMIAL-HEAP-EXTRACT-MIN(H)1. remove minimum key root x from H’s root list;2. H′ � Make-B-H();

2. H Make-B-H();3. root list of H′ = x’s children in reverse order;4. H � Union(H, H′);5. return x

Time O(lg n).

Page 31: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Extract-Min: Example

41

37

42

23 1826

251216

1

17 3811

29148

6

13

77

28

10head[H]

27

41

37

42

23 1826

251216

1

27

17 3811

29148

6

13

77

28

10head[H]x

Page 32: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Extract-Min: Example

41

37

13

77

28

10head[H]

42

2318 26

25 12 16

27

17 3811

29148

6head[H′]

25

27

17 3811

29148

6

42

23 7726

132816

10

18

41

37

12head[H]

Page 33: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Decrease-Key

BINOMIAL-HEAP-DECREASE-KEY(H, x, k)1. if k > key[x] 2. then error “new key is greater than current key” 3. key[x] � k;

Decreases the key of a node x in a binomial heap H to a new value k.

3. key[x] k;4. y � x;5. z � p[y];6. while z ≠ NIL and key[y] < key[z] 7. do exchange key[y] and key[z];8. exchange other satellite fields of y and z9. y � z;10. z � p[y]

O(lg n)

Page 34: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

Decrease-Key Example25

27

17 3811

29148

6

42

23 7726

132816

10

18

41

37

12head[H]

42

Decrease key 26 to 7

25

27

17 3811

29148

6

42

23 777

132816

10

18

41

37

12head[H]

y

z

Page 35: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

25

27

17 3811

29148

6

42

23 7716

13287

10

18

41

37

12head[H]

y

z

42

25

27

17 3811

29148

6

42

23 7716

132810

7

18

41

37

12head[H]

y

z

Page 36: ManojKumar DTU, Delhipeople.dtu.ac.in/faculty/lectures/manojkumar/DSA_MK_Lect... · 2016. 2. 8. · 3 50 48 31 17 10 44 29 6 55 32 24 45 23 22 30 8 33 41 28 15 25 7 head[H] 18 12

DELETE a node

BINOMIAL-HEAP-DELETE(H, x)1. BINOMIAL-HEAP-DECREASE-KEY(H, x, −∞);2. BINOMIAL-HEAP-EXTRACT-MIN(H)

Time is O(lg n)