0% found this document useful (0 votes)
44 views5 pages

Time Complexity

The document provides a detailed analysis of time and space complexity for various algorithms, including sum of an array, matrix addition, and multiplication. It also discusses the time complexity for different types of loops and categorizes time functions into constant, logarithmic, linear, quadratic, cubic, and exponential. Additionally, it presents the relationships among different types of time functions.

Uploaded by

Huzaifa Siam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

Time Complexity

The document provides a detailed analysis of time and space complexity for various algorithms, including sum of an array, matrix addition, and multiplication. It also discusses the time complexity for different types of loops and categorizes time functions into constant, logarithmic, linear, quadratic, cubic, and exponential. Additionally, it presents the relationships among different types of time functions.

Uploaded by

Huzaifa Siam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Time and Space Complexity Analysis

1. Sum of array:

Algorithm Time Taken


Algorithm getSum(arr, n) {
sum = 0; 1
for (i = 0; i < n; i++) { n+1
sum += arr[i]; n
}
return sum; 1
}
f(n) = 2n + 3

Time Complexity: O(n)

Space Complexity: O(n)


arr → n
sum → 1
i→ 1
n→ 1
--------------
s(n) → n+3

2. Matrix Addition:

Algorithm Time Taken


Algorithm add(A, B, C)
{
for (i = 0; i < n; i++) n+1
for (j = 0; j < n; j++) n*(n+1)
C[i][j] = A[i][j] + B[i][j]; n*n
}
f(n) = 2n2+2n+1
Time Complexity: O(n2)

Space Complexity: O(n2)


A→ n2
B→ n2
C→ n2
N→ 1
i→ 1
j→ 1
------------
s(n) → 3n2+3

3. Matrix Multiplication:

Algorithm Time Taken


Algorithm multiplyMatrix(A, B, n)
{
for (i = 0; i < n; i++) { n+1
for (j = 0; j < n; j++) { n*(n+1)
C[i][j] = 0; n*n
for (k = 0; k < n; k++) { n*n*(n+1)
C[i][j] += A[i][k] * B[k][j]; n*n*n
}
}
Return C; 1
}
}
f(n) = 2n3+3n2+2n+2
Time Complexity: O(n3)

Space Complexity: O(n2)


A → n2
B → n2
C → n2
n→ 1
i→ 1
j→ 1
k→ 1
------------
s(n) → 3n2+4
4. Time Complexity Analysis for loops:
a.

Algorithm Time Taken


for(i=0; i<n; i=i+2){ 𝑛⁄ + 1
2
Stmt; 𝑛⁄
2
}
f(n) = n+1
Time Complexity: O(n)

b.
Algorithm Time Taken
for(i=0; i<n; i++){ 𝑛+1
for(j=0; j<n; j++){ n(n+1)
Stmt; n*n
}
}
f(n) = 2n2+2n+1
Time Complexity: O(n2)
c.
Algorithm
for(i=0; i<n; i++){
for(j=0; j<i; j++){
Stmt;
}
}

i j #time
0 0 × 0
1 0 √ 1
1 ×
2 0 √ 2
1 √
2 ×
3 0 √ 3
1 √
2 √
3 ×
. 0 √ n
. 1 √
. .
. .
n n ×
𝑛(𝑛+1) 𝑛2 +𝑛
f(n) = 1+2+3+……….+n = =
2 2
Time Complexity = O(n ) 2

d.
Algorithm
p=0;
for(i=1; p<=n; i++){
p=p+i;
}

i p
1 0+1 = 1
2 1+2 = 3
3 1+2+3 = 6
4 1+2+3+4 = 10
. .
. .
. .
. .
k 1+2+3+……+ k
Let’s assume, p > n
k2 +k
then p =
2
k2 +k
 >n
2

Roughly, k2>n
So, k > √n

Time Complexity: O(√n)


e.
Algorithm i
for(i=0; i<n; i=i*2){ 1
stmt; 1*2 = 2
} 2*2 = 22
22*2 = 23
.
.
.
.
2k
Let’s assume, i >= n
as i = 2k
so 2k >= n
For terminating condition
2k = n
Finally, k = log 2 𝑛

Time Complexity: O(log 2 𝑛)

f.
Algorithm Time Taken
for(i=0; i<n; i++){ n+1
for(j=1; j<n; j=j*2){ n * log 𝑛
Stmt; n * log 𝑛
}
}
f(n) = 2n log 𝑛 + n
Time Complexity: O(n log 𝑛)
5. Types of time function:
Time complexity Type
O(1) Constant
O(logn) Logarithmic
O(n) Linear
O(n )2 Quadratic
O(n )3 Cubic
O(2 )n Exponential

6. Relation among Different Types of Time Functions:


1 < logn < √𝑛 < nlogn < n2 < n3 < ………. < 2n < 3n ……… < nn

You might also like