Introduction Fibonacci Heap Time Complexity Analysisdominik/teaching/2016-cs214/...2016/12/08  ·...

Preview:

Citation preview

IntroductionFibonacci Heap

Time Complexity Analysis

Fibonacci Heap

Group Minus One Second

December 6, 2016

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Outline

IntroductionMotivationPerformance Goal

Fibonacci HeapStructureRankInsertDelete MinDecrease Key

Time Complexity AnalysisAmortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

MotivationPerformance Goal

Motivation

I Design a data structure that supports the following operationsI Insert(x)I DeleteMin()I DecreaseKey(u, v)

12

57

3

15

2 9

21

30

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

MotivationPerformance Goal

Motivation

I Design a data structure that supports the following operationsI Insert(x)I DeleteMin()I DecreaseKey(u, v)

12

57

3

15

2 9

21

30

Insert(10)

10

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

MotivationPerformance Goal

Motivation

I Design a data structure that supports the following operationsI Insert(x)I DeleteMin()I DecreaseKey(u, v)

12

57

3

15

2 9

21

30

DeleteMin()

10

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

MotivationPerformance Goal

Motivation

I Design a data structure that supports the following operationsI Insert(x)I DeleteMin()I DecreaseKey(u, v)

12

57

3

15

9

21

30

DecreaseKey(21, 17)

10

17

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

MotivationPerformance Goal

Performance Goal

I Our performance goals of this data structureI Insert(x): O(1)I DeleteMin(): O(log n)I DecreaseKey(u, v): O(1)

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Structure

I Fibonacci heap is essentially a set of heap-ordered trees.

17

24

30

26

46

35

23

7 3

18

52 41

44

39

roots

heap-ordered tree

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Rank

I The rank of a tree in a Fibonacci heap is the number ofchildren of the root.

17 24

30 26 46

35

23 7 3

18 52 41

4439

rank = 1 rank = 2 rank = 0 rank = 0 rank = 3

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Insert

I To insert a new value x into Fibonacci Heap H, simply createa new tree that contains only x and add it to H.

I Example: Insert(21)

17 24

30 26 46

35

23 7 3

18 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Insert

I To insert a new value x into Fibonacci Heap H, simply createa new tree that contains only x and add it to H.

I Example: Insert(21)

17 24

30 26 46

35

23 7 3

18 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Delete Min

I The minimum must be one of the roots.I But there can be too many(⌦(n)) roots!I Example: DeleteMin() for the heap below

17 24 23 7 32146

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Delete Min

I We introduce consolidation, a process of merging trees of thesame rank.

17 24

30 26 46

35

23 7 3

18 52 41

4439

21

merge

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Delete Min

I We introduce consolidation, a process of merging trees of thesame rank.

17 24

30 26 46

35

23

7 3

18 52 41

4439

21

merge

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Delete Min

I We introduce consolidation, a process of merging trees of thesame rank.

17

24

30

26 46

35

23

7 3

18 52 41

4439

21

merge

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Delete Min

I We introduce consolidation, a process of merging trees of thesame rank.

17 24

30 26 46

35

23

7 3

18 52 41

4439

21

merge

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Delete Min

I We introduce consolidation, a process of merging trees of thesame rank.

17 24

30 26 46

35

23

7

3

18 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Delete Min

I After consolidation, we simply remove the minimum value andleave the remaining trees in the heap.

17 24

30 26 46

35

23

718 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key

I Case 1: we can finish the operation immediatelyI Example: decreaseKey(46, 36)

17 24

30 26 46

35

23

718 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key

I Case 1: we can finish the operation immediatelyI Example: decreaseKey(46, 36)

17 24

30 26 36

35

23

718 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key

I Case 2: we have to cut the whole subtree rooted at uI Example: DecreaseKey(26, 16)

17 24

30 26 46

35

23

718 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key

I Case 2: we have to cut the whole subtree rooted at uI Example: DecreaseKey(26, 16)

17 24

30 16 46

35

23

718 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key

I Case 2: we have to cut the whole subtree rooted at uI Example: DecreaseKey(26, 16)

17 24

30

16

46

3523

718 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key

I But wait! Under this strategy, something undesirable mighthappen.

I The example below shows that this strategy can hurt theperformance of DeleteMin operation.

I Example:

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

