How to Check Diagonally Dominant Matrix in Java?

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

If the absolute value of the diagonal element in a matrix is larger than or equal to the sum of the absolute values of the other components in that row, the matrix is said to be diagonally dominating. When resolving systems of linear equations in numerical analysis, this characteristic is helpful. Even if the coefficients of the equations are not precisely known, it guarantees that the solution method will converge to the right answer.

Here we have given a matrix which contains set of elements and as per the problem statement we have to check diagonally dominant matrix.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

Given matrix =

45 	22	23
24	99	26
27	28	79

• Diagonal elements are : 45, 29 and 79

• Row1: 45 >= (22+23)

• Row2: 99 > (24+26)

• Row3: 79 > (27+28)

• Hence the given matrix is a diagonal dominant matrix.

Instance-2

Given matrix =

5 	1	2
2	9	7
5	1	7

• Diagonal elements are : 5, 9 and 7

• Row1: 5 > (1+2)

• Row2: 9 >+ (2+7)

• Row3: 7 > (5+1)

• Hence the given matrix is a diagonal dominant matrix.

Instance-3

Given matrix =

45 	22	23
24	19	26
27	28	79

• Diagonal elements are: 45, 19 and 79

• Row1: 45 >= (22+23)

• Row2: 19 < (24+26)

• Row3: 79 > (27+28)

• In Row2, you can see diagonal value is less than sum of other 2 elemnts of the that row. Hence the given matrix is not a diagonal dominant matrix.

Algorithm

(Explained for 2nd Approach - By User Defined Method)

• Step-1 − The function isDiagonallyDominant takes a 2D matrix represented by a 2D integer array matrix.

• Step-2 − The variable n is set to the length of the matrix, which is assumed to be a square matrix.

• Step-3 − The outer for loop iterates over each row of the matrix.

• Step-4 − The variable sum is used to keep track of the sum of the absolute values of the elements in the current row, excluding the diagonal element.

• Step-5 − The inner for loop iterates over each column in the current row.

• Step-6 − If the current row and column indices (i and j) are not equal, then the absolute value of the element at that position is added to sum.

• Step-7 − If the absolute value of the diagonal element is less than sum, then the matrix is not diagonally dominant, and the function returns false.

• Step-8 − If all rows are processed without finding a non-diagonally dominant row, the function returns true, indicating that the matrix is diagonally dominant.

Syntax

The Math.abs() method in Java returns the absolute value of a number, which is the non-negative value of a given number regardless of its sign.

Below refers to the syntax of it −

Math.abs(inputMatrix[i][j])


where, ‘inputMatrix’ refers to the given matrix.

Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of matrix Elements

• By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Matrix Elements

In this approach, matrix elements will be initialized in the program. Then as per the algorithm check whether the given matrix is a diagonal dominant matrix or not.

Example

public class Main {
public static void main(String[] args) {
int n = 3;
int[][] inputMatrix = {{7,  2,  1}, {3,  8,  2}, {1,  1, 10}};

//print the given matrix
System.out.println("The given matrix is: ");
for (int i = 0; i < inputMatrix.length; i++) {
for (int j = 0; j < inputMatrix[0].length; j++) {
System.out.print(inputMatrix[i][j] + " ");
}
System.out.println();
}
boolean isDiagonalDominant = true;
for (int i = 0; i < inputMatrix.length; i++) {
int sum = 0;
for (int j = 0; j < inputMatrix[0].length; j++) {
if (i != j) {
sum += Math.abs(inputMatrix[i][j]);
}
}
if (Math.abs(inputMatrix[i][i]) < sum) {
isDiagonalDominant = false;
break;
}
}
if (isDiagonalDominant) {
System.out.println("The given matrix is a diagonal dominant matrix.");
} else {
System.out.println("The given matrix is not a diagonal dominant matrix.");
}
}
}


Output

The given matrix is:
7 2 1
3 8 2
1 1 10
The given matrix is a diagonal dominant matrix.


Approach-2: By Using User Defined Method

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm check whether the given matrix is a diagonal dominant matrix or not.

Example

public class Main {
public static void main(String[] args) {
int n = 3;
int[][] inputMatrix = {{11, 22, 33}, {44, 55, 66}, {77, 88, 99}};

//print the given matrix
System.out.println("The given matrix is: ");
for (int i = 0; i < inputMatrix.length; i++){
for (int j = 0; j < inputMatrix[0].length; j++){
System.out.print(inputMatrix[i][j] + " ");
}
System.out.println();
}
boolean result = isDiagonalAbsoluteValueGreaterThanSum(inputMatrix);
if(result==false)
System.out.println("The given matrix is not a diagonal dominant matrix.");
else
System.out.println("The given matrix is a diagonal dominant matrix.");
}

//user defined method
public static boolean isDiagonalAbsoluteValueGreaterThanSum(int[][] mat) {
int n = mat.length;
for (int a = 0; a < n; a++) {
int sum = 0;

// Sum the absolute values of elements in the current row excluding the diagonal element
for (int b = 0; b < n; b++) {
if (a != b) {
sum += Math.abs(mat[a][b]);
}
}

// Check if the absolute value of the diagonal element is less than the sum
if (Math.abs(mat[a][a]) < sum) {
return false;
}
}
return true;
}
}


Output

The given matrix is:
11 22 33
44 55 66
77 88 99
The given matrix is not a diagonal dominant matrix.


In this article, we explored different approaches to check diagonally dominant matrix by using Java programming language.

Updated on: 04-May-2023

129 Views