Question

I'm taking a computation course which also teaches about regular expressions. There is a difficult question that I cannot answer.

Find a regular expression for the language that accepts words that contains at most two pair of consecutive 0's. The alphabet consists of 0 and 1.

First, I made an NFA of the language but cannot convert it to a GNFA (that later be converted to regex). How can I find this regular expressin? With or without converting it to a GNFA?

Was it helpful?

Solution

(Since this is a homework problem, I'm assuming that you just want enough help to get started, and not a full worked solution?)

Your mileage may vary, but I don't really recommend trying to convert an NFA into a regular expression. The two are theoretically equivalent, and either can be converted into the other algorithmically, but in my opinion, it's not the most intuitive way to construct either one.

Instead, one approach is to start by enumerating various possibilities:

  • No pairs of consecutive zeroes at all; that is, every zero, except at the end of the string, must be followed by a one. So, the string consists of a mixed sequence of 1 and 01, optionally followed by 0:

    (1|01)*(0|ε)
    
  • Exactly one pair of consecutive zeroes, at the end of the string. This is very similar to the previous:

    (1|01)*00
    
  • Exactly one pair of consecutive zeroes, not at the end of the string — and, therefore, necessarily followed by a one. This is also very similar to the first one:

    (1|01)*001(1|01)*(0|ε)
    

To continue that approach, you would then extend the above to support two pair of consecutive zeroes; and lastly, you would merge all of these into a single regular expression.

OTHER TIPS

(0+1)*00(0+1)*00(0+1)* + (0+1)*000(0+1)*

contains at most two pair of consecutive 0's

(1|01)*(00|ε)(1|10)*(00|ε)(1|10)*
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top