Arrays
Arrays
CH. 2
ARRAYS
Array Abstract
Data Type
4
Array Representations
Sequential mapping
◦ Element ai is stored in the location i of the
array
◦ The most commonly used
◦ Efficient random access (operation 1,2,3)
Non-sequential mapping
◦ Perform insertion and deletion efficiently
◦ E.g. Linked Lists in chapter 4
5
2.3
Polynomial
1 0 4 2 0 1
A[0] A[1] A[2] A[3] A[4] A[5] 7
Polynomial Operation
If 𝑎(𝑥) = ∑𝑎𝑖𝑥𝑖 and 𝑏(𝑥) = ∑𝑏𝑖𝑥𝑖
Polynomial addition
◦ 𝑎 𝑥 + 𝑏 𝑥 = ∑ (𝑎𝑖 + 𝑏𝑖)𝑥𝑖
Ex. 𝑎(𝑥) = 𝑥5 + 4𝑥3 + 2𝑥2 + 1 (degree = 5)
𝑏(𝑥) = 3𝑥6 + 4𝑥3 + 𝑥 (degree = 6)
𝑎(𝑥) + 𝑏(𝑥) = 3𝑥6 + 𝑥5 + 8𝑥3 + 2𝑥2 + 𝑥 +
1 (degree = 6)
Polynomial multiplication
◦ 𝑎(𝑥) ∙ 𝑏(𝑥) = ∑ (𝑎𝑖𝑥𝑖 ∙ ∑ (𝑏𝑗𝑥𝑗))
8
Polynomial : ADT
class Polynomial {
public:
// Construct p(x) = 0
Polynomial(void); We will ignore destructor in the codes
// Destructor hereafter. It is programmer’s responsibility
~Polynomial(void); to treat her memory well ☺
// Return the sum of *this and poly
Polynomial Add(Polynomial poly);
// Return multiplication of *this and poly
Polynomial Mult(Polynomial poly);
// Return the evaluation result
float Eval(float x );
private:
// Array representation
…
};
9
用 sequential mapping (Array) 方式
10
Polynomial: 2nd Representation
// in class Polynomial
class Term {
private:
friend Polynomial;
// array of nonzero terms
float coef;
Term* termArray;
int exp;
int capacity; // size of termArray
}; ai * (x^i) int terms; // number of nonzero terms
𝑎 𝑥 = 𝑥 5 + 9𝑥 4 + 7𝑥 3 + 2𝑥
𝑏 𝑥 = 𝑥 6 + 3𝑥 5 + 6𝑥 + 3
𝑐 𝑥 = 𝑥 6 + (1 + 3)𝑥 5 + 9𝑥 4 + 7𝑥 3 + (2 + 6)𝑥 + 3
= 𝑥 6 + 4𝑥 5 + 9𝑥 4 + 7𝑥 3 + 8𝑥 + 3
13
Time Complexity of Analysis
Inside the while loop: every statement takes O(1)
time
How many times the “while loop” is executed in
the worst case ?
◦ Let a(x) have m terms, and b(x) have n terms.
◦ In each iteration, we access next element in a(x) or
b(x), or both.
◦ Worst case: m + n.
e.g. It happens when
A(x) = 7x5 + x3 + x; B(x) = x6 + 2x4 + 6x2 +3
Access remaining terms in A(x): O(m)
Access remaining terms in B(x): O(n)
Hence, total run time = O(m + n)
14
2.4
Matrix
-27 3 4 row 0
𝐴5⋆3 = 6 82 -2 row 1
109 -64 11 row 2
12 8 9 row 3
48 27 47 row 4
16
Matrix Operations
Transpose
◦ Cnxm = ATmxn
◦ 𝑐 𝑖 𝑗 = 𝑎[𝑗][𝑖]
Addition
◦ Cmxn = Amxn + Bmxn
◦ 𝑐 𝑖 𝑗 = 𝑎 𝑖 𝑗 + 𝑏[𝑖][𝑗]
Multiplication
◦ Cmxp = Amxn + Bnxp
◦ 𝑐 𝑖 𝑗 = ∑𝑛−1
𝑘=0 𝑎 𝑖 𝑘 × 𝑏 𝑘 [𝑗]
17
Matrix: ADT
class Matrix{
public:
// Construct
Matrix(int r, int c);
// Return the transpose of (*this) matrix
Matrix Transpose(void);
// Return sum of *this and b
Matrix Add(Matrix b);
// Return the multiplication of *this and b
Matrix Multiply(Matrix b);
private:
// Array representation
int **a, rows, cols;
}; two dimensional array
18
Transpose : Code
Matrix Matrix::Transpose(void){
Matrix c(cols, rows);
for (i=0; i<rows; i++) // O(rows)
for (j=0; j<cols; j++) // O(cols)
c[j][i]=a[i][j];
return c;
}
19
Add: Code
Matrix Matrix::Add(Matrix b){
Matrix c(rows, cols);
for (i=0; i<rows; i++) // O(rows)
for (j=0; j<cols; j++) // O(cols)
c[i][j]=a[i][j]+b[i][j];
return c;
}
20
Multiply: Code
Matrix Matrix::Multiply(Matrix b){
Matrix c(rows, b.cols);
for (i=0; i<rows; i++) { // O(rows)
for (j=0; j<b.cols; j++) { // O(b.cols)
sum=0;
for (k=0; k<cols; k++) // O(cols)
sum += a[i][k]*b[k][j];
c[i][j]=sum;
}
}
return c; x
} mxp = mxn nxp
21
2.4.2 Sparse Matrix
15 0 0 22 0 -15
0 11 3 0 0 0
a[6][6] = 0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0
00304 list =
00570 row 1 1 2 2 4 4
00000 column 3 5 3 4 2 3
02600 value 3 4 5 7 2 6
One Linear List Per Row
(col, value)
00304 row1
row1==[(3,
[(3,3),
3),(5,4)]
(5,4)]
00570 row2
row2==[(3,5),
[(3,5),(4,7)]
(4,7)]
00000 row3
row3==[][]
02600 row4
row4==[(2,2),
[(2,2),(3,6)]
(3,6)]
Sparse Matrix Representation
We use an array, smArray[], of triple
<row, col, value> to store those nonzero
elements.
1 row -> 2 row -> 3 row ...
Triples are stored in a row-major order.
row col value
smArray[0] 0 0 15
15 0 0 22 0 -15
0 11 3 0 0 0 smArray[1] 0 3 22
2D array
◦ 5000 x 5000 x 4 = 100 million bytes
Class SparseMatrix
◦ 100 x 4 x 3 + 4 = 1204 bytes
2.4.3 Trivial Transpose
• 𝑐 𝑖 𝑗 =𝑎 𝑗 𝑖
row col value row col value
smArray[0] 0 0 15 smArray[0] 0 0 15
smArray[1] 0 3 22 smArray[1] 3 0 22
smArray[2] 0 5 -15 smArray[2] 5 0 -15
Transpose
smArray[3] 1 1 11 smArray[3] 1 1 11
smArray[4] 1 2 3 smArray[4] 2 1 3
smArray[5] 2 3 -6 smArray[5] 3 2 -6
smArray[6] 4 0 91 smArray[6] 0 4 91
smArray[7] 5 2 28 smArray[7] 2 5 28
28
Smart Transpose
Because the row and column are swapped, we
trace the nonzero terms in a column-major
order. For(all non-zero elements in column j)
Store a(i,j,value) as aT(j,i,value)
It can be faster!
return b;
}
30
Fast Transpose
Examine all terms only twice!
Use additional space to store
◦ rowSize[i]: # of nonzero terms in ith row of AT
◦ rowStart[i]: location of nonzero term in ith
row of AT
◦ For i>0, rowStart[i]=rowStart[i-1]+rowSize[i-
1]
Copy element from A to AT one by one.
Time complexity: O(terms + cols)! linear time
31
Fast Transpose
each column of A
◦ Count the # of nonzero terms in each row of AT
◦ Calculate the location of 1st nonzero term ith row of AT
A row col value col rowSize rowStart
smArray[0] 0 0 15 [0] 2 0
smArray[1] 0 3 22 [1] 1 2
smArray[2] 0 5 -15 [2] 2 3
smArray[3] 1 1 11 [3] 2 5
smArray[4] 1 2 3 [4] 0 7
smArray[5] 2 3 -6 [5] 1 7
smArray[6] 4 0 91
smArray[7] 5 2 28
32
Fast Transpose
Copy element from A to AT one by one
33
Fast Transpose
Copy element from A to AT one by one
34
Fast Transpose
Copy element from A to AT one by one
35
Fast Transpose
Copy element from A to AT one by one
36
Fast Transpose
Copy element from A to AT one by one
37
Fast Transpose
Copy element from A to AT one by one
38
Fast Transpose
Copy element from A to AT one by one
39
Fast Transpose: Codes
SparseMatrix SparseMatrix::FastTranspose( )
{ // Compute the transpose in O(terms + cols) time
SparseMatrix b(cols, rows, terms);
if (terms > 0) {
int *rowSize = new int[cols];
int *rowStart = new int[cols];
// compute rowSize[i]=number of terms in row i of b
fill(rowSize, rowSize+cols, 0);
for(int i=0; i<terms; i++) rowSize[smArray[i].col]++;
41
2.4.4 Sparse Matrix Multiplication
Compute the transpose of b
c: m x p a: m x n b: n x p
0 5 2 0 0 7 3
x 0
= 4
3
6
5
42
Sparse Matrix Multiplication
Use approach similar to “Polynomial
Addition” to compute the X!
c: m x p a: m x n bT: p x n
0 5 2 0 0 7 3 0 4 3 6 5
x
=
p p p q q q q q
• Complexity:
– O(rows ∙ b.cols ∙ (Terms[i] + b.Terms[j]))
– rows ∙ Terms[i] = a.terms and
b.cols ∙ b.Terms[j] = b.terms
– O(rows ∙ b.terms + b.cols ∙ a.terms) ???
non-zero non-zero
terms terms
44
2.6
The String
Abstract Data
Type
Pattern
p
String
s-p
O(s*p) p * (s - p) = s * p
j 0 1 2 3 4 5 6 7 8 9
pat a b c a b c a c a b
f -1 -1 -1 0 1 2 3 -1 0 1
largest 𝑘 < 𝑗, s. t. 𝑝0 … 𝑝𝑘 = 𝑝𝑗−𝑘 … 𝑝𝑗 , ∃𝑘 ≥ 0
𝑓=ቊ
−1 , 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
If a partial match is found such that
𝑠𝑖−𝑗 … 𝑠𝑖−1 = 𝑝0 … 𝑝𝑗−1 and 𝑠𝑖 ≠ 𝑝𝑗 then
matching may resume by comparing 𝑠𝑖
and 𝑝𝑓 𝑗−1 +1
2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 47
Observation
string
…. a b c a b c a x ….
a b c a b c a c a b
pattern