Binomial Heaps -...

Post on 24-Oct-2019

7 views 0 download

Transcript of Binomial Heaps -...

Binomial HeapsBryan M. Franklin

bmfrankl@mtu.edu

1Friday, October 10, 2008

TradeoffsWorst Case

Operation Binary Heap Binomial HeapMake-Heap Θ(1) Θ(1)

Insert Θ(lg n) O(lg n)Minimum Θ(1) O(lg n)

Extract-Min Θ(lg n) Θ(lg n)Union Θ(n) Θ(lg n)

Decrease-Key Θ(lg n) Θ(lg n)Delete Θ(lg n) Θ(lg n)

2Friday, October 10, 2008

Binomial Trees

Definition:

B0: A binomial tree with a single node.

Bk: Two Bk-1 binomial trees connected such that one tree has the other as its leftmost child.

3Friday, October 10, 2008

Binomial Trees

B0 B1 B2 B3 B4

4Friday, October 10, 2008

Properties of binomial tree Bk:

• 2k nodes

• Height of tree is k

• Exactly nodes at depth i

• Root has degree k

• Children of the root from left to right are binomial trees Bk-1, Bk-2, Bk-3, ... , B0

!k

i

"

Binomial Tree Properties

5Friday, October 10, 2008

Binomial Tree Properties

B4

2k Nodes

k=424=16 nodes

1

54

11

3

109

15

2

87

14

6

1312

16

6Friday, October 10, 2008

Binomial Tree Properties

B4

Height of k

k=4height is 4

4

01

0

2

01

0

3

01

0

2

01

0

7Friday, October 10, 2008

Binomial Tree Properties

B4

Exactly nodes at depth i

1

43

6

2

54

4

1

32

3

1

21

1

!k

i

"

!40

"= 1

!41

"= 4

!42

"= 6

!43

"= 4

!44

"= 1

8Friday, October 10, 2008

Binomial Tree Properties

B3

Root has degree k

4

01

0

2

01

0

3

01

0

2

01

0

k=4Root’s degree is 4

B2

B1

B0Root’s children:B3, B2, B1, B0

9Friday, October 10, 2008

RepresentationLeft-child, Right-sibling (ch. 10)

Parent

Left-child

Right-sibling

10Friday, October 10, 2008

Representation Example

B4

11Friday, October 10, 2008

Binomial Heap

A binomial heap is a set, H, of binomial trees, such that:

1) Each tree in H satisfies the min-heap property.

2) The root of every tree in H has a unique degree.

12Friday, October 10, 2008

Binomial Heaps

6

4410

17

29

3148

50

8

2223

24

30

3245

55

15

3328

41

3

37

18

7

25

12head[H1]

head[H2]

13Friday, October 10, 2008

Heap Operations

• Make-Heap( )

• Minimum(H)

• Union(H1, H2)

• Insert(H, x)• Extract-Min(H)

• Decrease-Key(H, x, k)

• Delete(H, x)

14Friday, October 10, 2008

Make-Binomial-Heap

Make-Binomial-Heap()

1 H ! new Heap2 head[H ]! NIL3 return H

15Friday, October 10, 2008

Binomial-Heap-Minimum

Binomial-Heap-Minimum(H)

1 y ! NIL2 x! head[H ]3 min!"4 while x #= NIL5 do if key[x] < min6 then min! key[x]7 y ! x8 x! sibling [x]9 return y

16Friday, October 10, 2008

Binomial-Heap-Minimum

15

3328

41

7

25

12head[H1]

x

min=∞

y

NIL

17Friday, October 10, 2008

Binomial-Heap-Minimum

15

3328

41

7

25

12head[H1]

x

min=12

y

NIL

18Friday, October 10, 2008

Binomial-Heap-Minimum

15

3328

41

7

25

12head[H1]

x

min=7

y

NIL

19Friday, October 10, 2008

Binomial-Heap-Minimum

15

3328

41

7

25

12head[H1]

x

min=7

y

NIL

20Friday, October 10, 2008

Binomial-Link

Binomial-Link(y, z)

1 p[y]! z2 sibling [y]! child [z]3 child [z]! y4 degree[z]! degree[z] + 1

21Friday, October 10, 2008

Binomial-Link

y z

22Friday, October 10, 2008

Binomial-Link

y z

p[y]

23Friday, October 10, 2008

Binomial-Link

y z

sibling[y]

24Friday, October 10, 2008

Binomial-Link

y z

child[z]

25Friday, October 10, 2008

Binomial-Heap-UnionBinomial-Heap-Union(H1, H2)

1 H !Make-Binomial-Heap()2 head [H ]! Binomial-Heap-Merge(H1, H2)3 free H1 and H2, but not the lists they point to4 if head [H ] = NIL5 then return H6 prev -x ! NIL7 x! head [H ]8 next -x ! sibling [x]9 while next -x "= NIL

10 do if degree[x] "= degree[next -x ] or(sibling [next -x ] "= NIL and degree[sibling [next -x ]] = degree[x])

