6

I'm working on a problem (from Introduction to Automata Theory, Languages and Computer by Hopcroft, Motwani and Ullman) 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?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 4
    If you are giving up after an hour, I suggest you try harder and do some searching. – AMissico Apr 17 '10 at 09:58
  • 1
    I'm not giving you the answer but try this: Draw a finite state machine (graph) that accepts 011 as an input and then negate it (all accepting states are none accepting and none accepting are accepting). You should be able to solve the regular expression from there as it also is a finite state machine. – mkorpela Apr 17 '10 at 10:02
  • 1
    Well that same trick works for that as well, Draw a finite state machine (graph) that accepts any string that contains 011 ... – BCS Apr 17 '10 at 14:30

2 Answers2

10

Automata diagram Edit: Updated to include start states and fixes, as per below comments.

RJFalconer
  • 10,890
  • 5
  • 51
  • 66
  • 1
    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 Apr 17 '10 at 11:11
  • 3
    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 Apr 17 '10 at 11:56
  • Yes, you are correct. I have fixed the image. It would have previously rejected (some) strings that ended in 011. – RJFalconer Apr 18 '10 at 16:39
  • I don't think your first graph is correct, when it reaches the last state, 011 will be accepted. – Blake Apr 06 '15 at 11:52
  • @Blake you're right; that's the machine for "all except exactly the string 011". The original question was unclear as to which the OP was looking for. The 2nd machine more closely matches the question as stands. – RJFalconer Apr 07 '15 at 08:26
  • Can you also add a regular expression for this case – Prasanna Sep 17 '20 at 02:17
  • Hey @PrasannaSasne; other answers have this already. What case are you looking for exactly? – RJFalconer Sep 17 '20 at 04:54
  • I am looking for a regex for the same condition which you have implemented in DFA. – Prasanna Sep 17 '20 at 13:05
  • Everything except 011? `^(?!(.*?011$)).*$` – RJFalconer Sep 17 '20 at 14:14
5

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
  • 37,799
  • 10
  • 82
  • 91
  • 1
    `λ+0+1+00+01+10+11+(1+00+010)(0+1)*` how did you reach to this RE? – Prasoon Saurav Apr 17 '10 at 11:22
  • 1
    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 Apr 17 '10 at 11:28
  • 3
    Your answer is wrong. 1*(0+01)* does not accept 10, nor does it accept 101. – Buğra Gedik May 22 '15 at 07:02
  • your answer `1*(0+01)*` how it will not accept 011. can you please explain? – Prasanna Sep 17 '20 at 02:16
  • 1
    @PrasannaSasne You can use https://regexr.com to explain the parts of the regex. In short, `1*(0+01)*` does not match `011` as the only `1`s it accepts are a `1` at the start (`1*` in the regex) or a `1` that is also prefixed by a `0` (the `01` in the `(0+01)*` regex) – RJFalconer Sep 17 '20 at 04:57