Unit 1
Unit 1
Unit-I
Algorithm
• Definition
An algorithm is a finite set of instructions that
accomplishes a particular task.
All algorithms must satisfy the following criteria.
• Criteria
1. Input : Zero or more quantities are externally
supplied.
2. Output : At least one quantity is produced.
3. Definiteness : Each instruction is clear and
unambiguous.
Statements such as “add 6 or 7 to x”
or “Compute 5/0” are not permitted”
4. Finiteness : The algorithm should terminate after
a finite number of steps.
5. Effectiveness : Instruction is basic enough to be
carried out.
Pseudo code for expressing algorithms
• We present most of our algorithms using pseudo code that
resembles C and Pascal.
3.
i. An identifier begins with a letter.
ii. The data types of variables are not explicitly declared.
iii. The types will be clear from the context.
iv. Whether a variable is global or local to a procedure will also be evident from
the context.
v. We assume simple data types such as integer, float, char, boolean, and so
on.
vi. Compound data types can be formed with records.
node = record
{ C style :-
repeat
<statement 1>
:
<statement n>
until ( condition )
General form :
Algorithm Name( <parameter list> )
{
body
}
where Name is the name of the procedure.
– Simple variables to procedures 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 sort an array of n
integers using bubble sort.
Algorithm(a,n)
// a is an array of size n
{
for i:=1 to n-1 do
{
for j:=1 to n-i do
{
if( a[j] >a[j+1] ) then
t=:a[j]; a[j]:=a[j+1]; a[j]:=t;
}
}
}
Performance Analysis
(machine independent)
• There are many things upon which the
performance will depend.
Think
Think
Think
Space Complexity
• The space needed by a program has the
following components.
– Instruction space
• Instruction space is the space needed to store the
compiled version of the program instructions.
• The amount of instruction space that is needed
depends on the compiler used to compile the
program into machine code.
– Data space
• Data space is the space needed to store all
constant and variable values. Data space has two
components.
– Space needed by constants( ex; 1 and 2 in max of n num
algorithm) and simple variables( such as i, j, n etc).
– Space needed by a dynamically allocated objects such
as arrays and class instances.
• Space taken by the variables and constants varies
from language to language and platform to
platform.
– Environmental stack space
• The environment stack is used to save information
needed to resume execution of partially completed
functions.
• Each time a function is invoked the following data
are saved on the environment stack.
– The return address .
– The values of all local variables and formal parameters in
the function being invoked( necessary for recursive
functions only).
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;
}
Each time the recursive function rfactorial is invoked,
the current values of n and fact and the program location to return to
on completion are saved in the environment stack.
Execution n: 2
R: n: 3
int rfactorial(n){ //3500
void main() int rfactorial(n){ //2500
fact=1
{int R=rfactorial(3); fact=1; if (n=0 or n=1)return fact
printf(R);} //2000 if (n=0 or n=1)return fact; else
else
fact=n*rfactorial(n-1); //4000
fact=n*rfactorial(n-1); //3000
return fact;}
return fact;
}
R n=3
R=6 fact=6
fact=1 n: 1
2000 n=2
3000 int rfactorial(n){
fact=1 4000
fact=2
fact=1 if (n=0 or n=1)
return 1;
else
fact=n*rfactorial(n-1);
return fact;
}
Stack
Summary of space complexity
• The space needed by a program depends on several
factors.
• We cannot make an accurate analysis of the space
requirements of a program unless we know the computer
or compile that will be used.
• However, we can determine the components that
depend on the characteristics of the problem instance
(e.x., the number of inputs and outputs or magnitude of the numbers
involved ) to be solved.
Algorithm abc(a,b,c)
{
return a+b+b*c+(a+b-c)/(a+b)+4.0;
}
• Problem instance characterized by the specific values of a,b,and c.
• If we assume one word (4 bytes) is adequate to store the values of
each a, b, and c , then the space needed by abc is independent of
the instance characteristics.
Therefore, Sabc( instance characteristics)=0
Example2
Algorithm sum(a,n)
{
Recall: Address of the
s:=0;
first element of the array will
for i:=1 to n do
be passed .
s:=s+a[i];
return s;
}
• Problem instance characterized by n.
• The amount of space needed does depend on the value of n.
• Time Complexity:
T(P)=C+TP(I)
– The time, T(P), taken by a program, P, is the sum of
its compile time C and its run (or execution) time,
TP(I).
– The compile time does not depend on the instance
characteristics.
– We will concentrate on estimating run time Tp(I).
• If we know the characteristics of the compiler to be used, we
can determine the
– No. of additions , subtractions, multiplications, divisions, compares,
and so on.
• Then we can obtain an expression for Tp(n) Of the form
TP(n)=caADD(n)+ csSUB(n)+ cm MUL(n)+ cd DIV(n)+……..
where,
– n denotes the instance characteristics.
– ca, cs, cm, cd and so on denote the time needed for an addition ,
subtraction, multiplication, division and so on. And
– ADD, SUB, MUL, DIV, and so on are functions whose values are the
no.of additions, subtractions, multiplications, divisions, and so on.
• Obtaining such an exact formula is an impossible task,
since time needed for an addition, subtraction, and so
on, depends an numbers being added, subtracted, and
so on.
• The value of Tp(n) for any given n can be obtained only
experimentally.
• Even with the experimental approach, one could face
difficulties.
• In a multi user system the execution time of a program p
depends on the number of other programs running on
the computer at the time program p is running.
Contd..
• As there were some problems in determining the execution
time using earlier methods, we will go one step further and
count only the number of program steps.
program step
program step is loosely defined as a syntactically or
semantically meaningful program segment whose
execution time is independent of the instance
characteristics.
– Example
result = a + b + b * c + (a + b - c) / (a + b) + 4.0;
sum = a + b + c;
• The number of steps assigned to any program statement
depends on the kind of statement.
• Comments are counted as zero number of steps.
• An assignment statement which does not involve any
calls to other functions counted as one step.
• Loops, such as the for, while, and repeat-until, we
consider the step counts only for the control part of the
statement.
• 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.
• The step count for each execution of the control part of a for
statement is one, unless <expr1> and <expr2> are functions of the
instance characteristics.
2n + 3 steps
Note : Step count tells us how the run time for a program changes with
changes in the instance characteristics.
Ex:- Addition of two m×n matrices
Algorithm Add(a,b,c,,m,n)
{
for i:=1 to m do
{
for j:=1 to n do
{
c[i,j]:=a[i,j]+b[i,j];
}
}
2mn + 2m+1 steps
}
EX:- Recursive sum of n numbers
Algorithm RSum(a,n)
{
count:=count+1; // for the if conditional
if(n ≤ 0) then
{
return 0;
count:=count+1; // for the return
}
else
{
return RSum(a,n-1)+a[n];
count:=count+1; // For the addition, function invocation and return
}
}
• When analyzing a recursive program for its step count, we often
obtain a recursive formula for the step count.
• We obtain the following recursive formula for above (RSum)
algorithm.
2 If n=0
tRSum (n)= 2+ tRSum(n-1) If n>0
• One way of solving such recursive formula is by repeated
substitutions for each occurrence of the function tRSum on the right
side until all such occurrences disappear:
Total 2n+3
• EX:- Addition of two m×n matrices
Total 2mn+2m+1
• EX:- Recursive sum of n numbers
Total 2 2+x
x=tRSum(n-1)
Best, Worst, Average Cases
✓ Not all inputs of a given size take the same number of program
steps.
✓ Sequential search for K in an array of n integers:
• Begin at first element in array and look at each element in
turn until K is found.
1. Best-Case Step count:-
Minimum number of steps executed by the algorithm for the
given parameters.
2. Worst-Case Step count:-
Maximum number of steps executed by the algorithm for the
given parameters.
3. Average-Case Step count:-
Average number of steps executed by an algorithm.
Contd..
4 ms
3 ms
Running
Best-case time
Time 2 ms
1 ms
A B C D E F G
Input
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
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.
• In other words,f(n) grows faster than or same rate as” g(n).
• 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)
• But, we can not write 10n2+4n+2 = (n3), since it does not satisfy
the omega relation for sufficiently large input.
• Theta () notation:
– The Theta notation describes a tight bound
on the asymptotic 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.
• 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 big oh notation describes a strict
upper bound on the asymptotic 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)
• Since 3n+2
Lim =0
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)