DAA-Unit-I - Part B
DAA-Unit-I - Part B
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.
Example1
Algorithm sum(a,n)
{
s:=0;
for i:=1 to n do
s:=s+a[i];
return s;
}
• The amount of space depends on the value of n.
Therefore, Ssum(n) = n
Time Complexity:
T(P)=C+TP(I)
Example :
c := a + b;
sum := sum + a[i];
• Method to compute the step count
Tabular method
• Determine the number of steps contributed by each statement per
execution frequency ( number of times statement is executed ).
• Add up the contribution of all statements
• Ex:- Iterative sum of n numbers
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
Asymptotic Notations
• Asymptotic notation describes the behavior
of functions for the large inputs.
• Big Oh(O) notation:
– f(n) = 10n2+4n+2
• 10n2+4n+2 <= 11n2, for all n >= 5, 10n2+4n+2 = (n2)
• Omega () notation:
– The omega notation specifies lower bound for
the growth rate of the function f.
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.
• 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)
• Theta () notation:
– The Theta notation specifies the tight
upper and lower bounds for the growth
rate of the function f.
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.
• 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)
Big-Oh, Theta, Omega and Little-oh
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)