Python Program for Diagonally Dominant Matrix
Last Updated :
25 May, 2022
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".
Python3
# Python Program to check
# whether given matrix is
# Diagonally Dominant Matrix.
# check the 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)
Output :
YES
Time Complexity: O(N2)
Auxiliary Space: O(1)
Please refer complete article on Diagonally Dominant Matrix for more details!
Similar Reads
Python sympy | Matrix.diagonalize() method With the help of sympy.Matrix().diagonalize() method, we can diagonalize a matrix. diagonalize() returns a tuple (P, D), where D is diagonal and M = PDP^{-1}. Syntax: Matrix().diagonalize() Returns: Returns a tuple of matrix where the second element represents the diagonal of the matrix. Example #1:
1 min read
Python | numpy.fill_diagonal() method With the help of numpy.fill_diagonal() method, we can get filled the diagonals of numpy array with the value passed as the parameter in numpy.fill_diagonal() method. Syntax : numpy.fill_diagonal(array, value) Return : Return the filled value in the diagonal of an array. Example #1 : In this example
1 min read
Python Program for Program to Print Matrix in Z form Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 Python3 # Python program to print
2 min read
Program to check Identity Matrix Given a square matrix mat[][] of order n*n, the task is to check if it is an Identity Matrix.Identity Matrix: A square matrix is said to be an identity matrix if the elements of main diagonal are one and all other elements are zero. The identity Matrix is also known as the Unit Matrix. Examples: Inp
5 min read
Python Program for Maximum size square sub-matrix with all 1s Write a Python program for a given binary matrix, the task is to find out the maximum size square sub-matrix with all 1s. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Approach: Let the given binary matrix be M[R][C]. The idea of the algorithm is to construct an
4 min read