SlideShare a Scribd company logo
1 of 71
Download to read offline
POSCAT Seminar 4 :
Adv. Data Structure
yougatup @ POSCAT
1
Topic
 Topic today
− Heap ( Just for your knowledge )
• D-ary heap
• Binomial heap
• Fibonacci heap
− Indexed Tree
• Binary Indexed Tree
• Fenwick Tree
− Implementation of Indexed Tree
POSCAT Seminar 1-2
2 July 2014
yougatup
Heap
 You already know what it is
− Definition ?
 Amazingly, there is faster data structures
− Fibonacci heap 은 이론적으로 Time complexity 가장 적음
− But, 구현해보면 느림  실제로 쓰지는 않습니다
− d-ary heap, Binomial heap, Fibonacci heap
 Operations
− find_min, delete_min, insert, delete, decrease_key
POSCAT Seminar 1-3
2 July 2014
yougatup
𝑑-ary heap
 It has child at most 𝑑
− Extended version of binary heap
− How it works ?
− We use it for Prim algorithm
POSCAT Seminar 1-4
2 July 2014
yougatup
find_min ?
insert ?
delete ?
decrease_key ?
delete_min ?
⋅⋅⋅
at most 𝑑
𝑑-ary heap
POSCAT Seminar 1-5
2 July 2014
yougatup
⋅⋅⋅
at most 𝑑find_min : root !
insert : insert to right-most
delete : decrease a value to -∞, and delete_min
decrease_key : decrease a value, and rearrange heap
delete_min : like binary heap 
 It has child at most 𝑑
− Extended version of binary heap
− How it works ?
− We use it for Prim algorithm
𝑑-ary heap
POSCAT Seminar 1-6
2 July 2014
yougatup
⋅⋅⋅
at most 𝑑find_min : root !
insert : insert to right-most
delete : decrease a value to -∞, and delete_min
decrease_key : decrease a value, and rearrange heap
delete_min : like binary heap 
Time complexity ?
 It has child at most 𝑑
− Extended version of binary heap
− How it works ?
− We use it for Prim algorithm
𝑑-ary heap
POSCAT Seminar 1-7
2 July 2014
yougatup
⋅⋅⋅
at most 𝑑
Time complexity ?
find_min : O(1)
insert : O(log 𝑑 𝑛)
delete : O(𝑑 log 𝑑 𝑛)
decrease_key : O(log 𝑑 𝑛)
delete_min : O(𝑑 log 𝑑 𝑛)
 It has child at most 𝑑
− Extended version of binary heap
− How it works ?
− We use it for Prim algorithm
Binomial tree
 Definition
A binomial tree of height 𝑘 (denoted by 𝐵 𝑘) is defined as follows
1. 𝐵0 consists of a single node.
2. 𝐵 𝑘 is formed by joining two 𝐵 𝑘−1 trees, making one’s root child
of the other
POSCAT Seminar 1-8
2 July 2014
yougatup
𝐵0 𝐵1 𝐵2 𝐵3
Binomial tree
 Property
1. 𝐵 𝑘 has 2 𝑘 nodes
2. The height of 𝐵 𝑘 is 𝑘 = log |𝐵 𝑘|
3. The root of 𝐵 𝑘 has 𝑘 children
Use mathematical induction to prove
Therefore, the height and the degree of a node 𝑣 in a binomial tree
are both at most logarithmic in the size of the subtree rooted at 𝑣
POSCAT Seminar 1-9
2 July 2014
yougatup
Binomial tree
 Storing data
How can we store 𝑛 ≠ 2 𝑘
nodes using binomial tree ?
POSCAT Seminar 1-10
2 July 2014
yougatup
Binomial tree
 Storing data
How can we store 𝑛 ≠ 2 𝑘
nodes using binomial tree ?
Think about binary notation of 𝑛
POSCAT Seminar 1-11
2 July 2014
yougatup
Binomial tree
 Storing data
How can we store 𝑛 ≠ 2 𝑘
nodes using binomial tree ?
Think about binary notation of 𝑛
We can use binomial tree chain ! We call it as root list
POSCAT Seminar 1-12
2 July 2014
yougatup
head 10 1 6
12 25
18
8
11 17
27
29 14
38
Binomial heap
 Definition
A binomial heap is a (set of) binomial tree(s) where each node is
associated with a key and the heap-order property is preserved.
We also have the requirement that for any 𝑖 there is at most one 𝐵𝑖.
In other words, it doesn’t contain two 𝐵𝑖 in the binomial heap.
POSCAT Seminar 1-13
2 July 2014
yougatup
head 10 1 6
12 18
25
8
11 17
27
29 14
38
unique 𝐵2
Binomial heap
 Operations
− find_min, delete_min, insert, delete, decrease_key
POSCAT Seminar 1-14
2 July 2014
yougatup
Binomial heap
 Find_min
We need to consider all the root nodes of binomial tree
It takes O(log 𝑛). Why ?
POSCAT Seminar 1-15
2 July 2014
yougatup
head 10 1 6
12 18
25
8
11 17
27
29 14
38
Binomial heap
 Find_min
We need to consider all the root nodes of binomial tree
It takes O(log 𝑛). Why ? we have log 𝑛 binomial trees
POSCAT Seminar 1-16
2 July 2014
yougatup
head 10 1 6
12 18
25
8
11 17
27
29 14
38
Binomial heap
 Decrease_key
Decrease the value and rearrange binomial tree
It takes O(log 𝑛)
POSCAT Seminar 1-17
2 July 2014
yougatup
head 10 1 6
12 18
25
8
11
27
29 14
38
17
Binomial heap
 Decrease_key
Decrease the value and rearrange binomial tree
It takes O(log 𝑛)
POSCAT Seminar 1-18
2 July 2014
yougatup
head 10 1 6
12 18
25
8
11
27
29 14
38
2
Binomial heap
 Decrease_key
Decrease the value and rearrange binomial tree
It takes O(log 𝑛)
POSCAT Seminar 1-19
2 July 2014
yougatup
head 10 1 6
12 18
25
11
27
29 14
38
2
8
Binomial heap
 Decrease_key
