0% found this document useful (0 votes)
11 views

Arrays

NTHU CS

Uploaded by

辣台妹
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Arrays

NTHU CS

Uploaded by

辣台妹
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

EECS 204002

Data Structures 資料結構


Prof. REN-SONG TSAY 蔡仁松 教授
NTHU

CH. 2
ARRAYS

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 1


2.2

Array Abstract
Data Type

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 2


2.2 Definition of Array
 A data structure that represents an
ordered or linear list.
 Elements in an array could be the same or
different data types.
◦ Days of the week:
 {Sunday, Monday, …, Saturday}
◦ Deck of cards:
 {Ace, 2, 3, …, King}
◦ Phone Book:
 {(James, #1), (Claire, #2), …, (Tony, #n)}
3
Common Array Operations
 ADT array[n]={a0, a1 ,…, an-1}
1. Find the length, n, of the array.
2. Read the array from left to right (or reverse).
3. Retrieve the ith element, 0 ≤ i < n.
4. Store a new element into ith position , 0 ≤ i < n.
5. Insert/delete the element at position i , 0 ≤ i < n.
 It is not necessary to include all operations
 Different representations carry out different
subset of operations efficiently.

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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 6


2.3 Polynomial
 𝑝 𝑥 = 𝑎0 𝑥 𝑒0 + 𝑎1 𝑥 𝑒1 + ⋯ + 𝑎𝑛 𝑥 𝑒𝑛 = ∑𝑎𝑖 𝑥 𝑒𝑖
 Each 𝑎𝑖 𝑥 𝑒𝑖 is called a term with coefficient 𝑎𝑖
◦ The degree of p(x) is the largest exponent from
among the non-zero terms.
◦ Ex. 𝑝(𝑥) = 𝑥5 + 4𝑥3 + 2𝑥2 + 1
Has 4 terms with coefficients 1, 4 ,2 and 1.
The degree of p(x) is 5
 Array representation
◦ Store (𝑎𝑖 , 𝑒𝑖 ) as (array[n-i], i) pair and n is the degree
x5 4x3 2x2 x0

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) 方式

Polynomial: 1st Representation


// in class Polynomial
public: // for convenience… Usage:
// degree ≤ MaxDegree Polynomial a;
int degree; a.degree = n;
// coefficient array a.coef[i] = an-i
float coef[MaxDegree+1];

 Coefficients are stored in order of


decreasing exponents
 Advantages:
◦ Simple algorithm of operations
 Disadvantages:
◦ Waste memory in a sparse polynomial

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

 Store only nonzero terms.


 Each nonzero term holds an exponent and
its corresponding coefficient.
 If polynomial is sparse, 2nd representation is
better. If polynomial is full, 2nd one has
double size of 1st.
因為要多記 exponent
11
根據 2nd representation 的方式

2.3.2 Polynomial Addition: Code


Polynomial Polynomial::Add(Polynomial b)
{ // Return sum of polynomial *this and b
Polynomial c;
int aPos = 0, bPos = 0; 從高指數項比到低指數項
while((aPos < terms) && (bPos < b.terms))
if(termArray[aPos].exp == b.termArray[bPos].exp){
float t = termArray[aPos].coef + b.termArray[bPos].coef;
If(t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++;
}
else if(termArray[aPos].exp < b.termArray[bPos].exp){
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
bPos++;
}
else{
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
// add in remaining terms of *this
for(; aPos < terms; aPos++)
c.NewTerm(termArray[aPos].coef, termArray[aPos].exp);
// add in remaining terms of b
for(; bPos < b.terms; bPos++)
c.NewTerm(b.termArray[bPos].coef, b.termArray[bPos].exp);
return c; 12
}
Example

𝑎 𝑥 = 𝑥 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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 15


2.4 Matrix
 Denote a matrix consists of m rows and
n columns as 𝐴𝑚⋆𝑛 (read A is a m by n
matrix).
 Usually stored as a two-dimensional array,
𝒂[𝒎][𝒏], in which element at ith row and
jth column is accessed by 𝒂[𝒊][𝒋].
col 0 col 1 col 2

-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;
}

 Time complexity: O(rows ∙ cols)


後面的 smart transpose 只要 O(cols * terms)

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;
}

 Time complexity: O(rows ∙ cols)

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

 Time complexity: O(rows ∙ cols ∙ b.cols)


A A B

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

 A matrix has few non-zero elements.


 2D array representation is inefficient.
◦ Wasteful memory and computing time
◦ Consider a matrix A5000X5000 with only 100
nonzero elements!
22
Single Linear List Example

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

a[6][6] = 0 0 0 -6 0 0 smArray[2] 0 5 -15


0 0 0 0 0 0 smArray[3] 1 1 11
91 0 0 0 0 0 smArray[4] 1 2 3
0 0 28 0 0 0
smArray[5] 2 3 -6
smArray[6] 4 0 91
smArray[7] 5 2 28 25
2.4.2
ADT2.4
Sparse Matrix: ADT
class SparseMatrix{
public:
// Construct, t is the capacity of nonzero terms
SparseMatrix(int r, int c, int t);
// Return the transpose of (*this) matrix
SparseMatrix Transpose(void);
// Return sum of *this and b
SparseMatrix Add(SparseMatrix b);
// Return the multiplication of *this and b
SparseMatrix Multiply(SparseMatrix b);
private:
// Sparse representation
int rows, cols, terms, capacity;
MatrixTerm *smArray;
class MatrixTerm {
}; friend SparseMatrix;
int row, col, value;
};
26
Approximate Memory
Requirements
 5000 x 5000 matrix with 100 nonzero
elements, 4 bytes per element

 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

• Problem: the nonzero terms in AT are no longer


stored in row major order!

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)

