fibonacci heaps - southeast university€¦ · •thenode points to the fibonacci heap node that...

23
Fibonacci Heaps Actual Amortized Insert O(1) O(1) Remove min (or max) O(n) O(log n) Meld O(1) O(1) Remove O(n) O(log n) Decrease key (or increase) O(n) O(1)

Upload: others

Post on 09-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Fibonacci Heaps

Actual Amortized Insert O(1) O(1)

Remove min (or max) O(n) O(log n)

Meld O(1) O(1)

Remove O(n) O(log n)

Decrease key (or increase)

O(n) O(1)

Page 2: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Analysis

• FibonacciAnalysis.ppt• Video

§ www.cise.ufl.edu/~sahni/cop5536; Internet Lectures; not registered

§ COP5536_FHA.rm

Page 3: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Single Source All Destinations Shortest Paths

1

2

3

4

5

6

7

2

616 7

8

103

14

44

5 3

1

Page 4: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Greedy Single Source All Destinations

• Known as Dijkstra’s algorithm.• Let d(i) be the length of a shortest one edge

extension of an already generated shortest path, the one edge extension ends at vertex i.

• The next shortest path is to an as yet unreached vertex for which the d() value is least.

• After the next shortest path is generated, some d() values are updated (decreased).

Page 5: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Greedy Single Source All Destinations

1

2

3

4

5

6

7

2

616 7

8

103

14

44

5 3

1

Path Length1 01 3 2

1 3 55

1 2 6

1 3 95 4

1 3 106

1 3 116 7

Page 6: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Operations On d()• Remove min.

§ Done O(n) times, where n is the number of vertices in the graph.

• Decrease d().§ Done O(e) times, where e is the number of edges in

the graph.• Array.

§ O(n2) overall complexity.• Min heap.

§ O(nlog n + elog n) overall complexity.• Fibonacci heap.

§ O(nlog n + e) overall complexity.

Page 7: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Prim’s Min-Cost Spanning Tree Algorithm

• Array.§ O(n2) overall complexity.

• Min heap.§ O(nlog n + elog n) overall complexity.

• Fibonacci heap.§ O(nlog n + e) overall complexity.

Page 8: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Min Fibonacci Heap

• Collection of min trees.• The min trees need not be Binomial trees.

Page 9: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Node Structure• Degree, Child, Data• Left and Right Sibling

§ Used for circular doubly linked list of siblings.• Parent

§ Pointer to parent node.• ChildCut

§ True if node has lost a child since it became a child of its current parent.

§ Set to false by remove min, which is the only operation that makes one node a child of another.

§ Undefined for a root node.

Page 10: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Fibonacci Heap Representation

• Degree, Parent and ChildCut fields not shown.

6

4

9

58

7 3

1

9

5

6

5 9

2

8

6 7

4

A

Page 11: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Remove(theNode)

• theNode points to the Fibonacci heap node that contains the element that is to be removed.

• theNode points to min element => do a remove min.§ In this case, complexity is the same as that for

remove min.

Page 12: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Remove(theNode)

• theNode points to an element other than the min element. § Remove theNode from its doubly linked sibling list.§ Change parent’s child pointer if necessary.§ Set parent field of theNode’s children to null.§ Combine top-level list and children list of theNode; do not

pairwise combine equal degree trees.§ Free theNode.

• In this case, actual complexity is O(log n) (assuming theNode has O(log n) children).

Page 13: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Remove(theNode)

Remove theNode from its doubly linked sibling list.

8

7 3

1

6

5 9

2

8

6 7

4 10

4

9

5

theNode

6

9

5

Page 14: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Remove(theNode)

Combine top-level list and children of theNode setting parent pointers of the children of theNode to null.

8

7 3

1

6

5 9

2

8

6 7

4

10

4

9

5

6

9

5

Page 15: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Remove(theNode)

8

7 3

1

6

5 9

2

8

6 7

4

10

9

5 6

9

5

Page 16: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

DecreaseKey(theNode, theAmount)

If theNode is not a root and new key < parent key, remove subtree rooted at theNode from its doubly linked sibling list.

Insert into top-level list.

8

7 3

1

6

5 9

2

8

6 7

4 10

4

9

5

theNode

6

9

5

Page 17: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

DecreaseKey(theNode, theAmount)

10

0

9

5

8

7 3

1

6

5 9

2

8

6 7

4

6

9

5

Update heap pointer if necessary

Page 18: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Cascading Cut

• When theNode is cut out of its sibling list in a remove or decrease key operation, follow path from parent of theNode to the root.

• Encountered nodes (other than root) with ChildCut = true are cut from their sibling lists and inserted into top-level list.

• Stop at first node with ChildCut = false.• For this node, set ChildCut = true.

Page 19: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Cascading Cut Example

8

7 3

1

6

5 9

2

8

6 7

4

9

9 8

theNode

T

T

F

Decrease key by 2.

Page 20: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Cascading Cut Example

8

7 3

1

6

5 9

2

6 7

4

6

9

9 8

T

T

F

Page 21: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Cascading Cut Example

8

7 3

1

6

5 9

2

6

7

4

6

9

9 8

T

F

Page 22: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Cascading Cut Example

8

7 3

1

6

5 9

2 6

7 46

9

9 8F

Page 23: Fibonacci Heaps - Southeast University€¦ · •theNode points to the Fibonacci heap node that contains the element that is to be removed. •theNodepoints to min element =>do a

Cascading Cut Example

8

7 3

1

6

5 9

2 6

7 46

9

9 8T

Actual complexity of cascading cut is O(h) = O(n).