CSE322Semester 5 • 4 CreditsCore Subjects

Theory of Computation

A comprehensive course on Formal Languages and Automata Theory covering finite automata (DFA/NFA/e-NFA), regular expressions and regular languages, context-free grammars and languages, pushdown automata, parsing, Turing machines, decidability, and computational complexity.

6 Units • Notes • Model Answers • MCQs
Select Course Unit (6 Units)
Active: Unit 1
Unit 1:Unit 1: FINITE AUTOMATA
26•High-Yield Unit Guide

Unit 1: FINITE AUTOMATA

Unit 1 Exam Focus & Core Takeaways

Review the core definitions, standard architectures, and syntax patterns below. Pay special attention to sections highlighted with bullet points for your mid-term and end-term subjective questions.

Unit 1: Finite Automata and Regular Languages

1. Mathematical Foundations: Alphabets, Strings, and Languages

The theory of computation investigates the fundamental capabilities and limitations of abstract computing devices. Before formalizing abstract machines, we must establish rigorous mathematical definitions for symbols, sequences, and computational problems.

1.1 Core Mathematical Definitions

  • Symbol: An indivisible, atomic character, token, or digit (e.g., 0,1,a,b0, 1, a, b).
  • Alphabet (Σ\Sigma): A finite, non-empty set of symbols.
    • Binary Alphabet: Σ={0,1}\Sigma = \{0, 1\}
    • Lowercase English Alphabet: Σ={a,b,c,…,z}\Sigma = \{a, b, c, \dots, z\}
    • Decimal Alphabet: Σ={0,1,2,…,9}\Sigma = \{0, 1, 2, \dots, 9\}
  • String (Word, ww): A finite sequence of symbols chosen from an alphabet Σ\Sigma.
    • Example: If Σ={a,b}\Sigma = \{a, b\}, then w=aababw = aabab is a string over Σ\Sigma.
  • Length of a String (∣w∣|w|): The number of symbol positions in ww.
    • If w=aababw = aabab, then ∣w∣=5|w| = 5.
    • The number of occurrences of a specific symbol aa in ww is denoted ∣w∣a|w|_a. Here, ∣aabab∣a=3|aabab|_a = 3.
  • Empty String (ϵ\epsilon or λ\lambda): The unique string containing zero symbols. Its length is zero:
    ∣ϵ∣=0|\epsilon| = 0
    For any string ww, the empty string acts as the identity element under concatenation:
    wϵ=ϵw=ww\epsilon = \epsilon w = w
  • Powers of an Alphabet (Σk\Sigma^k): The set of all strings of exact length kk formed from symbols of Σ\Sigma.
    • Σ0={ϵ}\Sigma^0 = \{\epsilon\}
    • If Σ={0,1}\Sigma = \{0, 1\}, then Σ1={0,1}\Sigma^1 = \{0, 1\}, Σ2={00,01,10,11}\Sigma^2 = \{00, 01, 10, 11\}, and ∣Σk∣=∣Σ∣k|\Sigma^k| = |\Sigma|^k.
  • Kleene Star (Σ∗\Sigma^*): The set of all possible finite-length strings over Σ\Sigma, including the empty string:
    Σ∗=⋃k=0∞Σk=Σ0∪Σ1∪Σ2∪…\Sigma^* = \bigcup_{k=0}^{\infty} \Sigma^k = \Sigma^0 \cup \Sigma^1 \cup \Sigma^2 \cup \dots
    Σ∗\Sigma^* is an countably infinite set for any non-empty alphabet Σ\Sigma.
  • Positive Closure (Σ+\Sigma^+): The set of all non-empty strings over Σ\Sigma:
    Σ+=⋃k=1∞Σk=Σ∗∖{ϵ}\Sigma^+ = \bigcup_{k=1}^{\infty} \Sigma^k = \Sigma^* \setminus \{\epsilon\}
  • Language (LL): A mathematical language over an alphabet Σ\Sigma is any subset of Σ∗\Sigma^*:
    L⊆Σ∗L \subseteq \Sigma^*
    • Finite Language: L1={0,01,001}L_1 = \{0, 01, 001\}
    • Infinite Language: L2={0n1n∣n≥0}={ϵ,01,0011,000111,… }L_2 = \{0^n 1^n \mid n \ge 0\} = \{\epsilon, 01, 0011, 000111, \dots\}
    • Empty Language: ∅={}\emptyset = \{\} (contains zero strings; note that ∅≠{ϵ}\emptyset \ne \{\epsilon\}).