11 then prev -x ! x12 x! next -x13 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 = NIL17 then head [H ]! next -x18 else sibling [prev -x ]! next -x19 Binomial-Link(x, next -x )20 x! next -x21 next -x ! sibling [x]22 return H

26Friday, October 10, 2008

Binomial-Heap-Union

6

4410

17

29

3148

50

8

2223

24

30

3245

55

15

3328

41

3

37

18

7

25

12head[H1]

head[H2]

27Friday, October 10, 2008

6

4410

17

29

3148

50

8

2223

24

30

3245

55

Binomial-Heap-Union

3

37

7

25

15

3328

41

12head[H] 18

x next-x

28Friday, October 10, 2008

6

4410

17

29

3148

50

8

2223

24

30

3245

55

Binomial-Heap-Union

3

37

7

25

15

3328

41

head[H] 12

18

x next-x

29Friday, October 10, 2008

6

4410

17

29

3148

50

8

2223

24

30

3245

55

Binomial-Heap-Union

3

37

7

25

15

3328

41

head[H] 12

18

x next-xprev-x

30Friday, October 10, 2008

6

4410

17

29

3148

50

8

2223

24

30

3245

55

Binomial-Heap-Union

15

3328

41

head[H] 12

18

x next-xprev-x3

377

25

31Friday, October 10, 2008

6

4410

17

29

3148

50

8

2223

24

30

3245

55

Binomial-Heap-Union

15

3328

41

head[H] 12

18

x next-xprev-x3

377

25

32Friday, October 10, 2008

Binomial-Heap-InsertBinomial-Heap-Insert(H, x)

1 H ! " Make-Binomial-Heap()2 p[x] " NIL3 child [x] " NIL4 sibling [x] " NIL5 degree[x] " 06 head [H !] " x7 H " Binomial-Heap-Union(H, H !)

33Friday, October 10, 2008

Extract-Min

Binomial-Heap-Extract-Min(H)

1 find root in H with minimum key value, remove as x2 H ! " Make-Binomial-Heap()3 fill heap H ! by linking x’s children in reverse order4 H " Binomial-Heap-Union(H, H !)5 return x

34Friday, October 10, 2008

1

2512

18

16

2326

42

6

2914

38

8

1711

27

head[H] 37

41

10

1328

77

Extract-Min

35Friday, October 10, 2008

1

2512

18

16

2326

42

6

2914

38

8

1711

27

head[H] 37

41

10

1328

77

x

Extract-Min

36Friday, October 10, 2008

2512

18

16

2326

42

6

2914

38

8

1711

27

head[H] 37

41

10

1328

77

1

x

Extract-Min

37Friday, October 10, 2008

25 12

18

16

2326

42

6

2914

38

8

1711

27

37

41

head[H] 10

1328

77

head[H’]

Extract-Min

1

x

38Friday, October 10, 2008

25 12

18

16

2326

42

6

2914

38

8

1711

27

37

41

head[H]10

1328

77

Extract-Min

1

x

39Friday, October 10, 2008

Decrease-KeyBinomial-Heap-Decrease-Key(H, x, k)

1 if k > key [x]2 then error ”new key is larger than current key”3 key [x]! k4 y ! x5 z ! p[y]6 while z "= NIL and key [y] < key [z]7 do exchange key [y]# key [z]8 ! exchange satellite data as well9 y ! z

10 z ! p[y]

40Friday, October 10, 2008

Decrease-Key

25 12

18

16

2326

42

6

2914

38

8

1711

27

37

41

head[H]10

1328

77x

41Friday, October 10, 2008

Decrease-Key

25 12

18

16

237

42

6

2914

38

8

1711

27

37

41

head[H]10

1328

77x

42Friday, October 10, 2008

Decrease-Key

25 12

18

16

237

42

6

2914

38

8

1711

27

37

41

head[H]10

1328

77y

z

43Friday, October 10, 2008

Decrease-Key

25 12

18

7

2316

42

6

2914

38

8

1711

27

37

41

head[H]10

1328

77y

z

44Friday, October 10, 2008

Decrease-Key

25 12

18

7

2316

42

6

2914

38

8

1711

27

37

41

head[H]10

1328

77

zy

45Friday, October 10, 2008

Decrease-Key

25 12

18

10

2316

42

6

2914

38

8

1711

27

37

41

head[H]7

1328

77

zy

46Friday, October 10, 2008

Decrease-Key

25 12

18

10

2316

42

6

2914

38

8

1711

27

37

41

head[H]7

1328

77

y

z

47Friday, October 10, 2008

Decrease-Key

25 12

18

10

2316

42

6

2914

38

8

1711

27

37

41

head[H]7

1328

77

y

z

48Friday, October 10, 2008

Delete

Binomial-Heap-Delete(H, x)

1 Binomial-Heap-Decrease-Key(H, x,!")2 Binomial-Heap-Extract-Min()

49Friday, October 10, 2008

The End

50Friday, October 10, 2008