Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmJava Program to check if a matrix is symmetric

Java Program to check if a matrix is symmetric

A square matrix is said to be symmetric matrix if the transpose of the matrix is same as the given matrix. Symmetric matrix can be obtain by changing row to column and column to row.

Examples: 

Input : 1 2 3
        2 1 4
        3 4 3
Output : Yes

Input : 3 5 8
        3 4 7
        8 5 3
Output : No

A Simple solution is to do following. 

1) Create transpose of given matrix. 
2) Check if transpose and given matrices are same or not.  

Java




// Simple java code for check a matrix is
// symmetric or not.
  
import java.io.*;
  
class GFG {
      
  
  
 static int  MAX = 100;
  
// Fills transpose of mat[N][N] in tr[N][N]
 static void transpose(int mat[][], int tr[][], int N)
{
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            tr[i][j] = mat[j][i];
}
  
// Returns true if mat[N][N] is symmetric, else false
 static boolean isSymmetric(int mat[][], int N)
{
    int tr[][] = new int[N][MAX];
    transpose(mat, tr, N);
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            if (mat[i][j] != tr[i][j])
                return false;
    return true;
}
  
// Driver code
    public static void main (String[] args)
 {
          
        int mat[][] = { { 1, 3, 5 },
                    { 3, 2, 4 },
                    { 5, 4, 1 } };
  
    if (isSymmetric(mat, 3))
        System.out.println( "Yes");
    else
        System.out.println ( "No");
      
    }
}


Output : 

 Yes

Time Complexity : O(N x N) 
Auxiliary Space : O(N x N)

An Efficient solution to check a matrix is symmetric or not is to compare matrix elements without creating a transpose. We basically need to compare mat[i][j] with mat[j][i].  

Java




// Efficient Java code for check a matrix is
// symmetric or no
  
import java.io.*;
  
class GFG {
     
  
static int MAX = 100;
  
// Returns true if mat[N][N]
// is symmetric, else false
 static boolean isSymmetric(int mat[][], int N)
{
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            if (mat[i][j] != mat[j][i])
                return false;
    return true;
}
  
// Driver code
      
    public static void main (String[] args)
 {
            int mat[][] = { { 1, 3, 5 },
                    { 3, 2, 4 },
                    { 5, 4, 1 } };
  
    if (isSymmetric(mat, 3))
        System.out.println(  "Yes");
    else
          
        System.out.println("NO");
          
    }
}
// 


Output: 

Yes

Time Complexity : O(N x N) 
Auxiliary Space : O(1)

Please refer complete article on Program to check if a matrix is symmetric for more details!

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 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments