0% found this document useful (0 votes)
14 views15 pages

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

Kartik Garg
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)
14 views15 pages

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

Kartik Garg
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/ 15

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

07/21/2024 DISCOVER . LEARN . EMPOWER


1
Content

Algorithm performance analysis:


Time complexity
Space complexity

2
Performance Analysis:

There are many Criteria to judge an algorithm.


•– Is it correct?
•– Is it readable?
•– How it works

 Performance evaluation can be divided into two major phases.

1. Performance Analysis (machine independent)


 space complexity: The space complexity of an algorithm is the amount of memory it needs
to run for completion.
 time complexity: The time complexity of an algorithm is the amount of computer time it
needs to run to completion.

3
Performance Analysis:
2. Performance Measurement (machine dependent).

 Space Complexity:
•The Space Complexity of any algorithm P is given by S(P)=C+SP(I),C is constant.

•1.Fixed Space Requirements (C)


•Independent of the characteristics of the inputs and outputs
•– It includes instruction space
•– space for simple variables, fixed-size structured variable, constants
•2. Variable Space Requirements (SP(I))
• Depending on the instance characteristic I
•– number, size, values of inputs and outputs associated with I
•– recursive stack space, formal parameters, local variables, return address

4
Reasons for estimating the time complexity

The reasons for estimating the time complexity of the program even before running the
program for the first time are:

(1) We need real-time responses for many applications. That is a faster execution of the
program is required for many applications.

(2) If the time complexity is estimated beforehand, then modifications can be done to the
program to improve its performance before running it.

(3) It is used to specify the upper limit for the time of execution for some programs. The
purpose of this is to avoid infinite loops.

5
Complexity Factor

The time complexity of the program depends on the following factors:

• Compiler used – some compilers produce optimized code which consumes less
time to get executed.

• Compiler options – The optimization options can be set in the options of the
compiler.

• Target computer – The speed of the computer or the number of instructions


executed per second differs from one computer to another.

6
• Examples:
Program 1 :Simple arithmetic function
•Algorithmabc( a, b, c)
•{
•return a + b + b * c + (a + b - c) / (a + b) + 4.00;
•}

•SP(I)=0

•HenceS(P)=Constant

7
Examples:

Program 2: Iterative function for sum a list of numbers


•Algorithm sum( list[ ], n)
•{
•tempsum = 0;
•for i = 0 ton do
•tempsum += list [i];
•return tempsum;
•}

8
Examples:

Program 3: Recursive function for sum a list of numbers


•Algorithmrsum( list[ ], n)
•{
•If (n<=0) then
•return 0.0
•else
•return rsum(list, n-1) + list[n];
•}

9
Methods to compute the step count:

 1) Introduce variable count into programs


 2) Tabular method

 1) Introduce variable count into programs


• Determine the total number of steps contributed by
each statement
• step per execution frequency
• add up the contribution of all statements

10
Methods to compute the step count
 2)Tabular methods.
• Complexity is determined by using a table that includes steps per execution(s/e) i.e
• Amount by which the count changes as a result of the execution of the statement.
• Frequency – number of times a statement is executed

11
TASKS END OF LECTURE LEARNING (TELL):

TASK 1:
What is the time complexity of the following code?
def fun(n,m):
for i in range(n):
print(i)
for i in range(m):
print(i)

TASK 2:
What is the time complexity of the following code?
def function(N,M):
counter=0
for i in range(N):
for j in range(M):
counter+=1
print(counter)

12
TASKS END OF LECTURE LEARNING (TELL):
TASK 3:
What is the time and space complexity of the following code?
def fun(N,M):
arr=[]
counter=0
for i in range(N):
arr.append(i)
for i in range(M):
counter+=1
print(counter)

TASK 4:
What is the time and space complexity of the following code?
def f(n):
if n == 0 or n == 1:
return 1
return f(n - 1) + f(n - 2)

TASK 5:
What are the time complexity and space complexity of the following code?
def fun(n,m):
arr=[[0]*m for i in range(n)]
for i in range(n):
for j in range(m):
k=1
while k<n*m:
k*=2

13
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald

14
THANK YOU

For queries
Email: [email protected]

07/21/2024 15

You might also like