1.2 Operations on Languages

Given two languages L1,L2⊆Σ∗L_1, L_2 \subseteq \Sigma^*:

  1. Union: L1∪L2={w∣w∈L1 or w∈L2}L_1 \cup L_2 = \{w \mid w \in L_1 \text{ or } w \in L_2\}
  2. Intersection: L1∩L2={w∣w∈L1 and w∈L2}L_1 \cap L_2 = \{w \mid w \in L_1 \text{ and } w \in L_2\}
  3. Concatenation: L1L2={uv∣u∈L1,v∈L2}L_1 L_2 = \{uv \mid u \in L_1, v \in L_2\}
  4. Complementation: L‾=Σ∗∖L\overline{L} = \Sigma^* \setminus L
  5. Reversal: LR={wR∣w∈L}L^R = \{w^R \mid w \in L\}, where (a1a2…an)R=an…a2a1(a_1 a_2 \dots a_n)^R = a_n \dots a_2 a_1 and ϵR=ϵ\epsilon^R = \epsilon.
  6. Kleene Star of a Language: L∗=⋃i=0∞LiL^* = \bigcup_{i=0}^{\infty} L^i, where L0={ϵ}L^0 = \{\epsilon\} and Li=LLi−1L^i = L L^{i-1}.

2. Overview and Structural Components of Finite Automata

A Finite Automaton (FA) is the simplest formal computational model. It possesses a strictly bounded, finite internal memory represented by discrete states. An FA inspects a sequence of input symbols sequentially from left to right, transitioning between internal states. Because its memory cannot scale with input length, a finite automaton cannot perform arbitrary unbounded counting.

2.1 Hardware Abstraction Components

  1. Input Tape: A linear tape divided into cells, each storing a single symbol a∈Σa \in \Sigma. The string terminates with an end-of-input marker.
  2. Read Head: Inspects one cell at a time and advances strictly from left to right (unidirectional, read-only). It cannot write to the tape or move backward.
  3. Finite Control: A set of internal states and transition logic that reads the current symbol under the head, looks up the transition function, and updates the automaton's internal state.

3. Deterministic Finite Automata (DFA)

A Deterministic Finite Automaton (DFA) is a machine in which every state has exactly one deterministic transition for each symbol of the alphabet.

3.1 Formal 5-Tuple Definition

Formally, a DFA is defined as a 5-tuple:
M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F)

Where:

  • QQ: A finite, non-empty set of internal states.
  • Σ\Sigma: A finite input alphabet.
  • δ\delta: The transition function mapping a state and an input symbol to exactly one next state:
    δ:Q×Σ→Q\delta: Q \times \Sigma \to Q
  • q0q_0: The initial (start) state, where q0∈Qq_0 \in Q.
  • FF: The set of final (accepting) states, where F⊆QF \subseteq Q.

3.2 Extended Transition Function (δ^\hat{\delta})

To describe how a DFA processes entire strings rather than individual symbols, we define the extended transition function δ^:Q×Σ∗→Q\hat{\delta}: Q \times \Sigma^* \to Q recursively:

  1. Basis: For the empty string ϵ\epsilon:
    δ^(q,ϵ)=q\hat{\delta}(q, \epsilon) = q
  2. Induction: For any string w=xaw = xa where x∈Σ∗x \in \Sigma^* and a∈Σa \in \Sigma:
    δ^(q,xa)=δ(δ^(q,x),a)\hat{\delta}(q, xa) = \delta(\hat{\delta}(q, x), a)

