Data Structure (Part II)

Download Report

Transcript Data Structure (Part II)

Data Structure (Part II)
Chapter 2 – Arrays
Matrix
• A matrix with 5 rows and 3 columns can be
represented by
n=3
m=5
4
 27 3
 6

82  2

 109  64 11 


8
9
 12
 48
27 47 
• 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
matrix square.
• We use two-tuple index to locate a matrix entry.
– Example:
• A(3, 2): entry locating at row 3, column 2.
Col 0
Row 0
Row 1
Row 2
Row 3
Col 1
Col 2
Col 3
3
5 17
4
0

0
22
9


 2 3
1 3


 1 2 0 2
• The matrix is square. It has
4 rows and 4 columns.
•A(3,2) = 0
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:
T
3 0 
3 0 5 


0  1 4  0  1


5 4 
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:
3 + 0
3 0 5 0 0 2  3 0 7
0  1 4  1 1  2  1 0 2

 
 

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:
Row 2
Col 1
0 6
 0 1
1
  1 0   2  1 0    2 1

0
1 0 6 




 23  7  2 18
 2 3 32 

 33
(2,3)  (1,0)T  2  (1)  3 0  2 
C(2, 1)
Sparse Matrix
• A sparse matrix is a matrix that has many zero
entries.
15
0

0

0
91

 0
22 0  15
11 3
0 0 0 
0 0 6 0 0 

0 0
0 0 0 
0 0
0 0 0 

0 28 0 0 0 
0
0
• 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 SparseMatrix
class 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 *this
SparseMatrix &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
columns.
2.4.2 Sparse Matrix Representation
• Example
15
0

0

0
91

 0
22 0  15
11 3
0 0 0 
0 0 6 0 0 

0 0
0 0 0 
0 0
0 0 0 

0 28 0 0 0 
0
0
0
0
R: 0
C: 0
1
1
R: 0
C: 3
2
3
2
R: 0
C: 5
4
0
1
2
3
4
5
0
15
0
0
22
0
-15
1
0
11
3
0
0
0
2
0
0
0
-6
0
0
3
0
0
0
0
0
0
4
91
0
0
0
0
0
5
0
0
28
0
0
0
5
3
R: 1
C: 1
6
7
4
R: 1
C: 2
5
R: 2
C: 3
6
R: 4
C: 0
7
R: 5
C: 2
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;
};
2.4.3 Transposing a Matrix
• Sparse matrix and its transpose
0
smArray
R: 0
C: 0
15
1
R: 3
0
C: 0
3
22
2
R: 0
5
C: 5
0
-15
3
R: 1
C: 1
11
4
R: 2
1
C: 1
2
5
R: 3
2
C: 2
3
3
-6
6
R: 4
0
C: 0
4
91
7
R: 5
2
C: 2
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
Algorithm of Program 2.10
SparseMatrix 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;
}
Algorithm of Program 2.10
Line 1:
2: Construct
3:
currentB
If
(terms >=a0)
0;SparseMatrix, b(cols, rows, terms), as output result;
Line 4:
Line 5:
For
c=1
0(c = 0; c < cols; c++)
For (i=0; i < terms; i++)
Line 6:
If (smArray[i].col == c
0)
Line 9:
7:
8:
b.smArray[currentB].row
b.smArray[currentB++].value
b.smArray[currentB].col==smArray[i].col;
smArray[i].row;
= smArray[i].value;
0
smArray
b.smArray
R: 0
C: 0
15
R: 0
C: 0
15
currentB
1
R: 0
C: 3
22
R: 0
C: 4
91
2
R: 0
C: 5
-15
R: 1
C: 1
11
3
R: 1
C: 1
4
R: 1
C: 2
11
R: 2
C: 1
R: 2
C: 3
3
R: 2
C: 5
3
5
28
-6
R: 3
C: 0
22
6
R: 4
C: 0
91
R: 3
C: 2
-6
7
R: 5
C: 2
28
R: 5
C: 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
in O(terms + cols).
• How to locate the entry in transposed smArray?
0
R: 0
C: 0
15
1
2
3
4
5
6
R: 0
C: 3
R: 0
C: 5
R: 1
C: 1
R: 1
C: 2
R: 2
C: 3
R: 4
C: 0
22
-15
11
3
-6
91
7
R: 5
C: 2
28
?
R: 0
C: 0
15
R: 0
C: 4
R: 1
C: 1
91
R: 2
C: 1
11
rowStart:
Col:
rowSize:
R: 2
C: 5
3
R: 3
C: 0
28
22
0
2
3
5
7
7
0
1
2
3
4
5
2
1
2
2
0
1
R: 3
C: 2
-6
R: 5
C: 0
-15
Algorithm of Program 2.11
SparseMatrix 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;
}
i
0
R: 0
C: 0
15
R: 0
C: 0
15
1
2
3
4
5
6
R: 0
C: 3
R: 0
C: 5
R: 1
C: 1
R: 1
C: 2
R: 2
C: 3
R: 4
C: 0
22
R: 0
C: 4
-15
11
R: 1
C: 1
91
R: 2
C: 1
11
rowStart:
Col:
rowSize:
3
-6
R: 2
C: 5
3
R: 3
C: 0
22
28
0
1
2
3
5
6
7
7
8
0
1
2
3
4
5
0
1
2
0
1
0
1
2
0
1
2
0
0
1
91
R: 3
C: 2
-6
7
R: 5
C: 2
28
R: 5
C: 0
-15
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,
Line 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
FastTranspose 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
matrix d has dimension m x p. Its (i, j) element is
n 1
d ij   aik bkj
k 0
for 0  i  m and 0  j  p.
2.4.4 Matrix Multiplication
• The product of two sparse matrices may no
longer be sparse.
1 0 0 1 1 1 1 1 1
1 0 0 0 0 0  1 1 1

 
 

1 0 0 0 0 0 1 1 1
• We would like to multiply two sparse matrices
represented as ordered lists.
2.4.4 Matrix Multiplication
j
i
15
0

0

0
91

 0
22 0  15
11 3
0 0 0 
0 0 6 0 0 

0 0
0 0 0 
0 0
0 0 0 

0 28 0 0 0 
0
0
15
0

0

0
91

 0
22 0  15
11 3
0 0 0 
0 0 6 0 0 

0 0
0 0 0 
0 0
0 0 0 

0 28 0 0 0 
0
0
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.
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
currRowBegin
R: 0
C: 0
R: 0
C: 3
15
R: 0
C: 5
22
R: 1
C: 1
-15
R: 1
C: 2
11
R: 2
C: 3
3
-6
R: 4
C: 0
91
R: 5
C: 2
28
currRowIndex
R: 0
C: 0
dummy
R: 0
C: 4
15
R: 1
C: 1
91
R: 2
C: 1
11
R: 2
C: 5
3
R: 3
C: 0
28
22
R: 3
C: 2
-6
R: 5
C: 0
R: 6
C: -1
-15
Sum = 0
225
currColIndex
currRowA
d:
currColB
15
0

0

0
91

 0
0
0
11 3
0
0
0
0
0
0
0 28
22 0  15
0 0 0 
6 0 0 

0 0 0 
0 0 0 

0 0 0 
15
0

0

0
91

 0
0
0
11 3
0
0
0
0
0
0
0 28
22 0  15
0 0 0 
6 0 0 

0 0 0 
0 0 0 

0 0 0 
0
225
0

0

0
0

0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

0 0 0 0 0
0 0 0 0 0

0 0 0 0 0