1. a) What is a regular expression?
A regular expression (regex) is a formal way to describe a set of strings
using a specific pattern. It consists of symbols from a given alphabet
along with special operators such as concatenation, union (+), and
Kleene star (*). Regular expressions are used in computer science for
tasks such as pattern matching, text search, and lexical analysis in
compilers. They are equivalent in expressive power to finite automata,
meaning that for every regular expression, there exists a finite
automaton that accepts the same language, and vice versa.
Apply the regular expression for the following languages over {0,1}*:
i) The set of all strings such that the number of 0’s is odd:
Regular Expression: (1*01*01*)*01*
Explanation: This expression represents zero or more pairs of 0's
(ensuring even occurrences) followed by one extra 0 to make the count
odd, with any number of 1's interleaved.
ii) The set of all strings that contain exactly three 1’s:
Regular Expression: 0*10*10*10*
Explanation: We have exactly three occurrences of '1', each possibly
surrounded by any number of 0's, ensuring no more than three '1's
appear in the string.
iii) The set of all strings that contain '1101':
Regular Expression: (0+1)*1101(0+1)*
Explanation: This matches any sequence of 0's and 1's before and after
the specific substring '1101'.
1. b) NFA for 10(0+11)0*1
To construct an NFA for the expression 10(0+11)0*1:
1. Start state reads '1' then '0'.
2. From here, a branch (union) allows either '0' or '11'.
3. Then a loop over '0' for 0*.
4. Finally, transition on '1' to the accepting state.
The NFA will use ε-transitions to handle the union in (0+11) cleanly.
1. c) Relationship between FA and RE
Finite automata (DFA/NFA) and regular expressions are two
representations of the same class of languages: the regular languages.
Every regular expression has an equivalent finite automaton and vice
versa.
Converting DFA to regular expression (state elimination method):
1. Add a new start state with an ε-transition to the old start state.
2. Add a new accept state with ε-transitions from each old accept state.
3. Iteratively remove intermediate states, replacing paths through them
with equivalent regular expressions.
4. Continue until only the new start and new accept states remain, with
a single regular expression representing all accepted strings.
2. a) Pumping lemma for regular languages
The pumping lemma provides a property that all regular languages
satisfy. It states that for any regular language L, there exists an integer p
(pumping length) such that any string s in L with length at least p can be
written as s = xyz, where:
- |xy| ≤ p
- |y| > 0
- For all i ≥ 0, the string xy^i z is also in L.
The lemma is often used to prove that certain languages are not regular
by contradiction.
2. b) FA for 1*01(0+11)*
We can build the FA step-by-step:
1. Loop on '1' in the start state for 1*.
2. Read '0', then '1' in sequence.
3. Loop on either '0' or '11' for (0+11)*. The '11' sequence requires two
transitions through an intermediate state.
2. c) Equivalent RE for given DFA
Given the DFA with states A, B, C, we can apply Arden's theorem or state
elimination. Label the transitions with regular expressions and
systematically eliminate states until only start and accept remain. This
yields the final equivalent RE.
3. a) RE for given constraints
i) Strings without '01':
Regular Expression: 1*0*
This ensures that all 1's (if any) occur before all 0's, so '01' never
appears.
ii) Strings with at least one '0' and at least one '1':
Regular Expression: (0+1)*0(0+1)*1(0+1)* + (0+1)*1(0+1)*0(0+1)*
This covers both possible orders of first appearance of '0' and '1'.
3. b) Pumping lemma for L={a^n | n is prime}
Assume L is regular. By the pumping lemma, there exists p such that any
string a^n with n ≥ p can be split into xyz with |xy| ≤ p, |y| > 0, and xy^i
z ∈ L for all i ≥ 0.
Choose s = a^q with q > p prime. Pumping y changes the length from q
to q ± k|y|. These lengths will not all be prime, leading to a
contradiction. Hence L is not regular.
3. c) RE for given FA
From q0: loop on 'b' (b*), on 'a' go to q1.
From q1: loop on 'a' (a*), on 'b' go to q2.
From q2: loop on 'a' or 'b' ((a+b)*).
Regular Expression: b* a a* b (a+b)*