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

Week 08 (Sparse Matrix)

Here are the key steps to store a 2D array in memory using a 1D array: 1. Declare a 1D array large enough to hold all elements of the 2D array. For example, if a 2D array is of size m x n, declare a 1D array of size m*n. 2. To access an element at index [i,j] of the 2D array: - Calculate the index in the 1D array as i*n + j - This maps the 2D indices to a linear index in the 1D array For example, consider a 3x4 2D array stored in a 1D array of size 12: 2D Array

Uploaded by

umarzain2005
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)
21 views

Week 08 (Sparse Matrix)

Here are the key steps to store a 2D array in memory using a 1D array: 1. Declare a 1D array large enough to hold all elements of the 2D array. For example, if a 2D array is of size m x n, declare a 1D array of size m*n. 2. To access an element at index [i,j] of the 2D array: - Calculate the index in the 1D array as i*n + j - This maps the 2D indices to a linear index in the 1D array For example, consider a 3x4 2D array stored in a 1D array of size 12: 2D Array

Uploaded by

umarzain2005
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/ 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