views:

144

answers:

2
+3  Q: 

Regular Expression

Ohh! this regular expression thing is eating my brain up. I have been reading it from Introduction to Automata Theory, Languages and Computer by Hopcroft, Motwani and Ullman.

I have solved a few exercises too but could not solve the following even after trying for almost one hr.

The problem is to write a regular expression that defines a language consisting of all strings of 0s and 1s not containing the substring 011.

Is the answer (0+1)* - 011 correct ? If not what should be the correct answer for this?

EDITED

+2  A: 

If you are looking for all strings that do not have 011 as a substring rather than simply excluding the string 011:

A classic regex for that would be:

1*(0+01)*

Basically you can have as many ones at the beginning as you want, but as soon as you hit a zero, it's either zeros, or zero-ones that follow (since otherwise you'd get a zero-one-one).

A modern, not-really-regular regex would be:

^((?!011)[01])*$

IF, however, you want any string that is not 011, you can simply enumerate short string and wildcard the rest:

λ+0+1+00+01+10+11+(1+00+010)(0+1)*

And in modern regex:

^(?!011)[01]*$
Max Shawabkeh
`λ+0+1+00+01+10+11+(1+00+010)(0+1)*` how did you reach to this RE?
equilibrium
The first part is an enumeration of all strings that are shorter than 3 chars. The second part are strings with which `011` can't start, followed by arbitrary data.
Max Shawabkeh
+5  A: 

Automata diagram

RJFalconer
As you mentioned automata, I thought I'd draw some, as they're so fun! :). Both are deterministic finite state. I'll be happy to answer questions on their construction if they're not explanatory enough. (Or correct any mistakes). Hope this helps!
RJFalconer
You have no arrow indicating the starting states! (-1 from the teacher)But we'll assume the leftmost shall we. The second is right on the money, the first is wrong. If you only accept exactly 011, all the arrows towards the leftmost state should go toward the righmost state in stead, because in those cases 011 can never be achieved any more.
NomeN
Yes, you are correct. I have fixed the image. It would have previously rejected (some) strings that ended in 011.
RJFalconer