Boolean Logic, Optimized

K-maps, how hard it really is, and the satisfiability problem

CS 2010 — Computing Fundamentals

2026-10-21

Four Questions

Last week we learned to turn any truth table into a circuit: one AND per row that should be 1, one OR to collect them. It always works.

This lecture is about what happens next.

  1. What does “smaller” even mean? Gates, delay and power are three different things, and you cannot optimise all three at once.
  2. How do you find a smaller circuit by hand? Algebra gets you part of the way. A K-map makes the rest mechanical.
  3. How hard is it, in general? Harder than it looks, and stranger than you would guess.
  4. What if we turn the question around? Given a formula, find an input that makes it true. That is SAT, and it is the most useful hard problem we know.

Part 0 — Why Bother

The Truth Table Always Works

The majority function: the output is 1 when at least two of the three inputs are 1.

A B C M
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1

Four rows have a 1, so the canonical sum of products has four terms:

¬A∧B∧C ∨ A∧¬B∧C ∨ A∧B∧¬C ∨ A∧B∧C

Twelve literals. Four 3-input AND gates, three NOT gates, one 4-input OR gate.

Correct — and twice the size it needs to be.

What a Circuit Costs

You pay in Measured as You feel it as
Gates gate count, or transistor count silicon area, money
Depth gates on the longest input-to-output path how long the answer takes
Power switching activity × capacitance heat, and battery

Depth is the one that surprises people. A ripple-carry adder has the right number of gates and gives the right answer, but its carry has to walk through every bit before the last sum bit is known — the depth grows with the width of the adder.

Smaller, faster, cooler: pick two. Optimisation is always against a cost function, and the cost function is a choice you make.

Eight Gates or Four

Same eight rows. Same four 1s. The circuit on the left is the truth table written as gates; the circuit on the right is the function.

Part I — Algebra

The Rules That Do the Work

Rule AND form OR form
identity X ∧ 1 = X X ∨ 0 = X
null X ∧ 0 = 0 X ∨ 1 = 1
idempotent X ∧ X = X X ∨ X = X
complement X ∧ ¬X = 0 X ∨ ¬X = 1
absorption X ∧ (X ∨ Y) = X X ∨ (X ∧ Y) = X
De Morgan ¬(X ∧ Y) = ¬X ∨ ¬Y ¬(X ∨ Y) = ¬X ∧ ¬Y
distributive X ∧ (Y ∨ Z) = (X∧Y) ∨ (X∧Z) X ∨ (Y∧Z) = (X∨Y) ∧ (X∨Z)

Two of these do almost all of the real work: absorption, which lets you throw away a term that is already covered, and De Morgan, which pushes a NOT inwards until it sits on a single variable.

Canonical to Minimal, Step by Step

Start: ¬A∧B∧C ∨ A∧¬B∧C ∨ A∧B∧¬C ∨ A∧B∧C

Step Pair up Factor out Rule used
1 ¬A∧B∧C ∨ A∧B∧C B∧C complement: X ∨ ¬X = 1
2 A∧¬B∧C ∨ A∧B∧C A∧C complement
3 A∧B∧¬C ∨ A∧B∧C A∧B complement

Steps 1, 2 and 3 all use the term A∧B∧C. That is allowed, because X = X ∨ X: one term can be used as many times as you like.

M = B∧C ∨ A∧C ∨ A∧B

Three terms, six literals, four gates. The same function.

Why Algebra Is Not an Algorithm

Three problems, and they are really the same problem:

  • You have to see the next move. No rule says “apply absorption here”. The order matters, and nothing tells you the order.
  • Every step is an equivalence. You are never nearer the answer, because every intermediate form is equally correct. There is no gradient to follow.
  • “Minimal” is undefined. Minimal in what — literals, gates, depth? Three different answers, three different circuits.

Algebra is a good way to check a simplification. It is a poor way to find one.

Part II — K-Maps

The Whole Trick

Two terms that differ in exactly one variable collapse into one term with one fewer literal:

A∧B ∨ A∧¬B = A∧(B ∨ ¬B) = A

So if you could see which of your 1s are one variable apart, minimising would be a matter of drawing loops around them.

Lay the Table Out So Neighbours Touch

Count the labels in Gray code — 00, 01, 11, 10 — so that each step flips exactly one bit. Then a shared edge on the map means “differs in one variable”, which is exactly the condition for merging.

The last label wraps round to the first: 10 and 00 differ in one bit too.

The Three-Variable Map

One variable drives the rows and two drive the columns. Each cell is one row of the truth table; the small number in the corner is that row’s minterm number — its binary value read as a number.

Fill It In

The majority function: four 1s, at minterms 3, 5, 6 and 7.

Nothing has happened yet. This is the same truth table, folded.

Grouping Rules

  • A group holds 1, 2, 4, 8, … cells — always a power of two.
  • Make each group as large as possible, then use as few groups as possible.
  • Groups may overlap. Every 1 must be in at least one group.
  • A group of 2 drops one variable; a group of 4 drops two; a group of 8 drops three.

