Sparse Matrices sparse many elements are zero dense few elements are zero
Special Matrices A square matrix has the same number of rows and columns. Some special forms of square matrices are Diagonal: M(i,j) = 0 for i j Tridiagonal: M(i,j) = 0 for i-j < 1 Lower triangular: M(i,j) = 0 for i < j Upper triangular: M(i,j) = 0 for i > j Symmetric M(i,j) = M(j,i) for all i and j See Figure 2
Special Matrices 3
Example Of Sparse Matrices Diagonal Tri-diagonal lower triangular (?) Two possible representations of sparse matrix: 1. array 2. linked list
For example consider a matrix having 100 row and 100 column that have only 2000 non zero elements. Space required by above matrix is 1000,000. So we can store all the non-zero elements of above matrix in a 1D matrix of size 2000. or Create a linked list of non-zero elements.
Array Representation of Sparse Matrix The nonzero entries may be mapped into a 1D array in row-major order To reconstruct the matrix structure, need to record the row and column each nonzero comes from 6
Linked Representation of Sparse Matrix A shortcoming of the 1-D array of a sparse matrix is that we need to know the number of nonzero terms in each of the sparse matrices when the array is created. A linked representation can overcome this shortcoming. 7
Unstructured Sparse Matrices Airline flight matrix. airports are numbered 1 through n flight(i,j) = list of nonstop flights from airport i to airport j n = 1000 (say) n x n array of list references => 4 million bytes total number of flights = 20,000 (say) need at most 20,000 list references => at most 80,000 bytes
Representation Of Unstructured Sparse Matrices Single linear list in row-major order. scan the nonzero elements of the sparse matrix in rowmajor order. each nonzero element is represented by a triple (row, column, value) the list of triples may be an array list or a linked list (chain)
Single Linear List Example 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 list = row 1 1 2 2 4 4 column 3 5 3 4 2 3 value 3 4 5 7 2 6
Array Linear List Representation row 1 1 2 2 4 4 list = column 3 5 3 4 2 3 value 3 4 5 7 2 6 element 0 1 2 3 4 5 row 1 1 2 2 4 4 column 3 5 3 4 2 3 value 3 4 5 7 2 6
Node structure. Chain Representation row value col next
Single Chain 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
Multi-Linked Lists A multilinked list is a more general linked list with multiple links from nodes. In multi-linked list, each node can have any number of pointers to other nodes, and there may or may not be inverses for each pointer. Multi-lists are essentially the technique of embedding multiple lists into a single data structure. A multi-list has more than one next pointer, like a doubly linked list, but the pointers create separate lists
head Multi-lists
head Multi-lists
Linked Structures A doubly-linked list or multi-list is a data structure with multiple pointers in each node. In a doubly-linked list the two pointers create bidirectional links. In a multi-list the pointers used to make multiple link routes through the data.
Example : List of List Suppose you want to make list of all Customers in particular bank. Bank will have multiple branches. Each branch will have multiple customers.
Multi-list linked representation of Sparse Matrix
3-Tuple Representation Of Sparse Matrix 1 0 0 0 0 0 0 4 0 sparse matrix 3-Tuple Representation Of Sparse Matrix Using Array: no of row no of col no of non zero value 3 3 2 0 0 1 2 1 4
Elements in the first row represents the number of rows, columns and non-zero values in sparse matrix. First Row - 3 3 2 3 - rows 3 - columns 2 - non- zero values Elements in the other rows gives information about the location and value of non-zero elements. 0 0 1 ( Second Row) - represents value 1 at 0th Row, 0th column 2 1 4 (Third Row) - represents value 4 at 2nd Row, 1st column
Multi-list Representation Both row and column lists. Node structure. row col value down next
Row Lists 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 null 1 3 3 1 5 4 n 2 3 5 2 4 7 n 4 2 2 4 3 6 n
Column Lists 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 1 3 3 1 5 4 n 2 3 5 2 4 7 4 2 2 4 3 6 n n
Muti linked Lists 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 null 1 3 3 1 5 4 n n 2 3 5 2 4 7 n row[] 4 2 2 4 3 6 n n n
* structure for col headnode */ struct cheadnode { int colno ; struct node *down ; struct cheadnode *next ; } ; /* structure for row headnode */ struct rheadnode { int rowno ; struct node * right ; struct rheadnode *next ; } ;
/* structure for node to store element */ struct node { int row ; int col ; int val ; struct node *right ; struct node *down ; } ; /* structure for special headnode */ struct spmat { struct rheadnode *firstrow ; struct cheadnode *firstcol ; int noofrows ; int noofcols ; } ;
struct sparse { int *sp ; int row ; struct spmat *smat ; struct cheadnode *chead[max2] ; struct rheadnode *rhead[max1] ; struct node *nd ; } ;
One Linear List Per Row 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 row1 = [(3, 3), (5,4)] row2 = [(3,5), (4,7)] row3 = [] row4 = [(2,2), (3,6)]
Node structure. Array Of Row Chains col next value
Array Of Row Chains null 3 3 5 4 0 0 3 0 4 null 0 0 5 7 0 3 5 4 7 0 0 0 0 0 0 2 6 0 0 null null 2 2 3 6 row[]
Orthogonal List Representation Both row and column lists. Node structure. row down col next value
Row Lists 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 null 1 3 3 1 5 4 n 2 3 5 2 4 7 n 4 2 2 4 3 6 n
Column Lists 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 1 3 3 1 5 4 n 2 3 5 2 4 7 4 2 2 4 3 6 n n
Orthogonal Lists 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 null 1 3 3 1 5 4 n n 2 3 5 2 4 7 n row[] 4 2 2 4 3 6 n n n
Variations May use circular lists instead of chains.
Approximate Memory Requirements 500 x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row 23928 + 500 x 4 = 25,928
Runtime Performance Matrix Transpose 500 x 500 matrix with 1994 nonzero elements 2D array Single Array List One Chain Per Row 210 ms 6 ms 12 ms
Performance Matrix Addition. 500 x 500 matrices with 1994 and 999 nonzero elements 2D array Single Array List One Chain Per Row 880 ms 18 ms 29 ms