0% found this document useful (0 votes)
7 views16 pages

DAA-Unit-I - Part B

Uploaded by

Hemanth Kumar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

DAA-Unit-I - Part B

Uploaded by

Hemanth Kumar1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Unit-I

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.
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)

– Time Complexity, T(P) is the sum of its compile time C


plus its run time, TP(I).

– Compile time does not depend on the instance


characteristics( Number of inputs ).

– We will concentrate on estimating run time Tp(I).


Contd..
Program step
program step is a program statement whose
execution time is independent of the number of inputs.

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

Statement s/e frequency Total steps


(s/e X frequency)

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:

– The big oh notation specifies an upper


bound for the growth rate of the function
f.
Definition: [Big “oh’’]
– f(n) = O(g(n)) (read as “f of n is big oh 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 <= 4n, for all n >= 2,  3n + 2 =  (n)

– 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)

• Think of Ω(g(n)) as “greater than or equal to” g(n)


– Lower bound: “grows faster than or same rate as” g(n)

• Think of Θ(g(n)) as “equal to” g(n)


– “Tight” bound: same growth rate

• Think of o(g(n)) as “less than to” g(n)


– Strict Upper bound: “grows slower than ” g(n)

• (True for large N)

You might also like