Introduction to Sparse Matrix in Data Structure4 min read

In this article, we will learn about Sparse matrix, its use and its different representation.

What is a sparse matrix?

A matrix can be defined with a 2-dimensional array with column m and row n represented as m * n matrix. However, there may be a matrix or matrices that may contain less Non-Zero values than Zero values. Such a matrix is known as the Sparse matrix.

We can say that it is a matrix that contains a few Non-Zero elements.

Why use Sparse Matrix instead of a simple matrix?

There are mainly two reasons:

  1. Storage: Sparse matrix allows us to use the memory to store only non-zero elements. So, less storage is required.
  2. Computing time: designing a data- structure that traverses only Non-Zero elements, computing time can be saved.

Sparse Matrix Representation:

There are two ways to represent a sparse matrix:

  1. Triplet Representation(Array Representation)
  2. Linked Representation

1. Triplet Representation(Array Representation)

It is an array representation where only Non-Zero elements are store in three rows named as:

  • Row: This contains an index number for a row where non-zero elements are situated.
  • Column: This contains an index number for a column where the non-zero element is stored.
  • Value: These contain all the values of Non-Zero elements located at their respective index i.e. rows and columns.
Sparse matrix

In the above diagram of matrix size 5* 6, there are 5 non-zero elements (8, 7, 5, 3, 1). The right-hand side table indicates it’s a sparse matrix with Rows, Columns, and Values. Values are the non-zero elements that are present in their particular row and column. Eg. 0th row and 2nd column contain the non-zero element 8 in the sparse matrix. Ans the following shows a similar pattern.

Triplet Representation(Array Representation) using C++

Output of sparse matrix using C++:


2. Linked List Representation:

The Linked list data structure is used to represent a sparse matrix.

Here, each node has four fields and they are:

  • Row: This contains an index number for a row where non-zero elements are situated.
  • Column: This contains an index number for a column where a non-zero element is stored.
  • Value: These contain all the values of Non-Zero elements located at their respective index i.e. rows and columns.
  • Next Node: This contains the address of the next node.

C Program to determine if a given matrix is in a sparse matrix or not.

Output of sparse matrix using C:


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …