/
Data Structures Haim Kaplan & Uri Zwick Data Structures Haim Kaplan & Uri Zwick

Data Structures Haim Kaplan & Uri Zwick - PowerPoint Presentation

liane-varnes
liane-varnes . @liane-varnes
Follow
369 views
Uploaded On 2019-03-19

Data Structures Haim Kaplan & Uri Zwick - PPT Presentation

December 2014 Binomial Heaps Fibonacci Heaps 1 Fibonacci Heaps Lazy Binomial Heaps Binomial Heaps Binary Heaps O1 O1 Olog n Olog n Insert O1 O1 O1 O1 Findmin ID: 757806

trees log key binomial log trees binomial key number decrease heaps child linking cascading successive rank cuts node cost

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation 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.


Presentation Transcript

Slide1

Data Structures

Haim Kaplan & Uri ZwickDecember 2014

Binomial HeapsFibonacci Heaps

1Slide2

Fibonacci 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

PrimSlide3

Binomial Heaps[

Vuillemin (1978)]

3Slide4

4

Binomial Trees

B

0

B

1

B

2

B

3

B

4Slide5

5

Binomial Trees

B

0

B

1

B

2

B

3

B

4Slide6

6

Binomial Trees

B

0

B

1

B

2

B

3

Bk

B

k−

1

B

k−

1

B

k−

1

B

k−

2

B

kSlide7

7

Binomial TreesB0Bk

B

k−

1Bk−1Bk−1Bk−2…

B

kSlide8

8

Min-heap Ordered Binomial Trees45

67

40

58203115351845511

17

38

2

25

key of child

key of parentSlide9

9

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. Slide10

Binomial 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 2

11 = (1011)

2 = 8+2+1At most

log2 (n

+1) treesSlide11

11

Ordered forest  Binary tree

45

67

405820311535933

23

next

next “sibling”

info

key

child

next

x

2 pointers per node

child –

leftmost childSlide12

F

orest

 Binary tree

45674058203115359

33

23

40

45

20

67

58

31

35

9

15

33

23

Heap order

“half ordered”Slide13

Binomial 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

?Slide14

Alternative 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

nextSlide15

15Bk

aBk−

1

b

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

16

Linking in first representationLinking in second representation

Linking

binomial treesSlide17

Melding

binomial heaps

Q

1

:

Q

2

:

Link trees

of same degreeSlide18

18Q

1:B0B1B3Q2:

B0

B

2B3Melding binomial heapsLink trees of same degreeB1B2B3B3B4Like adding binary numbersO(log n) timeMaintain a pointer to the minimumSlide19

19

Insert

45

67

405820319351533

23

New item is a one tree binomial heap

Meld

it to the original heapO(log

n) time

11Slide20

20

Delete-min4567

40

58

2031935153323When we delete the

minimum, we get a binomial heapSlide21

21

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)Slide22

22

Decrease-key using “sift-up”4567

40

58

203115351845511

17

38

2

25

I

Decrease-key(

Q,I,

7)Slide23

23

4567

40

58

20715351845511

17

38

2

25

Decrease-key using “sift-up”

I

Need

parent

pointers

(not needed before)Slide24

24

4567

40

58

72015351845511

17

38

2

25

Decrease-key using “sift-up”

I

Need to update the node pointed by

ISlide25

25

4567

40

58

72015351845511

17

38

2

25

Decrease-key using “sift-up”

I

Need to update the node pointed by

ISlide26

45

67

40

58

1520735184551117

38

2

25

Decrease-key using “sift-up”

I

How can we do it?Slide27

27

Adding parent pointers45

67

40

582031153593323

n

first

Q

minSlide28

item

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”Slide29

Heaps / 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

)

MeldSlide30

Lazy Binomial Heaps

30Slide31

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 keySlide32

32

4567

40

58

2031153593310Pointer to root with minimal keyLazy Binomial Heaps

An arbitrary list of binomial trees

29

59

87

19

20Slide33

33Update 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 timeSlide34

