DAA-Unit-I - Part A & B
DAA-Unit-I - Part A & B
Introduction:
Algorithm, Pseudo code for expressing algorithms,
Performance Analysis-Space complexity, Time
complexity,
Asymptotic Notation- Big oh notation, Omega notation,
Theta notation and Little oh notation,
Probabilistic analysis,
Amortized analysis.
Algorithm
• Definition
An algorithm is a set of steps used to solve a specific
problem.
Pseudo code for expressing algorithms
We present most of our algorithms using pseudo code that
looks like C and Pascal code.
9. Input and output are done using the instructions read and
write.
10. Procedure or function starts with the word
Algorithm.
General form :
Algorithm Name( <parameter list> )
{
body
}
where Name is the name of the procedure.
– Simple variables to functions are passed by value.
– Arrays and records are passed by reference.
Ex:-Algorithm that finds and returns the
maximum of n given numbers.
Algorithm max(a,n)
// a is an array of size n
{
result:=a[1];
for i:=2 to n do
if ( a[i] > result ) then
result:=a[i];
return result;
}
Ex:-Write an algorithm to find sum of n numbers
Algorithm sum(a, n)
// a is an array of size n
{ sum:=0;
for i :=1 to n do
{
sum := sum+a[i];
}
return sum;
}
Space Complexity
• The space needed by an algorithm is called a space complexity.
• It has the following components.
– Instruction space / Program space
• It does not depend on the number of inputs. (Instance characteristics
of a problem ).
– Data space
• Constants & simple variables – Don’t depend on the number of
inputs.
• Dynamically allocated objects - Depends on the number of inputs.
• Stack space - Generally does not depend on the number of inputs
unless recursive functions are in use.
Recursive algorithm
Algorithm rfactorial(n)
// n is an integer
{ fact=1;
if(n=1 or n=0) return fact;
else
fact=n*rfactorial(n-1);
return fact;
}
Note : Each time the recursive function is called, the current values
of n, fact and the address of the statement to return to on
completion are saved on the stack.
Execution
n: 2
R: n: 3
int rfactorial(n){
void main() int rfactorial(n){
fact=1
{int fact=1;
if (n=0 or n=1)return fact
R=rfactorial(3); if (n=0 or n=1)return fact;
else
printf(R);} else
fact=n*rfactorial(n-1);
fact=n*rfactorial(n-1);
return fact;
return fact;
}
}
location to return n: 1
fact 1 fact=1
n 1
int rfactorial(n){
fact=1
4000 if (n=0 or n=1)
fact 1 fact=2*
n 2
return 1;
else
3000 fact=n*rfactorial(n-1);
fact 1
3
return fact;
n fact=3*
}
2000
R Stack
Contd..
Therefore, We can divide the total space required by a
program into two parts:
i) Fixed Space Requirements (C)
Independent of the characteristics of the problem instance ( I )
• Instruction space
• Space for simple variables and constants.
ii) Variable Space Requirements (SP(I))
depend on the characteristics of the problem instance ( I )
• Dynamically allocated objects (Number of inputs associated with
I)
• Recursive stack space ( formal parameters, local variables,
return address ).
– Therefore, the space requirement of any problem P
can be written as
S(p)=C +Sp( Instance characteristics ).
Note:
– We concentrate only on estimating
Sp (Instance characteristics ).
Therefore, Ssum(n) = n
Example3
Algorithm RSum(a,n)
{
if(n ≤ 0) then return 0;
else return RSum(a,n-1)+a[n];
}
Example :
c := a + b;
sum := sum + a[i];
• Comments are counted as zero number of steps.
• The control parts for for and while statements have the
following forms:
for i:= <expr1> to <expr2> do
while ( <expr> ) do
• Each execution of the control part of a while statement is one,
unless <expr> is a function of instance characteristics.
Algorithm sum(a, n) 0 -- 0
{ 0 -- 0
s:=0 ; 1 1 1
for i:=1 to n do 1 n+1 n+1
s:=s+a[i]; 1 n n
return s; 1 1 1
} 0 -- 0
Total 2n+3
• EX:- Addition of two m×n matrices
Algorithm Add(a,b,c,m, n) 0 -- 0
{ 0 -- 0
for i:=1 to m do 1 m+1 m+1
for j:=1 to n do 1 m(n+1) mn+m
c[i,j]:=a[i,j]+b[i,j] ; 1 mn mn
} 0 -- 0
Total 2mn+2m+1
Best, Worst, Average Cases
1. Best-Case:-
Minimum number of steps taken by the algorithm
for the given inputs.
2. Worst-Case:-
Maximum number of steps taken by the algorithm
for the given inputs.
3.Average-Case:-
Average number of steps taken by an algorithm.
Best, Worst, Average Cases
The number of steps taken by the algorithm depends on the
input.
Ex:
Linear search.
i.e., we say that n2 + 100n + log10n + 1000 and n2 have the same
rate of growth
– f(n) = 10n2+4n+2
• 10n2+4n+2 <= 11n2, for all n >= 5, 10n2+4n+2 = (n2)
Definition: [Omega]
– f(n) = (g(n)) (read as “f of n is
omega of g of n”) iff there exist
positive constants c and n0 such
that f(n) cg(n) for all n,
n n0.
• The definition states that the function f(n) is at least c times
the function g(n) except when n is smaller than n0.
• Examples
– f(n) = 3n+2
• 3n + 2 >= 3n, for all n >= 1, 3n + 2 = (n)
– f(n) = 10n2+4n+2
• 10n2+4n+2 >= n2, for all n >= 1, 10n2+4n+2 = (n2)
• It also possible to write 10n2+4n+2 = (n) since 10n2+4n+2 >=n for n>=0
• Although n is a lower bound for 10n2+4n+2, it is not a tight
lower bound; we can find a larger function (n2 )that satisfies
omega relation.
Definition: [Theta]
– f(n) = (g(n)) (read as “f of n is theta
of g of n”) iff there exist positive
constants c1, c2, and n0 such that
c1g(n) f(n) c2g(n) for all n, n n0.
• The definition states that the function f(n) lies between c1 times the function
g(n) and c2 times the function g(n) except when n is smaller than n0.
• In other words, f(n) grows same rate as” g(n).
• Examples:-
– f(n) = 3n+2
• 3n <= 3n + 2 <= 4n, for all n >= 2, 3n + 2 = (n)
– f(n) = 10n2+4n+2
• n2<= 10n2+4n+2 <= 11n2, for all n >= 5, 10n2+4n+2 = (n2)
• But, we can not write either 10n2+4n+2= (n) or 10n2+4n+2= (n3), since
neither of these will satisfy the theta relation.
• Little Oh(O) notation:
– The little oh notation specifies a strict
upper bound for the growth rate of the
function f.
Lim f(n) =0
n->∞ g(n)
• The definition states that the function f(n) is less than c times the
function g(n) except when n is smaller than n0.
• In other words, f(n) grows slower than” g(n).
• Examples
– f(n) = 3n+2=o(n2)
Lim 3n+2
= 0
since n->∞ n2
Tips :
• Think of O(g(n)) as “less than or equal to” g(n)
– Upper bound: “grows slower than or same rate as” g(n)
Function Name
1 Growth is constant
logn Growth is logarithmic
n Growth is linear
nlogn Growth is n-log-n
n2 Growth is quadratic
n3 Growth is cubic
2n Growth is exponential
n! Growth is factorial