Theory of ComputationUnit 211 min read
Regular Languages & Finite Automata: Definitions, Models & Conversions
Unit 2 of Theory of Computation covers the formal definition of regular languages, their representation via regular expressions, finite automata (DFA/NFA/ε-NFA), and conversion methods between these models, with emphasis on equivalence proofs and minimization techniques.
TAKEAWAYS:
- Regular languages are exactly those recognized by finite automata (DFA/NFA/ε-NFA) and defined by regular expressions, forming the lowest level of Chomsky hierarchy.
- DFAs are deterministic (one transition per input symbol) and always accept/reject, while NFAs may have multiple transitions and ε-transitions (silent moves).
- ε-NFAs extend NFAs with ε-transitions (no input symbol) and require ε-closure computation for language recognition.
- Conversion methods include:
- Regex → NFA (Thompson’s construction),
- NFA → DFA (subset construction),
- DFA minimization (table-filling algorithm).
- Closure properties (union, concatenation, Kleene star) preserve regularity, while pumping lemma (next unit) proves non-regularity.
- Applications span lexical analysis (compilers), protocol validation, and real-world systems like eSewa’s transaction validation (DFA for input formats) and Khalti’s fraud detection (NFA for pattern matching).
1. Regular Languages: Definition and Examples
A regular language is any language over an alphabet Σ that can be:
- Defined by a regular expression (regex),
- Recognized by a finite automaton (DFA/NFA/ε-NFA),
- Generated by a right-linear grammar.
Key Properties
classDiagram
class RegularLanguage {
+Defined by: Regex
+Recognized by: DFA/NFA/ε-NFA
+Generated by: Right-linear grammar
+Closure under: Union, Concatenation, Kleene star, Intersection, Complement, Homomorphism
}
class Regex {
+Operators: Union (|), Concatenation (), Kleene star (*)
+Example: (a+b)*abb
}
class FiniteAutomaton {
+Types: DFA, NFA, ε-NFA
+States: Finite, Initial, Final
+Transitions: Deterministic (DFA) or Non-deterministic (NFA)
}
RegularLanguage --> Regex
RegularLanguage --> FiniteAutomatonExample: Regex for Even-Length Binary Strings Ending with 01
Language: . Regex: Simplified: Correct Regex: Worked Example: For over :
- Odd digits: , even digits: .
- Regex:
Trace: A string like
34568is accepted (starts with3, ends with8).
2. Finite Automata: Types and Transitions
Finite automata process strings by moving between states via transitions triggered by input symbols.
Types of Finite Automata
| Type | Deterministic? | ε-Transitions? | Example Use Case |
|---|---|---|---|
| DFA | Yes | No | Lexical analysis (compilers) |
| NFA | No | No | Pattern matching (grep) |
| ε-NFA | No | Yes | Parsing nested structures (e.g., XML) |
DFA vs. NFA: Key Differences
flowchart TD
A["DFA"] -->|"Single transition per input"| B["Deterministic"]
A -->|"No ε-transitions"| C["Simpler to minimize"]
D["NFA"] -->|"Multiple transitions per input"| E["Non-deterministic"]
D -->|"ε-transitions allowed"| F["More expressive"]
G["ε-NFA"] -->|"Extends NFA with ε-moves"| H["Used for grammar parsing"]Example: DFA for Strings Ending with 01
States: , where:
- : initial state,
- : saw
0, - : accepting state (saw
01).
Transitions:
- From :
0→ ,1→ . - From :
1→ ,0→ ,1→ . - From : any input → (reset).
Trace for 0101:
- Start at .
- Read
0: . - Read
1: (accept). - Read
0: . - Read
1: . Result: Rejected (last1doesn’t complete01).
3. ε-NFAs and ε-Closure
An ε-NFA allows ε-transitions (moves without consuming input). The ε-closure of a state is the set of states reachable from via ε-transitions.
ε-Closure Example
ε-NFA for :
stateDiagram-v2
[*] --> q0
q0 --> q1 : 0/ε
q1 --> q2 : 1
q2 --> [*] : ε
q0 --> q0 : 1
q1 --> q1 : 0ε-Closure of : (since ).
Trace for ε (empty string):
- Start at .
- ε-closure: .
- From , no input → accept via ε-transition to final state.
4. Conversion Methods
A. Regex to NFA (Thompson’s Construction)
Rules:
- Concatenation: → Connect ’s final to ’s initial.
- Union: → Add a new start/end state with ε-transitions to and .
- Kleene Star: → Loop from final state back to initial via ε-transition.
Example: Convert to NFA.
stateDiagram-v2
[*] --> q0
q0 --> q1 : a/ε
q0 --> q2 : b/ε
q1 --> q0 : ε
q2 --> q0 : ε
q0 --> q3 : a
q3 --> q4 : b
q4 --> [*] : ε
q3 --> q4 : bB. NFA to DFA (Subset Construction)
Steps:
- Start with the ε-closure of the NFA’s initial state.
- For each subset of states and input symbol, compute the next subset via transitions.
- Mark subsets containing final states as accepting.
Example: Convert the above NFA to DFA.
| DFA State (Subset) | a |
b |
Accepting? |
|---|---|---|---|
| No | |||
| No | |||
| No | |||
| No | |||
| Yes |
5. DFA Minimization (Table-Filling Algorithm)
Goal: Reduce states while preserving language recognition. Steps:
- Initialize a table of state pairs.
- Mark pairs where one state is final and the other isn’t.
- For unmarked pairs, check if transitions lead to marked pairs. If yes, mark them.
- Repeat until no new pairs are marked.
- Merge unmarked pairs into a single state.
Example: Minimize the DFA from the previous table.
flowchart TD
A["q0"] -->|"a"| B["q0,q1"]
A -->|"b"| C["q0,q2"]
B -->|"a"| D["q0,q1,q3"]
B -->|"b"| E["q0,q1"]
C -->|"a"| E
C -->|"b"| C
D -->|"a"| D
D -->|"b"| F["q0,q1,q4"]
F -->|"a"| E
F -->|"b"| FResult: Merge and , etc., to get a 3-state DFA.
In the Real World
eSewa Transaction Validation
- Idea Used: DFA for input format validation.
- How: eSewa’s web form for bill payments uses a DFA to ensure user inputs (e.g., phone number, amount) match expected patterns (e.g., 10-digit Nepali number, valid currency). Invalid inputs (e.g.,
abc123for amount) are rejected immediately.
Khalti Fraud Detection
- Idea Used: NFA for suspicious pattern matching.
- How: Khalti’s backend scans transactions for fraud patterns (e.g., rapid small-value transfers). An NFA with ε-transitions models sequences like:
If the NFA reaches an accepting state, the transaction is flagged for review.[high-value transaction] →ε→ [multiple low-value transactions] →ε→ [withdrawal]
Daraz Order Queue Management
- Idea Used: Finite automaton for order states.
- How: An order’s lifecycle (placed → processed → shipped → delivered) is modeled as a DFA where each state transition depends on input events (e.g., "payment received," "warehouse update"). Example:
This ensures no order is shipped before payment.State: Placed → Input: Payment → State: Processed
NTC’s Traffic Light Control (Simplified)
- Idea Used: DFA for signal timing.
- How: A DFA controls traffic lights at a junction with states:
Red→Green(after timer),Green→Yellow(after countdown),Yellow→Red. Inputs are time signals, and the DFA ensures no two lights are green simultaneously.
6. Closure Properties of Regular Languages
Regular languages are closed under:
- Union: is regular if are.
- Concatenation: is regular.
- Kleene Star: is regular.
- Intersection: is regular (via DFA product construction).
- Complement: is regular.
- Homomorphism: is regular for any homomorphism .
Example: Prove is regular.
- Use DFA with states :
- , ,
- , .
- Accepting state: .
7. Non-Regular Languages (Preview)
While regular languages are powerful, some are not regular, proven via the pumping lemma (next unit). Examples:
- : Requires unbounded memory (stack).
- : Needs counting.
- : No finite automaton can "know" primality.
Why?
- Finite automata have no memory beyond their current state.
- Languages requiring counting or nested structures need pushdown automata (PDA) or Turing machines.
Exam Tip
For Regex Problems:
- Always verify your regex with small strings (e.g., test on
a,ab,abb). - Use parentheses to group operations correctly (e.g., ≠ *).
- Always verify your regex with small strings (e.g., test on
For DFA/NFA Construction:
- Label all states (initial, final, intermediate).
- For ε-NFAs, explicitly show ε-transitions and compute ε-closures in traces.
- Minimization: Use the table-filling method step-by-step; partial credit is given for correct marking.
For Proofs (e.g., Non-Regularity):
- Assume is regular and derive a contradiction using the pumping lemma.
- Structure your proof:
- Assume is regular.
- Let be the pumping length.
- Pick a string longer than in .
- Pump to get , contradicting regularity.
Common Pitfalls:
- Off-by-one errors in regex (e.g., vs. ).
- Forgetting ε-transitions in NFA/ε-NFA constructions.
- Incorrect final states in minimized DFAs (always verify with test strings).
Real-World Tie-Ins:
- Examiners love questions like:
"Design a DFA to validate Ncell’s 10-digit phone number format (must start with 98, followed by 8 digits)."
Solution: Use a DFA with states for each digit position, rejecting invalid prefixes (e.g.,
97...).
- Examiners love questions like:
"Design a DFA to validate Ncell’s 10-digit phone number format (must start with 98, followed by 8 digits)."
Solution: Use a DFA with states for each digit position, rejecting invalid prefixes (e.g.,
Visual Summary:
mindmap
root((Regular Languages))
Regex
Operators: Union, Concatenation, Kleene star
Example: (a+b)*abb
DFA
Deterministic
Minimizable
Example: Even-length strings
NFA
Non-deterministic
ε-transitions
Example: Pattern matching
ε-NFA
Extends NFA with ε-moves
ε-closure computation
Closure Properties
Union, Intersection, Complement
Non-Regular
Pumping lemma
Example: {a^n b^n}Based on the TU BSc CSIT syllabus for Theory of Computation (CSC262), unit 2.
Discussion
Loading…