0% found this document useful (0 votes)
39 views

Design and Analysis of Algorithms (CS3052)

This document discusses time and space complexity analysis of algorithms. It provides examples of calculating time and space complexity for iterative and recursive algorithms. For iterative algorithms, it demonstrates using a tabular method to determine time complexity by counting the number of steps per execution and frequency of each statement. For recursive algorithms, it shows time complexity is the number of recursive calls times the space for each call. The document also provides exercises for analyzing matrix operations algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Design and Analysis of Algorithms (CS3052)

This document discusses time and space complexity analysis of algorithms. It provides examples of calculating time and space complexity for iterative and recursive algorithms. For iterative algorithms, it demonstrates using a tabular method to determine time complexity by counting the number of steps per execution and frequency of each statement. For recursive algorithms, it shows time complexity is the number of recursive calls times the space for each call. The document also provides exercises for analyzing matrix operations algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Design and Analysis of

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=

*Program 2: Iterative function for summing a list of numbers


float sum(float list[ ], int n)
{
float tempsum = 0;
int i;
for (i = 0; i<n; i++)
tempsum += list [i];
return tempsum;
Space Complexity=
}
Types of algorithms
Iterative Algorithms Recursive Algorithms
A() A(n)
{ {
For i=1 to n If ()
Max(a,b) A(n/2)
} }

Program without iterative and recursive function require


constant time (O(1))
Calculate Space complexity of Iterative
algorithms
a1(a,1,n) a2(a,1,n)
{ {
int i; int i,j;
for(i:=1 to n) for(i:=1 to n)
a[i]:=0; a[i]:=0;
} }

N+1=O(1) N+2=O(1)

Because Extra space doesn’t depends on input size i.


e. n
Calculate Space complexity of Iterative
algorithms
a3(a,1,n) a4(a,1,n)
{ {
int i; int i,j;
Create B[n]; Create B[n,n];
for(i:=1 to n) for(i:=1 to n)
B[i]:=a[i]; for(j:=1 to n)
} B[i,j]:=a[i];
}
N+1=O(N) N2+2=O(N2)

Amount of Extra space required grows along with


input size i. e. n
If i/p is a[100] then B[100] required
Calculate Space complexity
of Recursive algorithms
A(3)
A1(n)
{ A(2) p(3)
if(n>=1)
{ A(0) K size
A(1) p(2)
A(1) block
A(n-1);
A(2)
A(0) p(1) A(3)
print(“n”);
}
for n=3 No of recursive call=4
} Therefore, for n recursive calls n+1

Space Complexity= no. of recursive calls * space for


recursive call =n+1 * k
=O(nk) =O(n)
Calculate Space complexity
of Recursive algorithms
A(3)
A1(n)
{
A(2) p(3) A(2)
if(n>=1)
{ A(0) K
A(1) p(2) A(1) A(1) p(2) A(1)
A(n-1); A(1) size
print(“n”); A(2) block
A(n-1); A(0) p(1)A(0) A(0) p(1)A(0)A(0) p(1)A(0)A(0) p(1) A(0)
A(3)
}
for n=3 No of recursive call A(n)=15=23+1-1
}
Therefore, for n , A(n) recursive calls
=2n+1-1
But stack cells required=4

=n+1 * k
=O(nk) =O(n)

Space Complexity= no. of recursive calls * space for recursive call


Time Complexity
T(P)=C+TP(I)

• Compile time (C)


Independent of instance characteristics
• Run (execution) time TP
• Definition TP(n)=caADD(n)+csSUB(n)+clLDA(n)+cstSTA(n)
A program step is a syntactically or semantically meaningful program
segment whose execution time is independent of the instance
characteristics.
13

Methods to compute the step count

• Introduce variable count into programs


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

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)

Statement s/e Frequency Total steps


Void add (int 0 0 0
a[ ][MAX_SIZE]•••) 0 0 0
{ 0 0 0
int i, j; 1 rows+1 rows+1
for (i = 0; i < row; i++) 1 rows•(cols+1) rows•cols+rows
for (j=0; j< cols; j++) 1 rows•cols rows•cols
c[i][j] = a[i][j] + b[i][j]; 0 0 0
}
Total 2rows•cols+2rows+1
Calculate time complexity of iterative
algorithms
Algorithm Rsum(a,n) s/e Frequency Total
Steps
N=0 N>0 N=0 N>0
{ 0 0 0 0 0
if(n<=1) then 1 1 1 1 1
return 0.0; 1 1 0 1 0
else 0 0 0 0 0
return Rsum (a,n-1)+a[n]; 1+x 0 1 0 1+x
0 0 0 0 0
}
2 X+2
X=Rsum (a,n-1)
Calculate time complexity of iterative
algorithms
Algorithm Fib(n) s/e frequency Total steps
N=0 or 1 N>1 N=0 or 1 N>1
{ 0 0 0
If (n<=1) then 1 1 1 1 1
Write (n) 1 1 0 1
Else 0 0
{ 0 0
fnm2=0; fnm1=1 2 1 2
for(i=2 to n) do 1 N N
{ 0 0 0
fn=fnm1+fnm2; 1 N-1 N-1
fnm2=fnm1; 1 N-1 N-1
fnm1=fn; 1 N-1 N-1
} 0 0 0
write(fn) 1 1 1
} 0 0 0
} 0 0 0
2 4N+1
18

Exercise 1
*Program::: Printing out a matrix

void print_matrix(int matrix[ ][MAX_SIZE], int rows, int cols)


{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < cols; j++)
printf(“%d”, matrix[i][j]);
printf( “\n”);
}
}
19
Exercise 2
*Program :::Matrix multiplication function

void mult(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ]


[MAX_SIZE])
{
int i, j, k;
for (i = 0; i < MAX_SIZE; i++)
for (j = 0; j< MAX_SIZE; j++)
{
c[i][j] = 0;
for (k = 0; k < MAX_SIZE; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
20

Exercise 3
*Program :::::Matrix product function

void prod(int a[ ][MAX_SIZE], int b[ ][MAX_SIZE], int c[ ]


[MAX_SIZE], int rowsa, int colsb, int colsa)
{
int i, j, k;
for (i = 0; i < rowsa; i++)
for (j = 0; j< colsb; j++) {
c[i][j] = 0;
for (k = 0; k< colsa; k++)
c[i][j] += a[i][k] * b[k][j];
}
}

You might also like