📜  对角占优矩阵的Java程序

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

对角占优矩阵的Java程序

在数学中,如果对于矩阵的每一行,一行中对角线项的大小大于或等于所有其他(非对角线)项的大小之和,则称方阵为对角线占优在那一排。更准确地说,矩阵A是对角占优的,如果

例如,矩阵

对角占优,因为
|一个11 | ≥ |a 12 | + |一个13 |因为|+3| ≥ |-2| + |+1|
|a 22 | ≥ |a 21 | + |a 23 |因为|-3| ≥ |+1| + |+2|
|a 33 | ≥ |a 31 | + |a 32 |因为|+4| ≥ |-1| + |+2|
给定一个nn列的矩阵A。任务是检查矩阵 A 是否对角占优。
例子 :

Input : A = { { 3, -2, 1 },
              { 1, -3, 2 },
              { -1, 2, 4 } };
Output : YES
Given matrix is diagonally dominant
because absolute value of every diagonal
element is more than sum of absolute values
of corresponding row.

Input : A = { { -2, 2, 1 },
              { 1, 3, 2 },
              { 1, -2, 0 } };
Output : NO

这个想法是针对行数运行从 i = 0 到 n-1 的循环,对于每一行,运行一个循环 j = 0 到 n-1 找到非对角元素的总和,即 i != j。并检查对角元素是否大于或等于总和。如果对于任何一行,它是假的,则返回假或打印“否”。否则打印“是”。

Java
// JAVA Program to check whether given matrix
// is Diagonally Dominant Matrix.
import java.util.*;
 
class GFG {
     
    // check the given given matrix is Diagonally
    // Dominant Matrix or not.
    static boolean isDDM(int m[][], int n)
    {
        // for each row
        for (int i = 0; i < n; i++)
        {       
      
            // for each column, finding
            //sum of each row.
            int sum = 0;
            for (int j = 0; j < n; j++)            
                sum += Math.abs(m[i][j]);       
      
            // removing the diagonal element.
            sum -= Math.abs(m[i][i]);
      
            // checking if diagonal element is less
            // than sum of non-diagonal element.
            if (Math.abs(m[i][i]) < sum)
                return false;
        
        }
 
        return true;
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 3;
        int m[][] = { { 3, -2, 1 },
                      { 1, -3, 2 },
                      { -1, 2, 4 } };
      
        if (isDDM(m, n))
             System.out.println("YES") ;
        else 
            System.out.println("NO");
     
    }
}
 
// This code is contributed by  Arnav Kr. Mandal.


输出 :

YES

时间复杂度: O(N 2 )

辅助空间: O(1)

有关详细信息,请参阅有关对角支配矩阵的完整文章!