Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

matrix
datastructure
communitycreator

How to check if a given matrix is symmetric

abhilash

What is a symmetric matrix?

If the transpose of a square matrix is the same as the supplied matrix, then the matrix is symmetric.

For example, consider the matrix below.

[[1, 1, -1],
 [1, 2, 0],
 [-1, 0, 5]]

The transpose of the matrix is as follows.

[[1, 1, -1],
 [1, 2, 0],
 [-1, 0, 5]]

Since the transpose of the matrix is the same as the given matrix, the matrix is a symmetric matrix.

Algorithm

  1. Find the transpose of the matrix with the help of the algorithm described here.
  2. Compare the transpose obtained in Step 1 with the given matrix.
    1. If the matrices are equal, then the given matrix is a symmetric matrix.
    2. If the matrices are not equal, then the given matrix is not a symmetric matrix.
  • Time complexity - O(m*n)
  • Space complexity - O(m*n)

Code

import java.util.Arrays;

public class Main {

    private static void printMatrix(int[][] matrix){
        for(int[] row: matrix){
            System.out.println(Arrays.toString(row));
        }
    }

    private static int[][] transpose(int[][] matrix){
        int[][] transpose = new int[matrix[0].length][matrix.length];
        for(int i = 0;i < matrix.length; i++) for(int j = 0; j < matrix[0].length; j++) transpose[j][i] = matrix[i][j];
        return transpose;

    }

    private static boolean checkSymmetricMatrix(int[][] matrix){

        int[][] result = transpose(matrix);

        if(result.length != matrix.length || result[0].length != matrix[0].length) return false;

        for(int i=0;i < matrix.length; i++){
            for (int j = 0; j < matrix[i].length; j++)
                if(result[i][j] != matrix[i][j]) return false;
        }
        return true;
    }

    private static void wrapper(int[][] matrix){
        printMatrix(matrix);
        if(checkSymmetricMatrix(matrix)) System.out.println("The matrix is a symmetric matrix");
        else System.out.println("The matrix is not a symmetric matrix");
    }

    public static void main(String[] args){
        int[][] matrix = {{1, 1, -1},{1, 2, 0},{-1, 0, 5}};
        wrapper(matrix);
        System.out.println("-------------");
        int[][] matrix1 = {{4, 0},{5, -4}};
        wrapper(matrix1);
    }
}

Efficient algorithm

We can optimize the approach above. Instead of calculating the transpose and then comparing the matrices, we can compare the elements at (i, j) and (j, i). If the elements are not equal, then the matrix is not a symmetric matrix.

  • Time complexity - O(m*n)
  • Space complexity - O(1)
Check if the given matrix is symmetric or not
1 of 4
import java.util.Arrays;

public class Main {

    private static void printMatrix(int[][] matrix){
        for(int[] row: matrix){
            System.out.println(Arrays.toString(row));
        }
    }

    private static boolean checkSymmetricMatrix(int[][] matrix){
        for(int i=0;i < matrix.length; i++){
            for (int j = 0; j < matrix[i].length; j++)
                if(matrix[i][j] != matrix[j][i]) return false;
        }
        return true;
    }

    private static void wrapper(int[][] matrix){
        printMatrix(matrix);
        if(checkSymmetricMatrix(matrix)) System.out.println("The matrix is a symmetric matrix");
        else System.out.println("The matrix is not a symmetric matrix");
    }

    public static void main(String[] args){
        int[][] matrix = {{1, 1, -1},{1, 2, 0},{-1, 0, 5}};
        wrapper(matrix);
        System.out.println("-------------");
        int[][] matrix1 = {{4, 0},{5, -4}};
        wrapper(matrix1);
    }
}

RELATED TAGS

matrix
datastructure
communitycreator
RELATED COURSES

View all Courses

Keep Exploring