CSE 205 Lab Manual 12 KMP
CSE 205 Lab Manual 12 KMP
Algorithms Lab
CSE 206
2 Problem analysis
Given a text txt[0...n-1] and a pattern pat[0...m-1], a function search(char pat[], char txt[]) prints all
occurrences of pat[] in txt[]. It is always true that n > m. Example input outputs of pattern matching
algorithm are given below:
We now simulate an example of the construction of lps[] or prefix function pre-processing with a given
pattern below.
1. pat[] = AAACAAAA
2. len = 0, i = 0.
lps[0] is always 0, we move to i = 1
3. len = 0, i = 1.
Since pat[len] and pat[i] match, do len++, store it in lps[i] and do i++. len = 1, lps[1] = 1, i = 2
4. len = 1, i = 2.
Since pat[len] and pat[i] match, do len++, store it in lps[i] and do i++. len = 2, lps[2] = 2, i = 3
5. len = 2, i = 3.
Since pat[len] and pat[i] do not match, and len > 0, set len = lps[len-1] = lps[1] = 1
6. len = 1, i = 3.
Since pat[len] and pat[i] do not match and len > 0, len = lps[len-1] = lps[0] = 0
7. len = 0, i = 3.
Since pat[len] and pat[i] do not match and len = 0, Set lps[3] = 0 and i = 4.
8. len = 0, i = 4.
Since pat[len] and pat[i] match, do len++, store it in lps[i] and do i++. len = 1, lps[4] = 1, i = 5
9. len = 1, i = 5.
Since pat[len] and pat[i] match, do len++, store it in lps[i] and do i++. len = 2, lps[5] = 2, i = 6
10. len = 2, i = 6.
Since pat[len] and pat[i] match, do len++, store it in lps[i] and do i++. len = 3, lps[6] = 3, i = 7
11. len = 3, i = 7.
Since pat[len] and pat[i] do not match and len > 0, set len = lps[len-1] = lps[2] = 2
12. len = 2, i = 7.
Since pat[len] and pat[i] match, do len++, store it in lps[i] and do i++. len = 3, lps[7] = 3, i = 8
13. We stop here as we have constructed the whole lps[]. so the final lps[] = [0, 1, 2, 0, 1, 2, 3, 3]
4 Implementation in Java
1 // JAVA program for implementation of KMP pattern
2 // searching algorithm
3
4 class KMP_String_Matching {
5 void KMPSearch(String pat, String txt)
6 {
7 int M = pat.length();
8 int N = txt.length();
9
10 // create lps[] that will hold the longest
11 // prefix suffix values for pattern
12 int lps[] = new int[M];
13 int j = 0; // index for pat[]
14
15 // Preprocess the pattern (calculate lps[]
16 // array)
17 computeLPSArray(pat, M, lps);
18
19 int i = 0; // index for txt[]
20 while (i < N) {
21 if (pat.charAt(j) == txt.charAt(i)) {
22 j++;
23 i++;
24 }
25 if (j == M) {
26 System.out.println("Found pattern "
27 + "at index " + (i − j));
28 j = lps[j − 1];
29 }
30
31 // mismatch after j matches
32 else if (i < N && pat.charAt(j) != txt.charAt(i)) {
33 // Do not match lps[0..lps[j−1]] characters,
34 // they will match anyway
35 if (j != 0)
36 j = lps[j − 1];
37 else
38 i = i + 1;
39 }
40 }
41 }
42
43 void computeLPSArray(String pat, int M, int lps[])
44 {
45 // length of the previous longest prefix suffix
46 int len = 0;
47 int i = 1;
48 lps[0] = 0; // lps[0] is always 0
49
50 // the loop calculates lps[i] for i = 1 to M−1
51 while (i < M) {
52 if (pat.charAt(i) == pat.charAt(len)) {
53 len++;
54 lps[i] = len;
55 i++;
56 }
57 else // (pat[i] != pat[len])
58 {
59 // This is tricky. Consider the example.
60 // AAACAAAA and i = 7. The idea is similar
61 // to search step.
62 if (len != 0) {
63 len = lps[len − 1];
64
65 // Also, note that we do not increment
66 // i here
67 }
68 else // if (len == 0)
69 {
70 lps[i] = len;
71 i++;
72 }
73 }
74 }
75 }
76
77 // Driver program to test above function
78 public static void main(String args[])
79 {
80 String txt = "ABABDABACDABABCABAB";
81 String pat = "ABABCABAB";
82 new KMP_String_Matching().KMPSearch(pat, txt);
83 }
84 }
7 Lab Task (Please implement yourself and show the output to the
instructor)
1. Write a Java implementation using KMP algorithm to find out the indexes where a given pattern gets
matched to a given array. Repeat the algorithm for multiple pairs of given strings and patterns.
9 Policy
Copying from internet, classmate, seniors, or from any other source is strongly prohibited. 100% marks will be
deducted if any such copying is detected.