25

sparse matrices in tress

Embed Size (px)

Citation preview

• Definition

• Sequential Representation of sparse matrices

• Node structure for linked allocation representation of sparse matrices

• Applications of sparse matrices

Sparse matrices is defined as the matrix

containing maximum number of zero elements here we represent only non zero elements instead of representing all.

It contains hundreds or even thousands of rows

and columns.

• Consider a matrix

A =

6x7=42 Elements,10 are non zero

A[1,3]=6,A=[1,5]=9,A[2,1]=2,A[2,4]=7

A[2,5]=8,A[2,7]=4,A[3,1]=10,A[4,3]=12

A[6,4]=3,A[6,7]=5

0 0 6 0 9 0 02 0 0 7 8 0 4

10 0 0 0 0 0 00 0 12 0 0 0 00 0 0 0 0 0 0 0 0 0 3 0 0 5

Row Column A

1

2

3

4

5

6 (

7

8

9

10 3

1

1

2

2

2

2

3

4

6

6

3

5

1

4

5

7

1

3

4

7

6

9

2

7

8

4

10

12

3

5

Sequential representation of Sparse matrices

1

3

7

8

0

9

RowNumber

First columnFor rownumber

1

2

3

4

5

6

7

8

9

10

Column number

Column A6

9

2

7

8

4

10

12

3

5

3

5

14

5

7

1

3

4

7

Sequential representation of Sparse matrices

1

2

3

4

5

6

Matrix 3×3

MATRIX ELEMENT

ACOL[2]ACOL[1] ACOL[3]

AROW[1]

AROW[2]

AROW[3]

Algorithm: MATRIX Addition.A General algorithm for adding two matrices as follows.

1. Repeat thru Step 8 until all rows have been processed

2. Obtain current row indices for matrix A and B

3. Obtain staring positions in vectors of next row in matrix A and B

4. Repeat thru step 6 until either the end of matrix A’s or B’s rows is reached

5. If elements exist in the same column in matrix A and B then sum elements and move on to the next column in matrix A and B

Else

If matrix A column number is less than matrix B’s column number

Then move on to the next column in matrix A

Else move on to the next column in matrix B

6. Add new element to matrix C

7. If the end of matrix A’s row has not been reached

Then add remaining elements in row to matrix C

8. If the end of matrix B’s row has not been reached

Then add remaining elements in row to matrix C

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 matrix sum C=A+B.

C Must be represented in the same manner as A and B, so CROW and CCOL are formed. A and B have same dimensions, M×N, and contain R and S non zero elements respectively. The number of non zero elements in C on completion of the algorithm is T.

Auxiliary variable I is used to index the rows of the matrices and J and K index of the matrix elements in vector A and B respectively.

Variable Sum and COLUMN are used to contain each new element for matrix C and its column position.

1. [Initialize]

I 1

T 0

2. [Scan each row]

Repeat thru step 9 while l<=m

3. [Obtain row indices and starting position of rows]

j Arow[I]

K Crow[I]

Crow[1]T+1

AMAXBMAX0

If I<m

then repeat for p=1+1,1+2,….,M While AMAX=0

If AROW[P]!=0

then AMAXAROW[P]

Repeat for P=1+1,1+2,….,M While BMAX=0

Continued………If Brow[p]!=0

then BMAX BROW [P]if AMAX=0then AMAX=0if BMAX=0then BMAX=0then BMAXS+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[i]+B[k]COLUMNACOL[J]JJ+1KK+1

Else if ACOL[J]<BCOL[K]then SUM A[J]COLUMNACOL[J]

JJ+1

Continued………else if ACOL[J]<BCOL[K]then SUMACOL[J]

COLUMNACOL[J]JJ+1

else SUMB[K]COLUMNBCOL[K]KK+1

6. [Add new elements to sum of matrices]If SUM != 0then TT+1C[T]SUMCCOL[T]COLUMN

7.[End of either row?]If J =AMAXthen J0If K= BMAXthen K0

8.[Add remaining elements of row]If J = 0 and K != 0

Continued………..then Repeat while k < BMAX

T T + 1

C[T]BCOL[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]

JJ+1

9.[Adjust index to matrix C and increment row index]

If T<CROW[I]

then CROW[I]0

II+1

10.[Finished]

Exit

Algorithm: CONSTRUCT MATRIX

A General algorithm to construct a multilinked representation of sparse matrix is now presented.

1. Allocate and initialize head nodes for matrix

2. Repeat thru step 6 until there is no more input

3. Read new column, row, and value information

4. Allocate and initialize a new node

5. Find position for new node in row list and insert node in list

6. Find position for new node in column list and insert node in list

It is required to form a multilinked representation of a matrix using the MATRIX_ELEMENT node structure previously described. The matrix dimensions M and N

Representing the number of rows and number of columns

Respectively, are known before execution of algorithm. Arrays AROW and ACOL contain pointers to the head nodes of the circular list. X and Y are used as auxiliary pointers. A row index, column index and the value of a matrix element are read into variables ROW, COLUMN and VALUE respectively.

1.[Initialize matrix structure]

Repeat for I=1,2,….,M

AROW[I]MATRIX_ELEMENT

C(AROW[I]0

LEFT(AROW[I]AROW[I]

Repeat A for I=1,2,….,N

ACOL [I]MATRIX_ELEMENT

R(ACOL[I])0

UP(ACOL[I]ACOL[I]

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)

Continued………..4.[Allocate and initialize a node]

PMATRIX_ELEMENTR(P)ROWC(P)COLUMNV(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))QUP(Q) UP(P)UP(Q)UP(Q)P

7. [finished]Exit

A General algorithm to perform matrix multiplication with multilinked structures is as follows.

1. Allocate and initialize head nodes for matrix C

2. Repeat thru step 6 for each row of matrix A

3. Repeat thru step 6 for each column of matrix B

4. Repeat step 5 until either the end of a row list of column list is reached

5. If the current column number of matrix A is greater then the current row number of matrix B

Algorithm: MATRIX MULTIPLICATION

Then obtain the next higher row in matrix B

Else update the product

Obtain the next left column in matrix A and the higher row in matrix B

6. If the product is not equal zero

Then allocate and initialize a new node

Insert the node in matrix c

Given the pointer arrays AROW, ACOL, BROW and BCOL pointing to multilinked representation of sparse matrices A and B with dimensions M×N and N×T respectively, it is required to form the representation of the product of the product C=A×B . Pointer arrays CROW and CCOL are used to point to rows and columns of the matrix C, which has dimension M×T. Variables I and J are used to count the rows of matrix A and the column of matrix B respectively. 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,3,….M

CROW[I]MATRIX_ELEMENT

C(ROW[I])0;

LEFT(CROW[I])CROW[I]

2. [Set up head nodes for column lists]

Repeat for J==1,2,……T

CCOL[J]MATRIX _ELEMENT

R(CCOL[J])0

UP(CCOL[J])CCOL[J]

3. [Use M rows of matrix A]

Repeat thru step 7 for I=1,2,…..M

4. [Use T Columns of matrix B]

Repeat thru step 7 for J=1,2,…..T

Continued……..5. [Initialize for Scanning row I of Matrix A and Column J of Matrix B]

ALEFT(AROW[I])

B UP(BCOL[J])

PRODUCT0

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 BUP(B)

else PRODUCT PRODUCT+V(A)*V(B)

ALEFT(A)

BUP(B)

7. [if product is non zero add it to matrix c]

if PRODUCT !=0

then PMATRIX ELEMENT

Continued…………R(P)I

C(P)J

V(P)PRODUCT

LEFT(P)LEFT(CROW[I])

UP(P)UP(CCOL[J])

LEFT(CROW[I])P

UP(CCOL[J])P

8.[Finished]

Exit

Conclusion

By sparse matrices we can overcome the

disadvantage of representing zero elements and thus prevents wastage of memory.

Hence sparse matrices are mainly used to save the memory by representing only non zero element of the matrix thus increases the efficiency.

• Sparse matrices technique are used to search, retrieval, classification,and relationship analysis in large database systems.

• It is used in molecular dynamics.

Applications

Reference

An introduction to Data structures with applications.

Author: Jean-Trembley Paul G. Sorenson

www.google.co.in