DAA-DA-output
DAA-DA-output
#include <string.h>
#define d 256
int M = strlen(pattern);
int N = strlen(text);
int p = 0, t = 0, h = 1;
int i, j;
h = (h * d) % q;
p = (d * p + pattern[i]) % q;
t = (d * t + text[i]) % q;
if (p == t) {
if (text[i + j] != pattern[j])
break;
if (j == M)
if (i < N - M) {
if (t < 0) t += q; }}}
int main() {
return 0;
It calculates the hash value of the pattern and compares it with the hash values of substrings of
the text.
Variables are initialized to store the hash values of the pattern and the current substring of the
text.
This helps in efficiently removing the first character and adding a new character while
updating the rolling hash
The hash values of the pattern and the first window of text (of the same length as the pattern)
are computed.
The algorithm moves the pattern one character at a time over the text.
If the hash values match, a character-by-character comparison is performed to confirm the
match.
Instead of recalculating the hash from scratch for each new substring, the hash is updated using
the rolling hash formula:
If the updated hash value becomes negative, a small adjustment is made to convert it into a
positive value.
The Rabin-Karp function is called, and it finds all occurrences of "ABCD" in the text.
Worst case: O(NM)O(NM)O(NM) (when many hash collisions occur, leading to character-by-
character comparisons).
#include <string.h>
void computeLPSArray(char* pattern, int M, int* lps) {
int len = 0, i = 1;
lps[0] = 0;
while (i < M) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0)
else {
lps[i] = 0;
i++}}}}
int M = strlen(pattern);
int N = strlen(text);
int lps[M];
computeLPSArray(pattern, M, lps);
int i = 0, j = 0;
while (i < N) {
if (pattern[j] == text[i]) {
i++;
j++;
}
if (j == M) {
j = lps[j - 1];
if (j != 0)
j = lps[j - 1];
else
i++;}}}
int main() {
KMPSearch(pattern, text);
return 0;}
The LPS array helps determine how much the pattern should be shifted when a mismatch occurs.
It stores the length of the longest proper prefix which is also a suffix for each prefix of the
pattern.
If a mismatch happens, the LPS array tells where to resume the comparison instead of restarting
from the beginning.
Compare the current character with the last matched prefix character:
o If they don’t match, reduce the prefix length using previous LPS values until a match is
found or reset to zero.
Continue until the entire pattern is processed.
If a complete match occurs, print the index and use lps[j-1] to determine how much to shift.
If a mismatch occurs:
4. Time Complexity
Overall Complexity: O(N+M)O(N + M)O(N+M), making KMP much faster than naive string-
matching algorithms.
5. Key Advantage
Unlike brute force methods, KMP doesn’t backtrack in the text, making it highly efficient for
searching in large strings.
#include <string.h>
int i, lps = 0, x;
for (x = 0; x < NO_OF_CHARS; x++)
TF[0][x] = 0;
TF[0][pattern[0]] = 1;
TF[i][x] = TF[lps][x];
TF[i][pattern[i]] = i + 1;
if (i > 0)
lps = TF[lps][pattern[i]]; }
TF[M][x] = TF[lps][x];}
int M = strlen(pattern);
int N = strlen(text);
computeTF(pattern, M, TF);
int state = 0, i;
state = TF[state][text[i]];
if (state == M)
int main() {
search(text, pattern);
return 0;
}
The Transition Function (TF) table determines the next state based on the current state and the
incoming character.
It ensures that when a mismatch occurs, the algorithm jumps to the longest prefix which is also a
suffix of the matched portion.
1. Initialize the table: Set all transitions for state 0 to 0, except for the first character.
3. Final state transitions: Ensure that the last row contains proper shift values to continue pattern
matching efficiently.
3. Time Complexity
4. Key Advantages
Efficient searching: Uses precomputed shifts, avoiding unnecessary comparisons.
5. Example Execution
Text: "AABAACAADAABAABA"
Pattern: "AABA"
The algorithm quickly identifies matches at indices 0, 9, and 12 by efficiently shifting the pattern