2 Introduction To Finite Automata

Theory of Computation ยท Unit 2 ยท 8 hrs

Introduction to Finite Automata

Exam-focused notes for Introduction to Finite Automata (Theory of Computation, CSC262): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.

Introduction to Finite Automata in short

This unit introduces the finite automaton, the simplest machine model in the theory of computation, in its deterministic, non-deterministic, and epsilon-transition forms, proves that all three accept exactly the same class of languages, and closes with the two standard finite state machines that produce output.

Introduction to Finite Automata: notes

Introduction to Finite Automata

A finite automaton is the simplest abstract machine for recognizing patterns in strings. It reads an input string symbol by symbol and, depending only on the symbol read and the state it is currently in, moves to a new state; once the entire input has been read, the string is accepted if the machine ends in a designated final state and rejected otherwise. Because a finite automaton carries no auxiliary memory beyond its current state, it is the weakest of the machine models studied in this subject, yet it is powerful enough to specify lexical tokens, validate formats, and drive network protocols.

Introduction of Finite State Machine

A finite state machine (FSM) is defined formally by five components, written as the 5-tuple $$M = (Q, \Sigma, \delta, q_0, F),$$ where $Q$ is a finite non-empty set of states, $\Sigma$ is the input alphabet, $\delta$ is the transition function, $q_0 \in Q$ is the start state, and $F \subseteq Q$ is the set of final (accepting) states. A machine may have only one start state but any number of final states, including none. Finite state machines are classified along two axes: whether the transition function is deterministic (DFA) or non-deterministic (NFA), and whether the machine produces output at every step (Mealy and Moore machines) or simply accepts or rejects (finite automata without output). FSMs underlie spell checkers, indexers, text search, speech recognition, markup-language processors, and the network protocols that govern how computers exchange data.

Deterministic Finite Automata (DFA)

A deterministic finite automaton (DFA) is a 5-tuple $M = (Q, \Sigma, \delta, q_0, F)$ in which the transition function has signature $$\delta: Q \times \Sigma \to Q,$$ that is, for every state and every input symbol there is exactly one next state. A DFA has no move on the empty string $\epsilon$, and it cannot leave a state undefined for any symbol of $\Sigma$: every state must have a transition for every symbol, even if that transition goes to a non-accepting "dead" state that simply consumes the rest of the input. Because the next state is always uniquely determined, running a DFA on a string is a single deterministic walk through $Q$, which makes membership testing very fast, one transition per input symbol. Many different DFAs can accept the same language; the one with the fewest states is generally preferred, and unit 3 gives an algorithm (the table filling algorithm) that finds it.

Notations for DFA

A DFA is described in either of two equivalent notations. A transition table lists the states of $Q$ as rows and the symbols of $\Sigma$ as columns, with the entry in row $q$ and column $a$ giving $\delta(q, a)$; the start state's row is marked with an incoming arrow and every final state's row is marked with a double circle or asterisk. A transition diagram is the corresponding directed graph: each state is a node, and for every $q, a$ with $\delta(q,a) = p$ there is an arc from $q$ to $p$ labelled $a$ (several labels are combined onto one arc when they share the same source and target); the start state has an unlabelled arrow entering it from outside the diagram, and final states are drawn as double circles. For example, the DFA accepting all binary strings with an even number of $0$s and an even number of $1$s has states ${q_0, q_1, q_2, q_3}$ tracking the parity of each count, start and only final state $q_0$:

State01
$\to q_0$ (start, final)$q_1$$q_2$
$q_1$$q_0$$q_3$
$q_2$$q_3$$q_0$
$q_3$$q_2$$q_1$

Tracing "011": $q_0 \xrightarrow{0} q_1 \xrightarrow{1} q_3 \xrightarrow{1} q_1$, which is not final, correctly rejecting a string with one $0$ (odd) and two $1$s (even). Tracing "0011": $q_0 \xrightarrow{0} q_1 \xrightarrow{0} q_0 \xrightarrow{1} q_2 \xrightarrow{1} q_0$, ending in the final state, correctly accepting a string with two $0$s and two $1$s, both even.

