Many matrices in real problems are mostly zeros. Storing all those zeros wastes memory. This page defines the common kinds of such matrices and shows the standard compact form, the 3-tuple representation.
Special and sparse matrices
Two special square matrices have zeros in fixed places:
A triangular matrix has all elements on one side of the main diagonal equal to zero. In a lower triangular matrix, aij=0 whenever i<j (everything above the diagonal is 0). An n×n lower triangular matrix has at most 2n(n+1) non-zero elements.
A tridiagonal matrix has non-zero elements only on the main diagonal and on the diagonals directly above and below it: aij=0 whenever ∣i−j∣>1. An n×n tridiagonal matrix has at most 3n−2 non-zero elements.
A 4 x 4 lower triangular matrix and a 5 x 5 tridiagonal matrix
It has 8 non-zero elements, so its 3-tuple form has 8+1=9 rows, and the first row is (7, 6, 8).
How much memory is saved?
A full m×n matrix needs m⋅n cells. A 3-tuple table with t non-zero elements needs 3(t+1) cells. The 3-tuple form saves memory when
3(t+1)<m⋅n.
For the matrix above: 3(8+1)=27 cells instead of 7×6=42. For a 1000×1000 matrix with 5000 non-zero values: 3×5001=15,003 cells instead of 1,000,000.
A program that builds the 3-tuple form
Key takeaways
A sparse matrix has mostly zero elements; triangular and tridiagonal matrices have zeros in fixed places.
An n×n tridiagonal matrix has at most 3n−2 non-zeros; a triangular one at most n(n+1)/2.
3-tuple form: header (rows, cols, count), then (row, col, value) for each non-zero element.
It needs 3(t+1) cells instead of m⋅n.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Give the 3-tuple representation (0-based) of the matrix with rows 0 0 3 0, 0 0 0 0, 5 0 0 6, and compare the memory needed with the full matrix.
Show worked answer
The matrix has 3 rows, 4 columns and 3 non-zero elements. 3-tuple form: (3, 4, 3), (0, 2, 3), (2, 0, 5), (2, 3, 6). It needs 3×(3+1)=12 cells, the same as the full matrix (3×4=12), so for a matrix this small there is no saving; the method pays off only when 3(t+1)<m⋅n.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/arrays-pointers-and-structures/sparse-matrices-and-the-3-tuple-form/ to read this page.