CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 2 · Regular Expressions & Regular Languages

Lesson 2.1.2 · Item 32 of 91

Writing regexes, identities and equivalence

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. How is ab* parsed?
    Stuck? Peek at 2.1.1 Regular expressions: syntax and meaning
    • Atoms ∅, ε, a; operators r + s (∪), rs (concat), r* (star); r⁺ = rr*.
    • Precedence: * > concatenation > +.
    • Kleene: regular ⇔ described by a regex.
    • Patterns: Σ* = (a+b)*; ends w = (a+b)*w; starts w = w(a+b)*; contains w = (a+b)*w(a+b)*; even length = ((a+b)(a+b))*; exactly two a’s = b*ab*ab*.

    Read lesson 2.1.1

  2. Minimal DFA for “no two consecutive a’s” has how many states?
    Stuck? Peek at 1.3.2 Partition and table-filling methods
    • Partition: remove unreachable; P₀ = {F, Q−F}; split by signatures w.r.t. previous partition; stop when stable.
    • Table filling: n(n−1)/2 cells; mark (F, non-F); propagate “successor pair marked ⇒ mark”; unmarked = equivalent.
    • Count by distinct futures: list situations with different accepted continuations, include dead / satisfied states.
    • Exactly k of a symbol: k + 2 states; at least k: k + 1; at most k: k + 2.

    Read lesson 1.3.2

  3. Is the minimal DFA unique?
    Stuck? Peek at 1.3.1 Unreachable, equivalent and distinguishable states
    • Step 0: delete unreachable states (BFS from q₀).
    • p ≡ q ⇔ ∀w: δ̂(p,w) ∈ F ⇔ δ̂(q,w) ∈ F. Distinguishable ⇔ some w separates.
    • ≡₀ = {F, Q−F}; ≡ₖ₊₁ splits by where symbols lead w.r.t. ≡ₖ; stop when stable.
    • Merge classes → minimal DFA, unique up to renaming.

    Read lesson 1.3.1

The Hook

Two programmers write different search patterns for “binary strings without two consecutive 1s”. Are their patterns the same? Testing a few strings proves nothing if they agree; a single disagreement proves they differ. GATE loves this: four regexes, “which ones are equivalent to (a + b)*?”. You need identities for simplification and a fast hunt for counterexamples.

Predict First

Are a*b* and (ab)* the same language? Are (a + b)* and (a*b*)* the same?

Core Concept

Prediction check

a*b* vs (ab)*: different — “aab” ∈ a*b* but ∉ (ab)*. (a+b)* vs (a*b*)*: same — every symbol is itself an a*b* block, and nothing outside Σ* can appear.

length(a+b)*(a*b*)*a*b*(ab)*
0εεεε
1a, ba, ba, b—
2aa ab ba bbaa ab ba bbaa ab bbab
Shortest-strings table: the highlighted cells are where the odd ones out show themselves.

Identities to know cold

IdentityWhy
∅ + r = r, ∅r = r∅ = ∅∅ is the zero of concatenation
εr = rε = rε is the identity
∅* = ε, ε* = εstar always gives ε
r + r = runion of a set with itself
(r*)* = r*, r*r* = r*repeating repeats adds nothing
rr* = r*r = r⁺, r* = ε + rr*one-or-more plus zero
(ε + r)* = r*, (ε + r)r* = r*ε adds nothing to a star
(rs)*r = r(sr)*shifting identity (rsrs…r = r srsr…)
(r + s)* = (r*s*)* = (r* + s*)* = r*(sr*)* = (r*s)*r*all mean “any mix of r’s and s’s”
r(s + t) = rs + rt, (s + t)r = sr + trconcatenation distributes over union
zero and identity
∅ + r = r · ∅r = ∅ · εr = r
star facts
∅* = ε* = ε · (r*)* = r* · ε + rr* = r*
any mix
(r+s)* = (r*s*)* = r*(sr*)* = (r*s)*r*
shifting
(rs)*r = r(sr)*
The identities worth knowing cold.

Famous non-identities

  • (r + s)* ≠ r* + s* (ab is in the left only).
  • (rs)* ≠ r*s* (aab separates (ab)* from a*b*).
  • (r + s)* ≠ r*s* (ba separates).
  • r*s* ≠ (rs)*; (r*s*)* ≠ r*s*.
  • rs ≠ sr in general.
pairseparating stringin which side?
(r+s)* vs r* + s*ableft only
(rs)* vs r*s*aabright only
(a+b)* vs a*b*baleft only
(a+b)⁺ vs (a+b)*εright only
Famous non-identities, each killed by one string.

Writing regexes from descriptions

  1. Find the skeleton: what must appear, in what order.
  2. Fill the gaps with what is allowed between them.
  3. Test on the shortest strings and on edge cases (ε, single symbols).
  • At least one a: (a+b)*a(a+b)* or b*a(a+b)*.
  • At most one a: b*(ε + a)b*.
  • No two consecutive a’s: (b + ab)*(ε + a) — every a is followed by b except possibly the last.
  • Number of a’s divisible by 3: (b*ab*ab*ab*)* + b* → simplifies to b*(ab*ab*ab*)*.
  • Starts and ends with different symbols: a(a+b)*b + b(a+b)*a.
babbabb*ab*ab*
Writing b*ab*ab* (exactly two a’s): the skeleton is the two a’s, the gaps hold only b’s.

Checking equality of two regexes

  1. Compare the shortest strings of each (ε? length 1? length 2?) — most GATE distractors die here.
  2. Look for a structural difference: a string one forces (like ab adjacency) the other forbids.
  3. If you suspect equality, transform one into the other with identities.
  4. The guaranteed method: build both minimal DFAs and compare (the minimal DFA is unique).
flowplay illustration
How to decide whether two regexes are equal.

