Deterministic Finite Automata

The simplest computational model — and the foundation of everything

5-Tuple Definition State Diagrams Product Construction Minimization

Use Arrow Keys to navigate • Interactive Canvas visualizations throughout

The Big Picture

Where DFAs fit in the Chomsky hierarchy

DFAs recognize Regular Languages (Type 3)

The simplest class in the hierarchy. Every DFA reads input left-to-right, one symbol at a time, with no memory beyond its current state.

Think of it this way

A DFA is like a vending machine with a fixed set of buttons. It has no scratch paper, no stack, no tape — just a finite number of states and transitions between them.

Click on the hierarchy levels to highlight them.

The Turnstile Analogy

A DFA is just a machine that reads input and changes state

Subway Turnstile

A turnstile has two states: Locked and Unlocked. Insert a coin → unlocks. Push → locks again. This IS a DFA!

Key DFA Properties

  • Deterministic — exactly ONE transition per (state, symbol)
  • Finite — fixed number of states
  • No memory — only the current state matters
  • Total function — must handle every possible input

Formal Definition: The 5-Tuple

A DFA is defined as M = (Q, Σ, δ, q₀, F)

The 5 Components

  • Q — finite set of states
  • Σ (Sigma) — finite input alphabet
  • δ (delta) — transition function: Q × Σ → Q
  • q₀ — start state (q₀ ∈ Q)
  • F — set of accept/final states (F ⊆ Q)

δ must be a TOTAL function

Every state must have exactly one transition for every symbol. If you can't think of where to go, add a dead state (trap state) that rejects.

Hover over the DFA diagram to see each component highlighted.

Extended Transition Function δ̂

Processing an entire string, one symbol at a time

Recursive Definition

Base: δ̂(q, ε) = q

Inductive: δ̂(q, wa) = δ(δ̂(q, w), a)

In plain English: process all of w first to get some state, then take one more step on symbol a.

δ̂(q₀, "aab")
= δ(δ̂(q₀, "aa"), b)
= δ(δ(δ̂(q₀, "a"), a), b)
= δ(δ(δ(δ̂(q₀, ε), a), a), b)
= δ(δ(δ(q₀, a), a), b)
= δ(δ(q₁, a), b)
= δ(q₁, b)
= q₂ ∈ F ✓ Accept!

Example: Even Number of 0s

The classic first DFA — accepts binary strings with an even count of 0s

Input: 10010

Design Insight

The DFA only needs to remember one bit of information: is the count of 0s seen so far even or odd?

  • E (Even) — seen even # of 0s → accept
  • O (Odd) — seen odd # of 0s → reject

Transition Table

State01
→ *EOE
OEO

