[Math] Understanding Kleene star in relation to regular expressions

computer sciencediscrete mathematicsregular expressions

I have a homework problem for my intro computation class where I have to write a regular expression for an NFA. In order to help understand if my language is correct- I want to post some example regular expressions with the kleene star operation in order to ensure that I understand the operation correctly.

Suppose we have the language defined as {0, 1} and we have the following regular expression:

(0 + 1)*

My understanding is that this regular expression will return either the empty string- or any combination of 0 and 1's.

If I wanted to get rid of the empty string, I think this would be the way to do it:

(0+1)((0+1)*)

Such that we start with either a 0 or 1 and then append any combination of 0 or 1's to it (or none at all), which would still leave us with either a 0 or 1.

I guess my issue comes in understanding whether my above regular expressions are right for what I want to accomplish- and also if there's a difference between

(0+1)((0+1)*)

and

(0+1)(0*1*)

and

(0+1)((0*1*)*)

Are all three of these equivalent? If not, where do they differ? Im having trouble seeing if there's a difference. If they're equivalent then I guess I understand the kleene star operation. If not, can someone explain how and why they are different? Thanks. This has been bugging me for a while and I'm not sure if I'm just rewriting equivalent regular expressions or if I'm finding one that's right and others that look similar but are wrong.

Thanks for any help!

Best Answer

Your understanding of $(0+1)^*$ and $(0+1)(0+1)^*$ is correct.

$(0+1)0^*1^*$ is different from both. The regular expression $0^*1^*$ matches any string of the form $0^m1^n$, where $m,n\ge 0$. Thus, it matches every string that does not have a $1$ before a $0$. $(0+1)0^*1^*$ then matches any string that has at least one character, which may be $0$ or $1$, and after that does not have a $1$ that is followed by a $0$.

$(0^*1^*)^*$ is equivalent to $(0+1)^*$: it matches the empty string and any string of the form $$0^{m_1}1^{n_1}0^{m_2}1^{n_2}\ldots0^{m_r}1^{n_r}\;,$$ where the numbers $m_1,\ldots,m_r$ and $n_1,\ldots,n_r$ are non-negative integers, and it’s not hard to see that that covers every possible string of $0$s and $1$s. Thus, $(0+1)(0^*1^*)^*$ is equivalent to $(0+1)(0+1)^*$.

Related Question