Sparse Matrix Java

Hello everyone. Welcome to flower brackets blog. In this post we are going to learn sparse matrix java.

What is sparse matrix?

Sparse matrix is a two dimensional array also known as a sparse array. In this matrix majority of elements are zero and very few are non zero elements.

For example, consider two rows and three columns matrix. In this matrix there are only two values and the remaining are blank.

This vacant place is filled with zero.

Sparse Matrix Java

Why we are using sparse matrix?

Sparse matrix is used because sparse matrix reduces the scanning time and how exactly it reduces the scanning time is, if there is M X N matrix where m = 50 and n is also n = 50.

Also Read – Matrix Multiplication Java

This means, for searching all the elements we require 2500 times scanning to find the value.

So instead of scanning 2500 times you can use a sparse matrix. In sparse matrix you can directly get the values that exists in the matrix.

There are two ways to represent sparse matrix,

  1. three column form
  2. linked list

Three column form – Sparse Matrix Java

In three column form representation there are three columns row, column and value. For example, this is 4 X 4 matrix where the values are as shown below,

Sparse matrix java

We have to create a sparse matrix of the above 4 X 4 matrix. For that we have three columns row, column and value,

Sparse Matrix Java

In the above table first row is represented by total number of row, total number of column and total number of non-zero values existing in the above 4 X 4 matrix.

The first total number of rows in the above 4 X 4 matrix is 4. Total number of column is 4 and total number of non-zero values is 6.

Before going to next row here is how the indexing is done in matrix,

Sparse Matrix Java

As you can see above image, representation is in this format. For three column form representation we have to consider non-zero values.

In the next row of three column form representation, consider row 0 (of 4 X 4 matrix) at column 1 value is 3.

In next row (of 4 X 4 matrix) we have row 1 column 0 and value is 2. Next in the same row we have indexing value of column is 2 and value is 4.

Moving to the next row R2 value exists at first and third column. So the value is 1 and 2.

In the last row (of 4 X 4 matrix) that is R3, column 1 value is 3. Here’s the complete three column form representation table,

Sparse Matrix Java

Through this three column form representation we can directly access only non-zero values and we ignore the zero values.


Linked list – Sparse Matrix Java

Now we are going to learn linked list to represent sparse matrix in java. To represent linked list we require three nodes and three nodes has a different structure. Here’s the structure,

sparse matrix java

As you can see in the above image, first node has four columns. Where first column is total row, second is total column, third is total non-zero values in that matrix and last is a pointer that point to the next row.

Similar to previous three column form representation ( we had row, column, value ) after one two three column one pointer is required that would point next row.

Also Read – Matrix Transpose Java

Next is row node. In row node first is row number, second is pointer for next row (because is any matrix after scanning all the columns you have to move to next row. So there is one pointer that will take you to the next row) and third is pointer for column.

Last is column node. In column node first is column number, second is the value that exists in column and third is pointer for next value in the same row.

Let’s understand linked list representation with an example,

sparse matrix java

I am using the same 4 X 4 matrix used in three column form representation. So for linked list representation of 4 X 4 matrix first is head node.

This head node contains four row, four column and six non-zero values. Next is pointer that will point first row of the matrix.

First row means R0 ( refer image “Indexing” above ) and its next pointer point to next row R1. Next pointer point to row 2 (R2) and its next pointer point to last row R3.

So, in row 0 first column value is 3. Since there is no another value in the same row last pointer will be null ( see image above represented as X ).

Moving to next row, that is, row one (R1) pointer points to column 0 and value is 2. Then again it points to next column where column value is 4 and there is no other value in the same row so it is null.

Now second row ( R2 ) has two values so I have two nodes. The first in row 2 column 1 value is 1 and it points to next column value.

Column number 3 has value 2 and it does not point to next row so it is null.

Coming to last row, R3 the column node in column 1 we have value 3 and it does not point to any other non-zero value in the same row, so it is null.

So this is all about linked list representation of sparse matrix.


Example: sparse matrix java

import java.util.Scanner;

public class SparseMatrix
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter dimensions of matrix : ");
 
      int x = sc.nextInt();
      int y = sc.nextInt();
      double givenMatrix[][] = new double[x][y];
 
      int checkZero = 0;
      System.out.println("Please enter elements of matrix : ");
 
      for(int a = 0; a < x; a++)
      {
         for(int b = 0; b < y; b++)
         {
            givenMatrix[a][b] = sc.nextDouble();
            if(givenMatrix[a][b] == 0)
            {
               checkZero++;
            }
         }
      }
 
      if(checkZero > (x * y) / 2)
      {
         System.out.println("It is a sparse matrix!!!");
      }
      else
      {
         System.out.println("Sorry!! It is not a sparse matrix.");
      }
      sc.close();
   }
}

Output:

sparse matrix java


conclusion

This is all about sparse matrix representation in java. In this post you have learnt sparse matrix definition, sparse matrix linked list in java and three column form.

I hope you have understood the concept of sparse matrix algorithm. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like