Design and Analysis of Algorithms (CS3052)
Design and Analysis of Algorithms (CS3052)
Algorithms (CS3052)
Prof. G. G. Shingan
Computer science and Engineering
Department
RIT, Rajaramnagar.
Lecture No.3
Content
• Performance Analysis
▫ Time Complexity
▫ Space Complexity
Learning Outcomes
• Analysis the Space complexity of Algorithms
• Analysis the Time complexity of Algorithm
Space Complexity
S(P)=C+SP(I)
• Fixed Space Requirements (C)
Independent of the characteristics of the inputs and
outputs
▫ instruction space
▫ space for simple variables, fixed-size structured variable,
constants
• Variable Space Requirements (SP(I))
depend on the instance characteristic
▫ number, size, values of inputs and outputs associated with
▫ recursive stack space, formal parameters, local variables,
return address
*Program 1: Simple arithmetic function
float abc(float a, float b, float c)
{
return a + b + b * c + (a + b - c) / (a + b) + 4.00;
}
Space Complexity=
N+1=O(1) N+2=O(1)
=n+1 * k
=O(nk) =O(n)
Tabular Method
Iterative function to sum a list of numbers
*Figure 1.2: Step count table for Program 1.10 (p.26)
steps/execution
Statement s/e Frequency Total steps
float sum(float list[ ], int n) 0 0 0
{ 0 0 0
float tempsum = 0; 1 1 1
int i; 0 0 0
for(i=0; i <n; i++) 1 n+1 n+1
tempsum += list[i]; 1 n n
return tempsum; 1 1 1
} 0 0 0
Total 2n+3
15
Matrix Addition
*Figure 1.4: Step count table for matrix addition (p.27)
Exercise 1
*Program::: Printing out a matrix
Exercise 3
*Program :::::Matrix product function