If most of the elements in the matrix are zero then the matrix is called a sparse matrix. It is wasteful to store the zero elements in the matrix since they do not affect the results of our computation. This is why we implement these matrices in more efficient representations than the standard 2D Array. Using more efficient representations we can cut down space and time complexities of operations significantly.
We have discussed at 4 different representations in following articles :
In this article, we will discuss another representation of the Sparse Matrix which is commonly referred as the Yale Format.
The CSR (Compressed Sparse Row) or the Yale Format is similar to the Array Representation (discussed in Set 1) of Sparse Matrix. We represent a matric M (m * n), by three 1-D arrays or vectors called as A, IA, JA. Let NNZ denote the number of non-zero elements in M and note that 0-based indexing is used.
- The A vector is of size NNZ and it stores the values of the non-zero elements of the matrix. The values appear in the order of traversing the matrix row-by-row
- The IA vector is of size m+1 stores the cumulative number of non-zero elements upto ( not including) the i-th row. It is defined by the recursive relation :
- IA = 0
- IA[i] = IA[i-1] + no of non-zero elements in the (i-1) th row of the Matrix
- The JA vector stores the column index of each element in the A vector. Thus it is of size NNZ as well.
To find the no of non-zero elements in say row i, we perform IA[i+1] – IA[i]. Notice how this representation is different to the array based implementation where the second vector stores the row indices of non-zero elements.
The following examples show how these matrixes are represented.
Input : 0 0 0 0 5 8 0 0 0 0 3 0 0 6 0 0 Solution: When the matrix is read row by row, the A vector is [ 5 8 3 6] The JA vector stores column indices of elements in A hence, JA = [ 0 1 2 1]. IA = 0. IA = IA + no of non-zero elements in row 0 i.e 0 + 0 = 0. Similarly, IA = IA + 2 = 2 IA = IA + 1 = 3 IA = IA+1 = 4 Therefore IA = [0 0 2 3 4] The trick is remember that IA[i] stores NNZ upto and not-including i row. Input : 10 20 0 0 0 0 0 30 0 4 0 0 0 0 50 60 70 0 0 0 0 0 0 80 Output : A = [10 20 30 4 50 60 70 80], IA = [0 2 4 7 8] JA = [0 1 1 3 2 3 4 5]
SPARSIFY (MATRIX) Step 1: Set M to number of rows in MATRIX Step 2: Set N to number of columns in MATRIX Step 3: I = 0, NNZ = 0. Declare A, JA, and IA. Set IA to 0 Step 4: for I = 0 ... N-1 Step 5: for J = 0 ... N-1 Step 5: If MATRIX [I][J] is not zero Add MATRIX[I][J] to A Add J to JA NNZ = NNZ + 1 [End of IF] Step 6: Add NNZ to IA [ End of J loop ] [ End of I loop ] Step 7: Print vectors A, IA, JA Step 8: END
0 0 0 0 1 5 8 0 0 0 0 0 3 0 0 0 6 0 0 1 A = [ 1 5 8 3 6 1 ] IA = [ 0 1 3 4 6 ] JA = [ 4 0 1 2 1 4 ]
- The sparsity of the matrix = ( Total No of Elements – Number of Non Zero Elements) / ( Total No of Elements) or (1 – NNZ/mn ) or ( 1 – size(A)/mn ) .
- The direct array based representation required memory 3 * NNZ wile CSR requires ( 2*NNZ + m + 1) memory.
- CSR matrices are memory efficient as long as .
- Similar to CSR there exits CSC which stands for Compressed Sparse Columns. It is the column analogue for CSR.
- The ‘New’ Yale format further compresses the A and JA vectors into 1 vector.
- Sparse Matrix and its representations | Set 1 (Using Arrays and Linked Lists)
- Sparse Matrix and its representations | Set 2 (Using List of Lists and Dictionary of keys)
- C++ program to Convert a Matrix to Sparse Matrix
- Check if a given matrix is sparse or not
- Sparse Search
- Sparse Table
- Operations on Sparse Matrices
- Range sum query using Sparse Table
- How to store a Sparse Vector efficiently?
- Range maximum query using Sparse Table
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.