Knuth–Morris–Pratt Algorithm (KMP)

KMP - Algorithm

algorithm kmp_search:
    input:
        an array of characters, S (the text to be searched)
        an array of characters, W (the word sought)
    output:
        an array of integers, P (positions in S at which W is found)
        an integer, nP (number of positions)

    define variables:
        an integer, j ← 0 (the position of the current character in S)
        an integer, k ← 0 (the position of the current character in W)
        an array of integers, T (the table, computed elsewhere)

    let nP ← 0

    while j < length(S) do
        if W[k] = S[j] then
            let j ← j + 1
            let k ← k + 1
            if k = length(W) then
                (occurrence found, if only first occurrence is needed, m ← j - k  may be returned here)
                let P[nP] ← j - k, nP ← nP + 1
                let k ← T[k] (T[length(W)] can't be -1)
        else
            let k ← T[k]
            if k < 0 then
                let j ← j + 1
                let k ← k + 1

Partial Match Table

Therefore, we compile the following table:

i

0

1

2

3

4

5

6

7

W[i]

A

B

C

D

A

B

D

T[i]

-1

0

0

0

-1

0

2

0

Another example:

i

0

1

2

3

4

5

6

7

8

9

W[i]

A

B

A

C

A

B

A

B

C

T[i]

-1

0

-1

1

-1

0

-1

3

2

0

Another example (slightly changed from the previous example):

i

0

1

2

3

4

5

6

7

8

9

W[i]

A

B

A

C

A

B

A

B

A

T[i]

-1

0

-1

1

-1

0

-1

3

-1

3

Another more complicated example:

i

00

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

W[i]

P

A

R

T

I

C

I

P

A

T

E

I

N

P

A

R

A

C

H

U

T

E

T[i]

-1

0

0

0

0

0

0

-1

0

2

0

0

0

0

0

-1

0

0

3

0

0

0

0

0

0