One node is marked if it has lost one of its children.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 1: no violation of heap property, finish.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 2: simply remove the subtree and mark the parent node.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 2: simply remove the subtree and mark the parent node.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 2: simply remove the subtree and mark the parent node.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 3: recursively remove all subtrees whose parent is marked.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 3: recursively remove all subtrees whose parent is marked.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 3: recursively remove all subtrees whose parent is marked.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 3: recursively remove all subtrees whose parent is marked.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

StructureRankInsertDelete MinDecrease Key

Decrease Key with Marks

Case 3: recursively remove all subtrees whose parent is marked.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Amortized Analysis

I We need a new technique to analyze the time complexity ofFibonacci heap

I Consider the following question:I Drop: Dropping a single coin onto a table costs 1 unit of timeI Clean: Collecting each coin into a bag costs 1 unit of time.

Emptying the bag costs 1 unit of time.I Question: What is the amortized time complexity of Clean

operation?

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Amortized Analysis

I Observation: A coin that is cleaned must have been droppedbefore.

I So why not prepay the cost of clean of each coin as the costof drop?

I Drop: Dropping a single coin onto a table costs 2 units oftime

I Clean: Collecting each coin into a bag costs no time.Emptying the bag costs 1 unit of time.

I The amortized time complexity of Clean operation is O(1).

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Insert Complexity

I In addition to the cost of creating a tree($1), we also add amerge credit (a prepaid constant cost,$1) to each new tree.

I Since no other operations are needed, each Insert operationstill takes $2 = O(1) time.

17 24 23 7 32146

$1 $1 $1 $1 $1 $1 $1

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

I Recall that DeleteMin opeartion consists of two parts:I Consolidate the heap: for each rank i 2 [0, k], merge two

trees of the same rank i .I Linearly search for the minimum: traverse all k + 1 roots.I k is the largest rank in the heap.

I We will make and prove a few claims.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

I Claim 1: Merge operations are free.I Proof: Their costs are paid by merge credits.

17 46

$1 $1

17

46

$1

The other $ 1 is usd to pay for the cost of merge

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

I Claim 2: Consolidation operations are of O(k) where k is thelargest rank.

I Proof: For each rank i 2 [0, k], consolidation calls merge togenerate new trees, which is free. So the only cost is the timeconsumed while travsering k + 1 ranks.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

I Claim 3: Searching for minimum is of O(k) where k is thelargest rank.

I Proof: After consolidation there are at most k + 1 roots. Soa linear traversal will take O(k) time.

17 24

30 26 46

35

23

7

3

18 52 41

4439

21

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

I Combining all three claims, we can see that the complexity ofDeleteMin is O(k) where k is the largest rank.

I One last question: why k = O(log n)?I This is equivalent to ”what is the smallest number of nodes in

a tree of rank i?”

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

Some results for di↵erent ranks. Let Si be the smallest number ofnodes in a tree of rank i . We can see that

S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...

rank = 0

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

Some results for di↵erent ranks. Let Si be the smallest number ofnodes in a tree of rank i . We can see that

S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...

rank = 1

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

Some results for di↵erent ranks. Let Si be the smallest number ofnodes in a tree of rank i . We can see that

S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...

rank = 2

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

Some results for di↵erent ranks. Let Si be the smallest number ofnodes in a tree of rank i . We can see that

S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...

rank = 3

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

Some results for di↵erent ranks. Let Si be the smallest number ofnodes in a tree of rank i . We can see that

S0 = 1, S1 = 2, S2 = 3, S3 = 5, S4 = 8, ...

rank = 4

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Delete Min Complexity

I We can get a recurrence relation from the above observation:

Si = Si�1 + Si�2, S0 = 1, S1 = 2

which is exactly the Fibonacci sequence, and hence the heap’sname.

I Since Si � c i where c = 1+p5

2 , the number of nodes in eachtree grows expoentially with the tree’s rank.

I Therefore, there can be at most O(log n) ranks in a Fibonacciheap.

Group Minus One Second Fibonacci Heap

IntroductionFibonacci Heap

Time Complexity Analysis

Amortized AnalysisInsert ComplexityDelete Min ComplexityDecrease Key Complexity

Decrease Key Complexity

I By similar argument (using the amortized analysis), we canprove that the time complexity of DecreaseKey(u, v) isO(1).

I We will omit this part due to time limit.

I You can refer to the lecture note on the course website.

Group Minus One Second Fibonacci Heap

Q& A

Thankyou

Recommended