Top Banner
Analysis of Algorithms Fibonacci Heaps Andres Mendez-Vazquez October 29, 2015 1 / 120
259

16 Fibonacci Heaps

Apr 14, 2017

Download

Engineering

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 16 Fibonacci Heaps

Analysis of AlgorithmsFibonacci Heaps

Andres Mendez-Vazquez

October 29, 2015

1 / 120

Page 2: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

2 / 120

Page 3: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

3 / 120

Page 4: 16 Fibonacci Heaps

Some previous definitions

Free TreeA free tree is a connected acyclic undirected graph.

Rooted TreeA rooted tree is a free tree in which one of the nodes is a root.

Ordered TreeAn ordered tree is a rooted tree where the children are ordered.

4 / 120

Page 5: 16 Fibonacci Heaps

Some previous definitions

Free TreeA free tree is a connected acyclic undirected graph.

Rooted TreeA rooted tree is a free tree in which one of the nodes is a root.

Ordered TreeAn ordered tree is a rooted tree where the children are ordered.

4 / 120

Page 6: 16 Fibonacci Heaps

Some previous definitions

Free TreeA free tree is a connected acyclic undirected graph.

Rooted TreeA rooted tree is a free tree in which one of the nodes is a root.

Ordered TreeAn ordered tree is a rooted tree where the children are ordered.

4 / 120

Page 7: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

5 / 120

Page 8: 16 Fibonacci Heaps

Ordered Tree

DefinitionAn ordered tree is an oriented tree in which the children of a node aresomehow "ordered."

Example

ThusIf T1 and T2 are ordered trees then T1 6= T2 else T1 = T2.

6 / 120

Page 9: 16 Fibonacci Heaps

Ordered Tree

DefinitionAn ordered tree is an oriented tree in which the children of a node aresomehow "ordered."

Examplea

b c

a

bc

ThusIf T1 and T2 are ordered trees then T1 6= T2 else T1 = T2.

6 / 120

Page 10: 16 Fibonacci Heaps

Ordered Tree

DefinitionAn ordered tree is an oriented tree in which the children of a node aresomehow "ordered."

Examplea

b c

a

bc

ThusIf T1 and T2 are ordered trees then T1 6= T2 else T1 = T2.

6 / 120

Page 11: 16 Fibonacci Heaps

Some previous definitions

Types of Ordered TreesThere are several types of ordered trees:

k-ary treeBinomial treeFibonacci tree

Binomial TreeA binomial tree is an ordered tree defined recursively.

7 / 120

Page 12: 16 Fibonacci Heaps

Some previous definitions

Types of Ordered TreesThere are several types of ordered trees:

k-ary treeBinomial treeFibonacci tree

Binomial TreeA binomial tree is an ordered tree defined recursively.

7 / 120

Page 13: 16 Fibonacci Heaps

Some previous definitions

Types of Ordered TreesThere are several types of ordered trees:

k-ary treeBinomial treeFibonacci tree

Binomial TreeA binomial tree is an ordered tree defined recursively.

7 / 120

Page 14: 16 Fibonacci Heaps

Some previous definitions

Types of Ordered TreesThere are several types of ordered trees:

k-ary treeBinomial treeFibonacci tree

Binomial TreeA binomial tree is an ordered tree defined recursively.

7 / 120

Page 15: 16 Fibonacci Heaps

Some previous definitions

Types of Ordered TreesThere are several types of ordered trees:

k-ary treeBinomial treeFibonacci tree

Binomial TreeA binomial tree is an ordered tree defined recursively.

7 / 120

Page 16: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

8 / 120

Page 17: 16 Fibonacci Heaps

Examples

Recursive Structure

1

2

3

4

0

9 / 120

Page 18: 16 Fibonacci Heaps

This can be seen too as

Recursive Structure

10 / 120

Page 19: 16 Fibonacci Heaps

Properties of binomial trees

Lemma 19.1For the binomial tree Bk :

1 There are 2k nodes.2 The height of the tree is k.3 There are exactly

(ki)nodes at depth i for i = 0, 1, ..., k.

4 The root has degree k, which is greater than that of any other node;moreover if the children of the root are numbered from left to rightby k − 1, k − 2, ..., 0 child i is the root of a subtree Bi .

Proof!Look at the white-board.

11 / 120

Page 20: 16 Fibonacci Heaps

Properties of binomial trees

Lemma 19.1For the binomial tree Bk :

1 There are 2k nodes.2 The height of the tree is k.3 There are exactly

(ki)nodes at depth i for i = 0, 1, ..., k.

4 The root has degree k, which is greater than that of any other node;moreover if the children of the root are numbered from left to rightby k − 1, k − 2, ..., 0 child i is the root of a subtree Bi .

Proof!Look at the white-board.

11 / 120

Page 21: 16 Fibonacci Heaps

Properties of binomial trees

Lemma 19.1For the binomial tree Bk :

1 There are 2k nodes.2 The height of the tree is k.3 There are exactly

(ki)nodes at depth i for i = 0, 1, ..., k.

4 The root has degree k, which is greater than that of any other node;moreover if the children of the root are numbered from left to rightby k − 1, k − 2, ..., 0 child i is the root of a subtree Bi .

Proof!Look at the white-board.

11 / 120

Page 22: 16 Fibonacci Heaps

Properties of binomial trees

Lemma 19.1For the binomial tree Bk :

1 There are 2k nodes.2 The height of the tree is k.3 There are exactly

(ki)nodes at depth i for i = 0, 1, ..., k.

4 The root has degree k, which is greater than that of any other node;moreover if the children of the root are numbered from left to rightby k − 1, k − 2, ..., 0 child i is the root of a subtree Bi .

Proof!Look at the white-board.

11 / 120

Page 23: 16 Fibonacci Heaps

Properties of binomial trees

Lemma 19.1For the binomial tree Bk :

1 There are 2k nodes.2 The height of the tree is k.3 There are exactly

(ki)nodes at depth i for i = 0, 1, ..., k.

4 The root has degree k, which is greater than that of any other node;moreover if the children of the root are numbered from left to rightby k − 1, k − 2, ..., 0 child i is the root of a subtree Bi .

Proof!Look at the white-board.

11 / 120

Page 24: 16 Fibonacci Heaps

Properties of binomial trees

Lemma 19.1For the binomial tree Bk :

1 There are 2k nodes.2 The height of the tree is k.3 There are exactly

(ki)nodes at depth i for i = 0, 1, ..., k.

4 The root has degree k, which is greater than that of any other node;moreover if the children of the root are numbered from left to rightby k − 1, k − 2, ..., 0 child i is the root of a subtree Bi .

Proof!Look at the white-board.

11 / 120

Page 25: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

12 / 120

Page 26: 16 Fibonacci Heaps

Mergeable Heap Operations

The Fibonacci heap data structure is used to support the operations“meargeable heap” operations1. MAKE -HEAP() creates and returns a new heap containing no

elements.2. INSERT(H , x) inserts element x, whose key has already been filled in,

into heap H .3. MINIMUM(H ) returns a pointer to the element in heap H whose key

is minimum.4. EXTRACT-MIN(H ) deletes the element from heap H whose key is

minimum, returning a pointer to the element.

13 / 120

Page 27: 16 Fibonacci Heaps

Mergeable Heap Operations

The Fibonacci heap data structure is used to support the operations“meargeable heap” operations1. MAKE -HEAP() creates and returns a new heap containing no

elements.2. INSERT(H , x) inserts element x, whose key has already been filled in,

into heap H .3. MINIMUM(H ) returns a pointer to the element in heap H whose key

is minimum.4. EXTRACT-MIN(H ) deletes the element from heap H whose key is

minimum, returning a pointer to the element.

13 / 120

Page 28: 16 Fibonacci Heaps

Mergeable Heap Operations

The Fibonacci heap data structure is used to support the operations“meargeable heap” operations1. MAKE -HEAP() creates and returns a new heap containing no

elements.2. INSERT(H , x) inserts element x, whose key has already been filled in,

into heap H .3. MINIMUM(H ) returns a pointer to the element in heap H whose key

is minimum.4. EXTRACT-MIN(H ) deletes the element from heap H whose key is

minimum, returning a pointer to the element.

13 / 120

Page 29: 16 Fibonacci Heaps

Mergeable Heap Operations

The Fibonacci heap data structure is used to support the operations“meargeable heap” operations1. MAKE -HEAP() creates and returns a new heap containing no

elements.2. INSERT(H , x) inserts element x, whose key has already been filled in,

into heap H .3. MINIMUM(H ) returns a pointer to the element in heap H whose key

is minimum.4. EXTRACT-MIN(H ) deletes the element from heap H whose key is

minimum, returning a pointer to the element.

13 / 120

Page 30: 16 Fibonacci Heaps

Mergeable Heap Operations

The Fibonacci heap data structure is used to support the operations“meargeable heap” operations5. UNION(H1, H2) creates and returns a new heap that contains all the

elements of heaps H1 and H2. Heaps are “destroyed” by thisoperation.

6. DECREASE-KEY(H , x, k) assigns to element x within heap H thenew key value k.

7. DELETE(H , x) deletes element x from heap H .

14 / 120

Page 31: 16 Fibonacci Heaps

Mergeable Heap Operations

The Fibonacci heap data structure is used to support the operations“meargeable heap” operations5. UNION(H1, H2) creates and returns a new heap that contains all the

elements of heaps H1 and H2. Heaps are “destroyed” by thisoperation.

6. DECREASE-KEY(H , x, k) assigns to element x within heap H thenew key value k.

7. DELETE(H , x) deletes element x from heap H .

14 / 120

Page 32: 16 Fibonacci Heaps

Mergeable Heap Operations

The Fibonacci heap data structure is used to support the operations“meargeable heap” operations5. UNION(H1, H2) creates and returns a new heap that contains all the

elements of heaps H1 and H2. Heaps are “destroyed” by thisoperation.

6. DECREASE-KEY(H , x, k) assigns to element x within heap H thenew key value k.

7. DELETE(H , x) deletes element x from heap H .

14 / 120

Page 33: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

15 / 120

Page 34: 16 Fibonacci Heaps

Fibonacci Heap

DefinitionA Fibonacci heap is a collection of rooted trees that are min-heap ordered.

MeaningEach tree obeys the min-heap property:

