0% found this document useful (0 votes)
16 views33 pages

2-Asymptotic Notation, Time and Space Complexity of An Algorithm-25!05!2024

Uploaded by

Gaming world
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)
16 views33 pages

2-Asymptotic Notation, Time and Space Complexity of An Algorithm-25!05!2024

Uploaded by

Gaming world
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/ 33

Module 1

Computing the space and time complexity of the


Algorithm
Analysis of Iterative algorithms
What is Space complexity?

Total amount of computer memory required by an algorithm


to complete its execution is called as space complexity of that
algorithm.

1.To store program instructions.


2.To store constant values.
3.To store variable values.
4. And for few other things like funcion calls, jumping
statements etc,.
When a program is under execution it uses the
computer memory for THREE reasons.
1.Instruction Space: It is the amount of memory used to
store compiled version of instructions.

2.Environmental Stack: It is the amount of memory used to


store information of partially executed functions at the time
of function call.

3. Data Space: It is the amount of memory used to store all


the variables and constants.
Space complexity
• When we want to perform analysis of an algorithm based on its Space
complexity, we consider only Data Space and ignore Instruction Space
as well as Environmental Stack.
• That means we calculate only the memory required to store Variables,
Constants, Structures, etc.,
2 bytes to store Integer value.
4 bytes to store Floating Point value.
1 byte to store Character value.
6 (OR) 8 bytes to store double value.
Constant space complexity

2 bytes of memory to store variable 'a' and


another 2 bytes of memory is used for return value

If any algorithm requires a fixed amount of space for


all input values then that space complexity is said to
be Constant Space Complexity.
Linear space complexity
If the amount of space required by an algorithm is
increased with the increase of input value, then that
space complexity is said to be Linear Space Complexity.

n*2' bytes of memory to store array variable 'a[ ]'


2 bytes of memory for integer parameter 'n'
4 bytes of memory for local integer variables 'sum' and 'i' (2
bytes each)
2 bytes of memory for return value.= 2n+8 bytes
Constant Time complexity
If any program requires a fixed amount of time for all input values then its time complexity is said to be Constant
Time Complexity..

It requires 1 unit of time for Arithmetic and Logical operations


It requires 1 unit of time for Assignment and Return value
It requires 1 unit of time for Read and Write operations

Constant Time Complexity. = 2 units


Example : Linear time complexity
If the amount of time required by an algorithm is increased with the increase of
input value then that time complexity is said to be Linear Time Complexity
Types of algorithms
• What is f(n)
• time taken by the algorithm to execute this is not the real time it is an
approximate time
• How to find it out f(n)
Two Types of algorithm
• Iterative
• No of time loop executes
• Recursion
• Backward Substitution

NOTE: CONSTANT O(1)


Algorithms for computing the
Factorial
Recursion Iterative

int factorial (int n) int factorial (int n)


{ { fact =1;
If (n <= 1) if (n<=1)
return 1; return 1;
else else {
return n * factorial(n-1); for (k=2; k<=n; k++)
} fact *= k;
return fact;
}
}
Iterative : Examples

Function() Function()
{ {
int I; int I;
for(i=1 to n) for(i=1 to n)
print(“Hello”); for(j=1 to n)
} print(“Hello”);
}

O(n)
O(n2)
Example1
function(){
int i=1, s=1;
while(S<=n)
{
i++;
s=s+i;
printf(“hello”)
}
}
S 1 3 6 10 15 21
i 1 2 3 4 5 6
function(){
int i=1, s=1;
while(S<=n)
{
i++;
s=s+i;
printf(“hello”)
}
}
S 1 3 6 10 15 21
i 1 2 3 4 5 6.........n
function(){
int i=1, s=1;
while(S<=n)
{
i++;
s=s+i;
printf(“hello”)
}
}
S 1 3 6 10 15 21……n(n+1)/2 n>s stop
i 1 2 3 4 5 6.........n
Example2
function(){
int i=1, s=1;
while(S<=n)
{
i++;
s=s+i;
printf(“hello”)
}
}
S 1 3 6 10 15 21……k(k+1)/2
i 1 2 3 4 5 6.........k
K(K+1)/2 >=n
(K2+k)/2>=n

