SlideShare uma empresa Scribd logo
1 de 259
Baixar para ler offline
Analysis of Algorithms
Fibonacci Heaps
Andres Mendez-Vazquez
October 29, 2015
1 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
2 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
3 / 120
Some previous definitions
Free Tree
A free tree is a connected acyclic undirected graph.
Rooted Tree
A rooted tree is a free tree in which one of the nodes is a root.
Ordered Tree
An ordered tree is a rooted tree where the children are ordered.
4 / 120
Some previous definitions
Free Tree
A free tree is a connected acyclic undirected graph.
Rooted Tree
A rooted tree is a free tree in which one of the nodes is a root.
Ordered Tree
An ordered tree is a rooted tree where the children are ordered.
4 / 120
Some previous definitions
Free Tree
A free tree is a connected acyclic undirected graph.
Rooted Tree
A rooted tree is a free tree in which one of the nodes is a root.
Ordered Tree
An ordered tree is a rooted tree where the children are ordered.
4 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
5 / 120
Ordered Tree
Definition
An ordered tree is an oriented tree in which the children of a node are
somehow "ordered."
Example
Thus
If T1 and T2 are ordered trees then T1 = T2 else T1 = T2.
6 / 120
Ordered Tree
Definition
An ordered tree is an oriented tree in which the children of a node are
somehow "ordered."
Example
a
b c
a
bc
Thus
If T1 and T2 are ordered trees then T1 = T2 else T1 = T2.
6 / 120
Ordered Tree
Definition
An ordered tree is an oriented tree in which the children of a node are
somehow "ordered."
Example
a
b c
a
bc
Thus
If T1 and T2 are ordered trees then T1 = T2 else T1 = T2.
6 / 120
Some previous definitions
Types of Ordered Trees
There are several types of ordered trees:
k-ary tree
Binomial tree
Fibonacci tree
Binomial Tree
A binomial tree is an ordered tree defined recursively.
7 / 120
Some previous definitions
Types of Ordered Trees
There are several types of ordered trees:
k-ary tree
Binomial tree
Fibonacci tree
Binomial Tree
A binomial tree is an ordered tree defined recursively.
7 / 120
Some previous definitions
Types of Ordered Trees
There are several types of ordered trees:
k-ary tree
Binomial tree
Fibonacci tree
Binomial Tree
A binomial tree is an ordered tree defined recursively.
7 / 120
Some previous definitions
Types of Ordered Trees
There are several types of ordered trees:
k-ary tree
Binomial tree
Fibonacci tree
Binomial Tree
A binomial tree is an ordered tree defined recursively.
7 / 120
Some previous definitions
Types of Ordered Trees
There are several types of ordered trees:
k-ary tree
Binomial tree
Fibonacci tree
Binomial Tree
A binomial tree is an ordered tree defined recursively.
7 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
8 / 120
Examples
Recursive Structure
1
2
3
4
0
9 / 120
This can be seen too as
Recursive Structure
10 / 120
Properties of binomial trees
Lemma 19.1
For the binomial tree Bk:
1 There are 2k nodes.
2 The height of the tree is k.
3 There are exactly k
i 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 right
by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi.
Proof!
Look at the white-board.
11 / 120
Properties of binomial trees
Lemma 19.1
For the binomial tree Bk:
1 There are 2k nodes.
2 The height of the tree is k.
3 There are exactly k
i 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 right
by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi.
Proof!
Look at the white-board.
11 / 120
Properties of binomial trees
Lemma 19.1
For the binomial tree Bk:
1 There are 2k nodes.
2 The height of the tree is k.
3 There are exactly k
i 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 right
by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi.
Proof!
Look at the white-board.
11 / 120
Properties of binomial trees
Lemma 19.1
For the binomial tree Bk:
1 There are 2k nodes.
2 The height of the tree is k.
3 There are exactly k
i 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 right
by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi.
Proof!
Look at the white-board.
11 / 120
Properties of binomial trees
Lemma 19.1
For the binomial tree Bk:
1 There are 2k nodes.
2 The height of the tree is k.
3 There are exactly k
i 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 right
by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi.
Proof!
Look at the white-board.
11 / 120
Properties of binomial trees
Lemma 19.1
For the binomial tree Bk:
1 There are 2k nodes.
2 The height of the tree is k.
3 There are exactly k
i 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 right
by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi.
Proof!
Look at the white-board.
11 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
12 / 120
Mergeable Heap Operations
The Fibonacci heap data structure is used to support the operations
“meargeable heap” operations
1. 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
Mergeable Heap Operations
The Fibonacci heap data structure is used to support the operations
“meargeable heap” operations
1. 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
Mergeable Heap Operations
The Fibonacci heap data structure is used to support the operations
“meargeable heap” operations
1. 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
Mergeable Heap Operations
The Fibonacci heap data structure is used to support the operations
“meargeable heap” operations
1. 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
Mergeable Heap Operations
The Fibonacci heap data structure is used to support the operations
“meargeable heap” operations
5. UNION(H1, H2) creates and returns a new heap that contains all the
elements of heaps H1 and H2. Heaps are “destroyed” by this
operation.
6. DECREASE-KEY(H, x, k) assigns to element x within heap H the
new key value k.
7. DELETE(H, x) deletes element x from heap H.
14 / 120
Mergeable Heap Operations
The Fibonacci heap data structure is used to support the operations
“meargeable heap” operations
5. UNION(H1, H2) creates and returns a new heap that contains all the
elements of heaps H1 and H2. Heaps are “destroyed” by this
operation.
6. DECREASE-KEY(H, x, k) assigns to element x within heap H the
new key value k.
7. DELETE(H, x) deletes element x from heap H.
14 / 120
Mergeable Heap Operations
The Fibonacci heap data structure is used to support the operations
“meargeable heap” operations
5. UNION(H1, H2) creates and returns a new heap that contains all the
elements of heaps H1 and H2. Heaps are “destroyed” by this
operation.
6. DECREASE-KEY(H, x, k) assigns to element x within heap H the
new key value k.
7. DELETE(H, x) deletes element x from heap H.
14 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
15 / 120
Fibonacci Heap
Definition
A Fibonacci heap is a collection of rooted trees that are min-heap ordered.
Meaning
Each 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 binomial
tree except that the root of one tree is made any child of the root of
the other.
16 / 120
Fibonacci Heap
Definition
A Fibonacci heap is a collection of rooted trees that are min-heap ordered.
Meaning
Each 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 binomial
tree except that the root of one tree is made any child of the root of
the other.
16 / 120
Fibonacci Heap
Definition
A Fibonacci heap is a collection of rooted trees that are min-heap ordered.
Meaning
Each 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 binomial
tree except that the root of one tree is made any child of the root of
the other.
16 / 120
Fibonacci Heap
Definition
A Fibonacci heap is a collection of rooted trees that are min-heap ordered.
Meaning
Each 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 binomial
tree except that the root of one tree is made any child of the root of
the other.
16 / 120
Fibonacci Structure
Example
23 7 3 17 24
18 52 38
39 41
30 26 46
35
17 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
18 / 120
Why Fibonacci Heaps?
Fibonacci Heaps facts
Fibonacci heaps are especially desirable when the number of calls to
Extract-Min and Delete is small.
All other operations run in O(1).
Applications
Fibonacci heaps may be used in many applications. Some graph problems,
like minimum spanning tree and single-source-shortest-path.
19 / 120
Why Fibonacci Heaps?
Fibonacci Heaps facts
Fibonacci heaps are especially desirable when the number of calls to
Extract-Min and Delete is small.
All other operations run in O(1).
Applications
Fibonacci heaps may be used in many applications. Some graph problems,
like minimum spanning tree and single-source-shortest-path.
19 / 120
It is more...
We have that
Procedure 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
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
21 / 120
Fields in each node
The Classic Ones
Each node contains a x.parent and x.child field.
The ones for the doubled linked list
The children of each a node x are linked together in a circular double
linked list:
Each child y of x has a y.left and y.right to do this.
22 / 120
Fields in each node
The Classic Ones
Each node contains a x.parent and x.child field.
The ones for the doubled linked list
The children of each a node x are linked together in a circular double
linked list:
Each child y of x has a y.left and y.right to do this.
22 / 120
Thus, we have the following important labels
Field degree
Did you notice that there in no way to find the number of children
unless you have complex exploratory method?
We store the number of children in the child list of node x in x.degree.
The Amortized Label
Each child has the field mark.
IMPORTANT
1 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 another
node.
23 / 120
Thus, we have the following important labels
Field degree
Did you notice that there in no way to find the number of children
unless you have complex exploratory method?
We store the number of children in the child list of node x in x.degree.
The Amortized Label
Each child has the field mark.
IMPORTANT
1 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 another
node.
23 / 120
Thus, we have the following important labels
Field degree
Did you notice that there in no way to find the number of children
unless you have complex exploratory method?
We store the number of children in the child list of node x in x.degree.
The Amortized Label
Each child has the field mark.
IMPORTANT
1 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 another
node.
23 / 120
Thus, we have the following important labels
Field degree
Did you notice that there in no way to find the number of children
unless you have complex exploratory method?
We store the number of children in the child list of node x in x.degree.
The Amortized Label
Each child has the field mark.
IMPORTANT
1 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 another
node.
23 / 120
Thus, we have the following important labels
Field degree
Did you notice that there in no way to find the number of children
unless you have complex exploratory method?
We store the number of children in the child list of node x in x.degree.
The Amortized Label
Each child has the field mark.
IMPORTANT
1 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 another
node.
23 / 120
The child list
Circular, doubly linked list have two advantages for use in Fibonacci
heaps:
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
The child list
Circular, doubly linked list have two advantages for use in Fibonacci
heaps:
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
The child list
Circular, doubly linked list have two advantages for use in Fibonacci
heaps:
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
Additional
First
The roots of all the trees in a Fibonacci heap H are linked together using
their left and right pointers into a circular, doubly linked list called the
root list of the Fibonacci heap.
Second
The pointer H.min of the Fibonacci data structure thus points to the
node in the root list whose key is minimum.
Trees may appear in any order within a root list.
Third
The Fibonacci data structure has the field H.n =the number of nodes
currently in the Fibonacci Heap H.
25 / 120
Additional
First
The roots of all the trees in a Fibonacci heap H are linked together using
their left and right pointers into a circular, doubly linked list called the
root list of the Fibonacci heap.
Second
The pointer H.min of the Fibonacci data structure thus points to the
node in the root list whose key is minimum.
Trees may appear in any order within a root list.
Third
The Fibonacci data structure has the field H.n =the number of nodes
currently in the Fibonacci Heap H.
25 / 120
Additional
First
The roots of all the trees in a Fibonacci heap H are linked together using
their left and right pointers into a circular, doubly linked list called the
root list of the Fibonacci heap.
Second
The pointer H.min of the Fibonacci data structure thus points to the
node in the root list whose key is minimum.
Trees may appear in any order within a root list.
Third
The Fibonacci data structure has the field H.n =the number of nodes
currently in the Fibonacci Heap H.
25 / 120
Additional
First
The roots of all the trees in a Fibonacci heap H are linked together using
their left and right pointers into a circular, doubly linked list called the
root list of the Fibonacci heap.
Second
The pointer H.min of the Fibonacci data structure thus points to the
node in the root list whose key is minimum.
Trees may appear in any order within a root list.
Third
The Fibonacci data structure has the field H.n =the number of nodes
currently in the Fibonacci Heap H.
25 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
26 / 120
Idea behind Fibonacci heaps
Main idea
Fibonacci heaps are called lazy data structures because they delay work as
long as possible using the field mark!!!
27 / 120
Make Heap
Make a heap
You only need the following code:
MakeHeap()
1 min[H] = NIL
2 n(H) = 0
Complexity is as simple as
Cost O(1).
28 / 120
Make Heap
Make a heap
You only need the following code:
MakeHeap()
1 min[H] = NIL
2 n(H) = 0
Complexity is as simple as
Cost O(1).
28 / 120
Insertion
Code for Inserting a node
Fib-Heap-Insert(H, x)
1 x.degree = 0
2 x.p = NIL
3 x.child = NIL
4 x.mark = FALSE
5 if H.min = NIL
6 Create a root list for H containing just x
7 H.min = x
8 else insert x into H s root list
9 if x.key < H.min.key
10 H.min = x
11 H.n = H.n + 1
29 / 120
Insertion
Code for Inserting a node
Fib-Heap-Insert(H, x)
1 x.degree = 0
2 x.p = NIL
3 x.child = NIL
4 x.mark = FALSE
5 if H.min = NIL
6 Create a root list for H containing just x
7 H.min = x
8 else insert x into H s root list
9 if x.key < H.min.key
10 H.min = x
11 H.n = H.n + 1
29 / 120
Insertion
Code for Inserting a node
Fib-Heap-Insert(H, x)
1 x.degree = 0
2 x.p = NIL
3 x.child = NIL
4 x.mark = FALSE
5 if H.min = NIL
6 Create a root list for H containing just x
7 H.min = x
8 else insert x into H s root list
9 if x.key < H.min.key
10 H.min = x
11 H.n = H.n + 1
29 / 120
Insertion
Code for Inserting a node
Fib-Heap-Insert(H, x)
1 x.degree = 0
2 x.p = NIL
3 x.child = NIL
4 x.mark = FALSE
5 if H.min = NIL
6 Create a root list for H containing just x
7 H.min = x
8 else insert x into H s root list
9 if x.key < H.min.key
10 H.min = x
11 H.n = H.n + 1
29 / 120
Inserting a node
Example
23 7 3 17 24
18 52 38
39 41
30 26 46
35 51
30 / 120
Inserting a node
Example
23 7 3 17 24
18 52 38 30 26 46
39 41 35 51
21
31 / 120
Minimum
Finding the Minimum
Simply return the key of min(H).
The amortized cost is simply O (1).
We will analyze this later on...
32 / 120
Minimum
Finding the Minimum
Simply return the key of min(H).
The amortized cost is simply O (1).
We will analyze this later on...
32 / 120
What about Union of two Heaps?
Code for Union of Heaps
Fib-Heap-Union(H1, H2)
1 H =Make-Fib-Heap()
2 H.min = H1.min
3 Concatenate the root list of H2 with the root list of H
4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key)
5 H.min = H2.min
6 H.n = H1.n + H2.n
7 return H
33 / 120
What about Union of two Heaps?
Code for Union of Heaps
Fib-Heap-Union(H1, H2)
1 H =Make-Fib-Heap()
2 H.min = H1.min
3 Concatenate the root list of H2 with the root list of H
4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key)
5 H.min = H2.min
6 H.n = H1.n + H2.n
7 return H
33 / 120
What about Union of two Heaps?
Code for Union of Heaps
Fib-Heap-Union(H1, H2)
1 H =Make-Fib-Heap()
2 H.min = H1.min
3 Concatenate the root list of H2 with the root list of H
4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key)
5 H.min = H2.min
6 H.n = H1.n + H2.n
7 return H
33 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
34 / 120
In order to analyze Union...
We introduce some ideas from...
Our old friend amortized analysis and potential method!!!
35 / 120
Amortized potential function
We have the following function
Φ(H) = t(H) + 2m(H)
Where:
t(H) is the number of trees in the Fibonacci heap
m(H) is the number of marked nodes in the tree.
Amortized analysis
The 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
Amortized potential function
We have the following function
Φ(H) = t(H) + 2m(H)
Where:
t(H) is the number of trees in the Fibonacci heap
m(H) is the number of marked nodes in the tree.
Amortized analysis
The 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
Amortized potential function
We have the following function
Φ(H) = t(H) + 2m(H)
Where:
t(H) is the number of trees in the Fibonacci heap
m(H) is the number of marked nodes in the tree.
Amortized analysis
The 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
Amortized potential function
We have the following function
Φ(H) = t(H) + 2m(H)
Where:
t(H) is the number of trees in the Fibonacci heap
m(H) is the number of marked nodes in the tree.
Amortized analysis
The 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
Amortized potential function
We have the following function
Φ(H) = t(H) + 2m(H)
Where:
t(H) is the number of trees in the Fibonacci heap
m(H) is the number of marked nodes in the tree.
Amortized analysis
The 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
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, then
D(n) = log n.
We will prove this latter!!!
37 / 120
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, then
D(n) = log n.
We will prove this latter!!!
37 / 120
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, then
D(n) = log n.
We will prove this latter!!!
37 / 120
Back to Insertion
First
If H is the Fibonacci heap after inserting, and H before that:
t(H ) = t(H) + 1
Second
m(H ) = m(H)
Then the change of potential is
Φ(H ) − Φ(H) = 1 then complexity analysis results in O(1) + 1 = O
38 / 120
Back to Insertion
First
If H is the Fibonacci heap after inserting, and H before that:
t(H ) = t(H) + 1
Second
m(H ) = m(H)
Then the change of potential is
Φ(H ) − Φ(H) = 1 then complexity analysis results in O(1) + 1 = O
38 / 120
Back to Insertion
First
If H is the Fibonacci heap after inserting, and H before that:
t(H ) = t(H) + 1
Second
m(H ) = m(H)
Then the change of potential is
Φ(H ) − Φ(H) = 1 then complexity analysis results in O(1) + 1 = O
38 / 120
Other operations: Find Min
It is possible to rephrase this in terms of potential cost
By using the pointer min[H] potential cost is 0 then O(1).
39 / 120
Other operations: Union
Union of two Fibonacci heaps
Fib-Heap-Union(H1, H2)
1 H =Make-Fib-Heap()
2 H.min = H1.min
3 Concatenate the root list of H2 with the root list of H
4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key)
5 H.min = H2.min
6 H.n = H1.n + H2.n
7 return H
40 / 120
Other operations: Union
Union of two Fibonacci heaps
Fib-Heap-Union(H1, H2)
1 H =Make-Fib-Heap()
2 H.min = H1.min
3 Concatenate the root list of H2 with the root list of H
4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key)
5 H.min = H2.min
6 H.n = H1.n + H2.n
7 return H
40 / 120
Cost of uniting two Fibonacci heaps
First
t(H) = t (H1) + t (H2) and m(H) = m (H1) + m (H2).
Second
ci = O(1) this is because the number of steps to make the union
operations is a constant.
Potential analysis
ci = ci + Φ (H) − [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1).
We have then a complexity of O(1).
41 / 120
Cost of uniting two Fibonacci heaps
First
t(H) = t (H1) + t (H2) and m(H) = m (H1) + m (H2).
Second
ci = O(1) this is because the number of steps to make the union
operations is a constant.
Potential analysis
ci = ci + Φ (H) − [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1).
We have then a complexity of O(1).
41 / 120
Cost of uniting two Fibonacci heaps
First
t(H) = t (H1) + t (H2) and m(H) = m (H1) + m (H2).
Second
ci = O(1) this is because the number of steps to make the union
operations is a constant.
Potential analysis
ci = ci + Φ (H) − [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1).
We have then a complexity of O(1).
41 / 120
Extract min
Extract min
Fib-Heap-Extract-Min(H)
1 z = H.min
2 if z = NIL
3 for each child x of z
4 add x to the root list of H
5 x.p = NIL
6 remove z from the root list of H
7 if z == z.right
8 H.min = NIL
9 else H.min = z.right
10 Consolidate(H)
11 H.n = H.n − 1
12 return z
42 / 120
Extract min
Extract min
Fib-Heap-Extract-Min(H)
1 z = H.min
2 if z = NIL
3 for each child x of z
4 add x to the root list of H
5 x.p = NIL
6 remove z from the root list of H
7 if z == z.right
8 H.min = NIL
9 else H.min = z.right
10 Consolidate(H)
11 H.n = H.n − 1
12 return z
42 / 120
Extract min
Extract min
Fib-Heap-Extract-Min(H)
1 z = H.min
2 if z = NIL
3 for each child x of z
4 add x to the root list of H
5 x.p = NIL
6 remove z from the root list of H
7 if z == z.right
8 H.min = NIL
9 else H.min = z.right
10 Consolidate(H)
11 H.n = H.n − 1
12 return z
42 / 120
Extract min
Extract min
Fib-Heap-Extract-Min(H)
1 z = H.min
2 if z = NIL
3 for each child x of z
4 add x to the root list of H
5 x.p = NIL
6 remove z from the root list of H
7 if z == z.right
8 H.min = NIL
9 else H.min = z.right
10 Consolidate(H)
11 H.n = H.n − 1
12 return z
42 / 120
Extract min
Extract min
Fib-Heap-Extract-Min(H)
1 z = H.min
2 if z = NIL
3 for each child x of z
4 add x to the root list of H
5 x.p = NIL
6 remove z from the root list of H
7 if z == z.right
8 H.min = NIL
9 else H.min = z.right
10 Consolidate(H)
11 H.n = H.n − 1
12 return z
42 / 120
Extract min
Extract min
Fib-Heap-Extract-Min(H)
1 z = H.min
2 if z = NIL
3 for each child x of z
4 add x to the root list of H
5 x.p = NIL
6 remove z from the root list of H
7 if z == z.right
8 H.min = NIL
9 else H.min = z.right
10 Consolidate(H)
11 H.n = H.n − 1
12 return z
42 / 120
What is happening here?
First
Here, the code in lines 3-6 remove the node z and adds the children of z
to the root list of H.
Next
If the Fibonacci Heap is not empty a consolidation code is triggered.
43 / 120
What is happening here?
First
Here, the code in lines 3-6 remove the node z and adds the children of z
to the root list of H.
Next
If the Fibonacci Heap is not empty a consolidation code is triggered.
43 / 120
What is happening here?
Thus
The consolidate code is used to eliminate subtrees that have the
same 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. Without
loss 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 by
calling the FIB-HEAP-LINK procedure.
This procedure increments the attribute x.degree and clears the mark
on y.
44 / 120
What is happening here?
Thus
The consolidate code is used to eliminate subtrees that have the
same 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. Without
loss 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 by
calling the FIB-HEAP-LINK procedure.
This procedure increments the attribute x.degree and clears the mark
on y.
44 / 120
What is happening here?
Thus
The consolidate code is used to eliminate subtrees that have the
same 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. Without
loss 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 by
calling the FIB-HEAP-LINK procedure.
This procedure increments the attribute x.degree and clears the mark
on y.
44 / 120
What is happening here?
Thus
The consolidate code is used to eliminate subtrees that have the
same 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. Without
loss 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 by
calling the FIB-HEAP-LINK procedure.
This procedure increments the attribute x.degree and clears the mark
on y.
44 / 120
What is happening here?
Thus
The consolidate code is used to eliminate subtrees that have the
same 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. Without
loss 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 by
calling the FIB-HEAP-LINK procedure.
This procedure increments the attribute x.degree and clears the mark
on y.
44 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
45 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Consolidate Code
Consolidate(H)
1. Let A (0...D (H.n)) be a new array
2. for i = 0 to D (H.n)
3. A [i] = NIL
4. for each w in the root list of H
5. x = w
6. d = x.degree
7. while A [d] = NIL
8. y = A [d]
9. if x.key > y.key
10. exchange x with y
11. Fib-Heap-Link(H, y, x)
12. A [d] = NIL
13. d = d + 1
14. A [d] = x
15. H.min = NIL
16. for i = 0 to D (H.n)
17. if A [i] = NIL
18. if H.min == NIL
19. create a root list for H
containing just A [i]
20. H.min = A [i]
21. else
22. insert A [i] into H s
root list
24. if A [i] .key < H.min.key
25. H.min = A [i]
46 / 120
Fib-Heap-Link Code
Fib-Heap-Link(H, y, x)
1 Remove y from the root list of H
2 Make y a child of x, incrementing x.degree
3 y.mark = FALSE
47 / 120
Fib-Heap-Link Code
Fib-Heap-Link(H, y, x)
1 Remove y from the root list of H
2 Make y a child of x, incrementing x.degree
3 y.mark = FALSE
47 / 120
Fib-Heap-Link Code
Fib-Heap-Link(H, y, x)
1 Remove y from the root list of H
2 Make y a child of x, incrementing x.degree
3 y.mark = FALSE
47 / 120
Auxiliary Array
The Consolidate uses
An auxiliary pointer array A [0...D (H.n)]
It keeps track of
The roots according the degree
48 / 120
Auxiliary Array
The Consolidate uses
An auxiliary pointer array A [0...D (H.n)]
It keeps track of
The roots according the degree
48 / 120
Code Process
A while loop inside of the for loop - lines 1-15
Using a w variable to go through the root list
This is used to fill the pointers in A [0...D (H.n)]
Then you link both trees using who has a larger key
Then you add a pointer to the new min-heap, with new degree, in A.
Then - lines 15-23
Lines 15-23 clean the original Fibonacci Heap, then using the pointers at
the array A, each subtree is inserted into the root list of H.
49 / 120
Code Process
A while loop inside of the for loop - lines 1-15
Using a w variable to go through the root list
This is used to fill the pointers in A [0...D (H.n)]
Then you link both trees using who has a larger key
Then you add a pointer to the new min-heap, with new degree, in A.
Then - lines 15-23
Lines 15-23 clean the original Fibonacci Heap, then using the pointers at
the array A, each subtree is inserted into the root list of H.
49 / 120
Code Process
A while loop inside of the for loop - lines 1-15
Using a w variable to go through the root list
This is used to fill the pointers in A [0...D (H.n)]
Then you link both trees using who has a larger key
Then you add a pointer to the new min-heap, with new degree, in A.
Then - lines 15-23
Lines 15-23 clean the original Fibonacci Heap, then using the pointers at
the array A, each subtree is inserted into the root list of H.
49 / 120
Code Process
A while loop inside of the for loop - lines 1-15
Using a w variable to go through the root list
This is used to fill the pointers in A [0...D (H.n)]
Then you link both trees using who has a larger key
Then you add a pointer to the new min-heap, with new degree, in A.
Then - lines 15-23
Lines 15-23 clean the original Fibonacci Heap, then using the pointers at
the array A, each subtree is inserted into the root list of H.
49 / 120
Code Process
A while loop inside of the for loop - lines 1-15
Using a w variable to go through the root list
This is used to fill the pointers in A [0...D (H.n)]
Then you link both trees using who has a larger key
Then you add a pointer to the new min-heap, with new degree, in A.
Then - lines 15-23
Lines 15-23 clean the original Fibonacci Heap, then using the pointers at
the array A, each subtree is inserted into the root list of H.
49 / 120
Loop Invariance
We have the following Loop Invariance
At the start of each iteration of the while loop, d = x.degree.
Init
Line 6 ensures that the loop invariant holds the first time we enter the
loop.
Maintenance
We 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 tree
pointer to A with degree d + 1
50 / 120
Loop Invariance
We have the following Loop Invariance
At the start of each iteration of the while loop, d = x.degree.
Init
Line 6 ensures that the loop invariant holds the first time we enter the
loop.
Maintenance
We 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 tree
pointer to A with degree d + 1
50 / 120
Loop Invariance
We have the following Loop Invariance
At the start of each iteration of the while loop, d = x.degree.
Init
Line 6 ensures that the loop invariant holds the first time we enter the
loop.
Maintenance
We 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 tree
pointer to A with degree d + 1
50 / 120
Loop Invariance
Termination
We repeat the while loop until A [d] = NIL, in which case there is no
other root with the same degree as x.
51 / 120
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Example of consolidation
Because A [1] = NIL, then jump over the while loop and make
A [1] = x
23
7
1724
18
52
38
3026 46
39 41
35 51
21
0 1 2 3
76 / 120
Example of consolidation
Because A [1] = NIL insert into the root’s list and because
H.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
Example of consolidation
Because A [2] = NIL insert into the root’s list no exchange of min
because 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
Example of consolidation
Because A [3] = NIL insert into the root’s list no exchange of min
because 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
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
80 / 120
Cost of Extract-min
Amortized analysis observations
The 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
Cost of Extract-min
Amortized analysis observations
The 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
Cost of Extract-min
Amortized analysis observations
The 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
Cost of Extract-min
Amortized analysis observations
The 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
Next
We have that
The 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
Next
We have that
The 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
Next
We have that
The 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
Next
Then
At lines 4 to 14 in the CONSOLIDATE code:
The amount of work done by the for and the while loop is
proportional to D(n) + t(H) because each time we go through an
element in the root list (for loop).
The while loop consolidate the tree pointed by the pointer to a tree x
with same degree.
The Actual Cost is
Then, the actual cost is ci = O (D (n) + t (H)).
83 / 120
Next
Then
At lines 4 to 14 in the CONSOLIDATE code:
The amount of work done by the for and the while loop is
proportional to D(n) + t(H) because each time we go through an
element in the root list (for loop).
The while loop consolidate the tree pointed by the pointer to a tree x
with same degree.
The Actual Cost is
Then, the actual cost is ci = O (D (n) + t (H)).
83 / 120
Next
Then
At lines 4 to 14 in the CONSOLIDATE code:
The amount of work done by the for and the while loop is
proportional to D(n) + t(H) because each time we go through an
element in the root list (for loop).
The while loop consolidate the tree pointed by the pointer to a tree x
with same degree.
The Actual Cost is
Then, the actual cost is ci = O (D (n) + t (H)).
83 / 120
Next
Then
At lines 4 to 14 in the CONSOLIDATE code:
The amount of work done by the for and the while loop is
proportional to D(n) + t(H) because each time we go through an
element in the root list (for loop).
The while loop consolidate the tree pointed by the pointer to a tree x
with same degree.
The Actual Cost is
Then, the actual cost is ci = O (D (n) + t (H)).
83 / 120
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
H’ has at most D(n) + 1 elements after consolidation
No node is marked in the process.
84 / 120
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
H’ has at most D(n) + 1 elements after consolidation
No node is marked in the process.
84 / 120
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
H’ has at most D(n) + 1 elements after consolidation
No node is marked in the process.
84 / 120
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
H’ has at most D(n) + 1 elements after consolidation
No node is marked in the process.
84 / 120
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 that
D(n) = O(log n)
85 / 120
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 that
D(n) = O(log n)
85 / 120
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 that
D(n) = O(log n)
85 / 120
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 that
D(n) = O(log n)
85 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
86 / 120
Decreasing a key
Fib-Heap-Decrease-Key(H, x, k)
1 if k > x.key
2 error “new key is greater than current key”
3 x.key = k
4 y = x.p
5 if y = NIL and x.key < y.key
6 CUT(H, x, y)
7 Cascading-Cut(H, y)
8 if x.key < H.min.key
9 H.min = x
87 / 120
Decreasing a key
Fib-Heap-Decrease-Key(H, x, k)
1 if k > x.key
2 error “new key is greater than current key”
3 x.key = k
4 y = x.p
5 if y = NIL and x.key < y.key
6 CUT(H, x, y)
7 Cascading-Cut(H, y)
8 if x.key < H.min.key
9 H.min = x
87 / 120
Decreasing a key
Fib-Heap-Decrease-Key(H, x, k)
1 if k > x.key
2 error “new key is greater than current key”
3 x.key = k
4 y = x.p
5 if y = NIL and x.key < y.key
6 CUT(H, x, y)
7 Cascading-Cut(H, y)
8 if x.key < H.min.key
9 H.min = x
87 / 120
Decreasing a key
Fib-Heap-Decrease-Key(H, x, k)
1 if k > x.key
2 error “new key is greater than current key”
3 x.key = k
4 y = x.p
5 if y = NIL and x.key < y.key
6 CUT(H, x, y)
7 Cascading-Cut(H, y)
8 if x.key < H.min.key
9 H.min = x
87 / 120
Explanation
First
1 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
Explanation
First
1 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
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 H
3 x.p = NIL
4 x.mark = FALSE
Cascading-Cut(H, y)
1 z = y.p
2 if z = NIL
3 if y.mark == FALSE
4 y.mark=TRUE
5 else
6 Cut(H, y, z)
7 Cascading-Cut(H, y)(H, z)
89 / 120
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 H
3 x.p = NIL
4 x.mark = FALSE
Cascading-Cut(H, y)
1 z = y.p
2 if z = NIL
3 if y.mark == FALSE
4 y.mark=TRUE
5 else
6 Cut(H, y, z)
7 Cascading-Cut(H, y)(H, z)
89 / 120
Explanation
Second
Then CUT simply removes x from the child-list of y.
Thus
The CASCADING-CUT uses the mark attributes to obtain the desired
time bounds.
90 / 120
Explanation
Second
Then CUT simply removes x from the child-list of y.
Thus
The CASCADING-CUT uses the mark attributes to obtain the desired
time bounds.
90 / 120
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 and
one child of y has been cut.
The CUT procedure, therefore, clears y.mark in line 4, since it
performs step 1.
We can now see why line 3 of FIB-HEAP-LINK clears y.mark.
91 / 120
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 and
one child of y has been cut.
The CUT procedure, therefore, clears y.mark in line 4, since it
performs step 1.
We can now see why line 3 of FIB-HEAP-LINK clears y.mark.
91 / 120
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 and
one child of y has been cut.
The CUT procedure, therefore, clears y.mark in line 4, since it
performs step 1.
We can now see why line 3 of FIB-HEAP-LINK clears y.mark.
91 / 120
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 and
one child of y has been cut.
The CUT procedure, therefore, clears y.mark in line 4, since it
performs step 1.
We can now see why line 3 of FIB-HEAP-LINK clears y.mark.
91 / 120
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 and
one child of y has been cut.
The CUT procedure, therefore, clears y.mark in line 4, since it
performs step 1.
We can now see why line 3 of FIB-HEAP-LINK clears y.mark.
91 / 120
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 and
one child of y has been cut.
The CUT procedure, therefore, clears y.mark in line 4, since it
performs step 1.
We can now see why line 3 of FIB-HEAP-LINK clears y.mark.
91 / 120
Explanation
Now we have a new problem
x might be the second child cut from its parent y to another node.
Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a
cascading-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 done
the H.min is updated if necessary
92 / 120
Explanation
Now we have a new problem
x might be the second child cut from its parent y to another node.
Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a
cascading-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 done
the H.min is updated if necessary
92 / 120
Explanation
Now we have a new problem
x might be the second child cut from its parent y to another node.
Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a
cascading-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 done
the H.min is updated if necessary
92 / 120
Explanation
Now we have a new problem
x might be the second child cut from its parent y to another node.
Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a
cascading-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 done
the H.min is updated if necessary
92 / 120
Explanation
Now we have a new problem
x might be the second child cut from its parent y to another node.
Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a
cascading-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 done
the H.min is updated if necessary
92 / 120
Explanation
Now we have a new problem
x might be the second child cut from its parent y to another node.
Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a
cascading-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 done
the H.min is updated if necessary
92 / 120
Example
46 is decreased to 15
23
7
1724
18
52
38
3026 46
39 41
35 51
21
93 / 120
Example
46 is decreased to 15
23
7
1724
18
52
38
3026
39 41
35 51
21
15
94 / 120
Example
Cut 15 to the root’s list
23
7
1724
18
52
38
3026
39 41
35
51 21
15
95 / 120
Example
Mark 24 to True
23
7
1724
18
52
38
3026
39 41
35
51 21
15
96 / 120
Example
Then 35 is decreased to 5
23
7
1724
18
52
38
3026
39 41
5
51 21
15
97 / 120
Example
Cut 15 to the root’s list
23
7
1724
18
52
38
3026
39 41
5
51 21
15
98 / 120
Example
Initiate cascade cutting
23
7
1724
18
52
38
3026
39 41
5
51 21
15
99 / 120
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
Example
Mark 26 to false
23
7
1724
18
52
38
30
26
39 41
5
51 21
15
101 / 120
Example
Move 24 to root’s list
23
7
17
24 18
52
38
30
26
39 41
5
51 21
15
102 / 120
Example
Mark 26 to false
23
7
17
24 18
52
38
30
26
39 41
5
51 21
15
103 / 120
Example
Change the H.min
23
7
17
24 18
52
38
30
26
39 41
5
51 21
15
104 / 120
Potential cost
The procedure Decrease-Key takes
ci = O(1)+the cascading-cuts
Assume that you require c calls to cascade CASCADING-CUT
One for the line 6 at the code FIB-HEAP-DECREASE-KEY followed
by c − 1 others
The cost of it will take ci = O(c).
105 / 120
Potential cost
The procedure Decrease-Key takes
ci = O(1)+the cascading-cuts
Assume that you require c calls to cascade CASCADING-CUT
One for the line 6 at the code FIB-HEAP-DECREASE-KEY followed
by c − 1 others
The cost of it will take ci = O(c).
105 / 120
Potential cost
The procedure Decrease-Key takes
ci = O(1)+the cascading-cuts
Assume that you require c calls to cascade CASCADING-CUT
One for the line 6 at the code FIB-HEAP-DECREASE-KEY followed
by c − 1 others
The cost of it will take ci = O(c).
105 / 120
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
The # original trees + the ones created by the c calls.
m (H) − (c − 1) + 1
The original marks - (c − 1) cleared marks by Cut + the
branch to y.mark == FALSE true.
106 / 120
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
The # original trees + the ones created by the c calls.
m (H) − (c − 1) + 1
The original marks - (c − 1) cleared marks by Cut + the
branch to y.mark == FALSE true.
106 / 120
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
The # original trees + the ones created by the c calls.
m (H) − (c − 1) + 1
The original marks - (c − 1) cleared marks by Cut + the
branch to y.mark == FALSE true.
106 / 120
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
The # original trees + the ones created by the c calls.
m (H) − (c − 1) + 1
The original marks - (c − 1) cleared marks by Cut + the
branch to y.mark == FALSE true.
106 / 120
Final change in potential
Thus
Φ 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)
Observation
Now 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
Final change in potential
Thus
Φ 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)
Observation
Now 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
Final change in potential
Thus
Φ 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)
Observation
Now 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
Final change in potential
Thus
Φ 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)
Observation
Now 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
Final change in potential
Thus
Φ 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)
Observation
Now 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
Final change in potential
Thus
Φ 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)
Observation
Now 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
Delete a node
It is easy to delete a node in the Fibonacci heap following the next
code
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
Proving the D (n) bound!!!
Let’s define the following
We define a quantity size(x)= the number of nodes at subtree rooted at
x, 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
Proving the D (n) bound!!!
Let’s define the following
We define a quantity size(x)= the number of nodes at subtree rooted at
x, 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
Proving the D (n) bound!!!
Let’s define the following
We define a quantity size(x)= the number of nodes at subtree rooted at
x, 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
Now...
Lemma 19.1
Let 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 were
linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and
yi.degree ≥ i − 2 for i = 2, 3, ..., k.
Proof
1 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.
Note: It would have been cut from x if it had lost two children.
5 We conclude that yi.degree ≥ i − 2.
110 / 120
Now...
Lemma 19.1
Let 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 were
linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and
yi.degree ≥ i − 2 for i = 2, 3, ..., k.
Proof
1 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.
Note: It would have been cut from x if it had lost two children.
5 We conclude that yi.degree ≥ i − 2.
110 / 120
Now...
Lemma 19.1
Let 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 were
linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and
yi.degree ≥ i − 2 for i = 2, 3, ..., k.
Proof
1 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.
Note: It would have been cut from x if it had lost two children.
5 We conclude that yi.degree ≥ i − 2.
110 / 120
Now...
Lemma 19.1
Let 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 were
linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and
yi.degree ≥ i − 2 for i = 2, 3, ..., k.
Proof
1 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.
Note: It would have been cut from x if it had lost two children.
5 We conclude that yi.degree ≥ i − 2.
110 / 120
Now...
Lemma 19.1
Let 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 were
linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and
yi.degree ≥ i − 2 for i = 2, 3, ..., k.
Proof
1 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.
Note: It would have been cut from x if it had lost two children.
5 We conclude that yi.degree ≥ i − 2.
110 / 120
Now...
Lemma 19.1
Let 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 were
linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and
yi.degree ≥ i − 2 for i = 2, 3, ..., k.
Proof
1 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.
Note: It would have been cut from x if it had lost two children.
5 We conclude that yi.degree ≥ i − 2.
110 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
111 / 120
Why Fibonacci?
The kth Fibonacci number is defined by the recurrence
Fk =



