CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 2 · Regular Expressions & Regular Languages

Lesson 2.4.2 · Item 41 of 91

Algorithms for regular languages

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

On this page

Recall warm-up

  1. Is a subset of a regular language always regular?
    Stuck? Peek at 2.4.1 Closure properties of regular languages
    • Closed: ∪ ∩ − complement, concat, *, ⁺, reversal, homomorphism, inverse homomorphism, quotient (by any language), prefix, suffix, substring, substitution, finite ∪/∩.
    • Not guaranteed: infinite ∪/∩, subset, superset.
    • Prove non-regular: L ∩ R non-regular (R regular) ⇒ L non-regular.

    Read lesson 2.4.1

  2. When is L(M) infinite, in graph terms?
    Stuck? Peek at 1.1.2 Extended transition function and regular languages
    • δ̂(q, ε) = q; δ̂(q, wa) = δ(δ̂(q, w), a); δ̂(q, xy) = δ̂(δ̂(q, x), y).
    • L(M) = { w : δ̂(q₀, w) ∈ F }. Regular ⇔ accepted by some DFA. All finite languages are regular.
    • From the graph: empty ⇔ no reachable final; Σ* ⇔ all reachable states final; infinite ⇔ cycle on a path start→final.
    • Prove correctness with state meanings + induction on |w|.

    Read lesson 1.1.2

  3. How many states does the minimal DFA have, and is it unique?
    Stuck? Peek at 1.3.3 Myhill–Nerode theorem
    • x ≡_L y ⇔ ∀z: xz ∈ L ⇔ yz ∈ L. Right-invariant equivalence.
    • L regular ⇔ finite index; index = #states of minimal DFA (incl. dead class).
    • Infinitely many pairwise distinguishable strings ⇒ not regular. aⁿbⁿ: strings aⁱ separated by bⁱ.

    Read lesson 1.3.3

The Hook

A compiler team wants a tool that checks their token patterns: “Does this pattern match anything at all? Does it match infinitely many strings? Are these two patterns the same?”. For regular languages every one of these questions has a procedure that always finishes with the right answer. Later phases will show that for stronger machines, some of these questions have no such procedure at all — so this list is worth knowing exactly.

Predict First

A DFA has n states. To check whether its language is infinite by testing strings, which lengths would you need to test?

Core Concept

Prediction check

Lengths n to 2n − 1. If any string of those lengths is accepted, its run repeats a state (pigeonhole), so there is a loop you can pump forever ⇒ infinite. Conversely, if L is infinite it contains such a string. (The graph method is faster: look for a cycle on a path from q₀ to F.)

012345678lengths < n: emptinessn … 2n − 1: finiteness
For a 4-state DFA: the two length windows worth testing.

Membership: is w ∈ L?

Run the DFA on w: |w| steps. (From an NFA: track the set of states. From a regex: convert first.)

Ends with 01

States: q0, q1, q2

Transitions: q0>q0: 1; q0>q1: 0; q1>q1: 0; q1>q2: 1; q2>q0: 1; q2>q1: 0

Membership = run the DFA: exactly |w| steps.

Emptiness: is L = ∅?

L(M) = ∅ iff no accepting state is reachable from q₀ — BFS on the diagram. Equivalent test: L ≠ ∅ iff M accepts some string of length < n.

BFS from q₀
accepting state reached?
no → L = ∅
yes → L ≠ ∅
Emptiness is reachability.

Finiteness: is L finite?

Remove states that are unreachable or cannot reach F (“useless”). L is infinite iff the remaining graph has a cycle. Or: test lengths n … 2n − 1.

cycle on a q₀ → F path
infinite
cycle only in the dead state
does not count
no usable cycle
finite
Finiteness: trim useless states, then look for a cycle.

Universality: is L = Σ*?

Complement the (complete) DFA and test emptiness. Equivalently: every reachable state is accepting.

complete DFA
swap F
emptiness test
empty ⇒ L = Σ*
Universality via complement.

Equivalence: L(M₁) = L(M₂)?

Two methods: (1) minimize both and compare (unique minimal DFA); (2) build the product for the symmetric difference (L₁ − L₂) ∪ (L₂ − L₁) and test emptiness. Containment L₁ ⊆ L₂: test L₁ ∩ L̅₂ = ∅.

Equivalence and containment reduce to emptiness of a product DFA.
Equivalence and containment reduce to emptiness of a product DFA.

Cost notes

From an NFA, emptiness and membership stay cheap; complement-based questions (universality, equivalence) may need the subset construction first, which can be exponential. GATE asks only whether a procedure exists, and for regular languages the answer is always yes.

questionfrom a DFAfrom an NFA
membershiplineartrack sets
emptinessreachabilityreachability
universality / equivalencecomplement + emptinesssubset construction first (can blow up)
Every question has an algorithm; only the cost differs.

Putting it together

  • Membership, emptiness, finiteness, universality, equivalence and containment all have always-terminating algorithms for regular languages.
  • Most reduce to reachability in a (product) DFA.
  • From NFAs, complement-based questions may need the subset construction first.

Visual

question            procedure (DFA with n states)                   always terminates?
 w ∈ L ?            run the DFA, |w| steps                          ✔
 L = ∅ ?            BFS: is some accepting state reachable?         ✔
 L finite ?         trim useless states, look for a cycle           ✔
 L = Σ* ?           complement, then emptiness                      ✔
 L₁ = L₂ ?          minimize & compare, or symmetric diff empty     ✔
 L₁ ⊆ L₂ ?          L₁ ∩ complement(L₂) = ∅                         ✔

Worked Examples

Easy

DFA: q0 ─a→ q1, q1 ─a→ q2 (accepting), q2 has no further moves except to a dead state D on both symbols; b from q0, q1 goes to D. Is L finite?

Show the full solution

Useful states: q0, q1, q2. No cycle among them (D is useless). Finite: L = {aa}.

Medium

How do you check whether L(r₁) ⊆ L(r₂) for regexes r₁, r₂?

Show the full solution

Convert both to DFAs, complement the second, take the product with the first, test emptiness. If empty, L₁ ⊆ L₂.

GATE-level

Which of these questions about regular languages (given by DFAs) can be answered by an always-terminating algorithm? Emptiness, finiteness, equivalence, universality, “is L₁ ∩ L₂ infinite?”

Show the full solution

All of them. The last: build the product DFA, then run the finiteness test.

GATE Traps

  • Finiteness: a cycle only matters if it is reachable from q₀ AND can reach F (a self-loop on the dead state means nothing).
  • Universality needs the complement — complete the DFA first.
  • For regular languages, every standard question has an always-terminating algorithm; save the “no algorithm exists” answers for later phases.

Memory Hook

Regular languages are glass boxes: you can see the whole machine, so every question about it can be answered by looking.

Connect the Dots

We can answer questions about a given regular language. But how do we prove a language is not regular when no DFA is given? Myhill–Nerode is one tool; the pumping lemma — built on pigeonhole — is the one GATE expects. Next.

Self-Test

  1. MCQ

    L(M) is empty iff:

    1. M has no accepting state
    2. no accepting state is reachable from q₀
    3. M has a dead state
    4. M has a cycle
  2. NAT

    For a DFA with 6 states, testing lengths from n to 2n−1 means checking strings of lengths 6 to what?

    Write your answer before opening the key.

  3. MSQ

    For DFAs M₁, M₂, which are answerable by an always-terminating algorithm?

    1. L(M₁) = L(M₂)?
    2. L(M₁) ⊆ L(M₂)?
    3. Is L(M₁) finite?
    4. Is L(M₁) ∩ L(M₂) = ∅?
  4. MCQ

    To test universality (L = Σ*) of a DFA:

    1. test if q₀ ∈ F
    2. complement and test emptiness
    3. test for a cycle
    4. minimize and count states

Rule Card

  • Membership: run. Emptiness: reachable F? Finiteness: trim + cycle (or lengths n..2n−1). Universality: complement + emptiness. Equivalence: minimize & compare, or symmetric difference empty. Containment: L₁ ∩ L̅₂ = ∅.
  • All these have always-terminating algorithms for regular languages.

Concept Ledger

  • emptiness test
  • finiteness test
  • universality test
  • equivalence test
  • containment test
  • useless state (cannot reach F)
  • always-terminating algorithm

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. No.
  2. When a cycle lies on a path from q₀ to an accepting state.
  3. As many as Myhill–Nerode classes; unique up to renaming.

Self-test answers

  1. B — Reachability.
    • A: Sufficient, not necessary (an unreachable accepting state).
    • C: Irrelevant.
    • D: Irrelevant.
  2. 11 — 2n − 1 = 11.
  3. ABCD — All via products, complements, reachability.
  4. B — Σ* − L = ∅.
    • A: Only about ε.
    • C: Finiteness, not universality.
    • D: Only indirectly (1 accepting state).

Flashcards

  1. How to test L(M) = ∅?
    Reveal answerCheck whether any accepting state is reachable from q₀.
  2. How to test L(M) finite?
    Reveal answerRemove useless states; infinite iff a cycle remains.
  3. How to test L₁ = L₂ for DFAs?
    Reveal answerMinimize and compare, or check the symmetric difference is empty.

Ready for the next idea?

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