โ Matrices and Sparse Matrices
โ 1. Matrix (Matrices)
โ Definition
A Matrix is a rectangular arrangement of elements (numbers/data) in the form of rows and columns.
โ It is written as:
[
A = [a_{ij}]
]
Where:
- i = row number
- j = column number
๐ Example of a Matrix (3ร3):
[
A =
\begin{bmatrix}
1 & 2 & 3 \
4 & 5 & 6 \
7 & 8 & 9
\end{bmatrix}
]
โ In programming, matrices are represented using 2D Arrays.
Example:
int A[3][3];
โ Types of Matrices (Important)
โ 1. Row Matrix
Matrix having only one row.
Example (1ร3):
[
[ 10\ \ 20\ \ 30 ]
]
โ 2. Column Matrix
Matrix having only one column.
Example (3ร1):
[
\begin{bmatrix}
10\
20\
30
\end{bmatrix}
]
โ 3. Square Matrix
Matrix having same number of rows and columns.
Example (3ร3)
โ 4. Diagonal Matrix
All elements except diagonal are 0.
Example:
[
\begin{bmatrix}
5 & 0 & 0\
0 & 3 & 0\
0 & 0 & 1
\end{bmatrix}
]
โ 5. Identity Matrix
Diagonal elements are 1, others are 0.
Example:
[
\begin{bmatrix}
1 & 0 & 0\
0 & 1 & 0\
0 & 0 & 1
\end{bmatrix}
]
โ 6. Zero (Null) Matrix
All elements are 0.
Example:
[
\begin{bmatrix}
0 & 0\
0 & 0
\end{bmatrix}
]
โ Applications of Matrices
Matrices are used in:
โ
Computer graphics
โ
Image processing
โ
Scientific calculations
โ
Engineering problems
โ
Data representation in tables
โ
Graphs (Adjacency matrix)
โ 2. Sparse Matrix
โ Definition
A Sparse Matrix is a matrix in which most of the elements are zero.
โ
Condition:
If number of zero elements is greater than non-zero elements, then it is called a Sparse Matrix.
๐ Example:
[
\begin{bmatrix}
0 & 0 & 5 & 0\
0 & 0 & 0 & 0\
0 & 8 & 0 & 0\
0 & 0 & 0 & 0
\end{bmatrix}
]
Here:
- Non-zero elements = 2 (5 and 8)
- Remaining are zeros โ many zeros โ โ Sparse Matrix
โ Why Sparse Matrix is Used?
Storing sparse matrix in normal 2D array wastes memory because it stores too many zeros.
โ So we use special representation to save:
- Memory (Space)
- Processing time
โ 3. Sparse Matrix Representation
To store only non-zero elements, we use 3-tuple (Triplet) representation.
โ Triplet Form / 3-Tuple Representation
In this method, sparse matrix is stored using a table with 3 columns:
โ Columns are:
- Row number
- Column number
- Value
โ Example of Sparse Matrix
Matrix (4ร4):
[
\begin{bmatrix}
0 & 0 & 3 & 0\
0 & 0 & 0 & 0\
0 & 5 & 0 & 0\
0 & 0 & 0 & 7
\end{bmatrix}
]
Non-zero values are:
- 3 at (0,2)
- 5 at (2,1)
- 7 at (3,3)
โ Triplet Table:
| Row | Col | Value |
|---|---|---|
| 4 | 4 | 3 |
| 0 | 2 | 3 |
| 2 | 1 | 5 |
| 3 | 3 | 7 |
๐ First row stores:
- Total rows
- Total columns
- Total non-zero elements
โ 4. Advantages of Sparse Matrix
โ Benefits:
- Saves memory space (stores only non-zero values)
- Fast processing for big matrices
- Efficient storage representation
โ 5. Disadvantages of Sparse Matrix
โ Limitations:
- More complex than normal matrix
- Accessing elements becomes slower because searching is needed
- Not suitable if non-zero values are high
โ 6. Difference Between Matrix and Sparse Matrix
| Feature | Matrix | Sparse Matrix |
|---|---|---|
| Definition | Rows and columns data | Mostly zero values |
| Storage | Normal 2D array | Special compact form |
| Memory Usage | High for large matrices | Very less |
| Best For | General problems | Large matrix with many zeros |
| Example | Marks table | Adjacency matrix of sparse graph |
โ Conclusion
A matrix is a 2D structure of elements arranged in rows and columns and is commonly stored using 2D arrays. A sparse matrix is a special matrix in which most elements are zero, and it is stored efficiently using triplet (row, column, value) representation to save memory and improve performance.