Language of DFA

The language of a DFA $M = (Q, \Sigma, \delta, q_0, F)$, written $L(M)$, is the set of all strings over $\Sigma$ that drive $M$ from $q_0$ to some state in $F$: $$L(M) = { w \in \Sigma^* : \hat\delta(q_0, w) \in F },$$ using the extended transition function $\hat\delta$ defined next. A string not in $L(M)$ either ends in a non-final state or, since a DFA is total, is simply processed to completion in a non-accepting state; there is no notion of a DFA "getting stuck". Two DFAs are considered equivalent precisely when they define the same language, regardless of how many states each uses internally.

Extended Transition Function of DFA

The transition function $\delta$ only tells us how to consume one symbol. The extended transition function $\hat\delta: Q \times \Sigma^* \to Q$ generalizes this to whole strings by induction on string length: the base case is $$\hat\delta(q, \epsilon) = q,$$ and the inductive case, for a string $w = xa$ where $a$ is the last symbol, $$\hat\delta(q, xa) = \delta\big(\hat\delta(q, x), a\big),$$ that is, process the prefix $x$ first to land on some state, then take one more step on $a$. For the even-even DFA above and the string $w = 1001$, starting from $q_0$: $\hat\delta(q_0, 1) = q_2$, $\hat\delta(q_0, 10) = \delta(q_2, 0) = q_3$, $\hat\delta(q_0, 100) = \delta(q_3, 0) = q_2$, $\hat\delta(q_0, 1001) = \delta(q_2, 1) = q_0$, which is final, so $1001 \in L(M)$ (the string has two $0$'s and two $1$'s, both even, so it is correctly accepted).

What this unit covers

  • Introduction to Finite Automata
  • Introduction of Finite State Machine
  • Deterministic Finite Automata (DFA)
  • Notations for DFA
  • Language of DFA
  • Extended Transition Function of DFA
  • Non-Deterministic Finite Automaton (NFA)
  • Notations for NFA
  • Language of NFA
  • Extended Transition
  • Equivalence of DFA and NFA
  • Subset-Construction
  • Method for reduction of NFA to DFA
  • Theorems for equivalence of Language accepted by DFA and NFA
  • Finite Automaton with Epsilon Transition (ฮต - NFA)
  • Notations for ฮต - NFA
  • Epsilon Closure of a State
  • Extended Transition Function of ฮต โ€“ NFA
  • Removing Epsilon Transition using the concept of Epsilon Closure
  • Equivalence of NFA and ฮต โ€“NFA
  • Equivalence of DFA and ฮต โ€“ NFA
  • Finite State Machines with output: Moore machine and Mealy Machines

Epsilon Closure of a State

2081 ยท 5 marks

Define ฮต\varepsilonฮต-closure of a state. Differentiate between Moore and Mealy machine. [5]

Show solved answer

ฮต-closure of a State and Difference Between Moore and Mealy Machine


Part 1: ฮต-closure of a State

Definition:

ฮต-closure of a state is the set of all states that can be reached from a particular state without reading any input symbol, i.e., by reading only ฮต (epsilon) transitions.

Formally, for a state q, the ฮต-closure(q) includes:

  • The state q itself
  • All states reachable from q by following one or more ฮต-transitions

Example:

If state A has ฮต-transitions to B, and B has an ฮต-transition to C, then:

ฮต-closure(A) = {A, B, C}

Key Points:

  • Every state is always in its own ฮต-closure.
  • ฮต-closure is used when converting ฮต-NFA to DFA, where the start state of the DFA is taken as ฮต-closure of the start state of the ฮต-NFA.

Part 2: Difference Between Moore Machine and Mealy Machine

Both are finite state machines (FSMs) that produce output, but they differ in how output is generated.

