JavaScript program to check if a matrix is symmetric


A symmetric matrix is a special case of a matrix where both the matrix and the transpose of the matrix are the same. A matrix is a set of integers or numbers stored in a rectangular form that is equivalent to the 2D array and the transpose of the matrix is also a matrix that we can get by replacing all the rows with columns. We will be given a matrix and have to print whether it is a symmetric matrix or not.

Input

Mat = [[1, 2, 3],
	   [2, 3, 8],
	   [3, 8, 0]]

Output

Yes, the given matrix is the symmetric matrix. 

Explanation

As we know the transpose matrix is the matrix that has the columns replaced by the rows and rows by the columns so here there first row is the same as the first column, the second row, column, and third row, the column is also same.

Input

Mat = [[1, 2, 3],
	   [2, 3, 9],
	   [3, 8, 0]]

Output

No, the given matrix is not a symmetric matrix. 

Explanation

In the given matrix the transpose matrix will be −

Trans: [[1, 2, 3],
	    [2, 3, 8],
	    [3, 9, 0]]

We can see that the second and third rows or second and third columns are not the same.

Note − As we have seen that the transpose of a given matrix can be formed by swapping rows and columns with each other means if the dimensions of the matrix are N*M then the dimensions of the transpose matrix will be M*N. This means for the matrix to be symmetric matrix N must be equal to M and it leads to the square matrix.

Naive Approach

In this approach, we will first get the transpose matrix by creating a new matrix and storing the elements of the row’s column wise. Then we will simply traverse over the both matrices and compare them. If they mis-match at any index, then we will return false else we will return true.

Example

// function to find the transpose of the given matrix
function getTranspose(mat){   
 
   // getting the number of rows present in the given matrix. 
   var n = mat.length; 
   
   // getting the number of columns present in the given matrix. 
   var m = mat.length;   
   
   // creating a new array to store the transpose matrix 
   
   // new array will have m rows and n columns 
   var transP = new Array(m) 
   
   // traversing over the given matrix column-wise 
   for(var i = 0;i < m; i++){
      var cur = new Array(n);
      for(var j = 0; j<n; j++){
         cur[j] = mat[j][i];
      }
      transP[i] = cur;
   }
   
   // returing tranpose of the given matrix 
   return transP;
}

// function to check if the given matrix is symmetric or not
function check(mat){
   var n = mat.length;
   var m = mat[0].length;
   
   // matrix must be a square matrix 
   if(n != m){
      return false;
   }    
   
   // getting tranpose of the given matrix 
   var transP = getTranspose(mat);  
   
   // checking if both matrices are equal
   for(var i = 0; i<n ;i++){
      for(var j = 0; j<n ;j++){
         if(mat[i][j] != transP[i][j]){
            return false;
         }
      }
   }
   return true;
}

// defining the matrix 
var mat = [[1, 2, 3],
           [2, 3, 8],
           [3, 8, 0]]
console.log("The given matrix is: ")
console.log(mat);
if(check(mat)){
   console.log("The given matrix is a symmetric matrix")
}
else{
   console.log("The given matrix is not a symmetric matrix")
}

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the size of the given matrix.

The space complexity of the above code is O(N*N), as we are using extra space to store the transpose matrix elements.

Efficient Approach

Transpose matrix can be get by swapping rows and columns means each column is equal to the corresponding row. Hence value present at any index (i,j) will be equal to the value present at the (j,i) in the given matrix.

Example

// function to check if the given matrix is symmetric or not
function check(mat){
   var n = mat.length;
   var m = mat[0].length;  
   
   // matrix must be a square matrix 
   if(n != m){
      return false;
   }  
   
   // checking if mat[i][j] is equal to mat[j][i] or not
   for(var i = 0; i<n ;i++){
      for(var j = 0; j<i ;j++){
         if(mat[i][j] != mat[j][i]){
            return false;
         }
      }
   }
   return true;
}

// defining the matrix 
var mat = [[1, 2, 3],
           [2, 3, 8],
           [3, 9, 0]]
console.log("The given matrix is: ")
console.log(mat);
if(check(mat)){
   console.log("The given matrix is a symmetric matrix")
}
else{
   console.log("The given matrix is not a symmetric matrix")
}

Time and Space Complexity

The time complexity of the above code is O(N*N), where N is the size of the given matrix.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In the above tutorial, we have implemented a JavaScript code to find whether the given matrix is symmetric matrix or not. A symmetric matrix is a special case of a matrix where both the matrix and the transpose of the matrix are the same and transpose of the matrix can be get by swapping the rows and columns. A matrix must be a square matrix to be symmetric matrix. We have implemented two approaches with O(N*N) time and O(N*N), and O(1) space complexity.

Updated on: 20-Apr-2023

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements