You are on page 1of 5

SPARSE MATRICES

SPARSE MATRICES
A useful application of linear list is the representation of matrices that contain a preponderance of zero elements. These matrices are called sparse matrices. Consider the matrix 0 0 6 0 9 0 0 2 0 0 7 8 0 4 10 0 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 5 Of the 42 elements in the 6*7 matrix, only 10 are nonzero. One of the basic methods of storing is to store nonzero elements in a one-dimensional array and to identify each array element with row and column indices as shown in figure (a). The ith element of vector A is the matrix element with row and column indices ROW[i] and COLUMN[i]. Matrix elements are stored in row major order with zero elements removed. In a more efficient representation shown in figure (b), the ROW vector is changed so that its ith element is the index to the first of column indices for the elements in row i of the matrix. ROW COLUMN A 1 1 3 6 2 1 5 9 3 2 1 2 4 2 4 7 5 2 5 8 6 2 7 4 7 3 1 10 8 4 3 12 9 6 4 3 10 6 7 5 figure (a) COLUMN A 1 3 6 ROW 2 5 9 1 1 3 1 2 2 3 4 4 7 3 7 5 5 8 4 8 6 7 4 5 0 7 1 10 6 9 8 3 12 row 9 4 3 number 10 7 5 column number figure (b) An algorithm for adding 2 matrices that are represented in figure(b) is given below: Algorithm MATRIX_ADDITION. Given sparse matrices A and B represented by vectors A and B with row and column indices AROW and ACOL, BROW and BCOL, respectively, it is required to form the matrix sum C=A+B. C must be represented in the same manner A and B, so CROW and CCOL are formed. A and B have the same dimensions, M*N, and contain R and S nonzero elements, respectively. The number of nonzero elements in C on completion of the algorithm is T. Auxiliary variable l is used to index the rows of the matrices and J and K index the matrix elements in vectors A and B, respectively. Variables SUM and COLUMN are used to contain each new element for matrix C and column position.

http://www.mec.ac.in/resources/notes/notes/ds/sparse.htm[4/3/2013 5:50:23 PM]

SPARSE MATRICES

1. Initialize l=1 T=0 2. Scan each row Repeat thru step 9 while l<=M 3. Obtain row indices and starting positions of next rows J=AROW[l] K=BROW[l] CROW[l]=T+1 AMAX=BMAX=0 If l<M then Repeat for P=l+1, l+2, ......M while AMAX=0 If AROW[P]/=0 then AMAX=AROW[P] Repeat for P=l+1, l+2,.......M while BMAX=0 If BROW[P]/=0 then BMAX=BROW[P] If AMAX=0 then AMAX=R+1 If BMAX=0 then BMAX=S+1 4. Scan columns of this row Repeat thru step 7 while J/=0 and K/=0 5. Elements in same column? If ACOL[J]=BCOL[K] then SUM=A[J]+B[K] COLUMN=ACOL[J] J=J+1 K=K+1 else If ACOL[J]<BCOL[K] then SUM=A[J] COLUMN=ACOL[J] J=J+1 else SUM=B[K] COLUMN=BCOL[K] K=K+1 6. Add new elements to sum of matrices If SUM/=0 then T=T+1 C[T]=SUM CCOL[T]=COLUMN 7. End of either row? If J=AMAX then J=0 If K=BMAX then K=0 8. Add remaining elements of a row If J=0 and K/=0 then repeat while K<BMAX T=T+1 C[T]=B[K] CCOL[T]=BCOL[K] K=K+1 else if K=0 and J/=0

http://www.mec.ac.in/resources/notes/notes/ds/sparse.htm[4/3/2013 5:50:23 PM]

SPARSE MATRICES

