Sparse Matrix
Sparse Matrix
A matrix can be defined as a two-dimensional array having 'm' rows and 'n' columns. A matrix with
m rows and n columns is called m × n matrix. It is a set of numbers that are arranged in the horizontal
or vertical lines of entries.
For example -
Now, the question arises: we can also use the simple matrix to store the elements, then why is the
sparse matrix required?
Why is a sparse matrix required if we can use the simple matrix to store
elements?
There are the following benefits of using the sparse matrix -
Storage - We know that a sparse matrix contains lesser non-zero elements than zero, so less
memory can be used to store elements. It evaluates only the non-zero elements.
Computing time: In the case of searching in sparse matrix, we need to traverse only the non-zero
elements rather than traversing all the sparse matrix elements. It saves computing time by logically
designing a data structure traversing non-zero elements.
ADVERTISEMENT
o Array representation
o Linked list representation
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is because
zeroes in the matrix are of no use, so storing zeroes with non-zero elements is wastage of memory.
To avoid such wastage, we can store only non-zero elements. If we store only non-zero elements, it
reduces the traversal time and the storage space.
In 2D array representation of sparse matrix, there are three fields used that are named as -
o Row - It is the index of a row where a non-zero element is located in the matrix.
o Column - It is the index of the column where a non-zero element is located in the matrix.
o Value - It is the value of the non-zero element that is located at the index (row, column).
Example -
Let's understand the array representation of sparse matrix with the help of the example given below
-
In the above figure, we can observe a 5x4 sparse matrix containing 7 non-zero elements and 13 zero
elements. The above matrix occupies 5x4 = 20 memory space. Increasing the size of matrix will
increase the wastage space.
The size of the table depends upon the total number of non-zero elements in the given sparse
matrix. Above table occupies 8x3 = 24 memory space which is more than the space occupied by the
sparse matrix. So, what's the benefit of using the sparse matrix? Consider the case if the matrix is
8*8 and there are only 8 non-zero elements in the matrix, then the space occupied by the sparse
matrix would be 8*8 = 64, whereas the space occupied by the table represented using triplets would
be 8*3 = 24.
In the program below, we will show the tabular representation of the non-zero elements of the
sparse matrix stored in array.
1. #include <stdio.h>
2. int main()
3. {
4. // Sparse matrix having size 4*5
5. int sparse_matrix[4][5] =
6. {
7. {0 , 0 , 6 , 0 , 9 },
8. {0 , 0 , 4 , 6 , 0 },
9. {0 , 0 , 0 , 0 , 0 },
10. {0 , 1 , 2 , 0 , 0 }
11. };
12. // size of matrix
13. int size = 0;
14. for(int i=0; i<4; i++)
15. {
16. for(int j=0; j<5; j++)
17. {
18. if(sparse_matrix[i][j]!=0)
19. {
20. size++;
21. }
22. }
23. }
24. // Defining final matrix
25. int matrix[3][size];
26. int k=0;
27. // Computing final matrix
28. for(int i=0; i<4; i++)
29. {
30. for(int j=0; j<5; j++)
31. {
32. if(sparse_matrix[i][j]!=0)
33. {
34. matrix[0][k] = i;
35. matrix[1][k] = j;
36. matrix[2][k] = sparse_matrix[i][j];
37. k++;
38. }
39. }
40. }
41. // Displaying the final matrix
42. for(int i=0 ;i<3; i++)
43. {
44. for(int j=0; j<size; j++)
45. {
46. printf("%d ", matrix[i][j]);
47. printf("\t");
48. }
49. printf("\n");
50. }
51. return 0;
52. }
Output
In the output, first row of the table represent the row location of the value, second row represents
the column location of the value, and the third represents the value itself.
In the below screenshot, the first column with values 0, 2, and 6 represents the value 6 stored at the
0th row and 2nd column.
Unlike the array representation, a node in the linked list representation consists of four fields. The
four fields of the linked list are given as follows -
o Row - It represents the index of the row where the non-zero element is located.
o Column - It represents the index of the column where the non-zero element is located.
o Value - It is the value of the non-zero element that is located at the index (row, column).
o Next node - It stores the address of the next node.
The node structure of the linked list representation of the sparse matrix is shown in the below image
-
Example -
Let's understand the linked list representation of sparse matrix with the help of the example given
below -
In the above figure, the sparse matrix is represented in the linked list form. In the node, the first field
represents the index of the row, the second field represents the index of the column, the third field
represents the value, and the fourth field contains the address of the next node.
In the above figure, the first field of the first node of the linked list contains 0, which means 0th row,
the second field contains 2, which means 2nd column, and the third field contains 1 that is the non-
zero element. So, the first node represents that element 1 is stored at the 0th row-2nd column in the
given sparse matrix. In a similar manner, all of the nodes represent the non-zero elements of the
sparse matrix.
1. class Node {
2. int row;
3. int col;
4. int value;
5. Node next;
6. Node(int r, int c, int val)
7. { row = r; col = c; this.value = val; }
8. }
9. public class Sparse{
10. public static void main(String[] args)
11. {
12. /*Assume a 4x4 sparse matrix */
13. int sparseMatrix[][] = {
14. {0, 0, 1, 2},
15. {3, 0, 0, 0},
16. {0, 4, 5, 0},
17. {0, 6, 0, 0}
18. };
19. Node start = null; /*Start with the empty list*/
20. Node tail = null;
21. int k = 0;
22. for (int i = 0; i < 4; i++)
23. for (int j = 0; j < 4; j++)
24. {
25. if (sparseMatrix[i][j] != 0) /*Pass only non-zero values*/
26. {
27. Node temp = new Node(i, j, sparseMatrix[i][j]);
28. temp.next = null;
29. if(start == null){
30. start = temp;
31. tail=temp;
32. }
33. else{
34. tail.next = temp;
35. tail = tail.next;
36. }
37. }
38. }
39. Node itr = start;
40. while(start != null){
41. System.out.println(start.row + " " + start.col + " " + start.value);
42. start = start.next;
43. }
44. }
45. }
Output
Every row in the output represents the node of the linked list. In every row of the below screenshot,
the first element represents the row index location of the non-zero element, the second element
represents the column index location of the non-zero element, and the third element represents the
non-zero element itself.
So, that's all about the article. In this article, we have first discussed the brief description of Matrix
and Sparse Matrix. After that, we saw why the sparse matrix is useful, and at last, we have discussed
the array and linked list representation of the sparse matrix. Hope, the article will be helpful and
informative to you.