December 2014. Binomial Heaps. Fibonacci Heaps. 1. 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. ID: 757806

- Views :
**1**

**Direct Link:**- Link:https://www.docslides.com/liane-varnes/data-structures-haim-kaplan-amp-uri-zwick
**Embed code:**

Download this presentation

DownloadNote - The PPT/PDF document "Data Structures Haim Kaplan & Uri Zw..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Slide1

Data Structures

Haim Kaplan & Uri ZwickDecember 2014

Binomial HeapsFibonacci Heaps

1

Slide2Fibonacci Heaps

Lazy Binomial HeapsBinomial HeapsBinary HeapsO(1)

O(1)

O(log

n)O(log n)InsertO(1)O(1)O(1)O(1)Find-minO(log n)O(log n)O(log n)O(log n)Delete-minO(1)O(log n)O(log n)O(log n)Decrease-keyO(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

Slide3Binomial Heaps[

Vuillemin (1978)]

3

Slide44

Binomial Trees

B

0

B

1

B

2

B

3

B

4

Slide55

Binomial Trees

B

0

B

1

B

2

B

3

B

4

Slide66

Binomial Trees

B

0

B

1

B

2

B

3

Bk

B

k−

1

B

k−

1

B

k−

1

B

k−

2

…

B

k

Slide77

Binomial TreesB0Bk

B

k−

1Bk−1Bk−1Bk−2…

B

k

Slide88

Min-heap Ordered Binomial Trees45

67

40

58203115351845511

17

38

2

25

key of child

key of parent

Slide99

Tournaments

Binomial Trees

GHFEABDC

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.

Slide10Binomial Heap

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

67

40

582031153593323Pointer to root with minimal key

Each number n

can be written in a unique way as a sum of powers of 211 = (1011)

2 = 8+2+1

At most log2 (n

+1) trees

Slide1111

Ordered forest Binary tree

45

67

405820311535933

23

next

–

next “sibling”

info

key

child

next

x

2 pointers per node

child –

leftmost child

Slide12F

orest

Binary tree

45674058203115359

33

23

40

45

20

67

58

31

35

9

15

33

23

Heap order

“half ordered”

Slide13Binomial heap representation

4567

40

58

2031153593323

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

?

Slide14Alternative representation

4567

40

58

2031153593323

n

last

Reverse sibling pointers

Make lists circular

Q

min

Avoids reversals during meld

“Structure R”

[Brown

(1978

)]

info

key

child

next

Slide1515Bk

aBk−

1

b

Bk−1Linking binomial treesa ≤ bO(1) timexy

Slide1616

Linking in first representationLinking in second representation

Linking

binomial trees

Slide17Melding

binomial heaps

Q

1

:

Q

2

:

Link trees

of same degree

Slide1818Q

1:B0B1B3Q2:

B0

B

2B3Melding binomial heapsLink trees of same degreeB1B2B3B3B4Like adding binary numbersO(log n) timeMaintain a pointer to the minimum

Slide1919

Insert

45

67

405820319351533

23

New item is a one tree binomial heap

Meld

it to the original heapO(log

n) time

11

Slide2020

Delete-min4567

40

58

2031935153323When we delete

the minimum, we get a binomial heap

Slide2121

Delete-min

45

67

4058203135153323When we delete the minimum, we get a binomial heapMeld it to the original heapO(log

n) time(Need to reverse list of

roots in first representation)

Slide2222

Decrease-key using “sift-up”4567

40

58

203115351845511

17

38

2

25

I

Decrease-key(

Q,I,

7)

Slide2323

4567

40

58

20715351845511

17

38

2

25

Decrease-key using “sift-up”

I

Need

parent

pointers

(not needed before)

Slide2424

4567

40

58

72015351845511

17

38

2

25

Decrease-key using “sift-up”

I

Need to update the node pointed by

I

Slide2525

4567

40

58

72015351845511

17

38

2

25

Decrease-key using “sift-up”

I

Need to update the node pointed by

I

Slide2645

67

40

58

1520735184551117

38

2

25

Decrease-key using “sift-up”

I

How can we do it?

Slide2727

Adding parent pointers45

67

40

582031153593323

n

first

Q

min

Slide28item

child

next

x

parent28Adding a level of indirection45674058203115359

33

23

node

info

key

I

Nodes and items

are

distinct

entities

n

first

Q

min

“Endogenous vs. exogenous”

Slide29Heaps / Priority queues

Worst caseAmortized

Fibonacci Heaps

Lazy Binomial Heaps

Binomial HeapsBinary HeapsO(1)O(1)O(log n)O(log n)InsertO(1)O(1)O(1)O(1)Find-minO(log n)O(log n)

O(log n

)

O(log n)

Delete-minO(1)

O(log n)

O(log n)

O(log

n

)

Decrease-key

O(1)

O(1)

O(log

n

)

–

Meld

Slide30Lazy Binomial Heaps

30

Slide31Binomial 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

Slide3232

4567

40

58

2031153593310Pointer to root with minimal keyLazy Binomial Heaps

An arbitrary list of binomial trees

29

59

87

19

20

Slide3333Update the pointer to root with minimal key

Lazy MeldConcatenate the two lists of treesO(1) worst case timeUpdate the pointer to root with minimal key

Lazy Insert

Add the new item to the list of roots

O(1) worst case time

Slide34May need (n

) time to find the new minimumLazy Delete-min ?Remove the minimum root and meld ?

45

67

40582031935153310

29

59

19

20

Slide35Consolidating / Successive Linking

4567

40

58

20313515331029591920

0

1

2

…

3

Slide3645

67

40

58

20313515331029591920

0

1

2

…

3

Consolidating / Successive Linking

Slide3745

67

40

58

2031351533591920

0

1

2

…

3

10

29

Consolidating / Successive Linking

Slide3845

67

40

58

203135591920

01

2

…3

15

33

10

29

Consolidating / Successive Linking

Slide3945

67

40

58

203135591920

01

2

…3

15

33

10

29

Consolidating / Successive Linking

Slide4020

31

35

59

1920012…3456740

58

15

33

10

29

Consolidating / Successive Linking

Slide4120

31

35

59

1920012…3456740

58

15

33

10

29

Consolidating / Successive Linking

Slide4220

31

19

20

012…345674058

15

33

10

29

35

59

Consolidating / Successive Linking

Slide4319

20

0

1

2…3456740581533

10

29

35

59

20

31

Consolidating / Successive Linking

Slide4419

20

0

1

2…3456740581533

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

Slide45At the end of the process, we obtain a non-lazy binomial heap containing at most log n

trees,at most one of each degreeWorst case cost – O(n)Amortized cost – O(log n)Potential = Number of Trees

Consolidating / Successive Linking

Slide46Cost of Consolidating

T0 – Number of trees beforeL – Number of linksT1 – Number of trees afterT1 =

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 bucketsor finding trees to link withLinkingHandling the bucketsAs L ≤ T0

Slide47Amortized

Cost of Consolidating(Scaled) actual cost = T0 + log2n Change in potential = = T

1−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.

Slide48Amortized cost

Change in potentialActual costO(1)1O(1)

InsertO(1)

0

O(1)Find-minO(log n)T1−T0O(k+T0+log n)Delete-minO(log n)0O(log n)Decrease-keyO(1)0O(1)MeldLazy Binomial HeapsRank ofdeleted root

Slide49Heaps / Priority queues

Worst caseAmortized

Fibonacci Heaps

Lazy Binomial Heaps

Binomial HeapsBinary HeapsO(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

Slide50One-pass successive linking

A tree produced by a link is immediately put in the output list and not linked againWorst case cost – O(n)Amortized cost – O(log n)

Potential = Number of TreesExercise: Prove it!

Slide5145

67

40

58

20313515331029591920

0

1

2

…

3

One-pass successive Linking

Slide5245

67

40

58

20313515331029591920

0

1

2

…

3

One-pass successive Linking

Slide5345

67

40

58

2031351533591920

0

1

2

…

3

One-pass successive Linking

10

29

Output list:

Slide54Fibonacci Heaps[

Fredman-Tarjan (1987)]

54

Slide5555

Decrease-key in O(1) time?45

67

40

58203115351845511

17

38

2

25

x

Decrease-key(

Q,x,

30)

Slide5656

4567

30

58

203115351845511

17

38

2

25

x

Decrease-key(

Q,x,

10)

Decrease-key

in O(1) time?

Slide5757

4567

10

58

203115351845511

17

38

2

25

x

Heap order violation

Decrease-key

in O(1) time?

Slide5858

4567

10

58

203115351845511

17

38

2

25

x

Cut

the edge

Decrease-key

in O(1) time?

Slide5959

4567

10

58

20311535184551117

38

2

25

x

Tree no longer

binomial

Decrease-key

in O(1) time?

Slide6060

4058

20

31

153593310Pointer to root with minimal keyFibonacci HeapsA list of heap-ordered trees2959

87

19

25

Not

all trees may appear in Fibonacci heaps

22

Slide6161

Fibonacci heap representation

45

67

405820153593323

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

Slide6262

Fibonacci heap representation

45

67

405820153593323

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

Slide6363

Are simple cuts enough?Ranks not necessarily O(log n)

A binomial tree of rank k

contains at least

2kWe may get trees of rank k containing only k+1 nodesAnalysis breaks down

Slide6464

Cascading cutsInvariant: Each node looses at mostone child after becoming a child itselfTo maintain the invariant, use a

mark bitEach node is initially

unmarked

.When a non-root node looses its first child, it becomes markedWhen a marked node looses a second child,it is cut from its parent

Slide6565

Cascading cutsInvariant: Each node looses at mostone child after becoming a child itselfWhen

xy is cut:x becomes unmarked

If

y is unmarked, it becomes markedIf y is marked, cut y y.parentOur convention: Roots are unmarked

Slide6666

Cascading cuts

Cut

x

from its parent yPerform a cascading-cut process starting at x

Slide6767

Cascading cuts

…

…

…

Slide6868

Cascading cuts

…

…

…

Slide6969

Cascading cuts

…

…

…

Slide7070

Cascading cuts

…

…

…

Slide7171

Cascading cuts

…

…

…

Slide7272

Cascading cuts

…

…

…

Slide7373

Cascading cuts

…

…

…

Slide7474

Cascading cuts

…

…

…

Slide7575

Cascading cuts

…

…

Slide7676

Number of cutsA decrease-key operation may trigger many cutsProof 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-keyoperations trigger at most 2d cutsPotential = Number of marked nodes

Slide77Number of cuts

Potential = Number of marked nodes

c

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

Slide7878

Trees formed by cascading cutsLemma 2: Let x be a node of rank k andlet

y1,

y

2,…,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.

Slide7979

Lemma 3: A node of rank k in a Fibonacci Heaphas at least Fk+2 ≥ k

descendants, including itself.

n

0123456789

Fn

0

1

1

2

3

5

8

13

21

34

Trees formed by cascading cuts

Slide8080

Lemma 3: A node of rank k in a Fibonacci Heap has at least Fk+2 ≥ k descendants, including itself.

Let

S

k be the minimum number of descendants of a node of rank at least k…kk3k2

00

1

Trees formed by cascading cuts

Slide8181

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

Slide8282

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

Slide83Cost of Consolidating

T0 – Number of trees beforeL – Number of linksT1 – Number of trees afterT1 =

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 bucketsor finding trees to link withLinkingHandling the bucketsAs L ≤ T0

Slide84Cost of Consolidating

T0 – Number of trees beforeL – Number of linksT1 – Number of trees afterT1 =

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 log2nAs L ≤ T0

Slide85Amortized cost

Marks TreesActual cost

O(1)0

1

O(1)InsertO(1)00O(1)Find-minO(log n)≤ 0T1−T0O(k+T0+log n)Delete-minO(1)≤ 2ccO(c)Decrease-keyO(1)00O(1)MeldFibonacci heaps

Rank ofdeleted root

Number of cuts performed

Slide86Heaps / Priority queues

Worst caseAmortized

Fibonacci Heaps

Lazy Binomial Heaps

Binomial HeapsBinary HeapsO(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

Slide87Consolidating / Successive linking

Next Slides