0 if k = 0
1 if k = 1
Fk−1 + Fk−2 if k ≥ 2
Lemma 19.2
For al integers k ≥ 0
Fk+2 = 1 +
k
i=0
Fi
Proof by induction k = 0....
112 / 120
Why Fibonacci?
The kth Fibonacci number is defined by the recurrence
Fk =



0 if k = 0
1 if k = 1
Fk−1 + Fk−2 if k ≥ 2
Lemma 19.2
For al integers k ≥ 0
Fk+2 = 1 +
k
i=0
Fi
Proof by induction k = 0....
112 / 120
The use of the Golden Ratio
Lemma 19.3
Let 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
Golden Ratio
Building 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.
114 / 120
Golden Ratio
Building 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.
114 / 120
Golden Ratio
Building 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
The use of the Golden Ratio
Lemma 19.4
Let x be any node in a Fibonacci Heap, k = x.degree. Then
size(x) ≥ Fk+2 ≥ Φk.
Proof
Let sk denote the minimum value for size(x) over all nodes x such
that x.degree = k.
Restrictions for sk:
sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).
As in the previous lemma
y1, y2, ..., yk denote the children of x in the order they were linked to
x.
115 / 120
The use of the Golden Ratio
Lemma 19.4
Let x be any node in a Fibonacci Heap, k = x.degree. Then
size(x) ≥ Fk+2 ≥ Φk.
Proof
Let sk denote the minimum value for size(x) over all nodes x such
that x.degree = k.
Restrictions for sk:
sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).
As in the previous lemma
y1, y2, ..., yk denote the children of x in the order they were linked to
x.
115 / 120
The use of the Golden Ratio
Lemma 19.4
Let x be any node in a Fibonacci Heap, k = x.degree. Then
size(x) ≥ Fk+2 ≥ Φk.
Proof
Let sk denote the minimum value for size(x) over all nodes x such
that x.degree = k.
Restrictions for sk:
sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).
As in the previous lemma
y1, y2, ..., yk denote the children of x in the order they were linked to
x.
115 / 120
The use of the Golden Ratio
Lemma 19.4
Let x be any node in a Fibonacci Heap, k = x.degree. Then
size(x) ≥ Fk+2 ≥ Φk.
Proof
Let sk denote the minimum value for size(x) over all nodes x such
that x.degree = k.
Restrictions for sk:
sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).
As in the previous lemma
y1, y2, ..., yk denote the children of x in the order they were linked to
x.
115 / 120
The use of the Golden Ratio
Lemma 19.4
Let x be any node in a Fibonacci Heap, k = x.degree. Then
size(x) ≥ Fk+2 ≥ Φk.
Proof
Let sk denote the minimum value for size(x) over all nodes x such
that x.degree = k.
Restrictions for sk:
sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing).
As in the previous lemma
y1, y2, ..., yk denote the children of x in the order they were linked to
x.
115 / 120
Example
For example
Now
Look at the board
116 / 120
Example
For example
Now
Look at the board
116 / 120
The use of the Golden Ratio
Proof continuation
Now, 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
The use of the Golden Ratio
Proof continuation
Now, 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
The use of the Golden Ratio
Proof continuation
Now, 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
Finally
Corollary 19.5
The maximum degree D(n) of any node in an n-node Fibonacci heap is
O(log n).
Proof
Look at the board!
118 / 120
Finally
Corollary 19.5
The maximum degree D(n) of any node in an n-node Fibonacci heap is
O(log n).
Proof
Look at the board!
118 / 120
Outline
1 Introduction
Basic Definitions
Ordered Trees
2 Binomial Trees
Example
3 Fibonacci Heap
Operations
Fibonacci Heap
Why Fibonacci Heaps?
Node Structure
Fibonacci Heaps Operations
Mergeable-Heaps operations - Make Heap
Mergeable-Heaps operations - Insertion
Mergeable-Heaps operations - Minimum
Mergeable-Heaps operations - Union
Complexity Analysis
Consolidate Algorithm
Potential cost
Operation: Decreasing a Key
Why Fibonacci?
4 Exercises
Some Exercises that you can try
119 / 120
Exercises
From Cormen’s book solve the following
20.2-2
20.2-3
20.2-4
20.3-1
20.3-2
20.4-1
20.4-2
120 / 120

