0% found this document useful (0 votes)
33 views25 pages

2 Performance Analysis

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)
33 views25 pages

2 Performance Analysis

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/ 25

Department of CSE

DESIGN AND ANALYSIS OF ALGORITHMS


23CS2205R
Topic:
PERFORMANCE ANALYSIS

Session
Session- -22

12/30/24 1
CREATED BY K. VICTOR BABU
AIM OF THE SESSION

To familiarize students with the concept of the Performance Analysis.

INSTRUCTIONAL OBJECTIVES

This Session is designed to:


1.Demonstrate :- Recursive Algorithms, Performance Analysis.
2.Describe :- Time Complexity and Space Complexity.

LEARNING OUTCOMES

At the end of this session, you should be able to:


1.Define :- Recursive Algorithms, Performance Analysis
2.Describe :- Recursive algorithms, Time Complexity and Space Complexity
3.Summarize:- Towers of Hanoi and Examples of Time complexity and space complexity

12/30/24 2
CREATED BY K. VICTOR BABU
Recursive and Non-Recursive Algorithms

 A recursive function is a function that is defined in terms of itself with a termination


condition. Similarly an algorithm is said to be recursive if the same algorithm is invoked
in the body. An algorithm that calls itself is direct recursive.
 Recursive sorting algorithms work by splitting the input into two or more smaller
inputs and then sorting those, then combining the results.
 Merge sort, Quick-sort are examples of Recursive function.
 Algorithm A is said to be indirect recursive if it calls another algorithm which in turn
calls A.
 A non-recursive algorithm does the sorting all at once, without calling itself. Bubble-sort
is an example of a non-recursive algorithm.

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

top of tower”, y);


TowersOfHanoi(n-1, z, y, x);
}
}

12/30/24 5
CREATED BY K. VICTOR BABU
Performance Analysis:

Performance analysis of an algorithm depends upon two factors i.e.


amount of memory used and amount of compute time consumed on any
CPU. Formally they are notified as complexities in terms of:
•computing time(Time complexity)
•storage requirement (Space complexity)

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

Step Count 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

Introduce variable count into programs


EX:- 1) Iterative sum of n numbers
Algorithm sum(a, n)
{
s:=0;
count:=count+1; // for assignment statement
for i:=1 to n do
{ count:=count+1; // For for
s:=s+a[i];
count:=count+1; // for assignment statement
}
count:=count+1; // for last time of for
count:=count+1; // for return
12/30/24 9
CREATED BY K. VICTOR BABUreturn s; }
EX:- 2) 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
}
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

• Add up the contribution of all statements

12/30/24 13
CREATED BY K. VICTOR BABU
Method-II: Tabular method

EX:- 1) Iterative sum of n numbers

Statement s/e frequency Total steps


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

12/30/24 14
CREATED BY K. VICTOR BABU
• EX:- 2) Recursive sum of n numbers

Statement s/e Frequency Total steps


n=0 n>0 n=0 n>0
Algorithm RSum(a,n) 0 -- -- 0 0
{ 0 -- -- 0 0
if( n ≤ 0 ) then 1 1 1 1 1
return 0; 1 1 0 1 0
else
return Rsum(a,n-1)+a[n] ; 1+x 0 1 0 1+x
} 0 -- -- 0 0

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

Space=Fixed part +Variable part

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

Variable Space Requirements (SP (I))


depend on the instance characteristic I
number, size, values of inputs and outputs associated with I
recursive stack space, formal parameters, local variables, return address

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

• • For the recurrence relation T(n)=T(n−1)+1, what is the time complexity?


For the recurrence relation T(n)=T(n−1)+1, what is the time complexity?

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

1. Solve Towers of Hanoi problem using 5 disk


2. Explain the process of creating a table using the tabular method to
analyze an algorithm.

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

You might also like