Substring search Flashcards
What is the brute force approach for SS search?
Keep main pointer i, and pattern pointer j, for each i increment j until finding mismatch or reaching end of pattern.
What is the alternative brute force approach?
If main pointer i matches j, increment j. If they do not, send i back amount of mismatches indices given by j and reset j to 0.
If fonmd, return main i - pattern length
What is the worst case for brute force search?
Requires NM compares when both pattern and text are all A’s followed by a B.
For each N-M+1 possible match, all characters are cecked against text.
How do you search manually with KMP?
If mismatch, find prefix that is also suffix and allign with next occurance in text.
What are the steps for the RKF?
- Compute hash value for pattern
- Compute tvalue of R^(M-1)mod Q
- Use hashSearch():
* Compuite hash function for first M chars and compare against pattern hash
* If not match, proceed through text and compute
How does the RKM work?
Hash = tiR^(m-1) +…+ t(i+m-1)R^0
For the next position, we can calculate this by subtracting the contribution of the first character, times it with R and add the contribution of the next. We then mod this with Q. This computation is done in constant time.