row col value row col value


smArray[0] 0 0 15 smArray[0] 0 0 15
smArray[1] 0 3 22 smArray[1] 0 4 91
smArray[2] 0 5 -15 smArray[2]
smArray[3] 1 1 11 smArray[3]
smArray[4] 1 2 3 smArray[4]
smArray[5] 2 3 -6 smArray[5]
smArray[6] 4 0 91 smArray[6]
smArray[7] 5 2 28 smArray[7]
29
Smart Transpose: Code
SparseMatrix SparseMatrix::Transpose()
{ // Return the transpose of (*this) matrix
// b.smArray has the same number of nonzero terms
SparseMatrix b(cols, rows, terms);
if (terms > 0) // has nonzero terms
{
int currentB = 0;
for(int c=0; c<cols; c++) // O(cols)
for(int i=0; i<terms; i++) // O(terms)
if(smArray[i].col == c)
{
b.smArray[currentB].row = c;
b.smArray[currentB].col = smArray[i].row;
b.smArray[currentB++].value = smArray[i].value;
}Time complexity: O(cols ∙ terms).
}

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

A row col value col rowSize rowStart AT row col value


smArray[0] 0 0 15 [0] 2 0 smArray[0] 0 0 15
smArray[1] 0 3 22 [1] 1 2 smArray[1]
smArray[2] 0 5 -15 [2] 2 3 smArray[2]
smArray[3] 1 1 11 [3] 2 5 smArray[3]
smArray[4] 1 2 3 [4] 0 7 smArray[4]
smArray[5] 2 3 -6 [5] 1 7 smArray[5]
smArray[6] 4 0 91 smArray[6]
smArray[7] 5 2 28 smArray[7]

33
Fast Transpose
 Copy element from A to AT one by one

A row col value col rowSize rowStart AT row col value


smArray[0] 0 0 15 [0] 2 1 smArray[0] 0 0 15
smArray[1] 0 3 22 [1] 1 2 smArray[1]
smArray[2] 0 5 -15 [2] 2 3 smArray[2]
smArray[3] 1 1 11 [3] 2 5 smArray[3]
smArray[4] 1 2 3 [4] 0 7 smArray[4]
smArray[5] 2 3 -6 [5] 1 7 smArray[5]
smArray[6] 4 0 91 smArray[6]
smArray[7] 5 2 28 smArray[7]

34
Fast Transpose
 Copy element from A to AT one by one

A row col value col rowSize rowStart AT row col value


smArray[0] 0 0 15 [0] 2 1 smArray[0] 0 0 15
smArray[1] 0 3 22 [1] 1 2 smArray[1]
smArray[2] 0 5 -15 [2] 2 3 smArray[2]
smArray[3] 1 1 11 [3] 2 5 smArray[3]
smArray[4] 1 2 3 [4] 0 7 smArray[4]
smArray[5] 2 3 -6 [5] 1 7 smArray[5]
smArray[6] 4 0 91 smArray[6]
smArray[7] 5 2 28 smArray[7]

35
Fast Transpose
 Copy element from A to AT one by one

A row col value col rowSize rowStart AT row col value


smArray[0] 0 0 15 [0] 2 1 smArray[0] 0 0 15
smArray[1] 0 3 22 [1] 1 2 smArray[1]
smArray[2] 0 5 -15 [2] 2 3 smArray[2]
smArray[3] 1 1 11 [3] 2 6 smArray[3]
smArray[4] 1 2 3 [4] 0 7 smArray[4]
smArray[5] 2 3 -6 [5] 1 7 smArray[5] 3 0 22
smArray[6] 4 0 91 smArray[6]
smArray[7] 5 2 28 smArray[7]

36
Fast Transpose
 Copy element from A to AT one by one

A row col value col rowSize rowStart AT row col value


smArray[0] 0 0 15 [0] 2 1 smArray[0] 0 0 15
smArray[1] 0 3 22 [1] 1 3 smArray[1] 0 4 91
smArray[2] 0 5 -15 [2] 2 4 smArray[2] 1 1 11
smArray[3] 1 1 11 [3] 2 7 smArray[3] 2 1 3
smArray[4] 1 2 3 [4] 0 7 smArray[4]
smArray[5] 2 3 -6 [5] 1 8 smArray[5] 3 0 22
smArray[6] 4 0 91 smArray[6] 3 2 -6
smArray[7] 5 2 28 smArray[7] 5 0 -15

37
Fast Transpose
 Copy element from A to AT one by one

A row col value col rowSize rowStart AT row col value


smArray[0] 0 0 15 [0] 2 2 smArray[0] 0 0 15
smArray[1] 0 3 22 [1] 1 3 smArray[1] 0 4 91
smArray[2] 0 5 -15 [2] 2 4 smArray[2] 1 1 11
smArray[3] 1 1 11 [3] 2 7 smArray[3] 2 1 3
smArray[4] 1 2 3 [4] 0 7 smArray[4]
smArray[5] 2 3 -6 [5] 1 8 smArray[5] 3 0 22
smArray[6] 4 0 91 smArray[6] 3 2 -6
smArray[7] 5 2 28 smArray[7] 5 0 -15

38
Fast Transpose
 Copy element from A to AT one by one

A row col value col rowSize rowStart AT row col value


smArray[0] 0 0 15 [0] 2 2 smArray[0] 0 0 15
smArray[1] 0 3 22 [1] 1 3 smArray[1] 0 4 91
smArray[2] 0 5 -15 [2] 2 4 smArray[2] 1 1 11
smArray[3] 1 1 11 [3] 2 7 smArray[3] 2 1 3
smArray[4] 1 2 3 [4] 0 7 smArray[4] 2 5 28
smArray[5] 2 3 -6 [5] 1 8 smArray[5] 3 0 22
smArray[6] 4 0 91 smArray[6] 3 2 -6
smArray[7] 5 2 28 smArray[7] 5 0 -15

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]++;