Which means
k= ie O()
Example 3
Function(){
i=1;
for(i=1;i2<=n;i++)
printf(“hello”);
}

or

O(), or theta both are same


Example 4
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
for(k=1;k<=100;k++){
print(“Hello”);
}
}
}
}
I
J
K
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
for(k=1;k<=100;k++){
print(“Hello”);
}
}
}
}
I 1 2 3 4 5…………….n
J 1 2 3 4 5…………….n
K 1*1002*1003*1004*1005*100…….n

1*100+2*100+3*100+4*100+……n*100
100(1+2+3+4+……………n)
n(n+1)/2 ie O(n2)
Example 5
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<= ;j++){
for(k=1;k<=n/2;k++){
printf(“Hello”);
}
}
}
}
i
J
K
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<= ;j++){
for(k=1;k<=n/2;k++){
printf(“Hello”);
}
}
}
}
I 1 2 3 4 5
J 1 4 9 16 25
K 1*n/2 4*n/2 9*n/2 16*n/2 25*n/2
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<= ;j++){
for(k=1;k<=n/2;k++){
printf(“Hello”);
}
}
}
}
I 1 2 3 4 5…………………….n
J 1 4 9 16 25
K 1*n/2 4*n/2 9*n/2 16*n/2 25*n/2
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<= ;j++){
for(k=1;k<=n/2;k++){
printf(“Hello”);
}
}
}
}
I 1 2 3 4 5…………………….n
J 1 4 9 16 25…………………..n2
K 1*n/2 4*n/2 9*n/2 16*n/2 25*n/2
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<= ;j++){
for(k=1;k<=n/2;k++){
printf(“Hello”);
}
}
}
}
I 1 2 3 4 5…………………….n
J 1 4 9 16 25…………………..n2
K 1*n/2 4*n/2 9*n/2 16*n/2 25*n/2…………..n2*n/2
Function(){
int I,j,k,n;
for(i=1;i<=n;i++){
for(j=1;j<= ;j++){
for(k=1;k<=n/2;k++){
printf(“Hello”);
}
}
}
}
I 1 2 3 4 5…………………….n
J 1 4 9 16 25…………………..
K 1*n/2 4*n/2 9*n/2 16*n/2 25*n/2…………..*n/2
1*n/2 4*n/2 9*n/2 16*n/2 25*n/2…………..n2*n/2
n/2(12+….. )

n/2((n(n+1)(2n+1)/6)

O(n4)
Example 6
Function(){
for(i=1;i<n;i=i*2)
printf(“hello”);
}
i=1 2 4 8 16 n

=n
K=log(n)
O(log2 n)
Example 7
Function(){
int i,j,k;
for(i=n/2;i<=n;i++)
for(j=1;j<=n/2;j++)
for(k=1;k<=n;k=k*2)
printf(“hello”);
}
Function(){
int i,j,k;
n/2 for(i=n/2;i<=n;i++)
n/2 for(j=1;j<=n/2;j++)
Log2 n for(k=1;k<=n;k=k*2)
printf(“hello”);
}
n/2 *n/2*log2 n
O(n2 log2 n)
Example 8
Function(){
int I,j,k;
for(i=n/2;i<=n;i++)
for(j=1;j<=n;j=2*j)
for(k=1;k<=n;k=k*2)
printf(“Hello”);
}
Function(){
int I,j,k;
n/2 for(i=n/2;i<=n;i++)
Log2 n for(j=1;j<=n;j=2*j)
Log2 n for(k=1;k<=n;k=k*2)
printf(“Hello”);
}
n/2* (Log2 n)2

O(n* (Log2 n)2)


Example 9
Function(){
for(i=1;i<=n,i++)
for(j=I;j<=n;j=j+i)
printf(“hello”);
}
I
J
Function(){
for(i=1;i<=n,i++)
for(j=I;j<=n;j=j+i)
printf(“hello”);
}
i=1 2 3 4
J=n n/2 n/3 n/4
n+n/2+n/3+……1)
n(1+1/2+1/3+1/4+……..1/n)
n(logn)
Back to Our Example
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c 2 + N x c1 =
(c2 + c1) x N + c2

• Both algorithms are of the same order: O(N)

32
Example (cont’d)

Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2 = O(N2)

33

You might also like