1

The question was to give the regular expression for all the As appearing before any of the Bs, or all the Bs appearing before any of the As.

I have got the regular expression as a * b * + b * a * .

I am not sure if I have done this correct. I would appreciate any help thank you.

Sid
  • 15
  • 4
  • 1
    Maybe helpful : [how-to-convert-regular-expression-to-finite-state-machine](https://stackoverflow.com/questions/41038719/how-to-convert-regular-expression-to-finite-state-machine) and [converting-ab-regular-expression-to-finite-state-machine](https://stackoverflow.com/questions/51453230/converting-ab-regular-expression-to-finite-state-machine) – JohanC Jan 05 '20 at 14:10
  • 1
    I am possibly wrong, but your automaton seems to accept "baba" but not "ab"... – CiaPan Jan 05 '20 at 14:29
  • 1
    There is also [this article](https://www.geeksforgeeks.org/designing-finite-automata-from-regular-expression-set-1/) – JohanC Jan 05 '20 at 14:29

2 Answers2

2

If you want a non-deterministic finite automaton, you can use an algorithm that does the conversion based on operations in the regular expression:

  • + turns into nondeterministic branching epsilon transitions
  • concatenation turns into sequential transitioning from state to state
  • Kleene star turns into loops

Your NDA would look something like this:

        a        b
        ^        ^
q0--e-->q1--e-->q2
 |
 e
 |
 V
q4--e-->q5
v      v
b      a

If you want a deterministic finite automaton, you can determinize the nonseterministic one using a known algorithm. Otherwise, we can run Myhill-Nerode in reverse to find equivalence classes under the indistinguishability relation; this will give us the states of a minimal DFA.

  • e can be followed by any string in L, and is in our language; this class is [e] and corresponds to initial state q0 which is accepting
  • a can be followed only by ab; a is in the language, so class [a] corresponds to accepting state q1
  • b can be followed only by ba; b is in the language, so class [b] corresponds to accepting state q2
  • ab can be followed only by b*; [ab], q, accepting
  • ba can be followed only by a*; [ba], q4, accepting
  • aba, bab not in language and can never be fixed; [aba], q5, not accepting
  • all other strings of length 3 are indistinguishable from strings already seen; we're done

So there's a DFA with 6 states - 1 initial (q0), 5 accepting and 1 dead (q5) - which accepts the language. You can figure out the transitions as follows: each state is arrived at after following any string which belongs to its equivalence class. Note that the dead state q5 must have two self-loops as its transitions.

Frederik Spang
  • 3,379
  • 1
  • 25
  • 43
Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • Hi, thank you for your help. The two letters I am told to use are Σ = {a, b}. Will I be able to use 'e'?. If so do I need to chance the regular expression which is a * b * + b * a * – Sid Jan 05 '20 at 21:44
  • 1
    @Sid e represents the empty string. You can use it for NFAs, generally speaking. You won't need it in the DFA since all transitions consume input symbols. – Patrick87 Jan 06 '20 at 03:02
  • thank you. do I need to change the regular expression if I use an empty set? – Sid Jan 06 '20 at 13:14
  • 1
    @Sid Not the empty set, the empty string. And the regular expression does not need to change. – Patrick87 Jan 06 '20 at 13:32
  • @Patrick87 You say the DFA would have 6 states, but 1+5+1 add up to 7, not 6 ...? Or maybe the initial state belongs also to a group of five accepting states...? – CiaPan Feb 19 '20 at 23:00
0

Describe in your own words what strings are accepted, then describe what strings are accepted after an input of a or b. For every different set of accepted strings that is not identical you create a state. If the set of accepted strings contains the empty string then the state is an accepting state.

The set of accepted strings, accepted strings after input a, and accepted strings after input b are all different, so at least three states are needed. (a must be followed by a* b*, and b must be followed by b* a*).

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • So will there be two final states? Also is the regular expression incorrect aswell? – Sid Jan 05 '20 at 15:47