→ = start, * = accept. On 1, state unchanged (1 doesn't affect 0-count). On 0, state toggles.

DFA Simulator Playground

Enter any binary string — watch the "even 0s" DFA process it live

Enter a binary string and press Step.

Try these strings

  • ε (empty) — 0 zeros, that's even → Accept
  • 1111 — 0 zeros → Accept
  • 0 — 1 zero → Reject
  • 00 — 2 zeros → Accept
  • 10010 — 2 zeros → Accept
  • 000 — 3 zeros → Reject

Why this works

The DFA acts as a parity checker. Each 0 toggles between E and O. Each 1 stays put. After reading all input, if we're at E, the count was even.

Language of a DFA

L(A) = the set of all strings the DFA accepts

Formal Definition

L(A) = { w ∈ Σ* | δ̂(q₀, w) ∈ F }

The language is all strings w such that, starting from q₀ and reading all of w, you end up in an accept state.

For the even-0s DFA:

L(A) = { w ∈ {0,1}* | w has an even number of 0s }

  • ε ∈ L(A) — zero 0s is even
  • 1, 11, 111, ... ∈ L(A) — zero 0s
  • 00, 1001, 0110 ∈ L(A) — two 0s
  • 0, 010, 000 ∉ L(A) — odd 0s

Regular Language

A language is regular if and only if some DFA recognizes it. The class of regular languages equals the class of DFA-recognizable languages.

How to Design a DFA

A systematic approach to building DFAs from scratch

The State = Memory Principle

Ask yourself: "What do I need to remember about the input so far?" Each distinct "memory situation" becomes a state.

Step-by-Step Strategy

  1. Identify what to track — What property of the input determines acceptance?
  2. List all possible memory states — What are ALL the distinct situations?
  3. Define transitions — For each (state, symbol), where do you go?
  4. Mark start and accept states — Which memory state means "input seen so far is empty"? Which means "accept"?
  5. Add a dead state if needed — If some transitions have no natural target, add a trap state.

Example: "ends with 01"

What do I need to remember? The last 1-2 characters.

  • q₀: haven't seen useful suffix yet (or last char was 1 after non-0)
  • q₁: last char was 0 (potential start of "01")
  • q₂: last two chars were "01" → ACCEPT

Common Trap

Don't try to remember the entire input. DFAs have finite memory. Focus on what matters for the acceptance condition.

Building "Ends with 01"

Watch the DFA get constructed step by step

Test it: input "1001"

q₀ —1→ q₀ (last char: 1)
q₀ —0→ q₁ (last char: 0)
q₁ —0→ q₁ (last char: 0)
q₁ —1→ q₂ (ends with 01!)
q₂ ∈ F → ACCEPT ✓

State meanings

q₀: no useful suffix yet
q₁: last char was 0
q₂: last two chars were 01 (accept)

Product Construction

Combining two DFAs to handle union and intersection

How it works

Given DFA₁ = (Q₁, Σ, δ₁, q₁, F₁) and DFA₂ = (Q₂, Σ, δ₂, q₂, F₂):

  • States: Q₁ × Q₂ (all pairs)
  • Start: (q₁, q₂)
  • δ((p,q), a) = (δ₁(p,a), δ₂(q,a))
  • Union: F = F₁ × Q₂ ∪ Q₁ × F₂
  • Intersection: F = F₁ × F₂

Running two DFAs in parallel

Imagine running both DFAs simultaneously on the same input. Each state in the product tracks where both DFAs are. For union, accept if either accepts. For intersection, accept if both accept.

Challenge A: Trace the DFA

Given this DFA, predict whether each string is accepted or rejected

DFA accepts strings over {a,b} that contain "ab" as a substring.

1. Input: "ba"

2. Input: "aab"

3. Input: "bbb"

4. Input: "abba"

Complement of a DFA

To complement, just swap accept and non-accept states

Original: accepts even # of 0s

Complement is trivial for DFAs

Given DFA A = (Q, Σ, δ, q₀, F):
Complement: A' = (Q, Σ, δ, q₀, Q \ F)

Same states, same transitions, same start. Just flip which states are accepting!

Only works for DFAs!

You cannot complement an NFA by swapping accept states. You must first convert to a DFA, then complement. This is a classic exam mistake.

Why it works

Since δ is a total function, every string ends in exactly one state. If that state was accepting, it's now rejecting, and vice versa. Every string changes sides.

Real-World DFA: Tennis Scoring

Model a tennis game as a DFA — who wins?

Score: Love-Love

State: 0-0

Tennis as a DFA

Σ = {s, o} — server or opponent wins a point

States = all possible score combinations

Accept = "Server Wins"

Note the deuce rule: at 40-40, someone must win by 2 points.

Challenge B: Fix the Bug

This DFA is supposed to accept strings with an even number of 1s, but it has a bug

Q = {E, O}
Σ = {0, 1}
δ(E, 0) = O ← BUG?
δ(E, 1) = O
δ(O, 0) = E
δ(O, 1) = E
q₀ = E, F = {E}

What's wrong?

DFA Minimization: Table-Filling

Find equivalent states and merge them to get the smallest DFA

Step 0 / 10

Algorithm

  1. Base: Mark all pairs (p,q) where one is accept and one isn't
  2. Inductive: For unmarked (p,q), check if (δ(p,a), δ(q,a)) is marked for any a ∈ Σ. If so, mark (p,q)
  3. Repeat until no new marks
  4. Result: Unmarked pairs are equivalent — merge them!

Myhill-Nerode Theorem

The theoretical foundation for DFA minimization

The Theorem

A language L is regular if and only if the relation ≡L has a finite number of equivalence classes.

Moreover: the number of classes = the number of states in the minimum DFA.

The Equivalence Relation ≡L

x ≡L y iff for ALL strings z: xz ∈ L ↔ yz ∈ L

Two strings are equivalent if no suffix z can tell them apart.

Example: even 0s

Strings "10" and "0" are NOT equivalent: with z="0", "100" has 1 zero (reject) but "00" has 2 zeros (accept).

But "10" and "110" ARE equivalent: both have exactly 1 zero, so same parity for any suffix.

DFA Design Patterns

Common patterns you'll use again and again

Pattern: "At Least One a"

Two states: "not yet seen a" and "seen a". Once you see an 'a', stay in accept forever.

States needed: 2

General Rule

The number of states reflects how much you need to remember. "At least one" needs 1 bit. "Ends with xy" needs to track the last 2 chars. "Divisibility by n" needs n remainder states.

Challenge C: Pick the Right DFA

For each language, choose the correct number of states needed

1. L = { w ∈ {0,1}* | w has at least three 1s }

2. L = { w ∈ {a,b}* | w ends with "aba" }

3. L = { w ∈ {0,1}* | binary value of w is divisible by 5 }

4. To complement a DFA, you need to:

Common DFA Mistakes

Pitfalls that cost points on exams

1. Missing Transitions

Every state must have exactly one transition per symbol. If you forget one, it's not a valid DFA. Add a dead/trap state for unwanted transitions.

2. Forgetting the Dead State

When a path should reject, don't just "leave it hanging." Add a non-accepting state with self-loops on all symbols.

3. Complementing an NFA

You cannot swap accept states on an NFA to get the complement. You must first convert NFA → DFA, THEN complement. This is because NFAs can be in multiple states simultaneously.

4. Confusing State Count with Complexity

Having more states doesn't mean the language is "harder." The minimum DFA state count is determined by the Myhill-Nerode equivalence classes.

5. Incomplete 5-Tuple

When defining a DFA formally, you must specify ALL five components: Q, Σ, δ, q₀, F. Missing any one makes the definition invalid.

Quick Self-Check

  • Does every state have |Σ| outgoing transitions?
  • Is there exactly ONE start state?
  • Is δ a function (not a relation)?
  • Can every input string be processed to completion?

DFA Summary & Cheat Sheet

Everything you need to know on one slide

Core Facts

  • DFA = (Q, Σ, δ, q₀, F)
  • δ: Q × Σ → Q (total function)
  • Accepts w if δ̂(q₀, w) ∈ F
  • L(A) = { w | δ̂(q₀, w) ∈ F }

Closure Properties

  • Complement: swap F and Q\F
  • Union: product construction, accept if either
  • Intersection: product construction, accept if both

Minimization

  • Table-filling: mark distinguishable pairs
  • Myhill-Nerode: # classes = # min DFA states
  • Minimum DFA is unique (up to renaming)

Quiz 1: Multiple Choice

Test your understanding of DFA fundamentals

Q1: What makes a DFA "deterministic"?

Q2: What is the minimum # of states for a DFA accepting {w | w has odd # of a's}?

Q3: In the product construction for L₁ ∩ L₂, a state (p,q) is accepting when:

Quiz 2: Trace Exercise

Given this DFA, trace the input and determine the result

DFA: divisible by 3 in binary. States = remainders {0, 1, 2}. Accept = {0}.

Trace input "1010" (binary 10)

Fill in each state transition:

Start: q = 0
Read 1: δ(0,1) =
Read 0: δ(?,0) =
Read 1: δ(?,1) =
Read 0: δ(?,0) =
Accept/Reject?

Quiz 3: Design a DFA

Build the transition table for a DFA that accepts strings over {a,b} with an even number of a's AND an even number of b's

Hint

You need to track the parity of BOTH a's and b's. Think product construction: one DFA for even a's × one DFA for even b's.

States: (even-a, even-b), (even-a, odd-b), (odd-a, even-b), (odd-a, odd-b)

Shorthand: EE, EO, OE, OO

Start state: EE (0 a's and 0 b's — both even)
Accept state: EE

Fill in the transitions:

Stateon 'a'on 'b'
EE (start, accept)
EO
OE
OO