Java Program to Implement Sparse Matrix

«
»
This Java Program is to Implement Sparse Matrix.In the subfield of numerical analysis, a sparse matrix is a matrix populated primarily with zeros (Stoer & Bulirsch 2002, p. 619) as elements of the table. By contrast, if a larger number of elements differ from zero, then it is common to refer to the matrix as a dense matrix. The fraction of zero elements (non-zero elements) in a matrix is called the sparsity (density).

Here is the source code of the Java Program to Implement Sparse Matrix. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. public class SparseMatrix
  2. {
  3.     private int N;
  4.     private SparseArray sparsearray[];
  5.  
  6.     public SparseMatrix(int N)
  7.     {
  8.         this.N = N;
  9.         sparsearray = new SparseArray[N];
  10.         for (int index = 0; index < N; index++)
  11.         {
  12.             sparsearray[index] = new SparseArray(N);
  13.         }
  14.     }
  15.  
  16.     public void store(int rowindex, int colindex, Object value)
  17.     {
  18.         if (rowindex < 0 || rowindex > N)
  19.             throw new RuntimeException("row index out of bounds");
  20.         if (colindex < 0 || colindex > N)
  21.             throw new RuntimeException("col index out of bounds");
  22.         sparsearray[rowindex].store(colindex, value);
  23.     }
  24.  
  25.     public Object get(int rowindex, int colindex)
  26.     {
  27.         if (rowindex < 0 || colindex > N)
  28.             throw new RuntimeException("row index out of bounds");
  29.         if (rowindex < 0 || colindex > N)
  30.             throw new RuntimeException("col index out of bounds");
  31.  
  32.         return (sparsearray[rowindex].fetch(colindex));
  33.     }
  34.  
  35.     public static void main(String... arg)
  36.     {
  37.         Integer[][] iarray = new Integer[3][3];
  38.         iarray[0][0] = 1;
  39.         iarray[0][1] = null;
  40.         iarray[0][2] = 2;
  41.         iarray[1][0] = null;
  42.         iarray[1][1] = 3;
  43.         iarray[1][2] = null;
  44.         iarray[2][0] = 4;
  45.         iarray[2][1] = 6;
  46.         iarray[2][2] = null;
  47.  
  48.         SparseMatrix sparseMatrix = new SparseMatrix(3);
  49.         for (int rowindex = 0; rowindex < 3; rowindex++)
  50.         {
  51.             for (int colindex = 0; colindex < 3; colindex++)
  52.             {
  53.                 sparseMatrix.store(rowindex, colindex,
  54.                 iarray[rowindex][colindex]);
  55.             }
  56.         }
  57.         System.out.println("the sparse Matrix is ");
  58.         for (int rowindex = 0; rowindex < 3; rowindex++)
  59.         {
  60.             for (int colindex = 0; colindex < 3; colindex++)
  61.             {
  62.                 System.out.print(sparseMatrix.get(rowindex, colindex) + "\t");
  63.             }
  64.             System.out.println();
  65.         }
  66.     }
  67. }
  68.  
  69. class List
  70. {
  71.     private int index;
  72.     private Object value;
  73.     private List nextindex;
  74.  
  75.     public List(int index)
  76.     {
  77.         this.index = index;
  78.         nextindex = null;
  79.         value = null;
  80.     }
  81.  
  82.     public List()
  83.     {
  84.         index = -1;
  85.         value = null;
  86.         nextindex = null;
  87.     }
  88.  
  89.     public void store(int index, Object value)
  90.     {
  91.         List current = this;
  92.         List previous = null;
  93.  
  94.         List node = new List(index);
  95.         node.value = value;
  96.         while (current != null && current.index < index)
  97.         {
  98.             previous = current;
  99.             current = current.nextindex;
  100.         }
  101.         if (current == null)
  102.         {
  103.             previous.nextindex = node;
  104.         }
  105.         else
  106.         {
  107.             if (current.index == index)
  108.             {
  109.                 System.out.println("DUPLICATE INDEX");
  110.                 return;
  111.             }
  112.             previous.nextindex = node;
  113.             node.nextindex = current;
  114.         }
  115.         return;
  116.     }
  117.  
  118.     public Object fetch(int index)
  119.     {
  120.         List current = this;
  121.         Object value = null;
  122.         while (current != null && current.index != index)
  123.         {
  124.             current = current.nextindex;
  125.         }
  126.         if (current != null)
  127.         {
  128.             value = current.value;
  129.         }
  130.         else
  131.         {
  132.             value = null;
  133.         }
  134.         return value;
  135.     }
  136.  
  137.     public int elementCount()
  138.     {
  139.         int elementCount = 0;
  140.         for (List current = this.nextindex; (current != null); current = current.nextindex)
  141.         {
  142.             elementCount++;
  143.         }
  144.         return elementCount;
  145.     }
  146. }
  147.  
  148. public class SparseArray
  149. {
  150.     private List start;
  151.     private int index;
  152.  
  153.     SparseArray(int index)
  154.     {
  155.         start = new List();
  156.         this.index = index;
  157.     }
  158.  
  159.     public void store(int index, Object value)
  160.     {
  161.         if (index >= 0 && index < this.index)
  162.         {
  163.             if (value != null)
  164.                 start.store(index, value);
  165.         } else
  166.         {
  167.             System.out.println("INDEX OUT OF BOUNDS");
  168.         }
  169.     }
  170.  
  171.     public Object fetch(int index)
  172.     {
  173.         if (index >= 0 && index < this.index)
  174.             return start.fetch(index);
  175.         else
  176.         {
  177.             System.out.println("INDEX OUT OF BOUNDS");
  178.             return null;
  179.         }
  180.     }
  181.  
  182.     public int elementCount()
  183.     {
  184.         return start.elementCount();
  185.     }
  186. }


$javac SparseMatrix.java
$java SparseMatrix
the sparse Matrix is 
1    null  2	
null 3     null	
4    6     null

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.