The key of a node is greater than or equal to the key of its parent.It is an almost unordered binomial tree is the same as a binomialtree except that the root of one tree is made any child of the root ofthe other.

16 / 120

Page 35: 16 Fibonacci Heaps

Fibonacci Heap

DefinitionA Fibonacci heap is a collection of rooted trees that are min-heap ordered.

MeaningEach tree obeys the min-heap property:

The key of a node is greater than or equal to the key of its parent.It is an almost unordered binomial tree is the same as a binomialtree except that the root of one tree is made any child of the root ofthe other.

16 / 120

Page 36: 16 Fibonacci Heaps

Fibonacci Heap

DefinitionA Fibonacci heap is a collection of rooted trees that are min-heap ordered.

MeaningEach tree obeys the min-heap property:

The key of a node is greater than or equal to the key of its parent.It is an almost unordered binomial tree is the same as a binomialtree except that the root of one tree is made any child of the root ofthe other.

16 / 120

Page 37: 16 Fibonacci Heaps

Fibonacci Heap

DefinitionA Fibonacci heap is a collection of rooted trees that are min-heap ordered.

MeaningEach tree obeys the min-heap property:

The key of a node is greater than or equal to the key of its parent.It is an almost unordered binomial tree is the same as a binomialtree except that the root of one tree is made any child of the root ofthe other.

16 / 120

Page 38: 16 Fibonacci Heaps

Fibonacci Structure

Example

23 7 3 17 24

18 52 38

39 41

30 26 46

35

17 / 120

Page 39: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

18 / 120

Page 40: 16 Fibonacci Heaps

Why Fibonacci Heaps?

Fibonacci Heaps factsFibonacci heaps are especially desirable when the number of calls toExtract-Min and Delete is small.All other operations run in O(1).

ApplicationsFibonacci heaps may be used in many applications. Some graph problems,like minimum spanning tree and single-source-shortest-path.

19 / 120

Page 41: 16 Fibonacci Heaps

Why Fibonacci Heaps?

Fibonacci Heaps factsFibonacci heaps are especially desirable when the number of calls toExtract-Min and Delete is small.All other operations run in O(1).

ApplicationsFibonacci heaps may be used in many applications. Some graph problems,like minimum spanning tree and single-source-shortest-path.

19 / 120

Page 42: 16 Fibonacci Heaps

It is more...

We have thatProcedure Binary Heap (Worst Case) Fibonacci Heap (Amortized)Make-Heap Θ (1) Θ (1)

Insert Θ (log n) Θ (1)Minimum Θ (1) Θ (1)

Extract-Min Θ (log n) Θ (log n)Union Θ (n) Θ (1)

Decrease-Key Θ (log n) Θ (1)Delete Θ (log n) Θ (log n)

20 / 120

Page 43: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

21 / 120

Page 44: 16 Fibonacci Heaps

Fields in each node

The Classic OnesEach node contains a x.parent and x.child field.

The ones for the doubled linked listThe children of each a node x are linked together in a circular doublelinked list:

Each child y of x has a y.left and y.right to do this.

22 / 120

Page 45: 16 Fibonacci Heaps

Fields in each node

The Classic OnesEach node contains a x.parent and x.child field.

The ones for the doubled linked listThe children of each a node x are linked together in a circular doublelinked list:

Each child y of x has a y.left and y.right to do this.

22 / 120

Page 46: 16 Fibonacci Heaps

Thus, we have the following important labels

Field degreeDid you notice that there in no way to find the number of childrenunless you have complex exploratory method?We store the number of children in the child list of node x in x.degree.

The Amortized LabelEach child has the field mark.

IMPORTANT1 The field mark indicates whether a node has lost a child since the

last time was made the child of another node.2 Newly created nodes are unmarked (Boolean value FALSE), and a

node becomes unmarked whenever it is made the child of anothernode.

23 / 120

Page 47: 16 Fibonacci Heaps

Thus, we have the following important labels

Field degreeDid you notice that there in no way to find the number of childrenunless you have complex exploratory method?We store the number of children in the child list of node x in x.degree.

The Amortized LabelEach child has the field mark.

IMPORTANT1 The field mark indicates whether a node has lost a child since the

last time was made the child of another node.2 Newly created nodes are unmarked (Boolean value FALSE), and a

node becomes unmarked whenever it is made the child of anothernode.

23 / 120

Page 48: 16 Fibonacci Heaps

Thus, we have the following important labels

Field degreeDid you notice that there in no way to find the number of childrenunless you have complex exploratory method?We store the number of children in the child list of node x in x.degree.

The Amortized LabelEach child has the field mark.

IMPORTANT1 The field mark indicates whether a node has lost a child since the

last time was made the child of another node.2 Newly created nodes are unmarked (Boolean value FALSE), and a

node becomes unmarked whenever it is made the child of anothernode.

23 / 120

Page 49: 16 Fibonacci Heaps

Thus, we have the following important labels

Field degreeDid you notice that there in no way to find the number of childrenunless you have complex exploratory method?We store the number of children in the child list of node x in x.degree.

The Amortized LabelEach child has the field mark.

IMPORTANT1 The field mark indicates whether a node has lost a child since the

last time was made the child of another node.2 Newly created nodes are unmarked (Boolean value FALSE), and a

node becomes unmarked whenever it is made the child of anothernode.

23 / 120

Page 50: 16 Fibonacci Heaps

Thus, we have the following important labels

Field degreeDid you notice that there in no way to find the number of childrenunless you have complex exploratory method?We store the number of children in the child list of node x in x.degree.

The Amortized LabelEach child has the field mark.

IMPORTANT1 The field mark indicates whether a node has lost a child since the

last time was made the child of another node.2 Newly created nodes are unmarked (Boolean value FALSE), and a

node becomes unmarked whenever it is made the child of anothernode.

23 / 120

Page 51: 16 Fibonacci Heaps

The child list

Circular, doubly linked list have two advantages for use in Fibonacciheaps:

First, we can remove a node from a circular, doubly linked list in O(1)time.Second, given two such lists, we can concatenate them (or “splice”them together) into one circular, doubly linked list in O(1) time.

Example

24 / 120

Page 52: 16 Fibonacci Heaps

The child list

Circular, doubly linked list have two advantages for use in Fibonacciheaps:

First, we can remove a node from a circular, doubly linked list in O(1)time.Second, given two such lists, we can concatenate them (or “splice”them together) into one circular, doubly linked list in O(1) time.

Example

24 / 120

Page 53: 16 Fibonacci Heaps

The child list

Circular, doubly linked list have two advantages for use in Fibonacciheaps:

First, we can remove a node from a circular, doubly linked list in O(1)time.Second, given two such lists, we can concatenate them (or “splice”them together) into one circular, doubly linked list in O(1) time.

Example

26 3 45 34 12

24 / 120

Page 54: 16 Fibonacci Heaps

Additional

FirstThe roots of all the trees in a Fibonacci heap H are linked together usingtheir left and right pointers into a circular, doubly linked list called theroot list of the Fibonacci heap.

SecondThe pointer H .min of the Fibonacci data structure thus points to thenode in the root list whose key is minimum.Trees may appear in any order within a root list.

ThirdThe Fibonacci data structure has the field H .n =the number of nodescurrently in the Fibonacci Heap H .

25 / 120

Page 55: 16 Fibonacci Heaps

Additional

FirstThe roots of all the trees in a Fibonacci heap H are linked together usingtheir left and right pointers into a circular, doubly linked list called theroot list of the Fibonacci heap.

SecondThe pointer H .min of the Fibonacci data structure thus points to thenode in the root list whose key is minimum.Trees may appear in any order within a root list.

ThirdThe Fibonacci data structure has the field H .n =the number of nodescurrently in the Fibonacci Heap H .

25 / 120

Page 56: 16 Fibonacci Heaps

Additional

FirstThe roots of all the trees in a Fibonacci heap H are linked together usingtheir left and right pointers into a circular, doubly linked list called theroot list of the Fibonacci heap.

SecondThe pointer H .min of the Fibonacci data structure thus points to thenode in the root list whose key is minimum.Trees may appear in any order within a root list.

ThirdThe Fibonacci data structure has the field H .n =the number of nodescurrently in the Fibonacci Heap H .

25 / 120

Page 57: 16 Fibonacci Heaps

Additional

FirstThe roots of all the trees in a Fibonacci heap H are linked together usingtheir left and right pointers into a circular, doubly linked list called theroot list of the Fibonacci heap.

SecondThe pointer H .min of the Fibonacci data structure thus points to thenode in the root list whose key is minimum.Trees may appear in any order within a root list.

ThirdThe Fibonacci data structure has the field H .n =the number of nodescurrently in the Fibonacci Heap H .

25 / 120

Page 58: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

26 / 120

Page 59: 16 Fibonacci Heaps

Idea behind Fibonacci heaps

Main ideaFibonacci heaps are called lazy data structures because they delay work aslong as possible using the field mark!!!

27 / 120

Page 60: 16 Fibonacci Heaps

Make Heap

Make a heapYou only need the following code:MakeHeap()

1 min[H ] = NIL2 n(H ) = 0

Complexity is as simple asCost O(1).

28 / 120

Page 61: 16 Fibonacci Heaps

Make Heap

Make a heapYou only need the following code:MakeHeap()

1 min[H ] = NIL2 n(H ) = 0

Complexity is as simple asCost O(1).

28 / 120

Page 62: 16 Fibonacci Heaps

Insertion

Code for Inserting a nodeFib-Heap-Insert(H , x)

1 x.degree = 02 x.p = NIL3 x.child = NIL4 x.mark = FALSE5 if H .min = NIL6 Create a root list for H containing just x7 H .min = x8 else insert x into H ′s root list9 if x.key < H .min.key10 H .min = x11 H .n = H .n + 1

29 / 120

Page 63: 16 Fibonacci Heaps

Insertion

Code for Inserting a nodeFib-Heap-Insert(H , x)

1 x.degree = 02 x.p = NIL3 x.child = NIL4 x.mark = FALSE5 if H .min = NIL6 Create a root list for H containing just x7 H .min = x8 else insert x into H ′s root list9 if x.key < H .min.key10 H .min = x11 H .n = H .n + 1

29 / 120

Page 64: 16 Fibonacci Heaps

Insertion

Code for Inserting a nodeFib-Heap-Insert(H , x)

