A square matrix in which all the elements of the principal diagonal are 1, and all other elements are 0, then this matrix is known as identity matrix.
For example:
INPUT: A matrix
OUTPUT: whether it is an identity matrix or not
PROCESS:
Step 1: [taking the input]
Read m, n [number of rows and columns of the matrix]
For i=0 to m-1 repeat
For j=0 to n-1 repeat
Read a[i][j]
[End of ‘for’ loop]
[End of ‘for’ loop]
Step 2: [Checking for identity matrix]
for i = 0 to m-1 repeat
for j = 0 to n-1 repeat
if i = j and a[i][j]≠1 then
Set f <- 1
break
else if i ≠ j and a[i][j] ≠ 0 then
Set f <- 1
break
[End of ‘if’]
[End of ‘for’ loop]
[End of ‘for’ loop]
If f=0 then
Print "The matrix is an identity matrix"
else
print "The matrix is not an identity matrix"
[End of ‘if’]
Step 3: Stop.
TIME COMPLEXITY:
for (i = 0; i < m; i++)--------------- O(m)
{ for (j = 0; j < n; j++)------------ O(n)
{ if (i == j && a[i][j] != 1)----- O(c1)
{ f = 1;
break; }
else if (i != j && a[i][j] != 0)------- O(c2)
{ f = 1;
break; }
}
The complexity is O(m*n). as the matrix is a square matrix therefore, the value of m and n should be the same. Therefore, the complexity is O().
Related
Contributed by