Professional Documents
Culture Documents
DSA Complexity
DSA Complexity
DSA Complexity
algorithm
Unit 1: introduction
Shikha Sharma
Algorithm
10 Km
5 Km 20Km
The complexity means how complex an algorithm is, which can be reflected
as relative amount of time or space they require.
It gives the idea about the cost of running an algorithm.
The cost can be either in terms of time and space occupied .
It can be of two types:
Time complexity: It is defined as an estimate of time required by an algorithm
to run to completion.
Suppose input size n , so number of steps require by algorithm to solve an
instance of size n is called time complexity of algorithm in terms of n. The
algorithm with lesser time complexity will save time and run faster than
others.
Space complexity: It is an estimate of space in memory that the algorithm
require to complete the task.
That is amount of storage needed while solving a problem using the algorithm
is called space complexity of that algorithm. An algorithm with lesser space
complexity will require less memory than others.
Asymptotic notation
Asymptotic notation of an algorithm is a mathematical representation of its complexity.
“ known as Algorithm's growth rate.
It is also
2. Big O Notation:
Theta (Θ) Notation: (Average Case) The notation θ(n) is the formal
way to express both the lower bound and the upper bound of an
algorithm's running time.
θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all
n > n0. }
ASYMPTOTIC NOTATION
Q5. Evaluate time and space complexity of a matrix multiplication algorithm for multiplying two
square matrix of size n*n.
Solution:
matrix –multiplication(A,B)
Step 1:for i= 1 to n
Step2:for j:= 1 to n
Step3:c[i,j]= 0
Step4:for i=1 to n
Step5:for j:= 1 to n
Step6:for k:= 1 to n
Step7:c[i,j]=c[i,j]+a[i,k]*b[j,k];
Step8:end for
Step9:end for
Step10:end for
Step11:return c
Solution:
The initialization steps
2
1 to 3 are a nested loop of n size each. Hence, total number of
iterations is n*n=𝑛 .
The actual multiplication steps 4 to 9 are nested loops of n size.
Hence total number of iteration is n*n*n=𝑛3 .
Time taken for calculation in step 7 is 3 units( one multiplication, one addition, one
assignment).
Thus , total time taken by the algorithm is :
T(n)= 1.𝑛2 +3.𝑛3 = O(𝑛3 )
Time complexity of matrix multiplication algorithm . O(𝑛3 )
Space complexity:
The algorithm needs memory space for matrix A, B, and C and for variables i ,j ,and k.
Space occupied by any matrix of size n*n=𝑛2 . We have three such matrix.
Space occupied by other three variable is one unit each. Thus ,the space complexity of
algorithm can be expressed using sum rule.
S(n)=3.𝑛2 + 3.1 =O(𝑛2 )
Space complexity of matrix multiplication algorithm is O(𝑛2 )