Putting it together

  • Learn the identities: ∅* = ε, (r*)* = r*, (r+s)* has many equal forms, (rs)*r = r(sr)*.
  • (r+s)* ≠ r* + s* ≠ r*s*; (rs)* ≠ r*s*.
  • Compare shortest strings first; one counterexample settles inequality.

Visual

no two consecutive a's:   (b + ab)*(ε + a)
 strings ≤ 3:  ε  a  b  ab  ba  bb  aba  abb  bab  bba  bbb      (11 = 1+2+3+5 Fibonacci-like)
 check "aab": (b+ab)* can't make "aa"  → not in L ✓
No two consecutive a’s

States: A, B, D

Transitions: A>A: b; A>B: a; B>A: b; B>D: a; D>D: a,b

equivalence hunt:  r1 = (a+b)*      r2 = a* + b*
 length 2:  r1 gives aa ab ba bb       r2 gives aa bb
 counterexample "ab" → r1 ≠ r2

Worked Examples

Easy

Simplify ε + aa*.

Show the full solution

ε + aa* = ε + a⁺ = a*.

Medium

Write a regex for binary strings with an even number of 0s.

Show the full solution

Skeleton: 0s come in pairs, with 1s anywhere: 1*(01*01*)*. Check: ε ✓ (zero 0s), 1 ✓, 00 ✓, 0 ✗, 1010 ✓ (leading 1, then one block 0·1·0·ε).

GATE-level

Which are equal to (a + b)*? (i) (a*b*)* (ii) (a* + b*)* (iii) a*(ba*)* (iv) (ab + b)*a*

Show the full solution

(i) yes, (ii) yes (each single symbol is a block), (iii) yes — the identity (r+s)* = r*(sr*)* with r = a, s = b. (iv) no: blocks ab and b can never produce “aa” before a b: “aab” — (ab+b)* gives strings where every a is followed by b, and the trailing a* can only add a’s at the end; “aab” would need “aa” before the last b ⇒ not generated. Answer: (i), (ii), (iii).

GATE Traps

  • (a + b)* ≠ a* + b* ≠ a*b*.
  • ∅* = ε, not ∅. And r∅ = ∅ (a single ∅ factor kills a concatenation).
  • Always test ε: many wrong options differ from the right one only on ε (e.g. (a+b)⁺ vs (a+b)*).
  • Two regexes agreeing on ten strings proves nothing; one disagreement proves they differ.

Memory Hook

Equality hunting: “smallest strings first” — compare ε, then length 1, then length 2. Most traps fall in the first three rows.

Connect the Dots

Regexes and DFAs describe the same languages — but so far that is a claim. Next chapter proves it: converting a regex into an ε-NFA (Thompson), and an automaton back into a regex (state elimination, Arden).

Self-Test

  1. MSQ

    Which are equivalent to a*?

    1. (a*)*
    2. ε + aa*
    3. a*a*
    4. a⁺
  2. MSQ

    Which strings distinguish (ab)* from a*b* (belong to exactly one of them)?

    1. ε
    2. ab
    3. aab
    4. abab
  3. MCQ

    Regex for binary strings with at most one 0:

    1. 1*01*
    2. 1*(0 + ε)1*
    3. (1 + 0)*
    4. 1*0*1*
  4. NAT

    How many strings of length 4 are in L((b + ab)*(ε + a))?

    Write your answer before opening the key.

  5. MCQ

    (rs)*r equals:

    1. r(sr)*
    2. r*s*r
    3. (sr)*r
    4. rs*r

Rule Card

  • ∅ + r = r; ∅r = ∅; εr = r; ∅* = ε* = ε; (r*)* = r*; r*r* = r*; ε + rr* = r*; (rs)*r = r(sr)*.
  • (r+s)* = (r*s*)* = (r*+s*)* = r*(sr*)* = (r*s)*r*.
  • ≠: (r+s)* vs r*+s* vs r*s*; (rs)* vs r*s*.
  • No “aa”: (b+ab)*(ε+a). At most one a: b*(ε+a)b*. Even #0: 1*(01*01*)*.
  • Equality: compare shortest strings; counterexample kills; identities or minimal DFAs prove.

Concept Ledger

  • regex identities
  • shifting identity (rs)*r = r(sr)*
  • equivalent regexes
  • counterexample string

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. a(b*).
  2. 3 (last not a, last a, dead).
  3. Yes, up to renaming.

Self-test answers

  1. ABC — Standard identities.
    • D: a⁺ excludes ε.
  2. CD — aab ∈ a*b* only; abab ∈ (ab)* only.
    • A: In both.
    • B: In both.
  3. B — Zero or one 0 surrounded by 1s.
    • A: Exactly one 0.
    • C: All strings.
    • D: Allows 00.
  4. 8 — Strings of length 4 with no two consecutive a’s: Fibonacci count 1,2,3,5,8 → 8.
  5. A — Shifting identity.
    • B: Allows rrr.
    • C: Starts with s.
    • D: Allows ss.

Flashcards

  1. Is (a+b)* = a* + b*?
    Reveal answerNo — ab is only in the left side.
  2. (a+b)* equals which other forms?
    Reveal answer(a*b*)*, (a*+b*)*, a*(ba*)*, (a*b)*a*
  3. Regex: no two consecutive a’s over {a,b}?
    Reveal answer(b + ab)*(ε + a)
  4. ε + rr* = ?
    Reveal answerr*
  5. (rs)*r = ?
    Reveal answerr(sr)*

Exam shortcuts

  • Count strings of length n with no two consecutive a’s: Fibonacci — 1, 2, 3, 5, 8, 13 for n = 0..5.
  • Fast regex-equality check: write the strings of length 0, 1, 2 for each option. The odd one out shows itself.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.