Search code examples
c++regexalgorithmpattern-matchingfinite-automata

Pattern matching with finite automata


Recently I was reading the famous Algorithm design book CLRS(Cormen, Leiserson, Rivest, Stain, 3-rd edition). And between the classical KMP and Rabin - Karp algorithm there is a part about string matching with finite automata. So the algorithm creates the automata according to the pattern and starts processing on the string. enter image description here

So here in this example, the algorithm searches the pattern "ababaca" in the input string. So everything seems logical to me, beside two things.

Why there is no path to the previous states from state 4 when I get to "b", because in that case I will have "ababb", which already is a mismatch???? And what happen when I read "b" or "c" from the state 6?? Is there something that I misunderstood? Also there is no reading "c" case from the states 0 to 4 and so on..


Solution

  • Check the table (b). All the states you are talking about are marked as 0. So you go back to the beginning. In the image you would get a lot of edges back to 0 so they don't show them (for clarity).