Skip to main content

Exercises — Module 21 (Boolean Functions and Optimal Evaluation)

Self-contained problems on this module's material — counting special functions, normal forms, monotonicity, Boolean chains, and the combinational complexity C(f)C(f). You can work every one without the books: each states the problem in full, gives a hint to peek at when stuck, and a worked answer sketch to check against after you try. Numeric answers here are reproduced by the code you write in the lab (or a few lines at a REPL).

Ratings follow Knuth's scale (00–50; M = needs mathematics, ▶ = especially instructive). Where a problem mirrors a TAOCP exercise, its number in §7.1.1–7.1.2 is noted for readers who own Volume 4A.

Tracking​

#TopicRatingStatus
1Count the self-dual functions of nn variables10⬜
2▶ Symmetric functions ↔\leftrightarrow n+1n+1 weight bits20⬜
3Monotone   ⟺  \iff positive-literal DNF22⬜
4▶ Optimum 4-gate median-of-3; no 3-gate chain15⬜
5CC of the full adder's two outputs; can they share?20⬜
6▶ A random ff needs ≈2n/n\approx 2^n/n gates (Shannon/Lupanov)30⬜
7Compute the Dedekind number M(5)M(5); the M(9)M(9) story40⬜

Problems​

1. Count the self-dual functions of nn variables (rating 10 · cf. 7.1.1–2)​

Problem. A function is self-dual if f(¬x)=¬f(x)f(\lnot x) = \lnot f(x) for every input xx (complementing all inputs complements the output). How many self-dual Boolean functions of nn variables are there?

Hint. The 2n2^n inputs pair up under complementation: xx with ¬x\lnot x. How many such pairs are there, and how much freedom does self-duality leave you on each pair?

Answer sketch. The map x↦¬xx \mapsto \lnot x has no fixed points (an nn-bit string never equals its own complement for n≥1n \ge 1), so the 2n2^n inputs split into exactly 2n/2=2n−12^n / 2 = 2^{n-1} complementary pairs. Self-duality forces f(¬x)=¬f(x)f(\lnot x) = \lnot f(x), so fixing ff on one member of a pair fixes it on the other. You may choose ff freely (0 or 1) on one representative of each of the 2n−12^{n-1} pairs, and nothing else. Hence there are

22n−1\boxed{2^{2^{n-1}}}

self-dual functions. Check: n=1⇒21=2n = 1 \Rightarrow 2^{1} = 2 (the two projections x1,¬x1x_1, \lnot x_1); n=2⇒22=4n = 2 \Rightarrow 2^{2} = 4; n=3⇒24=16n = 3 \Rightarrow 2^{4} = 16 — all confirmed by brute-force enumeration. Compare the grand total 22n2^{2^n} of all functions: self-dual ones are the square root of that count, a vanishing fraction, yet they include the important median/majority function the lesson singles out.

2. ▶ Symmetric functions ↔\leftrightarrow n+1n+1 weight bits (rating M20 · cf. 7.1.1–5)​

Problem. A function is symmetric if its value depends only on how many inputs are 1, not which. Prove that every symmetric function of nn variables is determined by the n+1n+1 bits w0,…,wnw_0, \dots, w_n (where wjw_j is the value when exactly jj inputs are 1), and count how many symmetric functions there are.

Hint. Group the 2n2^n inputs by popcount. What does symmetry say about all inputs in one group? Then count the free choices.