then repeat while J<AMAX T=T+1 C[T]=A[J] CCOL[T]=ACOL[J] J=J+1 9. Adjust index to matrix C and increment row index If T<CROW[l] then CROW[l]=0 l=l+1 10. Finished Exit For each pair of correspondin rows in matrices A and B, steps 3 to 9 are executed to add matrix elements from those rows. When J or K is zero, the nonzero elements in the row of matrix A or B, respectively, have all been accounted for. When both J and K are zero, the algorithm can proceed to add the next rows of the matrices. If J or K are nor initially zero, they are set to zero when they reach the values AMAX or BMAX, respectively. AMAX and BMAX are the positions in the ACOL and A and BCOL and B vectors where the next row starts. However, if there is no next row, AMAX and BMAX have values R+1 and S+1, respectively. Steps 5 to 7 inclusive perform the required additions of matrix elements. Step 6 checks for an element having a value zero before adding it to matrix C. If no elements are added to row 1 of matrix C, then CROW[l] is set to zero in step 9. A basic node structure called MATRIX_ELEMENT as depicted in the below figure is required to represent sparse matrices. The V, R and C fields of one of those nodes contain the value, row, and column indices, respectively, of one matrix element. The fields LEFT and UP are pointers to the next element in a circular list containing matrix elements for a row or column. LEFT points to the node with the next smallest column subscript, and UP points to the node with the next smallest row subscript. _____________ | LEFT UP| | V | R | C | A circular list represent each row and column. A column's list can share nodes with one or more of the row's list. Each row and column list has a head node such that more efficient insertion and deletion algorithms can be implemented. The head node of each row list contains 0 in the C field. The head node of each column list has 0 in the R field. The row head nodes are pointed to by respective elements in the array of pointers AROW. Elements of ACOL point to the column head nodes. A row or column without nonzero elements is represented by a head node whose LEFT and UP field points to itself. In scanning a circular list we encount matrix elements in order of decreasing row or column subscripts. This is used to simplify the insertion of new nodes to the structue. We assume that new nodes being added to a matrix are usually ordered by ascending-row subscript and ascending-column subscript. A new node is inserted following the head node all the time and no searching of the list is necessary. Algorithm for constructing a multilinked structue representing a matrix is given below. It is assumed that input records for the algorithm consist of row, column, and nonzero matrix-element values in arbitrary order. Algorithm CONSTRUCT_MATIX. It is required to form a multilinked representation of a matrix using the MATRIX-ELEMENT node structure. The matrix dimensions M and N, representing the number of rows and columns are known before execution of algorithm. Arrays AROW and ACOL contain pointers to the head nodes of the circular lists. X and Y are used as auxiliary pointers. A row index, column index, and value of a matrix element are read into variables ROW, COLUMN, and VALUE. 1. Initialize matix structures repeat for l=1,2,.....M ACROW[l]=MATRIX_ELEMENT C(AROW[l])=0 LEFT(AROW[l]=AROW[l] repeat for l=1,2......N ACOL[l]=MATRIX_ELEMENT R(ACOL[l]=0 UP(ACOL[l]=ACOL[l] 2. Loop until there is no more input repeat thru step 7 until input records are exhausted 3. Obtain the next matrix element

http://www.mec.ac.in/resources/notes/notes/ds/sparse.htm[4/3/2013 5:50:23 PM]

SPARSE MATRICES

read(ROW, COLUMN, VALUE) 4. Allocate and initialize a node P=MATRIX_ELEMENT R(P)=ROW C(P)=COLUMN V(P)=VALUE 5. Find new node's position in row list Q=AROW[R(P)] repeat while C(P)<C(LEFT(Q)) Q=LEFT(Q) LEFT(P)=LEFT(Q) LEFT(Q)=P 6. Find new node's position in column list Q=ACOL[C(P)] repeat while R(P)<R(UP(Q)) Q=UP(Q) UP(P)=UP(Q) UP(Q)=P 7. Finished Exit In step 1, the required head nodes are allocated and initialized. For each ROW, COLUMN, and VALUE triplet subsequently read, a node is allocated and initialized. Step 5 and 6 insert the new node in the appropriat row and column lists. When 2 matrices A and B are multiplied to form matrix C, its necessary that the number of columns in A equal the number of rows in B. If A has m rows and n columns, and B has n rows and t columns, then the product matrix C will have m rows and t columns. An algorithm for matrix multiplication is given below. Algorithm MATRIX_MULTIPLICATION. Give the pointer arrays AROW, ACOL, BROW, AND BCOL pointing to multilinked representations of sparse matrices A and B with dimensions M*N and N*T, its required to form the representation of the product matrix C=A*B. Pointer arrays CROW and CCOL are used to point to rows and columns of the matrix C, which has dimensions M*T. Variables l and j are used to count the rows of matrix A and the columns of matrix B. A and B are used as pointers for scanning the rows of matrix A and columns of matrix B. P is an auxiliary pointer. 1. Set up head nodes for row lists repeat for l=1,2,.....M CROW[l]=MATRIX_ELEMENT C(CROW[l]=0 LEFT(CROW[l]=CROW[l] 2. Set up head nodes for column lists repeat for j=1,2,.....T CCOL[j]=MATRIX_ELEMENT R(CCOL[j]=CCOL[j] 3. Use M rows of matrix A repeat thru step 7 for l=1,2,......M 4. Use T columns of matrix B repeat thru step 7 for j=1,2,......T 5. Initialize for scanning row l of matrix A and column j of matrix B A=LEFT(AROW[l]) B=UP(BCOL[j]) PRODUCT=0 6. Move pointers as necessary and multiply matching elements repeat while R(B)/=0 and C(A)/=0 if C(A)>R(B) then A=LEFT(A) else if R(B)>C(A)

http://www.mec.ac.in/resources/notes/notes/ds/sparse.htm[4/3/2013 5:50:23 PM]

SPARSE MATRICES

then B=UP(B) else PRODUCT=PRODUCT+V(A)*V(B) A=LEFT(A) B=UP(B) 7. If product is nonzero add it to matrix C if PRODUCT/=0 then P=MATRIX_ELEMENT R(P)=l C(P)=j V(P)=PRODUCT LEFT(P)=LEFT(CROWN[l]) UP(P)=UP(CCOL[j]) LEFT(CROWN[l])=P UP(CCOL[j])=P 8. Finished Exit Steps 1 and 2 initialize the head nodes for the product matrix C. Steps 3 and 4 provide repetitions necessary to multiply each row of matrix A by each column of matrix B in steps 5 to 7, inclusively. Step 5 initializes pointers A and B to scan the circular lists of row l of matrix A and column j of matrix B. The variable PRODUCT is initialized in this step, and it will be used to total the products of corresponding row and column elements. In step 6, note that row l and column j are being scanned in order of decreading column and row subscripts. If the column subscript and the row subscript of the nodes pointed to by A and B, resp. are not equal, then one pointer is moved to the next node in the circular list. If those row and column subscripts are equal, however, then the variable PRODUCT is updated and both pointers A and B are changed to point to the next elements in each list. When the head node in either list is reached, the required product of row l and column j has been computed. Step 7 allocates and initializes a new node if PRODUCT is nonzero. Because rows and columns are being scanned according to increasing subscript values, and because pointes in the nodes point left and up in the list structure, the new node can be inserted as successor to the head nodes of row l and column j.

http://www.mec.ac.in/resources/notes/notes/ds/sparse.htm[4/3/2013 5:50:23 PM]

You might also like