Check if a Matrix is Symmetric in C#



In this article, we are going to discuss how we can check if a matrix is symmetric or not using C#.

What is a Symmetric Matrix?

A symmetric matrix is a square matrix (matrix which has the same number of rows and columns) in which the element at position A[i][j] is equal to the element at A[j][i] for all i and j. In other simple words, we can say that a matrix is called a symmetric matrix if it is a square matrix that is equal to its transpose. Below are a few examples of understanding a symmetric matrix:

Examples

Input

1 2 3
2 4 5
3 5 6

Output

True

Explanation: The above matrix is a square matrix and also we can check for every element matrix[I][j] equal to matrix[j][I]. So, the above matrix is a symmetric matrix.

Input

1 2 3 4
5 6 7 8
9 10 11 12

Output

False

Explanation: The above matrix is not a square matrix so it can never be a symmetric matrix.

Input

2 1 3
1 4 5
3 5 6

Output

True

Explanation: The above matrix is a square matrix and also we can check for every element matrix[I][j] equal to matrix[j][I]. So, the above matrix is a symmetric matrix.

Checking Symmetric Matrix

You can check whether a matrix is symmetric or not using multiple approaches. Here, we are mainly explaining the following two approaches:

Let's discuss these approaches in detail.

Checking Symmetric Matrix Using Brute Force

This is a simple and straightforward approach. Here are the steps for implementation:

  • We check if the matrix is a square matrix or not.
  • Now, for each element of the matrix, we compare matrix[i][j] with matrix[j][i].
  • If at any place we found matrix[i][j] not equal to matrix[j][i], the matrix is not symmetric.
  • If all checks pass, the matrix is symmetric.

Implementation Code

using System;

class SymmetricMatrix {
    static void Main() {
        int[,] matrix = {
            { 2, 1, 3 },
            { 1, 4, 5 },
            { 3, 5, 6 }
        };

        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        if (rows != cols) {
            Console.WriteLine("The matrix is not square, so it cannot be symmetric.");
            return;
        }

        bool isSymmetric = true;

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix[i, j] != matrix[j, i]) {
                    isSymmetric = false;
                    break;
                }
            }
            if (!isSymmetric) break;
        }

        Console.WriteLine(isSymmetric ? "The matrix is symmetric." : "The matrix is not symmetric.");
    }
}

Output

The matrix is symmetric.

Time Complexity: O(n²), as we use a nested loop to traverse the matrix.

Space Complexity: O(1), constant space.

Checking Symmetric Matrix Using Upper Triangle Comparison

Here are the steps for Implementation:

  • First, we check if the matrix is not a square matrix then it can never be a symmetric matrix.
  • Now, we only iterate over the upper triangle of the matrix.
  • For each i, we iterate j > i and compare matrix[i][j] with matrix[j][i].
  • If we find any mismatch, the matrix is not symmetric.
  • If all checks pass, the matrix is symmetric.

Implementation Code

using System;

class SymmetricMatrixCheckOptimized {
    static void Main() {
        int[,] matrix = {
            { 2, 1, 3 },
            { 1, 4, 5 },
            { 3, 5, 6 }
        };

        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        if (rows != cols) {
            Console.WriteLine("The matrix is not square, so it cannot be symmetric.");
            return;
        }

        bool isSymmetric = true;

        for (int i = 0; i < rows; i++) {
            for (int j = i + 1; j < cols; j++) {
                if (matrix[i, j] != matrix[j, i]) {
                    isSymmetric = false;
                    break;
                }
            }
            if (!isSymmetric) break;
        }

        Console.WriteLine(isSymmetric ? "The matrix is symmetric." : "The matrix is not symmetric.");
    }
}

Output

The matrix is symmetric.

Time Complexity: O(n²/2), as we only traverse half the elements.

Space Complexity: O(1), constant space.

Updated on: 2024-12-12T12:29:56+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started