Javascript Program for Diagonally Dominant Matrix
Last Updated :
12 Sep, 2024
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".
Example:
JavaScript
// JavaScript 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 (let i = 0; i < n; i++) {
// for each column, finding
//sum of each row.
let sum = 0;
for (let 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 code
let n = 3;
let m = [[3, -2, 1],
[1, -3, 2],
[-1, 2, 4]];
if (isDDM(m, n))
console.log("YES");
else
console.log("NO");
Complexity Analysis:
- Time Complexity: O(N2)
- Auxiliary Space: O(1)
Please refer complete article on Diagonally Dominant Matrix for more details!
Similar Reads
Javascript Program for Mirror of matrix across diagonal Given a 2-D array of order N x N, print a matrix that is the mirror of the given tree across the diagonal. We need to print the result in a way: swap the values of the triangle above the diagonal with the values of the triangle below it like a mirror image swap. Print the 2-D array obtained in a mat
4 min read
Java 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
3 min read
Python 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
3 min read
C++ 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 dominan
3 min read
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
6 min read
Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix Given a matrix of order NxN, the task is to find the minimum number of steps to convert given matrix into Diagonally Dominant Matrix. In each step, the only operation allowed is to decrease or increase any element by 1.Examples: Input: mat[][] = {{3, 2, 4}, {1, 4, 4}, {2, 3, 4}} Output: 5 Sum of the
6 min read