4.6.2 · D4Theory of Computation

Exercises — Finite automata — DFA - formal definition (5-tuple), state diagrams

2,395 words11 min readBack to topic

Level 1 — Recognition

Can you read the tuple and the diagram, and quote the definition back?

Exercise 1.1

Given the DFA with , , , and (a) How many states does have? (b) What is the start state? (c) Which states are accepting? (d) What is ?

Recall Solution 1.1

This is pure reading of the 5-tuple — no computation. (a) states. (b) The start state is always (the third-listed component after and ). Here . (c) , so the only accepting state is . (d) Look up the row for : .

Exercise 1.2

Look at the diagram below.

Figure — Finite automata — DFA -  formal definition (5-tuple), state diagrams

(a) Which state is the start state, and how do you know? (b) Which state is accepting, and how do you know? (c) Write down and by reading the arrows.

Recall Solution 1.2

(a) The start state is — it is the one with an arrow coming from nowhere (the free arrow on the left pointing into it). (b) The accepting state is — it is drawn as a double circle, which means . (c) Follow the labelled arrows: the arrow leaving labelled 1 lands on , so . The arrow leaving labelled 0 lands on , so .


Level 2 — Application

Run the machine. Trace strings symbol by symbol using .

Exercise 2.1

Using from Exercise 1.1, trace the string from . Give the full sequence of states and say whether accepts or rejects .

Recall Solution 2.1

We apply one symbol at a time — this is the recursion unrolled left to right.

step reading from rule used to
1
2
3
4

So . Since (only accepts), rejects .

Exercise 2.2

Still using from Exercise 1.1, decide whether accepts (a) and (b) the empty string .

Recall Solution 2.2

(a) Trace : . End accepted. (b) For there is nothing to read: by the base case . Is ? No — . So is rejected. (A DFA accepts iff its start state is accepting.)


Level 3 — Analysis

Given a machine, discover WHAT LANGUAGE it recognizes; and reason about edge cases.

Exercise 3.1

Consider the DFA drawn below over .

Figure — Finite automata — DFA -  formal definition (5-tuple), state diagrams

Formally: , start , , and Describe in one plain-English sentence.

Recall Solution 3.1

Notice what each symbol does. A 1 is always a self-loop: for every . So 1s never change the state — they are ignored. A 0 steps you forward one place around the cycle .

So the state you end in equals (number of 0s read) mod 3: means "count of 0s is ", means "", means "".

Accept iff you end in . Therefore in words: strings whose number of 0s is a multiple of 3 (0, 3, 6, ... zeros). Check : it has zero 0s, and is a multiple of 3, and indeed — consistent. ✓

Exercise 3.2

For the machine of Exercise 1.1, is the string (four s) accepted? What single fact about on from makes this instant?

Recall Solution 3.2

From , reading gives — a self-loop. So reading any number of s from leaves you at : . Since , rejects . (In fact rejects any string of s only, because a b never moves you off once you're there.)


Level 4 — Synthesis

Now YOU build the machine: design , , from a language spec.

Exercise 4.1

Design a DFA over that accepts exactly the strings containing the substring ab (i.e. a immediately followed by b somewhere). Give the full 5-tuple and draw/describe the diagram.

Recall Solution 4.1

What must I remember? Only how close I am to completing an ab. Three situations:

  • = "I have not just seen an a that could start ab, and I've not yet seen ab."
  • = "the last symbol was an a — one b away from success."
  • = "I have already seen ab somewhere — success is locked in forever."

Transitions (reason each one):

  • (an a arms us), (a lone b does nothing).
  • (another a — still armed, last symbol is a), (a then b = success!).
  • , (once found, ab can't un-happen — is a trap of the good kind).

Tuple: , , start , .

Sanity trace : . End ⇒ accepted. ✓ ( contains ab.) Sanity trace : . End ⇒ rejected. ✓ (ba has no ab.)

Figure — Finite automata — DFA -  formal definition (5-tuple), state diagrams

Exercise 4.2

Design a DFA over accepting strings whose length is a multiple of 2 and whose last symbol (if any) is 1. Handle explicitly. Give the tuple.

Recall Solution 4.2

Two independent facts to track:

  • parity of length — even (needed) vs odd. Every symbol flips it.
  • last symbol — was it 1? Only a final 1 should be accepted.

Because the last symbol fact is only checked at the end, we fold it into the states. Track a pair (length-parity, last-symbol). Use states where the letter is length parity (=even, =odd) and the subscript is the last symbol; is the fresh state before any symbol.

Transitions (each symbol flips parity and records itself as last symbol):

  • From : 0, 1.
  • From /: 0, 1.
  • From /: 0, 1.

Accept states: we need even length and last symbol 1, i.e. .

  • : stays in , which is not in ⇒ rejected. Correct, since has no last symbol 1.
  • Trace : ⇒ accepted (length 2 even, ends in 1). ✓
  • Trace : ⇒ rejected (ends in 0). ✓

Tuple: , , start , .


Level 5 — Mastery

Prove things about , or reason at the level of the whole class of DFAs.

Exercise 5.1

Prove that composes over concatenation: for any state and strings ,

Recall Solution 5.1

We induct on the length of , because is defined by recursion peeling one symbol off the right.

Base case , i.e. . Then , so the left side is . The right side is , which by the base rule equals . Both sides equal — base holds.

Inductive step. Assume the claim for all strings of length ; let with and a single symbol. This closes the induction, so the identity holds for all .

Why we care: this is the formal justification for "feed , land in some state, then continue with as if starting fresh from there" — the reasoning you already used informally in every trace.

Exercise 5.2

A DFA has states. Show that if accepts any string of length , then accepts infinitely many strings. (This is the seed of the Pumping Lemma.)

Recall Solution 5.2

Let with be accepted, and list the states visited: That is states in the sequence . But there are only distinct states in . By the pigeonhole principle (more items than boxes ⇒ two items share a box), two of these visited states are the same: say with .

The substring takes the machine from back to the same state — a loop. Call this loop-piece (with ). Write where and .

Now use Exercise 5.1: from , reading lands at ; reading returns to ; so reading any number of times still returns to ; then finishes at . Hence Since , the strings have strictly increasing length — infinitely many distinct accepted strings.


Recall Feynman recap: what these 5 levels drilled

L1 you learned to read a machine (find start via the free arrow, accept via double circles). L2 you learned to run it (trace every symbol, judge only the final state). L3 you learned to reverse-engineer the language a machine recognizes. L4 you learned to build a machine from a spec, always testing . L5 you learned to prove things about all DFAs — composition of and the pigeonhole loop that seeds the Pumping Lemma. Related roads from here: NFAs, subset construction, Regular expressions, and Lexical analysis / tokenizers.