Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Sparse Matrix

Manipulation
What is Sparse Matrix?
• It is a special case of matrix in which number of zero elements is
more than non-zero elements.
• MATRIX can be defined as a 2D array having ‘m’ rows and ‘n’
columns representing m*n matrix.
Why do we need sparse matrix?
There are two major advantages of sparse matrix:
• Storage: Sparse matrix contains lesser non-zero elements than zero
elements so less memory can be used to store elements. It evaluates only the
non-zero elements.
• Computing Time: In case of searching ‘n’, we need to traverse only the non-
zero elements rather than traversing all the sparse matrix elemnts.
• It saves computing time by logically designing a data structure traversing
nn-zero elements.
Represenation:
There are two representations of sparse matrix:
• Array
• Linked List

[ ]
• Example of Sparse Matrix: 0 0 3 0 4
0 0 5 7 0
𝐴( 5 , 4 )=
0 0 0 0 0
0 2 6 0 0
[ ]
0 0 3 0 4
0 0 5 7 0
𝐴( 5 , 4 )=
0 0 0 0 0
0 2 6 0 0

Representing a sparse matrix by a 2D array leads to wastage of


lots of memory as zeroes in the matrix are of no use in most of
the cases. So, instead of storing zeroes with non-zero elements,
we only store non-zero elements
Using Arrays:
2D array is used to represent a sparse matrix in which there are
three rows named as
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index –
(row,column)
Using Arrays:
How it will be stored in memory?
Using Linked List:
In linked list, each node has four fields. These four fields are
defined as:
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index –
(row,column)
• Next node: Address of the next node
Using Linked List:

NODE
Row Column Data Next Pointer
Using Linked List:
ASSIGNMENT 02

Part 01: How 2D array is stored in memory.


Show all calculations with examples.

You might also like