Algo PPT
Algo PPT
Unit-1
Insertion_Sort(A)
1. n length[A]
2. for j2 to n
1. aA[j]
2. //Insert A[j] into the sorted sequence A[1 .. j-1].
3. i j-1
4. While i > 0 and a < A[i]
1. A[i+1]A[i]
2. i i-1
5. A[i+1] a
Insertion Sort Algorithm Analysis
Insertion_Sort(A)
Instruction cost times
n length[A] c1 1
for j2 to n c2 n
aA[j] c3 n-1
//Insert A[j] into the sorted 0 n-1
sequence A[1 .. j-1].
i j-1 c4 n-1
𝑛
While i > 0 and a < A[i] c5 𝑗<2 𝑡𝑗
𝑛
A[i+1]A[i] c6 𝑗<2(𝑡𝑗-1)
𝑛
i i-1 c7 𝑗<2(𝑡𝑗-1)
A[i+1] a c8 n-1
Time complexity of Insertion sort
Now, we have to find c1, c2 and n0, such that equation (1) is satisfied.
Consider, left part of (1), c1 ≤ (1/2)- (3/n) …………. (2)
The value of c1 will be positive value less than or equal to the minimum value
of (1/2)- (3/n). Minimum value of (1/2)- (3/n) = 1/14. Therefore, c1 =
1/14 . This value of c1 will satisfy equation (2) for n ≥ 7.
Here, c1 = 1/14 and n ≥ 7 which satisfy (2).
Asymptotic Notations
Consider, right part of (1), (1/2)- (3/n) ≤ c2 , …………. (3)
The value of c2 will be positive value greater than or equal
to the maximum value of (1/2)- (3/n). Maximum value of
(1/2) - (3/n) = 1/2. Therefore, c2 = 1/2 . This value of c2
will satisfy equation (3) for n ≥ 1.
Here, c2 = 1/2 and n ≥ 1 which satisfy (3).
Therefore, for c1 = 1/14 , c2 = 1/2 and n0 = 7, equation (1)
is satisfied.
Hence by using definition of θ-notation ,
(1/2)n2 - 3n = θ(n2).
It is proved.
Asymptotic Notations
Example: Show that 2n+5 = O(n2).
Solution: Using definition of O-notation,
f(n) ≤ cg(n) , ∀ n ≥ n0
In this question, f(n) = 2n+5 and g(n) = n2 , therefore
2n+5 ≤ c n2 ∀ n ≥ n0
We divide above by n2, we get
(2/n)+(5/n2) ≤ c , ∀ n ≥ n0 ………(1)
Now, we have to find c and n0, such that equation (1) is
satisfied.
Asymptotic Notations
The value of c will be positive value greater than or equal to
the maximum value of (2/n)+(5/n2) .
Maximum value of (2/n)+(5/n2) = 7.
Therefore, c = 7.
Clearly equation (1) is satisfied for c = 7 and n ≥ 1.
Hence by using definition of O-notation ,
2n+5 = O(n2).
It is proved.
Asymptotic Notations
Example: Show that 2n2+5n+6 = 𝛀(n).
Solution: Using definition of 𝛀 -notation,
cg(n) ≤ f(n) , ∀ n ≥ n0
In this question, f(n) = 2n2+5n+6 and g(n) = n , therefore
cn ≤ 2n2+5n+6 , ∀ n ≥ n0
We divide above by n, we get
c ≤ 2n + 5 + (6/n) , ∀ n ≥ n0 ………(1)
Now, we have to find c and n0, such that equation (1) is
always satisfied.
Asymptotic Notations
The value of c will be positive value less than or equal to the
minimum value of 2n + 5 + (6/n) .
Minimum value of 2n + 5 + (6/n) = 12.
Therefore, c = 12.
Clearly equation (1) is satisfied for c = 12 and n ≥ 2.
Hence by using definition of 𝛀 -notation ,
2n2+5n+6 = 𝛀 (n).
It is proved.
Asymptotic Notations
Example: Show that 2n2 = o(n3).
Solution: Using definition of o-notation,
f(n) < cg(n) , ∀ n ≥ n0
Here, f(n) = 2n2, and g(n) = n3. Therefore,
2n2 < cn3 , ∀ n ≥ n0
We divide above by n3, we get
(2/n) < c , ∀ n ≥ n0 …………(1)
for c = 1, there will be n0 = 3, which satisfy (1).
for c = 0.5, there will be n0 = 7, which satisfy (1).
Therefore, for every c, there exists n0 which satisfy (1).
Hence 2n2 = o(n3).
Asymptotic Notations
Example: Show that 2n2 ≠ o(n2).
Solution: Using definition of o-notation,
f(n) < cg(n) , ∀ n ≥ n0
Here, f(n) = 2n2, and g(n) = n2. Therefore,
2n2 < cn2 , ∀ n ≥ n0
We divide above by n2, we get
2 <c, ∀ n ≥ n0 …………(1)
Clearly for c = 1, inequality (1) does not satisfy.
Therefore, for every c, there does not exist n0 which satisfy
(1). Hence 2n2 ≠ o(n2).
Asymptotic Notations
Example: Show that 2n2 = 𝛚(n).
Solution: Using definition of 𝛚 -notation,
cg(n) < f(n) , ∀ n ≥ n0
Here, f(n) = 2n2, and g(n) = n. Therefore,
cn < 2n2 , ∀ n ≥ n0
We divide above by n, we get
c < 2n , ∀ n ≥ n0 …………(1)
for c = 1, there will be n0 = 1, which satisfy (1).
for c = 10, there will be n0 = 6, which satisfy (1).
Therefore, for every c, there exists n0 which satisfy (1).
Hence 2n2 = 𝛚(n).
Asymptotic Notations
Example: Show that 2n2 ≠ 𝛚(n2).
Solution: Using definition of 𝛚 -notation,
cg(n) < f(n) , ∀ n ≥ n0
Here, f(n) = 2n2, and g(n) = n2. Therefore,
cn2 < 2n2 , ∀ n ≥ n0
We divide above by n2, we get
c <2, ∀ n ≥ n0 …………(1)
Clearly for c = 3, there does not exists n0, which satisfy (1).
Therefore, for every c, there does not exist n0 which satisfy
(1). Hence 2n2 ≠ 𝛚(n2).
Asymptotic Notations
Example: Show that using definition of notations
(a) 3n3-10n+50 = θ(n3)
(b) 5n2-100n ≠ θ(n3)
(c) 3n3-10n+50 = O(n3)
(d) 5n2-100n ≠ O(n)
(e) 3n3-10n+50 = 𝛀(n3)
(f) 5n2-100n ≠ 𝛀(n3)
Asymptotic Notations
Limit based method to compute notations for a function
𝑓 𝑛
First compute lim = c.
𝑛→∞ 𝑔 𝑛
Solution-(7):
n3, (3/2)n, 2n, n2, log(n), 22n, loglog(n), n!, en
Ascending order is
loglog(n), log(n), n2, n3, (3/2)n , 2n , en, n!, 22n
Asymptotic Notations
AKTU questions
1. Take the following list of functions and arrange them in
ascending order of growth rate. That is, if function g(n)
immediately follows function f(n) in your list, then it
should be the case that f(n) is O(g(n)). f1(n) = n2.5, f2(n) =
√2n, f3(n) = n + 10, f4(n) = 10n, f5(n) = 100n, and
f6(n) = n2 log n
T(n) cn cn
(d)
Recurrence tree method
Let the height of the tree is h. Therefore
𝑛
3 = 1 ⇒ n = lg3/2n
2
T(1)
Let h is the height of the tree. Therefore,
αhn = 1 ⇒ h = log1/α(n)
Therefore, total cost of the tree
T(n) = cn + cn +cn + …………………+ cn
= (h+1) cn
= (log1/α(n) +1) cn
= cn log1/α(n) + cn
= O(n log1/α(n) )
= O(nlog n)
Therefore, T(n) = O(nlog n).
Master Theorem
Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and let
T(n) be defined on the nonnegative integers by the
recurrence
T(n) = aT(n/b) + f(n);
Where we interpret n/b to mean either ⌊n/b⌋ or ⌈n/b⌉. Then
T(n) has the following asymptotic bounds:
1. If f(n) = 𝑂(nlogba − ϵ) for some constant ϵ > 0, then
T(n) = θ(nlogba).
2. If f(n) =θ(nlogba) , then T(n) = θ(nlogba lg n).
3. If f(n) = 𝛺(nlogba + ϵ) for some constant ϵ > 0, and if
af(n/b) ≤ cf(n) for some constant c < 1 and all sufficiently
large n, then T(n) = θ(f(n).
Master Theorem Method
Example: Solve the following recurrence relations using
master theorem method
(a) T(n) = 9T(n/3) + n
(b) T(n) = T(2n/3) + 1
(c) T(n) = 3T(n/4) + n log n
Solution:
(a) Consider T(n) = 9T(n/3) + n
In this recurrence relation, a = 9, b= 3 and f(n) = n.
Therefore, n𝑙𝑜𝑔𝑏𝑎 = nlog39 = n2
Clearly, n𝑙𝑜𝑔𝑏𝑎 > f(n) , therefore case1 can be applied.
Now determine ϵ such that f(n) = 𝑂(n2−ϵ) . Here ϵ = 1.
Therefore case 1 will be applied.
Hence solution will be T(n) = θ(n2).
Master Theorem Method
Solution:
(b) Consider T(n) = T(2n/3) + 1
In this recurrence relation, a = 1, b= 3/2 and f(n) = 1.
Therefore, n𝑙𝑜𝑔𝑏𝑎 = nlog3/2 1 = 0
Clearly, f(n) =θ(nlogba) , therefore case 2 will be applied.
Hence solution will be T(n) = θ(log n).
Master Theorem Method
Solution:
(c) Consider T(n) = 3T(n/4) + n log n
In this recurrence relation, a = 3, b= 4 and f(n) = n log n.
Therefore, n𝑙𝑜𝑔𝑏𝑎 = nlog43 = n0.793
Clearly, n𝑙𝑜𝑔𝑏𝑎 < f(n) , therefore case 3 can be applied.
Now determine ϵ such that f(n) =𝛺(n0.793+ϵ) . Here ϵ = 0.207.
Now, af(n/b) ≤ cf(n) imply that 3f(n/4) ≤ cf(n)
⇒ 3(n/4)log (n/4) ≤ c n log n
⇒ (¾)log(n/4) ≤ c log n
Clearly above inequality is satisfied for c = 3/4. Therefore
case 3 will be applied.
Hence solution will be T(n) = θ(n log n).
Master theorem method
Example: Solve the following recurrence relation
T(n) = 2T(n/2) + n log n
Solution: Here, a = 2 , b=2 and f(n) = n log n.
nlogba = nlog22 = n
If we compare nlogba and f(n), we get f(n) is greater than nlogba
. Therefore, case 3 may be applied.
Now we have to determine ϵ >0 which satisfy f(n) = 𝛺
(nlogba+ϵ), i.e. n logn = 𝛺(n1+ϵ). Clearly there does not exist
any ϵ which satisfy this condition. Therefore case 3 can not
be applied. Other two cases are also not satisfied. Therefore
Master theorem can not be applied in this recurrence
relation.
Generalized Master theorem
Theorem: If f(n) = θ(nlogba lgkn), where k ≥ 0, then the
solution of recurrence will be T(n) = θ(nlogba lgk+1n).
Now, consider the previous example:-
T(n) = 2T(n/2) + n log n
Solve it using aboe theorem,
Here a =2, b= 2, and k = 1. Therefore, the solution of this
recurrence will be
T(n) = θ(nlog22 lg1+1n)
= θ(n lg2n)
Hence, T(n) = θ(n lg2n)
Recurrence relation
Exercise
1. Use the master method to give tight asymptotic bounds for
the following recurrences:-
(a) T(n) = 8T(n/2) + θ(n2)
(b) T(n) = 7T(n/2) + θ(n2)
(c) T(n) = 2T(n/4) + 1
(d) T(n) = 2T(n/4) + √n
2. Can the master method be applied to the recurrence
4T(n/2) + n2 log n ? Why or why not? Give an asymptotic
upper bound for this recurrence.
Recurrence relation
3. Give asymptotic upper and lower bounds for T(n) in each
of the following recurrences. Assume that T(n) is constant
for n ≤ 2. Make your bounds as tight as possible, and justify
your answers.
(a) T(n) = 2T(n/2) + n4
(b) T(n) = T(7n/10) + n
(c) T(n) = 16T(n/4) + n2
(d) T(n) = 2T(n/4) + √n
(e) T(n) = T(n-2) + n2
(f) T(n) = 7T(n/3) + n2
(g) T(n) = 3T(n/3 -2) + n/2
Recurrence relation
4. Give asymptotic upper and lower bounds for T(n) in each of the
following recurrences. Assume that T(n) is constant for sufficiently small
n. Make your bounds as tight as possible, and justify your answers.
(a) T(n) = 4T(n/3) + n lgn
(b) T(n) = 3T(n/3) + n/lgn
(c) T(n) = 2T(n/2) + n/lgn
(d) T(n) = T(n/2) + T(n/4) + T(n/8) + n
(e) T(n) = T(n-1) + 1/n
(f) T(n) = T(n-1) + lg n
(g) T(n) = T(n-2) + 1/lg n
(h) T(n) = √n T(√n) + n
Recurrence relation
Some exercise solution
(3-e) Recurrence relation is T(n) = T(n-2) + n2 .
We will solve it using iteration method.
T(n) = T(n-2) + n2
= T(n-4) + (n-2)2 + n2
= T(n-6) + (n-4)2 + (n-2)2 + n2
……………………………………………….
………………………………………………
= T(0) + 22 + 42 + 62 +……………..+(n-2)2 + n2
= d + 22 + 42 + 62 +………+(n-2)2 + n2 (Let T(0) =d)
𝑛 𝑛:1 𝑛:2
=d+ = θ(n3)
6
Recurrence relation
Some exercise solution
(3-g) Recurrence relation is T(n) = 3T(n/3 -2) + n/2.
This recurrence relation is equivalent to the following
equation T(n) = 3T(n/3) + n/2
Apply master theorem, here a = 3, b = 3 and f(n) = n/2.
nlogba = nlog33 = n
Clearly, f(n) = n/2 = θ(n) = θ(nlogba)
Therefore case 2 will be applied. Hence the solution is
T(n) = θ(n lg n)
Recurrence relation
Some exercise solution
(4-a) Recurrence relation is T(n) = 4T(n/3) + n lgn.
Apply master theorem, here a = 4, b = 3 and f(n) = n lgn.
nlogba = nlog34 = n1.26
Clearly, f(n) = n lgn = O(n1.26-ϵ) = O(nlogba - ϵ)
Therefore case 1 will be applied. Hence the solution is
T(n) = θ(n1.26)
Recurrence relation
Some exercise solution
(4-b) Recurrence relation is T(n) = 3T(n/3) + n/lgn.
We will use recurrence tree method to solve it.
Recurrence tree will be n/lgn
…………………… n/lgn
𝑛 𝑛
………… n/lg 𝑛
3
𝑛 𝑛 𝑛 𝑛 /lg
/lg /lg 3 3
3 3 3 3
𝑛 𝑛 𝑛
𝑛 𝑛 ………………………………….. /lg …. … n/lg
/lg 9 9 32
9 9
………………………………………………..
T(1)=d T(1) = d…………………………T(1) = d……. 3hd
Here d -> constant , h-> height of tree
Recurrence relation
Now, we calculate height of tree.
𝑛
Clearly, = 1 , ⇒ h = 𝑙𝑜𝑔3𝑛.
3
Now, total cost of this tree is
𝑛 𝑛 𝑛 𝑛
T(n) = + 𝑛 + 𝑛 + …………+ 𝑛 + 3hd
𝑙𝑔𝑛 lg( 3 ) lg(32) lg(3ℎ−1)
1 1 1 1
=n( + 𝑛 + 𝑛 + …………+ 𝑛 ) + 3hd
𝑙𝑔𝑛 lg( 3 ) lg(32) lg(3ℎ−1)
Suppose h = 2m
𝑛 1 1 1 1 1
= ( 1+ + + + + ⋯………+ 𝑚 ) + 𝑑𝑛
𝑙𝑔3 2 3 4 5 2
𝑛 1 1 1 1 1 1 1 1
< ( 1+( + ) + + + + + ⋯………+ ( + +
𝑙𝑔3 2 2 4 4 4 4 2𝑚 2𝑚
1
……..+ )) + 𝑑𝑛
2𝑚
𝑛
= ( 1+1+1+………….+1) + dn
𝑙𝑔3
𝑛 𝑛
= (m+1) + dn = (lg h +1) +dn
𝑙𝑔3 𝑙𝑔3
𝑛
= (lg log3n +1) +dn
𝑙𝑔3
= O(n lg lg n)
Some exercise solution
(4-d) Recurrence relation is
T(n) = T(n/2) + T(n/4) + T(n/8) + n
We will use recurrence tree method to solve it.
Recurrence tree will be n n
𝑛 𝑛 𝑛
7𝑛
2 4 8
8
7
𝑛 𝑛 𝑛 𝑛 𝑛 𝑛
( 8 )2 𝑛
𝑛 𝑛 𝑛
4 8 16 8 16 32 16 32 64
7
( 8 )3 𝑛
𝑛
Let h is the height of the tree. Therefore, = 1 ⇒ h = lg n.
2
Some exercise solution
Therefore total cost
7𝑛 7 2 7 3 7 ℎ
T(n) =n+ + 𝑛+ 𝑛 + ………….+ 𝑛
8 8 8 8
7 7 2 7 3 7 ℎ
= n( 1+ + + + ………….+ )
8 8 8 8
7 7 2 7 3
< n ( 1+ + + + … … … … … … .)
8 8 8
Here, we consider up to infinite term because common ratio
7
of this series is that is less than 1.
8
1
= n( 7 )
(1; )
8
= 8n = O(n)
Therefore, T(n) = O(n).
AKTU Examination Questions
1. Solve the recurrence T (n) = 2T(n/2) + n2+ 2n+ 1
2. Solve the recurrence using recursion tree method:
T (n) = T (n/2) + T (n/4) + T (n/8) + n
3. Use a recursion tree to give an asymptotically tight solution
to the recurrence T(n) = T(αn) + T((1 - α)n) + cn, where α is a
constant in the range 0 <α< 1 and c > 0 is also a constant.
4. The recurrence T (n) = 7T (n/3) + n2 describes the running
time of an algorithm A. Another competing algorithm B has a
running time of S (n) = a S (n/ 9) + n2. What is the smallest
value of ‗a‘ such that A is asymptotically faster than B?
5. Solve the recurrence relation by substitution method
T(n)= 2T(n/2) + n
AKTU Examination Questions
6. Show that the solution to T (n) = 2T (⌊n/2⌋ + 17) + n is
O(nlgn).
7. Solve the recurrence: T (n) = 50 T (n/49) + log n!
8. Solve the following recurrence using Master method:
T (n) = 4T (n/3) + n2
9. Find the time complexity of the recurrence relation
T(n) = n +T(n/10)+T(7n/5)
10. Solve the following By Recursion Tree Method
T(n) = n + T(n/5)+T(4n/5)
11. The recurrence T (n) =7T (n/2) +n2 describe the running
time of an algorithm A. A competing algorithm A has a running
time of T‘ (n) =aT‘ (n/4) +n2. What is the largest integer value
for a A‘ is asymptotically faster than A?
Heapsort
Heap
Max-heap Array
Heapsort
Index: If i the index of a node, then the index of parent and its
child are the following:-
Parent(i) = ⌊i/2⌋
Left(i) = 2i
Right(i) = 2i+1
Note: Root node has always index 1 i.e. A[1] is root element.
Heap-size: Heap-size is equal to the number of elements in the heap.
Height of a node: The height of a node in a heap is the
number of edges on the longest simple downward path from the
node to a leaf.
Height of heap: The height of the heap is equal to the height of its
root.
Heapsort
Types of heap
There are two kinds of binary heaps:
(1) max-heaps (2) min-heaps
Max-heap: The heap is said to be max-heap if it satisfy the
max-heap property.
The max-heap property is that the value at the parent node
is always greater than or equal to value at its children.
Min-heap: The heap is said to be min-heap if it satisfy the
min-heap property.
The min-heap property is that the value at the parent node
is always less than or equal to value at its children.
Heapsort
Heap sort algorithm consists of the following two sub-
algorithms.
(1)Max-Heapify: It is used to maintain the max-heap
property.
(2)Build-Max-Heap: It is used to construct a max-heap for
the given set of elements.
Heapsort
Max-Heapify Algorithm
Action done by max-heapify algorithm is shown in the following
figures:-
Heapsort
Max-Heapify Algorithm
Heapsort
Time complexity of Max-Heapify Algorithm
The running time of max-heapify is determined by the
following recurrence relation:-
T(n) ≤ T(2n/3) + θ(1)
Here n is the size of the sub-tree rooted at node i.
Using master theorem, the solution of this recurrence
relation is
T(n) = θ(lg n)
Heapsort
Build-Max-Heap Algorithm
Example: Construct max-heap corresponding to the
following elements
4, 1, 3, 2, 16, 9, 10, 14, 8, 7.
Solution:
Heapsort
Build-Max-Heap Algorithm (cont.)
Heapsort
Build-Max-Heap Algorithm (cont.)
Our tighter analysis relies on the properties that an n-element heap has
height ⌊lg n⌋ and at most ⌈n/2h+1⌉ nodes of any height h.
=
Since
Therefore,
T(n) = O( 2n)
= O(n)
Heapsort Algorithm
Example: Sort the following elements using heapsort
5, 13, 2, 25, 7, 17, 20, 8, 4.
Solution: The operation of HEAPSORT is shown as
following:-
Heapsort Algorithm
Heapsort Algorithm
Heapsort Algorithm
Pivot
There are many ways we can choose the pivot element.
i) The first element in the array
ii) The last element in the array
iii) The middle element in the array
iv) We can also pick the element randomly.
Note: In our algorithm, we are going to pick the last element as the
pivot element.
Quicksort
Example: Sort the following elements using quicksort
2, 8, 7, 1, 3, 5, 6, 4
Solution: Here, we pick the last element in the list as a pivot
1 2 3 4 5 6 7 8
82
8 2 8 7 1 3 5 6 4
7 2 8 7 1 3 5 6 4
2 8 7 1 3 5 6 4
2 8 7 1 3 5 6 4
2 1 7 8 3 5 6 4
2 1 3 8 4 5 6 4
Quicksort
82 1 2 3 4 5 6 7 8
8 2 1 3 8 7 5 6 4
7
2 1 3 4 7 5 6 8
Partition completed in first pass
2 1 3 4 7 5 6 8
2 1 3 4 7 5 6 8
2 1 3 4 7 5 6 8
2 1 3 4 7 5 6 8
2 1 3 4 7 5 6 8
Partition completed in second pass
Quicksort
82 1 2 3 4 5 6 7 8
8 2 1 3 4 7 5 6 8
7
2 1 3 4 7 5 6 8
1 2 3 4 5 7 6 8
1 2 3 4 5 6 7 8
Partition completed in third pass
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Process
1 2 3 4 5 6 7 8 completed
Quicksort Algorithm
8
Quicksort(A, p, r)
1 if p < r
2 q = PARTITION(A, p, r)
3 QUICKSORT(A, p, q-1)
4 QUICKSORT(A, q+1, r)
Thank you.