Quiz 2

Finite Automata — DFA, NFA, ε-NFA

1044 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

# Finite Automata — DFA, NFA, ε-NFA ## 🎯 Learning Objectives - Design DFAs and NFAs for given languages - Convert NFA to DFA using subset construction - Prove language properties using closure operations - Trace computation of automata on input strings * * * ## 1. Deterministic Finite Automata (DFA) ### 1.1 Intuiti...

Finite Automata — DFA, NFA, ε-NFA

🎯 Learning Objectives

  • Design DFAs and NFAs for given languages
  • Convert NFA to DFA using subset construction
  • Prove language properties using closure operations
  • Trace computation of automata on input strings

1. Deterministic Finite Automata (DFA)

1.1 Intuition

A DFA is the simplest computational model — like a vending machine that changes state based on coin inputs and eventually decides "yes" or "no" (accept/reject). The key property: from every state, for every input symbol, there is exactly one transition.

1.2 Formal Definition

A DFA is a 5-tuple M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F):
  • Q: Finite set of states
  • Σ\Sigma: Finite alphabet of input symbols
  • δ:Q×ΣQ\delta : Q \times \Sigma \to Q: Transition function (deterministic!)
  • q0Qq_0 \in Q: Start state
  • FQF \subseteq Q: Accepting (final) states

1.3 DFA Example: Even Number of 1s

Language: L={w{0,1}wL = \{w \in \{0,1\}^* \mid w has an even number of 1s}\} (Diagram) Tracing string "1011":
StepStateInputNext State
0q0 (start)
1q01q1
2q10q1
3q11q0
4q01q1
5q1Reject (q1 not accepting)
Result: "1011" has three 1s (odd) → rejected ✓

1.4 DFA Example: Strings Ending with "01"

(Diagram) Tracing "0101":
StepStateInputNext State
0q0
1q00q1
2q11q0
3q00q1
4q11q0
5q0Reject (doesn't end with 01)
For "00101": q0→q1(0)→q2(0? Actually q1→q2 on 0)→q0(1)→q1(0)→q2(1)→Accept

2. Non-Deterministic Finite Automata (NFA)

2.1 Intuition

An NFA is like a DFA that can guess the right path. From a state on a given input, there can be zero, one, or multiple possible next states. A string is accepted if any path leads to an accepting state.

2.2 Formal Definition

M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) where δ:Q×ΣεP(Q)\delta : Q \times \Sigma_\varepsilon \to \mathcal{P}(Q) The transition function returns a set of possible next states.

2.3 NFA Example: Strings Ending with "01"

(Diagram) Tracing "101":
StepCurrent StatesInputNext States
0{q0}
1{q0}1δ(q0,1) = {q0}
2{q0}0δ(q0,0) = {q0, q1}
3{q0, q1}1δ(q0,1)∪δ(q1,1) = {q0} ∪ {q2} = {q0, q2}
4q2 ∈ accepting set → Accept

2.4 NFA to DFA Conversion (Subset Construction)

Algorithm:
  • Each DFA state = a set of NFA states
  • Start state = ε-closure of NFA start
  • Transition: DFA δ(S, a) = ε-closure(∪{q∈S} δ{NFA}(q, a))
  • Accept if any NFA accepting state is in the set NFA → DFA Example: For the NFA above (strings ending with "01"):
DFA StateNFA SetOn 0On 1
A{q0}δ(q0,0)={q0,q1}→Bδ(q0,1)={q0}→A
B{q0,q1}δ(q0,0)∪δ(q1,0)={q0,q1}→Bδ(q0,1)∪δ(q1,1)={q0,q2}→C
C (accept){q0,q2}δ(q0,0)∪δ(q2,0)={q0,q1}→Bδ(q0,1)∪δ(q2,1)={q0}→A
The resulting DFA is exactly the one we designed earlier (with states A=q0, B=q1, C=q2).

3. ε-NFA (NFA with ε-Transitions)

3.1 Intuition

An ε-NFA can move between states without consuming any input (ε transitions). This is useful for combining automata (union, concatenation, Kleene star).

3.2 ε-Closure

