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.
- Create transpose of given matrix.
- Check if transpose and given matrices are same or not,
Implementation:
C++
#include <iostream>
using namespace std;
const int MAX = 100;
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];
}
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 ;
}
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;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
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];
}
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 ;
}
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" );
}
}
|
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"
|
C#
using System;
class GFG {
static int MAX = 100;
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];
}
static bool 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 ;
}
public static void Main ()
{
int [,]mat = { { 1, 3, 5 },
{ 3, 2, 4 },
{ 5, 4, 1 } };
if (isSymmetric(mat, 3))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isSymmetric( $mat , $N )
{
$tr = array ( array ());
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
$tr [ $i ][ $j ] = $mat [ $j ][ $i ];
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
if ( $mat [ $i ][ $j ] != $tr [ $i ][ $j ])
return false;
return true;
}
$mat = array ( array (1, 3, 5),
array (3, 2, 4),
array (5, 4, 1));
if (isSymmetric( $mat , 3))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
let MAX = 100;
function transpose(mat, tr, N)
{
for (let i = 0; i < N; i++)
for (let j = 0; j < N; j++)
tr[i][j] = mat[j][i];
}
function isSymmetric(mat, N)
{
let tr = new Array(N);
for (let i = 0; i < N; i++)
{
tr[i] = new Array(MAX);
}
transpose(mat, tr, N);
for (let i = 0; i < N; i++)
for (let j = 0; j < N; j++)
if (mat[i][j] != tr[i][j])
return false ;
return true ;
}
let mat = [ [ 1, 3, 5 ],
[ 3, 2, 4 ],
[ 5, 4, 1 ] ];
if (isSymmetric(mat, 3))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
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].
Implementation:
C++
#include <iostream>
using namespace std;
const int MAX = 100;
bool isSymmetric( int mat[][MAX], 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 ;
}
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;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
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 ;
}
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" );
}
}
|
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"
|
C#
using System;
class GFG
{
static bool 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 ;
}
public static void Main ()
{
int [,]mat = { { 1, 3, 5 },
{ 3, 2, 4 },
{ 5, 4, 1 } };
if (isSymmetric(mat, 3))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
$MAX = 100;
function isSymmetric( $mat , $N )
{
for ( $i = 0; $i < $N ; $i ++)
for ( $j = 0; $j < $N ; $j ++)
if ( $mat [ $i ][ $j ] != $mat [ $j ][ $i ])
return false;
return true;
}
$mat = array ( array (1, 3, 5),
array (3, 2, 4),
array (5, 4, 1));
if (isSymmetric( $mat , 3))
echo ( "Yes" );
else
echo ( "No" );
?>
|
Javascript
<script>
let MAX = 100;
function isSymmetric(mat, N)
{
for (let i = 0; i < N; i++)
for (let j = 0; j < N; j++)
if (mat[i][j] != mat[j][i])
return false ;
return true ;
}
let mat = [ [ 1, 3, 5 ],
[ 3, 2, 4 ],
[ 5, 4, 1 ] ];
if (isSymmetric(mat, 3))
document.write( "Yes" );
else
document.write( "NO" );
</script>
|
Time Complexity : O(N x N)
Auxiliary Space : O(1)
This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.