May need (n

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

45

67

40582031935153310

29

59

19

20Slide35

Consolidating / Successive Linking

4567

40

58

20313515331029591920

0

1

2

3Slide36

45

67

40

58

20313515331029591920

0

1

2

3

Consolidating / Successive LinkingSlide37

45

67

40

58

2031351533591920

0

1

2

3

10

29

Consolidating / Successive LinkingSlide38

45

67

40

58

203135591920

01

2…

3

15

33

10

29

Consolidating / Successive LinkingSlide39

45

67

40

58

203135591920

01

2…

3

15

33

10

29

Consolidating / Successive LinkingSlide40

20

31

35

59

1920012…3456740

58

15

33

10

29

Consolidating / Successive LinkingSlide41

20

31

35

59

1920012…3456740

58

15

33

10

29

Consolidating / Successive LinkingSlide42

20

31

19

20

012…345674058

15

33

10

29

35

59

Consolidating / Successive LinkingSlide43

19

20

0

1

2…3456740581533

10

29

35

59

20

31

Consolidating / Successive LinkingSlide44

19

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 LinkingSlide45

At 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 LinkingSlide46

Cost 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 Slide47

Amortized

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.Slide48

Amortized 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 rootSlide49

Heaps / 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

)

MeldSlide50

One-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!Slide51

45

67

40

58

20313515331029591920

0

1

2

3

One-pass successive LinkingSlide52

45

67

40

58

20313515331029591920

0

1

2

3

One-pass successive LinkingSlide53

45

67

40

58

2031351533591920

0

1

2

3

One-pass successive Linking

10

29

Output list:Slide54

Fibonacci Heaps[

Fredman-Tarjan (1987)]

54Slide55

55

Decrease-key in O(1) time?45

67

40

58203115351845511

17

38

2

25

x

Decrease-key(

Q,x,

30)Slide56

56

4567

30

58

203115351845511

17

38

2

25

x

Decrease-key(

Q,x,

10)

Decrease-key

in O(1) time?Slide57

57

4567

10

58

203115351845511

17

38

2

25

x

Heap order violation

Decrease-key

in O(1) time?Slide58

58

4567

10

58

203115351845511

17

38

2

25

x

Cut

the edge

Decrease-key

in O(1) time?Slide59

59

4567

10

58

20311535184551117

38

2

25

x

Tree no longer

binomial

Decrease-key

in O(1) time?Slide60

60

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

22Slide61

61

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

= degreesSlide62

62

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

0Slide63

63

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 downSlide64

64

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 parentSlide65

65

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

xy is cut:x becomes unmarked

If

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

66

Cascading cuts

Cut

x

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

67

Cascading cuts

…Slide68

68

Cascading cuts

…Slide69

69

Cascading cuts

…Slide70

70

Cascading cuts

…Slide71

71

Cascading cuts

…Slide72

72

Cascading cuts

…Slide73

73

Cascading cuts

…Slide74

74

Cascading cuts

…Slide75

75

Cascading cuts

…Slide76

76

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 nodesSlide77

Number of cuts

Potential = Number of marked nodes

c

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

78

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 i2.Proof: When yi was linked to x, y1,…yi1 were already children of x. At that time, the rank of x and yi was at least i1.As yi is still a child of x, it lost at most one child.Slide79

79

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

descendants, including itself.

n

0123456789F

n

0

1

1

2

3

5

8

13

21

34

Trees formed by cascading cutsSlide80

80

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…kk3k2

00

1

Trees formed by cascading cutsSlide81

81

Corollary: In a Fibonacci heap containing n items, all ranks are at most logn ≤ 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 cutsSlide82

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 #markedSlide83

Cost 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 + logn ) = O( T0+ logn ) Putting trees into bucketsor finding trees to link withLinkingHandling the bucketsAs L ≤ T0 Slide84

Cost 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 + logn ) = O( T0+ logn ) Only change:logn instead of log2nAs L ≤ T0 Slide85

Amortized 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)≤ 2ccO(c)Decrease-keyO(1)00O(1)MeldFibonacci heaps

Rank ofdeleted root

Number of cuts performedSlide86

Heaps / 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

)

MeldSlide87

Consolidating / Successive linking