3.3 Language Acceptance

A string w∈Σ∗w \in \Sigma^* is accepted by a DFA MM if and only if processing ww from the start state ends in an accepting state:
w∈L(M)  ⟺  δ^(q0,w)∈Fw \in L(M) \iff \hat{\delta}(q_0, w) \in F
The language recognized by MM is:
L(M)={w∈Σ∗∣δ^(q0,w)∈F}L(M) = \{w \in \Sigma^* \mid \hat{\delta}(q_0, w) \in F\}

CODE
+-----------------------------------------------------------------------------------+
|               DFA State Transition Diagram: Strings Ending in "01"                |
|                                                                                   |
|            +--------- 1 ---------+                                                |
|            |                     |                                                |
|            v     0         1     |                                                |
|  --> (( q0 )) -----> ( q1 ) -----> ((( q2 )))                                     |
|        ^   ^           |             |                                            |
|        |   +--- 0 -----+             |                                            |
|        |                             |                                            |
|        +-------------- 1 ------------+                                            |
|                                                                                   |
|  Alphabet: {0, 1} | Start: q0 | Accepting: {q2}                                   |
+-----------------------------------------------------------------------------------+

4. Non-Deterministic Finite Automata (NFA and ϵ\epsilon-NFA)

In a Non-Deterministic Finite Automaton (NFA), the machine can transition to zero, one, or multiple next states for a given state and input symbol. Conceptually, an NFA explores all possible execution paths simultaneously in parallel (or guesses the correct path non-deterministically).

4.1 Formal Definition of an NFA

An NFA is defined as a 5-tuple:
M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F)

Where:

  • δ\delta is the transition function mapping a state and a symbol to a subset of states (an element of the powerset 2Q2^Q):
    δ:Q×Σ→2Q\delta: Q \times \Sigma \to 2^Q
  • Acceptance condition: A string ww is accepted if at least one computation path terminates in an accepting state:
    w∈L(M)  ⟺  δ^(q0,w)∩F≠∅w \in L(M) \iff \hat{\delta}(q_0, w) \cap F \ne \emptyset

4.2 NFA with ϵ\epsilon-Transitions (ϵ\epsilon-NFA)

An ϵ\epsilon-NFA allows transitions between states spontaneously without consuming any input symbol:
δ:Q×(Σ∪{ϵ})→2Q\delta: Q \times (\Sigma \cup \{\epsilon\}) \to 2^Q

ϵ\epsilon-Closure Definition

For any state q∈Qq \in Q, the ϵ\epsilon-closure of qq, denoted ϵ-CLOSURE(q)\epsilon\text{-CLOSURE}(q), is the set of all states reachable from qq by following zero or more ϵ\epsilon-transitions:

  1. q∈ϵ-CLOSURE(q)q \in \epsilon\text{-CLOSURE}(q) (reflexive basis).
  2. If p∈ϵ-CLOSURE(q)p \in \epsilon\text{-CLOSURE}(q) and r∈δ(p,ϵ)r \in \delta(p, \epsilon), then r∈ϵ-CLOSURE(q)r \in \epsilon\text{-CLOSURE}(q).

For a set of states S⊆QS \subseteq Q:
ϵ-CLOSURE(S)=⋃q∈Sϵ-CLOSURE(q)\epsilon\text{-CLOSURE}(S) = \bigcup_{q \in S} \epsilon\text{-CLOSURE}(q)


5. Equivalence of DFA and NFA: The Subset Construction

A foundational theorem in automata theory (proven by Michael O. Rabin and Dana Scott, 1959) establishes that NFAs, ϵ\epsilon-NFAs, and DFAs have identical computational expressive power. Every language accepted by an NFA can be accepted by an equivalent DFA.

