In mathematics, a square matrix is said to be diagonally dominant if for every row of the matrix, the magnitude of the diagonal entry in a row is larger than or equal to the sum of the magnitudes of all the other (non-diagonal) entries in that row. More precisely, the matrix A is diagonally dominant if

For example, The matrix

is diagonally dominant because
|a11| ? |a12| + |a13| since |+3| ? |-2| + |+1|
|a22| ? |a21| + |a23| since |-3| ? |+1| + |+2|
|a33| ? |a31| + |a32| since |+4| ? |-1| + |+2|
Given a matrix A of n rows and n columns. The task is to check whether matrix A is diagonally dominant or not.
Examples :
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
The idea is to run a loop from i = 0 to n-1 for the number of rows and for each row, run a loop j = 0 to n-1 find the sum of non-diagonal element i.e i != j. And check if diagonal element is greater than or equal to sum. If for any row, it is false, then return false or print “No”. Else print “YES”.
Implementation:
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
bool isDDM(int m[N][N], int n)
{
for (int i = 0; i < n; i++)
{
int sum = 0;
for (int j = 0; j < n; j++)
sum += abs(m[i][j]);
sum -= abs(m[i][i]);
if (abs(m[i][i]) < sum)
return false;
}
return true;
}
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
import java.util.*;
class GFG {
static boolean isDDM(int m[][], int n)
{
for (int i = 0; i < n; i++)
{
int sum = 0;
for (int j = 0; j < n; j++)
sum += Math.abs(m[i][j]);
sum -= Math.abs(m[i][i]);
if (Math.abs(m[i][i]) < sum)
return false;
}
return true;
}
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");
}
}
|
Python3
def isDDM(m, n) :
for i in range(0, n) :
sum = 0
for j in range(0, n) :
sum = sum + abs(m[i][j])
sum = sum - abs(m[i][i])
if (abs(m[i][i]) < sum) :
return False
return True
n = 3
m = [[ 3, -2, 1 ],
[ 1, -3, 2 ],
[ -1, 2, 4 ]]
if((isDDM(m, n))) :
print ("YES")
else :
print ("NO")
|
C#
using System;
class GFG {
static bool isDDM(int [,]m, int n)
{
for (int i = 0; i < n; i++)
{
int sum = 0;
for (int j = 0; j < n; j++)
sum += Math.Abs(m[i, j]);
sum -= Math.Abs(m[i, i]);
if (Math.Abs(m[i,i]) < sum)
return false;
}
return true;
}
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");
}
}
|
PHP
<?php
function isDDM( $m, $n)
{
for ($i = 0; $i < $n; $i++)
{
$sum = 0;
for ( $j = 0; $j < $n; $j++)
$sum += abs($m[$i][$j]);
$sum -= abs($m[$i][$i]);
if (abs($m[$i][$i]) < $sum)
return false;
}
return true;
}
$n = 3;
$m = array(array( 3, -2, 1 ),
array( 1, -3, 2 ),
array( -1, 2, 4 ));
if((isDDM($m, $n)))
echo "YES";
else
echo"NO";
?>
|
Javascript
<script>
function isDDM(m, n)
{
for (let i = 0; i < n; i++)
{
let sum = 0;
for (let j = 0; j < n; j++)
sum += Math.abs(m[i][j]);
sum -= Math.abs(m[i][i]);
if (Math.abs(m[i][i]) < sum)
return false;
}
return true;
}
let n = 3;
let m = [[ 3, -2, 1 ],
[ 1, -3, 2 ],
[ -1, 2, 4 ]];
if (isDDM(m, n))
document.write("YES") ;
else
document.write("NO");
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
19 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...