×

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

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

6 minutes read.

Array Data Structure

Data Structure Array: The array is a non-primitive and linear data structure that is a group of similar data items. That is, it can store only one type of data....

6 minutes read.

Segregate Even and Odd nodes in a Linked List

Segregate even and odd nodes in a Linked List In this problem, we have given a linked list with integer numbers. We need to modify the given linked list in such...

4 minutes read.

Lowest common ancestor in a binary search tree

Suppose you have given two values of nodes in a binary search tree. You have to find out the lowest common ancestor between the nodes. Let’s take an example tree- For the...

4 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 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.

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 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.

Properties of Binary Tree

Trees are maybe of the most significant datum structures. They are used to store and figure out data. A binarytree is a tree data structure made from nodes, all of which has...

3 minutes read.

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Bubble sort algorithm using Javascript

Sorting is a very useful technique in many algorithms and programs. Basically, sorting operations help us to arrange a set of data in a particular manner. Bubble sort is one...

3 minutes read.

Introduction to 1D-Arrays

One Dimensional Array Technical Definitions The simplest version of an Array is a One-Dimensional Array, in which the items are stored linearly and may be accessed individually by supplying the index value...

6 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 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.

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.

Find Bridges in a Graph

You have been given a graph. You have to find out the bridges in that graph. Graph may be connected or disconnected. You have to print vertices of particular edge...

4 minutes read.

Bookshop management system using file handling in C++

We see different software in every hospitals or library to manage their database. It is very important to store organization’s data. So we use this software. Now we are going...

5 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 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.