Bird
0
0
DSA Cprogramming~10 mins

KMP Pattern Matching Algorithm in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - KMP Pattern Matching Algorithm
Build LPS array for pattern
Start matching text and pattern
Compare characters
Move both i,j
If j == pattern length
Pattern found at i-j
Continue until text end
First build the LPS array for the pattern, then scan the text comparing characters. On mismatch, use LPS to avoid rechecking matched characters.
Execution Sample
DSA C
void KMPSearch(char* pat, char* txt) {
  int M = strlen(pat);
  int N = strlen(txt);
  int lps[M];
  computeLPSArray(pat, M, lps);
  int i = 0, j = 0;
  while (i < N) {
    if (pat[j] == txt[i]) { i++; j++; }
    if (j == M) { printf("Found at %d\n", i-j); j = lps[j-1]; }
    else if (i < N && pat[j] != txt[i]) {
      if (j != 0) j = lps[j-1]; else i++;
    }
  }
}
This code searches for pattern occurrences in text using the KMP algorithm with an LPS array to skip unnecessary comparisons.
Execution Table
StepOperationi (text index)j (pattern index)LPS UsedActionMatch FoundVisual State
1Build LPS array---Compute LPS for pattern 'ABABCABAB' in text 'ABABDABACDABABCABAB'-LPS = [0,0,1,2,0,1,2,3,4]
2Start matching00-Compare txt[0]='A' and pat[0]='A'YesText: A B A B D A B A C D A B A B C A B A B Pattern: A B A B C A B A B
3Match11-Characters match, increment i and jNoi=1, j=1
4Match22-Characters match, increment i and jNoi=2, j=2
5Match33-Characters match, increment i and jNoi=3, j=3
6Match44-Characters match, increment i and jNoi=4, j=4
7Mismatch44lps[3]=2Mismatch at txt[4]='D' vs pat[4]='C', use LPS to shift j=2Noi=4, j=2
8Mismatch42lps[1]=0Mismatch at txt[4]='D' vs pat[2]='A', use LPS to shift j=0Noi=4, j=0
9Mismatch40-j=0 and mismatch, increment iNoi=5, j=0
10Match50-Characters match, increment i and jNoi=6, j=1
11Match61-Characters match, increment i and jNoi=7, j=2
12Match72-Characters match, increment i and jNoi=8, j=3
13Mismatch83lps[2]=1Mismatch at txt[8]='C' vs pat[3]='B', use LPS to shift j=1Noi=8, j=1
14Mismatch81lps[0]=0Mismatch at txt[8]='C' vs pat[1]='B', use LPS to shift j=0Noi=8, j=0
15Mismatch80-j=0 and mismatch, increment iNoi=9, j=0
16Mismatch90-j=0 and mismatch txt[9]='D' vs pat[0]='A', increment iNoi=10, j=0
17Pattern found199-Characters match till j == 9 at i=19, pattern found at 19-9=10YesPattern found at index 10
18Shift pattern199lps[8]=4Set j = lps[8]=4 to continue searchingNoi=19, j=4
19End194-i reached text length, stopNoSearch complete
💡 i reached text length 19, no more characters to match
Variable Tracker
VariableStartAfter Step 6After Step 9After Step 12After Step 16After Step 17Final
i (text index)0458101919
j (pattern index)0403094
LPS arrayN/AComputedComputedComputedComputedComputedComputed
Key Moments - 3 Insights
Why do we use the LPS array when a mismatch happens instead of moving i forward?
Because the LPS array tells us the longest prefix that is also a suffix, so we can shift the pattern without rechecking characters, as shown in step 7 where j moves from 4 to 2 without incrementing i.
Why do we sometimes move j back but not i when a mismatch occurs?
Because i points to the text and we only move it forward when we have no partial match to reuse. When mismatch happens, j moves back using LPS to reuse previous matches, as in step 7 and 13.
What does it mean when j equals the pattern length?
It means the entire pattern matched in the text ending at index i-1. We found a pattern occurrence at i-j, as shown in step 17.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 8. What happens to the indices i and j?
Ai increments, j resets to 0 using LPS
Bi stays the same, j resets to 0 using LPS
CBoth i and j increment
DBoth i and j reset to 0
💡 Hint
Check the 'Action' and 'i (text index)', 'j (pattern index)' columns at step 8.
At which step is the pattern found in the text according to the execution table?
AStep 13
BStep 16
CStep 17
DStep 19
💡 Hint
Look for the row where 'Match Found' is 'Yes' and 'Action' mentions pattern found.
If the LPS array was all zeros, how would the matching process change?
AThe pattern would shift by one on mismatch, no reuse of previous matches
BThe pattern would shift by the full length on mismatch
CThe pattern would never shift and cause infinite loop
DThe pattern would match instantly
💡 Hint
Refer to the 'LPS Used' column and how it affects shifting in the execution table.
Concept Snapshot
KMP Pattern Matching Algorithm:
- Precompute LPS (Longest Prefix Suffix) array for pattern
- Use LPS to skip unnecessary comparisons on mismatch
- Compare text and pattern characters with indices i, j
- On match: increment both i and j
- On mismatch: if j != 0, set j = lps[j-1], else increment i
- When j == pattern length, pattern found at i-j
- Efficiently finds all occurrences in O(N) time
Full Transcript
The KMP Pattern Matching Algorithm first builds an LPS array for the pattern, which stores the longest prefix that is also a suffix for each prefix of the pattern. Then it scans the text with two indices: i for text and j for pattern. When characters match, both indices move forward. On mismatch, instead of moving i forward blindly, the algorithm uses the LPS array to shift the pattern index j to reuse previous matches, avoiding rechecking characters. When j reaches the pattern length, it means the pattern is found in the text at position i-j. This process continues until the entire text is scanned. The LPS array is key to the algorithm's efficiency, allowing it to run in linear time relative to the text length.