Sparse Matrix
Sparse Matrix
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
Representation of Sparse Matrices
• There are two ways to represent the sparse matrix that are listed as
follows :
➢ Array representation
➢ Linked list representation
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
Array Representation
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
0 0 3 0 4
0 0 5 7 0
Matrix = 0 0 0 0 0
0 2 6 0 0
0 0 0 0 0
Linked List Representation
Node structure:
row col
value next
row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6
1 3 1 5 2 3 2 4 4 2 4 3
3 4 5 7 2 6 null
firstNode
Array of Row Chains
Node structure:
next
col value
02600 null
3 5 4 7
// Node to represent row - list
struct row_list
{
int row_number;
struct row_list *link_down; null
struct value_list *link_right;
};
Node structure:
row col value
down right
Header node:
next
down right
Row Lists
1 3 3 1 5 4
n
00304 2 3 5 2 4 7
00570 n
00000
null
02600
4 2 2 4 3 6
n
Column Lists
1 3 3 1 5 4
00304 n
00570 2 3 5 2 4 7
n
00000
02600
4 2 2 4 3 6
n n
1 3 3 1 5 4
00304 n n
00570 2 3 5 2 4 7
n n
00000
02600 null
4 2 2 4 3 6
n n n
row[ ]
typedef struct
{
matrixPointer down;
matrixPointer right;
tagfield tag;
union
{
matrixPointer next;
entryNode entry;
}u;
}matrixNode;
matrixPointer hdnode[MAX_SIZE];
#define MAX_SIZE 50
// Enum for distinguishing between 'head' and 'entry' nodes
enum tagfield {head, entry};
union {
struct matrixNode* next; // Used when the node is a 'head'
struct entryNode entry; // Used when the node is an 'entry'
} u;
};
// Array of pointers to the head nodes of the sparse matrix
struct matrixNode* hdnode[MAX_SIZE];
H0 H1 H2 H3 H4
H
4 5 6 next next next next next
down right down right down right down right down right down right
H0 next 0 0 2
down right down right
next 1 0 4 1 3 3
H1
down right down right down right
next
H2
down right
next 3 0 8 3 3 1
H3
down right down right down right
next 4 2 6
H4 down right down right
NOTE: