Quiz 2

Regular Expressions & Pumping Lemma

988 words
5 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Regular Expressions & Pumping Lemma ## 🎯 Learning Objectives - Write regular expressions for regular languages - Convert between regex and DFA/NFA - Apply the pumping lemma to prove non-regularity - Use closure properties to construct regular languages * * * ## 1. Regular Expressions ### 1.1 Intuition Regular exp...

Regular Expressions & Pumping Lemma

🎯 Learning Objectives

  • Write regular expressions for regular languages
  • Convert between regex and DFA/NFA
  • Apply the pumping lemma to prove non-regularity
  • Use closure properties to construct regular languages

1. Regular Expressions

1.1 Intuition

Regular expressions describe patterns in text. They're equivalent to finite automata — every regex corresponds to some DFA/NFA, and vice versa.

1.2 Operations

OperationNotationExample
Union$R_1R_2$
ConcatenationR1R2R_1 \cdot R_2ab matches "ab"
Kleene starRR^*a* matches "", "a", "aa", ...
Grouping(R)(R)(ab)* matches "", "ab", "abab", ...

1.3 Examples

LanguageRegex
Strings starting with 'a'$a(a
Strings ending with '01'$(0
Strings with even number of 0s(10101)(1^*01^*01^*)^*
Strings of length exactly 3$(a
Strings with no consecutive 0s$(1

1.4 Algebraic Laws

LawExpression
Associativity$(R_1
Commutativity$R_1
Distributivity$R_1(R_2
Identity=ε∅^* = ε , εR=RεR = R , R=∅R = ∅
Idempotent$R
Star laws(R)=R(R^*)^* = R^* , =ε\emptyset^* = ε , ε=εε^* = ε

2. Equivalence of Regex and FA

2.1 Regex → NFA (Thompson's Construction)

Every regex can be systematically converted to an NFA: (Diagram)

2.2 DFA → Regex (State Elimination)

  1. Add a new start state with ε to old start
  2. Add a new accept state with ε from old accepts
  3. Eliminate states one by one, updating edge labels
  4. The remaining edge from start to accept is the regex

3. Pumping Lemma for Regular Languages

3.1 Intuition

The pumping lemma says: every sufficiently long string in a regular language can be "pumped" — a middle section can be repeated indefinitely while staying in the language. If a language violates this, it's not regular.

3.2 Formal Statement

For every regular language LL, there exists a constant pp (pumping length) such that for any string wLw \in L with wp|w| \geq p, we can write w=xyzw = xyz where:
  1. xyp|xy| \leq p (the pumpable part is near the start)
  2. y1|y| \geq 1 (y is non-empty)
  3. xyizLxy^iz \in L for all i0i \geq 0 (pumping works)

3.3 Proof Template

To prove LL is not regular:
pseudo
1. Assume L is regular (for contradiction)
2. Let p be the pumping length
3. Choose w ∈ L with |w| ≥ p (be strategic!)
4. For ALL ways to split w = xyz with |xy| ≤ p, |y| ≥ 1:
   Show there exists i such that xy^iz ∉ L
5. Contradiction → L is not regular

3.4 Worked Example 1: L={anbnn0}L = \{a^n b^n \mid n \geq 0\}

  1. Assume L is regular
  2. Let p be pumping length
  3. Choose w=apbpw = a^p b^p
  4. Since xyp|xy| \leq p, yy consists only of a's: y=aky = a^k for k1k \geq 1
  5. Pump i=2i=2: xy2z=ap+kbpxy^2z = a^{p+k}b^p
    • More a's than b's → not in L
  6. Contradiction → L is not regular ✓

3.5 Worked Example 2: L={www{0,1}}L = \{ww \mid w \in \{0,1\}^*\}

  1. Choose w=0p10p1w = 0^p 1 0^p 1
  2. xyp|xy| \leq p, so yy is within the first 0p0^p block
  3. y=0ky = 0^k, pump i=2i=2: xy2z=0p+k10p1xy^2z = 0^{p+k} 1 0^p 1
  4. The string before the middle is 0p+k10^{p+k}1 and after is 0p10^p1 — not equal
  5. Result: not regular ✓

3.6 When Pumping Lemma Fails

Some non-regular languages CAN be pumped! Example: L={anbmnm}L = \{a^n b^m \mid n \neq m\} is not regular, but the pumping lemma can't disprove it directly (you need more powerful tools like the Myhill-Nerode theorem).

4. Common Pitfalls

Pitfall 1: Pumping the wrong string

Mistake: Choosing a string that doesn't force a contradiction. Fix: Choose a string where every possible y (within first p characters) breaks the language.

Pitfall 2: Thinking pumping lemma can prove regularity

Mistake: "I pumped the string and it stayed in L, so L is regular." Correction: Pumping lemma gives a necessary condition for regularity, not a sufficient one. Some non-regular languages also satisfy pumping.

Pitfall 3: Forgetting to consider all splits

Mistake: Only checking one possible split of w=xyzw = xyz. Fix: Your proof must work for all valid splits (|xy| ≤ p, |y| ≥ 1).

5. 📝 Practice Questions

Q1: Write a regex for binary strings that do NOT contain "00".
Answer: (101)(0ε)(1|01)^*(0|ε) — strings where every 0 is followed by 1 (or at end). Q2: Prove L={0n2n0}L = \{0^{n^2} \mid n \geq 0\} is not regular.
Answer: Assume regular with pumping length p. Choose w=0p2w = 0^{p^2}. Then y=0ky = 0^k for 1kp1 \leq k \leq p. Pump i=2i=2: xy2z=0p2+kxy^2z = 0^{p^2+k}. But (p+1)2=p2+2p+1>p2+kp2+1(p+1)^2 = p^2 + 2p + 1 > p^2 + k \geq p^2 + 1, so p2<p2+k<(p+1)2p^2 < p^2+k < (p+1)^2. Thus p2+kp^2+k is not a perfect square → not in L. Contradiction. Q3: Convert regex (01)00(0|1)^*00 to an NFA.
Answer: NFA with states q0 (start), q1 (after first 0), q2 (accept, after "00"). Transitions: q0—0,1→q0; q0—0→q1; q1—0→q2; q2—0→q2; q2—1→q0. Q4: Is the language L = {w ∈ {0,1} | w has equal number of 0s and 1s} regular?*
Answer: No. Proof using pumping lemma: choose w=0p1pw = 0^p 1^p. y=0ky = 0^k. Pump i=2i=2: 0p+k1p0^{p+k}1^p has more 0s than 1s → not in L. Not regular. Q5: Show that regular languages are closed under intersection.
Answer: Given DFAs for L₁ and L₂, construct a product DFA where states are pairs (q₁, q₂), transitions are δ((q₁,q₂), a) = (δ₁(q₁,a), δ₂(q₂,a)), and accept states are (q₁,q₂) where q₁ ∈ F₁ AND q₂ ∈ F₂. This DFA accepts exactly L₁ ∩ L₂.

6. 🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.