0% found this document useful (0 votes)
26 views14 pages

Lec-3 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 PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views14 pages

Lec-3 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 PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Design & Analysis of Algorithms

(BCAE0106)

Lec.-3: Performance Analysis

Presented by:
Akanksha
Assistant Professor
Dept. of Computer Engineering & Applications, IET,
GLA University, Mathura
Analyzing Algorithms

Analysing the algorithm is a process of comparing two

algorithms with respect to the space acquired by the

algorithm(Space Complexity) and time taken for algorithm

execution (Time Complexity).

2
Analyzing Algorithms

A Priori Analysis A Posterior Analysis

• This is a practical analysis of an algorithm. The


• This is a theoretical analysis of an practical analysis is achieved by implementing the
algorithm using any programming language.
algorithm which is done before
implementing the algorithm. • The selected algorithm is implemented using
programming language and executed on target
computer machine.
• Efficiency of an algorithm is
measured by assuming that all
other factors, for example, • This analysis depends on the hardware.
processor speed, are constant and
have no effect on the • In this analysis, actual statistics like running time
implementation. and space required, are collected.

• It is independent of the hardware.


3
Algorithm Complexity
 Suppose X is an algorithm and n is the size of input data, the time and
space used by the algorithm X are the two main factors, which decide
the efficiency of X.

 Time Factor − Time is measured by counting the number of key


operations such as comparisons in the sorting algorithm.

 Space Factor − Space is measured by counting the maximum memory


space required by the algorithm.

 The complexity of an algorithm f(n) gives the running time and/or the
storage space required by the algorithm in terms of n as the size of
input data.
Categorization of Complexity:

5
Space Complexity:

 Space complexity of an algorithm represents the


amount of memory space required by the algorithm
in its life cycle.

 Space complexity is expressed in big O notation.

6
Space Complexity:

 For an algorithm, the space is required for the following purposes:

 To store program instructions


 To store constant values
 To store variable values
 To track the function calls, jumping statements, etc.

 Auxiliary space: The extra space required by the algorithm, excluding the
input size, is known as an auxiliary space.

 The space complexity considers both the spaces, i.e., auxiliary space, and
space used by the input.

 So, Space complexity = Auxiliary space + Input size.


7
Space Complexity:

 Following is a simple example that tries to explain the concept −


Algorithm: SUM(A, B)

Step 1 − START

Step 2 − C ← A + B +
10

Step 3 − Stop
 Here we have three variables A, B, and C and one constant.

 Hence S(P) = 1 + 3. Now, space depends on data types of given variables


and constant types and it will be multiplied accordingly.
8
Time Complexity:
 The time complexity T(P) taken by a program P is the sum of the
compile time and run (execution) time.
 Compiler time does not depend on the instance characteristics.
 Also we assume that a compiled program will be run several times
without recompilation.
 That is why we focus only on Run (Execution) Time.
 Time complexity of an algorithm represents the amount of time
required by the algorithm to run to completion.
 Time requirements can be defined as a numerical function T(n),
where T(n) can be measured as the number of steps, provided each
step consumes constant time.
 For example, addition of two n-bit integers takes n steps.
Consequently, the total computational time is T(n) = c ∗ n, where c is
the time taken for the addition of two bits. Here, we observe that
T(n) grows linearly as the input size increases. 9
Example 1:
Statement Step/execution Frequency Total step
(s/e)
Algorithm sum(a,n) 0 1 0
{ 0 1 0
s=0.0; 0 1 0
for i=1 to n do 1 n+1 n+1
s=s+a[i]; 1 n n
return s; 0 1 0
} 0 1 0
Time complexity/ time taken by the above code = 0 + 0 + 0 + n+1 + n + 0 + 0
=2n + 1
10
Example 2:
1. int fun1(int n) Time Complexity =
order of n^2
2. {
3. int sum=0; and
4. for(int i=1; i<=n; i++)
Space Complexity
5. for(int j=1; j<=n; j++) = order of 1
(constant)
6. sum++;
7. return sum;
8. }
11
Example 3:
1. int sum =0;
2. int fun1(n)
3. {
4. If(n==1)
5. return 1;
Time Complexity= order of n
6. sum = n+ fun1(n-1);
7. return sum; and
8. }
Space Complexity= order of n
12
Example 4:
1. int fun2(int n) Time Complexity=
2. { order of n
3. int sum=0;
and
4. for(int i=1;i<=n;i+
+) Space Complexity=
5. sum=sum + i; order of 1
6. return sum;
7. }
13
Example 5:
1. int fun3(int n) Time Complexity=
2. { order of 1
3. Return
and
n*(n+1)/2;
4. } Space Complexity=
order of 1

14

You might also like