Professional Documents
Culture Documents
UNIT 1: Analysis Techniques Chapter 1: Introduction To Analysis of Algorithms
UNIT 1: Analysis Techniques Chapter 1: Introduction To Analysis of Algorithms
Presidency College
(Autonomous)
(Autonomous)
Presidency
Group
Presidency College
(Autonomous)
Definition
Reaccredited by
NAAC with A+
Presidency
Group
7
Design an algorithm
Presidency College
(Autonomous)
• Brute force
• Divide and conquer
• Decrease and conquer
• Transform and conquer
Reaccredited by
NAAC with A+ • Space and time tradeoffs
• Dynamic programming
• Backtracking
• Branch and bound
Presidency
Group
• Greedy method
8
Analysis of algorithm efficiency
Presidency College
/performance analysis
(Autonomous)
3. To maintain simplicity.
4. To maintain generality.
9
Presidency College
(Autonomous)
Algorithms are analyzed in two ways
Presidency
Group
10
Presidency College
Two phases of analysis of algorithm
(Autonomous)
11
Presidency College
Contd..
(Autonomous)
Presidency
Group
12
Presidency College
(Autonomous)
Difference between analysis and
profiling or difference between priori
analysis and posteriori analysis.
Reaccredited by
NAAC with A+
Presidency
Group
13
PRESIDENCY COLLEGE
Presidency College
(Autonomous)
(Autonomous)
Space complexity
Reaccredited by
NAAC with A+
By
Veena S Badiger
B.E, M.Tech
Presidency
Asst Professor
Group
It is the amount of a memory that may be
Presidency College
required to run a program.
(Autonomous)
Following components are important in
calculating space requirements.
1. Instruction space: space requried to store the
machine code generated by the compiler.
2. Data space: space needed for constants , static
variables , intermediate variables , dynamic
Reaccredited by
NAAC with A+
variables etc.
3. Stack space: space to store return addresses ,
return values etc .
space compelxity given as
S(p)=cp+sp [sp-> space for code segments
Presidency
Group
Example 1:
(Autonomous)
#include <stdio.h>
Void main()
{
int x,y,z,sum;
Printf(“enter the three numbers”);
Reaccredited by
NAAC with A+ Scanf(“%d%d%d”,&x,&y,&z);
Sum=x+y+z;
Printf(“the sum =%d”,sum);
}
Presidency
Group
ANS : s(p)=4n
Presidency College
(Autonomous)
Example 2:
algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0
Reaccredited by
NAAC with A+
}
Presidency
Group ANS : s(p)= 0
Example 3:
Algorithm sum(a,n)
Presidency College
(Autonomous)
{
s=0.0;
For i=1 to n do
s=s+a[i]
Reaccredited by
NAAC with A+
Return s;
}
Presidency
Group
ANS: s(p)=n+3
Example 4:
Find min(int [] x)
Presidency College
(Autonomous)
{
int k=0; int n=x.length;
For(int i=1 ; i<n; i++)
{
Reaccredited by
NAAC with A+
if (x[i] < x[k])
K=i;
}
Presidency
Group
Return k;
}
ANS: s(p)=n+3
Example 5:
Void multvect (int [] x , int [] [] a)
Presidency College
(Autonomous)
{
int k=0;int n=x.length;
For(int i=1;i<n;i++)
{
for (int j=1 ; j<n;j++)
Reaccredited by
{
NAAC with A+
a[i][j]=x[i]*x[j]
}
}
Presidency
Group
}
ANS:s(p)= n x n+n+4
Examples for recursive algorithm
Presidency College
(Autonomous)
Example 1:
Algorithm Rsum(a,n)
{
Reaccredited by if(n<=0) then return 0.0
NAAC with A+
ANS: s(p)≥3(n+1)
Presidency College
(Autonomous)
Reaccredited by
NAAC with A+
Presidency
Group
Example 2:
Algorithm fact(n)
Presidency College
(Autonomous)
{
if (n==0) then
Return 1
Reaccredited by
NAAC with A+
else
Return (n* fact(n-1))
}
Presidency
Group
ANS: s(p)≥2(n+1)
PRESIDENCY COLLEGE
Presidency College
(Autonomous)
(Autonomous)
Time complexity
Reaccredited by
NAAC with A+
Presidency
Group
Presidency College
Definition
(Autonomous)
Reaccredited by
NAAC with A+
• Operation counts
• Step counts
Presidency
• Asymptotic notations
Group
Operation counts
Presidency College
(Autonomous)
Reaccredited by
What is basic operation?
NAAC with A+
• The operation that contributes most towards
the running time of the algorithm is called
basic operation.
Presidency
Group
• A statement that executes maximum number
of times in a function is called basic
operation.
Importance of basic operation
Presidency College
(Autonomous)
• The number of times basic operation is
executed depends on the size of the input.
• Basic operation is the time consuming
operation in the algorithm.
• The running time is given by:
Reaccredited by
NAAC with A+
T(n) ~ b*c(n)
T -> is the running time of the algorithm.
n-> the size of the input.
Presidency
Group b-> execution time for basic operation.
c-> represent number of times the basic operation is
executed.
Examples
Presidency College
(Autonomous)
A statement present in the innermost loop.
For (i=0;i<n;i++)
if(a[i]<a[j]) --------basic operation
Addition operation while adding two matrices.
For (i=0;i<n;i++)
for(j=0;j<n;j++)
Reaccredited by c[i][j]=a[i][j]+b[i][j]--------basic operation
NAAC with A+
Search for a key in an array.(key comparisons)
Sorting using bubble sort.(comparison of successive
elements) .
Presidency
Multiplication of two matrices ,in this both
Group
multiplication and addition is performed since
multiplication takes longer this is considered to be
the basic operation.
orders of growth
Presidency College
(Autonomous)
• The behavior of some algorithm changes with
increase in value of n. the change in behavior of
the algorithm and algorithm efficiency can be
analyzed by considering the highest order of n.
• The order of growth is normally determined for
larger values of n.
Reaccredited by
NAAC with A+
2 1 2 2 4 8 4 2
Reaccredited by 4 2 4 8 16 64 16 24
NAAC with A+
i=1
n
2. ∑ 1 = n
i=1
n -1
3. ∑ 1 = n-1
Reaccredited by
NAAC with A+
1
n
4. ∑ n = n + n + ------ + n = n.n = n2
Presidency
Group
i=1
n
5. ∑ i = 1+2+3 ------+n = n(n+1)/2 ≈ ½ n2
i=1
n-1
6. ∑ i = 1+2+3 ----- --------- +(n-1) = n(n-1)/2 ≈ ½ n2
i=1
Presidency College
n
(Autonomous)
7. ∑ i2 = 12 + 22 + ----------n2 = n(n+1)(2n+1)/6 ≈
(1/3)n3
i=1
n
8. ∑
i=1ik = 1k + 2k + ----------- nk ≈ nk+1/k+1
Reaccredited by
n
NAAC with A+
9. ∑
i=0a = 1+a+a +-----------------+a =a -1 / a-1 (a≠1)
i 2 n n+1
n
Presidency
Group
10. ∑ 2
i=0
i
= =2n+1
-1
n
11. ∑ 1/i = 1+ 1/2+ 1/3+ ------------- +1/n ≈ log(n)+ y
i=1
n
12. ∑ log i ≈ n log n
i=1
Presidency College
(Autonomous)
n
13. ∑ 2i -1 = 2 + 22 + 23 -------- 2k-1 + 2k = 2k – 2
i=1
n
14. ∑ 2i = 2i – 2
i=1
Reaccredited by
NAAC with A+
Presidency
Group
Logarithmic equations
Presidency College
(Autonomous)
1. Log a1 = 0
2. Log aa = 1
3. Log ax < 0
4. Log ax >1
Reaccredited by
NAAC with A+
5. Log a(xy )= (Log ax) + (Log ay)
6. Log a(x/y) =(Log ax) - (Log ay)
Presidency
Group
7. Log axz = zLog ax
8. Log aa2 = 2
9. aLogax = x
Exponential equations
Presidency College
(Autonomous)
1. a0 = 1
2. a1 = a
3. a-1 = 1/a
4. (am)n= amn
5. am.an = am+n
Reaccredited by
NAAC with A+
Presidency
Group
Examples on step count
Algorithm sum(a,n)
Presidency College
(Autonomous) {
s=0.0
For (i=1 to n do)
{
s=s+a[i]
Reaccredited by
NAAC with A+
}
Return s; T(p)=2n+3
Presidency
}
Group
Presidency College
Example 2
(Autonomous)
Algorithm add(a,b,c,m,n)
{
for i=1 to m do
{
Reaccredited by
NAAC with A+
for j=1 to n do
{
c[i,j]=a[i,j]+b[i,j]
T(p)=2mn+2m+1
Presidency
Group }
}
}
Example 3
Algorithm fibo(n)
Presidency College
{
(Autonomous)
if(n<=1) then
write n
else
{
f2=0;f1=1;
{
Tfibo = n<=1 2
for i=2 to n do If n>1 4n+1
Reaccredited by
NAAC with A+
{
f3=f1+f2
f1=f2
f2=f3
Presidency
Group
}
write (f3)
}
}
Recursive algorithm example
Presidency College
(Autonomous)
Example 4
Algorithm Rsum(a,n)
{
Reaccredited by if(n<=0) then {
Trsum= 2 if n =0
NAAC with A+
2 + Trsum(n-1) if n>
return 0.0 = 2(n+1)
else
Presidency
Group
return( Rsum( a,n-1 )+a[n]);
}
Example 5:
Presidency College
Algorithm fact(n)
(Autonomous)
{
if (n==0) then {
Tfact= 2 if n =0
return 1 2 + Tfact(n-1) if n>0
Reaccredited by = 2(n+1)
NAAC with A+
else
return (n* fact(n-1))
Presidency
Group
}
Example 6:
Algorithm fibon(n)
Presidency College
(Autonomous)
// input the number of elements
// output Fibonacci series
{
if n==0 or n==1
return 0;
Reaccredited by
NAAC with A+
Else
If n==2
return 1
Presidency
Group
else
return fibon(n-1) + fibon(n-2)
}
Asymptotic notations
Presidency College
(Autonomous) • The word asymptotic means the study of
functions of parameters ‘n’, as n grows larger and
larger without bound . i.e the runtime of
algorithm increases with the size of the input.
• In this we are concerned with how the running
time increases when the size of i/p increases.
• The value of function may increase or decrease
Reaccredited by as the value of n increases. Based on the order of
NAAC with A+
growth of n the behavior of the function varies.
• Asymptotic notations are the notations using which
two algorithms can be compared with respect to
efficiency based on the order of growth of an
algorithm.
Presidency
Group
Different asymptotic notations
Presidency College
(Autonomous)
• O ( Big Oh)
• ( Big Omega)
• Θ ( Big Theta)
Reaccredited by
NAAC with A+
Presidency
Group
Bigh Oh(O)
Presidency College
Landau notation.
• This was introduced by number theorist Paul
bachman.
• It is often used to describe how the size of the
Reaccredited by input data affects an algorithm running time.
NAAC with A+
• It is a measure of the longest amount of time that
it could possibly take for the algorithm to
complete execution.
Presidency
Group • Bigoh notation gives upper bound on a function
f(n).the upper bound on f(n) indicates that the
function f(n) will be the worst case that it does
not consume more than this computing time.
Contd ..
Presidency College
• Let n indicates the size of input and g
(Autonomous)
(n) is a function Big Oh is defined as a
set of functions with a small or same
order of growth as g (n) as n goes to
infinity.
• Let f (n ) be the time efficiency of an algorithm . the
Reaccredited by
NAAC with A+ function f (n) is said to be O (g (n)) denoted as
F (n) € O (g (n))
• If c is positive constant n0 is positive integer then
Presidency F (n) ≤ c*g (n) for all n≥ n0
Group
Presidency College
(Autonomous)
Reaccredited by
NAAC with A+
Presidency
Group
Problem’s on BigOh notation
Presidency College
Reaccredited by
NAAC with A+
Presidency
Group
• Always big Oh gives upper bound i.e worst
Presidency College
case.
• Always big omega gives lower bound i.e best
(Autonomous)
case.
Example:
• find max min element worst case is O(n) and
best case is (n).
Reaccredited by • In linear search
NAAC with A+
a. for key found case best case (1) and
worst
Presidency
case is O(n).
Group
b. For key not found case best case (n) and
worst case is O(n).
Problem‘s on big omega( ) notation
Presidency College
(Autonomous)
Reaccredited by
NAAC with A+
Presidency
Group
Problem’s on big theta (Θ)notation
Presidency College
(Autonomous)
5.
NAAC with A+
Let f(n)=6*2n+n2, p.t f(n)= Θ(2n)
Presidency
Group
Mathematical analysis of Non-
Presidency College
recursive Algorithms
Steps:
(Autonomous)
Presidency
4. Using the formula, establish f(n). Once we know
Group
f(n), finding its order of growth or the complexity
is straight forward.
Mathematical analysis of non-
Presidency College
recursive algorithms
(Autonomous)
The following steps specify the general plan for
analyzing time efficiency of non recursive
algorithms
1. Decide on the parameters indicating input size.
2. Identify the basic operations in the algorithm.
3. Check whether the number of times the basic
Reaccredited by
operation is executed depends only on the size
NAAC with A+ of an input. If it also depends on some additional
property , the worst case , average case and best
case efficiencies has to be determined.
4. Obtain the total number of times a basic
Presidency
Group operation is executed.
5. Using standard formulae and rules , find the
order of growth.
Examples on non recursive
algorithms
Presidency College
(Autonomous)
Example 1:
Algorithm : MaxElement(A[0:n-1])
// to find the largest element in an array
// input: An array A[0: n-1] of real numbers
// output: value of largest element of array A.
Reaccredited by
{
NAAC with A+
max=A[0]
for(i=1 to n-1 do)
if (A[i]>max)
Presidency
Group
max=A[i]
return (max)
}
Example 2
Presidency College
(Autonomous)
Algorithm : Uniqueness (A[0 : n-1])
// to check whether there are any duplicates .
// Input: An array A[0 : n-1]
// output: Return ‘1’ if all the elements are distinct and ‘0’ if not.
{
for (i=1 to n-1 do)
{
Reaccredited by for(j=i+1 to n do)
NAAC with A+
{
if ( A[i]==A[j])
return 0
}
Presidency
Group }
return 1
}
Example 3:
Presidency College
(Autonomous)
Algorithm: Matrix Multiplication (A[0:n-1],B[0:n-
1],C[0:n-1])
// Multiplication of two n x n matrices A and B
// Input : Two n X n matrices A and B.
// output : Another n X n matrix c=AB
{
Reaccredited by
NAAC with A+
for i=0 to n-1 do
for j= 0 to n-1 do
c[i,j]=0
for k=1 to n do
Presidency
Group
c[i,j]= c[i,j]+A[i,k]*B[k,j]
}
Mathematical analysis of
Presidency College
recursive Algorithms
(Autonomous)
Presidency
Group
Steps to be followed for analyzing
Presidency College
time efficiency of recursive algorithm
(Autonomous)
Algorithm Fact(n)
//compute n factorial for non-negative number
{
Reaccredited by
NAAC with A+
if n==0
return 1;
Presidency
else
Group
return n*Fact(n-1);
}
EXAMPLE 2: Fibonacci
Presidency College
(Autonomous)
Algorithm: Fibonacci(n)
// input : A non negative number
// output: nth element of the Fibonacci series
{
if n=0 || n=1
Reaccredited by
NAAC with A+ return n;
else
Presidency
return (Fibonacci(n-1) + Fibonacci(n-
Group
2));
}
EXAMPLE 3: Tower of Hanoi
Presidency College Algorithm: ToweroHanoi(n)
(Autonomous)
// input : Number of discs n
// output: sequence of moves
{
if n==1
Reaccredited by
NAAC with A+ move disk from source to destination(s to d)
else
ToweroHanoi(n-1,s,d,t)
Presidency
Group
move disk from source to destination(s to d)
ToweroHanoi(n-1,t,s,d)
}