Title

ECS 120 Theory of Computation
DFA Formal Definitions and Examples
Julian Panetta
University of California, Davis

Formal Definition of a DFA (syntax)

data/images/dfa/dfa_m1/diagram.svg
  • While state diagrams are easy to grasp intuitively, we need a precise, formal definition and good notation to prove theorems about them.

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

    1. A set of states (\(\{q_1, q_2, q_3\)}) — 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 (\(q_1\)).
    5. Accept/“final” states (\(\{q_2\}\)). — call this \(F\).

How do we formalize the transitions?

  • An arrow from state \(x\) to state \(y\) labeled with symbol \(a\) means: when the DFA is in state \(x\) and reads \(a\) from the input, it transitions to \(y\).
  • Think of this as a function that maps \((x, a)\) to \(y\).
  • We call this function the transition function \(\delta\): \[ \delta(x, a) = 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 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?

  • True
  • False

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 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 alphabet?

  • True
  • False

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, q_1, F)\) where:

  1. \(Q = \{q_1, q_2, q_3\}\)

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

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

    \(0\) \(1\)
    \(q_1\) \(q_1\) \(q_2\)
    \(q_2\) \(q_3\) \(q_2\)
    \(q_3\) \(q_2\) \(q_2\)
  4. \(q_1\) is the start state.

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

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{\{q_1, q_2\},} \fragment{\{0, 1\},} \fragment{\delta,} \fragment{q_1,} \fragment{\{q_2\}}),\]

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

\(0\) \(1\)
\(q_1\) \(q_1\) \(q_2\)
\(q_2\) \(q_1\) \(q_2\)

What language does this DFA decide?

  • \(L(M_2) = \emptyset\)
  • \(L(M_2) = \setbuild{w \in \{0, 1\}^*}{\texttt{even}\big(\#_1(w)\big)}\)
  • \(L(M_2) = \setbuild{w \in \{0, 1\}^*}{\lnot\texttt{even}\big(\#_1(w)\big)}\)
  • \(L(M_2) = \setbuild{w \in \{0, 1\}^*}{w[|w|] = 1}\)

Some More Examples

data/images/dfa/dfa_m3.svg

What is the formal description of this DFA?

\[M_3 = (\{q_1, q_2\}, \{0, 1\}, \delta, q_1, \{q_1\}),\]

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

\(0\) \(1\)
\(q_1\) \({q_1}\) \({q_2}\)
\(q_2\) \({q_1}\) \({q_2}\)

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?

  • 3f8979ce-9bb7-4fff-bc79-87ba393d0c45 q1 q1 3f8979ce-9bb7-4fff-bc79-87ba393d0c45->q1 q1->q1 0 q2 q2 q1->q2 1 q2->q1 0 q2->q2 1
  • ad0d8d4b-ccf5-4735-acff-89bbdeab1c72 q1 q1 ad0d8d4b-ccf5-4735-acff-89bbdeab1c72->q1 q1->q1 0 q2 q2 q1->q2 1 q2->q2 1 q3 q3 q2->q3 0 q3->q2 0,1
  • 2d8db2bc-bacc-4db7-8a5a-8cde741feaa2 q1 q1 2d8db2bc-bacc-4db7-8a5a-8cde741feaa2->q1 q1->q1 0 q2 q2 q1->q2 1 q2->q1 0 q2->q2 1

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\))
    • 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: Dictionary

Task: Design a DFA that accepts strings that are in a given dictionary (finite list of words).

Solution: Build a trie (prefix tree) data structure!
(With some minor modifications to satisfy the formal definition of a DFA.)

Formal Definition of Computation

  • 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 exists 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 exists 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\)
\(q_1\) \(q_1\) \(q_2\)
\(q_2\) \(q_3\) \(q_2\)
\(q_3\) \(q_2\) \(q_2\)
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) = (q_1, q_1, q_2, q_3, q_2, q_2, q_3, q_2) \] which satisfies the three conditions:

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

Demo of Automaton Simulator