1 x.degree = 02 x.p = NIL3 x.child = NIL4 x.mark = FALSE5 if H .min = NIL6 Create a root list for H containing just x7 H .min = x8 else insert x into H ′s root list9 if x.key < H .min.key10 H .min = x11 H .n = H .n + 1

29 / 120

Page 65: 16 Fibonacci Heaps

Insertion

Code for Inserting a nodeFib-Heap-Insert(H , x)

1 x.degree = 02 x.p = NIL3 x.child = NIL4 x.mark = FALSE5 if H .min = NIL6 Create a root list for H containing just x7 H .min = x8 else insert x into H ′s root list9 if x.key < H .min.key10 H .min = x11 H .n = H .n + 1

29 / 120

Page 66: 16 Fibonacci Heaps

Inserting a node

Example

23 7 3 17 24

18 52 38

39 41

30 26 46

35 51

30 / 120

Page 67: 16 Fibonacci Heaps

Inserting a node

Example

23 7 3 17 24

18 52 38 30 26 46

39 41 35 51

21

31 / 120

Page 68: 16 Fibonacci Heaps

Minimum

Finding the MinimumSimply return the key of min(H ).The amortized cost is simply O (1).

I We will analyze this later on...

32 / 120

Page 69: 16 Fibonacci Heaps

Minimum

Finding the MinimumSimply return the key of min(H ).The amortized cost is simply O (1).

I We will analyze this later on...

32 / 120

Page 70: 16 Fibonacci Heaps

What about Union of two Heaps?

Code for Union of HeapsFib-Heap-Union(H1, H2)

1 H =Make-Fib-Heap()2 H .min = H1.min3 Concatenate the root list of H2 with the root list of H4 If (H1.min == NIL) or (H .min 6= NIL and H2.min.key < H1.min.key)5 H .min = H2.min6 H .n = H1.n + H2.n7 return H

33 / 120

Page 71: 16 Fibonacci Heaps

What about Union of two Heaps?

Code for Union of HeapsFib-Heap-Union(H1, H2)

1 H =Make-Fib-Heap()2 H .min = H1.min3 Concatenate the root list of H2 with the root list of H4 If (H1.min == NIL) or (H .min 6= NIL and H2.min.key < H1.min.key)5 H .min = H2.min6 H .n = H1.n + H2.n7 return H

33 / 120

Page 72: 16 Fibonacci Heaps

What about Union of two Heaps?

Code for Union of HeapsFib-Heap-Union(H1, H2)

1 H =Make-Fib-Heap()2 H .min = H1.min3 Concatenate the root list of H2 with the root list of H4 If (H1.min == NIL) or (H .min 6= NIL and H2.min.key < H1.min.key)5 H .min = H2.min6 H .n = H1.n + H2.n7 return H

33 / 120

Page 73: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

34 / 120

Page 74: 16 Fibonacci Heaps

In order to analyze Union...

We introduce some ideas from...Our old friend amortized analysis and potential method!!!

35 / 120

Page 75: 16 Fibonacci Heaps

Amortized potential function

We have the following functionΦ(H ) = t(H ) + 2m(H )

Where:I t(H ) is the number of trees in the Fibonacci heapI m(H ) is the number of marked nodes in the tree.

Amortized analysisThe amortized analysis will depend on there being a known bound D(n)on the maximum degree of any node in an n-node heap.

36 / 120

Page 76: 16 Fibonacci Heaps

Amortized potential function

We have the following functionΦ(H ) = t(H ) + 2m(H )

Where:I t(H ) is the number of trees in the Fibonacci heapI m(H ) is the number of marked nodes in the tree.

Amortized analysisThe amortized analysis will depend on there being a known bound D(n)on the maximum degree of any node in an n-node heap.

36 / 120

Page 77: 16 Fibonacci Heaps

Amortized potential function

We have the following functionΦ(H ) = t(H ) + 2m(H )

Where:I t(H ) is the number of trees in the Fibonacci heapI m(H ) is the number of marked nodes in the tree.

Amortized analysisThe amortized analysis will depend on there being a known bound D(n)on the maximum degree of any node in an n-node heap.

36 / 120

Page 78: 16 Fibonacci Heaps

Amortized potential function

We have the following functionΦ(H ) = t(H ) + 2m(H )

Where:I t(H ) is the number of trees in the Fibonacci heapI m(H ) is the number of marked nodes in the tree.

Amortized analysisThe amortized analysis will depend on there being a known bound D(n)on the maximum degree of any node in an n-node heap.

36 / 120

Page 79: 16 Fibonacci Heaps

Amortized potential function

We have the following functionΦ(H ) = t(H ) + 2m(H )

Where:I t(H ) is the number of trees in the Fibonacci heapI m(H ) is the number of marked nodes in the tree.

Amortized analysisThe amortized analysis will depend on there being a known bound D(n)on the maximum degree of any node in an n-node heap.

36 / 120

Page 80: 16 Fibonacci Heaps

Observations about D(n)

About the known bound D(n)D(n) is the maximum degree of any node in the binomial heap.It is more if the Fibonacci heap is a collection of unordered trees, thenD(n) = log n.

I We will prove this latter!!!

37 / 120

Page 81: 16 Fibonacci Heaps

Observations about D(n)

About the known bound D(n)D(n) is the maximum degree of any node in the binomial heap.It is more if the Fibonacci heap is a collection of unordered trees, thenD(n) = log n.

I We will prove this latter!!!

37 / 120

Page 82: 16 Fibonacci Heaps

Observations about D(n)

About the known bound D(n)D(n) is the maximum degree of any node in the binomial heap.It is more if the Fibonacci heap is a collection of unordered trees, thenD(n) = log n.

I We will prove this latter!!!

37 / 120

Page 83: 16 Fibonacci Heaps

Back to Insertion

FirstIf H ′ is the Fibonacci heap after inserting, and H before that:

t(H ′) = t(H ) + 1

Secondm(H ′) = m(H )

Then the change of potential isΦ(H ′)− Φ(H ) = 1 then complexity analysis results in O(1) + 1 = O

38 / 120

Page 84: 16 Fibonacci Heaps

Back to Insertion

FirstIf H ′ is the Fibonacci heap after inserting, and H before that:

t(H ′) = t(H ) + 1

Secondm(H ′) = m(H )

Then the change of potential isΦ(H ′)− Φ(H ) = 1 then complexity analysis results in O(1) + 1 = O

38 / 120

Page 85: 16 Fibonacci Heaps

Back to Insertion

FirstIf H ′ is the Fibonacci heap after inserting, and H before that:

t(H ′) = t(H ) + 1

Secondm(H ′) = m(H )

Then the change of potential isΦ(H ′)− Φ(H ) = 1 then complexity analysis results in O(1) + 1 = O

38 / 120

Page 86: 16 Fibonacci Heaps

Other operations: Find Min

It is possible to rephrase this in terms of potential costBy using the pointer min[H ] potential cost is 0 then O(1).

39 / 120

Page 87: 16 Fibonacci Heaps

Other operations: Union

Union of two Fibonacci heapsFib-Heap-Union(H1, H2)

1 H =Make-Fib-Heap()2 H .min = H1.min3 Concatenate the root list of H2 with the root list of H4 If (H1.min == NIL) or (H .min 6= NIL and H2.min.key < H1.min.key)5 H .min = H2.min6 H .n = H1.n + H2.n7 return H

40 / 120

Page 88: 16 Fibonacci Heaps

Other operations: Union

Union of two Fibonacci heapsFib-Heap-Union(H1, H2)

1 H =Make-Fib-Heap()2 H .min = H1.min3 Concatenate the root list of H2 with the root list of H4 If (H1.min == NIL) or (H .min 6= NIL and H2.min.key < H1.min.key)5 H .min = H2.min6 H .n = H1.n + H2.n7 return H

40 / 120

Page 89: 16 Fibonacci Heaps

Cost of uniting two Fibonacci heaps

Firstt(H ) = t (H1) + t (H2) and m(H ) = m (H1) + m (H2).

Secondci = O(1) this is because the number of steps to make the unionoperations is a constant.

Potential analysisci = ci + Φ (H )− [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1).

We have then a complexity of O(1).

41 / 120

Page 90: 16 Fibonacci Heaps

Cost of uniting two Fibonacci heaps

Firstt(H ) = t (H1) + t (H2) and m(H ) = m (H1) + m (H2).

Secondci = O(1) this is because the number of steps to make the unionoperations is a constant.

Potential analysisci = ci + Φ (H )− [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1).

We have then a complexity of O(1).

41 / 120

Page 91: 16 Fibonacci Heaps

Cost of uniting two Fibonacci heaps

Firstt(H ) = t (H1) + t (H2) and m(H ) = m (H1) + m (H2).

Secondci = O(1) this is because the number of steps to make the unionoperations is a constant.

Potential analysisci = ci + Φ (H )− [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1).

We have then a complexity of O(1).

41 / 120

Page 92: 16 Fibonacci Heaps

Extract minExtract minFib-Heap-Extract-Min(H )

1 z = H .min2 if z 6= NIL3 for each child x of z4 add x to the root list of H5 x.p = NIL6 remove z from the root list of H7 if z == z.right8 H .min = NIL9 else H .min = z.right10 Consolidate(H )11 H .n = H .n − 112 return z

42 / 120

Page 93: 16 Fibonacci Heaps

Extract minExtract minFib-Heap-Extract-Min(H )

1 z = H .min2 if z 6= NIL3 for each child x of z4 add x to the root list of H5 x.p = NIL6 remove z from the root list of H7 if z == z.right8 H .min = NIL9 else H .min = z.right10 Consolidate(H )11 H .n = H .n − 112 return z

42 / 120

Page 94: 16 Fibonacci Heaps

Extract minExtract minFib-Heap-Extract-Min(H )

1 z = H .min2 if z 6= NIL3 for each child x of z4 add x to the root list of H5 x.p = NIL6 remove z from the root list of H7 if z == z.right8 H .min = NIL9 else H .min = z.right10 Consolidate(H )11 H .n = H .n − 112 return z

42 / 120

Page 95: 16 Fibonacci Heaps

Extract minExtract minFib-Heap-Extract-Min(H )

1 z = H .min2 if z 6= NIL3 for each child x of z4 add x to the root list of H5 x.p = NIL6 remove z from the root list of H7 if z == z.right8 H .min = NIL9 else H .min = z.right10 Consolidate(H )11 H .n = H .n − 112 return z