// rowStart[i] = starting position of row i in b


rowStart[0] = 0;
for(int i=1; i<cols; i++)
rowStart[i]=rowStart[i-1]+rowSize[i-1];

for(int i=0; i<terms; i++)


{ // copy terms from *this to b
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++; // Increase the start pos by 1
}
delete [] rowSize;
delete [] rowStart;
}
return b; 40
Computation Time Comparison
Trivial Transpose Smart Transpose Fast Transpose
𝑶(𝒓𝒐𝒘𝒔 ∙ 𝒄𝒐𝒍𝒔) 𝑶(𝒄𝒐𝒍𝒔 ∙ 𝒕𝒆𝒓𝒎𝒔) 𝑶(𝒕𝒆𝒓𝒎𝒔 + 𝒄𝒐𝒍𝒔)

 For a dense matrix ( terms = 𝑟𝑜𝑤𝑠 ∙ 𝑐𝑜𝑙𝑠 )


Fast equals to Trivial: 𝑂 𝑟𝑜𝑤𝑠 ∙ 𝑐𝑜𝑙𝑠
Smart is the slowest: 𝑂(𝑟𝑜𝑤𝑠 ∙ 𝑐𝑜𝑙𝑠 2 )
 For a sparse matrix (terms ≪ 𝑟𝑜𝑤𝑠 ∙
𝑐𝑜𝑙𝑠)
◦ Fast is faster than Trivial and Smart ones

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

ref code in the textbook


x =(2)(4) + (7)(5)= 43
43
Time Complexity
SparseMatrix SparseMatrix::Multiply(SparseMatrix b)
{ // Compute the transpose of b
SparseMatrix bT = b.FastTranspose(); // O(b.terms+b.cols)

for ith row in smArray // O(rows)


for jth row in bT.smArray // O(b.cols)
Perform “Polynomal Addition” // O(Terms[i]+b.Terms[j])
}

• 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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 45


2.6.1 Simple String Pattern Matching
 s= string.length();
 p= pattern.length();

Pattern
p

String
s-p

 O(s*p) p * (s - p) = s * p

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 46


The Knuth-Morris-Pratt Alg.
 Complexity: O(p+s) linear time

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

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 48


Pattern Matching
string
…. a b c a b c a x ….
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
pattern

2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 49


2.6.2 Pattern-matching with a
P2.16 Failure Function
int String::FastFind(String pat) {
// Determine if pat is a substring of s
int PosP = 0, PosS = 0; // j=> PosP, i=> PosS
int LengthP = pat.Length(), LengthS = Length();

while((PosP < LengthP) && (PosS < LengthS))


{
if (pat.str[PosP] == str[PosS]) {
PosP++; PosS++;
} else
if (PosP == 0) PosS++;
else PosP = pat.f[PosP-1] + 1;
}
if (PosP < LengthP) return -1;
else return PosS-LengthP;
}
2021/9/24 © Ren-Song Tsay, NTHU, Taiwan 50

You might also like