Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m.

Examples:

Input:  txt[] = "THIS IS A TEST TEXT"
        pat[] = "TEST"
Output: Pattern found at index 10

Input:  txt[] =  "AABAACAADAABAABA"
        pat[] =  "AABA"
Output: Pattern found at index 0
        Pattern found at index 9
        Pattern found at index 12
C programming for Searching for Patterns Set 2 KMP Algorithm

Pattern searching is an important problem in computer science. When we do search for a string in notepad/word file or browser or database, pattern searching algorithms are used to show the search results.

[ad type=”banner”]

We have discussed the following algorithms in the previous posts:

Naive Algorithm
KMP Algorithm
Rabin Karp Algorithm

In this post, we will discuss Finite Automata (FA) based pattern searching algorithm. In FA based algorithm, we preprocess the pattern and build a 2D array that represents a Finite Automata. Construction of the FA is the main tricky part of this algorithm. Once the FA is built, the searching is simple. In search, we simply need to start from the first state of the automata and the first character of the text. At every step, we consider next character of text, look for the next state in the built FA and move to a new state. If we reach the final state, then the pattern is found in the text. The time complexity of the search process is O(n).
Before we discuss FA construction, let us take a look at the following FA for pattern ACACAGA.

C-Searching for Patterns Set 5 Finite Automata

The above diagrams represent graphical and tabular representations of pattern ACACAGA.

Number of states in FA will be M+1 where M is length of the pattern. The main thing to construct FA is to get the next state from the current state for every possible character. Given a character x and a state k, we can get the next state by considering the string “pat[0..k-1]x” which is basically concatenation of pattern characters pat[0], pat[1] … pat[k-1] and the character x. The idea is to get length of the longest prefix of the given pattern such that the prefix is also suffix of “pat[0..k-1]x”. The value of length gives us the next state. For example, let us see how to get the next state from current state 5 and character ‘C’ in the above diagram. We need to consider the string, “pat[0..5]C” which is “ACACAC”. The length of the longest prefix of the pattern such that the prefix is suffix of “ACACAC”is 4 (“ACAC”). So the next state (from state 5) is 4 for character ‘C’.

[ad type=”banner”]

In the following code, computeTF() constructs the FA. The time complexity of the computeTF() is O(m^3*NO_OF_CHARS) where m is length of the pattern and NO_OF_CHARS is size of alphabet (total number of possible characters in pattern and text). The implementation tries all possible prefixes starting from the longest possible that can be a suffix of “pat[0..k-1]x”. There are better implementations to construct FA in O(m*NO_OF_CHARS) (Hint: we can use something like lps array construction in KMP algorithm). We have covered the better implementation in our next post on pattern searching.

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%23include%3Cstring.h%3E%0A%23define%20NO_OF_CHARS%20256%0A%20%0Aint%20getNextState(char%20*pat%2C%20int%20M%2C%20int%20state%2C%20int%20x)%0A%7B%0A%20%20%2F%2F%20If%20the%20character%20c%20is%20same%20as%20next%20character%20in%20pattern%2C%0A%20%20%2F%2F%20then%20simply%20increment%20state%0A%20%20if%20(state%20%3C%20M%20%26%26%20x%20%3D%3D%20pat%5Bstate%5D)%0A%20%20return%20state%2B1%3B%0A%20%0A%20%20int%20ns%2C%20i%3B%20%2F%2F%20ns%20stores%20the%20result%20which%20is%20next%20state%0A%20%0A%20%20%2F%2F%20ns%20finally%20contains%20the%20longest%20prefix%20which%20is%20also%20suffix%0A%20%20%2F%2F%20in%20%22pat%5B0..state-1%5Dc%22%0A%20%0A%20%20%2F%2F%20Start%20from%20the%20largest%20possible%20value%20and%20stop%20when%20you%20find%0A%20%20%2F%2F%20a%20prefix%20which%20is%20also%20suffix%0A%20%20for%20(ns%20%3D%20state%3B%20ns%20%3E%200%3B%20ns–)%0A%20%20%7B%0A%20%20if(pat%5Bns-1%5D%20%3D%3D%20x)%0A%20%20%7B%0A%20%20for(i%20%3D%200%3B%20i%20%3C%20ns-1%3B%20i%2B%2B)%0A%20%20%7B%0A%20%20if%20(pat%5Bi%5D%20!%3D%20pat%5Bstate-ns%2B1%2Bi%5D)%0A%20%20break%3B%0A%20%20%7D%0A%20%20if%20(i%20%3D%3D%20ns-1)%0A%20%20return%20ns%3B%0A%20%20%7D%0A%20%20%7D%0A%20%0A%20%20return%200%3B%0A%7D%0A%20%0A%2F*%20This%20function%20builds%20the%20TF%20table%20which%20represents%20Finite%20Automata%20for%20a%0A%20%20given%20pattern%20*%2F%0Avoid%20computeTF(char%20*pat%2C%20int%20M%2C%20int%20TF%5B%5D%5BNO_OF_CHARS%5D)%0A%7B%0A%20%20int%20state%2C%20x%3B%0A%20%20for%20(state%20%3D%200%3B%20state%20%3C%3D%20M%3B%20%2B%2Bstate)%0A%20%20for%20(x%20%3D%200%3B%20x%20%3C%20NO_OF_CHARS%3B%20%2B%2Bx)%0A%20%20TF%5Bstate%5D%5Bx%5D%20%3D%20getNextState(pat%2C%20M%2C%20state%2C%20x)%3B%0A%7D%0A%20%0A%2F*%20Prints%20all%20occurrences%20of%20pat%20in%20txt%20*%2F%0Avoid%20search(char%20*pat%2C%20char%20*txt)%0A%7B%0A%20%20int%20M%20%3D%20strlen(pat)%3B%0A%20%20int%20N%20%3D%20strlen(txt)%3B%0A%20%0A%20%20int%20TF%5BM%2B1%5D%5BNO_OF_CHARS%5D%3B%0A%20%0A%20%20computeTF(pat%2C%20M%2C%20TF)%3B%0A%20%0A%20%20%2F%2F%20Process%20txt%20over%20FA.%0A%20%20int%20i%2C%20state%3D0%3B%0A%20%20for%20(i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%7B%0A%20%20state%20%3D%20TF%5Bstate%5D%5Btxt%5Bi%5D%5D%3B%0A%20%20if%20(state%20%3D%3D%20M)%0A%20%20%7B%0A%20%20printf%20(%22%5Cn%20Pattern%20found%20at%20index%20%25d%22%2C%20i-M%2B1)%3B%0A%20%20%7D%0A%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20char%20*txt%20%3D%20%22AABAACAADAABAAABAA%22%3B%0A%20%20char%20*pat%20%3D%20%22AABA%22%3B%0A%20%20search(pat%2C%20txt)%3B%0A%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

  Pattern found at index 0
  Pattern found at index 9
  Pattern found at index 13