Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87...

87
Data Structures Haim Kaplan & Uri Zwick December 2013 Binomial Heaps Fibonacci Heaps 1

Transcript of Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87...

Page 1: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Data Structures

Haim Kaplan & Uri Zwick

December 2013

Binomial Heaps

Fibonacci Heaps

1

Page 2: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Fibonacci

Heaps

Lazy

Binomial

Heaps

Binomial

Heaps

Binary

Heaps

O(1) O(1) O(log n) O(log n) Insert

O(1) O(1) O(1) O(1) Find-min

O(log n) O(log n) O(log n) O(log n) Delete-min

O(1) O(log n) O(log n) O(log n) Decrease-key

O(1) O(1) O(log n) – Meld

Heaps / Priority queues

Worst case Amortized

Delete can be implemented using Decrease-key + Delete-min

Decrease-key in O(1) time important for Dijkstra and Prim

Page 3: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Binomial Heaps [Vuillemin (1978)]

3

Page 4: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

4

Binomial Trees

B0 B1 B2 B3

B4

Page 5: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

5

Binomial Trees

B0 B1 B2 B3

B4

Page 6: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

6

Binomial Trees

B0 B1 B2 B3

Bk

Bk−1

Bk−1 Bk−1 Bk−2

Bk

Page 7: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

7

Binomial Trees

B0 Bk

Bk−1

Bk−1

Bk−1 Bk−2

Bk

Page 8: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

8

Min-heap Ordered Binomial Trees

45

67

40

58

20

31

15

35 18

45

5

11

17

38

2

25

key of child key of parent

Page 9: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

9

Tournaments Binomial Trees

G

H

F

E

A

B

D

C F D

D

B H C E D F

D G F A

A G

The children of x are the items that lost matches with x,

in the order in which the matches took place.

Page 10: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Binomial Heap

A list of binomial trees, at most one of each rank

45

67

40

58

20

31

15

35

9

33

23

Pointer to root with minimal key

Each number n can be written

in a unique way as a sum of

powers of 2

11 = (1011)2 = 8+2+1

At most

log2 (n+1) trees

Page 11: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

11

Ordered forest Binary tree

45

67

40

58

20

31

15

35

9

33

23

next – next “sibling”

info key

child next

x

2 pointers per node

child – leftmost child

Page 12: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Forest Binary tree

45

67

40

58

20

31

15

35

9

33

23

40

45 20

67 58 31 35

9

15 33

23

Heap order

“half ordered”

Page 13: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Binomial heap representation

45

67

40

58

20

31

15

35

9

33

23

n first

2 pointers per node

No explicit rank information

Q

min

info key

child next

x

“Structure V”

[Brown (1978)]

How do we determine ranks?

Page 14: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Alternative representation

45

67

40

58

20

31

15

35

9

33

23

n first

Reverse sibling pointers

Make lists circular

Q

min Avoids reversals during meld

“Structure R”

[Brown (1978)]

info key

child next

Page 15: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

15

Bk a

Bk−1

b

Bk−1

Linking binomial trees

a ≤ b

O(1) time

x

y

Page 16: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

16

Linking in first

representation

Linking in second

representation

Linking binomial trees

Page 17: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Melding binomial heaps

Q1:

Q2:

Link trees of same degree

Page 18: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

18

Q1: B0 B1 B3

Q2: B0 B2 B3

Melding binomial heaps Link trees of same degree

B1 B2 B3

B3 B4

Like adding binary numbers

O(log n) time

Maintain a pointer to the minimum

Page 19: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

19

Insert

45

67

40

58

20

31

9

35

15

33

23

New item is a one tree binomial heap

Meld it to the original heap

O(log n) time

11

Page 20: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

20

Delete-min

45

67

40

58

20

31

9

35

15

33

23

When we delete the

minimum, we get a

binomial heap

Page 21: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

21

Delete-min

45

67

40

58

20

31

35

15

33

23

When we delete the

minimum, we get a

binomial heap

Meld it to the original heap

O(log n) time

(Need to reverse list of

roots in first representation)

Page 22: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

22

Decrease-key using “sift-up”

45

67

40

58

20

31

15

35 18

45

5

11

17

38

2

25

I

Decrease-key(Q,I,7)

Page 23: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

23

45

67

40

58

20

7

15

35 18

45

5

11

17

38

2

25

Decrease-key using “sift-up”

I

Need parent pointers

(not needed before)

