![]() |
VOOZH | about |
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
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.
The string-matching automaton is a very useful tool which is used in string matching algorithm.
String matching algorithms build a finite automaton scans the text string T for all occurrences of the pattern P.
FINITE AUTOMATA
?=Sets of input symbols
q. = Initial state
F = Final State
? = Transition function
A finite automaton M is a 5-tuple (Q, q0,A,??), where
Q is a finite set of states,
q0 ? Q is the start state,
A ? Q is a notable set of accepting states,
? is a finite input alphabet,
? is a function from Q x ? into Q called the transition function of M.
The finite automaton starts in state q0 and reads the characters of its input string one at a time. If the automaton is in state q and reads input character a, it moves from state q to state ? (q, a). Whenever its current state q is a member of A, the machine M has accepted the string read so far. An input that is not allowed is rejected.
A finite automaton M induces a function ? called the called the final-state function, from ?* to Q such that ?(w) is the state M ends up in after scanning the string w. Thus, M accepts a string w if and only if ?(w) ? A.
FINITE AUTOMATA (T, P) State <- 0 for l <- 1 to n State <- ?(State, ti) If State == m then Match Found end end
Why it is efficient?
These string matching automaton are very efficient because they examine each text character exactly once, taking constant time per text character. The matching time used is O(n) where n is the length of Text string.
But the preprocessing time i.e. the time taken to build the finite automaton can be large if ? is large.
Before we discuss Finite Automaton construction, let us take a look at the following Finite Automaton for pattern ACACAGA.
👁 Finite Automata algorithm for Pattern Searching 1
👁 Finite Automata algorithm for Pattern Searching 2
The above diagrams represent graphical and tabular representations of pattern ACACAGA.
Number of states in Finite Automaton will be M+1 where M is length of the pattern. The main thing to construct Finite Automaton 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..4]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'.
In the following code, computeTF() constructs the Finite Automaton. 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 Finite Automaton 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.
Output:
Pattern found at index 0 Pattern found at index 9 Pattern found at index 13
Time Complexity: O(m2)
Auxiliary Space: O(m)
References:
Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein