Finite Automata
Dr. Suneel Pappala
Associate Professor
Artificial Intelligence and Data Science
St. Mary’s Group of Institutions Hyderabad
1. Introduction to Finite Automata
• A finite automaton is an abstract machine that has:
• A finite set of states.
• A set of input symbols (alphabet).
• A transition function that describes how to move from one state to
another based on an input symbol.
• A start state.
• A set of accept (final) states.
Formal (Mathematical) Representation
• A finite automaton (let’s use DFA as the example) is formally defined as
a 5-tuple:
• (Q,Σ,δ,q0,F)
Where:
• Q = finite set of states
• Σ = finite input alphabet
• δ = transition function (δ : Q × Σ → Q)
• q₀ = start state (q₀ Q)
∈
• F = set of accept states (F Q)
⊆
State Transition Table
• This is a tabular representation of the transition function δ.
• Example Table for DFA:
Current State Input = 0 Input = 1
q₀ q₁ q₀
q₁ q₁ q₂
q₂ q₁ q₀
State Transition Diagram
• A graphical representation
where:
• States are circles.
• The start state has an incoming arrow from nowhere.
• Accept states are double circles.
• Transitions are labeled arrows between states.
Example :
• 0 1
• +-----> q1 -------> q2
• | ^ |
• 0 | | 0 | 1
• | | v
• q0 <--------------- q0
• 1
Summary of Structural Representations
Form Features Use Case
Mathematical Precise, formal 5-tuple Theoretical proofs, definitions
Table Compact, easy to implement Programming, design
Diagram Visual, intuitive Teaching, documentation
Automata and Complexity
• 1. Automata Theory
• Automata Theory is the study of abstract computing devices
(automata) and the problems they can solve. It provides the formal
basis for understanding how machines process and recognize
languages.
Automaton Type Language Class Power/Capability
Finite Automata (FA) Regular Languages Recognizes simple patterns
Pushdown Automata (PDA) Context-Free Languages
Handles nested structures (e.g.,
parentheses)
Turing Machine (TM) Recursively Enumerable Languages Can simulate any computation
Complexity Theory
• Complexity Theory deals with classifying computational problems
based on how much time and space they require.
• Key Terms:
• Time Complexity: How the runtime grows with input size (e.g., O(n),
O(n²))
• Space Complexity: How much memory is used
• Efficiency: Whether a problem can be solved in a reasonable time
(polynomial time).
Important Complexity Classes:
Class Description
P Problems solvable in polynomial time
NP Problems verifiable in polynomial time
NP-Complete
Hardest problems in NP (if one can be solved in P, all
can)
NP-Hard As hard as NP-Complete but not necessarily in NP
PSPACE Problems solvable in polynomial space
Alphabets (Σ)
• An alphabet is a finite set of symbols.
• Symbols are the basic building blocks used to form strings.
• Denoted by the Greek letter Σ (Sigma).
• Examples:
• Σ = {0, 1} (binary alphabet)
• Σ = {a, b, c}
• Σ = {A, C, G, T} (for DNA sequences)
Strings
• A string is a finite sequence of symbols from the alphabet.
• The length of a string is the number of symbols it contains.
• The empty string (length 0) is denoted by ε (epsilon).
• Examples (for Σ = {0, 1}):
• ε
• 0
• 1101
• 000
Languages
• A language is any set of strings over an alphabet.
• Formally, L Σ*
⊆ , where Σ* is the set of all possible strings over Σ.
• Examples:
• L = {ε, 0, 1, 00, 11}
• L = {w | w ends with 01}
• L = all binary strings with even number of 0’s.
Problems
•In automata theory, a problem is typically defined as a question about strings—
•like whether a given string belongs to a language.
•More generally, a problem can be thought of as a set of strings
•for which the answer is "yes" (accepted by the automaton).
Formal Definition of Nondeterministic Finite
Automaton (NFA)
• An NFA is a 5-tuple:
• N=(Q,Σ,δ,q0,F)
Symbol Meaning
Q A finite set of states
Σ A finite set of input symbols (alphabet)
δ Transition function: δ: Q × (Σ {ε}) → 2^
∪ Q
q₀ Initial (start) state, where q₀ Q
∈
F Set of accept (final) states, F Q
⊆
An Application of NFA
• Lexical Analysis in Compilers
• One classic application of NFAs is in lexical analysis—the phase in a
compiler that converts source code into tokens.
• Task: Identify keywords, identifiers, operators, etc.
• How NFAs Help:
• Each token type is described by a regular expression.
• Regular expressions are converted to NFAs (easier than directly building
DFAs).
• NFAs are then converted to DFAs for fast scanning.
Text Search (Pattern Matching) Using NFAs
NFAs are useful in searching for patterns in text—this is the basis of tools
Find all occurrences of patterns like"ab*c" in text.
• Build an NFA for the regular expression:
• 'a'
• Followed by zero or more 'b's (b*)
• Followed by 'c
Finite Automata with Epsilon (ε) – Transition
• An ε-transition (epsilon-transition) is a special feature of NFAs that
allows the automaton to change state without consuming any input
symbol.
• Formal Definition of ε-NFA:
• An ε-NFA is a 5-tuple:
(Q,Σ,δ,q0,F)
where:
• δ: Q × (Σ {ε}) → 2^
∪ Q
Types of Finite Automata
• Deterministic Finite Automaton (DFA)
• For each state and input symbol, there is exactly one transition.
• Simple to implement
• Nondeterministic Finite Automaton (NFA)
• For a given state and input symbol, there can be multiple possible
transitions (including transitions without consuming input, called ε-
transitions).
• Equivalent in power to DFA (for every NFA there exists an equivalent
DFA).
Formal Definition of DFA
• A DFA is a 5-tuple (Q, Σ, δ, q₀, F) where:
• Q: finite set of states
• Σ: finite input alphabet
• δ: transition function δ : Q × Σ → Q
• q₀: start state (q₀ Q)
∈
• F: set of accept states (F Q)
⊆
Example
• Imagine a DFA that accepts binary strings ending with '01'.
• States: {q₀, q₁, q₂}
• Alphabet: {0, 1}
• Start State: q₀
• Accept State: q₂
Transition Function:
•δ(q₀, 0) = q₁
•δ(q₀, 1) = q₀
•δ(q₁, 0) = q₁
•δ(q₁, 1) = q₂
•δ(q₂, 0) = q₁
•δ(q₂, 1) = q₀
How a DFA Processes a String
• A DFA is a machine that reads an input string symbol by symbol, moving
deterministically between states based on its transition function.
• Formally:
• A DFA is a 5-tuple:
• (Q,Σ,δ,q0,F, where:
• Q = set of states
• Σ = input alphabet
• δ = transition function (δ: Q × Σ → Q)
• q₀ = start state
• F = set of accept (final) states
Processing Steps (Intuitive):
• 1. Start in the start state (q₀).
2. Read the input string left to right, one symbol at a time.
3. For each symbol, apply δ to find the next state.
4. After all input symbols are read, check the final state:
• If it's in F, accept.
• If not, reject
DFA Example:
•Σ = {0, 1}
•States: Q = {q , q }
₀ ₁
•q = start state
₀
•F = {q }
₁
δ:
•δ(q , 0) = q
₀ ₀
•δ(q , 1) = q
₀ ₁
•δ(q , 0) = q
₁ ₁
•δ(q , 1) = q
₁ ₁
Language: Strings containing at least one '1'.
• Processing "0010":
• Start at q₀
• Read '0' → q₀
• Read '0' → q₀
• Read '1' → q₁
• Read '0' → q₁
• Final state = q₁ F → ACCEPT
∈
The Language of a DFA
• The language accepted by a DFA is the set of all strings that it
accepts.
• Formally, if M is a DFA, then:
• L(M)={w Σ M accepts w}
∈ ∗∣
• The DFA defines this language completely.
Conversion of NFA with Epsilon – Transition to
without Epsilon – Transition
• Suppose we have this ε-NFA:
• States: Q = {q0, q1, q2}
• Alphabet: Σ = {a}
• Start: q0
• Accept: q2
• Transitions:
• δ(q0, ε) = {q1}
• δ(q1, a) = {q2}
Step 1: ε-closures
•ε-closure(q0) = {q0, q1}
•ε-closure(q1) = {q1}
•ε-closure(q2) = {q2}
Step 2: New δ′
• For δ (
′ q0, a):
• From ε-closure(q0) = {q0, q1}:
• δ(q0, a) = ∅
• δ(q1, a) = {q2}
• Take ε-closure({q2}) = {q2}
δ (
′ q0, a) = {q2}
For δ′(q1, a):
•ε-closure(q1) = {q1}
•δ(q1, a) = {q2}
•ε-closure({q2}) = {q2}
δ′(q1, a) = {q2}
For δ′(q2, a):
•ε-closure(q2) = {q2}
•δ(q2, a) = ∅
•ε-closure( ) =
∅ ∅
δ′(q2, a) = ∅
Conversion of NFA to DFA
• Given NFA:
• N=(Q,Σ,δ,q0,F)
Where:
• Q: Set of NFA states
• Σ: Input alphabet
• δ: Transition function (δ: Q × Σ → 2^Q)
• q₀: Start state
• F: Set of accepting states
Construct DFA
• D=(Q ,
′ Σ,δ ,
′ q0 ​
,F ) Where:
′ ′
• Q′: Set of DFA states (each is a set of NFA states)
• q₀′ = ε-closure({q₀}) (if ε-transitions exist, otherwise just {q₀})
• δ′: Transition function for DFA
• F′: Set of DFA accepting states — any state that contains at least one
NFA accepting state
Algorithm Steps
• Step 1: Start State
• The start state of DFA is the ε-closure of the NFA's start state (if no ε-transitions, just {q₀}).
• Step 2: Transition Function δ′
• For each DFA state (a set of NFA states), and for each symbol a Σ:
∈
• Combine all NFA transitions for all states in the set on symbol a.
• Result is a new set (subset) of states = δ (current_state_set, a)
′ .
• Step 3: Repeat Until Closure
• Add new sets (new DFA states) as they appear during transitions.
• Continue until no more new DFA states are generated.
• Step 4: Define Accepting States
• Any DFA state that includes at least one accepting NFA state becomes an accepting DFA
state.
Example: NFA to DFA Conversion
• Let’s consider an NFA:
• Σ = {0, 1}
• Q = {q0, q1, q2}
• q₀ = start state
• F = {q2}
• Transitions:
• δ(q0, 0) = {q0, q1}
• δ(q0, 1) = {q0}
• δ(q1, 1) = {q2}
Step 1: DFA Start State
DFA start state = {q0}
• Step 2: Build Transition Table
DFA State Input 0 Input 1
{q0} {q0, q1} {q0}
{q0, q1} {q0, q1} {q0, q2}
{q0, q2} {q0, q1} {q0}
• Step 3: Define Accepting States
• NFA accepting state = q2
So any DFA state containing q2 is an accepting state:
• DFA Accepting States = { {q0, q2}, {q0, q1, q2}, {q2} }
Resulting DFA
•States: {q0}, {q0, q1}, {q0, q2}, etc.
•Start State: {q0}
•Accept States: All that contain q2
•Transitions defined by subset construction
Moore Machine
• A Moore Machine is a finite state machine where outputs depend only on states.
• Formal Definition
• A Moore machine is a 6-tuple:
• M=(Q,Σ,Δ,δ,λ,q0)
Where:
• Q: Finite set of states
• Σ: Input alphabet
• Δ: Output alphabet
• δ: Transition function Q×Σ→QQ times Sigma rightarrow QQ×Σ→Q
• λ: Output function Q→ΔQ rightarrow DeltaQ→Δ
• q₀: Start state
Mealy Machine
• A Mealy Machine is a finite state machine where outputs depend on states and input symbols.
• Formal Definition
• A Mealy machine is a 6-tuple:
• M=(Q,Σ,Δ,δ,λ,q0)
Where:
• Q: Finite set of states
• Σ: Input alphabet
• Δ: Output alphabet
• δ: Transition function Q×Σ→QQ times Sigma rightarrow QQ×Σ→Q
• λ: Output function Q×Σ→ΔQ times Sigma rightarrow DeltaQ×Σ→Δ
• q₀: Start state
INTRODUTION Formal Language and Automatic Theory.pptx

INTRODUTION Formal Language and Automatic Theory.pptx

  • 1.
    Finite Automata Dr. SuneelPappala Associate Professor Artificial Intelligence and Data Science St. Mary’s Group of Institutions Hyderabad
  • 2.
    1. Introduction toFinite Automata • A finite automaton is an abstract machine that has: • A finite set of states. • A set of input symbols (alphabet). • A transition function that describes how to move from one state to another based on an input symbol. • A start state. • A set of accept (final) states.
  • 3.
    Formal (Mathematical) Representation •A finite automaton (let’s use DFA as the example) is formally defined as a 5-tuple: • (Q,Σ,δ,q0,F) Where: • Q = finite set of states • Σ = finite input alphabet • δ = transition function (δ : Q × Σ → Q) • q₀ = start state (q₀ Q) ∈ • F = set of accept states (F Q) ⊆
  • 4.
    State Transition Table •This is a tabular representation of the transition function δ. • Example Table for DFA: Current State Input = 0 Input = 1 q₀ q₁ q₀ q₁ q₁ q₂ q₂ q₁ q₀
  • 5.
    State Transition Diagram •A graphical representation where: • States are circles. • The start state has an incoming arrow from nowhere. • Accept states are double circles. • Transitions are labeled arrows between states.
  • 6.
    Example : • 01 • +-----> q1 -------> q2 • | ^ | • 0 | | 0 | 1 • | | v • q0 <--------------- q0 • 1
  • 7.
    Summary of StructuralRepresentations Form Features Use Case Mathematical Precise, formal 5-tuple Theoretical proofs, definitions Table Compact, easy to implement Programming, design Diagram Visual, intuitive Teaching, documentation
  • 8.
    Automata and Complexity •1. Automata Theory • Automata Theory is the study of abstract computing devices (automata) and the problems they can solve. It provides the formal basis for understanding how machines process and recognize languages. Automaton Type Language Class Power/Capability Finite Automata (FA) Regular Languages Recognizes simple patterns Pushdown Automata (PDA) Context-Free Languages Handles nested structures (e.g., parentheses) Turing Machine (TM) Recursively Enumerable Languages Can simulate any computation
  • 9.
    Complexity Theory • ComplexityTheory deals with classifying computational problems based on how much time and space they require. • Key Terms: • Time Complexity: How the runtime grows with input size (e.g., O(n), O(n²)) • Space Complexity: How much memory is used • Efficiency: Whether a problem can be solved in a reasonable time (polynomial time).
  • 10.
    Important Complexity Classes: ClassDescription P Problems solvable in polynomial time NP Problems verifiable in polynomial time NP-Complete Hardest problems in NP (if one can be solved in P, all can) NP-Hard As hard as NP-Complete but not necessarily in NP PSPACE Problems solvable in polynomial space
  • 11.
    Alphabets (Σ) • Analphabet is a finite set of symbols. • Symbols are the basic building blocks used to form strings. • Denoted by the Greek letter Σ (Sigma). • Examples: • Σ = {0, 1} (binary alphabet) • Σ = {a, b, c} • Σ = {A, C, G, T} (for DNA sequences)
  • 12.
    Strings • A stringis a finite sequence of symbols from the alphabet. • The length of a string is the number of symbols it contains. • The empty string (length 0) is denoted by ε (epsilon). • Examples (for Σ = {0, 1}): • ε • 0 • 1101 • 000
  • 13.
    Languages • A languageis any set of strings over an alphabet. • Formally, L Σ* ⊆ , where Σ* is the set of all possible strings over Σ. • Examples: • L = {ε, 0, 1, 00, 11} • L = {w | w ends with 01} • L = all binary strings with even number of 0’s.
  • 14.
    Problems •In automata theory,a problem is typically defined as a question about strings— •like whether a given string belongs to a language. •More generally, a problem can be thought of as a set of strings •for which the answer is "yes" (accepted by the automaton).
  • 15.
    Formal Definition ofNondeterministic Finite Automaton (NFA) • An NFA is a 5-tuple: • N=(Q,Σ,δ,q0,F) Symbol Meaning Q A finite set of states Σ A finite set of input symbols (alphabet) δ Transition function: δ: Q × (Σ {ε}) → 2^ ∪ Q q₀ Initial (start) state, where q₀ Q ∈ F Set of accept (final) states, F Q ⊆
  • 16.
    An Application ofNFA • Lexical Analysis in Compilers • One classic application of NFAs is in lexical analysis—the phase in a compiler that converts source code into tokens. • Task: Identify keywords, identifiers, operators, etc. • How NFAs Help: • Each token type is described by a regular expression. • Regular expressions are converted to NFAs (easier than directly building DFAs). • NFAs are then converted to DFAs for fast scanning.
  • 17.
    Text Search (PatternMatching) Using NFAs NFAs are useful in searching for patterns in text—this is the basis of tools Find all occurrences of patterns like"ab*c" in text. • Build an NFA for the regular expression: • 'a' • Followed by zero or more 'b's (b*) • Followed by 'c
  • 18.
    Finite Automata withEpsilon (ε) – Transition • An ε-transition (epsilon-transition) is a special feature of NFAs that allows the automaton to change state without consuming any input symbol. • Formal Definition of ε-NFA: • An ε-NFA is a 5-tuple: (Q,Σ,δ,q0,F) where: • δ: Q × (Σ {ε}) → 2^ ∪ Q
  • 19.
    Types of FiniteAutomata • Deterministic Finite Automaton (DFA) • For each state and input symbol, there is exactly one transition. • Simple to implement • Nondeterministic Finite Automaton (NFA) • For a given state and input symbol, there can be multiple possible transitions (including transitions without consuming input, called ε- transitions). • Equivalent in power to DFA (for every NFA there exists an equivalent DFA).
  • 20.
    Formal Definition ofDFA • A DFA is a 5-tuple (Q, Σ, δ, q₀, F) where: • Q: finite set of states • Σ: finite input alphabet • δ: transition function δ : Q × Σ → Q • q₀: start state (q₀ Q) ∈ • F: set of accept states (F Q) ⊆
  • 21.
    Example • Imagine aDFA that accepts binary strings ending with '01'. • States: {q₀, q₁, q₂} • Alphabet: {0, 1} • Start State: q₀ • Accept State: q₂
  • 22.
    Transition Function: •δ(q₀, 0)= q₁ •δ(q₀, 1) = q₀ •δ(q₁, 0) = q₁ •δ(q₁, 1) = q₂ •δ(q₂, 0) = q₁ •δ(q₂, 1) = q₀
  • 23.
    How a DFAProcesses a String • A DFA is a machine that reads an input string symbol by symbol, moving deterministically between states based on its transition function. • Formally: • A DFA is a 5-tuple: • (Q,Σ,δ,q0,F, where: • Q = set of states • Σ = input alphabet • δ = transition function (δ: Q × Σ → Q) • q₀ = start state • F = set of accept (final) states
  • 24.
    Processing Steps (Intuitive): •1. Start in the start state (q₀). 2. Read the input string left to right, one symbol at a time. 3. For each symbol, apply δ to find the next state. 4. After all input symbols are read, check the final state: • If it's in F, accept. • If not, reject
  • 25.
    DFA Example: •Σ ={0, 1} •States: Q = {q , q } ₀ ₁ •q = start state ₀ •F = {q } ₁
  • 26.
    δ: •δ(q , 0)= q ₀ ₀ •δ(q , 1) = q ₀ ₁ •δ(q , 0) = q ₁ ₁ •δ(q , 1) = q ₁ ₁
  • 27.
    Language: Strings containingat least one '1'. • Processing "0010": • Start at q₀ • Read '0' → q₀ • Read '0' → q₀ • Read '1' → q₁ • Read '0' → q₁ • Final state = q₁ F → ACCEPT ∈
  • 28.
    The Language ofa DFA • The language accepted by a DFA is the set of all strings that it accepts. • Formally, if M is a DFA, then: • L(M)={w Σ M accepts w} ∈ ∗∣ • The DFA defines this language completely.
  • 29.
    Conversion of NFAwith Epsilon – Transition to without Epsilon – Transition • Suppose we have this ε-NFA: • States: Q = {q0, q1, q2} • Alphabet: Σ = {a} • Start: q0 • Accept: q2 • Transitions: • δ(q0, ε) = {q1} • δ(q1, a) = {q2}
  • 30.
    Step 1: ε-closures •ε-closure(q0)= {q0, q1} •ε-closure(q1) = {q1} •ε-closure(q2) = {q2}
  • 31.
    Step 2: Newδ′ • For δ ( ′ q0, a): • From ε-closure(q0) = {q0, q1}: • δ(q0, a) = ∅ • δ(q1, a) = {q2} • Take ε-closure({q2}) = {q2} δ ( ′ q0, a) = {q2}
  • 32.
    For δ′(q1, a): •ε-closure(q1)= {q1} •δ(q1, a) = {q2} •ε-closure({q2}) = {q2} δ′(q1, a) = {q2}
  • 33.
    For δ′(q2, a): •ε-closure(q2)= {q2} •δ(q2, a) = ∅ •ε-closure( ) = ∅ ∅ δ′(q2, a) = ∅
  • 34.
    Conversion of NFAto DFA • Given NFA: • N=(Q,Σ,δ,q0,F) Where: • Q: Set of NFA states • Σ: Input alphabet • δ: Transition function (δ: Q × Σ → 2^Q) • q₀: Start state • F: Set of accepting states
  • 35.
    Construct DFA • D=(Q, ′ Σ,δ , ′ q0 ​ ,F ) Where: ′ ′ • Q′: Set of DFA states (each is a set of NFA states) • q₀′ = ε-closure({q₀}) (if ε-transitions exist, otherwise just {q₀}) • δ′: Transition function for DFA • F′: Set of DFA accepting states — any state that contains at least one NFA accepting state
  • 36.
    Algorithm Steps • Step1: Start State • The start state of DFA is the ε-closure of the NFA's start state (if no ε-transitions, just {q₀}). • Step 2: Transition Function δ′ • For each DFA state (a set of NFA states), and for each symbol a Σ: ∈ • Combine all NFA transitions for all states in the set on symbol a. • Result is a new set (subset) of states = δ (current_state_set, a) ′ . • Step 3: Repeat Until Closure • Add new sets (new DFA states) as they appear during transitions. • Continue until no more new DFA states are generated. • Step 4: Define Accepting States • Any DFA state that includes at least one accepting NFA state becomes an accepting DFA state.
  • 37.
    Example: NFA toDFA Conversion • Let’s consider an NFA: • Σ = {0, 1} • Q = {q0, q1, q2} • q₀ = start state • F = {q2} • Transitions: • δ(q0, 0) = {q0, q1} • δ(q0, 1) = {q0} • δ(q1, 1) = {q2}
  • 38.
    Step 1: DFAStart State DFA start state = {q0} • Step 2: Build Transition Table DFA State Input 0 Input 1 {q0} {q0, q1} {q0} {q0, q1} {q0, q1} {q0, q2} {q0, q2} {q0, q1} {q0}
  • 39.
    • Step 3:Define Accepting States • NFA accepting state = q2 So any DFA state containing q2 is an accepting state: • DFA Accepting States = { {q0, q2}, {q0, q1, q2}, {q2} }
  • 40.
    Resulting DFA •States: {q0},{q0, q1}, {q0, q2}, etc. •Start State: {q0} •Accept States: All that contain q2 •Transitions defined by subset construction
  • 41.
    Moore Machine • AMoore Machine is a finite state machine where outputs depend only on states. • Formal Definition • A Moore machine is a 6-tuple: • M=(Q,Σ,Δ,δ,λ,q0) Where: • Q: Finite set of states • Σ: Input alphabet • Δ: Output alphabet • δ: Transition function Q×Σ→QQ times Sigma rightarrow QQ×Σ→Q • λ: Output function Q→ΔQ rightarrow DeltaQ→Δ • q₀: Start state
  • 42.
    Mealy Machine • AMealy Machine is a finite state machine where outputs depend on states and input symbols. • Formal Definition • A Mealy machine is a 6-tuple: • M=(Q,Σ,Δ,δ,λ,q0) Where: • Q: Finite set of states • Σ: Input alphabet • Δ: Output alphabet • δ: Transition function Q×Σ→QQ times Sigma rightarrow QQ×Σ→Q • λ: Output function Q×Σ→ΔQ times Sigma rightarrow DeltaQ×Σ→Δ • q₀: Start state