Lecture 3. DAA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

Performance Evaluation

Time complexity of an algorithm can be


calculated by using two methods:
– Posteriori Analysis
– Priori Analysis
A priori analysis
• It is independent of language of compiler and types of
hardware.
• It will give approximate answer.
• It uses the asymptotic notations to represent how much
time the algorithm will take in order to complete its
execution.
• The time complexity of an algorithm using a priori analysis
is same for every system.
• If the algorithm running faster, credit goes to the
programmer.
• It is done before execution of an algorithm.
• It is cheaper than Posteriori Analysis.
A Posteriori analysis
• It is dependent on language of compiler and type of
hardware.
• It will give exact answer.
• It doesn’t use asymptotic notations to represent the time
complexity of an algorithm.
• The time complexity of an algorithm using a posteriori
analysis differ from system to system.
• If the time taken by the program is less, then the credit will
go to compiler and hardware.
• It is done after execution of an algorithm.
• It is costlier than priori analysis because of requirement of
software and hardware for execution.
Space Complexity
When we design an algorithm to solve a
problem, it needs some computer memory to
complete its execution.
Components of space complexity

There are three components of space complexity:


• Instruction or Code Space:
– This space holds the collected version of the program
instructions.
• Data space:
– This space holds all the static data such as constant,
variables and dynamic data such as growing structure.
• Environment Stack Space:
– This space is used to store return address for the functions.
So that, execution can begin again where they left earlier
and it may also be used for parameters.
Space Complexity
Space Complexity

S(P) = c + Sp
Example #1
Algorithm abc(a,b,c)
{
return a+b+b*c+(a-c)/(a+b) +2.0;
}
Example #1
Algorithm abc(a,b,c)
{
return a+b+b*c+(a-c)/(a+b) +2.0;
}

S(P) = c + Sp
Sp = 0
Example #2
Algorithm sum(a,n)
{
s = 0.0;
for i:=1 to n do
s:=s+a[i];
return s;
}
Example #2
Algorithm sum(a,n)
{
s = 0.0;
for i:=1 to n do
s:=s+a[i];
return s;
}
s(n) >= n+3
n for a[ ] , one for each n,i,s
Example #3
algorithm rsum (a,n)
{
if (n<=0) then return 0.0
else
return rsum(a,n-1)+a[n];
}
Example #3
algorithm rsum (a,n)
{
if (n<=0) then return 0.0
else
return rsum(a,n-1)+a[n];
}
• Instance characterized by n
• Recursive stack space required at each level
•(n, return address,*a[]) 3 words
• Depth of recursion considering first invocation - (n+1)
• S(n) >= 3*(n+1)
Time Complexity

• Amount of computer time needed for the


algorithm to run to completion
• T(P) = C(P) + R(P)
• C(P) is independent of problem instance
and is always not required (recompilation)
• So usually only R(P) is considered
Apriori Measures
• Instruction count
– Basic operations
– Basic steps
• Size of input.
Computing Instruction Count

• Initialization instructions
• Loops.
– Number of passes of the loops
• Count the basic operations/steps
Basic Steps - Example

• Search
– Basic operation is compare x and y
• Matrix multiplication
– Basic operation is multiplication of real numbers
Counting Steps

• Time complexity can also be expressed in


terms of the basic steps performed.
• Expressed as a function of instance
characteristics
– input size
– output size etc .
Step in Algorithm
A Program Step is loosely defined as a
syntactically meaningful segment of program
that has an execution time that is independent of
the instance characteristics
• Example
– 10 or 100 additions can be one step
– 200 multiplications can be another step
Step Count
• Count the exact number of steps needed for
an algorithm as a function of the problem size
• Each atomic operation is counted as one step:
– Arithmetic operations
– Comparison operations
– Other operations, such as “assignment” and
“return”
Counting Steps
Implementation
• Assignment – count 1
• For, while, repeat loops
– Using a variable count
– Using steps per execution of instructions
Use of a variable count

• Use a variable count to store step count and


