CSC262 · 2080 · 5 marks
Write regular expression over {a,b} that represents a. Strings having exactly two a's and at least two b's. b. Strings having an even number of a's and each a followed by at least one b.
Write regular expression over {a,b} that represents a. Strings having exactly two a's and at least two b's. b. Strings having an even number of a's and each a followed by at least one b. [5]
Regular Expressions over {a, b}
Given data
- Alphabet: $\Sigma = {a, b}$
- Part (a): strings with exactly two a's AND at least two b's.
- Part (b): strings with an even number of a's, and each a immediately followed by at least one b.
Part (a): Exactly two a's and at least two b's
Structure. With exactly two a's, every string has the shape:
$$b^,a,b^,a,b^*$$
The three $b^*$ blocks (before, between, after) hold all the b's. The constraint is that the total number of b's across all three blocks is at least 2.
Deriving a compact expression. The plain shape $b^*ab^ab^$ allows 0 or 1 b's, which we must forbid. So we subtract those cases by forcing the b-count $\ge 2$. This is cleanly expressed by casing on where the "guaranteed" b's sit. A correct enumeration (each covers b-total $\ge 2$, union covers all):
$$R = b^{2+}ab^ab^ ;+; b^+ab^+ab^* ;+; b^+ab^*ab^+ ;+; b^ab^{2+}ab^ ;+; b^*ab^+ab^+ ;+; b^*ab^*ab^{2+}$$
where $b^{2+} = bbb^$ (at least two b's) and $b^+ = bb^$.
A shorter equivalent that is fully acceptable in an exam:
$$\boxed{R = b^*a,b^a,b^\ \text{restricted to total } b\text{'s} \ge 2}$$
expressed formally as the union above. In terms of "at least one block has $bb$, OR two blocks each have a $b$":
$$R = bbb^* a, b^* a, b^* + b^* a, bbb^* a, b^* + b^* a, b^* a, bbb^* + bb^* a, bb^* a, b^* + bb^* a, b^* a, bb^* + b^* a, bb^* a, bb^*$$
Check. Every accepted string must carry exactly two a's, so test $baab$: it has one b before the a's, the two a's adjacent, and one b after, giving a b-total of 2, and it matches the term $b^+ab^*ab^+$. The string $aab$ carries only one b and is correctly rejected. The string $bbaa$ has two b's ahead of the first a and matches the term $b^{2+}ab^ab^$.
Part (b): Even number of a's, each a followed by at least one b
Structure.
- Each a must be immediately followed by at least one b, so the basic unit is $ab^+$ (i.e. $abb^*$).
- The number of a's must be even, so units come in pairs: $(ab^+ab^+)$, repeated zero or more times.
- Extra b's may appear at the start (before any a).
Regular expression:
$$\boxed{R = b^,(ab^+,ab^+)^}$$
Verification:
| String | Match? | Reason |
|---|---|---|
| $\varepsilon$ | Yes | zero a's (even) |
| $bb$ | Yes | $b^*$ only |
| $abab$ | Yes | one pair $ab,ab$ |
| $abbabb$ | Yes | $abb,abb$ |
| $babbab$ | Yes | $b^*=b$, then $abb,ab$ |
| $ab$ | No | one a (odd) |
| $aab$ | No | first a not followed by b |
| $abba$ | No | last a not followed by b |
Final Answers
(a) Exactly two a's and at least two b's: $$R = b^{2+}ab^ab^ + b^+ab^+ab^* + b^+ab^*ab^+ + b^ab^{2+}ab^ + b^*ab^+ab^+ + b^*ab^*ab^{2+}$$
(b) Even number of a's, each a followed by at least one b: $$R = b^(ab^+ab^+)^$$
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.