42 / 120

Page 96: 16 Fibonacci Heaps

Extract minExtract minFib-Heap-Extract-Min(H )

1 z = H .min2 if z 6= NIL3 for each child x of z4 add x to the root list of H5 x.p = NIL6 remove z from the root list of H7 if z == z.right8 H .min = NIL9 else H .min = z.right10 Consolidate(H )11 H .n = H .n − 112 return z

42 / 120

Page 97: 16 Fibonacci Heaps

Extract minExtract minFib-Heap-Extract-Min(H )

1 z = H .min2 if z 6= NIL3 for each child x of z4 add x to the root list of H5 x.p = NIL6 remove z from the root list of H7 if z == z.right8 H .min = NIL9 else H .min = z.right10 Consolidate(H )11 H .n = H .n − 112 return z

42 / 120

Page 98: 16 Fibonacci Heaps

What is happening here?

FirstHere, the code in lines 3-6 remove the node z and adds the children of zto the root list of H .

NextIf the Fibonacci Heap is not empty a consolidation code is triggered.

43 / 120

Page 99: 16 Fibonacci Heaps

What is happening here?

FirstHere, the code in lines 3-6 remove the node z and adds the children of zto the root list of H .

NextIf the Fibonacci Heap is not empty a consolidation code is triggered.

43 / 120

Page 100: 16 Fibonacci Heaps

What is happening here?

ThusThe consolidate code is used to eliminate subtrees that have thesame root degree by linking them.It repeatedly executes the following steps:

1 Find two roots x and y in the root list with the same degree. Withoutloss of generality, let x.key ≤ y.key.

2 Link y to x: remove y from the root list, and make y a child of x bycalling the FIB-HEAP-LINK procedure.

F This procedure increments the attribute x.degree and clears the markon y.

44 / 120

Page 101: 16 Fibonacci Heaps

What is happening here?

ThusThe consolidate code is used to eliminate subtrees that have thesame root degree by linking them.It repeatedly executes the following steps:

1 Find two roots x and y in the root list with the same degree. Withoutloss of generality, let x.key ≤ y.key.

2 Link y to x: remove y from the root list, and make y a child of x bycalling the FIB-HEAP-LINK procedure.

F This procedure increments the attribute x.degree and clears the markon y.

44 / 120

Page 102: 16 Fibonacci Heaps

What is happening here?

ThusThe consolidate code is used to eliminate subtrees that have thesame root degree by linking them.It repeatedly executes the following steps:

1 Find two roots x and y in the root list with the same degree. Withoutloss of generality, let x.key ≤ y.key.

2 Link y to x: remove y from the root list, and make y a child of x bycalling the FIB-HEAP-LINK procedure.

F This procedure increments the attribute x.degree and clears the markon y.

44 / 120

Page 103: 16 Fibonacci Heaps

What is happening here?

ThusThe consolidate code is used to eliminate subtrees that have thesame root degree by linking them.It repeatedly executes the following steps:

1 Find two roots x and y in the root list with the same degree. Withoutloss of generality, let x.key ≤ y.key.

2 Link y to x: remove y from the root list, and make y a child of x bycalling the FIB-HEAP-LINK procedure.

F This procedure increments the attribute x.degree and clears the markon y.

44 / 120

Page 104: 16 Fibonacci Heaps

What is happening here?

ThusThe consolidate code is used to eliminate subtrees that have thesame root degree by linking them.It repeatedly executes the following steps:

1 Find two roots x and y in the root list with the same degree. Withoutloss of generality, let x.key ≤ y.key.

2 Link y to x: remove y from the root list, and make y a child of x bycalling the FIB-HEAP-LINK procedure.

F This procedure increments the attribute x.degree and clears the markon y.

44 / 120

Page 105: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

45 / 120

Page 106: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 107: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 108: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 109: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 110: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 111: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 112: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 113: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 114: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 115: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 116: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 117: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 118: 16 Fibonacci Heaps

Consolidate CodeConsolidate(H )

1. Let A (0...D (H .n)) be a new array2. for i = 0 to D (H .n)

3. A [i] = NIL4. for each w in the root list of H5. x = w

6. d = x.degree7. while A [d] 6= NIL

8. y = A [d]9. if x.key > y.key

10. exchange x with y11. Fib-Heap-Link(H , y, x)12. A [d] = NIL

13. d = d + 1

14. A [d] = x

15. H .min = NIL16. for i = 0 to D (H .n)

17. if A [i] 6= NIL18. if H .min == NIL19. create a root list for H

containing just A [i]

20. H .min = A [i]21. else22. insert A [i] into H ′s

root list24. if A [i] .key < H .min.key25. H .min = A [i]

46 / 120

Page 119: 16 Fibonacci Heaps

Fib-Heap-Link Code

Fib-Heap-Link(H , y, x)1 Remove y from the root list of H2 Make y a child of x, incrementing x.degree3 y.mark = FALSE

47 / 120

Page 120: 16 Fibonacci Heaps

Fib-Heap-Link Code

Fib-Heap-Link(H , y, x)1 Remove y from the root list of H2 Make y a child of x, incrementing x.degree3 y.mark = FALSE

47 / 120

Page 121: 16 Fibonacci Heaps

Fib-Heap-Link Code

Fib-Heap-Link(H , y, x)1 Remove y from the root list of H2 Make y a child of x, incrementing x.degree3 y.mark = FALSE

47 / 120

Page 122: 16 Fibonacci Heaps

Auxiliary Array

The Consolidate usesAn auxiliary pointer array A [0...D (H .n)]

It keeps track ofThe roots according the degree

48 / 120

Page 123: 16 Fibonacci Heaps

Auxiliary Array

The Consolidate usesAn auxiliary pointer array A [0...D (H .n)]

It keeps track ofThe roots according the degree

48 / 120

Page 124: 16 Fibonacci Heaps

Code Process

A while loop inside of the for loop - lines 1-15Using a w variable to go through the root listThis is used to fill the pointers in A [0...D (H .n)]Then you link both trees using who has a larger keyThen you add a pointer to the new min-heap, with new degree, in A.

Then - lines 15-23Lines 15-23 clean the original Fibonacci Heap, then using the pointers atthe array A, each subtree is inserted into the root list of H .

49 / 120

Page 125: 16 Fibonacci Heaps

Code Process

A while loop inside of the for loop - lines 1-15Using a w variable to go through the root listThis is used to fill the pointers in A [0...D (H .n)]Then you link both trees using who has a larger keyThen you add a pointer to the new min-heap, with new degree, in A.

Then - lines 15-23Lines 15-23 clean the original Fibonacci Heap, then using the pointers atthe array A, each subtree is inserted into the root list of H .

49 / 120

Page 126: 16 Fibonacci Heaps

Code Process

A while loop inside of the for loop - lines 1-15Using a w variable to go through the root listThis is used to fill the pointers in A [0...D (H .n)]Then you link both trees using who has a larger keyThen you add a pointer to the new min-heap, with new degree, in A.

Then - lines 15-23Lines 15-23 clean the original Fibonacci Heap, then using the pointers atthe array A, each subtree is inserted into the root list of H .

49 / 120

Page 127: 16 Fibonacci Heaps

Code Process

A while loop inside of the for loop - lines 1-15Using a w variable to go through the root listThis is used to fill the pointers in A [0...D (H .n)]Then you link both trees using who has a larger keyThen you add a pointer to the new min-heap, with new degree, in A.

Then - lines 15-23Lines 15-23 clean the original Fibonacci Heap, then using the pointers atthe array A, each subtree is inserted into the root list of H .

49 / 120

Page 128: 16 Fibonacci Heaps

Code Process

A while loop inside of the for loop - lines 1-15Using a w variable to go through the root listThis is used to fill the pointers in A [0...D (H .n)]Then you link both trees using who has a larger keyThen you add a pointer to the new min-heap, with new degree, in A.

Then - lines 15-23Lines 15-23 clean the original Fibonacci Heap, then using the pointers atthe array A, each subtree is inserted into the root list of H .

49 / 120

Page 129: 16 Fibonacci Heaps

Loop Invariance

We have the following Loop InvarianceAt the start of each iteration of the while loop, d = x.degree.

InitLine 6 ensures that the loop invariant holds the first time we enter theloop.

MaintenanceWe have two nodes x and y such that they have the same degree then

We link them together and increase the d to d + 1 adding a new treepointer to A with degree d + 1

50 / 120

Page 130: 16 Fibonacci Heaps

Loop Invariance

We have the following Loop InvarianceAt the start of each iteration of the while loop, d = x.degree.

InitLine 6 ensures that the loop invariant holds the first time we enter theloop.

MaintenanceWe have two nodes x and y such that they have the same degree then

We link them together and increase the d to d + 1 adding a new treepointer to A with degree d + 1

50 / 120

Page 131: 16 Fibonacci Heaps

Loop Invariance

We have the following Loop InvarianceAt the start of each iteration of the while loop, d = x.degree.

InitLine 6 ensures that the loop invariant holds the first time we enter theloop.

MaintenanceWe have two nodes x and y such that they have the same degree then

We link them together and increase the d to d + 1 adding a new treepointer to A with degree d + 1

50 / 120

Page 132: 16 Fibonacci Heaps

Loop Invariance

TerminationWe repeat the while loop until A [d] = NIL, in which case there is noother root with the same degree as x.

51 / 120

Page 133: 16 Fibonacci Heaps

Example of consolidation

We remove H .min == 3

23 7 3 17 24

18 52 38 30 26 46

39 41 35 51

21

52 / 120

Page 134: 16 Fibonacci Heaps

Example of consolidation

The children are moved to the root’s list

23 7 17 2418 52 38

30 26 4639 41

35 51

21

53 / 120

Page 135: 16 Fibonacci Heaps

Example of consolidation

Now, you get Consolidation running beginning A [1]→ 17

23 7 17 2418 52 38

30 26 4639 41

35 51

21

0 1 2 3

54 / 120

Page 136: 16 Fibonacci Heaps

Example of consolidation

Now, A [2]→ 24

23 7 17 2418 52 38

30 26 4639 41

35 51

21

0 1 2 3

55 / 120

Page 137: 16 Fibonacci Heaps

Example of consolidation

We have a pointer to a node with degree = 0

23 7 17 2418 52 38