BasisMealy MachineMoore Machine
Output depends onBoth present state and present inputOnly the present state
Number of statesGenerally fewer statesGenerally more states
Number of outputsFor n inputs, there are n outputsFor n inputs, there are n+1 outputs
Output functionFunction of transitions; changes when input logic on present state changesFunction of current state; changes at clock edges when state changes occur
Reaction speedReacts faster to inputs (same number of clock cycles)Reacts slower to inputs (generally one clock cycle later)
Final statesNo final states in Mealy machineFinal states are present in Moore machine
Output placementOutput is placed on transitionsOutput is placed on states

Summary

  • ฮต-closure captures all states reachable via epsilon moves alone, and is fundamental to converting ฮต-NFA to DFA.
  • Mealy machine produces output based on both state and input (faster, fewer states), while Moore machine produces output based only on the current state (slower, more states).
Open this answer on its own page โ†’

2079 ยท 5 marks

Explain the ฮต\varepsilonฮต-closure of states on an ฮต\varepsilonฮต-NFA with suitable examples. [5]

ฮต-closure of a state q is the set of all states that can be reached from state q by following zero or more ฮต (epsilon) transitions, without consuming any input symbol. Formally: ฮต-closure(q) = { p โˆˆ Q p is reachable from q using only ฮต-transitions } Key points: - Every state is always in its own ...

Open this answer on its own page โ†’

Deterministic Finite Automata

2081 ยท 5 marks

Design the DFA that accepts binary string ending with '00' and show its extended transition function for the string 111000. [5]

Show solved answer

DFA Accepting Binary Strings Ending with '00'

Given

  • Alphabet: $\Sigma = {0, 1}$
  • Language: $L = {w \in \Sigma^* \mid w \text{ ends with } 00}$
  • Test string for extended transition function: $111000$

No numeric data missing; the problem is fully solvable.

Solution

Part 1: DFA Design

We track the relevant suffix (last two symbols). Three states suffice:

StateMeaning
$q_0$Start; string is empty or does not end in a useful $0$
$q_1$String currently ends with exactly one $0$
$q_2$String ends with $00$ (Accepting)

Formal 5-tuple: $$M = ({q_0, q_1, q_2},\ {0,1},\ \delta,\ q_0,\ {q_2})$$

Transition Table:

State01
$\to q_0$$q_1$$q_0$
$q_1$$q_2$$q_0$
$*q_2$$q_2$$q_0$

Transition Function:

  • $\delta(q_0, 0) = q_1,\quad \delta(q_0, 1) = q_0$
  • $\delta(q_1, 0) = q_2,\quad \delta(q_1, 1) = q_0$
  • $\delta(q_2, 0) = q_2,\quad \delta(q_2, 1) = q_0$

State Diagram (description):

      1                    1
    (loop)              (self on q2 via 1 back to q0)
   โ”Œโ”€โ”€โ”€โ”€โ”
   โ†“    โ”‚
 โ†’( q0 )โ”€โ”€0โ”€โ”€โ–ถ( q1 )โ”€โ”€0โ”€โ”€โ–ถ(( q2 ))โ”€โ”€0โ”€โ”€โ–ถ (self loop 0)
   โ–ฒ            โ”‚             โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€1โ”€โ”€โ”€โ”€โ”€โ”€โ”˜             โ”‚
   โ–ฒโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€1โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
  • $q_0$: on $1$ self-loop; on $0 \to q_1$
  • $q_1$: on $0 \to q_2$; on $1 \to q_0$
  • $q_2$ (accepting): on $0$ self-loop; on $1 \to q_0$

Part 2: Extended Transition Function $\delta^*(q_0, 111000)$

Definition: $$\delta^(q, \varepsilon) = q, \qquad \delta^(q, wa) = \delta(\delta^*(q, w), a)$$