increment it wherever necessary
• Display count
Example 1
Algorithm sum(a,n)
{
s = 0.0;
for i: =1 to n do
{
s:= s + a[i];
}
return s;
}
Example 1
Algorithm sum(a,n)
{
s=0.0;
count:=count+1;
for i: =1 to n do
{
count:=count+1; //For for
s:=s+a[i];
count:=count+1; //For assignment
}
count:=count+1; //For last time of for
count:count+1; //For return
return s;
}
count = 2n+3
Example 2
algorithm rsum(a,n)
{
count:=count+1;
if (n<=0) then
{ count:=count+1;
return 0.0; }
else
{ count:=count+1;
return rsum(a,n-1)+a[n];} }
Count ?
Example 2
Algorithm Rsum (a,n)
{
if (n<=0) then return 0.0
else
return rsum(a,n-1)+a[n];
}
Example 2
Algorithm Rsum (a,n)
{ count = count +1 //For the if statement
if (n<=0) then
{ count = count+1 //for the return statement
return 0.0
}
else
{ count = count +1 //For the addition, function
invocation and return
return rsum(a,n-1)+a[n];
}
Example 2

Recursive formula
Example 3

algorithm add(a,b,b,m,n)
{
for i:=1 to m do
for j:=1 to n do
c[i,j]:=a[i,j]+b[i,j];
}
Example 3
algorithm add(a,b,b,m,n)
{ Count = count + 1 // For the i loop
for i:=1 to m do
{ count = count +1 //For the j loop
for j:=1 to n do
{ count = count + 1 //For the ij loop
c[i,j]:=a[i,j]+b[i,j];
} count = count + 1 //for the last time of j loop
}
count =count + 1 //for the last time of i loop
}
Example 3
algorithm add(a,b,b,m,n)
{ Count = count + 1 // For the i loop
for i:=1 to m do
{ count = count +1 //For the j loop
for j:=1 to n do
{ count = count + 1 //For the ij loop
c[i,j]:=a[i,j]+b[i,j];
} count = count + 1 //for the last time of j loop
}
count =count + 1 //for the last time of i loop
}
Example 3
algorithm add(a,b,b,m,n)
{ Count = count + 1 // For the i loop
for i:=1 to m do
{ count = count +1 //For the j loop
for j:=1 to n do
{ count = count + 1 //For the ij loop
c[i,j]:=a[i,j]+b[i,j];
} count = count + 1 //for the last time of j loop
}
count =count + 1 //for the last time of i loop
} m + mn + mn + m + 1 = 2mn + 2m + 1
Computing Step Count from s/e
• Each statement will have an (s/e) steps/execution
depending on the type of statement
• Frequency of each statement is multiplied with the
corresponding s/e of that statement to get the step count
for that statement.
• Statement Step counts are added to give the total count
Example 1

s/e freq total


Algorithm sum(a,n)
1 1 1
{ s=0.0;
1 n+1 n+1
for i: =1 to n do
1 n n
{ s:=s+a[i];
1 1 1
}
-------
return s;
2n+3
}
Example 2

algorithm add(a,b,b,m,n) S/e freq Total


{for i:=1 to m do 1 m+1 m+1
for j:=1 to n do 1 m(n+1) m(n+1)
c[i,j]:=a[i,j]+b[i,j]; } 1 mn mn
----------
2mn+2m+1
Example 3
s/e freq freq total total
algorithm rsum(a,n) n=0 n>0 n=0 n>0
{if (n<=0) then 1 1 1 1 1
1 1 0 1 0
{ return 0.0; }
else
{return rsum(a,n-1)+a[n]; 1+x 0 1 0 1+x
}} -----------------
2 2+x
Computing Complexity
X = trsum(n-1)
trsum(n)= { 2 2+ trsum(n-1) if
if
n=0
n>0

Known as a recurrence relation.


Solving the Recurrence
trsum(n)=2+ trsum(n-1)
= 2+2+ trsum(n-2)
= 2*2+ trsum(n-2)
= 2*3+ trsum(n-3)
----------
=2*n + trsum(n-n)
= 2n+2 n>=0

You might also like