Page 24: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

24

45

67

40

58

7

20

15

35 18

45

5

11

17

38

2

25

Decrease-key using “sift-up”

I

Need to update the node pointed by I

Page 25: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

25

45

67

40

58

7

20

15

35 18

45

5

11

17

38

2

25

Decrease-key using “sift-up”

I

Need to update the node pointed by I

Page 26: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

15

20

7

35 18

45

5

11

17

38

2

25

Decrease-key using “sift-up”

I

How can we do it?

Page 27: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

27

Adding parent pointers

45

67

40

58

20

31

15

35

9

33

23

n first

Q

min

Page 28: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

item child next

x

parent

28

Adding a level of indirection

45

67

40

58

20

31

15

35

9

33

23

node

info key

I

Nodes and items

are distinct entities

n first

Q

min

“Endogenous vs. exogenous”

Page 29: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Heaps / Priority queues

Worst case Amortized

Fibonacci

Heaps

Lazy

Binomial

Heaps

Binomial

Heaps

Binary

Heaps

O(1) O(1) O(log n) O(log n) Insert

O(1) O(1) O(1) O(1) Find-min

O(log n) O(log n) O(log n) O(log n) Delete-min

O(1) O(log n) O(log n) O(log n) Decrease-key

O(1) O(1) O(log n) – Meld

Page 30: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Lazy Binomial Heaps

30

Page 31: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Binomial Heaps

A list of binomial trees,

at most one of each rank, sorted by rank

(at most O(log n) trees)

Pointer to root with minimal key

Lazy Binomial Heaps

An arbitrary list of binomial trees

(possibly n trees of size 1)

Pointer to root with minimal key

Page 32: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

32

45

67

40

58

20

31

15

35

9

33

10

Pointer to root with minimal key

Lazy Binomial Heaps

An arbitrary list of binomial trees

29 59 87 19

20

Page 33: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

33

Update the pointer to root with minimal key

Lazy Meld

Concatenate the two lists of trees

O(1) worst case time

Update the pointer to root with minimal key

Lazy Insert

Add the new item to the list of roots

O(1) worst case time

Page 34: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

May need (n) time to find the new minimum

Lazy Delete-min ?

Remove the minimum root and meld ?

45

67

40

58

20

31

9

35

15

33

10 29 59 19

20

Page 35: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Consolidating / Successive Linking

45

67

40

58

20

31

35 15

33

10 29 59 19

20

0 1 2

3

Page 36: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

20

31

35 15

33

10

29 59 19

20

0 1 2

3

Consolidating / Successive Linking

Page 37: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

20

31

35 15

33

59 19

20

0 1 2

3

10

29

Consolidating / Successive Linking

Page 38: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

20

31

35 59 19

20

0 1 2

3

15

33

10

29

Consolidating / Successive Linking

Page 39: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

20

31

35

59

19

20

0 1 2

3

15

33

10

29

Consolidating / Successive Linking

Page 40: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

20

31

35

59

19

20

0 1 2

3

45

67

40

58

15

33

10

29

Consolidating / Successive Linking

Page 41: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

20

31

35

59

19

20

0 1 2

3

45

67

40

58

15

33

10

29

Consolidating / Successive Linking

Page 42: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

20

31

19

20

0 1 2

3

45

67

40

58

15

33

10

29

35

59

Consolidating / Successive Linking

Page 43: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

19

20

0 1 2

3

45

67

40

58

15

33

10

29

35

59

20

31

Consolidating / Successive Linking

Page 44: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

19

20

0 1 2

3

45

67

40

58

15

33

10

29

35

59

20

31

At the end of the process, we obtain a non-lazy

binomial heap containing at most log (n+1) trees,

at most one of each rank

Consolidating / Successive Linking

Page 45: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

At the end of the process, we obtain a non-lazy

binomial heap containing at most log n trees,

at most one of each degree

Worst case cost – O(n)

Amortized cost – O(log n)

Potential = Number of Trees

Consolidating / Successive Linking

Page 46: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Cost of Consolidating

T0 – Number of trees before

L – Number of links

T1 – Number of trees after

T1 = T0−L (Each link reduces the number of tree by 1)

Total number of trees processed – T0+L

(Each link creates a new tree)

Total cost = O( (T0+L) + L + log2n )

= O( T0+ log2n )

Putting trees into buckets

or finding trees to link with Linking

Handling

the buckets

As L ≤ T0

