📜  Java程序检查矩阵是否对称

📅  最后修改于: 2022-05-13 01:55:36.893000             🧑  作者: Mango

Java程序检查矩阵是否对称

如果矩阵的转置与给定矩阵相同,则称方阵为对称矩阵。可以通过将行改为列、将列改为行来获得对称矩阵

例子:

Input : 1 2 3
        2 1 4
        3 4 3
Output : Yes

Input : 3 5 8
        3 4 7
        8 5 3
Output : No

一个简单的解决方案是执行以下操作。

1)创建给定矩阵的转置。
2)检查转置矩阵和给定矩阵是否相同。

Java
// 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");
      
    }
}


Java
// 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.


输出 :

Yes

时间复杂度:O(N x N)
辅助空间:O(N x N)

检查矩阵是否对称的有效解决方案是在不创建转置的情况下比较矩阵元素。我们基本上需要比较 mat[i][j] 和 mat[j][i]。

Java

// 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.

输出:

Yes

时间复杂度:O(N x N)
辅助空间:O(1)

请参阅有关程序的完整文章以检查矩阵是否对称以获取更多详细信息!