In this tutorial, we are going to learn about the sparse matrix in C.

Before we start let us first discuss what a little bit about two-dimensional arrays. A matrix is represented by 2D arrays where the first index represents the number of rows and the second index represents the number of columns in the matrix. An m*n matrix is represented by a 2D array.

1 |
arr[m][n] |

**Sparsity**: A matrix is said to be sparse matrix if most of the elements (More than half) elements in the matrix are zero and the number of elements divided by the total number of elements present in the array is called the sparsity of the matrix.

Sparsity = Number of non-zero elements/ Total number of elements

Matrix is sparse if sparsity is less than 0.5 and dense otherwise.

When storing data in the array the zero does not actually represent any information but they are taking space in memory so instead of storing this type of data in the 2D arrays we can store that data in some other format and reduce the overall space required by the program.

Creating table from the sparse matrix :

Instead of having an array containing zeroes we can actually remove all those zeroes from the array and to can store indices of the elements in the array separately.

4 | 0 | 1 | 0 |

0 | 0 | 2 | 3 |

0 | 0 | 0 | 5 |

Rows | 0 | 0 | 1 | 1 | 2 |

Cols | 0 | 2 | 2 | 3 | 3 |

Val | 4 | 1 | 2 | 3 | 5 |

The above array can be converted to a sparse matrix using the following code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#include <stdio.h> int nonZeroElements(int m,int n,int arr[][n]) { int i,j; int counter = 0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(arr[i][j]!=0) counter++; } } return counter; } void convertIntoSparse(int m,int n,int arr[][n]) { int nonZero = nonZeroElements(m,n,arr); double sparsity = nonZero/16.0; int applicable = sparsity <= 0.5; if(applicable) { int sparseMatrix[3][nonZero]; int i,j,counter = 0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(arr[i][j]!=0) { sparseMatrix[0][counter] = i; sparseMatrix[1][counter] = j; sparseMatrix[2][counter] = arr[i][j]; counter++; } } } printf("The final Sparse Matrix is : \n"); for(i=0;i<nonZero;i++) { printf("At (%d, %d) value is : %d\n",sparseMatrix[0][i],sparseMatrix[1][i],sparseMatrix[2][i]); } } else { printf("Matrix is not a sparse matrix !!"); } } int main(void) { /* int m,n,i,j; scannf("%d%d",&m,&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&arr[i][j]); } } */ int m = 4, n = 4; int arr[4][4] = { {4, 0, 1, 0}, {0, 0, 2, 3}, {0, 0, 0, 5} }; convertIntoSparse(m,n,arr); return 0; } |

Using a linked list:

We can also use a linked list for the same purpose. The structure of the linked list will look as below :

1 2 3 4 5 6 |
struct node{ int rows; int cols; int val; struct node* next; } |

All the other operations can be performed similary on the linked list and space can be saved.

Most of the languages nowadays use vector of pairs/structures or list of pairs/structures to store the sparse matrix.