Java Program to Check Diagonally Dominant Matrix

In the previous article, we have seen Java Program to Check Involutory Matrix

In this article we are going to see how we can write a program to check whether matrix is Diagonally Dominant Matrix or not.

Java Program to Check Diagonally Dominant Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Note:

A matrix whose every row of the matrix diagonal entry in a row is larger than or equal to the sum of magnitude of all other entries in that row is called Diagonally Dominant Matrix.

Let’s see different ways to check whether matrix is Diagonally Dominant Matrix or not.

Method-1: Java Program to Check Diagonally Dominant Matrix By Static Initialization of Array Elements

Approach:

  • Declare and initialize a matrix.
  • For each column find the sum of each row.
  • Remove the diagonal element form the sum .
  • Check the diagonal element is less than result. If condition pass then it is not a diagonally dominant matrix .

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
       int mat[][] = { { 3, -2, 1 },{ 1, -3, 2 },{ -1, 2, 4 } }; 
       for (int x = 0; x < 3; x++) 
        {         
            // for each column, finding sum of each row. 
            int s = 0; 
            for (int y = 0; y < 3; y++)              
                s += Math.abs(mat[x][y]); 
                
            // removing the diagonal element. 
            s -= Math.abs(mat[x][x]); 
            
            // checking if diagonal element is less than sum of non-diagonal element. 
            if (Math.abs(mat[x][x]) < s)
            {
                System.out.println("Not a Diagonally Dominant Matrix ");
                System.exit(0); 
            }
        } 
  
        System.out.println("Diagonally Dominant Matrix "); 
    } 
}
Output:

Diagonally Dominant Matrix

Method-2: Java Program to Check Diagonally Dominant Matrix By Dynamic Initialization of Array Elements

Approach:

  • Take user input of  a matrix.
  • For each column find the sum of each row.
  • Remove the diagonal element form the sum .
  • Check the diagonal element is less than result. If condition pass, then it is not a diagonally dominant matrix .

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
        // Initializing the 3X3 matrix i.e. 2D array
                int mat[][] = new int[3][3];
        int row, col ;
        
        // Taking matrix input
        System.out.println("\nEnter matrix elements");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                mat[row][col] = sc.nextInt();
                
       for (int x = 0; x < 3; x++) 
        {         
            // for each column, finding sum of each row. 
            int s = 0; 
            for (int y = 0; y < 3; y++)              
                s += Math.abs(mat[x][y]);         
            // removing the diagonal element. 
            s -= Math.abs(mat[x][x]); 
            
            // checking if diagonal element is less than sum of non-diagonal element. 
            if (Math.abs(mat[x][x]) < s)
            {
                System.out.println("Not a Diagonally Dominant Matrix ");
                System.exit(0); 
            }
        } 
  
        System.out.println("Diagonally Dominant Matrix "); 
    } 
}

Output:

Case-1

Enter matrix elements
3 -2 1 
1 -3 2
-1 2 4 
Diagonally Dominant Matrix

Case-2

Enter matrix elements
3 -2 4
1 -3 2
-1 2 4 
Not a Diagonally Dominant Matrix

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: