×

What is a Sparse Matrix in Data Structure?

Definition

A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix that contains m * n dimensions is treated as a 2-D array where n represents the number of columns and m represents the number of rows. A matrix is called a Sparse matrix when the number of non-zero elements in the given matrix is more significant in count than the number of zero elements present in the respective matrix.

If the matrix is extensive and the number of non-zero elements is much less in the count, then a lot of memory and space is wasted in such cases. Also, it can take a lot of time to scan the respective non-zero element.

Syntax

Rather than storing fewer non-zero elements, we use two representations to limit space usage and processing time in a matrix. The mentioned two representations are stated below:

  1. Array representation
    In the array representation of a sparse matrix, we convert a 2 – D array into a 1 – D array. The converted array contains three columns, among which represent the following:
    1. Row – The sparse matrix represents the index of the row of the given non-zero element.
    2. Column - The sparse matrix represents the index of the column of the given non-zero element.
    3. Value - The sparse matrix represents the value of the non-zero elements in the same row of the given column index in the allotted 2 – D matrix.
  2. Linked list representation
    Each node has four fields in the linked list representing a sparse matrix. The four fields are stated below:
    1. Row – The sparse matrix represents the index of the row of the given non-zero element.
    2. Column - The sparse matrix represents the index of the column of the given non-zero element.
    3. Value – In a sparse matrix, it represents the value of the respective non-zero element at (r, c), where r represents the index of the row of the non-zero element and c represents the index of the column of the non-zero element.
    4. Next node – A sparse matrix represents the reference to the next node considering the given node.

The working of Sparse matrix in data structures

The sparse matrix can be expressed as a two-dimensional matrix in which the count of non-zero elements is much less than the count of zero elements present in the respective two-dimensional matrix. If the given matrix is extensive and the number of non-zero elements is much less in the count, then a lot of memory and space is wasted in such cases. Also, it can take a lot of time to scan the respective non-zero element. The problem of loss in space is caused due to the storage of zeros in the two-dimensional matrix.

When it comes to Sparse matrices, they are used to store the non-zero elements present in the two-dimensional matrices. By using Sparse matrices, we can avoid the problems caused by storing these matrices with significant zero elements and a few non-zero elements (these problems include wastage of space and colossal processing time). Sparse matrices store only the non-zero elements in the given two-dimensional matrix. Hence, they solve the problem of wastage of memory and space that is caused due to the storage of the zero elements in the regular two-dimensional matrices. The processing time in finding a given non-zero element in matrices having large dimensions is also reduced by storing them in the sparse matrices.

Rather than storing fewer non-zero elements, we use two representations to limit space usage and processing time in a matrix. The mentioned two representations are stated below:

  1. Array representation
    In the array representation of a sparse matrix, we convert a 2 – D array into a 1 – D array.
    An array representation in a Sparse matrix is often used when accessing the elements is done more frequently. As arrays store elements based on indices, this representation is helpful in such scenarios.
    The converted array of the array representation of a Sparse tree contains three columns, among which each represents the following:
    1. Row – The sparse matrix represents the index of the row of the given non-zero element.
    2. Column - The sparse matrix represents the index of the column of the given non-zero element.
    3. Value - The sparse matrix represents the value of the non-zero elements in the same row of the given column index in the allotted 2 – D matrix.
  2. Linked list representation
    Each node has four fields in the linked list representing a sparse matrix. A linked list representation is used in a Sparse matrix in the cases where we perform the operations of insertion and deletion elements very frequently on the matrix. As the insertion and deletion operations are more accessible to serve on a linked list rather than an array, this representation is instrumental in these scenarios.
    The four fields of linked list representation of a sparse tree are stated below:
    1. Row – The sparse matrix represents the index of the row of the given non-zero element.
    2. Column - The sparse matrix represents the index of the column of the given non-zero element.
    3. Value – In a sparse matrix, it represents the value of the respective non-zero element at (r, c), where r represents the index of the row of the non-zero element and c represents the index of the column of the non-zero element.
    4. Next node – A sparse matrix represents the reference to the next node considering the given node.

Example of Sparse matrix

A sparse matrix representation can be explained by the example stated below. The below-mentioned two-dimensional matrix contains five rows and eight columns.

What is a Sparse Matrix in Data Structure

We can observe that we have eight (8) non-zero elements among all the (8 * 5 = 40) forty elements present in the two-dimensional matrix. This means that it is enough if we store the eight non-zero elements in the memory. As the rest of the elements are zero elements, they can be ignored.

We know that we can represent the given matrix as a sparse matrix in two different ways:

  1. Array representation
    Step -1: Make a list of the non-zero elements in the given two-dimensional matrix, along with their column and row indices.
    Step -2: Create an array having:
    1. Rows = count of non-zero elements
    2. Column = (Row, column, value) 3
      Step – 3: Fill the non-zero elements into the array
What is a Sparse Matrix in Data Structure

Advantages of Array list representation of Sparse matrix

An array representation in a Sparse matrix is often used when accessing the elements is done more frequently. As arrays store elements based on indices, this representation is helpful in such scenarios.

Array list representation of Sparse matrices is better in the location of elements compared to linked lists representation of sparse matrices as the elements are stored based on indices in the array list representation of the Sparse matrices.

  • Linked list representation
What is a Sparse Matrix in Data Structure

Advantages of linked list representation of Sparse matrix

Each node has four fields in the linked list representing a sparse matrix. A linked list representation is used in a Sparse matrix in the cases where we perform the operations of insertion and deletion elements very frequently on the matrix. As the insertion and deletion operations are more accessible to serve on a linked list rather than an array, this representation is instrumental in these scenarios.


Related Topics

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the...

3 minutes read.

Bucket Sort

Bucket Sort: In the sorting algorithm, we create buckets and put elements into them. We can apply some sorting algorithm (insertion sort) to sort the elements in each bucket. Finally,...

4 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Tree in Data Structure

Tree A tree is a non-linear data structure by which hierarchical data is displayed. As we know that there are many trees in the forest, similarly the data structure also contains...

3 minutes read.

Recursion in Fibonacci

Fibonacci heap is considered to be a particular execution of the heap data structure that ultimately helps in making use of not just any number but the Fibonacci numbers. It...

3 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Finding the Maximum Element in a Binary Tree

Implementation // Creating a C++ program to excavate the minimum and maximum in a given binary tree. #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new tree node. class __nod { public: int record; __nod *Lft, *Rt; /*...

4 minutes read.

What are the types of Trees in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

6 minutes read.

Timsort

TimSort Time Complexity Timsort is a sorting algorithm that is quite efficient for real-world data. Timsort is created in 2001 by Tim Peters for the python programming language. Timsort is a...

3 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children...

4 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.

Data Structure Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.