Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deterministic Finite Automata

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Deterministic Finite Automata

Avatar for Jaime Arias Almeida

Jaime Arias Almeida

June 27, 2025

More Decks by Jaime Arias Almeida

Other Decks in Research

Transcript

  1. Hello! I’m Jaime Arias • CNRS Research Engineer @ LIPN

    • Head of the development team @ LIPN • Lecturer (65 hrs) @ École SupGalilée ◦ Software Engineering and OOP courses • Member @ Software/Source Codes College • Ambassador @ Software Heritage • Author of 31 publications (5 journal articles) You can find me at: ͐ [email protected] Ì www.jaime-arias.fr Jaime Arias Computability and Complexity 2/24
  2. Motivation • In the real world, we often need to

    model systems that can be in different states and can change state based on some input. Jaime Arias Computability and Complexity 3/24
  3. Motivation • In the real world, we often need to

    model systems that can be in different states and can change state based on some input. Example (Mario Bros) • Mario can be in different states e.g. walking, jumping, or attacking Jaime Arias Computability and Complexity 3/24
  4. Motivation • In the real world, we often need to

    model systems that can be in different states and can change state based on some input. Example (Mario Bros) • Mario can be in different states e.g. walking, jumping, or attacking • Mario’s state changes based on player input e.g. pressing a button to jump Jaime Arias Computability and Complexity 3/24
  5. Motivation Figure: Some other examples • A system that consists

    of only finitely many states and transitions among them is called a finite-state transition system. Jaime Arias Computability and Complexity 4/24
  6. Motivation Figure: Some other examples • A system that consists

    of only finitely many states and transitions among them is called a finite-state transition system. • These systems can be modelled abstractly by a mathematical model called a finite automaton. Jaime Arias Computability and Complexity 4/24
  7. Table of contents 1. Deterministic Finite Automata (DFA) Formal Definition

    Graphical Representation Implementation 2. Regular Languages Strings Extending the Transition Function Formal Definition Jaime Arias Computability and Complexity 5/24
  8. Deterministic Finite Automata Formal Definition Formally, a deterministic finite automaton

    (DFA) is the quintuple: A = ⟨ , , , , ⟩ Jaime Arias Computability and Complexity 7/24
  9. Deterministic Finite Automata Formal Definition Formally, a deterministic finite automaton

    (DFA) is the quintuple: A = ⟨ Q , , , , ⟩ states • Q is a finite set of states; Jaime Arias Computability and Complexity 7/24
  10. Deterministic Finite Automata Formal Definition Formally, a deterministic finite automaton

    (DFA) is the quintuple: A = ⟨ Q , Σ , , , ⟩ states input alphabet • Q is a finite set of states; • Σ is a finite set of symbols; Jaime Arias Computability and Complexity 7/24
  11. Deterministic Finite Automata Formal Definition Formally, a deterministic finite automaton

    (DFA) is the quintuple: A = ⟨ Q , Σ , δ , , ⟩ states input alphabet transition function • Q is a finite set of states; • Σ is a finite set of symbols; • δ : Q × Σ → Q is a transition function; Jaime Arias Computability and Complexity 7/24
  12. Deterministic Finite Automata Formal Definition Formally, a deterministic finite automaton

    (DFA) is the quintuple: A = ⟨ Q , Σ , δ , q0 , ⟩ states input alphabet transition function initial state • Q is a finite set of states; • Σ is a finite set of symbols; • δ : Q × Σ → Q is a transition function; • q0 ∈ Q is the initial state; and Jaime Arias Computability and Complexity 7/24
  13. Deterministic Finite Automata Formal Definition Formally, a deterministic finite automaton

    (DFA) is the quintuple: A = ⟨ Q , Σ , δ , q0 , F ⟩ states input alphabet transition function initial state final states • Q is a finite set of states; • Σ is a finite set of symbols; • δ : Q × Σ → Q is a transition function; • q0 ∈ Q is the initial state; and • F ⊆ Q is a set of final states. Jaime Arias Computability and Complexity 7/24
  14. Deterministic Finite Automata Graphical Representation q1 q2 Figure: Two-state DFA

    A1 • Q = {q1, q2}; Jaime Arias Computability and Complexity 8/24
  15. Deterministic Finite Automata Graphical Representation q1 q2 Figure: Two-state DFA

    A1 • Q = {q1, q2}; • Σ = {0, 1}; Jaime Arias Computability and Complexity 8/24
  16. Deterministic Finite Automata Graphical Representation q1 q1 q2 Figure: Two-state

    DFA A1 • Q = {q1, q2}; • Σ = {0, 1}; • q0 = q1 ; Jaime Arias Computability and Complexity 8/24
  17. Deterministic Finite Automata Graphical Representation q1 q2 Figure: Two-state DFA

    A1 • Q = {q1, q2}; • Σ = {0, 1}; • q0 = q1 ; • F = {q2}; Jaime Arias Computability and Complexity 8/24
  18. Deterministic Finite Automata Graphical Representation q1 q2 0 Figure: Two-state

    DFA A1 • Q = {q1, q2}; • Σ = {0, 1}; • q0 = q1 ; • F = {q2}; • δ(q1, 0) = q1 ; Jaime Arias Computability and Complexity 8/24
  19. Deterministic Finite Automata Graphical Representation q1 q2 0 1 Figure:

    Two-state DFA A1 • Q = {q1, q2}; • Σ = {0, 1}; • q0 = q1 ; • F = {q2}; • δ(q1, 0) = q1 ; • δ(q1, 1) = q2 ; Jaime Arias Computability and Complexity 8/24
  20. Deterministic Finite Automata Graphical Representation q1 q2 0 1 1

    Figure: Two-state DFA A1 • Q = {q1, q2}; • Σ = {0, 1}; • q0 = q1 ; • F = {q2}; • δ(q1, 0) = q1 ; • δ(q1, 1) = q2 ; • δ(q2, 1) = q2 ; and Jaime Arias Computability and Complexity 8/24
  21. Deterministic Finite Automata Graphical Representation q1 q2 0 1 1

    0 Figure: Two-state DFA A1 • Q = {q1, q2}; • Σ = {0, 1}; • q0 = q1 ; • F = {q2}; • δ(q1, 0) = q1 ; • δ(q1, 1) = q2 ; • δ(q2, 1) = q2 ; and • δ(q2, 0) = q1 . Jaime Arias Computability and Complexity 8/24
  22. Deterministic Finite Automata Graphical Representation q1 q2 0 1 1

    0 Figure: Two-state DFA A1 • Q = {q1, q2}; • Σ = {0, 1}; • q0 = q1 ; • F = {q2}; • δ(q1, 0) = q1 ; • δ(q1, 1) = q2 ; • δ(q2, 1) = q2 ; and • δ(q2, 0) = q1 . Jaime Arias Computability and Complexity 8/24
  23. Deterministic Finite Automata Exercise s0 s1 s2 0 1 0

    1 1 0 ▷ What is Q? ▷ What is Σ? ▷ What is q0 ? ▷ What if F? ▷ Determine δ(s2, 1)? Jaime Arias Computability and Complexity 9/24
  24. Deterministic Finite Automata Implementation 1 class DFA: 2 def __init__(self,

    states, alphabet, trans, initial_s, final_s): 3 self.Q = states 4 self.Sigma = alphabet 5 self.transitions = trans 6 self.q0 = initial_s 7 self.F = final_s Jaime Arias Computability and Complexity 10/24
  25. Deterministic Finite Automata Implementation 1 class DFA: 2 def __init__(self,

    states, alphabet, trans, initial_s, final_s): 3 self.Q = states 4 self.Sigma = alphabet 5 self.transitions = trans 6 self.q0 = initial_s 7 self.F = final_s q1 q2 0 1 1 0 Figure: Two-state DFA A1 1 states = {'q1', 'q2'} 2 alphabet = {'0', '1'} 3 transitions = { 4 'q1': {'0': 'q1', '1': 'q2'}, 5 'q2': {'0': 'q1', '1': 'q2'} 6 } 7 initial_s = 'q1' 8 final_states = {'q2'} 9 10 dfa = DFA(states, alphabet, transitions , initial_s, final_states) Jaime Arias Computability and Complexity 10/24
  26. Regular Languages Strings • A string over Σ is any

    finite-length sequence of elements of Σ. • The length of a string s, denoted by |s|, is the number of symbols in s. Jaime Arias Computability and Complexity 12/24
  27. Regular Languages Strings • A string over Σ is any

    finite-length sequence of elements of Σ. • The length of a string s, denoted by |s|, is the number of symbols in s. s = a a b a b where Σ = {a, b} |s| = 5 Jaime Arias Computability and Complexity 12/24
  28. Regular Languages Strings • A string over Σ is any

    finite-length sequence of elements of Σ. • The length of a string s, denoted by |s|, is the number of symbols in s. s = a a b a b where Σ = {a, b} |s| = 5 • The empty string (ϵ) is the unique string of length 0, i.e. |ϵ| = 0. Jaime Arias Computability and Complexity 12/24
  29. Regular Languages Strings • A string over Σ is any

    finite-length sequence of elements of Σ. • The length of a string s, denoted by |s|, is the number of symbols in s. s = a a b a b where Σ = {a, b} |s| = 5 • The empty string (ϵ) is the unique string of length 0, i.e. |ϵ| = 0. • The set of all strings over Σ is denoted by Σ∗. {a, b}∗ = {ϵ, a, b, aa, ab, ba, bb, aa, aaa, aab, . . . } Jaime Arias Computability and Complexity 12/24
  30. Regular Languages Strings • A string over Σ is any

    finite-length sequence of elements of Σ. • The length of a string s, denoted by |s|, is the number of symbols in s. s = a a b a b where Σ = {a, b} |s| = 5 • The empty string (ϵ) is the unique string of length 0, i.e. |ϵ| = 0. • The set of all strings over Σ is denoted by Σ∗. {a, b}∗ = {ϵ, a, b, aa, ab, ba, bb, aa, aaa, aab, . . . } • Note: If Σ is nonempty, then Σ∗ is an infinite set of finite-length strings. Jaime Arias Computability and Complexity 12/24
  31. Regular Languages Strings • Until now, we have only considered

    a move from one state to another when a single symbol is given (i.e. δ). Let’s extend this to a string of symbols Jaime Arias Computability and Complexity 13/24
  32. Regular Languages Extending the transition function – Intuition q1 q2

    0 1 1 0 s = 0 1 1 Jaime Arias Computability and Complexity 14/24
  33. Regular Languages Extending the transition function – Intuition q1 q2

    0 1 1 0 s = 0 1 1 Jaime Arias Computability and Complexity 14/24
  34. Regular Languages Extending the transition function – Intuition q1 q2

    0 1 1 0 s = 0 1 1 Jaime Arias Computability and Complexity 14/24
  35. Regular Languages Extending the transition function – Intuition q1 q2

    0 1 1 0 s = 0 1 1 Jaime Arias Computability and Complexity 14/24
  36. Regular Languages Extending the transition function – Formal Definition Extended

    Transition Function: δ∗ : Q × Σ∗ → Q Jaime Arias Computability and Complexity 15/24
  37. Regular Languages Extending the transition function – Formal Definition Extended

    Transition Function: δ∗ : Q × Σ∗ → Q Formally, we can define δ∗ recursively as follows: δ∗(q, ϵ) def = q δ∗(q, sa) def = δ(δ∗(q, s), a) for all q ∈ Q, s ∈ Σ∗, and a ∈ Σ. Jaime Arias Computability and Complexity 15/24
  38. Regular Languages Extending the transition function – Formal Definition δ∗(q1,

    011) = δ(δ∗(q1, 01), 1) = δ(δ(δ∗(q1, 0), 1), 1) = δ(δ(δ(δ∗(q1, ϵ), 0), 1), 1) = δ(δ(δ(q1, 0), 1), 1) = = = q1 q2 0 1 1 0 Jaime Arias Computability and Complexity 16/24
  39. Regular Languages Extending the transition function – Formal Definition δ∗(q1,

    011) = δ(δ∗(q1, 01), 1) = δ(δ(δ∗(q1, 0), 1), 1) = δ(δ(δ(δ∗(q1, ϵ), 0), 1), 1) = δ(δ(δ(q1, 0), 1), 1) = = = q1 q2 0 1 1 0 Jaime Arias Computability and Complexity 16/24
  40. Regular Languages Extending the transition function – Formal Definition δ∗(q1,

    011) = δ(δ∗(q1, 01), 1) = δ(δ(δ∗(q1, 0), 1), 1) = δ(δ(δ(δ∗(q1, ϵ), 0), 1), 1) = δ(δ(δ(q1, 0), 1), 1) = δ(δ(q1, 1), 1) = = q1 q2 0 1 1 0 Jaime Arias Computability and Complexity 16/24
  41. Regular Languages Extending the transition function – Formal Definition δ∗(q1,

    011) = δ(δ∗(q1, 01), 1) = δ(δ(δ∗(q1, 0), 1), 1) = δ(δ(δ(δ∗(q1, ϵ), 0), 1), 1) = δ(δ(δ(q1, 0), 1), 1) = δ(δ(q1, 1), 1) = = q1 q2 0 1 1 0 Jaime Arias Computability and Complexity 16/24
  42. Regular Languages Extending the transition function – Formal Definition δ∗(q1,

    011) = δ(δ∗(q1, 01), 1) = δ(δ(δ∗(q1, 0), 1), 1) = δ(δ(δ(δ∗(q1, ϵ), 0), 1), 1) = δ(δ(δ(q1, 0), 1), 1) = δ(δ(q1, 1), 1) = δ(q2, 1) = q1 q2 0 1 1 0 Jaime Arias Computability and Complexity 16/24
  43. Regular Languages Extending the transition function – Formal Definition δ∗(q1,

    011) = δ(δ∗(q1, 01), 1) = δ(δ(δ∗(q1, 0), 1), 1) = δ(δ(δ(δ∗(q1, ϵ), 0), 1), 1) = δ(δ(δ(q1, 0), 1), 1) = δ(δ(q1, 1), 1) = δ(q2, 1) = q1 q2 0 1 1 0 Jaime Arias Computability and Complexity 16/24
  44. Regular Languages Extending the transition function – Formal Definition δ∗(q1,

    011) = δ(δ∗(q1, 01), 1) = δ(δ(δ∗(q1, 0), 1), 1) = δ(δ(δ(δ∗(q1, ϵ), 0), 1), 1) = δ(δ(δ(q1, 0), 1), 1) = δ(δ(q1, 1), 1) = δ(q2, 1) = q2 q1 q2 0 1 1 0 Jaime Arias Computability and Complexity 16/24
  45. Regular Languages Extending the transition function – Implementation δ∗(q, ϵ)

    def = q δ∗(q, sa) def = δ(δ∗(q, s), a) 1 def delta(self, state, symbol): 2 try: 3 return self.transitions[state][symbol] 4 except KeyError: 5 raise ValueError("The transition is not defined") 6 7 def delta_star(self, state, input_string): 8 current_state = state 9 for symbol in input_string: 10 current_state = self.delta(current_state , symbol) 11 return current_state Jaime Arias Computability and Complexity 17/24
  46. Regular Languages Extending the transition function – Implementation q1 q2

    0 1 1 0 δ∗(q1, 011) = q2 ... >>> dfa = DFA(states, alphabet, transitions , initial_s, final_states) >>> dfa.delta_star(initial_s, '011') 'q2' Jaime Arias Computability and Complexity 18/24
  47. Regular Languages String Accepted by a DFA String Accepted by

    a DFA A string s ∈ Σ∗ is accepted or recognized by a DFA A iff it takes the initial state q0 to a final state. That is: δ∗(q0, s) ∈ F Jaime Arias Computability and Complexity 19/24
  48. Regular Languages String Accepted by a DFA String Accepted by

    a DFA A string s ∈ Σ∗ is accepted or recognized by a DFA A iff it takes the initial state q0 to a final state. That is: δ∗(q0, s) ∈ F q1 q2 0 1 1 0 s = 0 1 1 Jaime Arias Computability and Complexity 19/24
  49. Regular Languages String Accepted by a DFA String Accepted by

    a DFA A string s ∈ Σ∗ is accepted or recognized by a DFA A iff it takes the initial state q0 to a final state. That is: δ∗(q0, s) ∈ F q1 q2 0 1 1 0 s = 0 1 1 ✓ Jaime Arias Computability and Complexity 19/24
  50. Regular Languages String Accepted by a DFA String Accepted by

    a DFA A string s ∈ Σ∗ is accepted or recognized by a DFA A iff it takes the initial state q0 to a final state. That is: δ∗(q0, s) ∈ F q1 q2 0 1 1 0 0 s = 0 1 1 0 Jaime Arias Computability and Complexity 19/24
  51. Regular Languages String Accepted by a DFA String Accepted by

    a DFA A string s ∈ Σ∗ is accepted or recognized by a DFA A iff it takes the initial state q0 to a final state. That is: δ∗(q0, s) ∈ F q1 q2 0 1 1 0 0 s = 0 1 1 0  Jaime Arias Computability and Complexity 19/24
  52. Regular Languages String Accepted by a DFA – Exercise s0

    s1 s2 0 1 0 1 1 0 ▷ Which strings are accepted? (i) 01 (ii) 0011 (iii) 0101100 (iv) 10101 Jaime Arias Computability and Complexity 20/24
  53. Regular Languages String Accepted by a DFA – Implementation q1

    q2 0 1 1 0 ▷ is ”011” accepted by A? ▷ is ”0110” accepted by A? 1 def accepts(self, input_string): 2 final_state = self.delta_star(self.q0, input_string) 3 return final_state in self.F Jaime Arias Computability and Complexity 21/24
  54. Regular Languages String Accepted by a DFA – Implementation q1

    q2 0 1 1 0 ▷ is ”011” accepted by A? ▷ is ”0110” accepted by A? 1 def accepts(self, input_string): 2 final_state = self.delta_star(self.q0, input_string) 3 return final_state in self.F >>> dfa.accepts("011") True >>> dfa.accepts("0110") False Jaime Arias Computability and Complexity 21/24
  55. Regular Languages Language Accepted by a DFA Language Accepted by

    a DFA The language accepted or recognized by a DFA A is the set of all strings accepted by A. That is: L(A) def = {s ∈ Σ∗ | δ∗(q0, s) ∈ F} Jaime Arias Computability and Complexity 22/24
  56. Regular Languages Language Accepted by a DFA Language Accepted by

    a DFA The language accepted or recognized by a DFA A is the set of all strings accepted by A. That is: L(A) def = {s ∈ Σ∗ | δ∗(q0, s) ∈ F} q1 q2 0 1 1 0 L(A) = {1, 01, 11, 011, 01001, . . . } Jaime Arias Computability and Complexity 22/24
  57. Regular Languages Language Accepted by a DFA Language Accepted by

    a DFA The language accepted or recognized by a DFA A is the set of all strings accepted by A. That is: L(A) def = {s ∈ Σ∗ | δ∗(q0, s) ∈ F} q1 q2 0 1 1 0 L(A) = {1, 01, 11, 011, 01001, . . . } = {s ∈ {0, 1}∗ | s ends in a 1} Jaime Arias Computability and Complexity 22/24
  58. Regular Languages Formal Definition Equivalence Two DFAs A1 and A2

    are equivalent iff they accept the same language L(A1) = L(A2) Jaime Arias Computability and Complexity 23/24
  59. Regular Languages Formal Definition Equivalence Two DFAs A1 and A2

    are equivalent iff they accept the same language L(A1) = L(A2) Regular Language A language L is called a regular language iff some DFA A recognizes it L = L(A) Jaime Arias Computability and Complexity 23/24