Decrease the value and rearrange binomial tree
It takes O(log 𝑛)
POSCAT Seminar 1-20
2 July 2014
yougatup
head 10 1
12 18
25
11
27
29 14
38
8
6
2
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
POSCAT Seminar 1-21
2 July 2014
yougatup
head 10
12 18
25
11
27
29 14
38
8
6
21
delete it !
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
POSCAT Seminar 1-22
2 July 2014
yougatup
head 10
12 18
25
11
27
29 14
38
8
6
2
delete it ?
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
POSCAT Seminar 1-23
2 July 2014
yougatup
head 10 12 18
25
11
27
29 14
38
8
6
2 head’
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one ! How ?
POSCAT Seminar 1-24
2 July 2014
yougatup
head 10 12 18
25
11
27
29 14
38
8
6
2 head’
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one ! How ? Merging on merge sort ?
POSCAT Seminar 1-25
2 July 2014
yougatup
head 10 12 18
25
11
27
29 14
38
8
6
2 head’
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one !
POSCAT Seminar 1-26
2 July 2014
yougatup
head 10 12 18
25
11
27
29 14
38
8
6
2
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one !
Rearrange the binomial heap
POSCAT Seminar 1-27
2 July 2014
yougatup
head 10 12 18
25
11
27
29 14
38
8
6
2
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one !
Rearrange the binomial heap
POSCAT Seminar 1-28
2 July 2014
yougatup
head 18
25
11
27
29 14
38
8
6
210 12
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one !
Rearrange the binomial heap
POSCAT Seminar 1-29
2 July 2014
yougatup
head 18
25
11
27
29 14
38
8
6
2
12
10
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one !
Rearrange the binomial heap
POSCAT Seminar 1-30
2 July 2014
yougatup
head
11
27
29 14
38
8
6
218
2512
10
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one !
Rearrange the binomial heap
POSCAT Seminar 1-31
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Binomial heap
 Delete_min
If we delete the root node of binomial tree, it is split into many trees
Make another chain ! Then we get another binomial heap.
Merge two binomial heaps into one !
Rearrange the binomial heap
POSCAT Seminar 1-32
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Binomial heap
 Delete_min
Time complexity ?
POSCAT Seminar 1-33
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Binomial heap
 Delete_min
Time complexity ?
Each node has at most log 𝑛 child.
POSCAT Seminar 1-34
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Binomial heap
 Delete_min
Time complexity ?
Each node has at most log 𝑛 child.
New binomial heap consists of at most log 𝑛 binomial trees
POSCAT Seminar 1-35
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Binomial heap
 Delete_min
Time complexity ?
Each node has at most log 𝑛 child.
New binomial heap consists of at most log 𝑛 binomial trees
Therefore, Merging and rearrange takes O(log 𝑛).
POSCAT Seminar 1-36
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Binomial heap
 Insert
Insert a new node and rearrange binomial heap
O(log 𝑛)
POSCAT Seminar 1-37
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Binomial heap
 Delete
Make it as -∞, and delete_min !
O(log 𝑛)
POSCAT Seminar 1-38
2 July 2014
yougatup
head
11
27
29 14
38
8
6
2
18
25
12
10
Fibonacci heap
 Theoretically fastest heap
− But it is just theoretical. It is slow if you implement it
− We don’t use it
 Similar with binomial heap
− Relaxed version of binomial heap
− Use a lazy update scheme.
POSCAT Seminar 1-39
2 July 2014
yougatup
Fibonacci heap
 Main feature
− Individual trees are not necessarily binomial (let me denote it as 𝐵𝑖′)
− Allow many copies of 𝐵𝑖′ for the same 𝑖.
 Operations
− find_min, delete_min, insert, delete, decrease_key
POSCAT Seminar 1-40
2 July 2014
yougatup
head 10 1 6
12 18
8
11 17
14
38
I allow it because I’m lazy 
𝐵0′ 𝐵2′ 𝐵3′
3
11 27
𝐵2′
20
Fibonacci heap
 Find_min
via pointer !
POSCAT Seminar 1-41
2 July 2014
yougatup
head 10 1 6
12 18
8
11 17
14
38
𝐵0′ 𝐵2′ 𝐵3′
3
11 27
𝐵2′
20
Fibonacci heap
 Insert
Create a node. No update because I’m lazy 
Give $1 to inserted node. I’ll explain the meaning of ‘coin’ later.
POSCAT Seminar 1-42
2 July 2014
yougatup
10 1 6
12 18
8
11 17
14
38
𝐵0′ 𝐵2′ 𝐵3′
3
11 27
𝐵2′
20
head 7
𝐵0′
Fibonacci heap
 Delete
-∞, delete_min
POSCAT Seminar 1-43
2 July 2014
yougatup
10 1 6
12 18
8
11 17
14
38
𝐵0′ 𝐵2′ 𝐵3′
3
11 27
𝐵2′
20
head 7
𝐵0′
Fibonacci heap
 Delete_min
Delete min and rearrange Fibonacci heap.
Time complexity will proportional to the number of trees.
Is it fast ?
POSCAT Seminar 1-44
2 July 2014
yougatup
10 1 6
12 18
8
11 17
14
38
𝐵0′ 𝐵2′ 𝐵3′
3
11 27
𝐵2′
20
head 7
𝐵0′
Fibonacci heap
 Delete_min
Delete min and rearrange Fibonacci heap.
Time complexity will proportional to the number of trees.
Is it fast ?
POSCAT Seminar 1-45
2 July 2014
yougatup
10 612 18
8
11 17
14
38
𝐵0′ 𝐵3′
3
11 27
𝐵2′
20
head 7
𝐵0′ 𝐵0′ 𝐵1′
Fibonacci heap
 Delete_min
Delete min and rearrange Fibonacci heap.
Time complexity will proportional to the number of trees.
Is it fast ?
POSCAT Seminar 1-46
2 July 2014
yougatup
10 6
12
18
8
11 17
14
38
𝐵1′ 𝐵3′
3
11 27
𝐵2′
20
head 7
𝐵0′ 𝐵1′
Fibonacci heap
 Delete_min