5.1 The Subset Construction (Powerset Construction) Algorithm

Given an NFA M=(Q,Σ,δN,q0,FN)M = (Q, \Sigma, \delta_N, q_0, F_N), we construct an equivalent DFA M′=(Q′,Σ,δD,q0′,FD)M' = (Q', \Sigma, \delta_D, q_0', F_D):

  1. States of DFA (Q′Q'): Each state in the DFA represents a subset of states of the NFA:
    Q′⊆2QQ' \subseteq 2^Q
    If the NFA has n=∣Q∣n = |Q| states, the DFA has at most 2n2^n states.
  2. Start State (q0′q_0'):
    q0′=ϵ-CLOSURE(q0)q_0' = \epsilon\text{-CLOSURE}(q_0)
  3. Transition Function (δD\delta_D): For any DFA state S∈Q′S \in Q' and input symbol a∈Σa \in \Sigma:
    δD(S,a)=ϵ-CLOSURE(⋃q∈SδN(q,a))\delta_D(S, a) = \epsilon\text{-CLOSURE}\left( \bigcup_{q \in S} \delta_N(q, a) \right)
  4. Accepting States (FDF_D): Any DFA state SS that contains at least one accepting state of the original NFA is an accepting state in the DFA:
    FD={S∈Q′∣S∩FN≠∅}F_D = \{S \in Q' \mid S \cap F_N \ne \emptyset\}

5.2 Worst-Case State Complexity

For the language LkL_k of strings over {0,1}\{0, 1\} where the kk-th symbol from the end is a '1':

  • An NFA requires only k+1k + 1 states.
  • The minimal equivalent DFA strictly requires 2k2^k states.
    This proves that non-determinism provides exponential succinctness in state representation, even though computational language power is identical.

6. Regular Languages and Closure Properties

A language L⊆Σ∗L \subseteq \Sigma^* is defined to be Regular if and only if there exists a finite automaton (DFA, NFA, or ϵ\epsilon-NFA) such that L=L(M)L = L(M).

6.1 Closure Properties of Regular Languages

Regular languages form a Boolean algebra closed under the following fundamental operations:

OperationDefinitionConstruction Method
Union (L1∪L2L_1 \cup L_2)Strings in L1L_1 or L2L_2Product Automaton (Cartesian Product) or ϵ\epsilon-transitions joining start states.
Intersection (L1∩L2L_1 \cap L_2)Strings in both L1L_1 and L2L_2Cross-Product DFA construction where F=F1×F2F = F_1 \times F_2.
Concatenation (L1L2L_1 L_2)Strings formed by xyxy where x∈L1,y∈L2x \in L_1, y \in L_2Connect final states of M1M_1 to start state of M2M_2 via ϵ\epsilon-transitions.
Complement (L‾\overline{L})Σ∗∖L\Sigma^* \setminus LIn a complete DFA, swap accepting and non-accepting states: F′=Q∖FF' = Q \setminus F.
Kleene Star (L∗L^*)Zero or more concatenationsAdd new start/accept state with ϵ\epsilon-transitions to/from existing states.
Reversal (LRL^R)Reversed stringsReverse all directed transition edges; swap start state and accepting states.
Difference (L1∖L2L_1 \setminus L_2)L1∩L2‾L_1 \cap \overline{L_2}Combine complement and intersection constructions.

7. Finite Automata with Output: Mealy and Moore Machines

Standard DFAs and NFAs act as Language Recognizers (yielding a binary accept/reject decision). In practical hardware design (sequential circuits, parity checkers, traffic light controllers), finite state machines must generate an output sequence associated with states or transitions.

7.1 Moore Machines

In a Moore Machine, the output depends exclusively on the current state:
M=(Q,Σ,Δ,δ,λ,q0)M = (Q, \Sigma, \Delta, \delta, \lambda, q_0)
Where:

  • Δ\Delta: Finite output alphabet.
  • δ:Q×Σ→Q\delta: Q \times \Sigma \to Q: State transition function.
  • λ:Q→Δ\lambda: Q \to \Delta: Output function mapping each state to an output symbol.
  • Output Length: For an input string of length nn, a Moore machine produces an output sequence of length n+1n + 1, because it emits an output symbol for the initial state before processing any input.

7.2 Mealy Machines

In a Mealy Machine, the output depends both on the current state and the current input symbol:
M=(Q,Σ,Δ,δ,λ,q0)M = (Q, \Sigma, \Delta, \delta, \lambda, q_0)
Where:

  • λ:Q×Σ→Δ\lambda: Q \times \Sigma \to \Delta: Output function mapping a state-symbol transition to an output symbol.
  • Output Length: For an input string of length nn, a Mealy machine produces an output sequence of length nn.

7.3 Equivalence and Conversion Between Moore and Mealy Machines

Moore and Mealy machines are computationally equivalent: any language translation performed by a Moore machine can be performed by a Mealy machine, and vice-versa.

  • Converting Moore to Mealy: Retain states and transitions; set the output of each transition edge entering state qq to the output assigned to state qq in the Moore machine:
    λMealy(p,a)=λMoore(δ(p,a))\lambda_{\text{Mealy}}(p, a) = \lambda_{\text{Moore}}(\delta(p, a))
  • Converting Mealy to Moore: If a state qq in a Mealy machine receives transitions emitting different outputs, qq is split into multiple duplicate states qxq_x, each dedicated to emitting a single specific output x∈Δx \in \Delta.

8. Minimization of Finite Automata

Given a regular language LL, there are infinitely many distinct DFAs that recognize LL. However, by the Myhill-Nerode Theorem, there exists a unique minimal DFA (up to state renaming/isomorphism) having the fewest possible states.

Minimizing a DFA reduces hardware circuit complexity and memory footprint in lexical analyzers (e.g., Lex/Flex).

8.1 State Equivalence Criteria

Two states p,q∈Qp, q \in Q are said to be equivalent (p∼qp \sim q) if for all possible input strings w∈Σ∗w \in \Sigma^*, both states lead either to an accepting state or both lead to a non-accepting state:
p∼q  ⟺  ∀w∈Σ∗,(δ^(p,w)∈F  ⟺  δ^(q,w)∈F)p \sim q \iff \forall w \in \Sigma^*, (\hat{\delta}(p, w) \in F \iff \hat{\delta}(q, w) \in F)
If there exists even one string ww such that δ^(p,w)∈F\hat{\delta}(p, w) \in F while δ^(q,w)∉F\hat{\delta}(q, w) \notin F, the states are distinguishable (p≁qp \not\sim q).

8.2 The Table-Filling Algorithm (Myhill-Nerode Minimization)

  1. Pre-processing (Eliminate Unreachable States): Perform a graph traversal (BFS/DFS) starting from q0q_0. Discard all unreachable states.
  2. Step 1 (Base Case - Distinguish Accepting from Non-Accepting):
    Create a triangular table of all unordered state pairs (p,q)(p, q). Mark pair (p,q)(p, q) if one state is in FF and the other is not in FF:
    p∈F and q∉F  ⟹  Mark (p,q)p \in F \text{ and } q \notin F \implies \text{Mark } (p, q)
  3. Step 2 (Inductive Marking):
    Repeat until no new marks can be placed:
    For every unmarked pair (p,q)(p, q) and every symbol a∈Σa \in \Sigma:
    Compute p′=δ(p,a)p' = \delta(p, a) and q′=δ(q,a)q' = \delta(q, a).
    If the pair (p′,q′)(p', q') is already marked, mark (p,q)(p, q).
  4. Step 3 (State Partitioning and Merging):
    Any state pairs remaining unmarked at the end of the algorithm are equivalent (p∼qp \sim q). Merge equivalent pairs into unified composite states to construct the minimal DFA.