30 26 4639 41

35 51

21

0 1 2 3

56 / 120

Page 138: 16 Fibonacci Heaps

Example of consolidation

We don’t do an exchange

23 7 17 2418 52 38

30 26 4639 41

35 51

21

0 1 2 3

57 / 120

Page 139: 16 Fibonacci Heaps

Example of consolidation

Remove y from the root’s list

23

7 17 2418 52 38

30 26 4639 41

35 51

21

0 1 2 3

58 / 120

Page 140: 16 Fibonacci Heaps

Example of consolidation

Make y a child of x

23

7 17 2418 52 38

30 26 4639 41

35 51

21

0 1 2 3

59 / 120

Page 141: 16 Fibonacci Heaps

Example of consolidation

Remove y from the root list

23

7

17

2418 52 38

30

26 4639 41

35 51

21

0 1 2 3

60 / 120

Page 142: 16 Fibonacci Heaps

Example of consolidation

Make y a child of x

23

7

17

2418 52 38

30

26 4639 41

35 51

21

0 1 2 3

61 / 120

Page 143: 16 Fibonacci Heaps

Example of consolidation

and we point to the the element with degree = 3

23

7

1724

18 52 38

3026 46

39 41

35 51

21

0 1 2 3

62 / 120

Page 144: 16 Fibonacci Heaps

Example of consolidation

We move to the next root’s child and point to it from A

23

7

1724

18 52 38

3026 46

39 41

35 51

21

0 1 2 3

63 / 120

Page 145: 16 Fibonacci Heaps

Example of consolidation

We move to the next root’s child and point to it from A

23

7

1724

18 52 38

3026 46

39 41

35 51

21

0 1 2 3

64 / 120

Page 146: 16 Fibonacci Heaps

Example of consolidation

We move to the next root’s child and point to it from A

23

7

1724

18 52 38

3026 46

39 41

35 51

21

0 1 2 3

65 / 120

Page 147: 16 Fibonacci Heaps

Example of consolidation

We point y = A [0] then do the exchange between x ←→ y

23

7

1724

18 52 38

3026 46

39 41

35 51

21

0 1 2 3

66 / 120

Page 148: 16 Fibonacci Heaps

Example of consolidation

Link x and y

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

67 / 120

Page 149: 16 Fibonacci Heaps

Example of consolidation

Make A [d] = NIL

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

68 / 120

Page 150: 16 Fibonacci Heaps

Example of consolidation

We make y = A [1]

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

69 / 120

Page 151: 16 Fibonacci Heaps

Example of consolidation

Do an exchange between x ←→ y

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

70 / 120

Page 152: 16 Fibonacci Heaps

Example of consolidation

Remove y from the root’s list

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

71 / 120

Page 153: 16 Fibonacci Heaps

Example of consolidation

Make y a child of x

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

72 / 120

Page 154: 16 Fibonacci Heaps

Example of consolidation

Make A [1] = NIL, then we make d = d + 1

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

73 / 120

Page 155: 16 Fibonacci Heaps

Example of consolidation

Because A [2] = NIL, then A [2] = x

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

74 / 120

Page 156: 16 Fibonacci Heaps

Example of consolidation

We move to the next w and make x = w

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

75 / 120

Page 157: 16 Fibonacci Heaps

Example of consolidation

Because A [1] = NIL, then jump over the while loop and makeA [1] = x

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

76 / 120

Page 158: 16 Fibonacci Heaps

Example of consolidation

Because A [1] 6= NIL insert into the root’s list and becauseH .min = NIL, it is the first node in it

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

77 / 120

Page 159: 16 Fibonacci Heaps

Example of consolidation

Because A [2] 6= NIL insert into the root’s list no exchange of minbecause A [2] .key > H .min.key

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

78 / 120

Page 160: 16 Fibonacci Heaps

Example of consolidation

Because A [3] 6= NIL insert into the root’s list no exchange of minbecause A [3] .key > H .min.key

23

7

1724

18

52

38

3026 46

39 41

35 51

21

0 1 2 3

79 / 120

Page 161: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

80 / 120

Page 162: 16 Fibonacci Heaps

Cost of Extract-min

Amortized analysis observationsThe cost of FIB-EXTRACT-MIN contributes at most O (D (n)) because

1 The for loop at lines 3 to 5 in the code FIB-EXTRACT-MIN.2 for loop at lines 2-3 and 16-23 of CONSOLIDATE.

81 / 120

Page 163: 16 Fibonacci Heaps

Cost of Extract-min

Amortized analysis observationsThe cost of FIB-EXTRACT-MIN contributes at most O (D (n)) because

1 The for loop at lines 3 to 5 in the code FIB-EXTRACT-MIN.2 for loop at lines 2-3 and 16-23 of CONSOLIDATE.

81 / 120

Page 164: 16 Fibonacci Heaps

Cost of Extract-min

Amortized analysis observationsThe cost of FIB-EXTRACT-MIN contributes at most O (D (n)) because

1 The for loop at lines 3 to 5 in the code FIB-EXTRACT-MIN.2 for loop at lines 2-3 and 16-23 of CONSOLIDATE.

81 / 120

Page 165: 16 Fibonacci Heaps

Cost of Extract-min

Amortized analysis observationsThe cost of FIB-EXTRACT-MIN contributes at most O (D (n)) because

1 The for loop at lines 3 to 5 in the code FIB-EXTRACT-MIN.2 for loop at lines 2-3 and 16-23 of CONSOLIDATE.

81 / 120

Page 166: 16 Fibonacci Heaps

Next

We have thatThe size of the root list when calling Consolidate is at most

D(n) + t(H )− 1 (1)

because the min root was extracted an it has at most D(n) children.

82 / 120

Page 167: 16 Fibonacci Heaps

Next

We have thatThe size of the root list when calling Consolidate is at most

D(n) + t(H )− 1 (1)

because the min root was extracted an it has at most D(n) children.

82 / 120

Page 168: 16 Fibonacci Heaps

Next

We have thatThe size of the root list when calling Consolidate is at most

D(n) + t(H )− 1 (1)

because the min root was extracted an it has at most D(n) children.

82 / 120

Page 169: 16 Fibonacci Heaps

Next

ThenAt lines 4 to 14 in the CONSOLIDATE code:

The amount of work done by the for and the while loop isproportional to D(n) + t(H ) because each time we go through anelement in the root list (for loop).The while loop consolidate the tree pointed by the pointer to a tree xwith same degree.

The Actual Cost isThen, the actual cost is ci = O (D (n) + t (H )).

83 / 120

Page 170: 16 Fibonacci Heaps

Next

ThenAt lines 4 to 14 in the CONSOLIDATE code:

The amount of work done by the for and the while loop isproportional to D(n) + t(H ) because each time we go through anelement in the root list (for loop).The while loop consolidate the tree pointed by the pointer to a tree xwith same degree.

The Actual Cost isThen, the actual cost is ci = O (D (n) + t (H )).

83 / 120

Page 171: 16 Fibonacci Heaps

Next

ThenAt lines 4 to 14 in the CONSOLIDATE code:

The amount of work done by the for and the while loop isproportional to D(n) + t(H ) because each time we go through anelement in the root list (for loop).The while loop consolidate the tree pointed by the pointer to a tree xwith same degree.

The Actual Cost isThen, the actual cost is ci = O (D (n) + t (H )).

83 / 120

Page 172: 16 Fibonacci Heaps

Next

ThenAt lines 4 to 14 in the CONSOLIDATE code:

The amount of work done by the for and the while loop isproportional to D(n) + t(H ) because each time we go through anelement in the root list (for loop).The while loop consolidate the tree pointed by the pointer to a tree xwith same degree.

The Actual Cost isThen, the actual cost is ci = O (D (n) + t (H )).

83 / 120

Page 173: 16 Fibonacci Heaps

Potential cost

Thus, assuming that H ’ is the new heap and H is the old oneΦ (H ) = t (H ) + 2 ·m (H ).Φ (H ′) = D (n) + 1 + 2 ·m (H ) because

I H ’ has at most D(n) + 1 elements after consolidationI No node is marked in the process.

84 / 120

Page 174: 16 Fibonacci Heaps

Potential cost

Thus, assuming that H ’ is the new heap and H is the old oneΦ (H ) = t (H ) + 2 ·m (H ).Φ (H ′) = D (n) + 1 + 2 ·m (H ) because

I H ’ has at most D(n) + 1 elements after consolidationI No node is marked in the process.

84 / 120

Page 175: 16 Fibonacci Heaps

Potential cost

Thus, assuming that H ’ is the new heap and H is the old oneΦ (H ) = t (H ) + 2 ·m (H ).Φ (H ′) = D (n) + 1 + 2 ·m (H ) because

I H ’ has at most D(n) + 1 elements after consolidationI No node is marked in the process.

84 / 120

Page 176: 16 Fibonacci Heaps

Potential cost

Thus, assuming that H ’ is the new heap and H is the old oneΦ (H ) = t (H ) + 2 ·m (H ).Φ (H ′) = D (n) + 1 + 2 ·m (H ) because

I H ’ has at most D(n) + 1 elements after consolidationI No node is marked in the process.

84 / 120

Page 177: 16 Fibonacci Heaps

Potential cost

The Final Potential Cost is

ci = ci + Φ(H ′)− Φ (H )

= O (D (n) + t (H )) + D (n) + 1 + 2 ·m (H )− t (H )− 2 ·m (H )= O (D (n) + t (H ))− t (H )= O (D (n)) ,

We shall see thatD(n) = O(log n)

85 / 120

Page 178: 16 Fibonacci Heaps

Potential cost

The Final Potential Cost is

ci = ci + Φ(H ′)− Φ (H )

= O (D (n) + t (H )) + D (n) + 1 + 2 ·m (H )− t (H )− 2 ·m (H )= O (D (n) + t (H ))− t (H )= O (D (n)) ,

We shall see thatD(n) = O(log n)

85 / 120

Page 179: 16 Fibonacci Heaps

Potential cost

The Final Potential Cost is

ci = ci + Φ(H ′)− Φ (H )

= O (D (n) + t (H )) + D (n) + 1 + 2 ·m (H )− t (H )− 2 ·m (H )= O (D (n) + t (H ))− t (H )= O (D (n)) ,

We shall see thatD(n) = O(log n)

85 / 120