Page 47: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Amortized Cost of Consolidating

(Scaled) actual cost = T0 + log2n

Change in potential = = T1−T0

Amortized cost = (T0 + log2n ) + (T1−T0)

= T1 + log2n

≤ 2 log2n As T1 ≤ log2n

Another view: A link decreases the potential by 1.

This can pay for handling all the trees involved in the link.

The only “unaccounted” trees are those that

were not the input nor the output of a link operation.

Page 48: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Amortized

cost

Change in

potential Actual cost

O(1) 1 O(1) Insert

O(1) 0 O(1) Find-min

O(log n) k1+T1−T0 O(k+T0+log n) Delete-min

O(log n) 0 O(log n) Decrease-key

O(1) 0 O(1) Meld

Lazy Binomial Heaps

Rank of

deleted root

Page 49: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Heaps / Priority queues

Worst case Amortized

Fibonacci

Heaps

Lazy

Binomial

Heaps

Binomial

Heaps

Binary

Heaps

O(1) O(1) O(log n) O(log n) Insert

O(1) O(1) O(1) O(1) Find-min

O(log n) O(log n) O(log n) O(log n) Delete-min

O(1) O(log n) O(log n) O(log n) Decrease-key

O(1) O(1) O(log n) – Meld

Page 50: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

One-pass successive linking

A tree produced by a link is immediately

put in the output list and not linked again

Worst case cost – O(n)

Amortized cost – O(log n)

Potential = Number of Trees

Exercise: Prove it!

Page 51: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

20

31

35 15

33

10 29 59 19

20

0 1 2

… 3

One-pass successive Linking

Page 52: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

20

31

35 15

33

10

29 59 19

20

0 1 2

… 3

One-pass successive Linking

Page 53: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

45

67

40

58

20

31

35 15

33

59 19

20

0 1 2

… 3

One-pass successive Linking

10

29

Output list:

Page 54: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Fibonacci Heaps [Fredman-Tarjan (1987)]

54

Page 55: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

55

Decrease-key in O(1) time?

45

67

40

58

20

31

15

35 18

45

5

11

17

38

2

25 x

Decrease-key(Q,x,30)

Page 56: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

56

45

67

30

58

20

31

15

35 18

45

5

11

17

38

2

25 x

Decrease-key(Q,x,10)

Decrease-key in O(1) time?

Page 57: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

57

45

67

10

58

20

31

15

35 18

45

5

11

17

38

2

25 x

Heap order violation

Decrease-key in O(1) time?

Page 58: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

58

45

67

10

58

20

31

15

35 18

45

5

11

17

38

2

25 x

Cut the edge

Decrease-key in O(1) time?

Page 59: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

59

45

67

10

58

20

31

15

35 18

45

5

11

17

38

2

25 x

Tree no longer binomial

Decrease-key in O(1) time?

Page 60: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

60

40

58

20

31

15

35

9

33

10

Pointer to root with minimal key

Fibonacci Heaps

A list of heap-ordered trees

29 59 87 19

25

Not all trees may appear in Fibonacci heaps

22

Page 61: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

61

Fibonacci heap representation

45

67

40

58

20

15

35

9

33

23

4 pointers + rank + mark bit per node

min

Q

0 1

3

2 0 0

1 0

0

0

Doubly linked lists of children

Arbitrary order of children

child points to an arbitrary child

Explicit ranks = degrees

Page 62: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

62

Fibonacci heap representation

45

67

40

58

20

15

35

9

33

23

4 pointers + rank + mark bit per node

x

info

rank

child next prev

parent

mark

key

min

Q

0 1

3

2 0 0

1 0

0

0

Page 63: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

63

Are simple cuts enough?

Ranks not necessarily O(log n)

A binomial tree of rank k

contains at least 2k

We may get trees of rank k

containing only k+1 nodes

Analysis breaks down

Page 64: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

64

Cascading cuts

Invariant: Each node looses at most

one child after becoming a child itself

To maintain the invariant, use a mark bit

Each node is initially unmarked.

When a non-root node looses its first child,

it becomes marked

When a marked node looses a second child,

it is cut from its parent

Page 65: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

65

Cascading cuts

Invariant: Each node looses at most

one child after becoming a child itself

When xy is cut:

x becomes unmarked

If y is unmarked, it becomes marked

If y is marked, it is cut from its parent

Our convention: Roots are unmarked

Page 66: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

66

