LINEAR PATTERN SEARCH
Algorithm - 3
By
Dept. of CSE
PVPSIT, Kanuru.
PRASAD V. POTLURI SIDDHARTHA INSTITUTE OF TECHNOLOGY
B. Vinay Kumar
Friday, March 18, 2022
Algorithm 3
LINEAR PATTERN SEARCH
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
PVPSIT (Autonomous)
Problem Solving Techniques
Calculating LPS table
The Knuth Morris Pratt Algorithm = KMP algorithm
LPS - The longest prefix of the substring which is also a suffix.
PVPSIT (Autonomous)
Problem Solving Techniques
1. Pre-processing: The LPS Table
Before searching, we create the LPS (Longest Prefix Suffix) table.
This table tells the algorithm the "recovery position"—how many characters to skip when a mismatch occurs.
Pattern (P): abcabd
Index | 0 | 1 | 2 | 3 | 4 | 5 |
Char | a | b | c | a | b | d |
LPS Value | 0 | 0 | 0 | 1 | 2 | 0 |
PVPSIT (Autonomous)
Problem Solving Techniques
Linear Search Pattern Algorithm
PVPSIT (Autonomous)
Problem Solving Techniques
Step | i (String) | j (Pattern) | Comparison | Action |
1 | 0-4 | 0-4 | abcab matches abcab | Matches! Increment both i and j |
2 | 5 (c) | 5 (d) | Mismatch! | Look at LPS[j-1] (LPS of index 4). The value is 2. So, j=2 |
3 | 5 (c) | 2 (c) | Match! | Instead of restarting at i=1, we keep i=5 and set j=2. Since S[5] and P[2] match, increment both. |
4 | 6-8 | 3-5 | abd | Continue matching until the end of the pattern. |
2. The Search (Dry Run) String (S): abcabcabcabd Pattern (P): abcabd
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
Character | a | b | c | a | b | c | a | b | c | a | b | d |
Index (j) | 0 | 1 | 2 | 3 | 4 | 5 |
Pattern | a | b | c | a | b | d |
LPS Value | 0 | 0 | 0 | 1 | 2 | 0 |
PVPSIT (Autonomous)
Problem Solving Techniques
Why this is "Linear"
In a naive search, if you fail at the 6th character, you might have to go back and start again from the 2nd character.
In Linear Search Pattern, the string pointer (i) never moves backward. It only ever stays still or moves forward, which is why the time complexity is O(n).
PVPSIT (Autonomous)
Problem Solving Techniques
B. Vinay Kumar
Friday, March 18, 2022
Applications
PVPSIT (Autonomous)
Problem Solving Techniques