Page 180: 16 Fibonacci Heaps

Potential cost

The Final Potential Cost is

ci = ci + Φ(H ′)− Φ (H )

= O (D (n) + t (H )) + D (n) + 1 + 2 ·m (H )− t (H )− 2 ·m (H )= O (D (n) + t (H ))− t (H )= O (D (n)) ,

We shall see thatD(n) = O(log n)

85 / 120

Page 181: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

86 / 120

Page 182: 16 Fibonacci Heaps

Decreasing a key

Fib-Heap-Decrease-Key(H , x , k)1 if k > x.key2 error “new key is greater than current key”3 x.key = k4 y = x.p5 if y 6= NIL and x.key < y.key6 CUT(H , x, y)7 Cascading-Cut(H , y)8 if x.key < H .min.key9 H .min = x

87 / 120

Page 183: 16 Fibonacci Heaps

Decreasing a key

Fib-Heap-Decrease-Key(H , x , k)1 if k > x.key2 error “new key is greater than current key”3 x.key = k4 y = x.p5 if y 6= NIL and x.key < y.key6 CUT(H , x, y)7 Cascading-Cut(H , y)8 if x.key < H .min.key9 H .min = x

87 / 120

Page 184: 16 Fibonacci Heaps

Decreasing a key

Fib-Heap-Decrease-Key(H , x , k)1 if k > x.key2 error “new key is greater than current key”3 x.key = k4 y = x.p5 if y 6= NIL and x.key < y.key6 CUT(H , x, y)7 Cascading-Cut(H , y)8 if x.key < H .min.key9 H .min = x

87 / 120

Page 185: 16 Fibonacci Heaps

Decreasing a key

Fib-Heap-Decrease-Key(H , x , k)1 if k > x.key2 error “new key is greater than current key”3 x.key = k4 y = x.p5 if y 6= NIL and x.key < y.key6 CUT(H , x, y)7 Cascading-Cut(H , y)8 if x.key < H .min.key9 H .min = x

87 / 120

Page 186: 16 Fibonacci Heaps

Explanation

First1 Lines 1–3 ensure that the new key is no greater than the current key

of x and then assign the new key to x.2 If x is not a root and if x.key ≤ y.key, where y is x’s parent, then

CUT and CASCADING-CUT are triggered.

88 / 120

Page 187: 16 Fibonacci Heaps

Explanation

First1 Lines 1–3 ensure that the new key is no greater than the current key

of x and then assign the new key to x.2 If x is not a root and if x.key ≤ y.key, where y is x’s parent, then

CUT and CASCADING-CUT are triggered.

88 / 120

Page 188: 16 Fibonacci Heaps

Decreasing a key (continuation - cascade cutting)

Be lazy to remove keys!Cut(H , x, y)

1 Remove x from the child list of y,decreasing y.degree

2 Add x to the root list of H3 x.p = NIL4 x.mark = FALSE

Cascading-Cut(H , y)

1 z = y.p2 if z 6= NIL3 if y.mark == FALSE4 y.mark=TRUE5 else6 Cut(H , y, z)7 Cascading-Cut(H , y)(H , z)

89 / 120

Page 189: 16 Fibonacci Heaps

Decreasing a key (continuation - cascade cutting)

Be lazy to remove keys!Cut(H , x, y)

1 Remove x from the child list of y,decreasing y.degree

2 Add x to the root list of H3 x.p = NIL4 x.mark = FALSE

Cascading-Cut(H , y)

1 z = y.p2 if z 6= NIL3 if y.mark == FALSE4 y.mark=TRUE5 else6 Cut(H , y, z)7 Cascading-Cut(H , y)(H , z)

89 / 120

Page 190: 16 Fibonacci Heaps

Explanation

SecondThen CUT simply removes x from the child-list of y.

ThusThe CASCADING-CUT uses the mark attributes to obtain the desiredtime bounds.

90 / 120

Page 191: 16 Fibonacci Heaps

Explanation

SecondThen CUT simply removes x from the child-list of y.

ThusThe CASCADING-CUT uses the mark attributes to obtain the desiredtime bounds.

90 / 120

Page 192: 16 Fibonacci Heaps

Explanation

The mark label records the following events that happened to y:1 At some time, y was converted into an element of the root list.2 Then, y was linked to (made the child of) another node.3 Then, two children of y were removed by cuts.

As soon as the second child has been lost, we cut y from its parent,making it a new root.

The attribute y.mark is TRUE if steps 1 and 2 have occurred andone child of y has been cut.The CUT procedure, therefore, clears y.mark in line 4, since itperforms step 1.We can now see why line 3 of FIB-HEAP-LINK clears y.mark.

91 / 120

Page 193: 16 Fibonacci Heaps

Explanation

The mark label records the following events that happened to y:1 At some time, y was converted into an element of the root list.2 Then, y was linked to (made the child of) another node.3 Then, two children of y were removed by cuts.

As soon as the second child has been lost, we cut y from its parent,making it a new root.

The attribute y.mark is TRUE if steps 1 and 2 have occurred andone child of y has been cut.The CUT procedure, therefore, clears y.mark in line 4, since itperforms step 1.We can now see why line 3 of FIB-HEAP-LINK clears y.mark.

91 / 120

Page 194: 16 Fibonacci Heaps

Explanation

The mark label records the following events that happened to y:1 At some time, y was converted into an element of the root list.2 Then, y was linked to (made the child of) another node.3 Then, two children of y were removed by cuts.

As soon as the second child has been lost, we cut y from its parent,making it a new root.

The attribute y.mark is TRUE if steps 1 and 2 have occurred andone child of y has been cut.The CUT procedure, therefore, clears y.mark in line 4, since itperforms step 1.We can now see why line 3 of FIB-HEAP-LINK clears y.mark.

91 / 120

Page 195: 16 Fibonacci Heaps

Explanation

The mark label records the following events that happened to y:1 At some time, y was converted into an element of the root list.2 Then, y was linked to (made the child of) another node.3 Then, two children of y were removed by cuts.

As soon as the second child has been lost, we cut y from its parent,making it a new root.

The attribute y.mark is TRUE if steps 1 and 2 have occurred andone child of y has been cut.The CUT procedure, therefore, clears y.mark in line 4, since itperforms step 1.We can now see why line 3 of FIB-HEAP-LINK clears y.mark.

91 / 120

Page 196: 16 Fibonacci Heaps

Explanation

The mark label records the following events that happened to y:1 At some time, y was converted into an element of the root list.2 Then, y was linked to (made the child of) another node.3 Then, two children of y were removed by cuts.

As soon as the second child has been lost, we cut y from its parent,making it a new root.

The attribute y.mark is TRUE if steps 1 and 2 have occurred andone child of y has been cut.The CUT procedure, therefore, clears y.mark in line 4, since itperforms step 1.We can now see why line 3 of FIB-HEAP-LINK clears y.mark.

91 / 120

Page 197: 16 Fibonacci Heaps

Explanation

The mark label records the following events that happened to y:1 At some time, y was converted into an element of the root list.2 Then, y was linked to (made the child of) another node.3 Then, two children of y were removed by cuts.

As soon as the second child has been lost, we cut y from its parent,making it a new root.

The attribute y.mark is TRUE if steps 1 and 2 have occurred andone child of y has been cut.The CUT procedure, therefore, clears y.mark in line 4, since itperforms step 1.We can now see why line 3 of FIB-HEAP-LINK clears y.mark.

91 / 120

Page 198: 16 Fibonacci Heaps

Explanation

Now we have a new problemx might be the second child cut from its parent y to another node.

I Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform acascading-cut on y.

We have three cases:1 If y is a root return.2 if y is not a root and it is unmarked then y is marked.3 If y is not a root and it is marked, then y is CUT and a cascading cut

is performed in its parent z.

Once all the cascading cuts are donethe H .min is updated if necessary

92 / 120

Page 199: 16 Fibonacci Heaps

Explanation

Now we have a new problemx might be the second child cut from its parent y to another node.

I Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform acascading-cut on y.

We have three cases:1 If y is a root return.2 if y is not a root and it is unmarked then y is marked.3 If y is not a root and it is marked, then y is CUT and a cascading cut

is performed in its parent z.

Once all the cascading cuts are donethe H .min is updated if necessary

92 / 120

Page 200: 16 Fibonacci Heaps

Explanation

Now we have a new problemx might be the second child cut from its parent y to another node.

I Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform acascading-cut on y.

We have three cases:1 If y is a root return.2 if y is not a root and it is unmarked then y is marked.3 If y is not a root and it is marked, then y is CUT and a cascading cut

is performed in its parent z.

Once all the cascading cuts are donethe H .min is updated if necessary

92 / 120

Page 201: 16 Fibonacci Heaps

Explanation

Now we have a new problemx might be the second child cut from its parent y to another node.

I Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform acascading-cut on y.

We have three cases:1 If y is a root return.2 if y is not a root and it is unmarked then y is marked.3 If y is not a root and it is marked, then y is CUT and a cascading cut

is performed in its parent z.

Once all the cascading cuts are donethe H .min is updated if necessary

92 / 120

Page 202: 16 Fibonacci Heaps

Explanation

Now we have a new problemx might be the second child cut from its parent y to another node.

I Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform acascading-cut on y.

We have three cases:1 If y is a root return.2 if y is not a root and it is unmarked then y is marked.3 If y is not a root and it is marked, then y is CUT and a cascading cut

is performed in its parent z.

Once all the cascading cuts are donethe H .min is updated if necessary

92 / 120

Page 203: 16 Fibonacci Heaps

Explanation

Now we have a new problemx might be the second child cut from its parent y to another node.

I Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform acascading-cut on y.

We have three cases:1 If y is a root return.2 if y is not a root and it is unmarked then y is marked.3 If y is not a root and it is marked, then y is CUT and a cascading cut

is performed in its parent z.

Once all the cascading cuts are donethe H .min is updated if necessary

92 / 120

Page 204: 16 Fibonacci Heaps

Example

46 is decreased to 15

23

7

1724

18

52

38

3026 46

39 41

35 51

21

93 / 120

Page 205: 16 Fibonacci Heaps

Example

46 is decreased to 15

23

7

1724

18

52

38

3026

39 41

35 51

21

15

94 / 120

Page 206: 16 Fibonacci Heaps

Example

Cut 15 to the root’s list

23

7

1724

18

52

38

3026