Mais conteúdo relacionado

Mais procurados (20)

Heaps
HeapsHeaps
Heaps
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 
B tree short
B tree shortB tree short
B tree short
 
Elements of Dynamic Programming
Elements of Dynamic ProgrammingElements of Dynamic Programming
Elements of Dynamic Programming
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Discrete Mathematics Tree
Discrete Mathematics  TreeDiscrete Mathematics  Tree
Discrete Mathematics Tree
 
Avl trees final
Avl trees finalAvl trees final
Avl trees final
 
Binomial Heap
Binomial HeapBinomial Heap
Binomial Heap
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
Heap sort
Heap sortHeap sort
Heap sort
 
B trees and_b__trees
B trees and_b__treesB trees and_b__trees
B trees and_b__trees
 
Chapter 8 ds
Chapter 8 dsChapter 8 ds
Chapter 8 ds
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Trees
TreesTrees
Trees
 
Tree (Data Structure & Discrete Mathematics)
Tree (Data Structure & Discrete Mathematics)Tree (Data Structure & Discrete Mathematics)
Tree (Data Structure & Discrete Mathematics)
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
Binomial heaps
Binomial heapsBinomial heaps
Binomial heaps
 

Destaque

Fibonacci Heaps
Fibonacci Heaps Fibonacci Heaps
Fibonacci Heaps Naseeba P P
 
Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Saliha Bilal
 
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12sumitbardhan
 
Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memoryRohini Shinde
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Treekhabbab_h
 
Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)raj upadhyay
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...sethuraman R
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Javageeksrik
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data StructuresJibrael Jos
 