Answer sketch. Partition the 2n2^n inputs by their number of 1s: group jj holds the (nj)\binom{n}{j} inputs with popcount jj, for j=0,1,…,nj = 0, 1, \dots, n. Symmetry means ff is constant on each group (any two inputs with the same popcount give the same value), so ff is completely described by its value wjw_j on group jj — exactly the n+1n+1 bits w0,…,wnw_0, \dots, w_n, giving f(x)=wpopcount⁡(x)f(x) = w_{\operatorname{popcount}(x)} (the module's symmetric_function). Conversely any choice of the wjw_j yields a symmetric function. So the symmetric functions are in bijection with (n+1)(n+1)-bit strings, and there are exactly

2 n+12^{\,n+1}

of them. Check: n=2⇒23=8n = 2 \Rightarrow 2^3 = 8, n=3⇒24=16n = 3 \Rightarrow 2^4 = 16 — confirmed by enumeration. This is a spectacular compression: n+1n+1 bits instead of 2n2^n, and it is why majority, threshold, and parity — all symmetric — are among the cheap functions the counting bound (Problem 6) says must be rare.

3. Monotone   ⟺  \iff positive-literal DNF (rating 22 · cf. 7.1.1–16)​

Problem. Call ff monotone (nondecreasing) if x⊆y⇒f(x)≤f(y)x \subseteq y \Rightarrow f(x) \le f(y) under the bitwise-subset order (flipping any input 0→10 \to 1 never flips the output 1→01 \to 0). Prove that ff is monotone iff it can be written as a DNF (an OR of product terms) using only positive literals — no complemented variables.

Hint. One direction: show a positive term is monotone and that OR preserves monotonicity. Other direction: for a monotone ff, build a DNF from its minimal 1-inputs, using for each the product of the variables that are 1 there.

Answer sketch. (⇐\Leftarrow) A positive product term xi1∧⋯∧xikx_{i_1} \land \cdots \land x_{i_k} is monotone: raising an input from 0 to 1 can only turn the AND from 0 to 1, never the reverse. The OR of monotone functions is monotone (raising an input cannot turn any disjunct off, so it cannot turn the OR off). Hence any positive-literal DNF is monotone. (⇒\Rightarrow) Let ff be monotone. Call an input xx minimal if f(x)=1f(x) = 1 but f(x′)=0f(x') = 0 for every x′⊊xx' \subsetneq x. For each minimal xx form the positive term Tx=⋀j: xj=1xjT_x = \bigwedge_{j:\,x_j = 1} x_j, and let D=⋁x minimalTxD = \bigvee_{x \text{ minimal}} T_x. Then D=fD = f: if f(y)=1f(y) = 1, walk down from yy by clearing 1-bits while the value stays 1 until you reach a minimal x⊆yx \subseteq y; then TxT_x is satisfied by yy (all its variables are 1 in yy), so D(y)=1D(y) = 1. Conversely if TxT_x is satisfied by yy then x⊆yx \subseteq y, so by monotonicity f(y)≥f(x)=1f(y) \ge f(x) = 1. Thus DD and ff agree on every input, and DD uses only positive literals. ■\blacksquare (The minimal 1-inputs are exactly the prime implicants of a monotone function; this is a proof by exhaustive agreement — the two formulas match on all 2n2^n rows.)

4. ▶ Optimum 4-gate median-of-3; no 3-gate chain (rating 15 · cf. 7.1.2–1)​

Problem. Exhibit a 4-gate Boolean chain computing the median (majority) of three inputs, and argue that no chain of 3 gates can compute it — so C(majority3)=4C(\text{majority}_3) = 4 over the full 16-operation basis.

Hint. For the witness, combine an AND, an OR, and one more AND/OR. For the lower bound, recall why the "reachable functions" shortcut that reports 3 is wrong — it assumes both operands of a gate are free, ignoring that sharing is what a real chain provides.

Answer sketch. Upper bound (witness). The chain

v3=x1∧x2,v4=x1∨x2,v5=x3∧v4,v6=v3∨v5  (output),\begin{aligned} v_3 &= x_1 \land x_2, & v_4 &= x_1 \lor x_2,\\ v_5 &= x_3 \land v_4, & v_6 &= v_3 \lor v_5 \;(\text{output}), \end{aligned}

computes majority(x1,x2,x3)=(x1∧x2)∨(x3∧(x1∨x2))\text{majority}(x_1,x_2,x_3) = (x_1 \land x_2) \lor (x_3 \land (x_1 \lor x_2)) in 4 gates. Check the cases: if x1=x2=1x_1 = x_2 = 1, v3=1v_3 = 1; if exactly one of x1,x2x_1, x_2 is 1 then v4=1v_4 = 1 and v5=x3v_5 = x_3, so the output is 1 iff x3x_3 supplies the second vote; if x1=x2=0x_1 = x_2 = 0 everything is 0. So the output is 1 iff at least two inputs are 1 — the majority. Lower bound. The state-BFS of §6 — which carries the whole set of computed values forward so any later gate can reuse an earlier one — finds no 3-gate chain for majority; its first appearance is at depth 4. The tempting "reachable set" shortcut (Rc=Rc−1∪{g(a,b)}R_c = R_{c-1} \cup \{g(a,b)\}) reports 3, but it double-counts by pretending both operands are available for free when each may itself cost gates; it computes only a lower bound on cost, not the cost. Since the shortcut's 3 is not achievable and the witness achieves 4, C(majority3)=4C(\text{majority}_3) = 4.

5. CC of the full adder's two outputs; can they share? (rating 20 · cf. 7.1.2–2)​

Problem. A full adder has inputs x1,x2,x3x_1, x_2, x_3 and two outputs: the sum s=x1⊕x2⊕x3s = x_1 \oplus x_2 \oplus x_3 and the carry c=majority(x1,x2,x3)c = \text{majority}(x_1, x_2, x_3). Individually, C(s)=2C(s) = 2 and C(c)=4C(c) = 4 over the full basis. If we build one chain producing both outputs, can it cost fewer than 2+4=62 + 4 = 6 gates by sharing an intermediate value? Find the smallest such chain.

Hint. Is there a subexpression common to a cheap chain for ss and a cheap chain for cc? Rewrite the carry using x1⊕x2x_1 \oplus x_2 instead of x1∨x2x_1 \lor x_2 where they agree.

Answer sketch. Yes — the two outputs share the half-sum x1⊕x2x_1 \oplus x_2, so both fit in 5 gates, not 6. When x1∧x2=0x_1 \land x_2 = 0 (the region that matters for the second disjunct) x1∨x2x_1 \lor x_2 equals x1⊕x2x_1 \oplus x_2, giving the identity c=(x1∧x2)∨(x3∧(x1⊕x2))c = (x_1 \land x_2) \lor (x_3 \land (x_1 \oplus x_2)). Now:

g1=x1⊕x2,g2=g1⊕x3  (=sum s),g3=x1∧x2,g4=x3∧g1,g5=g3∨g4  (=carry c).\begin{aligned} g_1 &= x_1 \oplus x_2, & g_2 &= g_1 \oplus x_3 \;(=\text{sum } s),\\ g_3 &= x_1 \land x_2, & g_4 &= x_3 \land g_1, & g_5 &= g_3 \lor g_4 \;(=\text{carry } c). \end{aligned}

Gate g1g_1 feeds both the sum (via g2g_2) and the carry (via g4g_4) — that is the sharing. Evaluating all 23=82^3 = 8 inputs confirms g2=x1⊕x2⊕x3g_2 = x_1 \oplus x_2 \oplus x_3 and g5=majority(x1,x2,x3)g_5 = \text{majority}(x_1, x_2, x_3) on every row. So the joint (multi-output) complexity is 5, strictly below the sum 66 of the separate complexities — a concrete instance of why circuit cost is about sharing, the theme behind the module's state-BFS.

6. ▶ A random ff needs ≈2n/n\approx 2^n/n gates (Shannon/Lupanov) (rating M30 · cf. 7.1.2–23)​

Problem. Explain why almost every Boolean function of nn variables has combinational complexity C(f)=Θ(2n/n)C(f) = \Theta(2^n / n): most functions require exponentially many gates. Give the counting argument for the lower bound and state the matching Lupanov upper bound.

Hint. Count how many distinct functions a chain of rr gates can possibly compute, and compare with the total number of functions 22n2^{2^n}. If there are too few small chains, most functions cannot have one.

Answer sketch. Lower bound (counting chains). A chain of rr gates over the 16-operation basis is specified by choosing, per gate, an operation (16 ways) and two earlier operands (at most (n+r)2(n + r)^2 ways). So at most

(16 (n+r)2)r=2O(rlog⁡r)\big(16\,(n+r)^2\big)^r = 2^{O(r \log r)}

distinct functions have an rr-gate chain. To realize even a constant fraction of all 22n2^{2^n} functions we need 2O(rlog⁡r)≥const⋅22n2^{O(r \log r)} \ge \text{const} \cdot 2^{2^n}, i.e. O(rlog⁡r)≥2n−O(1)O(r \log r) \ge 2^n - O(1), which forces r=Ω(2n/n)r = \Omega(2^n / n). Because the count of realizable functions is so much smaller than the count of functions whenever r≪2n/nr \ll 2^n/n, the fraction of functions with a small chain tends to 0: almost every function needs Ω(2n/n)\Omega(2^n/n) gates. Upper bound (Lupanov). Lupanov proved O(2n/n)O(2^n/n) gates always suffice — every function has a chain of that order — so the bound is tight and the typical complexity is C(f)=Θ(2n/n)C(f) = \Theta(2^n / n). Moral. This is the double exponential of §1 biting back: there are 22n2^{2^n} functions but only 2O(rlog⁡r)2^{O(r \log r)} small circuits, so small circuits are precious. The functions we actually build hardware for (adders, comparators, symmetric functions) are the rare cheap exceptions.

7. Compute the Dedekind number M(5)M(5); the M(9)M(9) story (rating 40 · cf. §7.1.1)​

Problem. The Dedekind number M(n)M(n) counts the monotone Boolean functions of nn variables. Compute M(5)M(5) by brute force — enumerate the relevant space and count the monotone functions — and describe why M(8)M(8) and M(9)M(9) needed heroic computation rather than a simple loop.

Hint. For a direct count you would sweep all 22n2^{2^n} functions and test monotonicity (single-bit raises), as the lab does for n≤4n \le 4. At n=5n = 5 the space 225=2322^{2^5} = 2^{32} is already large; think about what makes n=8,9n = 8, 9 astronomically worse.

Answer sketch. By definition (README §4) M(0..4)=2,3,6,20,168M(0..4) = 2, 3, 6, 20, 168, and the next value is

M(5)=7581.M(5) = 7581.

A brute-force enumeration of all 225=232≈4.3×1092^{2^5} = 2^{32} \approx 4.3 \times 10^9 functions, testing each for monotonicity via single-bit raises, reproduces 75817581 — feasible but no longer instant, which is why the lab caps its live computation at n≤4n \le 4 (6553665536 functions). The wall past n=5n = 5 is the double exponential 22n2^{2^n}: the search space is 2642^{64} at n=6n = 6, 22562^{256} at n=8n = 8, 25122^{512} at n=9n = 9 — far beyond any exhaustive sweep, and there is no known closed form for M(n)M(n). So M(8)M(8) was found only in 1991 on a supercomputer, and M(9)M(9) was computed only in 2023 — independently by Christian Jäkel and by Van Hirtum et al. — using months of FPGA and GPU time on cleverly structured counts (not naïve enumeration), giving M(9)=286386577668298411128469151667598498812366M(9) = 286386577668298411128469151667598498812366. Dedekind posed the problem in 1897; each new value has been a milestone, and the count remains open past n=9n = 9.


Your solutions​

Use this space to log your own work — a restated problem, your approach, and how it compared with the sketch above.

Exercise N (rating RR)​

Approach. Answer / proof. Compared with the sketch: what you did differently, what you missed.