MCS-208 · Unit 2

Arrays, Pointers and Structures

Section 4 of 8

Block 1 · Unit 2Arrays, Pointers and Structures9 min · Section 4 of 8

2.4Sparse Matrices and the 3-Tuple Form

Covers IGNOU MCS-208 §2.3

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=0a_{ij} = 0 whenever i<ji < j (everything above the diagonal is 0). An n×nn \times n lower triangular matrix has at most n(n+1)2\frac{n(n+1)}{2} 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=0a_{ij} = 0 whenever ∣i−j∣>1|i - j| > 1. An n×nn \times n tridiagonal matrix has at most 3n−23n - 2 non-zero elements.
A 4 x 4 lower triangular matrix and a 5 x 5 tridiagonal matrix
 4  0  0  0          3  1  0  0  0
 3 -5  0  0          6  9 -3  0  0
 1  0  6  0          0  2  4 -7  0
-7  8 -1  3          0  0  8 -2  1
                     0  0  0  5 -1

The 3-tuple representation

Take this 7×67 \times 6 matrix (rows and columns numbered from 0):

A 7 x 6 sparse matrix
      0  1  2  3  4  5
  0   0  0  0  5  0  0
  1   0  4  0  0  0  0
  2   0  0  0  0  9  0
  3   0  3  0  2  0  0
  4   1  0  2  0  0  0
  5   0  0  0  0  0  0
  6   0  0  8  0  0  0

It has 8 non-zero elements, so its 3-tuple form has 8+1=98 + 1 = 9 rows, and the first row is (7, 6, 8).

How much memory is saved?

A full m×nm \times n matrix needs m⋅nm \cdot n cells. A 3-tuple table with tt non-zero elements needs 3(t+1)3(t + 1) cells. The 3-tuple form saves memory when

3(t+1)<m⋅n.3(t + 1) < m \cdot n.

For the matrix above: 3(8+1)=273(8 + 1) = 27 cells instead of 7×6=427 \times 6 = 42. For a 1000×10001000 \times 1000 matrix with 5000 non-zero values: 3×5001=15,0033 \times 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×nn \times n tridiagonal matrix has at most 3n−23n - 2 non-zeros; a triangular one at most n(n+1)/2n(n+1)/2.
  • 3-tuple form: header (rows, cols, count), then (row, col, value) for each non-zero element.
  • It needs 3(t+1)3(t + 1) cells instead of m⋅nm \cdot n.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many rows does the 3-tuple representation of a 5×85 \times 8 matrix with 6 non-zero elements have?

Show answer

Answer: B. 7

One header row plus one row per non-zero element: 6+1=76 + 1 = 7. 18 is the number of cells for 6 triples without the header.

Question 2 · multiple choice

What is the maximum number of non-zero elements in a 6×66 \times 6 tridiagonal matrix?

Show answer

Answer: D. 16

3n−2=3×6−2=163n - 2 = 3 \times 6 - 2 = 16 (6 on the main diagonal and 5 on each neighbouring diagonal). 18 forgets that the two side diagonals are one element shorter.

Question 3 · multiple choice

Which is the correct header row for the 3-tuple form of this matrix: rows 0 0 2, 0 0 0, 7 0 0, 0 1 0?

Show answer

Answer: B. 4, 3, 3

The matrix has 4 rows, 3 columns and 3 non-zero values (2, 7, 1). Writing 12 gives the total number of cells instead of the non-zero count.

Question 4 · multiple choice

A 100×100100 \times 100 matrix has 400 non-zero elements. How many cells does its 3-tuple form need?

Show answer

Answer: C. 1203

3(t+1)=3×401=12033(t + 1) = 3 \times 401 = 1203, compared with 10,000 for the full matrix. 1200 forgets the header row.

Question 5 · multiple choice

In a lower triangular matrix, which elements must be zero?

Show answer

Answer: D. All elements with i<ji < j

Lower triangular means the non-zeros are on and below the diagonal, so the elements above it (i<ji < j) are zero. ∣i−j∣>1|i - j| > 1 describes the zeros of a tridiagonal matrix.

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)=123 \times (3 + 1) = 12 cells, the same as the full matrix (3×4=123 \times 4 = 12), so for a matrix this small there is no saving; the method pays off only when 3(t+1)<m⋅n3(t + 1) < m \cdot n.

↑ ↓ move↵ openTab switch type