Title

ECS 120 Theory of Computation
Deterministic finite automata (DFA)
David Doty (adapted from slides authored by Julian Panetta)
University of California, Davis

Finite automata (Chapter 3)

Models of computation

  • We will study several computational models of increasing power.
  • The simplest model is a finite state machine or finite automaton (FA).
    • FAs are a good model for computers with extremely limited memory.
    • Beyond theory, they are also remarkably useful:
      • to implement advanced search features in text editors;
      • as part of a compiler’s front end (the lexer, or tokenizer);
      • for formally defining network protocols (e.g., TCP);
      • designing simple embedded electronic systems;
      • structuring the code of a complicated user interface involving many states;
      • for verifying software correctness using model checking;
      • and more…
  • Specifically, we will start with deterministic finite automata (DFAs), and show how they can solve a certain class of decision problems.

Deterministic finite automata

A DFA can be visually specified by a state diagram:

data/images/dfa/dfa_m1/diagram.svg

(A DFA can be seen as a special type of directed graph with labeled edges and nodes.)

  • This example has three states (\(a\), \(b\), \(c\)):
  • State \(a\) is the start state (denoted by an arrow pointing to it from nowhere).
  • State \(b\) is an accept state (denoted by a double circle).
  • The arrows between states are called transitions.

Operation of a DFA

data/images/dfa/dfa_m1/diagram.svg
  • A DFA processes an input string one symbol at a time.
  • After reading the last input symbol, it produces a Boolean output: “accept” or “reject” depending on whether it ends up in an accept state.

Example processing the string 1101:

1101

  • Begin in the start state \(a\). data/images/dfa/dfa_m1/state_a.svg

  • Read 1 and transition to state \(b\). data/images/dfa/dfa_m1/state_b.svg

    1101

  • Read 1 and transition to state \(b\). data/images/dfa/dfa_m1/state_b.svg

    1101

  • Read 0 and transition to state \(c\). data/images/dfa/dfa_m1/state_c.svg

    1101

  • Read 1 and transition to state \(b\). data/images/dfa/dfa_m1/state_b.svg

    1101

  • Since the final state \(b\) is an accept state, the DFA accepts the string.

Operation of a DFA

data/images/dfa/dfa_m1/diagram.svg

Which string(s) below are accepted by this DFA?

Formal definition of a DFA (syntax)

data/images/dfa/dfa_m1/diagram.svg
  • The formal definition of syntax of a model of computation says what an instance of the model is. (Semantics is a formal definition of what the model does; more later.)

  • The DFA above has five associated objects that we need to formalize:

    1. A set of states (\(\{a, b, c\}\)) — call this \(Q\).
    2. A set of symbols that it expects to read
      from the input string (\(\{0, 1\}\)) — call this \(\Sigma\).
    3. Rules for moving between states when
      these symbols are read (the arrows) — ???
    4. A start state (\(a\)).
    5. Accept/“final” states (\(\{b\}\)). — call this \(F\).

How do we formalize the transitions?

  • An arrow from state \(x\) to state \(y\) labeled with symbol \(\sigma\) means: when the DFA is in state \(x\) and reads \(\sigma\) from the input, it transitions to \(y\).
  • Think of this as a function that maps \((x, \sigma)\) to \(y\).
  • We call this function the transition function \(\delta\): \[ \delta(x, \sigma) = y \quad \quad \quad \fragment{\delta: Q \times \Sigma \to Q} \]

Formal definition of a DFA (syntax)

All together, we have the definition:

A deterministic finite automaton (DFA) is a 5-tuple \((Q, \Sigma, \delta, s, F)\) where:

  1. \(Q\) is a finite set of states.
  2. \(\Sigma\) is a finite set of symbols called the input alphabet.
  3. \(\delta: Q \times \Sigma \to Q\) is the transition function.
  4. \(s \in Q\) is the start state.
  5. \(F \subseteq Q\) is the set of accept states.

True or false:
a DFA must have at least one accept state?

Formal definition of a DFA (syntax)

All together, we have the definition:

A deterministic finite automaton (DFA) is a 5-tuple \((Q, \Sigma, \delta, s, F)\) where:

  1. \(Q\) is a finite set of states.
  2. \(\Sigma\) is a finite set of symbols called the input alphabet.
  3. \(\delta: Q \times \Sigma \to Q\) is the transition function.
  4. \(s \in Q\) is the start state.
  5. \(F \subseteq Q\) is the set of accept states.

True or false:
every state must have exactly one outbound transition for each symbol in the input alphabet?

Note that \(|Q \times \Sigma| = |Q| \cdot |\Sigma|\)

Translating a state diagram into the formal definition

data/images/dfa/dfa_m1/diagram.svg

