DM 12 Boolean algebra and automata

Previous: DM 11 Trees and Graph Algorithms | DM 00 Course Map

One sentence

Boolean algebra is propositional logic written as arithmetic on 0 and 1, which is how circuits compute; a finite automaton is the simplest machine with memory, and it recognizes exactly the patterns that regular expressions describe.

Part 1: Boolean algebra

Logic as arithmetic

Write 1 for true and 0 for false. The connectives of DM 01 Logic and Propositions become operations:

LogicBooleanGatePython bit operator
(or )ANDp & q
ORp \| q
NOT~p (or 1 - p)
XORp ^ q

Caution: in Boolean algebra (true or true is true). With XOR, , which is addition modulo 2 (DM 08 Number Theory and Modular Arithmetic).

Laws

The logical equivalences become algebraic identities:

LawIdentity
identity,
domination,
idempotent,
complement,
De Morgan,
absorption,
distributive,

The second distributive law is false for ordinary numbers. Boolean algebra is its own system.

Worked simplification.

A two-gate expression collapses to a wire. Fewer gates mean cheaper, faster, cooler chips.

From truth table to formula

Any truth table can be written as a formula: for each row where the output is 1, write the AND of the inputs (negated where the input is 0), then OR those terms together. This is the sum of products (disjunctive normal form).

Example. Majority of three inputs: output 1 when at least two inputs are 1. Rows with output 1: .

Simplify: can be absorbed into each of the others. Using and similar,

Consequence: AND, OR and NOT together can compute any Boolean function. Even NAND alone () suffices, since and the rest follows by De Morgan. Real chips are built mostly from NAND gates.

A circuit that adds

Adding two bits gives a sum bit and a carry bit:

carrysum
0000
0101
1001
1110

So and : a half adder. Chain full adders (which also take an incoming carry) and you add 64-bit numbers. Every calculation a computer does reduces to gates like these.

Part 2: Finite automata

The machine

A deterministic finite automaton (DFA) reads a string one symbol at a time. It has:

  • a finite set of states,
  • a start state,
  • a transition function: from each state, each input symbol leads to exactly one next state,
  • a set of accepting states.

After the last symbol, the string is accepted if the machine is in an accepting state. The DFA’s only memory is which state it is in.

Example: binary strings with an even number of 1s. Two states: (even so far, start, accepting) and (odd so far).

Stateon 0on 1

Run on 1011: . Ends in : rejected (three 1s).

The DFA is a directed graph (DM 10 Graphs) with labelled edges, and running it is following a path.

Example: binary numbers divisible by 3. Track the value mod 3. Reading bit turns value into , so the state moves to :

State on 0on 1
0 (start, accept)01
120
212

110 (six): . Accepted. Three states can test divisibility of numbers of any length.

Regular expressions

A regular expression describes a set of strings:

PatternMatches
aba followed by b
a\|ba or b
a*zero or more as
(ab)*"", ab, abab, …
0*10*strings with exactly one 1

Kleene’s theorem. A language can be described by a regular expression exactly when some DFA recognizes it. These are the regular languages. Regex engines work by compiling the expression into an automaton.

What a DFA cannot do

A DFA has finitely many states, so it can only remember a bounded amount. It cannot recognize

Proof by pigeonhole (DM 07 Counting). Suppose a DFA with states recognizes it. Feed it : that is prefixes, so two of them, and with , leave the machine in the same state. Then appending must give the same verdict for both. But should be accepted and rejected. Contradiction.

Matching brackets is the same problem: you must count how many are open. Recognizing such languages needs extra memory, such as a stack (pushdown automata and context-free grammars, the basis of parsers). With unbounded memory you reach the Turing machine, the model of all computation, and its limits: some problems, such as deciding whether an arbitrary program halts, no algorithm can solve.

State machines in practice

Robots, protocols and user interfaces are routinely written as finite state machines:

StateEventNext state
idletask receivedapproach
approachobject in reachgrasp
graspgrip confirmedlift
graspgrip failedapproach
anyemergency stophalt

Explicit states make behaviour testable: you can list every state and check that every event is handled in each. Behaviour trees, common in robotics, extend the same idea with hierarchy.

Common mistakes

  • Treating Boolean as numeric addition. .
  • Leaving a transition undefined. A DFA needs exactly one transition per state per symbol; add an explicit “dead” state for invalid input.
  • Trying to match nested structure with a regular expression. Regex cannot balance brackets in general; use a parser.

Learn by doing

  1. Simplify .
  2. Write the sum of products for that is 1 only when , and name the function.
  3. Design a DFA over accepting strings that end in 1.
  4. Trace the divisible-by-3 DFA on 1001 (nine). Accepted?

Where this shows up

Task-level robot control is often a state machine or behaviour tree wrapped around learned skills (15 Robot Systems and Deployment). Neural network hardware is built from these gates, and tokenizers for language models are built on regular-expression rules (13 Vision Language Action Models).

You have finished the course. Return to the checkpoint questions in DM 00 Course Map.