1_Introduction
1_Introduction
Algorithms
Suppose we want to implement a problem P in any programming
language. So, before writing the program, we first design algorithm(s).
How to decide which is the best algorithm among all? What are the
different parameters that helps us to compare algorithms?
Algorithms
Let first discuss Algorithm followed by the different ways used for analysis.
Algorithms
What is an Algorithm?
Algorithm Program
Design dependent Implementation dependent
Requires domain knowledge Programmer
Any language or mathematical Programming language
notations can be used for writing
algorithm
H/W and S/W independent Dependent on H/W and S/W
Analyze the algorithm for desired Test the program for the correct result
result
Algorithm requires priori analysis Programs need posteriori testing
How to Analyse Algorithms
Main Aspects
• Time
• Need to define a time function to determine the time taken by the
algorithm
• Space
• Need to define a space function to determine the space required for
the algorithm
Other Aspects
• Network consumption
• Power consumption
Reasons to analyse algorithms
• Predict performance.
• Compare algorithms.
• Provide guarantees.
Time: ?
Space: ?
How to Analyse Algorithms
Algorithm swap(a, b)
{
temp=a; ----------- 1 unit
a=b; ----------- 1 unit
b=temp; ----------- 1 unit
} _______
f(n)=3 [time function]
Time
It means this algorithm will require constant time to return the output.
Space:
Number of variables in the algorithm are temp, a, and b. So, space
complexity will be S(n)=1+1+1=3 word and it is also constant.
Space: ?
Another Example
Algorithm Add (A, B, n)
{
for(i=0;i<n;i++) -------------------- n+1
{
for(j=0;j<n;j++) ------------- n*(n+1)
{
C[i,j]= A[i, j]+B[i, j];-- n*n
} ____________
} f(n)=2*n2+2n+1
} Highest degree is 2, so it could be
written as O(n2)
Space complexity
Variables are A, B, C, n, i, j, where A, B, and C are matrices so, space required
for them will be n2 and for n, i, and j it will 1 words. Total space required
will be 3n2+3 words equivalent to O (n2).
Another Example
Algorithm multi (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];
}
}
}
Time: ?
Space: ?
Another Example
Algorithm multi (A, B, n)
{
for(i=0;i<n;i++) --------------------------- n+1
{
for(j=0;j<n;j++) ------------------------- n*(n+1)
{
c[i,j]=0; ------------------------- n*n
for (k=0; k<n; k++)------------------- n*n*(n+1)
{
c[i,j]= c[i,j]+ A[i,k]=B[k,j]; ---- n*n*n
} ___________________
f(n)= 2n3+3n2+2n+1
= O(n3)
}
}
Space complexity
Variables are A, B, C, n, i, j,k where A, B, and C are matrices so, space required
for them will be n2 and for n, i, j and k, it will 1 words. Total space required will
be 3n2+4 words equivalent to O (n2).
Another Example
for(i=0;i<n; i++)
{
for(j=0;j<i; j++)
{
Stmt;
}
}
Time: ?
Another Example
for(i=0;i<=n; i++)
i j no. of time
{
0 0 0 time
for(j=0;j<i; j++)
{ 1 0/1 1
Stmt; 2 0,1/2 2
} 3 0,1,2/3 3
} .
n 0,1,..n/n+1 n times
Time: ?
Example
5. p=0;
for(i=1;p<=n; i++)
{
p=p+i;
}
i p
1 0+1 Assume it will stop when p>n,
2 0+1+2 As p= 1+2+3+…+k= k(k+1)/2
3 0+1+2+3 So, k(k+1)/2 > n
. . = k2 > n
k 0+1+2+…+k K>√n
This for loop will execute for
O(√n)times.
Example
6. for(i=1;i<n; i=i*2)
{
stmt;
}
Time: ?
Example
6. for(i=1;i<n;i=i*2) n=8 n=10
{ i i
stmt; 1<8 Y 1
} 2<8 Y 3 times 2
i 4<8 Y 4 4 times
1 8<8 N 8
1*2=2 16>10 N
2*2= 22 log 8=3 log 10=3.2<4
22*2 =23 log 23=3 Since, it
. must be 4. therefore, we will take
. ceil(log 10)
2k-1*2= 2k
This will stop when i>=n
As, i=2k
So, 2k>n=> k*log22=log2n
It means stmt will execute log2n times
k= O (ceil(log2n))
Observation from Analysis
I. for(i=0; i<n; i++)-→ O(n)
Time: ?
Analysis of if and while
Algorithm Test(n)
{
if(n<5) If n is less 5 than the algo
printf(“%d”, n); will execute 1 time
else
for(i=0; i<n; i++)
{ If n is greater 5 than the
printf(“%d”, i); algo will execute n+1 times
}
}
For the above, if statement decides, how many times it will execute. In the above
statement if condition will be true then it will execute only once i.e., the best-case
time O(1), otherwise it will execute (n+1) times which will be the worst-case time.
f(n)= n/2000 +6
Types of time functions
Quadratic time functions
f(n)= O(n2 )
Cubic time functions
f(n)= O(n3 )
Exponential time functions
f(n)= O(2n )
f(n)= O(3n )
f(n)= O(nn )
Comparison of time functions
1< log n < √n < n < n log n < n2 < n3 < … < 2n < 3n < … < nn
Log n n n2 2n
0 1 1 2
1 2 4 4
2 4 16 16
3 8 64 256
4 16 256 65536