[18 Feb 2020] Program to check if a matrix is symmetric
- Get link
- X
- Other Apps
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 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
// Simple c++ code for check a matrix is
// symmetric or not.
#include <iostream>
using
namespace
std;
const
int
MAX = 100;
// Fills transpose of mat[N][N] in tr[N][N]
void
transpose(
int
mat[][MAX],
int
tr[][MAX],
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
bool
isSymmetric(
int
mat[][MAX],
int
N)
{
int
tr[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
int
main()
{
int
mat[][MAX] = { { 1, 3, 5 },
{ 3, 2, 4 },
{ 5, 4, 1 } };
if
(isSymmetric(mat, 3))
cout <<
"Yes"
;
else
cout <<
"No"
;
return
0;
}
// 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)
Auxiliary Space : O(N x N)
// 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" ); } } // This article is contributed by vt_m. |
Output:
Yes
# Efficient Python code for check a matrix is # symmetric or not. # Returns true if mat[N][N] is symmetric, else false 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 # Driver code mat = [ [ 1 , 3 , 5 ], [ 3 , 2 , 4 ], [ 5 , 4 , 1 ] ] if (isSymmetric(mat, 3 )): print "Yes" else : print "No" # This code is contributed by Sachin Bisht |
Output:
Yes
Time Complexity : O(N x N)
Auxiliary Space : O(1)
Auxiliary Space : O(1)
- Get link
- X
- Other Apps
Comments
Post a Comment