Lecture 24 - McGill University
Transcript of Lecture 24 - McGill University
1
COMP 250
Lecture 24
heaps 3
Nov. 4, 2016
RECALL: min Heap (definition)
2
Complete binary tree with (unique) comparable elements, such that each nodeβs element is less than its childrenβs element(s).
l uf
e
a
b
k
m
Heap index relations
3
l uf
e
a
b
k
m
1
2 3
4 5 6 7
8
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Not used
parent = child / 2left = 2*parentright = 2*parent + 1
Given a list with size elements:
buildHeap(list){ create new heap array // length > list.sizefor (k = 0; k < list.size; k++)
add( list[k] ) // add to heap[ ]}
RECALL: How to build a heap
4
5
e ga
c
f
m
d
j
jd
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
ddd
2πππ£ππ β€ π < 2πππ£ππ +1
πππ£ππ β€ πππ2 π < πππ£ππ + 1
level0
1
2
3
Thus, πππ£ππ = πππππ( πππ2 π )
jd
Worse case of buildHeap
= π=1π πππ£ππ ππ ππππ π
= π=1π πππππ( πππ2 π )
π‘ π = π=1π ππ’ππππ ππ π π€πππ πππ ππππ π
6
πππ2 π
π π
π‘ π = π=1π πππππ( πππ2 π )
Area under the dashed curve is the totalnumber of swaps (worst case) of buildHeap.
0
1
2
8
4
0
πππ2 π
7
π π0 1000 2000 3000 4000 5000
12
8
4
0
πππ2 π
1
2π πππ2π β€ π‘ π β€ π πππ2π
8
Thus, worst case: buildHeap is π π πππ2 π
This is a tight π( ) bound.
Today, I will show you a π(π) algorithm.
9
Given a list with size elements:
buildHeapFast(list){ copy list into a heap arrayfor (k = size/2; k >= 1; k--)
downHeap( k, size ) }
10
1 2 3 4 5 6
----------------------
w x t p r f
r fp
x
w
t
1
2 3
4 5 6
11
1 2 3 4 5 6
----------------------
w x t p r f
r fp
x
w
t
1
2 3
4 5 6
downHeap( 3, 6 )
12
1 2 3 4 5 6
----------------------
w x f p r t
r tp
x
w
f
1
2 3
4 5 6
downHeap( 3, 6 )
13
1 2 3 4 5 6
----------------------
w x f p r t
r tp
x
w
f
1
2 3
4 5 6
downHeap( 2, 6 )
14
1 2 3 4 5 6
----------------------
w p f x r t
r tx
p
w
f
1
2 3
4 5 6
15
1 2 3 4 5 6
----------------------
w p f x r t
r tx
p
w
f
1
2 3
4 5 6
downHeap( 1, 6 )
16
1 2 3 4 5 6
----------------------
f p w x r t
r tx
p
f
w
1
2 3
4 5 6
17
1 2 3 4 5 6
----------------------
f p t x r w
r wx
p
f
t
1
2 3
4 5 6
18
buildHeapFast(list){ copy list into a heap arrayfor (k = size/2; k >= 1; k--)
downHeap( k, size ) }
Claim: this algorithm is O(n).
Intuition for why this algorithm is so fast?
19
buildheap algorithms
20
last lecture
upHeap based downHeap based
today
We tends to draw binary trees like this:
But the number of nodes doubles at each level.So we should draw trees like this:
height h
height h
21
buildheap algorithms
22
last lecture
Most nodes swap htimes in worst case.
today
Few nodes swap h times in worst case.
height h
How to show buildHeapFast is π(π) ?
π‘ π = π=1π βπππβπ‘ ππ ππππ π
Worst case number of swaps needed to add node π is the the height of that node.
(Recall the height of a node is the length of the longest path from that node to a leaf.)
23
24
e ga
c
f
m
d
j
jd
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
ddd
level
0
1
2
3jd
height
3
2
1
0
Worse case of buildHeapFast ?
How many elements at πππ£ππ π ? (π β 0, . . ., β)
What is the height of each πππ£ππ π node?
25
Worse case of buildHeapFast ?
π‘ π = π=1π βπππβπ‘ ππ ππππ π
πππ£ππ π has 2π elements, π β 0, . . ., β
πππ£ππ π nodes have height β β π.
= ?26
Worse case of buildHeapFast ?
π‘ π = π=1π βπππβπ‘ ππ ππππ π
πππ£ππ π has 2π elements, π β 0, . . ., β
πππ£ππ π nodes have height β β π.
= π=0β β β π 2π
27
Easy Difficult
(number of nodes)
28
(sum of nodedepths)
29
I have removed the next two slides which derived a closed for expression for the second summation (the difficult one). Please see lecture notes for a slightly different derivation, which is simpler.
30
(See lecture notes)
Summary: buildheap algorithms
31
last lecture
π π πππ2 π
today
π(π)
height h