Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SPARSE MATRICES
SPARSE MATRICES
A useful application of linear list is the representation of matrices that contain a preponderance of zero e 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 st nonzero elements in a one-dimensional array and to identify each array element with row and column indices as sh figure (a). The ith element of vector A is the matrix element with row and column indices ROW[i] and COLUMN Matrix elements are stored in row major order with zero elements removed. In a more efficient representation sho figure (b), the ROW vector is changed so that its ith element is the index to the first of column indices for the elem row i of the matrix. 1 2 3 4 5 6 7 8 9 10 ROW COLUMN A 1 3 6 1 5 9 2 1 2 2 4 7 2 5 8 2 7 4 3 1 10 4 3 12 6 4 3 6 7 5 figure (a)
11/22/13
SPARSE MATRICES
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 indices AROW and ACOL, BROW and BCOL, respectively, it is required to form the matrix sum C=A+B. C m represented in the same manner A and B, so CROW and CCOL are formed. A and B have the same dimensions, and contain R and S nonzero elements, respectively. The number of nonzero elements in C on completion of the is T. Auxiliary variable l is used to index the rows of the matrices and J and K index the matrix elements in vector B, respectively. Variables SUM and COLUMN are used to contain each new element for matrix C and column po 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
www.mec.ac.in/resources/notes/notes/ds/sparse.htm 2/6
11/22/13
SPARSE MATRICES
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 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 f those rows. When J or K is zero, the nonzero elements in the row of matrix A or B, respectively, have all been ac 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 no zero, they are set to zero when they reach the values AMAX or BMAX, respectively. AMAX and BMAX are the in the ACOL and A and BCOL and B vectors where the next row starts. However, if there is no next row, AMAX 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 havin 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 ze step 9. A basic node structure called MATRIX_ELEMENT as depicted in the below figure is required to represen matrices. The V, R and C fields of one of those nodes contain the value, row, and column indices, respectively, o matrix element. The fields LEFT and UP are pointers to the next element in a circular list containing matrix elemen row or column. LEFT points to the node with the next smallest column subscript, and UP points to the node with 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 Each row and column list has a head node such that more efficient insertion and deletion algorithms can be implem 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. T head nodes are pointed to by respective elements in the array of pointers AROW. Elements of ACOL point to th head nodes. A row or column without nonzero elements is represented by a head node whose LEFT and UP fie to itself.
www.mec.ac.in/resources/notes/notes/ds/sparse.htm 3/6
11/22/13
SPARSE MATRICES
In scanning a circular list we encount matrix elements in order of decreasing row or column subscripts. T used to simplify the insertion of new nodes to the structue. We assume that new nodes being added to a matrix ar ordered by ascending-row subscript and ascending-column subscript. A new node is inserted following the head 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 in 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 MATR ELEMENT node structure. The matrix dimensions M and N, representing the number of rows and columns are k before execution of algorithm. Arrays AROW and ACOL contain pointers to the head nodes of the circular lists Y are used as auxiliary pointers. A row index, column index, and value of a matrix element are read into variables 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 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 t subsequently read, a node is allocated and initialized. Step 5 and 6 insert the new node in the appropriat row and lists.
When 2 matrices A and B are multiplied to form matrix C, its necessary that the number of columns in A eq
www.mec.ac.in/resources/notes/notes/ds/sparse.htm 4/6
11/22/13
SPARSE MATRICES
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 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 pointi 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 co the matrix C, which has dimensions M*T. Variables l and j are used to count the rows of matrix A and the colum matrix B. A and B are used as pointers for scanning the rows of matrix A and columns of matrix B. P is an auxilia 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) 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 nec
www.mec.ac.in/resources/notes/notes/ds/sparse.htm 5/6
11/22/13
SPARSE MATRICES
to multiply each row of matrix A by each column of matrix B in steps 5 to 7, inclusively. Step 5 initializes pointers B to scan the circular lists of row l of matrix A and column j of matrix B. The variable PRODUCT is initialized in 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 subsc the column subscript and the row subscript of the nodes pointed to by A and B, resp. are not equal, then one po moved to the next node in the circular list. If those row and column subscripts are equal, however, then the variab PRODUCT is updated and both pointers A and B are changed to point to the next elements in each list. When th 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 according to increasing subscript values, and because pointes in the nodes point left and up in the list structure, th node can be inserted as successor to the head nodes of row l and column j.
www.mec.ac.in/resources/notes/notes/ds/sparse.htm
6/6