StepExpressionComputationResult
0$\delta^*(q_0, \varepsilon)$base case$q_0$
1$\delta^*(q_0, 1)$$\delta(q_0, 1)$$q_0$
2$\delta^*(q_0, 11)$$\delta(q_0, 1)$$q_0$
3$\delta^*(q_0, 111)$$\delta(q_0, 1)$$q_0$
4$\delta^*(q_0, 1110)$$\delta(q_0, 0)$$q_1$
5$\delta^*(q_0, 11100)$$\delta(q_1, 0)$$q_2$
6$\delta^*(q_0, 111000)$$\delta(q_2, 0)$$q_2$

Result

$$\delta^*(q_0,\ 111000) = q_2$$

Since $q_2$ is the accepting state, the string $111000$ (which ends in $00$) is accepted. The DFA design is verified correct.

Open this answer on its own page โ†’

2080 ยท 5 marks

Design a DFA that accepts single line and multi-line comments of the C-Language. [5]

  • Single-line comment: begins with //, terminates at newline \n. - Multi-line comment: begins with /, terminates at first /. - Alphabet symbols needed: /, , \n, and other (any character that is not /, , or \n). No numeric matrices are involved; this is a construction problem. All required specifi...
Open this answer on its own page โ†’

2076 ยท 5 marks

Give the formal definition of DFA. Construct a DFA accepting all strings of {0, 1} with even number of 0's and even number of 1's. [5]

A Deterministic Finite Automaton (DFA) is a 5-tuple: $$M = (Q, \Sigma, \delta, q0, F)$$ Where: Component Description ------------------------ $Q$ A finite, non-empty set of states $\Sigma$ A finite set of input symbols (alphabet) $\delta$ A transition function:

Open this answer on its own page โ†’

Method for reduction of NFA to DFA

2080 ยท 10 marks

What is NFA? How is it different from DFA? How is NFA to DFA conversion done? Convert the following NFA into DFA.[10]

Show solved answer

NFA to DFA: Definition, Differences, Conversion, and Worked Example

Given

The question asks for definitions and a conversion. The specific NFA to be converted was not provided in the question text (the diagram or table is missing), so the worked example below uses this standard NFA:

Stateab
โ†’qโ‚€{qโ‚€, qโ‚}{qโ‚€}
qโ‚โˆ…{qโ‚‚}
*qโ‚‚โˆ…โˆ…

Start state: $q_0$; Final state: $q_2$; Alphabet $\Sigma = {a, b}$; no ฮต-transitions.

Since the original NFA is missing, the theory below is given in full and the conversion is worked on this assumed NFA. In the exam, apply the same subset-construction steps to the NFA printed on your paper.


Solution

1. What is an NFA?

A Non-deterministic Finite Automaton (NFA) is a finite automaton where, for a given state and input symbol, there may be zero, one, or more next states, and ฮต-moves (transitions without consuming input) are permitted.

Formally, $M = (Q, \Sigma, \delta, q_0, F)$ where:

SymbolMeaning
$Q$Finite set of states
$\Sigma$Input alphabet
$\delta$$Q \times (\Sigma \cup {\varepsilon}) \to 2^{Q}$
$q_0$Start state
$F$Set of final states

A string is accepted if at least one computation path ends in a final state.

2. Difference Between NFA and DFA

FeatureDFANFA
Transitions per (state, symbol)Exactly oneZero, one, or more
ฮต-movesNot allowedAllowed
Transition function$\delta: Q \times \Sigma \to Q$$\delta: Q \times (\Sigma \cup {\varepsilon}) \to 2^{Q}$
AcceptanceUnique pathAny accepting path
States neededUsually moreUsually fewer
Ease of designHarderEasier
Language classRegularRegular (same power)

Every DFA is an NFA; every NFA has an equivalent DFA.

3. NFA to DFA Conversion (Subset Construction)

  • Step 1: DFA start state = $\varepsilon\text{-closure}(q_0)$.
  • Step 2: For each DFA state $T$ and symbol $a$: compute $\varepsilon\text{-closure}(\text{move}(T,a))$ = a new DFA state.
  • Step 3: Repeat until no new states appear.
  • Step 4: A DFA state is final if it contains any NFA final state.
  • Step 5: Missing transitions lead to a dead state $\emptyset$.

