Professional Documents
Culture Documents
Course Objectives
To design and analyze the performance of algorithms by mathematical logic
building
To evaluate instances by using Divide and Conquer techniques.
To apply problem solving techniques such as Greedy and Dynamic
Programming strategy.
To demonstrates Backtracking and Branch-Bound algorithmic design strategies
To identifies and analyze NP-Hard and NP-Complete algorithms
To study Parallel and String Matching algorithms.
problem
For example,
Task: to make a cup of tea.
Algorithm:
12 = 0
Data types
Assignment statements
Expression
If statement
Switch statement
Looping statements
Input/output statements
Functions
T(n) = Cop.C(n)
S(p) = C + Sp
Fixed part: Instruction, Variables, space for constants
and array size
Variable part: whose size is dependent upon the
particular problem instance being solved
Ms. Savitri B. Patil DAA 14
Analysis of algorithm is the process of analyzing the problem-solving
capability of the algorithm in terms of the time and size required (the
size of memory for storage while implementation). However, the
main concern of analysis of algorithms is the required time or
performance. Generally, we perform the following types of analysis −
#include <stdio.h>
int main()
{
printf("Hello World");
}
#include <stdio.h>
void main()
{
int i, n;
for (i = 1; i <= n; i++) { 3C n+1 3(n+1)+n = 3n+3+n
printf("Hello Word !!!\n"); 1C n 4n+3 =O(n)
}
}
Sum(a,b){
return a+b ; or c=a+b; cost 2 no of times 1
return c cost 1 no of time 1
} O(1)
list_Sum(A,n){
total =0 cost 1 no of times 1
for i=0 to n-1 cost 2 no of times n+1
sum = sum + A[i] cost 2 no of times n
return sum cost 1 no of times 1
}
1+2n+2+2n+1 = 4n+4 = O(n)
for i=0 to n-1 cost = 2 no of times n+1
for j=0 to n-1 2 n(n+1)
sum= sum+a[i] 2 nn
return sum 1 1
2(n+1)+2(nn+n)+2(nn)+1
2n+2+2n.n+2n+2n.n+1
4n2+4n+3
O(n2)
c1+(n+1)+c2n+(n+1)+c3n
If else statement
if ( )
{
O(n)
}
else
{
O(n2)
}
for (i=1; i<n; i=i*2)
Statement;
…..
2k log28 =3 log210 =3.3
log223 = 3log22 =3
Assume i>=n
i=2k 2k>=n we can write 2k=n
k=log n O(log n)
Frequency Count Method
s/c Frequency Total
Function ArraySum(A , n) 0 0 0
Sum=0; 1 1 1
for(i=0; i<n; i++) 2 n+1 2n+2
{
sum=sum+A[i]; 2 n 2n
}
return sum; 1 1 1
End Function
(n) 4n+4
Frequency Count Method
Step Count
int sum(int n)
{
int sum=0; 1
for(i=0; i<n; i++) 1+(n+1)+n
sum+=i*i*i 4n
return sum; 1
}
O(n) 6n+4
1
1
1+(n+1)+n
2n+2
n
2n
n
1
1
1
6n+7 =>O(n)
i P
p=0; 1 0+1=1
for(i=1;p<=n; i++) 2 0+1+2=3
p=p+i; 3 0+1+2+3=4
return p; 4 0+1+2+3+4=5
} . p>n
. k(k+1)/2
k (k2 +k)/2>n
k2>n
k>n =O(n)
i
for(i=n; i>=1; i=i/2)
{
Statement; n=6
} i= 6 stat-1
i=3 stat-2
i=1 stat-3
i=0
i
for(i=1; i*i<=n; i++) 1*1
{ 2*2 i*i >=n
Statement; 3*3 i2 >=n
} 4*4 i >= n
. O(n)
.
.
.
i2
i j No of
times
for (i=0; i< n; i++) 0 0 0
1 0 1
for (j=0; j < i; j++)
statements 1x
2 0 2
1
2x
1+2+3+……n 3
0 3
1
n(n+1)/2 =(n2 +n)/2 2
3
4x
(n2)
for( ; m!=n; )
{
if(m>n)
m=m-n; M=24 n=12
12 12
else
m=16 N=2 m=34 n=12
n=n-m; M=8 N=1
14 2 22 12
7 1
} 12 2 10 12
6 1
10 2 10 2
5 1
8 2 8 2
4 1
6 2 6 2
3 1
4 2 4 2
2 1
2 2 2 2
1 1
Test(n)
{
if(n>5)
printf(“%d”, n);
else
{
for(i=0; i<n; i++)
printf(“%d”, i);
}
Space complexity
int add (int x, int y, int z)
{
int sum= x+y+z;
return sum;
}
Variables
a - n.n
b - n.n
c - n.n
n , i, j = 3n2+3 O(n2)
Algorithm add(a, b, n)
for(i=0; i<n; i++)
for(j=0; j<n; j++)
c[i][j]=0;
for(k=0; k<n; k++)
c[i][j]=c[i][j]+a[i][k]+b[k][j];
end
O(n3 )
Variables
a - n.n
b - n.n
c - n.n
n , i, j k,= 3n2+4 O(n2)
Imagine a classroom of 50 students in which you gave your pen to
one person. Now, you want that pen.
The ways to find the pen and what the O order is?
Now I divide the class into two groups, then ask: “Is it on the left
side, or the right side of the classroom?” Then I take that group and
divide it into two and ask again, and so on. Repeat the process till
you are left with one student who has your pen.
You go and ask the first person of the class, if he has the pen. Also,
you ask this person about other 49 people in the classroom if they
have that pen and so on,