39 41

35

51 21

15

95 / 120

Page 207: 16 Fibonacci Heaps

Example

Mark 24 to True

23

7

1724

18

52

38

3026

39 41

35

51 21

15

96 / 120

Page 208: 16 Fibonacci Heaps

Example

Then 35 is decreased to 5

23

7

1724

18

52

38

3026

39 41

5

51 21

15

97 / 120

Page 209: 16 Fibonacci Heaps

Example

Cut 15 to the root’s list

23

7

1724

18

52

38

3026

39 41

5

51 21

15

98 / 120

Page 210: 16 Fibonacci Heaps

Example

Initiate cascade cutting

23

7

1724

18

52

38

3026

39 41

5

51 21

15

99 / 120

Page 211: 16 Fibonacci Heaps

Example

Initiate cascade cutting moving 26 to the root’s list

23

7

1724

18

52

38

30

26

39 41

5

51 21

15

100 / 120

Page 212: 16 Fibonacci Heaps

Example

Mark 26 to false

23

7

1724

18

52

38

30

26

39 41

5

51 21

15

101 / 120

Page 213: 16 Fibonacci Heaps

Example

Move 24 to root’s list

23

7

17

24 18

52

38

30

26

39 41

5

51 21

15

102 / 120

Page 214: 16 Fibonacci Heaps

Example

Mark 26 to false

23

7

17

24 18

52

38

30

26

39 41

5

51 21

15

103 / 120

Page 215: 16 Fibonacci Heaps

Example

Change the H .min

23

7

17

24 18

52

38

30

26

39 41

5

51 21

15

104 / 120

Page 216: 16 Fibonacci Heaps

Potential cost

The procedure Decrease-Key takesci = O(1)+the cascading-cuts

Assume that you require c calls to cascade CASCADING-CUTOne for the line 6 at the code FIB-HEAP-DECREASE-KEY followedby c − 1 others

I The cost of it will take ci = O(c).

105 / 120

Page 217: 16 Fibonacci Heaps

Potential cost

The procedure Decrease-Key takesci = O(1)+the cascading-cuts

Assume that you require c calls to cascade CASCADING-CUTOne for the line 6 at the code FIB-HEAP-DECREASE-KEY followedby c − 1 others

I The cost of it will take ci = O(c).

105 / 120

Page 218: 16 Fibonacci Heaps

Potential cost

The procedure Decrease-Key takesci = O(1)+the cascading-cuts

Assume that you require c calls to cascade CASCADING-CUTOne for the line 6 at the code FIB-HEAP-DECREASE-KEY followedby c − 1 others

I The cost of it will take ci = O(c).

105 / 120

Page 219: 16 Fibonacci Heaps

Potential cost

Finally, assuming H ′ is the new Fibonacci Heap and H the old one

Φ(H ′)

= (t (H ) + c) + 2 (m (H )− (c − 1) + 1) (2)

Where:t (H ) + c

I The # original trees + the ones created by the c calls.

m (H )− (c − 1) + 1I The original marks - (c − 1) cleared marks by Cut + the

branch to y.mark == FALSE true.

106 / 120

Page 220: 16 Fibonacci Heaps

Potential cost

Finally, assuming H ′ is the new Fibonacci Heap and H the old one

Φ(H ′)

= (t (H ) + c) + 2 (m (H )− (c − 1) + 1) (2)

Where:t (H ) + c

I The # original trees + the ones created by the c calls.

m (H )− (c − 1) + 1I The original marks - (c − 1) cleared marks by Cut + the

branch to y.mark == FALSE true.

106 / 120

Page 221: 16 Fibonacci Heaps

Potential cost

Finally, assuming H ′ is the new Fibonacci Heap and H the old one

Φ(H ′)

= (t (H ) + c) + 2 (m (H )− (c − 1) + 1) (2)

Where:t (H ) + c

I The # original trees + the ones created by the c calls.

m (H )− (c − 1) + 1I The original marks - (c − 1) cleared marks by Cut + the

branch to y.mark == FALSE true.

106 / 120

Page 222: 16 Fibonacci Heaps

Potential cost

Finally, assuming H ′ is the new Fibonacci Heap and H the old one

Φ(H ′)

= (t (H ) + c) + 2 (m (H )− (c − 1) + 1) (2)

Where:t (H ) + c

I The # original trees + the ones created by the c calls.

m (H )− (c − 1) + 1I The original marks - (c − 1) cleared marks by Cut + the

branch to y.mark == FALSE true.

106 / 120

Page 223: 16 Fibonacci Heaps

Final change in potentialThus

Φ(H ′)

= t (H ) + c + 2(m(H )− c + 2) = t (H ) + 2m (H )− c + 4 (3)

Then, we have that the amortized cost is

ci = ci + t (H ) + 2m (H )− c + 4− t (H )− 2m (H )= ci + 4− c = O(c) + 4− c = O(1)

ObservationNow we can see why the term 2m(H ):

One unit to pay for the cut and clearing the marking.One unit for making of a node a root.

107 / 120

Page 224: 16 Fibonacci Heaps

Final change in potentialThus

Φ(H ′)

= t (H ) + c + 2(m(H )− c + 2) = t (H ) + 2m (H )− c + 4 (3)

Then, we have that the amortized cost is

ci = ci + t (H ) + 2m (H )− c + 4− t (H )− 2m (H )= ci + 4− c = O(c) + 4− c = O(1)

ObservationNow we can see why the term 2m(H ):

One unit to pay for the cut and clearing the marking.One unit for making of a node a root.

107 / 120

Page 225: 16 Fibonacci Heaps

Final change in potentialThus

Φ(H ′)

= t (H ) + c + 2(m(H )− c + 2) = t (H ) + 2m (H )− c + 4 (3)

Then, we have that the amortized cost is

ci = ci + t (H ) + 2m (H )− c + 4− t (H )− 2m (H )= ci + 4− c = O(c) + 4− c = O(1)

ObservationNow we can see why the term 2m(H ):

One unit to pay for the cut and clearing the marking.One unit for making of a node a root.

107 / 120

Page 226: 16 Fibonacci Heaps

Final change in potentialThus

Φ(H ′)

= t (H ) + c + 2(m(H )− c + 2) = t (H ) + 2m (H )− c + 4 (3)

Then, we have that the amortized cost is

ci = ci + t (H ) + 2m (H )− c + 4− t (H )− 2m (H )= ci + 4− c = O(c) + 4− c = O(1)

ObservationNow we can see why the term 2m(H ):

One unit to pay for the cut and clearing the marking.One unit for making of a node a root.

107 / 120

Page 227: 16 Fibonacci Heaps

Final change in potentialThus

Φ(H ′)

= t (H ) + c + 2(m(H )− c + 2) = t (H ) + 2m (H )− c + 4 (3)

Then, we have that the amortized cost is

ci = ci + t (H ) + 2m (H )− c + 4− t (H )− 2m (H )= ci + 4− c = O(c) + 4− c = O(1)

ObservationNow we can see why the term 2m(H ):

One unit to pay for the cut and clearing the marking.One unit for making of a node a root.

107 / 120

Page 228: 16 Fibonacci Heaps

Final change in potentialThus

Φ(H ′)

= t (H ) + c + 2(m(H )− c + 2) = t (H ) + 2m (H )− c + 4 (3)

Then, we have that the amortized cost is

ci = ci + t (H ) + 2m (H )− c + 4− t (H )− 2m (H )= ci + 4− c = O(c) + 4− c = O(1)

ObservationNow we can see why the term 2m(H ):

One unit to pay for the cut and clearing the marking.One unit for making of a node a root.

107 / 120

Page 229: 16 Fibonacci Heaps

Delete a node

It is easy to delete a node in the Fibonacci heap following the nextcode

Fib-Heap-Delete(H , x)1 Fib-Heap-Decrease-Key(H , x,−∞)2 Fib-Heap-Extract-Min(H )

Again, the cost is O(D(n))

108 / 120

Page 230: 16 Fibonacci Heaps

Proving the D (n) bound!!!

Let’s define the followingWe define a quantity size(x)= the number of nodes at subtree rooted atx, x itself.

The key to the analysis is as follows:We shall show that size (x) is exponential in x.degree.x.degree is always maintained as an accurate count of the degree of x.

109 / 120

Page 231: 16 Fibonacci Heaps

Proving the D (n) bound!!!

Let’s define the followingWe define a quantity size(x)= the number of nodes at subtree rooted atx, x itself.

The key to the analysis is as follows:We shall show that size (x) is exponential in x.degree.x.degree is always maintained as an accurate count of the degree of x.

109 / 120

Page 232: 16 Fibonacci Heaps

Proving the D (n) bound!!!

Let’s define the followingWe define a quantity size(x)= the number of nodes at subtree rooted atx, x itself.

The key to the analysis is as follows:We shall show that size (x) is exponential in x.degree.x.degree is always maintained as an accurate count of the degree of x.

109 / 120

Page 233: 16 Fibonacci Heaps

Now...Lemma 19.1Let x be any node in a Fibonacci heap, and suppose that x.degree = k.Let y1, y2, ..., yk denote the children of x in the order in which they werelinked to x, from the earliest to the latest. Then y1.degree ≥ 0 andyi .degree ≥ i − 2 for i = 2, 3, ..., k.

Proof1 Obviously, y1.degree ≥ 0.2 For i ≥ 2, yi was linked to x, all of y1, y2, ..., yi−1were children of x,

so we must have had x.degree ≥ i − 1.3 Node yi is linked to x only if we had x.degree = yi .degree, so we

must have also had yi .degree ≥ i − 1 when node yi was linked to x.4 Since then, node yi has lost at most one child.

I Note: It would have been cut from x if it had lost two children.5 We conclude that yi .degree ≥ i − 2.

110 / 120

Page 234: 16 Fibonacci Heaps

Now...Lemma 19.1Let x be any node in a Fibonacci heap, and suppose that x.degree = k.Let y1, y2, ..., yk denote the children of x in the order in which they werelinked to x, from the earliest to the latest. Then y1.degree ≥ 0 andyi .degree ≥ i − 2 for i = 2, 3, ..., k.

Proof1 Obviously, y1.degree ≥ 0.2 For i ≥ 2, yi was linked to x, all of y1, y2, ..., yi−1were children of x,

so we must have had x.degree ≥ i − 1.3 Node yi is linked to x only if we had x.degree = yi .degree, so we

