2

Letting {a b} be the alphabet set, write a regular expression for:

1) The language of all those words in which the number of a's and the number of b's are both odd;

2) The language of all those words whose length is odd and which contain the substring ab.

Also, if possible, please help me find two different expressions for each so as to help strengthen my understanding of how to go about solving such problems.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
Mi7
  • 23
  • 2
  • You need to give some more info about your background. Have you seen the DFA --> regex reductions or pumping lemma? – Foo Bah Oct 02 '11 at 14:09
  • not sure what to say ... first time studying theory of automata , have done a bit of regular expression ... i can probably give you an example of how much i can solve this question. – Mi7 Oct 02 '11 at 14:14
  • answer for the first one , a(aa)*b(bb)*+b(bb)*a(aa)* ... this will generate a odd # of a's and b's language but the positions won't be random – Mi7 Oct 02 '11 at 14:15
  • @user975411: I think the intent is that the positions should be "random", i.e. arbitrary... – Patrick87 Oct 02 '11 at 14:46

1 Answers1

3

For the first one, there's an easy 4-state DFA you can construct to recognize the language. Then, you can use the algorithm recoverable from Kleene's theorem (the part where he says all languages recognized by a FA are generated by a RE) to get an RE that works... or just reason it out from the diagram.

For the second one, you know that (ab) is part of the RE; now, you need to think of all the unique ways you could add an odd number of characters to this (front or back), and connect all those possibilities with + for an easy, correct RE.

I don't think anybody particularly likes the idea of just giving you the answer.

EDIT:

So now that some time has passed, I'll work through the answer to the first one to show interested readers how it can be done.

Our first FA is this:

   Q s f(Q, s)
  -- - -------
  EE a     OE
  EE b     EO
  OE a     EE
  OE b     OO
  EO a     OO
  EO b     EE
  OO a     EO
  OO b     OE

We will remove states from this and replace s with a regular expression to cover that state. We start with an easy one... let's get rid of OE. Here's the table for that...

   Q                  regex f(Q, s)
  -- ---------------------- -------
  EE                     aa      EE
  EE                     ab      OO
  EE                      b      EO
  EO                      a      OO
  EO                      b      EE
  OO                      a      EO
  OO                     ba      EE
  OO                     bb      OO

Convince yourself this is correct before continuing. Next, we get rid of EO:

   Q                  regex f(Q, s)
  -- ---------------------- -------
  EE                  aa+bb      EE
  EE                  ab+ba      OO
  OO                  ab+ba      EE
  OO                  aa+bb      OO

To make the next step simpler, we introduce a new start set X and a new accepting state Y; OO is no longer accepting. We eliminate the need for OO:

   Q                        regex f(Q, s)
  -- ---------------------------- -------
   X                        empty      EE
  EE aa+bb+(ab+ba)(aa+bb)*(ab+ba)      EE
  EE              (ab+ba)(aa+bb)*       Y

Therefore, the final regex is

  (aa+bb+(ab+ba)(aa+bb)*(ab+ba))*(ab+ba)(aa+bb)*

We can begin trying to list the smallest strings this generates, just as a basic sanity check: {ab, ba, aaab, aaba, bbab, bbba, abaa, abbb, baaa, babb, ...} Looks good to me!

The rules for reducing at each step can be formalized, or you can just apply careful reasoning to ensure that you're getting the right thing. Check a proof of Kleene's theorem for a careful analysis. Also, Martin's Introduction to Formal Languages or something has good examples of using this algorithm.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • yeh thanks ... but the problem i'm having is i'm not sure how to randomize while keeping that expression (a(aa)*b(bb)*+b(bb)*a(aa)*) that i made consistent ... perhaps i need to read more parts of book which i am doing ... – Mi7 Oct 02 '11 at 15:02
  • Can you make the DFA? If so, there's an algorithm to recover an RE, and you don't need to do any thinking at all... just follow the steps! There is probably a Java app online that does this transformation for free. – Patrick87 Oct 02 '11 at 15:05
  • i can still not derive the expression .. can you only tell me how to draw an DFA for an odd number of characters .. – Mi7 Oct 02 '11 at 16:20
  • Have four states, called EE, EO, OE and OO. EE is the start state. OO is the only accepting state. E means "an even number so far", O means "an odd number so far", the first letter says how many a's, the second how many b's. E goes to O and vice versa for each letter each time you see it. – Patrick87 Oct 02 '11 at 17:32