Cascading cuts

Cut x from its parent y

Perform a cascading-cut

process starting at x

Page 67: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

67

Cascading cuts

Page 68: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

68

Cascading cuts

… …

Page 69: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

69

Cascading cuts

… …

Page 70: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

70

Cascading cuts

… …

Page 71: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

71

Cascading cuts

… …

Page 72: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

72

Cascading cuts

… …

Page 73: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

73

Cascading cuts

… …

Page 74: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

74

Cascading cuts

… …

Page 75: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

75

Cascading cuts

… …

Page 76: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

76

Number of cuts

A decrease-key operation may trigger many cuts

Proof in a nutshell:

Number of times a second child is lost is at most

the number of times a first child is lost

Lemma 1: The first d decrease-key

operations trigger at most 2d cuts

Potential = Number of marked nodes

Page 77: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Number of cuts

Potential = Number of marked nodes

Amortized

number of cuts ≤ c + (1(c1)) = 2

Page 78: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

78

Trees formed by cascading cuts

Lemma 2: Let x be a node of rank k and

let y1,y2,…,yk be the current children of x,

in the order in which they were linked to x.

Then, the rank of yi is at least i2.

Proof: When yi was linked to x,

y1,…yi1 were already children of x.

At that time, the rank of x and yi was at least i1.

As yi is still a child of x, it lost at most one child.

Page 79: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

79

Lemma 3: A node of rank k in a Fibonacci Heap

has at least Fk+2 ≥ k descendants, including itself.

n 0 1 2 3 4 5 6 7 8 9

Fn 0 1 1 2 3 5 8 13 21 34

Trees formed by cascading cuts

Page 80: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

80

Lemma 3: A node of rank k in a Fibonacci Heap

has at least Fk+2 ≥ k descendants, including itself.

Let Sk be the minimum

number of descendants of a

node of rank at least k

k

k3 k2

0 0 1

Trees formed by cascading cuts

Page 81: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

81

Corollary: In a Fibonacci heap containing n items,

all ranks are at most logn ≤ 1.4404 log2n

Lemma 3: A node of rank k in a Fibonacci Heap

has at least Fk+2 ≥ k descendants, including itself.

Ranks are again O(log n)

Are we done?

Trees formed by cascading cuts

Page 82: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

82

A cut increases the number of trees…

We need a potential function that gives good

amortized bounds on both successive linking

and cascading cuts

Putting it all together

Are we done?

Potential = #trees + 2 #marked

Page 83: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Cost of Consolidating

T0 – Number of trees before

L – Number of links

T1 – Number of trees after

T1 = T0−L (Each link reduces the number of tree by 1)

Total number of trees processed – T0+L

(Each link creates a new tree)

Total cost = O( (T0+L) + L + logn )

= O( T0+ logn )

Putting trees into buckets

or finding trees to link with Linking

Handling

the buckets

As L ≤ T0

Page 84: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Cost of Consolidating

T0 – Number of trees before

L – Number of links

T1 – Number of trees after

T1 = T0−L (Each link reduces the number of tree by 1)

Total number of trees processed – T0+L

(Each link creates a new tree)

Total cost = O( (T0+L) + L + logn )

= O( T0+ logn )

Only change:

logn instead of log2n

As L ≤ T0

Page 85: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Amortized

cost Marks Trees Actual cost

O(1) 0 1 O(1) Insert

O(1) 0 0 O(1) Find-min

O(log n) ≤ 0 k1+T1−T0 O(k+T0+log n) Delete-min

O(1) ≤ 2c c O(c) Decrease-

key

O(1) 0 0 O(1) Meld

Fibonacci heaps

Rank of

deleted root Number of cuts

performed

Page 86: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Heaps / Priority queues

Worst case Amortized

Fibonacci

Heaps

Lazy

Binomial

Heaps

Binomial

Heaps

Binary

Heaps

O(1) O(1) O(log n) O(log n) Insert

O(1) O(1) O(1) O(1) Find-min

O(log n) O(log n) O(log n) O(log n) Delete-min

O(1) O(log n) O(log n) O(log n) Decrease-key

O(1) O(1) O(log n) – Meld

Page 87: Data Structures Binomial Heaps Fibonacci HeapsFibonacci Heaps A list of heap-ordered trees 29 59 87 19 25 Not all trees may appear in Fibonacci heaps 22 61 Fibonacci heap representation

Consolidating / Successive linking