must have also had yi .degree ≥ i − 1 when node yi was linked to x.4 Since then, node yi has lost at most one child.

I Note: It would have been cut from x if it had lost two children.5 We conclude that yi .degree ≥ i − 2.

110 / 120

Page 235: 16 Fibonacci Heaps

Now...Lemma 19.1Let x be any node in a Fibonacci heap, and suppose that x.degree = k.Let y1, y2, ..., yk denote the children of x in the order in which they werelinked to x, from the earliest to the latest. Then y1.degree ≥ 0 andyi .degree ≥ i − 2 for i = 2, 3, ..., k.

Proof1 Obviously, y1.degree ≥ 0.2 For i ≥ 2, yi was linked to x, all of y1, y2, ..., yi−1were children of x,

so we must have had x.degree ≥ i − 1.3 Node yi is linked to x only if we had x.degree = yi .degree, so we

must have also had yi .degree ≥ i − 1 when node yi was linked to x.4 Since then, node yi has lost at most one child.

I Note: It would have been cut from x if it had lost two children.5 We conclude that yi .degree ≥ i − 2.

110 / 120

Page 236: 16 Fibonacci Heaps

Now...Lemma 19.1Let x be any node in a Fibonacci heap, and suppose that x.degree = k.Let y1, y2, ..., yk denote the children of x in the order in which they werelinked to x, from the earliest to the latest. Then y1.degree ≥ 0 andyi .degree ≥ i − 2 for i = 2, 3, ..., k.

Proof1 Obviously, y1.degree ≥ 0.2 For i ≥ 2, yi was linked to x, all of y1, y2, ..., yi−1were children of x,

so we must have had x.degree ≥ i − 1.3 Node yi is linked to x only if we had x.degree = yi .degree, so we

must have also had yi .degree ≥ i − 1 when node yi was linked to x.4 Since then, node yi has lost at most one child.

I Note: It would have been cut from x if it had lost two children.5 We conclude that yi .degree ≥ i − 2.

110 / 120

Page 237: 16 Fibonacci Heaps

Now...Lemma 19.1Let x be any node in a Fibonacci heap, and suppose that x.degree = k.Let y1, y2, ..., yk denote the children of x in the order in which they werelinked to x, from the earliest to the latest. Then y1.degree ≥ 0 andyi .degree ≥ i − 2 for i = 2, 3, ..., k.

Proof1 Obviously, y1.degree ≥ 0.2 For i ≥ 2, yi was linked to x, all of y1, y2, ..., yi−1were children of x,

so we must have had x.degree ≥ i − 1.3 Node yi is linked to x only if we had x.degree = yi .degree, so we

must have also had yi .degree ≥ i − 1 when node yi was linked to x.4 Since then, node yi has lost at most one child.

I Note: It would have been cut from x if it had lost two children.5 We conclude that yi .degree ≥ i − 2.

110 / 120

Page 238: 16 Fibonacci Heaps

Now...Lemma 19.1Let x be any node in a Fibonacci heap, and suppose that x.degree = k.Let y1, y2, ..., yk denote the children of x in the order in which they werelinked to x, from the earliest to the latest. Then y1.degree ≥ 0 andyi .degree ≥ i − 2 for i = 2, 3, ..., k.

Proof1 Obviously, y1.degree ≥ 0.2 For i ≥ 2, yi was linked to x, all of y1, y2, ..., yi−1were children of x,

so we must have had x.degree ≥ i − 1.3 Node yi is linked to x only if we had x.degree = yi .degree, so we

must have also had yi .degree ≥ i − 1 when node yi was linked to x.4 Since then, node yi has lost at most one child.

I Note: It would have been cut from x if it had lost two children.5 We conclude that yi .degree ≥ i − 2.

110 / 120

Page 239: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

111 / 120

Page 240: 16 Fibonacci Heaps

Why Fibonacci?

The kth Fibonacci number is defined by the recurrence

Fk =

0 if k = 01 if k = 1Fk−1 + Fk−2 if k ≥ 2

Lemma 19.2For al integers k ≥ 0

Fk+2 = 1 +k∑

i=0Fi

Proof by induction k = 0....

112 / 120

Page 241: 16 Fibonacci Heaps

Why Fibonacci?

The kth Fibonacci number is defined by the recurrence

Fk =

0 if k = 01 if k = 1Fk−1 + Fk−2 if k ≥ 2

Lemma 19.2For al integers k ≥ 0

Fk+2 = 1 +k∑

i=0Fi

Proof by induction k = 0....

112 / 120

Page 242: 16 Fibonacci Heaps

The use of the Golden Ratio

Lemma 19.3Let x be any node in a Fibonacci heap, and let k = x.degree. Then,Fk+2 ≥ Φk , where Φ = 1+

√5

2 (The golden ratio).

113 / 120

Page 243: 16 Fibonacci Heaps

Golden Ratio

Building the Golden Ratio1 Construct a unit square.2 Draw a line from the midpoint of one side to an opposite corner.3 Use that line as a radius for a circle to define a rectangle.

114 / 120

Page 244: 16 Fibonacci Heaps

Golden Ratio

Building the Golden Ratio1 Construct a unit square.2 Draw a line from the midpoint of one side to an opposite corner.3 Use that line as a radius for a circle to define a rectangle.

114 / 120

Page 245: 16 Fibonacci Heaps

Golden RatioBuilding the Golden Ratio

1 Construct a unit square.2 Draw a line from the midpoint of one side to an opposite corner.3 Use that line as a radius for a circle to define a rectangle.

1

114 / 120

Page 246: 16 Fibonacci Heaps

The use of the Golden Ratio

Lemma 19.4Let x be any node in a Fibonacci Heap, k = x.degree. Thensize(x) ≥ Fk+2 ≥ Φk .

ProofLet sk denote the minimum value for size(x) over all nodes x suchthat x.degree = k.Restrictions for sk :

I sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).

As in the previous lemmay1, y2, ..., yk denote the children of x in the order they were linked tox.

115 / 120

Page 247: 16 Fibonacci Heaps

The use of the Golden Ratio

Lemma 19.4Let x be any node in a Fibonacci Heap, k = x.degree. Thensize(x) ≥ Fk+2 ≥ Φk .

ProofLet sk denote the minimum value for size(x) over all nodes x suchthat x.degree = k.Restrictions for sk :

I sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).

As in the previous lemmay1, y2, ..., yk denote the children of x in the order they were linked tox.

115 / 120

Page 248: 16 Fibonacci Heaps

The use of the Golden Ratio

Lemma 19.4Let x be any node in a Fibonacci Heap, k = x.degree. Thensize(x) ≥ Fk+2 ≥ Φk .

ProofLet sk denote the minimum value for size(x) over all nodes x suchthat x.degree = k.Restrictions for sk :

I sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).

As in the previous lemmay1, y2, ..., yk denote the children of x in the order they were linked tox.

115 / 120

Page 249: 16 Fibonacci Heaps

The use of the Golden Ratio

Lemma 19.4Let x be any node in a Fibonacci Heap, k = x.degree. Thensize(x) ≥ Fk+2 ≥ Φk .

ProofLet sk denote the minimum value for size(x) over all nodes x suchthat x.degree = k.Restrictions for sk :

I sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).

As in the previous lemmay1, y2, ..., yk denote the children of x in the order they were linked tox.

115 / 120

Page 250: 16 Fibonacci Heaps

The use of the Golden Ratio

Lemma 19.4Let x be any node in a Fibonacci Heap, k = x.degree. Thensize(x) ≥ Fk+2 ≥ Φk .

ProofLet sk denote the minimum value for size(x) over all nodes x suchthat x.degree = k.Restrictions for sk :

I sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).

As in the previous lemmay1, y2, ..., yk denote the children of x in the order they were linked tox.

115 / 120

Page 251: 16 Fibonacci Heaps

Example

For example

NowLook at the board

116 / 120

Page 252: 16 Fibonacci Heaps

Example

For example

NowLook at the board

116 / 120

Page 253: 16 Fibonacci Heaps

The use of the Golden Ratio

Proof continuationNow, we need to proof that sk ≥ Fk+2

The cases for k = 0→ s0 = 1, and k = 1→ s1 = 2 are trivial because

F2 = F1 + F0 = 1 + 0 = 1 (4)

F3 = F2 + F1 = 1 + 1 = 2 (5)

117 / 120

Page 254: 16 Fibonacci Heaps

The use of the Golden Ratio

Proof continuationNow, we need to proof that sk ≥ Fk+2

The cases for k = 0→ s0 = 1, and k = 1→ s1 = 2 are trivial because

F2 = F1 + F0 = 1 + 0 = 1 (4)

F3 = F2 + F1 = 1 + 1 = 2 (5)

117 / 120

Page 255: 16 Fibonacci Heaps

The use of the Golden Ratio

Proof continuationNow, we need to proof that sk ≥ Fk+2

The cases for k = 0→ s0 = 1, and k = 1→ s1 = 2 are trivial because

F2 = F1 + F0 = 1 + 0 = 1 (4)

F3 = F2 + F1 = 1 + 1 = 2 (5)

117 / 120

Page 256: 16 Fibonacci Heaps

Finally

Corollary 19.5The maximum degree D(n) of any node in an n-node Fibonacci heap isO(log n).

ProofLook at the board!

118 / 120

Page 257: 16 Fibonacci Heaps

Finally

Corollary 19.5The maximum degree D(n) of any node in an n-node Fibonacci heap isO(log n).

ProofLook at the board!

118 / 120

Page 258: 16 Fibonacci Heaps

Outline1 Introduction

Basic DefinitionsOrdered Trees

2 Binomial TreesExample

3 Fibonacci HeapOperationsFibonacci HeapWhy Fibonacci Heaps?Node StructureFibonacci Heaps OperationsMergeable-Heaps operations - Make HeapMergeable-Heaps operations - InsertionMergeable-Heaps operations - MinimumMergeable-Heaps operations - Union

Complexity AnalysisConsolidate AlgorithmPotential costOperation: Decreasing a KeyWhy Fibonacci?

4 ExercisesSome Exercises that you can try

119 / 120

Page 259: 16 Fibonacci Heaps

Exercises

From Cormen’s book solve the following20.2-220.2-320.2-420.3-120.3-220.4-120.4-2

120 / 120