DAA-Unit-1 Students (204-25)
DAA-Unit-1 Students (204-25)
(DAA)
Problem
algorithm
▪ NP Problems
▪ Time complexity
e.g.
Sum ( a, b)
{
int c;
c=a+b;
}
Space required= 3 unit ( for a, b, c)
@Dr. Shankar Thawkar-HOD
Time Complexity
▪ Time required for the execution of an Algorithm.
▪ Analysis are of two types-
▪ Priori Analysis
▪ Posterior Analysis
▪ Priori Analysis-
▪ Analyze the algorithm before execution on a machine.
▪ Use frequency count method.
▪ Time complexity of an algorithm is denoted by f(n), where n is input size.
▪ No unit for time
@Dr. Shankar Thawkar-HOD
Ex-2 Find the following algorithms-
Ex. -1 10 1 20 6 7 15 100 25 30
1. Best Case :
▪ It occur , if the searching element found at first position. i.e. f(n)=O(1)
▪ It indicate minimum time required for the algorithm.
@Dr. Shankar Thawkar-HOD
Best , Average and Worst case Analysis
Example- 10 1 20 6 7 15 100 25 30
2. Average case :
▪ In this, we assume that element may be found at any position. So we consider a
probability-
i.e. f(n)= 1/n + 2/n+ 3/n+……………..+n/n
= (n+1)/2= O(n)
▪ It indicate average time required for the algorithm.
3. Worst case :
▪ It occur , if the searching element is found at last position. Or does not found i.e.
f(n)=O(n)
▪ It indicate maximum time required for the algorithm.
@Dr. Shankar Thawkar-HOD
Asymptotic Notations
▪ Big oh ‘O’ Notation
▪ Omega ‘Ω’ Notation
▪ Theta ‘θ’ Notation
Given-
Given-
Given-
Given-
Given-
Given-
▪ Following summation formulas are used in solving most of the problems using
iterative substitution method
𝒙𝒏+𝟏 −𝟏
𝐟𝐨𝐫 𝐱 ≠ 𝟏; σ𝒏𝒌=𝟎 𝒙𝒌 = 1+x+x2 +…… =
𝒙−𝟏
𝟏
for |x|<1 ; σ∞
𝒌=𝟎 𝒙𝒌
=
𝟏−𝒙
Ans-
1. T(n) = (n2)
2. T(n) = (n3)
3. T(n) = (logn)
4. T(n) = (nlogn)
Representation of Heap :
1. Illustrate the operation of Heap-sort on array A=<25, 57, 48, 37, 12, 92, 86, 33>
2. What are the minimum and maximum number of nodes in a heap of height h.
3. Illustrate the operation of Heap-sort on array A=<25, 57, 48, 37, 12, 92, 86, 33>
4. What are the minimum and maximum number of nodes in a heap of height h.
• Let h be the height of the tree and n be the maximum number of nodes then-
4 2 2 2 2 2
4 4 4 4 4 4 2 2 2
9 4 4 4 1 1
9 2 2 2 2 2 4 4 4
2 1 1 1 4 3
2 9 6 6 6 6 6 6 1
6 6 6 9 1 1 1 1 1 3 3 3 3 4
6
1 1 1 1 9 3 3 3 3 3 6 6 6 6 6
3 3 3 3 3 9 9 9
9 9 9 9 9 9 9
2 1 1 1 1
Logic for , n=6 for i= 1 to n-1
1 2 2 2 2
For i=1 , J=1 to 5 { for j=1 to n-i
3 3 3 3 3 i=2 j=1 to 4 { if (A[j]> A[j+1] then
4 4 4 4 i=3 j= 1 to 3 exchange A[j] with A[j+1]
4
6 6 6 6 i=4 j=1 to 2 }
6
9 9 i=5 j=1 to 1 } Complexity-
9 9 9 @Dr. Shankar Thawkar-HOD f(n)=O(n2)
Selection Sort
• Idea:
– Find the smallest element in the array
– Exchange it with the element in the first position
– Find the second smallest element and exchange it with
the element in the second position
– Continue until the array is sorted
1 2 6 9 4 3 8 1 2 3 4 6 9 8
1 2 3 4 6 8 9
1 2 3 9 4 6 8
1 2 3 4 6 8 9
Complexity f(n)=O(n 2
@Dr. Shankar Thawkar-HOD )
Insertion Sort
input array
5 2 4 6 1 3
at each iteration, the array is divided in two sub-arrays:
left sub-array right sub-array
sorted unsorted
A[i + 1] ← key
➢ It is a bottom-up approach.
➢ The complexity of DAC algorithms is expressed as-
T(n)= aT(n/b)+f(n)
≤ x ≥
▪ Combine – It is trivial.
2 10 8 20 4 9 5 2 4 8 20 10 9 5
2 4 5 20 10 9 8
2 10 8 20 4 9 5
Partition (A,p,r)
{
x=A[r] // pivot
2 10 8 20 4 9 5 i=p-1
for j=p to r-1
{
if A[j]≤ x
2 10 8 20 4 9 5 {
i=i+1
exchange A[i] with A[j]
}}
2 10 8 20 4 9 5 exchange A[i+1] with A[r]
return (i+1) // value of q
}
@Dr. Shankar Thawkar-HOD
Quick-Sort Algorithm
2 4 5 20 10 9 8 Partition (A,p,r)
{
x=A[r] // pivot
i=p-1
2 4 5 20 10 9 8 for j=p to r-1
{
if A[j]≤ x
{
i=i+1
8 10 9 20 exchange A[i] with A[j]
2 4 5 }}
exchange A[i+1] with A[r]
return (i+1) // value of q
10 9 20 }
2 4
9 10
▪ Worst case : occurs when array is partitioned such that one contain ‘0’ elements
other contain n-1 elements
T(n)=T(0)+T(n-1)+n
= ϴ (n2 ) (by Tree or iteration method)
▪ Average case : occurs when array is partitioned in the ratio n/10 : 9n/10
T(n) =T(n/10)+T(9n/10)+n
=ϴ(nlogn) (by@Dr.
Tree or iteration method)
Shankar Thawkar-HOD
Thank You
12 10 8 2 40 9 15 2 8 9 10 12 15 40
12 10 8 2 40 9 15 2 8 10 12 9 15 40
12 10 8 2 40 9 15 10 12 2 8 9 40 15
12 10 8 2 40 9 12 10 8 2 40 9
T(n)=2T(n/2)+n
= (nlogn) [ by Master method]
▪ Requirement –
▪ Array must be in sorted order.
//p and r be the LB and UB & Compare A[mid]=x, lies in 2nd half
Call Binary_search(A, x, 5, 8)
1. If p>r then return -1 // not found
25 30 40 50
2. mid=(p+r)/2
3. If A[mid]=x then Compare A[mid]=x
4. return mid
5. else Call Binary_search(A, x, 7, 8)
6. If x<A[mid] then
7. binary_search (A,x,p,mid-1) 40 50
8. else & Compare A[mid]=x , lies in 2nd half
9. binary_search (A,x,mid+1, r) Call Binary_search(A, x, 7, 8)
50
@Dr. Shankar Thawkar-HOD
Compare A[mid]=x , Found
x= 2
x= 10 5 10 15 20 25 30 40 50
5 10 15 20 25 30 40 50
Call Binary_search(A, x, 1, 8)
Call Binary_search(A, x, 1, 8)
Compare A[mid]=x , x lies in 1st half
Call Binary_search(A, x, 1, 3) Compare A[mid]=x , x lies in 1st half
5 10 15 Call Binary_search(A, x, 1, 3)
Call Binary_search(A, x, 1, 2) 5 10 15
Compare A[mid]=x , x found at 2nd position , Compare A[mid]=x , x lies in 1st half
Call Binary_search(A, x, 1, 1)
Compare A[mid]=x
Call Binary_search(A, x, 1, 0)
@Dr. Shankar Thawkar-HOD Since, LB>UB , So return -1
Complexity of Binary search Algorithm
▪ The complexity of binary search is give by
T(n)=T(n/2)+1
= (logn) [ by Master method]
▪ The basic idea of counting sort is to determine, for each element x , the number
of elements less than x. This information is used to place the element x directly to
its correct place.
▪ For example if there are 17 elements less than x then x must be placed at position
18.
6 0 2 0 1 3 4 6 1 3 2
▪ So, radix sort perform sorting on ‘n’ numbers of d-digits where LSD is sorted
first then MSD is sorted
329
768
423
121
190
@Dr. Shankar Thawkar-HOD
Radix Sort Algorithm
Algorithm-
Radix_sort(A,d)
1. For j=1 to d do
use stable sort to array A on digit I
f(n)= d(n+k)
so, for k=n
f(n)= O(n)