Delete min and rearrange Fibonacci heap.
Time complexity will proportional to the number of trees.
Is it fast ?
POSCAT Seminar 1-47
2 July 2014
yougatup
10 6
12 18
8
11 17
14
38
𝐵2′ 𝐵3′
3
11 27
𝐵2′
20
head 7
𝐵0′
Fibonacci heap
 Delete_min
Delete min and rearrange Fibonacci heap.
Time complexity will proportional to the number of trees.
Is it fast ?
POSCAT Seminar 1-48
2 July 2014
yougatup
10
6
12 18
8
11 17
14
38
𝐵3′
3
11 27 20
head 7
𝐵0′ 𝐵3′
Fibonacci heap
 Delete_min
Delete min and rearrange Fibonacci heap.
Time complexity will proportional to the number of trees.
Is it fast ?
POSCAT Seminar 1-49
2 July 2014
yougatup
106
12 18
8
11 17
14
38
3
11 27
20
head 7
𝐵0′ 𝐵4′
Fibonacci heap
 Delete_min
Delete min and rearrange Fibonacci heap.
Time complexity will proportional to the number of trees.
Is it fast ?  we’ll analyze it soon.
POSCAT Seminar 1-50
2 July 2014
yougatup
106
12 18
8
11 17
14
38
3
11 27
20
head 7
𝐵0′ 𝐵4′
Fibonacci heap
 Decrease_key
Decrease the value of the element. If the heap-order property is
violated, cut the link between the node and its parent. ( It may
produce a result which is not a binomial tree)
Not just cut, we use “Cascading cut”
POSCAT Seminar 1-51
2 July 2014
yougatup
106
12 18
8
11 17
14
38
3
11 27
20
head 7
𝐵0′ 𝐵4′
Fibonacci heap
 Decrease_key
Decrease the value of the element. If the heap-order property is
violated, cut the link between the node and its parent. ( It may
produce a result which is not a binomial tree)
Not just cut, we use “Cascading cut”
POSCAT Seminar 1-52
2 July 2014
yougatup
6
12 18
8
11 17
14
38
3
11 27
20
head 7
𝐵0′ 𝐵4′
10
decrease it to 4
Fibonacci heap
 Decrease_key
Decrease the value of the element. If the heap-order property is
violated, cut the link between the node and its parent. ( It may
produce a result which is not a binomial tree)
Not just cut, we use “Cascading cut”
POSCAT Seminar 1-53
2 July 2014
yougatup
6
12 18
8
11 17
14
38
3
11 27
20
head 7
𝐵4′
4
decrease it to 4
 heap-order is violated
𝐵0′
Fibonacci heap
 Decrease_key
Decrease the value of the element. If the heap-order property is
violated, cut the link between the node and its parent. ( It may
produce a result which is not a binomial tree)
Not just cut, we use “Cascading cut”
POSCAT Seminar 1-54
2 July 2014
yougatup
6
12 18
8
11 17
14
38
3
11 27
20
head 7
𝐵4′
4
decrease it to 4
 heap-order is violated
 Cut !
𝐵0′𝐵0′
Fibonacci heap
 Decrease_key
Decrease the value of the element. If the heap-order property is
violated, cut the link between the node and its parent. ( It may
produce a result which is not a binomial tree)
Not just cut, we use “Cascading cut”
POSCAT Seminar 1-55
2 July 2014
yougatup
6
12 18
8
11 17
14
38
3
11 27
20
head 7
𝐵4′
4
decrease it to 4
 heap-order is violated
 Cut !
𝐵0′ 𝐵0′ 𝐵1′𝐵0′
Cascading cut
 Definition
1. Whenever a node is being cut, mark the parent of the cut node in
the original tree, and
• Pay $1 for the cut
• Store $2 in the parent of the cut node
• Store $1 in the new root (cut node).
POSCAT Seminar 1-56
2 July 2014
yougatup
6
12 18
8
11 17
14
38
11 27
20
head 7
𝐵0′
𝐵4′
decrease it to 4
 heap-order is violated
 Cut !
𝐵0′ 𝐵0′ 𝐵1′
34 marked!
Cascading cut
 Definition
2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already
marked node), by that time node 𝑣 will have accumulated $ 4;
recursively cut that node from its parent, marking again the parent
of 𝑣 and using $4 to pay for the operation before.
$1 for the cut, $2 to its parent, $1 to the new root
POSCAT Seminar 1-57
2 July 2014
yougatup
6
12 18
8
11 17
14
38
27
20
head 7
𝐵4′
𝐵0′ 𝐵0′ 𝐵1′
4 3
11
decrease it to 2 !
𝐵0′
Cascading cut
 Definition
2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already
marked node), by that time node 𝑣 will have accumulated $ 4;
recursively cut that node from its parent, marking again the parent
of 𝑣 and using $4 to pay for the operation before.
$1 for the cut, $2 to its parent, $1 to the new root
POSCAT Seminar 1-58
2 July 2014
yougatup
6
12 18
8
11 17
14
38
27
20
head 7
𝐵4′
𝐵0′ 𝐵0′ 𝐵1′
4 3
2
decrease it to 2 !
𝐵0′
Cascading cut
 Definition
2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already
marked node), by that time node 𝑣 will have accumulated $ 4;
recursively cut that node from its parent, marking again the parent
of 𝑣 and using $4 to pay for the operation before.
$1 for the cut, $2 to its parent, $1 to the new root
POSCAT Seminar 1-59
2 July 2014
yougatup
6
12 18
8
11 17
14
38
27
20
head 7
𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′
4 32
decrease it to 2 !
𝐵1′ 𝐵4′
marked twice. It has $4
Cascading cut
 Definition
2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already
marked node), by that time node 𝑣 will have accumulated $ 4;
recursively cut that node from its parent, marking again the parent
of 𝑣 and using $4 to pay for the operation before.
$1 for the cut, $2 to its parent, $1 to the new root
POSCAT Seminar 1-60
2 July 2014
yougatup
612 18
8
11 17
14
38
27
20
head 7
𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′
4 2
𝐵1′ 𝐵3′𝐵0′
3
𝐵0′
Cascading cut
 Time complexity of cascading cut
