notesonly.in

One notebook for every subject — open it anywhere.

Log in

Finite Automata Explained: DFA, NFA and State Diagrams

A finite automaton is an abstract machine with a fixed, finite number of states. It reads an input string one symbol at a time, moves between states according to fixed rules, and at the end accepts or rejects the string depending on which state it lands in.

Automata matter because they are the simplest model of computation and the theoretical backbone of text processing. Every time a compiler scans your code for keywords or a search box validates an email pattern, a finite automaton is conceptually at work.

Key Concepts

  • State: one of finitely many configurations the machine can be in; drawn as a circle in a state diagram.
  • Alphabet: the set of input symbols, e.g. {0, 1}.
  • Transition function: the rule mapping (current state, symbol) to the next state.
  • Start state: where the machine begins, marked with an arrow.
  • Accept states: drawn with double circles; ending in one means the input is accepted.
  • DFA vs NFA: a DFA has exactly one transition per state-symbol pair; an NFA may have zero, one or many, plus empty moves — yet both recognise the same class of languages.

How It Works

  1. Begin in the start state before reading any input.
  2. Read the next input symbol and follow the transition for (current state, symbol).
  3. Repeat until the input is exhausted.
  4. Accept the string if the final state is an accept state; otherwise reject it.
  5. The set of all accepted strings is the language recognised by the automaton.

Worked Example

Build a DFA over {0, 1} accepting strings that end in 1. Use two states: q0 (last symbol was 0, or nothing read yet — start, non-accepting) and q1 (last symbol was 1 — accepting). Transitions: from q0 on 0 stay at q0, on 1 go to q1; from q1 on 0 go to q0, on 1 stay at q1. Run "101": start q0, read 1 to reach q1, read 0 to reach q0, read 1 to reach q1. The input ends in accept state q1, so "101" is accepted. Run "100": q0 to q1 to q0 to q0 ends in q0, rejected. Exactly the strings ending in 1 are accepted.

Common Misconceptions

  • "An NFA is more powerful than a DFA." Surprising but false — every NFA can be converted to an equivalent DFA; NFAs are just easier to design.
  • "The machine's memory grows with the input." No — the number of states is fixed forever; the machine remembers only which state it is in.
  • "Accepting means stopping early." The machine always reads the entire input; acceptance is decided only at the end.

Real-World Applications

  • Lexical analysis: compilers use automata to recognise keywords, numbers and identifiers in source code.
  • Pattern matching: regular-expression engines compile patterns into automata for fast searching.
  • Protocol and input validation: vending machines, turnstiles and form validators are finite-state systems.

A finite automaton reads symbols, hops between a fixed set of states via its transition function, and accepts exactly the strings ending in an accept state. DFAs and NFAs recognise the same languages, and this tiny model powers real-world text scanning and validation.