4. Worked Example (using the assumed NFA above)

Start state: $\varepsilon\text{-closure}(q_0) = {q_0} = A$

State $A = {q_0}$:

  • On $a$: ${q_0, q_1} = B$
  • On $b$: ${q_0} = A$

State $B = {q_0, q_1}$:

  • On $a$: ${q_0, q_1} \cup \emptyset = {q_0, q_1} = B$
  • On $b$: ${q_0} \cup {q_2} = {q_0, q_2} = C$

State $C = {q_0, q_2}$:

  • On $a$: ${q_0, q_1} \cup \emptyset = {q_0, q_1} = B$
  • On $b$: ${q_0} \cup \emptyset = {q_0} = A$

No new states. Final DFA Transition Table:

DFA StateNFA StatesOn $a$On $b$Final?
โ†’A${q_0}$BANo
B${q_0, q_1}$BCNo
C${q_0, q_2}$BAYes (contains $q_2$)

Start state = $A$; Final state = $C$ (since it contains the NFA final state $q_2$). This DFA accepts all strings over ${a,b}$ ending in "$ab$".

Note on Missing Data

The actual NFA diagram or table intended by the exam was not included in the question, so the worked conversion above uses the assumed NFA. The theory portions (definition, differences, algorithm) are complete and correct regardless.

Check: the transition computations are consistent ($A\to B/A$, $B\to B/C$, $C\to B/A$), and the final state is $C$, the only subset containing $q_2$.

Open this answer on its own page โ†’

2078 ยท 10 marks

Give the formal definition of DFA and NFA. How NFA can be converted into equivalent DFA? Explain with suitable example.[10]

--- A DFA is a 5-tuple: $$M = (Q, \Sigma, \delta, q0, F)$$ Where: Component Description ------------------------ $Q$ Finite, non-empty set of states $\Sigma$ Finite set of input symbols (alphabet) $\delta$ Transition function: $\delta: Q \times \Sigma \rightarrow Q$ (maps exactly one next state) ...

Open this answer on its own page โ†’

2076 ยท 5 marks

Construct a NFA accepting language of {0, 1} with each string ending with 01 and convert it into equivalent DFA. [5]

Show solved answer

NFA Accepting Strings Ending with 01, and its Equivalent DFA

Part 1: Constructing the NFA

Language: L = { w | w โˆˆ {0,1}* and w ends with 01 }

Example strings: 01, 001, 101, 0001, 1101, ...

NFA States and Design

We need 3 states:

StateRole
q0Start state (reading any symbol, staying in loop)
q1Just read '0' (possible start of ending "01")
q2Just read '01' -- Final/Accept state

NFA Transition Diagram

     0,1          0              1
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚      โ–ผ    โ”‚      โ–ผ       โ”‚      โ–ผ
 (q0) โ”€โ”€0โ”€โ”€โ–บ (q1) โ”€โ”€1โ”€โ”€โ–บ  ((q2))
  โ–ฒ                           
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

More precisely:

  • q0 loops on both 0 and 1 (to consume any prefix)
  • q0 on input 0 also goes to q1 (non-deterministic choice)
  • q1 on input 1 goes to q2 (accept)
  • q2 has no outgoing transitions

NFA Transition Table

StateInput 0Input 1
โ†’q0{q0, q1}{q0}
q1{}{q2}
*q2{}{}

โ†’ = start state, * = final state


Part 2: Converting NFA to Equivalent DFA (Subset Construction)

Each DFA state represents a subset of NFA states.

Step 1: Start State

DFA start state = {q0}

Step 2: Compute Transitions for Each DFA State

For {q0}:

  • On 0: ฮด(q0, 0) = {q0, q1} โ†’ new state {q0, q1}
  • On 1: ฮด(q0, 1) = {q0} โ†’ state {q0}