We can formally describe this DFA as \(M_1 = (Q, \Sigma, \delta, a, F)\) where:

  1. \(Q = \{a, b, c\}\)

  2. \(\Sigma = \{0, 1\}\)

  3. \(\delta\) is defined by the following lookup table:

    \(0\) \(1\)
    \(a\) \(a\) \(b\)
    \(b\) \(c\) \(b\)
    \(c\) \(b\) \(b\)
  4. \(a\) is the start state.

  5. \(F = \{b\}\)

The language decided by a DFA

data/images/dfa/dfa_m1/diagram.svg
  • The set of strings \(A\) that a machine \(M\) accepts is called the language decided by \(M\).
  • We denote this formally as \(L(M) = A\).
    • This means \(M\) decides the language \(A\). (Some authors say recognizes.) Note: We say \(M\) accepts or rejects strings, but it decides languages.
    • If \(M\) accepts no strings (e.g., \(F = \emptyset\)), does it decide a language? Yes: \(L(M) = \emptyset\).
  • What is the language of our example DFA above (\(M_1\))?
    • The strings it accepts must end in a 1 followed by an even number of 0s.
    • So, \(L(M_1) = \setbuild{w \in \{0, 1\}^*}{w \text{ has at least one } 1 \text{ and has an even number of } 0\text{s after the last } 1 }\).
    • How can we prove this formally? Using the upcoming formal definition of computation!

Some more examples

data/images/dfa/dfa_m2.svg

What is the formal description of this DFA?

\[M_2 = (\fragment{\{a, b\},} \fragment{\{0, 1\},} \fragment{\delta,} \fragment{a,} \fragment{\{b\}}),\]

where \(\delta\) is defined by the table:

\(0\) \(1\)
\(a\) \(a\) \(b\)
\(b\) \(a\) \(b\)

What language does this DFA decide?

Some more examples

data/images/dfa/dfa_m3.svg

What is the formal description of this DFA?

\[M_3 = (\{a, b\}, \{0, 1\}, \delta, a, \{a\}),\]

where \(\delta\) is defined by the table:

\(0\) \(1\)
\(a\) \(a\) \(b\)
\(b\) \(a\) \(b\)

What language does this DFA recognize?

\[ L(M_3) = \setbuild{w \in \{0, 1\}^*}{w[|w|] = 0} \fragment{\cup \{\emptystring\}} \fragment{= \setcomplement{L(M_2)}} \]

Designing DFAs

  • Creating a DFA to solve a decision problem can require creativity, just like programming.
  • Guiding principles:
    • Think about how you would solve the problem, reading symbols one at a time,
      always ready to answer “yes” or “no” for the string read so far.
    • What information do you need to keep track of?
      Usually not the entire string, but some compact summary of its relevant properties.
  • Simple example: decide if a string contains an even number of 1s.
    • What do we need to keep track of? The parity of the number of 1s seen so far!
    • How many states do we need? Two: one for even, one for odd.
    • What are the transitions?
      • Remain in the same state on 0
      • Transition between states on 1
    • What are the start and accept states?
even odd odd even->even 0 even->odd 1 odd->even 1 odd->odd 0 8e833e7b-f2da-45fd-b05e-5f32311434cd 8e833e7b-f2da-45fd-b05e-5f32311434cd->even

DFA design example: String length modulo 3

Task: Design a DFA that accepts strings of symbol \(\string{a}\) whose length is a multiple of 3.

\[ \setbuild{\string{a}^{3n}}{n \in \N} = \setbuild{w \in \{\string{a} \}^*}{|w| \text{ is a multiple of 3}} = \string{\{\emptystring, aaa, aaaaaa, \ldots\}} \]

  • What do we need to keep track of?
    • The length of the string modulo 3: 0, 1, or 2.
  • What states and transitions do we need?
q0 q1 q1 q2 q2 q0->q1 a q1->q2 a q2->q0 a 5808362c-cf51-4b72-b43d-14cfd3334b49 5808362c-cf51-4b72-b43d-14cfd3334b49->q0

DFA design example: String length modulo 3

Task: Design a DFA that accepts strings of symbol \(\string{a}\) whose length is of the form \(3n + 2\)

\[ \setbuild{\string{a}^{3n + 2}}{n \in \N} = \setbuild{w \in \{\string{a} \}^*}{|w| \equiv 2 \mod 3} = \string{\{aa, aaaaa, \ldots\}} \]

  • What do we need to keep track of?
    • The length of the string modulo 3: 0, 1, or 2.
  • What states and transitions do we need?
data/images/dfa/len_mod3_eq2.svg

DFA design example: Binary values

Task: Design a DFA that accepts binary strings that represent an even number.

\[ \setbuild{w \in \{0, 1\}^*}{w \text{ represents a multiple of 2 in binary}} \]

We’ve already done this! Which of the following is it?

Binary numbers are even if and only if they end in 0.

DFA design example: Binary number congruence