The ε-closure of a state q is the set of all states reachable from q via ε-transitions (including q itself). ε-NFA Example: Accept strings with at least one 'a' (Diagram) ε-closure(q0) = {q0, q1} (q0 → ε → q1)

4. Closure Properties

Regular languages are closed under:
OperationDescriptionConstruction
UnionL1L2L_1 \cup L_2ε-NFA: start → ε → NFA₁, start → ε → NFA₂
ConcatenationL1L2L_1 \cdot L_2ε from accepting state of NFA₁ to start of NFA₂
Kleene StarLL^*ε from accepting back to start
ComplementL\overline{L}Swap accepting/non-accepting states in DFA
IntersectionL1L2L_1 \cap L_2Product construction (pair of states)
ReversalLRL^RReverse arrows, swap start/accept

5. Common Pitfalls

Pitfall 1: DFA must have a transition for every symbol

Mistake: Creating a DFA where some states don't have transitions for some inputs. Fix: Add a "dead state" (non-accepting, self-loops) for missing transitions.

Pitfall 2: Confusing NFA acceptance

Mistake: Thinking NFA rejects if any path rejects. Correction: NFA accepts if at least one path accepts. All paths must be explored (or simulated via subset construction).

Pitfall 3: Forgetting ε-closure in NFA→DFA

Mistake: Using raw δ_NFA results without taking ε-closure. Fix: Always compute ε-closure after each transition during subset construction.

6. 📐 Key Formulas / Concepts

ConceptFormula
DFA transitionδ(q,a)=q\delta(q, a) = q' (single state)
NFA transitionδ(q,a)={q1,q2,...}\delta(q, a) = \{q_1, q_2, ...\} (set)
Extended transitionδ^(q,w)\hat{\delta}(q, w) = state after reading entire string w
DFA states from NFASDFA=ε-closure(SNFA)S_{DFA} = \varepsilon\text{-closure}(S_{NFA})
Union of DFAsL1L2L_1 \cup L_2 via product construction
Complement of DFAAccepting ↔ Non-accepting states swapped

7. 📝 Practice Questions

Q1: Design a DFA for the language L = {w ∈ {0,1} | w contains "00" as substring}.*
Answer: States: q0 (no 0 yet), q1 (saw first 0), q2 (saw "00" → accept), q3 (dead).
Transitions: q0--0→q1, q0--1→q0; q1--0→q2, q1--1→q0; q2--0→q2, q2--1→q2; q3--0,1→q3. Q2: Convert this NFA to DFA: q0--a,b→q0, q0--a→q1, q1 --ε→ q2 (accept).
Answer:
  • Start: ε-closure(q0) = {q0}
  • On a: ε-closure(δ(q0,a)) = ε-closure({q0,q1}) = {q0,q1,q2} = State B (accept)
  • On b: ε-closure(δ(q0,b)) = ε-closure({q0}) = {q0} = State A
DFA: A--a→B, A--b→A, B--a→B, B--b→B Q3: Prove that the language L = {a^n b^n | n ≥ 0} is NOT regular.
Answer: Use the pumping lemma. Assume L is regular with pumping length p. Choose w = a^p b^p. By pumping lemma, w = xyz, |xy| ≤ p, |y| ≥ 1, and xy^iz ∈ L for all i. Since |xy| ≤ p, y must consist only of a's. Pumping y (i=2) gives a^{p+|y|} b^p, which has more a's than b's → not in L. Contradiction. Hence L is not regular. Q4: Design an NFA for the set of all binary strings that contain "00" AND end with "11".
Answer: Use product construction or NFA with branching: create an NFA that tracks both "saw 00" and "ends with 11" in parallel. Accept if both conditions met at end. Q5: What is the difference between Moore and Mealy machines?
Answer: Moore machines produce output based on the current state (output associated with state). Mealy machines produce output based on transitions (output associated with edge). Any Mealy machine can be converted to a Moore machine and vice versa.

8. 🔗 Cross-References

  • Week 2 - Regular Expressions: Equivalence with DFA/NFA
  • Week 3 - DFA Minimization: Merging indistinguishable states
  • BSCS4032 (Compiler Design): Lexical analysis uses DFA Join Discord NextNFA to DFA
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.