Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Diagonally Dominant Matrix
A diagonally dominant matrix is a square matrix where the absolute value of each diagonal element is greater than or equal to the sum of absolute values of all other elements in that row.
In this article, we'll write a JavaScript program to check if a given matrix is diagonally dominant. This requires understanding 2D arrays, nested loops, and conditional statements.
Formula
For a matrix to be diagonally dominant, this condition must hold for all rows:
$$\mathrm{|\:a_{ii}\:|\:\geq\:\displaystyle\sum\limits_{j ? i}\:|\:a_{ij}|}$$
Where aij denotes the entry in the ith row and jth column.
Example
Input Matrix: [[6, -2, 0, 0], [2, 8, -3, 0], [1, 2, 9, -4], [0, 1, -2, 7]] Checking each row: Row 0: |6| >= |-2| + |0| + |0| ? 6 >= 2 ? Row 1: |8| >= |2| + |-3| + |0| ? 8 >= 5 ? Row 2: |9| >= |1| + |2| + |-4| ? 9 >= 7 ? Row 3: |7| >= |0| + |1| + |-2| ? 7 >= 3 ? Output: Diagonally Dominant Matrix
Algorithm Steps
The algorithm follows these steps with O(n²) time complexity:
- Iterate through each row of the matrix using a for loop
- For each row, get the absolute value of the diagonal element
- Calculate the sum of absolute values of all non-diagonal elements in that row
- Use conditional statements to compare diagonal element with the sum
- Return false immediately if any row fails the condition
- Return true if all rows satisfy the diagonal dominance condition
JavaScript Implementation
const matrix = [
[6, -2, 0, 0],
[2, 8, -3, 0],
[1, 2, 9, -4],
[0, 1, -2, 7]
];
console.log("Input Matrix:");
console.log(matrix);
function isDiagonallyDominant(matrix) {
for (let i = 0; i = ${sum} ? ${diagonal >= sum ? '?' : '?'}`);
}
return true;
}
if (isDiagonallyDominant(matrix)) {
console.log("\nResult: Given matrix is Diagonally Dominant");
} else {
console.log("\nResult: Given matrix is NOT Diagonally Dominant");
}
Input Matrix: [ [ 6, -2, 0, 0 ], [ 2, 8, -3, 0 ], [ 1, 2, 9, -4 ], [ 0, 1, -2, 7 ] ] Row 0: |6| >= 2 ? ? Row 1: |8| >= 5 ? ? Row 2: |9| >= 7 ? ? Row 3: |7| >= 3 ? ? Result: Given matrix is Diagonally Dominant
Testing with Non-Dominant Matrix
const nonDominantMatrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 1]
];
console.log("Testing non-dominant matrix:");
console.log(nonDominantMatrix);
if (isDiagonallyDominant(nonDominantMatrix)) {
console.log("\nResult: Matrix is Diagonally Dominant");
} else {
console.log("\nResult: Matrix is NOT Diagonally Dominant");
}
Testing non-dominant matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 1 ] ] Row 0: |1| >= 5 ? ? Result: Matrix is NOT Diagonally Dominant
Key Points
- The matrix must be square (n × n) for diagonal dominance to be meaningful
- We use
Math.abs()to handle negative values correctly - The algorithm short-circuits and returns false as soon as one row fails
- Time complexity is O(n²) due to nested loops examining each element
Conclusion
A diagonally dominant matrix is checked by ensuring each diagonal element's absolute value exceeds the sum of absolute values of other elements in its row. This property is important in numerical analysis and iterative solving methods.