Task: Design a DFA that accepts binary strings that represent a multiple of 3.

  • What do we need to keep track of?
    • The integer value of the bitstring read so far, modulo 3: 0, 1, or 2.
  • What states and transitions do we need?
    • Reading a 0 multiplies the number by 2. (shift left)
      • If \(n = 3k\) for some \(k \in \N\) (\(n\) is a multiple of 3), then \(2n = 6k\) (also a multiple of 3)
      • If \(n = 3k+1\) for some \(k \in \N\) (\(n \equiv 1 \mod 3\)), then \(2n = 2(3k+1) = 6k+2\) (\(2n \equiv 2 \mod 3\))
      • If \(n = 3k+2\) for some \(k \in \N\) (\(n \equiv 2 \mod 3\)), then \(2n = 2(3k+2) = 6k+4\) (\(2n \equiv 1 \mod 3\))
    • Reading a 1 multiplies the number by 2 and adds 1.
\(0\) \(1\)
\(q_0\) \(\fragment{q_0}\) \(\fragment{q_1}\)
\(q_1\) \(\fragment{q_2}\) \(\fragment{q_0}\)
\(q_2\) \(\fragment{q_1}\) \(\fragment{q_2}\)
a6571b4d-47ec-4009-86d8-bbafd2af07b1 q0 q0 a6571b4d-47ec-4009-86d8-bbafd2af07b1->q0 q0->q0 0 q1 q1 q0->q1 1 q1->q0 1 q2 q2 q1->q2 0 q2->q1 0 q2->q2 1

DFA design example: Substring

Task: Design a DFA that accepts binary strings that contain the substring \(\string{001}\).

  • What do we need to keep track of? Which parts of this substring have we seen so far?

  • What states do we need?

    • \(q\): input processed so far does not contain 001 or end with 0 or 00
    • \(q_0\): we just saw a single 0
    • \(q_{00}\): we just saw two 0s
    • \(q_{001}\): the full target substring \(\string{001}\) has been seen
  • What are the transitions?

    \(0\) \(1\)
    $q $ \(\fragment{q_0}\) \(\fragment{q }\)
    $q_0 $ \(\fragment{q_{00}}\) \(\fragment{q }\)
    $q_{00} $ \(\fragment{q_{00}}\) \(\fragment{q_{001}}\)
    $q_{001} $ \(\fragment{q_{001}}\) \(\fragment{q_{001}}\)
    data/images/dfa/substring_001.svg

DFA design example: Finite language

Task: Design a DFA that accepts strings that are in a given finite language.

Solution: Build a trie (prefix tree) data structure!

Example: \(A = \string{\{0, 000, 001, 1001, 1010, 10010\}}\)

data/images/dfa/trie.svg
  • One state for each prefix of a string in \(A\)
    (the input read so far).
  • Accept states: the strings in \(A\)
    (not only leaves: \(\string{0}\) and \(\string{1001}\) are prefixes of other strings in \(A\)).
  • Every state needs a transition on every symbol:
    missing ones go to a dead state that never accepts.
data/images/dfa/trie_dfa.svg

Formal definition of computation (semantics)

  • We have formally defined the structure of a DFA (its syntax) as a 5-tuple: \(M = (Q, \Sigma, \delta, s, F)\)
  • Now what about its semantics? How do we formally define the strings that \(M\) accepts?
  • Intuitively: those traversing a path through the DFA from \(s\) to an accept state.

The finite automaton \(M = (Q, \Sigma, \delta, s, F)\) accepts a string \(w = w_1 w_2 \ldots w_n \in \Sigma^n\) if there is a sequence of states \(r_0, r_1, \ldots, r_n \in Q\) such that:

  1. \(r_0 = s\)
  2. \(r_n \in F\)
  3. \(r_i = \delta(r_{i-1}, w_i)\) for all \(1 \leq i \leq n\)

\(M\) decides a language \(A\) (denoted \(L(M) = A\)) if \(A = \setbuild{w \in \Sigma^*}{M \text{ accepts } w}\).

A language/decision problem \(A\) is DFA-decidable if there is a DFA \(M\) such that \(L(M) = A\). Equivalently: if it uses a constant amount of memory and runs in exactly \(n\) steps on inputs \(w \in \Sigma^n\).

Formal definition of computation: Example

\(0\) \(1\)
\(a\) \(a\) \(b\)
\(b\) \(c\) \(b\)
\(c\) \(b\) \(b\)
data/images/dfa/dfa_m1/diagram.svg

According to the formal definition of computation, this DFA accepts the string \(w = \string{0100100}\) because the sequence of states it enters when processing this string is: \[ (r_0, r_1, r_2, r_3, r_4, r_5, r_6, r_7) = (a, a, b, c, b, b, c, b) \] which satisfies the three conditions:

  1. \(r_0 = a\) (the start state)
  2. \(r_7 = b \in F\) (the last state is an accept state)
  3. The transition rules are satisfied: \(a \xrightarrow{0} a \xrightarrow{1} b \xrightarrow{0} c \xrightarrow{0} b \xrightarrow{1} b \xrightarrow{0} c \xrightarrow{0} b\)

Demo of automaton simulator