Friday, July 10, 2026
HomeData Modelling & AIPhp Program for Diagonally Dominant Matrix

Php Program for Diagonally Dominant Matrix

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

PHP




<?php
// PHP Program to check whether
// given matrix is Diagonally
// Dominant Matrix.
 
// check the given  matrix
// is Diagonally Dominant Matrix or not.
function isDDM( $m, $n)
{
    // for each row
    for ($i = 0; $i < $n; $i++)
         
    {
        // for each column, finding
        // sum of each row.
        $sum = 0;
        for ( $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;
}
 
// Driver Code
$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";
 
// This code is contributed by SanjuTomar
?>


Output : 

YES

Time Complexity: O(N2)

Auxiliary Space: O(1)

Please refer complete article on Diagonally Dominant Matrix for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32519 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6901 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12111 POSTS0 COMMENTS
Shaida Kate Naidoo
7020 POSTS0 COMMENTS
Ted Musemwa
7263 POSTS0 COMMENTS
Thapelo Manthata
6978 POSTS0 COMMENTS
Umr Jansen
6968 POSTS0 COMMENTS