1

I am learning String matching with finite automata from CLRS . I am solving some exercise problems .For the exercise problem 32.3-1 ,

Construct the string-matching automaton for the pattern P = aabab and illustrate its operation on the text string T = aaababaabaababaab.

the following is my transition function ,

states   a   b
 0       1   0
 1       2   0
 2       2   3
 3       4   3
 4       4   5
 5       ?   ?

Is my transition function correct ? And how do i fill the last row ? Any help

MdKamil
  • 41
  • 3
  • 10
  • 1
    your transition function is wrong. in state 3, when a `b` is encountered, state should go back to 0. in state 4, when an `a` encountered, state should go back to 2. – Jason Hu Aug 13 '15 at 02:32
  • @HuStmpHrrr thanks for your response :).. Can u elaborate on this ? I mean in state 3 when b is encountered , the longest prefix of pattern that matches the suffix of the string is "aab" , hence it should go back to state 3, how can it go back to 0.Silmilarly in state 4 , when a is encountered,the longest prefix of the pattern that matches the suffix of the text is "aaba" , hence it should go back to state 4 , how can it go back to state 2..Correct me if i am wrong in my understanding. – MdKamil Aug 13 '15 at 04:00

1 Answers1

1

I assume you are creating a Finite Automata which accepts a string containing the pattern aabab.

There are two mistakes in your finite automata,

on state 3 and state 4,

For state 3, if the input is b, you have to go back to state 0. For example the pattern aabb will force you back to state 0. Here you have to start all over again from state 0.

For state 4, if the input is a, you have to go back to state 2 because you have the pattern aa. For example the pattern aabaa will force you back to state 2.

The corrected Finite Automaton is given below,

states   a   b
 0       1   0
 1       2   0
 2       2   3
 3       4   0
 4       2   5
 5       5   5

Here 5 is your Accepting state. You will reach this state only when you have found the required pattern in a string. Once a pattern is found no matter what the string remains in the accepting state. Hence for both inputs a and b on state 5 remains on 5 itself.

The transition function is that of an fa accepting a string with sub string 'aabab'. If you are going back to state 1 for a and 0 for b, then the transition function accepts strings ending with the sub string 'aabab'. Given that only state 5 is the accepting state.

Deepu
  • 7,592
  • 4
  • 25
  • 47
  • thanks for the response :)... One clarification for **state 5** it should be **1** for **a** and **0** for **b** according to the pseudo-code given in **CLRS** page no **1001** ? Correct me if i am wrong !! – MdKamil Aug 13 '15 at 07:15
  • @402951 it seems your question has been answered. if you reach state 5, you should stay there because the match has been found and no further move should be taken. the program has accepted your input and that should be it. – Jason Hu Aug 13 '15 at 12:59