27
Data Structure (Part II) Chapter 2 – Arrays

Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix

Embed Size (px)

Citation preview

Data Structure (Part II)

Chapter 2 – Arrays

Matrix

• A matrix with 5 rows and 3 columns can be represented by

472748

9812

1164109

2826

4327

n = 3

m = 5

• We say this is a 5×3 matrix.• A 5×3 matrix has 15 entries.

entry

Matrix• Suppose a m×n matrix A. If m = n, we call the m

atrix square.• We use two-tuple index to locate a matrix entry.

– Example:• A(3, 2): entry locating at row 3, column 2.

2021

3132

92200

17534 • The matrix is square. It has 4 rows and 4 columns.

•A(3,2) = 0

Col 0 Col 1 Col 2 Col 3

Row 0

Row 1

Row 2

Row 3

In C++, it is natural to store a matrix in a 2D array, say a[m][n], and use a[i][j] to access the entry at (i, j).

Matrix Operations

• Transposition– Suppose A is a m×n matrix.– AT is called the transpose of A where AT is n×m and AT(i, j) = A(j, i)

for i = 0, …., m-1, and j = 0, …, n-1. – Example:

45

10

03

410

503T

A(0, 2) = AT(2, 0) = 5

Matrix Operations

• Addition:– Suppose A is a RA×CA matrix and B a RB×CB matrix.

– To sum up A and B, RA must equal to RB; CA must equal to CB.

– If C = A + B, then C(i, j) = A(i, j) + B(i, j) for i=0, …, RA-1, j=0, …, CA-1.

– Example:

201

703

211

200

410

503

3 + 0

Matrix Operations

• Multiplication– Suppose A is a RA×CA matrix and B a RB×CB matrix.

– To compute A×B, CA must equal to RB. If C = A×B, C is a RA×CB matrix, where C(i, j) = Σ(A(i, k). A(k, j)).

– Example:

3332

231827

012

601

601

012

32

01

10

C(2, 1)

Row 2

Col 1