Each node has the coin they already have.
Each cut requires only $4, and decrease_key takes overall still
amortized time O(1) ( Overall cost / The number of operation )
POSCAT Seminar 1-61
2 July 2014
yougatup
612 18
8
11 17
14
38
27
20
head 7
𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′
4 2
𝐵1′ 𝐵3′𝐵0′
3
𝐵0′
Fibonacci heap
 Decrease_key
Decrease the value of the element. If the heap-order property is
violated, cut the link between the node and its parent. ( It may
produce a result which is not a binomial tree)
Not just cut, we use “Cascading cut”
POSCAT Seminar 1-62
2 July 2014
yougatup
612 18
8
11 17
14
38
27
20
head 7
𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′
4 2
𝐵1′ 𝐵3′𝐵0′
3
𝐵0′
Fibonacci heap
 Decrease_key
Decrease the value of the element. If the heap-order property is
violated, cut the link between the node and its parent. ( It may
produce a result which is not a binomial tree)
Not just cut, we use “Cascading cut”
POSCAT Seminar 1-63
2 July 2014
yougatup
6
12
18
8
11 17
14
38
2720
head 7
𝐵0′
4
3
Rearrange !
𝐵4′
2
Fibonacci heap
 Analysis
Cascading cut takes O(1)
How much is merging cost ?
POSCAT Seminar 1-64
2 July 2014
yougatup
6
12
18
8
11 17
14
38
2720
head 7
𝐵0′
4
3
Rearrange !
𝐵4′
2
Fibonacci heap
 Analysis
Cascading cut takes O(1)
How much is merging cost ? O(1) because of the coin
POSCAT Seminar 1-65
2 July 2014
yougatup
6
12
18
8
11 17
14
38
2720
head 7
𝐵0′
4
3
Rearrange !
𝐵4′
2
Fibonacci heap
 Analysis
