skip to content

Why must a finite automaton be deterministic and total before you complement it by flipping its accepting states?

level: middleimportance: should knowfreq 45%

answer

  1. flip is cheap, but only sometimes
  2. complement wants a total machine
  3. missing edge, nothing to flip
  4. some run versus every run
  5. determinise first, then flip

basics

~20 s

Flipping accepting states complements the language only for a deterministic, total machine. A missing transition leaves a rejected string with no state to flip, and flipping a nondeterministic machine answers "some run ends non-accepting" instead of "no run accepts".

solid answer

~50 s

Flipping works because a deterministic total machine gives every input **exactly one** run, ending in exactly one state, so inverting the accepting set inverts every verdict. Break either property and the argument collapses. If the machine is **partial**, a string that falls off a missing edge is rejected by the original *and* by the flipped copy, so those strings are lost from the complement; the fix is to add one non-accepting trap state, route every missing edge to it, and let the trap become accepting when you flip. If the machine is **nondeterministic**, an input has many runs, and the flipped machine accepts when *some* run ends outside the original accepting set — which is not the same as *no* run accepting. You must determinise first, which can cost exponentially many states, and only then flip.

go deeper

for a junior

Remember the headline: swapping accepting and non-accepting states complements the language, but only for a machine that is deterministic and has an outgoing edge for every symbol from every state.

for a middle

Explain both preconditions with a concrete failure each: a stuck string that never reaches a state to flip, and a nondeterministic machine where one surviving run makes the flipped copy agree with the original.

for a senior

Show the repair sequence and its cost: complete with a trap, determinise when needed, then flip — and note that the flipped trap must be accepting.

for a principal

Treat negation as the expensive primitive in any rule algebra. Decide where in a pipeline it is allowed to appear, and on which operand, because everything else combines in polynomial time.

## What flipping is supposed to do Complementing a language means accepting exactly the strings the original rejects. For a **deterministic, total** finite automaton the construction is as cheap as it gets: keep the states, the alphabet, the transitions and the start state exactly as they are, and swap the accepting and non-accepting sets. The state count does not change at all. The justification is a single sentence, and an interviewer wants to hear it rather than the recipe: in a deterministic total machine every input string has **exactly one** run, and that run ends in **exactly one** state. Acceptance is therefore a yes/no function of that final state, so inverting the set of final states inverts the answer on every string. Both words in "deterministic total" are load-bearing, and each one fails differently. ## Failure one: the machine is partial A partial machine has states with no outgoing edge on some symbol. Such a machine rejects by *getting stuck*, not by ending in a non-accepting state. Flipping the accepting set does nothing for those strings: they still get stuck, and the flipped machine still rejects them — yet they belong in the complement, because the original rejected them. Concretely, take a machine over the symbols `a` and `b` that accepts only `a`, with no `b` edges anywhere. Its complement should accept `b`. Flip its accepting states and the copy still has no `b` edge, so it still rejects `b`. The flipped machine accepts a strict subset of the true complement. The repair is mechanical: 1. Add one fresh **trap state** with a self-loop on every symbol. 2. Route every missing transition to the trap. The machine now has exactly one edge per state per symbol and accepts the same language as before. 3. Flip. The trap, previously non-accepting, **becomes accepting** — which is right, because exactly the strings that drove the original into the trap are the ones it rejected. That last point catches people out: a candidate who adds the trap and then leaves it rejecting after the flip has reintroduced the original bug at one remove. ## Failure two: the machine is nondeterministic A nondeterministic machine accepts a string when **at least one** run ends in an accepting state. It rejects when **every** run fails. Flipping the accepting set changes what each run reports but leaves the existential quantifier alone, so the flipped machine accepts when *some* run ends outside the original accepting set. That is a different question, and the two can both say yes on the same string. Take a machine with a start state `s` and an accepting state `t`, where `s` has two edges on `a`: one back to `s` and one to `t`. It accepts any nonempty run of `a`s. Flip the accepting set so `s` accepts and `t` does not. On input `a` the runs end in `s` or in `t`; the run ending in `s` is accepting, so the flipped machine accepts `a` — and so did the original. A machine and its supposed complement agreeing on a string is proof the construction is wrong. The legal route is determinise first, complete the result, then flip. ## What it costs | what you have | complement recipe | state count | |---|---|---| | deterministic and total | flip the accepting set | unchanged | | deterministic, partial | add a trap, route missing edges, flip | one more than before | | nondeterministic | determinise, complete, flip | up to two to the power of the original count | The asymmetry is the reason the order of closure operations matters in practice. Intersection and union by pairing stay polynomial, so a pipeline that complements **once**, on the smallest operand, and pairs everything else is affordable; one that complements repeatedly on nondeterministic intermediates may not be. The blow-up is not merely an artefact of a lazy algorithm: families of machines are known for which any deterministic machine for the complement really does need exponentially many states. ## Why this matters away from the whiteboard Any rule engine that supports "everything except" is complementing something. If its rules compile to nondeterministic machines, the negation is the operation that can make build time explode, while the positive combinations stay cheap. Knowing which single step in a rule pipeline is the expensive one — and that it is negation, not conjunction — is the whole payoff of this material.

  • After you add a trap state and flip the accepting set, is the trap state accepting?
    Yes, and it must be. The strings that drive the original machine into the trap are exactly the ones it rejects, so the complement has to accept them. Leaving the trap non-accepting after the flip recreates the partial-machine bug: those strings would be rejected by both the machine and its supposed complement.
  • Are the regular languages closed under complement, and does the cost change that answer?
    They are closed: every regular language has a deterministic total recogniser, and flipping its accepting set recognises the complement. Cost is a separate axis — starting from a nondeterministic machine you may need exponentially many states to write the complement down, but the resulting language is still regular.
  • How would you build a recogniser for the strings in the first language but not the second?
    Do not complement the second machine on its own and then pair. Build the product directly and mark a pair accepting when the first component accepts and the second does not. That gets the difference in one polynomial step, provided both machines are deterministic and total.

saying these in an interview costs you the question

  • Flips a nondeterministic machine's accepting states and calls the result a complement
  • Says a partial machine complements fine because missing edges already reject
  • Claims regular languages are not closed under complement at all
  • Thinks complementing always costs an exponential blow-up, even when deterministic
  • Keeps the trap state rejecting after the flip, so rejected strings stay rejected
  • Believes determinising and complementing can be done in either order