How will you store the following sparse matrix in vector and linked list representation so that only nonzero elements are stored?
To store the given sparse matrix in vector and linked list representation, we need to identify the nonzero elements along with their row and column indices. We can then store this information in a compact manner.
For the given matrix, the nonzero elements and their corresponding row and column indices are:
| Nonzero Element | Row Index | Column Index |
|---|---|---|
| 2 | 1 | 1 |
| 1 | 2 | 1 |
| 4 | 3 | 2 |
| 5 | 3 | 3 |
| 1 | 3 | 5 |
| -1 | 1 | 5 |
| 2 | 2 | 3 |
| 1 | 2 | 4 |
| 9 | 5 | 1 |
Vector representation:
In vector representation, we can create three separate arrays:
value[]: an array to store the values of the nonzero elements
row[]: an array to store the row indices of the nonzero elements
col[]: an array to store the column indices of the nonzero elements
We can then store the information in the following manner:
value[] = {2, 1, 4, 5, 1, -1, 2, 1, 9}
row[] = {1, 2, 3, 3, 3, 1, 2, 2, 5}
col[] = {1, 1, 2, 3, 5, 5, 3, 4, 1}
Linked list representation:
In linked list representation, we can create a linked list of nodes, where each node stores the value of a nonzero element along with its row and column indices. We can also create a separate header array to store the starting node of each row. We can then link the nodes row-wise using their right pointers and column-wise using their down pointers.
We can represent the linked list as follows:
value | row | col |
____ ____ ___ _______ ____.
_______ _________ ______ ________ ________ _________ _____.
___ ____ _____ _________ _______ ________ ___ ________.
___ _________ ____ ____ ___ __________ _______.
______ _______ ______ _________ ___ _____ _____ ______.
___ __________ _______ __________ _______ _______ _________ _________ _________ _______ ________.
_____ ______ __________ _________ ____ ________ _________ _______ __________ ___ ____.
_______ __________ _________ ______ ___ ________ ____ ____ _____ _____.
____ __________ ____ _________ _________ _____ __________ ____ ________.
____ _____ _________ ____ _______ ____ ______ ________ ___ ______.
______ _______ ___ __________ _________ __________ _________.
____ _______ _____ ____ ___ ________ _________ ________ ______.
__________ ________ ___ __________ _______ ________.
_________ ______ _______ _____ __________ ____ _________.
______ ____ ______ _________ _______ __________ _______.
____ _________ ____ _______ _________ ___ ______ _______ __________ ____ _______.
___ ____ _______ _______ ___ ____ _________ _________ ____ ________ __________ _______.
__________ ______ ______ ________ ___ ____.
____ ________ ______ _______ ____ _______ _____ __________ _____ ____ ________.
_____ _______ _______ _____ __________ _______ _________ ______ ___ ___ _____ _________.
________ ____ ____ ___ _______ ______.
________ ______ _________ __________ ___ ____ ________ ________ ________.
______ ______ _________ _____ ________ _____ ___ ___ _______.
_______ ________ ___ ________ __________ _____.
________ _______ _____ ______ ___ ________ _________.
______ ________ ____ _________ _____ _______ ____ ______.
_______ _______ _______ _____ _______.
____ _________ ____ ___ _______ _______ _______ ___ ________.
__________ ____ _______ ______ ___.
____ ____ _______ _____ _______ _______ __________.
________ ____ ______ _______ ___ _____.
___ ____ ____ _______ ________ ________ _________ ________ _____ ______ ______ ___.
________ ____ ___ ________ __________ ___ _________ _______ _______ ________ _________.
__________ ___ ____ ______ _________.
___ ______ ______ ________ ____ _______ __________ __________ __________ ____ ________ _____.
_______ ___ _________ _______ ___ _______ __________ ____ ___ ___.
________ ________ _________ ______ ________ ______ ________ __________ ______ _______.
______ ______ _______ ____ __________ _______ __________.
_____ ____ ________ ______ ________ _______ ___ ______ ________ _____.
____ ________ ________ _______ ___ ______ __________ ___.
________ __________ _______ _____ ___ _______ ___ _____ _______ _________.
_____ ________ ______ __________ _________ ___ ________ ____ __________ ____ __________ __________.
_________ ___ _____ ___ __________.
_____ ____ _____ ____ _______ _______ _______ __________ _______.
_____ _______ __________ __________ _________ _________.
_______ __________ ________ ___ ____.
_______ _________ ______ _________ _________ _________.
__________ _______ __________ ________ ________.
___ ______ _________ _____ _______.
________ _________ ___ _________ _____ ____ ___ _____ ____ ____ ___ _______.
_____ ___ ______ ____ _______ __________ _____ __________ __________ ______ __________.
___ ____ _______ ________ ________ __________ __________ ___.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required