Cascading cut takes O(1)
After cut, we get (# of child) additional trees
How much is merging cost per child ? O(1) because of the coin
How many child does a node has ?
POSCAT Seminar 1-66
2 July 2014
yougatup
6
12
18
8
11 17
14
38
2720
head 7
𝐵0′
4
3
Rearrange !
𝐵4′
2
Fibonacci heap
 Analysis
Cascading cut takes O(1)
After cut, we get (# of child) additional trees
How much is merging cost per child ? O(1) because of the coin
How many child does a node has ?  Degree of a node
POSCAT Seminar 1-67
2 July 2014
yougatup
6
12
18
8
11 17
14
38
2720
head 7
𝐵0′
4
3
Rearrange !
𝐵4′
2
Fibonacci heap
 Analysis
Let 𝑥 be any node in a Fibonacci heap. Then
size(𝑥) ≥ 𝐹𝑘+2 ≥ ∅ 𝑘
where 𝑘 is the degree of 𝑥, ∅ is golden ratio
Proof by induction on 𝑘. Consider a node 𝑥 of degree 𝑘 = 𝑛 + 1.
Let 𝑦𝑖 be the 𝑘 children of 𝑥, from oldest to youngest (i.e. 𝑦0 was
made children of 𝑥 before 𝑦1, and so on). Then
size(𝑥) = 1 + size(𝑦0) + size(𝑦1) + … + size(𝑦 𝑘−1)
POSCAT Seminar 1-68
2 July 2014
yougatup
Fibonacci heap
 Analysis
Let 𝑥 be any node in a Fibonacci heap. Then
size(𝑥) ≥ 𝐹𝑘+2 ≥ ∅ 𝑘
where 𝑘 is the degree of 𝑥, ∅ is golden ratio
Proof by induction on 𝑘. Consider a node 𝑥 of degree 𝑘 = 𝑛 + 1.
The degree of 𝑦𝑖 is at least 𝑖 − 1 for 1 ≤ 𝑖 ≤ 𝑑 because we merge two
trees when their degree is the same.
POSCAT Seminar 1-69
2 July 2014
yougatup
Fibonacci heap
 Analysis
Let 𝑥 be any node in a Fibonacci heap. Then
size(𝑥) ≥ 𝐹𝑘+2 ≥ ∅ 𝑘
where 𝑘 is the degree of 𝑥, ∅ is golden ratio
Proof by induction on 𝑘. Consider a node 𝑥 of degree 𝑘 = 𝑛 + 1.
Therefore,
size(𝑥) = 1 + size(𝑦0) + size(𝑦1) + size(𝑦2) + … + size(𝑦 𝑘−1)
≥ 1 + 1 + 𝐹2 + 𝐹3 + … + 𝐹𝑘
≥ 𝐹𝑘+2 ≥ ∅ 𝑘 ( also it can be proved by induction )
POSCAT Seminar 1-70
2 July 2014
yougatup
Fibonacci heap
 Analysis
Therefore, 𝑘 ≤ log∅ 𝑠𝑖𝑧𝑒 (𝑥).
∴ 𝑘 ≤ log 𝑛
Therefore, decrease_key takes O(log 𝑛)
POSCAT Seminar 1-71
2 July 2014
yougatup

More Related Content

Similar to Poscat seminar 4

Poscat seminar 3-1
Poscat seminar 3-1Poscat seminar 3-1
Poscat seminar 3-1Hyungyu Shin
 
Pitfalls of Object Oriented Programming
Pitfalls of Object Oriented ProgrammingPitfalls of Object Oriented Programming
Pitfalls of Object Oriented ProgrammingSlide_N
 
Using the search engine as recommendation engine
Using the search engine as recommendation engineUsing the search engine as recommendation engine
Using the search engine as recommendation engineLars Marius Garshol
 
Computer notes - Analysis of Union
Computer notes  - Analysis of Union Computer notes  - Analysis of Union
Computer notes - Analysis of Union ecomputernotes
 

Similar to Poscat seminar 4 (6)

Poscat seminar 8
Poscat seminar 8Poscat seminar 8
Poscat seminar 8
 
Poscat seminar 3-1
Poscat seminar 3-1Poscat seminar 3-1
Poscat seminar 3-1
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
Pitfalls of Object Oriented Programming
Pitfalls of Object Oriented ProgrammingPitfalls of Object Oriented Programming
Pitfalls of Object Oriented Programming
 
Using the search engine as recommendation engine
Using the search engine as recommendation engineUsing the search engine as recommendation engine
Using the search engine as recommendation engine
 
Computer notes - Analysis of Union
Computer notes  - Analysis of Union Computer notes  - Analysis of Union
Computer notes - Analysis of Union
 

More from Hyungyu Shin

More from Hyungyu Shin (9)

Poscat seminar 9
Poscat seminar 9Poscat seminar 9
Poscat seminar 9
 
Poscat seminar 10
Poscat seminar 10Poscat seminar 10
Poscat seminar 10
 
Poscat seminar 11
Poscat seminar 11Poscat seminar 11
Poscat seminar 11
 
Poscat seminar 7
Poscat seminar 7Poscat seminar 7
Poscat seminar 7
 
Poscat seminar 6
Poscat seminar 6Poscat seminar 6
Poscat seminar 6
 
Poscat seminar 5
Poscat seminar 5Poscat seminar 5
Poscat seminar 5
 
Poscat seminar 3-2
Poscat seminar 3-2Poscat seminar 3-2
Poscat seminar 3-2
 
Poscat seminar 2
Poscat seminar 2Poscat seminar 2
Poscat seminar 2
 
Poscat seminar 1
Poscat seminar 1Poscat seminar 1
Poscat seminar 1
 

Recently uploaded

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Poscat seminar 4

  • 1. POSCAT Seminar 4 : Adv. Data Structure yougatup @ POSCAT 1
  • 2. Topic  Topic today − Heap ( Just for your knowledge ) • D-ary heap • Binomial heap • Fibonacci heap − Indexed Tree • Binary Indexed Tree • Fenwick Tree − Implementation of Indexed Tree POSCAT Seminar 1-2 2 July 2014 yougatup
  • 3. Heap  You already know what it is − Definition ?  Amazingly, there is faster data structures − Fibonacci heap 은 이론적으로 Time complexity 가장 적음 − But, 구현해보면 느림  실제로 쓰지는 않습니다 − d-ary heap, Binomial heap, Fibonacci heap  Operations − find_min, delete_min, insert, delete, decrease_key POSCAT Seminar 1-3 2 July 2014 yougatup
  • 4. 𝑑-ary heap  It has child at most 𝑑 − Extended version of binary heap − How it works ? − We use it for Prim algorithm POSCAT Seminar 1-4 2 July 2014 yougatup find_min ? insert ? delete ? decrease_key ? delete_min ? ⋅⋅⋅ at most 𝑑
  • 5. 𝑑-ary heap POSCAT Seminar 1-5 2 July 2014 yougatup ⋅⋅⋅ at most 𝑑find_min : root ! insert : insert to right-most delete : decrease a value to -∞, and delete_min decrease_key : decrease a value, and rearrange heap delete_min : like binary heap   It has child at most 𝑑 − Extended version of binary heap − How it works ? − We use it for Prim algorithm
  • 6. 𝑑-ary heap POSCAT Seminar 1-6 2 July 2014 yougatup ⋅⋅⋅ at most 𝑑find_min : root ! insert : insert to right-most delete : decrease a value to -∞, and delete_min decrease_key : decrease a value, and rearrange heap delete_min : like binary heap  Time complexity ?  It has child at most 𝑑 − Extended version of binary heap − How it works ? − We use it for Prim algorithm
  • 7. 𝑑-ary heap POSCAT Seminar 1-7 2 July 2014 yougatup ⋅⋅⋅ at most 𝑑 Time complexity ? find_min : O(1) insert : O(log 𝑑 𝑛) delete : O(𝑑 log 𝑑 𝑛) decrease_key : O(log 𝑑 𝑛) delete_min : O(𝑑 log 𝑑 𝑛)  It has child at most 𝑑 − Extended version of binary heap − How it works ? − We use it for Prim algorithm
  • 8. Binomial tree  Definition A binomial tree of height 𝑘 (denoted by 𝐵 𝑘) is defined as follows 1. 𝐵0 consists of a single node. 2. 𝐵 𝑘 is formed by joining two 𝐵 𝑘−1 trees, making one’s root child of the other POSCAT Seminar 1-8 2 July 2014 yougatup 𝐵0 𝐵1 𝐵2 𝐵3
  • 9. Binomial tree  Property 1. 𝐵 𝑘 has 2 𝑘 nodes 2. The height of 𝐵 𝑘 is 𝑘 = log |𝐵 𝑘| 3. The root of 𝐵 𝑘 has 𝑘 children Use mathematical induction to prove Therefore, the height and the degree of a node 𝑣 in a binomial tree are both at most logarithmic in the size of the subtree rooted at 𝑣 POSCAT Seminar 1-9 2 July 2014 yougatup
  • 10. Binomial tree  Storing data How can we store 𝑛 ≠ 2 𝑘 nodes using binomial tree ? POSCAT Seminar 1-10 2 July 2014 yougatup
  • 11. Binomial tree  Storing data How can we store 𝑛 ≠ 2 𝑘 nodes using binomial tree ? Think about binary notation of 𝑛 POSCAT Seminar 1-11 2 July 2014 yougatup
  • 12. Binomial tree  Storing data How can we store 𝑛 ≠ 2 𝑘 nodes using binomial tree ? Think about binary notation of 𝑛 We can use binomial tree chain ! We call it as root list POSCAT Seminar 1-12 2 July 2014 yougatup head 10 1 6 12 25 18 8 11 17 27 29 14 38
  • 13. Binomial heap  Definition A binomial heap is a (set of) binomial tree(s) where each node is associated with a key and the heap-order property is preserved. We also have the requirement that for any 𝑖 there is at most one 𝐵𝑖. In other words, it doesn’t contain two 𝐵𝑖 in the binomial heap. POSCAT Seminar 1-13 2 July 2014 yougatup head 10 1 6 12 18 25 8 11 17 27 29 14 38 unique 𝐵2
  • 14. Binomial heap  Operations − find_min, delete_min, insert, delete, decrease_key POSCAT Seminar 1-14 2 July 2014 yougatup
  • 15. Binomial heap  Find_min We need to consider all the root nodes of binomial tree It takes O(log 𝑛). Why ? POSCAT Seminar 1-15 2 July 2014 yougatup head 10 1 6 12 18 25 8 11 17 27 29 14 38
  • 16. Binomial heap  Find_min We need to consider all the root nodes of binomial tree It takes O(log 𝑛). Why ? we have log 𝑛 binomial trees POSCAT Seminar 1-16 2 July 2014 yougatup head 10 1 6 12 18 25 8 11 17 27 29 14 38
  • 17. Binomial heap  Decrease_key Decrease the value and rearrange binomial tree It takes O(log 𝑛) POSCAT Seminar 1-17 2 July 2014 yougatup head 10 1 6 12 18 25 8 11 27 29 14 38 17
  • 18. Binomial heap  Decrease_key Decrease the value and rearrange binomial tree It takes O(log 𝑛) POSCAT Seminar 1-18 2 July 2014 yougatup head 10 1 6 12 18 25 8 11 27 29 14 38 2
  • 19. Binomial heap  Decrease_key Decrease the value and rearrange binomial tree It takes O(log 𝑛) POSCAT Seminar 1-19 2 July 2014 yougatup head 10 1 6 12 18 25 11 27 29 14 38 2 8
  • 20. Binomial heap  Decrease_key Decrease the value and rearrange binomial tree It takes O(log 𝑛) POSCAT Seminar 1-20 2 July 2014 yougatup head 10 1 12 18 25 11 27 29 14 38 8 6 2
  • 21. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees POSCAT Seminar 1-21 2 July 2014 yougatup head 10 12 18 25 11 27 29 14 38 8 6 21 delete it !
  • 22. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees POSCAT Seminar 1-22 2 July 2014 yougatup head 10 12 18 25 11 27 29 14 38 8 6 2 delete it ?
  • 23. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. POSCAT Seminar 1-23 2 July 2014 yougatup head 10 12 18 25 11 27 29 14 38 8 6 2 head’
  • 24. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! How ? POSCAT Seminar 1-24 2 July 2014 yougatup head 10 12 18 25 11 27 29 14 38 8 6 2 head’
  • 25. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! How ? Merging on merge sort ? POSCAT Seminar 1-25 2 July 2014 yougatup head 10 12 18 25 11 27 29 14 38 8 6 2 head’
  • 26. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! POSCAT Seminar 1-26 2 July 2014 yougatup head 10 12 18 25 11 27 29 14 38 8 6 2
  • 27. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! Rearrange the binomial heap POSCAT Seminar 1-27 2 July 2014 yougatup head 10 12 18 25 11 27 29 14 38 8 6 2
  • 28. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! Rearrange the binomial heap POSCAT Seminar 1-28 2 July 2014 yougatup head 18 25 11 27 29 14 38 8 6 210 12
  • 29. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! Rearrange the binomial heap POSCAT Seminar 1-29 2 July 2014 yougatup head 18 25 11 27 29 14 38 8 6 2 12 10
  • 30. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! Rearrange the binomial heap POSCAT Seminar 1-30 2 July 2014 yougatup head 11 27 29 14 38 8 6 218 2512 10
  • 31. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! Rearrange the binomial heap POSCAT Seminar 1-31 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 32. Binomial heap  Delete_min If we delete the root node of binomial tree, it is split into many trees Make another chain ! Then we get another binomial heap. Merge two binomial heaps into one ! Rearrange the binomial heap POSCAT Seminar 1-32 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 33. Binomial heap  Delete_min Time complexity ? POSCAT Seminar 1-33 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 34. Binomial heap  Delete_min Time complexity ? Each node has at most log 𝑛 child. POSCAT Seminar 1-34 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 35. Binomial heap  Delete_min Time complexity ? Each node has at most log 𝑛 child. New binomial heap consists of at most log 𝑛 binomial trees POSCAT Seminar 1-35 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 36. Binomial heap  Delete_min Time complexity ? Each node has at most log 𝑛 child. New binomial heap consists of at most log 𝑛 binomial trees Therefore, Merging and rearrange takes O(log 𝑛). POSCAT Seminar 1-36 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 37. Binomial heap  Insert Insert a new node and rearrange binomial heap O(log 𝑛) POSCAT Seminar 1-37 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 38. Binomial heap  Delete Make it as -∞, and delete_min ! O(log 𝑛) POSCAT Seminar 1-38 2 July 2014 yougatup head 11 27 29 14 38 8 6 2 18 25 12 10
  • 39. Fibonacci heap  Theoretically fastest heap − But it is just theoretical. It is slow if you implement it − We don’t use it  Similar with binomial heap − Relaxed version of binomial heap − Use a lazy update scheme. POSCAT Seminar 1-39 2 July 2014 yougatup
  • 40. Fibonacci heap  Main feature − Individual trees are not necessarily binomial (let me denote it as 𝐵𝑖′) − Allow many copies of 𝐵𝑖′ for the same 𝑖.  Operations − find_min, delete_min, insert, delete, decrease_key POSCAT Seminar 1-40 2 July 2014 yougatup head 10 1 6 12 18 8 11 17 14 38 I allow it because I’m lazy  𝐵0′ 𝐵2′ 𝐵3′ 3 11 27 𝐵2′ 20
  • 41. Fibonacci heap  Find_min via pointer ! POSCAT Seminar 1-41 2 July 2014 yougatup head 10 1 6 12 18 8 11 17 14 38 𝐵0′ 𝐵2′ 𝐵3′ 3 11 27 𝐵2′ 20
  • 42. Fibonacci heap  Insert Create a node. No update because I’m lazy  Give $1 to inserted node. I’ll explain the meaning of ‘coin’ later. POSCAT Seminar 1-42 2 July 2014 yougatup 10 1 6 12 18 8 11 17 14 38 𝐵0′ 𝐵2′ 𝐵3′ 3 11 27 𝐵2′ 20 head 7 𝐵0′
  • 43. Fibonacci heap  Delete -∞, delete_min POSCAT Seminar 1-43 2 July 2014 yougatup 10 1 6 12 18 8 11 17 14 38 𝐵0′ 𝐵2′ 𝐵3′ 3 11 27 𝐵2′ 20 head 7 𝐵0′
  • 44. Fibonacci heap  Delete_min Delete min and rearrange Fibonacci heap. Time complexity will proportional to the number of trees. Is it fast ? POSCAT Seminar 1-44 2 July 2014 yougatup 10 1 6 12 18 8 11 17 14 38 𝐵0′ 𝐵2′ 𝐵3′ 3 11 27 𝐵2′ 20 head 7 𝐵0′
  • 45. Fibonacci heap  Delete_min Delete min and rearrange Fibonacci heap. Time complexity will proportional to the number of trees. Is it fast ? POSCAT Seminar 1-45 2 July 2014 yougatup 10 612 18 8 11 17 14 38 𝐵0′ 𝐵3′ 3 11 27 𝐵2′ 20 head 7 𝐵0′ 𝐵0′ 𝐵1′
  • 46. Fibonacci heap  Delete_min Delete min and rearrange Fibonacci heap. Time complexity will proportional to the number of trees. Is it fast ? POSCAT Seminar 1-46 2 July 2014 yougatup 10 6 12 18 8 11 17 14 38 𝐵1′ 𝐵3′ 3 11 27 𝐵2′ 20 head 7 𝐵0′ 𝐵1′
  • 47. Fibonacci heap  Delete_min Delete min and rearrange Fibonacci heap. Time complexity will proportional to the number of trees. Is it fast ? POSCAT Seminar 1-47 2 July 2014 yougatup 10 6 12 18 8 11 17 14 38 𝐵2′ 𝐵3′ 3 11 27 𝐵2′ 20 head 7 𝐵0′
  • 48. Fibonacci heap  Delete_min Delete min and rearrange Fibonacci heap. Time complexity will proportional to the number of trees. Is it fast ? POSCAT Seminar 1-48 2 July 2014 yougatup 10 6 12 18 8 11 17 14 38 𝐵3′ 3 11 27 20 head 7 𝐵0′ 𝐵3′
  • 49. Fibonacci heap  Delete_min Delete min and rearrange Fibonacci heap. Time complexity will proportional to the number of trees. Is it fast ? POSCAT Seminar 1-49 2 July 2014 yougatup 106 12 18 8 11 17 14 38 3 11 27 20 head 7 𝐵0′ 𝐵4′
  • 50. Fibonacci heap  Delete_min Delete min and rearrange Fibonacci heap. Time complexity will proportional to the number of trees. Is it fast ?  we’ll analyze it soon. POSCAT Seminar 1-50 2 July 2014 yougatup 106 12 18 8 11 17 14 38 3 11 27 20 head 7 𝐵0′ 𝐵4′
  • 51. Fibonacci heap  Decrease_key Decrease the value of the element. If the heap-order property is violated, cut the link between the node and its parent. ( It may produce a result which is not a binomial tree) Not just cut, we use “Cascading cut” POSCAT Seminar 1-51 2 July 2014 yougatup 106 12 18 8 11 17 14 38 3 11 27 20 head 7 𝐵0′ 𝐵4′
  • 52. Fibonacci heap  Decrease_key Decrease the value of the element. If the heap-order property is violated, cut the link between the node and its parent. ( It may produce a result which is not a binomial tree) Not just cut, we use “Cascading cut” POSCAT Seminar 1-52 2 July 2014 yougatup 6 12 18 8 11 17 14 38 3 11 27 20 head 7 𝐵0′ 𝐵4′ 10 decrease it to 4
  • 53. Fibonacci heap  Decrease_key Decrease the value of the element. If the heap-order property is violated, cut the link between the node and its parent. ( It may produce a result which is not a binomial tree) Not just cut, we use “Cascading cut” POSCAT Seminar 1-53 2 July 2014 yougatup 6 12 18 8 11 17 14 38 3 11 27 20 head 7 𝐵4′ 4 decrease it to 4  heap-order is violated 𝐵0′
  • 54. Fibonacci heap  Decrease_key Decrease the value of the element. If the heap-order property is violated, cut the link between the node and its parent. ( It may produce a result which is not a binomial tree) Not just cut, we use “Cascading cut” POSCAT Seminar 1-54 2 July 2014 yougatup 6 12 18 8 11 17 14 38 3 11 27 20 head 7 𝐵4′ 4 decrease it to 4  heap-order is violated  Cut ! 𝐵0′𝐵0′
  • 55. Fibonacci heap  Decrease_key Decrease the value of the element. If the heap-order property is violated, cut the link between the node and its parent. ( It may produce a result which is not a binomial tree) Not just cut, we use “Cascading cut” POSCAT Seminar 1-55 2 July 2014 yougatup 6 12 18 8 11 17 14 38 3 11 27 20 head 7 𝐵4′ 4 decrease it to 4  heap-order is violated  Cut ! 𝐵0′ 𝐵0′ 𝐵1′𝐵0′
  • 56. Cascading cut  Definition 1. Whenever a node is being cut, mark the parent of the cut node in the original tree, and • Pay $1 for the cut • Store $2 in the parent of the cut node • Store $1 in the new root (cut node). POSCAT Seminar 1-56 2 July 2014 yougatup 6 12 18 8 11 17 14 38 11 27 20 head 7 𝐵0′ 𝐵4′ decrease it to 4  heap-order is violated  Cut ! 𝐵0′ 𝐵0′ 𝐵1′ 34 marked!
  • 57. Cascading cut  Definition 2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already marked node), by that time node 𝑣 will have accumulated $ 4; recursively cut that node from its parent, marking again the parent of 𝑣 and using $4 to pay for the operation before. $1 for the cut, $2 to its parent, $1 to the new root POSCAT Seminar 1-57 2 July 2014 yougatup 6 12 18 8 11 17 14 38 27 20 head 7 𝐵4′ 𝐵0′ 𝐵0′ 𝐵1′ 4 3 11 decrease it to 2 ! 𝐵0′
  • 58. Cascading cut  Definition 2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already marked node), by that time node 𝑣 will have accumulated $ 4; recursively cut that node from its parent, marking again the parent of 𝑣 and using $4 to pay for the operation before. $1 for the cut, $2 to its parent, $1 to the new root POSCAT Seminar 1-58 2 July 2014 yougatup 6 12 18 8 11 17 14 38 27 20 head 7 𝐵4′ 𝐵0′ 𝐵0′ 𝐵1′ 4 3 2 decrease it to 2 ! 𝐵0′
  • 59. Cascading cut  Definition 2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already marked node), by that time node 𝑣 will have accumulated $ 4; recursively cut that node from its parent, marking again the parent of 𝑣 and using $4 to pay for the operation before. $1 for the cut, $2 to its parent, $1 to the new root POSCAT Seminar 1-59 2 July 2014 yougatup 6 12 18 8 11 17 14 38 27 20 head 7 𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′ 4 32 decrease it to 2 ! 𝐵1′ 𝐵4′ marked twice. It has $4
  • 60. Cascading cut  Definition 2. When a 2nd child of a node 𝑣 is lost (cutting a child of an already marked node), by that time node 𝑣 will have accumulated $ 4; recursively cut that node from its parent, marking again the parent of 𝑣 and using $4 to pay for the operation before. $1 for the cut, $2 to its parent, $1 to the new root POSCAT Seminar 1-60 2 July 2014 yougatup 612 18 8 11 17 14 38 27 20 head 7 𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′ 4 2 𝐵1′ 𝐵3′𝐵0′ 3 𝐵0′
  • 61. Cascading cut  Time complexity of cascading cut Each node has the coin they already have. Each cut requires only $4, and decrease_key takes overall still amortized time O(1) ( Overall cost / The number of operation ) POSCAT Seminar 1-61 2 July 2014 yougatup 612 18 8 11 17 14 38 27 20 head 7 𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′ 4 2 𝐵1′ 𝐵3′𝐵0′ 3 𝐵0′
  • 62. Fibonacci heap  Decrease_key Decrease the value of the element. If the heap-order property is violated, cut the link between the node and its parent. ( It may produce a result which is not a binomial tree) Not just cut, we use “Cascading cut” POSCAT Seminar 1-62 2 July 2014 yougatup 612 18 8 11 17 14 38 27 20 head 7 𝐵0′ 𝐵0′ 𝐵0′ 𝐵1′ 4 2 𝐵1′ 𝐵3′𝐵0′ 3 𝐵0′
  • 63. Fibonacci heap  Decrease_key Decrease the value of the element. If the heap-order property is violated, cut the link between the node and its parent. ( It may produce a result which is not a binomial tree) Not just cut, we use “Cascading cut” POSCAT Seminar 1-63 2 July 2014 yougatup 6 12 18 8 11 17 14 38 2720 head 7 𝐵0′ 4 3 Rearrange ! 𝐵4′ 2
  • 64. Fibonacci heap  Analysis Cascading cut takes O(1) How much is merging cost ? POSCAT Seminar 1-64 2 July 2014 yougatup 6 12 18 8 11 17 14 38 2720 head 7 𝐵0′ 4 3 Rearrange ! 𝐵4′ 2
  • 65. Fibonacci heap  Analysis Cascading cut takes O(1) How much is merging cost ? O(1) because of the coin POSCAT Seminar 1-65 2 July 2014 yougatup 6 12 18 8 11 17 14 38 2720 head 7 𝐵0′ 4 3 Rearrange ! 𝐵4′ 2
  • 66. Fibonacci heap  Analysis Cascading cut takes O(1) After cut, we get (# of child) additional trees How much is merging cost per child ? O(1) because of the coin How many child does a node has ? POSCAT Seminar 1-66 2 July 2014 yougatup 6 12 18 8 11 17 14 38 2720 head 7 𝐵0′ 4 3 Rearrange ! 𝐵4′ 2
  • 67. Fibonacci heap  Analysis Cascading cut takes O(1) After cut, we get (# of child) additional trees How much is merging cost per child ? O(1) because of the coin How many child does a node has ?  Degree of a node POSCAT Seminar 1-67 2 July 2014 yougatup 6 12 18 8 11 17 14 38 2720 head 7 𝐵0′ 4 3 Rearrange ! 𝐵4′ 2
  • 68. Fibonacci heap  Analysis Let 𝑥 be any node in a Fibonacci heap. Then size(𝑥) ≥ 𝐹𝑘+2 ≥ ∅ 𝑘 where 𝑘 is the degree of 𝑥, ∅ is golden ratio Proof by induction on 𝑘. Consider a node 𝑥 of degree 𝑘 = 𝑛 + 1. Let 𝑦𝑖 be the 𝑘 children of 𝑥, from oldest to youngest (i.e. 𝑦0 was made children of 𝑥 before 𝑦1, and so on). Then size(𝑥) = 1 + size(𝑦0) + size(𝑦1) + … + size(𝑦 𝑘−1) POSCAT Seminar 1-68 2 July 2014 yougatup
  • 69. Fibonacci heap  Analysis Let 𝑥 be any node in a Fibonacci heap. Then size(𝑥) ≥ 𝐹𝑘+2 ≥ ∅ 𝑘 where 𝑘 is the degree of 𝑥, ∅ is golden ratio Proof by induction on 𝑘. Consider a node 𝑥 of degree 𝑘 = 𝑛 + 1. The degree of 𝑦𝑖 is at least 𝑖 − 1 for 1 ≤ 𝑖 ≤ 𝑑 because we merge two trees when their degree is the same. POSCAT Seminar 1-69 2 July 2014 yougatup
  • 70. Fibonacci heap  Analysis Let 𝑥 be any node in a Fibonacci heap. Then size(𝑥) ≥ 𝐹𝑘+2 ≥ ∅ 𝑘 where 𝑘 is the degree of 𝑥, ∅ is golden ratio Proof by induction on 𝑘. Consider a node 𝑥 of degree 𝑘 = 𝑛 + 1. Therefore, size(𝑥) = 1 + size(𝑦0) + size(𝑦1) + size(𝑦2) + … + size(𝑦 𝑘−1) ≥ 1 + 1 + 𝐹2 + 𝐹3 + … + 𝐹𝑘 ≥ 𝐹𝑘+2 ≥ ∅ 𝑘 ( also it can be proved by induction ) POSCAT Seminar 1-70 2 July 2014 yougatup
  • 71. Fibonacci heap  Analysis Therefore, 𝑘 ≤ log∅ 𝑠𝑖𝑧𝑒 (𝑥). ∴ 𝑘 ≤ log 𝑛 Therefore, decrease_key takes O(log 𝑛) POSCAT Seminar 1-71 2 July 2014 yougatup