Represent matrices containing mostly zero values efficiently using triplet, CSR and CSC formats, then perform transpose, addition and multiplication without wasting storage.
LEARNING GOALS
๐ฏ Learning Objectives
After completing this level, you should be able to:
Identify when a matrix should be treated as sparse.
Compare dense storage with triplet, CSR and CSC representations.
Construct row pointers and column pointers correctly.
Transpose a sparse matrix using the simple and fast methods.
Add compatible sparse matrices by merging ordered terms.
Explain sparse-matrix multiplication and its practical uses.
Analyze storage requirements and operation complexity.
FOUNDATION
๐งญ 1. What Is a Sparse Matrix?
A matrix is called sparse when most of its elements are zero. Instead of storing every zero, a sparse representation stores only the non-zero values and enough position information to reconstruct the matrix.
Density: nnz รท (rows ร columns). Sparsity: 1 โ density, where nnz is the number of non-zero terms.
00500800700600000009
Example: The 4 ร 5 matrix above has 20 positions but only 5 non-zero terms. Its density is 25% and its sparsity is 75%.
WHY SPARSE STORAGE?
๐พ 2. Dense and Sparse Storage Comparison
Dense Representation
Stores rows ร columns values, including every zero.
Storage: ฮ(mn)
Sparse Representation
Stores only nnz values plus row/column metadata.
Storage: ฮ(nnz + m + n)
Situation
Suitable Choice
Reason
Most values are non-zero
Dense array
Direct indexing with little metadata overhead
Mostly zero, row operations dominate
CSR
Efficient row traversal and matrix-vector multiplication
Mostly zero, column operations dominate
CSC
Efficient column traversal
Simple storage or teaching
Triplet/COO
Easy to construct and understand
Important: Sparse storage is not always smaller. When nnz is large, index metadata can cost more than storing the matrix densely.
COORDINATE FORMAT
๐ 3. Triplet Representation (COO)
Every non-zero term is stored as (row, column, value). A header may additionally store the matrix dimensions and the number of non-zero terms.
Row
Column
Value
Meaning
4
5
5
4 rows, 5 columns, 5 non-zero terms
0
2
5
A[0][2] = 5
1
0
8
A[1][0] = 8
1
3
7
A[1][3] = 7
2
1
6
A[2][1] = 6
3
4
9
A[3][4] = 9
Advantages
Simple to build
Easy to append terms
Convenient intermediate format
Limitations
Row boundaries are not explicit
Searching a particular row may scan terms
Repeated row/column indices add overhead
ROW-COMPRESSED FORMAT
โก๏ธ 4. Compressed Sparse Row (CSR)
CSR stores non-zero terms row by row using three arrays:
values[5, 8, 7, 6, 9]
Non-zero values in row-major order.
columnIndex[2, 0, 3, 1, 4]
Column of every stored value.
rowPointer[0, 1, 3, 4, 5]
Starting offset of each row; the final entry equals nnz.
Terms in row r: indexes rowPointer[r] through rowPointer[r + 1] โ 1.
Reading Row 1
rowPointer[1] = 1 and rowPointer[2] = 3. Therefore, CSR entries 1 and 2 belong to row 1: values 8 and 7 at columns 0 and 3.
COLUMN-COMPRESSED FORMAT
โฌ๏ธ 5. Compressed Sparse Column (CSC)
CSC is the column-oriented counterpart of CSR. It stores:
values[8, 6, 5, 7, 9]
Non-zero values in column-major order.
rowIndex[1, 2, 0, 1, 3]
Row of every stored value.
columnPointer[0, 1, 2, 3, 4, 5]
Starting offset of each column.
Choose CSR When
Rows are accessed repeatedly, such as sparse matrix-vector multiplication.
Choose CSC When
Columns are accessed repeatedly, such as many numerical factorization routines.
CORE OPERATIONS
โ๏ธ 6. Operations on Sparse Matrices
1
Access
Locate a row/column range, then search its stored indices.
O(nnz in row)2
Transpose
Swap each termโs row and column while maintaining order.
O(nnz + columns)3
Addition
Merge two ordered term sequences and combine matching positions.
O(nnzA + nnzB)4
Multiplication
Match non-zero entries from rows of A with compatible rows/columns of B.
Depends on structure5
Insertion
Insert metadata while preserving sorted positions.
May shift entries6
MatrixโVector
Multiply using only stored terms instead of every matrix cell.
O(nnz)
ROWโCOLUMN EXCHANGE
๐ 7. Simple and Fast Transpose
The transpose Aแต changes an m ร n matrix into an n ร m matrix, with Aแต[j][i] = A[i][j].
Simple Transpose
For each column, scan all triplet terms and copy matching entries.
O(columns ร nnz)
Fast Transpose
Count terms per column, compute starting positions and place each term once.
O(columns + nnz)
1
Count each original column
These columns become rows in the transpose.
2
Calculate starting positions
Use prefix sums of the column counts.
3
Place every term
Swap row and column and advance that columnโs next position.
MERGING TERMS
โ 8. Sparse Matrix Addition
Two matrices can be added only when they have identical dimensions. If their triplets are sorted by (row, column), addition resembles merging two sorted arrays.
1
Compare positions
Copy the term with the smaller (row, column) position.
2
Combine equal positions
Add their values; store the sum only when it is non-zero.
3
Copy remaining terms
Append any unprocessed terms from either matrix.
Cancellation: If values at the same position sum to zero, do not store that term in the result.
ADVANCED OPERATION
โ๏ธ 9. Sparse Matrix Multiplication
For A(m ร n) ร B(n ร p), a result entry C[i][j] is formed from matching column indices of row i in A and row indices of column j in B. Efficient implementations commonly use CSR for A and CSC for B.
Main saving: zero terms never participate. Work is performed only for compatible non-zero pairs.
Practical warning: The product of two sparse matrices can become much denser. This is called fill-in and may significantly increase memory use.