Generating Compact Sparse Matrix and Traspose
Generating Compact Sparse Matrix and Traspose
A sparse matrix is a special case of a matrix in which the number of zero elements is
much higher than the number of non-zero elements. As a rule of thumb, if 2/3 of the
total elements in a matrix are zeros, it can be called a sparse matrix. Using a sparse
matrix representation — where only the non-zero values are stored — the space
used for representing data and the time for scanning the matrix are reduced
significantly.
Many applications in data science and machine learning involve sparse matrices,
such as:
Let's take the example of a movie recommendation system. There are millions of
users and thousands of movies, so it's not possible for users to watch and rate all
movies. This data can be represented as a matrix where the rows are the users, and
the columns are the movies.
Most of the matrix elements will be empty, where the missing values will be
replaced with zeros. Since a small percentage of the matrix has non-zero values, this
matrix can be considered a sparse matrix. A small portion of the data is given below:
sparsity=nzerosntotal=2635=0.742
It can be seen that the number of zeros in a sparse matrix is very high. Representing
all zero values in a matrix like this would result in high memory usage, so in
practice, only non-zero values of the sparse matrix are stored.
Numerical example 2
To generate a compact form of a sparse matrix from a normal sparse matrix in C, you can store
only the non-zero elements along with their row and column positions. This is more memory-
efficient for sparse matrices, where most of the elements are zero.
Steps:
1. Normal Sparse Matrix: A sparse matrix that is represented by a 2D array, but you want
to store only the non-zero elements.
2. Compact Form: A more efficient representation, where only the non-zero elements and
their row/column positions are stored.
Example:
We will convert the normal sparse matrix (2D array) into a compact form, which will only store
the non-zero values along with their row and column indices.
C Code:
#include <stdio.h>
int main() {
int matrix[MAX_SIZE][MAX_SIZE] = {
{0, 0, 3, 0, 4},
{5, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 6}
};
return 0;
}
Explanation:
1. Matrix Representation:
o The normal sparse matrix is represented as a 2D array
matrix[MAX_SIZE][MAX_SIZE].
o The compact form is represented by an array of term structures, each holding the
row, column, and value of a non-zero element.
2. generateCompactForm Function:
o Loops through the entire matrix, checks for non-zero values, and stores them
along with their row and column indices in the compact array.
o Each non-zero element is stored in the compact array in the form (row, column,
value).
3. printCompactForm Function:
o Prints the elements stored in the compact array.
Sparse Matrix 6
4. Main Function:
o Initializes a 2D array matrix representing a sparse matrix.
o Calls the generateCompactForm function to convert the matrix into a compact
form.
o Counts the number of non-zero elements.
o Prints the compact form.
Example Output:
0 0 3 0 4
5 0 0 0 0
0 0 0 0 0
0 0 0 0 6
typedef struct {
int row;
int col;
int value;
} term;
int main() {
term a[] = {
{5, 5, 4}, // Matrix size (5 rows, 5 columns, 4 non-zero elements)
{0, 1, 3}, // Non-zero element at (0, 1) with value 3
{1, 0, 5}, // Non-zero element at (1, 0) with value 5
{2, 3, 8}, // Non-zero element at (2, 3) with value 8
{3, 4, 6} // Non-zero element at (3, 4) with value 6
};
transpose(a, b);
return 0;
}
Explanation: