Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for bit strings with even number of 1s

Let L= { w in (0+1)* | w has even number of 1s}, i.e. L is the set of all bit strings with even number of 1s. Which one of the regular expressions below represents L?

A) (0*10*1)*
B) 0*(10*10*)*
C) 0*(10*1)* 0*
D) 0*1(10*1)* 10*

According to me option D is never correct because it does not represent the bit string with zero 1s. But what about the other options? We are concerned about the number of 1s(even or not) not the number of zeros doesn't matter.

Then which is the correct option and why?

like image 516
Prasoon Saurav Avatar asked Apr 24 '10 05:04

Prasoon Saurav


2 Answers

A if false. It doesn't get matched by 0110 (or any zeros-only non-empty string)

B represents OK. I won't bother proving it here since the page margins are too small.

C doesn't get matched by 010101010 (zero in the middle is not matched)

D as you said doesn't get matched by 00 or any other # with no ones.

So only B

like image 197
DVK Avatar answered Oct 22 '22 09:10

DVK


To solve such a problem you should

  1. Supply counterexample patterns to all "incorrect" regexps. This will be either a string in L that is not matched, or a matched string out of L.
  2. To prove the remaining "correct" pattern, you should answer two questions:

    • Does every string that matches the pattern belong to L? This can be done by devising properties each of matched strings should satisfy--for example, number of occurrences of some character...
    • Is every string in L matched by the regexp? This is done by dividing L into easily analyzable subclasses, and showing that each of them matches pattern in its own way.

(No concrete answers due to [homework]).

like image 43
P Shved Avatar answered Oct 22 '22 10:10

P Shved