For {q0, q1}:

  • On 0: ฮด(q0,0) โˆช ฮด(q1,0) = {q0,q1} โˆช {} = {q0, q1}
  • On 1: ฮด(q0,1) โˆช ฮด(q1,1) = {q0} โˆช {q2} = {q0, q2} โ†’ new state

For {q0, q2}:

  • On 0: ฮด(q0,0) โˆช ฮด(q2,0) = {q0,q1} โˆช {} = {q0, q1}
  • On 1: ฮด(q0,1) โˆช ฮด(q2,1) = {q0} โˆช {} = {q0}

Step 3: DFA Transition Table

DFA StateOn 0On 1Final?
โ†’ A = {q0}B = {q0,q1}A = {q0}No
B = {q0,q1}B = {q0,q1}C = {q0,q2}No
*C = {q0,q2}B = {q0,q1}A = {q0}Yes (contains q2)

C is a final state because it contains q2 (the NFA's final state).

Step 4: DFA Transition Diagram

        0              0
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚          โ–ผ    โ”‚         โ–ผ
 (A) โ”€โ”€0โ”€โ”€โ–บ (B) โ”€โ”€1โ”€โ”€โ–บ  ((C))
  โ–ฒ    1     โ”‚              โ”‚
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    0         โ”‚ 1
       โ–ฒโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Simplified transitions:

     1          0           1
(A) โ”€โ”€โ–บ (A)   (A) โ”€โ”€โ–บ (B) โ”€โ”€โ–บ (C)
                (B) โ”€โ”€โ–บ (B)
                (C) โ”€โ”€โ–บ (B)
                (C) โ”€โ”€1โ”€โ”€โ–บ (A)

Summary

  • 3 DFA states are sufficient: A, B, C
  • C = {q0, q2} is the only final state
  • The DFA correctly accepts all strings over {0,1} that end with 01
Open this answer on its own page โ†’

Equivalence of DFA and NFA

2079 ยท 10 marks

Show that, for any NFA $N=(Q,\Sigma,\delta,q_0,F)$ accepting language $L=\Sigma^*$, there is a DFA $D=(Q',\Sigma',q_0',\delta',F')$ accepting the same language $L$.[10]

For any NFA N = (Q, ฮฃ, ฮด, qโ‚€, F) accepting language L, there exists a DFA D = (Q', ฮฃ, ฮด', qโ‚€', F') such that L(D) = L(N) = L. --- The core insight is that while an NFA can be in multiple states simultaneously, a DFA must be in exactly one state at any point. We simulate the NFA by making each sta...

Open this answer on its own page โ†’

Extended Transition

2080.1 ยท 10 marks

Describe the extended transition function of NFA. Construct a NFA, using transition table and transition diagram, over {0, 1} that accept the string having substring 01 and ends with 1. Show the acceptance of 0111.[10]

  • Alphabet: $\Sigma = {0, 1}$ - Language: strings containing substring 01 AND ending with 1 - Test string: 0111 --- For an NFA $M = (Q, \Sigma, \delta, q0, F)$ where $\delta : Q \times \Sigma \to 2^Q$, the ordinary transition function processes only a single symbol. The extended transition func...
Open this answer on its own page โ†’

Finite State Machines with output

2080.1 ยท 5 marks

Design a Mealy machine over {a, b} that generates output 'A' if the input string ends with aa else output 'B' if the string ends with bb. [5]

A Mealy Machine is a 6-tuple M = (Q, ฮฃ, ฮ”, ฮด, ฮป, qโ‚€) where: - Q = finite set of states - ฮฃ = input alphabet - ฮ” = output alphabet - ฮด = transition function (Q ร— ฮฃ โ†’ Q) - ฮป = output function (Q ร— ฮฃ โ†’ ฮ”) - qโ‚€ = initial state Note: In a Mealy machine, output depends on both the current state and cur...

Open this answer on its own page โ†’