2 Performance Analysis
2 Performance Analysis
Session
Session- -22
12/30/24 1
CREATED BY K. VICTOR BABU
AIM OF THE SESSION
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
12/30/24 2
CREATED BY K. VICTOR BABU
Recursive and Non-Recursive Algorithms
12/30/24 3
CREATED BY K. VICTOR BABU
Example: Towers of Hanoi problem
12/30/24 4
CREATED BY K. VICTOR BABU
Algorithm TowersOfHanoi(n, x, y, z)
{
If (n ≥ 1) then
{
TowersOfHanoi(n-1, x, z, y);
Write(“move top disk from tower” , x, “to
12/30/24 5
CREATED BY K. VICTOR BABU
Performance Analysis:
12/30/24 6
CREATED BY K. VICTOR BABU
Time Complexity
Is the amount of time it needs to run to completion
• 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).
T(P)=C+TP(I)
• The compile time does not depend on the instance characteristics.
• We will concentrate on estimating run time Tp(I).
12/30/24 7
CREATED BY K. VICTOR BABU
• Methods to compute Time Complexity
• 1) Step Count Method
• 2) Tabular Method
• Introduce global variable count into programs with initial value zero.
• Statements to increment count by the appropriate amount are introduced into the program.
• The value of the count by the time program terminates is the number steps taken by the program.
12/30/24 8
CREATED BY K. VICTOR BABU
Step Count Method
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
}
12/30/24 10
}
CREATED BY K. VICTOR BABU
• 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.
12/30/24 11
CREATED BY K. VICTOR BABU
•One way of solving such recursive formula is by using substitution method.
tRSum (n) = 2+tRSum(n-1)
= 2+2+tRSum(n-2)
= 2(2)+tRSum(n-2)
= 2+2+2+tRSum(n-3)
= 3(2)+tRSum(n-3)
:
:
= n(2) +tRSum(n-n)
=2n+tRSum(0)
= 2n+2
12/30/24 12
The step count for Rsum is 2n+2
CREATED BY K. VICTOR BABU
Tabular method
• Determine the total number of steps contributed by each
statement per execution frequency
12/30/24 13
CREATED BY K. VICTOR BABU
Method-II: Tabular method
Total 2n+3
12/30/24 14
CREATED BY K. VICTOR BABU
• EX:- 2) Recursive sum of n numbers
Total 2 2+x
x=tRSum(n-1)
12/30/24 15
CREATED BY K. VICTOR BABU
Space Complexity
Space Complexity
Amount of computer memory that is required during program execution as a function of
input size.
OR
Space complexity is the amount of memory it needs to run to completion
Fixed: varies from problem to problem. Includes space needed for storing instructions
variables , constants and structured variables.[arrays , struct].
Variable: varies from program to program. Includes space needed for stack and for
structured variables that are dynamically allocated during run time.
12/30/24 16
CREATED BY K. VICTOR BABU
S(P)=C+SP (I)
Fixed Space Requirements (C)
Independent of the characteristics of the inputs and outputs
instruction space(space for code)
space for simple variables, fixed-size structured variable, constants
12/30/24 17 17
CREATED BY K. VICTOR BABU
Example 1
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
12/30/24 18
CREATED BY K. VICTOR BABU
Example 2
Algorithm sum(a,n)
{
s:=0;
for i:=1 to n do
s:=s+a[i];
return s;
}
The amount of space needed depends on the value of n.
Therefore, Ssum(n)>=(n+3)
12/30/24 19 19
CREATED BY K. VICTOR BABU
Example 3
Algorithm RSum(a,n)
{
if(n ≤ 0) then return 0;
else return RSum(a,n-1)+a[n];
}
Total no.of recursive calls n, therefore SRSum (n)>=3(n+1)
12/30/24 20 20
CREATED BY K. VICTOR BABU
SUMMARY
Performance analysis helps us to select the best algorithm from multiple algorithms
to solve a problem.
When there are multiple alternative algorithms to solve a problem, we analyze them
and pick the one which is best suitable for our requirements.
Time complexity of an algorithm quantifies the amount of time taken by an algorithm
to run as a function of the length of the input.
Space complexity of an algorithm quantifies the amount of space or memory taken
by an algorithm to run as a function of the length of the input.
12/30/24 21
CREATED BY K. VICTOR BABU
SELF-ASSESSMENT QUESTIONS
• • For moving 10 disks in towers of Hanoi problem, total moves required are
For moving 10 disks in towers of Hanoi problem, total moves required are
A. 1023
B. 512
C. 1024
D. 511
A. O(1)
B. O(n)
C. O(n log n)
D. O(n^2)
12/30/24 22
CREATED BY K. VICTOR BABU
TERMINAL QUESTIONS
12/30/24 23
CREATED BY K. VICTOR BABU
REFERENCES FOR FURTHER LEARNING OF THE SESSION
Text Books :
1. Ellis Horowitz, Sartaj Sahni and Sanguthevar Rajasekaran, “Fundamentals of
ComputerAlgorithms”, 2nd Edition, University Press, 2008.
2. Cormen, Leizerson &Rivest, “Introduction toalgorithms”, 3rd Edition, Prentice-Hall, 2002.
3. Jon Kleinberg and Eva Tardos, “Algorithm Design”,Pearson Education, 2006.
Reference Books :
1. Robert Sedgewick and Kevin wayne , “Algorithms”, 4th edition, Addison WesleyProf.,(2011).
2. Anny Levitin, “Introduction to Design and Analysis of Algorithms”, 2rd Edition,
PersonEducation Press. (2007).
3. Michael T.Goodrich and Roberto Tamassia, Algorithm Design: Foundations,Analysis and
Internet Examples, Second Edition, Wiley-India, (2006).
4. Steven S. Skiena, “The AlgorithmDesign Manual”, Second Edition, Springer, (2008)
MOOCS :
1. https://www.coursera.org/specializations/algorithms?=
2.https://www.coursera.org/learn/dynamic-programming-greedy-algorithms#modules
12/30/24
CREATED BY K. VICTOR BABU
24
THANK YOU
12/30/24 25
CREATED BY K. VICTOR BABU