patternMinor
Regular Expression for even-odd language of string
Viewed 0 times
expressionregularlanguageforevenstringodd
Problem
I am new to Automata theory and would to make a regular expression for "even-odd" strings, defined over $\Sigma = \{a,b\}$, which is the set of strings with even numbers of $b$'s and odd number of $a$'s. I am also interested in constructing a DFA and NFA for the language.
I tried
Even number of $b$'s = $(a^{\ast}ba^{\ast}ba^{\ast})^{\ast}$
Edit: I got
I also tried this Regular Expression
$(a(bb)^{\ast}(aa)^{\ast})^{\ast} $
Note: From above RE only those strings are generated which start from a but i want that RE which generate string of even number of $b$'s and odd number of $a$'s regardles of starting from a or b
I tried
Even number of $b$'s = $(a^{\ast}ba^{\ast}ba^{\ast})^{\ast}$
Edit: I got
DFA and NFA for odd number of $a$'s and even numbers of $b$'s and still unable to make regular expression which will allow only even number of $b$'s and odd nubmer of $a$'s .I also tried this Regular Expression
$(a(bb)^{\ast}(aa)^{\ast})^{\ast} $
Note: From above RE only those strings are generated which start from a but i want that RE which generate string of even number of $b$'s and odd number of $a$'s regardles of starting from a or b
Solution
Constructing a DFA (and thus an NFA), is quite trivial, so let's focus on the regular expression. (well, you can always convert a DFA into a regular expression, but this will not be very insightful, is it?)
Let's start simple. How do we construct an expression that describes only even length strings (over $\{a,b\}$)? Or even more simple, how to construct an expression for a language of only even length strings over $\{a\}$?
If there's only $\{a\}$ in the alphabet, we either have none, or they come in groups of two, so a possible answer is $(aa)^*$.
When we have both $a$ and $b$, things become a bit more difficult. For instance, $(ab)^$ is incorrect, since it doesn't allow $aa$. On the other hand, any expression of the form $(a^b^)^$ not going to work, since we can't control the number of each letter. However, as before, we know that the letters must come in groups of 2, thus we should expect the form to be something like $(xy)^*$ with $x,y$ describing only even length strings, but what should $x,y$ be in this case? Well, we can just write all possible options, getting:
$$ ( aa+ ab+ ba+ bb)^*$$
(can you make it shorter?)
Now let's ask how do we get odd-length strings. Again, if the alphabet has only $a$, this would be quite easy - we can construct an even string, and add another $a$, getting:
$$ (aa)^*a$$
A similar approach works for the case where the alphabet is of size two:
$$ ( aa+ ab+ ba+ bb)^*(a+b)$$
Now a sanity check: we add the "a" or "b" only at the end. Maybe it comes in the middle, and not in the end?! Convince yourself that adding at the end is equivalent, and gives the right answer.
Finally, try to use the above ideas to solve your question. There is still some leap left, so don't give up too fast. Some hints:
Hint1: the length of the final string must be odd (if it contains both letters)
.
Hint2: get rid of one letter, so the length is even, and furthermore, both letters appear an even number of times!
.
Hint3: don't forget to put back the one letter you got rid of. Convince yourself you placed it in the correct place...
Let's start simple. How do we construct an expression that describes only even length strings (over $\{a,b\}$)? Or even more simple, how to construct an expression for a language of only even length strings over $\{a\}$?
If there's only $\{a\}$ in the alphabet, we either have none, or they come in groups of two, so a possible answer is $(aa)^*$.
When we have both $a$ and $b$, things become a bit more difficult. For instance, $(ab)^$ is incorrect, since it doesn't allow $aa$. On the other hand, any expression of the form $(a^b^)^$ not going to work, since we can't control the number of each letter. However, as before, we know that the letters must come in groups of 2, thus we should expect the form to be something like $(xy)^*$ with $x,y$ describing only even length strings, but what should $x,y$ be in this case? Well, we can just write all possible options, getting:
$$ ( aa+ ab+ ba+ bb)^*$$
(can you make it shorter?)
Now let's ask how do we get odd-length strings. Again, if the alphabet has only $a$, this would be quite easy - we can construct an even string, and add another $a$, getting:
$$ (aa)^*a$$
A similar approach works for the case where the alphabet is of size two:
$$ ( aa+ ab+ ba+ bb)^*(a+b)$$
Now a sanity check: we add the "a" or "b" only at the end. Maybe it comes in the middle, and not in the end?! Convince yourself that adding at the end is equivalent, and gives the right answer.
Finally, try to use the above ideas to solve your question. There is still some leap left, so don't give up too fast. Some hints:
Hint1: the length of the final string must be odd (if it contains both letters)
.
Hint2: get rid of one letter, so the length is even, and furthermore, both letters appear an even number of times!
.
Hint3: don't forget to put back the one letter you got rid of. Convince yourself you placed it in the correct place...
Context
StackExchange Computer Science Q#41830, answer score: 8
Revisions (0)
No revisions yet.