203)1(2)0,1()3,2( T

Sparse Matrix

• A sparse matrix is a matrix that has many zero entries.

0002800

0000091

000000

006000

0003110

150220015• This is a __×__ matrix.

• There are _____ entries.

• There are _____ nonzero entries.

• There are _____ zero entries.

Consider we use a 2D array to represent a n×n sparse matrix. How many entries are required? _____ entries. The space complexity is O( ).

2.4 Sparse Matrix

• If n is large, say n = 5000, we need 25 million elements to store the matrix.

• 25 million units of time for operation such as addition and transposition.

• Using a representation that stores only the nonzero entries can reduce the space and time requirements considerably.

ADT SparseMatrixclass SparseMatrix {public:

//r rows, c columns, a capacity of t nonzero entries. SparseMatrix(int r, int c, int t);//interchange the row and column value of every tuple in *thisSparseMatrix &Transpose();//If the dimensions of *this and b are the same, sum up a*this and b.//Otherwise, throw exception.SparseMatrix &Add(SparseMatrix &b);//If the number of columns in *this equals to the number of rows in

b, //compute and return the multiplication of *this and b.SparseMatrix &Multiply(SparseMatrix &b);

};

2.4.2 Sparse Matrix Representation

• The information we need to know– The number of rows– The number of columns– The number of nonzero entries– All the nonzero entries are stored in an array.

Therefore, we also have to know• The capacity of the array• Each element contains a triple <row, col, value> to

store.– The triples are ordered by rows and within rows by colum

ns.

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

0 1 2 3 4 5 6 7

15

91

11 3

28

22

-6

-15

2.4.2 Sparse Matrix Representation

• Example

0002800

0000091

000000

006000

0003110

150220015 15

0

0

0

91

0

0

11

0

0

0

0

0

3

0

0

0

28

22

0

-6

0

0

0

0

0

0

0

0

0

-15

0

0

0

0

0

0

1

2

3

4

5

0 1 2 3 4 5

0 1 2 3 4 5 6 7

2.4.2 Sparse Matrix Representation

class SparseMatrix;class MatrixEntry {friend class SparseMatrix;private:

int row, col, value;};

class SparseMatrix {private:

int rows, cols, terms, capacity;MatrixEntry *smArray;

};

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

2.4.3 Transposing a Matrix

• Sparse matrix and its transpose

0 1 2 3 4 5 6 7

smArray

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

R: 0C: 0

15

R: 3C: 0

22

R: 5C: 0

-15

R: 1C: 1

11

R: 2C: 1

3

R: 3C: 2

-6

R: 0C: 4

91

R: 2C: 5

28

smArray

Consider column 0.

For all elements in column 0

Store (i, 0, value) of the original matrix as (0, i, value) of the transpose

R: 0C: 0

15

R: 4C: 0

91

Algorithm of Program 2.10SparseMatrix SparseMatrix::Transpose(){1 Construct a SparseMatrix, b(cols, rows, terms), as output result;2 currentB = 0;3 If (terms > 0) {4 For (c = 0; c < cols; c++)5 For (i=0; i < terms; i++)6 If (smArray[i].col == c) {7 b.smArray[currentB].row = smArray[i].col;8 b.smArray[currentB].col = smArray[i].row;9 b.smArray[currentB++].value = smArray[i].value;10 }11 }12 Return b;}

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

Algorithm of Program 2.10

0 1 2 3 4 5 6 7

smArray

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

R: 0C: 0

15

R: 0C: 3

22

R: 0C: 5

-15

R: 1C: 1

11

R: 1C: 2

3

R: 2C: 3

-6

R: 4C: 0

91

R: 5C: 2

28

b.smArray

currentB

Line 1: Construct a SparseMatrix, b(cols, rows, terms), as output result;Line 2: currentB = 0; Line 3: If (terms > 0)

Line 4: For (c = 0; c < cols; c++)c = 0

Line 5: For (i=0; i < terms; i++)

Line 6: If (smArray[i].col == )c0

R: 0C:R: 0C: 0R: 0C: 0

15

R: 0C:R: 0C: 4R: 0C: 4

91

Line 7: b.smArray[currentB].row = smArray[i].col;Line 8: b.smArray[currentB].col = smArray[i].row;Line 9: b.smArray[currentB++].value = smArray[i].value;

c = 1

R: 1C: 1

11

R: 2C: 1

3

R: 2C: 5

28

R: 3C: 0

22

R: 3C: 2

-6

R: 5C: 0

-15

Analysis of Transpose()

• Space complexity:– We need an extra SparseMatrix to buffer the entries. – O(terms)

• Time complexity:– Line 1 to 2: constant time.– Line 4 to 10: cols iterations.

• For each iteration, every term is checked through Line 5 to 10. Line 6 is executed exactly terms times.

– Line 12: constant time.– Overall, time complexity is O(cols. terms).

Considerations

• In worst case, the matrix contains rows. cols entries. Therefore, the time complexity becomes

O(cols. terms) = O(rows. cols2)– worse than O(rows. cols) time using the simple form!

• Why do we need the for-loop at Line 5?– Because the transposed smArray has to be ordered by

columns. – If using a little more space, we can transpose a matrix i

n O(terms + cols).

• How to locate the entry in transposed smArray?

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

0 1 2 3 4 5 6 7R: 0C: 0

15

R: 0C: 3

22

R: 0C: 5

-15

R: 1C: 1

11

R: 1C: 2

3

R: 2C: 3

-6

R: 4C: 0

91

R: 5C: 2

28

R: 0C: 0

15

R: 0C: 4

91

R: 1C: 1

11

R: 2C: 1

3

R: 2C: 5

28

R: 3C: 0

22

R: 3C: 2

-6

R: 5C: 0

-15

?R: 0C: 0

15

R: 0C: 4

91

R: 1C: 1

11

R: 2C: 1

3

R: 2C: 5

28

R: 3C: 0

22

R: 3C: 2

-6

R: 5C: 0

-15

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

R: 0C: 0

15

R: 0C: 3

22

R: 0C: 5

-15

R: 1C: 1

11

R: 1C: 2

3

R: 2C: 3

-6

R: 4C: 0

91

R: 5C: 2

28

Col: 0 1 2 3 4

0 2 3 5 7rowStart: 7

5

2 1 2 2 0rowSize: 1

Algorithm of Program 2.11SparseMatrix SparseMatrix::FastTranspose(){1. Construct a SparseMatrix, b(cols, rows, terms), as output result;2. If (terms > 0) {3. Let rowSize be an integer array of size cols.4. Let rowStart be an integer array of size cols. 5. Initialize each element in rowSize to 0.6. For (i=0; i<terms; i++)7. rowSize [smArray[i].col]++;8. rowStart [0] = 0;9. For (i = 1; i < cols; i++)10. rowStart [i] = rowSize [i-1] + rowStart [i-1];11. For (i=0; i < terms; i++) {12. j = rowStart [ smArray [i].col ];13. Copy smArray[ i ] to smArray[ j ];14. Interchange row and col of smArray[ j ];15. rowStart [ smArray [i].col ]++; 16. } 17. }18. Return b;}

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

0 1 2 3 4 5 6 7R: 0C: 0

15

R: 0C: 3

22

R: 0C: 5

-15

R: 1C: 1

11

R: 1C: 2

3

R: 2C: 3

-6

R: 4C: 0

91

R: 5C: 2

28

R: 0C: 0

15

R: 3C: 0

22

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

R: 0C: 0

15

R: 0C: 3

22

R: 0C: 5

-15

R: 1C: 1

11

R: 1C: 2

3

R: 2C: 3

-6

R: 4C: 0

91

R: 5C: 2

28

Col: 0 1 2 3 4 5

0 0 0 0 0rowSize: 01 0 0 0 0rowSize: 01 0 0 1 0rowSize: 01 0 0 1 0rowSize: 11 1 0 1 0rowSize: 11 1 1 1 0rowSize: 11 1 1 2 0rowSize: 12 1 1 2 0rowSize: 12 1 2 2 0rowSize: 1

0 2 3 5 7rowStart: 70rowStart: 0 2rowStart: 0 2 3rowStart: 0 2 3 5rowStart: 0 2 3 5 7rowStart: 0 2 3 5 7rowStart: 7

i

1 2 3 5 7rowStart: 71 2 3 6 7rowStart: 7

R: 0C: 4

91

R: 1C: 1

11

R: 2C: 1

3

R: 2C: 5

28

R: 3C: 2

-6

R: 5C: 0

-15

1 2 3 6 7rowStart: 8

Analysis of FastTranspose()

• Space complexity:– SparseMatrix b to buffer the entries: O(terms).– rowSize: cols elements; O(cols). – rowStart: cols elements; O(cols).– Overall, O(terms+cols).

• Time complexity:– Line 5: O(cols) to initialize rowSize.– Line 6: O(terms) to scan all terms in smArray.– Line 9: O(cols) to compute rowStart.– Line 11 to 16: the loop executes terms times. In each iteration, L

ine 12 to 15 runs in constant time. Therefore, time complexity is O(terms).

– Overall, time complexity of FastTranspose() is O(terms+cols).

Analysis of FastTranspose()

• The time of O(terms+cols) becomes O(rows.cols) when terms equals to rows. cols. – The same complexity with the simple form.– However, the actual computation time of FastTranspo

se will be a bit larger.

• When terms is sufficiently small, FastTranspose will be faster.

2.4.4 Matrix Multiplication

• Definition: – Given two matrices, amxn and bnxp. The product matri

x d has dimension m x p. Its (i, j) element is

.0 and 0for

1

0

pjmi

badn

kkjikij

2.4.4 Matrix Multiplication

• The product of two sparse matrices may no longer be sparse.

• We would like to multiply two sparse matrices represented as ordered lists.

111

111

111

000

000

111

001

001

001

2.4.4 Matrix Multiplication

0002800

0000091

000000

006000

0003110

150220015

0002800

0000091

000000

006000

0003110

150220015

We have to find all the elements in column j.• Scan all the elements in smArray? Exhausted!• We can compute the transpose of b.

•This puts all column elements in consecutive order.

i

j

StoreSum() and ChangeSize()

• ChangeSize1D(int newSize)– Program 2.13– To change the size of smArray to newSize.

• StoreSum(int sum, int r, int c)– Program 2.12– To store sum in the row of r and the column of

c if sum is nonzero.– Invoke ChangeSize1D when smArray is full.

Program 2.14

R: 0C: 0

15

R: 0C: 4

91

R: 1C: 1

11

R: 2C: 1

3

R: 2C: 5

28

R: 3C: 0

22

R: 3C: 2

-6

R: 5C: 0

-15

R: 0C: 0

R: 5C: 2

R: 0C: 5

R: 1C: 1

R: 1C: 2

R: 2C: 3

R: 4C: 0

R: 0C: 3

15 22 -15 11 3 -6 91 28

R: 0C: 0

15

R: 0C: 3

22

R: 0C: 5

-15

R: 1C: 1

11

R: 1C: 2

3

R: 2C: 3

-6

R: 4C: 0

91

R: 5C: 2

28

currColIndex

currRowIndexR: 6C: -1

dummy

0002800

0000091

000000

006000

0003110

150220015

0002800

0000091

000000

006000

0003110

150220015currRowA

currColB

000000

000000

000000

000000

000000

000000

d:

currRowBegin

Sum = 225

225

Sum = 0