📜  对角占优矩阵

📅  最后修改于: 2021-04-29 10:36:24             🧑  作者: Mango

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

例如,矩阵

对角占主导地位,因为
| a 11 | ≥| a 12 | + | a 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。并检查对角线元素是否大于或等于和。如果对于任何行,它为false,则返回false或打印“ No”。否则打印“是”。

C++
// CPP Program to check whether given matrix
// is Diagonally Dominant Matrix.
#include 
#define N 3
using namespace std;
 
// check the given given matrix is Diagonally
// Dominant Matrix or not.
bool isDDM(int m[N][N], 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 += abs(m[i][j]);       
 
        // removing the diagonal element.
        sum -= abs(m[i][i]);
 
        // checking if diagonal element is less
        // than sum of non-diagonal element.
        if (abs(m[i][i]) < sum)
            return false;
        
    }
 
    return true;
}
 
// Driven Program
int main()
{
    int n = 3;
    int m[N][N] = { { 3, -2, 1 },
                    { 1, -3, 2 },
                    { -1, 2, 4 } };
 
    (isDDM(m, n)) ? (cout << "YES") : (cout << "NO");
 
    return 0;
}


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.


Python3
# Python Program to check
# whether given matrix is
# Diagonally Dominant Matrix.
 
# check the given given
# matrix is Diagonally
# Dominant Matrix or not.
def isDDM(m, n) :
 
    # for each row
    for i in range(0, n) :        
     
        # for each column, finding
        # sum of each row.
        sum = 0
        for j in range(0, n) :
            sum = sum + abs(m[i][j])    
 
        # removing the
        # diagonal element.
        sum = sum - abs(m[i][i])
 
        # checking if diagonal
        # element is less than
        # sum of non-diagonal
        # element.
        if (abs(m[i][i]) < sum) :
            return False
 
    return True
 
# Driver Code
n = 3
m = [[ 3, -2, 1 ],
    [ 1, -3, 2 ],
    [ -1, 2, 4 ]]
 
if((isDDM(m, n))) :
    print ("YES")
else :
    print ("NO")
 
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# Program to check whether given matrix
// is Diagonally Dominant Matrix.
using System;
 
class GFG {
     
    // check the given given matrix is Diagonally
    // Dominant Matrix or not.
    static bool 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
    public static void Main()
    {
        int n = 3;
        int [,]m = { { 3, -2, 1 },
                    { 1, -3, 2 },
                    { -1, 2, 4 } };
     
        if (isDDM(m, n))
            Console.WriteLine("YES") ;
        else
            Console.WriteLine("NO");
     
    }
}
 
// This code is contributed by Vt_m.


PHP


Javascript


输出 :

YES