Answers to Selected Exercises
Worked solutions to the daggered (†) and odd-numbered exercises from each chapter. Try every problem before reading its solution.
Chapter 1 — Propositional Logic
1.1 † (a) Proposition, true ($17$ is prime). (b) Not a proposition — a command has no truth value. (c) Proposition, true: with the specific value $n=5$ there is no free variable, and $6 > 5$. (d) Not a proposition — a question has no truth value. (e) Proposition, false — "This sentence has five words" contains six words. (It is declarative and has a definite truth value, so it counts as a proposition; it just happens to be false.)
1.3 † Table:
| $p$ | $q$ | $p \oplus q$ | $p \leftrightarrow q$ | $\neg(p \leftrightarrow q)$ |
|---|---|---|---|---|
| $T$ | $T$ | $F$ | $T$ | $F$ |
| $T$ | $F$ | $T$ | $F$ | $T$ |
| $F$ | $T$ | $T$ | $F$ | $T$ |
| $F$ | $F$ | $F$ | $T$ | $F$ |
The columns $p \oplus q$ and $\neg(p \leftrightarrow q)$ are identical ($F,T,T,F$). This suggests the equivalence $p \oplus q \equiv \neg(p \leftrightarrow q)$ — "differ" is exactly the negation of "agree," as the chapter notes in §1.2.
1.5 † Table for $(p \lor q) \land \neg r$:
| $p$ | $q$ | $r$ | $p \lor q$ | $\neg r$ | $(p \lor q)\land \neg r$ |
|---|---|---|---|---|---|
| $T$ | $T$ | $T$ | $T$ | $F$ | $F$ |
| $T$ | $T$ | $F$ | $T$ | $T$ | $T$ |
| $T$ | $F$ | $T$ | $T$ | $F$ | $F$ |
| $T$ | $F$ | $F$ | $T$ | $T$ | $T$ |
| $F$ | $T$ | $T$ | $T$ | $F$ | $F$ |
| $F$ | $T$ | $F$ | $T$ | $T$ | $T$ |
| $F$ | $F$ | $T$ | $F$ | $F$ | $F$ |
| $F$ | $F$ | $F$ | $F$ | $T$ | $F$ |
True in 3 rows (rows 2, 4, 6). In English: "at least one of $p$, $q$ holds, and $r$ does not."
1.6 (a) $(p \rightarrow q)\lor(q \rightarrow p)$ is a tautology (an implication fails only with true hypothesis/false conclusion; both can't fail at once, so at least one disjunct is always true). (b) $(p \land \neg p)\rightarrow q$ is a tautology (the hypothesis $p \land \neg p$ is always false, so the implication is vacuously true — this is the "from a contradiction, anything follows" pattern). (c) $p \leftrightarrow \neg p$ is a contradiction ($p$ can never have the same truth value as its own negation). (Even-numbered: included for completeness.)
1.7 † Filled table:
| $p$ | $q$ | $p \lor q$ | $\neg(p \lor q)$ | $\neg p$ | $\neg q$ | $\neg p \land \neg q$ |
|---|---|---|---|---|---|---|
| $T$ | $T$ | $T$ | $F$ | $F$ | $F$ | $F$ |
| $T$ | $F$ | $T$ | $F$ | $F$ | $T$ | $F$ |
| $F$ | $T$ | $T$ | $F$ | $T$ | $F$ | $F$ |
| $F$ | $F$ | $F$ | $T$ | $T$ | $T$ | $T$ |
Finishing sentence: the columns $\neg(p \lor q)$ and $\neg p \land \neg q$ are identical ($F,F,F,T$), so by the definition of logical equivalence $\neg(p \lor q) \equiv \neg p \land \neg q$. $\blacksquare$
1.8 The missing line is $\neg(\neg q)\lor\neg p \equiv q \lor \neg p$ (Double negation). Full chain: $\neg q \rightarrow \neg p \equiv \neg(\neg q)\lor \neg p \equiv q \lor \neg p \equiv \neg p \lor q \equiv p \rightarrow q$. $\blacksquare$ (Even-numbered: included as scaffold completion.)
1.9 † Show $(p \land q)\rightarrow p$ is a tautology by the algebra of logic: $$ \begin{aligned} (p \land q)\rightarrow p &\equiv \neg(p \land q)\lor p && \text{(Implication law)}\\ &\equiv (\neg p \lor \neg q)\lor p && \text{(De Morgan's law)}\\ &\equiv (\neg p \lor p)\lor \neg q && \text{(Commutative \& Associative)}\\ &\equiv T \lor \neg q && \text{(Negation law: } \neg p \lor p \equiv T\text{)}\\ &\equiv T && \text{(Domination law)}. \end{aligned} $$ The expression is equivalent to $T$, hence a tautology. $\blacksquare$
1.11 † Show $p \leftrightarrow q \equiv (p \land q)\lor(\neg p \land \neg q)$ by truth table:
| $p$ | $q$ | $p \leftrightarrow q$ | $p \land q$ | $\neg p \land \neg q$ | $(p\land q)\lor(\neg p\land\neg q)$ |
|---|---|---|---|---|---|
| $T$ | $T$ | $T$ | $T$ | $F$ | $T$ |
| $T$ | $F$ | $F$ | $F$ | $F$ | $F$ |
| $F$ | $T$ | $F$ | $F$ | $F$ | $F$ |
| $F$ | $F$ | $T$ | $F$ | $T$ | $T$ |
Both output columns are $T,F,F,T$, so the two propositions are logically equivalent. $\blacksquare$ (In words: the biconditional is true exactly when $p$ and $q$ are both true or both false.)
1.13 † See code/exercise-solutions.py. Define xor(p, q): return p != q and call
truth_table(xor, ["p", "q"]). Expected output (four rows):
p q xor
T T F
T F T
F T T
F F F
This matches the $p \oplus q$ column of the chapter's connective table (true exactly where $p, q$ differ).
1.15 † See code/exercise-solutions.py for classify(fn, n) (reusing is_tautology and
is_contradiction). classify(lambda p: p or (not p), 1) returns "tautology" (excluded middle, all
$T$); classify(lambda p, q: (not p) or q, 2) returns "contingency" (this is $p \rightarrow q$, true
in three rows and false in one). Expected output line: tautology contingency.
1.17 † The "proof" is wrong: negation does not distribute over $\rightarrow$, and the appeal to De Morgan is bogus because De Morgan governs $\land$/$\lor$, not $\rightarrow$. The correct value (from §1.4) is $\neg(p \rightarrow q)\equiv p \land \neg q$, not $\neg p \rightarrow \neg q$. Disproving row: take $p=T, q=T$. Then $\neg(p \rightarrow q)=\neg T = F$, but the claimed $\neg p \rightarrow \neg q = F \rightarrow F = T$. Since $F \ne T$, the claim fails.
1.19 † The error: in Python and binds tighter than or (§1.2), so a or b and c parses as
a or (b and c), not (a or b) and c. "Left to right" describes evaluation order within one
precedence level, not grouping across levels. What Python computes is $a \lor (b \land c)$. Disagreeing
assignment: $a=T, b=F, c=F$. Then $a \lor (b \land c)=T \lor F = T$, but $(a \lor b)\land c=(T)\land F =
F$. They differ.
1.21 † (a) $v \equiv o \lor (b \land l)$ ("owner, or public-and-logged-in"). (b) The code
if o or b and l: groups (by precedence) as o or (b and l) $= o \lor (b \land l)$ — which is what
the spec wants here, so the precedence happens to be correct (no parentheses needed, though adding them
aids the reader). (c) Truth table:
| $o$ | $b$ | $l$ | $b \land l$ | $v=o\lor(b\land l)$ |
|---|---|---|---|---|
| $T$ | $T$ | $T$ | $T$ | $T$ |
| $T$ | $T$ | $F$ | $F$ | $T$ |
| $T$ | $F$ | $T$ | $F$ | $T$ |
| $T$ | $F$ | $F$ | $F$ | $T$ |
| $F$ | $T$ | $T$ | $T$ | $T$ |
| $F$ | $T$ | $F$ | $F$ | $F$ |
| $F$ | $F$ | $T$ | $F$ | $F$ |
| $F$ | $F$ | $F$ | $F$ | $F$ |
An anonymous user (not owner, not logged in: $o=F, l=F$) corresponds to rows 6 and 8; in both, $v=F$ — so an anonymous user can never view, which is correct. (The only $F$-owner viewer is row 5: public and logged in.)
1.23 † (a) $g \equiv (u \lor m) \land \neg w$ (page on high CPU or memory, but not during maintenance). (b) Push the negation in / distribute: $g \equiv (u \lor m)\land \neg w \equiv (u \land \neg w)\lor(m \land \neg w)$ (distributive law); the negation is already fully pushed in (it sits on the atom $w$). (c) During maintenance, $w=T$, so $\neg w = F$, and $g = (u \lor m)\land F \equiv F$ by the domination law — we can never page during maintenance, regardless of CPU or memory. (Read straight off the formula, not from intuition.)
1.25 † (NAND functional completeness — Deep Dive.)
(a) See code/exercise-solutions.py: nand(p,q)=not(p and q); then
my_not(p)=nand(p,p), my_and(p,q)=my_not(nand(p,q)),
my_or(p,q)=nand(my_not(p),my_not(q)).
(b) equivalent(my_not, lambda p: not p, 1), equivalent(my_and, lambda p,q: p and q, 2), and
equivalent(my_or, lambda p,q: p or q, 2) each return True (expected output line: True True True),
confirming all three rebuilt connectives match the built-ins on every assignment.
(c) Proof $\neg p \equiv p \uparrow p$ (two rows): $p \uparrow p = \neg(p \land p) \equiv \neg p$ by
idempotence; tabulating, $p=T \Rightarrow \neg(T\land T)=F=\neg p$, and $p=F \Rightarrow \neg(F\land
F)=T=\neg p$. Columns match. **Proof $p \land q \equiv (p\uparrow q)\uparrow(p\uparrow q)$ (four rows):
let $u = p \uparrow q = \neg(p \land q)$; then $u \uparrow u \equiv \neg u \equiv \neg\neg(p\land q)
\equiv p \land q$ by double negation. Tabulating $p\land q$ vs. the right side gives $T,F,F,F$ on both.
$\blacksquare$ Conclusion: since $\{\neg,\land,\lor\}$ already builds every truth table and we have
now built all three of $\neg,\land,\lor$ from NAND alone, NAND is functionally complete. Hardware payoff
(§1.1): a fabricator needs to manufacture only one** kind of gate (NAND) and can wire up any Boolean
function from it — which is exactly how real chips are built.
1.27 † (a) Hand-trace first_satisfying on $\phi=(p\lor q)\land(\neg p\lor r)\land\neg q$ in row
order ($T$ first), variables $p,q,r$:
| # | $p$ | $q$ | $r$ | $p\lor q$ | $\neg p\lor r$ | $\neg q$ | $\phi$ |
|---|---|---|---|---|---|---|---|
| 1 | $T$ | $T$ | $T$ | $T$ | $T$ | $F$ | $F$ |
| 2 | $T$ | $T$ | $F$ | $T$ | $F$ | $F$ | $F$ |
| 3 | $T$ | $F$ | $T$ | $T$ | $T$ | $T$ | $T$ |
The search stops at row 3: the first satisfying assignment is $\{p:T,\ q:F,\ r:T\}$ (matching the chapter's §1.6 code output). (b) $p \rightarrow (p \lor q)$ is a tautology because its negation is unsatisfiable: $\neg(p \rightarrow (p\lor q)) \equiv p \land \neg(p \lor q) \equiv p \land(\neg p \land \neg q) \equiv (p \land \neg p)\land \neg q \equiv F \land \neg q \equiv F$ (negated-implication, De Morgan, associativity, negation, domination). A formula equivalent to $F$ has no satisfying assignment, so by "$A$ is a tautology iff $\neg A$ is unsatisfiable," $p \rightarrow (p \lor q)$ is a tautology. $\blacksquare$ (c) About $2^{50} \approx 1.13 \times 10^{15}$ rows — over a quadrillion. Even at a billion rows per second that is roughly two weeks; double the variables and it is hopeless. This is the exponential wall of §1.6.
1.29 The argument "If it compiles, it ships; it compiled; therefore it ships" is modus ponens. It is valid iff the implication $\big((c \rightarrow h)\land c\big)\rightarrow h$ is a tautology (this is the premises-imply-conclusion form). One can verify by truth table that it is a tautology — it is false only if the hypothesis $(c \rightarrow h)\land c$ is true and $h$ is false, but $(c\rightarrow h)\land c$ true forces $c=T$ and $c \rightarrow h$ true, hence $h=T$, a contradiction. So the argument is valid. (This is exactly the reduction "validity = a certain implication is a tautology" that Chapter 3 builds its whole theory on.)
1.31 Verifying a satisfying assignment is easy: you are handed a specific truth value for every variable, so you plug them in and evaluate the formula once — a single row of a truth table, $O(\text{size of formula})$ work, no search. *Finding* one with no hint may force you to search the space of $2^n$ assignments, which grows exponentially (§1.6). The precise equivalence: "is $A$ a tautology?" asks whether $A$ is true on every assignment; "is $\neg A$ satisfiable?" asks whether $\neg A$ is true on some assignment. Since $\neg A$ is true exactly where $A$ is false, $A$ is a tautology iff $\neg A$ has no satisfying assignment, i.e. $\neg A$ is unsatisfiable. The two questions are the same coin viewed from opposite sides.
Chapter 2 — Predicate Logic and Quantifiers
2.1 † (a) Predicate, free variable $n$. (b) Proposition (the hole is filled; $12$ is divisible by $3$, definitely true). (c) Predicate, free variables $x$ and $y$. (d) Proposition: $x$ is bound by the $\forall$, so there is no free variable — it is definitely true (every integer plus $0$ is itself).
2.3 † Let $c$ = Carol, $d$ = Dan. "Carol is a manager, Dan is not, and Carol earns more than Dan": $$M(c) \land \neg M(d) \land E(c, d).$$
2.5 † Domain: all people; $S(x)$ = "$x$ is a student," $A(x)$ = "$x$ aced the final." (a) "Every student aced the final" (restricted universal, $\rightarrow$): $\forall x\,(S(x) \rightarrow A(x))$. (b) "Some student aced the final" (restricted existential, $\land$): $\exists x\,(S(x) \land A(x))$. (c) "No student aced the final" = "every student did not ace it": $\forall x\,(S(x) \rightarrow \neg A(x))$, equivalently $\neg\,\exists x\,(S(x) \land A(x))$.
2.7 † Domain: integers, $S(x,y)$ = "$x + y = 0$." (a) $\forall x\,\exists y\,S(x,y)$: "for every integer $x$ there is an integer $y$ with $x + y = 0$." True — given $x$, take $y = -x$ (the witness depends on $x$, which is allowed since $\exists y$ is inside $\forall x$). (b) $\exists y\,\forall x\,S(x,y)$: "there is one integer $y$ that cancels every $x$." False — a fixed $y$ satisfying $x + y = 0$ for all $x$ is impossible (if $y$ worked for $x=1$ then $y=-1$, but then $2 + (-1) = 1 \ne 0$). (c) $\exists x\,\exists y\,S(x,y)$: "some pair sums to $0$." True — e.g. $x=1, y=-1$.
2.9 † Domain: integers. (a) "Every positive integer is $\ge 1$" (restricted universal, so $\rightarrow$): $\forall x\,(x > 0 \rightarrow x \ge 1)$. (b) "Some even integer is positive" (restricted existential, so $\land$): $\exists x\,(\text{Ev}(x) \land \text{Pos}(x))$, equivalently $\exists x\,(\text{Ev}(x) \land x > 0)$ (witness $x = 2$).
2.11 † $\forall x\,\forall y\,\exists z\,(x < z \land z < y)$ reads: "between any two integers $x$ and $y$ there is an integer $z$ strictly between them." Over the integers this is false: take $x = 0$, $y = 1$; there is no integer strictly between $0$ and $1$. (It is also false whenever $x \ge y$, since then no $z$ can satisfy $x < z < y$ at all.) Over the rationals $\mathbb{Q}$ the analogous statement is true — between any two distinct rationals lies another rational, e.g. their average $z = \frac{x+y}{2}$ (this property is called density). Same formula, different domain, different truth value — the §2.1 lesson that the domain is part of the meaning.
2.13 † (Scaffolded.) - Start: $\neg\,\forall x\,(Q(x) \rightarrow P(x))$. - Quantifier negation law $\neg\forall x\,(\cdots) \equiv \boxed{\exists x\,\neg(\cdots)}$, giving $\exists x\,\neg\big(Q(x) \rightarrow P(x)\big)$. - Implication-negation law $\neg(a \rightarrow b) \equiv \boxed{a \land \neg b}$ applied to the body, giving $\exists x\,\big(\boxed{Q(x) \land \neg P(x)}\big)$. - Conclusion: the negation says "there is an $x$ that is $Q$ and fails $P$" — i.e. a counterexample (an element satisfying the hypothesis but not the conclusion). $\blacksquare$
2.15 † (Scaffolded.) Suppose $\exists y\,\forall x\,L(x,y)$ holds, so there is a specific $y_0$ with $\forall x\,L(x, y_0)$. To prove $\forall x\,\exists y\,L(x,y)$, take an arbitrary $x$. Choose $y = \boxed{y_0}$. Then $L(x, y_0)$ holds because $\forall x\,L(x, y_0)$ holds for every $x$, in particular this one (universal instantiation). Since $x$ was arbitrary, $\forall x\,\exists y\,L(x,y)$. $\blacksquare$ The converse fails because $\forall x\,\exists y$ lets the $y$ depend on $x$ (each $x$ may use a different witness), so there need be no single $y_0$ that works for all $x$ — exactly the $\forall x\,\exists y\,(y > x)$ vs. $\exists y\,\forall x\,(y > x)$ example over the integers.
2.16 † all(0 < x < 100 for x in xs) is "$\forall x.\ 0 < x < 100$"; the existential dual is
any(not (0 < x < 100) for x in xs). On xs = [10, 50, 250]: the universal is False ($250$ is not
$< 100$) and the existential is True ($250$ is outside the range). See code/exercise-solutions.py,
ex_2_16.
# Expected output:
# False
# True
2.17 is_strictly_increasing(xs) = all(xs[i] < xs[i+1] for i in range(len(xs)-1)). On
[1, 4, 4, 9] it is False (at $i = 1$, $4 < 4$ fails); on [1, 4, 7, 9] it is True. See
ex_2_17.
2.18 † find_witness(predicate, domain) loops and returns the first x with predicate(x) true,
else None. It is the existential mirror of §2.4's counterexample: that returns the first element
where a predicate is false (a witness to $\exists x\,\neg P(x)$); find_witness returns the first
where it is true (a witness to $\exists x\,P(x)$). Finding the first composite in range(2, 8):
$2, 3$ are prime, $4$ is composite, so it returns 4. See ex_2_18.
2.19 all(any(x + y == 0 for y in D) for x in D) with D = range(-3, 4) is True: the slice
$\{-3, \dots, 3\}$ is symmetric, so each $x$ has its partner $-x$ inside it. A True here is not a
proof over all of $\mathbb{Z}$ — it only checks the finitely many $x$ in this slice (§2.3 pitfall, theme
four). (Over the asymmetric slice range(-3, 3) $= \{-3,\dots,2\}$ it would be False: $x = -3$
needs $y = 3$, which is absent.) See ex_2_19.
2.20 † Find the error. (i) Wrong translation: "some prime is even" is an existential restriction, so it must use $\land$: $\exists x\,(\text{Prime}(x) \land \text{Even}(x))$, not $\exists x\,(\text{Prime}(x) \rightarrow \text{Even}(x))$ with $\rightarrow$. (ii) Why $x = 9$ does not establish it: in the (wrong) implication form, $x = 9$ makes the hypothesis $\text{Prime}(9)$ false, so the implication is vacuously true — but a vacuously satisfied implication exhibits no even prime at all; it just exploits a non-prime. The correct statement is settled by the witness $x = 2$, which is genuinely prime and even. Correct translation: $\exists x\,(\text{Prime}(x) \land \text{Even}(x))$, true via $x = 2$.
2.21 Find the error. The proposed negation "$\exists x\,(x > 0 \rightarrow x^2 \le 0)$" mishandles the implication. Use $\neg(a \rightarrow b) \equiv a \land \neg b$ inside the existential: $$\neg\,\forall x\,(x > 0 \rightarrow x^2 > 0) \equiv \exists x\,\neg(x > 0 \rightarrow x^2 > 0) \equiv \exists x\,(x > 0 \land x^2 \le 0).$$ So the correct negation is $\exists x\,(x > 0 \land x^2 \le 0)$ — "some positive integer has a non-positive square." The original statement is true over the integers (any $x > 0$ has $x^2 > 0$), so its negation is false: there is no positive integer with $x^2 \le 0$.
2.22 † Find the error. The false premise is "reordering adjacent quantifiers never changes meaning." That holds only for like quantifiers ($\forall\forall$, $\exists\exists$); a $\forall$ and an $\exists$ in mixed order generally do not commute (§2.3). Truth values over $\mathbb{Z}$: $\forall x\,\exists y\,(y > x)$ is true (take $y = x + 1$); $\exists y\,\forall x\,(y > x)$ is false (no single integer exceeds every integer — the integers have no maximum).
2.23 Find the error. all(u.has_paid for u in customers) is a universal that is vacuously
true when customers == [] (§2.5): with no customers, "all customers have paid" is satisfied with no
witness required. So all_paid == True does not imply a paying customer exists. When
customers == [], all_paid is True. The reviewer's real intent ("everyone paid and there is at
least one") is captured by guarding the empty case:
all_paid = bool(customers) and all(u.has_paid for u in customers)
2.24 † (Model it.) Domains: Users, Resources; predicate $\text{Can}(u, r)$ = "$u$ may access
$r$."
(a) "Every user can access at least one resource": $\forall u\,\exists r\,\text{Can}(u, r)$.
(b) "There is a public resource every user can access": $\exists r\,\forall u\,\text{Can}(u, r)$ (note
the reversed order — strictly stronger than (a)).
(c) "No resource is accessible to every user":
$\neg\,\exists r\,\forall u\,\text{Can}(u, r)$, equivalently
$\forall r\,\exists u\,\neg\text{Can}(u, r)$.
Negation of (c) (what an auditor checks): $\exists r\,\forall u\,\text{Can}(u, r)$ — "there is a
resource accessible to every user" (a possibly-undesired universally-open resource). In plain English,
the auditor looks for a single resource that every account can reach.
2.25 (Model it.) Domains: Requests, Servers; $\text{Handles}(s, r)$ = "server $s$ handles
request $r$." Reliability: $\forall r\,\exists s\,\text{Handles}(s, r)$ ("every request is handled by
some server"). Redundancy:
$\forall r\,\exists s_1\,\exists s_2\,\big(s_1 \ne s_2 \land \text{Handles}(s_1, r) \land
\text{Handles}(s_2, r)\big)$. Redundancy is stronger — it implies reliability (two handlers imply
at least one). Order matters when comparing $\exists s\,\forall r\,\text{Handles}(s, r)$ ("one server
handles every request" — a single point of overload/failure) to the reliability statement
$\forall r\,\exists s$: the former fixes a single server for all requests; the latter lets each request
pick its own. $\exists s\,\forall r$ implies $\forall r\,\exists s$, never the reverse (§2.3).
2.26 † (Conjecture and test, then identify.)
(a) Test snippet (see code/exercise-solutions.py, ex_2_26):
def is_prime(n):
return n > 1 and all(n % d != 0 for d in range(2, int(n**0.5) + 1))
holds = all(any(is_prime(p) for p in range(n+1, 2*n+1)) for n in range(1, 51))
print(holds)
# Expected output:
# True
It returns True: every $n$ from $1$ to $50$ has a prime in $(n, 2n]$. This establishes the claim
for $n \le 50$ only — it is evidence, not proof, since it leaves all $n > 50$ untested.
(b) Negation: $\exists n\,\forall p\,\big(n < p \le 2n \rightarrow \neg\,\text{Prime}(p)\big)$ — "some
$n$ has no prime in $(n, 2n]$." A counterexample would be such an $n$.
(c) This is Bertrand's postulate (proved by Chebyshev; a famous theorem). The code is evidence,
not proof, exactly because a universal over the infinitely many positive integers cannot be settled by
checking finitely many of them (theme four).
2.27 (Conjecture and test, then disprove.)
(a) Using the counterexample pattern over range(0, 45) (see ex_2_27), the search returns 40:
the first $n$ for which $n^2 + n + 41$ is not prime.
(b) By the negation law, a single returned value $n = 40$ proves $\exists n\,\neg\text{Prime}(n^2 +
n + 41)$ — it disproves the universal claim "$\forall n\,\text{Prime}(n^2 + n + 41)$" outright (§2.4).
One counterexample is sufficient; no further search is needed.
(c) By hand: $40^2 + 40 + 41 = 1600 + 40 + 41 = 1681 = 41 \times 41 = 41^2$, which is composite (it has
the divisor $41$). So $n = 40$ is a genuine counterexample. (This is the well-known Euler polynomial
$n^2 + n + 41$, prime for $n = 0, \dots, 39$ but not at $n = 40$.) $\blacksquare$
2.28 † (Ch. 1 + 2.) Using $a \rightarrow b \equiv \neg a \lor b$, the body $Q(x) \rightarrow P(x)$ becomes $\neg Q(x) \lor P(x)$, so "every $Q$ is a $P$" is $\forall x\,(\neg Q(x) \lor P(x))$. For an element that is not $Q$ (so $\neg Q(x)$ is true), the disjunction is automatically true — the statement imposes no constraint on non-$Q$ elements. This is precisely why the restricted universal harmlessly ignores elements outside the restriction (the vacuous case from §2.2).
2.29 (Ch. 1 + 2.) The statement $\forall (p,q)\,\big((p \land q) \rightarrow (p \lor q)\big)$ claims that $(p \land q) \rightarrow (p \lor q)$ is true for all truth-value assignments — i.e. it is a tautology (a §1 concept). Verify all four rows:
| $p$ | $q$ | $p \land q$ | $p \lor q$ | $(p\land q)\rightarrow(p\lor q)$ |
|---|---|---|---|---|
| T | T | T | T | T |
| T | F | F | T | T |
| F | T | F | T | T |
| F | F | F | F | T (hypothesis false) |
All four rows are T, confirming the tautology. (A quantifier over all assignments is exactly what "is a tautology" means.)
2.30 † (Ch. 1 + 2.) Over a finite domain $D = \{d_1, \dots, d_n\}$, the universal is a giant conjunction: $\forall x\,P(x) \equiv P(d_1) \land \dots \land P(d_n)$. Applying De Morgan's law $\neg(p \land q) \equiv \neg p \lor \neg q$ repeatedly across all $n$ conjuncts gives $\neg\big(P(d_1) \land \dots \land P(d_n)\big) \equiv \neg P(d_1) \lor \dots \lor \neg P(d_n)$, which is the giant disjunction $\exists x\,\neg P(x)$. So $\neg\forall x\,P(x) \equiv \exists x\,\neg P(x)$ is literally De Morgan's law stretched over the domain — the quantifier negation law and the propositional one are the same law.
2.31 (Ch. 1 + 2.)
(a) Precondition as code: all(a[i] <= a[i+1] for i in range(n-1)) (with n = len(a)).
(b) If the precondition is violated, its negation holds:
$\exists i\,(0 \le i < n-1 \land a[i] > a[i+1])$ — there is an index $i$ where order breaks. A checker
returns that witnessing index $i$ (the first place $a[i] > a[i+1]$), exactly the
$\exists i\,\neg(\dots)$ from §2.4.
(c) "Passed 100 tests" verifies the postcondition for 100 specific inputs — 100 true conjuncts of the
giant $\land$ over all valid inputs. The input domain (e.g. all sorted arrays) is infinite, so
infinitely many conjuncts remain unchecked; a counterexample among the untested cannot be ruled out by
any finite test, only by a proof (theme two).
Chapter 3 — Worked Solutions (dagger † and odd-numbered exercises)
Full solutions to the † and odd-numbered exercises from exercises.md. Rubric reminders: name a rule on
every derivation line; justifications cite only earlier lines; for "find the error," state whether
the defect is invalidity (bad form) or unsoundness (a false premise).
Part A — Warm-ups
3.1 † The rule is modus tollens ($p \rightarrow q,\ \neg q \vdash \neg p$). The conditional whose tautology-hood confirms validity is $$\big((p \rightarrow q) \land \neg q\big) \rightarrow \neg p.$$ (Its four-row truth table is all-T; see 3.28.)
3.3 † (a) Simplification ($a \land b \vdash a$). (b) Conjunction ($a, b \vdash a \land b$). (c) Addition ($a \vdash a \lor b$). (d) Disjunctive syllogism ($a \lor b, \neg a \vdash b$). (e) Hypothetical syllogism ($a \rightarrow b, b \rightarrow c \vdash a \rightarrow c$).
3.5 † (a) UI: $c$ is any (arbitrary or specific) element of the domain — no restriction. (b) EI: $c$ is a fresh name, not used anywhere earlier in the derivation. (c) UG: $c$ is truly arbitrary — the subproof of $P(c)$ assumed no special property of $c$.
Part B — Prove it
3.7 † Completed scaffold:
| Step | Statement | Justification |
|---|---|---|
| 1 | $p \rightarrow q$ | Premise |
| 2 | $q \rightarrow r$ | Premise |
| 3 | $p$ | Premise |
| 4 | $q$ | Modus ponens, lines 1, 3 |
| 5 | $r$ | Modus ponens, lines 2, 4 |
Two-line version using hypothetical syllogism:
| Step | Statement | Justification |
|---|---|---|
| 1 | $p \rightarrow q$ | Premise |
| 2 | $q \rightarrow r$ | Premise |
| 3 | $p$ | Premise |
| 4 | $p \rightarrow r$ | Hypothetical syllogism, 1, 2 |
| 5 | $r$ | Modus ponens, 3, 4 |
The HS version is shorter, but "shorter" is not "more valid." Both derivations are valid; validity is all-or-nothing (the conclusion either is or isn't a valid consequence). A shorter derivation is just more economical — there is no partial credit for validity.
3.9 † Strategy: the goal $u$ comes only from $r \rightarrow u$ by modus ponens, so I need $r$. I can get $q \lor r$ from $p$ and $p \rightarrow (q \lor r)$ (modus ponens); ruling out $q$ with $\neg q$ gives $r$ by disjunctive syllogism; then modus ponens yields $u$.
| Step | Statement | Justification |
|---|---|---|
| 1 | $p$ | Premise |
| 2 | $p \rightarrow (q \lor r)$ | Premise |
| 3 | $\neg q$ | Premise |
| 4 | $r \rightarrow u$ | Premise |
| 5 | $q \lor r$ | Modus ponens, 1, 2 |
| 6 | $r$ | Disjunctive syllogism, 5, 3 |
| 7 | $u$ | Modus ponens, 4, 6 |
3.11 † Strategy: strip the $\forall$ with UI at $c = \text{milo}$, then finish with modus ponens.
| Step | Statement | Justification |
|---|---|---|
| 1 | $\forall x\,(C(x) \rightarrow R(x))$ | Premise |
| 2 | $C(\text{milo})$ | Premise |
| 3 | $C(\text{milo}) \rightarrow R(\text{milo})$ | Universal instantiation, 1 (take $c = \text{milo}$) |
| 4 | $R(\text{milo})$ | Modus ponens, 2, 3 |
This is the two-phase rhythm of §3.4: instantiate to a specific element, then do propositional logic.
Part C — Implement it
(Reference code: code/exercise-solutions.py.)
3.13 † Test that denying the antecedent is invalid:
da = is_valid([lambda p, q: implies(p, q), lambda p, q: not p],
lambda p, q: not q, ["p", "q"])
print(da)
# Expected output:
# False
Expected boolean: False. The producing assignment is $p=$False, $q=$True: then $p \rightarrow q$
is true and $\neg p$ is true (both premises hold), while the conclusion $\neg q$ is false — a
counterexample, so is_valid returns False.
3.15 † See solution code. Output is True True False. The first two values show some element is
even and some element is odd over range(1,11); the third shows no single element is both. That is
exactly why you may not instantiate $\exists x\,P(x)$ and $\exists x\,Q(x)$ with the same fresh name:
the witness for "even" and the witness for "odd" are different elements, so a shared name would falsely
assert one element satisfies both (the EI freshness pitfall, §3.4).
Part D — Find the error
3.17 † Symbolize: $D$ = "deadlock," $T$ = "request times out." The argument is $D \rightarrow T$, $T$, $\therefore D$ — affirming the consequent, so it is invalid (a form failure, not a premise failure). Counterexample: a timeout can have other causes — a slow downstream API, a thread-pool exhaustion, GC pause, or a network blip — so $T$ can be true while $D$ is false ($p=$False, $q=$True makes both premises true and the conclusion false). The fix is to gather evidence that distinguishes deadlock from the alternatives (e.g., a thread dump showing a lock cycle), not to restart on the symptom.
3.19 † The broken side condition is UG's "arbitrary" requirement. Choosing $c = 0$ assumes a special property ($c$ is the specific value $0$), so $c$ is not arbitrary and UG does not apply. What the argument actually proves is just $P(0)$ — a single instance — not $\forall x\,P(x)$. (Proving $P$ for one element, or even for "all even elements," never licenses the universal unless the element was genuinely unconstrained throughout.)
3.21 † Symbolize: $C$ = "input crashes the parser," $M$ = "input is malformed." Premise 1 is $C \rightarrow M$ ("all crashing inputs are malformed"). The argument is $C \rightarrow M$, $M$, $\therefore C$ — affirming the consequent, hence invalid (bad form). Counterexample: a malformed input the parser handles gracefully (e.g., it is rejected with a clean error rather than crashing) makes $M$ true and $C$ false. Malformed-ness does not imply a crash; the implication runs the other way.
Part E — Model it
3.22 † Let $O$ = "u is an owner," $W$ = "u may write," $R$ = "u may read." The two rules are $O \rightarrow W$ and $W \rightarrow R$. Observation: $\neg R$ ("u cannot read"). Derivation:
| Step | Statement | Justification |
|---|---|---|
| 1 | $O \rightarrow W$ | Premise |
| 2 | $W \rightarrow R$ | Premise |
| 3 | $\neg R$ | Premise (observation) |
| 4 | $O \rightarrow R$ | Hypothetical syllogism, 1, 2 |
| 5 | $\neg O$ | Modus tollens, 4, 3 |
Valid conclusion: u is not an owner ($\neg O$). (You may also derive $\neg W$ by modus tollens on 2, 3; then $\neg O$ by modus tollens on 1 — either route is valid.)
3.24 † Premises $x : \text{int}$ and $y : \text{int}$; one-step derivation:
| Step | Statement | Justification |
|---|---|---|
| 1 | $x : \text{int}$ | Premise |
| 2 | $y : \text{int}$ | Premise |
| 3 | $x + y : \text{int}$ | typing rule (+), 1, 2 |
The horizontal bar of a typing rule corresponds to the $\therefore$ of an inference (an inference rule): the judgment below the bar follows from the premises above it. A typing rule is an inference rule (§3.6), so step 3 is "modus-ponens-like" application of the (+) rule.
Part F — Conjecture and test
3.25 † Test the proposed "rule" $p \rightarrow q,\ p \lor r \vdash q \lor r$:
ok = is_valid([lambda p, q, r: implies(p, q), lambda p, q, r: p or r],
lambda p, q, r: q or r, ["p", "q", "r"])
print(ok)
# Expected output:
# True
It is valid. Proof / matching rule: note $p \rightarrow q \equiv \neg p \lor q$ (Ch. 1), so the
premises are $\neg p \lor q$ and $p \lor r$. Resolving on $p / \neg p$ (the resolution rule, §3.2,
$\neg p \lor q,\ p \lor r \vdash q \lor r$) gives exactly $q \lor r$. So the conjecture is a disguised
instance of resolution — valid. (Hand-check of is_valid: the only way to falsify $q \lor r$ is
$q=$False and $r=$False; then $p \lor r$ forces $p=$True, but $p \rightarrow q$ with $p=$True forces
$q=$True — contradiction. No counterexample exists, so the output is True.)
3.27 † Conjecture: a chain of $n$ implications entails $p_1 \rightarrow p_n$.
- $n = 2$: $p_1 \rightarrow p_2,\ p_2 \rightarrow p_3 \vdash p_1 \rightarrow p_3$ — this is exactly one
application of hypothetical syllogism.
- $n = 3, 4$: apply HS repeatedly (chain the first two into $p_1 \rightarrow p_3$, then with
$p_3 \rightarrow p_4$ into $p_1 \rightarrow p_4$, etc.). To check with is_valid, build the list of
premises [implies(p1,p2), implies(p2,p3), implies(p3,p4)] and conclusion implies(p1,p4) over four
named variables and confirm it returns True.
The single rule that, applied repeatedly, proves the general claim is hypothetical syllogism (transitivity of implication). Making "for all $n$" rigorous needs mathematical induction on the chain length — the technique of Chapter 6 (proof techniques begin in Chapter 4). One HS application is the inductive step; the two-link case is the base.
Part G — Interleaved
3.28 † Conditional for modus tollens: $\big((p \rightarrow q) \land \neg q\big) \rightarrow \neg p$. Truth table:
| $p$ | $q$ | $p \rightarrow q$ | $\neg q$ | $(p \rightarrow q) \land \neg q$ | $\neg p$ | conditional |
|---|---|---|---|---|---|---|
| T | T | T | F | F | F | T |
| T | F | F | T | F | F | T |
| F | T | T | F | F | T | T |
| F | F | T | T | T | T | T |
The last column is all-T, so the conditional is a tautology and modus tollens is valid. (The only row where the antecedent $(p \rightarrow q) \land \neg q$ is true is row 4, and there $\neg p$ is true.)
3.29 Rewrite $p \lor q \equiv \neg p \rightarrow q$ (Ch. 1: implication-as-disjunction). Now the premises of disjunctive syllogism, $p \lor q$ and $\neg p$, become $\neg p \rightarrow q$ and $\neg p$ — and modus ponens immediately yields $q$. So disjunctive syllogism is just modus ponens on the implication-form of the disjunction; that establishes its validity without a truth table.
3.31 Over range(1,6), $E(x)$ = "x is even," $D(x)$ = "x divisible by 2." Since "even" and
"divisible by 2" coincide, implies(E(x), D(x)) is True for every $x$ in $1..5$, so
forall(domain, lambda x: implies(E(x), D(x))) evaluates to True. Instantiation at $c = 4$ (UI)
gives $E(4) \rightarrow D(4)$; since $E(4)$ holds ($4$ is even), modus ponens yields $D(4)$
("therefore $4$ is divisible by 2"). This is the §3.4 two-phase rhythm: UI then modus ponens.
3.32 † Premises: $\forall x\,\big((\text{Prime}(x) \land x > 2) \rightarrow \text{Odd}(x)\big)$ and $\exists x\,(\text{Prime}(x) \land x > 2)$. Conclusion: $\exists x\,\text{Odd}(x)$. Derivation:
| Step | Statement | Justification |
|---|---|---|
| 1 | $\forall x\,((\text{Prime}(x) \land x>2) \rightarrow \text{Odd}(x))$ | Premise |
| 2 | $\exists x\,(\text{Prime}(x) \land x>2)$ | Premise |
| 3 | $\text{Prime}(c) \land c>2$ | Existential instantiation, 2 ($c$ fresh) |
| 4 | $(\text{Prime}(c) \land c>2) \rightarrow \text{Odd}(c)$ | Universal instantiation, 1 (take that same $c$) |
| 5 | $\text{Odd}(c)$ | Modus ponens, 3, 4 |
| 6 | $\exists x\,\text{Odd}(x)$ | Existential generalization, 5 |
Rules in order: EI, UI, modus ponens, EG. (Order matters: do EI first so $c$ is fresh, then UI on that $c$.) The argument is valid and sound — both premises are true number-theoretic facts (every prime $> 2$ is odd; primes $> 2$ exist, e.g. $3$), so the conclusion "there exists an odd number" is guaranteed true. Hence: both (valid and sound).
Chapter 4 — Direct Proof and Contraposition
Solutions to the daggered (†) and odd-numbered exercises. For every proof, the rubric rewards: a named strategy, an arbitrary variable in the setup, the correct hypothesis assumed, explicit use of definitions (unpack/repackage), and a clean close ending in $\blacksquare$.
Part A — Warm-ups
4.1 † (a) "$4 \mid 28$" means $\exists c \in \mathbb{Z},\ 28 = 4c$; witness $c = 7$. (b) "$-72$ is even" means $\exists k \in \mathbb{Z},\ -72 = 2k$; witness $k = -36$. (c) "$a \mid 0$" means $\exists c \in \mathbb{Z},\ 0 = ac$; witness $c = 0$ (works for every nonzero $a$).
4.3 † (a) conjecture (the Goldbach conjecture — believed, not proved). (b) corollary (a special case obtained for free from a general result already proved). (c) lemma (a helper proved to make a later proof read cleanly). (d) theorem (a provable, established statement of some interest).
4.5 † Unpack: "By definition, there is an integer $k$ with $n = 2k + 1$." Repackage: "Let $m = 2k^2 + 2k$ … $n^2 = 2m + 1$. By the definition of odd, $n^2$ is odd." The unpack turns the hypothesis into an equation; the repackage forces the result into the shape the goal's definition requires.
Part B — Prove it
4.7 † Blanks, in order: $n = \mathbf{2k}$; $5(\mathbf{2k}) + 4$; $2(\mathbf{5k + 2})$; $m = \mathbf{5k + 2}$. Full proof. Let $n$ be an arbitrary integer and assume $n$ is even, so $n = 2k$ for some integer $k$. Then $5n + 4 = 10k + 4 = 2(5k + 2)$. With $m = 5k + 2$ (an integer), $5n + 4 = 2m$, so $5n + 4$ is even. Since $n$ was arbitrary, the statement holds for all integers $n$. $\blacksquare$
4.9 † Theorem. For all integers $a, b, c$ with $a \neq 0$, if $a \mid b$ then $a \mid bc$. Proof (direct). Assume $a \mid b$. By the definition of divides, there is an integer $s$ with $b = as$. Then $bc = (as)c = a(sc)$. Since $s$ and $c$ are integers, $sc$ is an integer, so there is an integer — namely $sc$ — with $bc = a(sc)$. By the definition of divides, $a \mid bc$. $\blacksquare$
4.11 † Theorem. For every integer $n$, if $3n + 5$ is even, then $n$ is odd. Strategy. Contraposition: the contrapositive is "if $n$ is not odd (i.e. even), then $3n + 5$ is not even (i.e. odd)." Proof (by contraposition). Assume $n$ is even, so $n = 2k$ for some integer $k$. Then $3n + 5 = 6k + 5 = 2(3k + 2) + 1$. With $m = 3k + 2$ (an integer), $3n + 5 = 2m + 1$, so $3n + 5$ is odd — that is, not even. This proves the contrapositive, hence the original: if $3n + 5$ is even, then $n$ is odd. $\blacksquare$
4.13 † Theorem. For every integer $n$, $n$ is even iff $7n$ is even. Forward ($n$ even $\Rightarrow 7n$ even), direct. Assume $n = 2k$. Then $7n = 14k = 2(7k)$, even (this is the §4.3 "$a \mid b \Rightarrow a \mid bc$" pattern with $a=2$). Reverse ($7n$ even $\Rightarrow n$ even). The reverse is more naturally done by contraposition: its contrapositive is "if $n$ is odd, then $7n$ is odd." Assume $n = 2k + 1$. Then $7n = 14k + 7 = 2(7k + 3) + 1$, odd. This proves the contrapositive of the reverse direction. Both directions hold, so the biconditional holds. $\blacksquare$ (Why contraposition for the reverse: assuming "$7n$ is even" gives an equation about $7n$, not a clean handle on $n$ itself — the same stall that motivated §4.4.)
Part C — Implement it
4.15 † See code/exercise-solutions.py.
def same_parity(a, b):
return a % 2 == b % 2
print(same_parity(4, 10)) # True (both even)
print(same_parity(3, 8)) # False (odd, even)
print(same_parity(-7, 5)) # True (both odd: -7 % 2 == 1, 5 % 2 == 1)
# Expected output:
# True
# False
# True
Note $-7 \bmod 2 = 1$ in Python (the result takes the sign of the divisor), so two odd negatives are correctly reported same-parity.
4.17 † See code/exercise-solutions.py.
claim = lambda n: (n % 2 == 0) or (5 * n + 3) % 2 == 0 # "n odd -> 5n+3 even"
print(find_counterexample(claim, range(-50, 51)))
# Expected output:
# None
Output None supports the claim (no counterexample in $[-50,50]$); a returned integer would
refute it. As §4.1 stresses, None is evidence, not proof — the actual proof is in Exercise 4.24.
Part D — Find the error
4.18 † The student wrote the converse, not the contrapositive. For the claim "$n^2$ even $\rightarrow n$ even," set $P$ = "$n^2$ is even" and $Q$ = "$n$ is even." The true contrapositive is $\neg Q \rightarrow \neg P$ = "if $n$ is odd, then $n^2$ is odd" (negate both parts). What the student labeled "the contrapositive" — "if $n$ is even then $n^2$ is even" — is $Q \rightarrow P$, the converse, and the converse is not logically equivalent to the original (§4.4). So although the student's algebra is correct and the claim happens to be true, the argument proves the wrong statement and establishes nothing about the original. Correct contraposition: assume $n$ is odd, $n = 2k+1$, then $n^2 = 2(2k^2 + 2k) + 1$ is odd; this is $\neg Q \rightarrow \neg P$ and finishes the proof. Lesson: write $\neg P$ and $\neg Q$ out explicitly before you start — this is exactly the §4.4 Find-the-Error trap. (Contrast: the converse-looking move would actually be a correct contraposition for the different claim "$n^2$ odd $\rightarrow n$ odd," whose contrapositive genuinely is "$n$ even $\rightarrow n^2$ even." The direction of the original claim is what decides it.)
4.19 Bug: the line "$ak + 1 = a(k) + 1$, so $a$ divides $b+1$" is unjustified — $a(k) + 1$ is not of the form $a \cdot (\text{integer})$ unless $a \mid 1$. The author never exhibited an integer $c$ with $b + 1 = ac$. The claim is false: take $a = 3, b = 3$. Then $3 \mid 3$, but $3 \nmid 4$. Counterexample $a = 3, b = 3$ (indeed any $a \ge 2$ dividing $b$).
4.21 This is proof by example — checking $n = 1, 3, 5$ proves nothing about all odd $n$ (§4.1, §4.5). The one-word fix to the setup: make $n$ arbitrary ("let $n$ be an arbitrary odd integer, $n = 2k+1$ …") instead of three specific values. (The claim is true; the argument isn't a proof. A correct proof is Example 1 of §4.3.)
Part E — Model it
4.22 † Translation. "Full build" = $4 \mid n$; "passes the lightweight check" = $2 \mid n$. The claim is: for every integer $n$, if $4 \mid n$ then $2 \mid n$. Proof (direct). Assume $4 \mid n$, so $n = 4c$ for some integer $c$. Then $n = 4c = 2(2c)$. With $k = 2c$ (an integer), $n = 2k$, so $2 \mid n$ (equivalently $n$ is even). Hence every full-build commit also passes the lightweight check. $\blacksquare$ (This is the §4.3, Example 2 / §4.3 transitivity pattern: $4 = 2 \cdot 2$, so a multiple of 4 is a multiple of 2. The converse — "every even commit triggers a full build" — is false: $n = 2$ is even but $4 \nmid 2$.)
Part F — Conjecture and test, then prove
4.24 † Test. find_counterexample(lambda n: (n%2==0) or (5*n+3)%2==0, range(-50,51)) returns
None — no counterexample in $[-50, 50]$. So we expect the claim to be true and prove it.
Theorem. For every integer $n$, if $n$ is odd, then $5n + 3$ is even.
Proof (direct). Assume $n$ is odd, so $n = 2k + 1$ for some integer $k$. Then
$5n + 3 = 5(2k+1) + 3 = 10k + 8 = 2(5k + 4)$. With $m = 5k + 4$ (an integer), $5n + 3 = 2m$, so
$5n + 3$ is even. Since $n$ was arbitrary, the claim holds for all integers $n$. $\blacksquare$
(Contrast 4.17: the code gave evidence; this proof gives the guarantee — theme four.)
4.26 † Test. Searching pairs $(a,b)$ with $a,b \in \{-5,\dots,5\}\setminus\{0\}$ for a pair where
$a \mid b$, $b \mid a$, but $a \neq b$ returns (-5, 5) (the first such pair scanning ascending):
$5 \mid -5$ and $-5 \mid 5$, yet $-5 \neq 5$. So the claim is false. Other witnesses: $(2,-2)$,
$(3,-3)$, etc.
Fixed claim (true). For all nonzero integers $a, b$: if $a \mid b$ and $b \mid a$, then
$\lvert a \rvert = \lvert b \rvert$ (equivalently $a = b$ or $a = -b$). This repairs the dropped
negative case that broke the §4.6 proof ($km = 1$ has solutions $k=m=1$ and $k=m=-1$). (No proof
required, but the fix is exactly: from $a = a(km)$ with $a \neq 0$ deduce $km = 1$, so
$(k,m) \in \{(1,1),(-1,-1)\}$, giving $b = ak = \pm a$.)
Part G — Interleaved
4.27 † The two truth tables:
| $P$ | $Q$ | $P \rightarrow Q$ | $\neg Q$ | $\neg P$ | $\neg Q \rightarrow \neg P$ |
|---|---|---|---|---|---|
| T | T | T | F | F | T |
| T | F | F | T | F | F |
| F | T | T | F | T | T |
| F | F | T | T | T | T |
The boxed columns ($P\rightarrow Q$ and $\neg Q \rightarrow \neg P$) are identical row for row, so the two statements are logically equivalent ($\equiv$). That equivalence — a fact about all truth assignments, established once by the table — is what licenses proving the contrapositive in place of the original; no number of example integers could establish a logical equivalence.
4.29 † (a) A direct proof of $P \rightarrow Q$ constructs modus ponens: combined with a proof of $P$, it yields $Q$. (b) Proving $Q \rightarrow P$ and claiming $P \rightarrow Q$ commits the fallacy of affirming the consequent (Chapter 3). It is the converse error: $Q \rightarrow P$ is the converse of $P \rightarrow Q$, and the converse is not logically equivalent to the original (unlike the contrapositive).
4.31 † Since $P \leftrightarrow Q \equiv (P \rightarrow Q) \land (Q \rightarrow P)$, proving only $P \rightarrow Q$ establishes one conjunct and leaves $Q \rightarrow P$ (the converse) unproved; the biconditional can therefore still fail. A claim where the converse is genuinely false: "if $n$ is divisible by 6, then $n$ is divisible by 3" — the converse "if $3 \mid n$ then $6 \mid n$" fails at $n = 9$ ($3 \mid 9$ but $6 \nmid 9$). For that claim, proving only the forward direction would leave a real, false gap — which is exactly why both directions are mandatory.
All math hand-verified. Daggered/odd solutions only; even-numbered exercises are left for the reader per the style bible.
Chapter 5 — Proof by Contradiction and Cases
5.1 † "Assume $\neg P$ (that $P$ is false), then derive a contradiction — a statement of the form $q \land \neg q$, i.e. something both true and false." Since $\neg P$ forces an impossibility, $\neg P$ cannot hold, so $P$ is true (law of the excluded middle).
5.3 † For $[2, 3, 5]$: $N = 2 \cdot 3 \cdot 5 + 1 = 31$. By hand, $31$ is prime (not divisible by $2, 3, 5$, and $7^2 = 49 > 31$). So here $N$ is prime — but the proof does not claim $N$ is always prime; it claims only that $N$ has a prime factor not in the list (here $31$ itself qualifies). The six-prime list gives the standard composite example $30031 = 59 \times 509$.
5.5 † One counterexample suffices. The negation of "$\forall n\, P(n)$" is "$\exists n\, \neg P(n)$" (Chapter 2), so a single $n$ with $P(n)$ false is a complete proof of the negation, hence a complete disproof of the original.
5.7 † Blanks, in order: lowest terms; $p^2 = \mathbf{3}\,q^2$; $q^2 = \mathbf{3}a^2$; $3$ divides both $p$ and $q$. Full proof: assume $\sqrt 3 = p/q$ in lowest terms, $q \ne 0$. Then $3 = p^2/q^2$, so $p^2 = 3q^2$, giving $3 \mid p^2$, hence (stated fact) $3 \mid p$; write $p = 3a$. Then $9a^2 = 3q^2$, so $q^2 = 3a^2$, giving $3 \mid q^2$, hence $3 \mid q$. So $3$ divides both $p$ and $q$, contradicting "lowest terms." Therefore $\sqrt 3$ is irrational. $\blacksquare$
5.9 † Claim: if $n^2$ is even then $n$ is even. Proof (by contradiction / contrapositive flavor): suppose $n^2$ is even but $n$ is odd. Then $n = 2a + 1$, so $n^2 = 4a^2 + 4a + 1 = 2(2a^2 + 2a) + 1$, which is odd — contradicting that $n^2$ is even. Hence $n$ is even. $\blacksquare$ (Equivalently: this is the contrapositive of "$n$ odd $\Rightarrow n^2$ odd," which the computation also proves directly.)
5.11 † Claim: $ab$ is even $\iff$ at least one of $a, b$ is even. ($\Leftarrow$) Suppose at least one is even. WLOG assume $a$ is even — legitimate because the claim is symmetric in $a$ and $b$ (swapping them leaves "$ab$ even" and "at least one even" unchanged). Write $a = 2k$; then $ab = 2(kb)$ is even. ($\Rightarrow$) Contrapositive: if neither is even, both are odd, $a = 2k+1$, $b = 2m+1$, so $ab = 4km + 2k + 2m + 1 = 2(2km + k + m) + 1$ is odd. So $ab$ even forces at least one even. $\blacksquare$ WLOG is justified here because the two variables play interchangeable roles.
5.13 † Claim: for $a \ne 0$, there is a unique real $x$ with $ax = 1$. Existence: take $x = 1/a$ (defined since $a \ne 0$); then $a(1/a) = 1$. Uniqueness: suppose $ax_1 = 1$ and $ax_2 = 1$. Then $ax_1 = ax_2$; multiplying both sides by $1/a$ (valid since $a \ne 0$) gives $x_1 = x_2$. So the inverse is unique. $\blacksquare$ (Existence used $a \ne 0$ to form $1/a$; uniqueness used it to divide.)
5.15 † same_parity(a, b) returns a % 2 == b % 2. The check over all pairs in range(-5, 6)
finds no counterexample, so the output reports any counterexample? False, confirming the claim on
the $11 \times 11 = 121$ tested pairs. This is not a proof: it tests finitely many pairs, leaving
infinitely many integer pairs unchecked (theme two). The proof is Exercise 5.11. See
code/exercise-solutions.py; expected output includes 5.15 any counterexample to claim? False.
5.17 † first_witness(2000) returns None: no denominator $q \le 2000$ admits an integer $p$
with $p^2 = 2q^2$. The relationship to §5.1: the theorem "$\sqrt 2$ is irrational" guarantees there is
no such fraction for any $q$, so the search must return None for every limit. The empty search
is consistent with the theorem but does not by itself prove it — the proof settles all $q$ at once, the
search only the ones tried (themes two and four).
5.18 † Flaw: WLOG is abused. The move "assume $a \ge 0$ and $b \ge 0$" is not without loss of generality, because the claim is not symmetric under sign changes — making $a$ and $b$ opposite signs is a genuinely different situation the assumption silently discards. The claim itself is false: take $a = 1$, $b = -1$: $\lvert a + b\rvert = \lvert 0\rvert = 0$, but $\lvert a\rvert + \lvert b\rvert = 1 + 1 = 2$, and $0 \ne 2$. (The *true* statement is the triangle inequality with $\le$, not $=$.)
5.19 Missing ingredient: the "in lowest terms" assumption and the contradiction it creates. The algebra correctly shows $p$ and $q$ are both even, but "both even" is only absurd when the fraction was assumed reduced. Without "lowest terms," every rational equals infinitely many fractions with even numerator and denominator (e.g. $\tfrac{2}{4}$), so "both even" is unremarkable and no contradiction arises. The proof never reaches a clash, so it proves nothing — it is incomplete, not merely terse.
5.20 † The author confuses checking an example with proving a $\forall$ statement, and is fumbling toward a counterexample without realizing it. The claim "$a \mid bc \Rightarrow a \mid b$ or $a \mid c$" is false. Clean counterexample: $a = 6$, $b = 2$, $c = 3$. Then $bc = 6$ and $6 \mid 6$, yet $6 \nmid 2$ and $6 \nmid 3$. So the implication fails. (The statement is true when $a$ is prime — that is Euclid's lemma, Chapter 22 — but not for composite $a$.)
5.21 Stopping at $n = 41$ does give a genuine failing case ($41^2 + 41 + 41 = 41 \cdot 43$, composite), so the conclusion is correct and the claim is indeed disproved — a counterexample need not be the smallest to be valid. The markdown is only about credit/clarity: the chapter's canonical disproof uses $n = 40$ (where $40^2 + 40 + 41 = 1681 = 41^2$), the smallest counterexample, which is the cleaner exhibit and matches the in-text result. For correctness, any single composite value works; $n = 41$ is a perfectly valid (if non-minimal) counterexample.
5.22 † Precise claim: there exists a total function loopguard(P, x) that for every program P
and input x returns True if P halts on x and False otherwise, and itself always halts.
That is the existence claim to refute. Shape of the refutation: proof by contradiction; the
self-referential gadget is a program trouble(P) that calls loopguard(P, P) and then does the
opposite of the prediction (loops if predicted to halt, halts if predicted to loop); asking what
trouble(trouble) does yields a contradiction either way. The assumption the contradiction destroys is
"loopguard exists and is always correct." Made rigorous in Chapter 36 (with the §5.2 preview
as the sketch).
5.23 As an $\exists!$ statement: "for every user $u$, $\exists!\, v\ \text{Match}(u, v)$." The two obligations: existence (at least one ideal match $v$ exists) and uniqueness (any two ideal matches are equal). If the database is empty, the existence obligation fails (there is no $v$ at all), while "at most one match" is vacuously true — so the guarantee is technically satisfiable on the uniqueness half yet practically empty, exactly the §5.4 vacuous-uniqueness pitfall. A correct guarantee must prove existence too.
5.24 † Testing $n = 1, \dots, 45$: the value $n^2 - n + 41$ is prime for $n = 1$ through $40$, and
the smallest counterexample is $n = 41$, where $41^2 - 41 + 41 = 41^2 = 1681$ is composite. So the
claim is false, and $n = 41$ is the disproof. (It is the close cousin of Euler's $n^2 + n + 41$:
substituting $n \to n-1$ shifts one polynomial to the other, so this one breaks at $41$ rather than
$40$.) Testing alone could only ever find the counterexample; it could never certify the claim for
all $n$, so even a clean run to $n = 40$ would not have proved it (theme two). find_counterexample
applied to this predicate over range(1, 46) returns 41.
5.25 Fermat numbers $f(n) = 2^{2^n} + 1$: $f(0) = 2^1 + 1 = 3$, $f(1) = 2^2 + 1 = 5$, $f(2) = 2^4 + 1 = 17$, $f(3) = 2^8 + 1 = 257$, $f(4) = 2^{16} + 1 = 65537$ — all prime (each is not divisible by any prime up to its square root; these five are classically known primes). But $f(5) = 2^{32} + 1 = 4294967297 = 641 \times 6700417$, which Euler discovered (1732) to be composite, refuting Fermat's conjecture. Theme-two paragraph: Fermat verified five cases and conjectured a universal law; the sixth case broke it. Five confirmations — indeed any finite number — cannot establish a "for all" claim over the infinitely many $n$; only a proof could, and here a proof of the conjecture is impossible because the conjecture is false. Euler's discovery is a counterexample — a single $n$ (namely $5$) with $\neg P(n)$ — which is a complete disproof of the universal claim (§5.5).
5.26 Claim: inserting $m + 1$ keys into $m$ buckets forces some bucket to hold $\ge 2$ keys. Proof (by contradiction). Suppose not: every bucket holds at most one key. Then the total number of keys across all $m$ buckets is at most $1 \cdot m = m$. But we inserted $m + 1$ keys, and they all sit in buckets, so the total is $m + 1$. Thus $m + 1 \le m$, i.e. $1 \le 0$ — a contradiction. Hence some bucket holds at least two keys (a collision is unavoidable). $\blacksquare$ (This is the Pigeonhole Principle, Chapter 17, proved here directly by contradiction.)
5.27 † First-choice strategy and the one-word tell: (a) "No integer between $0$ and $1$" — contradiction (tell: no; an absence — assume such an integer and derive $0 < n < 1$ has no integer solution). (b) "$n^3 - n$ divisible by $3$ for all $n$" — proof by cases (tell: all; split on $n \bmod 3$), or induction. (c) "There exists an even prime" — existence, constructive (tell: exists; witness $2$). (d) "Every graph with $\ge 2$ vertices has two vertices of equal degree" — contradiction/pigeonhole (tell: every/two-the-same; assume all degrees distinct and count). [Pigeonhole, Ch. 17.] (e) "$2^n > n^2$ for all $n \ge 5$" — induction (tell: for all $n \ge$ base; the subject of Ch. 6). (f) "$\sin$ is not a polynomial" — contradiction (tell: not; assume it is a polynomial and derive an impossibility, e.g. infinitely many roots / unbounded derivatives).
5.29 † Truth table for $q \land \neg q$:
| $q$ | $\neg q$ | $q \land \neg q$ |
|---|---|---|
| T | F | F |
| F | T | F |
It is false in every row, so it is a contradiction (Chapter 1's category — the opposite of a tautology). Now, an implication $\neg P \rightarrow (q \land \neg q)$ with a false consequent in every case can be true only when its antecedent $\neg P$ is false (a true antecedent with false consequent makes the implication false). So proving $\neg P \rightarrow (q \land \neg q)$ forces $\neg P$ to be false, i.e. $P$ true.
5.31 † Direct proof handles a claim whose conclusion is something you can construct or compute from the hypothesis: "if $n$ is odd then $n^2$ is odd" lets you write $n = 2a+1$ and build $n^2 = 2(2a^2+2a)+1$, exhibiting the odd form. "$\sqrt 2$ is irrational," by contrast, asserts an absence — "there is no pair of integers $p, q$ with $(p/q)^2 = 2$" — and you cannot construct the absence of a thing; there is nothing to point at. So you must assume the thing exists and break it, which is exactly proof by contradiction.
5.33 The self-referential step in the halting argument is feeding the program its own
description: trouble consults halts(trouble, trouble) — a prediction about itself — and then
contradicts that prediction. The $\sqrt 2$ proof needs no self-reference because its contradiction is
purely arithmetic (a fraction in lowest terms turning out to have both parts even); nothing in it refers
to itself. Per the chapter's forward references, contradiction-plus-self-reference recurs in Chapter
10 (diagonalization, proving some infinities are larger than others) and again in Chapter 36 (the
full undecidability of the halting problem).
5.34 † Claim: for every integer $n$, exactly one of $n, n+1$ is even. Proof (by cases). Case $n$ even: $n = 2a$, so $n$ is even and $n + 1 = 2a + 1$ is odd — exactly one even. Case $n$ odd: $n = 2a + 1$ is odd and $n + 1 = 2a + 2 = 2(a+1)$ is even — exactly one even. The cases are exhaustive, so in all cases exactly one of $n, n+1$ is even; hence the product $n(n+1)$ has an even factor and is even. $\blacksquare$ This is the "slicker" §5.3 argument: $n^2 + n = n(n+1)$ is even because one of two consecutive integers is even — the case split is on which of the two.
5.35 The claim is true (the gap is forced). Proof (contradiction/pigeonhole). Partition $\{1, \dots, 2000\}$ into the $1000$ consecutive pairs $\{1,2\}, \{3,4\}, \dots, \{1999, 2000\}$. Suppose, for contradiction, that we chose $1001$ integers with no two differing by exactly $1$. Then no pair contributes both of its members (those differ by $1$), so each of the $1000$ pairs contributes at most one chosen integer — giving at most $1000$ chosen integers total. But we chose $1001$. Contradiction. Hence two chosen integers differ by exactly $1$. $\blacksquare$ (What is guaranteed is a difference of exactly $1$; this is the "$k+1$ items from $k$ pairs" Pigeonhole argument, Chapter 17.)
Chapter 6 — Mathematical Induction
6.1 † Base case: $n=1$. Inductive hypothesis: assume $\sum_{i=1}^{k} i^2 = \frac{k(k+1)(2k+1)}{6}$ for a fixed $k \ge 1$. (The step would then add $(k+1)^2$ and simplify to the formula at $k+1$.)
6.3 † "$5 \mid (n^5 - n)$" means there exists an integer $m$ with $n^5 - n = 5m$.
6.5 † Claim: $\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}$ for $n \ge 1$. Base ($n=1$): LHS $=1$; RHS $= \frac{1\cdot2\cdot3}{6}=1$. ✓ Step: assume the formula at $k$. Then $\sum_{i=1}^{k+1} i^2 = \frac{k(k+1)(2k+1)}{6} + (k+1)^2 = \frac{k(k+1)(2k+1) + 6(k+1)^2}{6} = \frac{(k+1)\big(k(2k+1) + 6(k+1)\big)}{6} = \frac{(k+1)(2k^2+7k+6)}{6} = \frac{(k+1)(k+2)(2k+3)}{6}$, which is the formula at $k+1$. ∎
6.7 † Blanks: base case LHS $= 2^0 = 1$, RHS $= 2^1-1 = 1$. Step: the added term is $2^{k}$; $(2^k-1) + 2^k = 2\cdot 2^k - 1 = 2^{k+1}-1$. ∎
6.9 † Claim: $2^n < n!$ for $n \ge 4$. Base ($n=4$): $16 < 24$. ✓ Step: assume $2^k < k!$ for $k \ge 4$. Then $2^{k+1} = 2\cdot 2^k < 2\cdot k! \le (k+1)\,k! = (k+1)!$ since $k+1 \ge 5 > 2$. ∎ (Base case must be 4: $2^3 = 8 > 6 = 3!$ fails.)
6.11 † Recursive: f(n)=0 if n==0 else n*n+f(n-1). Iterative with invariant "at the loop test,
total == sum_{i=1..count} i^2": initialization count=0,total=0 ✓; maintenance adds
(count+1)**2; termination at count==n gives the sum. Correctness is the invariant proved by
induction on iterations. (See code/exercise-solutions.py.)
6.13 † from dmtoolkit.recurrences import check_identity; check_identity(lambda n: sum(i**3 for i
in range(1,n+1)), lambda n: (n*(n+1)//2)**2, upto=30, start=1) returns None. None means no
counterexample up to 30 — strong evidence but not a proof, because it leaves $n>30$ untested
(Chapter 6, theme four).
6.14 † The inductive step is algebraically valid, but the base case is false: $\sum_{i=1}^{1} i = 1$, while $\frac{1^2+1+1}{2} = \frac{3}{2}$. With a false base case the whole chain is unsupported; the claim is false (the correct formula is $\frac{n(n+1)}{2}$).
6.15 The category of error is an inductive step that silently assumes the cases overlap / that $k$ is large enough — exactly the all-horses failure (the step is invalid at the smallest values, so the chain never gets started even though each individual line "looks" fine).
6.16 † Let $P(n)$ be "$n = n+1$." The step is valid: if $k = k+1$ then adding 1 gives $k+1 = k+2$, i.e. $P(k+1)$. But there is no valid base case ($P(0)$: $0=1$ is false), so the conclusion ("all $n$ satisfy $n=n+1$") is false. This shows a valid step with no base case proves nothing.
6.17 † Regions cut by chords/points on a circle: the counts $1,2,4,8,16$ for small cases suggest $2^{n-1}$, but the next value is $31$, not $32$ — the true formula is a degree-4 polynomial. Testing in code (compute regions for $n=6$) reveals the break. Lesson: a pattern matching the first few cases is a conjecture, not a theorem; induction would fail because the (false) $2^{n-1}$ step cannot be proved.
6.19 † Claim: triangle(n) makes exactly $n+1$ self-calls (counting the initial call). Base
($n=0$): one call, returns at the base case; $0+1=1$. ✓ Step: assume triangle(k) makes $k+1$ calls.
triangle(k+1) is one call plus all the calls of triangle(k), i.e. $1 + (k+1) = (k+1)+1$. ∎
6.21 † Suppose $S = \{ n : P(n) \text{ is false}\}$ is non-empty. By well-ordering it has a least element $n_0$. Since the base case holds, $n_0 \ne \text{start}$, so $n_0 - 1 \ge \text{start}$ and $n_0 - 1 \notin S$, i.e. $P(n_0-1)$ is true. By the inductive step $P(n_0-1)\rightarrow P(n_0)$, so $P(n_0)$ is true — contradicting $n_0 \in S$. Hence $S = \emptyset$. This is proof by contradiction (Chapter 5).
6.23 † Claim: a formula with $n$ distinct variables has $2^n$ truth-table rows. Base ($n=1$): one variable has rows T, F — that's $2^1=2$. ✓ Step: assume $n=k$ gives $2^k$ rows. Adding a $(k+1)$-th variable: each existing row splits into two (the new variable T or F), giving $2 \cdot 2^k = 2^{k+1}$ rows. ∎ (Connects to Chapter 1's truth tables and Chapter 8's subset count.)
6.25 Example: $P(n) :=$ "$\,\prod_{i=1}^{n}(\text{something}) \dots$" — simplest is $P(n) := (n \ne 41)$, or more naturally $P(n) := $ "$n^2 - n + 41$ is prime" (true for $n=1,\dots,40$, false at $n=41$ since $41^2-41+41 = 41^2$). Either shows that verifying many cases never substitutes for a proof (theme four).
Chapter 7 — Strong Induction, Well-Ordering, and Recursive Definitions
7.1 † The step uses $a_{n-1}$ and $a_{n-2}$, reaching two cases back, so two base cases are needed: $n = 0$ and $n = 1$. (The first index at which the step is fully supported is $n = 2$, needing $a_1$ and $a_0$.)
7.3 † (a) Ordinary suffices: the summation step adds the single $(k+1)$ term, using only $P(k)$. (b) Strong is needed: a composite $n = ab$ splits into two factors $a, b$ that may be far below $n$, not at $n-1$, so you must assume all smaller cases. (c) Ordinary suffices: $4^{k+1} - 1 = 4\cdot 4^k - 1 = 4(4^k - 1) + 3$, using only the single predecessor $P(k)$.
7.5 † Basis: $1 \in S$. Recursive clause: if $x \in S$ then $2x \in S$. Exclusion: nothing is in $S$ unless produced by finitely many applications of these clauses. (This generates $1, 2, 4, 8, \dots$, the powers of two.)
7.7 † Blanks: base case — $2$ is a product of one prime. Step — if $k+1 = a\cdot b$ is composite then $2 \le a, b \le k$ (a proper factor of $k+1$ is at least 2 and at most $k$); by the strong hypothesis $a$ and $b$ are each products of primes, so their product $a\cdot b = k+1$ is too. Why strong induction: a factor $a$ of $k+1$ can be any value from $2$ up to $k$ — not necessarily the immediately preceding case $k$ — so we must be allowed to assume $P(a)$ and $P(b)$ for all smaller indices, which is exactly the strong (not ordinary) hypothesis.
7.9 † Claim: every $n \ge 12$ is $4a + 5b$ with $a, b \ge 0$. We check four base cases — $n = 12, 13, 14, 15$ — namely $12 = 4\cdot3$, $13 = 4\cdot2 + 5$, $14 = 4 + 5\cdot2$, $15 = 5\cdot3$. Strong step ($k \ge 15$, prove $P(k+1)$): since $k+1 \ge 16$, $(k+1)-4 = k-3 \ge 12$ is covered by the hypothesis, so $k - 3 = 4a + 5b$; then $k+1 = 4(a+1) + 5b$. ∎ Four base cases because the step reduces by 4: every amount $\ge 16$ bottoms out, via repeated $-4$, at one of $12, 13, 14, 15$. Three base cases would leave the step at $n = 15$ referencing the false $P(11)$.
7.11 † Suppose, for contradiction, $C = \{ n \ge 2 : n \text{ has no prime divisor}\}$ is non-empty. By well-ordering $C$ has a least element $m$. Now $m$ is not prime (a prime divides itself, so a prime would have a prime divisor and not be in $C$). So $m$ is composite: $m = ab$ with $2 \le a < m$. Since $a < m$ and $a \ge 2$, $a \notin C$, so $a$ has a prime divisor $p$. But $p \mid a$ and $a \mid m$ give $p \mid m$ — so $m$ has a prime divisor, contradicting $m \in C$. Hence $C = \emptyset$: every $n \ge 2$ has a prime divisor (and every $n \ge 1$ is $1$ or has one). ∎
7.13 † Claim: $n(T)$ is odd for every full binary tree $T$. Proof (structural induction). Use $\ell(T) = i(T) + 1$ from §7.5 and $n(T) = \ell(T) + i(T)$. Then $n(T) = (i(T)+1) + i(T) = 2\,i(T) + 1$, which is odd. (Direct structural version: basis — a single leaf has $n = 1$, odd; step — combining $T_1, T_2$ under a new root gives $n(T) = n(T_1) + n(T_2) + 1$; if $n(T_1), n(T_2)$ are odd, their sum is even and $+1$ makes it odd.) ∎
7.14 † See code/exercise-solutions.py. make_postage(11) returns None; make_postage(12)
returns (3, 0); make_postage(27) returns (3, 3) (since $27 = 12 + 15 = 4\cdot3 + 5\cdot3$). The
None at $11$ is the largest impossible amount — the Frobenius number $g(4,5) = 4\cdot5 - 4 - 5 = 11$ —
and is exactly the boundary the strong-induction theorem of 7.9 pins down (everything from 12 up works).
7.15 See code/exercise-solutions.py. With tri_step = lambda t, k: t[k-1] + t[k-2] + t[k-3] and
base [0, 0, 1], recursive_seq(tri_step, [0,0,1], 10) returns 81. The tribonacci sequence reaches
back three terms, so it requires three base cases ($T_0, T_1, T_2$). Hand-trace:
$0,0,1,1,2,4,7,13,24,44,81$.
7.16 † See code/exercise-solutions.py. For tree = (("L","L"), "L"): count_leaves returns
$3$, count_nodes returns $5$, so internal $= 5 - 3 = 2$, and indeed $\ell = 3 = 2 + 1 = i + 1$,
confirming the §7.5 identity.
7.17 † The algebra is fine but there is only one base case ($n = 1$), while the step reaches back two (it uses $F_k$ and $F_{k-1}$). At $k = 1$ the step would prove $P(2)$ from $P(1)$ and $P(0)$ — but $P(0)$ was never established (and $0$ lies below the start $n = 1$). The fix is the chapter's proof: check two base cases, $n = 1$ and $n = 2$. (Too few base cases — the chapter's headline pitfall.)
7.19 † The structural-induction proof correctly establishes the property "every element of $S$ is a multiple of 5" (basis: $5$ is; step: $x + 5$ is whenever $x$ is). It does not establish the reverse inclusion — that every multiple of 5 is in $S$. The claim "$S$ contains only multiples of 5" is proved; the claim "$S = \{5, 10, 15, \dots\}$" additionally needs the reverse direction, e.g. an ordinary induction showing $5m \in S$ for all $m \ge 1$ (base $5 \in S$; step: if $5m \in S$ then $5m + 5 = 5(m+1) \in S$ by the recursive clause). Structural induction gives "$S \subseteq$ multiples of 5"; the converse is a separate argument.
7.21 † Statement: every integer $n \ge n_0$ is $3a + 7b$ with $a, b \ge 0$, where the conjectured threshold is $n_0 = 12$ (the impossible amounts are $1, 2, 4, 5, 8, 11$, with largest $11 = 3\cdot7 - 3 - 7$, the Frobenius number $g(3,7)$). Structure: the step reduces by 3 (add one 3-cent token), so it needs three base cases — $12 = 3\cdot4$, $13 = 3\cdot2 + 7$, $14 = 7\cdot2$ — and the step proves $P(k+1)$ from $P(k-2)$, valid once $k+1 \ge 15$ so that $k - 2 \ge 12$.
7.23 † Definition (from 7.22): $B$ is the smallest set with basis $\varepsilon$ (empty string) and
recursive clauses: if $w \in B$ then $(w) \in B$, and if $u, v \in B$ then $uv \in B$. Code: generate
e.g. (), (()), ()(), (()()) and check s.count("(") == s.count(")") for each — all True.
Conjecture: every $w \in B$ has equal numbers of ( and ). Proof (structural induction). Let
$P(w)$ be $L(w) = R(w)$. Basis: $\varepsilon$ has $L = 0 = R$. Step 1, $w = (u)$ with $P(u)$: $L(w) =
L(u) + 1 = R(u) + 1 = R(w)$. Step 2, $w = uv$ with $P(u), P(v)$: $L(w) = L(u) + L(v) = R(u) + R(v) =
R(w)$. ∎ Not established: equal counts does not prove proper nesting/ordering — e.g. )(also has
equal counts but is not balanced. (Equal counts is necessary, not sufficient; cf. §7.4.)
7.25 † Let the strictly decreasing quantity be $n$ itself: each step replaces $n$ by $\lfloor n/2 \rfloor$, and for $n \ge 1$, $\lfloor n/2 \rfloor < n$, so the sequence of values is strictly decreasing while it stays positive. Suppose the process did not terminate; then it produces an infinite strictly decreasing sequence $n_0 > n_1 > n_2 > \cdots$ of non-negative integers. The set $\{n_0, n_1, n_2, \dots\}$ is a non-empty subset of $\mathbb{N}$, so by well-ordering it has a least element $n_j$; but $n_{j+1} < n_j$ is also in the set — contradiction. Hence the process terminates (at $0$). Why not ordinary Collatz: the $3n+1$ branch can increase $n$, so no obvious quantity strictly decreases at every step — there is no known well-ordering measure, which is exactly why Collatz remains open.
7.26 † Re-proving $\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$ by smallest-counterexample: suppose $C = { n \ge 1 : \sum_{i=1}^n i \ne \tfrac{n(n+1)}{2}}$ is non-empty; by well-ordering it has a least element $m$. Since the formula holds at $n = 1$ ($1 = \tfrac{1\cdot2}{2}$), $m \ge 2$, so $m - 1 \ge 1$ and $m - 1 \notin C$: $\sum_{i=1}^{m-1} i = \tfrac{(m-1)m}{2}$. Then $\sum_{i=1}^{m} i = \tfrac{(m-1)m}{2} + m = \tfrac{(m-1)m + 2m}{2} = \tfrac{m(m+1)}{2}$, contradicting $m \in C$. So $C = \emptyset$. Most readers find ordinary induction cleaner here because the claim's natural structure is "add one more term," which matches ordinary induction's single-step hypothesis directly; well-ordering shines instead when "grab the smallest bad case" is the natural move (number theory, termination).
7.27 The strategy is proof by contradiction (Chapter 5). Its opening move: assume the negation — here, assume the set $C$ of counterexamples is non-empty. The contradiction is forced at template step 4: using the smaller true cases (guaranteed by minimality, step 3) to show $P(m)$ holds after all, which contradicts $m \in C$.
7.29 Strong induction as a formula: for a predicate $P$ over integers $\ge n_0$, $$\Big[\,\forall k \ge n_0\ \big( (\forall j,\ n_0 \le j \le k \rightarrow P(j)) \rightarrow P(k+1)\big) \,\Big] \rightarrow \big[\,\forall n \ge n_0\ P(n)\,\big],$$ with base cases folded into the step at the smallest $k$ (or stated separately). The inductive-step antecedent is the bundled universal $\forall j\,(n_0 \le j \le k \rightarrow P(j))$ — "all cases up to $k$" — whereas ordinary induction's antecedent (Exercise 6.22) is the single statement $P(k)$. That bundled antecedent is the only difference between the two principles.
7.30 † Claim: every propositional formula (basis: a variable, with $L = R = 0$; recursive: $(\neg\alpha)$, $(\alpha\land\beta)$, $(\alpha\lor\beta)$) has $L(\varphi) = R(\varphi)$. Proof (structural induction). Let $P(\varphi)$ be $L(\varphi) = R(\varphi)$. Basis: a variable has $0 = 0$. Step, $(\neg\alpha)$ with $P(\alpha)$: $L = L(\alpha) + 1 = R(\alpha) + 1 = R$. Step, $(\alpha\land\beta)$ with $P(\alpha), P(\beta)$ (and identically for $\lor$): $L = L(\alpha) + L(\beta) + 1 = R(\alpha) + R(\beta) + 1 = R$. ∎ This is the same shape as the §7.4 expression proof — one case per clause, recursive cases using the hypotheses on their sub-formulas.
7.31 Cycle of three implications. (i) Ordinary ⟹ strong: given an ordinary-induction-valid setting, to prove $P$ by strong induction define $Q(n) := P(n_0) \land \dots \land P(n)$ and prove $Q$ by ordinary induction (base $Q(n_0) = P(n_0)$; step: $Q(k)$ is the strong hypothesis, which yields $P(k+1)$, hence $Q(k+1)$). Since $Q(n)$ implies $P(n)$, done. (ii) Strong ⟹ well-ordering: let $T \subseteq \mathbb{N}$ have no least element; prove $n \notin T$ for all $n$ by strong induction (if $0, \dots, n-1 \notin T$ but $n \in T$, then $n$ would be least — contradiction). So $T = \emptyset$; contrapositively every non-empty $T$ has a least element. (iii) Well-ordering ⟹ ordinary: given $P(0)$ and $P(k) \rightarrow P(k+1)$, suppose $C = {n : \neg P(n)} \ne \emptyset$; by well-ordering it has a least element $m \ge 1$ (since $P(0)$); then $m - 1 \notin C$ so $P(m-1)$, and the step forces $P(m)$ — contradiction. So $C = \emptyset$. Why a cycle suffices: if A⟹B, B⟹C, and C⟹A, then from any one of the three you can reach the other two by following arrows around the loop, so all three are mutually derivable, i.e. logically equivalent. None is more powerful; they prove exactly the same theorems.
Chapter 8 — Sets
8.1 † (a) $\{-2, -1, 0, 1, 2\}$. (b) Perfect cubes strictly between $0$ and $70$: $1, 8, 27, 64$, so $\{1, 8, 27, 64\}$. (c) $\{ n^2 \mid n \in \mathbb{N},\ n \le 4\} = \{0, 1, 4, 9, 16\}$ (recall $\mathbb{N}$ includes $0$ in this book).
8.3 † (a) $\lvert\{a,b,a,c,b\}\rvert = 3$ (distinct: $a,b,c$). (b) $\lvert\emptyset\rvert = 0$. (c) $\lvert\{\emptyset\}\rvert = 1$ (one element, which is the empty set). (d) $\lvert\{\emptyset,\{\emptyset\}\}\rvert = 2$ (two distinct elements: $\emptyset$ and $\{\emptyset\}$). (e) $\lvert\{1, \{1,2\}, \{1,2,3\}\}\rvert = 3$ (three distinct objects; the inner sets' sizes are irrelevant to the outer count).
8.5 † $\mathcal{P}({a,b,c}) = {\emptyset, {a}, {b}, {c}, {a,b}, {a,c}, {b,c}, {a,b,c}}$. There are $2^3 = 8$ subsets. Those containing $a$: fix $a$ "in," freely choose the other two elements, giving $2^2 = 4$ — namely $\{a\}, \{a,b\}, \{a,c\}, \{a,b,c\}$.
8.6 † Blanks: ($\subseteq$) "$x \in A$ and $x \in \underline{A \cup B}$"; "in particular $x \in \underline{A}$." ($\supseteq$) "the 'or' in $\underline{A \cup B}$ is true"; "we have $x \in \underline{A \cap (A \cup B)}$." Conclusion: "$\underline{A \cap (A \cup B) = A}$." Full proof: ($\subseteq$) if $x \in A \cap (A\cup B)$ then $x \in A$ (and $x \in A \cup B$), so the left side is a subset of $A$. ($\supseteq$) if $x \in A$ then $x \in A \cup B$ too, so $x \in A \cap (A \cup B)$. Both inclusions give $A \cap (A \cup B) = A$. $\blacksquare$
8.7 Claim: $A \setminus B = A \cap \overline{B}$. Proof (iff-chain). For arbitrary $x$: $x \in A \setminus B \iff (x \in A \land x \notin B)$ (definition of difference) $\iff (x \in A \land x \in \overline{B})$ (definition of complement) $\iff x \in A \cap \overline{B}$ (definition of intersection). Same elements, so the sets are equal. $\blacksquare$
8.8 † Claim: $\overline{A \cap B} = \overline{A} \cup \overline{B}$. Proof. For arbitrary $x$: $$ \begin{aligned} x \in \overline{A \cap B} &\iff x \notin A \cap B && \text{(definition of complement)}\\ &\iff \neg(x \in A \land x \in B) && \text{(definition of intersection)}\\ &\iff (x \notin A) \lor (x \notin B) && \text{(De Morgan, Chapter 1)}\\ &\iff (x \in \overline{A}) \lor (x \in \overline{B}) && \text{(definition of complement, twice)}\\ &\iff x \in \overline{A} \cup \overline{B} && \text{(definition of union).} \end{aligned} $$ Same elements for every $x$, so $\overline{A \cap B} = \overline{A} \cup \overline{B}$. $\blacksquare$ The single content step is the De Morgan line; the rest is unfolding/folding definitions.
8.9 Claim: $A \subseteq B \iff A \cup B = B$. ($\Rightarrow$) Assume $A \subseteq B$. Always $B \subseteq A \cup B$. Conversely, if $x \in A \cup B$ then $x \in A$ or $x \in B$; in the first case $x \in B$ since $A \subseteq B$, in the second $x \in B$ directly — so $A \cup B \subseteq B$. Both inclusions give $A \cup B = B$. ($\Leftarrow$) Assume $A \cup B = B$. If $x \in A$ then $x \in A \cup B = B$, so $x \in B$; hence $A \subseteq B$. $\blacksquare$
8.10 † Claim: $A \cup (B \cap C) = (A \cup B) \cap (A \cup C)$. Strategy: two inclusions; the content is the logical distributive law $p \lor (q \land r) \equiv (p \lor q) \land (p \lor r)$. ($\subseteq$) Let $x \in A \cup (B \cap C)$, so $x \in A$ or $x \in B \cap C$. If $x \in A$: then $x \in A \cup B$ and $x \in A \cup C$, so $x \in (A\cup B)\cap(A\cup C)$. If $x \in B \cap C$: then $x \in B$ (so $x \in A \cup B$) and $x \in C$ (so $x \in A \cup C$), hence $x \in (A\cup B)\cap(A\cup C)$. ($\supseteq$) Let $x \in (A\cup B)\cap(A\cup C)$, so $x \in A\cup B$ and $x \in A\cup C$. If $x \in A$, then $x \in A \cup (B\cap C)$. If $x \notin A$, then from $x \in A \cup B$ we get $x \in B$, and from $x \in A \cup C$ we get $x \in C$; so $x \in B \cap C$, hence $x \in A \cup (B \cap C)$. Both inclusions hold. $\blacksquare$
8.11 Claim: $A \subseteq B \Rightarrow \mathcal{P}(A) \subseteq \mathcal{P}(B)$. Proof. Assume $A \subseteq B$. Let $X \in \mathcal{P}(A)$ be arbitrary; by definition of power set, $X \subseteq A$. Then for any $x \in X$ we have $x \in A$ (since $X \subseteq A$) and hence $x \in B$ (since $A \subseteq B$), so $X \subseteq B$, i.e. $X \in \mathcal{P}(B)$. As $X$ was arbitrary, $\mathcal{P}(A) \subseteq \mathcal{P}(B)$. $\blacksquare$ (Subset is transitive: $X \subseteq A \subseteq B$.)
8.12 † Build $A \triangle B$ as $(A \setminus B) \cup (B \setminus A)$ from scratch. With $A =
{1,2,3,4}$, $B = {3,4,5,6}$: $A \setminus B = {1,2}$, $B \setminus A = {5,6}$, union $=
{1,2,5,6}$. Code in code/exercise-solutions.py (symmetric_difference); expected output
[1, 2, 5, 6].
8.13 is_subset(a, b) returns all(x in b for x in a) — a direct transcription of
$\forall x\,(x \in A \rightarrow x \in B)$. On $\{1,2\}, \{1,2,3\}$: every element of $\{1,2\}$ is in
$\{1,2,3\}$, so True. On $\{1,5\}, \{1,2,3\}$: $5 \notin \{1,2,3\}$, so False. (See
code/exercise-solutions.py.)
8.14 † Using cartesian(a, b) from the Project Checkpoint: build it for $A=\{1,2\}$,
$B=\{x,y,z\}$, then compare len(cartesian(A,B)) to len(A)*len(B). The check
cartesian_count_matches(A,B) prints True — that single boolean confirms $\lvert A \times B\rvert =
\lvert A\rvert\cdot\lvert B\rvert = 2\cdot 3 = 6$. (See code/exercise-solutions.py.)
8.15 power_set_size_check(s) builds power_set(s) and returns len(...) == 2 ** len(set(s)). For
$s=\{1,2,3\}$ it returns True ($8 = 2^3$). The inner subsets must be frozenset because a set's
elements must be hashable (8.6), and a mutable set is unhashable — so it cannot be an element of the
outer set. (See code/exercise-solutions.py.)
8.16 † The flaw: $\emptyset$ and $\{\emptyset\}$ are not equal. $\emptyset$ has zero elements ($\lvert\emptyset\rvert = 0$); $\{\emptyset\}$ has one element — namely the empty set —so $\lvert\{\emptyset\}\rvert = 1$. Sets are equal iff they have the same elements (8.2); these have different cardinalities, hence different elements, hence are different sets. "Empty-ish" is not a mathematical criterion. (Think: an empty box vs. a box containing one empty box.)
8.17 Counterexample: $A=\{1,2\}$, $B=\{1\}$, $C=\{2\}$. Then $B \cup C = \{1,2\}$, so $A \setminus (B \cup C) = \emptyset$. But $A \setminus B = {2}$ and $A \setminus C = {1}$, so $(A\setminus B)\cup (A\setminus C) = {1,2} \ne \emptyset$. The *correct* identity is $A \setminus (B \cup C) = (A \setminus B) \cap (A \setminus C)$ — removing "$B$ or $C$" keeps only elements avoiding both, an and. The underlying logical law is De Morgan: $\neg(p \lor q) \equiv \neg p \land \neg q$.
8.18 † Counterexample: let $A = \{1\}$ and $B = \{\{1\}\}$ (a set whose single element is the set $\{1\}$). Then $A \in B$ is true (the element of $B$ is exactly $A$), but $A \subseteq B$ is false: the element $1 \in A$ is not an element of $B$ (the only element of $B$ is $\{1\}$, not $1$). The argument confuses $\in$ (being an element) with $\subseteq$ (every element being an element) — different "levels," exactly the power-set pitfall of 8.2.
8.19 Non-commutativity of $\times$ concerns whether $A \times B = B \times A$; the identity $A \times (B \cup C) = (A \times B) \cup (A \times C)$ keeps $A$ on the left in every term, so commutativity is never invoked — the classmate's objection is irrelevant. Verification on $A=\{1\}$, $B=\{x\}$, $C=\{y\}$: LHS $= \{1\}\times\{x,y\} = \{(1,x),(1,y)\}$; RHS $= {(1,x)}\cup{(1,y)} = {(1,x),(1,y)}$. Equal. (The identity is genuinely true in general — distribution of $\times$ over $\cup$.)
8.20 † (a) "Never fetch twice": $\texttt{visited}$ is a set, and the requirement is that
fetching a URL adds it to visited and we only fetch URLs not already in visited — enforced by
membership test / the distinctness property of sets. (b) "Next URL is discovered-but-not-visited":
the candidate pool is $\texttt{frontier} \setminus \texttt{visited}$ (difference). (c) "Add a new
URL only if never seen": add url to frontier only when $\texttt{url} \notin \texttt{seen}$ where
$\texttt{seen} = \texttt{visited} \cup \texttt{frontier}$ (union to form seen, membership to
gate). The property that makes (a) automatic: a set stores distinct elements, so re-adding a URL is
a no-op (8.1) — a list would accumulate duplicates and require manual checking.
8.21 The universe of all conceivable enrollments is the Cartesian product $S \times C$ (every
(student, course) pair). The actual Enrollment table records only the pairs that really hold, which
is some collection of those pairs — i.e. a subset of $S \times C$. The maximum number of rows is
therefore $\lvert S \times C \rvert = \lvert S\rvert \cdot \lvert C\rvert$ (product rule, 8.4),
attained only if every student takes every course. (Chapter 12 defines a relation as exactly a subset
of a Cartesian product — this is that idea.)
8.22 † Testing $\lvert A \cup B\rvert = \lvert A\rvert + \lvert B\rvert$: disjoint pair $A=\{1,2\}, B=\{3,4\}$ gives $\lvert A\cup B\rvert = 4 = 2+2$ ✓; overlapping pair $A={1,2,3}, B={2,3,4}$ gives $\lvert A\cup B\rvert = \lvert{1,2,3,4}\rvert = 4$ but $\lvert A\rvert+\lvert B\rvert = 3+3 = 6$ ✗ — the formula over-counts the shared elements ${2,3}$. Corrected formula: $$\lvert A \cup B\rvert = \lvert A\rvert + \lvert B\rvert - \lvert A \cap B\rvert.$$ Proof (counting). Partition $A \cup B$ into three disjoint pieces: $A \setminus B$, $A \cap B$, and $B \setminus A$. Then $\lvert A\rvert = \lvert A\setminus B\rvert + \lvert A\cap B\rvert$ and $\lvert B\rvert = \lvert B\setminus A\rvert + \lvert A\cap B\rvert$, while $\lvert A\cup B\rvert = \lvert A\setminus B\rvert + \lvert A\cap B\rvert + \lvert B\setminus A\rvert$. Adding $\lvert A\rvert + \lvert B\rvert$ counts $\lvert A\cap B\rvert$ twice, so subtracting it once yields $\lvert A\cup B\rvert$. $\blacksquare$ Equality holds exactly when $A \cap B = \emptyset$ (disjoint). (This is inclusion– exclusion, Chapter 15.)
8.23 Conjecture: $\lvert\mathcal{P}(A\cup B)\rvert = \lvert\mathcal{P}(A)\rvert\cdot \lvert\mathcal{P}(B)\rvert$. Test (disjoint) $A={1}, B={2}$: $\lvert\mathcal{P}({1,2})\rvert = 4$, and $\lvert\mathcal{P}(A)\rvert\cdot\lvert\mathcal{P}(B)\rvert = 2\cdot 2 = 4$ ✓. Test (overlap) $A=\{1,2\}, B=\{2,3\}$: $A\cup B = \{1,2,3\}$ so LHS $= 2^3 = 8$, but RHS $= 2^2\cdot 2^2 = 16$ ✗. So the conjecture is false in general; smallest counterexample is $A=B=\{1\}$ (LHS $2^1 = 2$, RHS $2 \cdot 2 = 4$). Reason via $\lvert\mathcal{P}(S)\rvert = 2^{\lvert S\rvert}$: LHS $= 2^{\lvert A\cup B\rvert}$, RHS $= 2^{\lvert A\rvert}\cdot 2^{\lvert B\rvert} = 2^{\lvert A\rvert + \lvert B\rvert}$. These match iff $\lvert A \cup B\rvert = \lvert A\rvert + \lvert B\rvert$, i.e. (by 8.22) iff $A$ and $B$ are disjoint.
8.24 † Truth table for $\neg(p \land q)$ vs $\neg p \lor \neg q$:
| $p$ | $q$ | $p\land q$ | $\neg(p\land q)$ | $\neg p \lor \neg q$ |
|---|---|---|---|---|
| T | T | T | F | F |
| T | F | F | T | T |
| F | T | F | T | T |
| F | F | F | T | T |
The two columns agree on all four rows, so $\neg(p \land q) \equiv \neg p \lor \neg q$. With $p := (x\in A)$, $q := (x \in B)$, this proves the set identity $\overline{A \cap B} = \overline{A} \cup \overline{B}$ (De Morgan).
8.25 "$A \subseteq B$" $= \forall x\,(x \in A \rightarrow x \in B)$. Negation: $\neg\forall x\,(x\in A \to x\in B) \equiv \exists x\,\neg(x\in A \to x\in B) \equiv \exists x\,(x\in A \land x\notin B)$. In plain language: *there is an element of $A$ that is not in $B$* — i.e. $A \not\subseteq B$. A witness lives in $A \setminus B$ (an element of $A$ but not of $B$).
8.26 † (a) $\{x \in \mathbb{Z} \mid x \bmod 2 = 0\}$ (even integers). (b) ${x \in \mathbb{Z} \mid x
1 \land \forall d\,(d \mid x \rightarrow (d = 1 \lor d = x))}$, i.e. $x$ has no divisors other than $1$ and itself (the primes). (c) Complement of the multiples of $3$ in $\mathbb{Z}$: $\{x \in \mathbb{Z}\mid 3 \nmid x\} = \{x \in \mathbb{Z}\mid x \bmod 3 \ne 0\}$.
8.27 Claim: $\lvert\mathcal{P}(S)\rvert = 2^{\lvert S\rvert}$ for finite $S$, by induction on
$n=\lvert S\rvert$. Base ($n=0$): $S=\emptyset$ has exactly one subset, $\emptyset$ itself, and
$2^0 = 1$. ✓ Step: assume every $n$-element set has $2^n$ subsets. Let $\lvert S\rvert = n+1$; pick an
element $x \in S$ and write $S' = S \setminus \{x\}$, so $\lvert S'\rvert = n$. Every subset of $S$
either (i) omits $x$ — these are exactly the subsets of $S'$, of which there are $2^n$ by hypothesis —
or (ii) contains $x$ — each is $T \cup \{x\}$ for a unique subset $T \subseteq S'$, again $2^n$ of them.
The two families are disjoint and exhaust $\mathcal{P}(S)$, so $\lvert\mathcal{P}(S)\rvert = 2^n + 2^n =
2\cdot 2^n = 2^{n+1}$. ∎ (This is the "in or out" doubling of 8.2, and it matches the Toolkit
power_set algorithm.)
8.28 † Loop invariant. Process the elements of s in some fixed order $x_1, x_2, \dots, x_n$.
The invariant is: after processing the first $j$ elements, subsets equals $\mathcal{P}({x_1, \dots,
x_j})$ — exactly the $2^j$ subsets of the first $j$ elements.* **Initialization** ($j=0$): before the
loop subsets = {frozenset()} $= \mathcal{P}(\emptyset)$, which has $2^0 = 1$ element. ✓
Maintenance: assume the invariant after $j$ elements, so subsets $= \mathcal{P}(\{x_1,\dots,x_j\})$
with $2^j$ elements. Processing $x_{j+1}$ computes {sub | {x_{j+1}} for sub in subsets} — each existing
subset with $x_{j+1}$ added — and unions it back in, so subsets becomes every subset of
$\{x_1,\dots,x_j\}$ both without and with $x_{j+1}$. Those are precisely the subsets of
$\{x_1,\dots,x_{j+1}\}$ (each either omits or contains $x_{j+1}$, disjoint families), giving
$\mathcal{P}(\{x_1,\dots,x_{j+1}\})$ with $2 \cdot 2^j = 2^{j+1}$ elements. ✓ Termination: after all
$n$ elements, subsets $= \mathcal{P}(s)$ with $2^n$ elements. So the function returns $2^{\lvert
s\rvert}$ subsets — the algorithm is* the induction of Exercise 8.27, with the loop variable playing
the role of the induction variable.
8.29 Russell's paradox. Suppose for contradiction that $R = \{x \mid x \notin x\}$ exists. Ask whether $R \in R$. By the defining property of $R$, $R \in R$ holds iff $R$ satisfies the membership condition, i.e. iff $R \notin R$. So $R \in R \iff R \notin R$ — a statement equivalent to its own negation, which is a contradiction. Hence no such set $R$ exists; "the set of all sets that do not contain themselves" is incoherent, and unrestricted set-builder notation must be restricted (this is why we usually write $\{x \in U \mid P(x)\}$ over a known universe $U$). The technique is proof by contradiction (Chapter 5): assume the object exists, derive an impossibility.
Chapter 9 — Functions
9.1 † (a) Function. (b) Not a function — fails totality: input $4$ has no output. (c) Not a function — fails single-valuedness: input $1$ has two outputs ($2$ and $3$). (d) Function (two inputs sharing the output $0$ is fine; that bears on injectivity, not on being a function).
9.3 † $f(x) = 3x - 2$. Image $f(\{0,1,2\}) = \{f(0), f(1), f(2)\} = \{-2, 1, 4\}$. Preimage $f^{-1}(\{1,4,5\}) = \{x \in \mathbb{Z} \mid 3x-2 \in \{1,4,5\}\}$: solve $3x-2 = 1 \Rightarrow x = 1$; $3x-2 = 4 \Rightarrow x = 2$; $3x-2 = 5 \Rightarrow x = 7/3 \notin \mathbb{Z}$. So $f^{-1}({1,4,5}) = {1, 2}$. (Note the target $5$ contributes nothing — the preimage is still a well-defined set even when some targets have no integer preimage.)
9.5 † $f(x) = x \bmod 5$ on $\{0,\dots,14\}$. $f^{-1}({2}) = {x \mid x \bmod 5 = 2} = {2, 7, 12}$. It has **3** elements: the domain ${0,\dots,14}$ holds exactly $15/5 = 3$ full residue classes, so each remainder is hit exactly three times. (This many-to-one structure is precisely why $x \bmod n$ is surjective-but-not-injective, §9.5, and the seed of hash collisions, §9.6.)
9.7 † Filled scaffold: - Assume $f(a_1) = f(a_2)$. By definition of $f$, this means $7a_1 - 4 = 7a_2 - 4$. - Adding $4$ to both sides gives $7a_1 = 7a_2$. - Dividing both sides by $7$ gives $a_1 = a_2$, which is what injectivity requires. $\blacksquare$
9.9 † Injective: assume $f(a_1) = f(a_2)$, i.e. $2a_1 + 1 = 2a_2 + 1$; subtract $1$ and divide by $2$ to get $a_1 = a_2$. So $f$ is injective. Not surjective: the even number $2 \in \mathbb{Z}$ has no preimage, since $2n + 1$ is odd for every integer $n$, so $2n + 1 = 2$ has no integer solution. One such counterexample (Chapter 2) suffices to disprove surjectivity. Hence $f$ is injective but not surjective (and therefore not bijective).
9.11 † Claim: if $f\colon A \to B$ and $g\colon B \to C$ are surjective, so is $g\circ f$. Proof (surjectivity template — start from a target in $C$). Let $c \in C$ be arbitrary. Since $g$ is surjective, there is some $b \in B$ with $g(b) = c$. Since $f$ is surjective, there is some $a \in A$ with $f(a) = b$. Then $(g\circ f)(a) = g(f(a)) = g(b) = c$. So every $c \in C$ has a preimage under $g\circ f$, and $g\circ f$ is surjective. $\blacksquare$ (We "peeled one layer at a time," using $g$'s surjectivity to step from $C$ back to $B$, then $f$'s to step from $B$ back to $A$.)
9.13 † See code/exercise-solutions.py. image(f, S) = {f[a] for a in S}. On
f = {0:0, 1:1, 2:4, 3:9}, S = {1,2,3}: outputs $\{f(1),f(2),f(3)\} = \{1,4,9\}$. Expected output:
[1, 4, 9] (sorted).
9.15 † See code/exercise-solutions.py. is_bijective(f, codomain) returns
is_injective(f) and set(f.values()) == set(codomain), where is_injective(f) is len(set(f.values()))
== len(f). For p = {0:2, 1:0, 2:3, 3:1} onto range(4): values $\{0,1,2,3\}$ are $4$ distinct outputs
for $4$ inputs (injective) and equal the codomain (surjective), so True. The guarded inverse swaps
keys/values only because p is bijective, giving {2:0, 0:1, 3:2, 1:3}. Expected output: True then
{2: 0, 0: 1, 3: 2, 1: 3}.
9.17 † The flaw is the step "taking square roots of both sides gives $a_1 = a_2$." From $a_1^2 = a_2^2$ it follows only that $\lvert a_1 \rvert = \lvert a_2 \rvert$, i.e. $a_1 = \pm a_2$, not $a_1 = a_2$. The claim is false: $f(-1) = 1 = f(1)$ but $-1 \ne 1$, a collision. (On the restricted domain $[0,\infty)$ the square-root step would be valid, which is why $x^2$ is injective there — §9.4.)
9.19 † The bug hides in the word "the" preimage. Surjectivity guarantees at least one preimage for each $b$, not a unique one, so $f^{-1}(b) = a$ does not define a single value — it leaves a choice whenever $b$ has several preimages, and a function may not make arbitrary choices. The missing property is injectivity, which provides uniqueness (§9.4: surjectivity → existence, injectivity → uniqueness). Concretely, $f\colon \{0,1\} \to \{0\}$ with $f(0) = f(1) = 0$ is surjective, but "$f^{-1}(0) = a$ where $f(a)=0$" is ambiguous ($a$ could be $0$ or $1$), so no inverse function exists.
9.21 † Model the shortener as $f\colon \text{LongURLs} \to \text{ShortCodes}$, $f(\text{url}) = \text{code}$. Domain: the set of long URLs being shortened; codomain: the set of short codes. For "turn the code back into the original URL" to be possible at all, $f$ must be injective (so the code determines the URL uniquely) — equivalently, $f$ must be a bijection onto the set of codes it actually issues, so that decoding is the inverse $f^{-1}$ (§9.4: invertible iff bijective). If two different long URLs ever receive the same short code, $f$ is not injective: that code's preimage $f^{-1}(\{\text{code}\})$ has two URLs, and a visitor following the code cannot be sent to a unique destination — the lookup is no longer a function of the code. (In practice shorteners store the mapping and ensure injectivity by never reusing a code.)
9.23 † "Lossless" means the compressor is injective on the files it accepts: distinct inputs
produce distinct compressed outputs. Decompression is exactly the inverse function, and by the §9.4
invertibility theorem an inverse exists only if the map is injective (a bijection onto its range) — if two
files compressed to the same blob, decompression could not recover both, so information would be lost. The
type signature alone Callable[[bytes], bytes] guarantees only the codomain (output is bytes); it
says nothing about the range (which byte strings actually occur) and nothing about injectivity, so it
cannot certify losslessness (§9.2: codomain ≠ range, and the type checker tracks only the codomain).
9.25 † Setup/test. For each small $n$, enumerate all $n^n$ functions ${0,\dots,n-1} \to {0,\dots,n-1}$ and count the bijections. Code outline (hand-traced, not run):
from itertools import product
def count_bijections(n):
dom = range(n)
total = 0
for outputs in product(dom, repeat=n): # all n^n functions as value-tuples
if len(set(outputs)) == n: # injective <=> n distinct values
total += 1 # (finite thm: then also bijective)
return total
print([count_bijections(n) for n in range(5)])
# Expected output:
# [1, 1, 2, 6, 24]
Conjecture from the table $1, 1, 2, 6, 24$: the number of bijections of an $n$-element set is $n!$. Proof (direct count). A bijection $f\colon A \to A$ is determined by choosing the images of $a_1, a_2, \dots, a_n$ subject to all images being distinct (injectivity, which for equal finite sizes equals bijectivity by §9.3's finite theorem). There are $n$ choices for $f(a_1)$; given that, $n-1$ remaining values for $f(a_2)$; then $n-2$ for $f(a_3)$; …; and $1$ for $f(a_n)$. By the multiplication principle the total is $n(n-1)(n-2)\cdots 1 = n!$. $\blacksquare$ (For $n=0$ the empty function is the one bijection, $0! = 1$.) Such bijections are permutations; counting them systematically is the subject of Chapter 16.
9.27 † $\lvert A \rvert = 3$, $\lvert B \rvert = 2$. Total functions $A \to B$: each of the $3$ inputs independently chooses one of $2$ outputs, so $2^3 = 8$ functions (this is the §9.1 "exactly one output per input" counted with Chapter 8's product rule; $\lvert A\times B\rvert = 6$ is a different count, the number of pairs). Surjective functions: total minus those missing at least one codomain value. Functions hitting only one value: $2$ (all-to-$b_1$, all-to-$b_2$). So surjections $= 8 - 2 = 6$. Injective? No function $A \to B$ can be injective: $3$ distinct inputs would need $3$ distinct outputs, but the codomain has only $2$ (the finite count behind §9.3 / pigeonhole).
9.29 † Claim: for a bijection $f$ on $\{0,\dots,n\}$, the $k$-fold composition $f^{(k)}$ is a bijection for all $k \ge 0$. Let $P(k)$ be "$f^{(k)}$ is a bijection." Base case ($k=0$): $f^{(0)} = \operatorname{id}$, which is a bijection (it is injective and surjective — every element is its own unique preimage). ✓ Inductive step: assume $P(k)$, i.e. $f^{(k)}$ is a bijection. Then $f^{(k+1)} = f \circ f^{(k)}$ is the composition of two bijections ($f$ by hypothesis of the problem, $f^{(k)}$ by the inductive hypothesis), and by the in-chapter theorem that a composition of bijections is a bijection (§9.4), $f^{(k+1)}$ is a bijection. ✓ By induction, $P(k)$ holds for all $k \ge 0$. $\blacksquare$ (This is the Chapter 6 recursion–induction correspondence: the recursive structure $f^{(k+1)} = f\circ f^{(k)}$ is the inductive step.)
9.31 † Take $A = B = \mathbb{N}$ (including $0$). (a) Injective, not surjective: $f(n) = n + 1$. Injective ($a+1 = b+1 \Rightarrow a = b$); not surjective ($0$ has no preimage, since the smallest output is $f(0) = 1$). (b) Surjective, not injective: $g(n) = \max(n - 1, 0)$ (i.e. $g(0)=0$ and $g(n) = n-1$ for $n \ge 1$). Surjective (any $m$ is $g(m+1)$); not injective ($g(0) = 0 = g(1)$, a collision). These near-misses are impossible on finite sets of equal size (§9.3's finite theorem forces injective ⇔ surjective there), so they are exactly the phenomenon that breaks when sets become infinite — and Chapter 10 turns that break into the definition of "different sizes of infinity."
Chapter 10 — Cardinality and Infinity
Conventions: $\mathbb{N} = \{0, 1, 2, \dots\}$ includes 0. To prove $\lvert A\rvert = \lvert B\rvert$ build a bijection (injective + surjective); to prove a set uncountable, diagonalize.
10.1 † (a) To prove $\lvert A\rvert = \lvert B\rvert$ you must produce a bijection $f\colon A \to B$ — a function that is both injective and surjective. (b) To prove $\lvert A\rvert \le \lvert B\rvert$ you produce an **injection** $A \to B$. The bijection of (a) additionally has the property of being surjective (onto); an injection need not be.
10.3 † (a) finite ($10^{100}$ elements); (b) countably infinite (bijection $n \mapsto 7n$ with $\mathbb{N}$); (c) uncountable ($\mathbb{R}$, §10.3); (d) countably infinite (finite binary strings list by length then lexicographically, §10.4 Step 1); (e) uncountable ($\mathcal{P}(\mathbb{N})$ corresponds to functions $\mathbb{N}\to\{0,1\}$, §10.4); (f) countably infinite (a subset of $\mathbb{Q}$, which is countable; infinite since $1/2,1/3,1/4,\dots \in [0,1]$).
10.5 † Diagonal digits are $d_{00}=3,\ d_{11}=7,\ d_{22}=5,\ d_{33}=5$. The rule outputs 5 unless the diagonal digit is 5, then 4: so $3\to5,\ 7\to5,\ 5\to4,\ 5\to4$. First four digits of $x$: $5,5,4,4$ (i.e. $x = 0.5544\dots$). Each differs from $r_k$'s $k$-th digit, so $x$ is on no listed row.
10.7 † Blanks, in order: $f(n) = 2n + 1$; "$2m+1 = 2n+1 \Rightarrow 2m = 2n \Rightarrow m = n$"; the input is $k$ (since $f(k) = 2k+1 = b$); conclusion $\lvert O\rvert = \lvert \mathbb{N}\rvert = \aleph_0$. So $O$ is countably infinite. *Full check:* $f(0)=1, f(1)=3, f(2)=5,\dots$ lists every odd number once; injective and surjective onto $O$ as shown.
10.9 † Let $A = \{a_0, a_1, \dots\}$ and $B = \{b_0, b_1, \dots\}$ be countably infinite, listed by bijections with $\mathbb{N}$. Build a list of $A \cup B$ by interleaving: $a_0, b_0, a_1, b_1, a_2, b_2, \dots$ Formally define $h\colon \mathbb{N} \to A \cup B$ by $h(2k) = a_k$ and $h(2k+1) = b_k$. Every element of $A$ appears (at an even index) and every element of $B$ appears (at an odd index), so $h$ is onto $A \cup B$; each element is at a finite position. If $A$ and $B$ overlap, the interleaving still lists every element of the union — simply delete the second (and later) occurrence of any repeated element when scanning the list; deleting duplicates from a countable list leaves a countable list (Exercise 10.11). Hence $A \cup B$ is countable; it is infinite since it contains the infinite set $A$, so it is countably infinite. $\blacksquare$
10.11 † Let $S$ be countable with listing $s_0, s_1, s_2, \dots$ and let $T \subseteq S$. Scan the listing left to right and keep only the entries that belong to $T$, in the order encountered: this produces a listing of $T$ (every element of $T$ is some $s_i$, hence appears; no element appears twice because the original had no repeats). If $T$ is finite the listing is finite; otherwise it is an infinite listing, i.e. a bijection $\mathbb{N} \to T$. Either way $T$ is countable. $\blacksquare$ The primes are a subset of the countable set $\mathbb{N}$, hence countable.
10.13 † Generator sweeping anti-diagonals via cantor_unpair, skipping values already seen (full
code in code/exercise-solutions.py):
from fractions import Fraction
def rationals_positive():
seen = set(); n = 0
while True:
i, j = cantor_unpair(n)
p, q = i + 1, j + 1
v = Fraction(p, q)
if v not in seen:
seen.add(v); yield (v.numerator, v.denominator)
n += 1
# Expected output: [(1,1),(2,1),(1,2),(3,1),(1,3),(4,1),(3,2),(2,3)]
Trace: cells $(0,0),(1,0),(0,1),(2,0),(1,1),(0,2),(3,0),(2,1),(1,2)$ map to fractions $1, 2, 1/2, 3, [1\text{ skip}], 1/3, 4, 3/2, 2/3$. The skip is $(1,1)\to 2/2 = 1$, already seen. This is "$\mathbb{Q}^+$ is countable" as a running enumerator.
10.15 † diagonal_escape(rows) returns [4 if row[k]==5 else 5 for k,row in enumerate(rows)]. On
[[1,1,1],[2,2,2],[3,3,3]] the diagonal entries are $1, 2, 3$ (none equal 5), so the rule outputs
[5, 5, 5], and all(esc[k] != table[k][k] for k in range(3)) is True (since $5 \ne 1, 5 \ne 2, 5 \ne
3$).
# Expected output:
# [5, 5, 5] True
Whatever the table — even one listing every real — the returned row differs from row $k$ at position $k$, hence equals no row: the computational kernel of §10.3 (and of Chapter 36).
10.17 † The flaw is the sentence "a limit of elements drawn from a countable list is itself reachable from that list." This is false, and it is exactly the gap Cantor's diagonal number $x$ exploits. Density of $\mathbb{Q}$ gives rationals arbitrarily close to every real, but "arbitrarily close" is not "equal," and a countable set can be dense in an uncountable one (§10.3's "🐛 Find the Error"). The diagonal $x$ is the limit of no finite portion of any rational list and equals no entry, so the reals are genuinely uncountable. $\mathbb{Q}$ countable and $\mathbb{R}$ uncountable are both true and not in conflict.
10.19 † The fallacy is conflating "$f(n)$ is finite for each $n$" with "$f$ has a finite description." Knowing each individual value $f(n)$ is one bit does not mean the whole function is specifiable by a finite string — specifying an arbitrary $f\colon \mathbb{N}\to\{0,1\}$ requires choosing infinitely many independent bits, and almost all such infinite bit-sequences have no finite description. The §10.4 diagonal bit-flip $g(n) = 1 - f_n(n)$ proves the set of these functions is uncountable, so they cannot all be coded by finite strings (which are countable). Concretely: finite strings are countable; the functions are uncountable; an uncountable set cannot inject into a countable one.
10.21 † Model: let $\mathrm{In} = \{f \mid f\colon \mathbb{N}\to\{0,1\}\}$ be the set of all infinite bit-streams and $\mathrm{Out} = \{\text{finite files}\}$ the set of finite output files. "Losslessly compress every stream to a finite file with perfect reconstruction" asserts an injection $c\colon \mathrm{In} \to \mathrm{Out}$ (distinct streams must get distinct files, else the decompressor cannot tell them apart). By §10.4, $\mathrm{In}$ is uncountable and $\mathrm{Out}$ (finite strings) is countable. No uncountable set injects into a countable one (an injection would make $\mathrm{In}$ equinumerous with a subset of the countable $\mathrm{Out}$, hence countable — contradiction). Therefore no such compressor exists. (This is the counting argument behind "no lossless compressor shrinks every input.") $\blacksquare$
10.23 † Conjecture: $T$ (reals in $[0,1)$ using only digits 0,1) is uncountable. Code lists terminating ones in diagonal order — $0.0, 0.1, 0.01, 0.11, 0.001, \dots$ — which only shows the terminating ones are countable; that is not all of $T$. Proof (diagonalize within $T$): suppose $T$ were countable, $t_0, t_1, t_2, \dots$, with $t_n = 0.d_{n0}d_{n1}d_{n2}\dots$ and every $d_{nk} \in {0,1}$. Build $x = 0.x_0 x_1 x_2\dots$ by the **bit-flip** $x_k = 1 - d_{kk}$ (so $x_k \in {0,1}$, keeping $x \in T$). Then $x_k \ne d_{kk}$ for all $k$, so $x$ differs from every $t_n$ and is on no list, yet $x \in T$. Contradiction; $T$ is uncountable. (The digit rule that "stays inside $\{0,1\}$" is the flip $1 - d$.) Note $T$ is essentially the set of infinite bit-strings, which §10.4 already shows is uncountable. $\blacksquare$
10.25 The total programs are a subset of all programs; all programs form a countable set (§10.4, Step 1: finite strings over a finite alphabet), and a subset of a countable set is countable (Exercise 10.11). So the total programs are countable. This does not name an uncomputable problem because the counting argument only proves uncomputable problems exist (there are uncountably many decision problems but only countably many programs, so some problem has no program) — it is non-constructive. Chapter 36 supplies the extra ingredient: a specific, self-referential construction (the diagonal flip applied to programs run on their own source — "does program $p$ halt on input $p$?"), turning the existence proof into a named, concrete uncomputable problem (the halting problem).
10.27 The indicator function (a.k.a. characteristic function) gives the bijection. For a subset $S \subseteq \mathbb{N}$, define $\chi_S\colon \mathbb{N}\to\{0,1\}$ by $\chi_S(n) = 1$ if $n \in S$ and $0$ otherwise. Conversely, any $f\colon \mathbb{N}\to\{0,1\}$ defines the subset $S_f = \{n : f(n) = 1\}$. The maps $S \mapsto \chi_S$ and $f \mapsto S_f$ are mutual inverses, so $\mathcal{P}(\mathbb{N})$ and the set of functions $\mathbb{N}\to\{0,1\}$ are equinumerous. Hence $\lvert \mathcal{P}(\mathbb{N})\rvert > \lvert \mathbb{N}\rvert$ (§10.4) says: there are uncountably many decision problems (each "yes"-set is a subset of $\mathbb{N}$), strictly more than the countably many programs — so almost every decision problem is uncomputable.
10.29 Claim: for every $n \ge 0$, the set $A^n$ of length-$n$ strings over an alphabet $A$ with $\lvert A\rvert = a \ge 1$ has exactly $a^n$ elements. Proof by induction on $n$. Base ($n=0$): there is exactly one length-0 string, the empty string $\varepsilon$, and $a^0 = 1$. ✓ Step: assume $\lvert A^k\rvert = a^k$. Every length-$(k+1)$ string is a length-$k$ string followed by one more symbol; there are $a^k$ choices for the prefix and $a$ choices for the last symbol, giving $a^k \cdot a = a^{k+1}$ by the product rule. So $\lvert A^{k+1}\rvert = a^{k+1}$. $\blacksquare$ This feeds §10.4: the set of all finite strings is $\bigcup_{n=0}^\infty A^n$, a countable union (over $n \in \mathbb{N}$) of finite sets (each $A^n$ of size $a^n$), hence countable. (Same counting as the $2^n$ truth-table rows in Exercise 6.23, with $a = 2$.)
10.33 Two of the four §10.6 consequences, each as (a) countable set / (b) uncountable set / (c) impossibility: - Most reals are unnameable. (a) Finite descriptions (formulas, algorithms, programs) are finite strings, hence countable. (b) The reals $\mathbb{R}$ are uncountable (§10.3). (c) A countable set of names cannot tag an uncountable set, so almost every real has no finite description — it is undescribable. - No language is universal over all functions. (a) The programs of any fixed language, computing functions $\mathbb{N}\to\mathbb{N}$, are countable (finite strings, §10.4 Step 1). (b) The functions $\mathbb{N}\to\mathbb{N}$ are uncountable (diagonalize as in §10.4). (c) Countably many programs cannot compute uncountably many functions, so no language expresses them all. - (Floating point and limits of computation are analogous: finite/countable resource vs. uncountable demand.) The single shared pattern: wherever a countable supply of tools must cover an uncountable set of targets, the strict inequality $\aleph_0 < \mathfrak{c}$ guarantees most targets have no tool — the impossibility is a counting fact, not an engineering shortfall.
10.31 Apply Cantor's theorem (Exercise 10.30) repeatedly. Start with $\mathbb{N}$ and iterate the power-set operation: define $S_0 = \mathbb{N}$ and $S_{k+1} = \mathcal{P}(S_k)$. Cantor's theorem says $\lvert S_k\rvert < \lvert \mathcal{P}(S_k)\rvert = \lvert S_{k+1}\rvert$ for every $k$, so $$\lvert \mathbb{N}\rvert < \lvert \mathcal{P}(\mathbb{N})\rvert < \lvert \mathcal{P}(\mathcal{P}(\mathbb{N}))\rvert < \cdots,$$ a strictly increasing tower. Each "$<$" is strict because Cantor's theorem rules out any bijection $S_k \to \mathcal{P}(S_k)$ (no surjection exists, by the diagonal set argument). The tower never terminates because the power-set operation can always be applied again, producing a strictly larger cardinality — so there is no largest infinity. The first two rungs are named: $\lvert \mathbb{N}\rvert = \aleph_0$ and $\lvert \mathcal{P}(\mathbb{N})\rvert = \mathfrak{c} = 2^{\aleph_0}$ (the continuum, the cardinality of $\mathbb{R}$).
Chapter 11 — Sequences and Summations
11.1 † Closed form: $a_n = 3 + 4n$ (indexed from $n=0$; check $a_0=3, a_1=7, a_2=11$). Recurrence: $a_0 = 3$, $a_n = a_{n-1} + 4$ for $n \ge 1$.
11.3 † $\sum_{i=2}^{5}(2i-1) = (2\cdot2-1)+(2\cdot3-1)+(2\cdot4-1)+(2\cdot5-1) = 3+5+7+9 = 24$.
11.5 † Empty sum $\sum_{i=3}^{2} a_i = 0$; empty product $\prod_{i=3}^{2} a_i = 1$. Each is the identity of its operation: adding nothing leaves an accumulator at $0$, multiplying nothing leaves it at $1$, so formulas stay valid at their boundaries (e.g. $0! = 1$).
11.7 † By linearity, $\sum_{i=1}^{n}(6i^2 - 4i + 5) = 6\sum i^2 - 4\sum i + 5\sum 1 = 6\cdot\frac{n(n+1)(2n+1)}{6} - 4\cdot\frac{n(n+1)}{2} + 5n = n(n+1)(2n+1) - 2n(n+1) + 5n$. Expanding: $n(n+1)(2n+1) = 2n^3 + 3n^2 + n$ and $2n(n+1) = 2n^2 + 2n$, so the total is $2n^3 + 3n^2 + n - 2n^2 - 2n + 5n = 2n^3 + n^2 + 4n$.
11.9 † Let $j = i - 3$ (so $i = j+3$). As $i$ runs $3 \to n$, $j$ runs $0 \to n-3$: $\sum_{i=3}^{n} a_i = \sum_{j=0}^{n-3} a_{j+3}$.
11.11 † Telescoping with $t_i = -\frac{1}{i+1}$: $\sum_{i=1}^{n}\left(\frac1i - \frac{1}{i+1}\right) = \left(\frac11-\frac12\right)+\left(\frac12-\frac13\right)+\cdots+\left(\frac1n-\frac1{n+1}\right) = 1 - \frac{1}{n+1} = \frac{n}{n+1}$. As $n \to \infty$, this $\to 1$.
11.13 † Blanks, in order: the $i$-th column sums to $2a + (n-1)d$; there are $n$ columns; $2S = n\big(2a + (n-1)d\big)$; $S = \dfrac{n\big(2a + (n-1)d\big)}{2}$. (This is the forwards-and-backwards derivation of the arithmetic series.)
11.15 † Verify the split: $\frac12\left(\frac{1}{2i-1} - \frac{1}{2i+1}\right) = \frac12\cdot\frac{(2i+1)-(2i-1)}{(2i-1)(2i+1)} = \frac12\cdot\frac{2}{(2i-1)(2i+1)} = \frac{1}{(2i-1)(2i+1)}$. ✓ Then the sum telescopes (pull the constant $\frac12$ out by linearity): $\sum_{i=1}^{n}\frac{1}{(2i-1)(2i+1)} = \frac12\sum_{i=1}^{n}\left(\frac{1}{2i-1}-\frac{1}{2i+1}\right) = \frac12\left(\frac11 - \frac{1}{2n+1}\right) = \frac12\cdot\frac{2n}{2n+1} = \frac{n}{2n+1}$.
11.17 † Closed form triangular(n) returns n*(n+1)//2. Loop version with invariant "at the loop
test, total == sum_{i=1..count} i": initialize total=0, count=0 (✓, empty sum is 0); each pass does
count += 1; total += count (maintains the invariant); terminate at count == n, giving
$\sum_{i=1}^{n} i$. Both return $5050$ at $n=100$. (See code/exercise-solutions.py.)
11.19 † Recursive: factorial(n) = 1 if n == 0 else n * factorial(n - 1), matching $0! = 1$ and
$n! = n\cdot(n-1)!$. Printing $[0!,\dots,6!]$ gives [1, 1, 2, 6, 24, 120, 720]. (See
code/exercise-solutions.py.)
11.21 † Flaw: the constant $-3$ is added inside the summand, so it is added once per term and sums to $-3n$, not $-3$. Linearity factors a multiplied constant ($5$) but a summed constant must be counted $n$ times. Correct: $\sum_{i=1}^{n}(5i-3) = 5\sum i - \sum 3 = 5\cdot\frac{n(n+1)}{2} - 3n = \frac{5n(n+1)}{2} - 3n = \frac{5n^2 + 5n - 6n}{2} = \frac{5n^2 - n}{2}$.
11.23 † The equation $\sum_{i=0}^{n} i = \frac{n(n+1)}{2}$ is true — but the student's reasoning is only half right and the indices are subtly off. Adding the $i=0$ term ($=0$) indeed changes nothing, so $\sum_{i=0}^{n} i = \sum_{i=1}^{n} i = \frac{n(n+1)}{2}$. The error is conflating "start at $0$" with "stop at $n-1$": a genuine index shift of $\sum_{i=1}^{n} i$ to start at $0$ is $\sum_{j=0}^{n-1}(j+1)$ (re-base AND adjust the summand), which is not what the student wrote. They got a true equation by luck (the $i=0$ term is zero), not by a valid shift. Moral: adding/removing a zero term is legitimate; renaming the index requires compensating in the summand and the bounds together.
11.25 † Model. People who see the post on day $k$: $s_k = 3^{k+1}$ (day 0: $3 = 3^1$; day 1: $9 = 3^2$; …), a geometric sequence with first term $3$, ratio $3$. Total through day $n$: $\sum_{k=0}^{n} 3^{k+1} = 3\sum_{k=0}^{n} 3^k = 3\cdot\frac{3^{n+1}-1}{3-1} = \frac{3(3^{n+1}-1)}{2}$. (Equivalently $\sum_{k=1}^{n+1} 3^k = \frac{3^{n+2}-3}{2}$.) Through day $5$ ($n=5$): $\frac{3(3^6-1)}{2} = \frac{3(729-1)}{2} = \frac{3\cdot728}{2} = \frac{2184}{2} = 1092$ people.
11.27 † Test: summation gives $S(1),\dots,S(8) = 1, 4, 9, 16, 25, 36, 49, 64$ — the perfect
squares. Conjecture: $S(n) = n^2$. Proof by telescoping: write $2i-1 = i^2 - (i-1)^2$ (check:
$i^2 - (i^2 - 2i + 1) = 2i - 1$ ✓). Then with $t_i = i^2$,
$\sum_{i=1}^{n}(2i-1) = \sum_{i=1}^{n}\big(i^2 - (i-1)^2\big) = t_n - t_0 = n^2 - 0 = n^2$. This matches
the Chapter 6 result "the sum of the first $n$ odd numbers is $n^2$," now derived rather than verified.
11.29 † Test: with Fraction, $H_1 = 1 \ge 1+0$; $H_2 = \frac32 \ge 1+\frac12 = \frac32$ (equal);
$H_4 = \frac{25}{12} \approx 2.083 \ge 2$; $H_8 = \frac{761}{280} \approx 2.718 \ge 2.5$; $H_{16}
\approx 3.380 \ge 3$. All hold. *Proof:* group $H_{2^k}$ as $1 + \frac12 + \big(\frac13+\frac14\big) +
\big(\frac15+\cdots+\frac18\big) + \cdots + \big(\frac{1}{2^{k-1}+1}+\cdots+\frac{1}{2^k}\big)$. The
$j$-th block (for $j \ge 1$) has $2^{j-1}$ terms, each $\ge \frac{1}{2^j}$, so the block is $\ge
2^{j-1}\cdot\frac{1}{2^j} = \frac12$. There are $k$ such blocks plus the leading $1$, so $H_{2^k} \ge 1 +
k\cdot\frac12 = 1 + \frac{k}{2}$. ∎ Since $H_{2^k}$ grows at least linearly in $k = \log_2(2^k)$, the
harmonic number grows without bound, and at rate $\Theta(\log n)$ — matching $H_n \approx \ln n$.
11.30 † Domain $\mathbb{N} = \{0,1,2,\dots\}$; codomain the positive integers $\{1,2,3,\dots\}$. Injective? No on all of $\mathbb{N}$, because $0! = 1! = 1$ (two inputs, same output); it is injective when restricted to $n \ge 1$, where $n!$ is strictly increasing. Surjective onto the positive integers? No — e.g. $3$ is never a factorial ($2! = 2 < 3 < 6 = 3!$).
11.31 Inductive proof of $\sum_{i=1}^{n} i^2 = \frac{n(n+1)(2n+1)}{6}$. Base ($n=1$): LHS $=1$, RHS $=\frac{1\cdot2\cdot3}{6}=1$ ✓. Step: assume it at $k$; add $(k+1)^2$ to both sides: $\frac{k(k+1)(2k+1)}{6} + (k+1)^2 = \frac{k(k+1)(2k+1) + 6(k+1)^2}{6} = \frac{(k+1)(2k^2+7k+6)}{6} = \frac{(k+1)(k+2)(2k+3)}{6}$, the formula at $k+1$. The one move is "peel off the last term and apply the hypothesis." Discovery vs. verification: telescoping (§11.4) discovered the formula from a cube difference; induction only verifies a formula handed to you.
11.32 † Induction. $P(n)$: $\sum_{i=0}^{n-1} 2^i = 2^n - 1$. Base ($n=1$): LHS $= 2^0 = 1$, RHS $= 2^1 - 1 = 1$ ✓. Step: assume $P(k)$; then $\sum_{i=0}^{k} 2^i = (2^k - 1) + 2^k = 2\cdot2^k - 1 = 2^{k+1} - 1 = P(k+1)$ ✓. *In one line from the geometric formula:* with $a=1, r=2$, $\sum_{i=0}^{n-1} 2^i = \frac{2^n - 1}{2-1} = 2^n - 1$. Two roads, one destination (theme four).
11.33 The map $j = i - 1$ has domain $\{1, 2, \dots, n\}$ (the original index values) and codomain $\{0, 1, \dots, n-1\}$ (the new index values). It is a bijection between these two finite sets (strictly increasing, hence injective, and it hits every value $0$ through $n-1$, hence surjective). Because it pairs each original index $i$ with exactly one new index $j$ and vice versa, the substitution $a_i = a_{(j+1)}$ reproduces every original term exactly once and no term is added or lost — which is precisely why $\sum_{i=1}^{n} a_i = \sum_{j=0}^{n-1} a_{j+1}$.
11.35 Claim: i = 1; while i < n: i *= 2 runs its body $\lceil \log_2 n \rceil$ times for
$n \ge 1$. After $t$ iterations $i = 2^t$; the loop continues while $2^{t} < n$ and stops at the first
$t$ with $2^t \ge n$. That smallest $t$ is $\lceil \log_2 n \rceil$ (for $n=1$, $2^0 = 1 \ge 1$ stops
immediately, $t=0 = \lceil\log_2 1\rceil$). Connection: if pass $j$ does $2^j$ work, the total is
$\sum_{j=0}^{k-1} 2^j = 2^k - 1 < 2\cdot 2^{k-1}$ — less than twice the last pass — so even though there
are $\sim\log_2 n$ passes, the geometric series makes the total work linear ($< 2n$), not
$n\log n$.
Chapter 12 — Answers to Selected Exercises
Solutions to the daggered (†) and odd-numbered problems. Relations are sets of pairs; "$R$ on $A$" means the same set on both sides. Code outputs are hand-derived (never executed).
Part A — Warm-ups
12.1 † "$a + b$ is even" on $A = \{1,2,3,4\}$. A sum is even iff both addends have the same parity. Odds in $A$: $1, 3$; evens: $2, 4$. The pairs: $$\{(1,1),(1,3),(3,1),(3,3),(2,2),(2,4),(4,2),(4,4)\}.$$ (Eight pairs: $2 \times 2$ within the odds plus $2 \times 2$ within the evens.)
12.3 † Reading the matrix $M$ (order $a, b, c$), a $1$ in row $r$, column $s$ means $(r, s) \in R$: $$R = \{(a,a),\ (a,c),\ (b,b),\ (c,a),\ (c,c)\}.$$
12.5 † Digraph pictures: - Reflexive: every node has a self-loop. - Symmetric: every arrow has a matching arrow in the opposite direction (so you may draw undirected edges). - Antisymmetric: no pair of distinct nodes has arrows both ways (no 2-cycle between distinct nodes); self-loops are allowed. - Transitive: whenever there is a two-step path $a \to b \to c$, there is also the direct arrow $a \to c$ (every two-step path has a one-step shortcut).
Part B — Ordinary computation
12.7 † $R = \{(a,b) \mid a \equiv b \pmod 4\}$ on $A = \{0,\dots,7\}$. Congruence mod $n$ is always an equivalence relation (reflexive: $a - a = 0$; symmetric: $a - b = 4k \Rightarrow b - a = 4(-k)$; transitive: add the multiples of 4). It is not antisymmetric ($0 \equiv 4$ and $4 \equiv 0$ but $0 \ne 4$). So it is reflexive, symmetric, transitive — an equivalence relation. Classes (by remainder mod 4): $$[0] = \{0,4\},\quad [1] = \{1,5\},\quad [2] = \{2,6\},\quad [3] = \{3,7\}.$$
12.9 † $R = \{(1,2),(2,3),(3,1)\}$ on $\{1,2,3\}$. - Reflexive closure (add the diagonal): $\{(1,2),(2,3),(3,1),(1,1),(2,2),(3,3)\}$. - Symmetric closure (add reverses): $\{(1,2),(2,1),(2,3),(3,2),(3,1),(1,3)\}$. - Transitive closure: the cycle $1 \to 2 \to 3 \to 1$ makes every node reach every node. Pass 1 adds the two-step pairs $(1,3),(2,1),(3,2)$. Pass 2 adds the self-loops $(1,1),(2,2),(3,3)$ (e.g. $1 \to 2 \to 3 \to 1$ gives $(1,1)$). Result: all nine pairs $\{1,2,3\}^2$.
12.11 † Partition $\{1,3,5\}, \{2,4\}$ of $\{1,2,3,4,5\}$. The equivalence relation is the within-block pairs (both directions, with self-loops): $$\{(1,1),(1,3),(1,5),(3,1),(3,3),(3,5),(5,1),(5,3),(5,5),(2,2),(2,4),(4,2),(4,4)\}.$$ Count: $3^2 + 2^2 = 9 + 4 = \mathbf{13}$ pairs.
Part C — Prove it
12.13 † (Scaffolded.) Filled blanks for transitivity of "$\equiv \pmod 5$": - $b - c = 5m$. - $a - c = (a-b) + (b-c) = 5k + 5m = 5(k + m)$. - so $(a, c) \in R$.
Full statement: Suppose $(a,b),(b,c) \in R$, so $a - b = 5k$ and $b - c = 5m$ for integers $k, m$. Then $a - c = (a-b)+(b-c) = 5(k+m)$, and $k+m \in \mathbb{Z}$, so $5 \mid (a-c)$, i.e. $(a,c) \in R$. Hence $R$ is transitive. $\blacksquare$
12.15 † Claim: If $R$ on $A$ is symmetric and transitive, and every $a \in A$ appears in some pair of $R$, then $R$ is reflexive. Proof. Fix $a \in A$. By hypothesis there is some $b$ with $(a, b) \in R$. By symmetry, $(b, a) \in R$. By transitivity applied to $(a, b)$ and $(b, a)$, we get $(a, a) \in R$. Since $a$ was arbitrary, $(a, a) \in R$ for all $a \in A$, so $R$ is reflexive. Combined with the given symmetry and transitivity, $R$ is an equivalence relation. $\blacksquare$ (The "every $a$ appears in some pair" hypothesis is essential — it is exactly what Exercise 12.18's bogus proof silently assumes. Without it, an element in no pair never gets its self-loop.)
12.17 † Claim: $R$ on $A$ (with adjacency matrix $M$, fixed element ordering) is symmetric iff $M = M^{\mathsf{T}}$. Proof. By definition $M_{ij} = 1 \iff (a_i, a_j) \in R$, and $(M^{\mathsf{T}}){ij} = M = 1 \iff (a_j, a_i) \in R$. ($\Rightarrow$) Suppose $R$ is symmetric. For any $i, j$: $M_{ij} = 1 \iff (a_i, a_j) \in R \iff (a_j, a_i) \in R$ (symmetry) $\iff M_{ji} = 1$. So $M_{ij} = M_{ji}$ for all $i, j$, i.e. $M = M^{\mathsf{T}}$. ($\Leftarrow$) Suppose $M = M^{\mathsf{T}}$, so $M_{ij} = M_{ji}$ for all $i, j$. If $(a_i, a_j) \in R$ then $M_{ij} = 1$, hence $M_{ji} = 1$, hence $(a_j, a_i) \in R$. So $R$ is symmetric. $\blacksquare$
Part D — Find the error
12.18 † The flaw: the argument only ever produces $(a, a)$ for elements $a$ that already appear as the first coordinate of some pair $(a, b) \in R$. It says nothing about an element that is in no pair of $R$. For such an element, reflexivity demands $(a, a)$ but the argument never supplies it. So "symmetric + transitive $\Rightarrow$ reflexive" is false in general. Counterexample: $R = \emptyset$ on $A = \{1\}$ is vacuously symmetric and transitive but not reflexive (no $(1,1)$). The claim becomes true only with the extra hypothesis of Exercise 12.15 (every element appears in some pair).
12.19 "Antisymmetric" is not "not symmetric." They are different conditions on different logical shapes: symmetric requires the reverse arrow to exist; antisymmetric forbids a reverse arrow between distinct elements. A relation can be both, neither, or one. A relation on $\{1, 2\}$ that is both symmetric and antisymmetric: the equality relation $\{(1,1),(2,2)\}$. It is symmetric (every pair's reverse is present — trivially, since each is a self-loop) and antisymmetric (there is no pair $(a, b)$ with $a \ne b$, so the antisymmetry implication holds vacuously). Hence the two are not negations.
12.20 † A single pass over $R = \{(1,2),(2,3),(3,4)\}$ examines the two-step paths present now and adds their shortcuts: $1 \to 2 \to 3$ gives $(1,3)$; $2 \to 3 \to 4$ gives $(2,4)$. But the pair $(1,4)$ is missed: it requires the path $1 \to 3 \to 4$, and $(1,3)$ did not exist until this pass added it. The single-pass argument ignores the cascade — newly added shortcuts create new two-step paths. You must iterate "add all missing shortcuts" until a full pass adds nothing; here that takes two passes (pass 2 adds $(1,4)$). So the transitive closure is $\{(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)\}$.
12.21 for a in R iterates over the pairs of R (each a is a tuple like (1, 2)), not over the
elements of the domain A. So (a, a) in R asks whether ((1,2),(1,2)) is in R, which is essentially
never true — the reflexive check is nonsense. Concrete failure: let R = {(0,0),(1,1)} on A = {0,1}.
This R is a genuine equivalence relation (it is equality on $\{0,1\}$), so the correct answer is
True. But is_equivalence_buggy evaluates reflexive = all((a,a) in R for a in R) over a ranging in
$\{(0,0),(1,1)\}$, checking ((0,0),(0,0)) in R — false — so it returns False. Wrong answer. The fix:
all((a, a) in R for a in A).
Part E — Implement it
12.22 † Antisymmetric test (see code/exercise-solutions.py):
def is_antisymmetric(R):
return all(a == b for (a, b) in R if (b, a) in R)
print(is_antisymmetric({(1, 2), (2, 1)})) # (1,2) & (2,1) present, 1 != 2 -> False
print(is_antisymmetric({(1, 2), (1, 1)})) # only (1,1) has its reverse present -> True
# Expected output:
# False
# True
For each pair $(a,b)$ whose reverse $(b,a)$ is also present, antisymmetry demands $a = b$; the generator
checks exactly that. {(1,2),(2,1)} has a distinct two-way pair, so False; in {(1,2),(1,1)} the only
pair whose reverse is present is the self-loop $(1,1)$, which satisfies $a = b$, so True.
12.23 Reflexive and symmetric closures (non-mutating):
def reflexive_closure(R, A):
return R | {(a, a) for a in A}
def symmetric_closure(R):
return R | {(b, a) for (a, b) in R}
print(sorted(reflexive_closure({(1, 2), (2, 3)}, {1, 2, 3})))
print(sorted(symmetric_closure({(1, 2), (2, 3)})))
# Expected output:
# [(1, 1), (1, 2), (2, 2), (2, 3), (3, 3)]
# [(1, 2), (2, 1), (2, 3), (3, 2)]
Reflexive closure unions in the diagonal $\{(1,1),(2,2),(3,3)\}$; symmetric closure unions in the reverses
$\{(2,1),(3,2)\}$. Both build new sets via |, leaving the input untouched.
12.25 Equivalence relation from a partition:
def relation_from_partition(blocks):
return {(x, y) for b in blocks for x in b for y in b}
print(sorted(relation_from_partition([{0, 3}, {1, 4}, {2, 5}])))
# Expected output:
# [(0, 0), (0, 3), (1, 1), (1, 4), (2, 2), (2, 5), (3, 0), (3, 3), (4, 1), (4, 4), (5, 2), (5, 5)]
For each block we take all within-block ordered pairs $(x, y)$ (including $x = y$), which is exactly the construction in §12.4's converse theorem. Block $\{0,3\}$ contributes $(0,0),(0,3),(3,0),(3,3)$, and similarly for the others — 12 pairs total ($3 \times 2^2$).
Part F — Model it & Conjecture and test
12.26 † Build dependencies $D = \{(A,B),(A,C),(B,D),(C,D),(D,E)\}$. (a) "$X$ must be built at some point before $Y$" is the transitive closure of $D$ — reachability by following dependency arrows. (b) Is $A$ required before $E$? Follow arrows: $A \to B \to D \to E$ (also $A \to C \to D \to E$). So $(A, E)$ is in the transitive closure — yes, $A$ is required (indirectly) before $E$. (c) A self-loop $(X, X)$ in the transitive closure means $X$ must be built before itself — a circular dependency (a cycle). No valid build order can satisfy "before itself," so the build cannot be linearized; Chapter 13 will say a topological sort fails to exist exactly when such a cycle is present.
12.28 † Conjecture: transitive closure stabilizes in at most $n - 1$ passes. (a) Chain $\{(1,2),(2,3),(3,4),(4,5)\}$, $n = 5$. Pass 1 adds the length-2 reachabilities $(1,3),(2,4),(3,5)$; pass 2 adds length-3 $(1,4),(2,5)$; pass 3 adds length-4 $(1,5)$; pass 4 adds nothing. So 4 passes detect stability, with new pairs appearing on passes 1–3 — i.e. $n - 1 = 4$ rounds at most, matching the bound (the last "no-op" pass just confirms stability). (b) Cycle $\{(1,2),(2,3),(3,1)\}$, $n = 3$. Pass 1 adds $(1,3),(2,1),(3,2)$; pass 2 adds the self-loops $(1,1),(2,2),(3,3)$; pass 3 adds nothing. New pairs appear on passes 1–2, i.e. within $n - 1 = 2$ rounds. (c) Argument. If a reachability pair $(a, b)$ first appears on pass $k$, then the shortest path $a \rightsquigarrow b$ in the digraph has length exactly $k + 1$: pass 1 captures length-2 paths (one shortcut over a single intermediate), and each later pass composes a freshly-found pair with one more edge, extending the captured path length by one. A shortest path in an $n$-node digraph that does not repeat a vertex has length at most $n - 1$ edges (it visits at most $n$ distinct vertices). Reachability via a path that repeats a vertex is already witnessed by the shorter path that skips the repeat, so the shortest witnessing path has length $\le n - 1$, hence is found by pass $\le n - 2$; thus no new pair appears after pass $n - 1$, and the closure stabilizes within $n - 1$ passes. $\blacksquare$
Part G — Interleaved & Deep Dive
12.30 † (Ch. 8 + 12.) (a) $\lvert A \times A \rvert = n^2$ (Cartesian-product count, Chapter 8). (b) A relation on $A$ is any subset of $A \times A$, and a set with $n^2$ elements has $2^{n^2}$ subsets. So there are $\mathbf{2^{n^2}}$ relations on $A$. (c) A reflexive relation must contain all $n$ diagonal pairs $(a,a)$ and may contain any subset of the remaining $n^2 - n$ off-diagonal pairs. So there are $\mathbf{2^{\,n^2 - n}}$ reflexive relations.
12.31 (Ch. 9 + 12.) (a) $R_f \subseteq A \times B$ is a function iff: (i) total — for every $a \in A$ there is at least one $b$ with $(a, b) \in R_f$; and (ii) single-valued — for every $a$ there is at most one such $b$. Together: exactly one $b$ per $a$ (§12.1). (b) Define $x \sim y \iff f(x) = f(y)$. Reflexive: $f(x) = f(x)$, so $x \sim x$. Symmetric: $f(x) = f(y) \Rightarrow f(y) = f(x)$, so $x \sim y \Rightarrow y \sim x$. Transitive: $f(x) = f(y)$ and $f(y) = f(z) \Rightarrow f(x) = f(z)$, so $x \sim y \land y \sim z \Rightarrow x \sim z$. All three hold (they are inherited from equality on $B$), so $\sim$ is an equivalence relation. $\blacksquare$ (c) For a key projection $f$, the class $[x]$ is "all rows that share $x$'s key value." When $f$ is injective (the key constraint, §12.6), distinct rows have distinct key values, so each class is a singleton — every row is alone in its class, which is exactly what "no two rows share a key" means.
12.33 (Ch. 6 + 12 — Deep Dive.) Let $M$ be the Boolean adjacency matrix of $R$, and let $M^{(k)}$ be the $k$-th Boolean matrix power (using $\land$ for product, $\lor$ for sum). Claim: $(M^{(k)})_{ij} = 1$ iff there is a path of length exactly $k$ from $a_i$ to $a_j$. Proof by induction on $k \ge 1$. Base case ($k = 1$): $M^{(1)} = M$, and $M_{ij} = 1$ iff $(a_i, a_j) \in R$, i.e. iff there is a path of length exactly 1 from $a_i$ to $a_j$. ✓ Inductive step: assume the claim for $k$. By definition of Boolean matrix product, $$\big(M^{(k+1)}\big)_{ij} = \bigvee_{t} \big(M^{(k)}\big)_{it} \land M_{tj}.$$ This is $1$ iff there exists some intermediate index $t$ with $(M^{(k)})_{it} = 1$ and $M_{tj} = 1$ — i.e. (by the hypothesis) a length-$k$ path $a_i \rightsquigarrow a_t$ followed by a length-1 edge $a_t \to a_j$. Concatenating gives a length-$(k+1)$ path $a_i \rightsquigarrow a_j$, and conversely any length-$(k+1)$ path splits at its second-to-last vertex $a_t$ into a length-$k$ path plus a final edge. So $(M^{(k+1)})_{ij} = 1$ iff a length-$(k+1)$ path exists. ✓ By induction the claim holds for all $k \ge 1$. $\blacksquare$ Transitive-closure remark: $(a_i, a_j)$ is in the transitive closure iff a path of length $\ge 1$ exists, i.e. a path of some length $k$ with $1 \le k \le n$ (longer paths repeat a vertex and a shorter path already witnesses reachability). So the closure's matrix is $M^{(1)} \lor M^{(2)} \lor \dots \lor M^{(n)}$.
12.35 (Deep Dive — Warshall's algorithm.) (a) Warshall's algorithm:
def warshall(M): # M: n x n list of 0/1; modified in place
n = len(M)
for k in range(n): # allow vertex k as an intermediate
for i in range(n):
for j in range(n):
if M[i][k] and M[k][j]:
M[i][j] = 1
return M
(b) On $\{(1,2),(2,3)\}$ over $\{1,2,3\}$ (indices $0,1,2$ for $1,2,3$), start $M = \begin{smallmatrix}0&1&0\\0&0&1\\0&0&0\end{smallmatrix}$. With $k = 1$ (vertex 2): $M[0][1]=1$ and $M[1][2]=1$ give $M[0][2] = 1$. No other $k$ adds anything. Final matrix $$\begin{array}{c|ccc} & 1 & 2 & 3\\ \hline 1 & 0 & 1 & 1\\ 2 & 0 & 0 & 1\\ 3 & 0 & 0 & 0 \end{array}$$ i.e. the closure $\{(1,2),(1,3),(2,3)\}$. (c) The pass-until-stable method of §12.5 may take several passes because a shortcut created in one pass can enable a further shortcut only on a later pass. Warshall reorders the work by intermediate vertex: after the iteration for $k$, $M[i][j]$ already accounts for all paths whose interior vertices are among $\{0, \dots, k\}$ (provable by induction on $k$). So a single sweep $k = 0, 1, \dots, n-1$ accounts for all possible interiors at once — one pass over $k$, total cost $O(n^3)$ — whereas the naive method re-discovers the same composition across multiple whole-relation passes.
Chapter 13 — Partial Orders, Lattices, and Boolean Algebra
13.1 † (a) $\le$ on $\mathbb{Z}$: partial order (in fact a total order). (b) $<$ on $\mathbb{Z}$: not a partial order — fails reflexivity ($a < a$ is false). (c) $\mid$ on $\mathbb{Z}^{+}$: partial order. (d) $\mid$ on $\mathbb{Z}$: not — fails antisymmetry ($2 \mid -2$ and $-2 \mid 2$ but $2 \ne -2$). (e) same-parity on $\mathbb{Z}$: not a partial order — fails antisymmetry (2 and 4 have the same parity in both directions but $2 \ne 4$); it is an equivalence relation, not an order.
13.3 † In $(\{1,2,3,4,6,12\}, \mid)$: (a) comparable to 4: $\{1, 2, 4, 12\}$ (1 and 2 divide 4; 4 divides 12; 4 divides itself). (b) incomparable to 4: $\{3, 6\}$ (4 neither divides nor is divided by 3 or by 6). (c) maximal: only $12$. (d) greatest element: $12$ (it is a multiple of every element, so $x \mid 12$ for all $x$ in the set).
13.5 † $(\{1,2,4,8\}, \mid)$: yes, it is a total order — $1 \mid 2 \mid 4 \mid 8$, so every pair is comparable. Longest chain: the whole set $\{1,2,4,8\}$, length 4. Largest antichain: size 1 (in a total order no two distinct elements are incomparable).
13.7 † Blanks, in order: reflexive — $a = a \cdot \mathbf{1}$. Antisymmetric — $k\ell = \mathbf{1}$; positive integers forcing $k = \ell = \mathbf{1}$; so $b = a \cdot \mathbf{1} = a$. Transitive — $c = bm = (ak)m = a(km)$, i.e. $c = \mathbf{a(km)}$, so $a \mid c$. (Full statement: $\mid$ on $\mathbb{Z}^{+}$ is reflexive, antisymmetric, and transitive, hence a partial order. $\blacksquare$)
13.9 † Every greatest element is maximal. Let $g$ be greatest, so $g \succeq x$ for all $x$. Suppose $g$ were not maximal; then some $z$ has $g \prec z$, i.e. $g \preceq z$ and $g \ne z$. But $g$ greatest gives $z \preceq g$ as well, so by antisymmetry $g = z$ — contradicting $g \ne z$. Hence $g$ is maximal. $\blacksquare$ Maximal but not greatest: in $(\{\{a\},\{b\}\}, \subseteq)$ both singletons are maximal, but neither is greatest. The implication "maximal $\Rightarrow$ greatest" fails because a maximal element only beats everything comparable to it; it need not be comparable to everything, and $\{a\}$ is incomparable to $\{b\}$.
13.11 † Prove $a \wedge (a \vee b) = a$: $$ a \wedge (a \vee b) = (a \vee 0) \wedge (a \vee b) \;\text{(identity: } a = a \vee 0) = a \vee (0 \wedge b) \;\text{(distributive)} = a \vee 0 \;\text{(domination: } 0 \wedge b = 0) = a \;\text{(identity).} \;\blacksquare $$ The principle of duality (swap $\vee\leftrightarrow\wedge$, $0\leftrightarrow1$) then gives the $\vee$-form $a \vee (a \wedge b) = a$ for free — it is exactly the dual of what we just proved.
13.13 † Meet exists and equals $\gcd$. For $a,b \in \{1,\dots,n\}$, $d = \gcd(a,b)$ is a positive integer $\le \min(a,b) \le n$, so $d$ is in the set. $d \mid a$ and $d \mid b$, so $d$ is a lower bound. For any lower bound $w$ (i.e. $w \mid a$ and $w \mid b$), $w$ divides $\gcd(a,b) = d$ (a defining property of gcd, Chapter 22), so $w \preceq d$; thus $d$ is the greatest lower bound. Hence $a \wedge b = \gcd(a,b)$ always exists in this set. Join can escape the set: $\operatorname{lcm}(a,b)$ may exceed $n$. Smallest counterexample: $n = 3$, take $a = 2, b = 3$. $\operatorname{lcm}(2,3) = 6 > 3$, and no element of $\{1,2,3\}$ is a common multiple of 2 and 3, so $2 \vee 3$ does not exist — the poset $(\{1,2,3\},\mid)$ has all meets but not all joins, so it is not a lattice.
13.14 † ```python def is_partial_order(pairs, elements): reflexive = all((a, a) in pairs for a in elements) antisym = all(a == b for (a, b) in pairs if (b, a) in pairs) transitive = all((a, d) in pairs for (a, b) in pairs for (c, d) in pairs if b == c) return reflexive and antisym and transitive
On divisibility over $\{1,2,3,6\}$ (with all reflexive pairs included) it returns **True**. On
$\{(1,2),(2,1)\}$ over $\{1,2\}$ it returns **False**: it is not reflexive ($(1,1),(2,2)$ are absent)
*and* not antisymmetric ($(1,2)$ and $(2,1)$ present with $1 \ne 2$). See `code/exercise-solutions.py`.
**13.15** ```python
def is_chain(subset, pairs):
return all(x == y or (x, y) in pairs or (y, x) in pairs
for x in subset for y in subset)
On the divisors-of-12 order: is_chain({1,2,4,12}, div12) is True (each pair comparable:
$1\mid2\mid4\mid12$); is_chain({4,6}, div12) is False ($4 \nmid 6$ and $6 \nmid 4$).
13.16 † ```python def maximal_elements(pairs, elements): strict = {(a, b) for (a, b) in pairs if a != b} return {x for x in elements if not any(a == x for (a, b) in strict)}
Divisors of 12: maximal $= \{12\}$ (and minimal $= \{1\}$). The "X" poset
$\{(a,c),(a,d),(b,c),(b,d)\}$ (plus reflexive pairs): **maximal** $= \{c, d\}$ (nothing strictly above
$c$ or $d$), **minimal** $= \{a, b\}$ (nothing strictly below $a$ or $b$). Two maximal and two minimal
elements — the hallmark of the non-lattice "X."
**13.17** ```python
def is_lattice(pairs, elements):
def least(cands):
for c in cands:
if all((c, d) in pairs for d in cands):
return c
return None
for a in elements:
for b in elements:
ub = [u for u in elements if (a, u) in pairs and (b, u) in pairs]
lb = [w for w in elements if (w, a) in pairs and (w, b) in pairs]
if least(ub) is None or least(lb) is None:
return False
return True
On the "X" poset it returns False: for the pair $(a,b)$ the upper bounds are $\{c,d\}$, and neither $c$ nor $d$ precedes the other, so there is no least upper bound. (On the divisors of 12 it returns True.)
13.18 † The confusion is conflating maximal ("nothing is strictly above me") with greatest ("I am above everyone"). A maximal element only dominates the elements comparable to it; it may be incomparable to others, in which case it is not greatest. Smallest refuting poset: the two-element antichain $(\{x, y\}, =)$ — equivalently $(\{\{a\},\{b\}\}, \subseteq)$ — where both elements are maximal and neither is greatest. (Two elements are the minimum: a one-element poset has its sole element both maximal and greatest, so it cannot refute the claim.)
13.19 Flaw: the relation is not antisymmetric on all of $\mathbb{Z}$. Counterexample: $2 \mid -2$ (since $-2 = 2\cdot(-1)$) and $-2 \mid 2$ (since $2 = (-2)\cdot(-1)$), yet $2 \ne -2$. So $(\mathbb{Z}, \mid)$ is not a poset. The "proof" simply asserted antisymmetry without using positivity; antisymmetry of $\mid$ requires $a \mid b$ and $b \mid a \Rightarrow \lvert a\rvert = \lvert b\rvert$, which collapses to $a = b$ only when both are positive. Corrected domain: $(\mathbb{Z}^{+}, \mid)$ is a poset.
13.20 † The missing hypothesis is finiteness (and that the relation is a genuine strict order / acyclic, i.e. antisymmetric, not merely transitive). Transitive relations on an infinite set can lack a minimal element: take $(\mathbb{Z}, <)$ — transitive, but no minimal element, since for every $n$ there is $n-1 < n$. Even on a finite set, "transitive" alone is not enough: a relation with a cycle (e.g. $a < b$, $b < a$ via transitivity giving $a < a$) is transitive but has no minimal element among the cycle. The chapter's Lemma needs the poset to be finite and a real partial order (the descending chain must terminate). Without finiteness, the "output a minimal element, delete, repeat" process can get stuck or never start.
13.21 Two errors. (1) Grouping: $a \vee (a \wedge b)$ is not $(a \vee a) \wedge b$ — there is no associativity between different operators, and rewriting $a \vee a \wedge b$ as $(a \vee a) \wedge b$ silently reassociates across $\vee$ and $\wedge$, which the laws never license. (2) Idempotence misuse: even granting the (wrong) regrouping, idempotence says $a \vee a = a$, giving $a \wedge b$, but that is the wrong final answer — absorption gives $a$, not $a \wedge b$. Correct derivation (§13.5): $$ a \vee (a \wedge b) = (a \wedge 1) \vee (a \wedge b) = a \wedge (1 \vee b) = a \wedge 1 = a. \;\blacksquare $$
13.22 † (a) Covering pairs: config.h–db.o, config.h–net.o, db.o–server,
net.o–server, log.o–server. Hasse diagram (time upward):
server
/ | \
db.o net.o log.o
\ /
config.h
(log.o stands alone at the bottom-left, connecting only up to server.) (b) Minimal:
config.h, log.o (nothing below them). Maximal: server (nothing above it). (c) Two topological
sorts: e.g. [config.h, db.o, net.o, log.o, server] and [log.o, config.h, net.o, db.o, server] —
both place config.h before db.o/net.o and everything before server. (d) Longest chain has length
3 (e.g. config.h $\prec$ db.o $\prec$ server). With unlimited workers the build still needs 3
sequential stages: process config.h; then db.o, net.o, log.o in parallel; then server. The
critical path is 3, not 5, even though there are 5 jobs.
13.23 (a) Hasse diagram:
Any
|
number
/ \
int float
\ /
Never
(b) The compiler infers number for cond ? x : y. The join (least upper bound) is right because
the result must be a supertype of both int and float (a value of either type might flow out), and
we want the most specific such type — i.e. the least upper bound — to lose as little type
information as possible. Here the only common supertypes are number and Any, and number is the
least. (c) Yes, a lattice: every pair has a join (walk up to the first common ancestor: int∨float
= number, anything ∨ Any = Any) and a meet (walk down: int∧float = Never, anything ∧ Never
= Never). With Never as bottom and Any as top, all four pairs of distinct middle elements resolve.
13.24 † (a) A label is a pair (level, compartments) with level in the chain
Public $\prec \cdots \prec$ TopSecret and compartments in $(\mathcal{P}({\text{NUCLEAR},
\text{CRYPTO}}), \subseteq)$. The natural order is the product order:
$(\ell_1, C_1) \preceq (\ell_2, C_2)$ iff $\ell_1 \preceq \ell_2$ and $C_1 \subseteq C_2$ — both
coordinates must be $\preceq$. (b) Yes, a lattice (a product of two lattices is a lattice,
coordinatewise). Join: $(\ell_1, C_1) \vee (\ell_2, C_2) = (\max(\ell_1, \ell_2),\, C_1 \cup C_2)$ —
take the higher clearance and the union of compartments. Meet:
$(\min(\ell_1, \ell_2),\, C_1 \cap C_2)$. This is the lattice of security labels in the Bell–LaPadula
model; "read iff your label $\succeq$ the document's label" is exactly the product order.
13.25 † ```python from itertools import combinations def largest_antichain_boolean(n): subsets = [] for r in range(n + 1): subsets += [set(c) for c in combinations(range(1, n + 1), r)] best = 0 # brute force: try all subsets-of-subsets is exponential; instead use that # the largest antichain is the middle layer (test small n by counting layers) from math import comb return max(comb(n, k) for k in range(n + 1))
Computed values: $n=1 \to 1$, $n=2 \to 2$, $n=3 \to 3$, $n=4 \to 6$. **Conjecture:** the largest
antichain has size $\binom{n}{\lfloor n/2 \rfloor}$ — the size of the **middle layer** (all subsets of
size $\lfloor n/2\rfloor$), a binomial coefficient (formalized in Chapter 16). (For $n=4$ that is
$\binom{4}{2} = 6$.) This is **Sperner's theorem**; the middle binomial coefficient is the maximum of
row $n$ of Pascal's triangle. (Note: a from-scratch antichain search over all families of subsets is
exponential; the point of the exercise is to *observe* the pattern $1,2,3,6$ and recognize the middle
binomial coefficient.)
**13.27 †** ```python
from math import gcd
def f(a, b):
meet = gcd(a, b)
join = a * b // gcd(a, b) # lcm(a,b)
return gcd(join, meet) # (a∨b) ∧ (a∧b) in (Z+, |)
Trying pairs (e.g. $(4,6),(6,10),(12,18),(7,21),\dots$) gives $f(a,b) = \gcd(a,b)$ every time: $(a \vee b) \wedge (a \wedge b) = \gcd(\operatorname{lcm}(a,b), \gcd(a,b))$, and since $\gcd(a,b)$ divides $\operatorname{lcm}(a,b)$, that gcd is just $\gcd(a,b)$. Conjecture: $f(a,b) = \gcd(a,b) = a \wedge b$. **Proof:** $a \wedge b = \gcd(a,b)$ divides both $a$ and $b$, hence divides any common multiple, in particular $\operatorname{lcm}(a,b) = a \vee b$ (and the identity $\gcd(a,b)\cdot\operatorname{lcm}(a,b) = ab$ confirms $\operatorname{lcm} = ab/\gcd$ is a multiple of $\gcd$). So $a \wedge b \preceq a \vee b$ in the divisibility order, and the meet of two comparable elements is the smaller one: $(a \vee b) \wedge (a \wedge b) = a \wedge b$. (More generally, in any lattice $x \wedge y = x$ whenever $x \preceq y$, by absorption.) $\blacksquare$
13.28 † "$\subseteq$" is reflexive, antisymmetric, transitive (a partial order); "same cardinality" is reflexive, symmetric, transitive (an equivalence relation). Both share reflexive and transitive. The separating property: antisymmetry (partial orders have it) versus symmetry (equivalence relations have it). Trading symmetry for antisymmetry is exactly what turns "groups things as the same" into "ranks things."
13.29 Boolean-algebra form: $\overline{a \wedge b} = \overline{a} \vee \overline{b}$ (renaming $\neg \to \overline{\phantom{a}}$, $\land \to \wedge$, $\lor \to \vee$). The truth table verified the equivalence by exhaustively checking all four input rows for $\{0,1\}$ — case by case. The algebraic law lets you manipulate expressions symbolically (substitute one side for the other inside a larger expression, e.g. to simplify a circuit) without re-checking cases, and it holds in any Boolean algebra, not just the two-element one.
13.30 † $(\mathcal{P}(S), \subseteq)$ has $2^{\lvert S\rvert}$ elements — the number of subsets of $S$ (Chapter 8, proved by induction: each of the $\lvert S\rvert$ elements is independently in or out). Bottom $0 = \emptyset$ (contained in every subset); top $1 = S$ (contains every subset). Complement $\overline{A} = S \setminus A$, the set-theoretic complement relative to $S$ — and one checks $A \cup \overline A = S = 1$ and $A \cap \overline A = \emptyset = 0$, the complement laws, so $(\mathcal{P}(S), \cup, \cap, \overline{\phantom{a}}, \emptyset, S)$ is a Boolean algebra. $\blacksquare$
13.31 If each of $n$ entries scans up to $n$ predecessors, the total work is at most $\sum_{i=1}^{n} n = n \cdot n = n^2$; more tightly, scanning $i$ predecessors for the $i$-th entry gives $\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$ (the triangular-number closed form, Chapter 11). Either way the work is $O(n^2)$. (Memoization keeps each entry computed once; without it, naive recomputation would be exponential — the point §13.2's table makes.)
13.32 † The underlying principle is the well-ordering principle (Chapter 7): every nonempty
subset of $\mathbb{N}$ has a least element — equivalently, there is no infinite strictly descending
sequence in $\mathbb{N}$. In a finite poset the same "no infinite descent" holds because the set has
finitely many elements, so a strictly descending chain must stop, and where it stops is a minimal
element. This is exactly what makes topo_sort terminate: each pass removes (outputs) a minimal element,
strictly shrinking the remaining set, so after at most $\lvert S\rvert$ passes the set is empty — the
while loop cannot run forever.
13.33 Every finite Boolean algebra has size a power of two. By the stated representation fact, every finite Boolean algebra $B$ is isomorphic to $(\mathcal{P}(S), \subseteq)$ for some finite set $S$ (its set of atoms). By Exercise 13.30, $\lvert \mathcal{P}(S)\rvert = 2^{\lvert S\rvert}$. An isomorphism is a bijection, so $\lvert B\rvert = 2^{\lvert S\rvert}$, a power of two. $\blacksquare$ (The number of atoms $\lvert S\rvert$ determines the size completely.)
Chapter 14 — Algorithms and Complexity
14.1 † Line by line on a list of length $n$: biggest = values[0] is one read + one write,
constant $c_1$; the loop runs $n$ times, each pass a comparison and possibly an assignment plus
bookkeeping, constant $c_2$ per pass; return biggest is constant $c_3$. Total
$T(n) = c_2 n + (c_1 + c_3)$. By the leading-term theorem this is $\Theta(n)$.
14.3 † (a) $\Theta(n)$; (b) $\Theta(n^2)$; (c) $\frac{n(n-1)}{2} = \frac{n^2 - n}{2} = \Theta(n^2)$; (d) $\Theta(1)$; (e) $\Theta(n^3)$. In each case drop constants and lower-order terms, keeping the leading power (§14.3 leading-term theorem).
14.5 † The input size is $n = \log_2 N$ (the number of bits in $N$), not $N$ itself. Then $\sqrt{N} = N^{1/2} = (2^{n})^{1/2} = 2^{n/2}$, so $\sqrt{N}$ trial divisions is $\Theta(2^{n/2})$ work — exponential in the input size $n$. This is exactly why trial-division factoring is infeasible for large $N$ (and why RSA, Chapter 25, is secure).
14.7 † Strategy: take $c$ just above the leading coefficient $5$, leaving margin to absorb the lower terms. Proof. Choose $c = 6$ and $n_0 = 11$. For $n \ge 11$ we have $3n + 100 \le 3n + 100$, and since $100 \le n^2$ and $3n \le n^2$ once $n \ge 11$ (indeed $3 \cdot 11 = 33 \le 121$ and $100 \le 121$), $3n + 100 \le n^2$, so $5n^2 + 3n + 100 \le 5n^2 + n^2 = 6n^2 = c n^2$. Witnesses $(6, 11)$ establish $5n^2 + 3n + 100 = O(n^2)$. $\blacksquare$ (Many other pairs work, e.g. $c = 108, n_0 = 1$.)
14.9 † Blanks filled: choose $c = 3$ and $n_0 = 2$. For all $n \ge 2$, $4n \le n^3$ (because $n^2 \ge 4$ once $n \ge 2$), and therefore $$2n^3 + 4n \le 2n^3 + n^3 = 3n^3 = c\,n^3.$$ Witnesses $(3, 2)$ give $2n^3 + 4n = O(n^3)$. $\blacksquare$
14.11 † Since $\log_a n = \dfrac{\log_b n}{\log_b a}$ and $k := \dfrac{1}{\log_b a}$ is a positive constant (as $a, b > 1$), we have $\log_a n = k \log_b n$. Then $\log_a n \le k \log_b n$ (upper, $c_2 = k$, $n_0 = 2$) and $\log_a n \ge k \log_b n$ (lower, $c_1 = k$), so $\log_a n = \Theta(\log_b n)$. Because all log bases are $\Theta$ of one another, the base carries no asymptotic information, so writing $O(\log n)$ with no base is unambiguous. $\blacksquare$
14.13 † Transitivity of $O$. Suppose $f = O(g)$ with witnesses $(c_1, n_1)$ and $g = O(h)$ with $(c_2, n_2)$. For all $n \ge n_1$, $f(n) \le c_1 g(n)$; for all $n \ge n_2$, $g(n) \le c_2 h(n)$. Let $n_0 = \max(n_1, n_2)$ and $c = c_1 c_2$. Then for $n \ge n_0$, $f(n) \le c_1 g(n) \le c_1 (c_2 h(n)) = c\, h(n)$, so $f = O(h)$ with witnesses $(c, n_0)$. $\blacksquare$
14.15 † Claim: $n! \ge 2^{n-1}$ for $n \ge 1$. Base ($n=1$): $1! = 1 \ge 2^0 = 1$. ✓ Step: assume $k! \ge 2^{k-1}$. Then $(k+1)! = (k+1)\,k! \ge (k+1)\,2^{k-1} \ge 2 \cdot 2^{k-1} = 2^k = 2^{(k+1)-1}$, using $k + 1 \ge 2$. ✓ By induction (Chapter 6) it holds for all $n \ge 1$. Consequently $2^{n} = 2 \cdot 2^{n-1} \le 2 \cdot n!$, so $2^n \le 2 \cdot n!$, i.e. $2^n = O(n!)$ (witnesses $c=2, n_0 = 1$). Hence $\Theta(n!)$ grows at least as fast as $\Theta(2^n)$, as the §14.5 hierarchy claims. $\blacksquare$
14.16 † See code/exercise-solutions.py. The worst case is "no duplicate," where neither loop
short-circuits, so the body runs exactly $n^2$ times. On a 4-element list with distinct values,
count_comparisons returns $4^2 = 16$, matching the §14.4 claim that has_duplicate_slow is $\Theta(n^2)$
in the worst case.
14.17 See code/exercise-solutions.py. count_halvings(16) returns $5$: the sequence is
$16 \to 8 \to 4 \to 2 \to 1 \to 0$, five integer-divisions. This equals $\lfloor \log_2 16 \rfloor + 1 =
4 + 1 = 5$, the trip count of a halving loop (§14.4: repeated halving $\Rightarrow \Theta(\log n)$).
14.18 † See code/exercise-solutions.py. On a 5-element list: target at index 0 gives $1$ comparison
(best, $\Theta(1)$); target at index 2 gives $3$; target absent gives $5$ (worst, $\Theta(n)$). These are
exactly the three cases of §14.6, confirming best $\ne$ worst for an input-content-dependent algorithm.
14.19 See code/exercise-solutions.py. For $n = 5$, the inner if runs $4 + 3 + 2 + 1 + 0 = 10$
times, equal to $\frac{n(n-1)}{2} = \frac{5 \cdot 4}{2} = 10$, matching the §14.4 double-sum derivation
$\sum_{i=0}^{n-1}(n-1-i) = \frac{n(n-1)}{2} = \Theta(n^2)$.
14.20 † The flaw: $c$ must be a constant, but the "proof" takes $c = n$, which depends on $n$. The definition of $O$ requires a single constant $c$ that works for all $n \ge n_0$. No constant $c$ satisfies $n^2 \le c n$ for all large $n$, because dividing by $n$ gives $n \le c$, which fails once $n > c$. So $n^2 \ne O(n)$; the claim is false and the "proof" smuggles $n$ into a slot reserved for a constant.
14.21 The claim is false because the rule "two nested loops $\Rightarrow O(n^2)$" assumes the inner loop runs $\Theta(n)$ times. Here the inner loop runs a constant $5$ times, so each outer pass costs $O(5) = O(1)$, and $n$ outer passes give $\Theta(n)$, not $\Theta(n^2)$ (§14.4 pitfall: always identify the inner trip count).
14.22 † The claim $2^{n+1} = O(2^n)$ is true; the reasoning is wrong. $2^{n+1} = 2 \cdot 2^n$, so taking $c = 2$ and $n_0 = 0$ gives $2^{n+1} = 2 \cdot 2^n \le c \cdot 2^n$ for all $n$ — witnesses $(2, 0)$. The error is conflating "the left side keeps doubling" (true of the value) with "no constant multiple bounds it" (false): a constant factor of $2$ absorbs the extra doubling exactly. (Contrast with $2^{2n} = (2^n)^2$, which is genuinely not $O(2^n)$.)
14.23 The category error is treating "worst case" (a question: which input?) and "$O$" (an answer shape: an upper bound) as the same kind of thing — they are independent axes (§14.6 pitfall). The conclusion does not follow because two statements happening to share the value $\Theta(n)$ does not make the concepts identical: one can equally put a bound on the best case ("best case is $\Omega(1)$") or a lower bound on the worst case. "$O$ = worst case" is a confusion of the bound shape with the input choice.
14.24 † The error: by the §14.5 definition, polynomial time means $O(n^k)$ for a constant $k$, and $100$ is a constant, so $O(n^{100})$ is polynomial, not exponential (exponential means $\Omega(c^n)$ with the variable in the exponent). However, "polynomial" $\ne$ "fast": $n^{100}$ is astronomically slow in practice. "Polynomial vs. exponential" is the theoretical tractability line; practical speed also depends on the exponent and constants (§14.5 Check Your Understanding).
14.25 † Model: comparing every pair of $n$ users is $T(n) = \binom{n}{2} = \frac{n(n-1)}{2}$ comparisons, which is $\Theta(n^2)$. At $n = 100{,}000$, $T \approx \frac{(10^5)^2}{2} = 5 \times 10^9$ comparisons; at 1 ns each that is about $5$ seconds — already too slow for an interactive feature. At $n = 10^8$, $\Theta(n^2)$ is hopeless ($\sim 5 \times 10^{15}$ ops). The asymptotic improvement that makes it feasible is dropping from $\Theta(n^2)$ to $\Theta(n)$ or $\Theta(n \log n)$ (e.g., bucketing/blocking so each user is compared only to a bounded set of candidates) — a change of growth class, not a constant-factor tweak (§14.4–14.5).
14.27 † Adapt the "count the losers" argument (§14.6): to find the minimum, call an element a winner once it has been on the larger side of some comparison. The algorithm can declare an element the minimum only if every other element has lost to something (been shown larger than some element); otherwise it could not distinguish the input from one where an unexamined element is the true minimum. Each comparison of distinct numbers produces exactly one winner, so it can newly "rule out as minimum" at most one element. Ruling out all $n - 1$ non-minima needs $\ge n - 1$ comparisons, giving the problem an $\Omega(n)$ lower bound. The obvious single-pass scan uses exactly $n - 1$ comparisons, so it is $O(n)$; matching the $\Omega(n)$ lower bound, it is optimal and the problem is $\Theta(n)$.
14.29 † Predicted ratios: a single $O(1)$-body loop of $n$ passes does $n$ operations, so doubling
$n$ doubles the work — ratios near $2.0$. For start=10, doublings=3 the sizes are $10, 20, 40, 80$, op
counts $10, 20, 40, 80$, and doubling_ratios returns [2.0, 2.0, 2.0]. Proof that the loop is
$\Theta(n)$: the body runs exactly $n$ times, each pass costing a constant $c$ units, so $T(n) = cn$;
then $T(n) \le c n$ (so $O(n)$, witnesses $(c, 0)$) and $T(n) \ge c n$ (so $\Omega(n)$), hence
$\Theta(n)$, and the ratio $\frac{c(2n)}{cn} = 2$. The empirical "ratio $\approx 2$" is evidence, not a
proof, because it samples finitely many sizes; the §14.3 argument settles all $n$ (theme four).
14.31 † Test on $f(n) = 2n$, $g(n) = n$ (and indeed $2n = O(n)$). Then $\frac{2^{f(n)}}{2^{g(n)}} = \frac{2^{2n}}{2^{n}} = 2^{n}$. For $n = 1, 2, 3, 4$ this is $2, 4, 8, 16$ — growing without bound. So no constant $c$ bounds $2^{2n}$ by $c \cdot 2^n$, i.e. $2^{2n} \ne O(2^n)$. This disproves the conjecture: a true $O$-statement inside an exponent is not preserved, because exponentiation magnifies the constant factor hidden in $f = O(g)$ into an unbounded one. (Moral: you may not "apply $O$" inside an exponent.)
14.33 † $\Theta$ is an equivalence relation. Reflexive: $f = \Theta(f)$ via $c_1 = c_2 = 1$. Symmetric: if $c_1 g \le f \le c_2 g$ for $n \ge n_0$, then $\frac{1}{c_2} f \le g \le \frac{1}{c_1} f$, so $g = \Theta(f)$ (swap and invert the constants). Transitive: if $f = \Theta(g)$ and $g = \Theta(h)$, compose the constant bounds (multiply them) and take the larger threshold to get $f = \Theta(h)$ (uses Exercise 14.13's idea twice). All three hold, so $\sim$ is an equivalence relation (Chapter 12). $\preceq$ ("$f = O(g)$") is a preorder but not a partial order: it is reflexive ($f = O(f)$) and transitive (14.13), but it fails antisymmetry — $n = O(2n)$ and $2n = O(n)$ yet $n \ne 2n$. (It lacks antisymmetry because distinct functions can be mutually $O$; the antisymmetry would only hold on the quotient by $\Theta$, i.e. on $\Theta$-classes.)
14.35 † $\lceil \cdot \rceil$ (Chapter 9) maps a real number to the least integer $\ge$ it, so its codomain is $\mathbb{Z}$ (here $\mathbb{N}$). The number of iterations must lie in that integer codomain because you cannot perform a fractional loop pass — the count is a cardinality, hence a natural number. $\log_2 n$ gives the ideal (generally irrational) number of halvings; ceiling rounds up to the actual whole-number count. The rounding adds at most $1$, and $\lceil \log_2 n \rceil \le \log_2 n + 1 = \Theta(\log n)$, so the classification is unchanged: binary search is $O(\log n)$ (§14.4).
14.37 † Transitive: if $f \prec g$ and $g \prec h$, then $f = O(g)$ and $g = O(h)$ give $f = O(h)$ (14.13); and $h \ne O(f)$ follows because $h = O(f)$ together with $f = O(g)$ would give $h = O(g)$, contradicting $g \prec h$ (which requires $h \ne O(g)$). So $f \prec h$. Irreflexive: $f \prec f$ would require $f \ne O(f)$, but $f = O(f)$ always, so $f \prec f$ is impossible. Thus $\prec$ is a strict order on $\Theta$-classes. Incomparable pair: let $f(n) = n^{1 + \sin n}$ (oscillating between $n^0$ and $n^2$) and $g(n) = n$. Neither $f = O(g)$ (since $f$ exceeds any $cn$ at the peaks where $\sin n \to 1$) nor $g = O(f)$ (since $f$ dips below $g$ at the troughs where $\sin n \to -1$) holds, so they are incomparable — the order is only partial. (Any function that oscillates across $g$ infinitely often works.)
Chapter 15 — Worked Solutions (dagger † and odd-numbered exercises)
Source: part-03-counting-and-probability/chapter-15-basics-of-counting/exercises.md.
These solutions are assembled into the appendix answers-to-selected.md. Every count is justified by
naming the rule and checking its proviso. Daggered exercises: 15.1, 15.3, 15.5, 15.7, 15.9, 15.11,
15.14, 15.16, 15.18, 15.20, 15.22, 15.24, 15.26, 15.28, 15.31, 15.33, 15.37. Odd exercises are also
included.
Part A — Warm-ups
15.1 † Five positions, 10 independent choices each (the choice at one position does not change the number of options at another), so by the product rule the count is $10^5 = 100{,}000$.
15.3 † A byte is 8 independent binary choices: $2^8 = 256$ values (product rule). A 16-bit register is $2^{16} = 65{,}536$ values. As powers of 2 and decimals: $2^8 = 256$ and $2^{16} = 65{,}536$.
15.5 † Multiples of 7 up to 200: $\lfloor 200/7 \rfloor = 28$ (since $7 \times 28 = 196 \le 200 < 203 = 7 \times 29$).
Part B — Computation
15.7 † Seven distinct lowercase letters: first position 26 options, second 25 (cannot reuse the first), …, seventh 20: $$26 \times 25 \times 24 \times 23 \times 22 \times 21 \times 20 = 3{,}315{,}312{,}000.$$ The product rule still applies because what it requires is that the number of choices at each step be the same regardless of history — here it is exactly $26, 25, 24, \dots$ no matter which specific letters were picked earlier. The identity of the available letters shrinks, but the count at each step is fixed, so we may multiply. (Hand check of the product: $26\cdot25=650$; $\cdot24=15{,}600$; $\cdot23=358{,}800$; $\cdot22=7{,}893{,}600$; $\cdot21=165{,}765{,}600$; $\cdot20=3{,}315{,}312{,}000$.)
15.9 † Multiples of 4 or 6 in $1..300$. Let $A$ = multiples of 4, $B$ = multiples of 6. $\lvert A \rvert = \lfloor 300/4 \rfloor = 75$; $\lvert B \rvert = \lfloor 300/6 \rfloor = 50$. The overlap is multiples of both 4 and 6, i.e. of $\operatorname{lcm}(4,6) = 12$ (not $4 \times 6 = 24$, because 4 and 6 are not coprime — they share a factor of 2). So $\lvert A \cap B \rvert = \lfloor 300/12 \rfloor = 25$. By inclusion-exclusion, $\lvert A \cup B \rvert = 75 + 50 - 25 = 100$.
15.11 † Universe: all length-6 strings over 36 symbols, $36^6$. Complement (no digit): only the 26 letters are allowed, $26^6$. By the subtraction principle, at-least-one-digit $= 36^6 - 26^6 = 2{,}176{,}782{,}336 - 308{,}915{,}776 = 1{,}867{,}866{,}560$. (Universe $36^6$: $36^2 = 1296$, $36^3 = 46{,}656$, $36^6 = 46{,}656^2 = 2{,}176{,}782{,}336$. Complement $26^6 = 308{,}915{,}776$, from §15.6.)
15.13 First character a vowel (5 options), last a consonant (21 options), middle two characters free (26 each). These four positions are independent (the count at each is fixed regardless of the others), so by the product rule: $5 \times 26 \times 26 \times 21 = 5 \times 21 \times 676 = 105 \times 676 = 70{,}980$. The first and last positions constrain the option count (5 and 21 instead of 26); the two middle positions are unconstrained (26 each). All four counts are still fixed independent of one another, so the product rule applies.
Part C — Prove it (scaffolded)
15.14 † Blanks filled: - The three disjoint pieces are $A \setminus B$, $\mathbf{B \setminus A}$, and $A \cap B$. By the sum rule, $\lvert A \cup B \rvert = \lvert A \setminus B \rvert + \mathbf{\lvert B \setminus A \rvert} + \lvert A \cap B \rvert$. - $A$ partitions into $A \setminus B$ and $A \cap B$, so $\lvert A \setminus B \rvert = \mathbf{\lvert A \rvert - \lvert A \cap B \rvert}$. Symmetrically $\lvert B \setminus A \rvert = \mathbf{\lvert B \rvert - \lvert A \cap B \rvert}$. - Substituting: $\lvert A \cup B \rvert = (\lvert A \rvert - \lvert A \cap B \rvert) + (\lvert B \rvert - \lvert A \cap B \rvert) + \lvert A \cap B \rvert = \mathbf{\lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert}$. $\blacksquare$
15.16 † Sum rule from inclusion-exclusion. Two-set inclusion-exclusion gives, for any finite $A, B$, $\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert$. If $A \cap B = \emptyset$ then $\lvert A \cap B \rvert = \lvert \emptyset \rvert = 0$, so the correction term vanishes and $\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert$ — which is exactly the two-set sum rule. The general $k$-set sum rule follows the same way (every pairwise intersection is empty, so every correction term in the $k$-set inclusion-exclusion is 0). $\blacksquare$
15.15 (Division rule, even though not daggered/odd — included for completeness of Part C scaffolds.) Blanks: the preimage sets are pairwise disjoint and their union is all of $S$, so they partition $S$. Then $\lvert S \rvert = \sum_{t \in T} \lvert f^{-1}(t)\rvert = \sum_{t \in T} \mathbf{d} = \mathbf{\lvert T \rvert \cdot d}$, hence $\lvert T \rvert = \lvert S \rvert / d$. $\blacksquare$
15.17 Symmetric difference. The elements in exactly one of $A, B$ form $(A \cup B) \setminus (A \cap B)$. Starting from $\lvert A \cup B \rvert = \lvert A \rvert + \lvert B \rvert - \lvert A \cap B \rvert$ and removing the $\lvert A \cap B \rvert$ "both" elements (which are not in exactly one): $$\lvert A \mathbin{\triangle} B \rvert = \lvert A \cup B \rvert - \lvert A \cap B \rvert = \lvert A \rvert + \lvert B \rvert - 2\lvert A \cap B \rvert.$$ Equivalently, the "both" elements were counted once in $\lvert A \rvert + \lvert B \rvert$ after the first subtraction; subtracting $\lvert A \cap B \rvert$ a second time removes them entirely, leaving only the elements in exactly one set. $\blacksquare$
Part D — Find the error
15.18 † The product rule fails because the number of valid second letters depends on which first
letter was chosen — it is not a constant 26 (or 25) per step. If the first letter is a, there are
25 valid second letters (b…z); if it is y, only 1 (z); if it is z, 0. Since the count at the
second step varies with the first choice, the "regardless of earlier steps" proviso is violated and you
may not multiply. (Correct count: this is "choose 2 distinct letters and put them in order," which has
exactly one valid order each, so it equals the number of 2-element subsets, $\binom{26}{2} = 325$ — a
Chapter 16 quantity, but you can see it must be far below 676.)
15.19 The two sets overlap, so the sum rule does not apply. Multiples of 4 and multiples of 6 both include multiples of $\operatorname{lcm}(4,6) = 12$, and those got counted twice. Correct count by inclusion-exclusion: $\lfloor 100/4 \rfloor + \lfloor 100/6 \rfloor - \lfloor 100/12 \rfloor = 25 + 16 - 8 = 33$.
15.20 † The division rule requires the over-count to be uniform — every object counted exactly $d$ times. The argument divides by $d = 2 \cdot 5 = 10$ assuming every bracelet arrangement has exactly $2 \cdot 5 = 10$ equivalent labelings (5 rotations × 2 flips). But that fails for arrangements with extra symmetry: a bracelet that coincides with its own reflection is counted fewer than 10 times, so dividing the total $5!$ by a single $d = 10$ is invalid. The argument never checks that no arrangement is symmetric. The situation in which the simple division rule fails is exactly non-uniform over-counting; the correct tool is Burnside's lemma (beyond this book), which averages the fixed points of each symmetry. (The exercise does not ask for the true count.)
15.21 The method over-counts: a password with two or more vowels is counted once for each vowel-position it could have been "the chosen vowel slot," so $8 \times 5 \times 26^7$ counts such passwords multiple times (it is not a valid division-rule situation because the over-count is not uniform). Correct method — the subtraction principle: at-least-one-vowel $= 26^8 - 21^8$ (universe minus the all-consonant strings, using the 21 consonants). That is the §15.6 lesson: for "at least one," count the complement.
Part E — Model it
15.22 † Model. The configuration space is the Cartesian product of five independent fields: log level (5) × region (4) × flag$_1$ (2) × flag$_2$ (2) × flag$_3$ (2) × timeout (31 values, 0–30 inclusive). By the product rule, $$5 \times 4 \times 2 \times 2 \times 2 \times 31 = 5 \times 4 \times 8 \times 31 = 160 \times 31 = 4{,}960.$$ Feasibility. 4,960 configurations is small — exhaustive unit testing is entirely feasible (run all of them in well under a second). If the timeout becomes a full 32-bit integer, the count becomes $5 \times 4 \times 8 \times 2^{32} = 160 \times 4{,}294{,}967{,}296 = 687{,}194{,}767{,}360 \approx 6.9 \times 10^{11}$. Exhaustive testing is then impossible; the strategy must shift to boundary values (0, 1, max, max−1, a few interior) and sampling. The single 32-bit field multiplied the space by $2^{32}$ — the combinatorial explosion of §15.6.
15.24 † Model. "$k$ stored items, each landing in one of $2^{32}$ buckets" is a sequence of $k$ independent choices, each with $2^{32}$ options. (a) All bucket-assignment sequences (collisions allowed): by the product rule, $(2^{32})^k = 2^{32k}$. (b) Collision-free sequences (all distinct buckets), with $k \le 2^{32}$: the first item has $2^{32}$ buckets, the second $2^{32}-1$ (any but the first's), the third $2^{32}-2$, …, the $k$-th $2^{32}-k+1$. The count per step is fixed regardless of which buckets were used, so the product rule applies: $$2^{32}\,(2^{32}-1)(2^{32}-2)\cdots(2^{32}-k+1) = P(2^{32},\, k),$$ the descending product with $n = 2^{32}$. (Chapter 17 divides (b) by (a) to get the probability of no collision — the birthday problem.)
Part F — Implement it & Conjecture and test
15.26 † See code/exercise-solutions.py. The function is the product rule:
def keyspace(alphabet_size, length):
return alphabet_size ** length
The exercise's target is keyspace(95, 10) = 95**10 ≈ 5.99 × 10^19 (about 66 bits; see
case-study-01). The reference file demonstrates on the hand-checkable keyspace(26, 4) = 456976
($26^2 = 676$, $676^2 = 456{,}976$) and states the 20-digit value as a magnitude, since $95^{10}$ is not
safely squared by hand.
15.28 † See code/exercise-solutions.py. Brute force and the floor-formula
$\lfloor N/2\rfloor + \lfloor N/3\rfloor - \lfloor N/6\rfloor$ agree:
$N=30 \to 20$; $N=60 \to 40$; $N=100 \to 67$.
Conjecture (proved). When $N = 6m$ is a multiple of 6, $g(N) = 4m = \tfrac{2N}{3}$.
Proof. In any block of 6 consecutive integers $\{6j+1, \dots, 6j+6\}$, exactly the four numbers
$6j+2, 6j+3, 6j+4, 6j+6$ are divisible by 2 or 3 (the two left out, $6j+1$ and $6j+5$, are coprime to 6).
So each block contributes exactly 4. There are $m = N/6$ disjoint blocks covering $1..N$, and by the
sum rule (the blocks are disjoint) the total is $4m = 4 \cdot (N/6) = 2N/3$. For $N = 30$ ($m=5$):
$4 \cdot 5 = 20$ ✓; $N = 60$ ($m=10$): $40$ ✓. $\blacksquare$
15.29 See code/exercise-solutions.py. The brute-force counts for $n = 1,\dots,5$ are
$2, 3, 5, 8, 13$ — the Fibonacci numbers. Recurrence: $h(n) = h(n-1) + h(n-2)$.
Justification by a sum-rule case split on the last bit (the two cases are disjoint and exhaust all
valid strings of length $n$):
- Last bit is 0: the first $n-1$ bits can be any valid no-11 string of length $n-1$ — $h(n-1)$ of
them.
- Last bit is 1: then the $(n-1)$-th bit must be 0 (else 11), and the first $n-2$ bits are any
valid no-11 string of length $n-2$ — $h(n-2)$ of them.
By the sum rule, $h(n) = h(n-1) + h(n-2)$. (With $h(1) = 2$, $h(2) = 3$. Solved via closed form in
Chapters 18–19.)
15.27, 15.30 See code/exercise-solutions.py. 15.27 prints $26^6 - 21^6 = 223{,}149{,}655$
(subtraction principle, value from §15.6). 15.30 prints $50+33+20-16-10-6+3 = 74$ (three-set
inclusion-exclusion, value from §15.4).
Part G — Interleaved & Deep Dive
15.31 † (Ch. 8 + 15.) (a) $\lvert A \times B \rvert = \lvert A \rvert \cdot \lvert B \rvert = mn$, by the product rule (forming an ordered pair is two independent choices: first coordinate from $A$, then second from $B$). (b) $\lvert \mathcal{P}(A) \rvert = 2^m$. Frame as a product: building a subset is $m$ independent binary choices — for each element, "in" or "out" (2 options each), and the choice for one element does not change the number of options for another. So $\underbrace{2 \times 2 \times \cdots \times 2}_{m} = 2^m$.
15.33 † (Ch. 11 + 15.) Using factorial notation (Chapter 11), the descending product $26 \times 25 \times \cdots \times 19$ has 8 factors ending at $19 = 26 - 8 + 1$, and $$26 \times 25 \times \cdots \times 19 = \frac{26!}{18!}.$$ In general, "$k$ ordered choices from $n$ without repetition" is $$n(n-1)\cdots(n-k+1) = \frac{n!}{(n-k)!},$$ because the factors of $(n-k)!$ — namely $(n-k)(n-k-1)\cdots 1$ — are exactly the tail of $n!$ that we don't want, so dividing cancels them and leaves the top $k$ factors $n, n-1, \dots, n-k+1$. (This is $P(n,k)$, named in Chapter 16.)
15.35 (Ch. 12–13 + 15.) (a) A binary relation on $S$ ($\lvert S \rvert = n$) is any subset of $S \times S$. There are $\lvert S \times S \rvert = n^2$ ordered pairs, and a relation chooses each pair to be in or out — $n^2$ independent binary choices — so there are $2^{n^2}$ binary relations (product rule + subset-counting, as in 15.31b). (b) A reflexive relation must contain all $n$ diagonal pairs $(x,x)$, so those $n$ choices are forced. The remaining $n^2 - n$ off-diagonal pairs are free (in or out), giving $2^{n^2 - n}$ reflexive relations.
15.37 † (Deep Dive — derangements via inclusion-exclusion.) For $n = 3$ label the positions and let $A_i$ = "item $i$ is in its original position." Total arrangements $= 3! = 6$. The deranged arrangements are those in no $A_i$, so $$D_3 = 3! - \lvert A_1 \cup A_2 \cup A_3 \rvert.$$ Compute $\lvert A_1 \cup A_2 \cup A_3 \rvert$ by three-set inclusion-exclusion. Singles: fixing one item leaves the other 2 free, so $\lvert A_i \rvert = 2! = 2$ each, total $3 \times 2 = 6$. Pairs: fixing two items forces the third too, $\lvert A_i \cap A_j \rvert = 1! = 1$ each, total $3 \times 1 = 3$. Triple: all three fixed, $\lvert A_1 \cap A_2 \cap A_3 \rvert = 0! = 1$. So $$\lvert A_1 \cup A_2 \cup A_3 \rvert = 6 - 3 + 1 = 4, \qquad D_3 = 6 - 4 = 2.$$ Verification by listing the 6 permutations of $(1,2,3)$: $(1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,1,2), (3,2,1)$. The ones with **no** element in its original position are $(2,3,1)$ and $(3,1,2)$ — exactly 2, matching $D_3 = 2$. (Chapter 17 generalizes to $D_n = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!}$.)
Note on code/ cross-references: solutions to 15.26–15.30 are implemented in
chapter-15-basics-of-counting/code/exercise-solutions.py with hand-derived # Expected output:
comments; the answers above restate the reasoning and the values.
Chapter 16 — Worked Solutions (dagger † and odd-numbered exercises)
Source: part-03-counting-and-probability/chapter-16-permutations-combinations/exercises.md.
These solutions are assembled into the appendix answers-to-selected.md. Every count first classifies
the problem (order? repetition?), then names and applies the model. Daggered exercises: 16.1, 16.3,
16.5, 16.7, 16.9, 16.11, 16.13, 16.15, 16.17, 16.18, 16.20, 16.22, 16.24, 16.26, 16.28, 16.30, 16.31,
16.33, 16.36. Odd-numbered exercises are also included.
Part A — Warm-ups
16.1 † Falling product: $P(7,3) = 7 \times 6 \times 5 = 210$. Factorial form: $\frac{7!}{4!} = \frac{5040}{24} = 210$. They agree. (The falling product is the better computation — three multiplications, no large intermediate factorials.)
16.3 † BANANA has 6 letters: B:1, A:3, N:2. Order matters within an arrangement but the repeated
letters are interchangeable, so this is a multiset permutation:
$$\frac{6!}{1!\,3!\,2!} = \frac{720}{1 \cdot 6 \cdot 2} = \frac{720}{12} = 60.$$
16.5 † Circular arrangements of 6 distinct objects, rotations identified. A linear arrangement has $6! = 720$ orderings, but each circular arrangement can be cut at any of its 6 positions to give 6 linear orderings that are the same ring (the §16.1 "divide out the symmetry" move). So $\frac{6!}{6} = (6-1)! = 5! = 120$ distinct rings.
16.7 (odd, shown in Part B below — placed there with the other Part B items). (See 16.7 under Part B.)
Part B — Computation & choosing the model
16.7 †
(a) Order matters (A then B differs from B then A in a key) and repetition allowed
(AA… is valid). Model: sequences, $n^r$. Count $= 62^5 = 916{,}132{,}832$.
(b) Order matters (1st ≠ 2nd ≠ 3rd) and no repetition (one place each). Model: $P(10,3)$.
Count $= 10 \times 9 \times 8 = 720$.
(c) Order does not matter (the chosen servers are interchangeable) and no repetition. Model:
$\binom{10}{4}$. Count $= \frac{10 \cdot 9 \cdot 8 \cdot 7}{24} = \frac{5040}{24} = 210$.
16.9 † A 5-card hand is an unordered, no-repetition selection — a combination. (a) $\binom{52}{5} = 2{,}598{,}960$ (the count from §16.2). (b) All hearts: choose 5 of the 13 hearts, $\binom{13}{5} = \frac{13\cdot12\cdot11\cdot10\cdot9}{120} = \frac{154440}{120} = 1287$. (c) Exactly two aces: choose 2 of the 4 aces and 3 of the 48 non-aces (product rule over the two disjoint groups, §16.2): $$\binom{4}{2}\binom{48}{3} = 6 \times \frac{48 \cdot 47 \cdot 46}{6} = 6 \times 17{,}296 = 103{,}776.$$
16.11 † Eight distinct lowercase letters, order matters (it is a password — position matters), no
repetition. This is an $r$-permutation, not $26^8$, precisely because repetition is forbidden:
$$P(26, 8) = 26 \times 25 \times 24 \times 23 \times 22 \times 21 \times 20 \times 19 =
62{,}990{,}928{,}000.$$
It is not $26^8$ because $26^8$ would count strings that reuse a letter (e.g. aardvark's repeated a),
which the "no repeated letter" rule excludes. (Hand check of the descending product:
$26\cdot25=650$; $\cdot24=15{,}600$; $\cdot23=358{,}800$; $\cdot22=7{,}893{,}600$;
$\cdot21=165{,}765{,}600$; $\cdot20=3{,}315{,}312{,}000$; $\cdot19=62{,}990{,}928{,}000$.)
16.13 (see Part C). (Daggered; solved under Part C.)
Part C — Prove it
16.13 † Absorption identity $r\binom{n}{r} = n\binom{n-1}{r-1}$, blanks filled:
- $r\binom{n}{r} = r \cdot \dfrac{n!}{r!\,(n-r)!} = \dfrac{n!}{\mathbf{(r-1)!}\,(n-r)!}$ (canceling the factor $r$ against $r! = r\,(r-1)!$).
- $\dfrac{n!}{(r-1)!\,(n-r)!} = n \cdot \dfrac{(n-1)!}{(r-1)!\,(n-r)!}$.
- The lower indices $(r-1)$ and $(n-r)$ sum to $\mathbf{n-1}$, so the fraction is $\dbinom{\mathbf{n-1}}{\,r-1\,}$ (its "bottoms" sum to its "top"). Hence $r\binom{n}{r} = n\binom{n-1}{r-1}$. $\blacksquare$
(Check $n=5, r=2$: left $2\binom{5}{2}=20$; right $5\binom{4}{1}=20$. ✓ — this is the §16.5 committee–chair identity in algebraic dress.)
16.15 † Hockey-stick identity $\sum_{i=r}^{n}\binom{i}{r} = \binom{n+1}{r+1}$.
Combinatorial proof. Count the $(r+1)$-element subsets of $\{1, 2, \dots, n+1\}$; there are $\binom{n+1}{r+1}$ of them by definition (RHS). Now count them another way, by classifying each subset by its largest element. If the largest element is $m$, then the remaining $r$ elements are chosen from $\{1, \dots, m-1\}$ (the $m-1$ elements below it), giving $\binom{m-1}{r}$ subsets. The largest element $m$ ranges over $r+1, r+2, \dots, n+1$ (it must be large enough that $r$ elements fit below it). Summing over $m$ and re-indexing with $i = m - 1$ (so $i$ runs $r$ to $n$): $$\binom{n+1}{r+1} = \sum_{m=r+1}^{n+1}\binom{m-1}{r} = \sum_{i=r}^{n}\binom{i}{r}.$$ Both expressions count the same set of $(r+1)$-subsets, so they are equal. $\blacksquare$ (The name: these terms form a "hockey-stick" diagonal in Pascal's triangle, summing to the entry just below-and-left of the stick's end.)
16.17 † Subset-of-a-subset identity $\binom{n}{k}\binom{k}{j} = \binom{n}{j}\binom{n-j}{k-j}$.
Combinatorial proof. Count the number of ways to choose, from $n$ people, a committee of $k$ and then a subcommittee of $j$ drawn from that committee.
Count one (committee, then subcommittee). Choose the $k$-member committee: $\binom{n}{k}$ ways. From those $k$, choose the $j$-member subcommittee: $\binom{k}{j}$ ways. Product: $\binom{n}{k}\binom{k}{j}$.
Count two (subcommittee first, then the rest of the committee). Choose the $j$ subcommittee members directly from all $n$ people: $\binom{n}{j}$ ways. Then fill the remaining $k - j$ committee seats from the $n - j$ people not yet chosen: $\binom{n-j}{k-j}$ ways. Product: $\binom{n}{j}\binom{n-j}{k-j}$.
Both count the identical set of (committee, subcommittee) outcomes, so the two products are equal. $\blacksquare$
Part D — Find the error
16.18 † The product $52 \cdot 51 \cdot 50 \cdot 49 \cdot 48$ is $P(52, 5)$ — the number of ordered 5-card deals (sequences), not hands. A poker hand is a set; the order the cards arrive does not change the hand. The argument overcounts each hand by its $5! = 120$ orderings. Correct count: $$\frac{P(52,5)}{5!} = \frac{311{,}875{,}200}{120} = 2{,}598{,}960 = \binom{52}{5}.$$ This is the single most common counting error: a permutation where the problem wants a combination (§16.3).
16.19 Two distinct errors. (1) Repetition: $8 \times 8 \times 8$ allows the same topping to be chosen more than once, but a pizza cannot have "pepperoni, pepperoni, mushroom" as a 3-distinct-topping order — toppings should not repeat. (2) Order: the product treats "pepperoni then mushroom then onion" as different from "onion then mushroom then pepperoni," but a topping set is unordered. Removing both errors: order does not matter, no repetition → combination. Correct count: $\binom{8}{3} = \frac{8 \cdot 7 \cdot 6}{6} = 56$. (The bogus 512 overcounts by allowing repeats and by distinguishing orders.)
16.20 † The false step is "$P(10,3) = P(10,7)$." The symmetry identity $\binom{n}{r}=\binom{n}{n-r}$ holds for combinations but there is no analogous symmetry for permutations. Compute: $P(10,3) = 10\cdot9\cdot8 = 720$, while $P(10,7) = 10\cdot9\cdot8\cdot7\cdot6\cdot5\cdot4 = 604{,}800$. They are wildly different. The claim "permutations are just ordered combinations" is true ($P(n,r)=r!\binom{n}{r}$), but the ordering factor is $r!$ — which differs for $r=3$ ($3!=6$) and $r=7$ ($7!=5040$) — so equal combinations do not give equal permutations. Concretely $P(10,3)=3!\binom{10}{3}=6\cdot120=720$ and $P(10,7)=7!\binom{10}{7}=5040\cdot120=604{,}800$; the $\binom{10}{3}=\binom{10}{7}=120$ are equal, but the $r!$ factors are not.
16.21 It is not a valid combinatorial proof because it never names a single concrete set and shows that both sides count it. "Binomial coefficients count subsets, so both sides count subsets" is hand-waving: the two sides count subsets of different sets ($\binom{n}{k}$ counts $k$-subsets of an $n$-set; $\binom{n-1}{k-1}$ and $\binom{n-1}{k}$ count subsets of an $(n-1)$-set), and the argument never explains why those counts add up. The missing ingredient is the §16.5 structure: fix one element $a$ of the $n$-set, observe that every $k$-subset either contains $a$ ($\binom{n-1}{k-1}$ of these) or does not ($\binom{n-1}{k}$), and these disjoint cases partition the $\binom{n}{k}$ subsets — that sum-rule split on a single element is the actual proof. A combinatorial proof must (1) name one set and (2) give two correct counts of that set.
Part E — Implement it
16.22 † See code/exercise-solutions.py. perm_count multiplies $n \cdot (n-1) \cdots (n-r+1)$
directly. perm_count(5,3) = 5*4*3 = 60; perm_count(6,0) = 1 (empty product — the loop
range(6, 6, -1) is empty, so result stays 1, matching $P(n,0)=1$).
Expected output: 60 1.
16.23 See code/exercise-solutions.py. The line result = result * (n - i) // (i + 1) keeps
result == C(n, i+1) after each step, which is always an integer (a product of $i+1$ consecutive
integers is divisible by $(i+1)!$), so // never discards a remainder. comb_count(10,3) = 120;
comb_count(52,5) = 2598960. Expected output: 120 2598960.
16.24 † See code/exercise-solutions.py. pascal_row(n) builds each entry from the previous via the
ratio $\binom{n}{k} = \binom{n}{k-1}\cdot\frac{n-k+1}{k}$. pascal_row(5) = [1, 5, 10, 10, 5, 1], and
its sum is $32 = 2^5$. In general the row sum is $\sum_{k}\binom{n}{k} = 2^n$ (the total-subsets identity,
§16.4). Expected output: [1, 5, 10, 10, 5, 1] 32.
16.25 See code/exercise-solutions.py. multiset_perms(counts) returns
$\frac{(\sum c)!}{\prod c!}$. For MISSISSIPPI the multiplicities are M:1, I:4, S:4, P:2, so
multiset_perms([1,4,4,2]) = 11! / (1! 4! 4! 2!) = 39{,}916{,}800 / 1152 = 34{,}650 — matching the
worked value in §16.1. Expected output: 34650.
Part F — Model it & Conjecture and test
16.26 † Model. $n = 9$ stages, choose $5$, order matters (later stages depend on earlier caches), no repetition (each stage runs once). Model: $r$-permutation. $$P(9, 5) = 9 \times 8 \times 7 \times 6 \times 5 = 15{,}120.$$ If order suddenly does not matter, the model becomes the combination $\binom{9}{5} = \frac{P(9,5)}{5!} = \frac{15120}{120} = 126$. The count drops by a factor of $5! = 120$ — exactly the number of orderings of the chosen 5 that the permutation distinguishes and the combination ignores (§16.3).
16.27 (a) 12 independent booleans → each flag in/out, 2 choices each, order irrelevant = subsets of a 12-set. Count $= 2^{12} = 4096$ (product rule, §16.4). (b) Exactly 3 flags on: choose which 3 of the 12 are on (unordered, no repetition) = $\binom{12}{3} = \frac{12\cdot11\cdot10}{6} = 220$. (c) The identity $\sum_{k=0}^{12}\binom{12}{k} = 2^{12}$ says: the total number of flag configurations (4096) equals the sum, over every possible number $k$ of flags turned on, of the number of configurations with exactly $k$ flags on.
16.28 † (Conjecture and test, then prove.) Alternating sums from pascal_row:
$n=1: 1-1 = 0$; $n=2: 1-2+1 = 0$; $n=3: 1-3+3-1 = 0$; $n=4: 1-4+6-4+1 = 0$; $n=5: 0$.
Conjecture: $\sum_{k=0}^{n}(-1)^k\binom{n}{k} = 0$ for all $n \ge 1$.
Proof. Set $x = 1, y = -1$ in the binomial theorem:
$$\sum_{k=0}^{n}\binom{n}{k}(1)^{n-k}(-1)^k = (1 + (-1))^n = 0^n = 0 \quad (n \ge 1). \quad\blacksquare$$
At $n = 0$: the sum is the single term $\binom{0}{0}(-1)^0 = 1$, and $0^0 = 1$ by convention, so the
formula gives $1$, not $0$ — consistent, because the theorem yields $(1-1)^0 = 0^0 = 1$. The "$=0$"
result requires $n \ge 1$ (so that $0^n = 0$). In words: every non-empty set has equally many
even-sized and odd-sized subsets; the empty set has one subset (itself, size 0, even), breaking the tie.
16.29 (Conjecture and test, then prove.) Testing rows: $n=4$: $1,4,6,4,1$ → max at $k=2$; $n=5$: $1,5,10,10,5,1$ → max at $k=2$ and $k=3$; $n=6$: $1,6,15,20,15,6,1$ → max at $k=3$. Conjecture: $\binom{n}{k}$ is maximized at $k = \lfloor n/2 \rfloor$ (and also $\lceil n/2 \rceil$ when $n$ is odd) — the "middle" of the row. Proof via the ratio. Consider $\dfrac{\binom{n}{k+1}}{\binom{n}{k}} = \dfrac{n!/((k+1)!(n-k-1)!)} {n!/(k!(n-k)!)} = \dfrac{k!(n-k)!}{(k+1)!(n-k-1)!} = \dfrac{n-k}{k+1}$. The entries increase while this ratio exceeds 1 and decrease once it drops below 1. Solve $\frac{n-k}{k+1} > 1 \iff n - k > k + 1 \iff k < \frac{n-1}{2}$. So entries strictly increase for $k < \frac{n-1}{2}$ and strictly decrease for $k > \frac{n-1}{2}$; the maximum sits at $k = \lfloor n/2 \rfloor$ (with a tie at $\frac{n-1}{2}$ and $\frac{n+1}{2}$ when $n$ is odd, since the ratio equals exactly 1 there). $\blacksquare$
16.30 † (Conjecture and test, then prove.) Testing: paths $(0,0)\to(1,1)$: RU, UR → 2; $(0,0)\to(2,1)$: RRU, RUR, URR → 3; $(0,0)\to(2,2)$: 6 paths. Conjecture: the number of monotone lattice paths from $(0,0)$ to $(a,b)$ is $\binom{a+b}{a}$ (equivalently $\binom{a+b}{b}$). Combinatorial proof. A monotone path is a sequence of $a$ R-steps and $b$ U-steps, in some order — a string of length $a + b$ over $\{R, U\}$ with exactly $a$ R's. The path is completely determined by which positions in the string hold the R's, i.e. by choosing an $a$-subset of the $a+b$ positions (unordered, no repetition). There are $\binom{a+b}{a}$ such choices, and each gives a distinct path, so the count is $\binom{a+b}{a}$. (Check: $(2,1) \to \binom{3}{2} = 3$ ✓; $(2,2) \to \binom{4}{2} = 6$ ✓; $(1,1) \to \binom{2}{1} = 2$ ✓.) Equivalently this is the multiset permutation $\frac{(a+b)!}{a!\,b!}$ of the letters, $\binom{a+b}{a}$. $\blacksquare$
Part G — Interleaved & Deep Dive
16.31 † (Ch. 8 + 16.) (a) Number of $k$-subsets of an $n$-set $= \binom{n}{k}$ (definition, §16.2). (b) Summing over all sizes: $\sum_{k=0}^{n}\binom{n}{k} = 2^n$ — the total-subsets identity (§16.4). (c) They are the same combinatorial proof seen two ways: the Chapter 8 "each element is in or out" argument builds each subset by $n$ independent binary decisions ($2^n$ total), while the sum in (b) groups those same subsets by size and counts each size with $\binom{n}{k}$. One set ($\mathcal{P}(S)$), two counts — exactly the §16.5 double-counting structure.
16.33 † (Ch. 6 + 16.) Binomial theorem by induction on $n$. Let $P(n)$: $(x+y)^n = \sum_{k=0}^{n}\binom{n}{k}x^{n-k}y^k$. Base case ($n=0$): LHS $=(x+y)^0 = 1$; RHS $= \binom{0}{0}x^0y^0 = 1$. ✓ Inductive step. Assume $P(n)$. Then $$(x+y)^{n+1} = (x+y)(x+y)^n = (x+y)\sum_{k=0}^{n}\binom{n}{k}x^{n-k}y^k.$$ Distribute: $$= \sum_{k=0}^{n}\binom{n}{k}x^{n-k+1}y^k + \sum_{k=0}^{n}\binom{n}{k}x^{n-k}y^{k+1}.$$ Re-index the second sum with $j = k+1$ (so $k = j-1$, $j$ runs $1$ to $n+1$), and rename $j \to k$: $$= \sum_{k=0}^{n}\binom{n}{k}x^{n+1-k}y^k + \sum_{k=1}^{n+1}\binom{n}{k-1}x^{n+1-k}y^{k}.$$ The $k=0$ term ($x^{n+1}$, coefficient $\binom{n}{0}=1=\binom{n+1}{0}$) comes only from the first sum; the $k=n+1$ term ($y^{n+1}$, coefficient $\binom{n}{n}=1=\binom{n+1}{n+1}$) only from the second. For $1 \le k \le n$, combine the coefficients using Pascal's rule (§16.4): $$\binom{n}{k} + \binom{n}{k-1} = \binom{n+1}{k}.$$ Hence $(x+y)^{n+1} = \sum_{k=0}^{n+1}\binom{n+1}{k}x^{n+1-k}y^k = P(n+1)$. By induction, $P(n)$ holds for all $n \ge 0$. $\blacksquare$
16.35 (Deep Dive — search-space feasibility.) At $10^9$ checks/second: (a) All subsets of 45 items: $2^{45} \approx 3.5 \times 10^{13}$ → exponential; $\approx 3.5\times10^4$ s $\approx 9.8$ hours → borderline/hopeless for interactive use, possible as a long offline job. (b) All orderings of 14 items: $14! \approx 8.7 \times 10^{10}$ → factorial; $\approx 87$ s → borderline (about a minute and a half); each extra item multiplies by the next integer, so 15 items ($\approx 22$ min) is already worse. (c) All 6-subsets of 60 items: $\binom{60}{6} = \frac{60\cdot59\cdot58\cdot57\cdot56\cdot55}{720} \approx 5.0 \times 10^7$ → polynomial in $n$ for fixed $k = 6$ ($\Theta(n^6)$); $\approx 0.05$ s → feasible. (d) All length-12 strings over a 4-symbol alphabet: $4^{12} = 2^{24} = 16{,}777{,}216 \approx 1.7\times 10^7$ → exponential in length; $\approx 0.017$ s → feasible at length 12, but doubling the length to 24 gives $4^{24} \approx 2.8\times10^{14}$ → hopeless. The lesson (§16.6): fixed-$k$ subsets are polynomial and stay feasible; subsets-of-everything ($2^n$), all-orderings ($n!$), and long strings ($n^r$) all explode.
16.36 † (Deep Dive.) Prove $\sum_{k=0}^{n}k\binom{n}{k} = n\,2^{n-1}$.
Combinatorial proof. Count the number of ways to choose a committee of any size from $n$ people and designate one member as its leader (every chosen committee must be non-empty, since it has a leader).
Count one (committee size first, then leader). For each size $k$, choose the $k$ members ($\binom{n}{k}$ ways) and the leader from among them ($k$ ways), giving $k\binom{n}{k}$; sum over all sizes $k = 0, 1, \dots, n$ (the $k=0$ term contributes 0, correctly excluding the empty committee): $\sum_{k=0}^{n}k\binom{n}{k}$.
Count two (leader first, then the rest). Choose the leader from all $n$ people ($n$ ways). Then each of the other $n-1$ people is independently in or out of the committee ($2^{n-1}$ ways, by the total-subsets idea). Product: $n\,2^{n-1}$.
Both count the same set of (committee, leader) outcomes, so $\sum_{k=0}^{n}k\binom{n}{k} = n\,2^{n-1}$. $\blacksquare$ (Check $n=3$: LHS $= 0 + 1\cdot3 + 2\cdot3 + 3\cdot1 = 0+3+6+3 = 12$; RHS $= 3\cdot2^2 = 12$. ✓ This is also obtainable algebraically by differentiating the binomial theorem and using absorption (16.13), but the committee–leader count explains the $n\,2^{n-1}$.)
16.37 (Deep Dive — bridge to Chapter 17.) Choose $r=2$ scoops from $n=3$ flavors $\{A,B,C\}$, repetition allowed, order irrelevant (a cup of two scoops is a multiset). List them: $$\{A,A\},\ \{A,B\},\ \{A,C\},\ \{B,B\},\ \{B,C\},\ \{C,C\}.$$ That is 6 multisets (3 "double" choices + 3 distinct unordered pairs). The formula predicts $\binom{n+r-1}{r} = \binom{3+2-1}{2} = \binom{4}{2} = 6$ — they match. (Chapter 17's "stars and bars" proves the formula: a 2-scoop multiset over 3 flavors corresponds to placing 2 stars among 2 bars, i.e. choosing positions for the 2 stars among $4$ symbols, $\binom{4}{2}$.)
Note on code/ cross-references: solutions to the Part E "implement it" exercises (16.22–16.25) are in
chapter-16-permutations-combinations/code/exercise-solutions.py with hand-derived # Expected output:
comments; the answers above restate the reasoning and the values. The conjecture-and-test exercises
(16.28–16.30) use pascal_row/generators whose output is hand-traced in the solutions above.
Chapter 17 — Worked Solutions (dagger † and odd-numbered exercises)
Source: part-03-counting-and-probability/chapter-17-advanced-counting/exercises.md.
These solutions are assembled into the appendix answers-to-selected.md. Every count names the tool and
checks its proviso; every pigeonhole argument names the pigeons and the holes. Daggered exercises:
17.1, 17.3, 17.5, 17.7, 17.9, 17.11, 17.13, 17.14, 17.16, 17.18, 17.20, 17.22, 17.24, 17.26, 17.28,
17.30, 17.32, 17.34. Odd-numbered exercises are also included.
Part A — Warm-ups
17.1 † Pigeons = the 400 employees; holes = the 366 possible birthdays. Since $400 > 366$, the pigeonhole principle forces at least two employees into the same hole — two share a birthday. The forcing inequality is $n > m$, i.e. $400 > 366$.
17.3 † "Distribute 9 identical stickers among 4 children, a child may get none" is the non-negative integer equation $x_1 + x_2 + x_3 + x_4 = 9$. By stars and bars (zeros allowed), the count is $\binom{9 + 4 - 1}{4 - 1} = \binom{12}{3} = \frac{12\cdot11\cdot10}{6} = 220$.
17.5 † For three sets, $$\lvert A\cup B\cup C\rvert = \lvert A\rvert + \lvert B\rvert + \lvert C\rvert - \lvert A\cap B\rvert - \lvert A\cap C\rvert - \lvert B\cap C\rvert + \lvert A\cap B\cap C\rvert.$$ Specializing to two sets, the triple-intersection term $\lvert A\cap B\cap C\rvert$ disappears (along with the two pairwise terms involving $C$), leaving $\lvert A\cup B\rvert = \lvert A\rvert + \lvert B\rvert - \lvert A\cap B\rvert$.
17.7 (odd) — see Part B.
Part B — Computation
17.7 † A box of 10 muffins chosen from 5 types, order irrelevant, repeats allowed: this is combinations with repetition, $n = 10$ choices from $k = 5$ types. The count is $\binom{10 + 5 - 1}{10} = \binom{14}{10} = \binom{14}{4} = \frac{14\cdot13\cdot12\cdot11}{24} = 1001$. (Equivalently $\binom{14}{4}$ by choosing the 4 bar positions.)
17.9 † Let $A, B, C$ be the multiples of $2, 3, 7$ in $\{1, \dots, 600\}$. Singles: $\lvert A\rvert = \lfloor 600/2\rfloor = 300$, $\lvert B\rvert = \lfloor 600/3\rfloor = 200$, $\lvert C\rvert = \lfloor 600/7\rfloor = 85$. Pairwise (the divisors are pairwise coprime, so use their products): $\lvert A\cap B\rvert = \lfloor 600/6\rfloor = 100$, $\lvert A\cap C\rvert = \lfloor 600/14\rfloor = 42$, $\lvert B\cap C\rvert = \lfloor 600/21\rfloor = 28$. Triple: $\lvert A\cap B\cap C\rvert = \lfloor 600/42\rfloor = 14$. By inclusion–exclusion, $$\lvert A\cup B\cup C\rvert = (300 + 200 + 85) - (100 + 42 + 28) + 14 = 585 - 170 + 14 = 429.$$ Divisible by none of $2, 3, 7$: $600 - 429 = 171$.
17.11 † Surjections from a 5-set onto a 3-set, $\sum_{j=0}^{3}(-1)^j\binom{3}{j}(3-j)^5$: $$\binom30 3^5 - \binom31 2^5 + \binom32 1^5 - \binom33 0^5 = 243 - 3\cdot32 + 3\cdot1 - 0 = 243 - 96 + 3 = 150.$$
17.13 † Holes = 4 suits; we want some hole to reach 4, i.e. $\lceil n/4\rceil \ge 4$. This first holds at $n = 13$, since $\lceil 13/4\rceil = \lceil 3.25\rceil = 4$. With 12 cards you could hold exactly 3 of each suit ($3\times 4 = 12$) and no suit reaches 4, so 12 is not enough; 13 is required. (Equivalently, $(4-1)\cdot 4 + 1 = 13$.)
Other computations (odd, non-dagger)
17.15 (in Part C) — see below.
Part C — Prove it
17.14 † (Scaffolded.) Filled blanks: - Pigeons: the 11 chosen integers. - Holes: the 10 possible remainders mod 10, namely $\{0, 1, \dots, \mathbf{9}\}$. - Since $\mathbf{11} > \mathbf{10}$, the pigeonhole principle forces two integers $a, b$ into the same hole, so $a \equiv b \pmod{10}$, meaning $a - b = 10\cdot \mathbf{(q_1 - q_2)}$ for integers $q_1, q_2$ (writing $a = 10q_1 + r$, $b = 10q_2 + r$), which is divisible by 10. $\blacksquare$
17.15 Claim: the number of solutions of $x_1 + \cdots + x_k = n$ with every $x_i \ge 1$ is $\binom{n-1}{k-1}$. Proof. Substitute $y_i = x_i - 1$ for each $i$; then $y_i \ge 0$, and summing, $\sum_i y_i = \sum_i (x_i - 1) = n - k$. So solutions with every $x_i \ge 1$ correspond bijectively to non-negative solutions of $y_1 + \cdots + y_k = n - k$ (the map $x_i \mapsto x_i - 1$ is invertible via $y_i \mapsto y_i + 1$). By non-negative stars and bars (§17.3), that count is $\binom{(n-k) + k - 1}{k - 1} = \binom{n-1}{k-1}$. $\blacksquare$
17.16 † Claim: any 6 people contain 3 mutual acquaintances or 3 mutual strangers. Proof. Fix a person $P$. Each of the other 5 people is, with respect to $P$, either an acquaintance or a stranger — 2 categories (holes), 5 people (pigeons). By the generalized pigeonhole principle, some category holds at least $\lceil 5/2\rceil = 3$ people; say (relabeling if needed) $P$ is acquainted with at least 3 people $X, Y, Z$. Now consider the three pairs among $X, Y, Z$. If any pair, say $X, Y$, are acquaintances, then $P, X, Y$ are three mutual acquaintances — done. If no pair among $X, Y, Z$ are acquaintances, then $X, Y, Z$ are three mutual strangers — done. The symmetric argument applies if $P$ instead has at least 3 strangers (swap "acquaintance" and "stranger"). In every case the conclusion holds. $\blacksquare$ (This proves $R(3,3) \le 6$.)
17.17 Claim (complement form): the number of elements of $U$ in none of $A_1, \dots, A_n$ is
$\lvert U\rvert - \sum_i\lvert A_i\rvert + \sum_{i 17.18 † (Deep Dive.) Claim: $D_n = (n-1)(D_{n-1} + D_{n-2})$ for $n \ge 2$. Combinatorial
proof. Count derangements of $\{1, \dots, n\}$ by where element $1$ maps. In a derangement, $1$ cannot
map to itself, so its image is some $i \ne 1$: there are $n - 1$ choices for $i$. Fix such an $i$ and
split into two disjoint cases by what maps to position $1$:
- Case (a): $i$ maps to $1$ (so $1$ and $i$ swap). The remaining $n - 2$ elements form a derangement
of the other $n - 2$ positions among themselves, contributing $D_{n-2}$.
- Case (b): $i$ does not map to $1$. Then think of $i$ as "forbidden from position $1$" in place of
element $1$. Concretely, the remaining elements $\{2, \dots, n\}$ must be deranged among positions
$\{2, \dots, n\}$ but with the modified constraint that $i$ avoids position $1$ — which is exactly a
derangement of $n - 1$ items (a bijection of the $n-1$ remaining elements with no element in its
own forbidden spot), contributing $D_{n-1}$. These cases are exhaustive and disjoint, so for each of the $n - 1$ choices of $i$ there are
$D_{n-1} + D_{n-2}$ derangements, giving $D_n = (n-1)(D_{n-1} + D_{n-2})$. $\blacksquare$ 17.19 (Deep Dive.) Using $\frac{1}{(1-x)^k} = \sum_{n\ge0}\binom{n+k-1}{k-1}x^n$: each of the $k$
boxes contributes the factor $\frac{1}{1-x} = \sum_{m\ge0} x^m$ (any number of objects in that box), so
$k$ boxes give $\left(\frac{1}{1-x}\right)^k = \frac{1}{(1-x)^k}$, whose coefficient of $x^n$ is the
number of ways to distribute $n$ objects — i.e. $\binom{n+k-1}{k-1}$, the stars-and-bars formula. Verify
for $k = 2$: $\frac{1}{(1-x)^2} = \left(\sum_{a\ge0}x^a\right)\left(\sum_{b\ge0}x^b\right)$, whose
coefficient of $x^n$ counts pairs $(a, b)$ with $a + b = n$, $a, b \ge 0$. There are $n + 1$ such pairs
($a = 0, 1, \dots, n$), and indeed $\binom{n + 2 - 1}{2 - 1} = \binom{n+1}{1} = n + 1$. The two agree.
$\blacksquare$ 17.20 † Inverse generalized pigeonhole: some hole reaches For 17.21 Spend the minimums, then stars and bars on the leftover; guard a negative remainder. Trace on 17.8: leftover $= 15 - 5 = 10$, $k = 4$, so $\binom{10 + 3}{3} = \binom{13}{3} =
\frac{13\cdot12\cdot11}{6} = 286$. 17.22 † Multiply generating-function factors as coefficient lists; each coin $d$ contributes
$1 + x^d + x^{2d} + \cdots$ truncated to degree $n$. Hand check (matches Exercise 17.12): the (pennies, nickels) pairs summing to 12¢ are $(12, 0)$,
$(7, 1)$, $(2, 2)$ — 3 ways. 17.23 $D_n/n!$ as a float, $n = 1, \dots, 8$. The values converge to $1/e \approx 0.3679$; already at $n = 6$ the ratio $265/720 \approx 0.3681$ is
within $0.0002$ of $1/e$. (Derangements used: $0, 1, 2, 9, 44, 265, 1854, 14833$; factorials
$1, 2, 6, 24, 120, 720, 5040, 40320$.) 17.24 † The flaw: with 30 keys and 30 slots we have $n = m$, not $n > m$, so the pigeonhole
principle does not apply — it guarantees a collision only when $n > m$ strictly. The stated
"proof" ("30 pigeons, 30 holes, so some hole gets two") is simply false; 30 pigeons can occupy 30 holes
one apiece. And the conclusion (a collision exists) is not necessarily true: a hash could place
all 30 keys in 30 distinct slots. (A collision is possible but not forced.) The fix: pigeonhole
forces a collision only at $n = 31$ keys. 17.25 The setup ignored the "each child gets at least one" constraint, so plain non-negative stars
and bars overcounts (it includes distributions where some child gets 0). For all $x_i \ge 1$, use the
non-empty formula $\binom{n-1}{k-1}$ (place one candy per child first, then distribute the remaining
$n - k$). Correct count: $\binom{8-1}{3-1} = \binom{7}{2} = 21$ (equivalently distribute $8 - 3 = 5$
freely: $\binom{5 + 3 - 1}{2} = \binom{7}{2} = 21$). The bogus answer 45 counts the zeros-allowed
problem. 17.26 † The error is the missing inclusion–exclusion overlap term: $A$ (multiples of 4) and $B$
(multiples of 6) intersect, and "$4$ or $6$" double-counts numbers divisible by both. The overlap is
multiples of $\operatorname{lcm}(4, 6) = 12$ (not $4\times6 = 24$, since 4 and 6 share a factor of 2),
so $\lvert A\cap B\rvert = \lfloor 100/12\rfloor = 8$. Correct count:
$\lvert A\cup B\rvert = 25 + 16 - 8 = 33$. (Also note $\lfloor 100/6\rfloor = 16$, which the original had
right.) 17.27 The student stopped the alternating sum after the $j = 1$ term. The surjection formula runs
over all $j$ from $0$ to $m$:
$$\sum_{j=0}^{4}(-1)^j\binom4j(4-j)^4 = 4^4 - \binom41 3^4 + \binom42 2^4 - \binom43 1^4 + \binom44 0^4
= 256 - 4\cdot81 + 6\cdot16 - 4\cdot1 + 0 = 256 - 324 + 96 - 4 = 24.$$
The correct value is $24$ (which is $4!$, as it must be: a surjection from a 4-set onto a 4-set is a
bijection, and there are $4! = 24$). The "absurd" $-68$ came from truncating the sum, not from a broken
formula. 17.28 † (Model it.) The $n$ identical cache copies are the identical objects; the $k$ edge
data centers are the distinct boxes. "How many placement configurations" = non-negative integer
solutions of $x_1 + x_2 + \cdots + x_k = n$ (with $x_i$ = copies at data center $i$). The count is
stars and bars: $\binom{n + k - 1}{k - 1}$. With the redundancy constraint "every data center holds at
least one copy" ($x_i \ge 1$), place one copy in each first and distribute the remaining $n - k$:
the count becomes $\binom{(n-k) + k - 1}{k - 1} = \binom{n - 1}{k - 1}$ (valid when $n \ge k$;
$0$ otherwise). 17.29 A valid secret-santa draw is a derangement of the $n$ coworkers — a permutation (each
person assigned exactly one giftee, each giftee assigned once) with no fixed point (nobody draws
their own name). The number of valid draws is $D_n$. The probability that a random assignment (a
random permutation) is valid is $D_n/n! = \sum_{t=0}^{n}(-1)^t/t!$, which converges to $e^{-1} \approx
0.3679$ as $n$ grows — the Taylor series for $e^{-1}$ — so for moderate $n$ roughly 37% of random
assignments are valid. 17.30 † (Conjecture and test, then prove.) Compute $f(k)$ = non-negative solutions of
$x_1 + x_2 = k$: $f(0) = 1$ (only $(0,0)$), $f(1) = 2$ ($(0,1),(1,0)$), $f(2) = 3$, $f(3) = 4$,
$f(4) = 5$, $f(5) = 6$. Conjecture: $f(k) = k + 1$. Proof (stars and bars):
$f(k) = \binom{k + 2 - 1}{2 - 1} = \binom{k + 1}{1} = k + 1$. $\blacksquare$ The sequence
$1, 2, 3, 4, 5, 6, \dots$ is the positive integers (equivalently the naturals shifted by 1). 17.31 (Conjecture and test.) Ratios $D_n/D_{n-1}$ from $D = 0,1,2,9,44,265,1854,14833$:
$D_2/D_1$ is undefined ($D_1 = 0$); $D_3/D_2 = 2/1 = 2$; $D_4/D_3 = 9/2 = 4.5$; $D_5/D_4 = 44/9 \approx
4.89$; $D_6/D_5 = 265/44 \approx 6.02$; $D_7/D_6 = 1854/265 \approx 6.997$; $D_8/D_7 = 14833/1854 \approx
8.00$. The ratio $D_n/D_{n-1}$ approaches $n$ (since $D_n \approx n!/e$ gives $D_n/D_{n-1} \approx
n!/( (n-1)!) = n$). Heuristic: $D_n \approx n!/e$, so $D_n/D_{n-1} \approx (n!/e)/((n-1)!/e) = n$ —
matching the computed $\approx 8.00$ at $n = 8$. 17.32 † (Ch. 16 + 17.) In the binomial theorem $(x + y)^s = \sum_{t=0}^{s}\binom{s}{t}x^t y^{s-t}$,
set $x = -1$ and $y = 1$: the left side is $(-1 + 1)^s = 0^s = 0$ (for $s \ge 1$), and the right side is
$\sum_{t=0}^{s}\binom{s}{t}(-1)^t\cdot 1^{s-t} = \sum_{t=0}^{s}(-1)^t\binom{s}{t}$. Hence
$\sum_{t=0}^{s}(-1)^t\binom{s}{t} = 0$. Subtracting the $t = 0$ term ($\binom{s}{0} = 1$) gives
$\sum_{t=1}^{s}(-1)^t\binom{s}{t} = -1$, so $\sum_{t=1}^{s}(-1)^{t+1}\binom{s}{t} = 1$ — exactly the
statement that an element lying in $s$ of the sets is counted once by inclusion–exclusion. 17.33 (Ch. 15 + 17.) A function from the $n$-set to the $m$-set that "misses a fixed set of $j$
targets" must send each of the $n$ inputs to one of the remaining $m - j$ allowed outputs. By the
product rule (Ch. 15), each input independently has $m - j$ choices, so there are $(m - j)^n$ such
functions. The number of ways to choose which $j$ of the $m$ targets are the missed ones is
$\binom{m}{j}$ (a $j$-subset of the $m$ targets). These are the two factors in the surjection formula's
$j$-th term. 17.34 † (Ch. 16 + 17.) By the symmetry identity $\binom{a}{b} = \binom{a}{a-b}$ (Ch. 16) with
$a = n + k - 1$ and $b = k - 1$: $\binom{n+k-1}{k-1} = \binom{n+k-1}{(n+k-1)-(k-1)} = \binom{n+k-1}{n}$.
Interpretation: among the $n + k - 1$ symbol positions, you may either choose the $k - 1$ bar positions
(form $\binom{n+k-1}{k-1}$) or equivalently choose the $n$ star positions (form $\binom{n+k-1}{n}$);
fixing one set fixes the other, so the two counts agree. 17.35 (Ch. 9 + 17.) Suppose $K$ is infinite and $m$ is finite, and let $h\colon K \to {0, \dots,
m-1}$. If $h$ were injective, it would be a one-to-one correspondence between $K$ and a subset of the
finite codomain, forcing $K$ to be finite — contradiction. (Concretely, pick any $m + 1$ distinct keys;
they form a set larger than the $m$-element codomain, so by pigeonhole two of them collide, and $h$ is
not injective.) Therefore no such $h$ is injective: a collision is unavoidable. This is exactly the
guarantee hashing needs — with infinitely many possible keys and finitely many slots, some two keys must
share a slot. 17.36 (Deep Dive — preview of Ch. 18.) For $a_0 = 1$, $a_n = 2a_{n-1}$: write
$A(x) = \sum_{n\ge0} a_n x^n$. Then
$$A(x) = a_0 + \sum_{n\ge1} a_n x^n = 1 + \sum_{n\ge1} 2a_{n-1}x^n = 1 + 2x\sum_{n\ge1}a_{n-1}x^{n-1} = 1 + 2x\,A(x).$$
Solving, $A(x)(1 - 2x) = 1$, so $A(x) = \frac{1}{1 - 2x} = \sum_{n\ge0}(2x)^n = \sum_{n\ge0} 2^n x^n$.
Reading the coefficient of $x^n$: $a_n = 2^n$. (As expected — $a_n = 2a_{n-1}$, $a_0 = 1$ doubles each
step; the method of forming $A(x)$, using the recurrence to get a closed equation in $A(x)$, and
reading off coefficients is what Chapters 18–19 generalize, e.g. to Fibonacci.) 18.1 † Unroll $a_n = 3a_{n-1} - 2a_{n-2}$ with $a_0 = 1, a_1 = 3$:
$a_2 = 3(3) - 2(1) = 7$; $a_3 = 3(7) - 2(3) = 15$; $a_4 = 3(15) - 2(7) = 31$. (The pattern $1, 3, 7,
15, 31$ is $2^{n+1} - 1$; the characteristic roots are $1$ and $2$.) 18.3 † Classifications:
(a) $a_n = 4a_{n-1} - 4a_{n-2}$ — linear, homogeneous, constant-coeff; method applies directly.
(b) $a_n = 2a_{n-1} + 3^n$ — linear, nonhomogeneous (forcing $3^n$), constant-coeff; needs a
particular solution (§18.5).
(c) $a_n = a_{n-1}a_{n-2}$ — nonlinear (product of terms); outside the method.
(d) $a_n = (n-1)a_{n-1}$ — linear but variable coefficient ($n-1$ depends on $n$); outside the
method.
(e) $a_n = 6a_{n-1} - 11a_{n-2} + 6a_{n-3}$ — linear, homogeneous, constant-coeff, order 3; method
applies. 18.5 † $a_n = 4\cdot 2^n + (-1)^n$: $a_0 = 4 + 1 = 5$; $a_1 = 8 - 1 = 7$; $a_2 = 16 + 1 = 17$.
Verify $a_n = a_{n-1} + 2a_{n-2}$: $a_1 + 2a_0 = 7 + 10 = 17 = a_2$. ✓ (The recurrence has
characteristic equation $r^2 - r - 2 = (r-2)(r+1) = 0$, roots $2$ and $-1$, matching the two
exponential pieces.) 18.7 † Scaffold for $a_n = 6a_{n-1} - 8a_{n-2}$, $a_0 = 3, a_1 = 10$:
- Characteristic equation: $r^2 - 6r - (\mathbf{-8}) = r^2 - 6r + 8 = 0$.
- Roots: $(r-2)(r-\mathbf{4})$, so $r_1 = 2, r_2 = \mathbf{4}$.
- General solution: $a_n = \alpha\, 2^n + \beta\, \mathbf{4^n}$.
- Initial conditions: $\alpha + \beta = \mathbf{3}$; $2\alpha + 4\beta = \mathbf{10}$. Solving:
$\beta = \mathbf{2}$, $\alpha = \mathbf{1}$.
- Closed form: $a_n = \mathbf{2^n + 2\cdot 4^n}$. Check $a_2$: formula $4 + 2(16) = 36$; recurrence
$6(10) - 8(3) = 36$. ✓ 18.9 † Superposition. Let $x_n, y_n$ each satisfy $a_n = c_1 a_{n-1} + c_2 a_{n-2}$, and put
$z_n = \alpha x_n + \beta y_n$. Then
$$c_1 z_{n-1} + c_2 z_{n-2} = c_1(\alpha x_{n-1} + \beta y_{n-1}) + c_2(\alpha x_{n-2} + \beta y_{n-2})$$
$$= \alpha(c_1 x_{n-1} + c_2 x_{n-2}) + \beta(c_1 y_{n-1} + c_2 y_{n-2}) = \alpha x_n + \beta y_n = z_n,$$
using the two hypotheses in the middle step. So $z_n$ satisfies the recurrence. $\blacksquare$ (This is
the property that lets us build every solution from a few basic ones — §18.3, §18.4.) 18.11 † Check $b_n = n\cdot 3^n$ solves $a_n = 6a_{n-1} - 9a_{n-2}$:
$$6b_{n-1} - 9b_{n-2} = 6(n-1)3^{n-1} - 9(n-2)3^{n-2} = 3^n\big[\tfrac{6(n-1)}{3} - \tfrac{9(n-2)}{9}\big]
= 3^n\big[2(n-1) - (n-2)\big] = 3^n\big[2n - 2 - n + 2\big] = n\,3^n = b_n.$$
So $n\,3^n$ is a solution. Since $r_0 = 3$ is the double root of $r^2 - 6r + 9 = (r-3)^2$, this
confirms the repeated-root rule: the second basic solution is $n\,r_0^n$, not another copy of $r_0^n$
(§18.4). 18.13 † The familiar sequence is Fibonacci, shifted: $t_n = F_{n+1}$. (See 18.15 † Iterative Hanoi from $H_n = 2H_{n-1} + 1$, $H_0 = 0$: Both lists agree, confirming the §18.5 closed form $H_n = 2^n - 1$. 18.17 † The flaw: the roots are not distinct — $r^2 - 4r + 4 = (r-2)^2$ has the repeated root
$r_0 = 2$, so the distinct-roots theorem does not apply. The proposed "$\alpha\,2^n + \beta\,2^n$"
collapses to $(\alpha + \beta)2^n$, a one-parameter family, which cannot match two independent initial
conditions. The correct general solution uses the repeated-root form (§18.4):
$$a_n = (\alpha + \beta n)\,2^n.$$ 18.19 † The student's consecutive-terms fit ($a_1 = 4, a_2 = 14$) is fine and gives the same
answer ($\alpha = -1, \beta = 2$), because the recurrence determines $a_2$ from $a_0, a_1$, so the
system is consistent. But the blanket claim "it doesn't matter which two terms you use" is false.
Using $a_0$ and $a_2$ (skipping $a_1$) gives the system $\alpha + \beta = a_0$,
$\alpha r_1^2 + \beta r_2^2 = a_2$, whose determinant is $r_2^2 - r_1^2 = (r_2 - r_1)(r_2 + r_1)$. This
vanishes whenever $r_1 = -r_2$ (e.g. $a_n = a_{n-2}$, roots $\pm 1$), leaving $\alpha, \beta$
unrecoverable. The theorem of §18.4 guarantees solvability only for $k$ consecutive initial
conditions: there the determinant is $r_2 - r_1 \ne 0$ for distinct roots, always. Lesson: a
second-order recurrence is pinned down by two consecutive terms; skipping one can destroy uniqueness. 18.21 † (Model it — ordered coin sequences.) Condition on the last coin: if it is \$1, the
preceding coins make any sequence summing to $n-1$ ($a_{n-1}$ ways); if it is \$2, the preceding coins
sum to $n-2$ ($a_{n-2}$ ways). These are disjoint and exhaustive, so by the sum rule (Ch. 15)
$$a_n = a_{n-1} + a_{n-2}, \qquad a_0 = 1,\ a_1 = 1.$$
($a_0 = 1$: one way to insert \$0 — insert nothing. $a_1 = 1$: a single \$1 coin.) This is the
Fibonacci sequence, $a_n = F_{n+1}$ — the same object as §18.2's ordered sums of 1s and 2s, in
financial costume. 18.23 † (Conjecture and test, then prove.) The Hanoi recurrence $a_0 = 0$, $a_n = 2a_{n-1} + 1$
gives $a_0, \dots, a_8 = 0, 1, 3, 7, 15, 31, 63, 127, 255$. The doubling-plus-one pattern (each value is
one less than a power of 2) suggests the conjecture $a_n = 2^n - 1$. Proof (§18.5): particular
solution $A = 2A + 1 \Rightarrow A = -1$; associated homogeneous $a_n = 2a_{n-1}$ has root 2, so
$a_n^{(h)} = \alpha\,2^n$; combine $a_n = \alpha\,2^n - 1$; fit $a_0 = 0$: $\alpha - 1 = 0 \Rightarrow
\alpha = 1$. Thus $a_n = 2^n - 1$. The computed values were *evidence* (they matched on $n = 0,\dots,8$),
but evidence cannot rule out a later deviation — only the derivation settles all $n$ (theme four). 18.25 † (Model it — from code.) 18.26 † (Ch. 7 + 18.) To prove $t_n = F_{n+1}$ for the second-order recurrence $t_n = t_{n-1} +
t_{n-2}$, the inductive step needs the result for *both* $t_{n-1}$ and $t_{n-2}$ at once — i.e., you must
assume it for the two previous cases. That is exactly what strong induction (Ch. 7) grants;
ordinary induction supplies only the single immediately-previous case. The proof needs two base
cases, $n = 1$ ($t_1 = 1 = F_2$) and $n = 2$ ($t_2 = 2 = F_3$), because a second-order recurrence
cannot get started until two terms are known (§18.1). (Structure only: base cases $n=1,2$; step assumes
$t_{k-1} = F_k$ and $t_k = F_{k+1}$, then $t_{k+1} = t_k + t_{k-1} = F_{k+1} + F_k = F_{k+2}$.) 18.27 (Ch. 11 + 18.) Unroll $H_n = 2H_{n-1} + 1$:
$$H_n = 2H_{n-1} + 1 = 2^2 H_{n-2} + 2 + 1 = 2^3 H_{n-3} + 4 + 2 + 1 = \cdots
= 2^n H_0 + (2^{n-1} + \cdots + 2 + 1).$$
With $H_0 = 0$, this is the geometric series $\sum_{i=0}^{n-1} 2^i = 2^n - 1$ (the Ch. 11 closed
form for ratio 2). So $H_n = 2^n - 1$, matching §18.5. Advantage of the particular/characteristic
method: for a harder forcing term (say $f(n) = n$, or $f(n) = 3^n$), unrolling produces an awkward sum
you must recognize and evaluate, whereas "guess a particular solution shaped like $f(n)$ and add the
homogeneous solution" stays mechanical regardless of $f$. 18.29 (Ch. 1/2 + 18.) The full statement proved is
$$\forall n \in \mathbb{N},\ \ a_n = 2^n - 1 \qquad (\mathbb{N} \text{ including } 0).$$
The recurrence-plus-initial-condition specification of the same sequence is the conjunction
$$\big(a_0 = 0\big) \ \land\ \big(\forall n \ge 1,\ a_n = 2a_{n-1} + 1\big).$$
Both halves are needed: the universally-quantified recurrence alone is satisfied by infinitely many
sequences (one per choice of starting value), and the lone equation $a_0 = 0$ fixes only the first term;
together they determine the unique sequence (§18.1). This is the logical content of "a recurrence
needs its initial conditions." 18.31 (Deep Dive.) Claim: for distinct roots $r_1 \ne r_2$, every solution of $a_n = c_1 a_{n-1}
+ c_2 a_{n-2}$ has the form $\alpha r_1^n + \beta r_2^n$.
(i) Every such combination is a solution. Immediate from superposition (18.9), since $r_1^n$ and
$r_2^n$ are each solutions (they satisfy the characteristic equation).
(ii) Every solution is such a combination. Let $\{a_n\}$ be any solution, with first two terms
$a_0, a_1$. By 18.30 there exist unique constants $\alpha, \beta$ with $\alpha + \beta = a_0$ and
$\alpha r_1 + \beta r_2 = a_1$ (the system's determinant is $r_2 - r_1 \ne 0$). Define $b_n = \alpha
r_1^n + \beta r_2^n$. Then ${b_n}$ and ${a_n}$ both satisfy the same second-order recurrence and
agree on $a_0, a_1$. Since a second-order recurrence plus two initial conditions determines a sequence
uniquely (§18.1), $a_n = b_n$ for all $n$. Hence $\{a_n\}$ has the claimed form. $\blacksquare$ (This is
the "these are all the solutions" fact the chapter's Deep Dive path asked you to assemble.) Solutions to the daggered (†) and odd-numbered exercises from 19.1 † Identify $a$, $b$, $f(n)$ and $E = \log_b a$.
(a) $a=2, b=2, f(n)=n$; $E = \log_2 2 = 1$.
(b) $a=9, b=3, f(n)=n^2$; $E = \log_3 9 = 2$.
(c) $a=1, b=4, f(n)=1$; $E = \log_4 1 = 0$.
(d) $a=7, b=2, f(n)=n^2$; $E = \log_2 7 \approx 2.807$.
(e) $a=4, b=2, f(n)=n^3$; $E = \log_2 4 = 2$. 19.3 † $T(n) = 8\,T(n/2) + n^2$: $E = \log_2 8 = 3$, so $n^E = n^3$. Compare $f(n) = n^2$ to $n^3$:
$\frac{n^2}{n^3} = \frac{1}{n} = n^{-1}$, so $n^2$ is polynomially smaller than $n^3$ (gap $n^{1}$).
This will be Case 1. 19.5 † (a) False. $a$ counts the recursive calls actually made, not the number of conceptual
pieces — binary search cuts the array in half but makes only one call, so $a=1$. (b) True. Each level
shrinks the input by a factor $b$, reaching size 1 after $\log_b n$ levels. (c) True. Level $i$ has
$a^i$ nodes; the leaf level is $i = \log_b n$, giving $a^{\log_b n} = n^{\log_b a}$ leaves. 19.7 Could the Master Theorem apply (form $a\,T(n/b)+f$, $a\ge1$, $b>1$ constant)?
(a) Yes — $a=2, b=2$ constant. (b) No — subtractive ($n-1$, not $n/b$); use Chapter 18.
(c) No — $a = \sqrt n$ and $b = \sqrt n$ are not constants (they depend on $n$). (d) Yes —
$a=4, b=2$ constant. 19.9 † $T(n) = 4\,T(n/2) + n^2$. $E = \log_2 4 = 2$, $n^E = n^2$. Compare $f(n) = n^2$ to $n^2$: same
order, $f(n) = \Theta(n^E)$. Case 2. Therefore $T(n) = \Theta(n^2 \log n)$. 19.11 † $T(n) = 2\,T(n/4) + \sqrt n$. $a=2, b=4$, so $E = \log_4 2 = \frac{1}{2}$, and
$n^E = n^{1/2} = \sqrt n$. Compare $f(n) = \sqrt n$ to $n^{1/2} = \sqrt n$: same order. Case 2.
Therefore $T(n) = \Theta(\sqrt n \log n) = \Theta(n^{1/2}\log n)$. 19.13 † $T(n) = 3\,T(n/3) + n$. $a=3, b=3$, so $E = \log_3 3 = 1$, $n^E = n$. Compare $f(n)=n$ to $n$:
same order. Case 2. Therefore $T(n) = \Theta(n\log n)$. (Shape seen before: this is "three-way merge
sort" from §19.4's Check Your Understanding — splitting into more parts than two changes the base of the
log, a constant factor, not the asymptotic class.) 19.15 † $T(n) = 2\,T(n/2) + \frac{n}{2}$. The constant factor $\frac12$ does not change $\Theta$:
$f(n) = \frac{n}{2} = \Theta(n)$. $E = \log_2 2 = 1$, $n^E = n$, $f = \Theta(n) = \Theta(n^E)$. Case 2.
Therefore $T(n) = \Theta(n\log n)$ — exactly merge sort. (The point: scale $f$ by any positive constant
and the case is unchanged, because the Master Theorem compares $\Theta$-classes, not exact functions.) 19.17 † Recursion-tree solution of $T(n) = 3\,T(n/2) + n$.
- At level $i$ there are $\mathbf{3^i}$ nodes, each of size $n/2^i$, each doing $n/2^i$ work, so the work
at level $i$ is $3^i \cdot \frac{n}{2^i} = n\cdot \mathbf{(3/2)^i}$.
- The per-level work is geometric with ratio $r = \mathbf{3/2}$. Because $r > 1$, the sum is dominated by
its last term (the leaves).
- The number of leaves is $3^{\log_2 n} = n^{\mathbf{\log_2 3}}$, so $T(n) = \Theta!\left(n^{\mathbf{\log_2 3}}\right)
\approx \Theta(n^{1.585})$.
- Master Theorem: $E = \log_2 3 \approx 1.585$, $f(n)=n$ is polynomially smaller, Case 1, giving the
same $\Theta(n^{\log_2 3})$. ✓ 19.19 † Substitution: prove $T(n) \le c\,n\log_2 n$ for $T(n) = 2T(n/2)+n$, $T(1)=1$, $n$ a power of 2.
Take $P(n)$: "$T(n) \le c\,n\log_2 n$." We choose $c \ge 1$ and prove $P(n)$ for $n \ge 2$ by induction on
powers of 2.
Base case ($n=2$): $T(2) = 2T(1) + 2 = 2(1) + 2 = 4$. We need $4 \le c\cdot 2\log_2 2 = 2c$, i.e.
$c \ge 2$. So take $c = 2$.
Inductive step: assume $P(n/2)$, i.e. $T(n/2) \le c\,\frac{n}{2}\log_2\frac{n}{2}$. Then
$$T(n) = 2\,T(n/2) + n \le 2\cdot c\,\tfrac{n}{2}\log_2\tfrac{n}{2} + n = c\,n\log_2\tfrac{n}{2} + n
= c\,n(\log_2 n - 1) + n = c\,n\log_2 n - cn + n.$$
Since $c \ge 1$, $-cn + n = (1-c)n \le 0$, so $T(n) \le c\,n\log_2 n$. This completes the step, and
$T(n) = O(n\log n)$. $\blacksquare$ (With 19.20's lower bound, $T(n) = \Theta(n\log n)$.) 19.21 † Exponentiation by squaring is logarithmic. $T(n) = T(n/2) + \Theta(1)$ is the same recurrence
as binary search; $E = \log_2 1 = \mathbf{0}$, $f(n) = \Theta(1) = \Theta(n^{\mathbf{0}})$, so it is
Case 2, giving $T(n) = \Theta(\mathbf{\log n})$. 19.23 † Everything wrong. (1) The naive Fibonacci recurrence is subtractive ($n-1$, $n-2$),
not divide-and-conquer, so the Master Theorem does not apply at all. (2) "$b = 1$" is illegal — the
Master Theorem requires $b > 1$ (subproblems must shrink); with $b=1$ they don't shrink. (3) $\log_1 2$
is genuinely undefined (base-1 logs don't exist), which should signal "wrong tool," not "call it linear."
The correct growth rate is $\Theta(\phi^n)$ — exponential — because the recurrence is Fibonacci-like
and grows like $F_n$ itself (Chapter 18's characteristic equation). 19.25 † Error: the two recurrences differ in $a$. Binary search recurses on one half (it throws
the other away), so $a=1$: $T(n) = T(n/2) + \Theta(1)$, which is Case 2 with $E=0$, giving
$\Theta(\log n)$. Merge sort recurses on both halves, so $a=2$: $T(n) = 2\,T(n/2) + \Theta(n)$, Case 2
with $E=1$, giving $\Theta(n\log n)$. "Cuts in half" describes $b=2$ for both, but $a$ (calls made)
differs, and that is what separates $\Theta(\log n)$ from $\Theta(n\log n)$. 19.27 † See Derivation: $\log_2 2 = 1$, $\log_2 1 = 0$; comparisons give 2, 2, 3. The tolerance matters because
19.29 † See 19.31 † See 19.32 † (Model it.) Single lookup, worst case: a binary search costs $\Theta(\log n)$; on a
miss, the $\Theta(n)$ backup query runs. Worst case combines them: $\Theta(\log n) + \Theta(n) =
\Theta(n)$ (the linear fallback dominates). So a single worst-case lookup is **$\Theta(n)$ — the backup
store, not the search, sets the cost.
Batch redesign:** sort $m$ incoming titles with merge sort, $\Theta(m\log m)$, then do $m$ binary-search
lookups against the size-$n$ index, $m\cdot\Theta(\log n) = \Theta(m\log n)$ (assume hits, so no
fallback). Total: $\Theta(m\log m + m\log n)$. When $m \ll n$, $\log n > \log m$, so the $m\log n$ term
dominates: the cost is $\Theta(m\log n)$ — set by the lookups against the large index, not by sorting the
small batch. 19.33 (Model it.) (a) Karatsuba $T(n) = 3T(n/2) + \Theta(n)$: $E = \log_2 3 \approx 1.585$,
$f(n) = n$ polynomially smaller, Case 1, so $T(n) = \Theta(n^{\log_2 3}) \approx \Theta(n^{1.585})$.
(b) Asymptotically Karatsuba ($n^{1.585}$) beats grade-school ($n^2$) for large enough $n$, but for
small $n$ grade-school wins because Karatsuba's hidden constant (extra additions, recursion overhead) is
larger; the crossover is where the constant-factor disadvantage is overcome by the better exponent (in
practice, dozens of digits). The $\Theta$ hides exactly those constants, so it predicts the eventual
winner, not the small-$n$ winner. (c) $T(n) = 2T(n/2)+\Theta(n)$ is merge sort's recurrence,
$\Theta(n\log n)$. Since $n\log n$ grows slower than $n^2$, it would asymptotically beat grade-school
— but no such 2-subproblem multiplication algorithm exists for this problem (you genuinely need 3
half-size products; the "improvement" is fictional). The recurrence math is fine; the algorithm isn't real. 19.34 † (Conjecture and test, then prove.) $G(n) = T(n)/n$ with $T(n)=2T(n/2)+n$, $T(1)=1$, $n=2^k$.
Compute $T$: $T(1)=1, T(2)=4, T(4)=12, T(8)=32, T(16)=80$. Then $G(1)=1, G(2)=2, G(4)=3, G(8)=4, G(16)=5$.
Conjecture: $G(n) = \log_2 n + 1$.
Proof by induction on $n=2^k$. $P(k)$: $G(2^k) = k+1$, equivalently $T(2^k) = 2^k(k+1)$.
Base ($k=0$): $T(1) = 1 = 2^0(0+1)$. ✓
Step: assume $T(2^k) = 2^k(k+1)$. Then $T(2^{k+1}) = 2\,T(2^k) + 2^{k+1} = 2\cdot 2^k(k+1) + 2^{k+1} =
2^{k+1}(k+1) + 2^{k+1} = 2^{k+1}(k+2)$, so $G(2^{k+1}) = k+2 = (k+1)+1$. ✓ $\blacksquare$
Since $G(n) = \log_2 n + 1 \to \infty$, the ratio $T(n)/n$ is unbounded, so $T(n)$ is not $\Theta(n)$
— merge sort is genuinely super-linear (it is $\Theta(n\log n)$). 19.35 (Conjecture and test, then correct.) Test $T(n) = a\,T(n/b) + n$ ($f = n^1$, so $d = 1$):
- $(a,b)=(2,2)$: $E = \log_2 2 = 1 = d$. Case 2 → $\Theta(n\log n)$. (Conjecture holds here.)
- $(a,b)=(4,2)$: $E = \log_2 4 = 2 > d = 1$. Case 1 → $\Theta(n^2)$. (Conjecture fails.)
- $(a,b)=(2,4)$: $E = \log_4 2 = \frac12 < d = 1$. Case 3 → $\Theta(n)$. (Conjecture fails.)
The conjecture is false. Corrected statement: $T(n) = a\,T(n/b) + n$ is $\Theta(n\log n)$ iff
$\log_b a = 1$, i.e. $a = b$ (the tie). If $a > b$ ($E > 1$) it is $\Theta(n^{\log_b a})$ (leaf-dominated);
if $a < b$ ($E < 1$) it is $\Theta(n)$ (root-dominated). 19.36 (Conjecture and test, Fibonacci.) Test $\gcd(F_m, F_n) = F_{\gcd(m,n)}$ for $1\le m,n\le 12$: 19.37 † (Conjecture and test, the doubling ratio.) (a) For $T(n) = \Theta(n^d)$, $\frac{T(2n)}{T(n)}
\to \frac{(2n)^d}{n^d} = 2^d$: $d=1 \Rightarrow 2$; $d=2 \Rightarrow 4$; $d = \log_2 3 \Rightarrow
2^{\log_2 3} = 3$. (b) For $T(n) = n\log_2 n$,
$$\frac{T(2n)}{T(n)} = \frac{2n\log_2(2n)}{n\log_2 n} = 2\cdot\frac{\log_2 n + 1}{\log_2 n} = 2\left(1 +
\frac{1}{\log_2 n}\right),$$
which is $> 2$ but tends to $2$ as $n\to\infty$. (c) Measure the running time at sizes $n$ and $2n$ and
take the ratio: $\approx 2$ (slowly shrinking toward 2) signals linearithmic, $\approx 4$ signals
quadratic. This is a cheap empirical test of complexity class — but, per theme four, it is evidence at
the sizes you tried, not a proof of the asymptotic class. 19.39 † (Ch. 14 + 19.) Slowest to fastest:
$$\Theta(\log n) < \Theta(n) < \Theta(n\log n) < \Theta(n^{\log_2 3}) < \Theta(n^2).$$
Labels: $\Theta(\log n)$ = binary search; $\Theta(n)$ = balanced tree traversal (or a linear scan);
$\Theta(n\log n)$ = merge sort; $\Theta(n^{\log_2 3})\approx\Theta(n^{1.585})$ = Karatsuba multiplication;
$\Theta(n^2)$ = grade-school multiplication / a root-heavy combine. 19.41 † (Ch. 6 + 19.) $T(n) = T(n/2) + 1$, $T(1)=1$, $n=2^k$; guess $T(n) = \log_2 n + 1$.
$P(k)$: "$T(2^k) = k+1$."
Base ($k=0$): $T(1) = 1 = 0+1$. ✓
Step: assume $T(2^k) = k+1$. Then $T(2^{k+1}) = T(2^k) + 1 = (k+1)+1$, matching
$\log_2(2^{k+1})+1 = (k+1)+1$. ✓ $\blacksquare$ So $T(n) = \log_2 n + 1 = \Theta(\log n)$ — confirming
binary search's bound by substitution. 19.43 † (Deep Dive.) Prove $a^{\log_b n} = n^{\log_b a}$. Write $a = b^{\log_b a}$ (definition of
$\log_b$). Then
$$a^{\log_b n} = \left(b^{\log_b a}\right)^{\log_b n} = b^{(\log_b a)(\log_b n)} = b^{(\log_b n)(\log_b a)}
= \left(b^{\log_b n}\right)^{\log_b a} = n^{\log_b a},$$
using $b^{\log_b n} = n$ in the last step. $\blacksquare$
Why it matters: the leaf count is literally $a^{\log_b n}$ (the bottom level has $a$-to-the-height
nodes), but this identity rewrites it as $n^{\log_b a}$ — a clean power of $n$ that can be compared
directly against $f(n)$. That comparison ($f(n)$ vs. $n^{\log_b a}$) is the entire Master-Theorem
trichotomy, which is why $n^{\log_b a}$, not $a^{\log_b n}$, is the quantity in the theorem. 19.45 † (Deep Dive — prove Case 2.) Specialize the tree sum to $f(n) = n^E$, $E = \log_b a$. Work
at level $i$ is
$$a^i f(n/b^i) = a^i \left(\frac{n}{b^i}\right)^E = a^i \cdot \frac{n^E}{(b^E)^i} = n^E \cdot
\frac{a^i}{(b^E)^i}.$$
Now $b^E = b^{\log_b a} = a$, so $\frac{a^i}{(b^E)^i} = 1$. Hence every level does exactly $n^E$ work.
The tree has $\log_b n + 1$ levels, so
$$T(n) = \sum_{i=0}^{\log_b n} n^E = n^E(\log_b n + 1) = \Theta(n^E \log n).$$
That is Master-Theorem Case 2, proved from the recursion-tree sum. $\blacksquare$ (Key step: $b^E = a$
makes the geometric ratio exactly 1 — equal work per level — the hallmark of the tie.) For completeness, the odd-numbered problems not separately daggered are: 19.7 (Part A, above),
19.33 and 19.35 (Part F, above), and 19.39, 19.41, 19.43, 19.45 (Part G, above).
The remaining odd problems coincide with daggered ones and are solved in their parts. These solutions cover the † and odd-numbered problems from Sample space $S = \{1, 2, 3, 4, 5, 6, 7, 8\}$, so $\lvert S \rvert = 8$. The multiples of 3 in $S$ are
$\{3, 6\}$, a 2-element event. By the equally-likely formula,
$$P(\text{multiple of } 3) = \frac{\lvert\{3,6\}\rvert}{\lvert S\rvert} = \frac{2}{8} = \frac14.$$ Standard deck, $\lvert S \rvert = 52$. By inclusion–exclusion (§20.2),
$$P(E \cup F) = P(E) + P(F) - P(E \cap F) = \frac{12}{52} + \frac{13}{52} - \frac{3}{52} = \frac{22}{52} = \frac{11}{26}.$$
Direct check: $E \cup F$ is "face card or heart" $= \{$12 face cards$\} \cup \{$13 hearts$\}$, and the 3
face-card hearts are counted in both, so $\lvert E \cup F\rvert = 12 + 13 - 3 = 22$, giving
$22/52 = 11/26$. ✓ The two methods agree. Four independent rolls; sample space of ordered 4-tuples has $6^4 = 1296$ equally-likely outcomes. "At
least one six" is messy; its complement "no sixes at all" is clean. Each roll avoids six with probability
$5/6$, and the rolls are independent, so
$$P(\text{no sixes}) = \left(\frac56\right)^4 = \frac{625}{1296}.$$
By the complement rule,
$$P(\text{at least one six}) = 1 - \frac{625}{1296} = \frac{671}{1296} \approx 0.518.$$ Claim (monotonicity). If $E \subseteq F$ then $P(E) \le P(F)$. Filled blanks and proof: Claim. For any event $E$, $0 \le P(E) \le 1$. Proof. The lower bound $P(E) \ge 0$ is the non-negativity axiom directly. For the upper bound: every
event satisfies $E \subseteq S$, so by monotonicity (Exercise 20.6) $P(E) \le P(S)$. By the normalization
axiom $P(S) = 1$. Hence $P(E) \le 1$. Combining, $0 \le P(E) \le 1$. $\blacksquare$ Claim (indicator-expectation lemma). For any event $A$, $E[\mathbf{1}_A] = P(A)$. Proof. The indicator $\mathbf{1}_A$ is a random variable taking exactly two values: it equals $1$ on
the outcomes in $A$ and $0$ on the outcomes outside $A$. So its distribution is
$$P(\mathbf{1}_A = 1) = P(A), \qquad P(\mathbf{1}_A = 0) = P(\overline{A}) = 1 - P(A).$$
Applying the definition of expectation $E[X] = \sum_a a\,P(X = a)$ over the two values $a \in \{0, 1\}$,
$$E[\mathbf{1}_A] = 1 \cdot P(\mathbf{1}_A = 1) + 0 \cdot P(\mathbf{1}_A = 0) = 1 \cdot P(A) + 0 = P(A).$$
$\blacksquare$ Claim (union bound, two events). $P(E \cup F) \le P(E) + P(F)$. Proof. By the inclusion–exclusion identity proved in §20.2,
$$P(E \cup F) = P(E) + P(F) - P(E \cap F).$$
The subtracted term $P(E \cap F)$ is the probability of an event, so by the non-negativity axiom
$P(E \cap F) \ge 0$. Subtracting a non-negative quantity can only decrease (or preserve) the value, hence
$$P(E \cup F) = P(E) + P(F) - P(E \cap F) \le P(E) + P(F).$$
$\blacksquare$ The axiom that guarantees the dropped term is non-negative is non-negativity. Claim. A fair coin flipped $n$ times has expected number of heads $E[X] = n/2$. Proof. Let $X_k = \mathbf{1}[\text{flip } k \text{ is heads}]$ for $k = 1, \dots, n$, so that the total
number of heads is $X = X_1 + X_2 + \cdots + X_n$. Each flip is heads with probability $1/2$, so by the
indicator lemma (Exercise 20.8), $E[X_k] = P(\text{flip } k \text{ heads}) = 1/2$. By linearity of
expectation,
$$E[X] = E\!\left[\sum_{k=1}^n X_k\right] = \sum_{k=1}^n E[X_k] = \sum_{k=1}^n \frac12 = n \cdot \frac12 = \frac{n}{2}.$$
$\blacksquare$ Where independence was used: nowhere. Linearity of expectation requires no independence assumption
(§20.4). The flips happen to be independent here, but we never used that fact — only the per-flip
probability $1/2$ and linearity. (Independence would be needed if we were computing the variance of $X$
or a probability like $P(X = k)$, but not for $E[X]$.) Claim. One roll of a fair $n$-sided die (faces $1, \dots, n$) has $E[X] = \frac{n+1}{2}$. Proof. Each face $i \in \{1, \dots, n\}$ has probability $1/n$, so by the definition of expectation,
$$E[X] = \sum_{i=1}^{n} i \cdot \frac1n = \frac1n \sum_{i=1}^{n} i.$$
By the closed form for the first $n$ integers (Chapter 11), $\sum_{i=1}^{n} i = \frac{n(n+1)}{2}$. Hence
$$E[X] = \frac1n \cdot \frac{n(n+1)}{2} = \frac{n+1}{2}.$$
$\blacksquare$ (Sanity check: $n = 6$ gives $7/2 = 3.5$, matching §20.4.) There are 6 pairs summing to 7 — $(1,6),(2,5),(3,4),(4,3),(5,2),(6,1)$ — out of 36, so
$P = 6/36 = 1/6$. The event $X = 5$ has 4 outcomes, so $P(X=5) = 4/36 = 1/9$. The probabilities sum to $1$ because the
events $\{X = a\}$ partition $S$ (their counts add back to $\lvert S\rvert = 36$). That "sums to 1" check
is the first thing to verify about any distribution (§20.3). Hand check (matching §20.4–20.5): $E[X] = \frac{1+2+\cdots+6}{6} = \frac{21}{6} = \frac72$, and
$E[X^2] = \frac{1+4+9+16+25+36}{6} = \frac{91}{6}$, so
$\operatorname{Var}(X) = \frac{91}{6} - \left(\frac72\right)^2 = \frac{91}{6} - \frac{49}{4}
= \frac{182 - 147}{12} = \frac{35}{12}$. The estimate should hover near the exact value from §20.2: for $n = 23$,
$P(\text{shared birthday}) = 1 - \frac{P(365,23)}{365^{23}} \approx 0.5073$. The simulation rounds to
about $0.51$ and is seed-dependent — evidence, not proof (§20.6). The check The flaw: the three "outcomes" — zero heads, one head, two heads — are not equally likely, so the
formula $P(E) = \lvert E\rvert/\lvert S\rvert$ does not apply to that sample space. The equally-likely
formula is valid only when outcomes are equally likely (the explicit hypothesis in §20.2, and the
The fix: build a sample space whose outcomes are equally likely — record both flips in order:
$S = \{HH, HT, TH, TT\}$, four equally-likely outcomes (product rule, $2 \times 2$). In this space, "two
heads" is the single outcome $\{HH\}$, so $P(\text{two heads}) = 1/4$, not $1/3$. The student's bundle
"one head" secretly lumps together two equally-likely outcomes $\{HT, TH\}$, which is why it is twice as
likely as either extreme. There are two equations claimed; one is fine, one is wrong. The error is conflating "$E$ of a product of independent variables" (legal) with "$E$ of a square"
(illegal): $X^2 = X \cdot X$ is a product of $X$ with itself, and a variable is not independent of
itself. The flaw: the argument confuses what linearity of expectation requires. Linearity says
$E[\sum_i X_i] = \sum_i E[X_i]$ for any random variables — independent or not (the The fix: apply linearity directly to the dependent indicators. With $X_i = \mathbf{1}[\text{person } i
\text{ gets own hat}]$ and $X = \sum_{i=1}^n X_i$, each $E[X_i] = 1/n$ by symmetry, and
$$E[X] = \sum_{i=1}^n E[X_i] = n \cdot \frac1n = 1$$
without ever touching the derangement count. The whole point of the hat-check example (§20.4) is that
linearity sidesteps the complicated joint distribution. The claim has the logic exactly backwards. The conclusion (a good coloring exists) is true; the argument is not a proof. Simulation gives evidence, not certainty (§20.6 (Subtle point worth noting: in this specific run, since a good coloring was literally produced, you
could exhibit it and that would prove existence for this one graph — but the stated reasoning, "I ran it
a lot, therefore it exists," is not what does the work, and fails entirely when no example is observed.) Load balancer: $n$ requests, each sent independently and uniformly to one of $m$ servers. (a) The assignment of requests to servers is the outcome — a tuple $(s_1, \dots, s_n)$ with
$s_i \in \{1, \dots, m\}$ the server handling request $i$. The sample space is
$S = \{1, \dots, m\}^n$, with $\lvert S\rvert = m^n$ equally-likely outcomes (product rule). (b) "Server 1 receives no requests" is an event $E_1 \subseteq S$: the set of tuples in which no
coordinate equals $1$, i.e. $E_1 = \{(s_1, \dots, s_n) : s_i \ne 1 \text{ for all } i\}$. (c) "The number of requests server 1 receives" is a random variable $R_1 \colon S \to \mathbb{R}$,
$R_1(s_1, \dots, s_n) = \lvert\{i : s_i = 1\}\rvert$ (a count of coordinates equal to 1). (d) "The number of idle servers" is a random variable $X = \sum_{j=1}^m \mathbf{1}[\text{server }
j \text{ idle}]$, counting how many servers receive zero requests. (Note (b) is the event
$\{R_1 = 0\} = \{\mathbf{1}[\text{server 1 idle}] = 1\}$.) Let $X_j = \mathbf{1}[\text{server } j \text{ is idle}]$ and $X = \sum_{j=1}^m X_j$ = number of idle
servers. Server $j$ is idle iff every one of the $n$ requests missed it; each request misses $j$ with
probability $\frac{m-1}{m}$, and the requests are independent, so
$$E[X_j] = P(\text{server } j \text{ idle}) = \left(\frac{m-1}{m}\right)^n = \left(1 - \frac1m\right)^n.$$
By linearity of expectation (no independence among the $X_j$ needed),
$$E[X] = \sum_{j=1}^m E[X_j] = m\left(1 - \frac1m\right)^n.$$
Specializing to $n = m$: $E[X] = m\left(1 - \frac1m\right)^m$. Since $\left(1 - \frac1m\right)^m \to
\frac1e$ as $m$ grows (a standard limit), $E[X] \approx m/e \approx 0.368\,m$. So with as many requests as
servers, about 37% of servers sit idle — the requests clump, leaving over a third of capacity unused
on average even at "full" load. This is the empty-buckets result of §20.4 in load-balancer clothing. Test (brute force): Conjecture: $E[X] = 1$ for every $n \ge 1$ — independent of $n$. Proof (linearity). Let $X_i = \mathbf{1}[\pi(i) = i]$ for each position $i$, so $X = \sum_{i=1}^n X_i$.
In a uniformly random permutation, position $i$'s image is equally likely to be any of the $n$ values, so
$E[X_i] = P(\pi(i) = i) = 1/n$. By linearity,
$$E[X] = \sum_{i=1}^n E[X_i] = n \cdot \frac1n = 1.$$
$\blacksquare$ The answer does not depend on $n$. The data showed $1$ for $n = 1, \dots, 5$; the proof
settles it for all $n$ (theme four). This is the hat-check problem of §20.4. Test (brute force for $E[Y^2]$ and hence the variance): Conjecture: $\operatorname{Var}(Y) = 1$ for all $n \ge 2$ (and $0$ for $n = 1$, where $Y$ is always
$1$, a constant). Why the variance is not the sum of the indicators' variances. Variance is not linear: in
general $\operatorname{Var}(\sum_i X_i) = \sum_i \operatorname{Var}(X_i) + \sum_{i \ne j}
\operatorname{Cov}(X_i, X_j)$, and the covariance terms vanish only when the variables are uncorrelated.
The fixed-point indicators $X_i$ are not independent — knowing $\pi(i) = i$ slightly changes the odds
that $\pi(j) = j$ (one fewer value and position remain to be matched). Those covariances are exactly what
make the variance come out to the clean value $1$ rather than the naive sum $\sum_i \operatorname{Var}(X_i)
= n \cdot \frac1n\left(1 - \frac1n\right) = 1 - \frac1n$. (Full proof is a Deep Dive; the point of the
exercise is to see that linearity rescued the mean but the variance genuinely needs the dependence.) The theoretical values $m H_m = m\left(1 + \frac12 + \cdots + \frac1m\right)$ are:
$$m=4:\ 4\left(1+\tfrac12+\tfrac13+\tfrac14\right) = 4 \cdot \tfrac{25}{12} = \tfrac{25}{3} \approx 8.33,$$
$$m=6:\ 6 \cdot H_6 = 6 \cdot \tfrac{49}{20} = 14.7, \qquad m=10:\ 10 \cdot H_{10} \approx 29.29.$$
The simulated averages track $m H_m$ closely — evidence (not proof) for the formula. (The proof, by the
way, is itself a linearity-of-expectation argument: the wait to see the $k$-th new coupon is geometric
with mean $\frac{m}{m-k+1}$, and summing those means gives $m H_m$ — a neat sequel to §20.4 once Chapter
21's geometric distribution is in hand.) Compute $P(\text{exactly one pair})$ in a 5-card hand. "Exactly one pair" = two cards of one rank,
and the other three cards of three distinct other ranks (and no two of those three matching, or it would
be two pair / trips). Denominator (total hands): $\lvert S\rvert = \binom{52}{5} = 2{,}598{,}960$ — the number of 5-card
subsets (combinations, Chapter 16). Numerator (favorable hands), by the product rule: So favorable $= 13 \cdot 6 \cdot 220 \cdot 64 = 1{,}098{,}240$, and
$$P(\text{exactly one pair}) = \frac{1{,}098{,}240}{2{,}598{,}960} \approx 0.4226.$$
Each factor comes from the product rule (independent successive choices); $\binom{4}{2}$, $\binom{12}{3}$
are combinations because suit-of-the-pair and the set of other ranks are unordered selections. Claim (law of total probability, two-part form).
$P(E) = P(E \cap F) + P(E \cap \overline{F})$. Proof. By set algebra (Chapter 8), $F$ and $\overline{F}$ partition the sample space, so every outcome
of $E$ lies in exactly one of $E \cap F$ or $E \cap \overline{F}$. Concretely,
$$E = E \cap S = E \cap (F \cup \overline{F}) = (E \cap F) \cup (E \cap \overline{F}),$$
using the distributive law for sets. The two pieces are disjoint: $(E \cap F) \cap (E \cap
\overline{F}) \subseteq F \cap \overline{F} = \emptyset$. By the additivity axiom (§20.2),
$$P(E) = P\big((E \cap F) \cup (E \cap \overline{F})\big) = P(E \cap F) + P(E \cap \overline{F}).$$
$\blacksquare$ This decomposition — splitting an event by whether $F$ also occurred — is the structural
backbone of Chapter 21's conditional probability and Bayes' theorem. Let $a_n$ = number of length-$n$ head/tail strings with no two consecutive heads. Recurrence. Classify such a string by its last symbol. If it ends in T, the first $n-1$ symbols
are any valid length-$(n-1)$ string: $a_{n-1}$ of them. If it ends in H, the symbol before it must be
T (no two consecutive heads), and the first $n-2$ symbols are any valid length-$(n-2)$ string:
$a_{n-2}$ of them. These cases are disjoint and exhaustive (sum rule, Chapter 15), so
$$a_n = a_{n-1} + a_{n-2}.$$
Initial values: $a_1 = 2$ (strings "H", "T"), $a_2 = 3$ ("HT", "TH", "TT" — not "HH"). This is the
Fibonacci recurrence (Chapters 18–19) shifted: $a_1 = 2, a_2 = 3, a_3 = 5, a_4 = 8$. Probability for $n = 4$. The full sample space of 4 flips has $2^4 = 16$ equally-likely strings, and
$a_4 = 8$ of them avoid consecutive heads, so
$$P(\text{no two consecutive heads in 4 flips}) = \frac{a_4}{2^4} = \frac{8}{16} = \frac12.$$
(The counts $a_n$ are Fibonacci numbers $F_{n+2}$ with $F_1 = F_2 = 1$; e.g. $a_4 = 8 = F_6$.) Setup. $m$ blocks, each a 4-element subset of the items; color every item red/blue by an independent
fair coin. For a fixed block $B$ of $k = 4$ items,
$$P(B \text{ monochromatic}) = 2 \cdot 2^{-k} = 2^{1-k} = 2^{1-4} = 2^{-3} = \frac18$$
(all four red, probability $2^{-4}$, or all four blue, probability $2^{-4}$; disjoint, so add). Union bound over the $m$ blocks (§20.6):
$$P(\text{some block monochromatic}) \le m \cdot 2^{1-k} = \frac{m}{8}.$$ Threshold. This bound is $< 1$ exactly when $\frac{m}{8} < 1$, i.e. $m < 8$, which is the general
condition $m < 2^{k-1} = 2^{4-1} = 8$. When $m \le 7$,
$$P(\text{no block monochromatic}) = 1 - P(\text{some block monochromatic}) \ge 1 - \frac{m}{8} > 0,$$
so a good coloring exists. Conclusion: for any collection of $m \le 7$ four-element blocks, there is a
red/blue coloring of the items with no monochromatic block. (At $m = 8$ the bound gives only $\le 1$,
which is not informative — the method guarantees existence for $m \le 7$.) Even-numbered, non-daggered exercises (20.2, 20.4, 20.26, 20.28, 20.30, and the others) are left for the
reader, per the book's convention; the methods above cover every technique they require. 21.1 Face cards = 12 (4 jacks, 4 queens, 4 kings); kings among them = 4. Conditioning on "face card"
shrinks the sample space from all 52 cards to those 12. So $P(\text{king}\mid\text{face card}) =
\frac{|\text{king}\cap\text{face}|}{|\text{face}|} = \frac{4}{12} = \frac{1}{3}$. The unconditional
$P(\text{king}) = \frac{4}{52} = \frac{1}{13}$ is much smaller — learning "it's a face card" raised the
probability because it eliminated the 40 non-face cards. 21.2 † (a) $P(C\mid\overline{A}) = \frac{P(C\cap\overline{A})}{P(\overline{A})} = \frac{0.15}{0.60} =
0.25$. (b) $P(\overline{A}\mid C) = \frac{P(\overline{A}\cap C)}{P(C)} = \frac{0.15}{0.25} = 0.60$.
(c) Both use the same corner cell $0.15$, but (a) divides by the organic row total ($0.60$) while (b)
divides by the converted column total ($0.25$); different denominators give different answers — the
$P(A\mid B)\ne P(B\mid A)$ asymmetry. 21.3 Multiplication rule: $P(A\cap B) = P(A\mid B)P(B) = P(B\mid A)P(A)$. The two products are equal
because each equals the same quantity $P(A\cap B)$ (the intersection doesn't care which event you condition
on first), which is the definition of conditional probability rearranged. With $P(A) = 0.3$ and $P(B\mid A)
= 0.5$: $P(A\cap B) = 0.5\times0.3 = 0.15$. 21.4 † Product test: $P(A)P(B) = 0.5\times0.4 = 0.20 = P(A\cap B)$, so $A$ and $B$ are independent.
Conditional check: $P(A\mid B) = \frac{P(A\cap B)}{P(B)} = \frac{0.20}{0.40} = 0.5 = P(A)$ ✓ — same
conclusion. 21.5 Sample space $\{1,\dots,6\}$. $A=\{2,4,6\}$, $B=\{2,3\}$, $A\cap B=\{2\}$. $P(A\mid B) =
\frac{P(A\cap B)}{P(B)} = \frac{1/6}{2/6} = \frac{1}{2}$. $P(B\mid A) = \frac{1/6}{3/6} = \frac{1}{3}$.
Since $\frac12\ne\frac13$, the two conditionals differ. Independence check: $P(A\cap B) = \frac16$ but
$P(A)P(B) = \frac12\cdot\frac13 = \frac16$ — equal, so $A$ and $B$ are independent (even though the two
conditionals differ in value; independence says $P(A\mid B) = P(A) = \frac12$, which holds). 21.6 † Base rate = $0.001$ (the unconditional disease prevalence $P(D)$); sensitivity = $0.99$ (the
likelihood $P(T\mid D)$); posterior = $0.0902$ (the updated belief $P(D\mid T)$ after a positive test). A
patient who tested positive cares about the posterior $P(D\mid T) \approx 0.09$, not the test's
accuracy $P(T\mid D) = 0.99$ — those are reverse conditionals, and the base rate is what separates them. 21.7 Without replacement, $P(\text{both red}) = P(\text{first red})\,P(\text{second red}\mid\text{first
red}) = \frac{3}{8}\cdot\frac{2}{7} = \frac{6}{56} = \frac{3}{28}$. The draws are dependent because
removing a red marble changes the composition of the bag: $P(\text{second red}) = \frac27$ given a first
red, versus $\frac38$ unconditionally — the first draw's outcome shifts the second draw's probability. 21.8 † Blanks, in order: the first denominator is $P(A\cap B)$, so $P(A\cap B\cap C) = P(C\mid A\cap
B)\cdot P(A\cap B)$. The expansion is $P(A\cap B) = P(B\mid A)\cdot P(A)$. Substituting,
$P(A\cap B\cap C) = P(C\mid A\cap B)\,P(B\mid A)\,P(A)$. (Each step is $P(X\mid Y) = P(X\cap Y)/P(Y)$
rearranged into a product.) ∎ 21.9 $P(A\cap\overline{B}) = P(A) - P(A\cap B) = P(A) - P(A)P(B)$ (using independence) $= P(A)(1 -
P(B)) = P(A)P(\overline{B})$. So $A$ and $\overline{B}$ satisfy the product rule and are independent. ∎ 21.10 † Assume $P(A\mid B) > P(A)$. By the multiplication rule, $P(A\cap B) = P(A\mid B)P(B) >
P(A)P(B)$. But also $P(A\cap B) = P(B\mid A)P(A)$. So $P(B\mid A)P(A) > P(A)P(B)$, and dividing by
$P(A) > 0$ gives $P(B\mid A) > P(B)$. Relevance is symmetric: if $B$ raises belief in $A$, then $A$ raises
belief in $B$. ∎ 21.11 Suppose $A,B$ are both mutually exclusive and independent. Mutual exclusivity gives $P(A\cap B) =
0$; independence gives $P(A\cap B) = P(A)P(B)$. Hence $P(A)P(B) = 0$, so $P(A) = 0$ or $P(B) = 0$ —
contradicting $0 < P(A) < 1$ and $0 < P(B) < 1$. Therefore no such pair exists. ∎ 21.12 † Since $A_1,A_2,A_3$ partition the sample space, the events $B\cap A_1, B\cap A_2, B\cap A_3$
are pairwise disjoint and their union is $B$ (every outcome of $B$ lies in exactly one $A_i$). By
additivity (Ch. 20), $P(B) = P(B\cap A_1) + P(B\cap A_2) + P(B\cap A_3)$. Applying the multiplication rule
to each term, $P(B\cap A_i) = P(B\mid A_i)P(A_i)$ (defined since each $P(A_i) > 0$), so
$P(B) = \sum_{i=1}^{3} P(B\mid A_i)P(A_i)$. ∎ 21.13 By definition $P(A_j\mid B) = \frac{P(A_j\cap B)}{P(B)}$. The numerator is $P(A_j\cap B) =
P(B\mid A_j)P(A_j)$ (multiplication rule). The denominator, by the law of total probability over the
partition $\{A_i\}$ (21.12), is $P(B) = \sum_{i=1}^{n} P(B\mid A_i)P(A_i)$. Substituting both gives
$P(A_j\mid B) = \frac{P(B\mid A_j)P(A_j)}{\sum_{i=1}^{n} P(B\mid A_i)P(A_i)}$. ∎ 21.14 † Let $Q(A) = P(A\mid B) = \frac{P(A\cap B)}{P(B)}$ with $P(B) > 0$.
(i) $P(A\cap B) \ge 0$ and $P(B) > 0$, so $Q(A) \ge 0$.
(ii) $Q(S) = \frac{P(S\cap B)}{P(B)} = \frac{P(B)}{P(B)} = 1$ (since $S\cap B = B$).
(iii) For disjoint $A_1,A_2$: $(A_1\cup A_2)\cap B = (A_1\cap B)\cup(A_2\cap B)$, and these are disjoint, so
$P((A_1\cup A_2)\cap B) = P(A_1\cap B) + P(A_2\cap B)$; dividing by $P(B)$ gives $Q(A_1\cup A_2) = Q(A_1) +
Q(A_2)$. All three axioms hold, so $Q$ is a genuine probability measure — "the world where $B$ happened"
is its own probability space. ∎ 21.15 † 21.17 † 21.19 † 21.21 † The error: disjoint events are not independent in general. Disjointness ($P(A\cap B) = 0$) is
about events that cannot co-occur; independence ($P(A\cap B) = P(A)P(B)$) is about events that carry no
information about each other. Counterexample with $P(A) = P(B) = 0.3$ disjoint: $P(A\cap B) = 0$ but
$P(A)P(B) = 0.09 \ne 0$, so the product test fails — they are dependent. (In fact learning $A$
occurred tells you $B$ definitely did not.) 21.23 † Summing likelihoods is not naive Bayes: naive Bayes multiplies per-feature likelihoods (and
the prior), corresponding to $P(c)\prod_i P(f_i\mid c)$ under conditional independence. A sum corresponds
to no coherent probabilistic quantity — it is not a probability of anything. Concretely, a sum lets one
large likelihood dominate and ignores how many features point a given way; e.g., an email with one
strong spam word but ten mild ham words could be scored spam by the sum (one big term) while the product
correctly down-weights it (ten ham factors compound). The sum also loses the zero-frequency veto that the
product (correctly, before smoothing) imposes. 21.25 † Events: $K$ = "headline is clickbait," $W$ = "headline contains 'shocking'." Numbers: prior
$P(K) = 0.05$ (so $P(\overline{K}) = 0.95$); likelihoods $P(W\mid K) = 0.60$ and $P(W\mid\overline{K}) =
0.04$. Asked for the posterior $P(K\mid W)$. Exact Bayes expression:
$$P(K\mid W) = \frac{P(W\mid K)P(K)}{P(W\mid K)P(K) + P(W\mid\overline{K})P(\overline{K})} =
\frac{0.60\cdot0.05}{0.60\cdot0.05 + 0.04\cdot0.95}.$$
(Evaluating, for reference: $\frac{0.03}{0.03 + 0.038} = \frac{0.03}{0.068} \approx 0.441$ — even a strong
"shocking" signal leaves it more likely not clickbait, because clickbait is rare.) 21.27 † Per-round error (calling a composite "prime") is $\le \frac14$ since the test catches a
composite with probability $\ge \frac34$. After $k$ independent rounds the error is $\le (\frac14)^k =
2^{-2k}$. Require $2^{-2k} < 2^{-80}$, i.e. $2k > 80$, i.e. $k > 40$. So **$k = 41$ rounds suffice**
($2^{-82} < 2^{-80}$). This is exactly the repeated-trial amplification that lets RSA key generation
(Part IV) certify large primes to cryptographic confidence. 21.29 † Outcomes (equally likely): HH, HT, TH, TT. $A$ = first heads = $\{HH, HT\}$, $B$ = second
heads = $\{HH, TH\}$, $C$ = match = $\{HH, TT\}$. Each has probability $\frac12$. Pairwise products:
$P(A\cap B) = P(\{HH\}) = \frac14 = \frac12\cdot\frac12 = P(A)P(B)$ ✓; $P(A\cap C) = P({HH}) = \frac14 =
P(A)P(C)$ ✓; $P(B\cap C) = P({HH}) = \frac14 = P(B)P(C)$ ✓ — so the three are pairwise independent.
But $P(A\cap B\cap C) = P(\{HH\}) = \frac14$, while $P(A)P(B)P(C) = \frac18$; since $\frac14 \ne \frac18$
they are not mutually independent. (Conjecture from code confirmed.) Conclusion: pairwise independence
does not imply mutual independence — exactly the §21.2 warning. Intuitively, any two of $A,B,C$ leave
the third uncertain, but knowing any two determines the third (e.g., $A$ and $B$ both true forces the
coins to match, so $C$ follows with certainty). 21.31 † Averaging random-pivot comparison counts on sorted input over 1000 runs gives roughly:
$n=10 \approx 24$, $n=20 \approx 67$, $n=40 \approx 175$, $n=80 \approx 420$ (values vary slightly by RNG).
Compare to $n^2$ ($100, 400, 1600, 6400$) and to $n\log_2 n$ ($\approx 33, 86, 213, 506$). The averages
track $n\log_2 n$ closely and fall far below $n^2$ — roughly doubling $n$ multiplies the count by about
$2.4$–$2.5$ (consistent with the extra $\log$ factor), not by $4$ (which $n^2$ would give). This supports
the chapter's $O(n\log n)$-in-expectation theorem (§21.6) and shows random pivoting escapes the
deterministic $\Theta(n^2)$ worst case even on the input that would trigger it. 21.33 † Conditioning on $B$ = even $= \{2,4,6\}$ shrinks the space to those three equally likely
outcomes. $E[X\mid B] = \frac{2+4+6}{3} = 4$. Unconditionally, $E[X] = \frac{1+2+\cdots+6}{6} = 3.5$. The
conditional expectation is higher because restricting to even faces drops the small odd values $1,3,5$ and
keeps the larger ones. 21.35 † Linearity of expectation: for any random variables $X_1,\dots,X_m$ (and constants
$a_i$), $E[\sum_i a_i X_i] = \sum_i a_i E[X_i]$ — it requires no independence assumption whatsoever.
Therefore $E[X] = E[\sum_{i 21.37 † $P(B) = P(B\mid S_1)P(S_1) + P(B\mid S_2)P(S_2) + P(B\mid S_3)P(S_3) = (0.01)(0.5) + (0.02)(0.3)
+ (0.05)(0.2) = 0.005 + 0.006 + 0.010 = 0.021$. Then $P(S_3\mid B) = \frac{P(B\mid S_3)P(S_3)}{P(B)} =
\frac{0.010}{0.021} \approx 0.476$. Server 3 handles only $20\%$ of traffic (lowest base rate) yet is the
most likely culprit given a failure, because its failure likelihood ($0.05$) is so much higher than the
others' that the product (likelihood × base rate) is largest. Bayes trades the low routing probability
against the high failure rate, and here the failure rate wins. 22.1 † (a) $7 \mid 56$ means $56 = 7k$ with witness $k = 8$. (b) $a \mid 0$ means $0 = ak$ with
witness $k = 0$ (valid because $a \neq 0$). (c) $13 \mid n$ means $n = 13k$ for some integer $k$. 22.3 † (a) $100 = 7 \cdot 14 + 2$, so $q = 14$, $r = 2$ ($0 \le 2 < 7$). (b)
$-100 = 7 \cdot (-15) + 5$, so $q = -15$, $r = 5$ (note $r$ is the non-negative canonical remainder,
not $-2$). (c) $7 = 100 \cdot 0 + 7$, so $q = 0$, $r = 7$. Each satisfies $a = dq + r$ with
$0 \le r < d$. 22.5 † $\gcd$ takes the minimum exponent of each shared prime: from $720 = 2^4 3^2 5$ and
$168 = 2^3 3 \cdot 7$, the shared primes are $2$ (min $3$) and $3$ (min $1$), so
$\gcd = 2^3 \cdot 3 = 24$. $\operatorname{lcm}$ takes the maximum:
$\operatorname{lcm} = 2^4 3^2 5 \cdot 7 = 5040$. Check via the identity:
$\gcd \cdot \operatorname{lcm} = 24 \cdot 5040 = 120960 = 720 \cdot 168 = ab$. ✓ 22.7 † $a \mid a$: by definition we need an integer $k$ with $a = ak$; take $k = 1$, an integer,
so $a \mid a$. $1 \mid b$: we need $k$ with $b = 1 \cdot k$; take $k = b$, an integer, so $1 \mid b$.
Both follow immediately from supplying the witness (the "repackage" step). $\blacksquare$ 22.9 † Blanks: $b = aj$ (witness $j$); $a = bk$ (witness $k$); substituting,
$a = (aj)k = a(jk)$; cancelling $a \neq 0$ gives $jk = 1$; the only integer pairs with product $1$ are
$(j,k) = (1,1)$ and $(j,k) = (-1,-1)$, so $k = \pm 1$, hence $a = bk = \pm b$. $\blacksquare$ 22.11 † Assume $a \mid b$, so $b = aj$ for some integer $j$. Then for any integer $c$,
$bc = (aj)c = a(jc)$. Since $jc$ is an integer, $a \mid bc$ (witness $jc$). The finishing "repackage"
move is recognizing $a(jc)$ as $a$ times an integer. $\blacksquare$ 22.13 † Suppose for contradiction only finitely many primes are $\equiv 3 \pmod 4$, listed
$p_1, \dots, p_r$ (note $3$ itself is on the list; $2$ is not, being even). Let
$N = 4(p_1 p_2 \cdots p_r) - 1$. Then $N \equiv -1 \equiv 3 \pmod 4$, and $N$ is odd, so $2 \nmid N$.
Every odd prime is $\equiv 1$ or $\equiv 3 \pmod 4$. If all prime factors of $N$ were $\equiv 1
\pmod 4$, their product $N$ would be $\equiv 1 \pmod 4$ (a product of numbers $\equiv 1$ stays
$\equiv 1$) — contradicting $N \equiv 3$. So $N$ has a prime factor $q \equiv 3 \pmod 4$. But $q$
cannot be on the list: if it were, $q \mid p_1 \cdots p_r$, hence $q \mid 4(p_1 \cdots p_r)$, and since
$q \mid N$ the linear-combination lemma gives $q \mid \big(4(p_1\cdots p_r) - N\big) = 1$ — impossible
for a prime. So $q$ is a new prime $\equiv 3 \pmod 4$, contradicting the finite list. Hence there are
infinitely many primes $\equiv 3 \pmod 4$. $\blacksquare$ 22.14 † See 22.15 † See 22.16 † See 22.17 † See 22.18 † The flaw: Euclid's Lemma requires $a$ to be prime; the claim drops that hypothesis. For
composite $a$ it can fail. Smallest counterexample: $a = 4$, with $b = 2$, $c = 6$: $4 \mid 12 = 2\cdot 6$,
yet $4 \nmid 2$ and $4 \nmid 6$. 22.19 The error is forgetting that every integer combination $6x + 9y$ is a multiple of
$\gcd(6,9) = 3$ (since $3 \mid 6$ and $3 \mid 9$, the linear-combination lemma forces $3 \mid (6x+9y)$).
So no combination can equal $1$ or $2$; the smallest positive value is $3$, and Bézout says that
smallest positive value is the gcd. Therefore $\gcd(6,9) = 3$, not $1$. The reasoning confused
"reaching a small value like $3$" with "reaching $1$"; you cannot get below the gcd. 22.20 † The proof concludes only that $N$ has a prime divisor not on the list — it never claims
$N$ is itself prime. Concretely, with the first six primes,
$N = 2\cdot 3\cdot 5\cdot 7\cdot 11\cdot 13 + 1 = 30031 = 59 \times 509$, which is composite. Euclid's
construction manufactures a new prime ($59$, say) dividing $N$, contradicting any "complete" finite
list; it does not assert $N$ is prime. 22.21 Two errors. (i) "$0$ divides everything" is false: by the definition (which requires the
divisor to be nonzero, and the equation $b = 0 \cdot k = 0$ to hold), $0$ divides only $0$, not every
integer. (ii) The convention/computation for $\gcd(a, 0)$: every integer divides $0$, so the common
divisors of $a$ and $0$ are exactly the divisors of $a$, the greatest of which is $\lvert a\rvert$.
Hence $\gcd(a, 0) = \lvert a\rvert$ for $a \neq 0$ (and $\gcd(0,0) = 0$ by separate convention), not
$0$. 22.22 † Model the teeth as multiples: gear A returns a given mark to contact every $252$ teeth,
gear B every $198$ teeth. (a) Both marks realign at the first common multiple of $252$ and $198$ — the
lcm: $\operatorname{lcm}(252,198) = (252\cdot 198)/\gcd(252,198) = 49896/18 = 2772$ teeth. (b) The
gcd $= 18$ governs the structure of repeated relative positions (it is the spacing that divides both
counts); the system's distinct relative configurations number $\operatorname{lcm}/(\text{tooth}) $ and
the shared period structure is set by $\gcd(252,198) = 18$. Definitions used: lcm (smallest common
multiple) for realignment, gcd (largest common divisor) for the shared period. 22.23 (a) Users $u_1, u_2$ collide on the same shard iff $u_1 \bmod k = u_2 \bmod k$, which by the
Division Algorithm is equivalent to $k \mid (u_1 - u_2)$. (b) For $k = 12$, a collision happens exactly
when $12 \mid (u_1 - u_2)$. Among all possible differences, exactly the multiples of $12$ cause a
collision; since one in every twelve consecutive integers is a multiple of $12$, about $1/12$ of
id-differences collide — i.e. with $12$ shards and well-spread ids, each shard receives roughly $1/12$
of the load. (Chapter 23 will phrase this as $u_1 \equiv u_2 \pmod{12}$.) 22.24 † (a) A square tile of side $s$ tiles the floor with no cutting and flush edges iff $s$
divides both $1071$ and $462$; the largest such $s$ is the gcd. Euclidean algorithm:
$1071 = 2\cdot 462 + 147$, $462 = 3\cdot 147 + 21$, $147 = 7\cdot 21 + 0$, so $\gcd(1071,462) = 21$ cm.
(b) Number of tiles: $(1071/21)\times(462/21) = 51 \times 22 = 1122$. (c) "Flush to both walls, no
cutting" means $s$ must divide each side length exactly — i.e. $s \mid 1071$ and $s \mid 462$, which
is precisely the definition of a common divisor; "largest tile" then selects the greatest common
divisor. 22.25 † (a)/(b) See 22.27 † (a)/(b) See 22.28 † Strong induction is forced because when $n = ab$ splits, the factors $a$ and $b$ can be
any size in $(1, n)$ — not specifically $n - 1$. Ordinary induction only grants the hypothesis for the
immediately preceding value $P(n-1)$, which need not equal $P(a)$ or $P(b)$. To apply the hypothesis to
arbitrary smaller factors, you must assume the statement for all integers strictly between $1$ and
$n$ — precisely the strong-induction hypothesis. The exact step is "by the inductive hypothesis each of
$a, b$ is a product of primes," which requires the hypothesis at $a$ and $b$, not just at $n-1$. 22.29 Euclid (infinitude of primes): (i) the contradicted assumption is "there are only finitely
many primes," so they can be listed $p_1, \dots, p_r$; (ii) the contradiction is that
$N = p_1\cdots p_r + 1$ forces a prime $q$ with $q \mid 1$, impossible since every prime is $\ge 2$.
FTA uniqueness: (i) the contradicted assumption is "some integer $> 1$ has two distinct prime
factorizations," and one takes the smallest such integer $n$; (ii) cancelling a shared prime (found
via Euclid's Lemma) yields a smaller integer $n/p_1$ with two distinct factorizations, contradicting
the minimality of $n$. 22.30 † Write each odd number via the Division Algorithm with $d = 2$: an integer is odd iff it has
the form $2q + 1$ (remainder $1$). Let $m = 2s + 1$ and $n = 2t + 1$ be odd. Then
$$mn = (2s+1)(2t+1) = 4st + 2s + 2t + 1 = 2(2st + s + t) + 1,$$
which is of the form $2q + 1$ with $q = 2st + s + t$ — i.e. $mn$ leaves remainder $1$ on division by
$2$, so $2 \nmid mn$ and $mn$ is odd. $\blacksquare$ 22.31 Euclid's Lemma as a formula:
$$\forall p\, \forall a\, \forall b\, \Big[\big(\text{prime}(p) \land p \mid ab\big) \rightarrow
\big(p \mid a \lor p \mid b\big)\Big],$$
quantifiers ranging over integers (with $\text{prime}(p)$ the primality predicate). The statement that
fails for a composite $m$ is the same shape with the primality hypothesis removed:
$\big(m \mid ab\big) \rightarrow \big(m \mid a \lor m \mid b\big)$, whose negation (the counterexample
pattern of 22.18) is $\exists m\, \exists a\, \exists b\, \big[m \mid ab \land m \nmid a \land m \nmid
b\big]$ — witnessed by $m = 4, a = 2, b = 6$. 22.32 † Assume $\gcd(a, n) = 1$. By Bézout's identity (§22.5) there are integers $x, y$ with
$ax + ny = 1$. Rearranging, $ax - 1 = -ny = n(-y)$, so $n \mid (ax - 1)$, i.e. $ax \equiv 1 \pmod n$;
thus $x$ is a modular inverse of $a$. The term that "vanishes mod $n$" is $ny$, a multiple of $n$.
Conversely, if $\gcd(a, n) = d > 1$, then $d \mid (ax + ny)$ for all integers $x, y$ (linear
combination), so $ax + ny$ is always a multiple of $d > 1$ and can never equal $1$; hence no $x$
satisfies $ax \equiv 1 \pmod n$, and no inverse exists. $\blacksquare$ 22.33 Claim: the Euclidean algorithm halts in $O(\log(\min(a,b)))$ division steps. Key fact: in
any two consecutive steps the smaller argument at least halves. Suppose a step has $a \ge b > 0$ and
produces $r = a \bmod b$ (so the next pair is $(b, r)$, and the one after starts from $r$). We show
$r < a/2$, splitting on $b$:
- If $b \le a/2$: then $r < b \le a/2$ directly (the remainder is below the divisor).
- If $b > a/2$: then $\lfloor a/b\rfloor = 1$ (since $a < 2b$), so $r = a - b < a - a/2 = a/2$. Either way $r < a/2$. Thus the first component is more than halved every two steps, so after
$2\log_2(\min(a,b))$ steps the argument drops below $1$ and the algorithm stops: the step count is
$O(\log(\min(a,b)))$. The bound is tight — consecutive Fibonacci numbers (Exercise 22.25) realize the
worst case, since the golden-ratio growth of $F_n$ means $\gcd(F_{n+1}, F_n)$ needs $\Theta(n) =
\Theta(\log F_n)$ steps. $\blacksquare$ 23.1 † Divisibility test $n \mid (a-b)$. (a) $38 - 2 = 36 = 9 \cdot 4$, so $9 \mid 36$ — yes.
(b) $-4 - 11 = -15 = 5 \cdot (-3)$, so $5 \mid (-15)$ — yes. (c) $100 - 0 = 100$, and $7 \nmid 100$
(since $7 \cdot 14 = 98$, remainder $2$) — no. 23.3 † The cycle of $7^k \bmod 10$ is $7, 9, 3, 1$ for $k \equiv 1, 2, 3, 0 \pmod 4$. (a) $53 \bmod
4 = 1 \Rightarrow$ last digit $\mathbf{7}$. (b) $200 \bmod 4 = 0 \Rightarrow \mathbf{1}$. (c) $4001
\bmod 4 = 1 \Rightarrow \mathbf{7}$. 23.5 † In the back-substitution $1 = 3\cdot 5 - 2\cdot 7 = 3(26 - 3\cdot 7) - 2\cdot 7 = 3\cdot 26 -
11\cdot 7$, the coefficient of $7$ first appears as $-2$ (in the term "$- 2\cdot 7$"). After the full
back-substitution it becomes $-11$, and reducing modulo $26$ gives $-11 + 26 = \mathbf{15}$ — which is
$7^{-1} \bmod 26$. 23.7 † Blanks, in order: (1) "an inverse"; (2) "$c^{-1}$"; (3) "$c^{-1}c \equiv \mathbf{1}
\pmod n$"; (4) the simplified conclusion is "$a \equiv b \pmod n$." Full statement: since $\gcd(c,n)=1$,
$c$ has an inverse $c^{-1}$ (Thm 23.3); multiplying $ca \equiv cb$ by $c^{-1}$ gives $c^{-1}ca \equiv
c^{-1}cb$, and as $c^{-1}c \equiv 1$ this is $a \equiv b \pmod n$. $\blacksquare$ 23.9 † Suppose $a \equiv b \pmod n$, so $a = b + nk$ for some integer $k$. By the Chapter 22 fact
$\gcd(x, n) = \gcd(x - nk,\, n)$ (subtracting a multiple of $n$ from one argument leaves the gcd
unchanged), $\gcd(a, n) = \gcd(b + nk,\, n) = \gcd(b, n)$. $\blacksquare$ 23.11 † Suppose $x^2 \equiv 1 \pmod p$, i.e. $p \mid (x^2 - 1) = (x-1)(x+1)$. Since $p$ is prime,
Euclid's lemma (Chapter 22) gives $p \mid (x-1)$ or $p \mid (x+1)$, i.e. $x \equiv 1 \pmod p$ or
$x \equiv -1 \equiv p - 1 \pmod p$. So $1$ and $p-1$ are the only self-inverse residues. $\blacksquare$ 23.13 † Apply Theorem 23.4. For $6x \equiv 4 \pmod 8$: $g = \gcd(6,8) = 2$ and $2 \mid 4$, so two
solutions. Divide by $2$: $3x \equiv 2 \pmod 4$; $3^{-1} \equiv 3 \pmod 4$, so $x_0 \equiv 3\cdot 2 = 6
\equiv 2 \pmod 4$. Lift to mod $8$: $x \equiv 2$ and $x \equiv 6$, i.e. **`[2, 6]`**. For $4x \equiv 3
\pmod 6$: $g = \gcd(4,6) = 2$ and $2 \nmid 3$, so 23.15 † Brute-force totient over $n = 1, \dots, 12$ gives
$[\,1, 1, 2, 2, 4, 2, 6, 4, 6, 4, 10, 4\,]$. At the primes the value is $p-1$: $\phi(2)=1,\ \phi(3)=2,\
\phi(5)=4,\ \phi(7)=6,\ \phi(11)=10$ ✓ (matching §23.6). And $\phi(12) = 4$ (units ${1,5,7,11}$) ✓.
(Convention: $\phi(1) = 1$, since $\gcd(1,1)=1$.) 23.17 † The cancellation is illegal because $2$ is not invertible modulo $6$ ($\gcd(2,6) = 2
\ne 1$, so by Theorem 23.3 it has no inverse). §23.2's pitfall: you may cancel a factor only when it is
a unit. Indeed the "conclusion" $4 \equiv 1 \pmod 6$ is false ($4 - 1 = 3$, and $6 \nmid 3$). 23.19 † The flaw: the moduli $4$ and $6$ are not coprime ($\gcd(4,6) = 2 \ne 1$), so the
hypothesis of Theorem 23.5 fails and the CRT does not guarantee a unique solution mod $24$. The
right modulus to reason about is $\operatorname{lcm}(4,6) = 12$. A solution does happen to exist here:
$x \equiv 1 \pmod 4$ and $x \equiv 3 \pmod 6$ are both satisfied by $x = 9$ (and $9 \bmod 4 = 1$,
$9 \bmod 6 = 3$), so $x \equiv 9 \pmod{12}$ — unique modulo $12$, not modulo $24$. (For other
right-hand sides, e.g. $x \equiv 0 \pmod 4,\ x \equiv 1 \pmod 6$, no solution exists at all, because the
two conditions disagree modulo $\gcd = 2$.) 23.21 † A "coincidence shot" $x$ is a multiple of $6$ and a multiple of $10$:
$$x \equiv 0 \pmod 6 \qquad\text{and}\qquad x \equiv 0 \pmod{10}.$$
The moduli are not coprime ($\gcd(6,10) = 2$), so the CRT does not apply directly; instead a number
divisible by both $6$ and $10$ is exactly a multiple of $\operatorname{lcm}(6,10)$. Since
$\operatorname{lcm}(6,10) = \frac{6\cdot 10}{\gcd(6,10)} = \frac{60}{2} = 30$, the first coincidence
is at shot $30$, and they recur every $30$ shots. The period is $\operatorname{lcm} = 30$, not the
product $60$, because the shared factor $2$ is counted only once (Theorem 23.4 / §23.4): the product
over-counts the common factor, exactly the uniqueness subtlety from the CRT proof. 23.23 † Tabulating $2^{n-1} \bmod n$ for $n = 3, \dots, 20$: it equals $1$ at every prime
($3,5,7,11,13,17,19$) by Fermat, and is $\ne 1$ at every composite in that range. Conjecture: maybe
$2^{n-1} \equiv 1 \pmod n$ characterizes primes. It does not. The smallest composite $n$ with
$2^{n-1} \equiv 1 \pmod n$ is $\mathbf{341} = 11 \cdot 31$ (a Fermat pseudoprime to base $2$). So
"$2^{n-1} \equiv 1 \pmod n$" is a necessary but not sufficient condition for primality: it lets a
few composites masquerade as primes, which is why real primality tests (Miller–Rabin) strengthen it with
extra checks and multiple bases. This is theme four in action — code reveals the counterexample at
$n = 341$ that a hasty conjecture would miss. 23.25 † Extended Euclidean on $240, 46$:
$$240 = 5\cdot 46 + 10,\quad 46 = 4\cdot 10 + 6,\quad 10 = 1\cdot 6 + 4,\quad 6 = 1\cdot 4 + 2,\quad
4 = 2\cdot 2.$$
So $\gcd(240, 46) = 2$. Back-substitute:
$$2 = 6 - 4 = 6 - (10 - 6) = 2\cdot 6 - 10 = 2(46 - 4\cdot 10) - 10 = 2\cdot 46 - 9\cdot 10
= 2\cdot 46 - 9(240 - 5\cdot 46) = 47\cdot 46 - 9\cdot 240.$$
Thus $240\cdot(-9) + 46\cdot 47 = 2$ (check: $46\cdot 47 = 2162$, $240\cdot 9 = 2160$, difference $2$),
giving $s = -9,\ t = 47$. Since $\gcd(240, 46) = 2 \ne 1$, $46$ has no inverse modulo $240$ (Theorem
23.3). 23.27 † Claim: $a^p \equiv a \pmod p$ for all integers $a \ge 0$ and prime $p$. Proof by
induction on $a$ (Chapter 6). Base ($a = 0$): $0^p = 0 \equiv 0 \pmod p$. ✓ Inductive step:
assume $a^p \equiv a \pmod p$ (the hypothesis). By the freshman's dream, $(a+1)^p \equiv a^p + 1
\pmod p$ — because every interior binomial coefficient $\binom{p}{k}$, $0 < k < p$, is divisible by the
prime $p$ (Chapter 17), so all middle terms of $(a+1)^p$ vanish modulo $p$. Apply the inductive
hypothesis $a^p \equiv a$: $(a+1)^p \equiv a^p + 1 \equiv a + 1 \pmod p$, which is the claim at
$a + 1$. By induction it holds for all $a \ge 0$. $\blacksquare$ 23.29 † (a) Each of $p, q$ has roughly half the bit-length of $n = pq$. Modular exponentiation costs
grow faster than linearly in operand size (multiplying two $k$-bit numbers is at least $\Theta(k)$ work,
and there are $\Theta(\log e)$ of them per §23.5 / Exercise 23.28), so two half-size exponentiations are
cheaper than one full-size one — in practice about a $4\times$ speed-up. (b) Working modulo the prime
$p$, Fermat gives $c^{\,p-1} \equiv 1 \pmod p$ (for $p \nmid c$), so $c^d \equiv c^{\,d \bmod (p-1)}
\pmod p$ — one may reduce the huge exponent $d$ modulo $p-1$ first, shrinking the exponentiation. (c)
24.1 † (a) $(\mathbb{Z}_9, +)$: elements $\{0,1,2,3,4,5,6,7,8\}$, order 9. (b)
$\mathbb{Z}_9^* = \{a : \gcd(a,9)=1\} = \{1,2,4,5,7,8\}$, order $\phi(9) = 6$. (c)
$\mathbb{Z}_{15}^* = \{1,2,4,7,8,11,13,14\}$ (coprime to $15 = 3\cdot 5$), order $\phi(15) = 8$. 24.3 † (a) In $\mathbb{Z}_8^*$: need $5x\equiv 1 \pmod 8$. $5\cdot 5 = 25 \equiv 1$, so $5^{-1} = 5$
(it is its own inverse). (b) In $\mathbb{Z}_{11}^*$: $5\cdot 9 = 45 = 44 + 1 \equiv 1 \pmod{11}$, so
$5^{-1} = 9$. (Both exist because $\gcd(5,8) = \gcd(5,11) = 1$.) 24.5 † The subgroup generated by $2$ in $(\mathbb{Z}_{10},+)$ is all multiples of $2$ mod 10:
$\{0, 2, 4, 6, 8\}$. Its order is 5, and $5 \mid 10$ ✓, consistent with Lagrange. 24.7 † Blanks: left-multiply by $a^{-1}$, which exists by the inverses axiom. Left side:
$(a^{-1}\ast a)\ast b = e \ast b = b$, using associativity then identity. Right side becomes
$(a^{-1}\ast a)\ast c = e\ast c = c$. Hence $b = c$. $\blacksquare$ — In a general ring, multiplicative
cancellation fails because nonzero elements need not be invertible: in $\mathbb{Z}_6$, $2\cdot 1 = 2 =
2\cdot 4$ (since $2\cdot 4 = 8\equiv 2$) but $1 \ne 4$; the culprit is that $2$ is a zero divisor, not a
unit. 24.9 † Claim: $(a^{-1})^{-1} = a$. By definition $a^{-1}$ satisfies $a^{-1}\ast a = a\ast a^{-1}
= e$. Reading that same pair of equations the other way, $a$ is an element whose product with $a^{-1}$
(on both sides) is $e$ — i.e. $a$ is an inverse of $a^{-1}$. By the uniqueness of inverses
(Theorem 24.1), $a^{-1}$ has exactly one inverse, so $(a^{-1})^{-1} = a$. $\blacksquare$ 24.11 † Claim: a field has no zero divisors. Suppose $x\cdot y = 0$ in a field $F$ with $x\ne 0$.
Since $F$ is a field, $x$ has a multiplicative inverse $x^{-1}$. Multiply both sides on the left by
$x^{-1}$: $\,x^{-1}(x\cdot y) = x^{-1}\cdot 0$. The left side is $(x^{-1}x)y = 1\cdot y = y$ (by
associativity and the inverse/identity axioms); the right side is $0$ (anything times $0$ is $0$). Hence
$y = 0$. $\blacksquare$ 24.13 † Trace 24.15 † 24.17 † The flaw: the "proof" never checks the inverses axiom, which is exactly the one that
fails. $(\mathbb{Z}_6, \cdot)$ — all of $\mathbb{Z}_6$ under multiplication — is not a group. The
element $0$ has no inverse (no $x$ with $0\cdot x = 1$), and even among nonzero elements, $2$ has no
inverse: $2\cdot x \pmod 6$ ranges over $\{0,2,4\}$, never $1$ (because $2\cdot 3 = 6\equiv 0$ makes $2$
a zero divisor). Correct conclusion: you must restrict to the units $\mathbb{Z}_6^* = \{1,5\}$ to get a
group. 24.19 † The flaw: "taking square roots" is not a valid group operation — there is no general
"$\sqrt{\ }$" in a group, and an equation $a^2 = e$ can have several solutions. Counterexample: in
$(\mathbb{Z}_6, +)$ (written additively, so "$a^2$" means $a + a = 2a$), the element $3$ satisfies
$3 + 3 = 6 \equiv 0 = e$ but $3 \ne 0$. (Multiplicatively, in $\mathbb{Z}_8^*$, $3^2 = 9\equiv 1$ yet
$3\ne 1$.) So $a^2 = e$ does not force $a = e$; elements of order 2 are exactly the counterexamples. 24.21 † Model. The twelve notes under "transpose up by $k$" form the group $(\mathbb{Z}_{12}, +)$:
notes are residues $0$–$11$, transposition is addition mod 12, identity is $0$ (transpose by nothing),
and inverses exist (transpose down). It is abelian of order 12. (i) Repeatedly transposing by $k$ visits
all twelve notes iff $k$ generates $\mathbb{Z}_{12}$, which happens iff $\gcd(k,12)=1$ — i.e.
$k\in\{1,5,7,11\}$. (ii) Otherwise $k$ generates the proper subgroup $\langle k\rangle$ of order
$12/\gcd(k,12)$ and gets "stuck" hitting only those notes (e.g. $k=3$ hits only $\{0,3,6,9\}$). The
circle of fifths corresponds to $k = 7$ (a perfect fifth is 7 semitones; since $\gcd(7,12)=1$, it
cycles through all twelve keys). 24.23 † Conjecture and test, then prove. Computing for $p = 3,5,7,11,13$ gives
$\lvert\mathbb{Z}_p^*\rvert = 2,4,6,10,12$ — i.e. $\lvert\mathbb{Z}_p^*\rvert = p-1$ — and every
element's order turns out to be a divisor of $p-1$. Proof of the count: for prime $p$, every
$a\in\{1,\dots,p-1\}$ satisfies $\gcd(a,p)=1$, so all $p-1$ nonzero residues are units; thus
$\lvert\mathbb{Z}_p^*\rvert = \phi(p) = p-1$. Why orders divide $p-1$: the powers of any element $a$
form a cyclic subgroup $\langle a\rangle$ whose size is the order of $a$; by Lagrange, that size
divides $\lvert\mathbb{Z}_p^*\rvert = p-1$. Hence the only possible element orders are the divisors of
$p-1$. 24.25 † Conjecture and test, then prove. Computing $a^{(p-1)/2}\bmod p$ gives $1$ when $a$ is a
nonzero square (quadratic residue) mod $p$ and $p-1\equiv -1$ when it is a non-residue — that is Euler's
criterion (the full statement; previews quadratic residues). Proof of the weaker fact: for $p\nmid a$,
let $y = a^{(p-1)/2}$. Then $y^2 = a^{p-1} \equiv 1\pmod p$ by Corollary 24.3 applied to
$\mathbb{Z}_p^*$ (order $p-1$). So $y^2 - 1 \equiv 0$, i.e. $(y-1)(y+1)\equiv 0\pmod p$. Since
$\mathbb{Z}_p$ is a field (Theorem 24.4) it has no zero divisors, so $y\equiv 1$ or $y\equiv -1$.
Hence $a^{(p-1)/2}\equiv \pm 1\pmod p$. $\blacksquare$ 24.27 † In $\mathrm{GF}(2^3)$ with $x^3\equiv x+1$, compute $(x^2+1)(x+1)$:
$$(x^2+1)(x+1) = x^3 + x^2 + x + 1.$$
Reduce $x^3\to x+1$: $\;(x+1) + x^2 + x + 1 = x^2 + (x+x) + (1+1) = x^2$ (since $x+x=0$ and $1+1=0$ in
characteristic 2). So $(x^2+1)(x+1) = x^2$ (bits $100$). 24.29 † Fermat's little theorem is Corollary 24.3 in the group $G = \mathbb{Z}_p^*$. That group
has order $\lvert\mathbb{Z}_p^*\rvert = p-1$. Corollary 24.3 ("$a^{\lvert G\rvert} = e$ in any finite
group") applied here reads $a^{p-1}\equiv 1\pmod p$ for every $a\in\mathbb{Z}_p^*$ (i.e. $p\nmid a$) —
which is exactly Fermat's little theorem. 24.31 † $\mathbb{Z}_{20}^* = \{a \mid 1\le a\le 19,\ \gcd(a,20)=1\} = \{1,3,7,9,11,13,17,19\}$;
cardinality $\phi(20) = \phi(4)\phi(5) = 2\cdot 4 = 8$. By Lagrange, every element's order divides
$\lvert\mathbb{Z}_{20}^*\rvert = 8$, so the only possible element orders are the divisors of $8$:
$1, 2, 4, 8$. 24.33 † Direct proof of Corollary 24.3 for finite abelian $G = \{g_1,\dots,g_N\}$. Fix
$a\in G$. The map $\mu\colon g\mapsto a\ast g$ is a bijection of $G$ onto itself: it is injective by
left-cancellation ($a\ast g = a\ast g' \Rightarrow g = g'$, Exercise 24.7) and, being an injection on a
finite set to itself, surjective. So $\{a\ast g_1,\dots,a\ast g_N\}$ is just a reordering of
$\{g_1,\dots,g_N\}$, and the products are equal:
$$\prod_{i=1}^{N}(a\ast g_i) = \prod_{i=1}^{N} g_i.$$
Now use commutativity to pull all $N$ copies of $a$ to the front of the left-hand product:
$\prod_i (a\ast g_i) = a^N \ast \prod_i g_i$. Therefore $a^N \ast \prod_i g_i = \prod_i g_i$, and
cancelling the (invertible) element $\prod_i g_i$ gives $a^N = e$. $\blacksquare$ Commutativity is used
precisely in the step that gathers the $a$'s; for non-abelian groups this trick fails and one needs the
Lagrange argument of the chapter. 24.35 A ring with zero divisors like $\mathbb{Z}_{256}$ cannot replace the field
$\mathrm{GF}(2^8)$ in Reed–Solomon coding because decoding must divide — e.g. solving the
error-locator/value equations requires inverting field elements, and in a ring a nonzero element with no
inverse (a zero divisor) cannot be divided out. By Exercise 24.11 a field has no zero divisors, so every
nonzero coefficient is invertible and the linear algebra of decoding always has a solution; in
$\mathbb{Z}_{256}$ the zero divisors (e.g. $2\cdot 128 \equiv 0$) make the decoding equations singular.
This is the §24.4 "fields let you divide; rings may not" lesson at the level of error correction. Even-numbered and non-daggered exercises (24.2, 24.4, 24.6, 24.8, 24.10, 24.12, 24.14, 24.16, 24.18,
24.20, 24.22, 24.24, 24.26, 24.28, 24.30, 24.32, 24.34) are intentionally left for the reader; full
keys live in the instructor solutions. Conventions: letters numbered $A=0,\dots,Z=25$; reduce mod $n$ after every multiplication. 25.1 † Shift cipher, key $k = 7$, on So 25.3 † The substitution-cipher key is a permutation of 26 letters, so the key space has size $26!
\approx 4.03\times 10^{26}$, about $\log_2(26!) \approx 88$ bits (both figures are from §25.1). 25.5 † $\phi(55) = (5-1)(11-1) = 40$. Check $ed \bmod 40$ for $e=3, d=27$: $3\cdot 27 = 81 = 2\cdot 40
+ 1$, so $ed \equiv 1 \pmod{40}$. ✓ This guarantees $(m^{e})^{d} = m^{ed} \equiv m \pmod{n}$ — decryption
inverts encryption for every message (the §25.5 theorem). 25.6 † (Scaffolded main-case proof.) Blanks filled:
- $ed = \mathbf{1 + k\phi(n)}$ for some integer $k \ge 0$ (definition of congruence).
- $(m^e)^d = m \cdot \big(\mathbf{m^{\phi(n)}}\big)^{k}$.
- Since $\gcd(m,n)=1$, Euler's theorem gives $m^{\phi(n)} \equiv \mathbf{1} \pmod n$.
- Substituting, $(m^e)^d \equiv m \cdot 1^k = \mathbf{m} \pmod n$. $\blacksquare$ The one hypothesis used was $\gcd(m,n)=1$ (Euler's requirement). §25.5 adds a second proof because a
message can be a multiple of $p$ or $q$ (so $\gcd(m,n)\ne1$); the general case handles every $m$ by
working mod $p$ and mod $q$ with Fermat and gluing with CRT. 25.7 Decryption: $m' \equiv c - k = (m + k) - k = m \pmod{26}$, so $m' = m$ since both lie in
$\{0,\dots,25\}$. The property used is that every element of $\mathbb{Z}_{26}$ has an additive inverse
($-k$); adding $k$ then $-k$ is the identity (the §25.1 25.8 † (Theorem direction of RSA security.) Suppose the attacker can factor $n = pq$. Then:
1. From $p, q$ compute $\phi(n) = (p-1)(q-1)$ (Chapter 23 totient formula).
2. Public $e$ is known and satisfies $\gcd(e,\phi(n))=1$, so its inverse exists.
3. Compute $d = e^{-1} \bmod \phi(n)$ with the extended Euclidean algorithm (Chapter 22/23). This is the private exponent, so the attacker can now decrypt any ciphertext. Hence factoring $\Rightarrow$
breaking RSA. (The converse — that breaking RSA requires factoring — is the unproven §25.6
assumption.) $\blacksquare$ 25.9 (Existence of $d$ iff $\gcd(e,\phi(n))=1$.)
($\Leftarrow$) If $\gcd(e,\phi(n))=1$, Bézout's identity (Chapter 22) gives integers $d, t$ with
$ed + t\,\phi(n) = 1$, i.e. $ed \equiv 1 \pmod{\phi(n)}$; reduce $d$ mod $\phi(n)$ to get the inverse.
($\Rightarrow$) If an inverse $d$ exists, then $ed \equiv 1 \pmod{\phi(n)}$, so $ed - 1 = t\,\phi(n)$ for
some integer $t$, i.e. $ed - t\,\phi(n) = 1$. Any common divisor of $e$ and $\phi(n)$ divides the left
side, hence divides $1$, so $\gcd(e,\phi(n)) = 1$. (This is the Chapter 23 modular-inverse existence
criterion.) $\blacksquare$ 25.10 † (Coprime divisors.) If $p \mid x$ and $q \mid x$ with $\gcd(p,q)=1$, then $pq \mid x$:
write $x = p a$; since $q \mid pa$ and $\gcd(p,q)=1$, Euclid's lemma gives $q \mid a$, so $a = qb$ and
$x = pq\,b$, i.e. $pq \mid x$. Applied with $x = m^{ed}-m$, this is the final §25.5 CRT step.
Counterexample without coprimality: take $p = q = 2$ and $x = 6$. Then $2 \mid 6$ (twice over), but
$pq = 4 \nmid 6$. The implication "$p\mid x$ and $q\mid x \Rightarrow pq \mid x$" fails when $p,q$ share a
factor — which is exactly why RSA requires $p \ne q$ distinct primes, so $\gcd(p,q)=1$ and the gluing
is valid. 25.11 † (See Decrypting the encryption returns 25.13 † RSA round-trip $m=9$, $p=5$, $q=11$, $e=3$ ($n=55$, $\phi=40$, $d=27$). Encrypt: $9^3 = 729 =
13\cdot 55 + 14$, so $c = 14$. Decrypt: $14^{27}\bmod 55 = 9$ by §25.5 (verify via CRT: $14\equiv4\pmod5$,
$4^{27}\equiv(-1)^{27}\equiv4$, and $9\bmod5=4$ ✓; $14\equiv3\pmod{11}$, $3^{27}\equiv3^{7}\equiv9$, and
$9\bmod11=9$ ✓). 25.15 † The claim conflates correctness with security. §25.5 proves only that decryption
inverts encryption ($(m^e)^d\equiv m$) — a property for the legitimate parties, and a genuine theorem.
It says nothing about an attacker. §25.6 says RSA's security rests on an assumption (factoring is
hard), not a theorem: it is proven that factoring ⇒ breaking RSA (Ex. 25.8), but it is only conjectured
that there is no feasible break without factoring. So RSA is not "proven secure" — its security is a
hardness assumption. 25.17 † A large key space stops only brute force; it does nothing against structure. The
substitution cipher maps each plaintext letter to a fixed ciphertext symbol, so the language's letter
frequencies survive encryption (e.g. 25.19 † (Model it — key distribution.)
(a) Pairwise symmetric keys: $\binom{1500}{2} = \dfrac{1500\cdot 1499}{2} = 1{,}124{,}250$ keys.
(b) Doubling to $3000$: $\binom{3000}{2} = \dfrac{3000\cdot 2999}{2} = 4{,}498{,}500$, a factor of
$4{,}498{,}500 / 1{,}124{,}250 \approx 4.0$. Because $\binom{n}{2}\approx n^2/2$, doubling $n$ quadruples
the count.
(c) With public-key crypto there is no shared secret to pre-distribute: each device publishes one
public key and keeps one private key, so the total is $n$ key pairs (1500, then 3000) — linear, not
quadratic. The $\binom{n}{2}$ explosion disappears because nobody needs a secret agreed in advance with
anybody. 25.21 † (Conjecture and test, then prove — determinism.)
(a) Encrypt $m=0,\dots,10$ under $(55,3)$ via $m^3 \bmod 55$: (Hand: $4^3=64\equiv9$; $5^3=125\equiv15$; $6^3=216\equiv51$; $7^3=343\equiv13$; $8^3=512\equiv17$;
$9^3=729\equiv14$; $10^3=1000\equiv10$.)
(b) Every ciphertext is distinct, so the conjecture is: textbook RSA encryption is injective on
$\{0,\dots,n-1\}$.
(c) Proof. Suppose $m_1^e \equiv m_2^e \pmod n$. Raise both sides to $d$: $(m_1^e)^d \equiv (m_2^e)^d
\pmod n$. By §25.5, $(m_i^e)^d \equiv m_i$, so $m_1 \equiv m_2 \pmod n$; since $0\le m_1,m_2 < n$, $m_1 =
m_2$. Hence encryption is injective. $\blacksquare$
(d) Injectivity is necessary for decryption to be well-defined (two messages with the same ciphertext
could not both be recovered). But injectivity is deterministic — the same $m$ always gives the same $c$
— and that lets an attacker who can guess the message re-encrypt and confirm it (§25.6). Necessary for
correctness, fatal for security; the fix is randomized padding. 25.23 † (Ch. 24 + 25.)
(a) $(\mathbb{Z}_{n},+,\times)$ with $n = pq$ is not a field: $p\cdot q \equiv 0 \pmod{pq}$ with
$p,q\not\equiv 0$, so $p$ is a zero divisor and has no multiplicative inverse.
(b) $\mathbb{Z}_n$ is a field iff $n$ is prime (Chapter 24).
(c) The units modulo $n$ form a group of order $\phi(n) = (p-1)(q-1)$; Lagrange's theorem (Chapter
24), equivalently Euler's theorem (Chapter 23), gives $m^{\phi(n)} \equiv 1$ for every unit $m$. 25.25 † (Ch. 23 + 25 — fast modular exponentiation.) Decrypt $c=13$, $d=27$, $n=55$. Repeated
squaring mod 55:
$$13^{1}\equiv 13,\ 13^{2}=169\equiv 4,\ 13^{4}=16,\ 13^{8}=256\equiv 36,\ 13^{16}=1296\equiv 31.$$
Binary $27 = 16+8+2+1$, so $13^{27} = 13^{16}\cdot 13^{8}\cdot 13^{2}\cdot 13^{1} \equiv 31\cdot 36\cdot
4\cdot 13$. Reduce after each multiply: $31\cdot 36 = 1116 \equiv 16$ ($1116 = 20\cdot 55 + 16$); $16\cdot
4 = 64 \equiv 9$; $9\cdot 13 = 117 \equiv 7$ ($117 = 2\cdot 55 + 7$). So $m = 7$. ✓ (matches §25.4). 25.27 (Ch. 23 + 25 — CRT decryption, Deep Dive.) $p=5,q=11,n=55,d=27,c=13$.
(a) Mod 5: $13\equiv 3$; by Fermat shrink the exponent $d_p = 27 \bmod 4 = 3$, so $m_p = 3^{3} = 27
\equiv 2 \pmod 5$. **Mod 11:** $13\equiv 2$; $d_q = 27 \bmod 10 = 7$, so $m_q = 2^{7} = 128 \equiv 7
\pmod{11}$ ($128 = 11\cdot 11 + 7$).
(b) CRT: find $m$ with $m\equiv 2\pmod 5$ and $m\equiv 7\pmod{11}$, $0\le m<55$. Test $m=7$:
$7\bmod 5 = 2$ ✓, $7\bmod 11 = 7$ ✓. So $m = 7$.
(c) This matches the slow $13^{27}\bmod 55 = 7$ of Ex. 25.25. It is the computational realization of the
§25.5 general-case proof ("compute mod $p$, compute mod $q$, glue with CRT"), which is also how real
RSA gets its ~$4\times$ decryption speedup. 26.1 † $100 = 13\cdot 7 + 9$, so $100 \bmod 13 = 9$. $200 = 13\cdot 15 + 5$ (since $13\cdot15=195$),
so $200 \bmod 13 = 5$. $300 = 13\cdot 23 + 1$ (since $13\cdot23=299$), so $300 \bmod 13 = 1$. The three
slots are $9, 5, 1$ — all distinct, so no collisions among these three. 26.3 † (a) $\alpha = n/m = 1000/250 = 4$. (b) By the generalized pigeonhole principle some slot holds
at least $\lceil n/m \rceil = \lceil 4 \rceil = 4$ keys. 26.5 † $11010$ in the convention $b_{4}x^{4} + b_{3}x^{3} + b_{2}x^{2} + b_{1}x + b_{0}$ with bits
$1,1,0,1,0$ is $x^{4} + x^{3} + x$. The generator $G = 101$ is $x^{2} + 1$. 26.7 † Augment $M = 10110$ with $r = 3$ zeros: $10110\,000$. XOR long division by $G = 1011$: The first XOR ($1011 \oplus 1011 = 0000$) clears the top four bits, and the remaining bits are already
$0$, so division stops. Remainder $R = 000$, transmitted frame $M$-then-$R = 10110\,000 = 10110000$.
(Check: $M(x)\,x^3 = x^7+x^5+x^4 = x^4(x^3+x+1) = x^4\,G(x)$, exactly divisible, so the remainder is $0$.) 26.9 † Horner with reduction mod $101$ at each step, codes $c_0=2,c_1=1,c_2=4$, base $b=31$:
- start $h = 2$;
- $h = (2\cdot 31 + 1) \bmod 101 = 63 \bmod 101 = 63$;
- $h = (63\cdot 31 + 4) \bmod 101 = 1957 \bmod 101$. Since $101\cdot 19 = 1919$, $1957 - 1919 = 38$. So $h(\texttt{bad}) = 38$. (Reducing each step gives the same result as computing $2\cdot31^2 + 1\cdot31 +
4 = 1922 + 31 + 4 = 1957$ and reducing once — the Chapter 23 rule.) 26.11 † (a) Bursts of length $\le 12$ are guaranteed detected: such an error polynomial has degree
$< 12 = \deg G$, and a nonzero polynomial of degree less than $\deg G$ cannot be divisible by $G$. (b)
Yes — a single-bit error is $E(x) = x^i$, and a $G$ with $\ge 2$ nonzero terms cannot divide a lone power
of $x$, so all single-bit errors are caught. (c) $G$ must have the factor $x + 1$; then every odd-weight
error $E$ has $E(1) = 1 \ne 0$, so $x+1 \nmid E$ and the error is detected (the polynomial version of the
parity argument). 26.12 † (Scaffolded parity proof — filled blanks.)
- Setup. By construction the transmitted block has an even number of 1-bits, so its count mod 2 is
0.
- Effect of one flip. One flip changes the count by exactly $\pm 1$ (one), so the count mod 2
toggles.
- Effect of $e$ flips. After $e$ flips the parity has toggled $e$ times, so the final parity equals
the original iff $e$ is even.
- Conclusion. The receiver detects an error exactly when the parity differs from the expected value,
i.e. exactly when $e$ is odd; an even number of flips $\ge 2$ is therefore missed (undetected).
$\blacksquare$ 26.13 Let the $m$ slots $\{0,\dots,m-1\}$ be the boxes and the keys of $U$ be the objects placed in
them (key $k$ goes in box $h(k)$). There are $\lvert U\rvert$ objects and $m < \lvert U\rvert$ boxes.
By the pigeonhole principle, since the number of objects exceeds the number of boxes, some box contains
at least two objects — i.e., some slot receives two distinct keys $k_1 \ne k_2$ with $h(k_1) = h(k_2)$.
That is a collision. $\blacksquare$ (No contradiction needed: pigeonhole is applied directly.) 26.15 ($\Rightarrow$, contrapositive) If $G(x)$ has fewer than two nonzero terms it is either $0$ or a
single monomial $x^t$. If $G = x^t$ then $G \mid x^i$ for every $i \ge t$, so the single-bit error $E(x) =
x^i$ (with $i \ge t$) is divisible by $G$ and undetected — not all single-bit errors are caught.
($\Leftarrow$) If $G$ has at least two nonzero terms, then for any $i$, $G \nmid x^i$: a divisor of the
monomial $x^i$ over $\mathbb{Z}_2$ must itself be a power of $x$ (the only factors of $x^i$ are $x^0,\dots,
x^i$), but $G$ has a nonzero constant or lower term in addition to its top term, so $G$ is not a pure power
of $x$. Hence $G \nmid x^i = E(x)$ for every single-bit error, and all single-bit errors are detected.
Therefore all single-bit errors are detected iff $G$ has $\ge 2$ nonzero terms. $\blacksquare$ 26.17 † Implementation: 26.19 † 26.21 † The flaw: the function is not allowed to depend on which 50 keys arrive — a hash function
$h\colon U \to \{0,\dots,63\}$ is fixed in advance over the whole universe $U$ of $2^{64}$ keys. The
pigeonhole principle compares $\lvert U\rvert = 2^{64}$ to $m = 64$ slots, and since $2^{64} > 64$, some
two keys in $U$ collide. The argument confuses "$50 < 64$" (true, but irrelevant: that would only matter
if you could rechoose $h$ after seeing the 50 keys) with the universe size. A perfect hash for a
known, fixed set of 50 keys does exist — but that is a different problem (the keys are known first); for
an arbitrary stream into a fixed function, collisions are guaranteed (§26.2). 26.23 † The flaw: digest length is not the relevant difference — the missing property is
collision/preimage resistance (§26.6). CRC-32 is linear over $\mathbb{Z}_2$ and uses a public,
keyless generator, so an attacker who alters the file can recompute the matching CRC-32 (or craft changes
that preserve it). File size is irrelevant: the attack is recomputing the checksum, which is cheap for a
file of any size. A cryptographic hash like SHA-256 is not "a longer CRC"; it is a different object whose
output an attacker cannot shortcut to forge. (For tamper-detection, prefer a keyed HMAC.) 26.25 † Modeling the dedup system:
- Key universe $U$: all possible audio files (arbitrary byte strings) — effectively infinite.
- Function: the fingerprint $h\colon U \to \{0,1\}^{d}$ (a $d$-bit digest); codomain: the $2^{d}$
possible digests.
- Collision here: two different songs $x_1 \ne x_2$ with $h(x_1) = h(x_2)$ — the system would
wrongly treat them as the same file and store only one, losing the other's data (or serving the wrong
track).
- Required property: collision resistance (and second-preimage resistance) — a malicious user must
not be able to craft a junk file $x_2$ with the same digest as a target copyrighted file $x_1$ to alias
it. An ordinary (fast, public) hash fails this; a cryptographic hash is required.
- Pigeonhole vs. reliance: pigeonhole guarantees collisions exist ($\lvert U\rvert > 2^d$). The
system does not rely on their nonexistence (impossible); it relies on collisions being infeasible to
find — a cryptographic-hash property, not a pigeonhole one (§26.2 vs. §26.6). 26.27 † (a) Slot counts for $h(k)=k \bmod m$ on consecutive keys $0,\dots,n-1$:
- $(n,m)=(10,5)$: keys $0..9$, each residue $0,1,2,3,4$ hit exactly twice → counts $[2,2,2,2,2]$, longest
$= 2$.
- $(12,5)$: keys $0..11$; residues $0,1$ get keys $\{0,5,10\},\{1,6,11\}$ (three each), residues $2,3,4$
get two each → counts $[3,3,2,2,2]$, longest $= 3$.
- $(13,5)$: keys $0..12$; residues $0,1,2$ get three each, $3,4$ get two each → counts $[3,3,3,2,2]$,
longest $= 3$. Conjecture: the longest chain is $\lceil n/m \rceil$. (b) Test: any further pair, e.g. $(n,m)=(11,5)$ should give counts $[3,2,2,2,2]$ (longest $3 = \lceil
11/5\rceil$) and $(20,5)$ should give $[4,4,4,4,4]$ (longest $4$). A single output where some slot exceeds
$\lceil n/m\rceil$ — say a longest chain of $4$ when $\lceil n/m\rceil = 3$ — would refute it. (c) Proof. Key $k$ (for $0 \le k \le n-1$) lands in slot $k \bmod m$. Slot $s$ receives exactly the keys
$k \in \{0,\dots,n-1\}$ with $k \equiv s \pmod m$, i.e. $k \in {s, s+m, s+2m, \dots} \cap {0,\dots,
n-1}$. The count for slot $s$ is the number of integers $t \ge 0$ with $s + tm \le n-1$, which is
$\lfloor (n-1-s)/m \rfloor + 1$. Writing $n = qm + \rho$ with $0 \le \rho < m$: slots $s = 0,\dots,\rho-1$
receive $q+1$ keys and slots $s = \rho,\dots,m-1$ receive $q$ keys. The maximum is $q+1$ when $\rho > 0$
and $q$ when $\rho = 0$ — in both cases exactly $\lceil n/m \rceil$. Hence the longest chain is
$\lceil n/m \rceil$, the best possible spread (it meets the generalized-pigeonhole lower bound with
equality). $\blacksquare$ 26.29 † (a) $h_{3,4}(k) = ((3k+4)\bmod 17)\bmod 5$. For $k=2$: $3\cdot2+4 = 10$, $10 \bmod 17 = 10$,
$10 \bmod 5 = 0$, so $h_{3,4}(2) = 0$. For $k=9$: $3\cdot9+4 = 31$, $31 \bmod 17 = 14$, $14 \bmod 5 = 4$,
so $h_{3,4}(9) = 4$. (b) Because $17$ is prime and $a = 3 \ne 0$, multiplication by $3$ is a bijection on
$\mathbb{Z}_{17}$ (Chapter 23: a unit times a complete residue system is a complete residue system), and
adding $b=4$ is also a bijection; so $k \mapsto (3k+4)\bmod 17$ is a bijection on $\{0,\dots,16\}$,
hitting each residue exactly once. 26.31 † Suppose $jd \equiv 0 \pmod m$ for some $0 < j < m$, with $m$ prime and $m \nmid d$. Then
$m \mid jd$. Since $m$ is prime, $m \mid jd$ forces $m \mid j$ or $m \mid d$ (Euclid's lemma, Chapter 22).
But $0 < j < m$ means $m \nmid j$, and by hypothesis $m \nmid d$ — contradiction. Hence $jd \not\equiv 0
\pmod m$ for all $0 < j < m$. Therefore the values $0\cdot d, 1\cdot d, \dots, (m-1)d$ are pairwise
distinct mod $m$ (if $id \equiv jd$ with $i>j$ then $(i-j)d \equiv 0$ with $0 < i-j < m$, impossible), so
they are all $m$ residues; the sequence $k, k+d, k+2d,\dots$ cycles through every slot before repeating.
$\blacksquare$ 26.33 † A CRC's guarantee is a theorem: given the generator $G$, every burst of length $\le
\deg G$ is detected, because such an error polynomial cannot be divisible by $G$ — this is settled by
algebra and no future algorithm can make an undetected short burst appear. A cryptographic hash's security
is an assumption (a belief about computational cost): collisions provably exist (pigeonhole), and we
merely assume no adversary can find one within feasible time. The cryptographic-hash guarantee
could be broken by a faster collision-finding algorithm without any new mathematics (indeed, this is
how MD5 and SHA-1 fell). The CRC's burst guarantee cannot be broken by a faster algorithm — it is not
about feasibility at all, but about divisibility, which is absolute. The contrast is exactly §26.6's:
detection of accidents rests on theorems; defense against adversaries rests on infeasibility. Even-numbered, non-daggered exercises (26.2, 26.4, 26.6, 26.8, 26.10, 26.14, 26.16, 26.18, 26.20, 26.22,
26.24, 26.26, 26.28, 26.30, 26.32, 26.34) are left for the reader; brief keys: 26.8 weighted sum of
$0,3,0,6,4,0,6,1,5$ with weights $1..9$ is $0+6+0+24+20+0+42+8+45=145$; $145 = 11\cdot13+2$, so $145\equiv
2$, need $2+10\,d_{10}\equiv0$, $10\,d_{10}\equiv9$, multiply by $10^{-1}=10$: $d_{10}\equiv90\equiv2$, so
check digit 2. 26.16 evaluate $E(x)$ at $x=1$ in $\mathbb{Z}_2$: $E(1)=$ (number of 1-terms) mod 2 $=$
parity of flips; $(x+1)\mid E \Rightarrow E(1)=0 \Rightarrow$ even flips, so any odd-flip error has
$E(1)=1$, is not divisible by $x+1$ (hence not by $G$), and is detected. 27.1 † $V = \{p, q, r, s\}$ and $E = \{\{p,q\}, \{q,r\}, \{r,s\}, \{s,p\}\}$ — a 4-cycle (square),
4 edges. $\deg(q) = 2$ (neighbors $p$ and $r$). 27.3 † A simple graph on $n=6$ vertices has at most $\binom{n}{2} = \binom{6}{2} =
\frac{6\cdot 5}{2} = 15$ edges. (Each edge is a distinct 2-element subset of $V$; there are $\binom{6}{2}$
of them.) 27.5 † $\deg^+(\text{Cam}) = 2$ (Cam→Ana, Cam→Ben), $\deg^-(\text{Cam}) = 1$ (only Ben→Cam),
$\deg^-(\text{Ben}) = 2$ (Ana→Ben and Cam→Ben). 27.7 † By the handshaking lemma, $\lvert E\rvert = \tfrac{1}{2}\sum_v \deg(v) =
\tfrac{1}{2}(5+3+3+2+2+1) = \tfrac{1}{2}(16) = 8$ edges. (Check the parity corollary: the odd degrees
are $5, 3, 3, 1$ — four of them, an even count, so the sequence is not ruled out.) 27.9 † Paths from Ana to Eve (no repeated vertex): Ana–Cam–Dev–Eve (length 3). That is the
only simple path: Eve's sole neighbor is Dev, Dev's only other neighbor is Cam, and Cam reaches Ana
directly or via Ben. The Ben detour, Ana–Ben–Cam–Dev–Eve, has length 4. Shortest: length 3. 27.11 † Apply the even-odd-degree corollary (count of odd-degree vertices must be even) and a sum
check.
- (a) $(3,3,3,3)$: four odd degrees — even count; sum $=12$, $\lvert E\rvert = 6$. Possible (e.g.
$K_4$).
- (b) $(4,4,4,2,1)$: one odd degree (the single 1) — odd count, impossible. (Also a degree-4 vertex
among only 5 vertices is fine, but the parity kills it.)
- (c) $(1,1,1,1)$: four odd degrees — even count; sum $=4$, $\lvert E\rvert = 2$. Possible (two
disjoint edges).
- (d) $(3,2,2,2,1)$: two odd degrees ($3$ and $1$) — even count; sum $=10$, $\lvert E\rvert = 5$.
Possible. 27.13 † Blanks, in order: $2\lvert E\rvert$ ; $2\lvert E\rvert$ ; even ; even ; even. Full
argument: $\sum_{v}\deg(v) = 2\lvert E\rvert$ is even; split into even- and odd-degree groups; the
even-degree group sums to an even number; subtracting, the odd-degree group also sums to an even number;
a sum of $\lvert V_{\text{odd}}\rvert$ odd numbers is even iff $\lvert V_{\text{odd}}\rvert$ is even.
Hence the number of odd-degree vertices is even. $\blacksquare$ 27.15 † Blanks, in order: $2$ ; $\binom{n}{2}$ ; $\binom{n}{2}$ ; a repeated (parallel) edge or a
loop. Argument: each simple-graph edge is a 2-element subset of $V$; there are exactly $\binom{n}{2}$
such subsets; a set $E$ of distinct edges has at most that many elements, so
$\lvert E\rvert \le \binom{n}{2}$; therefore $\lvert E\rvert > \binom{n}{2}$ forces a repeated pair —
parallel edges (or a loop) — so the graph is not simple. $\blacksquare$ 27.17 † Claim: if every vertex of an $n$-vertex graph has degree $\ge 1$, then
$\lvert E\rvert \ge \lceil n/2\rceil$. Proof: every degree is at least 1, so
$\sum_v \deg(v) \ge n$. By the handshaking lemma $2\lvert E\rvert = \sum_v \deg(v) \ge n$, so
$\lvert E\rvert \ge n/2$. Since $\lvert E\rvert$ is an integer, $\lvert E\rvert \ge \lceil n/2\rceil$.
$\blacksquare$ 27.18 † 27.19 27.20 † 27.21 27.22 † The flaw: a degree-3-for-all graph on 5 vertices would have $\sum_v\deg(v) = 5\times 3 =
15$, but by the handshaking lemma this must equal $2\lvert E\rvert$ — an **even** number, and $15$ is
odd. (Equivalently, 5 vertices of odd degree is an odd count, violating the corollary.) "Plenty of room"
is irrelevant; no such graph exists. A 3-regular graph needs an even number of vertices. 27.23 The flaw: the "proof" conflates "a bijection exists" with "an adjacency-preserving
bijection exists." Isomorphism (§27.5) requires a bijection $f$ with ${u,v}\in E_1 \iff
{f(u),f(v)}\in E_2$; equal vertex and edge counts do not guarantee one. Counterexample (4 vertices,
3 edges each): $G$ = the path $a$–$b$–$c$–$d$ (degree sequence $(1,1,2,2)$) and $H$ = the "triangle
plus an isolated vertex," edges $\{1,2\},\{2,3\},\{1,3\}$ with vertex $4$ alone (degree sequence
$(0,2,2,2)$). Same $\lvert V\rvert = 4$ and $\lvert E\rvert = 3$, but different degree sequences, so not
isomorphic. 27.24 † The flaw: removing an edge can destroy the only path between two vertices, even though
"other edges remain." Connectivity is about a path existing for every pair, not about edges existing.
Smallest example: the single edge $K_2$ (two vertices, one edge $\{u,v\}$) is connected; deleting
that edge leaves two isolated vertices — disconnected. (More generally, an edge whose removal
disconnects the graph is a bridge.) 27.25 The sloppy step is asserting you can "put two vertices in $X$ and one in $Y$" as if the choice
were free. Tighten it via the characterization bipartite $\iff$ no odd cycle (§27.3): a triangle is
a cycle of length 3, which is odd; a bipartite graph has no odd cycle; therefore a bipartite graph
contains no triangle. (Equivalently: 2-color the vertices red/blue so every edge is bichromatic;
following the triangle's three edges flips color three times and must return to the start, forcing the
start vertex to be both colors — impossible.) $\blacksquare$ 27.26 † Model: vertex = an exam; edge = an undirected edge between two exams iff some
student is enrolled in both (a conflict); undirected (the conflict relation is symmetric);
unweighted. The question "fewest time slots so that no two conflicting exams share a slot" is
graph coloring — the minimum number of colors (the chromatic number $\chi(G)$) needed so adjacent
vertices differ. Answered in Chapter 33. 27.27 Four questions: vertex = a person; edge = a mutual friendship; undirected; unweighted.
Specification of a suggestion: "Suggest to person $x$ each vertex $y$ at distance 2 from $x$ (a
friend-of-a-friend who is not already $x$'s friend and is not $x$), ranked by the number of common
neighbors $\lvert N(x)\cap N(y)\rvert$." For Eve in the study group: $N(\text{Eve}) = \{$Dev$\}$,
and Dev's other neighbor is Cam, who is not yet Eve's friend — so suggest Cam (one common
neighbor, Dev). (Ana and Ben are at distance 3, too far for a distance-2 suggestion.) 27.28 † Computing $\lvert E(K_n)\rvert$ for $n = 1,2,3,4,5$ gives $0, 1, 3, 6, 10$. Conjecture:
$\lvert E(K_n)\rvert = \binom{n}{2} = \frac{n(n-1)}{2}$ (the values are $0,1,3,6,10$ — the triangular
numbers). Proof from the definition: in $K_n$ every pair of distinct vertices is joined by exactly one
edge, so the edges are in bijection with the 2-element subsets of $V$; the number of those is
$\binom{n}{2} = \frac{n(n-1)}{2}$. $\blacksquare$ (Sanity: $\binom{5}{2} = 10$. ✓) 27.29 Testing: path on 3 vertices $P_3$ has degrees $(1,2,1)$ — the middle vertex has even degree,
so $\ge 1$ even-degree vertex ✓. Triangle: degrees $(2,2,2)$ — all even ✓. 4-cycle: $(2,2,2,2)$ ✓.
$K_4$: degrees $(3,3,3,3)$ — zero even-degree vertices! So $K_4$ is a counterexample: the
conjecture is false. (Even smaller: a single edge $K_2$ has degrees $(1,1)$, also zero even-degree
vertices.) The lesson (Theme 4): the conjecture survived several tests but a single well-chosen case
($K_4$) disproves it — testing builds suspicion, not proof, and one counterexample is decisive. 27.30 † Two graphs on 6 vertices, both with degree sequence $(2,2,2,2,2,2)$:
(i) a single 6-cycle $1$–$2$–$3$–$4$–$5$–$6$–$1$ (connected, one component); (ii) two disjoint
triangles $\{1,2,3\}$ and $\{4,5,6\}$ (two components). Every vertex has degree 2 in both, so the
degree sequences match — yet they are not isomorphic because they differ on the invariant number
of connected components (1 vs. 2), and isomorphisms map paths to paths, preserving component count.
What it proves about §27.5: matching degree sequences are necessary but not sufficient for
isomorphism — a finer invariant (here, component count) can still separate them. 27.31 † An undirected edge is a 2-element subset of $V$ — a set $\{u,v\}$ with $u\ne v$, in
which order is irrelevant ($\{u,v\} = \{v,u\}$), which is why a 27.33 † The digraph of $R = \{(1,2),(2,1),(2,3),(3,3)\}$: arcs $1\to 2$, $2\to 1$, $2\to 3$, and a
loop $3\to 3$. In/out-degrees — vertex 1: $\deg^- = 1$ (from 2), $\deg^+ = 1$ (to 2); vertex 2:
$\deg^- = 1$ (from 1), $\deg^+ = 2$ (to 1, to 3); vertex 3: $\deg^- = 2$ (from 2; from itself),
$\deg^+ = 1$ (the loop to itself). $R$ is not symmetric: $(2,3)\in R$ but $(3,2)\notin R$ (and the
loop $(3,3)$ is fine for symmetry, but the $2$–$3$ asymmetry breaks it). If $R$ were symmetric (every
arc paired with its reverse), you could draw it as an undirected graph instead (one unordered edge
per symmetric pair) — though the loop would still require a multigraph. 27.35 Pigeonhole proof that two vertices share a degree (rigorous). In a simple graph on $n\ge 2$
vertices each degree is an integer in $\{0, 1, \dots, n-1\}$ — that is $n$ possible values for $n$
vertices, so pigeonhole alone does not immediately force a repeat. The key observation: degree $0$
(an isolated vertex) and degree $n-1$ (a vertex adjacent to all others) cannot both occur — if
some vertex is adjacent to all $n-1$ others, then no vertex can have degree $0$, and conversely. So the
degrees actually fall into at most $n-1$ attainable values (either $\{0,\dots,n-2\}$ or
$\{1,\dots,n-1\}$, never the full range). Now $n$ vertices map into at most $n-1$ degree values, and by
the pigeonhole principle (Chapter 17) two vertices must share a value. $\blacksquare$ The "0 and
$n-1$ can't coexist" fact is exactly what shrinks the pigeonholes from $n$ to $n-1$ and closes the
argument. Running example throughout: 28.1 † The adjacency matrix is
$$A = \begin{pmatrix} 0&1&1&0&0&0\\ 1&0&0&1&0&0\\ 1&0&0&1&0&0\\ 0&1&1&0&1&0\\ 0&0&0&1&0&0\\ 0&0&0&0&0&0 \end{pmatrix}.$$
(a) $A[1][4] = 0$, so $\{1,4\}$ is not an edge. (b) $\deg(3) = \sum_j A[3][j] = 0+1+1+0+1+0 = 3$.
(c) Row $5$ is all zeros, telling you vertex $5$ has degree $0$ — it is isolated (no neighbors), which
is why the graph is disconnected. 28.3 † Queue-trace of Result: 28.5 † (a) Matrix cells: $n^2 = 5000^2 = 25{,}000{,}000$. (b) List entries: each of the $m = 9000$
undirected edges contributes two entries, $2 \times 9000 = 18{,}000$, plus $n = 5000$ keys, $\approx
23{,}000$ total. Ratio $\approx 25{,}000{,}000 / 23{,}000 \approx 1090\times$ — the list uses roughly
three orders of magnitude less space on this sparse graph. 28.7 † Symmetry. By definition $A[i][j] = 1$ exactly when $\{i,j\}$ is an edge. Since the graph
is undirected, the edge $\{i,j\}$ and the edge $\{j,i\}$ are the same edge, so $A[i][j] = 1 \iff
A[j][i] = 1$ (and likewise both $0$ together). Hence $A[i][j] = $ **$A[j][i]$** for all $i,j$. Zero
diagonal. $A[i][i] = 1$ would mean $\{i,i\}$ is an edge — i.e. a self-loop — which a simple graph
forbids. So $A[i][i] = $ $0$. $\blacksquare$ 28.9 † Claim. If $\mathrm{dist}[u] = \mathrm{dist}[v] = d$, then $\{u,v\}$ (if present) is not a BFS
tree edge. Proof. A BFS tree edge is recorded only when BFS discovers a new vertex $w$ from a vertex
$x$, and at that moment it sets $\mathrm{dist}[w] = \mathrm{dist}[x] + 1$ (the §28.2 code). So every tree
edge joins a vertex at some distance $d'$ to one at distance $d' + 1$ — its endpoints' distances differ by
exactly $1$. But $u$ and $v$ both have distance $d$, differing by $0 \ne 1$. Therefore $\{u,v\}$ cannot be
a tree edge; it is a non-tree ("cross"-type) edge connecting two vertices in the same ring. $\blacksquare$ 28.11 † Finishing-time property. Let $a \to b$ be an edge of a DAG; we show $b$ finishes before $a$.
Consider the moment DFS, while processing $a$, examines the edge $a \to b$. There are three cases for
$b$'s state:
- $b$ is unvisited. Then DFS recurses into $b$ and fully explores it, so $b$ finishes before DFS
returns to finish $a$. ✓
- $b$ is finished. Then $b$ already finished, i.e. before $a$ finishes. ✓
- $b$ is started but not finished (on the recursion stack). Then $b$ is an ancestor of $a$ in the DFS
tree, so there is a path $b \rightsquigarrow a$ of tree edges; together with the edge $a \to b$ this
forms a directed cycle $b \rightsquigarrow a \to b$. A DAG has no directed cycle, so this case is
impossible.
In the two possible cases, $b$ finishes before $a$. $\blacksquare$ Consequently, listing vertices in
decreasing finish time (i.e. reversing the post-order) puts $a$ before $b$ for every edge — a
topological order. 28.12 † Running time is $\Theta(n)$: the comprehension scans the entire row $A[v]$ — all $n$ cells — regardless of
how many are $1$. Even a degree-$1$ vertex forces a full row scan, because the $0$/$1$ entries are not
indexed by "where the neighbors are"; you must look at every column. (This is precisely the §28.1 weakness
of the matrix: "list neighbors of $v$" is $\Theta(n)$, versus $\Theta(\deg v)$ on a list.) 28.13 † (odd) Each vertex is adjacent to two others, forming the 4-cycle $0\!-\!1\!-\!2\!-\!3\!-\!0$ (it is connected,
with $4$ edges). 28.14 † For $t = 4$: BFS assigns $\mathrm{dist}[4] = 3$ (rings $0\!:\!\{0\}$, $1\!:\!\{1,2\}$, $2\!:\!\{3\}$,
$3\!:\!\{4\}$). For $t = 5$: vertex $5$ is never discovered, so it is absent from 28.15 † (odd) The loop launches BFS at vertex $0$ (sweeping up $\{0,1,2,3,4\}$, 28.16 † Iterative DFS reproducing the recursive pre-order — push neighbors reversed so the
smallest pops first; mark at pop and tolerate stack duplicates. Hand-trace. stack $[0]$ → pop $0$, order $[0]$, push reversed $[1,2]$ → stack $[2,1]$. Pop $1$, order
$[0,1]$, push $3$ (0 visited) → $[2,3]$. Pop $3$, order $[0,1,3]$, push $4,2$ (1 visited) → $[2,4,2]$. Pop
$2$, order $[0,1,3,2]$, nothing new → $[2,4]$. Pop $4$, order $[0,1,3,2,4]$ → $[2]$. Pop $2$, already
visited, skip. Result 28.17 † The bug: marking visited at dequeue lets a vertex be enqueued multiple times before it
is first processed — once for each already-queued neighbor that "sees" it (because the 28.19 † The bug: in an undirected graph, the edge back to the parent you just arrived from is the
same edge, not a second route, so it is not a cycle. Reporting on "any already-visited vertex" flags
it wrongly. Smallest exposing graph: two vertices with one edge, 28.21 † (Model it — social network.) (a) Edges (undirected "communicates with"): ${A,B}, {A,C},
{B,D}, {C,D}, {D,E}$; $F$ isolated. Adjacency list: (b) BFS from $A$: ring $0 = \{A\}$; ring $1 = \{B, C\}$; ring $2 = \{D\}$; ring $3 = \{E\}$; $F$ never
reached. Degrees of separation from Alice: $B\!:\!1$, $C\!:\!1$, $D\!:\!2$, $E\!:\!3$, $F\!:\!\infty$.
(c) Not connected — BFS from $A$ reaches $5$ of the $6$ people; $F$ is stranded. Adding any single
edge from $F$ to one of $\{A,B,C,D,E\}$ connects the graph (e.g. $\{F,D\}$ makes $F$ reachable, putting
everyone in one component). 28.23 † (Model it — maze.) Vertices: the open (non-wall) cells of the grid. Edges: join two
open cells that are orthogonally adjacent (north/south/east/west) and in-bounds — one undirected edge per
"the robot can step between these two cells." The maze is then an unweighted, undirected graph. The
fewest-step start-to-goal path is found by breadth-first search from the start cell: because every
step costs the same one move, BFS distance = minimum number of moves, and BFS is exactly the shortest-path
algorithm for unweighted graphs (§28.2). DFS would find a path but not necessarily the shortest, since
DFS order does not reflect distance from the source. 28.25 † (Conjecture and test, then prove.) Instrumenting DFS to count tree edges $T$ and back edges
$B$ on connected graphs: (a) Conjecture: $T(G) = n - 1$ and $B(G) = m - (n - 1)$. (b) Proof of $T(G) = n-1$: a DFS of a
connected graph discovers each of the $n$ vertices exactly once, recording one tree edge at each discovery
except for the source (which has no parent). So it records $n - 1$ tree edges — exactly the spanning
tree of §28.4. Since every one of the $m$ edges is examined and is either a tree edge or a back edge
(non-tree edge), the back edges number $B(G) = m - T(G) = m - (n-1)$. $\blacksquare$ (c) Test the
classmate's claim "$B(G) = 0 \iff G$ is a tree": the data agree — $B = 0$ only for the path (a tree).
Proof: $B(G) = 0 \iff m = n - 1$. A connected graph with $m = n-1$ edges is a tree (Exercise 28.10), and
a tree on $n$ vertices is connected with $m = n-1$. And $B(G) > 0 \iff$ there is a back edge $\iff$ there
is a cycle (§28.4 cycle-detection rule). So $B = 0 \iff$ no cycle $\iff$ (for a connected graph) a tree.
The classmate is right. This is theme four in action: the table suggested the formula on five cases;
the proof settles it for all connected graphs. 28.27 † (Ch. 27 + 28.) Total scanning work is $2m$. When BFS/DFS processes a vertex $v$ on an
adjacency list, it scans $v$'s neighbor list once — exactly $\deg(v)$ steps. Each vertex is processed
exactly once (marking guarantees this), so the total scanning work is
$$\sum_{v \in V} \deg(v) = 2m$$
by the handshaking lemma (Chapter 27: the sum of degrees counts every edge from both endpoints, hence
twice). So the scanning is $\Theta(m) = \Theta(E)$. Why this is the key step: without handshaking we
could only bound each scan by $n$ and would multiply, getting $n \cdot \Theta(n) = O(V^2)$; handshaking
lets us sum the degrees to $2m$ instead, replacing the loose $O(V^2)$ with the tight $\Theta(E)$, which
combined with the $\Theta(V)$ per-vertex bookkeeping gives $\Theta(V + E)$. 28.29 † (Ch. 6 + 28.) The invariant: at every moment during BFS, the distance values of the
vertices currently in the queue are nondecreasing from front to back and span at most two consecutive
values $d$ and $d+1$ (so BFS processes vertices in nondecreasing distance order). Initialization is
established by the line 28.31 (Ch. 13 + 28 — Deep Dive.) Let 29.1 † The four simple directed paths A → E and their weights:
- A → B → E: $4 + 7 = 11$
- A → C → B → E: $1 + 2 + 7 = 10$
- A → C → B → D → E: $1 + 2 + 1 + 3 = 7$
- A → C → D → E: $1 + 5 + 3 = 9$ The shortest is A → C → B → D → E, so $\delta(A, E) = 7$. (Note A → B → D → E, $4+1+3=8$, is also a path
but A → B starts worse; it loses to 7.) 29.3 † Dijkstra from B. Initialize Vertices A, C are unreachable from B (no incoming exploration), so $\text{dist}[A] = \text{dist}[C] =
\infty$. $\delta(B, E) = 4$, via B → D → E. 29.5 † Predecessor array from Dijkstra(A): 29.7 † Safety of relaxation. Blanks, in order: $\delta(s, s)$ (since $0$ is the empty-path weight);
the new value is $\text{dist}[u] + w(u, v)$; and $\delta(s, u) + w(u, v)$ is greater than or equal to
$\delta(s, v)$. Full argument: by induction on the number of relaxations. Base: before any relaxation,
$\text{dist}[s] = 0 = \delta(s, s)$ and $\text{dist}[v] = \infty \ge \delta(s, v)$ for $v \ne s$. Step:
a relaxation of $(u, v)$ changes only $\text{dist}[v]$, and only to $\text{dist}[u] + w(u, v)$. By the
hypothesis $\text{dist}[u] \ge \delta(s, u)$, so $\text{dist}[u] + w(u, v) \ge \delta(s, u) + w(u, v)$.
The quantity $\delta(s, u) + w(u, v)$ is the weight of the path "shortest route to $u$, then edge
$(u, v)$," which is a path to $v$, so it is $\ge \delta(s, v)$ (the minimum over all such paths). Hence
the new $\text{dist}[v] \ge \delta(s, v)$; all other distances are unchanged and still satisfy the bound.
$\blacksquare$ 29.9 † Path-relaxation step. Blanks, in order: the prefix uses $k - 1$ edges; after $k-1$ passes
$\text{dist}[u] = \delta(s, u)$; the relaxation gives $\text{dist}[v] \le \delta(s, v)$; and combined
with safety we conclude $\text{dist}[v] = \delta(s, v)$. (This is the inductive step; 29.32 writes the
full proof.) 29.11 † Finalization order is non-decreasing. Suppose Dijkstra finalizes $u$ before $v$. By the
correctness theorem (§29.3), at finalization $\text{dist}[u] = \delta(s, u)$ and $\text{dist}[v] =
\delta(s, v)$. When $u$ was finalized, $v$ was still unfinalized, and the extract-min rule picked $u$ as
the minimum tentative distance, so $\text{dist}[u] \le \text{dist}[v]$ at that moment. Tentative
distances only decrease, and $v$'s value at its finalization equals $\delta(s, v)$, which is $\le$ its
value at any earlier moment; but we also know $u$'s value was already final ($= \delta(s, u)$). At the
moment $u$ was chosen, $\delta(s, u) = \text{dist}[u] \le \text{dist}[v]_{\text{then}}$, and
$\text{dist}[v]_{\text{then}} \ge \delta(s, v)$ does not immediately give the claim — so argue directly:
$v$'s final value $\delta(s, v)$ is reached only by relaxations, each of which sets it to
$\text{dist}[x] + w \ge \delta(s, x)$ for some already-or-later vertex; the cleanest statement is that
because $u$ was extracted before $v$ and tentative values never increase, $\delta(s, u) = \text{dist}[u]
\le \text{dist}[v]$ at extraction of $u$, and $\text{dist}[v]$ can only have decreased to its final
$\delta(s, v)$ — but it cannot drop below $\delta(s, v)$ (safety), and any relaxation that lowered it came
from a vertex finalized no later than $v$. The intended takeaway: with non-negative weights, Dijkstra
extracts vertices in non-decreasing order of final distance, so $\delta(s, u) \le \delta(s, v)$.
$\blacksquare$ (Acceptable shorter answer: by safety $\text{dist}[v] \ge \delta(s,v)$ always, and at
$u$'s extraction $\delta(s,u) = \text{dist}[u] \le \text{dist}[v]$; since later relaxations only lower
$\text{dist}[v]$ toward $\delta(s,v) \ge \delta(s,u)$ is the content, the order is non-decreasing.) 29.13 † See 29.15 † See 29.17 † See 29.18 † The flaw is finalizing on discovery. On the road map, B is discovered in round 1 at
distance 4 (via the direct edge A → B). Finalizing it then locks in 4 — but B's true distance is
$\delta(A, B) = 3$, achieved by A → C → B, which is only found when C is finalized (round 2). The pitfall
is named in §29.2: a vertex is safe to finalize only when it is the minimum over all unfinalized
vertices, not the moment its 29.19 The "proof" uses only one of the two facts correctness needs. Safety of relaxation gives
$\text{dist}[v] \ge \delta(s, v)$ — a lower bound. But finalizing a vertex with the correct value
also requires the matching upper bound, i.e. that the finalized value is not too large; that is what
the §29.3 argument establishes, and it uses non-negativity at inequality (2), $\delta(s, y) \le
\delta(s, u)$ (extending a shortest path cannot lower its cost). With a negative edge that step fails, so
a vertex can be finalized at a value strictly above $\delta(s, v)$ even though $\text{dist}[v] \ge
\delta(s, v)$ still holds (the counterexample $s\to a:1$, $s\to b:2$, $b\to a:-2$ finalizes $a$ at 1 with
true distance 0). Safety alone proves nothing about correctness. 29.20 † Two separate points. (a) Is $|V|$ passes wrong? No — running $|V|$ passes instead of $|V|-1$
is harmless: in a graph with no negative cycle all distances are already final after $|V|-1$ passes, so
the extra pass changes nothing. The standard count is $|V|-1$ because that suffices (a simple shortest
path has $\le |V|-1$ edges); $|V|$ is just one wasted pass. (b) Is the negative-cycle conclusion correct?
No. "The last ordinary pass changed something" does not imply a negative cycle: a graph with no
negative cycle but a shortest path of exactly $|V|-1$ edges can still be updating a distance on pass
$|V|-1$. The unambiguous test is a separate detector pass run after the $|V|-1$ passes: if any
edge can still be relaxed then, a negative cycle is reachable. The student conflated "still improving"
with "negative cycle." Fix: keep a dedicated detector pass. 29.21 Buggy loop order 29.22 † Model it (cheapest flights). Vertices $V = \{S, A, B, C, T\}$. Directed edges with fares as
weights: $S\to A:120$, $S\to B:200$, $A\to B:60$, $A\to T:300$, $B\to T:150$, $A\to C:90$, $C\to T:140$.
Weight function $w$ = the fare. We solve single-source shortest paths (SSSP) with source $S$ (we want
the cheapest fare from one origin). Compute by hand (all fares non-negative ⇒ Dijkstra): $\text{dist}[S]=0$;
relax to $A:120$, $B:200$. Finalize $A(120)$: $A\to B$ gives $180$ (beats 200), $A\to T$ gives $420$,
$A\to C$ gives $210$. Finalize $B(180)$: $B\to T$ gives $330$ (beats 420). Finalize $C(210)$: $C\to T$
gives $350$ (does not beat 330). Finalize $T(330)$. Cheapest fare $S \to T$ is \$330, via
S → A → B → T ($120 + 60 + 150$). 29.23 Model it (most reliable path). Reliability of a path = product of link probabilities $p_i$.
Take $w(e) = -\log p_e$. Then a path's total weight is $-\sum \log p_i = -\log(\prod p_i)$, which is a
decreasing function of the product: maximizing $\prod p_i$ is equivalent to minimizing $\sum w$.
Since each $p_e \in (0, 1]$, $\log p_e \le 0$, so $w(e) = -\log p_e \ge 0$ — the weights are
non-negative. That is exactly the condition Dijkstra's correctness proof (§29.3) requires, so we may
run Dijkstra on the transformed weights and read off the most reliable path (Bellman-Ford is unnecessary
because no weight is negative). 29.25 † (Conjecture: shortest paths are always unique — false.) Counterexample with positive
weights: vertices $\{s, a, b, t\}$, edges $s\to a:1$, $s\to b:1$, $a\to t:1$, $b\to t:1$. Both $s \to a
\to t$ and $s \to b \to t$ have weight $2 = \delta(s, t)$, so the shortest path is not unique. Running
29.27 † (Conjecture: $\lceil |V|/2\rceil$ passes suffice — false.) Build a chain: vertices
$v_0 \to v_1 \to \dots \to v_{n-1}$, each edge weight 1, source $v_0$. The unique shortest path to
$v_{n-1}$ uses $n - 1$ edges. Bellman-Ford's path-relaxation property says after $k$ passes only vertices
within $k$ edges are settled; if edges are relaxed in a bad order (e.g. last edge first each pass), each
pass can advance the frontier by just one vertex, so $v_{n-1}$ is not correct until pass $n-1$. With only
$\lceil n/2 \rceil$ passes, $v_{n-1}$ still has $\text{dist} = \infty$ (or too large). So the conjecture
fails. This shows the $|V|-1$ count is tight: a longest simple path has $|V|-1$ edges, and in the
worst relaxation order you need one pass per edge of it. 29.29 (Ch. 27 + 29.) (a) The handshaking lemma for an undirected graph: $\sum_{v} \deg(v) =
2|E|$, because each edge contributes to the degree of both endpoints. It does not apply as stated to the
road map because the road map is directed; for digraphs we split degree into in-degree $\deg^-$ and
out-degree $\deg^+$, and each directed edge contributes $1$ to exactly one out-degree and one in-degree.
(b) The road map has 7 edges. Out-degrees: $\deg^+(A)=2$ (to B, C), $\deg^+(B)=2$ (D, E), $\deg^+(C)=2$
(B, D), $\deg^+(D)=1$ (E), $\deg^+(E)=0$; sum $= 7$. In-degrees: $\deg^-(A)=0$, $\deg^-(B)=2$ (from A, C),
$\deg^-(C)=1$ (from A), $\deg^-(D)=2$ (from B, C), $\deg^-(E)=2$ (from B, D); sum $= 7$. Both sums equal
$|E| = 7$, as required: $\sum_v \deg^+(v) = \sum_v \deg^-(v) = |E|$. 29.31 (Ch. 6 + 29.) The loop invariant of Dijkstra: every time a vertex $u$ is finalized,
$\text{dist}[u] = \delta(s, u)$ (its tentative distance already equals the true shortest distance). The
induction is on the number of finalized vertices. "Maintenance" is the inductive step: assuming all
previously finalized vertices have correct distances, show the next finalized vertex does too — which
is the contradiction argument of §29.3 (take the first wrong vertex and derive a contradiction). The
extract-min step enters at inequality (3), $\text{dist}[u] \le \text{dist}[y]$: because the algorithm
finalizes the unfinalized vertex of minimum tentative distance, $u$ was chosen over the boundary vertex
$y$, giving that inequality, which (chained with safety and non-negativity) closes the contradiction. So:
invariant ↔ the statement $P(n)$; maintenance ↔ the inductive step; extract-min ↔ the greedy choice the
step relies on. 29.33 (Deep Dive — A*.) A* uses priority key $f(v) = \text{dist}[v] + h(v)$ with $h$ admissible:
$h(v) \le \delta(v, t)$ for the goal $t$ (it never overestimates the remaining cost). The §29.3 proof's
load-bearing step was inequality (2), $\delta(s, y) \le \delta(s, u)$ — "extending a shortest path cannot
lower its cost," guaranteed by non-negativity. In A* the corresponding claim must compare $f$-values
along a shortest path to $t$. Admissibility is exactly what keeps $f$ a valid lower bound on the true cost
through a vertex to the goal: because $h(v) \le \delta(v, t)$, the estimate $f(v) = \text{dist}[v] +
h(v) \le \text{dist}[v] + \delta(v, t)$ never over-states the cost of completing the journey, so the
goal $t$ (with $h(t) = 0$, hence $f(t) = \text{dist}[t]$) cannot be popped before its true distance is
settled. If $h$ overestimated, $f$ could exceed the true through-cost and A* could pop a vertex (or the
goal) too early at an inflated value — returning a non-shortest path, exactly as a negative edge breaks
inequality (2) for Dijkstra. So admissibility plays the role for A* that non-negativity plays for
Dijkstra: it rescues the one inequality the correctness argument needs. 30.1 † Degree sequence $2,2,4,4,6$ — every degree is even, and we are told the graph is connected.
By Euler's theorem, an Euler circuit exists (and therefore an Euler path exists too, namely the
circuit itself). 30.3 † $K_n$ has every vertex of degree $n-1$, and $K_n$ is connected. An Euler circuit needs every
degree even, i.e. $n - 1$ even, i.e. $n$ odd. Check: $K_3$ ($n=3$ odd) all degrees $2$ → Euler
circuit ✓; $K_4$ ($n=4$ even) all degrees $3$ (odd) → no Euler circuit (in fact four odd vertices, so no
Euler path either); $K_5$ ($n=5$ odd) all degrees $4$ → Euler circuit ✓. 30.5 † Euler circuit visits every edge exactly once; Hamilton circuit visits every vertex
exactly once. Example with one but not the other: $K_4$ has a Hamilton circuit (e.g. $1\to2\to3\to4\to1$)
but no Euler circuit (all four vertices have odd degree $3$). (Conversely, two triangles sharing a
single vertex — a "bowtie" — has an Euler circuit, every degree even, but no Hamilton circuit, since the
shared cut-vertex must be visited twice to reach both triangles.) 30.7 † (Scaffolded necessity for an Euler path.) Blanks, in order:
- The path uses each edge exactly one time.
- For $v \notin \{s,t\}$, edges at $v$ are consumed in in/out pairs; hence $\deg(v)$ is even.
- At $s$, the first edge is a departure with no matching prior arrival; at $t$, the last edge is an
arrival with no matching following departure. So $\deg(s)$ and $\deg(t)$ are both odd.
- Therefore exactly two vertices ($s$ and $t$) have odd degree, all others even. $\blacksquare$ 30.9 † Claim: $K_n$ has a Hamilton circuit for all $n \ge 3$. Proof: list the vertices in any
order $v_1, v_2, \dots, v_n$ and form the cyclic sequence $v_1 \to v_2 \to \dots \to v_n \to v_1$. Each
consecutive pair $v_i, v_{i+1}$ (and the closing pair $v_n, v_1$) is an edge because $K_n$ is
complete — every pair of distinct vertices is joined. The sequence visits each vertex exactly once and
returns to $v_1$, so it is a Hamilton circuit. $\blacksquare$ (This is exactly why §30.5 can say a
Hamilton circuit always exists on the complete weighted graph, leaving all of TSP's difficulty in the
minimization.) 30.11 † Claim: a graph $G$ with all even degrees but disconnected into two components $C_1, C_2$,
each with $\ge 1$ edge, has no Euler circuit. Proof: an Euler circuit must traverse every edge of
$G$ as one closed walk. Pick an edge $e_1$ in $C_1$ and an edge $e_2$ in $C_2$. A single walk that uses
$e_1$ stays within the connected component reachable from $e_1$ — namely $C_1$ — because consecutive
edges of a walk share a vertex, and no vertex of $C_1$ is adjacent to any vertex of $C_2$ (they are
different components). So the walk can never reach $e_2$. Hence no single closed walk covers all edges,
and no Euler circuit exists — regardless of the even-degree condition. $\blacksquare$ This is the
companion to the §30.2 pitfall: parity alone is insufficient; connectivity is required too. 30.13 † 30.15 † 30.17 † The flaw: the graph is disconnected. Two disjoint triangles do have every vertex of even
degree ($2$), but Euler's theorem requires the graph to be connected and have the right parity. An
Euler circuit must traverse all six edges in one closed walk, which is impossible across two separate
components (you cannot cross from one triangle to the other). The "proof" quotes only the parity half of
the theorem and silently drops the connectivity hypothesis. Correct verdict: no Euler circuit. 30.19 † (Find the error — porting Euler code to Hamilton.) The plan fails because the Euler
detector's speed comes entirely from Euler's theorem reducing existence to a local degree count
($O(V+E)$), and Hierholzer's construction exploits that local structure to build a tour greedily without
backtracking. Hamiltonicity has no known local characterization — no degree count or greedy splice
decides it. Swapping the "degree even?" check for a "visit-count" check does not give a correct
Hamilton test, and any correct Hamilton method known reduces to exponential backtracking search. The
$O(V+E)$ guarantee comes from the theorem, not from the surface shape of the code; since no analogous
theorem exists for Hamilton, the guarantee cannot survive the "swap." Surface similarity of problem
statements says nothing about similarity of computational difficulty (§30.4). 30.21 † (Model it — snowplow.) Vertices = street intersections; edges = street segments
(an edge per segment; a divided road plowed on both sides would be two parallel edges, i.e. a
multigraph). The operator wants to drive every edge and return to the depot, so this is an Euler
circuit problem. A no-repeats closed route exists iff the street network is connected and every
intersection has even degree (Euler's theorem). If some intersections have odd degree, no perfect
closed route exists; the operator must re-plow some streets. Specifically, the odd-degree
intersections must be paired up and the shortest path between each pair re-driven, adding those streets'
length (this is the route-inspection / Chinese-postman idea from Case Study 1). The number of odd
vertices is always even (Exercise 30.8), so they can always be paired. 30.23 † (Model it — PCB drilling.) (a) This is the Traveling Salesman Problem: visit every
hole (vertex) exactly once, minimizing total head-movement, returning home — a minimum-cost Hamilton
circuit on the complete weighted graph of holes. (b) It is NP-hard, so for 60 holes exact brute
force ($\sim 59!/2$) is hopeless; the appropriate §30.6 response, since costs are metric (Euclidean
distances obey the triangle inequality), is an approximation algorithm. (c) Christofides'
algorithm carries a provable guarantee on metric TSP: its tour costs at most $\tfrac{3}{2}$ times
the optimal, in polynomial time. (In practice one might then polish with 2-opt.) 30.25 † (Conjecture and test, then prove.)
(a) Counting edges of $K_n$: $K_3$ has $3$, $K_4$ has $6$, $K_5$ has $10$, $K_6$ has $15$. Table
$(n,\lvert E\rvert)$: $(3,3),(4,6),(5,10),(6,15)$.
(b) Conjecture: $\lvert E(K_n)\rvert = \dfrac{n(n-1)}{2}$.
(c) Proof: every edge of $K_n$ joins a distinct unordered pair of vertices, and every such pair is
present (complete graph), so $\lvert E(K_n)\rvert = \binom{n}{2} = \dfrac{n(n-1)}{2}$ (Chapter 17). Check:
$\binom{5}{2}=10$ ✓.
(d) $K_n$ has all degrees $n-1$ and is connected, so by Euler's theorem it has an Euler circuit iff
$n-1$ is even, i.e. iff $n$ is odd — proving the rule of Exercise 30.3. (The edge count itself
isn't needed for the parity rule, but it confirms, e.g., that $K_5$'s $10$ edges can form one closed
Euler tour, while $K_4$'s $6$ cannot.) 30.27 † (Conjecture and test, then prove — the cycle graph $C_n$.)
(a) Testing small $n$: $C_3, C_4, C_5$ each have every vertex of degree $2$ (even) and are connected, so
each has an Euler circuit; each is itself a single cycle through all vertices, so each has a Hamilton
circuit.
(b) Conjecture: for every $n \ge 3$, $C_n$ has both an Euler circuit and a Hamilton circuit.
(c) Proofs. Euler: every vertex of $C_n$ has degree exactly $2$ (even) and $C_n$ is connected, so by
Euler's theorem an Euler circuit exists. In fact the cycle traversed once is the Euler circuit (it
uses each of the $n$ edges once and returns to start). Hamilton: the same single loop visits each of the
$n$ vertices exactly once and returns to start, so it is a Hamilton circuit by definition. $\blacksquare$
Note $C_n$ has all degrees $2 < n/2$ for $n > 4$, so it fails Dirac's hypothesis yet is
Hamiltonian — exactly why §30.3 uses it to show Dirac's theorem is sufficient, not necessary. 30.28 † (Ch. 27 + 30.) The handshaking lemma: $\sum_{v \in V} \deg(v) = 2\lvert E\rvert$ —
the degree sum is even because each edge contributes exactly $2$ to it (one to each endpoint). The
necessity half of Euler's theorem is the local counterpart: at a single vertex, an Euler circuit
consumes the edges incident to that vertex two at a time (one "in," one "out" per pass), forcing that
individual degree to be even. Handshaking is the global statement "the total is even"; Euler's
necessity is "each term is even," which is a strictly stronger, vertex-by-vertex fact that holds
precisely when an Euler circuit exists. 30.29 (Ch. 28 + 30.) (a) Run BFS or DFS (Chapter 28) from any single vertex and check that
every non-isolated vertex was reached; that confirms connectivity. (b) BFS/DFS runs in $O(V+E)$, and the
degree-parity count also runs in $O(V+E)$; their sum is still $O(V+E)$ — linear in the input — so the
full "does an Euler circuit exist?" decision is tractable. (c) Contrast: brute-force TSP must price
$\frac{(n-1)!}{2}$ tours, which is factorial and explodes past $\sim 12$–$25$ cities, whereas the Euler
decision scales to graphs with millions of edges. 30.30 † (Ch. 6 + 30 — the sufficiency proof.) (a) Base case: the smallest connected even
graph (e.g. $m=0$, or a single cycle), which trivially has an Euler circuit. Inductive hypothesis:
every connected even-degree graph with fewer than $m$ edges has an Euler circuit. (b) The
hypothesis is applied in the sentence: "The connected component of $G'$ containing $u$ … has fewer
than $m$ edges, so by the inductive hypothesis it has an Euler circuit $C_u$ starting and ending at
$u$." (c) Strong induction is needed because the leftover graph $G'$ (after removing the first
circuit $C_0$) can have any number of edges less than $m$ — not necessarily $m-1$ — so we must be able
to assume the result for all smaller edge-counts, not merely the immediately previous one. This is
exactly the Chapter 6 → Chapter 7 distinction: when a recursive/inductive step jumps to a much smaller
(not adjacent) case, ordinary induction is insufficient and strong induction is the right tool. 30.31 (Ch. 17 + 30 — counting tours.) (a) A tour is a cyclic arrangement of the $n$ cities;
rotating the starting point yields the same cycle, and there are $n$ rotations of each, so the $n!$
linear orderings collapse to $n!/n = (n-1)!$ distinct rooted tours (equivalently, fix one city as the
start — Chapter 17's standard "anchor an element" trick for circular arrangements). (b) With symmetric
costs, a tour and its exact reverse traverse the same edges for the same total cost, so they are the same
undirected cycle; this pairs the $(n-1)!$ tours into $\frac{(n-1)!}{2}$ distinct ones. (c) For $n=10$:
$\frac{9!}{2} = \frac{362880}{2} = 181440$ distinct tours, versus Held–Karp's $\sim n^2 2^n = 100 \cdot
1024 = 102400$ basic operations. Held–Karp ($\approx 1.0\times10^5$) is already smaller than brute force
($\approx 1.8\times10^5$) at $n=10$, and the gap widens explosively as $n$ grows ($2^n$ beats $n!$ for
all larger $n$) — foreshadowing why dynamic programming, though still exponential, dramatically
out-scales naive enumeration (§30.5 Connection). 30.33 (Deep Dive — decision vs. optimization.) (a) Binary-search the budget $B$ between a lower
bound (e.g. $0$) and an upper bound $U$ (e.g. the cost of any tour, or $n$ times the largest edge
cost). Each decision query "is there a tour of cost $\le B$?" halves the remaining range, so the number
of queries is about $\log_2 U$. (b) $U$ is at most $n \cdot c_{\max}$ where $c_{\max}$ is the largest
edge cost, so $\log_2 U \le \log_2 n + \log_2 c_{\max}$, which is polynomial in the input size (the
costs are written in $O(\log c_{\max})$ bits each). Thus a polynomial number of decision-oracle calls
recovers the optimum, so the decision and optimization forms are polynomial-time equivalent — they
"stand or fall together" on tractability. (c) Therefore Chapter 37 can develop its entire theory using
decision problems (clean yes/no membership in a language) without losing the practical optimization
question, since the two are interreducible in polynomial time. Full solutions to the daggered (†) and odd-numbered exercises from
A tree on $n$ vertices has exactly $n - 1$ edges (Theorem 31.2). For $n = 15$: For the rooted tree (root $a$): Inserting $8, 3, 10, 1, 6, 14$ into an empty BST: Height = 2 (e.g. path $8 \to 3 \to 1$). Inorder traversal: $1, 3, 6, 8, 10, 14$ — sorted, as
the BST invariant guarantees. Filled blanks and full statement. By induction, every forest on $n$ vertices with $c$ components has $n - c$ edges. $\blacksquare$
(Setting $c = 1$ recovers Theorem 31.2.) Claim. An inorder traversal of a BST on $n$ distinct keys outputs them in strictly increasing order. Proof (structural induction on the tree).
Base case: the empty tree outputs the empty sequence, vacuously sorted; a single node outputs its one
key, trivially sorted.
Inductive step: let the tree have root $r$ (key $k_r$), left subtree $L$, right subtree $R$. By the
inductive hypothesis, inorder of $L$ outputs $L$'s keys in increasing order, and likewise for $R$.
Inorder of the whole tree outputs $[\text{inorder}(L)] \,,\, k_r \,,\, [\text{inorder}(R)]$. By the BST
invariant, every key in $L$ is $< k_r$ and every key in $R$ is $> k_r$. Hence: the $L$-block is
sorted and all its entries are $< k_r$; then $k_r$; then the $R$-block, sorted and all entries $> k_r$.
The concatenation is therefore strictly increasing across the seams as well as within each block. So the
full output is sorted. $\blacksquare$ Claim. A binary tree of height $h$ has at most $2^{h+1} - 1$ vertices and at most $2^h$ leaves. Proof (induction on $h$).
Base case ($h = 0$): a single node. Vertices $= 1 = 2^{0+1} - 1 = 2 - 1$; leaves $= 1 = 2^0$. ✓
Inductive step: assume the bounds for all heights $< h$, and let $T$ have height $h \ge 1$ with root
$r$ and subtrees $L, R$ (either may be empty). Each subtree has height $\le h - 1$. By the hypothesis
each has at most $2^{(h-1)+1} - 1 = 2^h - 1$ vertices, so
$$\lvert T\rvert \le 1 + (2^h - 1) + (2^h - 1) = 2^{h+1} - 1.$$
For leaves: the root is not a leaf (height $\ge 1$ means it has a child), so every leaf lies in $L$ or
$R$; by the hypothesis each subtree has at most $2^{h-1}$ leaves, giving at most $2^{h-1} + 2^{h-1} =
2^h$ leaves. $\blacksquare$ (Equality holds for the perfect binary tree of height $h$.) Trace on (Empty tree height $-1$ so a single node is $0$, matching §31.2's convention.) Full file:
Why a local check is insufficient: the BST invariant is not "each node is between its two
children"; it is "each node is greater than all keys in its left subtree and less than all keys
in its right subtree." A purely local parent–child check can pass while a grandchild violates the
global order. Counterexample (3-node tree): Locally everything looks fine ($3 < 5$, $8 > 5$, $6 > 3$), but $6$ sits in the left subtree of $5$ while
$6 > 5$, breaking the invariant. The range version catches it: $6$ must lie in the interval $(3, 5)$
inherited along the path, and $6 \not< 5$. Full file: Flaw: the converse requires the word connected. The correct biconditional is "a connected
graph on $n$ vertices with $n - 1$ edges is a tree," not "every graph with $n - 1$ edges is a tree."
Dropping connectivity breaks it.
Counterexample (4 vertices, 3 edges, not a tree): take a triangle on $\{a, b, c\}$ (3 edges) plus an
isolated vertex $d$. This graph has $n = 4$ vertices and $3 = n - 1$ edges, yet it is not a tree: it
is disconnected ($d$ is isolated) and contains a cycle (the triangle). So "$n - 1$ edges" alone
guarantees nothing; you need connectivity (or, equivalently, acyclicity) as well. Flaw: the claim "each comparison halves the remaining keys" is true only for a balanced BST. A
plain BST has no balance guarantee; on adversarial input it degenerates into a stick of height
$\Theta(n)$, and then each comparison discards only the (empty) other subtree — one key, not half — so
search is $\Theta(n)$, not $O(\log n)$.
Insertion order giving $\Theta(n)$ search on $\{1,2,3,4\}$: insert in sorted order
$1, 2, 3, 4$. Each new key exceeds all current keys, so it attaches as the right child of the deepest
node, building the chain $1 \to 2 \to 3 \to 4$ of height 3. Searching for $4$ requires comparisons at
$1, 2, 3, 4$ — four comparisons, i.e. $\Theta(n)$. (Reverse-sorted order $4,3,2,1$ gives the mirror
stick.) The fix is a self-balancing BST (AVL, red–black), which keeps $h = O(\log n)$. Model: a rooted tree with the CEO at the root, an edge from each manager to each direct report
(oriented away from the root), and "exactly one direct manager, no cycles" guaranteeing it is a tree
(unique path from any employee up to the CEO). Data (complete trees; for $n = 1,3,7,15$ these are perfect, heights $0,1,2,3$). Internal path length
$\mathrm{IPL} = \sum_v \text{depth}(v)$: Conjecture. A perfect binary tree of height $h$ has $\mathrm{IPL}(h) = \sum_{d=0}^{h} d\cdot 2^{d}$.
Using the standard identity $\sum_{d=0}^{h} d\, 2^d = (h-1)2^{h+1} + 2$, this is
$$\mathrm{IPL}(h) = (h-1)\,2^{h+1} + 2.$$
Test at $h = 4$: formula gives $(4-1)2^5 + 2 = 3\cdot 32 + 2 = 98$. Direct:
$\mathrm{IPL}(3) + (\text{16 nodes at depth }4)\cdot 4 = 34 + 16\cdot 4 = 34 + 64 = 98$. ✓ Proof (induction on $h$). Base $h=0$: $\mathrm{IPL}(0)=0$ and $(0-1)2^{1}+2 = -2+2 = 0$. ✓
Step: a perfect tree of height $h$ is a root (depth 0) plus two perfect subtrees of height $h-1$.
Within a subtree every node's depth, measured from the whole tree's root, is one more than its depth
within the subtree. A subtree of height $h-1$ has $2^h - 1$ nodes, so re-rooting adds $2^h - 1$ to its
internal path length. Hence
$$\mathrm{IPL}(h) = 0 + 2\big[\mathrm{IPL}(h-1) + (2^{h}-1)\big].$$
By the hypothesis $\mathrm{IPL}(h-1) = (h-2)2^{h} + 2$, so
$$\mathrm{IPL}(h) = 2\big[(h-2)2^{h} + 2 + 2^{h} - 1\big] = 2\big[(h-1)2^{h} + 1\big] = (h-1)2^{h+1} + 2,$$
as claimed. $\blacksquare$ (Code: build the perfect tree recursively and sum depths via a
depth-passing traversal; output the IPL — hand-derived values above.) Conjecture. Huffman coding on $s$ distinct symbols performs exactly $s - 1$ merges.
Hand checks: $s=2$: one merge joins the two leaves → 1 merge $= 2-1$. $s=3$: merge two smallest
(1), then merge the result with the third (1) → 2 merges $= 3-1$. $s=4$: 3 merges. $s=5$: 4 merges
(see Case Study 2, four merges for ABRACADABRA's 5 symbols). ✓
Proof. The priority queue starts with $s$ trees (one per symbol) and ends with $1$. Each merge pops
two trees and pushes one, a net change of $-1$ tree per merge. To go from $s$ trees to $1$ tree
requires reducing the count by $s - 1$, hence exactly $s - 1$ merges. $\blacksquare$ (Equivalently:
every merge creates exactly one internal node, and a full binary tree with $s$ leaves has $s-1$ internal
nodes — Exercise 31.10.) Handshaking lemma (Ch. 27): $\sum_{v\in V}\deg(v) = 2\lvert E\rvert$ (each edge adds 1 to the degree
of each of its two endpoints). For a tree, $\lvert E\rvert = n - 1$ (Theorem 31.2), so
$$\sum_{v}\deg(v) = 2(n-1), \qquad \text{average degree} = \frac{1}{n}\sum_v \deg(v) = \frac{2(n-1)}{n} = 2 - \frac{2}{n}.$$
This is strictly less than 2 for every finite tree, and approaches 2 as $n \to \infty$. For the "degree $\ge 2$" claim: if $n \ge 3$, the tree cannot have every vertex of degree 1. If all
$n$ vertices had degree 1, the degree sum would be $n$, but the sum must equal $2(n-1)$; $n = 2(n-1)$
forces $n = 2$. So for $n \ge 3$ at least one vertex has degree $\ge 2$ (in fact, since leaves number
$\ge 2$ by Exercise 31.8, and the sum is $2(n-1)$, the non-leaf vertices carry the surplus degree). The
only tree in which every vertex has degree exactly 1 is the single edge $n = 2$. $\blacksquare$ Why level-order is BFS from the root: BFS (Ch. 28) explores a graph in waves of increasing distance
from the start vertex, using a queue. In a rooted tree, the distance from the root to a vertex is
precisely its depth. So BFS from the root visits depth-0 (the root), then all depth-1 vertices, then
all depth-2, … — which is exactly the level-order (breadth-first) traversal, left to right within a level
because children are enqueued in left-to-right order.
Why no "visited" check is needed on a tree: BFS on a general graph marks vertices visited to avoid
revisiting via cycles or multiple paths. A tree has no cycles and a unique path between any two
vertices (Theorem 31.1), so each vertex is enqueued exactly once — through its single parent — and can
never be reached a second time. The visited set is therefore redundant on a tree.
Level-order for the tree of 31.4 ( With a binary heap (§31.5) implementing the priority queue, Dijkstra (Ch. 29) does two kinds of heap
operations. (1) Extract-min: once per vertex as it is finalized — $V$ extractions, each $O(\log V)$,
total $O(V\log V)$. (2) Decrease-key / insert: at most once per edge as it is relaxed — $O(E)$
relaxations, each $O(\log V)$, total $O(E\log V)$. Summing: $O((V+E)\log V)$. The naive
array-as-priority-queue version instead scans all $V$ vertices to find the minimum each round, giving
$\Theta(V^2)$; the heap replaces that linear scan with a logarithmic extract-min, which is the whole
speedup — decisive on sparse graphs where $E \ll V^2$. We prove $2 \Rightarrow 3$ and $3 \Rightarrow 4$; combined with the chapter's $1 \Leftrightarrow 2$
this closes the cycle $1 \Rightarrow 2 \Rightarrow 3 \Rightarrow 4 \Rightarrow 1$ (the last link,
$4 \Rightarrow 1$, is sketched). $2 \Rightarrow 3$ (unique paths $\Rightarrow$ minimally connected). Assume there is exactly one path
between every pair. "Exactly one" includes "at least one," so $G$ is connected. Now take any edge
$e = \{u, v\}$ and delete it. The edge $e$ was a $u$–$v$ path; by uniqueness it was the only one.
So after deleting $e$ there is no $u$–$v$ path at all, hence $G - e$ is disconnected. Thus removing any
edge disconnects $G$: it is minimally connected. ✓ $3 \Rightarrow 4$ (minimally connected $\Rightarrow$ maximally acyclic). Assume $G$ is connected and
removing any edge disconnects it. First, $G$ is acyclic: if $G$ had a cycle, any single edge $e$ of
that cycle could be removed and the two endpoints of $e$ would still be joined by the rest of the cycle,
so $G - e$ would stay connected — contradicting minimality. So $G$ is acyclic. Second, adding any
non-edge creates a cycle: let $u, v$ be non-adjacent. $G$ is connected, so there is a $u$–$v$ path $P$
(of length $\ge 2$, since they are non-adjacent). Adding the edge $\{u, v\}$ together with $P$ forms a
cycle. So $G$ is maximally acyclic. ✓ $4 \Rightarrow 1$ (sketch). A maximally acyclic graph is acyclic by assumption; it is also connected,
for if two vertices $u, v$ lay in different components, adding $\{u,v\}$ would join components without
creating a cycle (no path between them existed) — contradicting maximality. Connected + acyclic = tree.
$\blacksquare$ All four conditions are therefore equivalent. Claim. For every integer $i \ge 0$: $\lfloor ((2i+1)-1)/2 \rfloor = i$ and
$\lfloor ((2i+2)-1)/2 \rfloor = i$. Proof. Left child: $\dfrac{(2i+1)-1}{2} = \dfrac{2i}{2} = i$, an integer, so its floor is $i$.
Right child: $\dfrac{(2i+2)-1}{2} = \dfrac{2i+1}{2} = i + \dfrac{1}{2}$; since $i$ is an integer,
$\lfloor i + \tfrac12 \rfloor = i$ (Chapter 9: $\lfloor n + r\rfloor = n$ for integer $n$ and
$0 \le r < 1$). Both children of the node at index $i$ therefore report parent index $i$, so the
Why completeness makes the indices gap-free: a binary heap is a complete binary tree — every
level is full except possibly the last, which is filled left to right. That is exactly the condition under
which the breadth-first numbering $0, 1, 2, \dots$ assigns a node to every array slot with no holes: if
the last level had a gap (a missing node before a present one), some index in the middle of the array
would correspond to no node and the child/parent arithmetic would point at empty slots. Completeness
forbids such gaps, so the array is packed and the index formulas land on real nodes. Claim $Q(d)$. A perfect binary tree of height $h$ has exactly $2^d$ nodes at depth $d$, for each
$0 \le d \le h$. Proof (induction on $d$). Base case ($d = 0$): the only node at depth 0 is the root, so there is
$1 = 2^0$ node. ✓ Inductive step: assume there are $2^d$ nodes at depth $d$ for some $d < h$. Since
$d < h$ and the tree is perfect, every depth-$d$ node is internal and so has exactly two children,
all of which sit at depth $d + 1$; and every depth-$(d+1)$ node is the child of some depth-$d$ node.
Hence the number of depth-$(d+1)$ nodes is $2 \times 2^d = 2^{d+1}$. ✓ By induction, $Q(d)$ holds for all
$0 \le d \le h$. $\blacksquare$ Total via geometric series. Summing over all depths,
$$\sum_{d=0}^{h} 2^d = 2^{h+1} - 1$$
(the standard geometric-sum identity, itself proved by induction in Chapter 6, Exercise 6.7). So a perfect
tree of height $h$ has $2^{h+1} - 1$ nodes — recovering the upper bound of Exercise 31.11, now as an
exact equality because perfection makes every level full. Full solutions to the remaining even-numbered exercises (31.2, 31.4, 31.6, 31.8, 31.10, 31.12, 31.14,
31.16, 31.18, 31.20, 31.22, 31.25, 31.28, 31.30, 31.32) are reserved for the instructor solution set, per
the book convention (only † and odd-numbered are published in Office-graph reminder: $A=0,B=1,C=2,D=3,E=4$; $w(AB)=2, w(AC)=3, w(BC)=1, w(BD)=4, w(CE)=5, w(DE)=6$;
its MST is $\{BC,AB,BD,CE\}$ of weight $12$. 32.1 † (a) Any spanning tree has $n-1 = 20-1 = 19$ edges. (b) Left out: $50 - 19 = 31$ edges.
(c) Adding one left-out edge to a spanning tree creates exactly one cycle (its endpoints were
already joined by a unique tree path). 32.3 † (a) $\{AB,AC,BD,CE\}$: $2+3+4+5 = 14$. (b) $\{BC,AB,BD,CE\}$: $1+2+4+5 = 12$. (c)
$\{BC,AC,BD,CE\}$: $1+3+4+5 = 13$. Only (b) is an MST (weight $12$, matching §32.2). (a) and (c)
each use the redundant heavier edge $AC$ instead of $BC$/$AB$. 32.5 † Cut $S=\{A,D\}$ vs. $\{B,C,E\}$. Crossing edges (one endpoint in each side): $AB(2)$
($A\in S, B\notin S$), $AC(3)$, $BD(4)$ ($D\in S, B\notin S$), $DE(6)$ ($D\in S, E\notin S$). (Edge
$BC$ has both endpoints outside $S$; $CE$ likewise.) The light edge is $AB(2)$, the minimum-weight
crossing edge. 32.6 † Sorted edges: $PR(1), QR(2), RT(3), PQ(4), RS(5), ST(6), QS(7)$. MST $= \{PR, QR, RT, RS\}$, total $1+2+3+5 = 11$. (Stop after 4 edges; $ST, QS$ never examined.) 32.7 Prim from $P$. Tree $\{P\}$: crossing $PQ(4), PR(1)$ → take $PR(1)$, add $R$. Tree $\{P,R\}$:
crossing $PQ(4), QR(2), RS(5), RT(3)$ → take $QR(2)$, add $Q$. Tree $\{P,Q,R\}$: crossing
$QS(7), RS(5), RT(3)$ → take $RT(3)$, add $T$. Tree $\{P,Q,R,T\}$: crossing $QS(7), RS(5)$ → take
$RS(5)$, add $S$. Tree edges $\{PR, QR, RT, RS\}$, total $1+2+3+5 = 11$ — same as Kruskal (32.6). ✓ 32.8 † Every spanning tree of a connected $5$-vertex graph has exactly $n-1 = 4$ edges, regardless
of weights. So the largest and smallest number of edges in an MST are both 4 — the count is fixed
by the vertex count, not the weights. (If the graph were disconnected the answer would be "fewer than 4,
forming a spanning forest," but a spanning tree presupposes connectivity.) 32.9 Start: 32.10 † By the cycle property, an edge is in no MST iff it is the unique heaviest edge of some
cycle. The office graph's cycles and their heaviest edges:
- Triangle $A\text{–}B\text{–}C$ (weights $AB=2, BC=1, AC=3$): heaviest is $AC(3)$ → in no MST.
- Square $B\text{–}D\text{–}E\text{–}C\text{–}B$ (weights $BD=4, DE=6, CE=5, BC=1$): heaviest is
$DE(6)$ → in no MST.
- The outer pentagon $A\text{–}B\text{–}D\text{–}E\text{–}C\text{–}A$ (weights $2,4,6,5,3$): heaviest is
again $DE(6)$ (already excluded).
So exactly $AC$ and $DE$ are in no MST — consistent with the unique MST $\{BC,AB,BD,CE\}$. 32.11 † The four weights $1+2+4+5 = 12$, the office-graph MST weight. (See 32.13 † Make Kruskal skip the early-stop and report component count (full code in
Two disjoint components $\{0,1\}$ and $\{2,3\}$; the result is one MST per component (a minimum spanning
forest), total $1+2=3$, two components. 32.15 † The flaw: "any greedy cycle-avoiding method gives an MST" is false. Adding edges in
descending order while avoiding cycles does not give an MST. Run it on the office graph: process
$DE(6), CE(5), BD(4), AC(3), AB(2), BC(1)$. Add $DE$, add $CE$, add $BD$, add $AC$ — now we have 4 edges
$\{DE,CE,BD,AC\}$ connecting all five vertices? Check: $DE$ joins $D,E$; $CE$ joins $C$; $BD$ joins $B$;
$AC$ joins $A$ — yes, 4 edges, spanning, total $6+5+4+3 = 18 > 12$. So descending "add if no cycle" gives
a (sub-optimal) spanning tree of weight $18$, not the MST. The correct descending-order algorithm is
reverse-delete (delete the heaviest edge whenever removal keeps the graph connected), which is a
different procedure and does give an MST (Exercise 32.31). Greedy-by-largest-first is not Kruskal. 32.17 † The gap: the step "remove any edge $e'$ on the cycle" is too loose, because an arbitrary
cycle edge need not satisfy $w(e) \le w(e')$, so the weight comparison $w(T') \le w(T)$ can fail. The
precise requirement (from §32.5): $e'$ must be a crossing edge of the same cut $(S, V\setminus S)$.
The proof guarantees such an $e'$ exists: the unique tree path between $e$'s endpoints $u\in S$ and
$v\in V\setminus S$ must cross the cut, so it contains a crossing edge $e'$, and that $e'$ lies on the
cycle. Since $e$ is a light (minimum-weight) crossing edge, $w(e) \le w(e')$ for that crossing $e'$,
which is exactly what the weight comparison needs. Removing a non-crossing cycle edge would not justify
$w(e) \le w(e')$. 32.19 † Filled blanks and full argument (uniqueness under distinct weights):
- Among edges in exactly one of $T_1, T_2$, let $e$ be the one of minimum weight; WLOG $e\in T_1,
e\notin T_2$.
- Add $e$ to $T_2$: creates exactly one cycle $C$ because $e$'s endpoints are already joined by a
unique path in the tree $T_2$ (adding any non-tree edge to a spanning tree makes one cycle, §32.1).
- $C$ must contain an edge $e'\notin T_1$ — otherwise $C\subseteq T_1$, but $T_1$ is a tree and has no
cycle, contradiction. By the choice of $e$ as the minimum-weight differing edge, $w(e) <
w(e')$ (here $e'$ is a differing edge in $T_2\setminus T_1$, weights distinct so strict). Care: the standard clean form takes $e$ = minimum-weight edge in the symmetric difference; then any
other differing edge $e'$ has $w(e') > w(e)$.
- Form $T_2' = (T_2\setminus\{e'\})\cup\{e\}$, a spanning tree (one cycle broken) with $w(T_2') =
w(T_2) - w(e') + w(e) < w(T_2)$. This contradicts **$T_2$ being an MST (minimum weight)**. Hence no two
distinct MSTs exist — the MST is unique. $\blacksquare$ 32.21 † Claim: a spanning tree $T$ is an MST iff every non-tree edge $e=(u,v)$ is a maximum-weight
edge on the cycle it forms with the tree path between $u$ and $v$. (⇐, the useful direction.) Suppose every non-tree edge is max-weight on its tree cycle. Take any MST
$T^\*$. We show $w(T) = w(T^\*)$. Consider any edge $f \in T^\* \setminus T$. Adding $f$ to $T$ forms a
cycle $C_f$ on which (by hypothesis) $f$ is a maximum-weight edge; pick a tree edge $g$ on $C_f$ with
$g\notin T^\*$ (it exists, as $C_f\not\subseteq T^\*$). Swapping gives a spanning tree no heavier than
$T$ and closer to $T^\*$. Iterating transforms $T^\*$ into $T$ without increasing weight, so
$w(T)\le w(T^\*)$; since $T^\*$ is minimum, $w(T) = w(T^\*)$ and $T$ is an MST. (⇒.) Suppose $T$ is an MST but some non-tree edge $e=(u,v)$ is not a maximum-weight edge of its tree
cycle — i.e. some tree edge $g$ on that cycle has $w(g) > w(e)$. Then $(T\setminus\{g\})\cup\{e\}$ is a
spanning tree of strictly smaller weight (the cycle edge $g$ is replaced by the cheaper $e$, and removing
$g$ from the cycle keeps the tree connected), contradicting minimality. So every non-tree edge is
max-weight on its cycle. $\blacksquare$ (This is the "no improving swap" characterization.) 32.23 † Model. Object: a connected weighted graph $G=(V,E,w)$ with $V$ = the $9$ buildings,
$E$ = the pairs that can be trenched, and $w(e)$ = the trenching cost of route $e$. Optimize:
minimize the total weight of a spanning tree $\sum_{e\in T} w(e)$ subject to $T$ connecting all $9$
buildings (a minimum spanning tree). Algorithm: run Kruskal (or Prim) — e.g. the toolkit's
32.25 † Conjecture. (i) Kruskal adds exactly $n-1$ edges (a spanning tree). (ii) In the worst
case Kruskal examines all $E$ edges (it may inspect every edge before completing, e.g. when the
heaviest edge is needed). Test (sketch): instrument the loop with two counters and run on, say, a
triangle ($n=3, E=3$: adds 2, examines 3 if the last is a cycle), a path ($n=4, E=3$: adds 3, examines
3), and a graph where the costliest edge is in the MST (examines all $E$). Proof that adds $= n-1$:
Kruskal adds an edge only when it joins two different components, which decreases the component count by
exactly one; it starts at $n$ components (singletons) and a connected spanning tree has $1$ component, so
the number of decreasing steps — i.e. edges added — is exactly $n-1$. $\blacksquare$ 32.27 † Claim: adding any single non-tree edge $e=(u,v)$ to a spanning tree $T$ of $G$ creates
exactly one cycle. Proof using the $n-1$ fact twice. $T$ has $n$ vertices and (fact #1) $n-1$ edges,
and is connected and acyclic. The graph $T+e$ has $n$ vertices and $n$ edges and is still connected
(adding an edge cannot disconnect). A connected graph on $n$ vertices with $n$ edges has exactly one
cycle: a connected acyclic graph would have only $n-1$ edges (fact #2), so the one extra edge forces at
least one cycle; and it forces exactly one because $e$'s endpoints $u,v$ were already joined by a
unique path $p$ in the tree, and the only cycle in $T+e$ is $e$ together with $p$ (any cycle must use
$e$, since $T$ alone is acyclic, and the rest of such a cycle is a $u$–$v$ path in $T$, which is unique).
Hence exactly one cycle. $\blacksquare$ 32.29 † Claim: throughout Prim's algorithm, the set $S$ of vertices added, together with the
chosen tree edges, is connected. Let $P(k)$ = "after $k$ vertices have been added, the $k$ vertices and
the $k-1$ chosen edges form a connected subtree." Base ($k=1$): $S$ is the single start vertex with
$0$ edges — trivially connected. Step: assume $P(k)$: the $k$ added vertices form a connected tree $T_k$
with $k-1$ edges. Prim's next move adds the minimum-weight edge $(x,y)$ with $x\in S$ (in $T_k$) and
$y\notin S$. The new vertex $y$ attaches to $T_k$ via the edge $(x,y)$ at $x$, so every vertex of
$S\cup\{y\}$ is still reachable (through $x$). The result is a connected subgraph on $k+1$ vertices with
$k$ edges — a tree — establishing $P(k+1)$. By induction, $S$ is connected at every step. $\blacksquare$
(This is why Prim grows one tree, in contrast to Kruskal's forest.) 32.31 (Reverse-delete is correct.) Reverse-delete sorts edges descending and deletes each edge
whose removal keeps the graph connected. Claim: the surviving edges form an MST. Argument via the
cycle property: when reverse-delete deletes an edge $e$, removal keeps the graph connected, so $e$ lay
on a cycle at that moment (an edge whose removal preserves connectivity is not a bridge, hence on a
cycle); and because we process in descending weight, $e$ is the heaviest remaining edge on that cycle —
by the cycle property it is in no MST, so deleting it is safe (no MST uses it). When reverse-delete
keeps an edge $e$, removal would have disconnected the graph, so $e$ is a bridge of the current
graph and must be in every spanning tree of what remains — hence safe to keep. The process never
disconnects the graph and ends when no further deletion is possible, i.e. when the remaining edges form a
spanning tree; since every deletion removed a non-MST edge and every kept edge is forced, the result is
an MST. Where "keeps connected" is used: it is the test that distinguishes a heaviest-cycle edge (safe
to delete) from a bridge (must keep) — exactly the connectivity condition the cycle property needs.
$\blacksquare$ Solutions to the starred-with-a-dagger and odd-numbered exercises from
(a) $\chi(K_6) = 6$: all six vertices are pairwise adjacent, so all need distinct colors.
(b) $\chi(C_{10}) = 2$: $10$ is even, so the two colors alternate around the ring with no collision.
(c) $\chi(C_9) = 3$: $9$ is odd, and an odd cycle cannot be 2-colored (the alternation collides on
wrap-around), but 3 colors suffice.
(d) A tree on 12 vertices has $\chi = 2$: a tree has no cycles, hence no odd cycle, so by Theorem 33.3 it
is bipartite, and having an edge it needs exactly 2 colors.
(e) The empty graph on 7 vertices has $\chi = 1$: with no edges there is no constraint, so one color
colors everything. Euler's formula for a connected plane graph is $V - E + F = 2$. With $V = 8$ and $F = 5$:
$$8 - E + 5 = 2 \implies E = 8 + 5 - 2 = 11.$$
So $E = 11$ edges. Vertices $0,1,2,3,4$ around the ring (edges $0\!-\!1\!-\!2\!-\!3\!-\!4\!-\!0$). A proper 3-coloring:
$$c(0)=1,\ c(1)=2,\ c(2)=1,\ c(3)=2,\ c(4)=3.$$
Check the five edges: $01\,(1,2)$, $12\,(2,1)$, $23\,(1,2)$, $34\,(2,3)$, $40\,(3,1)$ — all differ. No
2-coloring exists because $C_5$ is an odd cycle: alternating two colors around an odd ring forces the
last edge ($40$) to join two equal colors, so by Theorem 33.3 ($\chi=2 \iff$ no odd cycle) it is not
2-colorable. Live ranges (defined → last used): Interference edges (overlapping ranges): Largest clique: $\{p, q, r\}$ are pairwise adjacent (all alive at line 3), so $\omega = 3$ and $\chi \ge
3$. A proper 3-coloring: $p!=!R1,\ q!=!R2,\ r!=!R3,\ s!=!R1$ ($s$ reuses $R1$ with $p$, legal
since $ps$ is a non-edge). Check: $pq\,(R1,R2)$, $pr\,(R1,R3)$, $qr\,(R2,R3)$, $qs\,(R2,R1)$,
$rs\,(R3,R1)$ — all differ. So $\chi = 3$: 3 registers suffice and are necessary. For a connected simple triangle-free planar graph with $V \ge 3$, Theorem 33.5 gives $E \le 2V - 4$.
With $V = 11$:
$$E \le 2(11) - 4 = 18.$$
So at most 18 edges. (The ordinary bound $3V - 6 = 27$ is weaker here; triangle-freeness is what
tightens it.) Vertices = access points $1,2,3,4,5$; edges = overlap pairs ${1,2},{1,3},{2,3},{2,4},{3,4},
{4,5}$; colors = channels. This is the same graph as the chapter's opening exam example (relabel
$A,B,C,D,E \to 1,2,3,4,5$). The triangle $\{1,2,3\}$ forces $\chi \ge 3$. A proper 3-coloring:
$$\{1,4\}\to\text{ch }1,\quad \{2,5\}\to\text{ch }2,\quad \{3\}\to\text{ch }3.$$
Check: 1–2 (1,2), 1–3 (1,3), 2–3 (2,3), 2–4 (2,1), 3–4 (3,1), 4–5 (1,2) — all differ. Minimum =
3 channels. Filled blanks:
- $\lvert S \rvert = \boldsymbol{\omega(G)}$.
- $\{u,v\} \in \boldsymbol{E}$ (the edge set); $c(u) \boldsymbol{\neq} c(v)$.
- Therefore $c$ assigns pairwise distinct colors to the vertices of $S$, so $c$ uses at least
$\boldsymbol{\lvert S \rvert = \omega(G)}$ colors.
- Since $c$ was arbitrary, $\chi(G) \ge \boldsymbol{\omega(G)}$. $\blacksquare$ Full statement: a largest clique $S$ has $\lvert S\rvert = \omega(G)$ pairwise-adjacent vertices; any
proper coloring must give them all different colors (each pair is an edge), hence uses $\ge \omega(G)$
colors; as this holds for every proper coloring, $\chi(G)\ge\omega(G)$. Two colors are not enough. $C_n$ with $n$ odd is itself an odd cycle. By Theorem 33.3, a graph is
2-colorable iff it has no odd cycle; $C_n$ is an odd cycle, so it is not bipartite and $\chi(C_n) \ge
3$. (Directly: a 2-coloring must alternate colors along the path $v_0 v_1 \dots v_{n-1}$, forcing
$c(v_i)$ to depend on the parity of $i$; then the closing edge $v_{n-1}v_0$ joins $v_{n-1}$ (parity
$n-1$, even) and $v_0$ (parity 0, even) — same color — a contradiction.) Three colors suffice. Color $v_0, v_1, \dots, v_{n-2}$ by alternating $1,2,1,2,\dots$; since $n-1$ is
even, $v_{n-2}$ gets color $1$ and $v_0$ gets color $1$. Give the last vertex $v_{n-1}$ color $3$. Its
two neighbors are $v_{n-2}$ (color 1) and $v_0$ (color 1), both $\ne 3$, and every other edge alternates
$1/2$. Proper, 3 colors. Hence $\chi(C_n) = 3$. $\blacksquare$ Lemma (a low-degree vertex exists). Every simple planar graph has a vertex of degree $\le 5$.
Proof. Suppose not — every vertex has degree $\ge 6$. By the handshaking lemma (Ch. 27),
$2E = \sum_v \deg(v) \ge 6V$, so $E \ge 3V$. But for a simple planar graph with $V \ge 3$, Theorem 33.5
gives $E \le 3V - 6 < 3V$. (For $V \le 2$ the claim is trivial — degrees are at most 1.) Contradiction.
So some vertex has degree $\le 5$. $\blacksquare$ Main proof (induction on $V$). Let $P(n)$: "every simple planar graph on $n$ vertices is
6-colorable."
Base case ($n \le 6$): at most 6 vertices, give each its own color — trivially 6-colorable.
Inductive step: assume $P(n-1)$. Let $G$ be planar on $n$ vertices. By the lemma, $G$ has a vertex $v$
with $\deg(v) \le 5$. Delete $v$; the result $G - v$ is planar on $n-1$ vertices, so by the inductive
hypothesis it has a proper 6-coloring. Restore $v$: it has at most 5 colored neighbors, so at most 5 of
the 6 colors are forbidden, leaving at least one color free for $v$. Coloring $v$ that color extends the
proper coloring to all of $G$. By induction, every planar graph is 6-colorable. $\blacksquare$ Trace on $C_5$, Result Trace on $C_4$ Trace on $C_5$ The flaw is the asserted upper bound "$\chi(G) \le \omega(G)$" ("you never need more colors than the
worst mutual conflict"). That is false. Cliques force colors, but they are not the only structure
that does. The smallest counterexample from the chapter is the 5-cycle $C_5$: it is triangle-free, so
$\omega(C_5) = 2$, yet $\chi(C_5) = 3$ (an odd cycle cannot be 2-colored). So $\chi(C_5) = 3 > 2 =
\omega(C_5)$, refuting equality. Only the *lower* bound $\chi \ge \omega$ (Theorem 33.1) is valid. The false premise is "every graph can be colored with at most four colors." The Four Color Theorem
applies only to planar graphs: it states every planar graph is 4-colorable ($\chi \le 4$ for planar
$G$). Non-planar graphs can need far more — $K_5$ needs 5, $K_{100}$ needs 100. An exam-scheduling graph
with $\chi = 6$ is simply not planar (or at least not 4-colorable), and there is no arithmetic error to
find: six periods really are required. The theorem says nothing about it, because conflict graphs are not
constrained to be planar. The student must accept $\chi = 6$. Vertices = the 6 courses {CS, MATH, PHYS, CHEM, BIO, ENG}. Edges = "share a student" pairs:
CS–MATH, CS–PHYS, MATH–PHYS, PHYS–CHEM, CHEM–BIO, BIO–ENG, ENG–CHEM. Colors = exam periods. The triangle $\{$CS, MATH, PHYS$\}$ (all three pairwise adjacent) forces $\chi \ge 3$. Also
$\{$PHYS, CHEM, ENG$\}$? PHYS–CHEM ✓, CHEM–ENG ✓, PHYS–ENG? not listed — not a triangle. And
$\{$CHEM, BIO, ENG$\}$: CHEM–BIO ✓, BIO–ENG ✓, CHEM–ENG ✓ — another triangle. So two triangles, each
forcing $\ge 3$. A proper 3-coloring:
$$\text{CS}\to1,\ \text{MATH}\to2,\ \text{PHYS}\to3,\ \text{CHEM}\to1,\ \text{BIO}\to2,\ \text{ENG}\to3.$$
Check every edge: CS–MATH(1,2), CS–PHYS(1,3), MATH–PHYS(2,3), PHYS–CHEM(3,1), CHEM–BIO(1,2),
BIO–ENG(2,3), ENG–CHEM(3,1) — all differ. Minimum = 3 periods. For a two-period schedule to be possible, the graph would have to be 2-colorable, i.e. bipartite,
i.e. contain no odd cycle (Theorem 33.3). It does contain odd cycles (the triangles), so two periods
are impossible. Run on $\text{Crown}(n)$ with the adversarial order $a_1,b_1,a_2,b_2,\dots$ (Trace $n=3$ as in §33.6: $a_1\!\to\!0, b_1\!\to\!0, a_2\!\to\!1, b_2\!\to\!1, a_3\!\to\!2, b_3\!\to\!2$
— 3 colors. The pattern: the $k$-th pair $(a_k,b_k)$ is forced to color $k-1$ because $a_k$'s neighbors
$b_1,\dots,b_{k-1}$ already carry $0,\dots,k-2$, and likewise $b_k$.) Conjectures: greedy on the bad order uses $n$ colors; $\chi(\text{Crown}(n)) = 2$. Proof that $\chi(\text{Crown}(n)) = 2$. The crown graph is bipartite: every edge $a_i b_j$ goes
between the $a$-side and the $b$-side, with no edges within a side. Color all $a_i$ with 1 and all $b_j$
with 2; no edge is monochromatic. Since it has an edge, $\chi = 2$ (Theorem 33.3). $\blacksquare$ Proof that greedy uses $n$ colors on the order $a_1,b_1,\dots,a_n,b_n$. Claim by induction on $k$:
$a_k$ and $b_k$ both receive color $k-1$. Base ($k=1$): $a_1$ has no colored neighbors → 0; $b_1$'s
neighbors are $a_2,\dots,a_n$ (recall $a_1 b_1$ is not an edge, since $i\ne j$ is required), none yet
colored → 0. Step: assume $a_j, b_j$ got color $j-1$ for all $j < k$. When $a_k$ is colored, its colored
neighbors are $b_1,\dots,b_{k-1}$ (it is adjacent to every $b_j$ with $j \ne k$, and only those with
$j This is the §33.6 lesson quantified: greedy's count exceeds $\chi$ by an unbounded amount on a bad
order, while a good order (all $a$'s, then all $b$'s) finds the optimal 2-coloring. (The full proof is Exercise 33.17; here we only lay out the skeleton, as Chapter 6 teaches: state $P(n)$,
the hypothesis, the base, and the one fact that drives the step.) Deciding 2-colorability is easy (polynomial, in fact linear): it equals testing bipartiteness,
which a single BFS/DFS settles by coloring vertices by layer parity and checking for a within-layer edge
(an odd cycle). This is on the easy side of Chapter 30's boundary, like Euler paths. Deciding 3-colorability is hard: it is NP-complete (formalized in Chapter 37), on the same side
as Hamilton paths / TSP. The two differ because 2-colorability has a clean structural characterization
(no odd cycle) that yields a fast algorithm, whereas no such efficient test is known for 3 colors — the
jump from 2 to 3 is exactly where the difficulty appears (mirroring §33.5's note that even planar
3-colorability stays NP-complete while planar 4-colorability is guaranteed). Claim. $G$ can be partitioned into $k$ independent sets $\iff$ $G$ has a proper $k$-coloring; hence
$\chi(G)$ is the minimum number of independent sets needed to cover $V$. Coloring $\Rightarrow$ partition. Given a proper $k$-coloring $c$, let $V_i = c^{-1}(i)$ be color class
$i$. No edge has both endpoints in the same class (properness), so each $V_i$ is an independent set, and
$V = V_1 \cup \dots \cup V_k$ is a partition into $k$ independent sets. Partition $\Rightarrow$ coloring. Given a partition $V = S_1 \cup \dots \cup S_k$ into independent sets,
color every vertex of $S_i$ with color $i$. Any edge joins two different parts (both endpoints in one
$S_i$ would contradict independence), so its endpoints get different colors — the coloring is proper. Therefore the fewest colors ($\chi$) equals the fewest independent sets covering $V$. $\blacksquare$ Interpretation. For a community organizer, $\chi(G)$ of the friendship graph is the fewest groups of
mutual strangers into which the network splits — each color class is a set with no internal friendships,
i.e. a panel that fully "mixes the room." This is the separating view of §33.2. The opposite goal —
finding one large tightly-knit group, a big clique of mutual friends — is what the Chapter 39 Track B
analyzer also computes; coloring partitions into strangers, clique-finding extracts a community, and the
two are dual lenses on the same graph. Even-numbered and non-daggered exercises (33.2, 33.4, 33.6, 33.8, 33.10, 33.12, 33.14, 33.16, 33.18,
33.20, 33.22, 33.24, 33.26, 33.28, 33.30, 33.32, 33.34) are left for the reader; their methods mirror the
solved problems above. Network $M$: $c(s,a)=10,\ c(s,b)=3,\ c(a,b)=4,\ c(a,t)=3,\ c(b,t)=10$.
Network $N$: all five capacities $=3$. 34.1 †
(a) Not valid. Capacity is fine, but conservation fails at $a$: in $=f(s,a)=4$, out
$=f(a,b)+f(a,t)=4+0=4$ — that part is OK; check $b$: in $=f(a,b)+f(s,b)=4+3=7$, out $=f(b,t)=7$ — OK
too. Recheck $a$: in 4, out 4, OK. So actually conservation holds; the first (and only) broken
constraint is none on capacity/conservation — but the value out of $s$ is $4+3=7$ while into $t$ is
$0+7=7$, consistent. (a) is valid, value 7. (Trap: always check both vertices.)
(b) Not valid — conservation at $a$: in $=f(s,a)=5$, out $=f(a,b)+f(a,t)=2+3=5$ OK; at $b$: in
$=f(a,b)+f(s,b)=2+2=4$, out $=f(b,t)=4$ OK; value out of $s=5+2=7$, into $t=3+4=7$. (b) is valid,
value 7.
(c) Not valid. First broken constraint is capacity: $f(s,a)=11>10=c(s,a)$.
(Note: parts (a) and (b) are deliberately valid to train the habit of checking rather than assuming;
only (c) breaks a constraint.) 34.3 † Cut $S=\{s,b\}$, $T=\{a,t\}$. Edges from $S$ to $T$: $s\to a$ (cap 10) and $b\to t$ (cap 10).
The edge $s\to b$ stays inside $S$; $a\to b$ and $a\to t$ go from $T$, not counted. So
$c(S,T)=10+10=20$. Not a minimum cut: $20 > 10 = $ max flow. (The min cut is $\{s,a\}\mid\{b,t\}$,
capacity 10.) 34.5 † On $N$ with first path $s\to a\to t$:
- Iter 1: path $s\to a\to t$, bottleneck $\min(3,3)=3$. Flow value 3.
- Iter 2: path $s\to b\to t$, bottleneck $\min(3,3)=3$. Flow value 6.
- Iter 3: residual out of $s$: $s\to a$ saturated, $s\to b$ saturated; no forward room and no flow into
$s$ to cancel, so no augmenting path. Stop, max flow 6. Two augmenting paths, no back edge used. Contrast the chapter's diagonal-first trace, which needed a
back edge ($b\to a$) in iteration 2 to reroute. Lesson: the number of iterations and whether a back
edge is needed depend on path choices, but the final max-flow value (6) is invariant. 34.7 † (Builds on 34.6's max flow on $P$, value 5, with
$f(s,a)=3,f(s,b)=2,f(a,b)=2,f(a,c)=1,f(b,c)=3,f(c,t)=4,f(b,t)=1$.) Run a final BFS in the residual: from
$s$ you reach $a$ (residual $s\to a = 4-3=1$) and $b$ (via back edge $a\to b$? actually $s\to b$ is
saturated, but $a\to b$ has residual $3-2=1$ so $b$ is reachable from $a$). From $b$: $b\to c$ saturated
($3-3=0$), $b\to t$ saturated ($1-1=0$); back edges only return to visited vertices. So
$S=\{s,a,b\}$, $T=\{c,t\}$. Crossing $S\to T$ edges: $a\to c$ (cap 1), $b\to c$ (cap 3), $b\to t$ (cap 1).
$c(S,T)=1+3+1=5=$ max flow ✓. All three are saturated ($f(a,c)=1, f(b,c)=3, f(b,t)=1$), confirming the
theorem. 34.9 † Maximum matching by inspection, e.g. $\{1{-}w,\ 2{-}? \}$ — careful: $2$ only lists $w$, and
$w$ would be taken by $1$. Better: $\{2{-}w,\ 1{-}x,\ 3{-}y,\ 4{-}z\}$ — size 4, saturates $X$. Flow
network (§34.5 reduction): $s\to i$ cap 1 for $i\in\{1,2,3,4\}$; middle edges
$1{-}w,1{-}x,2{-}w,3{-}x,3{-}y,4{-}y,4{-}z$ cap 1; $j\to t$ cap 1 for $j\in\{w,x,y,z\}$. Max-flow value
$=4$, matching size 4 (every worker and every task used, so perfect). 34.10 † (Scaffolded.) Blanks:
- For $v\in S$, $v\neq s$, conservation gives net flow out $=$ 0.
- Hence $\lvert f\rvert = \sum_{v\in S}(\text{out}(v)-\text{in}(v))$ since $s$ contributes $\lvert f\rvert$
and all others contribute 0.
- An edge with both endpoints in $S$ contributes 0 (counted $+$ once as someone's out-flow, $-$ once
as someone's in-flow), so it cancels.
- The surviving terms are edges crossing into $T$, giving
$\lvert f\rvert = \sum_{u\in S,v\in T} f(u,v) - \sum_{u\in S,v\in T} f(v,u)$. $\blacksquare$ 34.11 Apply the value-across-a-cut identity (34.10) with $S=\{s\}$, $T=V\setminus\{s\}$:
$\lvert f\rvert = \sum_{v\neq s} f(s,v) - \sum_{v\neq s} f(v,s) \le \sum_{v\neq s} f(s,v)
\le \sum_{v\neq s} c(s,v) = c(s,\,V\setminus{s})$, using $f(v,s)\ge 0$ for the first inequality and the
capacity constraint for the second. $\blacksquare$ 34.12 † (Scaffolded.) Blanks:
- Base: the zero flow assigns 0 to every edge (an integer).
- Inductive step: residual capacities are sums/differences of integers, hence integers; so the
bottleneck $\Delta$ (a min of integers) is an integer, and pushing it keeps every $f(u,v)$ an
integer. $\blacksquare$
- One sentence: the matching reduction uses unit (integer) capacities, and only an integer flow has each
middle edge carrying 0 or 1 — i.e., corresponding to "unmatched / matched"; a fractional flow would not
describe a matching. 34.13 † The flaw: naive greedy halts when there is no augmenting forward path, but the theorem's
condition (ii) is "no augmenting path in the residual network" — which includes back edges. At
value 3 on $N$ (diagonal stuck), a residual augmenting path still exists ($s\to b\to a\to t$ using the
back edge $b\to a$), so condition (ii) is not met and the theorem does not apply. The claimant
conflated "no forward path" with "no residual path." (True max 6, reachable via the back edge.) 34.15 † The classmate is right; the $\infty$ version breaks the reduction. With middle edges of
capacity $\infty$, a single worker $x$ could carry several units out (one per job $y$ they connect to) —
if the source edge allowed it. The source edge $s\to x$ has capacity 1, so $x$ sends at most 1 unit
total, fine. The real break is on the right: the issue is integrality and which edges are saturated.
More precisely, capacity-$\infty$ middle edges mean a saturated/used middle edge no longer signals "this
pair is in the matching" cleanly — but the deeper failure appears once either side allows multiplicity.
The safe, standard reduction keeps all three capacities at 1 so that the flow-carrying middle edges
provably form a matching (the §34.5 proof uses unit capacity on $s\to x$ and $y\to t$; weakening the
middle to $\infty$ is tolerable only because the unit source/sink edges still bound each vertex to one
unit — but it buys nothing and invites error). Verdict: unit capacities on $s\to x$ and $y\to t$ are
what enforce the matching property; keep the standard reduction. (Accept answers that correctly identify
the source/sink unit capacities — not the middle — as the load-bearing constraint.) 34.17 † Bipartite matching / max flow. Left = nurses $\{$Ana, Ben, Cara, Dev, Eli$\}$. Right
= shifts $\{$Mon, Tue, Wed, Thu$\}$. Middle edges (eligibility, cap 1): Ana–Mon, Ana–Tue, Ben–Mon,
Cara–Tue, Cara–Wed, Dev–Wed, Dev–Thu, Eli–Thu. Source edges $s\to$ nurse, cap 1 (each nurse $\le 1$
shift). Sink edges shift $\to t$, cap 1 (each shift needs exactly one nurse). "All shifts covered"
$\iff$ max-flow value $=4$ (saturating the four shift-sink edges). (Note: there are 5 nurses for 4
shifts, so the sink side caps the flow at 4.) 34.19 † Computing the minimum cut of this network partitions the four pixels into $S$ (foreground)
and $T$ (background) so as to minimize total cut capacity = (background-likelihood edges $s\to p$ cut for
pixels put in $T$... ) — concretely, the cut pays a source edge $c(s,p_i)$ when $p_i$ is assigned to $T$
(background), pays a sink edge $c(p_i,t)$ when $p_i$ is assigned to $S$ (foreground), and pays the
smoothness penalty (2) for each adjacent pair split across the cut. The minimum cut balances per-pixel
evidence against boundary smoothness. The source side $S$ is the foreground (pixels still connected to
$s$). Given the strong foreground evidence on $p_1,p_4$ and strong background evidence on $p_2,p_3$, the
likely segmentation is $S=\{p_1,p_4\}$... but adjacency penalties (a row $p_1p_2p_3p_4$) discourage
isolating $p_4$ from $p_3$, so the conceptual point is what matters: the min cut is the segmentation. 34.20 † (Conjecture and test, then prove.) Computed matching sizes: $K_{1,3}\to 1$, $K_{2,3}\to 2$,
$K_{3,3}\to 3$, $K_{2,5}\to 2$. Conjecture: the maximum matching of $K_{m,n}$ has size
$\min(m,n)$. Proof. Upper bound: in the reduction the $m$ source edges have total capacity $m$ and
the $n$ sink edges total capacity $n$, so the max flow — hence the matching — is at most $\min(m,n)$
(weak duality with the source cut $\{s\}$ or the sink cut $V\setminus\{t\}$). Lower bound: WLOG
$m\le n$; since every left vertex is joined to every right vertex, pair left vertex $i$ with right vertex
$i$ for $i=1,\dots,m$ — these $m$ edges share no vertex, a matching of size $m=\min(m,n)$. Both bounds
meet, so the maximum is exactly $\min(m,n)$. $\blacksquare$ 34.21 (Network with $c(s,a)=c(a,t)=c(s,b)=c(b,t)=k$, $c(a,b)=k$.) Max flow $=2k$ for every $k$: send
$k$ along $s\to a\to t$ and $k$ along $s\to b\to t$, ignoring the cross edge. Lower bound: that is a
valid flow of value $2k$ (each used edge carries exactly its capacity $k$, conservation holds). Upper
bound (matching cut): $S=\{s\}$ has cut capacity $c(s,a)+c(s,b)=2k$. Flow $2k$ = cut $2k$ ⇒ both
optimal. So scaling $k$ scales the max flow linearly as $2k$; the cross edge $a\to b$ is never needed.
Conjecture confirmed: max flow $=2k$. 34.22 † (Conjecture and test.) On $M$ the base max flow is 10. Doubling all capacities gives max flow
20; tripling gives 30. Conjecture: scaling every capacity by a positive integer $k$ scales the
max flow by $k$ (here $10k$). Proof. Let $f$ be a max flow on $M$ with value 10 and $(S,T)$ the matching
min cut of capacity 10. (i) $kf$ is valid on the scaled network: $0\le kf(u,v)\le k\,c(u,v)$ (scaled
capacity), and conservation is linear, so each vertex still balances; its value is $k\cdot 10 = 10k$.
(ii) the same cut now has capacity $\sum_{u\in S,v\in T} k\,c(u,v) = k\cdot 10 = 10k$. A flow of $10k$
and a cut of $10k$ certify each other (weak duality), so the scaled max flow is exactly $10k$.
$\blacksquare$ (More generally, scaling all capacities by $k>0$ scales max flow by $k$.) 34.23 † ```python
def cut_capacity(cap, S):
return sum(c for (u, v), c in cap.items() if u in S and v not in S) Traces: 35.15 † Complementing the parity DFA ($Q=\{E,O\}$, $F=\{E\}$) gives accept $=\{O\}$, i.e. the machine now accepts
an odd number of $1$s. 35.17 † The error: the student chose the split $x,y,z$. The pumping lemma does not let the prover
pick the split; it guarantees only that some valid split exists. For the regular language
$L_{\text{even}}$ a good split exists — e.g. $y=11$ (two $1$s), whose repetition preserves parity, so
$xy^{i}z$ stays in $L_{\text{even}}$ for all $i$ — and exhibiting one bad-looking split (here $y=1$, a
single $1$) proves nothing. The lemma can only show a language is not regular, by defeating every
split of a well-chosen $s$; it can never be used to show a language is non-regular by exhibiting one bad
split. (Subtler point: even the correct use of the lemma is a necessary condition for regularity, not a
sufficient one — passing it would not prove regularity either.) 35.19 † The violation: a DFA's $\delta$ must be a total function on $Q\times\Sigma$ (§35.2), but
the given machine has no transition for $\delta(A,1)$. With a missing entry the machine is not
deterministic-and-total, so it is not a DFA. Fix with a dead state $d$: add $\delta(A,1)=d$ (the only
missing cell), plus $\delta(d,0)=\delta(d,1)=d$, with $d\notin F$. Now every state has an arrow for every
symbol: $2$ original states would need $2\times 2=4$ cells (only $3$ were given), and the dead state adds
its own $2$, for a total $6$ filled cells over $3$ states. The dead state absorbs the previously undefined
$(A,1)$ case and loops forever, non-accepting. 35.21 † (Model it — exact-15-cents vending machine.) Alphabet $\Sigma=\{n,d\}$ ($n=5$, $d=10$).
States track cents so far: $q_0$ (0), $q_5$ (5), $q_{10}$ (10), $q_{15}$ (accept, exactly 15), and a dead
state $X$ (overshoot/after-dispense). Transitions:
| $\delta$ | on 35.23 † (Conjecture and test, then prove.) "$k$-th symbol from the end is a The NFA needs $k+1$ states (a start that loops and guesses, then a chain of $k$); the minimal DFA must
remember the last $k$ symbols, of which there are $2^{k}$ distinct possibilities, so $a_k = 2^{k}$ and
$b_k = k+1$. Conjecture: $a_k = 2^{k}$, $b_k = k+1$. Proof of the bound $a_k \le 2^{k}$: the natural
NFA has $k+1$ states, so by the subset construction (§35.3) it converts to a DFA with at most
$2^{\,k+1}$ states — but more tightly, a DFA can decide "$k$-th from the end is 35.25 † (Conjecture and test, then prove — reversal closure.) Conjecture: the regular languages
are closed under reversal. Test (sketch): take $L=L(\,10^{*}\,)=\{1,10,100,\dots\}$ (a 35.26 † (Ch. 27 + 35.) A DFA with $\lvert Q\rvert$ states over an alphabet of size
$\lvert\Sigma\rvert$ has exactly $\lvert Q\rvert\cdot\lvert\Sigma\rvert$ transitions (labeled edges).
The count is exact because $\delta$ is a total function on $Q\times\Sigma$: every (state, symbol) pair
has exactly one outgoing edge — no fewer (totality) and no more (determinism / single-valuedness). So it
is not a bound but an equality, the same reasoning as counting edges by summing out-degrees in a directed
graph (Ch. 27). 35.27 (Ch. 17 + 35.) The pigeonhole principle (Ch. 17): if $n+1$ objects are placed into $n$
boxes, some box holds at least two objects. In the pumping lemma, take a DFA for $L$ with $p$ states and
feed it an accepted string of length $\ge p$; reading the first $p$ symbols visits $p+1$ states (counting
the start). The pigeons are those $p+1$ visited state-instances; the holes are the $p$ states;
the conclusion is that some state repeats. The input read between the two visits to that repeated state
is a loop $y$ that can be traversed any number of times without changing the final state — which is exactly
the pumpable middle of conditions (1)–(3). 35.28 † (Ch. 12 + 35.) Define $x\sim y$ iff $M$ drives both $x$ and $y$ from $q_0$ to the same
state. Let $\hat\delta(w)$ denote the state reached on input $w$. Then $x\sim y \iff \hat\delta(x) =
\hat\delta(y)$. **Reflexive:** $\hat\delta(x)=\hat\delta(x)$, so $x\sim x$. Symmetric: if
$\hat\delta(x)=\hat\delta(y)$ then $\hat\delta(y)=\hat\delta(x)$, so $y\sim x$. Transitive: if
$\hat\delta(x)=\hat\delta(y)$ and $\hat\delta(y)=\hat\delta(z)$ then $\hat\delta(x)=\hat\delta(z)$, so
$x\sim z$. Equality is an equivalence relation, so $\sim$ inherits all three properties (Ch. 12). Number
of classes: each class is determined by the single state $\hat\delta(\cdot)$ its members reach, and there
are at most $\lvert Q\rvert$ states, so at most $\lvert Q\rvert$ classes. (This is the seed of the
Myhill–Nerode theorem: a language is regular iff its analogous "indistinguishability" relation has
finitely many classes.) 35.29 (Ch. 8 + 35.) The subset construction sets $Q'=\mathcal{P}(Q)$, and by Chapter 8,
$\lvert\mathcal{P}(Q)\rvert = 2^{\lvert Q\rvert}$. So an $n$-state NFA yields a DFA with at most $2^{n}$
states — one per subset — and never more, because there are exactly $2^{n}$ subsets and each DFA state is
a distinct subset. Many subsets are typically unreachable, so the reachable DFA is often far smaller, but
$2^{n}$ is the hard ceiling. For $n=4$: $2^{4}=16$ DFA states. 35.31 (Deep Dive — closure under union via the product construction.) Claim: if $L_1=L(M_1)$ and
$L_2=L(M_2)$ for DFAs $M_i=(Q_i,\Sigma,\delta_i,s_i,F_i)$, then $L_1\cup L_2$ is regular. Proof. Build
the product DFA $M=(Q_1\times Q_2,\ \Sigma,\ \delta,\ (s_1,s_2),\ F)$ where
$$\delta\big((q,r),a\big) = \big(\delta_1(q,a),\ \delta_2(r,a)\big),$$
i.e. $M$ runs both machines simultaneously on the same input, one component each. By induction on
$\lvert w\rvert$, after reading $w$ the state is $(\hat\delta_1(w),\hat\delta_2(w))$ — each component tracks
its own machine. Choose
$$F = \{(q,r) : q\in F_1 \text{ **or** } r\in F_2\}.$$
Then $M$ accepts $w$ iff $\hat\delta_1(w)\in F_1$ or $\hat\delta_2(w)\in F_2$, i.e. iff $w\in L_1$ or
$w\in L_2$, i.e. iff $w\in L_1\cup L_2$. So $L_1\cup L_2 = L(M)$ is regular. $\blacksquare$ For
intersection, use the same construction but with $F=F_1\times F_2$ (accept iff both components
accept); for the difference $L_1\setminus L_2$, use $F=\{(q,r):q\in F_1\text{ and }r\notin F_2\}$. Conventions: "program" = "Turing machine" (Church–Turing thesis). A decider halts on every input;
a recognizer accepts the "yes" inputs but may loop forever on the "no" ones. To prove a problem $B$
undecidable, reduce $\mathrm{HALT}$ to $B$ ($\mathrm{HALT} \le B$): a decider for $B$ would decide
$\mathrm{HALT}$, contradicting §36.4. A reduction's gadget must be a finite program-builder — it emits
source code, it never runs the program it analyzes. 36.1 † Trace of the even-length machine on $$
q_{\text{even}}\,aaaa \to a\,q_{\text{odd}}\,aaa \to aa\,q_{\text{even}}\,aa \to aaa\,q_{\text{odd}}\,a
\to aaaa\,q_{\text{even}}\,\sqcup \to \text{accept}.
$$ Four $a$'s cause four flips ($q_{\text{even}}\to q_{\text{odd}}\to q_{\text{even}}\to q_{\text{odd}}\to
q_{\text{even}}$); the head then reads the end-blank in $q_{\text{even}}$ and accepts. That is the
right answer: four is an even length. 36.3 † (a) decidable (trial division always halts). (b) decidable (simulate the DFA; it
halts when $w$ ends — Chapter 35). (c) undecidable (the halting problem, §36.4). (d) decidable
(BFS/DFS always terminates — Chapter 28). (e) undecidable (program equivalence; reduces from
$\mathrm{HALT}$, §36.5, and is also a corollary of Rice's theorem). 36.5 † As a language,
$$\mathrm{HALT} = \{\, \langle P, x\rangle : P \text{ is a program that halts on input } x \,\}.$$
The notation $\langle P, x\rangle$ denotes a single string encoding of the pair (program $P$, input
$x$). Feeding a program to another program as data is possible because — as Chapter 10 established —
every program is a finite string over a finite alphabet; so a program's source can be handed to
another program as ordinary input (exactly what compilers, interpreters, and 36.6 † (Scaffolded halting proof — the filled blanks.)
- Assumption. $H(P,x)$ always halts and correctly reports "halts"/"loops."
- Construction. If $H$ says $P$ halts on $P$, then $D$ loops forever; if $H$ says $P$ loops on
$P$, then $D$ halts (returns immediately).
- The diagonal move. Run $D$ on its own description $D$ (consider $D(D)$).
- Case 1. If $D$ halts on $D$, then $H(D,D)$ reports "halts", so by $D$'s rule $D$ loops
forever — a contradiction.
- Case 2. If $D$ loops on $D$, then $H(D,D)$ reports "loops", so by $D$'s rule $D$ halts —
again a contradiction.
- Conclusion. "$D$ halts on $D$ $\iff$ $D$ does not halt on $D$" is self-contradictory; the only
assumption was that a decider $H$ for the halting problem exists, so it is false. $\blacksquare$ 36.7 (Odd.) $\overline{\mathrm{HALT}}$ is not recognizable. Suppose, for contradiction, it were
recognizable. We are given (i) $\mathrm{HALT}$ is recognizable and (ii) if a language and its complement
are both recognizable, the language is decidable. By (i), $\mathrm{HALT}$ is recognizable; by our
assumption, $\overline{\mathrm{HALT}}$ is recognizable; so by (ii) $\mathrm{HALT}$ would be
decidable — contradicting §36.4. Hence $\overline{\mathrm{HALT}}$ is not recognizable.
$\blacksquare$ (This makes $\overline{\mathrm{HALT}}$ strictly harder than $\mathrm{HALT}$: the latter
is recognizable, the former is not even that.) 36.8 † Decidable languages are closed under complement. Let $L$ be decidable, with decider $M$ that
halts on every input. Build $M'$ identical to $M$ but with the accept and reject states swapped. For
any input $x$: since $M$ halts on $x$ (it is a decider), $M'$ halts on $x$ too; and $M'$ accepts $x$
exactly when $M$ rejects $x$, i.e. exactly when $x \notin L$, i.e. exactly when $x \in \overline{L}$. So
$M'$ is a decider for $\overline{L}$, and $\overline{L}$ is decidable. $\blacksquare$
Why the trick fails for recognizers: swapping accept/reject on a recognizer does not give a
recognizer for the complement, because a recognizer may loop forever on some inputs — and looping is
unaffected by swapping the two halting states. On an input where the original recognizer loops, the
"swapped" machine also loops, so it neither accepts nor rejects; it fails to recognize the complement.
(Indeed Exercise 36.7 shows $\overline{\mathrm{HALT}}$ is not recognizable, so no such trick could
exist.) 36.9 (Odd.) $L$ is decidable $\iff$ $L$ is both recognizable and co-recognizable.
($\Rightarrow$) If $L$ is decidable, a decider for $L$ is in particular a recognizer for $L$ (it accepts
exactly $L$ and always halts), so $L$ is recognizable; and by Exercise 36.8 $\overline{L}$ is decidable,
hence recognizable, so $L$ is co-recognizable.
($\Leftarrow$) Suppose $L$ has a recognizer $R$ and $\overline{L}$ has a recognizer $\overline{R}$.
Build a decider $M$ for $L$ that runs $R$ and $\overline{R}$ in parallel, alternating one step of
each (dovetailing). For any input $x$: either $x \in L$, in which case $R$ eventually accepts; or $x \in
\overline{L}$, in which case $\overline{R}$ eventually accepts. Exactly one of the two will accept after
finitely many steps. When $R$ accepts, $M$ accepts; when $\overline{R}$ accepts, $M$ rejects.
Because one of them is guaranteed to accept in finite time, $M$ always halts, and it is correct, so
$M$ decides $L$. $\blacksquare$ 36.10 † A non-recognizable language exists, by counting (Chapter 10). There are only countably
many Turing machines: each machine is a finite object (a finite 7-tuple), hence encodable as a finite
string over a finite alphabet, and the set of finite strings is countable. Therefore there are only
countably many recognizers, so at most countably many recognizable languages. But the set of all
languages over $\{0,1\}$ is the power set of $\{0,1\}^*$; since $\{0,1\}^*$ is countably infinite, its
power set is uncountable (Chapter 10's $\aleph_0 < 2^{\aleph_0}$). An uncountable collection cannot
be exhausted by a countable one, so some language is recognized by no Turing machine — i.e. is not
recognizable (and hence not decidable). $\blacksquare$ 36.11 † General simulator and the "divisible by 3" machine. (Solution code in
Trace 36.13 † Bounded halting check returning an honest Returning 36.15 † (Find the error.) The procedure "simulate $10^9$ steps, else say 'loops'" always
terminates but is not correct. It satisfies halts-on-all-inputs (it always returns after at most
$10^9$ steps) but violates correctness: any program that halts at step $10^9 + 1$ is wrongly labeled
"loops." A decider must be both total and correct; this trades correctness for termination. No
fixed step bound $N$ works, because halting times are unbounded — for every $N$, some program first
halts at step $N+1$ (the §36.4 Find-the-Error trap). 36.17 † (Find the error.) "Run $P$ and watch" is a recognizer, not a decider. The flaw is in
the 'loops' branch: it can fire only after you have observed $P$ not stopping — but "not stopping" is
never something you observe in finite time. If $P$ halts, you see it stop and correctly answer "halts";
if $P$ loops, your watching also goes on forever, so you never reach a moment where the 'loops'
branch executes. The 'loops' branch can therefore never fire correctly: there is no finite point at
which "still running" justifies "will run forever," because some halting programs run for arbitrarily
many steps first. Hence the procedure decides nothing on the looping inputs (§36.3, §36.4 Common
Pitfall). 36.19 † (Model it.) The advertised CI tool claims to flag every test that hangs forever and to
never flag a test that finishes. As a language, it claims to decide
$$\mathrm{HALT}_{\text{test}} = \{\langle T\rangle : \text{test } T \text{ halts}\}$$
(more precisely, to compute its complement: flag exactly the non-halters). "Flag every hanger" =
completeness; "never flag a finisher" = soundness; an always-on CI tool also must always return
(totality). That is a decider for the halting problem, which §36.4 proves cannot exist — so a
sound-and-complete-and-total version is impossible. The honest behavior is a §36.6 coping strategy:
bound the resource — flag any test that exceeds a configured time/step budget (the decidable
$\mathrm{HALT}_N$), accepting that it cannot distinguish "slow but finishes after the budget" from "hangs
forever," and erring on the safe side (flag on overrun). Alternatively, stay sound and conservative,
proving termination only for a decidable subset and hedging on the rest. 36.21 † (Conjecture and test, then prove.)
(a) A test, using (b) Conjecture: $\mathrm{HALT}_N$ (halts within a fixed $N$ steps) is decidable for every fixed
$N$.
(c) Proof. Build a decider: on input $\langle P, x\rangle$, simulate $P$ on $x$ for at most $N$
steps. If $P$ enters a halting state within those $N$ steps, accept; otherwise reject. This
machine always halts — it performs at most $N$ simulation steps, a finite, fixed amount of work — and it
is correct by definition of $\mathrm{HALT}_N$: $P$ halts on $x$ within $N$ steps iff the bounded
simulation witnesses a halt. So $\mathrm{HALT}_N$ is decidable. $\blacksquare$
(d) This does not contradict §36.4 because the bounded question is a different, easier problem than
the unbounded "halts ever?"; the bound $N$ is what makes the simulation guaranteed to finish.
Undecidability is about the general case with no step limit. 36.23 † (Ch. 5 + 36.) Both proofs are proofs by contradiction. Shared structure: each assumes the existence of an object with a stated property, derives a
consequence that the object cannot have, and concludes the object does not exist. In both, every step
after the single assumption is forced and valid, so the assumption is the culprit. (The halting proof
additionally uses self-reference/diagonalization to manufacture its contradiction; the $\sqrt 2$ proof
uses parity.) 36.25 † (Ch. 35 + 36.) For a DFA, "accepts no string?" (emptiness) is decidable because a
DFA is a finite, fully analyzable object: you can compute, by graph reachability, whether any
accepting state is reachable from the start state — a terminating BFS/DFS over finitely many states
(Chapter 28). Crucially, you never have to run the DFA on inputs; you inspect its finite structure
directly, and a DFA always halts anyway. For a Turing machine / general program, emptiness is
undecidable (§36.5) because a TM has an unbounded tape and may loop forever: there is no finite
reachability computation over its (infinitely many) configurations, and you cannot decide emptiness by
inspection — doing so would let you decide halting. What changes: the move from a finite-state device
whose behavior you can fully chart to a device with unbounded memory and the ability not to halt. The
one thing you can always do with a DFA but not with a TM is decide, by finite inspection, everything
about its behavior. 36.27 † (Deep Dive — Rice's theorem.)
(a) "The source code contains the token 36.29 (Odd — Deep Dive, toward Ch. 37.) A Chapter 37 (NP-completeness) reduction must add a
resource bound on the transforming algorithm itself: the reduction has to run in polynomial
time. A Chapter 36 (undecidability) reduction only needs the transformation to be a finite,
mechanical program-builder — it may take as long as it likes, because all that matters is that a
decider for the target would yield a decider for the source. But when "hard" means intractable
(slow) rather than impossible, an unboundedly slow transformation would destroy the very efficiency
you are trying to track: a "reduction" that itself took exponential time could not certify that an
efficient solver for $B$ yields an efficient solver for $A$. So Chapter 37 reductions are
polynomial-time reductions — same logical shape (reduce a known-hard problem to the new one), with a
time budget bolted onto the transformer. Reductions are written $A \le_p B$ = "$A$ is no harder than $B$; $B$ is at least as hard." The rubric
rewards the correct direction (known-hard problem is the source), a polynomial-time construction $f$,
a proven iff, and a membership argument where NP-completeness is claimed. 37.1 † All three are in NP; the verifier scans the certificate in polynomial time.
(a) PARTITION: Certificate: the subset $T$ (a bit string of length $n$). Verifier: sum $T$, sum
$S \setminus T$, accept iff equal — $O(n)$ additions. (b) SUBGRAPH ISOMORPHISM: Certificate: a map
$\mu$ from $H$'s vertices to distinct vertices of $G$. Verifier: check $\mu$ is injective and that every
edge of $H$ maps to an edge of $G$ — $O(\lvert V_H \rvert + \lvert E_H \rvert)$. (c) DOMINATING SET:
Certificate: a set $D$ of $\le k$ vertices. Verifier: check $\lvert D \rvert \le k$ and that every
vertex is in $D$ or adjacent to some member of $D$ — $O(V + E)$. In each case a yes-instance supplies a
valid certificate and no certificate fools the verifier on a no-instance, so the problem is in NP. 37.3 † "Sort a list" outputs a reordered list (not yes/no), so it is not a decision problem. Two
related decision problems: (i) "Is this list already sorted?" — in P, one linear pass comparing
adjacent elements. (ii) "Is $x$ the element in sorted position $k$?" (an order statistic) — in P, since
the $k$-th smallest can be found in $O(n)$ (or just sort in $O(n \log n)$) and compared to $x$. Both are
tractable, illustrating that decision versions of easy problems stay easy. 37.5 † To prove $B$ is NP-complete you must show two things: (1) $B \in \text{NP}$ — exhibit
a polynomial-size certificate and a polynomial-time verifier; and (2) $B$ is NP-hard — give a
polynomial-time reduction from a known NP-complete problem to $B$, e.g. $\text{SAT} \le_p B$ or
$\text{3-SAT} \le_p B$. Direction: the known-hard problem is the source (left side), $B$ is the
target (right side). Hardness flows forward into $B$. 37.7 † $\phi = (x_1 \lor \neg x_2) \land (\neg x_1 \lor x_3) \land (x_2 \lor \neg x_3)$.
(a) $x_1{=}T, x_2{=}T, x_3{=}T$: C1 $x_1 \lor \neg x_2$ — $x_1{=}T$ ✓; C2 $\neg x_1 \lor x_3$ — $x_3{=}T$ ✓;
C3 $x_2 \lor \neg x_3$ — $x_2{=}T$ ✓. Satisfied. (b) $x_1{=}F, x_2{=}F, x_3{=}F$: C1 — $\neg x_2$ true
($x_2{=}F$) ✓; C2 — $\neg x_1$ true ($x_1{=}F$) ✓; C3 — $\neg x_3$ true ($x_3{=}F$) ✓. Satisfied.
(c) $x_1{=}T, x_2{=}F, x_3{=}T$: C1 — $x_1{=}T$ ✓; C2 — $x_3{=}T$ ✓; C3 — $x_2 \lor \neg x_3$: $x_2{=}F$
and $\neg x_3$ false ($x_3{=}T$) → C3 unsatisfied. Not satisfied (C3 fails). 37.9 † $\phi = (a \lor b \lor \neg c) \land (\neg a \lor \neg b \lor c)$. Six vertices, one per
literal occurrence: column 0 = $\{a, b, \neg c\}$, column 1 = $\{\neg a, \neg b, c\}$. Edges connect the
$3 \times 3 = 9$ cross-clause pairs unless contradictory (same variable, opposite sign). Check each
column-0 literal against each column-1 literal:
- $a$ vs $\neg a$ → contradictory (forbidden); $a$ vs $\neg b$ → ok; $a$ vs $c$ → ok.
- $b$ vs $\neg a$ → ok; $b$ vs $\neg b$ → contradictory (forbidden); $b$ vs $c$ → ok.
- $\neg c$ vs $\neg a$ → ok; $\neg c$ vs $\neg b$ → ok; $\neg c$ vs $c$ → contradictory (forbidden). Three forbidden pairs, so $9 - 3 = \mathbf{6}$ edges. A clique of size $k = 2$ needs one vertex per column,
adjacent. Many choices work, e.g. $a$ (col 0) and $c$ (col 1): different clauses, non-contradictory →
adjacent. This corresponds to setting $a = \text{True}, c = \text{True}$ (and $b$ arbitrary), which
satisfies C1 (via $a$) and C2 (via $c$). So a 2-clique exists and $\phi$ is satisfiable. 37.11 † GRAPH 3-COLORING verifier. Certificate: a coloring $c \colon V \to \{1,2,3\}$.
Verifier: (i) confirm each $c(v) \in \{1,2,3\}$ — $O(V)$; (ii) scan every edge $(u,v)$ and confirm
$c(u) \ne c(v)$ — $O(E)$. Total $O(V + E)$, polynomial. A yes-instance has a proper 3-coloring (certificate
exists); a no-instance has none, so every candidate leaves some monochromatic edge and is rejected. See
37.12 † PARTITION $\in$ NP, blanks filled.
- Certificate: a subset $T \subseteq S$, encoded as a bit string of length $n$ (bit $i$ = "is $s_i$
in $T$?"). Its size is $n$ bits — polynomial.
- Verifier $V(S,T)$: compute $\sigma = \sum_{s \in S} s$, then compute $\sum_{s \in T} s$ and accept
iff it equals $\sigma / 2$. This takes $O(n)$ time (one pass).
- Correctness: if $S$ is a yes-instance there is a valid $T$, so a certificate exists; if $S$ is a
no-instance then no subset sums to half the total, so the verifier rejects every candidate. $\blacksquare$
(Equivalently, accept iff $\sum_{s \in T} s = \sum_{s \in S \setminus T} s$.) 37.13 $\le_p$ is reflexive. Take $f = \text{identity}$: $f(x) = x$. It is computable in linear time
(copy the input), hence polynomial. And $x$ is a yes-instance of $A$ $\iff$ $f(x) = x$ is a yes-instance of
$A$ — trivially, the same instance. So $f$ is a polynomial-time reduction and $A \le_p A$. $\blacksquare$ 37.14 † If $B \in \text{P}$ and $A \le_p B$ then $A \in \text{P}$, blanks filled.
- Let $M_B$ solve $B$ in time $O(n^c)$ (a polynomial), and $f$ reduce $A$ to $B$ in time $O(n^d)$,
with $x \in A \iff \mathbf{f(x)} \in B$.
- Algorithm for $A$ on input $x$: compute $f(x)$, then return $M_B(f(x))$.
- Running time: computing $f(x)$ is polynomial; $\lvert f(x) \rvert$ is polynomial in $\lvert x
\rvert$ (output length $\le$ running time); so $M_B$ on it costs $O(\lvert f(x)\rvert^c)$, polynomial. The
composition of polynomials is polynomial. Hence $A \in \text{P}$. $\blacksquare$
- Contrapositive: if $A \notin \text{P}$ and $A \le_p B$, then $B \notin \text{P}$ — "if $A$ is hard
(not in P) and reduces to $B$, then $B$ is hard too." This is the engine of NP-hardness proofs: reduce a
problem believed/known hard into your target, and the target inherits the hardness. 37.15 INDEPENDENT SET $\le_p$ CLIQUE. Reduction $f$: given $(G, k)$ for INDEPENDENT SET, output
$(\overline{G}, k)$ where $\overline{G}$ is the complement graph (same vertices; $uv$ is an edge of
$\overline{G}$ iff it is not an edge of $G$). Polynomial time: build $\overline{G}$ by examining all
$\binom{n}{2}$ pairs — $O(n^2)$. Iff: a set $S$ is independent in $G$ $\iff$ no two vertices of $S$ are
adjacent in $G$ $\iff$ every two vertices of $S$ are adjacent in $\overline{G}$ $\iff$ $S$ is a clique in
$\overline{G}$; and $\lvert S \rvert = k$ is preserved. So $(G,k) \in \text{IND-SET} \iff (\overline{G}, k)
\in \text{CLIQUE}$. $\blacksquare$
Direction note: this proves INDEPENDENT SET $\le_p$ CLIQUE, i.e. INDEPENDENT SET is no harder than
CLIQUE. Since CLIQUE is known NP-complete, this does not by itself prove CLIQUE hard (we already knew
it). To prove INDEPENDENT SET is NP-hard we'd use the same complement map the other way
($\text{CLIQUE} \le_p \text{INDEPENDENT SET}$, which the construction also gives since complementation is
its own inverse). The lesson: state which problem your reduction certifies as hard, and check the source is
the known-hard one. 37.16 † Flaw: TSP-optimization is not in NP (NP is defined for decision problems, §37.2). The
proposed "verifier" only checks that a given tour has cost $\le$ some value — that verifies the decision
version ("is there a tour of cost $\le B$?"), not the optimization version ("is this tour the cheapest?").
To verify a claimed optimum, a checker would have to confirm no cheaper tour exists, which is not a
single short certificate to check (it's a universally-quantified claim over all tours). So the argument
proves TSP-decision $\in$ NP, not TSP-optimization. (The optimization version is NP-hard but not in NP.) 37.17 Flaw: the reduction is backwards. $B \le_p \text{3-SAT}$ shows $B$ is no harder than 3-SAT
— it proves nothing about $B$'s hardness (indeed every NP problem reduces to 3-SAT, so this is nearly free).
To prove $B$ is NP-hard, reduce a known NP-complete problem to $B$: $\text{3-SAT} \le_p B$ (the
known-hard problem must be the source). The student also still needs $B \in \text{NP}$ for
NP-completeness. 37.18 † Category error: NP-complete $\ne$ undecidable. Undecidable (Chapter 36, e.g. the halting
problem) means no algorithm solves the problem correctly on all inputs, at any speed — a limit on what
is computable at all. Intractable (an NP-complete problem like SAT, assuming $\text{P} \ne \text{NP}$)
means an algorithm exists (e.g. brute-force search) but the best known one takes super-polynomial
time — a limit on what is computable efficiently. NP-complete problems are perfectly solvable in
principle; they are merely (apparently) slow. 37.19 Flaw: $\text{P} \subseteq \text{NP}$ lets you conclude "in P $\Rightarrow$ in NP," not the
reverse "in NP $\Rightarrow$ in P." Membership in NP says only that SAT is verifiable fast; it says
nothing about solving it fast. To conclude SAT $\in$ P you would need an actual polynomial-time algorithm
for SAT (or a proof one exists). Because SAT is NP-complete, that would force $\text{P} = \text{NP}$ — the
open problem — so no such conclusion is available today. 37.20 † Subtle error: the difficulty is not monotone in $k$. Show $\text{3-SAT} \le_p
\text{4-SAT}$: given a 3-CNF formula, transform each clause $(\ell_1 \lor \ell_2 \lor \ell_3)$ into a
4-literal clause by introducing one fresh variable $z$ per clause and replacing it with the pair
$(\ell_1 \lor \ell_2 \lor \ell_3 \lor z) \land (\ell_1 \lor \ell_2 \lor \ell_3 \lor \neg z)$ — for any value
of $z$ one of the two clauses reduces to the original $(\ell_1 \lor \ell_2 \lor \ell_3)$, so the new formula
is satisfiable iff the original is. (Even simpler when "exactly 4" is not required: pad with a repeated
literal $(\ell_1 \lor \ell_2 \lor \ell_3 \lor \ell_3)$.) This is polynomial and preserves satisfiability,
so 4-SAT is at least as hard as 3-SAT. Conversely 4-SAT $\in$ NP, and one can reduce 4-SAT to 3-SAT by
splitting long clauses, so they are interreducible — all of 3-SAT, 4-SAT, $k$-SAT for $k \ge 3$ are
NP-complete and hence equally hard. The "steadily harder" intuition is wrong: hardness plateaus at $k=3$;
the only real cliff is $2 \to 3$. 37.21 † Conference scheduling = GRAPH $k$-COLORING (= GRAPH COLORING decision). Model: vertices =
talks; put an edge between two talks iff they share an attendee (can't be concurrent); a "time slot" is a
color. "Schedule into at most $k$ slots with no conflict" = "properly color the graph with at most $k$
colors." Decision problem: "Is the conflict graph $k$-colorable?" Certificate: an assignment of a slot
(color) $1..k$ to each talk. Verifier: check each edge's endpoints get different slots and every slot is
in $1..k$ — $O(V+E)$. (This is exactly Case Study 1's MIN-WORKERS model.) 37.22 Delivery routing = HAMILTON CIRCUIT / TSP (decision). Precisely: with the depot and addresses
as vertices, edge weights as driving times, and a budget $B$, the question "is there a closed route
visiting every vertex once with total time $\le B$?" is TSP (decision) (§37.5). Certificate: an
ordering of the addresses (a tour). Verifier: check it visits each address exactly once, returns to the
depot, and sums to $\le B$ — $O(n)$. NP-hardness chain (from §37.5): Cook–Levin gives SAT NP-complete,
then $\text{SAT} \le_p \text{3-SAT} \le_p \text{Hamilton circuit} \le_p \text{TSP (decision)}$. So routing
is NP-hard (and, being verifiable, NP-complete). 37.23 † Circuit equivalence via UNSAT. (a) Two circuits $C_1, C_2$ compute the same function iff
they agree on every input, i.e. there is no input on which they differ. Form the "miter" circuit
$C_1 \oplus C_2$ (XOR of their outputs); it outputs true exactly on inputs where they disagree. The
circuits are equivalent $\iff$ the miter's output formula is unsatisfiable (no input makes the
disagreement true). So equivalence-checking is naturally a question about unsatisfiability — the
universally-quantified "for all inputs they agree" becomes "no input satisfies the difference." (b) Even
though SAT is NP-complete (§37.6), this is good news in practice: real circuits produce structured
formulas, and industrial CDCL SAT solvers routinely decide such miters with millions of variables. This
is the §37.6 "give up fast" row — exact and general, with a worst case that rarely bites on real,
structured inputs. 37.25 † Brute-force SAT search size = $2^v$. (a) 37.26 Edge complement. (a) 37.27 † Nearest-neighbor is not optimal. (a) On §37.6's matrix $D$ (rows/cols 0–3, with
$d_{0,1}{=}10, d_{0,2}{=}15, d_{0,3}{=}20, d_{1,2}{=}35, d_{1,3}{=}25, d_{2,3}{=}30$) from city 0:
go to nearest unvisited each step — $0 \to 1$ (10) $\to 3$ (25, since $d_{1,2}{=}35 > d_{1,3}{=}25$) $\to 2$
(30) $\to 0$ (15), total $10+25+30+15 = 80$. Another tour $0 \to 2 \to 3 \to 1 \to 0$ costs $15+30+25+10 =
80$ here. (b) A clean instance where NN provably fails: 4 cities on a line at positions $0, 1, 2, 4$ with
$d$ = distance; from start at position $1$, NN goes $1 \to 0$ (1) $\to 2$ (2) $\to 4$ (2) $\to 1$ (3) =
$8$, but the route $1 \to 2 \to 4 \to 0 \to 1$ along/around the line costs $1 + 2 + 4 + 1 = 8$ as well —
construct asymmetric spacing (e.g. a "trap" where the greedy first hop strands a far city) to force NN
strictly above optimum; the standard textbook trap graph yields NN/optimum ratios growing like
$\Theta(\log n)$. (c) Nearest-neighbor gives up exact (the §37.6 "approximation/heuristic" row): it
keeps fast and general but sacrifices the optimality guarantee. 37.28 † Undecidability reduction vs. NP-hardness reduction. An undecidability reduction (Chapter
36) need only be computable — it shows "if I could decide $B$, I could decide the halting problem,"
transferring uncomputability. An NP-hardness reduction must additionally be polynomial-time
computable, because it must preserve efficiency: it shows "a fast algorithm for $B$ gives a fast
algorithm for the source," and a slow translation would not certify that $B$ is hard to solve quickly.
The complexity setting needs the extra adjective because it cares not just whether you can transform but
whether you can do it cheaply enough to keep the answer fast. 37.29 CNF for "at least one of $p,q,r$ true and not all three true." "At least one true" is the
clause $(p \lor q \lor r)$. "Not all three true" is $\neg(p \land q \land r) \equiv (\neg p \lor \neg q
\lor \neg r)$ (De Morgan, Chapter 1). Conjoining:
$$(p \lor q \lor r) \land (\neg p \lor \neg q \lor \neg r).$$
This is a 3-CNF SAT instance. Certificate: a satisfying assignment, e.g. $p{=}T, q{=}F, r{=}F$ (clause 1
true via $p$; clause 2 true via $\neg q$). Confirm: it is an AND of OR-of-literal clauses, so it is a valid
SAT (indeed 3-SAT) input. 37.30 † FACTORING. (a) Decision form: "does $N$ have a factor $d$ with $1 < d \le k$?" Certificate:
such a divisor $d$. Verifier: check $1 < d \le k$ and $N \bmod d = 0$ — a single division, polynomial in
the number of bits of $N$ (Chapter 14). So FACTORING $\in$ NP. See 37.31 Euler vs. Hamilton. (a) EULER CIRCUIT $\in$ P; HAMILTON CIRCUIT is NP-complete (in NP
and NP-hard, §37.5). (b) Euler's polynomial algorithm: a connected (multi)graph has an Euler circuit iff
every vertex has even degree (Chapter 30) — checkable in $O(V + E)$ by counting degrees. (c) The two
problems look almost identical ("traverse every edge once" vs. "visit every vertex once") yet land on
opposite sides of the tractability boundary, the perfect illustration that a small change in a problem's
statement can flip it from polynomial to NP-complete (compare 2-SAT vs. 3-SAT, §37.5). 37.32 † A reduction is a function with two extra properties. A polynomial-time reduction $f$ is a
function (Chapter 9) that is additionally (i) polynomial-time computable (the resource condition) and
(ii) answer-preserving: $x \in A \iff f(x) \in B$ (the correctness condition). For $f$ to be a
bijection it would need to be both injective and surjective between instance sets — but reductions are
not required to be bijective. Injectivity/surjectivity are irrelevant: $f$ may send many $A$-instances
to the same $B$-instance, and need not hit every $B$-instance; all that matters is that each input's
yes/no answer is preserved and the map is cheap. (Indeed the trivial reduction in 37.13 is a bijection, but
the constant-collapsing reductions in many hardness proofs are far from injective.) 37.33 Transitivity of $\le_p$ (full). Let $f$ reduce $A$ to $B$ in time $\le p(n)$ and $g$ reduce
$B$ to $C$ in time $\le q(n)$, $p, q$ polynomials. Define $h(x) = g(f(x))$. Correctness: $x \in A \iff
f(x) \in B$ (by $f$) $\iff g(f(x)) \in C$ (by $g$), so $h$ preserves yes/no answers. Running time:
computing $f(x)$ costs $\le p(\lvert x\rvert)$, and crucially $\lvert f(x)\rvert \le p(\lvert x\rvert)$
— an algorithm cannot output more symbols than its running time (this is the essential step). Then $g$ on
$f(x)$ costs $\le q(\lvert f(x)\rvert) \le q(p(\lvert x\rvert))$. Total $\le p(\lvert x\rvert) +
q(p(\lvert x\rvert))$, a polynomial in $\lvert x\rvert$ (a polynomial of a polynomial is a polynomial).
$\blacksquare$ Without the output-length bound, $f(x)$ could be (say) exponentially long while $f$ still
"ran in polynomial time" in some accounting, and then $g$'s polynomial running time in its own input
would be exponential in $\lvert x\rvert$ — breaking the composition. The bound $\lvert f(x)\rvert \le
p(\lvert x\rvert)$ is what keeps the intermediate instance polynomially sized. 37.34 One NP-complete problem in P collapses NP; and the contrapositive. Forward: suppose $C$ is
NP-complete and $C \in \text{P}$ via polynomial $M_C$. Take any $A \in \text{NP}$. Since $C$ is NP-hard,
$A \le_p C$ via some polynomial $f$. On input $x$: compute $f(x)$ (polynomial) and return $M_C(f(x))$
(polynomial, as $\lvert f(x)\rvert$ is polynomial in $\lvert x\rvert$). This decides $A$ in polynomial
time, so $A \in \text{P}$; as $A$ was arbitrary, $\text{NP} \subseteq \text{P}$, and with $\text{P}
\subseteq \text{NP}$, $\text{P} = \text{NP}$. *Contrapositive:* if $\text{P} \ne \text{NP}$, then no
NP-complete problem is in P (any one in P would force equality). Why CLIQUE and 3-SAT stand or fall
together: both are NP-complete, so each reduces to the other (via NP-hardness + membership); a polynomial
algorithm for either yields one for the other (and for all of NP), and a super-polynomial lower bound for
either dooms the other. They are one hardness in two costumes. 37.35 Why experts believe $\text{P} \ne \text{NP}$ (without proof). A model answer marshals three
chapter ideas. (1) The verify/find asymmetry (§37.3): all of human experience says recognizing a good
solution is easier than producing one — grading a proof vs. writing it, checking factors vs. finding them.
$\text{P} = \text{NP}$ would abolish that asymmetry, making (in a precise sense) creativity mechanizable;
that is wildly counter to experience. (2) The reduction web (§37.5): thousands of NP-complete problems
across unrelated fields (logistics, biology, logic, scheduling) are secretly the same problem — one fast
algorithm for any of them would crack all of them. (3) Decades of collective failure: enormous effort
by many brilliant people across all those problems has produced no polynomial algorithm for even one. That
convergent failure on problems that look unrelated but are interreducible is strong empirical evidence the
wall is real. Honesty (theme four): none of this is a proof — strong evidence and ruling out specific
approaches are not the same as demonstrating no polynomial algorithm can exist; that demonstration is the
open Millennium Prize Problem, and natural proof strategies are themselves provably insufficient (§37.6). 38.1 † $d(\texttt{10110}, \texttt{11100})$: compare position by position (10110 vs 11100) — agree at
positions 1, 3, 5; differ at positions 2 and 4. So $d = 2$. $d(\texttt{0000}, \texttt{1011}) =
\operatorname{wt}(\texttt{1011}) = 3$.
$\operatorname{wt}(\texttt{101101}) = 4$. $\operatorname{wt}(\texttt{11110000}) = 4$. 38.3 † By Theorem 38.2, a code with $d_{\min} = 7$ detects up to $d_{\min} - 1 = 6$ errors. By Theorem
38.3, it corrects up to $\lfloor (d_{\min}-1)/2 \rfloor = \lfloor 6/2 \rfloor = 3$ errors. (Equivalently
$d_{\min} \ge 2t+1 \Rightarrow 7 \ge 2(3)+1 = 7$, so $t = 3$.) 38.5 † Syndrome $s_4 s_2 s_1 = 011_2 = 3$. Since it is nonzero, the receiver flips position 3
(the data bit $d_1$), then reads the corrected data at positions 3, 5, 6, 7. The syndrome equals the error
position because the columns of $H$ are the position numbers in binary (§38.5). 38.7 † Blanks, in order: $d(x,y) \le \mathbf{t}$; if $y$ were a distinct codeword then $d_{\min} \le
\mathbf{t}$ (i.e. $\le d(x,y) \le t$); contradiction with $d_{\min} \ge t+1$; therefore $y$ is not a
codeword, so the error is detected (the receiver sees an illegal string). $\blacksquare$ 38.9 † The all-zeros word $\mathbf{0} = [0,0,0,0,0,0,0]$ is a codeword (encode $0000$: all parities 0).
The chapter's codeword $[1,1,1,0,0,0,0]$ (encode $1000$) has weight 3. Their distance is
$\operatorname{wt}([1,1,1,0,0,0,0] \oplus \mathbf 0) = 3$. Two distinct codewords at distance 3 exist, so
$d_{\min} \le 3$. (Combined with "no codeword has weight 1 or 2," §38.4, this gives $d_{\min} = 3$.) 38.11 † The single-parity-check code $C = \{x : \operatorname{wt}(x) \text{ even}\}$ is linear (contains
$\mathbf 0$; XOR of two even-weight words is even-weight — parities add mod 2). By Theorem 38.4,
$d_{\min}$ = least nonzero weight in $C$. No nonzero codeword has weight 1 (weight 1 is odd, excluded). A
weight-2 word, e.g. $\texttt{1100\dots0}$, is even-weight, hence in $C$. So the least nonzero weight is 2,
and $d_{\min} = 2$. (By Theorem 38.2 it detects all single-bit errors and corrects none — the Chapter 26
parity bit.) 38.13 † 38.15 † Encode 38.17 † The flaw: detection and correction use different thresholds, and the claim used the wrong
one. To correct $t = 2$ errors, Theorem 38.3 requires $d_{\min} \ge 2t+1 = 5$, not merely
$d_{\min} \ge 2$. A $d_{\min} = 4$ code corrects only $\lfloor (4-1)/2 \rfloor = 1$ error (and can
detect up to 3, or run as SECDED: correct 1, detect 2). It cannot guarantee correction of a 2-bit error. 38.19 † The flaw: "correct $t_c$ AND detect $t_d$" requires $d_{\min} \ge t_c + t_d + 1$, not
$d_{\min} \ge t_c + t_d$. For $t_c = 1, t_d = 2$ that is $d_{\min} \ge 4$, but $\mathrm{Hamming}(7,4)$ has
$d_{\min} = 3 < 4$. What it actually does on a double error: the syndrome is nonzero and names some third
position, so the decoder silently miscorrects (flips a bit that was never wrong) — it does not detect
the double error. To get "correct 1 and detect 2" you need the SECDED extension with $d_{\min} = 4$. 38.21 † (a) A burst of up to 16 consecutive bit-flips spans at most $\lceil 16/8 \rceil + 1 = 3$
symbols if it can straddle two byte boundaries — more carefully, a 16-bit run touches at most 3
consecutive bytes (it covers at most two full bytes plus parts of the bytes on each end:
$\lceil (16 + 7)/8 \rceil = 3$). (b) The $[255, 223]$ RS code has $d_{\min} = 255 - 223 + 1 = 33$ and
corrects $\lfloor (255-223)/2 \rfloor = 16$ symbol errors per block — far more than the 3 symbols a single
16-bit burst can damage, so it is comfortably within budget. (c) Counting in symbols turns "16 adjacent
bad bits" (catastrophic for a bit-level code) into "≤ 3 bad symbols" (trivial for an RS code that corrects
16) — the modeling decision is the solution. 38.23 † Conjecture: the $n$-fold repetition code $\{0^n, 1^n\}$ has $d_{\min} = n$ and corrects
$\lfloor (n-1)/2 \rfloor$ errors. Test (with 38.25 † $x^2 \equiv 1 \pmod n$: $n=5 \to \{1, 4\}$ (2 roots); $n=7 \to \{1, 6\}$ (2 roots);
$n=8 \to \{1, 3, 5, 7\}$ (4 roots); $n=12 \to \{1, 5, 7, 11\}$ (4 roots); $n=15 \to \{1, 4, 11, 14\}$ (4
roots). Rule: a quadratic has at most 2 roots exactly when $\mathbb{Z}_n$ is a field, i.e. when $n$ is
prime (5, 7 here); composite $n$ has zero divisors and can give more roots. Coding-theory link: the
Reed–Solomon distance argument ("degree-$d$ nonzero polynomial has $\le d$ roots") is valid only over a
field, so it holds for $n = 5, 7$ (prime) and fails for $n = 8, 12, 15$ — which is why RS lives over
$\mathrm{GF}(2^8)$, not $\mathbb{Z}_{256}$. 38.26 † (a) The parity-checked code on $k$ data bits appends one parity bit, giving block length
$n = k+1$; any two codewords differ in at least 2 positions (changing one data bit forces the parity bit to
change too), and weight-2 codewords exist, so $d_{\min} = 2$. (b) By Theorem 38.2 it detects up to
$d_{\min} - 1 = 1$ error — all single-bit errors. (c) It cannot correct even one error because correction
needs $d_{\min} \ge 2t+1 = 3$, but $d_{\min} = 2 < 3$; a single flip lands distance 1 from two codewords,
so nearest-codeword decoding is ambiguous — detection only, exactly Chapter 26's verdict. 38.27 Let $E = \{x \in \mathrm{GF}(2)^n : \operatorname{wt}(x) \text{ even}\}$. (i) $\mathbf 0$ has
weight 0, which is even, so $\mathbf 0 \in E$. (ii) For $x, y \in E$, use $\operatorname{wt}(x \oplus y) =
\operatorname{wt}(x) + \operatorname{wt}(y) - 2\,(\text{overlap})$. Since $\operatorname{wt}(x)$ and
$\operatorname{wt}(y)$ are both even and $2\,(\text{overlap})$ is even, $\operatorname{wt}(x \oplus y)$ is
even, so $x \oplus y \in E$. Closed under XOR and containing $\mathbf 0$, $E$ is a subspace of
$\mathrm{GF}(2)^n$ — hence a linear code. $\blacksquare$ 38.28 † A degree-$ 38.29 Claim: a Hamming code with $r$ parity bits has $n = 2^r - 1$ and $k = 2^r - 1 - r$. Proof by
induction on $r$. Base ($r = 2$): two parity bits, $n = 2^2 - 1 = 3$, $k = 3 - 2 = 1$ — the
$\mathrm{Hamming}(3,1)$ code, whose parity-check matrix has the 3 distinct nonzero binary columns
$01, 10, 11$ of length 2; this is the repetition code $\{000, 111\}$, $d_{\min} = 3$, correct. Step:
assume an $r$-parity-bit Hamming code has block length $2^r - 1$ (its $H$ uses all $2^r - 1$ distinct
nonzero length-$r$ columns). Adding one more parity bit makes columns length $r+1$; the number of distinct
nonzero length-$(r+1)$ binary columns is $2^{r+1} - 1$, so the new block length is $n = 2^{r+1} - 1$ and
$k = n - (r+1) = 2^{r+1} - 1 - (r+1)$, the claim at $r+1$. $\blacksquare$ (The columns of $H$ being all
distinct nonzero vectors is what keeps $d_{\min} = 3$ at every $r$, by Exercise 38.32.) 38.31 † Each codeword of a single-error-correcting code "owns" a Hamming ball of radius 1: itself plus
the $n$ strings one flip away, total $1 + n$ strings. By Theorem 38.3 ($d_{\min} \ge 3$) these balls are
disjoint. The $M$ disjoint balls together hold $M(n+1)$ distinct strings, all inside $\mathrm{GF}(2)^n$,
which has only $2^n$ strings. Hence $M(n+1) \le 2^n$, i.e. $M \le \dfrac{2^n}{n+1}$ (the Hamming /
sphere-packing bound). For $\mathrm{Hamming}(7,4)$: $n = 7$, $M = 2^4 = 16$, and
$\dfrac{2^7}{7+1} = \dfrac{128}{8} = 16$. The bound is met with equality, so the radius-1 balls exactly
tile $\mathrm{GF}(2)^7$ — the Hamming code is perfect. $\blacksquare$ (In this chapter every daggered exercise is also odd-numbered, so the worked set is 39.1, 39.3, …, 39.35.) 39.1 † Load-bearing theorem and operational module per track:
- A (RSA): $m^{ed} \equiv m \pmod n$ (Euler's theorem, Ch. 25); core module 39.3 † With $n = 3233$, a single byte fits iff its value is $< 3233$, and every byte is $0$–$255 < 3233$, so any one byte fits (the largest byte value, 255, is well below 3233). 39.5 † A code with minimum distance $d$ detects up to $d - 1$ errors and corrects up to $\lfloor (d-1)/2 \rfloor$ errors. For Hamming(7,4), $d = 3$: it detects up to 2 errors and corrects up to 1 ($\lfloor 2/2 \rfloor = 1$). 39.7 † Blanks and the hypothesis used at each step:
- $ed = 1 + k\phi(n)$ for some integer $k$ (uses: $ed \equiv 1 \pmod{\phi(n)}$).
- $m^{\phi(n)} \equiv \mathbf{1} \pmod n$ (uses: Euler's theorem, valid because $\gcd(m,n) = 1$).
- $m^{ed} = m^{1+k\phi(n)} = m\cdot(m^{\phi(n)})^k \equiv m\cdot \mathbf{1}^{\,k} = \mathbf{m} \pmod n$ (uses: substitution of the previous two facts).
The single hypothesis carrying the middle step is the coprimality $\gcd(m,n)=1$, which licenses Euler's theorem. 39.9 † Soundness of the backtracking solver. Claim: if 39.11 † Blocking layer (see For 39.13 † Uniqueness counter (see A well-formed Sudoku returns 1. Hand-trace logic: 39.15 † 39.17 † This conflates demonstration with guarantee, violating theme two of the book ("it passed the tests" ≠ "it is correct"). 50,000 passing cases are evidence over a finite sample; infinitely many messages remain untested. The sentence that converts it into a guarantee: "and decryption is correct for every $m < n$ by the RSA correctness theorem (Euler's theorem, Chapter 25), which proves $m^{ed} \equiv m \pmod n$." That cites the proof that settles all cases at once. 39.19 † Exam scheduling as a CSP / graph coloring.
- Variables: the 12 exams.
- Domains: each variable ranges over the 4 time slots $\{1,2,3,4\}$.
- Constraint: for every pair of exams sharing a student, the two exams must receive different slots.
Build a conflict graph $G$ with one vertex per exam and an edge between two exams iff some student takes both. The constraint "adjacent exams get different slots" is exactly a proper graph coloring (Chapter 33): the time slots are the colors, and a valid schedule exists iff $G$ is 4-colorable ($\chi(G) \le 4$). Track C's engine solves it by swapping variables = vertices, domains = colors, and 39.21 † Noisy storage as a coding problem. To survive one bit-flip per 4-bit nibble and recover the data, the code must correct 1 error, so it needs minimum distance $d \ge 3$ (since correcting $t$ errors requires $d \ge 2t+1 = 3$). Hamming(7,4) has exactly $d = 3$, so it meets the requirement; the storage overhead is the code rate $4/7 \approx 0.57$ — 7 stored bits per 4 data bits, i.e. 75% extra. If the chip can flip two bits per nibble, you need to correct $t = 2$, requiring $d \ge 5$; Hamming(7,4)'s $d = 3$ only corrects 1, so a 2-bit flip can land nearer a different codeword and be "corrected" wrongly — you would need a stronger code (larger $d$). 39.23 † Diameter vs. vertex count for paths. Pattern: the diameter of $P_V$ is $V - 1$. Conjecture: $\operatorname{diam}(P_V) = V - 1$. Proof: the two endpoints of the path are joined by a unique simple path of $V-1$ edges, and no pair is farther (any two vertices $i, j$ are at distance $|i - j| \le V-1$, achieved by the endpoints). So the maximum pairwise distance is $V - 1$. $\blacksquare$ It is an upper bound for all connected graphs on $V$ vertices (a path is the "longest-stretched" connected graph; adding edges can only shorten distances), and it is exact only for paths — e.g. $K_V$ has diameter 1 for $V \ge 2$. 39.25 † Does every single flip change the decoded data? Predicted by hand: each single flip is distance 1 from the codeword, so by single-error correction the decoder snaps back to it and recovers 39.27 † BFS distances from 39.29 † Euclidean algorithm for $\gcd(252, 198)$:
$$252 = 1\cdot 198 + 54,\quad 198 = 3\cdot 54 + 36,\quad 54 = 1\cdot 36 + 18,\quad 36 = 2\cdot 18 + 0.$$
The last nonzero remainder is 18, so $\gcd(252,198) = 18$, matching the integration test's assertion. (Check: $252 = 14\cdot 18$, $198 = 11\cdot 18$, and $\gcd(14,11)=1$.) 39.31 † "Check every case and find none works" is proof by exhaustion (cases) — Chapter 5. Establishing the universal negative "no satisfying assignment exists": the search tree of partial assignments is finite (at most 81 cells, each branching at most 9 ways), and 39.33 † 39.35 † Five-section write-up (sample, Track A core). A model answer hits these marks (graded items in bold):
1. Problem & model. "I built an RSA system that encrypts short text. I modeled a message as an integer $m$ in $\mathbb{Z}_n$: bytes read big-endian as one number, with the constraint $m < n$ so arithmetic in $\mathbb{Z}_n$ is recoverable."
2. Design & build. "Architecture: 40.1 $H({1/2, 1/4, 1/4}) = -\big(\tfrac12\log_2\tfrac12 + \tfrac14\log_2\tfrac14 +
\tfrac14\log_2\tfrac14\big)$. The three terms are $-\tfrac12(-1) = \tfrac12$, $-\tfrac14(-2) =
\tfrac12$, $-\tfrac14(-2) = \tfrac12$. Sum $= 1.5$ bits. 40.2 † A fair 8-sided die is uniform over 8 outcomes, so $H = \log_2 8 = 3$ bits. A fixed-length
code needs $\lceil\log_2 8\rceil = 3$ bits per outcome. It is not wasteful: the source is uniform,
so entropy equals the fixed length exactly — there is nothing to compress. 40.3 At $(5,0)$: $3(5) + 5(0) = 15$. At $(0,7)$: $3(0) + 5(7) = 35$. The four corners give
$0, 15, 35, 36$ (the last at $(2,6)$), so the optimum is \$36 at $(2,6)$, the corner where both
constraints bind. 40.4 † $R(3,3) = 6$. It guarantees that in any group of six people, there are always three who
are mutual friends or three who are mutual strangers — and six is the smallest group size for which
this is guaranteed (five people can be arranged to avoid it). 40.5 Identity law: mapping the do-nothing function over a list returns the list unchanged.
Composition law: mapping 40.7 † Blanks, in order:
- Adding the two scaled inequalities: $x(u+2v) + y(2u+v) \ge \mathbf{3x + 5y}$.
- Regrouping the left side by dual variables: $x(u+2v) + y(2u+v) = \mathbf{u(x+2y) + v(2x+y)}$.
- Bounding above via the primal constraints with $u,v \ge 0$: $u(x+2y) + v(2x+y) \le \mathbf{14u + 10v}$.
- Chaining: $3x + 5y \le x(u+2v)+y(2u+v) = u(x+2y)+v(2x+y) \le 14u + 10v$. $\blacksquare$ 40.9 † Each term $-p_i\log_2 p_i \ge 0$ on $0 \le p_i \le 1$: if $p_i = 0$ the term is $0$ (by the
$0\log_2 0 = 0$ convention); if $0 < p_i \le 1$ then $\log_2 p_i \le 0$, so $-p_i\log_2 p_i \ge 0$.
A sum of nonnegative terms is $\ge 0$, hence $H(X) \ge 0$. Equality requires every term to be $0$:
$-p_i\log_2 p_i = 0$ forces $p_i \in \{0, 1\}$, and since the probabilities sum to 1 exactly one $p_i$
equals $1$ and the rest $0$ — a certain outcome. $\blacksquare$ 40.11 † $f(x) = 2x$, $g(y) = y - 3$, list $[5,6,7]$.
- $F(g\circ f)$ (map once): $(g\circ f)(x) = g(2x) = 2x - 3$, so $[5,6,7] \mapsto [7, 9, 11]$.
- $F(g)\circ F(f)$ (map twice): map $f$ gives $[10, 12, 14]$; map $g$ over that gives $[7, 9, 11]$.
The two agree, $[7,9,11] = [7,9,11]$, illustrating the functor composition law $F(g\circ f) =
F(g)\circ F(f)$ for the list functor — the same fact that makes map fusion a sound optimization. 40.12 † See 40.13 See 40.14 † See 40.15 See 40.16 † The flaw is "read the good coloring off the proof." The probabilistic-method argument is
non-constructive: it computes $E[X] < 1$ and concludes from "$X$ is a nonnegative integer with mean
below 1" that some outcome has $X = 0$ — but it never identifies which random outcome that is. There
is no coloring written down anywhere in the proof to read off. The property missed is exactly
non-constructiveness: the argument proves existence (positive probability of success) without exhibiting
any witness. 40.17 The claim violates Shannon's source-coding theorem (§40.2): the entropy $H = 1.5$ bits
(from 40.1) is a hard floor on the average length of any lossless code, not a soft average you can
optimize past. "Averages can be beaten by optimizing" is false here — the theorem proves no code's
average length can dip below $H$. (You can match it, e.g. with a near-optimal code, but never beat it.) 40.18 † The argument stalls at "if $ab$ is blue, we look further" — with only two same-colored
neighbors $a, b$ there is nothing further to look at: a single blue edge $ab$ gives no triangle, and
there is no third guaranteed same-color neighbor to complete one. Pigeonhole on a vertex of $K_5$ (four
edges, two colors) forces only $\lceil 4/2\rceil = 2$ of one color, which is too few. The $R(3,3) \le 6$
proof needs three same-color neighbors, which requires five incident edges, i.e. six vertices. This
is exactly why $R(3,3) = 6$ and not $5$ — and indeed $K_5$ has the pentagram coloring with no
monochromatic triangle, so the claim $R(3,3) \le 5$ is false. 40.19 Two errors. (1) Existence: not every LP has a finite optimum — an LP can be infeasible
(no feasible point) or unbounded (objective grows without limit), in which case there is no optimum
to speak of. The corner theorem is conditional: "if a finite optimum exists." (2) Location: a finite
optimum sits at a corner (vertex) of the feasible region, not in its interior — a linear objective
has constant gradient, so from any interior point you can move to improve until a constraint stops you,
at a corner (§40.1). 40.20 † (a) An assignment problem, a case of combinatorial optimization (a minimum-cost
perfect matching). (b) Variables $x_{ij} \in \{0,1\}$ ("truck $i$ on route $j$"); objective
$\text{minimize}\ \sum_{i,j} c_{ij}\,x_{ij}$; constraints $\sum_j x_{ij} = 1$ for each truck $i$ (one
route per truck), $\sum_i x_{ij} = 1$ for each route $j$ (one truck per route), and $x_{ij}\in\{0,1\}$.
(c) Trying all assignments enumerates $n!$ permutations: $3! = 6$ is trivial, but $300!$ is astronomically
larger than anything enumerable (cf. Chapters 15–17) — so for large $n$ you need an algorithm (the
Hungarian method, or an ILP/LP solver), not enumeration. 40.21 (a) Shannon entropy $H(X)$ (§40.2) — the theoretical minimum average bits per logged
choice. (b) $H = -\big(0.7\log_2 0.7 + 0.1\log_2 0.1 + 0.1\log_2 0.1 + 0.1\log_2 0.1\big)$ (do not
evaluate). (c) A fixed 2-bit-per-event log spends 2 bits regardless of skew, but the source is highly
predictable (70% one layout), so its entropy is well below 2 — the fixed code wastes the difference. 40.22 † (a) An edge-2-coloring of the complete graph $K_6$ (each of the six attendees a vertex; each
pair an edge colored "contact" = red or "non-contact" = blue). (b) The theorem is $R(3,3) = 6$ (§40.3):
every red/blue coloring of $K_6$ has a monochromatic triangle. The smallest guaranteeing group size is
six. (c) Five guests do not suffice: the pentagram coloring of $K_5$ (red iff endpoints differ
by 1 or 4 mod 5) has no monochromatic triangle, a construction witnessing $R(3,3) > 5$. 40.23 (a) The concept is the functor (§40.4), and specifically its composition law
$F(g\circ f) = F(g)\circ F(f)$. (b) 40.24 † (a) For $n=3$: uniform $\{1/3,1/3,1/3\}$ gives $H = \log_2 3 \approx 1.585$; e.g.
$\{1/2,1/4,1/4\}$ gives $1.5$, $\{0.8,0.1,0.1\}$ gives $\approx 0.922$, $\{0.6,0.3,0.1\}$ gives
$\approx 1.295$ — the uniform one is largest. (b) Conjecture: entropy on $n$ outcomes is maximized
by the uniform distribution, value $\log_2 n$. (c) $n=2$ proof: $H(p) = -p\log_2 p - (1-p)\log_2(1-p)$
is symmetric about $p = 1/2$ ($H(p) = H(1-p)$) and equals $0$ at the endpoints $p = 0, 1$; it is
continuous and has a single interior maximum, which by symmetry is at $p = 1/2$, where $H(1/2) =
-\tfrac12(-1) - \tfrac12(-1) = 1 = \log_2 2$. (With calculus: $H'(p) = \log_2\frac{1-p}{p} = 0 \Rightarrow
p = 1/2$, and $H'' < 0$, so it is the max.) $\blacksquare$ 40.25 (a) For $k = 4$: $E[X] = \binom{n}{4}\,2^{\,1-\binom{4}{2}} = \binom{n}{4}\,2^{-5} =
\binom{n}{4}/32$. We need $\binom{n}{4} < 32$: $\binom{6}{4} = 15 < 32$ ✓ but $\binom{7}{4} = 35 \ge 32$
✗, so the largest such $n$ is $6$, yielding $R(4,4) > 6$. (b) The known value is $R(4,4) = 18$, so the
probabilistic bound ($>6$) is correct but loose — far below the truth. (c) A loose lower bound is
still a genuine theorem: it truly proves $R(4,4) > 6$ (a coloring of $K_6$ avoiding a monochromatic
$K_4$ exists). Being far from the exact value doesn't make it false — the method's strength is large
$k$, where it gives $R(k,k) > 2^{k/2}$, an exponential bound nothing constructive matches. 40.26 † (a) Using $C = 1 - H(p)$: $C(0) = 1$, $C(0.1) = 1 - 0.469 = 0.531$, $C(0.2) = 1 - 0.722 =
0.278$, $C(0.3) = 1 - 0.881 = 0.119$, $C(0.4) = 1 - 0.971 = 0.029$, $C(0.5) = 1 - 1 = 0$. (b) Not
linear: consecutive drops are $0.469, 0.253, 0.159, 0.090, 0.029$ — shrinking, not constant, so the
decrease is not linear. The conjecture is disproved. (c) $C$ is decreasing in $p$ on $[0, 1/2]$ and,
mirroring the binary entropy $H(p)$ (which rises steeply from 0 near $p=0$ then flattens toward its max
at $1/2$), $C$ falls steeply near $p = 0$ then flattens — it is convex on this range (since $H$ is
concave, $C = 1 - H$ is convex), dropping fast for small noise and slowly as $p$ approaches $1/2$. 40.27 † The max-flow problem is a linear program: maximize the flow value subject to capacity and
conservation constraints — this is the primal maximization. Its dual is the min-cut problem — a
minimization over $s$–$t$ cuts. Strong duality (§40.1) says a finite primal optimum equals its dual
optimum, which forces max flow $=$ min cut exactly. So the theorem is strong duality made concrete:
the maximization and minimization are dual LPs with equal optimal values. 40.29 † (1) The pigeonhole principle (Chapter 17) drives the upper bound: "a chosen vertex of
$K_6$ has five incident edges in two colors, so at least three share a color." (2) Linearity of
expectation (Chapter 20) drives Erdős's lower bound: "summing the monochromatic-indicator over all
$\binom{n}{k}$ subsets gives $E[X] = \binom{n}{k}\,2^{\,1-\binom{k}{2}}$ regardless of dependence." 40.31 † (a) The hardness of integer factoring (factoring a large $n = pq$). (b) Shor's
algorithm, which factors efficiently on a large quantum computer. (c) Many post-quantum schemes are
built on lattices (a discrete-geometry structure) and the problems believed hard on them. (d) The
number theory (modular arithmetic, primes, Euler/Fermat) is the on-ramp because it gives you the
language and intuition for these schemes, but the destination — lattice problems, post-quantum
constructions — lies beyond what Chapter 25 built; RSA is the start of the road, not its end. 40.33 Open-ended; a model answer picks three rows and writes honest skill→area→industry links, e.g.:
- "Because I can now build an adjacency matrix and run BFS/Dijkstra, I am on the on-ramp to
spectral graph theory and graph neural networks, which I'd see in routing and recommendation
systems."
- "Because I can now implement RSA and prove its correctness via Euler's theorem, I am on the
on-ramp to modern and post-quantum cryptography, which I'd see in TLS/HTTPS and secure
messaging."
- "Because I can now set up a linear program and read its duality, I am on the on-ramp to
approximation algorithms and operations research, which I'd see in scheduling and logistics
optimization."
Any three correct, specific links (a real skill from the book, not a vague gesture) earn full credit.
Part D — Implement it
target once
$\lceil n/\text{holes}\rceil \ge \text{target}$, which first happens at
$n = (\text{target}-1)\cdot\text{holes} + 1$.def min_to_guarantee(target, holes):
return (target - 1) * holes + 1
print(min_to_guarantee(4, 4)) # 4 cards of one suit
print(min_to_guarantee(3, 12)) # 3 sharing a birth month
# Expected output:
# 13
# 25
(4, 4): $(4-1)\cdot4 + 1 = 13$ (matches Exercise 17.13). For (3, 12): $(3-1)\cdot12 + 1 = 25$.from math import comb
def solutions_with_floor(n, lowers):
leftover = n - sum(lowers)
if leftover < 0:
return 0
k = len(lowers)
return comb(leftover + k - 1, k - 1)
print(solutions_with_floor(15, [2, 3, 0, 0])) # Exercise 17.8
# Expected output:
# 286
def poly_mult(p, q):
out = [0] * (len(p) + len(q) - 1)
for i, a in enumerate(p):
for j, b in enumerate(q):
out[i + j] += a * b
return out
def make_change_count(n, coins):
product = [1]
for d in coins:
factor = [1 if t % d == 0 else 0 for t in range(n + 1)]
product = poly_mult(product, factor)
return product[n] if n < len(product) else 0
print(make_change_count(12, [1, 5]))
# Expected output:
# 3
def derangement_prob(n):
a, b = 1, 0
for k in range(2, n + 1):
a, b = b, (k - 1) * (a + b)
d_n = b if n >= 1 else 1
fact = 1
for k in range(1, n + 1):
fact *= k
return d_n / fact
print([round(derangement_prob(n), 4) for n in range(1, 9)])
# Expected output:
# [0.0, 0.5, 0.3333, 0.375, 0.3667, 0.3681, 0.3679, 0.3679]
Part E — Find the error
Part F — Model it & Conjecture and test
Part G — Interleaved & Deep Dive
Chapter 18 — Recurrence Relations
tilings(n) from $t_n = t_{n-1} + t_{n-2}$, $t_1 = 1, t_2 = 2$:def tilings(n):
if n == 1: return 1
if n == 2: return 2
return tilings(n - 1) + tilings(n - 2)
print([tilings(n) for n in range(1, 9)])
# Expected output:
# [1, 2, 3, 5, 8, 13, 21, 34]
code/exercise-solutions.py.)def hanoi(n):
h = 0
for _ in range(n):
h = 2 * h + 1
return h
def hanoi_closed(n):
return 2**n - 1
print([hanoi(n) for n in range(7)]) # [0, 1, 3, 7, 15, 31, 63]
print([hanoi_closed(n) for n in range(7)]) # [0, 1, 3, 7, 15, 31, 63]
mystery makes one recursive call on $n-1$ and runs a loop of $n$
iterations (the linear scan), so its running time satisfies
$$T(n) = T(n-1) + cn.$$
Unroll: $T(n) = cn + c(n-1) + c(n-2) + \cdots + c\cdot 2 + T(1) = c\sum_{i=2}^{n} i + T(1)
= c\big(\tfrac{n(n+1)}{2} - 1\big) + T(1) = O(n^2)$ — quadratic. This matches the §18.6 table row
"one call on $n-1$, $O(n)$ work $\Rightarrow T(n) = T(n-1) + cn \Rightarrow O(n^2)$" (the cost shape of
selection sort).
Notes for the appendix assembler
Chapter 19 — Solutions to Selected Exercises
exercises.md.
Part A
Part B
Part C
Part D
Part E
code/exercise-solutions.py. Core:import math
def master_case(a, b, d):
E = math.log(a, b)
if abs(d - E) < 1e-9: # tolerance, not ==
return 2
return 1 if d < E else 3
# master_case(2,2,1) -> 2 (E=1=d, tie, merge sort)
# master_case(1,2,0) -> 2 (E=0=d, tie, binary search)
# master_case(2,2,2) -> 3 (E=1<d=2, root wins)
# Expected output (printed together): 2 2 3
math.log(8,2) can return 2.9999999999999996.code/exercise-solutions.py (fib_fast). For $n=10 = 1010_2$ the loop runs 4 times,
folding $M^2$ and $M^8$ into result to build $M^{10}$, whose top-right entry is $F_{10} = 55$. The list
[fib_fast(k) for k in range(8)] is $F_0,\dots,F_7 = [0,1,1,2,3,5,8,13]$.# Expected output:
# 55
# [0, 1, 1, 2, 3, 5, 8, 13]
code/exercise-solutions.py (tree_total). For $(2,2,1,8)$: sizes $8,4,2,1$; nodes
$1,2,4,8$; per-level work $8,8,8,8$; sum $= 32 = 8(\log_2 8 + 1) = 8\cdot 4$. ✓ For $(2,2,2,8)$:
sizes-squared $64,16,4,1$; nodes $1,2,4,8$; per-level work $64,32,16,8$; sum $= 64+32+16+8 = \mathbf{120}$.
(Note: a decreasing geometric series, root-dominated, consistent with Case 3 / $\Theta(n^2)$;
$120 < 2\cdot 8^2 = 128$.)# Expected output:
# 32
# 120
Part F
from math import gcd
from dmtoolkit.recurrences import fib
ok = all(gcd(fib(m), fib(n)) == fib(gcd(m, n))
for m in range(1, 13) for n in range(1, 13))
print(ok)
# Expected output:
# True
True confirms the identity held on every tested pair in the $12\times 12$ grid. By theme four, this
is strong evidence but not a proof: it checks finitely many pairs, while the identity claims all
$m, n \ge 1$. (Spot check: $\gcd(F_6, F_9) = \gcd(8, 34) = 2 = F_3 = F_{\gcd(6,9)}$. ✓)
Part G
Odd-numbered solutions also covered above
Chapter 20 — Worked Solutions to Daggered (†) and Odd-Numbered Exercises
exercises.md. They are written to be
assembled into appendices/answers-to-selected.md. Notation and results match Chapter 20's index.md
exactly. No code is executed; all # Expected output: values are hand-derived.
20.1 † (⭐, warm-up)
20.3 † (⭐, warm-up)
20.5 † (⭐, warm-up)
20.6 † (⭐⭐, prove it — scaffolded; fills the blanks)
20.7 (⭐⭐, prove it)
20.8 † (⭐⭐, prove it)
20.9 (⭐⭐, prove it)
20.10 † (⭐⭐, prove it — linearity)
20.11 (⭐⭐, prove it)
20.12 † (⭐⭐, implement it)
from fractions import Fraction
from itertools import product
def event_prob(space, predicate):
"""P(E) = |E| / |S| over an equally-likely space, as an exact Fraction."""
favorable = sum(1 for outcome in space if predicate(outcome))
return Fraction(favorable, len(space))
two_dice = list(product(range(1, 7), repeat=2)) # 36 equally likely (i, j)
print(event_prob(two_dice, lambda s: s[0] + s[1] == 7))
# Expected output:
# 1/6
Fraction reduces it automatically.
20.13 (⭐⭐, implement it)
from fractions import Fraction
from collections import Counter
from itertools import product
def distribution(space, rv):
"""Map each value of rv to its probability (Fraction) over an equally-likely space."""
counts = Counter(rv(s) for s in space)
n = len(space)
return {value: Fraction(c, n) for value, c in sorted(counts.items())}
space = list(product(range(1, 7), repeat=2))
dist = distribution(space, lambda s: s[0] + s[1])
print(dist[5], sum(dist.values()))
# Expected output:
# 1/9 1
20.14 † (⭐⭐, implement it)
from fractions import Fraction
def expected_value_uniform(space, rv):
n = len(space)
return sum(Fraction(rv(s), n) for s in space)
def variance_uniform(space, rv):
mu = expected_value_uniform(space, rv)
ex2 = expected_value_uniform(space, lambda s: rv(s) ** 2) # E[X^2]
return ex2 - mu * mu # Var = E[X^2] - (E[X])^2
die = [1, 2, 3, 4, 5, 6]
print(expected_value_uniform(die, lambda x: x),
variance_uniform(die, lambda x: x))
# Expected output:
# 7/2 35/12
20.15 (⭐⭐, implement it)
import random
def estimate_birthday(n, trials):
"""Empirical P(at least two of n people share a birthday in {1..365})."""
hits = 0
for _ in range(trials):
bdays = [random.randint(1, 365) for _ in range(n)]
if len(set(bdays)) < n: # a collision happened (a value repeated)
hits += 1
return hits / trials
random.seed(0)
print(round(estimate_birthday(23, 100_000), 2))
# Expected output (approximate; true value is 0.5073):
# 0.51
len(set(bdays)) < n detects
a repeat: a set discards duplicates, so its size drops below $n$ exactly when two birthdays match.
20.16 † (⭐⭐, find the error)
⚠️ Common Pitfall there).
20.17 (⭐⭐, find the error)
⚠️ Common Pitfall). So $E[XY] = 3.5 \times 3.5 = 12.25$ is valid.
20.18 † (⭐⭐, find the error)
🚪 Threshold
Concept in §20.4). Dependence among the events "person $i$ gets their own hat" does not block
linearity; it would only matter if we were multiplying the variables or computing a variance.
20.19 (⭐⭐, find the error)
⚠️ Common Pitfall and theme two of the book). "I
observed a good coloring once in 10 million trials" does establish existence in this case — you actually
saw one — but the reasoning offered does not generalize and is the wrong kind of justification: if the
good colorings had been rarer than 1 in 10 million you might never have seen one and would have (wrongly)
concluded none exist. Observing an event is consistent with any positive probability; not observing it
is consistent with a small-but-positive probability. To prove a good coloring exists for all instances
satisfying $m < 2^{k-1}$, you need the probabilistic-method argument of §20.6 (show
$P(\text{good coloring}) > 0$), which settles every case at once.
20.20 † (⭐⭐⭐, model it)
20.21 (⭐⭐⭐, model it then compute)
20.22 † (⭐⭐⭐, conjecture and test, then prove)
from itertools import permutations
from fractions import Fraction
def expected_fixed_points(n):
perms = list(permutations(range(n)))
total = sum(sum(1 for i, v in enumerate(p) if v == i) for p in perms)
return Fraction(total, len(perms))
for n in range(1, 6):
print(n, expected_fixed_points(n))
# Expected output:
# 1 1
# 2 1
# 3 1
# 4 1
# 5 1
20.23 (⭐⭐⭐, conjecture and test)
from itertools import permutations
from fractions import Fraction
def variance_fixed_points(n):
perms = list(permutations(range(n)))
fixed = [sum(1 for i, v in enumerate(p) if v == i) for p in perms]
N = len(perms)
mu = Fraction(sum(fixed), N)
ey2 = Fraction(sum(f * f for f in fixed), N)
return ey2 - mu * mu
for n in range(1, 7):
print(n, variance_fixed_points(n))
# Expected output:
# 1 0
# 2 1
# 3 1
# 4 1
# 5 1
# 6 1
20.24 † (⭐⭐⭐, conjecture and test — coupon collector)
import random
def collect_all(m):
"""Number of boxes bought until all m coupons are seen (one trial)."""
seen, boxes = set(), 0
while len(seen) < m:
seen.add(random.randint(1, m))
boxes += 1
return boxes
def estimate_coupon(m, trials):
return sum(collect_all(m) for _ in range(trials)) / trials
random.seed(0)
for m in (4, 6, 10):
print(m, round(estimate_coupon(m, 50_000), 2))
# Expected output (approximate; compare to m*H_m below):
# 4 8.33
# 6 14.70
# 10 29.29
20.25 † (⭐⭐⭐, interleaved — Ch. 16 + 20)
20.27 † (⭐⭐, interleaved — Ch. 8 + 20)
20.29 (⭐⭐⭐, interleaved — Ch. 19 + 20, Fibonacci)
20.31 (⭐⭐⭐, deep dive — probabilistic method, $k = 4$)
Chapter 21 — Conditional Probability and Bayes' Theorem
posterior_after_two_tests(0.001, 0.99, 0.01). First update: $P(B_1) = 0.99(0.001) +
0.01(0.999) = 0.01098$; posterior $= 0.00099/0.01098 \approx 0.0902$. Feed $0.0902$ in as the new prior:
$P(B_2) = 0.99(0.0902) + 0.01(0.9098) = 0.089298 + 0.009098 = 0.098396$; second posterior $=
0.089298/0.098396 \approx 0.9075$. The second positive pushes belief above the first posterior (from
$\approx 0.09$ to $\approx 0.91$): independent positives compound because each starts from a much higher
prior than $0.001$. (Code: code/exercise-solutions.py.)naive_bayes_predict(priors, likelihoods, features) returns the class maximizing $P(c)\prod_i
P(f_i\mid c)$ via `max(priors, key=score)`. On the two-word example: spam score $0.4\times0.30\times0.20 =
0.024$, ham score $0.6\times0.05\times0.04 = 0.0012$; since $0.024 > 0.0012$ it returns "spam". (Code:
code/exercise-solutions.py.)conditional_from_counts(counts, given, target) divides the count of rows matching both
given and target by the count of rows matching given. Example $2\times2$ count table: paid/convert
$=10$, paid/no $=30$, organic/convert $=15$, organic/no $=45$ (totals to 100). Then $P(\text{convert}\mid
\text{paid}) = \frac{10}{10+30} = \frac{10}{40} = 0.25$, which agrees with dividing the probability cells
$\frac{0.10}{0.40}$. Counts and probabilities give the same conditional because the common total ($100$)
cancels. (Code: code/exercise-solutions.py.)
Chapter 22 — Number Theory Foundations
code/exercise-solutions.py (divisors_fast). Testing past $\sqrt{n}$ is unnecessary
because divisors come in pairs $d \cdot (n/d) = n$: if $d > \sqrt{n}$ then $n/d < \sqrt{n}$, so the
larger partner is already discovered when we find the smaller one. On $n = 36$:
[1, 2, 3, 4, 6, 9, 12, 18, 36].code/exercise-solutions.py (lcm). Dividing first — a // gcd(a,b) * b — keeps the
intermediate value smaller than a * b, avoiding overflow in fixed-width integer languages and reducing
the size of the big-integer multiplication; since $\gcd(a,b) \mid a$, the division is exact, so the
result is unchanged. On $(12, 18)$: $\gcd = 6$, $12/6 \cdot 18 = 2 \cdot 18 = 36$.code/exercise-solutions.py (ext_gcd_iter). It maintains
(old_r, r), (old_x, x), (old_y, y) and applies the same update new = old - q*current to all
three; on $(252, 198)$ it returns (18, 4, -5), matching the recursive ext_gcd. Verify:
$252 \cdot 4 + 198 \cdot (-5) = 1008 - 990 = 18$. ✓code/exercise-solutions.py (mod_inverse_via_bezout). For $a = 7$, $n = 26$: the
Euclidean run is $26 = 3\cdot 7 + 5$, $7 = 1\cdot 5 + 2$, $5 = 2\cdot 2 + 1$, $2 = 2\cdot 1 + 0$, so
$\gcd = 1$. Back-substitution: $1 = 5 - 2\cdot 2 = 5 - 2(7 - 5) = 3\cdot 5 - 2\cdot 7 =
3(26 - 3\cdot 7) - 2\cdot 7 = 3\cdot 26 - 11\cdot 7$, so $7\cdot(-11) \equiv 1 \pmod{26}$;
$-11 \bmod 26 = 15$. Check: $7 \cdot 15 = 105 = 4\cdot 26 + 1$. Inverse is 15.code/exercise-solutions.py (gcd_steps): gcd_steps(13, 8) == 5 and
gcd_steps(100, 99) == 2. Hand-tracing Fibonacci pairs like $(13,8)$ shows every quotient is $1$
(maximal steps for the size), whereas a pair like $(100, 99)$ finishes in $2$ steps — evidence for the
worst-case conjecture. (c) Claim: $\gcd(F_{n+1}, F_n) = 1$ for all $n \ge 1$, where $F_1 = F_2 = 1$,
$F_{m} = F_{m-1} + F_{m-2}$. Proof by induction on $n$. Base ($n = 1$): $\gcd(F_2, F_1) =
\gcd(1, 1) = 1$. **Step:** assume $\gcd(F_{k+1}, F_k) = 1$. Since $F_{k+2} = F_{k+1} + F_k$, the
Division Algorithm gives $F_{k+2} = 1\cdot F_{k+1} + F_k$, so by the Euclidean key lemma
$\gcd(F_{k+2}, F_{k+1}) = \gcd(F_{k+1}, F_k) = 1$ by the inductive hypothesis. By induction,
consecutive Fibonacci numbers are coprime for all $n \ge 1$. $\blacksquare$ (This coprimality, with
every quotient equal to $1$, is exactly what makes them the Euclidean worst case.)code/exercise-solutions.py (is_prime_trial). For $p = 2,3,5,7$ the values
$2^p - 1 = 3, 7, 31, 127$ are prime; the smallest prime $p$ with $2^p - 1$ composite is $p = 11$,
since $2^{11} - 1 = 2047 = 23 \times 89$. So the conjecture "$p$ prime $\Rightarrow 2^p - 1$ prime" is
false. (c) Claim: if $2^n - 1$ is prime then $n$ is prime. Proof (contrapositive). Suppose $n$
is composite, $n = ab$ with $1 < a, b < n$. Using the factorization $x^b - 1 = (x - 1)(x^{b-1} + \cdots
+ x + 1)$ with $x = 2^a$:
$$2^n - 1 = 2^{ab} - 1 = (2^a)^b - 1 = (2^a - 1)\big((2^a)^{b-1} + \cdots + 2^a + 1\big).$$
Since $1 < a < n$, the factor $2^a - 1$ satisfies $1 < 2^a - 1 < 2^n - 1$, so it is a nontrivial
divisor; thus $2^n - 1$ is composite. Contrapositively, $2^n - 1$ prime forces $n$ prime.
$\blacksquare$
Chapter 23 — Modular Arithmetic
[] (no solutions). (See
code/exercise-solutions.py for linear_congruence.)crt([m_p, m_q], [p, q]) finds the unique $m$ in $[0, n)$ with $m \equiv m_p \pmod p$ and $m \equiv m_q
\pmod q$, via $m = m_p N_p M_p + m_q N_q M_q \bmod n$ where $N_p = q,\ N_q = p$. Because $p, q$ are
distinct primes (hence coprime), Theorem 23.5 makes this $m$ unique modulo $n = pq$ — it is exactly
the decryption $c^d \bmod n$.
Chapter 24 — Algebraic Structures: Groups, Rings, and Fields
element_order(a, n) adds a to a running total until it is $\equiv 0 \pmod n$, counting
steps:def element_order(a, n):
k, total = 1, a % n
while total % n != 0:
total += a; k += 1
return k
element_order(4, 12): total $=4$ ($k{=}1$) → $8$ ($k{=}2$) → $12\equiv 0$ ($k{=}3$). Expected
output: 3. (The order of $4$ in $(\mathbb{Z}_{12},+)$ is $3$, and $3\mid 12$ by Lagrange.) Full code
in code/exercise-solutions.py.is_generator(g, n) lists the distinct powers of g mod n and checks the count equals
$\phi(n) = \lvert\mathbb{Z}_n^*\rvert$:def is_generator(g, n):
phi = sum(1 for a in range(1, n) if gcd(a, n) == 1)
return len(powers_mod(g, n)) == phi
is_generator(3, 7): powers of 3 mod 7 are $[3,2,6,4,5,1]$ (6 distinct) and $\phi(7)=6$ → True.
is_generator(2, 7): powers of 2 are $[2,4,1]$ (3 distinct) $\ne 6$ → False. Expected output:
True False.
Chapter 25 — Cryptography: From Caesar Cipher to RSA
MATH. Encrypt $c \equiv m + 7 \pmod{26}$:
- M=12 → $19$ = T; A=0 → $7$ = H; T=19 → $26 \equiv 0$ = A; H=7 → $14$ = O.MATH $\to$ THAO. Decrypt $m \equiv c - 7 \pmod{26}$: T=19 → $12$=M; H=7 → $0$=A; A=0 →
$-7 \equiv 19$=T; O=14 → $7$=H. Recovers MATH. ✓🔗 Connection: $\mathbb{Z}_{26}$ is a ring,
Chapter 24). $\blacksquare$code/exercise-solutions.py.)def shift_encrypt(text, k):
return "".join(chr((ord(c.upper())-65+k) % 26 + 65) if c.isalpha() else c for c in text)
def shift_decrypt(text, k):
return shift_encrypt(text, -k) # -k is the additive inverse of k mod 26
ct = shift_encrypt("HELLO", 3) # H->K E->H L->O L->O O->R
print(ct, shift_decrypt(ct, 3))
# Expected output:
# KHOOR HELLO
HELLO because adding $k$ then $-k$ is the identity in $\mathbb{Z}_{26}$.pub, d = rsa_keygen_from_primes(5, 11, 3)
c = rsa_encrypt(9, pub); back = rsa_decrypt(c, d, pub[0])
print(pub, d, c, back)
# Expected output:
# (55, 3) 27 14 9
E ≈ 12.7%). Frequency analysis (§25.1) reads off the cipher by
matching the most common ciphertext symbols to E, T, A, O,… — exploiting that preserved-frequency
structure, never enumerating the $26!$ keys. Property exploited: deterministic, frequency-preserving
substitution.pub = (55, 3)
print([(m, pow(m, 3, 55)) for m in range(11)])
# Expected output:
# [(0,0),(1,1),(2,8),(3,27),(4,9),(5,15),(6,51),(7,13),(8,17),(9,14),(10,10)]
Chapter 26 — Hashing, Checksums, and Error Detection
10110000
1011 <- XOR at the leading 1
----
0000000 <- everything cancels; no further leading 1 to align G
verify_isbn10(digits) returns sum((i+1)*d for i,d in enumerate(digits)) %
11 == 0. Test on the valid ISBN $[1,2,3,4,5,6,7,8,9,10]$: weighted sum $= 285 + 10\cdot 10 = 385 =
11\cdot 35$, so $385 \bmod 11 = 0$ → `True`. Swap the first two digits to $[2,1,3,4,5,6,7,8,9,10]$:
weighted sum $= 1\cdot2 + 2\cdot1 + 285 - (1\cdot1 + 2\cdot2) = $ compute directly $2+2+9+16+25+36+49+64+
81+100 = 384$; $384 = 11\cdot34 + 10$, so $384 \bmod 11 = 10 \ne 0$ → False. The transposition is
caught because, with a prime modulus and distinct positional weights, swapping adjacent digits shifts the
sum by $\pm(d_i - d_{i+1}) \ne 0 \pmod{11}$ (§26.4). (See code/exercise-solutions.py.)chain_lengths(keys, m) builds counts=[0]*m and increments counts[k % m] for each key.
For keys=[10,21,32,43,7], m=11: $10,21,32,43$ all satisfy $k \bmod 11 = 10$ ($21=11+10$, $32=22+10$,
$43=33+10$), and $7 \bmod 11 = 7$. So counts = [0,0,0,0,0,0,0,1,0,0,4] (slot 7 has 1, slot 10 has 4).
Load factor $\alpha = 5/11 \approx 0.45$; the longest chain has length 4 — far above $\lceil\alpha
\rceil = 1$, because these particular keys were chosen congruent mod 11. (See
code/exercise-solutions.py.)
Chapter 27 — Introduction to Graphs
degree_sequence(adj) returns sorted(len(nbrs) for nbrs in adj.values()). On the study
group the degrees are Ana 2, Ben 2, Cam 3, Dev 2, Eve 1, so it returns [1, 2, 2, 2, 3]. The
handshaking lemma is confirmed because the sum of that sequence is $1+2+2+2+3 = 10 = 2\lvert E\rvert$
with $\lvert E\rvert = 5$ — an even sum, and exactly twice the edge count. (See
code/exercise-solutions.py.)is_handshaking_ok(adj) returns sum(len(nbrs) for nbrs in adj.values()) % 2 == 0. On
$\{$"a": {"b","c"}, "b": {"a"}, "c": {"a"}$\}$ the degrees are a 2, b 1, c 1, sum $=4$, which is even, so
it prints True. (Equivalently, there are two odd-degree vertices, b and c — an even count.)common_neighbors(self, u, v) returns self.adj[u] & self.adj[v] ($N(u)\cap N(v)$). For
the study group, $N(\text{Ana}) = \{$Ben, Cam$\}$ and $N(\text{Dev}) = \{$Cam, Eve$\}$, so
common_neighbors("Ana","Dev") $= \{$Cam$\}$. (Cam is the lone mutual friend — the basis of a
distance-2 "people you may know" suggestion.) See code/exercise-solutions.py.complete_graph(n) builds $K_n$ as {i: {j for j in range(n) if j != i} for i in range(n)}.
For $n=5$ every vertex has 4 neighbors; $\sum\deg = 20$, so num_edges() $= 20//2 = 10$, matching
$\binom{5}{2} = 10$. ✓frozenset is the faithful encoding.
A directed edge is an ordered pair $(u,v)$ (a Chapter 8 Cartesian-product element), where
$(u,v)\ne(v,u)$ unless $u=v$ — order is the direction. The set of all possible edges of a simple graph
on $n$ vertices is the set of 2-element subsets of $V$, of which there are $\binom{n}{2}$.
Chapter 28 — Graph Representations, Traversal, and Search
adj = {0: [1, 2], 1: [0, 3], 2: [0, 3], 3: [1, 2, 4], 4: [3], 5: []}
(edges $\{0,1\},\{0,2\},\{1,3\},\{2,3\},\{3,4\}$; vertex $5$ isolated).bfs_order(adj, 0) (front on the left):
Step
Dequeue
Append to order
Newly discovered (enqueued)
Queue after
start
—
—
$0$
$[0]$
1
$0$
$0$
$1, 2$
$[1,2]$
2
$1$
$1$
$3$ ($0$ already visited)
$[2,3]$
3
$2$
$2$
— ($0,3$ visited)
$[3]$
4
$3$
$3$
$4$ ($1,2$ visited)
$[4]$
5
$4$
$4$
— ($3$ visited)
$[\,]$
[0, 1, 2, 3, 4]. Vertex $5$ never appears (unreachable), confirming the §28.2 trace.def neighbors_via_matrix(A, v):
return [j for j in range(len(A)) if A[v][j] == 1]
print(neighbors_via_matrix(A_example, 3))
# Expected output:
# [1, 2, 4]
def to_adjacency_list(A):
return {i: [j for j in range(len(A)) if A[i][j] == 1] for i in range(len(A))}
A = [[0,1,0,1],[1,0,1,0],[0,1,0,1],[1,0,1,0]]
print(to_adjacency_list(A))
# Expected output:
# {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}
def degrees_of_separation(adj, s, t):
dist, _ = bfs_distances(adj, s)
return dist.get(t, -1)
print(degrees_of_separation(adj, 0, 4)) # 0->1->3->4, three edges
print(degrees_of_separation(adj, 0, 5)) # 5 unreachable from 0
# Expected output:
# 3
# -1
dist; dist.get(5, -1)
returns the sentinel $-1$, the correct "unreachable / $\infty$" report.def count_components(adj):
seen = set()
count = 0
for start in adj:
if start not in seen:
seen.update(bfs_order(adj, start))
count += 1
return count
print(count_components(adj))
# Expected output:
# 2
count = 1), skips $1$–$4$ (all in
seen), then launches at $5$ (the singleton $\{5\}$, count = 2). Two components.def dfs_iterative_matching(adj, s):
visited = set(); order = []; stack = [s]
while stack:
v = stack.pop()
if v in visited:
continue
visited.add(v); order.append(v)
for w in reversed(adj[v]):
if w not in visited:
stack.append(w)
return order
print(dfs_iterative_matching(adj, 0))
# Expected output:
# [0, 1, 3, 2, 4]
[0,1,3,2,4], identical to the recursive dfs_order(adj, 0).if w not in
visited test passes until the vertex is actually dequeued and marked). It is then dequeued and appended
to order once per copy. Exposing graph: the triangle {0:[1,2], 1:[0,2], 2:[0,1]} from source $0$.
Step 1 dequeues $0$ (mark $0$), enqueues $1$ and $2$ — queue $[1,2]$. Step 2 dequeues $1$ (mark $1$); its
neighbor $2$ is not yet visited, so $2$ is enqueued again — queue $[2,2]$. Step 3 dequeues $2$ (mark
$2$), appends $2$. Step 4 dequeues the second $2$; it is visited now but the buggy code appends it
before checking — so order contains $2$ twice: [0, 1, 2, 2]. Marking at enqueue fixes it because
then the very first discoverer marks $2$, and no later neighbor passes the not in visited test, so each
vertex enters the queue exactly once.{0:[1], 1:[0]}. DFS visits $0$, marks
it, recurses to $1$; from $1$ it sees neighbor $0$, which is visited — the flawed rule reports a cycle.
But there is no cycle (a single edge is a tree). The correct §28.4 rule adds the condition "and the
visited vertex is not the parent": a back edge counts only when it leads to a visited vertex other than
the one DFS came from. With that condition, $1$'s edge to its parent $0$ is correctly ignored.comm = {'A':['B','C'], 'B':['A','D'], 'C':['A','D'], 'D':['B','C','E'], 'E':['D'], 'F':[]}
Graph
$n$
$m$
$T(G)$
$B(G)$
path $P_4$
4
3
3
0
triangle $C_3$
3
3
2
1
cycle $C_4$
4
4
3
1
example big component
5
5
4
1
$K_4$
4
6
3
3
queue = deque([s]) together with dist = {s: 0}: the queue holds only $s$ at
distance $0$, trivially satisfying the invariant. Maintenance is the line dist[w] = dist[v] + 1 (with
the queue.append(w)): when the front vertex $v$ at distance $d$ is dequeued, each newly discovered
neighbor is assigned $d+1$ and appended to the back, keeping the queue's values within $\{d, d+1\}$ then
$\{d+1\}$. This mirrors the loop-invariant technique of Chapter 6 (initialization = base case,
maintenance = inductive step), now applied to a graph algorithm; reading the invariant off at termination
gives $\mathrm{dist}[w] = \delta(s,w)$.topological_sort output the list $L$. (a) Every edge points
forward. For any edge $a \to b$, the finishing-time property (Exercise 28.11) gives "$b$ finishes before
$a$," so in decreasing finish order $a$ precedes $b$; since $L$ is exactly the reversed finish order
(decreasing finish time), $a$ appears before $b$ in $L$. (b) $L$ is a linear extension. The partial
order $\preceq$ in question is the reachability order whose covering relation is the edge set: $x \prec y$
whenever there is a directed path $x \rightsquigarrow y$. By (a) every edge is respected, and respecting
all edges respects all paths (transitivity), so $x \prec y \Rightarrow x$ before $y$ in $L$. Thus $L$ is
a total order consistent with $\preceq$ — a linear extension, exactly Chapter 13's definition of a
topological sort. Where acyclicity is used (secretly): in (a), the finishing-time property's third case
(an edge to an on-stack ancestor) is ruled out only because a DAG has no directed cycle; on a graph with
a cycle the property fails and no $L$ can respect all edges (there is no linear extension of a relation
with $x \prec x$). Acyclicity is what makes $\preceq$ a genuine partial order and the extension exist.
Chapter 29 — Weighted Graphs and Shortest Paths
dist = {B:0, others:∞}.
Round
Finalize
Edges relaxed
dist after
1
B (0)
B→D: $\infty\to1$; B→E: $\infty\to7$
B:0, D:1, E:7
2
D (1)
D→E: $7\to4$
B:0, D:1, E:4
3
E (4)
(none)
B:0, D:1, E:4
prev = {A: None, C: A, B: C, D: B, E: D} (B's
predecessor is C because A → C → B beat A → B; D's is B; E's is D). Reconstructing the path to D:
start at D, prev[D] = B, prev[B] = C, prev[C] = A, prev[A] = None. Reversed: A → C → B → D, of cost
$\delta(A, D) = 4$.code/exercise-solutions.py. path_weight(adj, path) sums edge weights, returning
None on a missing hop. On ["A","C","B","D","E"]: $1 + 2 + 1 + 3 = 7$. On ["A","E"]: None (no
direct edge A → E). Expected output: 7 then None.code/exercise-solutions.py. dijkstra_paths threads a prev pointer through each
relaxation; path_to walks predecessors backward and reverses. For source "A", target "E":
['A', 'C', 'B', 'D', 'E'] cost 7.code/exercise-solutions.py. With $0\to1:4$, $0\to2:5$, $1\to2:-2$ the only improvement
is $D[0][2]: 5 \to 2$ (routing $0 \to 1 \to 2 = 4 + (-2)$). Expected output rows: [0, 4, 2],
['INF', 0, -2], ['INF', 'INF', 0].dist first becomes finite.i / j / k (waypoint innermost). Trace on $0\to2:1$, $2\to1:2$ (so the only
two-edge improvement is $0 \to 2 \to 1$, value 3). With i outermost, fix $i = 0$ and run j, then k.
For $i=0, j=1$: we test $D[0][k] + D[k][1]$ over $k$. We need $k = 2$: $D[0][2] + D[2][1] = 1 + 2 = 3 <
\infty$, so $D[0][1] \to 3$ — this particular trace happens to catch it. The bug bites when an update
needs a $D[i][k]$ that itself required an earlier waypoint not yet processed: because k is innermost,
$D[0][2]$ might still be its raw value when used, and a chain like $0 \to 3 \to 2 \to 1$ (needing $D[0][2]$
to already include vertex 3) is missed. The point: with k innermost the recurrence reads stale
sub-results, so longer routed paths are silently dropped. Correct order: k (waypoint) outermost, then
i, then j — the §29.5 mantra "waypoint first, then source, then destination."dijkstra_paths returns one of them (whichever the tie-break/heap order picks), confirming the tool
computes a shortest path, not the. The conjecture is false; ties produce multiple shortest paths.
(The chapter is careful about this in §29.1: "we compute a shortest path and the shortest distance.")
Chapter 30 — Euler Paths, Hamilton Paths, and the Traveling Salesman Problem
count_odd_degree(adj) returns sum(1 for v in adj if len(adj[v]) % 2 == 1), and
euler_kind maps the count $0\mapsto$ "circuit", $2\mapsto$ "path", else "neither". On the §30.2
graph {1:[2,5,3], 2:[1,3], 3:[2,4,1,5], 4:[3,5], 5:[4,1,3]} the degrees are $1{:}3, 2{:}2, 3{:}4,
4{:}2, 5{:}3$, so two odd vertices ($1$ and $5$) → output 2 path. (See code/exercise-solutions.py.)
This is the $O(V+E)$ existence decision — no route search — which is the whole point of Euler's theorem.is_hamilton_circuit(vertices, edge_set, candidate) checks three things: (1)
candidate[0] == candidate[-1] (closed); (2) the body candidate[:-1] is a permutation of vertices
(every vertex exactly once); (3) every consecutive pair is in edge_set. On $K_4$: [1,2,3,4,1] →
closed ✓, body $\{1,2,3,4\}$ ✓, all pairs are edges ✓ → True. [1,2,4,4,1] → body $[1,2,4,4]$ is
not a permutation of $\{1,2,3,4\}$ (4 appears twice, 3 missing) → False. Both checks run in $O(n)$,
the linear-time verifier §30.5 describes (verifying is easy even though finding is NP-hard).
Chapter 31 (Trees) — Worked Solutions
chapter-31-trees/exercises.md. (Daggered set: 31.1, 31.3, 31.5, 31.7, 31.9, 31.11, 31.13, 31.15,
31.17, 31.19, 31.21, 31.23, 31.24, 31.26, 31.27, 31.29, 31.31, 31.37. Odd set: all odd 31.1–31.37.
Union = every odd-numbered exercise plus the daggered evens 31.24 and 31.26.) These are destined for
appendices/answers-to-selected.md. The remaining odd exercises 31.33 and 31.35 are solved above; the
even-numbered ones are summarized at the end for the instructor set.
Part A — Warm-ups
31.1 †
31.3 †
31.5 †
8
/ \
3 10
/ \ \
1 6 14
Part B — Prove it
31.7 † (scaffolded — forest edge count)
31.9 † (inorder of a BST is sorted)
31.11 † (size and leaf bounds for height $h$)
Part C — Implement it
31.13 † (height of a binary tree)
def height(node):
if node is None:
return -1 # empty tree convention
return 1 + max(height(node.left), height(node.right))
Node(1, Node(2, Node(4)), Node(3)): height(4) = 1 + max(-1,-1) = 0;
height(2) = 1 + max(height(4), -1) = 1 + max(0, -1) = 1; height(3) = 0;
height(1) = 1 + max(height(2), height(3)) = 1 + max(1, 0) = 2.# Expected output:
# 2
code/exercise-solutions.py.31.15 † (BST validity via a passed-down range)
def is_bst(node, lo=float('-inf'), hi=float('inf')):
if node is None:
return True
if not (lo < node.val < hi):
return False
return is_bst(node.left, lo, node.val) and is_bst(node.right, node.val, hi)
5
/ \
3 8
\
6 6 is in 5's LEFT subtree but 6 > 5 -> NOT a BST
code/exercise-solutions.py.
Part D — Find the error
31.17 †
31.19 †
Part E — Model it
31.21 † (file systems)
/;
not binary because a directory may contain any number of entries./ to a file — i.e. the maximum directory nesting
depth. (Path length in the cd sense.)ls -R visits each file exactly once: because the structure is a tree — there is exactly
one path from the root to each object (Theorem 31.1, unique paths), and "no hard links" is precisely
the condition that nothing is contained twice (no second parent, no cycle). A recursive descent
therefore reaches every object once and only once. It most resembles a preorder traversal: ls -R
prints a directory's own listing before descending into its subdirectories (parent before children =
root-first = preorder).31.23 † (committee org chart)
Part F — Conjecture and test
31.24 † (internal path length of a perfect tree)
$h$
$n = 2^{h+1}-1$
nodes per depth
IPL
0
1
depth 0: 1
$0$
1
3
0:1, 1:2
$0 + 2(1) = 2$
2
7
0:1, 1:2, 2:4
$0 + 2 + 4(2) = 10$
3
15
…, 3:8
$10 + 8(3) = 34$
31.26 † (number of Huffman merges)
Part G — Interleaved & Deep Dive
31.27 † (handshaking + average degree of a tree)
31.29 † (level-order = BFS on a tree)
Node(7, Node(3, Node(1), Node(5)), Node(9))):
$7,\ 3,\ 9,\ 1,\ 5$ (root; then its children $3, 9$; then $3$'s children $1, 5$; node $9$ is a leaf).31.31 † (heap makes Dijkstra $O((V+E)\log V)$)
31.33 (complete the tree characterization)
31.35 (heap index arithmetic, Ch. 9 floor)
parent, left, and right formulas are mutually inverse. $\blacksquare$31.37 † (nodes per depth in a perfect tree)
Even-numbered exercises (not solved here)
answers-to-selected.md). Brief keys:
31.6 parent $\lfloor(6-1)/2\rfloor = 2$, children $13, 14$. 31.8 longest path has two endpoints,
each degree 1 by the Lemma 31.3 argument → $\ge 2$ leaves. 31.10 induct on leaves / strip a sibling
leaf pair → $\ell - 1$ internal nodes. 31.14/31.16 see code/exercise-solutions.py. 31.20 the
claim (postorder + inorder determine a binary tree) is true; for the contrast in the hint, a single
left-chain and a single right-chain on the same labels share preorder or postorder but not both — used
to show why one traversal is insufficient. 31.32 Kraft: leaves at depth $\ell_i$ own disjoint
fractions $2^{-\ell_i}$ of a perfect tree of depth $\max\ell_i$, so they sum to $\le 1$.
Chapter 32 — Spanning Trees, Minimum Spanning Trees, and Network Design
Edge
$w$
Components before
Same?
Action
$PR$
1
$\{P\},\{R\}$
no
add → $\{P,R\}$
$QR$
2
$\{P,R\},\{Q\}$
no
add → $\{P,Q,R\}$
$RT$
3
$\{P,Q,R\},\{T\}$
no
add → $\{P,Q,R,T\}$
$PQ$
4
$P,Q$ both in $\{P,Q,R,T\}$
yes
skip (cycle)
$RS$
5
$\{P,Q,R,T\},\{S\}$
no
add → all five
parent = [0,1,2,3,4].
- union(0,1): find(0)=0, find(1)=1 → parent[0]=1. Now [1,1,2,3,4].
- union(2,3): → parent[2]=3. Now [1,1,3,3,4].
- union(1,2): find(1)=1, find(2): $2\to3$, so $3$ → parent[1]=3. Now [1,3,3,3,4].
- find(3) → $3$ (root); no change.
- find(0): path $0\to1\to3$. With path compression (halving) parent[0]=parent[parent[0]]=parent[1]=3,
then $x=3$ is root. After this, parent[0]=3. Final parent = [3,3,3,3,4]. (Elements $0,1,2,3$ share
root $3$; element $4$ is its own set.)def tree_weight(mst_edges):
return sum(w for _, _, w in mst_edges)
print(tree_weight([(1, 2, 1), (0, 1, 2), (1, 3, 4), (2, 4, 5)]))
# Expected output:
# 12
code/exercise-solutions.py.)code/exercise-solutions.py):def kruskal_forest(n, edges):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]; x = parent[x]
return x
forest, total = [], 0
for w, u, v in sorted(edges):
ru, rv = find(u), find(v)
if ru != rv:
parent[ru] = rv; forest.append((u, v, w)); total += w
return forest, total, len({find(x) for x in range(n)})
print(kruskal_forest(4, [(1, 0, 1), (2, 2, 3)]))
# Expected output:
# ([(0, 1, 1), (2, 3, 2)], 3, 2)
mst_kruskal(g). To the committee: "This layout is the cheapest possible connected network; the
cut property guarantees no connected layout is cheaper, because each chosen edge is the minimum-weight
edge crossing the boundary of the already-connected set." Wrong tool: if the real objective is
minimum latency between two specific buildings, an MST is wrong — use a shortest-path tree
(Dijkstra, Chapter 29); if the objective is fault tolerance, the MST (a tree) has none — add edges to
make it 2-edge-connected.
Chapter 33 — Worked Solutions (daggered † and odd-numbered exercises)
chapter-33-graph-coloring-planarity/exercises.md. (Every daggered exercise in this chapter is
odd-numbered, so the set below is exactly the odd-numbered problems 33.1–33.35.) Notation as in the
chapter: $\chi$ chromatic number, $\omega$ clique number, $\Delta$ maximum degree, $V,E,F$ vertex/edge/
face counts.
33.1 † — Chromatic numbers of standard graphs
33.3 † — Euler's formula for $E$
33.5 † — A 3-coloring of $C_5$
33.7 (odd) — Interference graph and register count
Value
pqrs
Live lines
1–3
2–4
3–5
4–5
p(1–3) overlaps q(2–4) and r(3–5) → $pq, pr$; q(2–4)
overlaps r(3–5) and s(4–5) → $qr, qs$; r(3–5) overlaps s(4–5) → $rs$. So
$E = \{pq, pr, qr, qs, rs\}$. Note p–s is a non-edge (p dies at line 3, s born at line 4).
33.9 † — Maximum edges in a triangle-free planar graph
33.11 † — Channel assignment for 5 access points
33.13 † — Scaffolded proof of the clique lower bound (Theorem 33.1)
33.15 † — $\chi(C_n) = 3$ for odd $n \ge 3$
33.17 † — Every planar graph is 6-colorable
33.19 † —
greedy_coloring traced on $C_5$def greedy_coloring(adj, order):
color = {}
for v in order:
used = {color[u] for u in adj[v] if u in color}
c = 0
while c in used:
c += 1
color[v] = c
return color
adj = {0:[1,4],1:[0,2],2:[1,3],3:[2,4],4:[3,0]}, order=[0,1,2,3,4]:
- 0: no colored neighbors → 0.
- 1: neighbor 0=0 → 1.
- 2: neighbor 1=1 (3 uncolored) → 0.
- 3: neighbor 2=0 (4 uncolored) → 1.
- 4: neighbors 3=1, 0=0 → 2.{0:0, 1:1, 2:0, 3:1, 4:2}; colors used 1 + max = 3. (Matches $\chi(C_5)=3$ — greedy is optimal
this time.)# Expected output:
# {0: 0, 1: 1, 2: 0, 3: 1, 4: 2}
# colors: 3
33.21 † —
is_bipartite via BFS layer-parityfrom collections import deque
def is_bipartite(adj, start):
color = {start: 0}
q = deque([start])
while q:
u = q.popleft()
for w in adj[u]:
if w not in color:
color[w] = 1 - color[u]
q.append(w)
elif color[w] == color[u]:
return False
return True
{0:[1,3],1:[0,2],2:[1,3],3:[2,0]}, start 0: color 0=0; visit 0 → color 1=1, 3=1;
visit 1 → 0 already 0 (≠1 ok), color 2=0; visit 3 → 2 already 0 (≠1 ok), 0 already 0; visit 2 →
neighbors 1=1,3=1 both ≠ 0. No within-layer edge → True.{0:[1,4],1:[0,2],2:[1,3],3:[2,4],4:[3,0]}, start 0: color 0=0; 1=1,4=1; from 1
color 2=0; from 4 color 3=0; now processing 2, neighbor 3 has color 0 = color of 2 → within-layer
edge → False.# Expected output:
# C4 bipartite? True
# C5 bipartite? False
33.23 † — Find the error: "$\chi = \omega$ always"
33.25 † — Find the error: "Four Color Theorem ⇒ every graph 4-colorable"
33.27 † — Model it: exam scheduling for 6 courses
33.29 † — Conjecture and test: crown graph vs. greedy
$n$
greedy colors (bad order)
$\chi$
2
2
2
3
3
2
4
4
2
5
5
2
33.31 † — Induction skeleton for "planar ⇒ 6-colorable"
33.33 † — Easy vs. hard: 3-colorability vs. 2-colorability
33.35 — Anchor: $\chi$ as the fewest independent sets (social networks)
Chapter 34 — Network Flow and Matching
cut_capacity(M, {"s","a"}) -> s->b(3)+a->b(4)+a->t(3) = 10
Expected output: `10`. (See `code/exercise-solutions.py`.) Only $S\to T$ edges are summed; $s\to a$ is
internal to $S$ and excluded.
**34.25 †** Run Edmonds–Karp to a max flow, then one final BFS over the residual graph; return the reached
set. On $M$ the loop ends with residual edges out of $s$: $s\to a$ has residual $10-7=3>0$ (so $a\in S$),
but from $a$ every forward edge is saturated and the only back edge returns to $s$; $b,t$ unreachable.
**$S=\{s,a\}$.** (Full code in `code/exercise-solutions.py`; expected output `['a', 's']` when sorted.)
This $S$ is the source side of the min cut, capacity $3+4+3=10$.
**34.27 †** Kruskal is certified by the **cut property**: for *any* partition of the vertices, the lightest
edge crossing it is safe to add to some MST. Ford–Fulkerson is certified by **max-flow min-cut**:
exhibiting an s–t cut whose capacity *equals* the flow value proves the flow maximum. Structural analogy:
in both, "a cut" (a vertex partition) is the object that certifies a greedy step — but Kruskal *minimizes*
total edge weight to *connect* all vertices (the cut's lightest crossing edge is included), while
Ford–Fulkerson *maximizes* throughput, certified by the *minimum* crossing-capacity cut (whose edges are
all saturated). One uses the cheapest crossing *edge*; the other uses the cheapest crossing *total
capacity*.
**34.29 †** The reduction's correctness proof ("Why it works," §34.5) hinges on the claim that *each $x$
has a single incoming source edge and each $y$ a single outgoing sink edge*, which forces at most one
matched middle edge per vertex. That construction requires a clean two-sided split $X,Y$ with edges only
crossing between sides. In a **non-bipartite** graph (which has an odd cycle, Ch. 27) there is no such
split: a vertex might need to be on *both* sides, and an edge between two same-side vertices has no
direction in the $s$–$X$–$Y$–$t$ template. The exact failing step: "each $x\in X$ has a single incoming
edge $s\to x$… so $x$ sends out at most 1 unit" — there is no well-defined $X$ to attach source edges to,
so the matching read-out is not guaranteed to be a matching. (General matching needs the Blossom
algorithm, not this flow reduction.)
**34.31 †** (Hall from max-flow min-cut.) Assume no matching saturates $X$. Build the unit-capacity
reduction; the max flow has value $\lvert f_{\max}\rvert < \lvert X\rvert$, so by max-flow min-cut there is
a min cut $(S,T)$ with $c(S,T) < \lvert X\rvert$. Let $W = X\cap S$ and consider $N(W)$. **Claim
$N(W)\subseteq Y\cap S$:** if some $x\in W$ had a neighbor $y\in Y\cap T$, the middle edge $x\to y$ (cap 1)
would cross $S\to T$ and contribute 1 to the cut. **Counting the cut.** The cut consists of: (i) source
edges $s\to x$ for $x\in X\cap T = X\setminus W$, contributing $\lvert X\rvert - \lvert W\rvert$; (ii) sink
edges $y\to t$ for $y\in Y\cap S$, contributing $\lvert Y\cap S\rvert$; (iii) any crossing middle edges. A
*minimum* cut never needs to cut a middle edge: if $x\to y$ with $x\in W, y\in Y\cap T$ were cut, we could
instead move $y$ into $S$ (cutting its sink edge, cost 1) — so WLOG the min cut cuts no middle edges, which
forces $N(W)\subseteq Y\cap S$. Then
$$c(S,T) = (\lvert X\rvert - \lvert W\rvert) + \lvert Y\cap S\rvert \ge (\lvert X\rvert - \lvert W\rvert) + \lvert N(W)\rvert.$$
Since $c(S,T) < \lvert X\rvert$, we get $(\lvert X\rvert - \lvert W\rvert) + \lvert N(W)\rvert < \lvert X\rvert$,
i.e. $\lvert N(W)\rvert < \lvert W\rvert$. So $W$ violates Hall's condition. Contrapositive: if Hall's
condition holds for all $W$, an $X$-saturating matching exists. $\blacksquare$
---
## Chapter 35 — Automata and Formal Languages
**35.1 †** Strings of length $\le 2$ in $L_{\text{even}}$ (even number of $1$s): length $0$:
$\varepsilon$ (zero $1$s, even). Length $1$: $0$ (zero $1$s). Length $2$: $00$ (zero $1$s) and $11$ (two
$1$s). So $\{\varepsilon, 0, 00, 11\}$. Of length exactly $3$: we need an even number of $1$s among three
positions, i.e. $0$ or $2$ ones. Zero ones: $000$ (one string). Two ones: $\binom{3}{2} = 3$ strings
($110, 101, 011$). Total $1 + 3 = 4$.
**35.3 †** Trace of the parity DFA ($Q=\{E,O\}$, start $E$, accept $\{E\}$; `0` self-loops, `1` flips) on
`11010`:
$$E \xrightarrow{1} O \xrightarrow{1} E \xrightarrow{0} E \xrightarrow{1} O \xrightarrow{0} O.$$
Ends in $O$, which is non-accepting, so `11010` is **rejected**. Check: `11010` has three $1$s (odd), so
rejection is correct.
**35.5 †** A DFA accepting exactly $\{\varepsilon\}$ over $\Sigma=\{0,1\}$:
$Q=\{q_0, d\}$, $\Sigma=\{0,1\}$, start $q_0$, $F=\{q_0\}$, and
$\delta(q_0,0)=\delta(q_0,1)=d$, $\delta(d,0)=\delta(d,1)=d$. The start state is accepting (so
$\varepsilon$, read with no symbols, is accepted), but the very first symbol drives the machine to the
dead state $d$, which loops forever and is non-accepting. Hence only $\varepsilon$ is accepted.
**35.7 †** Blanks, with the cited conditions:
- Choose $s = 0^{p}1^{p}$, length $2p \ge p$.
- The first $p$ symbols are all **0**s, so (using $\lvert xy\rvert \le p$, condition 2) $y$ consists of
only **0**s, $y=0^{k}$, $k\ge 1$ (using $\lvert y\rvert > 0$, condition 1).
- Pump with $i = 0$ (or any $i \ne 1$; $i=2$ works too): $xy^{0}z = 0^{p-k}1^{p}$, which has fewer $0$s
than $1$s, so it is **not in** $L$ — contradicting condition **(3)** ($xy^{i}z\in L$ for all $i\ge 0$).
Therefore $L$ is not regular. $\blacksquare$
(With $i=2$: $xy^{2}z=0^{p+k}1^{p}$, more $0$s than $1$s, also not in $L$.)
**35.9 †** *Claim:* $L=\{0^{n}1^{m}\mid n>m\ge 0\}$ is not regular. *Proof.* Assume regular with pumping
length $p$. Choose $s = 0^{p+1}1^{p}$ (here $n=p+1 > m=p$, so $s\in L$); $\lvert s\rvert = 2p+1\ge p$. The
lemma gives $s=xyz$, $\lvert y\rvert>0$, $\lvert xy\rvert\le p$. Since the first $p$ symbols are all $0$s,
$y=0^{k}$ with $k\ge 1$. Pump *down* with $i=0$: $xy^{0}z = 0^{(p+1)-k}1^{p}$. Its number of $0$s is
$(p+1)-k$ and number of $1$s is $p$. Because $k\ge 1$, $(p+1)-k \le p$, so the count of $0$s is **not
strictly greater** than the count of $1$s; hence $xy^{0}z\notin L$, contradicting condition (3). Therefore
$L$ is not regular. $\blacksquare$ (Choosing $n$ just one above $m$ makes a single deletion break the
strict inequality.)
**35.11 †** *Claim:* $L=\{ww\mid w\in\{0,1\}^{*}\}$ is not regular. *Proof.* Assume regular with pumping
length $p$. Choose $s = 0^{p}1\,0^{p}1$. This is $ww$ with $w=0^{p}1$, so $s\in L$, and $\lvert s\rvert =
2p+2\ge p$. The lemma gives $s=xyz$ with $\lvert y\rvert>0$, $\lvert xy\rvert\le p$. Since $\lvert
xy\rvert\le p$, both $x$ and $y$ lie within the first block of $p$ zeros, so $y=0^{k}$, $k\ge 1$. Pump with
$i=2$: $xy^{2}z = 0^{p+k}1\,0^{p}1$. For this to be of the form $vv$, the two halves must be equal; the
total length is $2p+k+2$, which is even only if $k$ is even, but even when $k$ is even the first half is
$0^{p+k}1\cdots$ while the second half begins with $0$s of a different count — concretely, the string
$0^{p+k}10^{p}1$ has a single $1$ at position $p+k+1$ and another at the end, and no split into two equal
halves reproduces it (the unique candidate split point is after $p+1+\lfloor k/2\rfloor$ symbols, which
puts unequal numbers of leading $0$s in the two halves whenever $k\ge 1$). Hence $xy^{2}z\notin L$,
contradicting condition (3). Therefore $L$ is not regular. $\blacksquare$
*(Cleaner alternative $s$:* take $s=0^{p}10^{p}1$ as above, or use $s = 0^{p}0^{p}=0^{2p}$ — but $0^{2p}$
*is* in $L$ and pumps fine, so it is a* bad *choice; the $1$-markers are what make the halves rigid. The
marker trick is the point.)*
**35.13 †** DFA "ends in `00`," on the §35.2 driver. States: `S` (no useful suffix), `A` (last symbol a
single `0`), `B` (ends in `00`, accept).
```python
ends00 = {('S','0'):'A', ('S','1'):'S',
('A','0'):'B', ('A','1'):'S',
('B','0'):'B', ('B','1'):'S'}
print([run_dfa(ends00, 'S', {'B'}, w) for w in ["00","100","0","010"]])
# Expected output:
# [True, True, False, False]
00: $S\to A\to B$ accept. 100: $S\xrightarrow{1}S\xrightarrow{0}A\xrightarrow{0}B$ accept.
0: $S\to A$ reject. 010: $S\xrightarrow{0}A\xrightarrow{1}S\xrightarrow{0}A$ reject. (See
code/exercise-solutions.py.)complement_dfa flips the accepting set, keeping states and transitions:def complement_dfa(states, accept):
return set(states) - set(accept)
"1": $E\xrightarrow{1}O\in\{O\}$ → True. "11": $E\xrightarrow{1}
O\xrightarrow{1}E\notin{O}$ → False. This is the constructive proof that regular languages are closed
under complement (Exercise 35.8): only $F$ changes, to $Q\setminus F$.n (+5) | on d (+10) |
|---|---|---|
| $\to q_0$ | $q_5$ | $q_{10}$ |
| $q_5$ | $q_{10}$ | $q_{15}$ |
| $q_{10}$ | $q_{15}$ | $X$ (would be 20, overshoot) |
| $q_{15}$ (accept) | $X$ | $X$ |
| $X$ | $X$ | $X$ |
Start $q_0$, $F=\{q_{15}\}$. Traces: nnn: $q_0\xrightarrow{n}q_5\xrightarrow{n}q_{10}\xrightarrow{n}
q_{15}$ — **accept** (5+5+5=15). `dn`: $q_0\xrightarrow{d}q_{10}\xrightarrow{n}q_{15}$ — accept
(10+5=15). dd: $q_0\xrightarrow{d}q_{10}\xrightarrow{d}X$ — reject (10+10=20 overshoots). The
language is regular: finitely many reachable totals, so finitely many states.1."
$k$
$b_k$ (natural NFA states)
$a_k$ (minimal DFA states)
1
2
2
2
3
4
3
4
8
1" by remembering exactly
the last $k$ symbols read (a window of $k$ bits), which is $2^{k}$ states, so $a_k\le 2^{k}$; and these
$2^{k}$ windows are pairwise distinguishable (two windows differing in their lead bit are separated by the
appropriate suffix), so $a_k = 2^{k}$ exactly. Illustration of the §35.3 pitfall: $b_k=k+1$ grows
linearly while $a_k=2^{k}$ grows exponentially — the subset construction's exponential blow-up is real and,
here, unavoidable. NFAs can be exponentially smaller than the minimal equivalent DFA.1 then any
$0$s); its reversal is $\{1,01,001,\dots\}=L(0^{*}1)$, still regular. Take $L=L((01)^{*})$; reversing each
string gives $\{(10)^{*}\}=L((10)^{*})$, still regular. Both reversed sets are describable by a regex, as
the conjecture predicts. Proof (via NFA construction): given a DFA/NFA $M$ for $L$, build $M^{R}$ by
(i) reversing the direction of every transition, (ii) making $M$'s old start state the new accept state,
and (iii) adding a new start state with $\varepsilon$-transitions to all of $M$'s old accept states. A
path $q_0 \rightsquigarrow f$ spelling $w$ in $M$ becomes a path (new start) $\rightsquigarrow q_0$
spelling $w^{R}$ in $M^{R}$, so $L(M^{R})=L^{R}$; since $M^{R}$ is a finite automaton, $L^{R}$ is regular.
$\blacksquare$ (Equivalent proof via Kleene's theorem: define the reversal of a regular expression
recursively — $(RS)^{R}=S^{R}R^{R}$, $(R\mid S)^{R}=R^{R}\mid S^{R}$, $(R^{*})^{R}=(R^{R})^{*}$, and base
cases fixed — and induct on structure.)
Chapter 36 — Computability and the Halting Problem
aaaa, writing each configuration (head's state inserted
at the head position), starting in $q_{\text{even}}$:eval do).code/exercise-solutions.py.)def run_tm(delta, tape, start="q0", accept="ACC", reject="REJ", blank="_", max_steps=10_000):
state, head, tape = start, 0, list(tape)
for _ in range(max_steps):
if state in (accept, reject):
return state
if head < 0:
tape.insert(0, blank); head = 0
if head == len(tape):
tape.append(blank)
state, write, move = delta[(state, tape[head])]
tape[head] = write
head += 1 if move == "R" else -1
return "LOOP?"
div3 = {("q0", "a"): ("q1", "a", "R"), ("q0", "_"): ("ACC", "_", "R"),
("q1", "a"): ("q2", "a", "R"), ("q1", "_"): ("REJ", "_", "R"),
("q2", "a"): ("q0", "a", "R"), ("q2", "_"): ("REJ", "_", "R")}
print(run_tm(div3, "aaa"), run_tm(div3, "aaaa"))
# Expected output:
# ACC REJ
aaa: $q_0\to q_1\to q_2\to q_0$, end-blank in $q_0\Rightarrow$ ACC (3 is divisible by 3).
Trace aaaa: $q_0\to q_1\to q_2\to q_0\to q_1$, end-blank in $q_1\Rightarrow$ REJ. The "LOOP?"
return relates to §36.4 because, for a machine that never halts, no finite step budget can justify
"REJ"/"loops"; "LOOP?" is the simulator honestly refusing to decide the undecidable."unknown" (full code in
code/exercise-solutions.py):def halts_within(step_fn, state, n):
for _ in range(n):
nxt = step_fn(state)
if nxt is None:
return True # observed a halt within the budget
state = nxt
return "unknown" # budget reached -- cannot conclude 'loops'
def countdown_step(state):
return None if state == 0 else state - 1
print(halts_within(countdown_step, 3, 10)) # 3->2->1->0 within 10 -> True
print(halts_within(countdown_step, 8, 5)) # needs 8, budget 5 -> 'unknown'
# Expected output:
# True
# unknown
"unknown" (not "loops") is the only honest answer because reaching the step budget without
seeing a halt does not prove the program never halts — countdown_step from state 8 does halt, at
step 8, just beyond the budget of 5. Claiming "loops" would be the §36.4 Find-the-Error mistake:
always-terminating but incorrect, since for any bound $N$ some program halts at step $N+1$.halts_within from 36.13 (programs are made up; output reasoned by hand):N = 1000
print(halts_within(countdown_step, 5, N)) # halts in 5 -> True
print(halts_within(countdown_step, 2000, N)) # needs 2000 > N -> 'unknown'
print(halts_within(lambda s: s, 0, N)) # never decreases -> 'unknown' (never halts within N)
# Expected output:
# True
# unknown
# unknown
$\sqrt 2$ irrational (Ch. 5)
Halting undecidable (§36.4)
(i) Assumption for contradiction
$\sqrt 2 = a/b$ with $a,b$ integers in lowest terms (no common factor)
a decider $H$ for the halting problem exists
(ii) Contradictory fact
$a$ and $b$ are both even, contradicting "lowest terms"
"$D$ halts on $D$ $\iff$ $D$ does not halt on $D$" — a statement equivalent to its own negation
while" is a syntactic property — it is about the text of
the program, not the function it computes. Two programs computing the identical function can differ on
it (one written with while, an equivalent one with recursion), so it is not a property of the
computed function. Rice's theorem applies only to semantic properties (properties of the
input/output behavior), so it says nothing about this syntactic, and decidable, question (just scan the
source for the token).
(b) "Does $P$ compute the constant-zero function?" is a nontrivial semantic property (some programs
have it, some don't), so by Rice it is undecidable; here is the EMPTY-style reduction from
$\mathrm{HALT}$. Suppose a decider $C$ for it existed. Given a halting instance $\langle P, x\rangle$,
build (emit source for, do not run) the gadget
$$P'(w):\ \text{run } P \text{ on } x;\ \text{if that halts, return } 0.$$
If $P$ halts on $x$, then for every input $w$, $P'$ runs $P$ on $x$, sees it halt, and returns $0$ —
so $P'$ computes the constant-zero function. If $P$ loops on $x$, then $P'$ never returns on any
$w$ — so $P'$ does not compute constant-zero (it computes the totally-undefined function). Thus $P'$
computes constant-zero iff $P$ halts on $x$. Running $C$ on $\langle P'\rangle$ would then decide
$\mathrm{HALT}$ — impossible (§36.4). So the property is undecidable. $\blacksquare$
Chapter 37 — Introduction to Complexity Theory
code/exercise-solutions.py, verify_3coloring: on the triangle with colors $\{a{:}1, b{:}2, c{:}3\}$ it
returns True; with $\{a{:}1, b{:}1, c{:}2\}$ it returns False (edge $a\!-\!b$ monochromatic).count_assignments(v) returns the number of truth
assignments over $v$ variables; see code/exercise-solutions.py. Hand-traced for $v = 0,1,2,3,4$ it gives
[1, 2, 4, 8, 16] ($v=0$ yields the single empty assignment). (b) Proof: each of the $v$ variables is
independently True or False, so by the product rule (Chapter 1 / Chapter 15) the number of assignments is
$\underbrace{2 \cdot 2 \cdots 2}_{v} = 2^v$. $\blacksquare$ (c) The $2^v$ blow-up shows the brute-force
finder is exponential — consistent with SAT $\in$ NP, since being verifiable fast (a single assignment
checks in polynomial time) says nothing about finding one fast. It is not a proof that
$\text{P} \ne \text{NP}$: ruling out one algorithm (brute force) does not rule out all algorithms;
proving no polynomial algorithm exists is exactly the open problem.edge_counts(n, edges); see code/exercise-solutions.py. On the path
$0\!-\!1\!-\!2\!-\!3$ (3 edges, $\binom{4}{2}=6$ pairs) it returns (3, 3). (b) Claim: for every simple
graph on $n$ vertices, $\lvert E(G) \rvert + \lvert E(\overline{G}) \rvert = \binom{n}{2}$. Proof: there
are exactly $\binom{n}{2}$ unordered vertex pairs. Each pair is either an edge of $G$ or a non-edge; the
non-edges are precisely the edges of $\overline{G}$ (by definition of the complement). Partitioning the
$\binom{n}{2}$ pairs into edges and non-edges gives $\lvert E(G) \rvert + \lvert E(\overline{G}) \rvert =
\binom{n}{2}$. $\blacksquare$verify_factor in
code/exercise-solutions.py (verify_factor(91,10,7) → True; verify_factor(91,10,6) → False). (b) A
fast factoring algorithm would not prove $\text{P} = \text{NP}$ because FACTORING is not known to be
NP-complete — it appears to sit in a middle zone, so solving it fast would not (via known reductions)
solve all of NP. This matters for RSA (Chapter 25): even a proof that $\text{P} \ne \text{NP}$ would not
directly guarantee factoring is hard, and conversely fast factoring would break RSA without collapsing NP —
complexity theory makes the cryptographic assumption precise enough to worry about correctly.
Chapter 38 — Coding Theory: Error-Correcting Codes
weight(bits) = sum(bits); min_distance_linear(cws) = min(weight(c) for c in cws if any(c)).
On [[0,0,0],[1,1,1]] the only nonzero codeword is [1,1,1] of weight 3, so the result is 3. (See
code/exercise-solutions.py.) Valid because the repetition code is linear (Theorem 38.4).[1,0,1,1]: $p_1 = 1\oplus0\oplus1 = 0$, $p_2 = 1\oplus1\oplus1 = 1$, $p_4 =
0\oplus1\oplus1 = 0$, giving codeword `[0,1,1,0,0,1,1]`. Its syndrome $H c^{\mathsf T}$: row $s_4$
(4,5,6,7): $0\oplus0\oplus1\oplus1=0$; row $s_2$ (2,3,6,7): $1\oplus1\oplus1\oplus1=0$; row $s_1$
(1,3,5,7): $0\oplus1\oplus0\oplus1=0$. Syndrome [0,0,0] — confirms a valid codeword. Expected output:
([0, 1, 1, 0, 0, 1, 1], [0, 0, 0]).min_distance_linear): $n=3 \to 3$, $n=5 \to 5$,
$n=7 \to 7$. Proof: the code is linear with exactly one nonzero codeword $1^n$ of weight $n$; by Theorem
38.4, $d_{\min} = n$. By Theorem 38.3 it corrects $\lfloor (n-1)/2 \rfloor$ errors. For $n = 2m+1$ that is
$\lfloor 2m/2 \rfloor = m$ errors. (Majority vote: with $2m+1$ copies, $m$ flips still leave the majority
correct.)
Chapter 39 — Capstone: Applying Discrete Mathematics
crypto.py, resting on numbertheory.py (mod_pow, mod_inverse).
- B (network analyzer): BFS computes shortest-path distances (Ch. 28); core module graphs.py (Graph, bfs).
- C (Sudoku): backtracking search is sound and complete; no single Toolkit function — the engine is DFS over partial assignments with an is_legal check.
- D (error-correcting code): $d \ge 3 \Rightarrow$ corrects 1 error, via the triangle inequality (Ch. 38); core module coding.py (hamming_encode/decode)."Hi" does not fit because read as one integer it is $72 \times 256 + 105 = 18537$, and $18537 > 3233$; arithmetic mod 3233 cannot recover a value $\ge 3233$ (it would return $18537 \bmod 3233 = 2372$). The fix is blocking — one byte per block here.solve(grid) returns True, the final grid violates no row/column/box constraint. Proof by induction on the number of cells filled by the call.
Base case: the grid is already full (no empty cell); solve returns True immediately, and the grid was completed only through is_legal-checked placements, so it satisfies every constraint.
Inductive step: suppose every successful solve on a grid with one more empty cell yields a valid completion. Given a grid with an empty cell $(r,c)$, solve writes a digit $v$ only after is_legal(grid, r, c, v) confirms $v$ appears in no cell of $(r,c)$'s row, column, or box at that moment. It then recurses; if the recursion returns True, by the hypothesis the rest of the grid is a valid completion. The placement of $v$ never weakens a constraint (adding a digit can only make a future is_legal stricter, never looser), so $v$ remains legal in the completed grid. Hence the whole grid is valid. $\blacksquare$ (Completeness is the other half — not asked here.)code/exercise-solutions.py). For a toy modulus with $256 \le n \le 65536$, one byte per block is always safe, since each byte is $< 256 \le n$:def to_blocks(data, n): return [b for b in data] # each 0 <= b < 256 <= n
def from_blocks(blocks): return bytes(blocks)
"Hi" = bytes 72, 105 and small n, to_blocks yields [72, 105] — two single-byte blocks, each below n, instead of the over-large $18537 = 72\cdot256 + 105$. from_blocks([72,105]).decode() returns "Hi". (A production blocker would chunk by $\lfloor \log_{256} n \rfloor$ bytes per block, not one.)code/exercise-solutions.py): modify solve to sum the solution counts over all legal digits at each empty cell rather than returning at the first success, capping at 2 so the search stops as soon as non-uniqueness is proven:def count_solutions(grid, cap=2):
for r in range(9):
for c in range(9):
if grid[r][c] == 0:
total = 0
for v in range(1, 10):
if is_legal(grid, r, c, v):
grid[r][c] = v
total += count_solutions(grid, cap)
grid[r][c] = 0
if total >= cap: return total
return total
return 1 # complete grid: one solution found here
return 1 fires only at a complete grid; every empty cell sums its children's counts; the cap lets a puzzle with $\ge 2$ completions stop early returning 2. (A full $9\times9$ trace is impractical; the load-bearing invariant is "1 only at a complete grid, sums otherwise.")rsa_decrypt returns $18537 \bmod 3233 = 2372$, not $18537$. Reason: encryption computed $c = 18537^{17} \bmod 3233$, and decryption computes $c^{2753} \bmod 3233$, which the RSA correctness theorem guarantees equals $18537 \bmod 3233 = 2372$ — the theorem recovers the message reduced mod $n$, and $18537 \ge n = 3233$. The violated constraint is $m < n$ (§39.3): with the toy modulus, a two-byte message overflows a single block. It fails silently — no exception, just a wrong (and meaningless) plaintext — which is exactly why the $mis_legal = "no neighbor already has this color." The four time slots correspond to the four available colors.# (not executed) tabulate diameter of the path P_V for V = 2..8
for V in range(2, 9):
g = Graph()
for i in range(V - 1):
g.add_edge(i, i + 1) # a line 0-1-2-...-(V-1)
print(V, diameter(g))
# Expected output:
# 2 1
# 3 2
# 4 3
# 5 4
# 6 5
# 7 6
# 8 7
# (not executed) flip each of the 7 positions of the codeword of [1,0,1,1], decode, check
code = hamming_encode([1, 0, 1, 1]) # [0,1,1,0,0,1,1]
for pos in range(7):
rx = code[:]; rx[pos] ^= 1
print(pos, hamming_decode(rx) == [1, 0, 1, 1])
# Expected output:
# 0 True
# 1 True
# 2 True
# 3 True
# 4 True
# 5 True
# 6 True
[1,0,1,1] every time — all seven checks print True. So the conjecture as stated is false: the decoder does not "notice the flip and report it" — it silently and correctly repairs the data, so the recovered 4 bits are unchanged. The correct statement is single-error correction: with $d \ge 3$, any one flipped bit decodes back to the original codeword (and hence original data), invisibly. ("Noticing" without correcting is detection, a weaker guarantee.)"Dev" on the six-person graph (edges: Ana–Ben, Ana–Cam, Ben–Cam, Cam–Dev, Dev–Eve, Dev–Fay, Eve–Fay):
- Layer 0: Dev (0). Layer 1: Cam, Eve, Fay (1). Layer 2: Ana, Ben (2, via Cam).
- Full dictionary: {Dev:0, Cam:1, Eve:1, Fay:1, Ana:2, Ben:2}.
The eccentricity of Dev (its farthest distance) is 2. The graph's diameter is 3 (e.g. Ana→Eve crosses the Cam–Dev bridge). So Dev's eccentricity (2) is less than the diameter (3): Dev sits near the center (on the bridge), so it is closer to everyone than the cluster-end pairs are to each other.solve performs a complete depth-first traversal that tries every legal digit at every cell before reporting failure; so if it returns False, it has effectively examined every candidate completion and found each violates a constraint. Exhausting a finite space of candidates and finding all fail is a proof that none satisfies — the same logic as a finite disproof by cases.components running time. The seen set records every vertex absorbed by a completed BFS; the loop guard if v not in seen then skips any vertex already in a discovered component, so bfs is launched once per connected component, not once per vertex. Summed over all components, BFS touches each vertex and each edge a constant number of times, giving total work $O(V + E)$ (Chapter 30 Big-O). A naive "BFS from every vertex" would run $V$ separate $O(V+E)$ searches: $O(V(V+E))$. For a sparse social graph with $E = O(V)$, the smart version is $O(V + V) = O(V)$ and the naive version is $O(V\cdot V) = O(V^2)$ — linear vs. quadratic.text_to_int/int_to_text glue around the crypto.py core (rsa_encrypt = $m^e \bmod n$, rsa_decrypt = $c^d \bmod n$), plus a blocking layer so each block is $< n$. New code is ~15 lines; the heavy lifting (mod_pow, mod_inverse) is reused."
3. Correctness. "Decryption inverts encryption because $m^{ed}\equiv m\pmod n$, by Euler's theorem (Chapter 25), with CRT covering $\gcd(m,n)\ne 1$."
4. Evidence. "Demonstrated on the worked key $n=3233$: "A"$\to 65 \to 2790 \to 65 \to$"A". Tested the round-trip on all single-byte messages."
5. Complexity & limits. "Encryption is one $O(\log e)$ modular exponentiation per block. Limits: this is a toy key — secure against no real attacker — and I did not test resistance to chosen-ciphertext or timing attacks; the correctness theorem guarantees recovery, not secrecy." The graded essentials: a model stated before code, a correctness claim that cites Chapter 25 (does not re-derive Euler), and a limits section naming something not verified.
Chapter 40 — Where Discrete Math Goes Next
f and then mapping g gives the same list as mapping the single function
"do f, then g."code/exercise-solutions.py. binary_entropy(p) returns 0.0 at $p\in\{0,1\}$, else
$-p\log_2 p - (1-p)\log_2(1-p)$. Values: $H(0) = 0$; $H(1/2) = -\tfrac12(-1) - \tfrac12(-1) = 1.0$;
$H(1/4) = -\tfrac14(-2) - \tfrac34\log_2\tfrac34 = 0.5 + 0.75(0.41504) = 0.811$. Expected output:
0.0 1.0 0.811.code/exercise-solutions.py. capacity_bsc(p) = 1 - binary_entropy(p). $C(0) = 1 - 0 =
1.0$; $C(0.11) = 1 - 0.5 = 0.5$ (since $H(0.11)\approx 0.5$); $C(0.5) = 1 - 1 = 0.0$. Expected output:
1.0 0.5 0.0. The $p=0.5$ value of $0$ means the channel transmits no information — output is
independent of input.code/exercise-solutions.py. At $(u,v) = (7/3, 1/3)$: dual_objective $= 14(7/3) +
10(1/3) = 98/3 + 10/3 = 108/3 = 36.0$. `dual_feasible`: $u + 2v = 7/3 + 2/3 = 3 \ge 3$ ✓,
$2u + v = 14/3 + 1/3 = 5 \ge 5$ ✓, $u, v > 0$ ✓ → True. Expected output: 36.0 True.code/exercise-solutions.py for one valid answer. A $K_4$ coloring that avoids a
monochromatic triangle (possible since $R(3,3) = 6 > 4$): red 4-cycle $0\!-\!1\!-\!2\!-\!3\!-\!0$, blue
diagonals $02, 13$. Checking all $\binom43 = 4$ triangles — $\{0,1,2\}$: R,R,B; $\{0,1,3\}$: R,B,R;
$\{0,2,3\}$: B,R,R; $\{1,2,3\}$: R,R,B — none is monochromatic, so has_mono_triangle(4, k4) returns
False. (A different hand-coloring may give True; either is acceptable as long as the trace is
correct.)list/map is a functor, so mapping g after mapping f
(map(g, map(f, xs))) equals mapping the composite once (map(lambda x: g(f(x)), xs)) for every
f, g, and list — the equality is an instance of a law that holds for all functors, not a coincidence
of one program. Hence map fusion is sound in general.