Exercises — Module 08 (Combinatorial Generation, §7.2.1)
Self-contained problems on this module's material — the reflected Gray code and its rank, lexicographic permutations (Algorithm L) including multisets, plain changes (Algorithm P), combinations in colex order (Algorithm T) and the combinatorial number system, and integer partitions with Euler's pentagonal recurrence. You can work every one without the books: each states the problem in full, gives a hint to peek at when stuck, and a worked answer sketch to check against after you try. Computational answers here are reproduced by the code you write in the lab (or a few lines at a REPL).
Ratings follow Knuth's scale (00–50; M = needs mathematics, ▶ = especially
instructive). Where a problem mirrors a TAOCP exercise, its number is noted for
readers who own Volume 4A.
Tracking
| # | Topic | Rating | Status |
|---|---|---|---|
| 1 | Reflected Gray code is cyclic (last↔first differ in one bit) | 10 | ⬜ |
| 2 | ▶ Prove the prefix-XOR inverts the Gray map (gray_rank) | 22 | ⬜ |
| 3 | Hand-trace Algorithm L on the multiset | 15 | ⬜ |
| 4 | ▶ Algorithm L visits multiset arrangements in lex order, each once | 26 | ⬜ |
| 5 | Plain changes ends at (Hamiltonian cycle) | 20 | ⬜ |
| 6 | Hand-trace Algorithm T for ; confirm colex order | 15 | ⬜ |
| 7 | ▶ Combinatorial number system: colex rank | 24 | ⬜ |
| 8 | List the 11 partitions of 6 in reverse-lex | 10 | ⬜ |
| 9 | ▶ Derive the pentagonal recurrence for ; verify | M30 | ⬜ |
Problems
1. Reflected Gray code is cyclic (rating 10 · cf. 7.2.1.1–1)
Problem. The reflected binary Gray code lists all binary
-tuples with the closed form for
(in Rust, k ^ (k >> 1)). Consecutive words differ
in exactly one bit. Show the code is also cyclic: the last word
and the first word differ in exactly one bit too.
Hint. Compute directly. For the last word, has all low bits set, so has bits through set. XOR them.
Answer sketch. , the all-zeros word. For (bits all ), (bits all ). Then
and the low bits cancel, leaving only bit : the word . It differs from in exactly one bit (the top bit). Hence appending the first word after the last keeps the one-bit-change property all the way around — is a Hamiltonian cycle on the -cube. (For this is ; the closing flip is bit , matching the README's seam observation.)
2. ▶ Prove the prefix-XOR inverts the Gray map (rating 22 · cf. 7.2.1.1–7)
Problem. The Gray map is . Given a
codeword , the lab's gray_rank recovers by the shift-and-XOR
(shifting until zero). Prove this formula really is .
Hint. Work bit by bit from the top. Bit of equals bit of (nothing is shifted into the top). For a lower bit , expand and solve for using the already-recovered .
Answer sketch. Write bits with subscripts. From , bit of the word is (with ). Solving downward from the top: , and for each lower . Unrolling this recurrence,
That is precisely bit of : the term contributes to bit , so summing over all shifts gives . Since this holds for every bit, the prefix-XOR equals , so it inverts . The shifting terminates once , i.e. after at most terms. (Round-trips both directions are exactly what Stage 1 checks.)
3. Hand-trace Algorithm L on the multiset (rating 15 · cf. 7.2.1.2–1)
Problem. Algorithm L generates the next arrangement in lexicographic order:
L2 finds the pivot (walk left from the right while ); L3
swaps with the rightmost ; L4 reverses the suffix after .
Starting from the sorted multiset 1 2 2 3, list every distinct arrangement it
produces, in order. How many are there, and why?
Hint. The comparisons (not ) make equal elements behave as an already-maximal run, so each distinct arrangement appears once. The count is the multinomial because the two s are indistinguishable.
Answer sketch. The distinct arrangements, in increasing lexicographic order, are the strings
Algorithm L produces exactly this sequence: e.g. from 1223, L2 stops at
(value , since is ascending), L3 swaps the
and giving 1232, and L4 reverses the length-1 tail (no change). It
terminates at 3221, the non-increasing (greatest) arrangement, when L2 walks
all the way to . Twelve arrangements, each once.
4. ▶ Algorithm L visits multiset arrangements in lex order, each once (rating 26 · cf. 7.2.1.2–11)
Problem. Prove: started from the sorted multiset and iterated until L2 terminates, Algorithm L visits every distinct arrangement of the multiset exactly once, in increasing lexicographic order — where L2 and L3 use (not ) comparisons.
Hint. Show two things: (a) each step produces a string strictly greater than the current one, and (b) it produces the least string strictly greater — so nothing between is skipped. Together these give a strictly increasing walk with no gaps; termination at the greatest arrangement finishes it.
Answer sketch. Let the current arrangement be . L2 finds the longest non-increasing suffix and sets just before it. Because that suffix is non-increasing, no rearrangement of it alone can increase the string; to get a larger string we must raise position .
(a) Strictly greater. L3 swaps with the rightmost suffix element . Since the suffix is non-increasing, is the smallest element of the suffix that exceeds , so the new is larger than the old — the string strictly increases at position , hence overall.
(b) Least such. After the swap the suffix is still non-increasing (we removed one value and inserted a smaller-or-equal one in sorted position), so L4 reversing it makes it increasing — the lexicographically smallest possible tail for the raised prefix. Raising by the minimum amount and then minimizing the tail yields exactly the least arrangement strictly greater than .
Strictly increasing no arrangement repeats. "Least greater" no arrangement is skipped. The use of (not ) is what makes swaps of equal elements never count as an increase, so two orderings differing only by exchanging equal elements are never both produced. L2 terminates () exactly at the non-increasing arrangement — the lexicographically greatest — so all distinct arrangements have been seen. For a multiset with multiplicities the total is the multinomial ; e.g. gives (Problem 3).
5. Plain changes ends at (rating 20 · cf. 7.2.1.2)
Problem. Plain changes (Steinhaus–Johnson–Trotter, the module's Algorithm P) lists all permutations so that consecutive ones differ by a single adjacent transposition. Starting from , show it terminates at , and that this last permutation is itself one adjacent transposition away from the start — so the whole listing is a Hamiltonian cycle on the permutations.
Hint. Use the recursive "weave" picture: element slides back and forth across the block of the smaller elements' permutations, one step per visit. The terminal permutation of is determined by the terminal of with parked at one end.
Answer sketch. Induct on . Base : the list is ; the terminal is , and it is one adjacent swap from . Step: assume the plain-changes listing of starts at and ends at . In the weave, starts at the far right and sweeps left/right through each successive permutation of the smaller block; across the whole run it makes an even number of full sweeps back so that after the last smaller-block permutation it has returned to the far-right position. Hence the final permutation is . This matches the README's stated terminal and is what Stage 3 checks. Finally differs from the start in exactly the adjacent pair — a single adjacent transposition — so closing the list makes it a Hamiltonian cycle. (Direct check : the sequence is , ending at , one swap from .)
6. Hand-trace Algorithm T for (rating 15 · cf. 7.2.1.3–1)
Problem. Algorithm T lists the combinations (as sets of ) in colexicographic order: compare the reversed strings , i.e. group primarily by the largest element. List all combinations for in the order T produces, and confirm the largest element is non-decreasing down the list.
Hint. Colex sorts first by (the max), then by , then . Start with and enumerate all sets with max , then max , then max .
Answer sketch. In colex order the ten combinations are
The maxima run — non-decreasing, as colex requires; within each max-group the sets are in colex order of the remaining elements. matches the list length. (This is exactly the Stage 4 colex output, and note the first four entries — those using only — are a prefix equal to the full list, the colex "prefix stability" property.)
7. ▶ Combinatorial number system (rating 24 · cf. 7.2.1.3–4)
Problem. Prove that in colex order the rank (0-based position) of the combination of is
Verify it on two entries of the list from Problem 6.
Hint. Count how many -combinations come before a given one in colex order. A combination precedes iff at the highest index where they differ, its element is smaller. Condition on the largest element: how many -subsets have their maximum ?
Answer sketch. The rank is the number of -combinations that are colex-less than . Split them by their largest element. Those with maximum strictly below can use any elements of , and there are of these — all colex-smaller (smaller top element dominates the colex comparison). The remaining smaller combinations share the same top element and are colex-less among their lower elements; by the same argument applied recursively to (a -combination of ) they number . Adding the two pieces and unrolling the recursion gives . This is the combinatorial number system: every integer has a unique such representation with .
Verification. For : has rank (first). has rank , and indeed is the 9th entry (index ) of the Problem 6 list. Every entry's formula-rank equals its position .
8. List the 11 partitions of 6 in reverse-lex (rating 10 · cf. 7.2.1.4–1)
Problem. A partition of writes as a sum of positive parts in non-increasing order; . The module's Algorithm P generates partitions from down to in reverse-lexicographic order (each partition lexicographically less than the one before). List all 11 partitions of 6 in that order.
Hint. Start with the single part 6, then repeatedly decrease the rightmost
part greater than and redistribute. Reverse-lex means 6 first,
1+1+1+1+1+1 last.
Answer sketch. In the order Algorithm P produces (reverse-lex):
That is partitions, each lexicographically less than its
predecessor. (This matches partitions(6) in Stage 5.)
9. ▶ Derive the pentagonal recurrence and verify (rating M30 · cf. 7.2.1.4)
Problem. From Euler's generating function and the pentagonal number theorem , derive the recurrence
with and for . Then use it to verify .
Hint. The two products are reciprocals, so their product is . Multiply and match the coefficient of for (which must be ). Split the sum into (exponent ) and (exponent , with ).
Answer sketch. Because and are reciprocal,
The coefficient of on the left is (matching the RHS), and for every the coefficient of must vanish:
Peel off the term (, sign ), giving , and move the rest to the other side. Writing the positive index as (exponent ) and the negative index as (exponent ), and noting so :
The generalized pentagonal exponents are , and only of them are , so each costs .
Verification. Tabulating with : , , , , , (Problem 8), and iterating the recurrence up to gives — the headline count Stage 5 checks (the same routine yields ).
Your solutions
Use this space to log your own work — a restated problem, your approach, and how it compared with the sketch above.
Exercise N (rating RR)
Approach. Answer / proof. Compared with the sketch: what you did differently, what you missed.