Destaque (9)

Fibonacci Heaps
Fibonacci Heaps Fibonacci Heaps
Fibonacci Heaps
 
Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)Binomial heap (a concept of Data Structure)
Binomial heap (a concept of Data Structure)
 
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12
 
Representation of binary tree in memory
Representation of binary tree in memoryRepresentation of binary tree in memory
Representation of binary tree in memory
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)Tree Traversals (In-order, Pre-order and Post-order)
Tree Traversals (In-order, Pre-order and Post-order)
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
 
Trees - Data structures in C/Java
Trees - Data structures in C/JavaTrees - Data structures in C/Java
Trees - Data structures in C/Java
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data Structures
 

Semelhante a 16 Fibonacci Heaps

Binomial Heap DAA PPT (2).ppt
Binomial Heap DAA PPT (2).pptBinomial Heap DAA PPT (2).ppt
Binomial Heap DAA PPT (2).pptAzad988896
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
 
Cs501 mining frequentpatterns
Cs501 mining frequentpatternsCs501 mining frequentpatterns
Cs501 mining frequentpatternsKamal Singh Lodhi
 
Unit-5 Advanced tree zxcppt
Unit-5 Advanced tree                     zxcpptUnit-5 Advanced tree                     zxcppt
Unit-5 Advanced tree zxcpptDhruvilSTATUS
 
