7

I'm trying to freshen myself up on discrete math( I forgot a lot). I know this is a trivial question and not worth your time. But I forgot how to solve problems involving formal language theory.

For example lets say you were trying to

(a) write a regular expression for the language A of binary strings whose length is divisible by three

(b) the language B of all binary strings that do not contain two consecutive 1's.

Where would I start? Edited to make question clearer.

Timonse
  • 139

1 Answers1

3

One very straightforward approach to (a) is to begin by listing the $8$ binary strings of length $3$. Every string whose length is a multiple of $3$ must be formed by concatenating those strings. I’ll illustrate with strings of even length: any string of even length can be segmented into two-character chunks, each of which must be $00,01,10$, or $11$, so the binary strings of even length are described by the regular expression $(00+01+10+11)^*$. (You may use some other symbol than $+$ to indicate alternatives; common alternatives are $\mid$, $\lor$, and $\cup$.)

Problem (b) is a little harder. The condition says that if a string contains a $1$ at all, that $1$ must either be the last symbol in the string or be followed immediately by a $0$. Ignore for a moment the exceptional case of a final $1$; if we just require that every $1$ be followed immediately by a $0$, we’re allowing precisely those strings that can be built by concatenating $0$’s and $10$’s, i.e., the strings described by the regular expression $(0+10)^*$. We’d like to have all of those strings, but also all strings that consist of one of those followed by a single $1$; how can you modify or extend the regular expression to include the latter type as well as the former?

Brian M. Scott
  • 631,399
  • I've been thinking about this for a while and am still stuck do you think you could write out a solution? Sorry for the trouble. – Timonse Oct 29 '13 at 20:26
  • @Timonse: Which part? For (a) I gave a complete solution to a very closely related problem; all you need to do is follow the model. For (b) I stayed with the given problem but left just one tiny bit of the solution undone. – Brian M. Scott Oct 29 '13 at 20:29
  • I'm having trouble with both somewhat but for the first part I am having trouble coming up with the 8 binary strings. Can you give me atleast one example of a string divisible by 3 I forgot how to do this. – Timonse Oct 29 '13 at 20:39
  • @Timonse: The strings aren’t numbers, so it doesn’t make sense to ask for examples of strings divisible by $3$. It’s the length of the string that has to be divisible by $3$. Every string whose length is divisible by $3$ can be broken up into $3$-bit chunks. There are only $8$ possible chunks. One of them is $011$, for instance. What are the other $7$? – Brian M. Scott Oct 29 '13 at 20:42
  • Sorry I was busy. So all eight would be 011, 110, 1001, 1110, 1111, 10010, 10101, 11000. Is that right? Also how would I write the regular expression. Thanks. – Timonse Oct 29 '13 at 21:24
  • @Timonse: No, that’s not right: only your first two strings are of length $3$. $1001$ is a string of length $4$, and $10101$ is a string of length $5$. Once you get the right eight strings, model your regular expression on the one that I used for strings of length $2$: $(00+01+10+11)^*$. – Brian M. Scott Oct 29 '13 at 21:30
  • Oh, I see.. So they it would be (000, 001, 010, 011, 100, 101, 110, 111)* yes? Also would the answer for part b be long. – Timonse Oct 29 '13 at 21:37
  • @Timonse: (a) Almost: I’ve never seen commas used for the disjunction symbol in a regular expression. As I said in my answer, I don’t know what symbol you’re supposed to be using; the usual ones are $+$, $\mid$, $\lor$, and $\cup$. (b) Not very, no. There are two straightforward answers (equivalent, but not identical), both approximately twice as long as the partial answer $(0+10)^*$. – Brian M. Scott Oct 29 '13 at 21:41
  • Oops meant to put + wasn't paying attention. For b. I can only think of (00 + 01 + 10)* since 11 would break the rule in the problem. – Timonse Oct 29 '13 at 21:47
  • @Timonse: But if you actually test that regular expression, you’ll quickly find that it can’t be right: it generates only strings of even length, so it doesn’t generate $0$, $000$, or many other strings that do not contain the subsequence $11$. It also generates unwanted strings, like $0110$. You’re ignoring all of the work that I already did in my original answer. $(0+10)^$ generates all of the right strings except the ones that end in $1$, and each of those is a string of the form generated by $(0+10)^$ followed by a single $1$. Let’s look at something simpler for a moment. $(00)^*$ ... – Brian M. Scott Oct 29 '13 at 21:52
  • ... generates all strings consisting of an even number of $0$’s. What regular expression generates all strings that consist of an even number of $0$’s followed by a single $1$? – Brian M. Scott Oct 29 '13 at 21:53
  • Hmm. So for your answer in part b are you missing 010? – Timonse Oct 29 '13 at 22:10
  • @Timonse: No: it’s the concatenation of $0$ with $10$, so it’s covered by $(0+10)^*$. – Brian M. Scott Oct 29 '13 at 22:11
  • Argh.. If its not 01 and not 00 and not 11 what could it be. – Timonse Oct 29 '13 at 22:13
  • @Timonse: It’s hard to be sure, but I suspect that you’re looking in the wrong direction entirely. Have you attempted to answer the simpler question at the end of my long comment? – Brian M. Scott Oct 29 '13 at 22:15
  • Can you tell me what it is. I'll try to work backwards and it might make more sense to me. – Timonse Oct 29 '13 at 22:21
  • @Timonse: The answer to the simpler question is $(00)^1$: $(00)^$ generates any string of an even number of $0$’s, and the $1$ then appends a $1$, so you get $1,001,00001$, and so on. If you wanted to get both strings of an even number of $0$’s and such strings followed by a single $1$, you could use $(00)^+(00)^1$, or you could be a little fancier and use $(00)^*(\lambda+1)$, where $\lambda$ is the symbol for the empty word, i.e., the word with no symbols. (You may use $\epsilon$ for that; both conventions are fairly common.) – Brian M. Scott Oct 29 '13 at 22:23