0% found this document useful (0 votes)
2 views

DS Class 7-Sparse Matrix-AI&ML (1)

A sparse matrix is defined as a matrix with a majority of its elements equal to zero, allowing for efficient storage and computation by focusing on non-zero elements. It can be represented using triplets in array or linked list formats, which optimize memory usage and processing time. The document also includes examples of C and Java implementations for representing a sparse matrix using both array and linked list methods.

Uploaded by

basavananda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DS Class 7-Sparse Matrix-AI&ML (1)

A sparse matrix is defined as a matrix with a majority of its elements equal to zero, allowing for efficient storage and computation by focusing on non-zero elements. It can be represented using triplets in array or linked list formats, which optimize memory usage and processing time. The document also includes examples of C and Java implementations for representing a sparse matrix using both array and linked list methods.

Uploaded by

basavananda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Sparse Matrix

• A matrix can be defined as a two-


dimensional array having 'm' rows
and 'n' columns. A matrix with m
rows and n columns is called m × n
matrix. It is a set of numbers that
are arranged in the horizontal or
vertical lines of entries.
What is a sparse matrix?

• Sparse matrices are those matrices that have the

majority of their elements equal to zero. In other words,

the sparse matrix can be defined as the matrix that has

a greater number of zero elements than the non-zero

elements.
Benefits of using the sparse matrix

• Storage - We know that a sparse matrix contains lesser non-zero


elements than zero, so less memory can be used to store
elements. It evaluates only the non-zero elements.

• Computing time: In the case of searching in sparse matrix, we


need to traverse only the non-zero elements rather than
traversing all the sparse matrix elements. It saves computing time
by logically designing a data structure traversing non-zero
elements.
Representation of sparse matrix

• The non-zero elements in the sparse matrix can be stored using


triplets that are rows, columns, and values.
• There are two ways to represent the sparse matrix that are listed as
follows -

• Array representation
• Linked list representation
2D array representation of
sparse matrix
• Row - It is the index of a row where a non-zero element
is located in the matrix.
• Column - It is the index of the column where a non-zero
element is located in the matrix.
• Value - It is the value of the non-zero element that is
located at the index (row, column).
The sparse
matrix -

the space occupied by the sparse matrix would be 8*8 = 64, whereas
the space occupied by the table represented using triplets would be 8*3
= 24.
#include <stdio.h>
int main()
{
// Sparse matrix having size 4*5
int sparse_matrix[4][5] =
{
{0 , 0 , 6 , 0 , 9 },
{0 , 0 , 4 , 6 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 1 , 2 , 0 , 0 }
};
// size of matrix
int size = 0;
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
if(sparse_matrix[i][j]!=0)
{
size++;
}
}
}
// Defining final matrix
int matrix[3][size];
int k=0;
// Computing final matrix
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
if(sparse_matrix[i][j]!=0)
{
matrix[0][k] = i;
matrix[1][k] = j;
matrix[2][k] = sparse_matrix[i][j];
k++;
}
}
}
// Displaying the final matrix
for(int i=0 ;i<3; i++)
{
for(int j=0; j<size; j++)
{
printf("%d ", matrix[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
Linked List representation of the
sparse matrix
• The advantage of using a linked list to represent the
sparse matrix is that the complexity of inserting or
deleting a node in a linked list is lesser than the array.

• Unlike the array representation, a node in the linked list


representation consists of four fields.
The Four fields of the linked list are given as
follows -
• Row - It represents the index of the row where the non-
zero element is located.
• Column - It represents the index of the column where
the non-zero element is located.
• Value - It is the value of the non-zero element that is
located at the index (row, column).
• Next node - It stores the address of the next node.
The sparse matrix -

Above matrix occupies 4x4 = 16


memory space
The linked list representation of
the matrix -
• In the node, the first field represents the index of the
row,

• The second field represents the index of the column

• The third field represents the value, and

• The fourth field contains the address of the next node.


The linked list representation of the matrix -
• Here the first field of the first node of the linked list contains
0, which means 0th row,
• The second field contains 2, which means 2nd column,
• The third field contains 1 that is the non-zero element.

• So, the first node represents that element 1 is stored at the


0th row-2nd column in the given sparse matrix.

• In a similar manner, all of the nodes represent the non-zero


elements of the sparse matrix.
Implementation of linked list
representation of sparse matrix
class Node { public class Sparse{
public static void main(String[] ar
int row; gs)
int col; {
int value; /*Assume a 4x4 sparse matrix */
Node next; int sparseMatrix[][] = {
{0, 0, 1, 2},
Node(int r, int c, int val)
{3, 0, 0, 0},
{ row = r; col = c; this.valu {0, 4, 5, 0},
e = val; } {0, 6, 0, 0}
} };
Node start = null; /*Start with the empty list*/
Node tail = null;
int k = 0; else{
for (int i = 0; i < 4; i++) tail.next = temp;
for (int j = 0; j < 4; j++) tail = tail.next;
{
}
if (sparseMatrix[i][j] != 0) /*Pass only non-
zero values*/ }
{ }
Node temp = new Node(i, j, sparseMatrix[i][j]);
temp.next = null;
if(start == null){
start = temp;
tail=temp;
}
Node itr = start;
while(start != null){
System.out.println(start.row + " " + start.col + " " + star
t.value);
start = start.next;
}
}
}
• https://www.tutorialspoint.com/data_structures_algorithms/array_da
ta_structure.htm
• https://www.youtube.com/watch?v=yRErgiYyv_E

• https://www.youtube.com/watch?v=VL03KD-RxZc
• https://www.youtube.com/watch?v=sr_bR1WwcLY

• https://www.youtube.com/watch?v=E6IOrZUpvSE

You might also like