Professional Documents
Culture Documents
80 60
50 40
60 40 80 40 45 42 32 35
A Heap Not a Heap A Heap
Insertion into a HEAP :
2 45 3 70
4 40 5 35 6 50
1 80
2 45 3 70
4 40 5 35 6 50 7 90
STEP 2 : Check whether the insertion of the content in this
child location creates a HEAP
Latest child 7 3 70
Corresponding parent 7/2 = 3
Corresponding local heap structure
6 50 7 90
Ch1 2 * Parent 6
Ch2 Ch1 + 1 7
This does not make a HEAP
3 90
Hence the content of parent_node (3)
should be swapped with the content of
latest_child_node (7) 6 50 7 70
This implies the new content is moved to node 3
2 45 3 90 2 45 3 80
Ch2 x
This is INVALID 4 40 5 35 6 50 7 70
2 45 3 80
4 40 5 35 6 50 7 70
:
: Latest Location
.......
: N
ITEM
N-1
{
latest_location N
A(latest_location) ITEM
new_child latest_location
Back : corresponding_ parent new_child/2
if corresponding_parent < 1 then { Insertion is Completed
Terminate }
else
if( A(corresponding_parent) < ITEM
then { A(new_child) A(corresponding_parent}
A(corresponding_parent) ITEM
new_child corresponding_parent
go Back }
else { ITEM is settled
Terminate } }
1 90
2 45 3 80
4 40 5 35 6 50 7 70
2 45 3 80 2 45 3 98
4 40 5 35 6 50 7 98 4 40 5 35 6 50 7 80
1 98
Worst Case
Insertion 2 45 3 90
4 40 5 35 6 50 7 80
1 90 1 90
2 45 3 80 2 45 3 85
4 40 5 35 6 50 7 85 4 40 5 35 6 50 7 80
(1)
Worst Case Time Complexity :
The latest location will be in the most exterior level
say (l)
l = log 2 (N + 1) – 1 (1)
l log 2 (N ) O(log 2 (N ) ) n
Where N is the total number of vertices
i.e. N indicates the position of the latest location
O(log 2 (N )
How to Profile for Average Computing Time ?
Create a heap of (n-1) elements
x x x t max
x x x x x x x x
x 0 0
t avg 0 0
x 0 0 0 0
0 0 0 0
o o o o o o o o o o o o o o t min
n
Draw smooth curves to pass through the corresponding
points
Find an expression for t avg - the best fit expression
Heap Creation by Heap Insertion
for latest location 1 to n
{
Perform Heap Insertion }
{
latest_location N
A(latest_location) ITEM
new_child latest_location
Back : corresponding_ parent new_child/2
if corresponding_parent < 1 then { Insertion is Completed
Terminate }
else
if( A(corresponding_parent) < ITEM
then { A(new_child) A(corresponding_parent}
A(corresponding_parent) ITEM
new_child corresponding_parent
go Back }
else { ITEM is settled
Terminate } }
Illustration : Consider the data set {40, 80, 35, 90, 45, 50,70}
Latest location = 1
1 40 1 40
Latest location = 2
1 40 1 80
2 80 2 40
Illustration : Consider the data set {40, 80, 35, 90, 45, 50,70}
Latest location = 1
1 40 1 40
Latest location = 2
1 40 1 80
2 80 2 40
Latest location = 3
1 80 1 80
2 40 35 3 2 40 35 3
Illustration : Consider the data set {40, 80, 35, 90, 45, 50,70}
Latest location = 3
1 80 1 80
2 40 35 3 2 40 35 3
Latest location = 4
1 80 1 80 1 90
2 40 35 3 2 90 35 3 2 80 35 3
4 90 4 40 4 40
Illustration : Consider the data set {40, 80, 35, 90, 45, 50,70}
Latest location = 4
1 80 1 80 1 90
2 40 35 3 2 90 35 3 2 80 35 3
4 90 4 40 4 40
Latest location = 5
1 90 1 90
2 80 35 3 2 80 35 3
4 40 45 5 4 40 45 5
Illustration : Consider the data set {40, 80, 35, 90, 45, 50,70}
Latest location = 5
1 90 1 90
2 80 35 3 2 80 35 3
4 40 45 5 4 40 45 5
Latest location = 6
90
1 1 1 90
1
80 2 35 3 80 2 50 3
2 2
4 40 45 5 6 50 4 40 45 5 6 35
Illustration : Consider the data set {40, 80, 35, 90, 45, 50,70}
Latest location = 6
90
1 1 90
1
1
80 2 35 3 80 2 50 3
2 2
4 40 45 5 6 50 4 40 45 5 6 35
Latest location = 7
1 90
1 1 90
1
80 2 50 3 80 2 70 3
2 2
4 40 45 5 6 35 70 7 4 40 45 5 6 35 50 7
TIME COMPLEXITY ANALYSIS
Best Case
As and when a new element is inserted, it gets settled in the
latest child location.
n elements are inserted
Therefore
t best n (n)
TIME COMPLEXITY ANALYSIS
Worst Case
Let the level number be j where insertions are currently
being made.
There are 2j locations where elements are inserted
Let us assume in each case the inserted element moves up
to the root i.e. level 0
Therefore
t to insert each element j
t for all elements inserted in jth level j * 2j
TIME COMPLEXITY ANALYSIS
t worst ( 0 * 20 + 1 * 21 + 2 * 2 2 . . . + l * 2l
We know the identity
nlog n O(nlog n)
TIME COMPLEXITY ANALYSIS
O(nlog2n)
t
(n)
WHY ?
Profile :
2i (for i = 1, 2, 3, . . ., n) = 2 n+1 - 2
21 + 22 = 2 + 4 = 6 = 23 - 2
21 + 22 + 23 = 14 = 24 - 2
2 log n
2
=n
Hint : Take log on both sides (to base 2)
Heap Creation by Heap Shake / Heap Adjust
1 100
1
119 2 118 3
2
1 100
1
2 1192 151 3
2 1192 151 3
1192 151
p=p–1=2
2 3
Ch1 = 4
1 100
1
2 1712 151 3
2 1712 151 3
1 171
1
1 171
1
1 171
1
2 119 2 151 3
Youngest Child or
Latest Child = n
Let us Devise the Algorithm
Youngest Child or
Latest Child = n
Corresponding
Youngest Parent =
Pa n / 2
During Hand Simulation we have seen that we need two
control pointers namely
Parent Counter - Let p indicate Parent Counter
Initially p Pa
Subsequently p p – 1 till all parents are
exhausted
Disturbed New Parent Counter – Let P indicate this
To begin with p Pa
Pp
If there is a movement, new child node into which P ‘s
content moves is assigned = P
ALGORITHM for HEAP - ADJUST
p Pa
Back2 : P p
Back1 : If P > Pa then { p p – 1, if p < 1 then heap completes
else go Back2 }
else
{ Ch1 2 * P ; Ch2 Ch1 + 1}
If (A(Ch1) > A(Ch2) then { X A(Ch1) ; L Ch1}
else { X A(Ch2) ; L Ch2};
If (X >A(P) then { A(L) A(P) ; A(P) X;
P L; go Back1;}
else {p p – 1, if p < 1 then heap completes
else go Back2 }
ANALYSIS
Oth Level
ith Level
Due to
Movement Depth = l - i
PL
lth Level
Total number of nodes in ith level is 2i
2i (for i = 1, 2, 3, . . ., n) = 2 n+1 - 2
t l [20 + 21 + 22 . . . +2l-2 ] - [1 * 21 + 2 * 22 + 3 * 23
. . . (l – 2) * 2l-2 ] + 2l-1
We Know that
i* 2i (for i = 1, 2, 3, . . ., n) = (n –1) 2 n+1 + 2
2i (for i = 1, 2, 3, . . ., n) = 2 n+1 - 2
t - l + 4 * 2l-1 - 2
t - l + 22 * 2l-1 - 2 - l + 2l-1 + 2 - 2 - l + 2l+ 1 - 2
t 2l+ 1 t 2 log 2n
t n O(n)
1. Profile Heap adjust Algorithm
2. Devise an algorithm to recognize whether a complete binary tree
is a HEAP or NOT. Is it a - O algorithm or Complex
algorithm. Can there be an alternate way to develop this
algorithm ?
3. Redraft Heap adjust algorithm starting from the eldest parent
node instead of starting from youngest parent node
4. What is of Heap adjust algorithm
5. Compare and contrast Heap creation by insertion and Heap
creation by adjustment
1 171
1
6. Given a Heap devise an algorithm to locate
1. First Largest Element
2 1192 151 3
2. Second Largest Element
3. Third Largest Element
Derive Complexity Measures 112 5 6 118
4 100 132 7
HEAP SORT
Background :
We know the largest exist in the Heap apex
1 171
1
Consider a Heap of 7 elements
2 1192 151 3
1 171
1
Consider a Heap of 7 elements
2 1192 151 3 LAST = 7
1 171
1
Consider a Heap of 7 elements
2 1192 151 3 LAST = 7
1 171
1
Consider a Heap of 7 elements
2 1192 151 3 LAST = 7
2 1192 151 3
Swap the contents
of last and apex
100 112 118 171
4 5 7
6
Illustration :
1 132
1
2 1192 151 3
1 132
1
1 151
1
2 1192 151 3
2 1192 132 3
1 118
1
2 1192 132 3
2 1192 118 3
2 1192 132 3
2 1192 118 3
2 1192 118 3
1 100
1 1 100
1
1 100
1 1 100
1
2 1122 2 1002
118 119 132 151 171 118 119 132 151 171
3 4 5 6 7 3 4 5 6 7
1 1 100
1
100
2 1122
112 118 119 132 151 171 118 119 132 151 171
2 3 4 5 6 7 7
3 4 5 6
1
100