Professional Documents
Culture Documents
Motivation
Main operations
Dynamic
D
i insert
i
t
specification of a priority level (0-high, 1,2.. Low)
Finds the current minimum element (read: highest priority) in
the queue, deletes it from the queue, and returns it
10
19
3
13
8
22
11
insert()
deleteMin()
2
Can we build a data structure better suited to store and retrieve priorities?
Simple Implementations
10
10
O(n) insert
O(1) deleteMin
d l t Mi
Ordered array
O(1) insert
O(n) deleteMin
10
O(lg n + n) insert
O(n) deleteMin
Balanced BST
Bi
Binary
Heap
H
A priority queue data structure
Binary Heap
Structure property
Heap-order
Heap
order property
Structure Property
Structure property
Array representation:
Heap-order Property
10
Minimum
element
11
Implementing Complete
Binary Trees as Arrays
i
2i
2i + 1
i/2
Cpt S 223. School of EECS, WSU
12
Fix heap
p after
deleteMin
Cpt S 223. School of EECS, WSU
13
Heap Insert
14
Percolating Up
14 hole
15
Percolating Up
Insert 14:
14
14 hole
16
Percolating Up
Insert 14:
14
14 hole
(2)
14 vs. 21
14
17
Percolating Up
Insert 14:
14 hole
(2)
14 vs. 21
14
(3)
14 vs. 13
Path of p
percolation up
p
18
19
20
Heap DeleteMin
21
Percolating down
Make this
position
empty
22
Percolating down
Make this
position
empty
Is 31 > min(14,16)?
Yes - swap 31 with min(14,16)
23
Percolating down
31
Is 31 > min(19,21)?
Yes - swap 31 with min(19,21)
24
Percolating down
31
31
Is 31 > min(19,21)?
Yes - swap 31 with min(19,21)
Is 31 > min(65,26)?
Yes - swap 31 with min(65,26)
Percolating down
25
Percolating down
31
Percolating down
26
Percolating down
31
27
Heap DeleteMin:
Implementation
O(log N) time
Cpt S 223. School of EECS, WSU
28
Heap DeleteMin:
Implementation
Percolate
down
Left child
Right child
Pick child to
swap with
29
decreaseKey(p,v)
increaseKey(p,v)
remove(p)
(p)
First, decreaseKey(p,-)
Then, deleteMin
E.g., abort/cancel a job
Cpt S 223. School of EECS, WSU
O(lg n)
30
31
Building a Heap
Perform N inserts
O(N log2 N) worst-case
32
BuildHeap Example
I
Input:
{ 150,
150 80
80, 40
40, 10
10, 70
70, 110
110, 30
30, 120
120, 140
140, 60
60, 50
50, 130
130, 100
100, 20
20, 90 }
33
BuildHeap Example
Nothing
to do
Swap
with left
child
34
BuildHeap Example
Nothing
to do
Swap
with right
child
35
BuildHeap Example
Swap
p with
right child
& then with 60
Nothing
to do
36
BuildHeap Example
Swap path
Final Heap
Dotted lines show p
path of p
percolating
g down
Cpt S 223. School of EECS, WSU
37
BuildHeap Implementation
Start with
lowest,
rightmost
i
internal
l node
d
Cpt S 223. School of EECS, WSU
38
BuildHeap() : Run-time
Analysis
Run-time = ?
Theorem 6.1
Since h=lg
Si
h l N, then
th sum off h
heights
i ht is
i O(N)
Will be slightly better in practice
Implication: Each insertion costs O(1) amortized time
Cpt S 223. School of EECS, WSU
39
40
buildHeap
p insert: O(N)
( ) for N inserts
deleteMin: O(lg N)
decreaseKey: O(lg N)
increaseKey: O(lg N)
remove: O(lg N)
Cpt S 223. School of EECS, WSU
41
Applications
Graph algorithms
Event simulation
42
An Application:
The Selection Problem
A better algorithm:
43
1.
2.
3.
buildHeap(n)
b
ildHeap(n)
Perform k deleteMin() operations
Report the kth deleteMin output
==>
> O(n)
==> O(k log n)
==> O(1)
44
Binomial Heaps
dH
d-Heaps
Leftist Heaps
Skew Heaps
Fibonacci Heaps
Cpt S 223. School of EECS, WSU
45
DeleteMin
Merge (=H1+H2)
Binary heap
O(log n) worst-case
O(1) amortized for
buildHeap
O(log n)
O(n)
Leftist Heap
O(log n)
O(log n)
O(log n)
Skew Heap
O(log n)
O(log n)
O(log n)
Binomial
Bi
i l
Heap
O(l n))
O(log
O(l n))
O(log
Fibonacci Heap
O(1)
O(log n)
O(1)
46
Push,, top,
p, pop,
p p,
empty, clear
#include <priority_queue>
int main ()
{
priority_queue<int> Q;
Q.push (10);
cout << Q.top
Q top ();
Q.pop ();
}
Calls DeleteMax()
47
Binomial Heaps
48
Binomial Heap
49
It has 2k nodes
k
The number of nodes at depth d = ( d )
d=0
d
0
d=1
d=2
d=3
#nodes:
B3:
( 03 )
( 13 )
( 23 )
Cpt S 223. School of EECS, WSU
( 33 )
50
We know that:
i) A binomial heap should be a forest of binomial
trees
ii) Each binomial tree has power of 2 elements
S h
So
how many binomial
bi
i l trees do
d we need?
d?
B4 B3 B2 B1 B0
n = 31 = (1 1 1 1 1)2
51
A Binomial
Bi
i l Heap
H
w// n=31
31 nodes
d
B4 B3 B2 B1 B0
Bi == Bi-1 + Bi-1
Fores
st of binomial trees {B0, B1, B2, B3, B4 }
n = 31 = (1 1 1 1 1)2
B0
B1
B3
B2
52
Have one binomial tree for each power of two with co-efficient
of 1
Eg., n=10
10 ==>
> (1010)2 ==>
> forest contains {B3, B1}
53
Property
54
55
56
Key Properties
57
An Implementation
p
of a Binomial Heap
p
Maintain a linked list of
tree pointers (for the forest)
Example: n=13 == (1101)2
B7
B6
B5
B4
B3
B2
B1
B0
Analogous
g
to a bit-based representation
p
of a
binary number n
Cpt S 223. School of EECS, WSU
58
x <= DeleteMin()
Insert(x)
Merge(H1, H2)
59
DeleteMin()
60
B2
B3
B0 B1
B2
61
=>
> Insert (H
(H,x)
x) == Merge(H, {x})
62
Merge(H1,H2)
Assume n = n1 + n2
Logic:
63
Merge: Example
B0
13
B1
B2
64
B3:
B2:
65
Merge(H1,H
H2) example
carryover
13
14
26
16
26
66
How to Merge
g more than two
binomial trees of the same height?
14
26
16
26
67
Input:
+
Merge(H1,H2) : Example
Output:
log(max{n1,n
n2}) = O(log n) comparisons
Cpt S 223. School of EECS, WSU
68
Run-time Complexities
Corollary:
Proof Hint:
unaffected
affected
10010111
1
-------------10011000
For each insertion, if i is the least significant bit position with a 0, then
number of comparisons required to do the next insert is i+1
If you count the #bit flips for each insert, going from insert of the first
element to the insert of the last (nth) element, then
=>
>
amortized run-time
run time of O(1) per insert
Cpt S 223. School of EECS, WSU
69
Insert
DeleteMin, FindMin
O(lg n) worst-case
O(1) amortized time if insertion is done in an
uninterrupted sequence (i.e., without being
intervened by deleteMins)
O(lg n) worst-case
Merge
O(lg n) worst-case
70
DeleteMin
Merge (=H1+H2)
Binary heap
O(log n) worst-case
O(1) amortized for
buildHeap
O(log n)
O(n)
Leftist Heap
O(log n)
O(log n)
O(log n)
Skew Heap
O(log n)
O(log n)
O(log n)
Binomial
Bi
i l
Heap
O(l n))
O(log
O(l n))
O(log
Fibonacci Heap
O(1)
O(log n)
O(1)
71
Summary
insert deleteMin,
insert,
deleteMin merge
72