Applications of datastructures
Applications of datastructuresApplications of datastructures
Applications of datastructuresRatsietsi Mokete
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructuresHitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructuresHitesh Wagle
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptxTekle12
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of HeapMeghaj Mallick
 

Semelhante a 16 Fibonacci Heaps (11)

Binomial Heap DAA PPT (2).ppt
Binomial Heap DAA PPT (2).pptBinomial Heap DAA PPT (2).ppt
Binomial Heap DAA PPT (2).ppt
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
Cs501 mining frequentpatterns
Cs501 mining frequentpatternsCs501 mining frequentpatterns
Cs501 mining frequentpatterns
 
Unit-5 Advanced tree zxcppt
Unit-5 Advanced tree                     zxcpptUnit-5 Advanced tree                     zxcppt
Unit-5 Advanced tree zxcppt
 
Applications of datastructures
Applications of datastructuresApplications of datastructures
Applications of datastructures
 
Binary trees
Binary treesBinary trees
Binary trees
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
chapter 6.1.pptx
chapter 6.1.pptxchapter 6.1.pptx
chapter 6.1.pptx
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 

Mais de Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

Mais de Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Último

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 

Último (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 

16 Fibonacci Heaps

  • 1. Analysis of Algorithms Fibonacci Heaps Andres Mendez-Vazquez October 29, 2015 1 / 120
  • 2. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 2 / 120
  • 3. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 3 / 120
  • 4. Some previous definitions Free Tree A free tree is a connected acyclic undirected graph. Rooted Tree A rooted tree is a free tree in which one of the nodes is a root. Ordered Tree An ordered tree is a rooted tree where the children are ordered. 4 / 120
  • 5. Some previous definitions Free Tree A free tree is a connected acyclic undirected graph. Rooted Tree A rooted tree is a free tree in which one of the nodes is a root. Ordered Tree An ordered tree is a rooted tree where the children are ordered. 4 / 120
  • 6. Some previous definitions Free Tree A free tree is a connected acyclic undirected graph. Rooted Tree A rooted tree is a free tree in which one of the nodes is a root. Ordered Tree An ordered tree is a rooted tree where the children are ordered. 4 / 120
  • 7. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 5 / 120
  • 8. Ordered Tree Definition An ordered tree is an oriented tree in which the children of a node are somehow "ordered." Example Thus If T1 and T2 are ordered trees then T1 = T2 else T1 = T2. 6 / 120
  • 9. Ordered Tree Definition An ordered tree is an oriented tree in which the children of a node are somehow "ordered." Example a b c a bc Thus If T1 and T2 are ordered trees then T1 = T2 else T1 = T2. 6 / 120
  • 10. Ordered Tree Definition An ordered tree is an oriented tree in which the children of a node are somehow "ordered." Example a b c a bc Thus If T1 and T2 are ordered trees then T1 = T2 else T1 = T2. 6 / 120
  • 11. Some previous definitions Types of Ordered Trees There are several types of ordered trees: k-ary tree Binomial tree Fibonacci tree Binomial Tree A binomial tree is an ordered tree defined recursively. 7 / 120
  • 12. Some previous definitions Types of Ordered Trees There are several types of ordered trees: k-ary tree Binomial tree Fibonacci tree Binomial Tree A binomial tree is an ordered tree defined recursively. 7 / 120
  • 13. Some previous definitions Types of Ordered Trees There are several types of ordered trees: k-ary tree Binomial tree Fibonacci tree Binomial Tree A binomial tree is an ordered tree defined recursively. 7 / 120
  • 14. Some previous definitions Types of Ordered Trees There are several types of ordered trees: k-ary tree Binomial tree Fibonacci tree Binomial Tree A binomial tree is an ordered tree defined recursively. 7 / 120
  • 15. Some previous definitions Types of Ordered Trees There are several types of ordered trees: k-ary tree Binomial tree Fibonacci tree Binomial Tree A binomial tree is an ordered tree defined recursively. 7 / 120
  • 16. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 8 / 120
  • 18. This can be seen too as Recursive Structure 10 / 120
  • 19. Properties of binomial trees Lemma 19.1 For the binomial tree Bk: 1 There are 2k nodes. 2 The height of the tree is k. 3 There are exactly k i 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 right by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi. Proof! Look at the white-board. 11 / 120
  • 20. Properties of binomial trees Lemma 19.1 For the binomial tree Bk: 1 There are 2k nodes. 2 The height of the tree is k. 3 There are exactly k i 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 right by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi. Proof! Look at the white-board. 11 / 120
  • 21. Properties of binomial trees Lemma 19.1 For the binomial tree Bk: 1 There are 2k nodes. 2 The height of the tree is k. 3 There are exactly k i 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 right by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi. Proof! Look at the white-board. 11 / 120
  • 22. Properties of binomial trees Lemma 19.1 For the binomial tree Bk: 1 There are 2k nodes. 2 The height of the tree is k. 3 There are exactly k i 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 right by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi. Proof! Look at the white-board. 11 / 120
  • 23. Properties of binomial trees Lemma 19.1 For the binomial tree Bk: 1 There are 2k nodes. 2 The height of the tree is k. 3 There are exactly k i 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 right by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi. Proof! Look at the white-board. 11 / 120
  • 24. Properties of binomial trees Lemma 19.1 For the binomial tree Bk: 1 There are 2k nodes. 2 The height of the tree is k. 3 There are exactly k i 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 right by k − 1, k − 2, ..., 0 child i is the root of a subtree Bi. Proof! Look at the white-board. 11 / 120
  • 25. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 12 / 120
  • 26. Mergeable Heap Operations The Fibonacci heap data structure is used to support the operations “meargeable heap” operations 1. 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
  • 27. Mergeable Heap Operations The Fibonacci heap data structure is used to support the operations “meargeable heap” operations 1. 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
  • 28. Mergeable Heap Operations The Fibonacci heap data structure is used to support the operations “meargeable heap” operations 1. 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
  • 29. Mergeable Heap Operations The Fibonacci heap data structure is used to support the operations “meargeable heap” operations 1. 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
  • 30. Mergeable Heap Operations The Fibonacci heap data structure is used to support the operations “meargeable heap” operations 5. UNION(H1, H2) creates and returns a new heap that contains all the elements of heaps H1 and H2. Heaps are “destroyed” by this operation. 6. DECREASE-KEY(H, x, k) assigns to element x within heap H the new key value k. 7. DELETE(H, x) deletes element x from heap H. 14 / 120
  • 31. Mergeable Heap Operations The Fibonacci heap data structure is used to support the operations “meargeable heap” operations 5. UNION(H1, H2) creates and returns a new heap that contains all the elements of heaps H1 and H2. Heaps are “destroyed” by this operation. 6. DECREASE-KEY(H, x, k) assigns to element x within heap H the new key value k. 7. DELETE(H, x) deletes element x from heap H. 14 / 120
  • 32. Mergeable Heap Operations The Fibonacci heap data structure is used to support the operations “meargeable heap” operations 5. UNION(H1, H2) creates and returns a new heap that contains all the elements of heaps H1 and H2. Heaps are “destroyed” by this operation. 6. DECREASE-KEY(H, x, k) assigns to element x within heap H the new key value k. 7. DELETE(H, x) deletes element x from heap H. 14 / 120
  • 33. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 15 / 120
  • 34. Fibonacci Heap Definition A Fibonacci heap is a collection of rooted trees that are min-heap ordered. Meaning Each 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 binomial tree except that the root of one tree is made any child of the root of the other. 16 / 120
  • 35. Fibonacci Heap Definition A Fibonacci heap is a collection of rooted trees that are min-heap ordered. Meaning Each 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 binomial tree except that the root of one tree is made any child of the root of the other. 16 / 120
  • 36. Fibonacci Heap Definition A Fibonacci heap is a collection of rooted trees that are min-heap ordered. Meaning Each 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 binomial tree except that the root of one tree is made any child of the root of the other. 16 / 120
  • 37. Fibonacci Heap Definition A Fibonacci heap is a collection of rooted trees that are min-heap ordered. Meaning Each 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 binomial tree except that the root of one tree is made any child of the root of the other. 16 / 120
  • 38. Fibonacci Structure Example 23 7 3 17 24 18 52 38 39 41 30 26 46 35 17 / 120
  • 39. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 18 / 120
  • 40. Why Fibonacci Heaps? Fibonacci Heaps facts Fibonacci heaps are especially desirable when the number of calls to Extract-Min and Delete is small. All other operations run in O(1). Applications Fibonacci heaps may be used in many applications. Some graph problems, like minimum spanning tree and single-source-shortest-path. 19 / 120
  • 41. Why Fibonacci Heaps? Fibonacci Heaps facts Fibonacci heaps are especially desirable when the number of calls to Extract-Min and Delete is small. All other operations run in O(1). Applications Fibonacci heaps may be used in many applications. Some graph problems, like minimum spanning tree and single-source-shortest-path. 19 / 120
  • 42. It is more... We have that Procedure 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
  • 43. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 21 / 120
  • 44. Fields in each node The Classic Ones Each node contains a x.parent and x.child field. The ones for the doubled linked list The children of each a node x are linked together in a circular double linked list: Each child y of x has a y.left and y.right to do this. 22 / 120
  • 45. Fields in each node The Classic Ones Each node contains a x.parent and x.child field. The ones for the doubled linked list The children of each a node x are linked together in a circular double linked list: Each child y of x has a y.left and y.right to do this. 22 / 120
  • 46. Thus, we have the following important labels Field degree Did you notice that there in no way to find the number of children unless you have complex exploratory method? We store the number of children in the child list of node x in x.degree. The Amortized Label Each child has the field mark. IMPORTANT 1 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 another node. 23 / 120
  • 47. Thus, we have the following important labels Field degree Did you notice that there in no way to find the number of children unless you have complex exploratory method? We store the number of children in the child list of node x in x.degree. The Amortized Label Each child has the field mark. IMPORTANT 1 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 another node. 23 / 120
  • 48. Thus, we have the following important labels Field degree Did you notice that there in no way to find the number of children unless you have complex exploratory method? We store the number of children in the child list of node x in x.degree. The Amortized Label Each child has the field mark. IMPORTANT 1 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 another node. 23 / 120
  • 49. Thus, we have the following important labels Field degree Did you notice that there in no way to find the number of children unless you have complex exploratory method? We store the number of children in the child list of node x in x.degree. The Amortized Label Each child has the field mark. IMPORTANT 1 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 another node. 23 / 120
  • 50. Thus, we have the following important labels Field degree Did you notice that there in no way to find the number of children unless you have complex exploratory method? We store the number of children in the child list of node x in x.degree. The Amortized Label Each child has the field mark. IMPORTANT 1 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 another node. 23 / 120
  • 51. The child list Circular, doubly linked list have two advantages for use in Fibonacci heaps: 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
  • 52. The child list Circular, doubly linked list have two advantages for use in Fibonacci heaps: 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
  • 53. The child list Circular, doubly linked list have two advantages for use in Fibonacci heaps: 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
  • 54. Additional First The roots of all the trees in a Fibonacci heap H are linked together using their left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. Second The pointer H.min of the Fibonacci data structure thus points to the node in the root list whose key is minimum. Trees may appear in any order within a root list. Third The Fibonacci data structure has the field H.n =the number of nodes currently in the Fibonacci Heap H. 25 / 120
  • 55. Additional First The roots of all the trees in a Fibonacci heap H are linked together using their left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. Second The pointer H.min of the Fibonacci data structure thus points to the node in the root list whose key is minimum. Trees may appear in any order within a root list. Third The Fibonacci data structure has the field H.n =the number of nodes currently in the Fibonacci Heap H. 25 / 120
  • 56. Additional First The roots of all the trees in a Fibonacci heap H are linked together using their left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. Second The pointer H.min of the Fibonacci data structure thus points to the node in the root list whose key is minimum. Trees may appear in any order within a root list. Third The Fibonacci data structure has the field H.n =the number of nodes currently in the Fibonacci Heap H. 25 / 120
  • 57. Additional First The roots of all the trees in a Fibonacci heap H are linked together using their left and right pointers into a circular, doubly linked list called the root list of the Fibonacci heap. Second The pointer H.min of the Fibonacci data structure thus points to the node in the root list whose key is minimum. Trees may appear in any order within a root list. Third The Fibonacci data structure has the field H.n =the number of nodes currently in the Fibonacci Heap H. 25 / 120
  • 58. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 26 / 120
  • 59. Idea behind Fibonacci heaps Main idea Fibonacci heaps are called lazy data structures because they delay work as long as possible using the field mark!!! 27 / 120
  • 60. Make Heap Make a heap You only need the following code: MakeHeap() 1 min[H] = NIL 2 n(H) = 0 Complexity is as simple as Cost O(1). 28 / 120
  • 61. Make Heap Make a heap You only need the following code: MakeHeap() 1 min[H] = NIL 2 n(H) = 0 Complexity is as simple as Cost O(1). 28 / 120
  • 62. Insertion Code for Inserting a node Fib-Heap-Insert(H, x) 1 x.degree = 0 2 x.p = NIL 3 x.child = NIL 4 x.mark = FALSE 5 if H.min = NIL 6 Create a root list for H containing just x 7 H.min = x 8 else insert x into H s root list 9 if x.key < H.min.key 10 H.min = x 11 H.n = H.n + 1 29 / 120
  • 63. Insertion Code for Inserting a node Fib-Heap-Insert(H, x) 1 x.degree = 0 2 x.p = NIL 3 x.child = NIL 4 x.mark = FALSE 5 if H.min = NIL 6 Create a root list for H containing just x 7 H.min = x 8 else insert x into H s root list 9 if x.key < H.min.key 10 H.min = x 11 H.n = H.n + 1 29 / 120
  • 64. Insertion Code for Inserting a node Fib-Heap-Insert(H, x) 1 x.degree = 0 2 x.p = NIL 3 x.child = NIL 4 x.mark = FALSE 5 if H.min = NIL 6 Create a root list for H containing just x 7 H.min = x 8 else insert x into H s root list 9 if x.key < H.min.key 10 H.min = x 11 H.n = H.n + 1 29 / 120
  • 65. Insertion Code for Inserting a node Fib-Heap-Insert(H, x) 1 x.degree = 0 2 x.p = NIL 3 x.child = NIL 4 x.mark = FALSE 5 if H.min = NIL 6 Create a root list for H containing just x 7 H.min = x 8 else insert x into H s root list 9 if x.key < H.min.key 10 H.min = x 11 H.n = H.n + 1 29 / 120
  • 66. Inserting a node Example 23 7 3 17 24 18 52 38 39 41 30 26 46 35 51 30 / 120
  • 67. Inserting a node Example 23 7 3 17 24 18 52 38 30 26 46 39 41 35 51 21 31 / 120
  • 68. Minimum Finding the Minimum Simply return the key of min(H). The amortized cost is simply O (1). We will analyze this later on... 32 / 120
  • 69. Minimum Finding the Minimum Simply return the key of min(H). The amortized cost is simply O (1). We will analyze this later on... 32 / 120
  • 70. What about Union of two Heaps? Code for Union of Heaps Fib-Heap-Union(H1, H2) 1 H =Make-Fib-Heap() 2 H.min = H1.min 3 Concatenate the root list of H2 with the root list of H 4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key) 5 H.min = H2.min 6 H.n = H1.n + H2.n 7 return H 33 / 120
  • 71. What about Union of two Heaps? Code for Union of Heaps Fib-Heap-Union(H1, H2) 1 H =Make-Fib-Heap() 2 H.min = H1.min 3 Concatenate the root list of H2 with the root list of H 4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key) 5 H.min = H2.min 6 H.n = H1.n + H2.n 7 return H 33 / 120
  • 72. What about Union of two Heaps? Code for Union of Heaps Fib-Heap-Union(H1, H2) 1 H =Make-Fib-Heap() 2 H.min = H1.min 3 Concatenate the root list of H2 with the root list of H 4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key) 5 H.min = H2.min 6 H.n = H1.n + H2.n 7 return H 33 / 120
  • 73. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 34 / 120
  • 74. In order to analyze Union... We introduce some ideas from... Our old friend amortized analysis and potential method!!! 35 / 120
  • 75. Amortized potential function We have the following function Φ(H) = t(H) + 2m(H) Where: t(H) is the number of trees in the Fibonacci heap m(H) is the number of marked nodes in the tree. Amortized analysis The 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
  • 76. Amortized potential function We have the following function Φ(H) = t(H) + 2m(H) Where: t(H) is the number of trees in the Fibonacci heap m(H) is the number of marked nodes in the tree. Amortized analysis The 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
  • 77. Amortized potential function We have the following function Φ(H) = t(H) + 2m(H) Where: t(H) is the number of trees in the Fibonacci heap m(H) is the number of marked nodes in the tree. Amortized analysis The 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
  • 78. Amortized potential function We have the following function Φ(H) = t(H) + 2m(H) Where: t(H) is the number of trees in the Fibonacci heap m(H) is the number of marked nodes in the tree. Amortized analysis The 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
  • 79. Amortized potential function We have the following function Φ(H) = t(H) + 2m(H) Where: t(H) is the number of trees in the Fibonacci heap m(H) is the number of marked nodes in the tree. Amortized analysis The 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
  • 80. 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, then D(n) = log n. We will prove this latter!!! 37 / 120
  • 81. 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, then D(n) = log n. We will prove this latter!!! 37 / 120
  • 82. 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, then D(n) = log n. We will prove this latter!!! 37 / 120
  • 83. Back to Insertion First If H is the Fibonacci heap after inserting, and H before that: t(H ) = t(H) + 1 Second m(H ) = m(H) Then the change of potential is Φ(H ) − Φ(H) = 1 then complexity analysis results in O(1) + 1 = O 38 / 120
  • 84. Back to Insertion First If H is the Fibonacci heap after inserting, and H before that: t(H ) = t(H) + 1 Second m(H ) = m(H) Then the change of potential is Φ(H ) − Φ(H) = 1 then complexity analysis results in O(1) + 1 = O 38 / 120
  • 85. Back to Insertion First If H is the Fibonacci heap after inserting, and H before that: t(H ) = t(H) + 1 Second m(H ) = m(H) Then the change of potential is Φ(H ) − Φ(H) = 1 then complexity analysis results in O(1) + 1 = O 38 / 120
  • 86. Other operations: Find Min It is possible to rephrase this in terms of potential cost By using the pointer min[H] potential cost is 0 then O(1). 39 / 120
  • 87. Other operations: Union Union of two Fibonacci heaps Fib-Heap-Union(H1, H2) 1 H =Make-Fib-Heap() 2 H.min = H1.min 3 Concatenate the root list of H2 with the root list of H 4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key) 5 H.min = H2.min 6 H.n = H1.n + H2.n 7 return H 40 / 120
  • 88. Other operations: Union Union of two Fibonacci heaps Fib-Heap-Union(H1, H2) 1 H =Make-Fib-Heap() 2 H.min = H1.min 3 Concatenate the root list of H2 with the root list of H 4 If (H1.min == NIL) or (H.min = NIL and H2.min.key < H1.min.key) 5 H.min = H2.min 6 H.n = H1.n + H2.n 7 return H 40 / 120
  • 89. Cost of uniting two Fibonacci heaps First t(H) = t (H1) + t (H2) and m(H) = m (H1) + m (H2). Second ci = O(1) this is because the number of steps to make the union operations is a constant. Potential analysis ci = ci + Φ (H) − [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1). We have then a complexity of O(1). 41 / 120
  • 90. Cost of uniting two Fibonacci heaps First t(H) = t (H1) + t (H2) and m(H) = m (H1) + m (H2). Second ci = O(1) this is because the number of steps to make the union operations is a constant. Potential analysis ci = ci + Φ (H) − [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1). We have then a complexity of O(1). 41 / 120
  • 91. Cost of uniting two Fibonacci heaps First t(H) = t (H1) + t (H2) and m(H) = m (H1) + m (H2). Second ci = O(1) this is because the number of steps to make the union operations is a constant. Potential analysis ci = ci + Φ (H) − [Φ (H1) + Φ (H2)] = O(1) + 0 = O(1). We have then a complexity of O(1). 41 / 120
  • 92. Extract min Extract min Fib-Heap-Extract-Min(H) 1 z = H.min 2 if z = NIL 3 for each child x of z 4 add x to the root list of H 5 x.p = NIL 6 remove z from the root list of H 7 if z == z.right 8 H.min = NIL 9 else H.min = z.right 10 Consolidate(H) 11 H.n = H.n − 1 12 return z 42 / 120
  • 93. Extract min Extract min Fib-Heap-Extract-Min(H) 1 z = H.min 2 if z = NIL 3 for each child x of z 4 add x to the root list of H 5 x.p = NIL 6 remove z from the root list of H 7 if z == z.right 8 H.min = NIL 9 else H.min = z.right 10 Consolidate(H) 11 H.n = H.n − 1 12 return z 42 / 120
  • 94. Extract min Extract min Fib-Heap-Extract-Min(H) 1 z = H.min 2 if z = NIL 3 for each child x of z 4 add x to the root list of H 5 x.p = NIL 6 remove z from the root list of H 7 if z == z.right 8 H.min = NIL 9 else H.min = z.right 10 Consolidate(H) 11 H.n = H.n − 1 12 return z 42 / 120
  • 95. Extract min Extract min Fib-Heap-Extract-Min(H) 1 z = H.min 2 if z = NIL 3 for each child x of z 4 add x to the root list of H 5 x.p = NIL 6 remove z from the root list of H 7 if z == z.right 8 H.min = NIL 9 else H.min = z.right 10 Consolidate(H) 11 H.n = H.n − 1 12 return z 42 / 120
  • 96. Extract min Extract min Fib-Heap-Extract-Min(H) 1 z = H.min 2 if z = NIL 3 for each child x of z 4 add x to the root list of H 5 x.p = NIL 6 remove z from the root list of H 7 if z == z.right 8 H.min = NIL 9 else H.min = z.right 10 Consolidate(H) 11 H.n = H.n − 1 12 return z 42 / 120
  • 97. Extract min Extract min Fib-Heap-Extract-Min(H) 1 z = H.min 2 if z = NIL 3 for each child x of z 4 add x to the root list of H 5 x.p = NIL 6 remove z from the root list of H 7 if z == z.right 8 H.min = NIL 9 else H.min = z.right 10 Consolidate(H) 11 H.n = H.n − 1 12 return z 42 / 120
  • 98. What is happening here? First Here, the code in lines 3-6 remove the node z and adds the children of z to the root list of H. Next If the Fibonacci Heap is not empty a consolidation code is triggered. 43 / 120
  • 99. What is happening here? First Here, the code in lines 3-6 remove the node z and adds the children of z to the root list of H. Next If the Fibonacci Heap is not empty a consolidation code is triggered. 43 / 120
  • 100. What is happening here? Thus The consolidate code is used to eliminate subtrees that have the same 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. Without loss 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 by calling the FIB-HEAP-LINK procedure. This procedure increments the attribute x.degree and clears the mark on y. 44 / 120
  • 101. What is happening here? Thus The consolidate code is used to eliminate subtrees that have the same 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. Without loss 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 by calling the FIB-HEAP-LINK procedure. This procedure increments the attribute x.degree and clears the mark on y. 44 / 120
  • 102. What is happening here? Thus The consolidate code is used to eliminate subtrees that have the same 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. Without loss 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 by calling the FIB-HEAP-LINK procedure. This procedure increments the attribute x.degree and clears the mark on y. 44 / 120
  • 103. What is happening here? Thus The consolidate code is used to eliminate subtrees that have the same 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. Without loss 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 by calling the FIB-HEAP-LINK procedure. This procedure increments the attribute x.degree and clears the mark on y. 44 / 120
  • 104. What is happening here? Thus The consolidate code is used to eliminate subtrees that have the same 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. Without loss 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 by calling the FIB-HEAP-LINK procedure. This procedure increments the attribute x.degree and clears the mark on y. 44 / 120
  • 105. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 45 / 120
  • 106. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 107. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 108. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 109. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 110. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 111. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 112. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 113. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 114. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 115. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 116. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 117. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 118. Consolidate Code Consolidate(H) 1. Let A (0...D (H.n)) be a new array 2. for i = 0 to D (H.n) 3. A [i] = NIL 4. for each w in the root list of H 5. x = w 6. d = x.degree 7. while A [d] = NIL 8. y = A [d] 9. if x.key > y.key 10. exchange x with y 11. Fib-Heap-Link(H, y, x) 12. A [d] = NIL 13. d = d + 1 14. A [d] = x 15. H.min = NIL 16. for i = 0 to D (H.n) 17. if A [i] = NIL 18. if H.min == NIL 19. create a root list for H containing just A [i] 20. H.min = A [i] 21. else 22. insert A [i] into H s root list 24. if A [i] .key < H.min.key 25. H.min = A [i] 46 / 120
  • 119. Fib-Heap-Link Code Fib-Heap-Link(H, y, x) 1 Remove y from the root list of H 2 Make y a child of x, incrementing x.degree 3 y.mark = FALSE 47 / 120
  • 120. Fib-Heap-Link Code Fib-Heap-Link(H, y, x) 1 Remove y from the root list of H 2 Make y a child of x, incrementing x.degree 3 y.mark = FALSE 47 / 120
  • 121. Fib-Heap-Link Code Fib-Heap-Link(H, y, x) 1 Remove y from the root list of H 2 Make y a child of x, incrementing x.degree 3 y.mark = FALSE 47 / 120
  • 122. Auxiliary Array The Consolidate uses An auxiliary pointer array A [0...D (H.n)] It keeps track of The roots according the degree 48 / 120
  • 123. Auxiliary Array The Consolidate uses An auxiliary pointer array A [0...D (H.n)] It keeps track of The roots according the degree 48 / 120
  • 124. Code Process A while loop inside of the for loop - lines 1-15 Using a w variable to go through the root list This is used to fill the pointers in A [0...D (H.n)] Then you link both trees using who has a larger key Then you add a pointer to the new min-heap, with new degree, in A. Then - lines 15-23 Lines 15-23 clean the original Fibonacci Heap, then using the pointers at the array A, each subtree is inserted into the root list of H. 49 / 120
  • 125. Code Process A while loop inside of the for loop - lines 1-15 Using a w variable to go through the root list This is used to fill the pointers in A [0...D (H.n)] Then you link both trees using who has a larger key Then you add a pointer to the new min-heap, with new degree, in A. Then - lines 15-23 Lines 15-23 clean the original Fibonacci Heap, then using the pointers at the array A, each subtree is inserted into the root list of H. 49 / 120
  • 126. Code Process A while loop inside of the for loop - lines 1-15 Using a w variable to go through the root list This is used to fill the pointers in A [0...D (H.n)] Then you link both trees using who has a larger key Then you add a pointer to the new min-heap, with new degree, in A. Then - lines 15-23 Lines 15-23 clean the original Fibonacci Heap, then using the pointers at the array A, each subtree is inserted into the root list of H. 49 / 120
  • 127. Code Process A while loop inside of the for loop - lines 1-15 Using a w variable to go through the root list This is used to fill the pointers in A [0...D (H.n)] Then you link both trees using who has a larger key Then you add a pointer to the new min-heap, with new degree, in A. Then - lines 15-23 Lines 15-23 clean the original Fibonacci Heap, then using the pointers at the array A, each subtree is inserted into the root list of H. 49 / 120
  • 128. Code Process A while loop inside of the for loop - lines 1-15 Using a w variable to go through the root list This is used to fill the pointers in A [0...D (H.n)] Then you link both trees using who has a larger key Then you add a pointer to the new min-heap, with new degree, in A. Then - lines 15-23 Lines 15-23 clean the original Fibonacci Heap, then using the pointers at the array A, each subtree is inserted into the root list of H. 49 / 120
  • 129. Loop Invariance We have the following Loop Invariance At the start of each iteration of the while loop, d = x.degree. Init Line 6 ensures that the loop invariant holds the first time we enter the loop. Maintenance We 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 tree pointer to A with degree d + 1 50 / 120
  • 130. Loop Invariance We have the following Loop Invariance At the start of each iteration of the while loop, d = x.degree. Init Line 6 ensures that the loop invariant holds the first time we enter the loop. Maintenance We 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 tree pointer to A with degree d + 1 50 / 120
  • 131. Loop Invariance We have the following Loop Invariance At the start of each iteration of the while loop, d = x.degree. Init Line 6 ensures that the loop invariant holds the first time we enter the loop. Maintenance We 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 tree pointer to A with degree d + 1 50 / 120
  • 132. Loop Invariance Termination We repeat the while loop until A [d] = NIL, in which case there is no other root with the same degree as x. 51 / 120
  • 133. 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
  • 134. 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
  • 135. 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
  • 136. 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
  • 137. 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
  • 138. 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
  • 139. 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
  • 140. 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
  • 141. 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
  • 142. 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
  • 143. 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
  • 144. 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
  • 145. 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
  • 146. 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
  • 147. 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
  • 148. 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
  • 149. 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
  • 150. 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
  • 151. 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
  • 152. 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
  • 153. 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
  • 154. 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
  • 155. 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
  • 156. 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
  • 157. Example of consolidation Because A [1] = NIL, then jump over the while loop and make A [1] = x 23 7 1724 18 52 38 3026 46 39 41 35 51 21 0 1 2 3 76 / 120
  • 158. Example of consolidation Because A [1] = NIL insert into the root’s list and because H.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
  • 159. Example of consolidation Because A [2] = NIL insert into the root’s list no exchange of min because 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
  • 160. Example of consolidation Because A [3] = NIL insert into the root’s list no exchange of min because 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
  • 161. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 80 / 120
  • 162. Cost of Extract-min Amortized analysis observations The 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
  • 163. Cost of Extract-min Amortized analysis observations The 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
  • 164. Cost of Extract-min Amortized analysis observations The 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
  • 165. Cost of Extract-min Amortized analysis observations The 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
  • 166. Next We have that The 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
  • 167. Next We have that The 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
  • 168. Next We have that The 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
  • 169. Next Then At lines 4 to 14 in the CONSOLIDATE code: The amount of work done by the for and the while loop is proportional to D(n) + t(H) because each time we go through an element in the root list (for loop). The while loop consolidate the tree pointed by the pointer to a tree x with same degree. The Actual Cost is Then, the actual cost is ci = O (D (n) + t (H)). 83 / 120
  • 170. Next Then At lines 4 to 14 in the CONSOLIDATE code: The amount of work done by the for and the while loop is proportional to D(n) + t(H) because each time we go through an element in the root list (for loop). The while loop consolidate the tree pointed by the pointer to a tree x with same degree. The Actual Cost is Then, the actual cost is ci = O (D (n) + t (H)). 83 / 120
  • 171. Next Then At lines 4 to 14 in the CONSOLIDATE code: The amount of work done by the for and the while loop is proportional to D(n) + t(H) because each time we go through an element in the root list (for loop). The while loop consolidate the tree pointed by the pointer to a tree x with same degree. The Actual Cost is Then, the actual cost is ci = O (D (n) + t (H)). 83 / 120
  • 172. Next Then At lines 4 to 14 in the CONSOLIDATE code: The amount of work done by the for and the while loop is proportional to D(n) + t(H) because each time we go through an element in the root list (for loop). The while loop consolidate the tree pointed by the pointer to a tree x with same degree. The Actual Cost is Then, the actual cost is ci = O (D (n) + t (H)). 83 / 120
  • 173. 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 H’ has at most D(n) + 1 elements after consolidation No node is marked in the process. 84 / 120
  • 174. 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 H’ has at most D(n) + 1 elements after consolidation No node is marked in the process. 84 / 120
  • 175. 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 H’ has at most D(n) + 1 elements after consolidation No node is marked in the process. 84 / 120
  • 176. 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 H’ has at most D(n) + 1 elements after consolidation No node is marked in the process. 84 / 120
  • 177. 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 that D(n) = O(log n) 85 / 120
  • 178. 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 that D(n) = O(log n) 85 / 120
  • 179. 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 that D(n) = O(log n) 85 / 120
  • 180. 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 that D(n) = O(log n) 85 / 120
  • 181. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 86 / 120
  • 182. Decreasing a key Fib-Heap-Decrease-Key(H, x, k) 1 if k > x.key 2 error “new key is greater than current key” 3 x.key = k 4 y = x.p 5 if y = NIL and x.key < y.key 6 CUT(H, x, y) 7 Cascading-Cut(H, y) 8 if x.key < H.min.key 9 H.min = x 87 / 120
  • 183. Decreasing a key Fib-Heap-Decrease-Key(H, x, k) 1 if k > x.key 2 error “new key is greater than current key” 3 x.key = k 4 y = x.p 5 if y = NIL and x.key < y.key 6 CUT(H, x, y) 7 Cascading-Cut(H, y) 8 if x.key < H.min.key 9 H.min = x 87 / 120
  • 184. Decreasing a key Fib-Heap-Decrease-Key(H, x, k) 1 if k > x.key 2 error “new key is greater than current key” 3 x.key = k 4 y = x.p 5 if y = NIL and x.key < y.key 6 CUT(H, x, y) 7 Cascading-Cut(H, y) 8 if x.key < H.min.key 9 H.min = x 87 / 120
  • 185. Decreasing a key Fib-Heap-Decrease-Key(H, x, k) 1 if k > x.key 2 error “new key is greater than current key” 3 x.key = k 4 y = x.p 5 if y = NIL and x.key < y.key 6 CUT(H, x, y) 7 Cascading-Cut(H, y) 8 if x.key < H.min.key 9 H.min = x 87 / 120
  • 186. Explanation First 1 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
  • 187. Explanation First 1 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
  • 188. 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 H 3 x.p = NIL 4 x.mark = FALSE Cascading-Cut(H, y) 1 z = y.p 2 if z = NIL 3 if y.mark == FALSE 4 y.mark=TRUE 5 else 6 Cut(H, y, z) 7 Cascading-Cut(H, y)(H, z) 89 / 120
  • 189. 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 H 3 x.p = NIL 4 x.mark = FALSE Cascading-Cut(H, y) 1 z = y.p 2 if z = NIL 3 if y.mark == FALSE 4 y.mark=TRUE 5 else 6 Cut(H, y, z) 7 Cascading-Cut(H, y)(H, z) 89 / 120
  • 190. Explanation Second Then CUT simply removes x from the child-list of y. Thus The CASCADING-CUT uses the mark attributes to obtain the desired time bounds. 90 / 120
  • 191. Explanation Second Then CUT simply removes x from the child-list of y. Thus The CASCADING-CUT uses the mark attributes to obtain the desired time bounds. 90 / 120
  • 192. 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 and one child of y has been cut. The CUT procedure, therefore, clears y.mark in line 4, since it performs step 1. We can now see why line 3 of FIB-HEAP-LINK clears y.mark. 91 / 120
  • 193. 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 and one child of y has been cut. The CUT procedure, therefore, clears y.mark in line 4, since it performs step 1. We can now see why line 3 of FIB-HEAP-LINK clears y.mark. 91 / 120
  • 194. 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 and one child of y has been cut. The CUT procedure, therefore, clears y.mark in line 4, since it performs step 1. We can now see why line 3 of FIB-HEAP-LINK clears y.mark. 91 / 120
  • 195. 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 and one child of y has been cut. The CUT procedure, therefore, clears y.mark in line 4, since it performs step 1. We can now see why line 3 of FIB-HEAP-LINK clears y.mark. 91 / 120
  • 196. 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 and one child of y has been cut. The CUT procedure, therefore, clears y.mark in line 4, since it performs step 1. We can now see why line 3 of FIB-HEAP-LINK clears y.mark. 91 / 120
  • 197. 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 and one child of y has been cut. The CUT procedure, therefore, clears y.mark in line 4, since it performs step 1. We can now see why line 3 of FIB-HEAP-LINK clears y.mark. 91 / 120
  • 198. Explanation Now we have a new problem x might be the second child cut from its parent y to another node. Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a cascading-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 done the H.min is updated if necessary 92 / 120
  • 199. Explanation Now we have a new problem x might be the second child cut from its parent y to another node. Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a cascading-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 done the H.min is updated if necessary 92 / 120
  • 200. Explanation Now we have a new problem x might be the second child cut from its parent y to another node. Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a cascading-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 done the H.min is updated if necessary 92 / 120
  • 201. Explanation Now we have a new problem x might be the second child cut from its parent y to another node. Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a cascading-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 done the H.min is updated if necessary 92 / 120
  • 202. Explanation Now we have a new problem x might be the second child cut from its parent y to another node. Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a cascading-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 done the H.min is updated if necessary 92 / 120
  • 203. Explanation Now we have a new problem x might be the second child cut from its parent y to another node. Therefore, in line 7 of FIB-HEAP-DECREASE attempts to perform a cascading-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 done the H.min is updated if necessary 92 / 120
  • 204. Example 46 is decreased to 15 23 7 1724 18 52 38 3026 46 39 41 35 51 21 93 / 120
  • 205. Example 46 is decreased to 15 23 7 1724 18 52 38 3026 39 41 35 51 21 15 94 / 120
  • 206. Example Cut 15 to the root’s list 23 7 1724 18 52 38 3026 39 41 35 51 21 15 95 / 120
  • 207. Example Mark 24 to True 23 7 1724 18 52 38 3026 39 41 35 51 21 15 96 / 120
  • 208. Example Then 35 is decreased to 5 23 7 1724 18 52 38 3026 39 41 5 51 21 15 97 / 120
  • 209. Example Cut 15 to the root’s list 23 7 1724 18 52 38 3026 39 41 5 51 21 15 98 / 120
  • 211. 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
  • 212. Example Mark 26 to false 23 7 1724 18 52 38 30 26 39 41 5 51 21 15 101 / 120
  • 213. Example Move 24 to root’s list 23 7 17 24 18 52 38 30 26 39 41 5 51 21 15 102 / 120
  • 214. Example Mark 26 to false 23 7 17 24 18 52 38 30 26 39 41 5 51 21 15 103 / 120
  • 215. Example Change the H.min 23 7 17 24 18 52 38 30 26 39 41 5 51 21 15 104 / 120
  • 216. Potential cost The procedure Decrease-Key takes ci = O(1)+the cascading-cuts Assume that you require c calls to cascade CASCADING-CUT One for the line 6 at the code FIB-HEAP-DECREASE-KEY followed by c − 1 others The cost of it will take ci = O(c). 105 / 120
  • 217. Potential cost The procedure Decrease-Key takes ci = O(1)+the cascading-cuts Assume that you require c calls to cascade CASCADING-CUT One for the line 6 at the code FIB-HEAP-DECREASE-KEY followed by c − 1 others The cost of it will take ci = O(c). 105 / 120
  • 218. Potential cost The procedure Decrease-Key takes ci = O(1)+the cascading-cuts Assume that you require c calls to cascade CASCADING-CUT One for the line 6 at the code FIB-HEAP-DECREASE-KEY followed by c − 1 others The cost of it will take ci = O(c). 105 / 120
  • 219. 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 The # original trees + the ones created by the c calls. m (H) − (c − 1) + 1 The original marks - (c − 1) cleared marks by Cut + the branch to y.mark == FALSE true. 106 / 120
  • 220. 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 The # original trees + the ones created by the c calls. m (H) − (c − 1) + 1 The original marks - (c − 1) cleared marks by Cut + the branch to y.mark == FALSE true. 106 / 120
  • 221. 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 The # original trees + the ones created by the c calls. m (H) − (c − 1) + 1 The original marks - (c − 1) cleared marks by Cut + the branch to y.mark == FALSE true. 106 / 120
  • 222. 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 The # original trees + the ones created by the c calls. m (H) − (c − 1) + 1 The original marks - (c − 1) cleared marks by Cut + the branch to y.mark == FALSE true. 106 / 120
  • 223. Final change in potential Thus Φ 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) Observation Now 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
  • 224. Final change in potential Thus Φ 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) Observation Now 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
  • 225. Final change in potential Thus Φ 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) Observation Now 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
  • 226. Final change in potential Thus Φ 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) Observation Now 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
  • 227. Final change in potential Thus Φ 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) Observation Now 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
  • 228. Final change in potential Thus Φ 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) Observation Now 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
  • 229. Delete a node It is easy to delete a node in the Fibonacci heap following the next code 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
  • 230. Proving the D (n) bound!!! Let’s define the following We define a quantity size(x)= the number of nodes at subtree rooted at x, 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
  • 231. Proving the D (n) bound!!! Let’s define the following We define a quantity size(x)= the number of nodes at subtree rooted at x, 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
  • 232. Proving the D (n) bound!!! Let’s define the following We define a quantity size(x)= the number of nodes at subtree rooted at x, 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
  • 233. Now... Lemma 19.1 Let 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 were linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and yi.degree ≥ i − 2 for i = 2, 3, ..., k. Proof 1 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. Note: It would have been cut from x if it had lost two children. 5 We conclude that yi.degree ≥ i − 2. 110 / 120
  • 234. Now... Lemma 19.1 Let 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 were linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and yi.degree ≥ i − 2 for i = 2, 3, ..., k. Proof 1 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. Note: It would have been cut from x if it had lost two children. 5 We conclude that yi.degree ≥ i − 2. 110 / 120
  • 235. Now... Lemma 19.1 Let 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 were linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and yi.degree ≥ i − 2 for i = 2, 3, ..., k. Proof 1 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. Note: It would have been cut from x if it had lost two children. 5 We conclude that yi.degree ≥ i − 2. 110 / 120
  • 236. Now... Lemma 19.1 Let 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 were linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and yi.degree ≥ i − 2 for i = 2, 3, ..., k. Proof 1 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. Note: It would have been cut from x if it had lost two children. 5 We conclude that yi.degree ≥ i − 2. 110 / 120
  • 237. Now... Lemma 19.1 Let 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 were linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and yi.degree ≥ i − 2 for i = 2, 3, ..., k. Proof 1 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. Note: It would have been cut from x if it had lost two children. 5 We conclude that yi.degree ≥ i − 2. 110 / 120
  • 238. Now... Lemma 19.1 Let 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 were linked to x, from the earliest to the latest. Then y1.degree ≥ 0 and yi.degree ≥ i − 2 for i = 2, 3, ..., k. Proof 1 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. Note: It would have been cut from x if it had lost two children. 5 We conclude that yi.degree ≥ i − 2. 110 / 120
  • 239. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 111 / 120
  • 240. Why Fibonacci? The kth Fibonacci number is defined by the recurrence Fk =    0 if k = 0 1 if k = 1 Fk−1 + Fk−2 if k ≥ 2 Lemma 19.2 For al integers k ≥ 0 Fk+2 = 1 + k i=0 Fi Proof by induction k = 0.... 112 / 120
  • 241. Why Fibonacci? The kth Fibonacci number is defined by the recurrence Fk =    0 if k = 0 1 if k = 1 Fk−1 + Fk−2 if k ≥ 2 Lemma 19.2 For al integers k ≥ 0 Fk+2 = 1 + k i=0 Fi Proof by induction k = 0.... 112 / 120
  • 242. The use of the Golden Ratio Lemma 19.3 Let 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
  • 243. Golden Ratio Building 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. 114 / 120
  • 244. Golden Ratio Building 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. 114 / 120
  • 245. Golden Ratio Building 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
  • 246. The use of the Golden Ratio Lemma 19.4 Let x be any node in a Fibonacci Heap, k = x.degree. Then size(x) ≥ Fk+2 ≥ Φk. Proof Let sk denote the minimum value for size(x) over all nodes x such that x.degree = k. Restrictions for sk: sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing). As in the previous lemma y1, y2, ..., yk denote the children of x in the order they were linked to x. 115 / 120
  • 247. The use of the Golden Ratio Lemma 19.4 Let x be any node in a Fibonacci Heap, k = x.degree. Then size(x) ≥ Fk+2 ≥ Φk. Proof Let sk denote the minimum value for size(x) over all nodes x such that x.degree = k. Restrictions for sk: sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing). As in the previous lemma y1, y2, ..., yk denote the children of x in the order they were linked to x. 115 / 120
  • 248. The use of the Golden Ratio Lemma 19.4 Let x be any node in a Fibonacci Heap, k = x.degree. Then size(x) ≥ Fk+2 ≥ Φk. Proof Let sk denote the minimum value for size(x) over all nodes x such that x.degree = k. Restrictions for sk: sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing). As in the previous lemma y1, y2, ..., yk denote the children of x in the order they were linked to x. 115 / 120
  • 249. The use of the Golden Ratio Lemma 19.4 Let x be any node in a Fibonacci Heap, k = x.degree. Then size(x) ≥ Fk+2 ≥ Φk. Proof Let sk denote the minimum value for size(x) over all nodes x such that x.degree = k. Restrictions for sk: sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing). As in the previous lemma y1, y2, ..., yk denote the children of x in the order they were linked to x. 115 / 120
  • 250. The use of the Golden Ratio Lemma 19.4 Let x be any node in a Fibonacci Heap, k = x.degree. Then size(x) ≥ Fk+2 ≥ Φk. Proof Let sk denote the minimum value for size(x) over all nodes x such that x.degree = k. Restrictions for sk: sk ≤ size(x) and sk ≤ sk+1 (monotonically increasing). As in the previous lemma y1, y2, ..., yk denote the children of x in the order they were linked to x. 115 / 120
  • 251. Example For example Now Look at the board 116 / 120
  • 252. Example For example Now Look at the board 116 / 120
  • 253. The use of the Golden Ratio Proof continuation Now, 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
  • 254. The use of the Golden Ratio Proof continuation Now, 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
  • 255. The use of the Golden Ratio Proof continuation Now, 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
  • 256. Finally Corollary 19.5 The maximum degree D(n) of any node in an n-node Fibonacci heap is O(log n). Proof Look at the board! 118 / 120
  • 257. Finally Corollary 19.5 The maximum degree D(n) of any node in an n-node Fibonacci heap is O(log n). Proof Look at the board! 118 / 120
  • 258. Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci Heap Why Fibonacci Heaps? Node Structure Fibonacci Heaps Operations Mergeable-Heaps operations - Make Heap Mergeable-Heaps operations - Insertion Mergeable-Heaps operations - Minimum Mergeable-Heaps operations - Union Complexity Analysis Consolidate Algorithm Potential cost Operation: Decreasing a Key Why Fibonacci? 4 Exercises Some Exercises that you can try 119 / 120
  • 259. Exercises From Cormen’s book solve the following 20.2-2 20.2-3 20.2-4 20.3-1 20.3-2 20.4-1 20.4-2 120 / 120