0% found this document useful (0 votes)
15 views23 pages

Session2 Performance Analysis

The document discusses the analysis and design of algorithms, focusing on performance analysis using the step count method and the Random Access Machine (RAM) model. It outlines the requirements for algorithm analysis, including time and space complexity, and provides examples of counting elementary operations in algorithms. The summary emphasizes the importance of mathematical models in algorithm analysis and the utility of the RAM model for understanding algorithm performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views23 pages

Session2 Performance Analysis

The document discusses the analysis and design of algorithms, focusing on performance analysis using the step count method and the Random Access Machine (RAM) model. It outlines the requirements for algorithm analysis, including time and space complexity, and provides examples of counting elementary operations in algorithms. The summary emphasizes the importance of mathematical models in algorithm analysis and the utility of the RAM model for understanding algorithm performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Analysis and Design of Algorithms

Dr. Vandna Batra

Unit-1
Introduction to Algorithms
Session 2
Performance analysis using step count method

School of Engineering & Technology


K.R. Mangalam University, Gurugram (Haryana)
Requirement & Objectives for Algorithm Analysis

• Use some mathematical model of computer & execute the


algorithm on this model;
• Estimate time & space complexity;
• Design robustness, scalable & general algorithm;
• How well it behaves on real computer, network, internet,
interact with data?
• Design more efficient algorithms.
RAM -Model of implementation
 Before implementation we need a model of implementation technology.
 Machine-independent algorithm design depends upon a hypothetical computer called the
Random Access Machine or RAM
Assumptions:
• Each ``simple'' operation (+, *, -, =, if, call) takes exactly 1 time step.
• Loops and subroutines are not considered simple operations. Instead, they are the
composition of many single-step operations.
• Each memory access takes exactly one time step, and we have infinite memory. The
RAM model takes no notice of whether an item is in cache or on the disk, which
simplifies the analysis.
RAM Model of Computation
• The RAM is a simple model of how computers perform.
• A common complaint is that it is too simple & may not be perfect in practice.
• For example, multiplying two numbers takes more time than adding two
numbers, which violates the first assumption of the model.
• Memory access times differ greatly depending on whether data sits in cache
or on the disk, thus violating the third assumption.
• Still RAM is an excellent model for understanding how an algorithm will
perform on a real computer.
• Captures the essential behavior of computers while being simple to work
with. We use the RAM model because it is useful in practice.
Step Count Method

• A step is any computation unit that is independent of the problem size.


• Thus 5 additions can be one step; 50 multiplications can also be one step;
• But n additions, where n is the problem size, cannot be one step.
• For example, the entire statement - return x+y+y*z+(x+y-z)/(x+y)+10;
can be regarded as a single step if its execution time is independent of the problem
size.
 We may also count a statement such as x = y; as a single step.
Expressing Algorithm as Function of input N
F=N
STEP-COUNT METHOD
Statement Space
cost
F=1
Statement Frequency Total cost Requirement
A=N
1) Algorithm SUM(A,N) 0 0 0 N=1
2) { 0 0 0 S=1
3) S=0 1 1 1 i=1
4) For(i=0,i<n,i++) { 1 N+1 N+1
S(N)= N+3
5) S=S+A[i]; } 1 N N
6) return S; 1 1 1
7) } 0 0 0
F(N)= 2N+3
F=N+1
Counting Elementary Operation
In general, an elementary operation must have two properties:
There can’t be any other operations that are performed more frequently as the size of the
input grows.
The time to execute an elementary operation must be constant: it mustn’t increase as the
size of the input grows. This is known as unit cost. Depends on FOR loop & IF
statement
// Compute the maximum element in the array a. Max case: for all i, IF
statement is true
Algorithm max(a):
Min Case: for all i, IF
max ← a[0] statement is False
for i = 1 to len(a)-1
Elementary
if a[i] > max Operation
max ← a[i]
return max
Counting Elementary Operation

// Tell whether the array a contains x.


Algorithm contains(a, x): Basic operation
for i = 0 to len(a)-1
if x == a[i]
return true
return false

If x isn’t found in a the algorithm makes n comparisons,


but if x equals a[0] there is only one comparison.
Counting Elementary Operation

Algorithm DoubleLoop(a,b,n): Basic operation

for i = 1 to n
n*n times
for j=1 to n
b[i,j]== a[i,j]+a[i,j]

Algorithm Contains(a, n, item):


i=1
Depends on while loop
while (i <=n) Max case: i=1 to n
if a[i] == item Item not found
return true
return false
Test Your Knowledge
1. Determine the best & worst case step count
Test Your Knowledge
Solution: Best case step count
Test Your Knowledge
Solution: Worst case step count
Test Your Knowledge
2. Determine the best & worst cases by counting elementary
operation
Algorithm contains(a, x):
for i = 0 to len(a)-1
if x == a[i]
return true
return false
Test Your Knowledge
Solution
Algorithm contains(a, x):
for i = 0 to len(a)-1 Basic Operation
if x == a[i]
return true
return false
Here the number of comparisons depends not only on the number of
elements, n, in the array but also on the value of x and the values in a:
If x isn’t found in a the algorithm makes n comparisons,
but if x equals a[0] there is only one comparison.
14
Test Your Knowledge
3. Express the following algorithm as function of input ‘n’ using frequency
count method
void bubble(int [] a, int n)
{
for (int i = 0; i < n – 1; i++)
if (a[i] > a[i+1])
swap(a[i], a[i+1]);
}
Test Your Knowledge
Solution

void bubble(int [] a, int n)


N times
{
for (int i = 0; i < n – 1; i++)
N times OR
if (a[i] > a[i+1]) N-1 times ?
swap(a[i], a[i+1]);
Depends on no. of
} TRUE executions of
IF statement
Test Your Knowledge
4. Determine the frequency count for each statement

for i in 1..n loop


for j in 1..n loop
if i < j then
swop (a(i,j), a(j,i));
end if;
end loop;
end loop;
Test Your Knowledge
5. Express the following algorithm as function of input ‘n’ using
frequency count method

int linearSearch(int values[], int target, int n)


{
for(int i = 0; i < n; i++)
{
if (values[i] == target)
return i;
}
return -1;
}
Test Your Knowledge
6. Algorithm AnalyzeMe (n)

//input: A non-negative integer n


s=0
for i=1 to n do
s=s+i*i
return s

a) What does the algorithm compute?


b) What is its basic operation?
c)How many times is the basic operation is executed?
d)What is the efficiency of the algorithm?
Test Your Knowledge
5. Express the following algorithm as function of input ‘n’ using
frequency count method

int linearSearch(int values[], int target, int n)


{
for(int i = 0; i < n; i++)
{
if (values[i] == target)
return i;
}
return -1;
}
Summary
 Algorithm analysis provides theoretical estimates for the resources (Time &
Memory) needed by an algorithm before actual implementation.
 Analysis of algorithms is abstracted without the use of a
specific programming language or implementation.
 We need mathematical model to analyse algorithm
 Model must be similar to realistic computer.
 RAM Model is useful.
 Asymptotic analysis is useful
References
 https://www.geeksforgeeks.org/step-count-method-for-time-complexity-ana
lysis/
 Fundamentals of Computer Algorithms by Sartaj Sahni
and Sanguthevar Rajasekaran Ellis Horowitz

You might also like