Sparse Matrix Compression (Row Wise)

This program compresses sparse matrix row wise.

Problem Statement

Write a Java program to compress a 3x3 sparse matrix row wise.

Source Code

1 public class SparseMatrixRowCompression {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Sparse Matrix Row Compression");
4 int[][] A = {
5 {0,0,3},
6 {0,0,0},
7 {4,0,0}
8 };
9 for(int i=0;i<3;i++){
10 System.out.print("Row "+i+": ");
11 for(int j=0;j<3;j++){
12 if(A[i][j]!=0) System.out.print("("+j+","+A[i][j]+") ");
13 }
14 System.out.println();
15 }
16 }
17 }

Program Output

Eduinq Sparse Matrix Row Compression
Row 0: (2,3) 
Row 1: 
Row 2: (0,4) 

Explanation

Each row compressed by storing only non zero values with column index. - Reduces memory usage. - Together demonstrates row wise compression. Conclusion: Shows how to compress sparse matrix row wise.

Quick Links to Explore