What is rank of a matrix?
Rank of a matrix A of size M x N is defined as
- Maximum number of linearly independent column vectors in the matrix or
- Maximum number of linearly independent row vectors in the matrix.
Example:
Input: mat[][] = {{10, 20, 10},
{20, 40, 20},
{30, 50, 0}}
Output: Rank is 2
Explanation: Ist and IInd rows are linearly dependent.
But Ist and 3rd or IInd and IIIrd are
independent.
Input: mat[][] = {{10, 20, 10},
{-20, -30, 10},
{30, 50, 0}}
Output: Rank is 2
Explanation: Ist and IInd rows are linearly independent.
So rank must be atleast 2. But all three rows
are linearly dependent (the first is equal to
the sum of the second and third) so the rank
must be less than 3.
In other words rank of A is the largest order of any non-zero minor in A where order of a minor is the side-length of the square sub-matrix of which it is determinant.
So if M < N then maximum rank of A can be M else it can be N, in general rank of matrix can’t be greater than min(M, N).
The rank of a matrix would be zero only if the matrix had no non-zero elements. If a matrix had even one non-zero element, its minimum rank would be one.
How to find Rank?
The idea is based on conversion to Row echelon form.
1) Let the input matrix be mat[][]. Initialize rank equals
to number of columns
// Before we visit row 'row', traversal of previous
// rows make sure that mat[row][0],....mat[row][row-1]
// are 0.
2) Do following for row = 0 to rank-1.
a) If mat[row][row] is not zero, make all elements of
current column as 0 except the element mat[row][row]
by finding appropriate multiplier and adding a the
multiple of row 'row'
b) Else (mat[row][row] is zero). Two cases arise:
(i) If there is a row below it with non-zero entry in
same column, then swap current 'row' and that row.
(ii) If all elements in current column below mat[r][row]
are 0, then remove this column by swapping it with
last column and reducing number of rank by 1.
Reduce row by 1 so that this row is processed again.
3) Number of remaining columns is rank of matrix.
Example:
Input: mat[][] = {{10, 20, 10},
{-20, -30, 10},
{30, 50, 0}}
row = 0:
Since mat[0][0] is not 0, we are in case 2.a of above algorithm.
We set all entries of 0'th column as 0 (except entry mat[0][0]).
To do this, we subtract R1*(-2) from R2, i.e., R2 --> R2 - R1*(-2)
mat[][] = {{10, 20, 10},
{ 0, 10, 30},
{30, 50, 0}}
And subtract R1*3 from R3, i.e., R3 --> R3 - R1*3
mat[][] = {{10, 20, 10},
{ 0, 10, 30},
{ 0, -10, -30}}
row = 1:
Since mat[1][1] is not 0, we are in case 2.a of above algorithm.
We set all entries of 1st column as 0 (except entry mat[1][1]).
To do this, we subtract R2*2 from R1, i.e., R1 --> R1 - R2*2
mat[][] = {{10, 0, -50},
{ 0, 10, 30},
{ 0, -10, -30}}
And subtract R2*(-1) from R3, i.e., R3 --> R3 - R2*(-1)
mat[][] = {{10, 0, -50},
{ 0, 10, 30},
{ 0, 0, 0}}
row = 2:
Since mat[2][2] is 0, we are in case 2.b of above algorithm.
Since there is no row below it swap. We reduce the rank by 1 and
keep row as 2.
The loop doesn't iterate next time because loop termination condition
row <= rank-1 returns false.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 3
void swap( int mat[R][C], int row1, int row2,
int col)
{
for ( int i = 0; i < col; i++)
{
int temp = mat[row1][i];
mat[row1][i] = mat[row2][i];
mat[row2][i] = temp;
}
}
void display( int mat[R][C], int row, int col);
int rankOfMatrix( int mat[R][C])
{
int rank = C;
for ( int row = 0; row < rank; row++)
{
if (mat[row][row])
{
for ( int col = 0; col < R; col++)
{
if (col != row)
{
double mult = ( double )mat[col][row] /
mat[row][row];
for ( int i = 0; i < rank; i++)
mat[col][i] -= mult * mat[row][i];
}
}
}
else
{
bool reduce = true ;
for ( int i = row + 1; i < R; i++)
{
if (mat[i][row])
{
swap(mat, row, i, rank);
reduce = false ;
break ;
}
}
if (reduce)
{
rank--;
for ( int i = 0; i < R; i ++)
mat[i][row] = mat[i][rank];
}
row--;
}
}
return rank;
}
void display( int mat[R][C], int row, int col)
{
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < col; j++)
printf ( " %d" , mat[i][j]);
printf ( "\n" );
}
}
int main()
{
int mat[][3] = {{10, 20, 10},
{-20, -30, 10},
{30, 50, 0}};
printf ( "Rank of the matrix is : %d" ,
rankOfMatrix(mat));
return 0;
}
|
Java
import java.util.*;
class GFG {
static final int R = 3 ;
static final int C = 3 ;
static void swap( int mat[][],
int row1, int row2, int col)
{
for ( int i = 0 ; i < col; i++)
{
int temp = mat[row1][i];
mat[row1][i] = mat[row2][i];
mat[row2][i] = temp;
}
}
static void display( int mat[][],
int row, int col)
{
for ( int i = 0 ; i < row; i++)
{
for ( int j = 0 ; j < col; j++)
System.out.print( " "
+ mat[i][j]);
System.out.print( "\n" );
}
}
static int rankOfMatrix( int mat[][])
{
int rank = C;
for ( int row = 0 ; row < rank; row++)
{
if (mat[row][row] != 0 )
{
for ( int col = 0 ; col < R; col++)
{
if (col != row)
{
double mult =
( double )mat[col][row] /
mat[row][row];
for ( int i = 0 ; i < rank; i++)
mat[col][i] -= mult
* mat[row][i];
}
}
}
else
{
boolean reduce = true ;
for ( int i = row + 1 ; i < R; i++)
{
if (mat[i][row] != 0 )
{
swap(mat, row, i, rank);
reduce = false ;
break ;
}
}
if (reduce)
{
rank--;
for ( int i = 0 ; i < R; i ++)
mat[i][row] = mat[i][rank];
}
row--;
}
}
return rank;
}
public static void main (String[] args)
{
int mat[][] = {{ 10 , 20 , 10 },
{- 20 , - 30 , 10 },
{ 30 , 50 , 0 }};
System.out.print( "Rank of the matrix is : "
+ rankOfMatrix(mat));
}
}
|
Python3
class rankMatrix( object ):
def __init__( self , Matrix):
self .R = len (Matrix)
self .C = len (Matrix[ 0 ])
def swap( self , Matrix, row1, row2, col):
for i in range (col):
temp = Matrix[row1][i]
Matrix[row1][i] = Matrix[row2][i]
Matrix[row2][i] = temp
def Display( self , Matrix, row, col):
for i in range (row):
for j in range (col):
print ( " " + str (Matrix[i][j]))
print ( '\n' )
def rankOfMatrix( self , Matrix):
rank = self .C
for row in range ( 0 , rank, 1 ):
if Matrix[row][row] ! = 0 :
for col in range ( 0 , self .R, 1 ):
if col ! = row:
multiplier = (Matrix[col][row] /
Matrix[row][row])
for i in range (rank):
Matrix[col][i] - = (multiplier *
Matrix[row][i])
else :
reduce = True
for i in range (row + 1 , self .R, 1 ):
if Matrix[i][row] ! = 0 :
self .swap(Matrix, row, i, rank)
reduce = False
break
if reduce :
rank - = 1
for i in range ( 0 , self .R, 1 ):
Matrix[i][row] = Matrix[i][rank]
row - = 1
return (rank)
if __name__ = = '__main__' :
Matrix = [[ 10 , 20 , 10 ],
[ - 20 , - 30 , 10 ],
[ 30 , 50 , 0 ]]
RankMatrix = rankMatrix(Matrix)
print ( "Rank of the matrix is:" ,
(RankMatrix.rankOfMatrix(Matrix)))
|
C#
using System;
class GFG {
static int R = 3;
static int C = 3;
static void swap( int [,]mat,
int row1, int row2, int col)
{
for ( int i = 0; i < col; i++)
{
int temp = mat[row1,i];
mat[row1,i] = mat[row2,i];
mat[row2,i] = temp;
}
}
static void display( int [,]mat,
int row, int col)
{
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < col; j++)
Console.Write( " "
+ mat[i,j]);
Console.Write( "\n" );
}
}
static int rankOfMatrix( int [,]mat)
{
int rank = C;
for ( int row = 0; row < rank; row++)
{
if (mat[row,row] != 0)
{
for ( int col = 0; col < R; col++)
{
if (col != row)
{
double mult =
( double )mat[col,row] /
mat[row,row];
for ( int i = 0; i < rank; i++)
mat[col,i] -= ( int ) mult
* mat[row,i];
}
}
}
else
{
bool reduce = true ;
for ( int i = row + 1; i < R; i++)
{
if (mat[i,row] != 0)
{
swap(mat, row, i, rank);
reduce = false ;
break ;
}
}
if (reduce)
{
rank--;
for ( int i = 0; i < R; i ++)
mat[i,row] = mat[i,rank];
}
row--;
}
}
return rank;
}
public static void Main ()
{
int [,]mat = {{10, 20, 10},
{-20, -30, 10},
{30, 50, 0}};
Console.Write( "Rank of the matrix is : "
+ rankOfMatrix(mat));
}
}
|
PHP
<?php
$R = 3;
$C = 3;
function swap(& $mat , $row1 , $row2 , $col )
{
for ( $i = 0; $i < $col ; $i ++)
{
$temp = $mat [ $row1 ][ $i ];
$mat [ $row1 ][ $i ] = $mat [ $row2 ][ $i ];
$mat [ $row2 ][ $i ] = $temp ;
}
}
function rankOfMatrix( $mat )
{
global $R , $C ;
$rank = $C ;
for ( $row = 0; $row < $rank ; $row ++)
{
if ( $mat [ $row ][ $row ])
{
for ( $col = 0; $col < $R ; $col ++)
{
if ( $col != $row )
{
$mult = $mat [ $col ][ $row ] / $mat [ $row ][ $row ];
for ( $i = 0; $i < $rank ; $i ++)
$mat [ $col ][ $i ] -= $mult * $mat [ $row ][ $i ];
}
}
}
else
{
$reduce = true;
for ( $i = $row + 1; $i < $R ; $i ++)
{
if ( $mat [ $i ][ $row ])
{
swap( $mat , $row , $i , $rank );
$reduce = false;
break ;
}
}
if ( $reduce )
{
$rank --;
for ( $i = 0; $i < $R ; $i ++)
$mat [ $i ][ $row ] = $mat [ $i ][ $rank ];
}
$row --;
}
}
return $rank ;
}
function display( $mat , $row , $col )
{
for ( $i = 0; $i < $row ; $i ++)
{
for ( $j = 0; $j < $col ; $j ++)
print ( " $mat[$i][$j]" );
print ( "\n" );
}
}
$mat = array ( array (10, 20, 10),
array (-20, -30, 10),
array (30, 50, 0));
print ( "Rank of the matrix is : " .rankOfMatrix( $mat ));
?>
|
Javascript
<script>
var R = 3;
var C = 3;
function swap(mat, row1 , row2 , col)
{
for (i = 0; i < col; i++)
{
var temp = mat[row1][i];
mat[row1][i] = mat[row2][i];
mat[row2][i] = temp;
}
}
function display(mat,row , col)
{
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
document.write( " "
+ mat[i][j]);
document.write( '<br>' );
}
}
function rankOfMatrix(mat)
{
var rank = C;
for (row = 0; row < rank; row++)
{
if (mat[row][row] != 0)
{
for (col = 0; col < R; col++)
{
if (col != row)
{
var mult =
mat[col][row] /
mat[row][row];
for (i = 0; i < rank; i++)
mat[col][i] -= mult
* mat[row][i];
}
}
}
else
{
reduce = true ;
for ( var i = row + 1; i < R; i++)
{
if (mat[i][row] != 0)
{
swap(mat, row, i, rank);
reduce = false ;
break ;
}
}
if (reduce)
{
rank--;
for (i = 0; i < R; i ++)
mat[i][row] = mat[i][rank];
}
row--;
}
}
return rank;
}
var mat = [[10, 20, 10],
[-20, -30, 10],
[30, 50, 0]];
document.write( "Rank of the matrix is : "
+ rankOfMatrix(mat));
</script>
|
Output
Rank of the matrix is : 2
Time complexity: O(row x col x rank).
Auxiliary Space: O(1)
Since above rank calculation method involves floating point arithmetic, it may produce incorrect results if the division goes beyond precision. There are other methods to handle.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Dec, 2022
Like Article
Save Article