A square matrix is said to be a symmetric matrix if the transpose of the matrix is the same as the given matrix. The symmetric matrix can be obtained 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
Method 1:
A Simple solution is to do the following.
1) Create a transpose of the given matrix.
2) Check if transpose and given matrices are the same or not.
Python
def transpose(mat, tr, N):
for i in range (N):
for j in range (N):
tr[i][j] = mat[j][i]
def isSymmetric(mat, N):
tr = [[ 0 for j in range ( len (mat[ 0 ]))] for i in range ( len (mat))]
transpose(mat, tr, N)
for i in range (N):
for j in range (N):
if (mat[i][j] ! = tr[i][j]):
return False
return True
mat = [[ 1 , 3 , 5 ], [ 3 , 2 , 4 ], [ 5 , 4 , 1 ]]
if (isSymmetric(mat, 3 )):
print "Yes"
else :
print "No"
|
Time Complexity : O(N x N)
Auxiliary Space : O(N x N)
Method 2:
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].
Python
def isSymmetric(mat, N):
for i in range (N):
for j in range (N):
if (mat[i][j] ! = mat[j][i]):
return False
return True
mat = [[ 1 , 3 , 5 ], [ 3 , 2 , 4 ], [ 5 , 4 , 1 ]]
if (isSymmetric(mat, 3 )):
print "Yes"
else :
print "No"
|
Time Complexity : O(N x N)
Auxiliary Space : O(1)
Method 3: Using List Comprehension
Python3
def isSymmetric(mat, N):
transmat = [[(mat[j][i]) for j in range (N)] for i in range (N)]
if (mat = = transmat):
return True
return False
mat = [[ 1 , 3 , 5 ], [ 3 , 2 , 4 ], [ 5 , 4 , 1 ]]
if (isSymmetric(mat, 3 )):
print ( "Yes" )
else :
print ( "No" )
|
Time Complexity : O(N*N)
Auxiliary Space : O(N*N)
Please refer complete article on Program to check if a matrix is symmetric for more details!
Method 4:Using numpy.array() and numpy.transpose() :
Algorithm:
- Convert the given matrix into its transpose using numpy’s transpose method and store it in a new variable named “transmat”.
- Check if the original matrix is equal to its transpose using numpy’s array_equal method.
- If the matrices are equal, return True, otherwise return False.
Python3
import numpy as np
def isSymmetric(mat, N):
transmat = np.array(mat).transpose()
if np.array_equal(mat, transmat):
return True
return False
mat = [[ 1 , 3 , 5 ], [ 3 , 2 , 4 ], [ 5 , 4 , 1 ]]
if (isSymmetric(mat, 3 )):
print ( "Yes" )
else :
print ( "No" )
|
Output:
Yes
Time complexity:
The time complexity is O(N^2) because the transpose operation takes O(N^2) time and the array_equal operation takes O(N^2) time.
Auxiliary Space:
The space complexity is O(N^2) because we are creating a new matrix of size N x N for storing the transpose of the original matrix.
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 :
29 Mar, 2023
Like Article
Save Article