The Edges Wrap

Σm(0,2) — two 1s at opposite ends of the top row. They are neighbours, because the left and right edges are the same edge: 00 and 10 differ in one bit.

¬A∧¬B∧¬C ∨ ¬A∧B∧¬C = ¬A∧¬C

The map is really a cylinder. A four-variable map is a torus.

You Try It

Four variables, eight 1s, already filled in.

The Answer

Two groups of four: the four corners, and the middle square.

M = ¬B∧¬D ∨ B∧D

Don’t-Cares

The input is a BCD digit: 1010–1111 never arrive. An × is an input we are promised will never happen, so count it as a 1 whenever it makes a group bigger.

What K-Maps Can’t Do

Parity of four bits is a checkerboard: no two 1s are adjacent, so every group is a single cell. Eight terms, thirty-two literals — and that really is the smallest sum of products.

But A ⊕ B ⊕ C ⊕ D is three gates. The map cannot see it, because the map only ever produces two levels of logic.

Part III — How Hard Is It?

You Can’t Even Write the Input Down

A function of n inputs has a truth table with 2ⁿ rows.

Inputs Rows in the truth table Functions of that many inputs
3 8 256
4 16 65,536
8 256 about 10⁷⁷
32 4.3 × 10⁹ about 101.3 × 10⁹
64 1.8 × 10¹⁹ —

A 32-bit adder has 65 inputs. Its truth table has 2⁶⁵ ≈ 3.7 × 10¹⁹ rows — at one row per nanosecond, about a thousand years to write down.

So for anything real, “minimise the truth table” is not slow. It is impossible to even state.

The Honest Answer

Finding the smallest sum of products is NP-hard. Every exact method we know — Quine–McCluskey, Petrick’s method, branch and bound — can be forced to take time that grows exponentially with the number of inputs.

“NP-hard” in one line: no known method is fast on every input, and finding one would settle the biggest open question in computer science.

And the general question is stranger than hard — it is not even known to be NP-complete. “Is there a circuit of at most k gates for this function?” is in NP (guess a circuit, check it on every row), but nobody knows whether it is as hard as SAT. The hardness of circuit minimisation is itself an open problem.

So What Do We Actually Do?

We stop asking for the best circuit and start asking for a good enough one, fast.

  • Espresso (1980s) — heuristic two-level minimisation. The K-map’s industrial replacement.
  • ABC, Yosys — multi-level synthesis: they share sub-expressions across outputs, which no K-map can do.
  • Typical result: within a few percent of optimal, on functions with hundreds of inputs, in milliseconds.

Optimal is rarely the goal. Meeting an area and timing target is the goal.

Part IV — Satisfiability

A Different Question

Every question so far started with a function and asked for a circuit.

SAT starts with a formula and asks for an input:

Given a Boolean formula, is there any assignment of 0s and 1s to its variables that makes it true?

(A ∨ B) ∧ (¬A ∨ C) ∧ (¬B ∨ ¬C)

Try A = 1, B = 0, C = 1: the three clauses become 1, 1, 1. Satisfiable.

If no assignment works, the formula is unsatisfiable — and proving that is the hard part.

CNF: The Input Format

  • A literal is a variable or its negation: A, ¬A.
  • A clause is literals joined by OR: (A ∨ ¬B ∨ C).
  • CNF is clauses joined by AND: (A ∨ ¬B) ∧ (¬A ∨ C).
  • 3-SAT: every clause has exactly three literals.

You have already written this form. It is the one your truth table produces when you read off the 0 rows instead of the 1 rows.

Why CNF? Because any formula converts into it mechanically, and because each clause is then one small, local constraint. Every SAT solver takes CNF.

The First NP-Complete Problem

Cook and Levin, 1971: every problem in NP can be translated into SAT in polynomial time, so a fast SAT solver would be a fast solver for all of them.

The Surprise: The Solvers Work

Modern solvers — DPLL with unit propagation, and CDCL, which learns a new clause from every conflict — routinely solve instances with millions of variables.

They are inside chip verification and place-and-route, software model checking, package managers, scheduling, planning, cryptanalysis, and every puzzle solver you have ever used.

A problem can be NP-complete and still be easy in practice. Hardness is a claim about the worst case, and the instances people care about are rarely the worst case.

The Gap

Brute force at one million guesses per second, and the search space doubles with every variable: twenty variables is a second, forty is a fortnight.

Nobody knows whether a fundamentally better method exists. That question is P = NP. It is open, and it is worth a million dollars.

Part V — Wrap-Up

Four Things to Remember

  1. The truth table is always correct and always too big.
  2. A K-map is a truth table folded so that mergeable terms touch — Gray code, powers of two, wrap-around.
  3. Exact minimisation is NP-hard, and minimal circuits are not even known to be NP-complete. So we use heuristics and accept “good enough”.
  4. SAT asks the question backwards. It is the canonical hard problem and also the workhorse — because worst case and typical case are different questions.