Matrices are an important tool in computer science and mathematics and can be used to quickly approximate difficult calculations. A matrix is a collection of numbers organized in rows and columns that can represent data or a mathematical problem.
Through this article, we will understand the diagonal dominant matrix. We will study the concepts, algorithms, and examples of diagonally dominant matrices, as well as their implementation in various programming languages.
We can call a square matrix diagonally dominant if, for each row in the matrix, the size of the diagonal entry in the row is greater than or equal to the sum of the sizes of all non-diagonal entries. Simply put, if the sum of the elements in the matrix except the diagonal elements is less than the diagonal matrix.
If we have a square matrix a containing i rows and j columns, we can use mathematical equations to represent it as a diagonally dominant matrix -
$$\mathrm{|\:a_{ii}\:|\:\geq\:\displaystyle\sum\limits_{jeq\:i}\:|\:a_{ij} |} $$ is mine where aij represents the entries in columns i and j
A = [ [6, -2, 0, 0], [2, 8, -3, 0], [1, 2, 9, -4], [0, 1, -2, 7] ]
This matrix is diagonally dominant because it satisfies the following conditions -
|a11| ≥ |a12| + |a13| + |a14| == |+6| ≥ |+2| + |+1| + |+0| |a22| ≥ |a21| + |a23| + |a24| == |+8| ≥ |+2| + |+3| + |+0| |a33| ≥ |a31| + |a32| + |a34| == |+9| ≥ |+1| + |+2| + |+4| |a44| ≥ |a41| + |a42| + |a43| == |+7| ≥ |+0| + |+1| + |+2|
Given a square matrix, write a JavaScript program to check whether the matrix is diagonally dominant.
Let us consider a 3x3 matrix -
| 4 -1 0 | | -1 4 -1| | 0 -1 4 |
Here, the diagonal elements of each row are 4, 4, and 4, which are all greater than the sum of the absolute values of the other elements in the row. Therefore, this matrix is diagonally dominant.
Now let’s look at the solutions to the above problems.
The brute force method involves iterating through each row of the matrix and determining whether the diagonal element is greater than the sum of the absolute values of the other elements in the row.
Iterate over the rows of the matrix.
Calculate the sum of the absolute values of the other components in each row.
Check whether the diagonal elements of the row are greater than or equal to the sum determined in step 2.
If the diagonal element is greater than or equal to the sum, continue iterating to the next row.
If the diagonal elements are less than the sum, return false, indicating that the matrix is not diagonally dominant.
<!DOCTYPE html> <html> <body> <div id="matrix"></div> <div id="output"></div> <script> function isDiagonallyDominant(matrix) { const rows = matrix.length; const cols = matrix[0].length; for(let i = 0; i < rows; i++) { let sum = 0; for(let j = 0; j < cols; j++) { if(i !== j) { sum += Math.abs(matrix[i][j]); } } if(Math.abs(matrix[i][i]) < sum) { return false; } } return true; } const matrix = [[4, -1, 0], [-1, 4, -1], [0, -1, 4]]; const output = isDiagonallyDominant(matrix) ? 'Matrix is diagonally dominant.' : 'Matrix is not diagonally dominant.'; document.getElementById('matrix').innerHTML = 'Matrix: ' + JSON.stringify(matrix); document.getElementById('output').innerHTML = 'Output: ' + output; </script> </body> </html>
Time complexity: O(n2), where n is the size of the matrix.
In this method, we sort the absolute value of each row in descending order. We then determine whether the diagonal elements of the row are greater than or equal to the largest sum of n-1 absolute values, where n is the size of the matrix.
Iterate over the rows of the matrix.
Sort row items by absolute value in descending order.
Add the largest n-1 absolute values, where n is the size of the matrix.
Check whether the diagonal elements of the row are greater than or equal to the sum determined in step 3.
If the diagonal element is greater than or equal to the sum, continue iterating to the next row.
If the diagonal elements are less than the sum, return false, indicating that the matrix is not diagonally dominant.
<!DOCTYPE html> <html> <body> <h2>Diagonally Dominant Matrix</h2> <p id="matrix"></p> <p id="output"></p> <script> function isDiagonallyDominant(matrix) { const rows = matrix.length; const cols = matrix[0].length; for(let i = 0; i < rows; i++) { const sortedRow = matrix[i].map(Math.abs).sort((a, b) => b - a); const sum = sortedRow.slice(1, cols).reduce((acc, val) => acc + val, 0); if(sortedRow[0] < sum) { return false; } } return true; } // Example matrix const matrix = [[4, -1, 0], [-1, 4, -1], [0, -1, 4]]; // Display input matrix const matrixElement = document.getElementById("matrix"); matrixElement.innerHTML = "Input Matrix: <br>" + JSON.stringify(matrix); // Check if the matrix is diagonally dominant const isDominant = isDiagonallyDominant(matrix); // Display output const outputElement = document.getElementById("output"); outputElement.innerHTML = "Is diagonally dominant: " + isDominant; </script> </body> </html>
Time complexity: O(n2 log n), where n is the size of the matrix.
In this method, we first scale each row of the matrix so that its diagonal elements are equal to 1. We then see if the absolute value of the other entries in the row is less than 1.
Iterate over the rows of the matrix.
Identifies the row with the highest absolute value.
Scale rows until diagonal elements equal 1.
Check whether the absolute value of the remaining entries in the row is less than 1.
Returns true if all rows meet the criteria in step 4, indicating that the matrix is diagonally dominant.
If any row does not meet the requirements of step 4, return false, indicating that the matrix is not diagonally dominant.
<!DOCTYPE html> <html> <body> <h3>Diagonally Dominant Matrix</h3> <p>Matrix:</p> <pre id="matrix">
Is diagonally dominant:
<script> function isDiagonallyDominant(matrix) { const rows = matrix.length; const cols = matrix[0].length; for(let i = 0; i < rows; i++) { const maxAbsVal = Math.max(...matrix[i].map(Math.abs)); if(maxAbsVal === 0) { return false; } const scale = 1 / maxAbsVal; for(let j = 0; j < cols; j++) { matrix[i][j] *= scale; } const sum = matrix[i].slice(0, i).reduce((acc, val) => acc + Math.abs(val), 0) + matrix[i].slice(i+1, cols).reduce((acc, val) => acc + Math.abs(val), 0); if(sum >= 1) { return false; } } return true; } const matrix = [[4, -1, 0], [-1, 4, -1], [0, -1, 4]]; document.getElementById('matrix').innerHTML = matrix.map(row => row.join(' ')).join(''); document.getElementById('output').innerHTML = isDiagonallyDominant(matrix) ? 'true' : 'false'; </script>