File Organization & Processing
Instructor
Maher Hadiji [email protected]2016-2017 Computer Science Department
College of Computer at Al-Lith
Chapter 2:Advanced Heap Structures (Fibonacci Heaps)
Binomial Trees
• A binomial heap is a collection of binomial trees.
• The binomial tree Bk is an ordered tree defined recursively
Bo Consists of a single node ..
.
Bk Consists of two binominal trees Bk-1 linked together. Root of one is the leftmost child of the root of the other.
Binomial Trees
B
k-1B
k-1B
kBinomial Trees
B0 B1 B2 B3
B1 B1 B2
B1
B4
B0
B3 B2
B1
B0
Binomial Trees
B
k-2B
k-1B2 B1 Bo
B
kProperties of Binomial Trees
1. There are 2k nodes, 2. The height of tree is k,
3. There are exactly nodes at depth i for i = 0,1,..,k
4. The root has degree k > degree of any other node if the children of the root are numbered from left to right as k-1, k-2,...,0; child i is the root of a subtree Bi.
Only node with greater degree in Bk than those in Bk-1 is the root, The root of Bk has one more child than the root of Bk-1,
Degree of root Bk=Degree of Bk-1+1=(k-1)+1=k
k
i
Binomial Heaps
A BINOMIAL HEAP H is a set of BINOMIAL TREES that satisfies the following
“Binomial Heap Properties”
1. Each binomial tree in H is HEAP-ORDERED
• the key of a node is ≥ the key of the parent
• Root of each binomial tree in H contains the smallest key in that tree.
Fibonacci Heaps
• A Fibonacci heap is a collection of heap-ordered trees.
• Each tree is rooted but unordered.
• Each node x has pointed p[x] to its parent & child [x] to one of its children
• Children are linked together in a doubly-linked circular list.
• Doubly-linked circular list: Removing a node is O(1) and splicing two lists is O(1)
• Each node also has degree [x] indicating the number of children of x
• Each node also has mark [x], a boolean field indicating whether x has lost a child since the last time x was made the child of another node
• The entire heap is accessed by a pointer min [H] which points to the minimum-
key root
Fibonacci Heaps:Example
Nodes in Fibonacci Heaps
parent
child degree
key
mark
right left
7 23
30 17
35
26 46
24
39
18 52 41 3
44
Fibonacci Heaps: Structure
• Fibonacci heap.
• Set of heap-ordered trees.
• Maintain pointer to minimum element.
• Set of marked nodes.
roots heap-ordered tree
each parent greater than its children
7 23
30 17
35
26 46
24
18 52 41 3
44
Fibonacci Heaps: Structure
• Fibonacci heap.
• Set of heap-ordered trees.
• Maintain pointer to minimum element.
• Set of marked nodes.
min find-min takes O(1) time
7 23
30 17
35
26 46
24
Heap H
39
18 52 41 3
44
Fibonacci Heaps: Structure
• Fibonacci heap.
• Set of heap-ordered trees.
• Maintain pointer to minimum element.
• Set of marked nodes.
use to keep heaps flat (stay tuned) min
marked
Fibonacci Heaps: Notation
• Notation.
• n = number of nodes in heap.
• rank(x) = number of children of node x.
• rank(H) = max rank of any node in heap H.
• trees(H) = number of trees in heap H.
• marks(H) = number of marked nodes in heap H.
7 23
30 17
35
26 46
24
18 52 41 3
44 rank = 3
min
Heap H
trees(H) = 5 marks(H) = 3
marked
n = 14
Fibonacci Heaps: Insert
• Insert.
• Create a new singleton tree.
• Add to root list; update min pointer (if necessary).
7 23
30 17
35
26 46
24
39
41
18 52
3
44 21
insert 21
min
Fibonacci Heaps: Insert
• Insert.
• Create a new singleton tree.
• Add to root list; update min pointer (if necessary).
39
41 7
23
18 52
3
30 17
35
26 46
24
44 21
min
Heap H
insert 21
Linking Operation
• Linking operation. Make larger root be a child of smaller root.
39
41
18 52
3
77 44
56 24
15
tree T1 tree T2
39
41
18 52
3
44
77
56 24
15
tree T'
smaller root
larger root still heap-ordered
19
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41
18 52
3
44 17
23
30 7
35
26 46
24
min
18
39
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 17
23 18 52
30 7
35
26 46
24
44
min
18
39
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 17
23 18 52
30 7
35
26 46
24
44
min current
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 17
23 18 52
30 7
26 46
24
44
0 1 2 3
current
min
rank
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 17
23 18 52
30 7
35
26 46
24
44
0 1 2 3
min current
rank
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 17
23 18 52
30 7
26 46
24
44
0 1 2 3
min
current
rank
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 17
23 18 52
30 7
35
26 46
24
44
0 1 2 3
min
current
rank
link 23 into 17
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 17
23
18 52
30 7
26 46
24
44
0 1 2 3
min
current
rank
link 17 into 7
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 7
30
18 52
17
35
26 46
24
44
0 1 2 3
23
current
min rank
link 24 into 7
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 7
30
18 52
23 17
26 46
24 44
0 1 2 3
min
current
rank
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 7
30
18 52
23 17
35
26 46
24 44
0 1 2 3
min
current
rank
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 7
30
18 52
23 17
26 46
24 44
0 1 2 3
min
current
rank
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
39
41 7
30
18 52
23 17
35
26 46
24 44
0 1 2 3
min
current
rank
link 41 into 18
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
41 39 7
30
18 52
23 17
26 46
24
44
0 1 2 3
min
current
rank
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
7
30
52
23 17
35
26 46
24
0 1 2 3
min rank
41 39
18
44 current
Fibonacci Heaps: Delete Min
• Delete min.
• Delete min; meld its children into root list; update min.
• Consolidate trees so that no two roots have same rank.
7
30
52
23 17
26 46
24
min
41 39
18
44
stop
Fibonacci Heaps: Decrease Key
• Intuition for deceasing the key of node x .
• If heap-order is not violated, just decrease the key of x.
• Otherwise, cut tree rooted at x and meld into root list.
• To keep trees flat: as soon as a node has its second child cut, cut it off and meld into root list (and unmark it).
24
46
17
30
23 7
88 26
21
52
39 18
41 38
72 35
min
marked node:
one child already cut
Fibonacci Heaps: Decrease Key
• Case 1. [heap order not violated]
• Decrease key of x.
• Change heap min pointer (if necessary).
24
46
17
30
23 7
88 26
21
52
39 18
41 38
72 29
35
min
x
decrease-key of x from 46 to 29
39
Fibonacci Heaps: Decrease Key
• Case 1. [heap order not violated]
• Decrease key of x.
• Change heap min pointer (if necessary).
24
29
17
30
23 7
88 26
21
52
39 18
41 38
72 35
min
x
decrease-key of x from 46 to 29
Fibonacci Heaps: Decrease Key
• Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of xis unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
29
17
30
23 7
88 26
21
52
39 18
41 38
72 15
35
min
decrease-key of x from 29 to 15
p
x
Fibonacci Heaps: Decrease Key
• Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24
15
17
30
23 7
88 26
21
52
39 18
41 38
72 35
min
decrease-key of x from 29 to 15
p
x
Fibonacci Heaps: Decrease Key
• Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p,meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24 17
30
23 7
88 26
21
52
39 18
41 38
35
min
decrease-key of x from 29 to 15
p
15
72
x
Fibonacci Heaps: Decrease Key
• Case 2a. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of xis unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24 17
30
23 7
88 26
21
52
39 18
41 38
35
min
decrease-key of x from 29 to 15
p
15
72
x
mark parent
24
35
Fibonacci Heaps: Decrease Key
• Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of xis unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24 15
17
30
23 7
88 26
21
52
39 18
41 38
72 24
5
min
x
p
decrease-key of x from 35 to 5
5
Fibonacci Heaps: Decrease Key
• Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
24 15
17
30
23 7
88 26
21
52
39 18
41 38
72 24
min
x
p
decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key
• Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p,meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).
24 17
30
23 7
26
21
52
39 18
41 38
24 5
88 15
72
decrease-key of x from 35 to 5
x
p min
Fibonacci Heaps: Decrease Key
• Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).
24 17
30
23 7
26
21
52
39 18
41 38
24 5
88 15
72
decrease-key of x from 35 to 5
x
p
second child cut min
Fibonacci Heaps: Decrease Key
• Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of xis unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).
24 26
17
30
23 7
21
52
39 18
41 38
88 24
5 15
72
decrease-key of x from 35 to 5
x p
min
Fibonacci Heaps: Decrease Key
• Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of xis unmarked (hasn't yet lost a child), mark it Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child).
24 26
17
30
23 7
21
52
39 18
41 38
88 24
5 15
72
decrease-key of x from 35 to 5
x p
p'
second child cut min
Fibonacci Heaps: Decrease Key
• Case 2b. [heap order violated]
• Decrease key of x.
• Cut tree rooted at x, meld into root list, and unmark.
• If parent p of xis unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
26
17
30
23 7
21
52
39 18
41 38
88 5
15 24
72
decrease-key of x from 35 to 5
x p p'
min
don't mark parent if it's a root
p''
Fibonacci Heaps: Union
• Union. Combine two Fibonacci heaps.
• Representation. Root lists are circular, doubly linked lists.
41 7
17
18 52
3
30 23
35
26 46
24
44
21
min min
Heap H' Heap H''
Fibonacci Heaps: Union
• Union. Combine two Fibonacci heaps.
• Representation. Root lists are circular, doubly linked lists.
39
41 7
17
18 52
3
30 23
35
26 46
24
44
21
min
Heap H