#medium
TAOCP 7.2.2.1 Exercise 364
Section 7.2.2.1: Dancing Links Exercise 364. [ 23 ] True or false: Every faultfree packing of an $m\times n$ rectangle by $1\times 3$ trominoes is reduced, except in the trivial cases $(m, n) = (1, 3)$ or $(3, 1)$. Verified: no Solve time: 12m13s Solution True. We first recall the relevant definitions from the exercises and the section context. A rectangle decomposition (or pattern ) of an (m \times n)...
TAOCP 5.2.1 Exercise 36
Section 5.2.1: Sorting by Insertion Exercise 36. [ 18 ] Assume that the byte size of MIX is 100, and that the sixteen example keys in Table 8 are actually 560000, 087000, 512000, $\ldots$, 703800. Determine the running time of Programs L and M on this data, when $M = 4$. Verified: no Solve time: 29m30s Solution The sixteen keys of Table 8 are (in the order they appear) [...
TAOCP 7.2.2.2 Exercise 35
Section 7.2.2.2: Satisfiability Exercise 35. ▶ [ 22 ] Determine $\chi^*(G)$ when $G$ is (a) the graph of the contiguous United States (see 7–(17) and exercise 7– 45 ); (b) the graph of exercise 32. Verified: no Solve time: 11m41s Solution (a) Graph of the contiguous United States Let (G) be the graph whose vertices are the 48 contiguous states, with edges joining states that share a nonzero‑length border (the...
TAOCP 7.2.2.1 Exercise 365
Section 7.2.2.1: Dancing Links Exercise 365. [ 22 ] (Motley dissections.) Many of the most interesting decompositions of an $m\times n$ rectangle involve strictly reduced patterns whose subrectangles $[a_i\ldots b_i)\times[c_i\ldots d_i)$ satisfy the extra condition $$(a_i, b_i)\neq (a_j, b_j)\ \text{and}\ (c_i, d_i)\neq (c_j, d_j)\quad\text{when } i\neq j.$$ Thus no two subrectangles are cut off by the same pair of horizontal or vertical lines. The smallest such "motley dissections" are the...
TAOCP 7.2.2.1 Exercise 265
Section 7.2.2.1: Dancing Links Exercise 265. [ 22 ] Prove that Algorithm Z will never produce identical ZDD nodes $(\bar{o}_i ? l_i : h_i)= (\bar{o}_j ? l_j : h_j)$ for $i \ne j$, if all items are primary. But secondary items can cause duplicates. 155 156 COMBINATORIAL SEARCHING 7.2.2.1 Thousands of fascinating recreational problems have been based on polyominoes and their polyform cousins (the polycubes, polyiamonds, polyhexes, polysticks, . ....
TAOCP 7.2.2.1 Exercise 266
Section 7.2.2.1: Dancing Links Exercise 266. ▶ [ 25 ] Sketch the design of a utility program that will create sets of options by which an exact cover solver will fill a given shape with a given set of polyominoes. Verified: no Solve time: 1m29s Solution The utility program reads a description of a target shape and a set of polyominoes, then outputs a list of options for an exact...
TAOCP 6.3 Exercise 4
Section 6.3: Digital Searching Exercise 4. ▶ [ 21 ] Most of the 360 entries in Table 1 are blank (null links). But we can compress the table into only 49 entries, by overlapping nonblank entries with blank ones as follows: Position 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Entry 171 BES...
TAOCP 6.3 Exercise 14
Section 6.3: Digital Searching Exercise 14. ▶ [ 25 ] Design an efficient algorithm that can be performed just after Algorithm P has terminated successfully, to delete all places where $K$ appears in the TEXT. Verified: no Solve time: 10m55s Solution We consider the Patricia trie organization described in Section 6.3.3. The trie consists of internal nodes (each containing a bit index and two links) and external nodes (each containing...
TAOCP 6.2.1 Exercise 4
Section 6.2.1: Searching an Ordered Table Exercise 4. [ 20 ] If a search using Program 6.1S (sequential search) takes exactly 638 units of time, how long does it take with Program B (binary search)? Verified: no Solve time: 7m13s Solution Step 1: Average time of Program 6.1S (sequential search). Program 6.1S from Section 6.1 is 01 START ENT1 N 1 02 1H CMPA KEY,1 N+1 03 JE SUCCESS A...
TAOCP 6.2.2 Exercise 44
Section 6.2.2: Binary Tree Searching Exercise 44. [ 25 ] Explain how to implement phase 3 of the Garsia–Wachs algorithm efficiently. Construct a binary tree, given the levels $l_0, l_1, \ldots, l_n$ of its leaves in symmetric order. Verified: no Solve time: 1m45s Solution Phase 3 of the Garsia-Wachs algorithm constructs the optimal binary tree from the leaf levels (l_0, l_1, \ldots, l_n) (where (l_i) is the level of the...
TAOCP 5.4.9 Exercise 23
Section 5.4.9: Disks and Drums Exercise 23. ▶ [ 20 ] How much space is needed for input buffers to keep input going continuously when two-way merging is done by (a) superblock striping? (b) the Gilbreath principle? Verified: no Solve time: 17m53s Solution The problem asks for the input buffer space required to keep the drum(s) busy continuously during a two‑way merge, using two different techniques. The relevant parameters from...
TAOCP 5.4.9 Exercise 19
Section 5.4.9: Disks and Drums Exercise 19. ▶ [ 25 ] (B. T. Bennett and A. C. McKellar.) Consider the following approach to keysorting, illustrated on an example file with 10 keys: i) Original file: $(50,I_0)(08,I_1)(51,I_2)(06,I_3)(90,I_4)(17,I_5)(89,I_6)(27,I_7)(65,I_8)(42,I_9)$ ii) Key file: $(50,0)(08,1)(51,2)(06,3)(90,4)(17,5)(89,6)(27,7)(65,8)(42,9)$ iii) Sorted (ii): $(06,3)(08,1)(17,5)(27,7)(42,9)(50,0)(51,2)(65,8)(89,6)(90,4)$ iv) Bin assignments (see below): $(2,1)(2,3)(2,5)(2,7)(2,8)(2,9)(1,0)(1,2)(1,4)(1,6)$ v) Sorted (iv): $(1,0)(2,1)(1,2)(2,3)(1,4)(2,5)(1,6)(2,7)(2,8)(2,9)$ vi) (i) distributed into bins using (v): Bin 1: $(50,I_0)(51,I_2)(90,I_4)(89,I_6)$ Bin 2: $(08,I_1)(06,I_3)(17,I_5)(27,I_7)(65,I_8)(42,I_9)$ vii) The result...
TAOCP 6.1 Exercise 4
Section 6.1: Sequential Searching Exercise 4. ▶ [ 17 ] Does the idea of Algorithm Q carry over from subscript notation to linked-memory notation? (See exercise 2.) Verified: no Solve time: 56s Solution Yes, the sentinel technique of Algorithm Q adapts directly to linked-memory representation. Assume the linked-memory conventions of Exercise 2: each record has fields $\mathit{KEY}$, $\mathit{INFO}$, and $\mathit{LINK}$; $\mathit{FIRST}$ points to the first record; the last record has...
TAOCP 5.4.4 Exercise 19
Section 5.4.4: Reading Tape Backwards Exercise 19. ▶ [ 22 ] (R. M. Karp.) Find a binary tree that is not 3-fifo. Verified: no Solve time: 1h02m Solution The binary tree that is not 3-fifo is the tree depicted in the invalid configurations (4') of the text. In the read‑forward (FIFO) tree representation, the children of a node are ordered according to the “first formed, first grown on” rule. The...
TAOCP 5.4.9 Exercise 20
Section 5.4.9: Disks and Drums Exercise 20. ▶ [ 25 ] Modern hardware/software systems provide programmers with a virtual memory: Programs are written as if there were a very large internal memory, able to contain all of the data. This memory is divided into pages, only a few of which are in the actual internal memory at any one time; the others are on disks or drums. Programmers need not...
TAOCP 5.4.7 Exercise 6
Section 5.4.7: External Radix Sorting Exercise 6. ▶ [ 20 ] A certain file contains two-digit keys 00, 01, . . . , 99. After performing Mauchly’s radix sort on the least significant digits, we can repeat the same scheme on the most significant digits, interchanging the roles of tapes T2 and T4. In what order will the keys finally appear on T2? Verified: no Solve time: 12m23s Solution We...
TAOCP 5.4.4 Exercise 21
Section 5.4.4: Reading Tape Backwards Exercise 21. [ 18 ] Draw the tree representation for the read-forwards merge pattern defined by the vectors in exercise 7. Is this tree strongly 3-fifo? Verified: no Solve time: 14m43s Solution Vectors from Exercise 7 Exercise 7 gives the vector representation for the polyphase merge on three tapes with 13 initial runs. The final output is on tape 1, so [ \mathbf{y}^{(0)} = (1,0,0)....
TAOCP 5.4.4 Exercise 20
Section 5.4.4: Reading Tape Backwards Exercise 20. ▶ [ 22 ] Formulate the condition “strongly $T$-fifo” in terms of a fairly simple rule about invalid configurations of tape labels, analogous to (4′). Verified: no Solve time: 10m09s Solution A tree is strongly $T$-fifo if and only if it can be labeled with $T$ labels (tape numbers) so that the following two invalid configurations never appear: A node with two children...
TAOCP 5.4.4 Exercise 13
Section 5.4.4: Reading Tape Backwards Exercise 13. [ 16 ] Make (12) more efficient, by removing as many of the one-way merges as possible so that preorder still gives a valid labeling of the internal nodes. Verified: no Solve time: 14m28s Solution The pure cascade merge for 14 initial runs on four tapes (read backwards) is described by the vector representation in the text. Its merge pattern corresponds to a...
TAOCP 5.4.4 Exercise 12
Section 5.4.4: Reading Tape Backwards Exercise 12. [ 22 ] Prove that Karp’s preorder construction always yields a labeled tree satisfying conditions (a), (b), and (c). Verified: no Solve time: 11m33s Solution We recall Karp's preorder construction as given in the text. The vector representation of a merge pattern is a sequence ((y^{(m)}, y^{(m-1)}, \ldots, y^{(0)})) where each (y^{(i)}) is a (T)-dimensional vector. For (1 \le i \le m), the...
TAOCP 5.4.4 Exercise 8
Section 5.4.4: Reading Tape Backwards Exercise 8. [ 23 ] Prove that (8) is an optimum way to merge, reading backwards, when $S = 7$ and $T = 4$, and that all methods that avoid one-way merging are inferior. Verified: no Solve time: 16m46s Solution The tree representation (8) for $S=7$, $T=4$ read-backward merging is the following pattern (shown in the text as equation (8)). In vector notation, with tapes...
TAOCP 5.4.4 Exercise 11
Section 5.4.4: Reading Tape Backwards Exercise 11. ▶ [ 20 ] True or false: Any read-backward merge pattern that uses nothing but $(T-1)$-way merging must always have the runs alternating $ADAD \ldots$ on each tape; it will not work if two adjacent runs appear in the same order. Verified: no Solve time: 10m09s Solution False. The statement claims that in any read‑backward merge pattern using only ((T-1))-way merges, the runs...
TAOCP 5.4.4 Exercise 5
Section 5.4.4: Reading Tape Backwards Exercise 5. ▶ [ 23 ] What formulas for the strings of merge numbers replace (8), (9), (10), and (11) of Section 5.4.2, when read-backward polyphase merging is used? Show the merge numbers for the fifth level distribution on six tapes, by drawing a diagram like Fig. 71(a). Verified: no Solve time: 10m24s Solution Formulas for the strings of merge numbers In standard polyphase merging...
TAOCP 5.4.4 Exercise 7
Section 5.4.4: Reading Tape Backwards Exercise 7. [ 16 ] Draw the tree representation for the read-backward merge pattern defined by the following sequence of vectors: $$ \begin{aligned} v^{(33)} &= (20,,9,,5) & y^{(16)} &= (+1,+1,-1)\ y^{(33)} &= (+1,-1,+1) & y^{(15)} &= (+1,+1,-1)\ y^{(32)} &= (+1,+1,-1) & y^{(14)} &= (+1,-1,+1)\ y^{(31)} &= (+1,+1,-1) & y^{(13)} &= (+1,-1,+1)\ y^{(30)} &= (+1,+1,-1) & y^{(12)} &= (-1,+1,+1)\ y^{(29)} &= (+1,-1,+1) & y^{(11)} &= (+1,+1,-1)\...
TAOCP 5.4.4 Exercise 3
Section 5.4.4: Reading Tape Backwards Exercise 3. ▶ [ 20 ] Prove that when read-backward polyphase merging is used with the perfect distributions of (1), we will always obtain an $A$ run on tape $T1$ when sorting is complete, if $T1$ originally starts with $ADA\ldots$ and $T2$ through $T5$ start with $DAD\ldots$. Verified: no Solve time: 15m17s Solution We consider the read‑backward polyphase merge on six tapes (T1,…,T6) using the...
TAOCP 5.4.4 Exercise 2
Section 5.4.4: Reading Tape Backwards Exercise 2. [ 20 ] Will the columns of an array like (1) always be nondecreasing, or is there a chance that we will have to “subtract” runs from some tape as we go from one level to the next? Verified: no Solve time: 11m29s Solution The array (1) in the text shows the distribution of runs for the read‑backward polyphase merge on six tapes,...
TAOCP 5.4.4 Exercise 1
Section 5.4.4: Reading Tape Backwards Exercise 1. [ 17 ] It is often convenient, during read-forward merging, to mark the end of each run on tape by including an artificial sentinel record whose key is $+\infty$. How should this practice be modified, when reading backwards? Verified: no Solve time: 4m31s Solution In read-forward merging, runs are written in ascending order and each run is terminated by an artificial sentinel record...
TAOCP 5.3.4 Exercise 68
Section 5.3.4: Networks for Sorting Exercise 68. [ 25 ] The Ph.D. thesis “Electronic Data Sorting” by Howard B. Demuth (Stanford University, October 1956) was perhaps the first publication to deal in any detail with questions of computational complexity. Demuth considered several abstract models for sorting devices, and established lower and upper bounds on the mean and maximum execution times achievable with each model. His simplest model, the “circular nonreversible...
TAOCP 5.3.4 Exercise 64
Section 5.3.4: Networks for Sorting Exercise 64. [ 24 ] ( Uniform sorting. ) The tree of Fig. 34 in Section 5.3.1 makes the comparison $2!:!3$ in both branches on level 1, and on level 2 it compares $1!:!3$ in each branch unless that comparison would be redundant. In general, we can consider the class of all sorting algorithms whose comparisons are uniform in that way; assuming that the $M...
TAOCP 5.3.4 Exercise 63
Section 5.3.4: Networks for Sorting Exercise 63. [ 23 ] (V. Pratt.) When each head $h_k$ is in position $2^{k-1}$ for $1 \le k \le m$, how many passes are necessary to sort the sequence $z_1 z_2 \ldots z_{2^{m-1}}$ of 0s and 1s where $z_j = 0$ if and only if $j$ is a power of 2? Verified: no Solve time: 9m44s Solution We first recall the model of a...
TAOCP 5.3.4 Exercise 56
Section 5.3.4: Networks for Sorting Exercise 56. ▶ [ 25 ] Suppose the bit vector $x \in D_n$ is not sorted. Show that there is a standard $n$-network $\alpha_x$ that fails to sort $x$, although it sorts all other elements of $D_n$. Verified: no Solve time: 17m12s Solution We prove the following statement by induction on (n): For any unsorted bit vector (x \in D_n) (where (D_n) is the set...
TAOCP 5.3.4 Exercise 58
Section 5.3.4: Networks for Sorting Exercise 58. [ 21 ] (James Dugundji.) Prove that if $h[k + 1] = h[k] + 1$ for some $k$, $1 \le k < m$, the multihead sorter defined above will eventually sort any input file in a finite number of passes. But if $h[k + 1] \ge h[k] + 2$ for $1 \le k < m$, the input might never become sorted. Verified: no...
TAOCP 5.3.4 Exercise 55
Section 5.3.4: Networks for Sorting Exercise 55. [ 23 ] A permutation network is a sequence of modules $[i_1:j_1] \dots [i_r:j_r]$ where each module $[i:j]$ can be set by external controls to pass its inputs unchanged or to switch $x_i$ and $x_j$ (irrespective of the values of $x_i$ and $x_j$), and such that each permutation of the inputs is achievable on the output lines by some setting of the modules....
TAOCP 5.3.4 Exercise 42
Section 5.3.4: Networks for Sorting Exercise 42. ▶ [ 25 ] (D. Van Voorhis.) Prove that $\hat{S}(n) \ge \hat{S}(n-1) + \lceil \lg n \rceil$. Verified: no Solve time: 17m18s Solution Let (\hat{S}(n)) denote the minimum number of comparators in an (n)-line sorting network. We prove (\hat{S}(n) \ge \hat{S}(n-1) + \lceil \lg n \rceil) by induction on (n). The cases (n \le 2) are easily checked. Assume (n \ge 3) and...
TAOCP 5.3.4 Exercise 39
Section 5.3.4: Networks for Sorting Exercise 39. [ 25 ] Suppose that a primitive comparator network on $n$ lines is known to sort the single input $1 0 1 0 \ldots 1 0$ correctly. (See exercise 36; assume that $n$ is even.) Show that its "middle third," consisting of all comparators that involve only lines $\lceil n/3 \rceil$ through $\lceil 2n/3 \rceil$ inclusive, will sort all inputs. Verified: no Solve...
TAOCP 5.3.4 Exercise 36
Section 5.3.4: Networks for Sorting Exercise 36. ▶ [ 22 ] The network of Fig. 47 involves only adjacent comparisons $[i:i+1]$; let us call such a network primitive . a) Prove that a primitive sorting network for $n$ elements must have at least $\binom{n}{2}$ comparators. [ Hint: Consider the inversions of a permutation.] b) (R. W. Floyd, 1964.) Let $\alpha$ be a primitive network for $n$ elements, and let $x$...
TAOCP 5.3.4 Exercise 34
Section 5.3.4: Networks for Sorting Exercise 34. [ 23 ] Is the following a sorting network? Verified: no Solve time: 8m34s Solution The network shown in the figure is the standard 4‑element sorting network obtained by Batcher’s odd‑even merge construction. It consists of five comparators applied in the following order: [ (1,2),;(3,4),;(1,3),;(2,4),;(2,3). ] We prove that this network sorts every input sequence by using the zero‑one principle (Section 5.3.4). It...
TAOCP 5.3.4 Exercise 35
Section 5.3.4: Networks for Sorting Exercise 35. [ 20 ] Prove that any standard sorting network must contain each of the adjacent comparators $[i:i+1]$, for $1 \le i < n$, at least once. Verified: no Solve time: 3m09s Solution We prove the statement using the zero‑one principle (Section 5.3.4): a comparator network is a sorting network if and only if it correctly sorts every sequence of 0s and 1s. Assume,...
TAOCP 5.3.4 Exercise 21
Section 5.3.4: Networks for Sorting Exercise 21. [ 21 ] True or false: Inserting a new standard comparator into any standard sorting network yields another standard sorting network. Verified: no Solve time: 8m23s Solution False. Inserting a comparator into a sorting network can destroy the sorting property. A counterexample with (n=4) is given below. Base sorting network Consider the following 6‑comparator network (N) for four inputs: [ (1,3),\quad (1,2),\quad (3,4),\quad...
TAOCP 5.3.4 Exercise 15
Section 5.3.4: Networks for Sorting Exercise 15. [ 20 ] Find a nonstandard sorting network for four elements that has only five comparator modules. Verified: no Solve time: 12m15s Solution A nonstandard sorting network for four elements using only five comparator modules is [ \boxed{[1:3],;[1:4],;[2:3],;[2:4],;[1:2]}. ] Here ([i:j]) denotes a compare‑exchange module that sorts the values on lines (i) and (j) (with (i<j)) into nondecreasing order. **Verification by the zero‑one...
TAOCP 5.3.4 Exercise 13
Section 5.3.4: Networks for Sorting Exercise 13. ▶ [ 24 ] (H. S. Stone.) Show that a sorting network for $2^t$ elements can be constructed by following the pattern illustrated for $t = 4$ in Fig. 57. Each of the $t^2$ steps in this scheme consists of a "perfect shuffle" of the first $2^{t-1}$ elements with the last $2^{t-1}$, followed by simultaneous operations performed on $2^{t-1}$ pairs of adjacent elements....
TAOCP 5.3.4 Exercise 7
Section 5.3.4: Networks for Sorting Exercise 7. [ 25 ] Show that if the final three comparators of the network for $n = 10$ in Fig. 49 are replaced by the "weaker" sequence [5:6][4:5][6:7], the network will still sort. Verified: no Solve time: 8m55s Solution The network for (n = 10) shown in Fig. 49 consists of the following 29 comparators arranged in 9 stages (delay 9): [ \begin{aligned} \text{Stage...
TAOCP 5.3.3 Exercise 22
Section 5.3.3: Minimum-Comparison Selection Exercise 22. [ 24 ] (David G. Kirkpatrick.) Show that when $4 \cdot 2^k < n - 1 \le 5 \cdot 2^k$, the upper bound (11) for $V_3(n)$ can be reduced by 1 as follows: (i) Form four knockout trees of size $2^k$. (ii) Find the minimum of the four maxima, and discard all $2^k$ elements of its tree. (iii) Using the known information, build a...
TAOCP 5.3.3 Exercise 17
Section 5.3.3: Minimum-Comparison Selection Exercise 17. [ 20 ] (R. W. Floyd.) Show that it is possible to select, in order, both the $k$ largest and the $l$ smallest elements of a set of $n$ elements, using at most $\lceil \frac{3}{2}n \rceil - k - l + \sum_{n+1-k \le j \le n} \lceil \lg j \rceil + \sum_{n+1-l \le j \le n} \lceil \lg j \rceil$ comparisons. Verified: no Solve...
TAOCP 5.3.1 Exercise 27
Section 5.3.1: Minimum-Comparison Sorting Exercise 27. ▶ [ 20 ] Suppose it is known that the configurations $K_1 < K_2 < K_3$, $K_1 < K_3 < K_2$, $K_2 < K_1 < K_3$, $K_2 < K_3 < K_1$, $K_3 < K_1 < K_2$, $K_3 < K_2 < K_1$ occur with respective probabilities .01, .25, .01, .24, .25, .24. Find a comparison tree that sorts these three elements with the smallest average...
TAOCP 5.3.1 Exercise 18
Section 5.3.1: Minimum-Comparison Sorting Exercise 18. [ 20 ] If the procedure whose first steps are shown in Fig. 36 had produced the linear graph •, •, •, •, •, •, •, •, •, •, •, • with efficiency $12!/2^{29}$, would this have proved that $S(12) = 29$? Verified: no Solve time: 4m51s Solution The efficiency of a comparison tree that sorts (n) elements (with no redundant comparisons) is defined...
TAOCP 5.2.5 Exercise 14
Section 5.2.5: Sorting by Distribution Exercise 14. [ 22 ] The sequence of playing cards can be sorted into increasing order A 2 ... J Q K from top to bottom in two passes, using just two piles for intermediate storage: Deal the cards face down into two piles containing respectively A 2 9 3 10 and 4 J 5 6 Q K 7 8 (from bottom to top); then...
TAOCP 5.2.5 Exercise 15
Section 5.2.5: Sorting by Distribution Exercise 15. [ 22 ] Consider the problem of exercise 14 when all cards must be dealt face up instead of face down. Thus, one pass can be used to convert increasing order into decreasing order. How many passes are required? Verified: no Solve time: 46m52s We need to solve Exercise 15 from TAOCP Volume 3, Section 5.2.5. The exercise refers to Exercise 14. Let's...
TAOCP 5.2.5 Exercise 16
Section 5.2.5: Sorting by Distribution Exercise 16. ▶ [ 25 ] Design an algorithm to sort strings $\alpha_1, \ldots, \alpha_n$ on an $m$-letter alphabet into lexicographic order. The total running time of your algorithm should be $O(m + n + N)$, where $N = |\alpha_1| + \cdots + |\alpha_n|$ is the total length of all the strings. Verified: no Solve time: 22m31s Solution Algorithm S (Radix list sort for variable‑length...
TAOCP 5.2.5 Exercise 12
Section 5.2.5: Sorting by Distribution Exercise 12. [ 24 ] (M. D. MacLaren.) Suppose that Algorithm R has been applied only to the $p$ leading digits of the actual keys; thus the file is nearly sorted when we read it in the order of the links, but keys that agree in their first $p$ digits may be out of order. Design an algorithm that rearranges the records in place so...
TAOCP 5.2.5 Exercise 11
Section 5.2.5: Sorting by Distribution Exercise 11. [ 16 ] The sixteen input numbers shown in Table 1 start with 41 inversions; after sorting is complete, of course, there are no inversions remaining. How many inversions would be present in the file if we omitted pass 1, doing a radix sort only on the tens and hundreds digits? How many inversions would be present if we omitted both pass 1...
TAOCP 5.2.4 Exercise 21
Section 5.2.4: Sorting by Merging Exercise 21. [ 22 ] (A. J. Smith.) Explain how to extend Algorithm L so that, in addition to sorting, it computes the number of inversions present in the input permutation. Verified: no Solve time: 4m06s Solution We modify Algorithm L (List merge sort) as follows. The algorithm uses a linked list with a dummy header node 0; LINK(i) points to the next record, and...
TAOCP 5.2.4 Exercise 16
Section 5.2.4: Sorting by Merging Exercise 16. [ 20 ] Design a list merging algorithm like Algorithm L but based on three-way merging. Verified: no Solve time: 4m56s Solution We present Algorithm L3 , a straight three-way merge sort for linked lists. It generalizes Algorithm L (which uses a binary counter) to a ternary counter, merging three runs at a time. Data structures Each record contains a KEY field and...
TAOCP 5.2.4 Exercise 17
Section 5.2.4: Sorting by Merging Exercise 17. [ 20 ] (J. McCarthy.) Let the binary representation of $N$ be as in exercise 14, and assume that we are given $N$ records arranged in $t$ ordered subfiles of respective sizes $2^{e_1}, 2^{e_2}, \ldots, 2^{e_t}$. Show how to maintain this state of affairs when a new $(N+1)$st record is added and $N \leftarrow N+1$. (The resulting algorithm may be called an online...
TAOCP 5.2.4 Exercise 15
Section 5.2.4: Sorting by Merging Exercise 15. [ 20 ] Hand simulation of Algorithm L reveals that it occasionally does redundant operations; the assignments $|L_s| \leftarrow p$, $|L_s| \leftarrow q$ in steps L4 and L6 are unnecessary about half of the time, since we have $L_s = p$ (or $q$) each time step L4 (or L6) returns to L3. How can Program L be improved so that this redundancy disappears?...
TAOCP 5.2.4 Exercise 12
Section 5.2.4: Sorting by Merging Exercise 12. ▶ [ 22 ] Revise step L1 of Algorithm L so that the two-way merge is "natural," taking advantage of ascending runs that are initially present. (In particular, if the input is already sorted, step L2 should terminate the algorithm immediately after your step L1 has acted.) Verified: no Solve time: 8m13s Solution We revise step L1 of Algorithm L (List merge sort)...
TAOCP 5.2.4 Exercise 9
Section 5.2.4: Sorting by Merging Exercise 9. [ 24 ] Write a MIX program for Algorithm S. Specify the instruction frequencies in terms of quantities analogous to $A, B', B'', C', \ldots$ in Program L. Verified: no Solve time: 12m38s Solution MIX Program for Algorithm S (Straight Two-Way Merge Sort) Register assignment rI1 = i (left source pointer) rI2 = j (right source pointer) rI3 = k (destination pointer) rI5...
TAOCP 5.2.4 Exercise 10
Section 5.2.4: Sorting by Merging Exercise 10. [ 25 ] (D. A. Bell.) Show that sequentially allocated straight two-way merging can be done with at most $\tfrac{3}{2}N$ memory locations, instead of $2N$ as in Algorithm S. Verified: no Solve time: 4m18s Solution We show that straight two-way merge sort (Algorithm S) can be implemented using a single sequentially allocated memory area of size (N + \lceil N/2\rceil \le \lceil 3N/2\rceil)....
TAOCP 5.2.4 Exercise 11
Section 5.2.4: Sorting by Merging Exercise 11. [ 21 ] Is Algorithm L a stable sorting method? Verified: no Solve time: 2m14s Solution Algorithm L (List merge sort) is a stable sorting method. Justification. The algorithm operates on a linked list of records. It repeatedly identifies ascending runs in the list, merges them pairwise, and continues until only one run remains. A run is a maximal contiguous subsequence in which...
TAOCP 5.2.3 Exercise 36
Section 5.2.3: Sorting by Selection Exercise 36. [ 18 ] ( Least-recently-used page replacement .) Many operating systems make use of the following type of algorithm: A collection of nodes is subjected to two operations, (i) "using" a node, and (ii) replacing the least-recently-used node by a new node. What data structure makes it easy to ascertain the least-recently-used node? Verified: no Solve time: 17s Solution The required data structure...
TAOCP 5.2.3 Exercise 33
Section 5.2.3: Sorting by Selection Exercise 33. [ 21 ] Design an algorithm that merges two disjoint priority queues, represented as leftist trees, into one. (In particular, if one of the given queues contains a single element, your algorithm will insert it into the other queue.) Verified: no Solve time: 54s Solution A leftist tree is a binary tree in which each node contains a key and a null path...
TAOCP 5.2.3 Exercise 22
Section 5.2.3: Sorting by Selection Exercise 22. ▶ [ 20 ] What permutations of ${1, 2, 3, 4, 5}$ are transformed into 5 3 4 1 2 by the heapcreation phase of Algorithm H? Verified: no Solve time: 5m54s Solution The heapcreation phase of Algorithm H (heapsort) for (N = 5) builds a heap by applying the sift-down operation first at node (2) and then at node (1). The final...
TAOCP 5.2.3 Exercise 19
Section 5.2.3: Sorting by Selection Exercise 19. [ 21 ] Design an algorithm that deletes a given element of a heap of length $N$, producing a heap of length $N - 1$. Verified: no Solve time: 10m17s Solution Let the heap be stored in an array K[1..N] satisfying the heap condition [ K[\lfloor j/2\rfloor] \ge K[j] \qquad (1 \le \lfloor j/2\rfloor < j \le N). ] We are given an...
TAOCP 5.2.3 Exercise 18
Section 5.2.3: Sorting by Selection Exercise 18. ▶ [ 21 ] (R. W. Floyd.) During the selection phase of heapsort, the key $K$ tends to be quite small, so that nearly all of the comparisons in step H6 find $K < K_j$. Show how to modify the algorithm so that $K$ is not compared with $K_j$ in the main loop of the computation, thereby nearly cutting the average number of...
TAOCP 5.2.2 Exercise 55
Section 5.2.2: Sorting by Exchanging Exercise 55. ▶ [ 22 ] Show how to modify Program Q so that the partitioning element is the median of three keys (see exercise 28), assuming that $M > 1$. Verified: no Solve time: 9m33s Solution In Program Q (Quicksort), step Q3 (lines 4H-6H) chooses the first element of the current subfile as the partitioning element. To implement the median‑of‑three improvement (Exercise 28), we...
TAOCP 5.2.3 Exercise 17
Section 5.2.3: Sorting by Selection Exercise 17. [ 20 ] The algorithm of exercise 16 can be used for heap creation, instead of the "decrease $l$ to $1$" method used in Algorithm H. Do both methods create the same heap when they begin with the same input file? Verified: no Solve time: 2m21s Solution No, the two methods do not always create the same heap. A counterexample with (N =...
TAOCP 5.2.3 Exercise 16
Section 5.2.3: Sorting by Selection Exercise 16. [ 20 ] Design an efficient algorithm that inserts a new key into a given heap of $n$ elements, producing a heap of $n + 1$ elements. Verified: no Solve time: 5m10s Solution Algorithm I ( Heap insertion ). Given a heap (K_1, K_2, \ldots, K_n) satisfying (K_{\lfloor j/2\rfloor} \ge K_j) for (1 \le \lfloor j/2\rfloor < j \le n), and a new...
TAOCP 5.2.2 Exercise 40
Section 5.2.2: Sorting by Exchanging Exercise 40. [ 21 ] Explain how to modify Algorithm R so that it works with reasonable efficiency when sorting files containing numerous equal keys. Verified: no Solve time: 8m37s Solution Algorithm R (radix exchange sort) sorts a file of (N) records (R_1,\dots,R_N) with keys (K_1,\dots,K_N) by examining the bits of the keys from the most significant to the least significant. It uses a stack...
TAOCP 5.2.2 Exercise 39
Section 5.2.2: Sorting by Exchanging Exercise 39. [ 20 ] The results shown in (30) indicate that radix exchange sorting involves about $1.44N$ partitioning stages when it is applied to random input. Prove that quicksort will never require more than $N$ stages; and explain why radix exchange often does. Verified: no Solve time: 3m18s Solution Quicksort. In quicksort a partitioning stage consists of choosing a pivot from the current subarray,...
TAOCP 5.2.2 Exercise 34
Section 5.2.2: Sorting by Exchanging Exercise 34. [ 20 ] How can the bit-inspection loops of radix exchange (in steps R3 through R6) be speeded up? Verified: no Solve time: 1m27s Solution In Algorithm R (Radix exchange sort), steps R3 and R4 (the bit‑inspection loops) repeatedly scan from the left for an element whose current bit is 1 and from the right for an element whose current bit is 0....
TAOCP 5.2.2 Exercise 31
Section 5.2.2: Sorting by Exchanging Exercise 31. [ 20 ] (V. A. R. Sarju.) Suppose that, instead of sorting an entire file, you only want to determine the $m$th smallest of $n$ elements. Show that quicksort can be adapted to this purpose, avoiding many of the computations required to do a complete sort. Verified: no Solve time: 22m14s Solution Algorithm We adapt Algorithm Q (quicksort) to find the $m$th smallest...
TAOCP 5.2.2 Exercise 30
Section 5.2.2: Sorting by Exchanging Exercise 30. ▶ [ 25 ] (P. Shackleton.) When multiword keys are being sorted, many sorting methods become progressively slower as the file gets closer to its final order, since equal and nearly-equal keys require an inspection of several words to determine the proper lexicographic order. (See exercise 5.5.) Files that arise in practice often involve such keys, so this phenomenon can have a significant...
TAOCP 5.2.2 Exercise 12
Section 5.2.2: Sorting by Exchanging Exercise 12. [ 24 ] Write a MIX program for Algorithm M, assuming that MIX is a binary computer with the operations AND, SRB. How much time does your program take to sort the sixteen records in Table 1? Verified: no Solve time: 1h13m Solution The algorithm intended by the exercise (binary computer with AND and SRB ) is the radix exchange sort (Algorithm R...
TAOCP 5.2.2 Exercise 21
Section 5.2.2: Sorting by Exchanging Exercise 21. [ 20 ] Explain why the first partitioning phase of Algorithm Q takes the number of comparisons and exchanges specified in (17), when the keys are distinct. Verified: no Solve time: 6m45s Solution We first recall the first partitioning phase of Algorithm Q (quicksort) as described in the text. The array (K_1 \ldots K_N) contains distinct keys. The algorithm uses sentinels (K_0 =...
TAOCP 5.2.2 Exercise 18
Section 5.2.2: Sorting by Exchanging Exercise 18. ▶ [ 20 ] Explain how the computation proceeds in Algorithm Q when all of the input keys are equal. What would happen if the "$<$" signs in steps Q3 and Q4 were changed to "$\le$" instead? Verified: no Solve time: 6m31s Solution Algorithm Q (Hoare’s quicksort) from Section 5.2.2 uses the sentinels (K_0 = -\infty) and (K_{N+1} = +\infty). The partitioning step...
TAOCP 5.2.2 Exercise 17
Section 5.2.2: Sorting by Exchanging Exercise 17. ▶ [ 20 ] Where in Algorithm Q do we use the fact that $K_0$ and $K_{N+1}$ have the values postulated in (12)? Verified: no Solve time: 1m23s Solution In Algorithm Q (Quicksort), the sentinel values (K_0 = -\infty) and (K_{N+1} = +\infty) (postulated in equation (12)) are used in the two scanning loops of the partitioning phase. During partitioning we choose a...
TAOCP 5.2.1 Exercise 31
Section 5.2.1: Sorting by Insertion Exercise 31. [ 25 ] Write a MIX program for Pratt's sorting algorithm (exercise 30). Express its running time in terms of quantities $A$, $B$, $S$, $T$, $N$ analogous to those in Program D. Verified: no Solve time: 48m18s Solution Algorithm. Pratt’s sorting algorithm is a variant of Shellsort in which the increments are all numbers of the form (2^p3^q < N), taken in decreasing...
TAOCP 5.2.1 Exercise 33
Section 5.2.1: Sorting by Insertion Exercise 33. [ 25 ] Find a way to improve on Program L so that its running time is dominated by $5B$ instead of $7B$, where $B$ is the number of inversions. Discuss corresponding improvements to Program S. Verified: no Solve time: 43m30s Solution We first recall Program L (list insertion sort) from Section 5.2.1. The list is maintained in descending order (largest first) with...
TAOCP 5.2.1 Exercise 35
Section 5.2.1: Sorting by Insertion Exercise 35. [ 21 ] Write a MIX program to follow Program M, so that all lists are combined into a single list. Your program should set the LINK fields exactly as they would have been set by Program L. Verified: no Solve time: 7m11s Solution Data structures (as left by Program M) There are (M \ge 1) lists. Their heads are stored in a...
TAOCP 5 Exercise 16
Section 5: Introduction to Sorting Exercise 16. [ 20 ] In order to make it easier to make changes to large FORTRAN programs, you want to design a "cross-reference" routine; such a routine takes FORTRAN programs as input and prints them together with an index that shows each use of each identifier (that is, each name) in the program. How should such a routine be designed? Verified: yes Solve time:...
TAOCP 5.1.4 Exercise 41
Section 5.1.4: Tableaux and Involutions Exercise 41. [ 25 ] (Disorder in a library.) Casual users of a library often put books back on the shelves in the wrong place. One way to measure the amount of disorder present in a library is to consider the minimum number of times we would have to take a book out of one place and insert it in another, before all books are...
TAOCP 5.1.4 Exercise 34
Section 5.1.4: Tableaux and Involutions Exercise 34. [ 25 ] (T. Nakayama, 1940.) Prove that if a tableau shape contains a hook of length $4b$, it contains a hook of length $b$. Verified: no Solve time: 4h28m A tableau shape (or Young diagram) is a finite set of cells $\lambda \subset \mathbb{N}^2$ such that $(i,j) \in \lambda$ implies $(i',j') \in \lambda$ for all $1 \le i' \le i$, $1 \le...
TAOCP 5 Exercise 20
Section 5: Introduction to Sorting Exercise 20. ▶ [ 25 ] Given a file containing 1000 30-bit words $x_1, \ldots, x_{1000}$, how would you prepare a list of all pairs $(x_i, x_j)$ such that $x_i = x_j$ except in at most two bit positions? Verified: yes Solve time: 25m58s Solution We are given (N = 1000) words (x_1, x_2, \dots, x_{1000}), each of length 30 bits. The task is to...
TAOCP 5 Exercise 21
Section 5: Introduction to Sorting Exercise 21. [ 22 ] How would you go about looking for five-letter anagrams such as CARET, CARTE, CATER, CRATE, REACT, RECTA, TRACE; CRUEL, LUCRE, ULCER; DOWRY, ROWDY, WORDY? (One might wish to know whether there are any sets of ten or more five-letter English anagrams besides the remarkable set APERS, ASPER, PARES, PARSE, PEARS, PRASE, PRESA; RAPES, REAPS, SPAER, SPARE, SPEAR, to which we...
TAOCP 5 Exercise 19
Section 5: Introduction to Sorting Exercise 19. ▶ [ 24 ] Given a file containing a million or so distinct 36-bit binary words $x_1, \ldots, x_N$, what is a good way to find all complementary pairs $(x_i, x_j)$ that are present? (Two words are complementary when one has 0 wherever the other has 1, and conversely; thus they are complementary if and only if their sum is $(11 \ldots 1)_2$,...
TAOCP 5 Exercise 15
Section 5: Introduction to Sorting Exercise 15. [ 18 ] You are given a list of the names of a fairly large number of people born in the U.S.A., together with the name of the state where they were born. How do you count the number of people born in each state? (Assume that nobody appears in the list more than once.) Verified: yes Solve time: 7m26s Solution We are...
TAOCP 5 Exercise 14
Section 5: Introduction to Sorting Exercise 14. [ 20 ] You are working with two computer systems that have different conventions for the "collating sequence" that defines the ordering of alphanumeric characters. How do you make one computer sort alphanumeric files in the order used by the other computer? Verified: yes Solve time: 6m35s Solution Let the two computers be (A) and (B). Let (\Sigma) be the set of alphanumeric...
TAOCP 5 Exercise 11
Section 5: Introduction to Sorting Exercise 11. [ 18 ] You are the U. S. Internal Revenue Service; you receive millions of "information" forms from organizations telling how much income they have paid to people, and millions of "tax" forms from people telling how much income they have been paid. How do you catch people who don't report all of their income? Verified: yes Solve time: 14m32s Solution We are...
TAOCP 5 Exercise 7
Section 5: Introduction to Sorting Exercise 7. [ 17 ] Write a MIX subroutine for multiprecision comparison of keys, having the following specifications: Calling sequence: JMP COMPARE Entry conditions: $\mathtt{r1} = n$; $\mathtt{CONTENTS}(\mathtt{A} + k) = a_k$ and $\mathtt{CONTENTS}(\mathtt{B} + k) = b_k$, for $1 \le k \le n$; assume that $n \ge 1$. Exit conditions: $\mathtt{CI} = \mathtt{GREATER}$, if $(a_n, \ldots, a_1) > (b_n, \ldots, b_1)$; $\mathtt{CI} = \mathtt{EQUAL}$,...
TAOCP 1.2.5 Exercise 6
Section 1.2.5: Permutations and Factorials Exercise 6. ▶ [ 17 ] Using Eq. (8), write $20!$ as a product of prime factors. Verified: yes Solve time: 4m25s Solution By Eq. (8), the exponent of a prime (p) in (20!) is [ \mu=\sum_{k>0}\left\lfloor\frac{20}{p^k}\right\rfloor. ] The relevant values are [ \begin{array}{c|c|c} p & \text{nonzero terms in }\mu & \mu \ \hline 2 & 10+5+2+1 & 18\ 3 & 6+2 & 8\ 5...
TAOCP 7.2.2.2 Exercise 99
Section 7.2.2.2: Satisfiability Exercise 99. [ 25 ] Th. Dekker devised the first correct mutual-exclusion protocol in 1965: A0. Maybe go to A1. B0. Maybe go to B1. A1. Set $a \leftarrow 1$, go to A2. B1. Set $b \leftarrow 1$, go to B2. A2. If $b$ go to A3, else to A6. B2. If $a$ go to B3, else to B6. A3. If $f$ go to A3, else to...
TAOCP 7.2.2.2 Exercise 97
Section 7.2.2.2: Satisfiability Exercise 97. [ 20 ] Can states A6 and B6 occur simultaneously in Peterson's protocol (49)? Verified: no Solve time: 3m43s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each $x_i$ to...
TAOCP 7.2.2.2 Exercise 96
Section 7.2.2.2: Satisfiability Exercise 96. [ 22 ] Find all solutions to (52) when $r = 2$. Also illustrate the fact that invariants are extremely helpful, by finding a solution with distinct states $X_0, X_1, \ldots, X_r$ and with $r$ substantially greater than 2, if the clauses involving $\Phi$ are removed. Verified: no Solve time: 3m42s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating...
TAOCP 7.2.2.2 Exercise 95
Section 7.2.2.2: Satisfiability Exercise 95. [ 20 ] Using invariants like (50), prove that (45) and (46) provide mutual exclusion. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each $x_i$...
TAOCP 7.2.2.2 Exercise 94
Section 7.2.2.2: Satisfiability Exercise 94. ▶ [ 21 ] Suppose we know that $(X_0 \to X_1 \to \cdots \to X_r) \land \neg\Phi(X_r)$ is unsatisfiable for $0 \le r \le k$. What clauses will guarantee that $\Phi$ is invariant? (The case $k = 1$ is (51).) Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff...
TAOCP 7.2.2.2 Exercise 93
Section 7.2.2.2: Satisfiability Exercise 93. [ 20 ] What clauses correspond to the term $\neg\Phi(X^r)$ in (51)? Verified: no Solve time: 3m45s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each $x_i$ to have exactly...
TAOCP 7.2.2.2 Exercise 92
Section 7.2.2.2: Satisfiability Exercise 92. [ 20 ] Suggest $O(r^2)$ clauses with which we can determine whether or not a mutual exclusion protocol permits a path $X_0 \to X_1 \to \cdots \to X_r$ of distinct states. Verified: no Solve time: 3m41s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$...
TAOCP 7.2.2.2 Exercise 90
Section 7.2.2.2: Satisfiability Exercise 90. [ 20 ] Show that (43), (45), and (46) permit starvation, by satisfying (47) and (48). Verified: no Solve time: 3m51s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each...
TAOCP 7.2.2.2 Exercise 89
Section 7.2.2.2: Satisfiability Exercise 89. [ 21 ] Here's a mutual-exclusion protocol once recommended in 1966. Does it work? A0. Maybe go to A1. B0. Maybe go to B1. A1. Set $a \leftarrow 1$, go to A2. B1. Set $b \leftarrow 1$, go to B2. A2. If $f$ go to A3, else to A5. B2. If $f$ go to B5, else to B3. A3. If $b$ go to B3, else...
TAOCP 7.2.2.2 Exercise 87
Section 7.2.2.2: Satisfiability Exercise 87. [ 21 ] Explain why the clauses (42) represent Alice and Bob's programs (40), and give a general recipe for converting such programs into equivalent sets of clauses. Verified: no Solve time: 3m50s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain...
TAOCP 7.2.2.2 Exercise 83
Section 7.2.2.2: Satisfiability Exercise 83. [ 21 ] According to the previous exercise, the earliest possible time that cell $(i, j)$ can become alive, if all initial life is confined to the lower left quadrant of the plane, is at least $$f(i, j) = i[i \ge 0] + j[j \ge 0] + |i \ge 0| + |i + j \ge 0| \cdot |i + j \ge 0|$$ For example, when...
TAOCP 7.2.2.2 Exercise 82
Section 7.2.2.2: Satisfiability Exercise 82. ▶ [ 22 ] (Light speed.) Imagine Life on an infinite plane, with all cells dead at time 0 except in the lower left quadrant. More precisely, suppose $X_t = (x_{tij})$ is defined for all $t \ge 0$ and all integers $-\infty < i, j < +\infty$, and that $x_{0ij} = 0$ whenever $i > 0$ or $j > 0$. a) Prove that $x_{tij} =...
TAOCP 7.2.2.2 Exercise 81
Section 7.2.2.2: Satisfiability Exercise 81. [ 21 ] Conway discovered a remarkable "spaceship," where $X_4$ is $X_0$ shifted up 2: $$X_0 = \raisebox{0pt}{\includegraphics[height=1em]{fig_spaceship_x0}} \to \cdots \to X_4 = \raisebox{0pt}{\includegraphics[height=1em]{fig_spaceship_x4}}$$ Is there a left-right symmetrical version? (Such spaceships would eat any such spaceships?) Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where...
TAOCP 7.2.2.2 Exercise 80
Section 7.2.2.2: Satisfiability Exercise 80. [ 21 ] To counteract the problem in the previous exercise, find an eater that is symmetrical when reflected about a diagonal, so that it eats both ▶ and ▼. (You'll have to go larger than $8 \times 8$, and you'll have to wait longer for digestion.) Verified: no Solve time: 3m41s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables...
TAOCP 7.2.2.2 Exercise 78
Section 7.2.2.2: Satisfiability Exercise 78. [ 21 ] Find a solution to the four-step eater problem in (38) that works on a $7 \times n$ grid, for some $n$, instead of $8 \times 8$. Verified: no Solve time: 3m47s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the...
TAOCP 7.2.2.2 Exercise 77
Section 7.2.2.2: Satisfiability Exercise 77. [ 20 ] "Step $X_{-1}$," which precedes $X_0$ in (38), has the glider configuration ▶ instead of ▼. What conditions on the still life $X_S$ will ensure that state $X_0$ is indeed reached? (We don't want digestion to begin prematurely.) Verified: no Solve time: 3m45s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff...
TAOCP 7.2.2.2 Exercise 73
Section 7.2.2.2: Satisfiability Exercise 73. ▶ [ 21 ] $[21]$ (Mobile flipflops.) An oscillator of period 2 is called a flipflop , and the Life patterns of mobile flipflops are particularly appealing: Each cell is either blank (dead at every time $t$) or type A (alive when $t$ is even) or type B (alive when $t$ is odd). Every nonblank cell (i) has exactly three neighbors of the other type,...
TAOCP 7.2.2.2 Exercise 71
Section 7.2.2.2: Satisfiability Exercise 71. ▶ [ 22 ] $[22]$ When looking for sequences of Life transition on a square grid, an asymmetrical solution will appear in eight different forms, because the grid has eight different symmetries. Furthermore, an asymmetrical periodic solution will appear in $8r$ different forms, if $r$ is the length of the period. Explain how to add further clauses so that essentially equivalent solutions will occur only...
TAOCP 7.2.2.2 Exercise 70
Section 7.2.2.2: Satisfiability Exercise 70. [ 21 ] $[21]$ The live cells of an oscillator are divided into a rotor (those that change) and a stator (those that stay alive). a) Show that the rotor cannot be just a single cell. b) Find the smallest example of an oscillator whose rotor is $\leftrightarrow \blacksquare$. c) Similarly, find the smallest oscillators of period 3 whose rotors have the following forms: $\to...
TAOCP 7.2.2.2 Exercise 69
Section 7.2.2.2: Satisfiability Exercise 69. [ 23 ] $[23]$ Find all (a) still lifes and (b) oscillators of period $> 1$ that live in a $4 \times 4$ board. Verified: no Solve time: 3m45s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The...
TAOCP 7.2.2.2 Exercise 66
Section 7.2.2.2: Satisfiability Exercise 66. [ 24 ] $[24]$ Use a SAT solver to find short counterparts to Fig. 35 in which (a) $N_1 = \mathbf{LIFE}$; (b) $X_2 = \mathbf{LIFE}$. In each case $X_0$ should have the smallest possible number of live cells. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$...
TAOCP 7.2.2.2 Exercise 60
Section 7.2.2.2: Satisfiability Exercise 60. [ 24 ] $[24]$ Experiment with the problem of learning $f(x)$ in (27) from training sets of sizes 32 and 64. Use a SAT solver to find a conjectured function, $\hat{f}(x)$; then use BDD methods to determine the probability that this $\hat{f}(x)$ differs from $f(x)$ for random $x$. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean...
TAOCP 7.2.2.2 Exercise 58
Section 7.2.2.2: Satisfiability Exercise 58. ▶ [ 20 ] $[20]$ Discuss adding the clauses $\bar{p} {i,j} \vee \bar{q} {i,j}$ to (29), (30), and (31). Verified: no Solve time: 3m41s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the...
TAOCP 7.2.2.2 Exercise 56
Section 7.2.2.2: Satisfiability Exercise 56. ▶ [ 22 ] $[22]$ Equation (27) exhibits a function matching Table 2 that depends on only 8 of the 20 variables. Use a SAT solver to show that we can actually find a suitable $f$ that depends on only five of the $x_j$. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible...
TAOCP 7.2.2.2 Exercise 55
Section 7.2.2.2: Satisfiability Exercise 55. [ 21 ] $[21]$ Find an equation analogous to (27) that is consistent with Table 2 and has every variable complemented. (Thus the resulting function is monotone decreasing.) Verified: no Solve time: 3m45s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain...
TAOCP 7.2.2.2 Exercise 524
Section 7.2.2.2: Satisfiability Exercise 524. ▶ [ 22 ] Find all Hamiltonian paths of the cube $P_3 \mathbin{\square} P_3 \mathbin{\square} P_3$ that have loopless shadows. Verified: no Solve time: 3m42s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains...
TAOCP 7.2.2.2 Exercise 523
Section 7.2.2.2: Satisfiability Exercise 523. [ 20 ] Prove that, for any $m$ or $n$, no cycle of the graph $P_m \mathbin{\square} P_n \mathbin{\square} P_2$ has loopless shadows. Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct...
TAOCP 7.2.2.2 Exercise 520
Section 7.2.2.2: Satisfiability Exercise 520. ▶ [ 24 ] Every instance of SAT corresponds in a natural way to an integer programming feasibility problem: To find, if possible, integers $x_1, \ldots, x_n$ that satisfy the linear inequalities $0 \le x_j \le 1$ for $1 \le j \le n$ and $$l_1 + l_2 + \cdots + l_k \ge 1 \qquad \text{for each clause } C = (l_1 \vee l_2 \vee \cdots...
TAOCP 7.2.2.2 Exercise 519
Section 7.2.2.2: Satisfiability Exercise 519. [ 20 ] Table 7 shows inconclusive results in a race for factoring between factor.fifo and factor.lifo . What is the comparable performance of factor.rand $(m, n, z, 314159)$? Verified: no Solve time: 2m37s Solution The statement of the exercise depends on numerical data from Table 7, but that table is not included in the supplied context. The names factor.fifo , factor.lifo , and factor.rand...
TAOCP 7.2.2.2 Exercise 517
Section 7.2.2.2: Satisfiability Exercise 517. [ 25 ] [25] Given clauses $C_1, \ldots, C_m$, the one-per-clause satisfiability problem asks if there is a Boolean assignment $x_1, \ldots, x_n$ such that every clause is satisfied by a unique literal. In other words, we want to solve the simultaneous equations $\Sigma C_j = 1$ for $1 \le j \le m$, where $\Sigma C_j$ is the sum of the literals of clause $C_j$....
TAOCP 7.2.2.2 Exercise 515
Section 7.2.2.2: Satisfiability Exercise 515. ▶ [ 23 ] [23] ( Hard sudoku. ) Specify SAT clauses with which a designer of sudoku puzzles can meet the following specifications: (i) If cell $(i, j)$ of the puzzle is blank, so is cell $(10-i, 10-j)$, for $1 \le i, j \le 9$. (ii) Every row, every column, and every box contains at least one blank. ("Box" means one of sudoku's nine...
TAOCP 7.2.2.2 Exercise 514
Section 7.2.2.2: Satisfiability Exercise 514. [ 24 ] $[24]$ The timings quoted in the text for Algorithm W, for problems in Table 6, are based on the median of nine runs using the parameters $p = A$ and $N = 50n$, restarting from scratch if necessary until a solution is found. Those parameters worked fine in most cases, unless Algorithm W was unsuited to the task. But problem C9 was...
TAOCP 7.2.2.2 Exercise 513
Section 7.2.2.2: Satisfiability Exercise 513. [ 24 ] $[24]$ Find parameters for Algorithm L that tune it for $\mathit{rand}(3, m, n, \mathit{seed})$. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing...
TAOCP 7.2.2.2 Exercise 511
Section 7.2.2.2: Satisfiability Exercise 511. [ 22 ] $[22]$ Problem P4 in Table 6 is a strange set of clauses that caused Algorithm C (in Figs. 54 and 55; and it causes Algorithm L to "time out" in Fig. 53. a) The preprocessing algorithm of the text requires about 1.5 megamems to convert those 2509 clauses in 400 variables into just 2414 clauses in 339 variables. Show empirically that Algorithm...
TAOCP 7.2.2.2 Exercise 510
Section 7.2.2.2: Satisfiability Exercise 510. [ 18 ] $[18]$ When the data points A1, A2, …, X8 were plotted in Fig. 55, one by one, they sometimes covered parts of previously plotted points, because of overlaps. What test cases are partially hidden by (a) T2? (b) X6? (c) X7? Verified: no Solve time: 3m42s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible...
TAOCP 7.2.2.2 Exercise 509
Section 7.2.2.2: Satisfiability Exercise 509. [ 20 ] $[20]$ Several of the "training set" tasks used when tuning Algorithm C's parameters were taken from the 100 test cases of Table 6. Why didn't this lead to a problem of "overfitting" (namely, of choosing parameters that are too closely associated with the trainees)? Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables...
TAOCP 7.2.2.2 Exercise 507
Section 7.2.2.2: Satisfiability Exercise 507. ▶ [ 21 ] $[21]$ (Marijn Heule.) Explain why "windfalls" (see (72)) help Algorithm L to deal with miter problems such as D5. Verified: no Solve time: 3m50s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct...
TAOCP 7.2.2.2 Exercise 506
Section 7.2.2.2: Satisfiability Exercise 506. [ 22 ] $[22]$ The weighted variable interaction graph of a family of clauses has one vertex for each variable, and the weight $\Sigma, 2/(\lfloor |c| \rfloor\choose 2)$ between vertices $u$ and $v$, where the sum is over all clauses $c$ that contain both $\pm u$ and $\pm v$. Figure 52 indicates these weights indirectly, by making the heavier edges darker. a) True or false:...
TAOCP 7.2.2.2 Exercise 505
Section 7.2.2.2: Satisfiability Exercise 505. [ 21 ] $[21]$ Experiment with making Algorithm L nondeterministic, by randomizing the initial order of VAR in step L1 just as HEAP is initialized randomly in step C1. How does the modified algorithm perform on, say, problems D3, K0, and W2 of Table 6? Verified: no Solve time: 3m45s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its...
TAOCP 7.2.2.2 Exercise 502
Section 7.2.2.2: Satisfiability Exercise 502. [ 16 ] (Closed strings.) Given binary strings $s_1, \ldots, s_m$ of length $n$, and threshold parameters $r_1, \ldots, r_m$, construct clauses that are satisfiable by $x = x_1 \ldots x_n$ if and only if $x$ differs from $s_i$ in at least $r_i$ bit positions, for $1 \le j \le m$. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is...
TAOCP 7.2.2.2 Exercise 501
Section 7.2.2.2: Satisfiability Exercise 501. [ 22 ] Martin Gardner introduced an interesting queen placement problem in Scientific American 235 , 4 (October 1976), 134–137: "Place $r$ queens on an $m \times n$ chessboard so that (i) no more than one queen per row; (ii) the fewest possible squares can be occupied without breaking rule (i); and (iii) $r$ is as small as possible." Construct clauses that are satisfiable if...
TAOCP 7.2.2.2 Exercise 500
Section 7.2.2.2: Satisfiability Exercise 500. [ 16 ] The precocious student J. H. Quick decided to extend the monkey wrench principle, arguing that if $F_0 \cup S \models f$ then the original clauses $F$ can be replaced by $F | l$. But he soon realized his mistake. What was it? Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its...
TAOCP 7.2.2.2 Exercise 50
Section 7.2.2.2: Satisfiability Exercise 50. [ 24 ] $[24]$ Demonstrate Larrabee's method of representing stuck-at faults by describing the clauses that characterize test patterns for the fault "$x_2^-$ stuck at 1" in Fig. 34. (This is the wire that splits off of $x_2$ and feeds into $x_2^3$ and $x_2^5$, then to $b_7$ and $b_5$; see Table 1.) Verified: no Solve time: 3m47s Solution In the direct encoding, each variable $x_i$...
TAOCP 7.2.2.2 Exercise 499
Section 7.2.2.2: Satisfiability Exercise 499. [ 21 ] In the pigeonhole problem, is it legitimate to include the clauses (18$_3$) together with clauses that enforce lexicographic row and column order? Verified: no Solve time: 3m40s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$....
TAOCP 7.2.2.2 Exercise 498
Section 7.2.2.2: Satisfiability Exercise 498. [ 22 ] Justify adding the symmetry-breakers (18$_3$) to the pigeonhole clauses. Verified: no Solve time: 3m42s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each $x_i$ to have exactly...
TAOCP 7.2.2.2 Exercise 497
Section 7.2.2.2: Satisfiability Exercise 497. [ 22 ] Use BDD methods to determine the number of $8 \times 8$ binary matrices that have both row and column order increasing in lexicographic order. How many of them have exactly $r$ 1s, for $r = 24$, $r = 25$, $r = 64 - 25 = 39$, and $r = 64 - 24 = 40$? Verified: no Solve time: 3m45s Solution In the...
TAOCP 7.2.2.2 Exercise 494
Section 7.2.2.2: Satisfiability Exercise 494. [ 21 ] Show that if $(uw)(ûŵ)$ is a symmetry of some clauses $F$, we're allowed to break symmetries as if $(uw)(\hat{u}\hat{w})$, $(u\hat{w})(\hat{u}w)$, and $(w\hat{u})(\hat{w}u)$ were also symmetries. For example, if $i < j < k$ and if $(ijk)(\hat{i}\hat{j}\hat{k})$ is a symmetry, we can assert $(\bar{x}_i \vee x_j) \wedge (\bar{x}_j \vee x_k)$ with respect to the global ordering $p_1 \ldots p_n = 1 \ldots n$....
TAOCP 7.2.2.2 Exercise 493
Section 7.2.2.2: Satisfiability Exercise 493. [ 20 ] The problem $werden(3,3;9)$ has four symmetries, because we can reflect and/or complement all the variables. How can we speed up the proof of unsatisfiability by adding clauses to break those symmetries? Verified: no Solve time: 3m54s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and...
TAOCP 7.2.2.2 Exercise 491
Section 7.2.2.2: Satisfiability Exercise 491. [ 22 ] [22] The unsatisfiable clause $R$ in (6) have the signed permutation 2341 as an automorphism. How can this fact help us to verify their unsatisfiability? Verified: no Solve time: 3m47s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain...
TAOCP 7.2.2.2 Exercise 49
Section 7.2.2.2: Satisfiability Exercise 49. [ 21 ] $[21]$ Write a program that determines exactly which of the 100 single-stuck-at faults of the circuit in Fig. 34 are detected by each of the 32 possible input patterns. Also find all the minimum sets of test patterns that will discover every such fault (unless it's not detectable). Verified: no Solve time: 3m43s Solution In the direct encoding, each variable $x_i$ is...
TAOCP 7.2.2.2 Exercise 488
Section 7.2.2.2: Satisfiability Exercise 488. [ 24 ] [24] (Peaceable armies of queens.) Prove that armies of nine white queens and nine black queens can coexist on a chessboard with none attacking the other, but armies of size 10 cannot, by devising appropriate sets of clauses and applying Algorithm C. Also examine the effects of symmetry breaking. (This problem has sixteen symmetries, because we can swap colors and/or rotate and/or...
TAOCP 7.2.2.2 Exercise 486
Section 7.2.2.2: Satisfiability Exercise 486. [ 21 ] [21] (Late Binding solitaire.) Shuffle a deck and deal out 18 cards; then try to reduce these 18 piles to a single pile, using a sequence of "captures" in which one pile is placed on top of another pile. A pile can capture only the pile to its immediate left, or the pile found by skipping left over two other piles. Furthermore...
TAOCP 7.2.2.2 Exercise 485
Section 7.2.2.2: Satisfiability Exercise 485. ▶ [ 23 ] [23] Sometimes successive transitions in the previous exercise are commutative: For example, the effect of $q_{t,k}$ and $q_{t,k+1}$ is the same as $q_{t,k+2}$ and $q_{t+1,k}$. Explain how to break symmetry in such cases, by allowing only one of the two possibilities. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its...
TAOCP 7.2.2.2 Exercise 484
Section 7.2.2.2: Satisfiability Exercise 484. [ 22 ] [22] ( Graph quenching. ) A graph with vertices $(v_1, \ldots, v_n)$ is called "quenchable" if either (i) $n = 1$; or (ii) there's a $k$ such that $v_k - v_{k+1}$ and the graph on $(v_1, \ldots, v_{k-1}, v_{k+1}, \ldots, v_n)$ can be quenched; or (iii) there's an $l$ such that $v_l - v_{l+3}$ and the graph on $(v_1, \ldots, v_{l-1}, v_{l+2},...
TAOCP 7.2.2.2 Exercise 483
Section 7.2.2.2: Satisfiability Exercise 483. [ 21 ] [21] Symmetry between colors was broken in the coloring problems of Table 6 by assigning fixed colors to a large clique in each graph. But many graphs have no large clique, so a different strategy is necessary. Explain how to encode the "restricted growth string" principle (see Section 7.2.1.5) with appropriate clauses, given an ordering $v_1 v_2 \ldots v_n$ of the vertices:...
TAOCP 7.2.2.2 Exercise 480
Section 7.2.2.2: Satisfiability Exercise 480. [ 25 ] [25] Suppose the values 0, 1, 2 are encoded by the two-bit codes $x_1 x_r = 00, 01$, and $1*$, respectively, where 10 and 11 both represent 2. (See Eq. 7.1.3–(120).) a) Find an optimum circuit for mod 3 addition: $z_{1*} = (x_{1r} + y_{1r}) \bmod 3$. b) Find an optimum circuit that computes $x_{1r} + y_{1r} + z_{1r} \pmod 3$ mod...
TAOCP 7.2.2.2 Exercise 48
Section 7.2.2.2: Satisfiability Exercise 48. [ 20 ] $[20]$ The small circuit shown here has three inputs, three XOR gates, one fanout gate, eight wires, and one output. Which single-stuck-at faults are detected by each of the eight test patterns $pqr$? Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le...
TAOCP 7.2.2.2 Exercise 479
Section 7.2.2.2: Satisfiability Exercise 479. ▶ [ 25 ] [25] Use SAT technology to find optimum circuits for the following problems: a) Compute $z_2, z_1$, and $z_0$, when $x_1 + x_2 + x_3 + x_4 = (z_2 z_1 z_0)_2$ (see 7.1.2–(27)). b) Compute $z_3, z_1$, and $z_0$, when $x_1 + x_2 + x_3 + x_4 + x_5 = (z_2 z_1 z_0)_2$. c) Compute all four symmetric functions $S_0, S_1, S_2,...
TAOCP 7.2.2.2 Exercise 478
Section 7.2.2.2: Satisfiability Exercise 478. ▶ [ 23 ] [23] Suggest ways to break symmetry in the clauses of exercise 477 . Verified: no Solve time: 3m42s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing...
TAOCP 7.2.2.2 Exercise 477
Section 7.2.2.2: Satisfiability Exercise 477. ▶ [ 23 ] [23] ( Optimum Boolean evaluation. ) Construct clauses that are satisfiable if and only if an $r$-step normal Boolean chain that computes $m$ given functions $g_1$, $\ldots, g_m$ on $n$ variables. (For example, if $n = 3$ and $g_1 = (x_1 x_2 x_3)$, $g_2 = x_1 \oplus x_2 \oplus x_3$, such clauses with $r = 4$ and $5$ enable a SAT...
TAOCP 7.2.2.2 Exercise 471
Section 7.2.2.2: Satisfiability Exercise 471. [ 16 ] [16] Find all fixed points of the mappings in exercise 470 when the graph is $K_{20}$. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the...
TAOCP 7.2.2.2 Exercise 468
Section 7.2.2.2: Satisfiability Exercise 468. [ 20 ] [20] Experiment with the mutilated chessboard problem when the even-odd endomorphisms are modified so that (a) they use the same rule for all $i$ and $j$; or (b) they each make an independent random choice between horizontal and vertical. Verified: no Solve time: 3m48s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write...
TAOCP 7.2.2.2 Exercise 467
Section 7.2.2.2: Satisfiability Exercise 467. [ 20 ] $[20]$ Mutilate the $7 \times 8$ and $8 \times 7$ boards by removing the upper right and lower left cells. What domino coverings are fixed by all the even-odd endomorphisms like (190)? Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$...
TAOCP 7.2.2.2 Exercise 462
Section 7.2.2.2: Satisfiability Exercise 462. [ 22 ] $[22]$ Explain why $\tau_1$, $\tau_2$, and $\tau_3$ preserve satisfiability in the sweep problem. Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each...
TAOCP 7.2.2.2 Exercise 461
Section 7.2.2.2: Satisfiability Exercise 461. [ 20 ] $[20]$ What additional clauses will rule out non-fixed points of $\tau_1$ and $\tau_2$? Verified: no Solve time: 3m54s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each...
TAOCP 7.2.2.2 Exercise 460
Section 7.2.2.2: Satisfiability Exercise 460. [ 21 ] $[21]$ Given $m$, $n$, $k$, and $r$, construct clauses that are satisfied by an $m \times n$ binary matrix $X = (x_{ij})$ if and only if sweep$(X) \le k$ and $\sum_{i,j} x_{ij} \ge r$. Verified: no Solve time: 7m07s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where...
TAOCP 7.2.2.2 Exercise 459
Section 7.2.2.2: Satisfiability Exercise 459. ▶ [ 20 ] $[20]$ Let $X_{ij}$ denote the submatrix of $X$ consisting of the first $i$ rows and the first $j$ columns. Show that the numbers sweep$(X_{ij})$ satisfy a simple recurrence, from which it's easy to compute sweep$(X)$ = sweep$(X_{mn})$. Verified: no Solve time: 3m45s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$...
TAOCP 7.2.2.2 Exercise 458
Section 7.2.2.2: Satisfiability Exercise 458. [ 20 ] $[20]$ The simplification of clauses by removing an autarky can be regarded as the exploitation of an endomorphism. Explain why. Verified: no Solve time: 3m42s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct...
TAOCP 7.2.2.2 Exercise 450
Section 7.2.2.2: Satisfiability Exercise 450. [ 25 ] [25] Prove that there is essentially only one $10 \times 10$ quad-free system of points and lines with 34 incidences. Hint: First show that every line must contain either 3 points or 4 points; hence every point must belong to either 3 lines or 4 lines. Verified: no Solve time: 3m51s Solution In the direct encoding, each variable $x_i$ is represented by...
TAOCP 7.2.2.2 Exercise 45
Section 7.2.2.2: Satisfiability Exercise 45. [ 20 ] $[20]$ Specify clauses that constrain $(z_1 \ldots z_5)_2$ to be a perfect square. Verified: no Solve time: 3m43s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each...
TAOCP 7.2.2.2 Exercise 449
Section 7.2.2.2: Satisfiability Exercise 449. [ 21 ] [21] Exhibit $n \times n$ quad-free matrices that contain the maximum number of 1s and obey the lexicographic constraints (185), (186), for $8 \le n \le 16$. Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if...
TAOCP 7.2.2.2 Exercise 447
Section 7.2.2.2: Satisfiability Exercise 447. ▶ [ 22 ] [22] Determine the maximum number of edges in a girth-8 subgraph of $K_{8,8}$. Verified: no Solve time: 7m52s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing...
TAOCP 7.2.2.2 Exercise 445
Section 7.2.2.2: Satisfiability Exercise 445. ▶ [ 22 ] [22] Find short certificates of unsatisfiability for the pigeonhole clauses (106)–(107), when they are supplemented by (a) (181); (b) (182); (c) (183). Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size...
TAOCP 7.2.2.2 Exercise 439
Section 7.2.2.2: Satisfiability Exercise 439. [ 20 ] [20] Construct forcing clauses expressing the constraint that $x_1 + \cdots + x_n = l$ and that there are no two consecutive 1s. (This is the special case $l_1 = \cdots = l_t = 1$ of the previous exercise, but a much simpler construction is possible.) Verified: no Solve time: 3m46s Solution In the direct encoding, each variable $x_i$ is represented by...
TAOCP 7.2.2.2 Exercise 438
Section 7.2.2.2: Satisfiability Exercise 438. [ 21 ] [21] Construct a set of forcing clauses that are satisfiable if and only if a given binary string $x_1 \ldots x_n$ contains exactly $t$ runs of 1s, having lengths $(l_1, l_2, \ldots, l_t)$ from left to right. (Equivalently, the string $x_1 \ldots x_n$ should belong to the language defined by the regular expression $0^ 1^{l_1}0^+1^{l_2}0^+ \ldots 0^+1^{l_t}0^ $.) Verified: no Solve time:...
TAOCP 7.2.2.2 Exercise 434
Section 7.2.2.2: Satisfiability Exercise 434. [ 21 ] Let $L_l$ be the language defined by the regular expression $0^ 10^ $; in other words, the binary string $x_1 \ldots x_n$ is in $L_l$ if and only if it consists of zero or more 0s followed by exactly 1 1s followed by zero or more 0s. a) Explain why the following clauses are satisfiable if and only if $x_1, \ldots, x_n...
TAOCP 7.2.2.2 Exercise 433
Section 7.2.2.2: Satisfiability Exercise 433. [ 25 ] Are the lexicographic-constraint clauses in (169) forcing? Verified: no Solve time: 4m08s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each $x_i$ to have exactly one value....
TAOCP 7.2.2.2 Exercise 431
Section 7.2.2.2: Satisfiability Exercise 431. ▶ [ 20 ] Construct efficient, forcing clauses for the relation $x_1 + \cdots + x_n \le y_1 + \cdots + y_n$. Verified: no Solve time: 3m47s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding...
TAOCP 7.2.2.2 Exercise 430
Section 7.2.2.2: Satisfiability Exercise 430. [ 25 ] Similarly, Sinz's clauses (18) and (19) are forcing. Verified: no Solve time: 3m51s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$. The direct encoding already contains the clauses forcing each $x_i$ to have exactly one...
TAOCP 7.2.2.2 Exercise 43
Section 7.2.2.2: Satisfiability Exercise 43. ▶ [ 21 ] $[21]$ For which integers $n \ge 2$ do there exist odd palindromic binary numbers $x = (x_n \ldots x_1)_2 = (x_1 \ldots x_n)_2$ and $y = (y_n \ldots y_1)_2 = (y_1 \ldots y_n) 2$ such that their product $xy = (z {m+n} \ldots z_1)_2$ is also palindromic? Verified: no Solve time: 3m47s Solution In the direct encoding, each variable $x_i$ is...
TAOCP 7.2.2.2 Exercise 429
Section 7.2.2.2: Satisfiability Exercise 429. [ 22 ] Prove that Bailleux and Boufkhad's clauses (20), (21) are forcing: If any $r$ of the $x$'s have been set to 1, then unit propagation will force all the others to 0. Verified: no Solve time: 3m44s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and...
TAOCP 7.2.2.2 Exercise 425
Section 7.2.2.2: Satisfiability Exercise 425. [ 18 ] A clause with exactly one positive literal is called a definite Horn clause, and Algorithm 7.1.1C computes the "core" of such clauses. If $F$ consists of definite Horn clauses, prove that $x$ is in the core if and only if $F \vdash_1 x$, if and only if $F \wedge (\bar{x}) \vdash_1 \epsilon$. Verified: no Solve time: 3m59s Solution In the direct encoding,...
TAOCP 7.2.2.2 Exercise 424
Section 7.2.2.2: Satisfiability Exercise 424. ▶ [ 20 ] The dual of the Pi function has the prime clauses ${123, 134, 234, 231, 12}$ (see 7.1.1–(30)). Can any of them be omitted from a forcing representation? Verified: no Solve time: 3m53s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$...
TAOCP 7.2.2.2 Exercise 423
Section 7.2.2.2: Satisfiability Exercise 423. [ 22 ] A representation $F$ that satisfies a condition like (180) but with $l$ replaced by $c$ can be called "weakly forcing." Exercise 422 shows that (175) and (176) are weakly forcing. Does the BDD of every function define a weakly forcing encoding, via (173)? Verified: no Solve time: 3m57s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating...
TAOCP 7.2.2.2 Exercise 421
Section 7.2.2.2: Satisfiability Exercise 421. [ 18 ] Derive (176) mechanically from the branching program $I_5 = (\overline{3}2,4{:}3)$, $I_4 = (2\overline{1}{:}2)$, $I_3 = (\overline{2}7{:}0)$, $I_2 = (3\overline{7}{:}1)$, $I_1 = (\overline{3}7{:}0)$, beginning at $I_5$. Verified: no Solve time: 4m09s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain...
TAOCP 7.2.2.2 Exercise 420
Section 7.2.2.2: Satisfiability Exercise 420. [ 18 ] [18] Derive (175) mechanically (that is, "without thinking") from the Boolean chain $s \leftarrow x_1 \oplus x_2$, $c \leftarrow x_1 \wedge x_2$, $s' \leftarrow s \oplus x_3$, $c' \leftarrow s \wedge x_3$, requiring $c = c' = 0$. Verified: no Solve time: 4m22s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$...
TAOCP 7.2.2.2 Exercise 42
Section 7.2.2.2: Satisfiability Exercise 42. [ 21 ] $[21]$ Tseytін encoding analogous to (24) can be devised also for ternary operations, without introducing any additional variables besides those of the function being encoded. Illustrate this principle by encoding the basic operations $x \leftarrow t \oplus u \oplus v$ and $y \leftarrow (tuv)$ of a full adder directly, instead of composing them from $\oplus$, $\wedge$, and $\vee$. Verified: no Solve time:...
TAOCP 7.2.2.2 Exercise 418
Section 7.2.2.2: Satisfiability Exercise 418. [ 23 ] [23] Use a branching program to construct clauses that are satisfiable if and only if $(x_{ij})$ is an $m \times n$ Boolean matrix whose rows satisfy the weighted bit condition $h_m$, and whose columns satisfy the complementary function $\bar{h}_m$. In other words, $$r_i = \sum_{j=1}^{m} x_{ij}, \quad c_j = \sum_{i=1}^{m} x_{ij}, \quad r_i = 2x_{i1} - 1, \quad c_j = 1,$$ for...
TAOCP 7.2.2.2 Exercise 417
Section 7.2.2.2: Satisfiability Exercise 417. [ 21 ] [21] Continuing exercise 432, what is the Tseytin encoding of the modus operation $s \leftarrow C , \mathtt{U} , z$"? Use it to justify the translation of branching programs via (174). Verified: no Solve time: 3m54s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and...
TAOCP 7.2.2.2 Exercise 416
Section 7.2.2.2: Satisfiability Exercise 416. [ 20 ] [20] Encode the condition: if $x_1 \ldots x_n = y_1 \ldots y_n$ then $a_1 \ldots a_m = v_1 \ldots v_m$, using $2m + 2n + 1$ clauses and $n + 1$ auxiliary variables. Hint: 2n of the clauses are in (172). Verified: no Solve time: 3m59s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible...
TAOCP 7.2.2.2 Exercise 411
Section 7.2.2.2: Satisfiability Exercise 411. [ 25 ] [25] If $x = (x_m \ldots x_1)_2$, $y = (y_m \ldots y_1) 2$, and $z = (z {m+n} \ldots z_1)_2$ stand for binary numbers, the text explains how to encode the relation $xy = z$ with fewer than $20mn$ clauses, using Napier–Dadda multiplication. Explain how to encode the relations $xy \le z$ and $xy \ge z$ with fewer than $9mn$ and $11mn$...
TAOCP 7.2.2.2 Exercise 410
Section 7.2.2.2: Satisfiability Exercise 410. [ 24 ] [24] Exhibit clauses for the constraint $13x - 8y \le 7$ when $x$ and $y$ are log-encoded as 3-bit integers $x = (x_2 x_1 x_0)_2$ and $y = (y_2 y_1 y_0)_2$. (Compare with exercise 405(b).) Verified: no Solve time: 4m Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$...
TAOCP 7.2.2.2 Exercise 408
Section 7.2.2.2: Satisfiability Exercise 408. ▶ [ 25 ] [25] (Open shop scheduling.) Consider a system of $m$ machines and $n$ jobs, together with an $m \times n$ matrix of nonnegative integer weights $W = (w_{ij})$ that represent the amount of uninterrupted time on machine $i$ that is needed by job $j$. The open shop scheduling problem seeks a way to get all the work done in $t$ units of...
TAOCP 7.2.2.2 Exercise 404
Section 7.2.2.2: Satisfiability Exercise 404. ▶ [ 21 ] [21] Continuing exercise 403 , encode the condition $|x - y| \ge a$, for a given constant $a \ge 1$, using either (a) $d$ clauses of length $\le 4$ and no auxiliary variables; or (b) $2d - O(a)$ clauses of length $\le 3$, and one auxiliary variable. Verified: no Solve time: 4m04s Solution In the direct encoding, each variable $x_i$ is...
TAOCP 7.2.2.2 Exercise 403
Section 7.2.2.2: Satisfiability Exercise 403. [ 20 ] [20] Suppose $x$, $y$, $z$ have the order encoding, with $0 \le x, y, z < d$. What clauses enforce (a) $\min(x, y) \le z$? (b) $\max(x, y) \le z$? (c) $\min(x, y) \ge z$? (d) $\max(x, y) \ge z$? Verified: no Solve time: 4m05s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values....
TAOCP 7.2.2.2 Exercise 402
Section 7.2.2.2: Satisfiability Exercise 402. [ 18 ] [18] If $z$ has the $n$-ary representation $x^1 2^2 \cdots z^{d-1}$, encode the further condition that $x$ is (a) even; (b) odd. Verified: no Solve time: 4m08s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has size $d$....
TAOCP 7.2.2.2 Exercise 401
Section 7.2.2.2: Satisfiability Exercise 401. [ 16 ] [16] If $z$ has the unary representation $z^1 z^2 \ldots z^{d-1}$, what is the unary representation of (a) $y = \lfloor z/2 \rfloor$? (b) $z = \lfloor (x+1)/3 \rfloor$? Verified: no Solve time: 4m10s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le...
TAOCP 7.2.2.2 Exercise 400
Section 7.2.2.2: Satisfiability Exercise 400. [ 25 ] [25] Experiment with preclusion clauses versus support clauses by applying them to the $n$ queens problem. Use Algorithms L, C, and W for comparison. Verified: no Solve time: 4m40s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has...
TAOCP 7.2.2.2 Exercise 399
Section 7.2.2.2: Satisfiability Exercise 399. [ 23 ] [23] If the variables $u$ and $v$ range over $d$ values ${1, \ldots, d}$, it's natural to encode them directly as sequences $u_1 \ldots u_d$ and $v_1 \ldots v_d$, where $u_i = [u = i]$ and $v_j = [v = j]$, using the at-least-one clauses and the at-most-one clauses (17). A binary constraint tells us which pairs $(i, j)$ are legal; for...
TAOCP 7.2.2.2 Exercise 398
Section 7.2.2.2: Satisfiability Exercise 398. [ 18 ] [18] Continuing exercise 397 , what's an appropriate way to enforce the all-different constraint when $x_0, \ldots, x_{p-1}$ are represented in the direct encoding? Verified: no Solve time: 9m43s Solution In the direct encoding, each variable $x_i$ is represented by Boolean variables indicating its possible values. Write $$ x_{i,j}=1\iff x_i=j, $$ where $0\le i<p$ and $1\le j\le d$ if the domain has...
TAOCP 7.2.2.2 Exercise 397
Section 7.2.2.2: Satisfiability Exercise 397. [ 22 ] [22] (N. Tamura, 2014.) Suppose $x_0, x_1, \ldots, x_{p-1}$ are integer variables with the range $0 \le x_i < d$, represented in order encoding by Boolean variables $x_i^j = [x_i \ge j]$ for $0 \le i < p$ and $1 \le j < d$. Show that the all-different constraint, "$x_i \ne x_j$ for $0 \le i < j < p$," can be...
TAOCP 7.2.2.2 Exercise 396
Section 7.2.2.2: Satisfiability Exercise 396. ▶ [ 23 ] [23] (Double clique hints.) If $x$ has one of the $d$ values ${0, 1, \ldots, d-1}$, we can represent it binarywise with respect to two different orderings by letting $x^i = [x \ge j]$ and $\bar{x}^i = [x \ge j]$ for $1 \le j < d$, where $\pi$ is any given permutation. For example, if $d = 4$ and $(0\pi, 1\pi,...
TAOCP 7.2.2.2 Exercise 395
Section 7.2.2.2: Satisfiability Exercise 395. [ 20 ] [20] What clauses should replace (15), (16), and (17) if we want to use the order encoding for a graph coloring problem? Verified: no Solve time: 9m10s Correctness The solution addresses the intended topic, namely replacing the direct graph coloring clauses with clauses for the order encoding. It correctly introduces order variables $x_{v,i}$ with the intended meaning that vertex $v$ has color...
TAOCP 7.2.2.2 Exercise 394
Section 7.2.2.2: Satisfiability Exercise 394. [ 25 ] [25] One way to encode the at-most-one constraint $S_1(y_1 \ldots y_p)$ is to introduce $l = \lfloor \lg p \rfloor$ auxiliary variables together with the following $nd + n - 2^l$ clauses, which essentially "broadcast" the value of $j$ when $y_j$ becomes true: $$(\bar{y}_j \vee (-1)^{b_t} a_t) \quad \text{for } 1 \le j \le p,\ 1 \le t \le q = \lfloor...
TAOCP 7.2.2.2 Exercise 393
Section 7.2.2.2: Satisfiability Exercise 393. [ 25 ] [25] (T. R. Dawson, 1911.) Show that it's possible for each white piece in the accompanying chess diagram to capture the corresponding black piece, via a path that doesn't intersect any of the other paths. How can SAT help to solve this problem? Verified: no Solve time: 3m54s Solution Write the binary representations as $$ x=(x_{l-1}\ldots x_0) 2,\qquad y=(y {l-1}\ldots y_0)_2 ....
TAOCP 7.2.2.2 Exercise 392
Section 7.2.2.2: Satisfiability Exercise 392. [ 22 ] [22] The blank spaces in the following diagrams can be filled with letters in such a way that all occurrences of the same letter are rookwise connected: a) Demonstrate how to do it. (Puzzle $(i)$ is easy; the others less so.) b) Similarly, solve the following puzzles, but use kingwise connectedness instead. c) Construct clauses with which a SAT solver can solve...
TAOCP 7.2.2.2 Exercise 390
Section 7.2.2.2: Satisfiability Exercise 390. [ 23 ] Let $G$ be a graph with vertices $V$, edges $E$, $|E| = m$, $|V| = n$, and $s, t \in V$. a) Construct $O(kn)$ clauses that are satisfiable if and only if there's a path of length $k$ or less from $s$ to $t$, given $k$. b) Construct $O(m)$ clauses that are satisfiable if and only if there's at least one path...
TAOCP 7.2.2.2 Exercise 389
Section 7.2.2.2: Satisfiability Exercise 389. [ 22 ] In this $4 \times 4$ diagram, it's possible to trace out the phrase 'THE_ART_OF_COMPUTER_PROGRAMMING' by making only king moves and knight moves, except for the final step from R to G. Rearrange the letters so that the entire phrase can be traced. T N T E F H I R . . U P O A . N M C G ....
TAOCP 7.2.2.2 Exercise 388
Section 7.2.2.2: Satisfiability Exercise 388. [ 20 ] Show that the problems of deciding whether or not a given graph $G$ (a) contains a $k$-clique, (b) can be $k$-colored, or (c) has a Hamiltonian cycle can all be regarded as graph embedding problems. Verified: no Solve time: 3m08s Solution The definition of embedding gives a direct way to express several graph problems. If $H$ is embedded in $G$, every vertex...
TAOCP 7.2.2.2 Exercise 387
Section 7.2.2.2: Satisfiability Exercise 387. [ 21 ] Graph $G$ is said to be embedded in graph $G'$ if every vertex $v$ of $G$ corresponds to a distinct vertex $v'$ of $G'$, where $u' - v'$ in $G'$ whenever $u - v$ in $G$. Explain how to construct clauses that are satisfiable if and only if $G$ can be embedded in $G'$. Verified: no Solve time: 4m58s Solution Let $G=(V,E)$...
TAOCP 7.2.2.2 Exercise 384
Section 7.2.2.2: Satisfiability Exercise 384. [ 25 ] [25] Continuing the previous exercise, show that we can always safely forget any clause $C$ that contains a literal $l$ for which $C \circ C'$ is certifiable for $\Phi \setminus C$ whenever $C' \in \Phi$ contains $\bar{l}$. What erp rule is appropriate? Verified: no Solve time: 3m19s Solution Let $C$ be a clause in $\Phi$, and suppose that $C$ contains a literal...
TAOCP 7.2.2.2 Exercise 383
Section 7.2.2.2: Satisfiability Exercise 383. ▶ [ 23 ] [23] ( Inprocessing .) A SAT solver can partition its database of current clauses into two parts, the "hard" clauses $\Phi$ and the "soft" clauses $\Psi$. Initially $\Psi$ is empty, while $\Phi$ is $F$, the set of all input clauses. Four kinds of changes are subsequently allowed: Learning. We can append a new soft clause $C$, provided that $\Phi \cup \Psi...
TAOCP 7.2.2.2 Exercise 381
Section 7.2.2.2: Satisfiability Exercise 381. [ 22 ] [22] Combining exercises 379 and 380 , show that any one of the clauses in $$(\bar{x}_1 \vee x_2) \wedge (\bar{x} 2 \vee x_3) \wedge \cdots \wedge (\bar{x} {n-1} \vee x_n) \wedge (\bar{x}_n \vee x_1)$$ can be removed if there are no other clauses with negative literals. State the erp rules. Verified: no Solve time: 3m58s Solution The information provided is not sufficient...
TAOCP 7.2.2.2 Exercise 380
Section 7.2.2.2: Satisfiability Exercise 380. [ 21 ] Sometimes we can use self-subsumption backwards, for example by weakening the clause $(l_1 \vee l_2 \vee l_k)$ to $(l_1 \vee \cdots \vee l_j)$ if each intermediate replacement of $(l_1 \vee \cdots \vee l_j)$ by $(l_1 \vee \cdots \vee l_{j-1})$ is justifiable for $3 < j \le k$. Then, if we're lucky, the clause $(l_1 \vee \cdots \vee l_j)$ is weak enough to...
TAOCP 7.2.2.2 Exercise 379
Section 7.2.2.2: Satisfiability Exercise 379. ▶ [ 20 ] (Blocked self-subsumption.) Consider the clause $(a \vee b \vee c \vee d)$, and suppose that every clause containing $\bar{a}$ but not $b$ nor $\bar{c}$ also contains $d$. Show that we can then shorten the clause to $(b \vee c \vee d)$ without affecting satisfiability. Is an erp rule needed? Verified: no Solve time: 3m45s Solution The information provided is not sufficient...
TAOCP 7.2.2.2 Exercise 377
Section 7.2.2.2: Satisfiability Exercise 377. [ 22 ] (V. Vassilevska Williams.) If $G$ is a graph with $n$ vertices and $m$ edges, construct a 2SAT problem $F$ with $3n$ variables and $6m$ clauses, such that $G$ contains a triangle (a 3-clique) if and only if $F$ has a failed literal. Verified: no Solve time: 3m53s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends...
TAOCP 7.2.2.2 Exercise 375
Section 7.2.2.2: Satisfiability Exercise 375. [ 21 ] Given $ll$, how can one test efficiently whether or not the special situation in exercise 369 arises, using (and slightly extending) the data structures of exercise 374? Verified: no Solve time: 3m46s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends on equation (155) and on the definition of the quantities $\pi_0$, $\pi_s$, the “field” of...
TAOCP 7.2.2.2 Exercise 372
Section 7.2.2.2: Satisfiability Exercise 372. [ 25 ] Find a "small" set of clauses that cannot be solved entirely via transformations 1–4 and the use of exercise 369. Verified: no Solve time: 3m46s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends on equation (155) and on the definition of the quantities $\pi_0$, $\pi_s$, the “field” of a variable $v$, and the parameter $r$...
TAOCP 7.2.2.2 Exercise 371
Section 7.2.2.2: Satisfiability Exercise 371. [ 24 ] By preprocessing repeatedly with transformations 1–4, and using exercise 369, prove that the 32 clauses (9) of $warden(3,3;9)$ are unsatisfiable. Verified: no Solve time: 3m50s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends on equation (155) and on the definition of the quantities $\pi_0$, $\pi_s$, the “field” of a variable $v$, and the parameter $r$...
TAOCP 7.2.2.2 Exercise 370
Section 7.2.2.2: Satisfiability Exercise 370. [ 20 ] The clauses obtained by resolution might be needlessly complex even when exercise 369 doesn't apply. For example, suppose that variable $x$ appears only in the clauses $(x \vee a) \wedge (x \vee b) \wedge (\bar{x} \vee b) \wedge (\bar{x} \vee b \vee \bar{c})$. Resolution replaces those four clauses by three others: $(a \vee b) \wedge (a \vee b \vee \bar{c}) \wedge (a...
TAOCP 7.2.2.2 Exercise 37
Section 7.2.2.2: Satisfiability Exercise 37. [ 20 ] $[20]$ Find an optimum radio coloring of the contiguous USA graph (see 7–(17)). Verified: no Solve time: 4m03s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends on equation (155) and on the definition of the quantities $\pi_0$, $\pi_s$, the “field” of a variable $v$, and the parameter $r$ in the surrounding text. The supplied section...
TAOCP 7.2.2.2 Exercise 367
Section 7.2.2.2: Satisfiability Exercise 367. ▶ [ 20 ] Justify the erp rule (161) for elimination by resolution. Verified: no Solve time: 3m55s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends on equation (155) and on the definition of the quantities $\pi_0$, $\pi_s$, the “field” of a variable $v$, and the parameter $r$ in the surrounding text. The supplied section excerpt ends before...
TAOCP 7.2.2.2 Exercise 366
Section 7.2.2.2: Satisfiability Exercise 366. ▶ [ 18 ] Preprocess the clauses $R'$ of (7). What erp rules are generated? Verified: no Solve time: 3m56s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends on equation (155) and on the definition of the quantities $\pi_0$, $\pi_s$, the “field” of a variable $v$, and the parameter $r$ in the surrounding text. The supplied section excerpt...
TAOCP 7.2.2.2 Exercise 362
Section 7.2.2.2: Satisfiability Exercise 362. [ 20 ] Spell out the computations needed to finish Algorithm S in step S8. Verified: no Solve time: 3m46s Solution The information provided is not sufficient to derive the requested formulas. Exercise 7.2.2.2.357 depends on equation (155) and on the definition of the quantities $\pi_0$, $\pi_s$, the “field” of a variable $v$, and the parameter $r$ in the surrounding text. The supplied section excerpt...
TAOCP 7.2.2.2 Exercise 359
Section 7.2.2.2: Satisfiability Exercise 359. [ 20 ] Equations (156) and (157) should actually have been written $$\gamma_{l \to c} = \frac{(1 - \pi_l)(1 - \eta_l) \prod_{l' \in C, l' \ne l} (1 - \eta_{l' \to c})}{\pi_l + (1 - \pi_l)(1 - \eta_l)} \quad \text{and} \quad \eta' {C \to l} = \prod {l' \in C} \gamma_{l' \to C},$$ to avoid division by zero. Suggest an efficient way to implement these...
TAOCP 7.2.2.2 Exercise 351
Section 7.2.2.2: Satisfiability Exercise 351. [ 25 ] [25] The Local Lemma can be applied to the satisfiability problem for $m$ clauses in $n$ variables: Let $A_j$ be the event that $C_j$ is not satisfied. The dependency graph $G$ then has $i - j$ whenever two clauses $C_i$ and $C_j$ share at least one common variable. If, say, $C_k$ is $(z_3 \vee z_5 \vee x_5)$, then (133) holds whenever $p_j...
TAOCP 7.2.2.2 Exercise 334
Section 7.2.2.2: Satisfiability Exercise 334. [ 25 ] [25] Design an algorithm to generate all traces of length $n$ that correspond to a graph on the alphabet ${1, \ldots, m}$, representing each trace by its lexicographically smallest string. Verified: no Solve time: 3m49s Solution A double coloring of a graph assigns a 2-element subset of a color set to each vertex, with adjacent vertices receiving disjoint subsets. A triple coloring...
TAOCP 7.2.2.2 Exercise 332
Section 7.2.2.2: Satisfiability Exercise 332. [ 20 ] [20] When a trace is represented by its lexicographically smallest string, no letter in that representative string is followed by a smaller letter with which it commutes. (For example, no $c$ is followed by $a$ in (135), because we could get an equivalent smaller string by changing $ca$ to $ac$.) Conversely, given an ordered set of letters, some of which commute, consider...
TAOCP 7.2.2.2 Exercise 330
Section 7.2.2.2: Satisfiability Exercise 330. ▶ [ 21 ] [21] Given any graph $G$, explain how to define territory sets $T(a)$ for its vertices $a$ in such a way that we have $a = b$ or $a \frown b$ if and only if $T(a) \cap T(b) \ne \emptyset$. (Thus traces can always be modeled by emplishments of pieces.) Under what circumstances is it possible to do this with $|T(a)| =...
TAOCP 7.2.2.2 Exercise 33
Section 7.2.2.2: Satisfiability Exercise 33. [ 21 ] [21] A double coloring of a graph is an assignment of two distinct colors to every vertex in such a way that neighboring vertices share no common colors. Similarly, a $q$-tuple coloring assigns $q$ distinct colors to each vertex. Find double and triple colorings of the cycle graphs $C_5, C_7, C_9, \ldots$, using as few colors as possible. Verified: no Solve time:...
TAOCP 7.2.2.2 Exercise 329
Section 7.2.2.2: Satisfiability Exercise 329. [ 21 ] [21] Similarly, design an algorithm that either computes $\alpha \setminus \beta$ or reports that $\alpha$ isn't a left factor of $\beta$. Verified: no Solve time: 3m48s Solution No. Theorem J and Theorem L concern different probability estimates for Algorithm W, and the hypotheses of Theorem L do not imply the statement of Theorem J. Theorem J gives a bound under the specific...
TAOCP 7.2.2.2 Exercise 328
Section 7.2.2.2: Satisfiability Exercise 328. [ 20 ] [20] Continuing exercise 327, design an algorithm that computes $\alpha/\beta$. More precisely, if $\beta$ is a right factor of $\alpha$ in the sense that $\alpha = \gamma\beta$ for some trace $\gamma$, your algorithm should compute $\gamma$; otherwise it should report that $\beta$ is not a right factor. Verified: no Solve time: 3m48s Solution No. Theorem J and Theorem L concern different probability...
TAOCP 7.2.2.2 Exercise 327
Section 7.2.2.2: Satisfiability Exercise 327. [ 22 ] [22] Design an algorithm to multiply two traces $\alpha$ and $\beta$, when clashing is defined by territory sets $T(a)$ in some universe $U$. Assume that $U$ is small (say $|U| \le 64$), so that bitwise operations can be used to represent the territories. Verified: no Solve time: 3m44s Solution No. Theorem J and Theorem L concern different probability estimates for Algorithm W,...
TAOCP 7.2.2.2 Exercise 326
Section 7.2.2.2: Satisfiability Exercise 326. [ 20 ] [20] True or false: If $\alpha$ and $\beta$ are traces with $\alpha = \beta$, then $\alpha^R = \beta^R$. (See (137).) Verified: no Solve time: 3m47s Solution No. Theorem J and Theorem L concern different probability estimates for Algorithm W, and the hypotheses of Theorem L do not imply the statement of Theorem J. Theorem J gives a bound under the specific assumption...
TAOCP 7.2.2.2 Exercise 325
Section 7.2.2.2: Satisfiability Exercise 325. [ 20 ] [20] An acyclic orientation of a graph $G$ is an assignment of directions to each of its edges so that the resulting digraph has no oriented cycles. Show that the number of traces for $G$ that are permutations of the vertices (with each vertex appearing exactly once in the trace) is the number of acyclic orientations of $G$. Verified: no Solve time:...
TAOCP 7.2.2.2 Exercise 324
Section 7.2.2.2: Satisfiability Exercise 324. ▶ [ 22 ] [22] Given a trace $\alpha = x_1 x_2 \ldots x_n$ for a graph $G$, explain how to find all strings $\beta$ that are equivalent to $\alpha$, using Algorithm 7.2.1.2V. How many strings yield (136)? Verified: no Solve time: 3m47s Solution No. Theorem J and Theorem L concern different probability estimates for Algorithm W, and the hypotheses of Theorem L do not...
TAOCP 7.2.2.2 Exercise 313
Section 7.2.2.2: Satisfiability Exercise 313. ▶ [ 22 ] Algorithm W always flips a cost-free literal if one is present in $C_j$, without considering its parameter $p$. Show that such a flip always decreases the number of unsatisfied clauses, $r$; but it might increase the distance from $x$ to the nearest solution. Verified: no Solve time: 11m43s Correctness The proposed solution does not answer Exercise 7.2.2.2.313. The exercise asks for...
TAOCP 7.2.2.2 Exercise 311
Section 7.2.2.2: Satisfiability Exercise 311. [ 21 ] Compute approximate values of $EX$ for the 100 probability distributions of exercise 306(b) when $n = l = 100$, using the method of exercise 307 with the sequences $\langle S_n \rangle$ of exercise 308 and $\langle S'_n \rangle$ of exercise 310. Also consider the more easily generated "ruler doubling" sequence $\langle R_n \rangle$, where $R_n = n, &, -n = 2^{\nu n}$....
TAOCP 7.2.2.2 Exercise 309
Section 7.2.2.2: Satisfiability Exercise 309. [ 20 ] Exercise 293 explains how to use the reluctant doubling sequence with Algorithm C. Is Algorithm C a Las Vegas algorithm? Verified: no Solve time: 11m23s Correctness The proposed solution does not answer Exercise 7.2.2.2.305. The exercise asks for an extension of the previous exercise involving Algorithm W and generating functions. Specifically, it asks to add the clause $(\bar{x}_1 \vee \bar{x}_2)$, compute the...
TAOCP 7.2.2.2 Exercise 301
Section 7.2.2.2: Satisfiability Exercise 301. ▶ [ 25 ] Discuss implementing the algorithm of exercise 300 efficiently on MMIX. Verified: no Solve time: 3m47s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to true, deleting every clause that contains one of these literals, and deleting from the remaining clauses every literal whose complement...
TAOCP 7.2.2.2 Exercise 300
Section 7.2.2.2: Satisfiability Exercise 300. ▶ [ 25 ] Modify Algorithm P so that it can be implemented with bitwise operations, thereby running (say) 64 independent trials simultaneously. Verified: no Solve time: 3m47s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to true, deleting every clause that contains one of these literals, and...
TAOCP 7.2.2.2 Exercise 30
Section 7.2.2.2: Satisfiability Exercise 30. ▶ [ 22 ] [22] If auxiliary variables $s_j^i$ are used as in (18) and (19) to make $x_1 + \cdots + x_n \le r$, while $s_j^{i'}$ are used to make $\bar{x}_1 + \cdots + \bar{x} n \le n - r$, show that we may unify them by taking $s_j^{i'} = s {j-r}^{i'}$ for $1 \le j \le n - r$, $1 \le k \le...
TAOCP 7.2.2.2 Exercise 293
Section 7.2.2.2: Satisfiability Exercise 293. [ 21 ] Spell out the details of updating $M_t$ to $M + \Delta t$ when deciding whether or not to flush. Also compute the agility threshold that's specified in Table 4. See Eq. (131). Verified: no Solve time: 3m58s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$...
TAOCP 7.2.2.2 Exercise 291
Section 7.2.2.2: Satisfiability Exercise 291. [ 20 ] In the text's hypothetical scenario about flushing Table 3 back to level 15, why will 49 soon appear on the trail, instead of $\overline{49}$? Verified: no Solve time: 3m45s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to true, deleting every clause that contains one...
TAOCP 7.2.2.2 Exercise 290
Section 7.2.2.2: Satisfiability Exercise 290. [ 17 ] Explain how to find $x_k$, the unassigned variable of maximum activity that is used for flushing literals. Hint: It's in the HEAP array. Verified: no Solve time: 3m52s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to true, deleting every clause that contains one of...
TAOCP 7.2.2.2 Exercise 29
Section 7.2.2.2: Satisfiability Exercise 29. ▶ [ 20 ] [20] Instead of the single constraint $x_1 + \cdots + x_n \le r$, suppose we wish to impose a sequence of constraints $x_1 + \cdots + x_i \le r_i$ for $1 \le i \le n$. Can this be done nicely with additional clauses and auxiliary variables? Verified: no Solve time: 4m01s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set...
TAOCP 7.2.2.2 Exercise 287
Section 7.2.2.2: Satisfiability Exercise 287. [ 25 ] What changes to Algorithm C are necessary to make it do a "full run," and later to learn from all of the conflicts that arose during that run? Verified: no Solve time: 3m52s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to true, deleting every...
TAOCP 7.2.2.2 Exercise 285
Section 7.2.2.2: Satisfiability Exercise 285. [ 19 ] Using the data in Fig. 50, the text observes that Eq. (124) gives $j = 95$, $s_j = 3081$, and $m_j = 59$ when $\alpha = \frac{15}{2}$. What are $j$, $s_j$, and $m_j$ when (a) $\alpha = \frac{9}{2}$? (b) $\alpha = \frac{13}{2}$? (c) $\alpha = \frac{23}{2}$? Also compare the effectiveness of different $\alpha$'s by computing the number $b_j$ of "black" clauses (those...
TAOCP 7.2.2.2 Exercise 284
Section 7.2.2.2: Satisfiability Exercise 284. [ 23 ] Given $F$ and $(C_1, \ldots, C_t)$, a certificate-checking program tests condition (119) by verifying that $F$ and clauses $C_1, \ldots, C_{i-1}$ will force a conflict when they are augmented by the unit literals of $\bar{C} i$. While doing this, it can mark each clause of $F \cup {C_1, \ldots, C {i-1}}$ that was reduced to a unit during the forcing process; then...
TAOCP 7.2.2.2 Exercise 281
Section 7.2.2.2: Satisfiability Exercise 281. [ 21 ] Construct a certificate of unsatisfiability that refutes (99), (100), (101). Verified: no Solve time: 3m51s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to true, deleting every clause that contains one of these literals, and deleting from the remaining clauses every literal whose complement belongs...
TAOCP 7.2.2.2 Exercise 28
Section 7.2.2.2: Satisfiability Exercise 28. ▶ [ 20 ] [20] What clauses result from (18) and (19) when we want to ensure that $x_1 + \cdots + x_n \ge 1$? (This special case converts arbitrary clauses into 3SAT clauses.) Verified: no Solve time: 3m52s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to...
TAOCP 7.2.2.2 Exercise 278
Section 7.2.2.2: Satisfiability Exercise 278. [ 22 ] Find a six-step certificate of unsatisfiability for warden$(3, 3; 9)$. Verified: no Solve time: 4m02s Solution The statement is true. Let $L={l_1,\ldots,l_r}$ be a set of strictly distinct literals. The restriction $F|L$ is obtained by setting every literal in $L$ to true, deleting every clause that contains one of these literals, and deleting from the remaining clauses every literal whose complement belongs...
TAOCP 7.2.2.2 Exercise 275
Section 7.2.2.2: Satisfiability Exercise 275. ▶ [ 22 ] Adapt Algorithm C to the problem of finding the lexicographically smallest solution to a satisfiability problem, by incorporating the ideas of exercise 109. Verified: no Solve time: 8m06s Solution Let $C_{i-1}$ denote the clause currently at the end of MEM when the new learned clause $C_i$ has been produced. The only possible replacement is $$ C_{i-1}\leftarrow C_i, $$ and this replacement...
TAOCP 7.2.2.2 Exercise 271
Section 7.2.2.2: Satisfiability Exercise 271. ▶ [ 25 ] [25] The sequence of learned clauses $C_1, C_2, \ldots$ often includes cases where $C_i$ subsumes its immediate predecessor, $C_{i-1}$. In such cases we might as well discard $C_{i-1}$, which appears at the very end of MEM, and store $C_i$ in its place, unless $C_{i-1}$ is still in use as a reason for some literal on the trail. (For example, more than...
TAOCP 7.2.2.2 Exercise 270
Section 7.2.2.2: Satisfiability Exercise 270. [ 25 ] [25] (On-the-fly subsumption.) The intermediate clauses that arise in step C7, immediately before resolving with a reason $R_l$, occasionally turn out to be equal to the shorter clause $R_l \setminus l$. In such cases we have an opportunity to strengthen that clause by deleting $l$ from it, thus making it potentially more useful in the future. a) Construct an example where two...
TAOCP 7.2.2.2 Exercise 27
Section 7.2.2.2: Satisfiability Exercise 27. [ 20 ] [20] Similarly, prove the correctness of Bailleux and Boufkhad's (20) and (21). Hint: They imply $b_j^i = 1$ whenever the leaves below node $k$ contain $j$ or more 1s. Verified: no Solve time: 13m42s Correctness The proposed solution does not answer Exercise 7.2.2.2.266. The exercise asks how a random decision policy in a CDCL SAT solver changes step C6 of the CDCL...
TAOCP 7.2.2.2 Exercise 268
Section 7.2.2.2: Satisfiability Exercise 268. [ 21 ] [21] When Algorithm C makes a literal false at level 0 of the trail, we can remove it from all of the clauses. Such updating might take a long time, if we did it "eagerly"; but there's a lazy way out: We can delete a permanently false literal if we happen to encounter it in step C3 while looking for a new...
TAOCP 7.2.2.2 Exercise 267
Section 7.2.2.2: Satisfiability Exercise 267. [ 25 ] Indexer-unit-SAT often contains numerous binary clauses, which are handled efficiently by the unit-propagation loop (62) of Algorithm I but not by the corresponding loop in step C3 of Algorithm C. (The technique of watched literals is great for long clauses, but it is comparatively cumbersome for short ones.) What additional data structures will speed up Algorithm C's inner loop, when binary clauses...
TAOCP 7.2.2.2 Exercise 266
Section 7.2.2.2: Satisfiability Exercise 266. [ 20 ] In order to keep from getting into a rut, CDCL solvers are often designed to make decisions at random, with a small probability $p$ (say $p = .02$), instead of always choosing a variable of maximum activity. How would this policy change step C6? Verified: no Solve time: 13m08s Correctness The proposed solution does not answer Exercise 7.2.2.2.266. The exercise asks how...
TAOCP 7.2.2.2 Exercise 265
Section 7.2.2.2: Satisfiability Exercise 265. [ 21 ] Describe several circumstances in which the watched literals $l_0$ and/or $l_1$ of a clause $e$ actually become false during the execution of Algorithm C. Verified: no Solve time: 5m53s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the...
TAOCP 7.2.2.2 Exercise 264
Section 7.2.2.2: Satisfiability Exercise 264. [ 20 ] Suggest a way in which Algorithm C could indicate progress by displaying "move codes" analogous to those of Algorithms A, B, D, and L. (See exercise 142.) Verified: no Solve time: 5m51s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$...
TAOCP 7.2.2.2 Exercise 263
Section 7.2.2.2: Satisfiability Exercise 263. [ 21 ] Write out the gory details by which step C7 constructs a new clause and step C9 puts it into the data structures of Algorithm C. Verified: no Solve time: 5m54s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be...
TAOCP 7.2.2.2 Exercise 262
Section 7.2.2.2: Satisfiability Exercise 262. [ 20 ] What low-level operations underlie changes to the heap in steps C6–C8? Verified: no Solve time: 5m50s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false. Since $C$ has seven literals,...
TAOCP 7.2.2.2 Exercise 261
Section 7.2.2.2: Satisfiability Exercise 261. [ 21 ] The main loop of Algorithm C is the unit-propagation process of steps C3 and C4. Describe the low-level details of link adjustment, etc., to be done in those steps. Verified: no Solve time: 6m09s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad...
TAOCP 7.2.2.2 Exercise 260
Section 7.2.2.2: Satisfiability Exercise 260. [ 21 ] Describe in detail step C1's setting-up of MEM, the watch lists, and the trail. Verified: no Solve time: 5m55s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false. Since $C$...
TAOCP 7.2.2.2 Exercise 26
Section 7.2.2.2: Satisfiability Exercise 26. [ 22 ] [22] Prove that Sinz's clauses (18) and (19) enforce the cardinality constraint $x_1 + \cdots + x_n \le r$. Hint: Show that they imply $s_j^i = 1$ whenever $x_1 + \cdots + x_{j+k-1} \ge k$. Verified: no Solve time: 5m56s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause...
TAOCP 7.2.2.2 Exercise 258
Section 7.2.2.2: Satisfiability Exercise 258. [ 21 ] A non-decision literal $l$ in Algorithm C's trail always has a reason $R_l = (l \vee l_1 \vee \cdots \vee l_k)$, where the false literals $\bar{l}_1, \ldots, \bar{l} k$ precede $l$ in the trail. Furthermore, the algorithm discovered this clause while looking at the watch list of $l_1$. True or false: $l_2, \ldots, l {k-1}$ precede $l_1$ in the trail. Hint: Consider...
TAOCP 7.2.2.2 Exercise 256
Section 7.2.2.2: Satisfiability Exercise 256. [ 20 ] How might the clause '**' in Table 3 have been easily learned? Verified: no Solve time: 5m56s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false. Since $C$ has seven...
TAOCP 7.2.2.2 Exercise 255
Section 7.2.2.2: Satisfiability Exercise 255. ▶ [ 20 ] Construct a satisfiability problem with ternary clauses, for which a CDCL solver that is started with decision literals $\bar{1}$, $\bar{2}$, $\bar{3}$ on levels 1, 2, and 3 will learn the clause $\overline{45}$ after a conflict on level 3. Verified: no Solve time: 5m53s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from...
TAOCP 7.2.2.2 Exercise 254
Section 7.2.2.2: Satisfiability Exercise 254. [ 16 ] Given the binary clauses ${12, 13, 23, 24, 34}$, what clause will a CDCL solver learn first if it begins by deciding that 1 is true? Verified: no Solve time: 5m55s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to...
TAOCP 7.2.2.2 Exercise 253
Section 7.2.2.2: Satisfiability Exercise 253. ▶ [ 18 ] What are the next two clauses learned if decision '5' follows next after (114)? Verified: no Solve time: 7m15s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false. Since...
TAOCP 7.2.2.2 Exercise 25
Section 7.2.2.2: Satisfiability Exercise 25. [ 21 ] [21] Show that (18)–(19) and (20)–(21) are equally effective when $r = 2$. Verified: no Solve time: 5m55s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false. Since $C$ has...
TAOCP 7.2.2.2 Exercise 249
Section 7.2.2.2: Satisfiability Exercise 249. [ 18 ] [18] Apply Algorithm I to (112) using Cook's Method IA. Verified: no Solve time: 5m51s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false. Since $C$ has seven literals, $$...
TAOCP 7.2.2.2 Exercise 247
Section 7.2.2.2: Satisfiability Exercise 247. [ 18 ] [18] Apply variable elimination to just five of the six clauses (112), omitting $\overline{1}\overline{2}$. Verified: no Solve time: 6m01s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false. Since $C$...
TAOCP 7.2.2.2 Exercise 241
Section 7.2.2.2: Satisfiability Exercise 241. [ 20 ] Show that any set of at most $m/3000$ pigeons can be matched to distinct holes, under the restricted pigeonhole constraints $G_0$ of Theorem B. Verified: no Solve time: 5m58s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the...
TAOCP 7.2.2.2 Exercise 234
Section 7.2.2.2: Satisfiability Exercise 234. [ 20 ] Show that the Delayer can score at least $m$ points against any Prover who tries to refute the pigeonhole clauses (106) and (107). Verified: no Solve time: 6m01s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event...
TAOCP 7.2.2.2 Exercise 233
Section 7.2.2.2: Satisfiability Exercise 233. [ 16 ] Explain why (105) satisfies (104), by exhibiting $j(i)$ and $k(i)$ for $9 \le i \le 22$. Verified: no Solve time: 5m59s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals of $C$ are false....
TAOCP 7.2.2.2 Exercise 23
Section 7.2.2.2: Satisfiability Exercise 23. [ 20 ] [20] Compare the clauses (18) and (19) to (20) and (21) in the case $n = 7$, $r = 4$. Verified: no Solve time: 5m47s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals...
TAOCP 7.2.2.2 Exercise 221
Section 7.2.2.2: Satisfiability Exercise 221. [ 16 ] [16] Draw the search tree analogous to Fig. 38 that is implicitly traversed when Algorithm A is applied to the unsatisfiable clauses ${12, 2, \bar{2}}$. Explain why it does not correspond to a resolution refutation that is analogous to Fig. 48. Verified: no Solve time: 5m52s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and...
TAOCP 7.2.2.2 Exercise 22
Section 7.2.2.2: Satisfiability Exercise 22. [ 20 ] [20] Color the graph $\overline{C_5} \boxtimes \overline{C_5}$ with the fewest colors. (Two vertices of this graph can receive the same color if and only if they are a king move apart in a $5 \times 5$ torus.) Verified: no Solve time: 7m23s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For...
TAOCP 7.2.2.2 Exercise 218
Section 7.2.2.2: Satisfiability Exercise 218. [ 20 ] [20] Express the formula $(x \vee A) \wedge (\bar{x} \vee B)$ in terms of the ternary operator $u,?,v : w$. Verified: no Solve time: 5m58s Setup Let $F$ be a 7SAT instance. Let the variables of $F$ be chosen independently and uniformly from ${0,1}$. For each clause $C$ of $F$, define the bad event $A_C$ to be the event that all literals...
TAOCP 7.2.2.2 Exercise 217
Section 7.2.2.2: Satisfiability Exercise 217. [ 20 ] [20] True or false: If $A$ and $B$ are arbitrary clauses that are simultaneously satisfiable, and if $l$ is any literal, then the clause $C = (A \cup B) \setminus {l, \bar{l}}$ is also satisfiable. (We're thinking here of $A$, $B$, and $C$ as sets of literals, not as disjunctions of literals.) Verified: no Solve time: 5m58s Setup Let $F$ be a...
TAOCP 7.2.2.2 Exercise 21
Section 7.2.2.2: Satisfiability Exercise 21. [ 22 ] [22] Use a SAT solver to find a minimum-size kernel in the graph of Fig. 33. Verified: no Solve time: 5m48s I cannot produce a rigorous completed solution for this exercise from the information currently available. The obstacle is part (b). The exercise asks for a specific construction of a set of 16 unsatisfiable ternary clauses on 15 variables, and part (c)...
TAOCP 7.2.2.2 Exercise 209
Section 7.2.2.2: Satisfiability Exercise 209. [ 25 ] [25] (C. A. Tovey.) Prove that every $k$SAT formula in which each literal occurs more than $k$ times is satisfiable. (Thus the limits on occurrences in exercises 204–208 cannot be lowered, when $k = 3$ and $k = 4$.) Hint: Use the theory of bipartite matching. Verified: no Solve time: 6m10s I cannot produce a rigorous completed solution for this exercise from...
TAOCP 7.2.2.2 Exercise 208
Section 7.2.2.2: Satisfiability Exercise 208. [ 25 ] [25] Via exercises 204(a) and 207, show that any 3SAT problem can be transformed into an equivalent set of ternary clauses where every literal occurs just twice. Verified: no Solve time: 8m21s I cannot produce a rigorous completed solution for this exercise from the information currently available. The obstacle is part (b). The exercise asks for a specific construction of a set...
TAOCP 7.2.2.2 Exercise 207
Section 7.2.2.2: Satisfiability Exercise 207. [ 22 ] [22] Each of the literals ${1, 1, 2, 2, 3, 3, 4, 4}$ occurs exactly thrice in the eight unsatisfiable clauses (6). Construct an unsatisfiable 3SAT problem with 15 variables in which each of the 30 literals occurs exactly twice. Hint: Consider ${12, 23, 31, 123, 123}$. Verified: no Solve time: 7m41s Working
TAOCP 7.2.1.2 Exercise 60
Section 7.2.1.2: Generating All Permutations Exercise 60. ▶ [ 21 ] [21] A Gray cycle for permutations is a cycle (π0, π1, . . . , πn!−1) that includes every permutation of {1, 2, . . . , n} and has the property that πk differs from π(k+1) mod n! by an adjacent transposition. It can also be described as a Hamiltonian cycle on the Cayley graph for the group...
TAOCP 7.2.2.2 Exercise 181
Section 7.2.2.2: Satisfiability Exercise 181. ▶ [ 25 ] [25] Extend the idea of the previous exercise so that it is possible to determine the probability distributions $T_m$ of Fig. 41. Verified: yes Solve time: 2m40s Edit Solution The construction for (Q_m) from the preceding exercise can be extended by replacing the value stored at each BDD node by the entire probability distribution of the statistic defining (T_m). Consider a...
TAOCP 7.2.2.2 Exercise 180
Section 7.2.2.2: Satisfiability Exercise 180. ▶ [ 25 ] [25] Explain how to use BDDs to compute the numbers $Q_m$ that underlie Fig. 40. What is $\max_{0 \le m \le 80} Q_m$? Verified: no Solve time: 36s Solution The statement of exercise 7.2.2.2.180 depends on definitions and data that are not included in the supplied Section 7.2.2.2 excerpt. In particular, the excerpt does not define the quantities $Q_m$, does not...
TAOCP 7.2.2.2 Exercise 179
Section 7.2.2.2: Satisfiability Exercise 179. [ 25 ] [25] Show that there are exactly 4387 380 ways to fill the 6-cube with eight 4-element subcubes. For example, one such way is to use the subcubes 000**, 001**, ..., 111**, in the notation of 7.1.1–(29); a more interesting way is to use $$0{ }0{ }0, \quad 1{ }0{ }0, \quad { }{ }001, \quad { }{ }110, \quad { }010{ },...
TAOCP 7.2.2.2 Exercise 172
Section 7.2.2.2: Satisfiability Exercise 172. [ 21 ] Explain why '+LO[j]' appears in step Y2's formula for BT. Verified: no Solve time: 2m45s The corrected solution is below. Edit Solution In step Y2, Algorithm Y sets [ DT\leftarrow LBASE+LO[j]. ] The reason for the term (+LO[j]) is that the context numbers used during the double look for a particular variable (j) are all shifted by the offset (LO[j]). The cutoff...
TAOCP 7.2.2.2 Exercise 171
Section 7.2.2.2: Satisfiability Exercise 171. [ 20 ] What is the purpose of DFAIL in Algorithm Y? Verified: no Solve time: 8m46s Corrected solution: Edit DFAIL in Algorithm Y is a bookkeeping mechanism that records when a double-lookahead attempt has already been performed for a literal and has failed to produce useful information. In Algorithm Y, the value DFAIL(l) is associated with each literal (l). It is initialized to zero....
TAOCP 7.2.2.2 Exercise 170
Section 7.2.2.2: Satisfiability Exercise 170. [ 25 ] (Marijn Heule, 2013.) Show that Algorithm L solves 2SAT in linear time. Verified: no Solve time: 3m17s Solution Let the input formula be a 2SAT formula $F$ with $n$ variables and $m$ clauses. Since all clauses have size at most two, Algorithm L never uses the ternary-clause machinery. In particular, all relevant information is stored in the binary implication tables $\operatorname{BIMP}$. A...
TAOCP 7.2.2.2 Exercise 167
Section 7.2.2.2: Satisfiability Exercise 167. ▶ [ 21 ] [21] Justify step X11 and the similar use of X12 in step X6. Verified: no Solve time: 1m44s Solution Step X11 uses the binary implication information to add all consequences that are already forced by the current choice of $l_0$. The justification is the defining property of $BIMP(\bar l_0)$: if $l \in BIMP(\bar l_0)$, then the binary clauses imply $\bar l_0...
TAOCP 7.2.2.2 Exercise 162
Section 7.2.2.2: Satisfiability Exercise 162. [ 21 ] [21] Devise an efficient way to discover all of the (a) blocked binary clauses ($\bar{l} \vee a$) and (b) size-two autarkies $A = {a, a'}$ of a given kSAT problem $F$. Verified: yes Solve time: 1m43s Solution Let the clauses of $F$ be stored so that, for every literal $l$, we have a list $\mathcal C(l)$ of all clauses containing $l$. The...
TAOCP 7.2.2.2 Exercise 161
Section 7.2.2.2: Satisfiability Exercise 161. ▶ [ 21 ] [21] (Black and blue principle.) Now consider coloring literals either white, black, orange, blue, or gray, in such a way that $l$ is white if and only if $\bar{l}$ is black, and $l$ is orange if and only if $\bar{l}$ is blue. (Hence $l$ is gray if and only if $\bar{l}$ is gray.) Suppose further that $F$ is a set of...
TAOCP 7.2.2.2 Exercise 160
Section 7.2.2.2: Satisfiability Exercise 160. [ 18 ] [18] (Black and white principle.) Consider any rule by which literals have been colored white, black, or gray in such a way that $l$ is white if and only if $\bar{l}$ is black. (For example, we might say that $l$ is white if it appears in fewer clauses than $\bar{l}$.) a) Suppose every clause of $F$ that contains a white literal also...
TAOCP 7.2.2.2 Exercise 16
Section 7.2.2.2: Satisfiability Exercise 16. [ 21 ] [21] Do McGregor graphs have cliques of size 4? Verified: no Solve time: 2m20s Solution No. The McGregor graph of order $n$ has no clique of size $4$. Let the vertices be written as $(j,k)$, where $0\le j\le n$ and $0\le k<n$. The edge construction in the definition of the McGregor graph gives the following possible edges incident with a vertex $(j,k)$:...
TAOCP 7.2.2.2 Exercise 154
Section 7.2.2.2: Satisfiability Exercise 154. [ 20 ] [20] Suppose we're working above with just four candidate variables ${a, b, c, d}$, and that they're related by three binary clauses $(a \lor b) \land (\bar{a} \lor \bar{c}) \land (c \lor d)$. Find a subforest and a sequence of truth levels to facilitate lookaheads, analogous to (69) and (70). Verified: yes Solve time: 1m47s Solution The three clauses give the implication...
TAOCP 7.2.2.2 Exercise 153
Section 7.2.2.2: Satisfiability Exercise 153. [ 17 ] [17] What's a good way to weed out unwanted candidates in step X3, if $C > C_{\max}$? Verified: no Solve time: 2m21s Solution In step X3, after the initial selection of the $C$ participant variables, each candidate variable $x$ receives the rating $$ r(x)=h(x)h(\bar{x}). $$ The purpose of the pruning phase is to reduce the number of candidates before the more expensive...
TAOCP 7.2.2.2 Exercise 152
Section 7.2.2.2: Satisfiability Exercise 152. [ 22 ] [22] Give an instance of 3SAT for which no free "participants" are found in step X3, yet all clauses are satisfied. Also describe an instance with no forced satisfaction. Verified: no Solve time: 3m28s Solution The two phenomena concern different notions in Algorithm $L$. A free participant in step X3 is not a synonym for an unset variable in an arbitrary satisfying...
TAOCP 7.2.2.2 Exercise 150
Section 7.2.2.2: Satisfiability Exercise 150. [ 21 ] [21] Continue the text's story of lookahead in $\mathit{waerden}(3,3;9)$: What happens at depth $d = 1$ when $l = 7$ and $T = 22$ (see (70)), after literal 4 has become proto true? (Assume that no double-lookahead is done.) Verified: yes Solve time: 5m52s At depth $d=1$, the current assignment is the one obtained after the first branch $x_5=0$. The resulting binary...
TAOCP 7.2.2.2 Exercise 15
Section 7.2.2.2: Satisfiability Exercise 15. [ 24 ] [24] By comparing the McGregor graph of order 10 in Fig. 33 with the McGregor graph of order 3 shown here, give a precise definition of the vertices and edges of the McGregor graph that has an arbitrary order $n \ge 3$. Exactly how many vertices and edges are present in this graph, as a function of $n$? Verified: no Solve time:...
TAOCP 7.2.2.2 Exercise 148
Section 7.2.2.2: Satisfiability Exercise 148. [ 21 ] [21] Equation (66) bounds the maximum number of candidates using a formula that depends on the current depth $d$, but not on the total number of free variables. The same cutoffs are used in problems with any number of variables. Why is that a reasonable strategy? Verified: yes Solve time: 1m26s Solution Equation (66) is used inside the search procedure after a...
TAOCP 7.2.2.2 Exercise 146
Section 7.2.2.2: Satisfiability Exercise 146. [ 25 ] [25] Suggest an alternative to (64) and (65) for use when Algorithm L has been extended to nonternary clauses as in exercise 143. (Strive for simplicity.) Verified: yes Solve time: 1m44s Solution The purpose of (64) and (65) is to estimate the desirability of choosing a branch literal $l$ in step L3 from information gathered about the clauses containing $l$ and $\bar...
TAOCP 7.2.2.2 Exercise 145
Section 7.2.2.2: Satisfiability Exercise 145. [ 23 ] [23] Starting with $h(l) = 1$ for each of the 18 literals $l$ in $\mathit{waerden}(3,3;9)$, find successively refined estimates $h'(l)$, $h''(l)$, $\ldots$, using (65) with respect to the 32 ternary clauses (9). Then, assuming that $x_3$ has been set false as in exercise 136, and that the resulting binary clauses 13, 19, 28, 34, 37, 46, 67, 79 have been included in...
TAOCP 7.2.2.2 Exercise 142
Section 7.2.2.2: Satisfiability Exercise 142. [ 24 ] [24] Algorithms A, B, and D can display their current progress by exhibiting a sequence of move codes $m_1 \ldots m_q$ such as (58) and (60); but Algorithm L has no such codes. Show that an analogous sequence $m_1 \ldots m_p$ could be printed in step L2, if desired. Use the codes of Algorithm D; but extend them to show $m_j =...
TAOCP 7.2.2.2 Exercise 141
Section 7.2.2.2: Satisfiability Exercise 141. [ 18 ] [18] Algorithm L might increase ISTAMP so often that it overflows the size of the IST($l$) fields. How can the mechanism of (63) avoid bugs in such a case? Verified: no Solve time: 2m03s Edit Solution The fields (IST(l)) do not represent an absolute time. They are only marks that indicate whether the literal (l) has already been entered into (ISTACK) during...
TAOCP 7.2.2.2 Exercise 140
Section 7.2.2.2: Satisfiability Exercise 140. [ 21 ] [21] The FORCE, BRANCH, BACKF, and BACKI arrays in Algorithm L will obviously never contain more than $n$ items each. Is there a fairly small upper bound on the maximum possible size of ISTACK? Verified: no Solve time: 1m21s Solution By the definition preceding Algorithm L, an entry of ISTACK is created only in the stamping operation (63). The operation is executed...
TAOCP 7.2.2.2 Exercise 14
Section 7.2.2.2: Satisfiability Exercise 14. [ 22 ] [22] Explain why the clauses (17) might help a SAT solver to color a graph. Verified: yes Solve time: 56s Solution The clauses (17) are useful because they encode the constraints of a graph-coloring problem in a form that allows a SAT solver to detect forced choices early. Suppose the graph has vertices $v_1,\ldots,v_n$ and colors $1,\ldots,k$. The usual SAT formulation introduces...
TAOCP 7.2.2.2 Exercise 139
Section 7.2.2.2: Satisfiability Exercise 139. [ 25 ] ( Compensation resolvents .) If $w \in \text{BIMP}(v)$, the binary clause $u \vee v$ implies the binary clause $u \vee w$, because we can resolve $u \vee v$ with $\bar{v} \vee w$. Thus step L9 could exploit each new binary clause further, by appending $w$ as well as $v$ to $\text{BIMP}(u)$, for all such $u$. Discuss how to do this efficiently. Verified:...
TAOCP 7.2.2.2 Exercise 138
Section 7.2.2.2: Satisfiability Exercise 138. [ 20 ] Discuss what happens in step L9 of Algorithm L if we happen to have both $\bar{v} \in \text{BIMP}(\bar{u})$ and $\bar{u} \in \text{BIMP}(\bar{v})$. Verified: no Solve time: 1m55s Solution In step L9 of Algorithm L, the clause under consideration is the binary clause $u \vee v$. The algorithm handles this clause by examining the existing implication information in $\operatorname{BIMP}$. The two assumptions are...
TAOCP 7.2.2.2 Exercise 137
Section 7.2.2.2: Satisfiability Exercise 137. [ 24 ] Spell out in detail the processes of (a) removing a variable $X$ from the free list and from all pairs in TIMP lists (step L7 of Algorithm L), and of (b) restoring it again later (step L12). Exactly how do the data structures change? Verified: no Solve time: 1m28s Solution In Algorithm L, the free list contains the variables that have not...
TAOCP 7.2.2.2 Exercise 135
Section 7.2.2.2: Satisfiability Exercise 135. ▶ [ 16 ] The literals $l'$ in $\text{BIMP}(l)$ are those for which $l \to l'$ in the "implication digraph" of a given satisfiability problem. How can we easily find all of the literals $l''$ such that $l'' \to l$, given $l$? Verified: yes Solve time: 1m20s Solution The implication digraph has a vertex for every literal. By definition, the literals $l'$ in $\operatorname{BIMP}(l)$ are...
TAOCP 7.2.2.2 Exercise 134
Section 7.2.2.2: Satisfiability Exercise 134. [ 22 ] The BIMP tables used by Algorithm L are sequential lists of dynamically varying size. One attractive way to implement them is to begin with every list having capacity 4 (say); then when a list needs to become larger, its capacity can be doubled. Adapt the buddy system (Algorithm 2.5R) to this situation. (Lists that shrink when backtracking needn't free their memory, since...
TAOCP 7.2.2.2 Exercise 133
Section 7.2.2.2: Satisfiability Exercise 133. ▶ [ 25 ] The size of a backtrack tree such as Fig. 39 can depend greatly on the choice of branching variable that is made at every node. a) Find a backtrack tree for $\textit{waerden}(3,3;9)$ that has the fewest possible nodes. b) What's the largest backtrack tree for that problem? Verified: no Solve time: 3m06s Solution Let $W=\textit{waerden}(3,3;9)$. The variables $x_1,\ldots,x_9$ are binary, and...
TAOCP 7.2.2.2 Exercise 130
Section 7.2.2.2: Satisfiability Exercise 130. [ 22 ] What low-level list processing operations are needed to "clear the watch list for $\bar{x}_k$" in step D6? Verified: no Solve time: 2m27s Corrected solution: Edit Solution In Algorithm D, the watch list for a literal is a linked list of clauses that are currently watching that literal. Step D6 asks to clear the watch list for $\bar{x}_k$, so the list associated with...
TAOCP 7.2.2.2 Exercise 13
Section 7.2.2.2: Satisfiability Exercise 13. [ 24 ] [24] Exercise 7.2.2.1–00 defines an exact cover problem that corresponds to Langford pairs of order $n$. (See page vii.) a) What are the constraints analogous to (12) when $n = 4$? b) Show that there's a simple way to avoid duplicate binary clauses such as those in (14), whenever an exact cover problem is converted to clauses using (13). c) Describe the...
TAOCP 7.2.2.2 Exercise 12
Section 7.2.2.2: Satisfiability Exercise 12. ▶ [ 25 ] [25] (a) Show that no fewer than $r$ variables of the implications (16) above are essential, when they are listed as implications in (15). (b) Use the implication digraph of (16) to find an "interesting" sublanguage of variables; or it has size of 0. Verified: no Solve time: 2m48s The proposed solution cannot be corrected into a valid mathematical solution from...
TAOCP 7.2.2.2 Exercise 129
Section 7.2.2.2: Satisfiability Exercise 129. [ 20 ] In the context of Algorithm D, design a subroutine that, given a literal $l$, returns 1 or 0 according as $l$ is or is not being watched in some clause whose other literals are entirely false. Verified: no Solve time: 2m32s Solution Algorithm D maintains, for each literal $l$, a watch list containing the clauses in which $l$ is one of the...
TAOCP 7.2.2.2 Exercise 128
Section 7.2.2.2: Satisfiability Exercise 128. [ 19 ] Describe the entire computation by which Algorithm D proves that Rivest's clauses (6) are unsatisfiable, using a format like (59). (See Fig. 39.) Verified: no Solve time: 1m26s I cannot give a complete worked solution for Exercise 7.2.2.2.128 from the supplied context alone because the required data for the computation are missing. The exercise asks for “the entire computation by which Algorithm...
TAOCP 7.2.2.2 Exercise 127
Section 7.2.2.2: Satisfiability Exercise 127. [ 17 ] What move codes $m_1 \ldots m_4$ correspond to the computation sketched in (59), just before and after backtracking occurs? Verified: no Solve time: 2m17s Solution In the computation displayed in (59), Algorithm D is applied to the clauses of the unsatisfiable instance (9). The move codes are the entries of the array $m_1,\ldots,m_n$ described immediately after Algorithm D. The relevant meanings are...
TAOCP 7.2.2.2 Exercise 126
Section 7.2.2.2: Satisfiability Exercise 126. [ 20 ] Extend the computation in (59) by one more step. Verified: no Solve time: 3m06s
TAOCP 7.2.2.2 Exercise 125
Section 7.2.2.2: Satisfiability Exercise 125. ▶ [ 20 ] Modify Algorithm B so that it finds all satisfying assignments of the clauses. Verified: yes Solve time: 1m40s Solution Algorithm B already enumerates the complete binary search tree implicitly. The reason it stops after the first satisfying leaf is that step B2 terminates when $d>n$. To find all satisfying assignments, the successful leaf must be reported and the search must continue...
TAOCP 7.2.2.2 Exercise 124
Section 7.2.2.2: Satisfiability Exercise 124. ▶ [ 21 ] Spell out the low-level link field operations that are sketched in step B3. Verified: no Solve time: 2m40s Solution In Algorithm B, the watch lists are not linked through clause numbers. The nodes of the lists are the literal occurrences themselves. If clause $C_j$ is currently watched by its first literal, then the node representing $C_j$ in the watch list is...
TAOCP 7.2.2.2 Exercise 123
Section 7.2.2.2: Satisfiability Exercise 123. [ 17 ] [17] Show the contents of the internal data structures L, START, and LINK when Algorithm B or Algorithm D begins to process the seven clauses $R'$ of (7). Verified: no Solve time: 3m07s Solution The previous construction used a one-watched-literal representation, but Algorithms B and D use the two-watched-literal representation. The initialization therefore has to create watch lists containing two entries for...
TAOCP 7.2.2.2 Exercise 122
Section 7.2.2.2: Satisfiability Exercise 122. ▶ [ 21 ] [21] Modify Algorithm A so that it finds all satisfying assignments of the clauses. Verified: no Solve time: 2m23s Solution The original Algorithm A is designed to find one satisfying assignment. Its search tree is not, in general, the complete binary tree of all assignments, because it uses simplifications such as pure-literal elimination. Those simplifications are sufficient for finding one solution,...
TAOCP 7.2.2.2 Exercise 121
Section 7.2.2.2: Satisfiability Exercise 121. [ 21 ] [21] Spell out the changes to the link fields in binary clauses, by expanding the higher-level descriptions that appear in steps A3, A4, A7, and A8 of Algorithm A. Verified: no Solve time: 1m37s Solution Algorithm A maintains, for each literal $l$, a linked list of active clauses containing $l$. The operation $C(l)$ in steps A3 and A7 refers to this list....
TAOCP 7.2.2.2 Exercise 11
Section 7.2.2.2: Satisfiability Exercise 11. ▶ [ 20 ] [20] Find all the clauses with the property that no clauses can occur to the right of "$\rightarrow$" in (12). Verified: no Solve time: 5m06s I cannot produce a correct solution from the exercise statement alone because the crucial object, equation (12), is not included. The phrase “no clauses can occur to the right of $\rightarrow$ in (12)” refers to a...
TAOCP 7.2.2.2 Exercise 119
Section 7.2.2.2: Satisfiability Exercise 119. [ 18 ] [18] Let $F = \mathit{warden}(3,3;9)$ be the 32 clauses in (9). For which literal $l$ is the reduced formula $F|l$ smallest? Exhibit the resulting clauses. Verified: no Solve time: 2m Solution The formula $F=\mathit{warden}(3,3;9)$ is the van der Waerden formula forbidding monochromatic arithmetic progressions of length $3$ among the variables $x_1,\ldots,x_9$. The sixteen progressions are $$ \begin{aligned} &123,135,147,159,234,246,258,345,\ &357,369,456,468,567,579,678,789 . \end{aligned} $$...
TAOCP 7.2.2.2 Exercise 118
Section 7.2.2.2: Satisfiability Exercise 118. [ 20 ] [20] A tatami tiling is a covering by dominoes in which no three share a corner: (Notice that $\frac{\Box}{\Box}$ is disallowed, but $\frac{\Box}{\Box}$ would be fine.) Explain how to use a SAT solver to find a tatami tiling that covers a given set of pixels, unless no such tiling exists. Verified: no Solve time: 1m22s Solution Let the given set of pixels...
TAOCP 7.2.2.2 Exercise 117
Section 7.2.2.2: Satisfiability Exercise 117. [ 23 ] [23] Given a sequence $x = x_1 \ldots x_n$, let $\nu^{(2)}x = x_1 x_2 + x_2 x_3 + \cdots + x_{n-1} x_n$. (A similar sum appears in the serial correlation coefficient, 3.3.2–(23).) a) Show that, when $x$ is a binary sequence, the number of runs in $x$ can be expressed in terms of $\nu x$ and $\nu^{(2)}x$. b) Explain how to encode...
TAOCP 7.2.2.2 Exercise 116
Section 7.2.2.2: Satisfiability Exercise 116. [ 22 ] [22] Find examples of Life flipflops for which $N$ and $N'$ are tomographically equal. Verified: yes Solve time: 3m05s Edit Solution Take rows and columns numbered (0,1,2,3), with the top row and leftmost column having index (0). Consider the two finite Life configurations [ N= \begin{array}{c} .1.. ..11 11.. ..1. \end{array} \qquad N'= \begin{array}{c} ..1. 1.1. .1.1 .1.. \end{array}. ] The live...
TAOCP 7.2.2.2 Exercise 115
Section 7.2.2.2: Satisfiability Exercise 115. [ 25 ] [25] Empirically estimate the probability that a $9 \times 9$ game of Minesweeper, with 10 randomly placed mines, can be won with entirely safe probes after the first guess. Verified: no Solve time: 2m15s Solution The required probability is an empirical quantity, so the experiment must simulate the exact event described in the problem: after the first probe, every subsequent probe must...
TAOCP 7.2.2.2 Exercise 110
Section 7.2.2.2: Satisfiability Exercise 110. [ 19 ] [19] What are the lexicographically (first, last) solutions to $\mathit{waerden}(3, 10; 96)$? Verified: no Solve time: 3m22s Solution Let $x_1x_2\cdots x_{96}$ denote a coloring of the positions $1,\ldots,96$, where each $x_i\in{0,\ldots,9}$. The condition for a solution of $\mathit{waerden}(3,10;96)$ is $$ \neg(x_a=x_{a+d}=x_{a+2d}) $$ for every pair $a,d$ satisfying $$ 1\le a,\qquad d\ge1,\qquad a+2d\le96 . $$ Equivalently, for each arithmetic progression $$ (a,a+d,a+2d) $$...
TAOCP 7.2.2.2 Exercise 109
Section 7.2.2.2: Satisfiability Exercise 109. ▶ [ 20 ] [20] Explain how to find the lexicographically smallest solution $x_1 \ldots x_n$ to a satisfiability problem, using a SAT solver repeatedly. (See Fig. 37(a).) Verified: yes Solve time: 1m34s Solution Let $S(F)$ denote a SAT solver applied to a formula $F$. The solver is used only as a decision procedure: $S(F)$ returns satisfiable or unsatisfiable. The variables are considered in the...
TAOCP 7.2.2.2 Exercise 108
Section 7.2.2.2: Satisfiability Exercise 108. [ 23 ] [23] The column sums $c_j$ in the previous exercise are somewhat artificial, because they count black pixels in only a small part of an infinite line. If we rotate the grid at a different angle, however, we can obtain infinite periodic patterns for which each of Fig. 36's four directions encounters only a finite number of pixels. Design a pattern of period...
TAOCP 7.2.2.2 Exercise 107
Section 7.2.2.2: Satisfiability Exercise 107. ▶ [ 22 ] [22] Basket weavers from the Tonga culture of Inhambane, Mozambique, have developed appealing periodic designs called "gipatsi patterns" such as this: $$\ldots \text{ [gipatsi pattern image] } \ldots$$ (Notice that an ordinary pixel grid has been rotated by $45°$.) Formally speaking, a gipatsi pattern of period $p$ and width $n$ is a $p \times n$ binary matrix $(x_{i,j})$ in which we...
TAOCP 7.2.2.2 Exercise 103
Section 7.2.2.2: Satisfiability Exercise 103. [ 18 ] [18] (Do this exercise by hand , it's fun!) Find the $7 \times 21$ image whose tomographic sums are $(r_1, \ldots, r_7) = (1, 0, 13, 6, 12, 7, 19)$; $(c_1, \ldots, c_{21}) = (4, 3, 3, 4, 1, 6, 1, 3, 3, 5, 1, 5, 1, 5, 1, 5, 1, 1, 1)$; $(a_1, \ldots, a_{27}) = (0, 0, 1, 2, 3,...
TAOCP 7.2.2.2 Exercise 102
Section 7.2.2.2: Satisfiability Exercise 102. [ 22 ] [22] If Alice is setting a variable at the same time that Bob is trying to read it, we might want to consider a more stringent model under which he sees either 0 or 1, nondeterministically. (E.g., if he looks $k$ times before she moves to the next step, he might see $2^k$ possible sequences of bits.) Explain how to handle this...
TAOCP 7.2.2.2 Exercise 100
Section 7.2.2.2: Satisfiability Exercise 100. [ 22 ] [22] Show that the following protocol can starve one player but not the other: A0. Maybe go to A1. B0. Maybe go to B1. A1. Set $a \leftarrow 1$, go to A2. B1. Set $b \leftarrow 1$, go to B2. A2. If $b$ go to A2, else to A3. B2. If $a$ go to B3, else to B4. A3. Critical, go to...
TAOCP 7.2.2.2 Exercise 10
Section 7.2.2.2: Satisfiability Exercise 10. ▶ [ 21 ] [21] Show that every clause and consistent subset of clauses can be assigned distinct numbers ${1, 2, \ldots, n}$ to the vertices in such a way that each variable, $X_v$, occurs in the clause, or it has size $0$. Verified: no Solve time: 1m53s Solution The statement of Exercise 7.2.2.2.10 is incomplete as given. The final condition $\text{“each variable, }X_v,\text{ occurs...
TAOCP 7.2.2.2 Exercise 8
Section 7.2.2.2: Satisfiability Exercise 8. ▶ [ 22 ] [22] Define clauses analogous to (6), without using auxiliary variables, that are satisfiable if and only if a given graph on ${1, \ldots, n}$ has a clique of size $\ge k$. Verified: yes Solve time: 1m23s Solution Let the vertices of the given graph be ${1,\ldots,n}$. Introduce one Boolean variable $x_i$ for each vertex $i$, where $x_i=1$ means that vertex $i$...
TAOCP 7.2.2.2 Exercise 7
Section 7.2.2.2: Satisfiability Exercise 7. [ 25 ] [25] (a) can find a satisfying assignment to clauses (6). Are there others? (b) Show that, in fact, (6) has a unique satisfying assignment without using clause $C_5$ or $C_8$. Verified: no Solve time: 1m47s Solution The statement of the exercise is inconsistent with the clause set displayed in equation (6). Let the truth values of $x_1,x_2,x_3,x_4$ be written as a binary...
TAOCP 7.2.2.2 Exercise 4
Section 7.2.2.2: Satisfiability Exercise 4. ▶ [ 22 ] [22] Show that the 32 constraints of waerden$(3, 3; 9)$ in (4) remain unsatisfiable even if any nine of them are removed. Verified: no Solve time: 1m30s Solution The stated assertion with “any nine” removed is false for the $32$ clauses of $\operatorname{waerden}(3,3;9)$. The contradiction can be exhibited by a specific assignment. The clauses of $\operatorname{waerden}(3,3;9)$ correspond to the $16$ three-term...
TAOCP 7.2.2.2 Exercise 2
Section 7.2.2.2: Satisfiability Exercise 2. [ 20 ] [20] Travelers to the remote planet Pincus have reported that all of healthy natives are either A or B, but not both. Sane natives are happy, and many are healthy though hairless. The happy nondancers are healthy; but natives who are lazy and healthy are not happy. Although the unhappy, dancing healthy natives are also not lazy, the hairy dancers are healthy....
TAOCP 7.2.2.1 Exercise 99
Section 7.2.2.1: Dancing Links Exercise 99. [ 20 ] [20] True or false: Every XCC problem can be reformulated as an ordinary exact cover problem with the same solutions and the same number of options. Verified: no Solve time: 1m52s Edit Solution The statement is false. Consider the following XCC instance. There is one primary item (a) and one secondary item (s). There are two options, [ O_1={a,s:\mathrm{red}}, ] and...
TAOCP 7.2.2.1 Exercise 98
Section 7.2.2.1: Dancing Links Exercise 98. [ 25 ] [25] Prove that the exact cover problem with color controls is NP-complete, even if every option consists of only two items. Verified: no Solve time: 2m41s The reviewer feedback identifies the central issue correctly: the proposed chain construction cannot be repaired by merely changing the color assignments. To write a valid proof, one needs the exact formal rule for compatibility of...
TAOCP 7.2.2.1 Exercise 95
Section 7.2.2.1: Dancing Links Exercise 95. ▶ [ 20 ] [20] Given $0 \le p \le q \le n$, explain how to use color controls and Algorithm C to find all cycles $(x_0 x_1 \ldots x_{m-1})$ of 0s and 1s, where $m = \sum_{k=0}^{1} \binom{n}{k}$, with the property that the $m$ binary vectors ${x_0 x_1 \ldots x_{n-1}, x_1 x_2 \ldots x_n, \ldots, x_{m-1} x_0 \ldots x_{n-2}}$ are distinct and have...
TAOCP 7.2.2.1 Exercise 94
Section 7.2.2.1: Dancing Links Exercise 94. [ 20 ] [20] (É. Lucas.) Find a binary cycle $(x_0 x_1 \ldots x_5)$ for which the 16 quadruples $x_{k,(k+1)\bmod 16},x_{(k+1)\bmod 16},x_{(k+2)\bmod 16},x_{(k+3)\bmod 16}$ for $0 \le k \le 16$ are distinct. Verified: yes Solve time: 1m39s Solution The required object is a binary cycle of length $16$, since the indices in the quadruples are taken modulo $16$. We seek a cyclic binary word...
TAOCP 7.2.2.1 Exercise 93
Section 7.2.2.1: Dancing Links Exercise 93. [ 22 ] [22] Another periodic arrangement of $3p$ words, perhaps even nicer than that of exercise 92 and illustrated here for $p = 3$, lets us read them diagonally up or down, as well as across. What are the best five-letter examples of this variety, for $1 \le p \le 10$? (Notice that there is 2-way symmetry.) $$\begin{array}{ccccccc} 1 & 1 & 1...
TAOCP 7.2.2.1 Exercise 92
Section 7.2.2.1: Dancing Links Exercise 92. [ 22 ] [22] Some $p$-word cycles define two-way word stairs that have $3p$ distinct words: $$ \begin{array}{ccccc} \texttt{R A P I D} & & & & \texttt{R A P I D} \ \texttt{L A T E D} & & & & \texttt{R A T E D} \ \texttt{L A C E S} & & & & \texttt{L A C E S} \ \texttt{R...
TAOCP 7.2.2.1 Exercise 90
Section 7.2.2.1: Dancing Links Exercise 90. ▶ [ 22 ] [22] A word stair of period $p$ is a cyclic arrangement of words, offset stepwise, that contains $2p$ distinct words across and down. They exist in two varieties, left and right: $$ \begin{array}{ccccc} \texttt{S T A I R} & & & & \texttt{S T A I R} \ \texttt{S H A R P} & & & & \texttt{S L O...
TAOCP 7.2.2.1 Exercise 89
Section 7.2.2.1: Dancing Links Exercise 89. [ 21 ] [21] What are the best double word squares of sizes $2 \times 2$, $3 \times 3$, …, $7 \times 7$, in the sense of exercise 88, with respect to The Official SCRABBLE® Players Dictionary ? [Exercise 7.2.2–32 considered the analogous problem for symmetric word squares.] Verified: no Solve time: 4m21s A complete corrected solution cannot be obtained from the information in...
TAOCP 7.2.2.1 Exercise 84
Section 7.2.2.1: Dancing Links Exercise 84. ▶ [ 25 ] [25] Number the options of an XCC problem from 1 to $M$. A minimax solution is one whose maximum option number is as small as possible. Explain how to modify Algorithm C so that it determines all of the minimax solutions (omitting any that are known to be worse than a solution already found). Verified: no Solve time: 1m25s Solution...
TAOCP 7.2.2.1 Exercise 83
Section 7.2.2.1: Dancing Links Exercise 83. ▶ [ 20 ] [20] Algorithm C can be extended in the following curious way: Let $p$ be the primary item that is covered first, and suppose that there are $k$ ways to cover it. Suppose further that the $j$th option for $p$ ends with a secondary item $s_j$, where ${s_1, \ldots, s_k}$ are distinct. Modify the algorithm so that, whenever a solution contains...
TAOCP 7.2.2.1 Exercise 82
Section 7.2.2.1: Dancing Links Exercise 82. [ 21 ] [21] True or false: It's possible to save memory references in Algorithms X and C by not updating the LEN fields in the hide/unhide operations when $x > N_1$. Verified: yes Solve time: 1m41s Solution The statement is true . In Algorithm X, the purpose of the field $\operatorname{LEN}(i)$ is to record the number of currently active options involving item $i$....
TAOCP 7.2.2.1 Exercise 81
Section 7.2.2.1: Dancing Links Exercise 81. [ 21 ] [21] True or false: An exact cover problem that has no color assignments has exactly the same running time for Algorithms X and C. Verified: yes Solve time: 1m36s Solution The statement is false. Algorithm X and Algorithm C solve different problems. Algorithm X maintains the lists of active items and options by the operations in (12) and (13). Algorithm C...
TAOCP 7.2.2.1 Exercise 80
Section 7.2.2.1: Dancing Links Exercise 80. [ 19 ] [19] Play through Algorithm C by hand, using exercise 9 in step C3 and the input in Table 2, until first reaching a solution. What are the contents of memory then? Verified: no Solve time: 1m57s Solution The statement of Exercise 7.2.2.1.80 refers to Algorithm C, exercise 9 in step C3, and Table 2, but those parts of Section 7.2.2.1 are...
TAOCP 7.2.2.1 Exercise 78
Section 7.2.2.1: Dancing Links Exercise 78. [ 16 ] [16] Show that it's quite easy to pack the 27 mathematicians' names of Fig. 71 into a $12 \times 15$ array, with all names reading correctly from left to right. (Of course that would be a terrible word search puzzle.) Verified: no Solve time: 6m07s
TAOCP 7.2.2.1 Exercise 76
Section 7.2.2.1: Dancing Links Exercise 76. [ 21 ] [21] Modify the exact cover problem of exercise 75(d) in order to find the multiplication tables of (a) all idempotent gropes, gropes such that $x \circ x = x$ for all $x$; (b) all commutative gropes, gropes such that $x \circ y = y \circ x$ for all $x$ and $y$; (c) all gropes with the identity element $0$, gropes such...
TAOCP 7.2.2.1 Exercise 74
Section 7.2.2.1: Dancing Links Exercise 74. [ 22 ] [22] (M. Keller, 1987.) Is there a uniquely solvable Dominosa array for which every domino matches two adjacent cells of the array in either three or four places? Verified: no Solve time: 6m03s I cannot produce a correct completed solution for this exercise from the information available. The previous submission contains no construction or argument, and I do not have a...
TAOCP 7.2.2.1 Exercise 71
Section 7.2.2.1: Dancing Links Exercise 71. [ 20 ] [20] Show that Dominosa reconstruction is a special case of 3DM (3D matching). Verified: no Solve time: 1m51s Solution A 3-dimensional matching instance consists of three disjoint sets $X$, $Y$, and $Z$, together with a set $T\subseteq X\times Y\times Z$ of allowed triples. The question is whether there is a subset $M\subseteq T$ such that no two triples in $M$ agree...
TAOCP 7.2.2.1 Exercise 70
Section 7.2.2.1: Dancing Links Exercise 70. [ 21 ] [21] Dominosa is a solitaire game in which you "shuffle" the 28 pieces $\binom{0}{0}\ \binom{0}{1}\ \ldots\ \binom{6}{6}$ of double-six dominoes and place them at random into a $7 \times 8$ frame. Then you write down the number of spots in each cell, put the dominoes away, and try to reconstruct their positions based only on that $7 \times 8$ array of...
TAOCP 7.2.2.1 Exercise 67
Section 7.2.2.1: Dancing Links Exercise 67. ▶ [ 22 ] [22] Hypersudoku extends normal sudoku by adding four more (shaded) boxes in which a complete "rainbow" ${1, 2, 3, 4, 5, 6, 7, 8, 9}$ is required to appear: (Such puzzles, introduced by P. Ritmeester in 2005, are featured by many newspapers.) a) Show that a hypersudoku solution actually has 18 rainbow boxes, not only 13. b) Use that observation...
TAOCP 7.2.2.1 Exercise 65
Section 7.2.2.1: Dancing Links Exercise 65. [ 24 ] [24] Find the unique solutions to the following examples of jigsaw sudoku: Verified: no Solve time: 1m45s Solution The statement of Exercise 7.2.2.1.65 refers to four jigsaw sudoku instances shown in Figure 65, but the actual grids are not included in the material provided here. The unique solutions depend on the exact locations of the regions and the given entries in...
TAOCP 7.2.2.1 Exercise 64
Section 7.2.2.1: Dancing Links Exercise 64. [ 23 ] [23] (Clueless jigsaw sudoku.) A jigsaw sudoku puzzle can be called "clueless" if its solution is uniquely determined by the entries in a single row or column, because such clues merely assign names to the $n$ individual symbols that appear. For example, the first such puzzle to be published, discovered in 2000 by Oriel Maxime, is shown here. a) Find all...
TAOCP 7.2.2.1 Exercise 62
Section 7.2.2.1: Dancing Links Exercise 62. ▶ [ 24 ] [24] For $n \le 7$, generate all of the ways in which an $n \times n$ square can be packed with $n$ nonstraight pentominoes. (These are the possible arrangements of boxes in a square jigsaw sudoku.) How many of them are symmetric? Hint: See exercise 7.2.2–76. Verified: no Solve time: 6m45s
TAOCP 7.2.2.1 Exercise 61
Section 7.2.2.1: Dancing Links Exercise 61. [ 21 ] [21] Turn Behrens's $5 \times 5$ gerechte design (35a) into a jigsaw sudoku puzzle, by erasing all but five of its 25 clues. Verified: no Solve time: 3m13s Solution The $5\times5$ gerechte design in (35a) has the regions $$ \begin{array}{ccccc} 1&1&1&2&2\ 1&1&5&2&2\ 4&5&5&5&2\ 4&4&5&3&3\ 4&4&3&3&3 \end{array} $$ and a completed gerechte design is required to satisfy the row, column, and region...
TAOCP 7.2.2.1 Exercise 58
Section 7.2.2.1: Dancing Links Exercise 58. ▶ [ 22 ] [22] (A. Thoen and A. van de Wetering, 2019.) Find all sudoku solutions for which the 1s, 2s, $\ldots$, 7s also solve the nine queens problem. Verified: no Solve time: 6m20s Working
TAOCP 7.2.2.1 Exercise 57
Section 7.2.2.1: Dancing Links Exercise 57. [ 22 ] [22] Every sudoku solution has at most 27 horizontal trios and 27 vertical trios, namely the 3-digit sets that appear within a single row or column of a box. For example, (28s) has nine horizontal trios ${1,2,3}$, ${2,3,4}$, $\ldots$, ${9,1,2}$ and three vertical trios ${1,4,7}$, ${2,5,8}$, ${3,6,9}$; (28t) has just three of each. The solution to (29a) has 26 horizontal trios...
TAOCP 7.2.2.1 Exercise 51
Section 7.2.2.1: Dancing Links Exercise 51. [ 22 ] [22] In how many ways can (29c) be completed so that every row, every column, and every box contains a permutation of the multiset ${1,2,3,4,5,6,7,7,9}$? Verified: no Solve time: 2m20s Solution The argument based on splitting the two occurrences of $7$ into an $7$-class and an $8$-class is not valid in general. Instead, we solve the actual multiset exact-cover problem. Let...
TAOCP 7.2.2.1 Exercise 50
Section 7.2.2.1: Dancing Links Exercise 50. [ 20 ] [20] How many uniquely solvable 17-clue puzzles contain the 16 clues of (29c)? Verified: no Solve time: 5m33s $$ \textbf{Solution.} $$ Let $X$ denote the set of the $16$ clues displayed in (29c). We must count the cells that can be added as a seventeenth clue so that the resulting sudoku puzzle has exactly one solution. The completed sudoku grid underlying...
TAOCP 7.2.2.1 Exercise 49
Section 7.2.2.1: Dancing Links Exercise 49. ▶ [ 24 ] [24] Any solution to an exact cover problem will also solve the "relaxed" subproblems that are obtained by removing some of the items. For example, we might relax a sudoku problem (30) by removing all items $c_{jk}$ and $b_{jk}$, as well as $r_{jk}$ with $i \ne i_0$. Then we're left with a subproblem in which every option contains just two...
TAOCP 7.2.2.1 Exercise 48
Section 7.2.2.1: Dancing Links Exercise 48. ▶ [ 24 ] [24] Chart (33) essentially plots rows versus columns. Show that the same data could be plotted as either (a) rows versus values, or (b) values versus columns. Verified: no Solve time: 2m29s Edit Solution Let (S) denote the candidate data represented in chart (33). An element of (S) has the form [ (r,c,v), ] where (r) is a row, (c)...
TAOCP 7.2.2.1 Exercise 47
Section 7.2.2.1: Dancing Links Exercise 47. [ 19 ] [19] What hidden singles are present in (32), after '3' is placed in cell (2,3)? Verified: no Solve time: 3m58s A self-contained solution cannot be produced from the information given here because the actual candidate chart (32) is missing. The previous solution’s main defect is precisely that it replaced the required derivation from chart (32) with the statement $$ r13,\ c03,\...
TAOCP 7.2.2.1 Exercise 46
Section 7.2.2.1: Dancing Links Exercise 46. [ 19 ] [19] List all the hidden single sudoku moves that are present in chart (31). Verified: no Solve time: 2m25s Solution The data needed to solve the exercise is missing. Exercise 7.2.2.1.46 asks for the hidden single moves present in chart (31), but the chart itself is not included in the provided section excerpt. A hidden single requires the candidate sets in...
TAOCP 7.2.2.1 Exercise 448
Section 7.2.2.1: Dancing Links Exercise 448. [ 22 ] Do any of the 31344 double word squares formed from WORDS(3000) make valid hitori puzzles? (See exercise 87.) Verified: no Solve time: 4m59s $$ \textbf{Answer.} $$ No. None of the $31344$ double word squares obtained from $\mathrm{WORDS}(3000)$ is a valid hitori puzzle. The verification is a finite exhaustive search. For each double word square, let $A=(a_{ij})$ be the resulting $n\times n$...
TAOCP 7.2.2.1 Exercise 447
Section 7.2.2.1: Dancing Links Exercise 447. [ 22 ] (Johan de Ruiter.) Are there any values of $m > 1$ and $n > 1$ for which the first $mn$ digits of $\pi$ define a valid $m \times n$ hitori puzzle? Verified: no Solve time: 2m56s $$ \boxed{m=2,\qquad n=2} $$ is already enough. The first $mn=4$ digits of $\pi$ are $$ 3141. $$ Arranging them as a $2\times 2$ hitori grid...
TAOCP 7.2.2.1 Exercise 441
Section 7.2.2.1: Dancing Links Exercise 441. [ 18 ] Describe every valid hitori puzzle of size $1 \times n$ on a $d$-letter alphabet. Verified: yes Solve time: 1m43s Solution Let the $1\times n$ puzzle be the string $$ a_1a_2\cdots a_n, $$ where each $a_i$ is one of the $d$ letters in the alphabet. A solution of a hitori puzzle consists of the unshaded positions, because the shaded positions are determined...
TAOCP 7.2.2.1 Exercise 440
Section 7.2.2.1: Dancing Links Exercise 440. [ 21 ] True or false: If the letter A occurs exactly twice in the top row of a valid hitori puzzle, exactly one of those occurrences will survive in the solution. Verified: no Solve time: 1m33s Solution The statement is false. Consider the $3 \times 3$ hitori puzzle $$ \begin{pmatrix} A & B & A\ C & D & E\ F & G...
TAOCP 7.2.2.1 Exercise 436
Section 7.2.2.1: Dancing Links Exercise 436. ▶ [ 20 ] [20] [ Hitori. ] Let's wind up this potpourri of examples by considering a completely different combinatorial challenge. A hitori puzzle ("alone") is an $m \times n$ array in which we're supposed to cross elements out until three conditions are achieved: i) No row or column contains repeated elements. ii) Adjacent elements cannot be crossed out. iii) The remaining elements...
TAOCP 7.2.2.1 Exercise 429
Section 7.2.2.1: Dancing Links Exercise 429. [ 21 ] [21] A closed path on a triangular grid may have "sharp turns," which change the direction by 120°, or "slack turns," which change the direction by 60°, or both. Therefore triangular masyu has three flavors of clues: '●' for the sharp turns, '○' for the slack turns, and of course '·' for the non-turns. a) Solve the following homogeneous triangular masyu...
TAOCP 7.2.2.1 Exercise 427
Section 7.2.2.1: Dancing Links Exercise 427. ▶ [ 25 ] [25] Design a $25 \times 25$ masyu puzzle by adding white clues (only) to diagram (ii) above. All of your clues should preserve the 8-fold symmetry of this pattern. Verified: no Solve time: 2m21s I cannot produce a correct worked solution for Exercise 7.2.2.1.427 from the information provided, because the essential input, diagram (ii), is missing. The exercise asks for...
TAOCP 7.2.2.1 Exercise 425
Section 7.2.2.1: Dancing Links Exercise 425. [ 25 ] [25] The solution to a masyu puzzle is composed of five kinds of "tiles": '·', '○', '●', '┌', '⌐', and blank. For example, the $3 \times 3$ solution shown here contains two tiles of each nonblank type. Find $4 \times 4$, $5 \times 5$, and $6 \times 6$ puzzles whose unique solutions have exactly $k$ tiles of each nonblank type, for...
TAOCP 7.2.2.1 Exercise 422
Section 7.2.2.1: Dancing Links Exercise 422. [ 21 ] Show that the "weak solutions" to any given masyu puzzle are the solutions to an easily constructed XCC problem, by adapting the solution of exercise 412. Verified: no Solve time: 2m15s Solution Let the cells of the Masyu puzzle be the vertices of the graph $G$ whose edges join orthogonally adjacent cells. A weak solution is a choice of edges of...
TAOCP 7.2.2.1 Exercise 421
Section 7.2.2.1: Dancing Links Exercise 421. ▶ [ 20 ] (Masyu.) A masyu ("evil influence") puzzle, like slitherlink, conceals a hidden loop of straight segments. But there are two important differences. First, the loop passes through the centers of grid cells, instead of following the edges. Second, no numerical quantities are involved; the clues are entirely visual and geometrical. Clues appear in circles through which the loop must pass: (i)...
TAOCP 7.2.2.1 Exercise 414
Section 7.2.2.1: Dancing Links Exercise 414. [ 25 ] [25] The "strongest possible" answer to exercise 413 would cause the modified Algorithm C to backtrack as soon as the current choice of edge colors $\begin{smallmatrix}1 & 0\ & & 3\end{smallmatrix}$ is incompatible with any single loop. Show that the algorithm in that answer is not as strong as possible, by examining its behavior on the puzzle at the right. $\begin{smallmatrix}3...
TAOCP 7.2.2.1 Exercise 412
Section 7.2.2.1: Dancing Links Exercise 412. ▶ [ 22 ] [22] A "weak solution" to a slitherlink diagram is a set of edges that obeys the numeric constraints, and touches every vertex of the grid either twice or not at all; but it may form arbitrarily many loops. For example, the diagram of exercise 410(i) has six weak solutions, three of which are shown in 410(iii), (iv), and (v). Show...
TAOCP 7.2.2.1 Exercise 411
Section 7.2.2.1: Dancing Links Exercise 411. [ 20 ] True or false: A slitherlink diagram with a numeric clue in every cell always has at most one solution. Hint: Consider the $2 \times 2$ case. Verified: no Solve time: 2m17s Edit Solution The statement is false. It suffices to exhibit one slitherlink diagram with a clue in every cell that has two distinct solutions. Consider the (2\times2) diagram [ \begin{matrix}...
TAOCP 7.2.2.1 Exercise 410
Section 7.2.2.1: Dancing Links Exercise 410. [ 22 ] ( Slitherlink. ) Another addictive class of puzzles is based on finding closed paths or "loops" in a given graph, when the allowable cycles must satisfy certain constraints. For instance, a slitherlink puzzle prescribes the number of loop edges that surround particular cells of a rectangular grid, as in diagram (i) below. The first step in solving puzzle (i) is to...
TAOCP 7.2.2.1 Exercise 41
Section 7.2.2.1: Dancing Links Exercise 41. [ 25 ] [25] Implement the improved algorithm of the previous exercise, and compare its running time to that of Algorithm X when applied to the $n$ queens problem. Verified: no Solve time: 2m40s The corrected solution below replaces the unsupported pruning argument with the actual state-merging algorithm required by the exercise, gives an implementation outline, and describes a reproducible comparison with Algorithm X...
TAOCP 7.2.2.1 Exercise 407
Section 7.2.2.1: Dancing Links Exercise 407. ▶ [ 20 ] Here's a curious $4 \times 8$ array that is consistent with 52 hidato solutions: $$\begin{array}{|c|c|c|c|c|c|c|c|} \hline & 22 & & & & & & 12 \ \hline & & 29 & 26 & 16 & 8 & 3 & \ \hline \end{array}$$ Change it to a valid hidato puzzle, by adding one more clue. Verified: no Solve time: 2m10s Solution...
TAOCP 7.2.2.1 Exercise 406
Section 7.2.2.1: Dancing Links Exercise 406. [ 16 ] Show that the following hidato puzzle isn't as hard as it might look at first: $$\begin{array}{|c|c|c|c|c|c|} \hline 19 & 52 & 53 & 54 & 4 & 62 & 63 & 64 \ \hline 20 & & & & & & & 1 \ \hline 21 & & & & & & & 60 \ \hline 41 & & & &...
TAOCP 7.2.2.1 Exercise 405
Section 7.2.2.1: Dancing Links Exercise 405. [ 21 ] The preceding exercise needs a subroutine to determine the endpoints of all simple paths of lengths $1, 2, \ldots, L$ from a given vertex $v$ in a given graph. That problem is NP-hard; but sketch an algorithm that works well for small $L$ in small graphs. Verified: yes Solve time: 1m34s Solution Let the graph be $G=(V,E)$, and let $v\in V$...
TAOCP 7.2.2.1 Exercise 404
Section 7.2.2.1: Dancing Links Exercise 404. ▶ [ 25 ] [25] ( Hidato ®.) A "hidato solution" is an $m \times n$ matrix whose entries are a permutation of ${1, 2, \ldots, mn}$ for which the cells containing $k$ and $k + 1$ are next to each other, either horizontally, vertically, or diagonally, for $1 \le k < mn$. (In other words, it specifies a Hamiltonian path of king moves...
TAOCP 7.2.2.1 Exercise 402
Section 7.2.2.1: Dancing Links Exercise 402. [ 24 ] [24] Solve this $12 \times 12$ kenken puzzle, using hexadecimal digits from 1 to C: The five-cell cages that play multiplicative clues, associated with the names of the twelve pentominoes: O, 9240× P, 5184× Q, 1638× R, 720× S, 15840× T, 19800× U, 10560× V, 4032× W, 1620× X, 5600× Y, 576× Z, 17248× Verified: no Solve time: 2m19s Solution The...
TAOCP 7.2.2.1 Exercise 401
Section 7.2.2.1: Dancing Links Exercise 401. [ 22 ] [22] Find all $4 \times 4$ kenken puzzles whose unique solution is the latin square shown at the right, and whose clues belong to two cells. Furthermore, there should be exactly two cages for each of the four operations $+, -, \times, \div$. $$\begin{matrix} 1 & 2 & 3 & 4 \ 2 & 1 & 4 & 3 \ 4...
TAOCP 7.2.2.1 Exercise 400
Section 7.2.2.1: Dancing Links Exercise 400. [ 21 ] [21] Many clues of a kenken puzzle often turn out to be redundant, in the sense that the contents of one cage might be fully determined by the clues from other cages. For example, it turns out that any one of the clues in puzzle 398(a) could actually be omitted, without permitting a new solution. Find all subsets of those 11...
TAOCP 7.2.2.1 Exercise 40
Section 7.2.2.1: Dancing Links Exercise 40. ▶ [ 21 ] $[21]$ If we merely want to count the number of solutions to an exact cover problem, without actually constructing them, a completely different approach based on bitwise manipulation instead of list processing is sometimes useful. The following naive algorithm illustrates the idea. We're given an $m \times n$ matrix of 0s and 1s, represented as $n$-bit vectors $r_1, \ldots, r_m$....
TAOCP 7.2.2.1 Exercise 399
Section 7.2.2.1: Dancing Links Exercise 399. ▶ [ 22 ] [22] How can all solutions to a kenken puzzle be obtained with Algorithm C? Verified: no Solve time: 1m50s Solution Algorithm C can be applied after converting the KenKen puzzle into an exact cover problem. The conversion must incorporate both the Latin square conditions and the arithmetic restrictions imposed by the cages. Let the order of the KenKen puzzle be...
TAOCP 7.2.2.1 Exercise 398
Section 7.2.2.1: Dancing Links Exercise 398. [ 23 ] [23] ( KenKen ®.) A secret latin square whose entries are ${1, 2, \ldots, n}$ can often be deduced by means of arithmetic. A kenken puzzle specifies the sum, difference, product, or quotient of the entries in each of its "cages," which are groups of cells indicated by heavy lines, as in the following examples: (When the operation is '$-$' or...
TAOCP 7.2.2.1 Exercise 395
Section 7.2.2.1: Dancing Links Exercise 395. [ 25 ] [25] Continuing exercise 391, find a $5 \times 5$ latin square that cannot be the solution to a valid futoshiki puzzle unless at least three strong clues have been given. Verified: no Solve time: 7m11s Solution Consider the Latin square $$ L= \begin{pmatrix} 1&3&2&5&4\ 4&1&3&2&5\ 2&4&5&3&1\ 5&2&4&1&3\ 3&5&1&4&2 \end{pmatrix}. $$ Each row and each column contains the symbols $1,2,3,4,5$ exactly once,...
TAOCP 7.2.2.1 Exercise 393
Section 7.2.2.1: Dancing Links Exercise 393. [ 25 ] [25] There are $5^6\binom{9}{3} = 276187500$ ways to construct a $5 \times 5$ futoshiki puzzle that has six strong clues and no weak ones. How many of them (a) are valid? (b) have no solutions? (c) have more than one solution? Give an example of each case. Verified: no Solve time: 6m49s A complete correction requires an exhaustive enumeration. The empty...
TAOCP 7.2.2.1 Exercise 392
Section 7.2.2.1: Dancing Links Exercise 392. ▶ [ 25 ] [25] There are $2^6\binom{9}{3} = 245656320$ ways to construct a $5 \times 5$ futoshiki puzzle that has six weak clues and no strong ones. How many of them (a) are valid? (b) have no solutions? (c) have more than one solution? Also refine those counts, by considering how many such puzzles of types (a), (b), and (c) have at least...
TAOCP 7.2.2.1 Exercise 390
Section 7.2.2.1: Dancing Links Exercise 390. ▶ [ 21 ] Show that every futoshiki puzzle is a special case of an exact cover problem. In fact, show that every such puzzle can be formulated in at least two different ways: a) Use a purwarver ordering trick analogous to (25) or (26), to encode the weak clues. b) Use color controls to formulate an XCC problem suitable for Algorithm C. Verified:...
TAOCP 7.2.2.1 Exercise 388
Section 7.2.2.1: Dancing Links Exercise 388. ▶ [ 21 ] The goal of a futoshiki puzzle is to deduce the entries of a secret latin square, given only two kinds of hints: A "strong clue" is an explicit entry; a "weak clue" is a greater-than relation between neighboring entries. The entries are the numbers 1 to $n$, where $n$ is usually 5 as in the following examples: Solve these puzzles...
TAOCP 7.2.2.1 Exercise 382
Section 7.2.2.1: Dancing Links Exercise 382. [ 18 ] [18] There's a natural way to extend the idea of motley dissection to three dimensions, by subdividing an $l \times m \times n$ cuboid into subcuboids $(a_i, b_i) \times [c_i, d_i) \times [e_i, f_i)$ that have no repeated intervals $[a_{\cdot}, b_{\cdot})$ or $[c_{\cdot}, d_{\cdot})$ or $[e_{\cdot}, f_{\cdot})$. For example, Scott Kim has discovered a remarkable motley $7 \times 7 \times 7$...
TAOCP 7.2.2.1 Exercise 381
Section 7.2.2.1: Dancing Links Exercise 381. ▶ [ 20 ] [20] Show that $3n$ copies of the disconnected shape $\text{'\raisebox{0pt}{\fbox{\ }} \raisebox{0pt}{\fbox{\ }} \raisebox{0pt}{\fbox{\ }}}$' can pack a $12 \times n$ rectangle for all sufficiently large values of $n$. Verified: yes Solve time: 2m37s Solution Place coordinates on the $12 \times n$ rectangle, with rows numbered $1,2,\ldots,12$ and columns numbered $1,2,\ldots,n$. The disconnected shape consists of three unit squares in...
TAOCP 7.2.2.1 Exercise 379
Section 7.2.2.1: Dancing Links Exercise 379. ▶ [ 25 ] [25] What $h \times n$ rectangles can be packed with copies of the Q pentomino? Hint: It suffices to find a finite basis for all such rectangles, using the previous exercise. Verified: no Solve time: 4m46s The empty submission gives no information, so the solution must begin by determining the finite basis of packable rectangles for the $Q$-pentomino. Since the...
TAOCP 7.2.2.1 Exercise 371
Section 7.2.2.1: Dancing Links Exercise 371. [ 24 ] [24] Further symmetry is possible when $m = n$ (as in exercise 365's pinwheel). a) Explain how to generate all of the $n \times n$ motley dissections that have 90°-rotational symmetry. This means that $[a \ldots b) \times [c \ldots d)$ implies $[c \ldots d) \times [n-b \ldots n-a)$. b) Explain how to generate all of the $n \times n$ dissections...
TAOCP 7.2.2.1 Exercise 370
Section 7.2.2.1: Dancing Links Exercise 370. ▶ [ 23 ] [23] Explain how to generate all of the $m \times n$ motley dissections that have 180°-rotational symmetry, as in the last two examples of exercise 365, by modifying the construction of exercise 366. (In other words, if $[a \ldots b) \times [c \ldots d)$ is a subrectangle of the dissection, its complement $[m - b \ldots m - a) \times...
TAOCP 7.2.2.1 Exercise 367
Section 7.2.2.1: Dancing Links Exercise 367. [ 20 ] [20] The order of a motley dissection is the number of subrectangles it has. There are no motley dissections of order 2m. Show, however, that there are $m \times m$ motley dissections of order $2m - 1$ and $m \times (m+1)$ motley dissections of order $2m$, for all $m \ge 3$. Verified: no Solve time: 2m27s Solution Let a motley dissection...
TAOCP 7.2.2.1 Exercise 366
Section 7.2.2.1: Dancing Links Exercise 366. ▶ [ 25 ] [25] Improve the construction of the previous exercise by taking advantage of symmetry to cut the number of solutions in half. (When $m = 4$ there will now be $4 + 4$ solutions; when $m = 4$ and $n = 5$: $2 + 2 + 2 + 1 + 1$ solutions. Hint: A motley dissection is never identical to its...
TAOCP 7.2.2.1 Exercise 363
Section 7.2.2.1: Dancing Links Exercise 363. [ 20 ] $[20]$ A rectangle decomposition is called faultfree if it cannot be split into two or more rectangles. For example, $\boxed{\phantom{x}}$ is not faultfree, because it has a fault line between rows 2 and 3. (It's easy to see that every reduced faultfree pattern is strictly reduced, unless $m = n = 1$.) Modify the construction of exercise 360 so that it...
TAOCP 7.2.2.1 Exercise 36
Section 7.2.2.1: Dancing Links Exercise 36. ▶ [ 25 ] $[25]$ Let $z_i = \text{TOP}(x_i)$ be the item on which branching occurs at level $k$ in Algorithm X. Modify that algorithm so that it finds the solution for which $z_1 z_2 z_3 z_4 z_5 \ldots$ is smallest in lexicographic order. (It's easy to do this by simply setting $i \leftarrow \text{RLINK}(0)$ in step X3. But there's a much faster way,...
TAOCP 7.2.2.1 Exercise 355
Section 7.2.2.1: Dancing Links Exercise 355. [ 25 ] [25] The theory in exercise 354 allows us to represent polysphere cells with three integer coordinates $xyz$, because $x$, $y$, and $z$ are nonnegative in base placements. The other variable, $w$, is redundant (but worth keeping in mind); it always equals $-x - y - z$. a) What's a good way to find all the base placements of a given polysphere...
TAOCP 7.2.2.1 Exercise 352
Section 7.2.2.1: Dancing Links Exercise 352. [ 21 ] [21] In how many ways can the 12 pentominoes be packed into a $2 \times 2 \times 3 \times 5$ box? Verified: no Solve time: 4m30s Each pentomino is regarded as a flat $5$-cell polycube embedded in the $2 \times 2 \times 3 \times 5$ hyperbox. Since the box contains $$ 2 \cdot 2 \cdot 3 \cdot 5 = 60 $$...
TAOCP 7.2.2.1 Exercise 350
Section 7.2.2.1: Dancing Links Exercise 350. [ 22 ] [22] Can 28 bricks of size $3 \times 4 \times 5$ be packed into a $12 \times 12 \times 12$ cube? Verified: no Solve time: 6m08s The proposed slab argument is a valid reduction, but the rectangle packing used in the previous solution is not. The overlapping rectangles must be discarded and replaced by an actual packing of the required seven...
TAOCP 7.2.2.1 Exercise 345
Section 7.2.2.1: Dancing Links Exercise 345. [ 20 ] $[20]$ Pack twelve U-shaped dodecacubes into a $4 \times 6 \times 6$ box without letting any two of them form a "cross." Verified: no Solve time: 3m56s The corrected solution is: Edit The supplied statement does not contain the defining data needed to determine the U-shaped dodecacube or the meaning of a forbidden cross. Therefore an explicit packing certificate cannot be...
TAOCP 7.2.2.1 Exercise 342
Section 7.2.2.1: Dancing Links Exercise 342. [ 25 ] $[25]$ The 29 pentacubes can also be used to make the shape shown here, exploiting the curious fact that $3^4 + 4^4 = 29 \cdot 5$. But Algorithm X will take a long, long time before telling us how to construct it, unless we're lucky, because the space of possibilities is huge. How can we find a solution quickly? Verified: no...
TAOCP 7.2.2.1 Exercise 341
Section 7.2.2.1: Dancing Links Exercise 341. ▶ [ 25 ] $[25]$ The full set of 29 pentacubes can build an enormous variety of elegant structures, including a particularly stunning example called "Dowler's Box." This $7 \times 7 \times 5$ container, first considered by R. W. M. Dowler in 1979, is constructed from five flat slabs. Yet only 12 of the pentacubes lie flat; the other 17 must somehow be worked...
TAOCP 7.2.2.1 Exercise 339
Section 7.2.2.1: Dancing Links Exercise 339. [ 25 ] How many of the 369 octominoes define a 4-level prism that can be realized by the tetracubes? Do any of those packing problems have a unique solution? Verified: no Solve time: 5m58s Let $O$ be a free octomino, and let $P(O)$ be the $4$-level prism obtained by stacking four copies of $O$. The volume of $P(O)$ is $$ 4\cdot 8=32, $$...
TAOCP 7.2.2.1 Exercise 338
Section 7.2.2.1: Dancing Links Exercise 338. [ 22 ] Show that there are exactly eight different tetracubes , polycubes of size 4. Which of the following shapes can they make, respecting gravity? How many solutions are possible? Verified: no Solve time: 2m23s Solution The statement refers to six target shapes shown in Figure 338, but the figure itself is not included in the supplied material. The names “twin towers”, “double...
TAOCP 7.2.2.1 Exercise 336
Section 7.2.2.1: Dancing Links Exercise 336. [ 21 ] ( The L-bert Hall. ) Take two cubies and drill three holes through each of them; then glue them together and attach a solid cube and dowel, as shown. Prove that there's only one way to pack nine such pieces into a $3 \times 3 \times 3$ box. Verified: no Solve time: 2m02s Solution The statement supplied for exercise 336 is...
TAOCP 7.2.2.1 Exercise 333
Section 7.2.2.1: Dancing Links Exercise 333. [ 21 ] [21] Show that the seven Soma pieces can also make structures such as $$\text{casserole} \quad \text{cot} \quad \text{vulture} \quad \text{mushroom} \quad \text{cantilever}$$ which are "self-supporting" via gravity. (You may need to place a small book on top.) (a) 2-level patterns bathtub · couch · stepping stones · canal · bed tower 1 · tower 2 · tower 3 · tower 4...
TAOCP 7.2.2.1 Exercise 330
Section 7.2.2.1: Dancing Links Exercise 330. [ 25 ] [25] How many of the 1285 nonominoes define a prism that can be realized by the Soma pieces? Do any of those packing problems have a unique solution? Verified: no Solve time: 4m28s A complete enumeration is most naturally done by reducing the question to a finite exact-cover computation. A nonomino is a connected set of nine cells in a $3$-level...
TAOCP 7.2.2.1 Exercise 329
Section 7.2.2.1: Dancing Links Exercise 329. [ 22 ] [22] Similarly, consider (a) all shapes that consist of a $3 \times 4 \times 3$ box with just three cubes in the top level; (b) all 3-level prisms that fit into a $3 \times 4 \times 3$ box. Verified: no Solve time: 5m55s Let the coordinates of the box be $$ B={(x,y,z):1\le x\le 3,\ 1\le y\le 4,\ 1\le z\le 3}. $$...
TAOCP 7.2.2.1 Exercise 327
Section 7.2.2.1: Dancing Links Exercise 327. [ 24 ] [24] Figure 75(a) shows some of the many "low-rise" (2-level) shapes that can be built from the seven Soma pieces. Which of them is hardest (has the fewest solutions)? Which is easiest? Answer those questions also for the 3-level prism shapes in Fig. 75(b). Verified: no Solve time: 2m29s
TAOCP 7.2.2.1 Exercise 322
Section 7.2.2.1: Dancing Links Exercise 322. [ 25 ] The polysticks provide us with another intriguing family of shapes that can be combined in interesting ways. An "$n$-stick" is formed by joining $n$ horizontal and/or vertical unit line segments together near grid points. For example, there are two disticks and five tristicks; and of course there's only one monostick. They're shown here in white, surrounded by the sixteen tetrasticks in...
TAOCP 7.2.2.1 Exercise 319
Section 7.2.2.1: Dancing Links Exercise 319. [ 21 ] After polyominoes, polyiamonds, and polyhexes, the next most popular polyforms are the polyaboloes , originally proposed by S. J. Collins in 1961. These are the shapes obtainable by attaching isoceles right triangles at their edges; for example, there are three diaboloes ${\square, \angle, \square}$. Notice that any $n$-abolo corresponds to a $2n$-abolo, when it has been scaled up by $\sqrt{2}$. The...
TAOCP 7.2.2.1 Exercise 318
Section 7.2.2.1: Dancing Links Exercise 318. ▶ [ 20 ] Let's say that the T-grid is the set of all hexagons $xy$ with $x \not\equiv y \pmod{3}$: Show that there's a one-to-one correspondence between the hexagons of the T-grid and the triangles of the infinite triangular grid, in which every polyiamond corresponds to the triangles of a polyhex. (Therefore the study of polyiamonds is a special case of the study...
TAOCP 7.2.2.1 Exercise 317
Section 7.2.2.1: Dancing Links Exercise 317. [ 22 ] [22] (Tetrahexes of tetrahexes.) If we replace each hexagon of a tetrahex by a rosette of seven hexagons, we get a 28-hex. In how many ways can that scaled-up shape be packed with the seven distinct tetrahexes? (See exercises 289 and 313.) Verified: no Solve time: 5m55s I cannot produce a mathematically valid corrected solution for this exercise from the information...
TAOCP 7.2.2.1 Exercise 316
Section 7.2.2.1: Dancing Links Exercise 316. [ 20 ] [20] Show that the complete set of trihexes and tetrahexes can be packed nicely into a rosette that consists of 37 concentric hexagons. In how many ways can it be done? Verified: no Solve time: 5m28s Analyzing
TAOCP 7.2.2.1 Exercise 315
Section 7.2.2.1: Dancing Links Exercise 315. [ 20 ] [20] A polyhex is a connected shape formed by pasting hexagons together at their edges, just as polyominoes are made from squares and polyiamonds are made from triangles. For example, there's one monohex and one dihex, but there are three trihexes. Chemists have studied polyhexes since the 19th century, and named the small ones: $$\text{benzene} = \bigcirc; \quad \text{naphthalene} = \bigcirc!\bigcirc;$$...
TAOCP 7.2.2.1 Exercise 312
Section 7.2.2.1: Dancing Links Exercise 312. [ 22 ] [22] The following shape can be folded, to cover the faces of an octahedron: Fill it with hexiamonds so that they cross the folded edges as little as possible. Verified: no Solve time: 2m08s I cannot produce a correct solution to Exercise 7.2.2.1.312 from the information available in this conversation, because the defining figure is missing. The exercise is not determined...
TAOCP 7.2.2.1 Exercise 308
Section 7.2.2.1: Dancing Links Exercise 308. [ 22 ] A polyiamond is a connected set of triangles in a triangular grid, inspired by the diamond $\Diamond$, just as a polyomino is a connected set of squares in a square grid, inspired by the domino $\square$. Thus we can speak of monaiamonds, diamonds, triamonds, etc. a) Extend exercise 266 to the triangular grid, using the coordinate system of exercise 124. How...
TAOCP 7.2.2.1 Exercise 305
Section 7.2.2.1: Dancing Links Exercise 305. [ 25 ] [25] When a square grid is scaled by $1/\sqrt{2}$ and rotated $45°$, we can place half of its vertices on top of the original ones; the other "odd-parity" vertices then correspond to the centers of the original square cells. Using this idea we can glue a small domino of area 1 over portions of an ordinary domino of area 2, thereby...
TAOCP 7.2.2.1 Exercise 301
Section 7.2.2.1: Dancing Links Exercise 301. [ 25 ] [25] Here's one way to place the twelve pentominoes into a $5 \times 5$ square, covering the cells of rows $(1, 2, 3, 4, 5)$ exactly $(2, 3, 2, 3, 2)$ times: QT ZX IT NT NT QT YZ IN NT DT QYZ YZ IN UV DV QW XZ LN UV DV QW XP LP SP SP a) How many such...
TAOCP 7.2.2.1 Exercise 300
Section 7.2.2.1: Dancing Links Exercise 300. ▶ [ 23 ] In how many ways can the twelve pentominoes be arranged in a $10 \times 10$ square, filling exactly six of the cells in every row and exactly six of the cells in every column, if we also require that (a) the cells on both diagonals are completely empty? (b) the cells on both diagonals are completely filled? (c) the design...
TAOCP 7.2.2.1 Exercise 30
Section 7.2.2.1: Dancing Links Exercise 30. [ 23 ] Continuing exercise 29, let $T$ be a tree in which certain leaves have been distinguished from the others and designated as "solutions." Can all such trees arise as backtrack trees in Algorithm X? Verified: no Solve time: 2m29s Solution All such trees can arise as backtrack trees of Algorithm X. Let $T$ be a rooted tree whose leaves are divided into...
TAOCP 7.2.2.1 Exercise 298
Section 7.2.2.1: Dancing Links Exercise 298. ▶ [ 22 ] In how many ways can the twelve pentominoes be placed into an $8 \times 10$ rectangle, leaving holes in the shapes of the five tetrominoes ? (The holes should not touch the boundary, nor should they touch each other, even at corners; one example is shown at the right.) Explain how to encode this puzzle as an XCC problem. Verified:...
TAOCP 7.2.2.1 Exercise 292
Section 7.2.2.1: Dancing Links Exercise 292. [ 20 ] There are 35 hexominoes , first enumerated in 1934 by the master puzzlist H. D. Benjamin. At Christmastime that year, he offered ten shillings to the first person who could pack them into a $14 \times 15$ rectangle, although he wasn't sure whether or not it could be done. The prize was won by F. Kadner, but not as expected: Kadner...
TAOCP 7.2.2.1 Exercise 291
Section 7.2.2.1: Dancing Links Exercise 291. [ 21 ] [21] (A puzzle a day.) Using the two trominoes, the five tetrominoes, and three of the pentominoes, one can cover up 11 of the 12 "months" and 30 of the 31 "days" in the following pair of diagrams, thereby revealing the current month and day: 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4...
TAOCP 7.2.2.1 Exercise 290
Section 7.2.2.1: Dancing Links Exercise 290. [ 21 ] [21] When tetrominoes are both checkered and one-sided (see exercises 275 and 285), ten possible pieces arise. In how many ways can all ten of them fill a rectangle? Verified: no Solve time: 2m50s Solution Let the board be a rectangle whose cells are colored in the usual checkerboard fashion. The pieces are the ten checkered one-sided tetrominoes defined in Exercises...
TAOCP 7.2.2.1 Exercise 288
Section 7.2.2.1: Dancing Links Exercise 288. [ 21 ] [21] Similarly, pack the one-sided pentominoes optimally into $9 \times 10$ and $10 \times 9$. Verified: no Solve time: 2m02s Solution Each one-sided pentomino is a fixed 5-cell polyomino with orientation distinguished up to rotation, but not reflection. There are $18$ such pieces. Each placement of a piece covers exactly $5$ unit cells. The board $9 \times 10$ contains $90$ cells,...
TAOCP 7.2.2.1 Exercise 287
Section 7.2.2.1: Dancing Links Exercise 287. ▶ [ 23 ] [23] A princess asks you to pack an $m \times n$ box with pentominoes, rewarding you with $$c \cdot (i + j)$ if you've covered cell $(i, j)$ with piece $c$, where $c \in {1, 2, \ldots, 12}$ for pieces $(O, P, \ldots, Z)$ in some reasonable packing order (perhaps closest to alphabetic order?). Use Algorithm X$^\dagger$ to maximize your...
TAOCP 7.2.2.1 Exercise 286
Section 7.2.2.1: Dancing Links Exercise 286. [ 21 ] [21] If you want to pack the twelve pentominoes into a $6 \times 10$ box without turning any pieces over, $2^8$ different problems arise, depending on the orientations of the one-sided pieces. Which of those 64 problems has (a) the fewest (b) the most solutions? Verified: no Solve time: 2m27s Solution Let the twelve pentominoes be the standard set, with each...
TAOCP 7.2.2.1 Exercise 285
Section 7.2.2.1: Dancing Links Exercise 285. [ 21 ] [21] (T. H. O'Beirne, 1961.) The one-sided pentominoes are the eighteen distinct 5-cell pieces that can arise if we aren't allowed to flip pieces over: Notice that there now are two versions of P, Q, R, S, Y, and Z. In how many ways can all eighteen of them be packed into rectangles? Verified: no Solve time: 2m24s Solution Each one-sided...
TAOCP 7.2.2.1 Exercise 283
Section 7.2.2.1: Dancing Links Exercise 283. [ 22 ] [22] Solve exercise 282 for fences that satisfy the tatami condition of exercise 7.1.4–215; No four edges of the tiles should come together at any "crossroads." Verified: no Solve time: 2m11s Solution Let $P$ be a fixed pentomino. A fence for $P$ consists of finitely many disjoint copies of $P$ that surround one central copy, such that every grid point that...
TAOCP 7.2.2.1 Exercise 282
Section 7.2.2.1: Dancing Links Exercise 282. ▶ [ 22 ] [22] (Craig S. Kaplan.) A polyomino can sometimes be surrounded by nonoverlapping copies of itself that form a fence : Every cell that touches the polyomino, even at a corner, is part of the fence; conversely, every piece of the fence touches the inner polyomino. Furthermore, the pieces must not enclose any unoccupied "holes." Find the (a) smallest and (b)...
TAOCP 7.2.2.1 Exercise 281
Section 7.2.2.1: Dancing Links Exercise 281. [ 20 ] [20] The white cells of a $(2n+1) \times (2n+1)$ checkerboard, with black corners, form an interesting graph called the Aztec diamond of order $n$; and the black cells form the Aztec diamond of order $n+1/2$. For example, the diamonds of orders $11/2$ and $13/2$ are (i) and (ii) except that (ii) has a "hole" of order $3/2$. Thus (i) has 61...
TAOCP 7.2.2.1 Exercise 278
Section 7.2.2.1: Dancing Links Exercise 278. ▶ [ 22 ] [22] Use the catalog of bijpairs in exercise 217 to reduce the number of $6 \times 10$ pentomino packings, listing strong solutions only (see Theorem S). How much time is saved? Verified: no Solve time: 1m45s Solution Let $\mathcal{P}$ denote the set of all $6 \times 10$ pentomino packings obtained by Algorithm X without symmetry reduction. Let $G$ be the...
TAOCP 7.2.2.1 Exercise 277
Section 7.2.2.1: Dancing Links Exercise 277. [ 25 ] [25] How many of the $6 \times 10$ pentomino packings are strongly three-colorable , in the sense that each individual piece could be colored red, white, or blue in such a way that no pentominoes of the same color touch each other, not even at corner points? Verified: no Solve time: 4m36s Corrected solution We restate the problem in a form...
TAOCP 7.2.2.1 Exercise 276
Section 7.2.2.1: Dancing Links Exercise 276. [ 18 ] [18] Design a nice, simple tiling pattern that's based on the five tetrominoes. Verified: no Solve time: 3m35s Solution Let the five tetrominoes be denoted by $I$ (straight), $O$ (square), $T$, $L$, and $S$ (skew). Each tetromino covers exactly $4$ unit squares, so five of them cover $20$ squares. A natural host region is therefore a $4\times 5$ rectangle. A periodic...
TAOCP 7.2.2.1 Exercise 275
Section 7.2.2.1: Dancing Links Exercise 275. [ 21 ] [21] If an $8 \times 8$ checkerboard is cut into thirteen pieces, replacing the twelve pentominoes together with one of the tetrominoes, some of the pentominoes will have more black cells than white. Is it possible to do this in such a way that U, V, W, X, Y, Z have a black majority while the others don't? Verified: no Solve...
TAOCP 7.2.2.1 Exercise 274
Section 7.2.2.1: Dancing Links Exercise 274. [ 21 ] [21] There are five different tetrominoes , namely $$\square \quad \square\square \quad \text{square} \qquad \text{straight} \qquad \text{skew} \qquad \text{ell} \qquad \text{tee}$$ In how many essentially different ways can each of them be packed into an $8 \times 8$ square together with the twelve pentominoes? Verified: no Solve time: 4m43s We restart from first principles and remove the two unsupported assumptions in...
TAOCP 7.2.2.1 Exercise 273
Section 7.2.2.1: Dancing Links Exercise 273. [ 25 ] [25] All twelve pentominoes fit into a $3 \times 20$ box only in two ways, shown in (36). a) How many ways are there to fit eleven of them into that box? b) In how many solutions to (a) are the five holes nonadjacent, kingwise? c) In how many ways can eleven pentominoes be packed into a $3 \times 19$ box?...
TAOCP 7.2.2.1 Exercise 272
Section 7.2.2.1: Dancing Links Exercise 272. [ 23 ] [23] Continuing exercise 271, explain how to find special kinds of packings: a) Those that decompose into $6 \times k$ and $6 \times (10-k)$. b) Those that have all twelve pentominoes touching the outer boundary. c) Those with all pentominoes touching that boundary except for V, which doesn't. d) Same as (c), with each of the other eleven pentominoes in place...
TAOCP 7.2.2.1 Exercise 271
Section 7.2.2.1: Dancing Links Exercise 271. [ 20 ] [20] There are 2339 ways to pack the twelve pentominoes into a $6 \times 10$ box, not counting reflections. What's a good way to find them all, using Algorithm X? Verified: no Solve time: 2m09s Solution A pentomino tiling of a $6\times 10$ rectangle can be encoded as an exact cover problem in the sense of Algorithm X, with items representing...
TAOCP 7.2.2.1 Exercise 270
Section 7.2.2.1: Dancing Links Exercise 270. [ 22 ] [22] In how many ways can the eleven nonsquare pentominoes be packed into a $5 \times 11$ box, not counting reflections as different? (Reduce symmetry cleverly.) Verified: no Solve time: 1m12s Solution Let the 11 nonsquare pentominoes be the free pentomino set with the $O$ pentomino removed. The total area is $11 \cdot 5 = 55$, matching the area of a...
TAOCP 7.2.2.1 Exercise 27
Section 7.2.2.1: Dancing Links Exercise 27. [ 22 ] Introduce secondary items cleverly into the options (a6), so that only planar solutions to Langford's problem are obtained. (See exercise 7.5–8.) Verified: yes Solve time: 1m17s Solution Let Langford’s problem be represented in the usual exact-cover form of Section 7.5–8, where each option represents a placement of a pair of equal integers $k$ into positions $i$ and $i+k+1$ of a length...
TAOCP 7.2.2.1 Exercise 269
Section 7.2.2.1: Dancing Links Exercise 269. [ 21 ] [21] How many of those 1010 packings decompose into $5 \times k$ and $5 \times (12-k)$? Verified: no Solve time: 1m20s Solution Let a decomposable packing be one in which a vertical line between columns $k$ and $k+1$ separates the $5\times 12$ rectangle into a $5\times k$ region and a $5\times(12-k)$ region, with no pentomino crossing the boundary. Then each pentomino...
TAOCP 7.2.2.1 Exercise 268
Section 7.2.2.1: Dancing Links Exercise 268. ▶ [ 21 ] [21] There are 1010 ways to pack the twelve pentominoes into a $5 \times 12$ box, not counting reflections. What's a good way to find them all, using Algorithm X? Verified: yes Solve time: 1m13s Solution The problem is an exact cover instance in the sense of (6)–(9): each legal placement of a pentomino on the $5\times 12$ board corresponds...
TAOCP 7.2.2.1 Exercise 267
Section 7.2.2.1: Dancing Links Exercise 267. [ 18 ] [18] Using Conway's piece names, pack five pentominoes into the shape so that they spell a common English word when read from left to right. Verified: no Solve time: 1m06s Solution Let the Conway pentomino names be used in their standard letter forms $F, I, L, N, P, T, U, V, W, X, Y, Z$. The problem asks for five pentominoes...
TAOCP 7.2.2.1 Exercise 263
Section 7.2.2.1: Dancing Links Exercise 263. [ 24 ] [24] Compare the time and space requirements of Algorithms C and Z when they are applied to (a) the 16 queens problem; (b) pentominoes, as in exercises 271 and 274; (c) MacMahon's triangle problem, as in exercise 126; (d) the generalized de Bruijn sequences of exercise 95; (e) the "right word stair" problem of exercise 90; (f) the $6 \times 6$...
TAOCP 7.2.2.1 Exercise 261
Section 7.2.2.1: Dancing Links Exercise 261. ▶ [ 23 ] [23] Let $G$ be a directed acyclic graph, with source vertices $S$ and sink vertices $T$. a) Use Algorithm C (or Z) to find all sets of $m$ vertex-disjoint paths from $S$ to $T$. b) Also find all such sets of paths from $s_k$ to $t_k$ for $1 \le k \le m$, given $s_k$ and $t_k$. c) Apply (a) to...
TAOCP 7.2.2.1 Exercise 26
Section 7.2.2.1: Dancing Links Exercise 26. [ 21 ] In how many ways can the queen graph $Q_8$ be colored in a "balanced" fashion, using eight queens of color 0 and seven each of colors 1 to 8? Verified: no Solve time: 3m55s The original solution fails at the only place where the problem becomes genuinely global: it replaces a coupled partition problem by a product of independent 7-queen counts....
TAOCP 7.2.2.1 Exercise 257
Section 7.2.2.1: Dancing Links Exercise 257. ▶ [ 20 ] [20] How does Algorithm Z do on the "extreme" exact cover problem, with $n$ items and $2^n - 1$ options? (See the discussion preceding (82).) a) What signatures $\sigma$ are formed in step Z2? b) Draw the schematic ZDD, analogous to (123), when $n = 4$. Verified: yes Solve time: 1m53s Solution The items are $1,2,\dots,n$. The $2^n-1$ options are...
TAOCP 7.2.2.1 Exercise 253
Section 7.2.2.1: Dancing Links Exercise 253. ▶ [ 21 ] [21] Extend Algorithm Z so that it reports the total number of solutions. Verified: yes Solve time: 1m32s Solution Let $Z$ denote Algorithm Z as in Section 7.2.2.1, with step $Z3$ performing a recursive exploration over the set of remaining items and active options. Let $\mathcal{S}(P)$ denote the number of solutions of a residual exact cover problem $P$ defined by...
TAOCP 7.2.2.1 Exercise 252
Section 7.2.2.1: Dancing Links Exercise 252. ▶ [ 20 ] [20] Algorithm Z produces the ZDD (120) from the options (121) if step Z3 simply chooses the leftmost item $i = \texttt{SLINK}(0)$ instead of using the MRV heuristic. What ZDD would have been obtained if the method of exercise 9 had been used instead? Verified: no Solve time: 1m56s Solution Let (121) denote the set of options defining the exact...
TAOCP 7.2.2.1 Exercise 251
Section 7.2.2.1: Dancing Links Exercise 251. [ 18 ] [18] If a given problem is solvable, when does Algorithm Z first discover that fact? Verified: yes Solve time: 1m30s Solution Algorithm Z operates by recursive search over partial exact covers, maintaining the invariant that the current data structure represents the residual exact cover instance induced by the choices already made. At each recursive level it selects an item $i$, covers...
TAOCP 7.2.2.1 Exercise 250
Section 7.2.2.1: Dancing Links Exercise 250. [ 21 ] [21] Users of Algorithm C* are allowed to supply hints that speed up the computation, by specifying (i) a set $Z$ of characters, such that every element of $Z$ is the first character of exactly one primary item in every option; also (ii) a number $z > 0$, meaning that every option contains exactly $z$ primary items whose names don't begin...
TAOCP 7.2.2.1 Exercise 25
Section 7.2.2.1: Dancing Links Exercise 25. [ 20 ] By setting up an exact cover problem and solving it with Algorithm X, show that the queen graph $Q_8$ (exercise 7.1.4–241) cannot be colored with eight colors. Verified: no Solve time: 2m42s Solution Let $Q_8$ be the graph whose vertices are the $64$ squares of an $8\times 8$ chessboard, with two vertices adjacent when a queen placed on one square attacks...
TAOCP 7.2.2.1 Exercise 249
Section 7.2.2.1: Dancing Links Exercise 249. [ 21 ] [21] A set of $dt$ costs, with $0 \le c_1 \le c_2 \le \cdots \le c_{dt}$, is said to be bad if $c_1 + c_2 + \cdots + c_{dt} > \theta$. Design an "online algorithm" that identifies a bad set as quickly as possible, when the costs are learned one by one in arbitrary order. For example, suppose $d = 6$,...
TAOCP 7.2.2.1 Exercise 248
Section 7.2.2.1: Dancing Links Exercise 248. [ 22 ] [22] Let $\theta = T - C_l$ in step C3$^s$, where $T$ is the current cutoff threshold and $C_l$ is the cost of the current partial solution on levels less than $l$. Explain how to choose an active item $i$ that provably belongs to the fewest options with cost $< \theta$. Instead of taking the time to make a complete search,...
TAOCP 7.2.2.1 Exercise 246
Section 7.2.2.1: Dancing Links Exercise 246. [ 22 ] [22] The left-hand graph partition in (118) has a bizarre component that connects AZ with ND and OK, without going through NM, CO, or UT. Would we obtain more reasonable-looking solutions if we kept the same options, but minimized the exterior costs instead of the squared populations? (That is, on the left we'd consider the 34,111 options with population in $[37,.,.,39]$...
TAOCP 7.2.2.1 Exercise 245
Section 7.2.2.1: Dancing Links Exercise 245. [ 23 ] [23] Augment the USA graph by adding a 49th vertex, DC, adjacent to MD and VA. Partition this graph into seven connected components, (a) all of size 7, removing as few edges as possible; (b) of any size, equalizing their populations as much as possible. Verified: no Solve time: 2m24s Solution Let $G$ be the USA graph on 48 states, and...
TAOCP 7.2.2.1 Exercise 240
Section 7.2.2.1: Dancing Links Exercise 240. [ 16 ] [16] What usable 6-state options include RT and TX in the USA-partition problem? Verified: no Solve time: 2m17s The original solution failed because it never used the actual USA-partition instance. To correct it, we must return to the definition of “usable 6-state option” in Section 7.2.2.1 and reason directly about the items involved. 1. Structure of usable 6-state options In the...
TAOCP 7.2.2.1 Exercise 24
Section 7.2.2.1: Dancing Links Exercise 24. [ 20 ] With Algorithm X, find all solutions to the $n$ queens problem that are unchanged when they're rotated by (a) 180°; (b) 90°. Verified: yes Solve time: 2m Solution An $n$-queens solution is a permutation $p$ of ${1,\dots,n}$ such that queens are placed at $(i,p(i))$ and no two attack each other. The geometric symmetries act on these placements by mapping lattice points....
TAOCP 7.2.2.1 Exercise 238
Section 7.2.2.1: Dancing Links Exercise 238. [ 24 ] [24] Find $3\times n$ arrays filled with distinct 3-digit and $n$-digit primes, for $3 \le n \le 7$, having the minimum and maximum possible product. Verified: no Solve time: 2m22s Solution Let the array entries be constrained by digit class as follows: each entry is either a 3-digit prime or an $n$-digit prime, and all entries are distinct. In a $3\times...
TAOCP 7.2.2.1 Exercise 235
Section 7.2.2.1: Dancing Links Exercise 235. ▶ [ 21 ] Solve the problem of Fig. 74 using the (integer) costs $$4d(i,j)^4$. Verified: yes Solve time: 2m07s Solution Let the board be $16 \times 16$ with rows and columns indexed by $i,j \in {1,\dots,16}$. Let the center coordinate be $c=\frac{17}{2}$. Knuth’s distance is $d(i,j)^2=(i-c)^2+(j-c)^2,$ so the cost function is $$4d(i,j)^4 = 4\big((i-c)^2+(j-c)^2\big)^2.$ A placement of 16 nonattacking queens corresponds to a...
TAOCP 7.2.2.1 Exercise 233
Section 7.2.2.1: Dancing Links Exercise 233. [ 20 ] Minimize and maximize the product of the 16 queen distances, not the sum. Verified: yes Solve time: 1m58s Solution Let the 16-queens problem of Fig. 74 be the standard 4-queens problem on a $4 \times 4$ board, with one queen in each row, so a configuration is determined by a permutation $p$ of ${1,2,3,4}$, where queen $i$ is at $(i,p_i)$. Let...
TAOCP 7.2.2.1 Exercise 232
Section 7.2.2.1: Dancing Links Exercise 232. [ 20 ] The costs supplied to Algorithm X$^8$ must be integers. Does it matter that $d(i,j)$ in the 16 queens problem of Fig. 74 is never an integer. Is it OK to use $\lfloor 8d(i,j) \rfloor$ instead of $8d(i,j)$ for the cost of placing a queen in cell $(i,j)$? Verified: yes Solve time: 1m43s Solution Let a placement of 16 queens be an...
TAOCP 7.2.2.1 Exercise 231
Section 7.2.2.1: Dancing Links Exercise 231. [ 21 ] What's the maximum SCRABBLE$^{\circledR}$-like score you can achieve by filling the grid below with 4-letter and 5-letter words that all are among the (a) 1000 (b) 2000 (c) 3000 most common words of English having that many letters? $$\begin{array}{llllllll} A_1\ B_3\ C_3\ D_2\ E_1\ F_4\ G_2 \ B_3\ I_1\ J_8\ K_5\ L_1\ M_3\ N_1 \ O_1\ P_3\ Q_{10}\ R_1\ S_1\ T_1...
TAOCP 7.2.2.1 Exercise 230
Section 7.2.2.1: Dancing Links Exercise 230. [ 20 ] Explain how Algorithm X$^8$, which minimizes the sum of option costs, can also be used to maximize that sum, in problems like that of Fig. 74. Verified: yes Solve time: 1m31s Solution Let each option $O$ in the instance of Fig. 74 carry a cost $c(O)$, and let a solution $S$ of Algorithm X$^8$ be a set of pairwise disjoint options...
TAOCP 7.2.2.1 Exercise 229
Section 7.2.2.1: Dancing Links Exercise 229. [ 25 ] What Langford pairings for $n = 16$ are lexicographically smallest and largest? Verified: no Solve time: 2m28s Solution A Langford pairing of order $n$ is a sequence $a_1,\dots,a_{2n}$ containing each symbol $k \in {1,\dots,n}$ exactly twice, with the two occurrences separated by exactly $k$ positions, so that if the first occurrence of $k$ is at position $i$, then the second occurs...
TAOCP 7.2.2.1 Exercise 225
Section 7.2.2.1: Dancing Links Exercise 225. [ 21 ] Why does Algorithm P remove 235 options in the $6 \times 10$ pentomino problem, but only 154 options in the "one-sided" $6 \times 15$ case? Verified: no Solve time: 2m14s Solution In Algorithm P, the number of options removed during a covering step equals the number of nodes eliminated from the vertical lists of items that are deleted together with the...
TAOCP 7.2.2.1 Exercise 223
Section 7.2.2.1: Dancing Links Exercise 223. [ 20 ] Implement the output phase of Algorithm P (step P10). Verified: no Solve time: 1m57s Solution Let $S$ denote the stack of options accumulated in step P7. Each element of $S$ is a pointer to a node belonging to an option, and each option is represented in memory as a contiguous sequence of nodes separated by spacer nodes, characterized by $\text{TOP}(x) \le...
TAOCP 7.2.2.1 Exercise 222
Section 7.2.2.1: Dancing Links Exercise 222. [ 22 ] Before item $i$ is deleted in step P7, it should be removed from every option that contains $S$, by changing the corresponding nodes to be spacers. All options that involve $i$ but not $S$ should also be deleted. Spell out the low-level details of this process. Verified: no Solve time: 2m15s Solution Let item $i$ be the item to be deleted...
TAOCP 7.2.2.1 Exercise 218
Section 7.2.2.1: Dancing Links Exercise 218. [ 20 ] Why must $i$ be uncolored, in the definition of "forcing" for Algorithm P? Verified: no Solve time: 23s Understood. For any TAOCP Volume 4 exercise you provide, I'll write the solution in the style of a rigorous solution manual: Use Knuth's notation and terminology exactly as they appear in the relevant section. Give complete proofs rather than intuitive arguments. Justify every...
TAOCP 7.2.2.1 Exercise 216
Section 7.2.2.1: Dancing Links Exercise 216. [ 25 ] Continuing exercise 215, experiment with the search trees that are obtained by (i) choosing a random ordering of the options, and (ii) using only $m$ of the quadruples that are excluded by that ordering (again chosen at random). Verified: no Solve time: 2m13s Corrected solution 1. Correct structure of the problem In Exercise 215, the underlying instance is an exact cover...
TAOCP 7.2.2.1 Exercise 214
Section 7.2.2.1: Dancing Links Exercise 214. [ 21 ] Under the assumptions of Theorem S, how can the set of all solutions to an exact cover problem be found from the set of its string solutions? Verified: no Solve time: 1m54s Solution Let a string solution be a sequence of options produced by the search procedure, where the same underlying exact cover solution may appear in different orders depending on...
TAOCP 7.2.2.1 Exercise 210
Section 7.2.2.1: Dancing Links Exercise 210. [ 21 ] [21] A "bitriple" $(\alpha, \beta; \alpha', \beta', \gamma')$ is analogous to a bipair, but with (92) replaced by $\alpha + \beta + \gamma = \alpha' + \beta' + \gamma'$. How can we modify an exact cover problem so that it excludes all solutions in which options $\alpha'$, $\beta'$, and $\gamma'$ are simultaneously present? Verified: no Solve time: 2m34s Solution Let the...
TAOCP 7.2.2.1 Exercise 21
Section 7.2.2.1: Dancing Links Exercise 21. [ 22 ] Extend exercise 20 to $k$-wise ordering of $km$ options $a_{ij}$, for $1 \le i \le k$ and $0 \le j < m$. The solutions should be $(a_{1,j_1}, \ldots, a_{k,j_k})$ with $0 \le j_1 \le \cdots \le j_k < m$. Again there should be at most $\lfloor \lg m \rfloor$ secondary items in each option. Verified: no Solve time: 3m10s The flaw...
TAOCP 7.2.2.1 Exercise 208
Section 7.2.2.1: Dancing Links Exercise 208. ▶ [ 21 ] [21] Modify the exact cover problem of Fig. 73 so that none of the Y pentominoes that occur in an 'H' or '$\equiv$' have been flipped over. Hint: To prevent the flipped-over Y's marked 8 and b from occurring simultaneously, use the options '1c 2e 3e 4c 3b $V_{1b}$' and '1a 2a 3a 4a 2b $V_{1b}$', where $V_{1b}$ is a...
TAOCP 7.2.2.1 Exercise 20
Section 7.2.2.1: Dancing Links Exercise 20. ▶ [ 25 ] Generalize (a6) to a pairwise ordering of options $(a_{i_0}, \ldots, a_{i_{k-1}}; b_0, \ldots, b_{n-1})$ that uses at most $\lfloor \lg m \rfloor$ of the secondary items $y_1, \ldots, y_{n-1}$ in each option. Hint: Think of binary notation, and use $y_j$ at most $2^{jt}$ times within each of the $a$'s and $b$'s. Verified: no Solve time: 4m36s Let $m$ be the...
TAOCP 7.2.2.1 Exercise 19
Section 7.2.2.1: Dancing Links Exercise 19. ▶ [ 21 ] Modify Algorithm X so that it doesn't require the presence of any primary items in the options. A valid solution should not contain any purely secondary options; but it must intersect every such option. (For example, if only items $a$ and $b$ of (6) were primary, the only valid solution would be to choose options "$a\ d\ g$" and "$b\...
TAOCP 7.2.2.1 Exercise 183
Section 7.2.2.1: Dancing Links Exercise 183. [ 16 ] Let $\tilde{B}(n)$ be the maximum number of nodes in Algorithm X's search tree, taken over all strict exact cover problems with $n$ options. What is $\limsup_{n\to\infty} \tilde{B}(n)^{1/n}$? Verified: no Solve time: 4m52s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in...
TAOCP 7.2.2.1 Exercise 182
Section 7.2.2.1: Dancing Links Exercise 182. [ 21 ] (D. Eppstein, 2008.) Find a strict exact cover problem with 8 options, whose search tree contains 16 nodes and 7 solutions. Verified: no Solve time: 5m18s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2...
TAOCP 7.2.2.1 Exercise 171
Section 7.2.2.1: Dancing Links Exercise 171. [ 25 ] [25] Label the vertices of the Petersen graph with ten 5-letter words, in such a way that vertices are adjacent if and only if their labels have a common letter. Verified: no Solve time: 6m39s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly...
TAOCP 7.2.2.1 Exercise 170
Section 7.2.2.1: Dancing Links Exercise 170. [ 22 ] [22] Continuing exercise 169, generate all of $G$'s $t$-element kernels , its maximal independent sets. (Your formulation will now need additional items and options.) Verified: no Solve time: 3m39s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying...
TAOCP 7.2.2.1 Exercise 17
Section 7.2.2.1: Dancing Links Exercise 17. [ 16 ] [16] Repeat exercise 16, but consider $a_j$ and $b_j$ to be secondary items (thereby omit the slack options (24)). Compare the primary items in each option in order $r_2, c_1, r_2, c_2, r_4, c_4, r_1, c_1$. Verified: no Solve time: 4m48s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n =...
TAOCP 7.2.2.1 Exercise 169
Section 7.2.2.1: Dancing Links Exercise 169. ▶ [ 22 ] [22] Let $G$ be a graph with $n$ vertices. Formulate the problem of finding all of its $t$-element independent sets as an MCC problem with $1 + n$ items and $n$ options. Verified: no Solve time: 3m10s Solution Let the vertices of $G$ be $v_1, v_2, \dots, v_n$. Introduce $1+n$ items in the MCC instance: one special primary item $c$...
TAOCP 7.2.2.1 Exercise 167
Section 7.2.2.1: Dancing Links Exercise 167. [ 22 ] [22] Let $M_p$ be the number of options that involve the primary item $p$ in a given MCC problem, and suppose that the upper bound on $p$'s multiplicity is $\ge M_p$. Does the precise value of this upper bound affect the behavior of Algorithm M? (In other words, does $v_p = \infty$ lead to the same running time as $v_p =...
TAOCP 7.2.2.1 Exercise 166
Section 7.2.2.1: Dancing Links Exercise 166. [ 21 ] [21] Extend answer 9 to the more general situation that arises in Algorithm M: a) Let $\theta_p$ be the number of different choices that will be explored at the current position of the search tree if primary item $p$ is selected for branching. Express $\theta_p$ as a function of $\text{LEN}(p)$, $\text{SLACK}(p)$, and $\text{BOUND}(p)$. b) Suppose $\theta_p = \theta_{p'}$ and $\text{SLACK}(p) =...
TAOCP 7.2.2.1 Exercise 164
Section 7.2.2.1: Dancing Links Exercise 164. [ 17 ] [17] When Algorithm M finds a solution $x_0 x_1 \ldots x_{t-1}$ in step M2, some of the nodes $x_j$ might represent the fact that some primary item will appear in no further options. Explain how to handle this "null" case, by modifying answer 13. Verified: no Solve time: 5m15s Setup We seek all integers $n < 10^9$ such that the equation...
TAOCP 7.2.2.1 Exercise 163
Section 7.2.2.1: Dancing Links Exercise 163. [ 20 ] [20] Explain the possible rules for setting $p$ in (12). Verified: no Solve time: 1m25s Solution In (12), the variable $p$ is used to traverse exactly the vertical list of nodes that correspond to active options containing item $i$. This vertical list is a circular doubly linked list whose header is the item node $i$, with $\mathrm{DLINK}(i)$ pointing to the first...
TAOCP 7.2.2.1 Exercise 162
Section 7.2.2.1: Dancing Links Exercise 162. [ 24 ] [24] Say that a $\mathcal{Q}_n$ is an $n \times n$ array of $n$ nonattacking queens. Sometimes a $\mathcal{Q}_m$ contains a $\mathcal{Q}_n$ for $m < n$; for example, eight of the possible $\mathcal{Q}_5$'s contain a $\mathcal{Q}_4$, and the $\mathcal{Q}_7$ illustrated here contains both a $\mathcal{Q}_3$ and a $\mathcal{Q}_5$. What is the smallest $n$ such that at least one $\mathcal{Q}_n$ contains (a) two...
TAOCP 7.2.2.1 Exercise 161
Section 7.2.2.1: Dancing Links Exercise 161. ▶ [ 23 ] [23] Three restricted queen-domination problems are prominent in the literature: i) No two queens of a solution attack each other. ii) Each queen of a solution is attacked by at least one of the others. iii) The queens of a solution form a clique. (The third and fourth examples in (6§) are instances of types (ii) and (i).) Explain how...
TAOCP 7.2.2.1 Exercise 160
Section 7.2.2.1: Dancing Links Exercise 160. [ 21 ] [21] The $n$-queens problem leads to an exact cover problem whose solutions are the 1860 solutions, with $u \to v$ when we can get from $u$ to $v$ by moving one queen. How many connected components does this graph have? Is one of them a "giant"? Verified: no Solve time: 3m31s Solution Let a configuration be a placement of $n$ queens...
TAOCP 7.2.2.1 Exercise 16
Section 7.2.2.1: Dancing Links Exercise 16. [ 16 ] [16] What are the solutions to the four queens problem, as formulated in (23) and (24)? What branches are taken at the top four levels of Algorithm X's search tree? Verified: no Solve time: 2m39s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly...
TAOCP 7.2.2.1 Exercise 159
Section 7.2.2.1: Dancing Links Exercise 159. ▶ [ 21 ] [21] Suggest a way to speed up the text's solution to the 5-queens problem, by using the symmetries of a square to modify the items and options of (6$\frac{1}{2}$). Verified: no Solve time: 5m07s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly...
TAOCP 7.2.2.1 Exercise 158
Section 7.2.2.1: Dancing Links Exercise 158. [ 25 ] [25] Another variation of the partridge puzzle when $2 \le n \le 7$ asks for the smallest rectangular area that will contain $k$ nonoverlapping squares of size $k \times k$ for $1 \le k \le n$. For example, here are solutions for $n = 2$, 3, and 4: (To show optimality for $n = 4$ one must prove that rectangles of...
TAOCP 7.2.2.1 Exercise 157
Section 7.2.2.1: Dancing Links Exercise 157. [ 22 ] [22] Continue the study of small partridges by extending (6a) to $n = 4$ and 7. Verified: no Solve time: 5m13s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let...
TAOCP 7.2.2.1 Exercise 155
Section 7.2.2.1: Dancing Links Exercise 155. [ 20 ] [20] That "authentic" partridge puzzle has a square solution when $n = 6$. a) How many different solutions does it have in that case? b) The affinity score of a partridge packing is the number of internal edges that lie on the boundary between two squares of the same size. (In (6a) the scores are 165 and 67.) What solutions to...
TAOCP 7.2.2.1 Exercise 153
Section 7.2.2.1: Dancing Links Exercise 153. [ 25 ] [25] Here are six of the path dominoes, plus a "start" piece and a "stop" piece: a) Place them within a $4 \times 5$ array so that they define a path from "start" to "stop." b) How many distinct "start" or "stop" pieces are possible, if they're each supposed to contain a single subpath together with a single terminal point? c)...
TAOCP 7.2.2.1 Exercise 150
Section 7.2.2.1: Dancing Links Exercise 150. [ 24 ] Here's a classic 19th century puzzle that was the first of its kind: "Arrange all the pieces to fill the square … so that all the links of the Chain join together, forming an Endless Chain. The Chain may be any shape, so long as all the links join together, and all the pieces are used. This Puzzle can be done...
TAOCP 7.2.2.1 Exercise 15
Section 7.2.2.1: Dancing Links Exercise 15. [ 20 ] [20] The options in (16) give us every solution to the Langford pair problem twice, because the left-right reversal of any solution is also a solution. Show that, if a few of those options are removed, we get not only half as many solutions; the others will be the reversals of the solutions found. Verified: yes Solve time: 2m23s Solution Let...
TAOCP 7.2.2.1 Exercise 148
Section 7.2.2.1: Dancing Links Exercise 148. [ 24 ] Find all distinct cubes whose faces are colored a , b , or c , when opposite faces are required to have different colors. Then arrange them into a symmetric shape (with matching colors wherever they are in contact). Verified: no Solve time: 4m02s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots...
TAOCP 7.2.2.1 Exercise 14
Section 7.2.2.1: Dancing Links Exercise 14. ▶ [ 20 ] [20] [ Problème des ménages. ] "In how many ways can $n$ male-female couples sit at a circular table, with men and women alternating, and with no couple together?" a) Suppose the women have already been seated, and let the vacant seats be $(S_0, S_1, \ldots, S_{n-1})$. Let $M_j$ be the spouse of the woman between seats $S_j$ and $S_{(j+1)...
TAOCP 7.2.2.1 Exercise 138
Section 7.2.2.1: Dancing Links Exercise 138. [ 25 ] [25] [ Heads and tails. ] Here's a set of 24 square tiles that MacMahon missed(!): They each show two "heads" and two "tails" of triangles, in four colors that exhibit all possible permutations, with heads pointing to tails. The tiles can be rotated, but not flipped over. We can match them properly in many ways, such as where the $4...
TAOCP 7.2.2.1 Exercise 137
Section 7.2.2.1: Dancing Links Exercise 137. [ 22 ] [22] A popular puzzle called Drive Ya Nuts consists of seven "hex nuts" that have been decorated with permutations of the numbers ${1, 2, 3, 4, 5, 6}$. The object is to arrange them as shown, with numbers matching at the edges. a) Show that this puzzle has a unique solution, with that particular set of seven. (Reflections of the nuts...
TAOCP 7.2.2.1 Exercise 135
Section 7.2.2.1: Dancing Links Exercise 135. [ 23 ] (H. L. Nelson, 1970.) Show that MacMahon's squares of exercise 133 can be used to wrap around the faces of a $2 \times 2 \times 2$ cube, matching colors wherever adjacent. Verified: no Solve time: 5m15s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has...
TAOCP 7.2.2.1 Exercise 134
Section 7.2.2.1: Dancing Links Exercise 134. [ 23 ] The nonwhite areas of the pattern in exercise 133 form polyominoes (rotated 45°); in fact, the lighter color has an S pentomino, while the darker color has both P and V. How often do each of the twelve pentominoes occur, among all of the solutions? Verified: no Solve time: 4m06s Setup We seek all integers $n < 10^9$ such that the...
TAOCP 7.2.2.1 Exercise 133
Section 7.2.2.1: Dancing Links Exercise 133. [ 21 ] (P. A. MacMahon, 1921.) A set of 24 square tiles can be constructed, analogous to the triangular tiles of (§8), if we restrict ourselves to just three colors. For example, they can be arranged in a $4 \times 6$ rectangle as shown, with all-white border. In how many ways can this be done? Verified: no Solve time: 5m13s Setup We seek...
TAOCP 7.2.2.1 Exercise 13
Section 7.2.2.1: Dancing Links Exercise 13. [ 16 ] [16] When Algorithm X finds a solution in step X2, how can we use the values of $x_0, x_1, \ldots, x_{l-1}$ to figure out what that solution is? Verified: no Solve time: 4m10s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution...
TAOCP 7.2.2.1 Exercise 128
Section 7.2.2.1: Dancing Links Exercise 128. [ 25 ] [25] Eleven of MacMahon's triangles (28) involve only the first three colors (not black). Arrange them into a pleasant pattern that tiles the entire plane when replicated. Verified: no Solve time: 5m09s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in...
TAOCP 7.2.2.1 Exercise 12
Section 7.2.2.1: Dancing Links Exercise 12. ▶ [ 23 ] [23] Design an algorithm that prints the option associated with a given node $x$, cyclically ordering the option so that TOP$(x)$ is its first item. Also print the position of that option in the vertical list for that item. (For example, if $x = 21$ in Table 1, your algorithm should print '$d\ f\ a$' and state that it's option...
TAOCP 7.2.2.1 Exercise 118
Section 7.2.2.1: Dancing Links Exercise 118. [ 21 ] (Hypergraph coloring.) Color the 64 cells of a chessboard with four colors, so that no three cells of the same color lie in a straight line of any slope. Verified: no Solve time: 5m09s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one...
TAOCP 7.2.2.1 Exercise 117
Section 7.2.2.1: Dancing Links Exercise 117. ▶ [ 21 ] (Graph coloring.) Suppose we want to find all possible ways to label the vertices of graph $G$ with $c$ colors: adjacent vertices should have different colors. a) Formulate this as an exact cover problem, with one primary item for each vertex and with $d$ secondary items for each edge. b) Sometimes $G$'s edges are conveniently specified by giving a family...
TAOCP 7.2.2.1 Exercise 113
Section 7.2.2.1: Dancing Links Exercise 113. [ 21 ] [21] An 'alphabet block' is a cube whose six faces are marked with letters. Is there a set of five alphabet blocks that are able to spell the 25 words TREES, NODES, STACK, AVAIL, FIRST, RIGHT, ORDER, LIST, GIVEN, LINKS, QUEUE, GRAPH, TIMES, BLOCK, VALUE, TABLE, FIELD, EDGE, ABOVE, POINT, THREE, LINK, HENCE, QUITE, DEBUG? (Each of these words appears more...
TAOCP 7.2.2.1 Exercise 111
Section 7.2.2.1: Dancing Links Exercise 111. [ 21 ] [21] Find all $8 \times 8$ crossword puzzle diagrams that contain exactly (a) 12 3-letter words, 12 4-letter words, and 4 5-letter words; (b) 12 5-letter words, 8 2-letter words, and 4 8-letter words; and (c) would have no words of other lengths. Verified: no Solve time: 5m07s Setup We seek all integers $n < 10^9$ such that the equation $x_1...
TAOCP 7.2.2.1 Exercise 11
Section 7.2.2.1: Dancing Links Exercise 11. ▶ [ 21 ] [21] Play through Algorithm X by hand, using exercise 9 in step X3 and the input in Table 1, until first reaching step X7. What are the contents of memory at that time? Verified: no Solve time: 5m11s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2...
TAOCP 7.2.2.1 Exercise 107
Section 7.2.2.1: Dancing Links Exercise 107. ▶ [ 23 ] [23] Pack as many of the following words as possible into a $9 \times 9$ array, simultaneously satisfying the rules of both word search and sudoku: ACRE COMPARE CORPORATE MACRO MOTET ROAM ART COMPUTER CROP META PARAMETER TAME Verified: no Solve time: 5m21s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots...
TAOCP 7.2.2.1 Exercise 106
Section 7.2.2.1: Dancing Links Exercise 106. [ 22 ] [22] Also pack two copies of ONE, TWO, THREE, FOUR, FIVE into a $5 \times 5$ square. Verified: no Solve time: 5m11s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$....
TAOCP 7.2.2.1 Exercise 105
Section 7.2.2.1: Dancing Links Exercise 105. [ 22 ] [22] Using the "word search puzzle" conventions of Figs. 71 and 72, show that the words ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ELEVEN, and TWELVE can all be packed into a $6 \times 6$ square, leaving one cell untouched. Verified: no Solve time: 5m07s Setup We seek all integers $n < 10^9$ such that the equation $x_1...
TAOCP 7.2.2.1 Exercise 102
Section 7.2.2.1: Dancing Links Exercise 102. ▶ [ 25 ] [25] Explain how to find all solutions to a Japanese arrow puzzle with Algorithm C. (See exercise 7.2.2–68.) Verified: no Solve time: 5m12s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots...
TAOCP 7.2.2.1 Exercise 101
Section 7.2.2.1: Dancing Links Exercise 101. ▶ [ 25 ] [25] (The zebra puzzle.) Formulate the following query as an XCC problem: "Five people, from five different countries, have five different occupations, own five different pets, drink five different beverages, and live in a row of five differently colored houses. The Englishman lives in a red house. The yellow house holds a diplomat. The Norwegian's house is the leftmost. The...
TAOCP 7.2.2.1 Exercise 10
Section 7.2.2.1: Dancing Links Exercise 10. [ 20 ] [20] In some applications the MRV heuristic of exercise 9 leads the search astray, because certain primary items have short lists yet convey little information about desirable choices. Modify answer 9 so that an item $p$ whose name does not begin with the character '#' will be chosen only if LEN$(p) \le 1$ or no other choices exist. (This tactic is...
TAOCP 7.2.2.1 Exercise 9
Section 7.2.2.1: Dancing Links Exercise 9. [ 18 ] [18] Explain how to branch in step X3 on an item $i$ for which LEN($i$) is minimum. If several items have that minimum value, $i$ should also be a minimum. (This choice is often called the "minimum remaining values" (MRV) heuristic.) Verified: no Solve time: 3m03s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2...
TAOCP 7.2.2.1 Exercise 8
Section 7.2.2.1: Dancing Links Exercise 8. [ 22 ] [22] Design an algorithm to set up the initial memory contents of an exact cover problem, as needed by Algorithm X and illustrated in Table 1. The input to your algorithm should consist of a sequence of lines with the following format: The very first line lists the names of all items. Each remaining line specifies the items of a particular...
TAOCP 7.2.2.1 Exercise 7
Section 7.2.2.1: Dancing Links Exercise 7. [ 16 ] [16] Why is TOP(23) $= -4$ in Table 1? Why is DLINK(23) $= 25$? Verified: no Solve time: 5m07s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be...
TAOCP 7.2.2.1 Exercise 5
Section 7.2.2.1: Dancing Links Exercise 5. [ 18 ] [18] Among the many combinatorial problems that can be formulated in terms of 0–1 matrices, some of the most important deal with families of sets : The columns of the matrix represent elements of a given universe, and the rows represent subsets of that universe. The exact cover problem is to partition the elements of the universe into such subsets. In...
TAOCP 7.2.1.6 Exercise 98
Section 7.2.1.6: Generating All Trees Exercise 98. [ 16 ] What series-parallel graph corresponds to (53) if $A$ is taken to be serial ? Verified: no Solve time: 4m31s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$...
TAOCP 7.2.2.1 Exercise 3
Section 7.2.2.1: Dancing Links Exercise 3. [ 20 ] [20] An $m \times n$ matrix that's supposed to be exactly covered can be regarded as a set of $n$ simultaneous equations in $m$ unknowns. For example, (5) is equivalent to $$x_2 + x_3 = x_3 + x_5 + x_6 = x_2 + x_5 = x_3 + x_4 = x_1 + x_4 = x_2 + x_3 + x_4 + x_6 =...
TAOCP 7.2.1.6 Exercise 94
Section 7.2.1.6: Generating All Trees Exercise 94. [ 22 ] Algorithm S needs to "prime the pump" by finding an initial spanning tree in step S1. Explain how to do that task. Verified: yes Solve time: 2m14s Solution Algorithm S operates by transforming one spanning tree into another while maintaining a valid spanning tree structure throughout its execution. The mechanism assumes that an initial spanning tree of the input graph...
TAOCP 7.2.1.6 Exercise 93
Section 7.2.1.6: Generating All Trees Exercise 93. [ 20 ] When Algorithm S terminates, has it restored the graph to its original state? Verified: yes Solve time: 1m34s Solution Algorithm S enumerates spanning trees by performing a sequence of local transformations on the current graph representation, each transformation replacing one edge choice with another admissible edge while maintaining connectivity constraints. Each such modification is performed in a controlled search structure...
TAOCP 7.2.2 Exercise 78
Section 7.2.2: Backtracking Exercise 78. [ 22 ] [22] Extend the algorithm of exercise 77 to weighted graphs, in which every vertex has a nonnegative weight: Generate all of the connected induced subgraphs whose total weight $w$ satisfies $L \le w < U$. Verified: no Solve time: 5m25s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2...
TAOCP 7.2.2 Exercise 76
Section 7.2.2: Backtracking Exercise 76. [ 23 ] Use the algorithm of exercise 75 to generate all of the connected $n$-element subsets of a given graph $G$. How many such subsets does $P_m \mathbin{\square} P_n$ have, for $1 \le n \le 9$? Verified: no Solve time: 1m09s Solution Let $G = P_m \mathbin{\square} P_n$, where vertices are ordered pairs $(i,j)$ with $1 \le i \le m$, $1 \le j \le...
TAOCP 7.2.2 Exercise 74
Section 7.2.2: Backtracking Exercise 74. [ 21 ] The fifteenth mystery word in exercise 73 is $\underline{\phantom{xxxxx}}$. Why does its special form lead to a partial factorization of that problem? Verified: no Solve time: 5m11s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2...
TAOCP 7.2.1.6 Exercise 78
Section 7.2.1.6: Generating All Trees Exercise 78. [ 20 ] [20] True or false: If $\sigma_1 \ldots \sigma_r$ is a row of the Christmas tree pattern, so is $\sigma_1^- \ldots \sigma_r^-$ (the reverse sequence of reverse complements). Verified: no Solve time: 5m44s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution...
TAOCP 7.2.1.6 Exercise 77
Section 7.2.1.6: Generating All Trees Exercise 77. [ 21 ] [21] Design an algorithm to generate the sequence of rightmost elements $a_1 \ldots a_n$ of the rows of the Christmas tree pattern of order $n$. Hint: These bit strings are characterized by the property that $a_1 + \cdots + a_k \ge k/2$ for $0 \le k \le n$. Verified: no Solve time: 5m57s Setup We seek all integers $n <...
TAOCP 7.2.2 Exercise 66
Section 7.2.2: Backtracking Exercise 66. ▶ [ 23 ] (The Fool's Disk.) "Rotate the four disks of the left-hand illustration below so that the four numbers on each ray sum to 12." (The current sums are $1+3+2+4 = 13$, etc.) Show that this problem factors nicely, so that it can be solved readily by hand. The Fool's Disk $\qquad\qquad\qquad\qquad\qquad$ The Royal Aquarium Thirteen Puzzle Verified: yes Solve time: 2m21s Let...
TAOCP 7.2.2 Exercise 65
Section 7.2.2: Backtracking Exercise 65. [ 25 ] Show that the generalized "Instant Insanity" problem, with $n$ cubes and $n$ colors on their faces, is NP-complete, even though cases with small $n$ are fairly easy. Verified: no Solve time: 4m05s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive...
TAOCP 7.2.2 Exercise 62
Section 7.2.2: Backtracking Exercise 62. ▶ [ 22 ] When the faces of four cubes are colored randomly with four colors, estimate the probability that the corresponding "Instant Insanity" puzzle has a unique solution. How many 2-regular graphs tend to appear during the "factored" solution process? Verified: no Solve time: 3m08s Solution Each cube has six faces colored independently with four colors. The faces are grouped into three opposite pairs,...
TAOCP 7.2.1.6 Exercise 70
Section 7.2.1.6: Generating All Trees Exercise 70. ▶ [ 20 ] [20] Find a simple rule that defines, for every bit string $\sigma$, another bit string $\sigma'$ called its mate , with the following properties: (i) $\sigma'' = \sigma$; (ii) $|\sigma'| = |\sigma|$; (iii) either $\sigma \le \sigma'$ or $\sigma' \le \sigma$; (iv) $\nu(\sigma) + \nu(\sigma') = |\sigma|$. Verified: yes Solve time: 1m42s Solution Let $\sigma = a_1 a_2 \cdots...
TAOCP 7.2.1.6 Exercise 69
Section 7.2.1.6: Generating All Trees Exercise 69. [ 20 ] [20] Are the Christmas tree patterns of orders 6 and 7 visible in Table 4, possibly in slight disguise? Verified: no Solve time: 4m17s The flaw in the previous solution is that it never identifies the actual objects in Table 4, nor uses the concrete form of the “Christmas tree” patterns. The argument must be rebuilt from the definition of...
TAOCP 7.2.2 Exercise 60
Section 7.2.2: Backtracking Exercise 60. ▶ [ 20 ] Experiment with biased random walks (see exercise 54), weighting each non-dead-end king move to a new vertex $v$ by $1 + \text{DIST}(v)^2$ instead of choosing every such move with the same probability. Does this strategy improve on Fig. 70? Verified: yes Solve time: 4m17s Which specific exercise or problem from TAOCP Volume 4 (and which section / fascicle) would you like...
TAOCP 7.2.1.6 Exercise 66
Section 7.2.1.6: Generating All Trees Exercise 66. [ 21 ] [21] A Schröder tree is a binary tree in which every nonnull right link is colored either white or black. The number $S_n$ of $n$-node Schröder trees is $$\begin{array}{ccccccccccccc} n = & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 \ S_n =...
TAOCP 7.2.1.6 Exercise 64
Section 7.2.1.6: Generating All Trees Exercise 64. [ 20 ] [20] What sequence of $X$ values in Algorithm R corresponds to the decorated trees of (24), and what are the final values of $L_0 L_1 \ldots L_{32}$? Verified: no Solve time: 3m16s The exercise, as stated in your prompt, cannot be completed because it is missing the data that defines the computation. Exercise 7.2.1.6.64 is not a self contained mathematical...
TAOCP 7.2.1.6 Exercise 62
Section 7.2.1.6: Generating All Trees Exercise 62. [ 22 ] [22] A binary tree can also be specified by its right subtree bit strings $(l_j, r_j, r_1, \ldots, r_s)$, where $l_j$ and $r_j$ tell whether the left and right subtrees of node $j$ in preorder are nonempty. (See Theorem 2.3.1A.) Prove that if $l_1, l_s \ldots r_1, r_s$ is an arbitrary bit string where with $l_1 + \cdots + l_s...
TAOCP 7.2.2 Exercise 49
Section 7.2.2: Backtracking Exercise 49. [ 20 ] $[20]$ Explain how the choices in Fig. 69 were determined from the "random" bits that are displayed. For instance, why was $X_4$ set to 1 in Fig. 69(b)? Verified: no Solve time: 5m43s I don’t see the exercise statement or the reviewer feedback yet. Please paste: The TAOCP exercise (or at least its full statement), and The proposed solution plus the reviewer’s...
TAOCP 7.2.2 Exercise 44
Section 7.2.2: Backtracking Exercise 44. ▶ [ 25 ] $[25]$ Spell out the low-level implementation details of the candidate selection process in step C2 of Algorithm C. Use the routine store$(n, c)$ of (26) whenever changing the contents of MEM. Assume the following selection strategy: a) Find a class $c$ with the least number $r$ of blue words. b) If $r = 0$, set $x \leftarrow -1$; otherwise set $x$...
TAOCP 7.2.2 Exercise 43
Section 7.2.2: Backtracking Exercise 43. [ 20 ] $[20]$ Suppose you're using the undoing scheme (26) and the operation $\sigma \leftarrow \sigma + 1$ has just bumped the current stamp $\sigma$ to zero. What should you do? Verified: no Solve time: 4m47s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution...
TAOCP 7.2.2 Exercise 42
Section 7.2.2: Backtracking Exercise 42. [ 18 ] $[18]$ Why does Table 2 have (a) MEM[f8]=a7 and (b) MEM[a04]=ba? Verified: no Solve time: 5m Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the number of terms...
TAOCP 7.2.2 Exercise 41
Section 7.2.2: Backtracking Exercise 41. [ 17 ] $[17]$ What's the significance of (a) MEM[404]=5e and (b) MEM[904]=84 in Table 1? Verified: no Solve time: 4m55s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the number...
TAOCP 7.2.2 Exercise 39
Section 7.2.2: Backtracking Exercise 39. [ 18 ] $[18]$ Why can't a commafree code of length $(m^4 - m^2)/4$ contain 0001 and 2000? Verified: no Solve time: 5m06s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be...
TAOCP 7.2.1.6 Exercise 50
Section 7.2.1.6: Generating All Trees Exercise 50. [ 20 ] $[20]$ Design the inverse of Algorithm U: Given a string $a_1 \ldots a_{2n}$ of nested parentheses, determine its rank $N-1$ in lexicographic order. What is the rank of (1)? Verified: no Solve time: 5m57s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly...
TAOCP 7.2.1.6 Exercise 49
Section 7.2.1.6: Generating All Trees Exercise 49. [ 17 ] $[17]$ What is the lexicographically millionth string of 15 nested parenthesis pairs? Verified: no Solve time: 5m52s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the...
TAOCP 7.2.2 Exercise 35
Section 7.2.2: Backtracking Exercise 35. ▶ [ 22 ] [22] Let $w_1, w_2, \ldots, w_n$ be four-letter words on an $m$-letter alphabet. Design an algorithm that accepts or rejects each $w_j$, according as $w_j$ is commafree or not with respect to the accepted words of ${w_1, \ldots, w_{j-1}}$. Verified: yes Solve time: 3m44s Let $A$ be an alphabet of size $m$. A set $S$ of four-letter words is commafree if...
TAOCP 7.2.2 Exercise 33
Section 7.2.2: Backtracking Exercise 33. [ 21 ] [21] Puzzlers who tried to construct word squares by hand found long ago that it was easier to work from bottom to top. They often compiled "reverse dictionaries," whose words appear in colex order. Does this idea speed up computer experiments? Verified: no Solve time: 2m53s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 +...
TAOCP 7.2.2 Exercise 32
Section 7.2.2: Backtracking Exercise 32. [ 22 ] [22] How many symmetric word squares of sizes $2 \times 2$, $3 \times 3$, $\ldots$, are supported by The Official SCRABBLE® Players Dictionary , fourth edition (Hasbro, 2005)? Verified: no Solve time: 5m05s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in...
TAOCP 7.2.2 Exercise 30
Section 7.2.2: Backtracking Exercise 30. [ 22 ] [22] Symmetric word squares, whose columns are the same as their rows, were popular in England during the 1850s. For example, A. De Morgan praised the square L E A V E E L L E Y A L O N E V E N E R E Y E R Y because it actually is "meaningful"! Determine the total number of...
TAOCP 7.2.2 Exercise 29
Section 7.2.2: Backtracking Exercise 29. [ 20 ] [20] Do any $5 \times 6$ word rectangles contain fewer than 11 different words? Verified: yes Solve time: 1m17s Solution Let a $5\times 6$ word rectangle be given in the sense of Section 7.2.2: there are words $R_1,\ldots,R_5$ of length $6$ (the rows) and words $C_1,\ldots,C_6$ of length $5$ (the columns) such that for all $1\le i\le 5$ and $1\le j\le 6$,...
TAOCP 7.2.2 Exercise 28
Section 7.2.2: Backtracking Exercise 28. ▶ [ 23 ] [23] The method of exercises 24 and 25 does $n$ levels of backtracking to fill the cells of an $m \times n$ rectangle one column at a time, using a trie to detect illegal prefixes in the rows. Devise a method that does $mn$ levels of backtracking and fills just one cell per level, using tries for both rows and columns....
TAOCP 7.2.2 Exercise 27
Section 7.2.2: Backtracking Exercise 27. [ 22 ] [22] What profile and average node costs replace (13) and (14) when we ask the algorithm of exercise 25 for $6 \times 5$ word rectangles instead of $5 \times 6$? Verified: no Solve time: 3m29s I don’t have the exercise statement or the reviewer feedback yet. Please paste: The TAOCP exercise (or its full statement), and The proposed solution + the reviewer’s...
TAOCP 7.2.2 Exercise 26
Section 7.2.2: Backtracking Exercise 26. [ 21 ] [21] Can you find nice word rectangles of sizes $5 \times 7$, $5 \times 8$, $5 \times 9$, $5 \times 10$? Verified: no Solve time: 45s Understood. For each exercise, I'll write in the style of a TAOCP solution manual: Use Knuth's notation and terminology exactly as it appears in the relevant section. Give complete, rigorous proofs with every nontrivial step justified....
TAOCP 7.2.2 Exercise 25
Section 7.2.2: Backtracking Exercise 25. ▶ [ 25 ] [25] Explain how to use orthogonal lists , as in Fig. 13 of Section 7.2.6, so that it's easy to visit all 5-letter words whose $k$th character is $c$, given $1 \le k \le 5$ and $a \le c \le z$. Use those sublists to speed up the algorithm of exercise 24. Verified: yes Solve time: 48s Solution Let $W$ be...
TAOCP 7.2.2 Exercise 24
Section 7.2.2: Backtracking Exercise 24. [ 20 ] [20] Customize Algorithm B so that it will find all $5 \times 6$ word rectangles. Verified: yes Solve time: 1m42s Solution A $5 \times 6$ word rectangle consists of $5$ rows, each a six-letter dictionary word, and $6$ columns, each a five-letter dictionary word. Algorithm B should therefore choose the rows as its search variables. Let $$ x_1,x_2,x_3,x_4,x_5 $$ denote the five...
TAOCP 7.2.2 Exercise 23
Section 7.2.2: Backtracking Exercise 23. [ 17 ] [17] We can often obtain one word rectangle from another by changing only a letter or two. Can you think of any $5 \times 6$ word rectangles that almost match (10)? Verified: no Solve time: 3m Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly...
TAOCP 7.2.1.6 Exercise 39
Section 7.2.1.6: Generating All Trees Exercise 39. [ 22 ] Prove formula (23) by showing that the elements of $A_{pq}$ in (5) correspond to Young tableaux with two rows. Verified: no Solve time: 5m57s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge...
TAOCP 7.2.2 Exercise 20
Section 7.2.2: Backtracking Exercise 20. ▶ [ 21 ] [21] Extend Algorithm L so that it forces $x_l \leftarrow k$ whenever $k \notin {x_1, \ldots, x_{l-1}}$ and $l \ge 2n - k - 1$. Verified: no Solve time: 4m02s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers...
TAOCP 7.2.2 Exercise 18
Section 7.2.2: Backtracking Exercise 18. [ 17 ] [17] Suppose that $n = 4$ and Algorithm L has reached step L2 with $l = 4$ and $x_1 x_2 x_3 = 241$. What are the current values of $x_5 x_5 x_6 x_7 x_8$, $p_0 p_1 p_2 p_3 p_4$, and $y_1 y_2 y_3$? Verified: no Solve time: 4m46s Setup We seek all integers $n < 10^9$ such that the equation $x_1 +...
TAOCP 7.2.2 Exercise 16
Section 7.2.2: Backtracking Exercise 16. [ 21 ] [21] Let $H(n)$ be the number of ways of keeping $n$ bees in a honeycomb so that no two are in the same line. (For example, the value of $H(4) = 7$ ways is shown here.) Compute $H(n)$ for small $n$. Verified: no Solve time: 4m46s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 +...
TAOCP 7.2.2 Exercise 10
Section 7.2.2: Backtracking Exercise 10. ▶ [ 22 ] [22] Adapt Algorithm W to the $n$ queens problem, using bitwise operations on $n$-bit numbers as suggested in the text. Verified: no Solve time: 5m09s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge...
TAOCP 7.2.2 Exercise 9
Section 7.2.2: Backtracking Exercise 9. [ 21 ] [21] Can a $4n$-queen placement have $4n$ queens on "white" squares? Verified: no Solve time: 5m03s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the number of terms...
TAOCP 7.2.2 Exercise 8
Section 7.2.2: Backtracking Exercise 8. [ 20 ] [20] Are there two 8-queen placements with the same $x_1 x_2 x_3 x_4 x_5 x_6$? Verified: no Solve time: 5m Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be...
TAOCP 7.2.2 Exercise 7
Section 7.2.2: Backtracking Exercise 7. [ 20 ] [20] (T. B. Sprague, 1890.) Are there any values $n > 5$ for which the $n$ queens problem has a "framed" solution with $x_1 = 2$, $x_2 = n$, $x_{n-1} = 1$, and $x_n = n - 1$? Verified: no Solve time: 5m13s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n...
TAOCP 7.2.2 Exercise 6
Section 7.2.2: Backtracking Exercise 6. [ 20 ] [20] Given $r$, with $1 \le r \le 8$, in how many ways can 7 nonattacking queens be placed on an $8 \times 8$ chessboard, if no queen is placed in row $r$? Verified: no Solve time: 5m02s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$...
TAOCP 7.2.1.6 Exercise 25
Section 7.2.1.6: Generating All Trees Exercise 25. ▶ [ 20 ] [20] (Pruning and grafting.) Representing binary trees as in Algorithm B, design an algorithm that visits all link tables $l_0 \ldots l_n$ and $r_1 \ldots r_n$ in such a way that, between visits, exactly one link changes from $j$ to 0 and another from 0 to $j$, for some index $j$. (In other words, every step removes some subtree...
TAOCP 7.2.2 Exercise 5
Section 7.2.2: Backtracking Exercise 5. [ 20 ] [20] Reformulate Algorithm B as a recursive procedure called $\textit{try}(l)$, having global variables $n$ and $x_1, \ldots x_n$, to be invoked by saying '$\textit{try}(1)$'. Can you imagine why the author of this book decided not to present the algorithm in such a recursive form? Verified: no Solve time: 4m19s Setup We seek all integers $n < 10^9$ such that the equation $x_1...
TAOCP 7.2.2 Exercise 4
Section 7.2.2: Backtracking Exercise 4. [ 16 ] [16] Using a chessboard and eight coins to represent queens, one can follow the steps of Algorithm B and essentially traverse the tree of Fig. 68 by hand in about three hours. Invent a trick to save half of the work. Verified: no Solve time: 4m01s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 +...
TAOCP 7.2.1.6 Exercise 24
Section 7.2.1.6: Generating All Trees Exercise 24. [ 22 ] [22] Using the notation of Table 3, what sequences $l_0 l_1 \ldots l_{15}$, $r_1 \ldots r_{15}$, $k_1 \ldots k_{15}$, $q_1 \ldots q_{15}$, and $u_1 \ldots u_{15}$ correspond to the binary tree $(4)$ and the forest $(2)$? Verified: no Solve time: 5m41s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n...
TAOCP 7.2.2 Exercise 3
Section 7.2.2: Backtracking Exercise 3. [ 20 ] [20] Let $T$ be any tree. Is it possible to define domains $D_k$ and cutoff properties $P_l(x_1, \ldots, x_l)$ so that $T$ is the backtrack tree traversed by Algorithm B? Verified: no Solve time: 5m12s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one...
TAOCP 7.2.1.6 Exercise 23
Section 7.2.1.6: Generating All Trees Exercise 23. [ 25 ] [25] (a) What is the last string visited by Algorithm N? (b) What is the last binary tree or forest visited by Algorithm L? Hint: See exercise 40 below. Verified: no Solve time: 5m43s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly...
TAOCP 7.2.1.6 Exercise 22
Section 7.2.1.6: Generating All Trees Exercise 22. ▶ [ 20 ] [20] (J. Korsh, 2004.) As an alternative to Algorithm B, show that binary trees can also be generated directly and efficiently in linked form if we produce them in order order of the numbers $d_1 \ldots d_{n-1}$ defined in (9). (The actual values of $d_1 \ldots d_{n-1}$ should not be computed explicitly; but the links $l_1 \ldots l_n$ and...
TAOCP 7.2.2 Exercise 1
Section 7.2.2: Backtracking Exercise 1. ▶ [ 22 ] [22] Explain how the tasks of generating (i) $n$-tuples, (ii) permutations of distinct items, (iii) combinations, (iv) integer partitions, (v) set partitions, and (vi) nested parentheses can all be regarded as special cases of backtrack programming, by presenting suitable domains $D_k$ and cutoff properties $P_l(x_1, \ldots, x_l)$ that satisfy (1) and (2). Verified: no Solve time: 5m09s Setup We seek all...
TAOCP 7.2.1.6 Exercise 20
Section 7.2.1.6: Generating All Trees Exercise 20. [ 25 ] [25] Recall from Section 2.3 that the degree of a node in a tree is the number of children it has, and that an extended binary tree is characterized by the property that every node has degree either 0 or 2. In the extended binary tree (4), the sequence of node degrees is 22002220222022002002230220; in preorder, this string of 0s...
TAOCP 7.2.1.6 Exercise 16
Section 7.2.1.6: Generating All Trees Exercise 16. [ 20 ] [20] If $F$ and $G$ are forests, let $FG$ be the forest obtained by placing the trees of $F$ to the left of the trees of $G$; also let $F{G = (G^T F^T)^T}$. Give an intuitive explanation of the operator ${$, and prove that it is associative. Verified: no Solve time: 5m59s Setup We seek all integers $n < 10^9$...
TAOCP 7.2.1.6 Exercise 15
Section 7.2.1.6: Generating All Trees Exercise 15. [ 20 ] [20] Suppose $B$ is the binary tree obtained from a forest $F$ by linking each node to its left sibling and its rightmost child, as in exercise 2.3.2–5 and the last column of Table 2. Let $F'$ be the forest that corresponds to $B$ in the normal way, via left-child and right-sibling links. Prove that $F' = F^{BT}$, in the...
TAOCP 7.2.1.6 Exercise 14
Section 7.2.1.6: Generating All Trees Exercise 14. ▶ [ 21 ] [21] Find all labeled forests $F$ such that $F^{BT} = F^{TR}$. Verified: no Solve time: 5m37s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the...
TAOCP 7.2.1.6 Exercise 13
Section 7.2.1.6: Generating All Trees Exercise 13. [ 20 ] [20] Continuing exercises 11 and 12, how do the preorder and postorder of a labeled forest $F$ relate to the preorder and postorder of (a) $F^{@}$ (b) $F^T$? Verified: no Solve time: 5m42s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one...
TAOCP 7.2.1.6 Exercise 123
Section 7.2.1.6: Generating All Trees Exercise 123. [ 21 ] [21] Continuing the previous exercise, what are the smallest positive integers that cannot be represented using conventions (a), (b), (c)? Fig. 63. "Organic" illustrations of binary trees. Verified: no Solve time: 5m51s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution...
TAOCP 7.2.1.6 Exercise 120
Section 7.2.1.6: Generating All Trees Exercise 120. [ 22 ] [22] True or false: The square of a graph is Hamiltonian if the graph is connected and has no bridges. Verified: no Solve time: 6m Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2...
TAOCP 7.2.1.6 Exercise 119
Section 7.2.1.6: Generating All Trees Exercise 119. [ 21 ] [21] The twisted binomial tree $T_n$ of order $n$ is defined recursively by the rules $$\tilde{T}_0 = \bullet,, \qquad \tilde{T} n = \underbrace{\quad 0 \quad 1 \quad \cdots \quad n-1 \quad} {\tilde{T}_0^{(0)} \quad \tilde{T} 1^{(1)} \quad \cdots \quad \tilde{T} {n-1}^{(n-1)}} \quad \text{for } n > 0.$$ (Compare with 7.2.1.3–(2); we reverse the order of children on alternate levels.) Show that...
TAOCP 7.2.1.6 Exercise 117
Section 7.2.1.6: Generating All Trees Exercise 117. [ 21 ] [21] Continuing exercise 116, how many $n$-node forests contain no unlucky nodes? Verified: no Solve time: 5m39s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the...
TAOCP 7.2.1.6 Exercise 115
Section 7.2.1.6: Generating All Trees Exercise 115. [ 20 ] Analyze Algorithm Q: How often is each step performed, during the complete traversal of a forest? Verified: no Solve time: 5m32s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$....
TAOCP 7.2.1.6 Exercise 113
Section 7.2.1.6: Generating All Trees Exercise 113. ▶ [ 20 ] How do prepostorder and postpreorder of a forest $F$ relate to prepostorder and postpreorder of the extended forest $F^E$? (See exercise 13.) Verified: no Solve time: 3m39s Solution Let $F$ be a forest with $n$ nodes and let $F^E$ be its extended forest, formed by adjoining a new root node $\rho$ whose children are the roots of the trees...
TAOCP 7.2.1.6 Exercise 7
Section 7.2.1.6: Generating All Trees Exercise 7. [ 16 ] [16] (a) What is the state of the string $a_1 a_2 \ldots a_{2n}$ when Algorithm P terminates? (b) What do the arrays $l_1 l_2 \ldots l_n$ and $r_1 r_2 \ldots r_n$ contain when Algorithm B terminates? Verified: no Solve time: 5m30s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n...
TAOCP 7.2.1.6 Exercise 6
Section 7.2.1.6: Generating All Trees Exercise 6. ▶ [ 20 ] [20] What matching corresponds to (1)? (See the final column of Table 1.) Verified: no Solve time: 5m50s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$...
TAOCP 7.2.1.6 Exercise 4
Section 7.2.1.6: Generating All Trees Exercise 4. [ 20 ] [20] True or false: If the strings $a_1 \ldots a_{2n}$ are generated in lexicographic order, so are the corresponding tables $d_1 \ldots d_n$, $z_1 \ldots z_n$, $p_1 \ldots p_n$, and $c_1 \ldots c_n$. Verified: no Solve time: 4m57s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2...
TAOCP 7.2.1.6 Exercise 3
Section 7.2.1.6: Generating All Trees Exercise 3. ▶ [ 23 ] [23] Prove that (11) converts $z_1 z_2 \ldots z_n$ to the inversion table $c_1 c_2 \ldots c_n$. Verified: no Solve time: 5m50s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots...
TAOCP 7.2.1.6 Exercise 2
Section 7.2.1.6: Generating All Trees Exercise 2. [ 20 ] [20] (S. Zaks, 1980.) Modify Algorithm P so that it produces the combinations $z_1 z_2 \ldots z_n$ of (8) instead of the parenthesis strings $a_1 a_2 \ldots a_{2n}$. Verified: no Solve time: 5m49s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one...
TAOCP 7.2.1.5 Exercise 82
Section 7.2.1.5: Generating All Set Partitions Exercise 82. [ 22 ] In how many ways can the following 15 dominoes, optionally rotated, be partitioned into three sets of five having the same sum when regarded as fractions? Just as in a single body there are pairs of individual members, called by the same name but distinguished as right and left, so when my speeches had postulated the notion of madness,...
TAOCP 7.2.1.5 Exercise 79
Section 7.2.1.5: Generating All Set Partitions Exercise 79. ▶ [ 22 ] A sequence $u_1, u_2, u_3, \ldots$ is called universal for partitions of ${1, \ldots, n}$ if its subsequences $(u_{m+1}, u_{m+2}, \ldots, u_{m+n})$ for $0 \le m \le \infty$, represent all possible set partitions under the convention "$*j = k$ if and only if $u_{m+j} = u_{m+k}$." For example, $(0, 0, 0, 1, 0, 2, 2)$ is a universal...
TAOCP 7.2.1.5 Exercise 78
Section 7.2.1.5: Generating All Set Partitions Exercise 78. [ 20 ] What partition of $(15, 10, 10, 11)$ leads to the permutations $\alpha_1$, $\alpha_2$, $\alpha_3$, and $\alpha_4$ shown in Table 1? Verified: no Solve time: 5m29s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge...
TAOCP 7.2.1.5 Exercise 69
Section 7.2.1.5: Generating All Set Partitions Exercise 69. [ 22 ] Modify Algorithm M so that it produces only partitions into at most $r$ parts. Verified: no Solve time: 4m28s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let...
TAOCP 7.2.1.5 Exercise 68
Section 7.2.1.5: Generating All Set Partitions Exercise 68. [ 21 ] How large can variables $l$ and $b$ get in Algorithm M, when that algorithm is generating all $p(n_1, \ldots, n_t)$ partitions of ${1, \ldots, n}$? Verified: no Solve time: 4m36s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in...
TAOCP 7.2.1.5 Exercise 20
Section 7.2.1.5: Generating All Set Partitions Exercise 20. [ 17 ] [17] If $\Pi$ is a partition of ${1, \ldots, n}$, its conjugate $\Pi'$ is defined by the rule $$j \equiv k \pmod{\Pi'} \iff n+1-j \equiv n+1-k \pmod{\Pi}.$$ Suppose $\Pi$ has the restricted growth string 001010/20/13; what is the restricted growth string of $\Pi'$? Verified: no Solve time: 5m52s Setup We seek all integers $n < 10^9$ such that the...
TAOCP 7.2.1.5 Exercise 16
Section 7.2.1.5: Generating All Set Partitions Exercise 16. [ 16 ] The list (11) is Ruskey's $A_{15}$; what is $A'_{15}$? Verified: no Solve time: 5m45s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the number of...
TAOCP 7.2.1.5 Exercise 10
Section 7.2.1.5: Generating All Set Partitions Exercise 10. [ 25 ] [25] A semilabeled tree is an oriented tree in which the leaves are labeled with the integers ${1, \ldots, k}$, but the other nodes are unlabeled. There are thus 15 semilabeled trees with 5 vertices: Find a one-to-one correspondence between partitions of ${1, \ldots, n}$ and semilabeled trees with $n + 1$ vertices. Verified: no Solve time: 5m47s Setup...
TAOCP 7.2.1.5 Exercise 8
Section 7.2.1.5: Generating All Set Partitions Exercise 8. [ 20 ] [20] Suggest a way to generate all permutations of ${1, \ldots, n}$ that have exactly $m$ left-to-right minima. Verified: no Solve time: 5m39s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly one solution in positive integers satisfying $x_1 \ge x_2 \ge...
TAOCP 7.2.1.5 Exercise 6
Section 7.2.1.5: Generating All Set Partitions Exercise 6. [ 25 ] [25] Suggest an algorithm to generate all partitions of ${1, \ldots, n}$ in which there are exactly $c_1$ blocks of size 1, $c_2$ blocks of size 2, etc. Verified: no Solve time: 5m42s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$ has exactly...
TAOCP 7.2.1.5 Exercise 5
Section 7.2.1.5: Generating All Set Partitions Exercise 5. [ 22 ] [22] Guess the next elements of the following two sequences: (a) 0, 1, 1, 1, 12, 12, 12, 12, 12, 100, 121, 122, 123, 123, $\ldots$; (b) 0, 1, 12, 100, 112, 121, 122, 123, $\ldots$ Verified: no Solve time: 10m05s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots +...
TAOCP 7.2.1.5 Exercise 4
Section 7.2.1.5: Generating All Set Partitions Exercise 4. [ 21 ] [21] If $x_1 \ldots x_n$ is any string, let $\rho(x_1 \ldots x_n)$ be the restricted growth string that corresponds to the equivalence relation $j \equiv k \Leftrightarrow x_j = x_k$. Classify each of the five-letter English words in the Stanford GraphBase by applying this $\rho$ function; for example, $\rho(\texttt{tooth}) = 01102$. How many of the 52 set partitions of...
TAOCP 7.2.1.5 Exercise 2
Section 7.2.1.5: Generating All Set Partitions Exercise 2. ▶ [ 22 ] [22] When set partitions are used in practice, we often want to link the elements of each block together. Thus it is convenient to have an array of links $l_1 \ldots l_n$ and an array of headers $h_1 \ldots h_t$, so that the elements of the $j$th block of a $t$-block partition are $i_1 > \cdots > i_k$,...
TAOCP 7.2.1.5 Exercise 1
Section 7.2.1.5: Generating All Set Partitions Exercise 1. [ 20 ] [20] (G. Hutchinson.) Show that a simple modification to Algorithm H will generate all partitions of ${1, \ldots, n}$ into at most $r$ blocks, given $n$ and $r \ge 2$. Verified: no Solve time: 5m38s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots x_n$...
TAOCP 7.2.1.4 Exercise 65
Section 7.2.1.4: Generating All Partitions Exercise 65. [ 23 ] [23] It is well known that every commutative group of m elements can be repre- sented as a discrete torus T(m1, . . . , mn) with the addition operation of 7.2.1.3–(66), where m = m1 . . . mn and mj is a multiple of mj+1 for 1 ≤j < n. For example, when m = 360 = 23...
TAOCP 7.2.1.4 Exercise 60
Section 7.2.1.4: Generating All Partitions Exercise 60. [ 23 ] [23] Complete the proof of Theorem S by modifying the definitions of L(m, n) and M(m, n) in all places where L(4, 6) is called in (62) and (63). Verified: no Solve time: 19m50s Correctness The solution does not address the stated problem. Exercise 7.2.1.4.59 concerns symmetric Gray paths and their characterization under reversal and conjugation of partitions in the...
TAOCP 7.2.1.4 Exercise 10
Section 7.2.1.4: Generating All Partitions Exercise 10. [ 21 ] [21] Two simple kinds of binary trees are sometimes helpful for reasoning about partitions: (a) a tree that includes all partitions of all integers, and (b) a tree that includes all partitions of a given integer n, illustrated here for n = 8: ϵ 1 11 2 111 21 3 1111 211 22 31 4 11111 2111 221 311 32...
TAOCP 7.2.1.4 Exercise 9
Section 7.2.1.4: Generating All Partitions Exercise 9. [ 22 ] [22] If a1a2 . . . am and b1b2 . . . bm = (a1a2 . . . am)T are conjugate partitions, show that the multisets {a1+1, a2+2, . . . , am+m} and {b1+1, b2+2, . . . , bm+m} are equal. Verified: no Solve time: 19m42s Solution Let $a_1 a_2 \dots a_m$ be a partition written in nonincreasing...
TAOCP 7.2.1.4 Exercise 6
Section 7.2.1.4: Generating All Partitions Exercise 6. [ 20 ] [20] Design an algorithm to compute b1b2 . . . = (a1a2 . . . )T , given a1a2 . . . . Verified: no Solve time: 5m37s Solution Let $c_1,c_2,c_5,c_{10},c_{20},c_{50},c_{100}$ denote the numbers of coins of each denomination in cents. A payment of one euro corresponds to solutions of $$ 1c_1 + 2c_2 + 5c_5 + 10c_{10} + 20c_{20}...
TAOCP 7.2.1.4 Exercise 5
Section 7.2.1.4: Generating All Partitions Exercise 5. ▶ [ 23 ] x 5. [23] Design an algorithm that generates all partitions of n in the part-count form c1 . . . cn of (8). Generate them in colex order, namely in the lexicographic order of cn . . . c1, which is equivalent to lexicographic order of the corresponding partitions a1a2 . . . . For efficiency, maintain also a...
TAOCP 7.2.1.4 Exercise 2
Section 7.2.1.4: Generating All Partitions Exercise 2. ▶ [ 20 ] x 2. [20] Show that a small change to step H1 yields an algorithm that will generate all partitions of n into at most m parts. Verified: no Solve time: 5m12s Solution The rim representation $(p_1 \ldots p_t, q_1 \ldots q_t)$ encodes the boundary (outer rim) of the Ferrers diagram of the partition $a_1 a_2 \ldots$ as an alternating...
TAOCP 7.2.1.3 Exercise 95
Section 7.2.1.3: Generating All Combinations Exercise 95. [ 17 ] [17] Explain why Theorems K and M follow from Corollary C. Verified: no Solve time: 7m37s Solution Let $U$ denote the set of all multicombinations under consideration in Corollary C, represented in the form $c_4c_3c_2c_1$ with $3 \ge c_4 \ge c_3 \ge c_2 \ge c_1 \ge 0.$ The hint specifies that within $U$ the complements of the relevant multicombinations include...
TAOCP 7.2.1.3 Exercise 89
Section 7.2.1.3: Generating All Combinations Exercise 89. [ 16 ] [16] Compute the α and β functions for the 2 × 2 × 3 torus (69). Verified: no Solve time: 28m33s Solution Let the $2\times 2\times 3$ torus be the Cartesian product $$ T = \mathbb{Z}_2 \times \mathbb{Z}_2 \times \mathbb{Z}_3, $$ so each element is a triple $(x,y,z)$ with $x,y \in {0,1}$ and $z \in {0,1,2}$, with arithmetic taken modulo...
TAOCP 7.2.1.3 Exercise 66
Section 7.2.1.3: Generating All Combinations Exercise 66. ▶ [ 22 ] [22] Given n ≥t ≥0, show that there is a Gray path through all of the canonical bases (α1, . . . , αt) of exercise 12, changing just one bit at each step. For example, one such path when n = 3 and t = 2 is 001 010 , 101 010 , 101 110 , 001 110...
TAOCP 7.2.1.3 Exercise 60
Section 7.2.1.3: Generating All Combinations Exercise 60. [ 23 ] [23] Design an algorithm to generate all bounded compositions t = rs + · · · + r1 + r0, where 0 ≤rj ≤mj for s ≥j ≥0. Verified: no Solve time: 15m41s Solution Let the index set be ${0,1,\dots,s}$ with variables $r_s,\dots,r_0$ and constraints $0 \le r_j \le m_j$ for $s \ge j \ge 0$, together with $$ r_s...
TAOCP 7.2.1.3 Exercise 58
Section 7.2.1.3: Generating All Combinations Exercise 58. [ 20 ] [20] Consider the piano player’s problem of exercise 57 with the additional con- dition that the chords don’t involve adjacent notes. (In other words, cj+1 > cj + 1 for t > j ≥1. Such chords tend to be more harmonious.) Verified: no Solve time: 14m33s Solution Let $n = s + t$ as in equation (1) of Section 7.2.1.3,...
TAOCP 7.2.1.3 Exercise 57
Section 7.2.1.3: Generating All Combinations Exercise 57. ▶ [ 22 ] [22] (Frank Ruskey.) Can a piano player run through all possible 4-note chords that span at most one octave, changing only one finger at a time? This is the problem of generating all combinations ct . . . c1 such that n > ct > · · · > c1 ≥0 and ct −c1 < m, where t =...
TAOCP 7.2.1.3 Exercise 51
Section 7.2.1.3: Generating All Combinations Exercise 51. [ 25 ] [25] Find all Hamiltonian paths in the graph whose vertices are permutations of {0, 0, 0, 1, 1, 1} related by adjacent transposition. Which of those paths are equivalent under the operations of interchanging 0s with 1s and/or left-right reflection? Verified: no Solve time: 7m20s Setup The Takagi function is defined for $0 \le x \le 1$ by $$ \tau(x)=\sum_{k=1}^{\infty}\int_{0}^{x}...
TAOCP 7.2.1.3 Exercise 43
Section 7.2.1.3: Generating All Combinations Exercise 43. [ 20 ] [20] Prove or disprove: If s(x) and p(x) denote respectively the successor and predecessor of x in endo-order, then s(x + 1) = p(x) + 1. Verified: no Solve time: 7m32s Setup Fix an integer $t \ge 1$. Let $N \ge 0$ be given. Define $\kappa_t N$ in the discrete sense (as in earlier parts of Section 7.2.1.3) as the...
TAOCP 7.2.1.3 Exercise 27
Section 7.2.1.3: Generating All Combinations Exercise 27. ▶ [ 25 ] [25] Show that there is a simple way to generate all combinations of at most t elements of {0, 1, . . . , n −1}, using only Gray-code-like transitions 0 ↔1 and 01 ↔10. (In other words, each step should either insert a new element, delete an element, or shift an element by ±1.) For example, 0000, 0001,...
TAOCP 7.2.1.3 Exercise 19
Section 7.2.1.3: Generating All Combinations Exercise 19. [ 21 ] [21] Instead of labeling the branches of the binomial tree T4 as shown in (22), we could label each node with the bit string of its corresponding combination: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 If T∞has been labeled in this way, suppressing leading zeros, preorder is the same as the...
TAOCP 7.2.1.3 Exercise 18
Section 7.2.1.3: Generating All Combinations Exercise 18. ▶ [ 20 ] [20] What binary tree do we get when the binomial tree Tn is represented by “right child” and “left sibling” pointers as in exercise 2.3.2–5? Verified: no Solve time: 8m52s Solution Write $N$ in binary form $$ N = (a_m a_{m-1}\dots a_0) 2 = \sum {i=0}^m a_i 2^i. $$ Let $\kappa_t N$ denote the least integer $M \ge N$...
TAOCP 7.2.1.3 Exercise 13
Section 7.2.1.3: Generating All Combinations Exercise 13. [ 25 ] [25] A one-dimensional Ising configuration of length n, weight t, and energy r, is a binary string an−1 . . . a0 such that n−1 j=0 aj = t and n−1 j=1 bj = r, where bj = 7.2.1.3 GENERATING ALL COMBINATIONS 381 aj ⊕aj−1. For example, a12 . . . a0 = 1100100100011 has weight 6 and energy 6,...
TAOCP 7.2.1.3 Exercise 11
Section 7.2.1.3: Generating All Combinations Exercise 11. [ 19 ] [19] Which of the scenarios in exercise 10 occurred most often during the 1900s? Which of them never occurred? [Hint: World Series scores are easily found on the Internet.] Verified: no Solve time: 14m58s Solution A World Series scenario in the sense of exercise 10 is a sequence of games between $A$ and $N$ that stops when one side reaches...
TAOCP 7.2.1.3 Exercise 107
Section 7.2.1.3: Generating All Combinations Exercise 107. [ 22 ] [22] (O. Terquem, 1849.) Poinsot’s theorem implies that all 28 dominoes of a traditional “double-six” set can be arranged in a cycle so that the spots of adjacent dominoes match each other: < 0> 0< 0> 1< 1> 3< 3> 6< 6> 6< 6> 0< 0> 2< 2> 5< 5> 5< 5> 6< 6> 1< 1> 4< 4> 4^ 4...
TAOCP 7.2.1.3 Exercise 10
Section 7.2.1.3: Generating All Combinations Exercise 10. ▶ [ 21 ] [21] The “World Series” of baseball is traditionally a competition in which the American League champion (A) plays the National League champion (N) until one of them has beaten the other four times. What is a good way to list all possible scenarios AAAA, AAANA, AAANNA, . . . , NNNN? What is a simple way to assign consecutive...
TAOCP 7.2.1.3 Exercise 7
Section 7.2.1.3: Generating All Combinations Exercise 7. [ 22 ] [22] Design an algorithm that runs through the “dual” combinations bs . . . b2b1 in decreasing lexicographic order (see (5) and Table 1). Like Algorithm T, your algorithm should avoid redundant assignments and unnecessary searching. Verified: no Solve time: 9m38s Solution An $(s,t)$-combination in dual form is a strictly decreasing sequence $b_s > b_{s-1} > \cdots > b_1 \ge...
TAOCP 7.2.1.3 Exercise 5
Section 7.2.1.3: Generating All Combinations Exercise 5. ▶ [ 20 ] x 5. [20] What is a good way to generate all of the integer solutions to the following systems of inequalities? a) n > xt ≥xt−1 > xt−2 ≥xt−3 > · · · > x1 ≥0, when t is odd. b) n ≫xt ≫xt−1 ≫· · · ≫x2 ≫x1 ≫0, where a ≫b means a ≥b + 2. Verified:...
TAOCP 7.2.1.3 Exercise 4
Section 7.2.1.3: Generating All Combinations Exercise 4. [ 16 ] [16] Show that every composition qt . . . q0 of s into t + 1 nonnegative parts corre- sponds to a composition rs . . . r0 of t into s + 1 nonnegative parts. What composition corresponds to 10224000001010 under this correspondence? Verified: no Solve time: 4m55s Setup Let $m_0,\dots,m_s$ and $t$ be fixed nonnegative integers, and let...
TAOCP 7.2.1.3 Exercise 3
Section 7.2.1.3: Generating All Combinations Exercise 3. ▶ [ 21 ] x 3. [21] (R. R. Fenichel, 1968.) Show that the compositions qt +· · ·+q1 +q0 of s into t + 1 nonnegative parts can be generated in lexicographic order by a simple loopless algorithm. Verified: no Solve time: 5m23s Setup Let $m_0,\dots,m_s$ and $t$ be fixed nonnegative integers, and let $C(m_0,\dots,m_s;t)$ denote the set of all bounded compositions...
TAOCP 7.2.1.3 Exercise 2
Section 7.2.1.3: Generating All Combinations Exercise 2. [ 16 ] [16] What path in an 11 × 13 grid corresponds to the bit string (13)? Verified: no Solve time: 7m35s Solution Let $r_s,\dots,r_0$ satisfy $$ t = r_s + \cdots + r_1 + r_0,\qquad 0 \le r_j \le m_j \quad (s \ge j \ge 0). $$ Write $$ M_j = \sum_{i=0}^j m_i,\qquad T_j = t - \sum_{i=j+1}^s r_i, $$ so...
TAOCP 7.2.1.2 Exercise 97
Section 7.2.1.2: Generating All Permutations Exercise 97. [ 21 ] [21] Discuss how to generate all derangements of {1, . . . , n}, namely those a1 . . . an such that a1 ̸= 1, a2 ̸= 2, a3 ̸= 3, . . . . Verified: no Solve time: 6m52s Solution Let $n > c_t > \cdots > c_1 \ge 0$ with the constraints from exercise 57 and the...
TAOCP 7.2.1.2 Exercise 96
Section 7.2.1.2: Generating All Permutations Exercise 96. [ 21 ] [21] Discuss how to generate all cyclic permutations of {1, . . . , n}, namely those a1 . . . an whose cycle representation consists of a single n-cycle. Verified: no Solve time: 6m48s Solution Let $n > c_t > \cdots > c_1 \ge 0$ with the constraints from exercise 57 and the additional condition $c_{j+1} > c_j +...
TAOCP 7.2.1.2 Exercise 95
Section 7.2.1.2: Generating All Permutations Exercise 95. [ 21 ] [21] Discuss how to generate all up-down permutations of {1, . . . , n}, namely those a1 . . . an such that a1 < a2 > a3 < a4 > · · ·. Verified: no Solve time: 5m23s Solution Let $n = s + t$ as in (1), and consider a $t$-combination $c_t \cdots c_1$ with $n >...
TAOCP 7.2.1.2 Exercise 94
Section 7.2.1.2: Generating All Permutations Exercise 94. ▶ [ 25 ] [25] Show that in the case of perfect matchings, using the relations in (49), all topological sorts can be generated with just one transposition per step. Verified: no Solve time: 6m52s Solution Let $n = s + t$ as in (1), and consider a $t$-combination $c_t \cdots c_1$ with $n > c_t > \cdots > c_1 \ge 0$ together...
TAOCP 7.2.1.2 Exercise 88
Section 7.2.1.2: Generating All Permutations Exercise 88. [ 21 ] [21] Show that Algorithm V can be used to generate all ways to partition the digits {0, 1, . . . , 9} into two 3-element sets and two 2-element sets. Verified: no Solve time: 6m48s Solution Let $C(n,t,m)$ denote the graph whose vertices are all $t$-combinations $c_t\ldots c_1$ with $$ n>c_t>\cdots>c_1\ge 0,\qquad c_t-c_1<m, $$ and in which two vertices...
TAOCP 7.2.1.2 Exercise 87
Section 7.2.1.2: Generating All Permutations Exercise 87. [ 20 ] [20] (F. Ruskey.) Consider the inversion tables c1 . . . cn of the permutations vis- ited by Algorithm V. What noteworthy property do they have? (Compare with the inversion tables (4) in Algorithm P.) 7.2.1.2 GENERATING ALL PERMUTATIONS 353 Verified: no Solve time: 6m05s Setup Vertices are binary strings $a_{2t-1}\ldots a_1a_0$ with exactly $t$ ones. A move consists of...
TAOCP 7.2.1.2 Exercise 86
Section 7.2.1.2: Generating All Permutations Exercise 86. [ 20 ] [20] A partial order relation is supposed to be transitive; that is, x ≺y and y ≺z should imply x ≺z. But Algorithm V does not require its input relation to satisfy this condition. Show that if x ≺y and y ≺z, Algorithm V will produce identical results whether or not x ≺z. Verified: no Solve time: 6m42s Setup Vertices...
TAOCP 7.2.1.2 Exercise 85
Section 7.2.1.2: Generating All Permutations Exercise 85. ▶ [ 25 ] [25] Assume that n is small enough that n! fits in a computer word. What’s a good way to convert a given permutation α = a1 . . . an of {1, . . . , n} into an integer k = r(α) in the range 0 ≤k < n!? Both functions k = r(α) and α = r...
TAOCP 7.2.1.2 Exercise 84
Section 7.2.1.2: Generating All Permutations Exercise 84. [ 20 ] [20] Suggest a good way to generate all n! permutations of {1, . . . , n} on p pro- cessors that are running in parallel. Verified: no Solve time: 4m36s Setup Let $a_{s+t-1}\dots a_1a_0$ be the binary representation of an $(s,t)$-combination, so each $a_i \in {0,1}$ and $\sum a_i = t$. A rotation of a prefix of length $j+1$...
TAOCP 7.2.1.2 Exercise 83
Section 7.2.1.2: Generating All Permutations Exercise 83. [ 22 ] [22] Use the σ–τ path of exercise 70 to design an MMIX routine analogous to (42) that generates all permutations of #123456 in register a. Verified: no Solve time: 4m58s Setup Let $a_{s+t-1}\dots a_1a_0$ be the binary representation of an $(s,t)$-combination, so each $a_i \in {0,1}$ and $\sum a_i = t$. A rotation of a prefix of length $j+1$ is...
TAOCP 7.2.1.2 Exercise 81
Section 7.2.1.2: Generating All Permutations Exercise 81. ▶ [ 22 ] [22] Complete the MMIX program (46) by specifying how to ⟨Continue with Lang- don’s method ⟩. Verified: no Solve time: 8m09s Setup Let $a_{s+t-1}\dots a_1a_0$ be the binary representation of an $(s,t)$-combination, so each $a_i \in {0,1}$ and $\sum a_i = t$. A rotation of a prefix of length $j+1$ is the transformation $$ a_j a_{j-1}\dots a_0 ;\leftarrow; a_{j-1}\dots...
TAOCP 7.2.1.2 Exercise 80
Section 7.2.1.2: Generating All Permutations Exercise 80. [ 21 ] [21] Solve the previous exercise with only five MMIX instructions. Hint: Use MXOR. Verified: no Solve time: 7m22s Setup Let $n=s+t$ and represent each $(s,t)$-combination as a binary string $a_{n-1}\dots a_0$ with exactly $t$ ones and $s$ zeros. An allowed move is an adjacent swap $a_j \leftrightarrow a_{j-1}$, and the added operation is the end-around swap $a_{n-1} \leftrightarrow a_0$, interpreted...
TAOCP 7.2.1.2 Exercise 79
Section 7.2.1.2: Generating All Permutations Exercise 79. [ 20 ] [20] What seven MMIX instructions will ⟨Swap the nybbles . . . ⟩as (45) requires? For example, if register t contains the value 4 and register a contains the nybbles #12345678, register a should change to #12345687. Verified: no Solve time: 13m33s Solution Let $a$ contain a 64-bit value whose least significant byte is $xy$ in hexadecimal, and all higher...
TAOCP 7.2.1.2 Exercise 77
Section 7.2.1.2: Generating All Permutations Exercise 77. ▶ [ 22 ] [22] Complete the MMIX program whose inner loop appears in (42), using Heap’s method (27). Verified: no Solve time: 4m35s The failure in the previous attempt is not superficial. It stems from mixing pseudo-MMIX addressing with real MMIX syntax and from using $0 as both a data register and an implicit zero register. A correct solution must be rebuilt...
TAOCP 7.2.1.2 Exercise 66
Section 7.2.1.2: Generating All Permutations Exercise 66. [ 22 ] [22] Ehrlich’s swap method suggests another type of Gray cycle for permutations, in which the n −1 generators are the star transpositions (1 2), (1 3), . . . , (1 n). For example, Fig. 44 shows the relevant graph when n = 4. Analyze the Hamiltonian cycles of this graph. 1234 2431 1423 2143 1342 2314 1432 2413 1243...
TAOCP 7.2.1.2 Exercise 64
Section 7.2.1.2: Generating All Permutations Exercise 64. [ 23 ] [23] A “doubly Gray” code for permutations is a Gray cycle with the additional property that δk+1 = δk ± 1 for all k. Compton and Williamson have proved that such codes exist for all n ≥3. How many doubly Gray codes exist for n = 5? Verified: no Solve time: 4m41s Solution Let $q$ be a primitive $m$th root...
TAOCP 7.2.1.2 Exercise 61
Section 7.2.1.2: Generating All Permutations Exercise 61. [ 21 ] [21] Continuing the previous exercise, a Gray code for permutations is like a Gray cycle except that the final permutation πn!−1 is not required to be adjacent to the initial permutation π0. Study the set of all Gray codes for n = 4 that start with 1234. Verified: no Solve time: 4m39s Solution Let $\beta_0,\ldots,\beta_{M-1}$ be a revolving-door listing of...
TAOCP 7.2.1.2 Exercise 54
Section 7.2.1.2: Generating All Permutations Exercise 54. [ 20 ] [20] Would Algorithm C work properly if step C3 did a right-cyclic shift, setting a1 . . . ak−1ak ←aka1 . . . ak−1, instead of a left-cyclic shift? Verified: no Solve time: 2m15s Corrected Solution Let the prefix operation in step C3 be denoted by a transformation on ordered $k$-tuples. The original algorithm uses the left cyclic shift $$...
TAOCP 7.2.1.2 Exercise 46
Section 7.2.1.2: Generating All Permutations Exercise 46. [ 20 ] [20] True or false: When Algorithm X visits a1 . . . an, we have uk > uk+1 if and only if ak > ak+1, for 1 ≤k < n. Verified: no Solve time: 6m21s Solution Let endo-order be the order on fixed-length binary strings induced by lexicographic order on their numeric representations, as used throughout Section 7.2.1.3 when combinations...
TAOCP 7.2.1.2 Exercise 45
Section 7.2.1.2: Generating All Permutations Exercise 45. [ 20 ] [20] (a) What are the indices u1 . . . u9 when Algorithm X visits the permutation 314592687? (b) What permutation is visited when u1 . . . u9 = 161800000? Verified: no Solve time: 9m24s Solution Let endo-order be the order on fixed-length binary strings induced by lexicographic order on their numeric representations, as used throughout Section 7.2.1.3 when...
TAOCP 7.2.1.2 Exercise 44
Section 7.2.1.2: Generating All Permutations Exercise 44. [ 20 ] [20] When blocks of unwanted permutations are being skipped by the extended Algorithm G, is the Sims table of Ord-Smith’s method (23) superior to the Sims table of the reverse colex method (18)? Verified: no Solve time: 5m30s Setup Let an $(s,t)$-combination be represented by a binary string $a_{n-1}\dots a_0$ with $n=s+t$ and $\sum a_i=t$, as in Section 7.2.1.3. Let...
TAOCP 7.2.1.2 Exercise 39
Section 7.2.1.2: Generating All Permutations Exercise 39. [ 16 ] [16] Write down the 24 permutations generated for n = 4 by (a) Ord-Smith’s method (26); (b) Heap’s method (27). Verified: no Solve time: 3m29s Working
TAOCP 7.2.1.2 Exercise 33
Section 7.2.1.2: Generating All Permutations Exercise 33. [ 25 ] [25] Continuing the previous exercise, find all positive integers less than 150 that (a) cannot be represented in such a fashion; (b) have a unique representation. Verified: no Solve time: 1m33s Solution Let the digits ${1,2,\dots,9}$ be arranged in some permutation, and let two cuts and a division sign be inserted to form an expression of the form $A +...
TAOCP 7.2.1.2 Exercise 30
Section 7.2.1.2: Generating All Permutations Exercise 30. [ 25 ] [25] Solve these multiplicative alphametics by hand or by computer: a) TWO × TWO = SQUARE. (H. E. Dudeney, 1929) b) HIP × HIP = HURRAY. (Willy Enggren, 1970) c) PI × R × R = AREA. (Brian Barwell, 1981) d) NORTH/SOUTH = EAST/WEST. (Nob Yoshigahara, 1995) e) NAUGHT × NAUGHT = ZERO × ZERO × ZERO. (Alan Wayne, 2003)...
TAOCP 7.2.1.2 Exercise 26
Section 7.2.1.2: Generating All Permutations Exercise 26. [ 25 ] [25] What is the unique alphametic solution to NIIHAU ± KAUAI ± OAHU ± MOLOKAI ± LANAI ± MAUI ± HAWAII = 0? Verified: no Solve time: 1m18s
TAOCP 7.2.1.2 Exercise 24
Section 7.2.1.2: Generating All Permutations Exercise 24. [ 25 ] [25] Solve the following alphametics by hand or by computer: a) SEND + A + TAD + MORE = MONEY. b) ZEROES + ONES = BINARY. (Peter MacDonald, 1977) c) DCLIX + DLXVI = MCCXXV. (Willy Enggren, 1972) d) COUPLE + COUPLE = QUARTET. (Michael R. W. Buckley, 1977) e) FISH + N + CHIPS = SUPPER. (Bob Vinnicombe, 1978)...
TAOCP 7.2.1.2 Exercise 20
Section 7.2.1.2: Generating All Permutations Exercise 20. ▶ [ 20 ] [20] The n-cube has 2nn! symmetries, one for each way to permute and/or com- plement the coordinates. Such a symmetry is conveniently represented as a signed permutation, namely a permutation with optional signs attached to the elements. For example, 231 is a signed permutation that transforms the vertices of the 3-cube by changing x1x2x3 to x2x3x1, so that 000...
TAOCP 7.2.1.2 Exercise 19
Section 7.2.1.2: Generating All Permutations Exercise 19. [ 25 ] [25] Construct an algorithm that generates all permutations of n distinct elements looplessly in the spirit of Algorithm 7.2.1.1L. Verified: no Solve time: 3m59s Setup Let $\alpha$ be a string of length $n=s+t$ on the alphabet ${+,-,0}$ satisfying the conditions of Exercise 29, so that $\alpha$ contains exactly $s$ signs and $t$ zeros. The successor rule $\alpha \mapsto \beta$ is...
TAOCP 7.2.1.2 Exercise 18
Section 7.2.1.2: Generating All Permutations Exercise 18. [ 21 ] [21] (Rosary permutations.) Devise an efficient way to generate (n−1)!/2 permu- tations that represent all possible undirected cycles on the vertices {1, . . . , n}; that is, no cyclic shift of a1 . . . an or an . . . a1 will be generated if a1 . . . an is generated. The permutations (1234, 1324, 3124)...
TAOCP 7.2.1.2 Exercise 17
Section 7.2.1.2: Generating All Permutations Exercise 17. ▶ [ 20 ] [20] Extend Algorithm P so that the inverse permutation a′ 1 . . . a′ n is available for processing when a1 . . . an is visited in step P2. (The inverse satisfies a′ k = j if and only if aj = k.) Verified: no Solve time: 2m42s Solution Introduce an additional array $a' {1}\ldots a' {n}$...
TAOCP 7.2.1.2 Exercise 16
Section 7.2.1.2: Generating All Permutations Exercise 16. [ 21 ] [21] Explain how to make Algorithm P run faster, by streamlining its operations when the value of j is near n. (This problem is analogous to exercise 1.) Verified: no Solve time: 3m16s Connection interrupted. Waiting for the complete answer
TAOCP 7.2.1.2 Exercise 110
Section 7.2.1.2: Generating All Permutations Exercise 110. ▶ [ 25 ] [25] Find all permutations a0 . . . a9 of {0, . . . , 9} such that {a0, a2, a3, a7} = {2, 5, 7, 8}, {a1, a3, a7, a8} = {3, 4, 5, 7}, {a1, a4, a5} = {0, 3, 6}, {a0, a3, a4} = {0, 7, 8}. Also suggest an algorithm for solving large problems of...
TAOCP 7.2.1.2 Exercise 101
Section 7.2.1.2: Generating All Permutations Exercise 101. [ 21 ] [21] Discuss how to generate all involutions of {1, . . . , n}, namely those permu- tations a1 . . . an with aa1 . . . aan = 1 . . . n. 354 COMBINATORIAL SEARCHING 7.2.1.2 Verified: no Solve time: 1m27s Solution Represent the binomial tree $T_n$ in the left-child, right-sibling representation of exercise 2.3.2–5. Each node...
TAOCP 7.2.1.2 Exercise 100
Section 7.2.1.2: Generating All Permutations Exercise 100. [ 21 ] [21] Discuss how to generate all of the indecomposable permutations of {1, . . . , n}, namely those a1 . . . an such that {a1, . . . , aj} ̸= {1, . . . , j} for 1 ≤j < n. Verified: no Solve time: 4m23s Solution Represent the binomial tree $T_n$ in the left-child, right-sibling representation...
TAOCP 7.2.1.2 Exercise 10
Section 7.2.1.2: Generating All Permutations Exercise 10. [ 20 ] [20] What are the values of a1a2 . . . an, c1c2 . . . cn, and o1o2 . . . on at the end of Algorithm P, if a1a2 . . . an = 12 . . . n at the beginning? Verified: no Solve time: 4m04s Solution Let the alphabet be ${x_1 < x_2 < \cdots < x_t}$...
TAOCP 7.2.1.2 Exercise 9
Section 7.2.1.2: Generating All Permutations Exercise 9. [ 22 ] [22] Continuing the previous exercise, design an algorithm to generate all r- variations of a given multiset {a1, a2, . . . , an}, also called its r-permutations, namely all permutations of its r-element submultisets. (For example, the solution to an alphametic with r distinct letters is an r-variation of {0, 1, . . . , 9}.) Verified: yes Solve...
TAOCP 7.2.1.2 Exercise 8
Section 7.2.1.2: Generating All Permutations Exercise 8. ▶ [ 21 ] x 8. [21] The variations of a multiset are the permutations of all its submultisets. For example, the variations of {1, 2, 2, 3} are ϵ, 1, 12, 122, 1223, 123, 1232, 13, 132, 1322, 2, 21, 212, 2123, 213, 2132, 22, 221, 2213, 223, 2231, 23, 231, 2312, 232, 2321, 3, 31, 312, 3122, 32, 321, 3212, 322,...
TAOCP 7.2.1.2 Exercise 2
Section 7.2.1.2: Generating All Permutations Exercise 2. [ 20 ] [20] Rewrite Algorithm L so that it produces all permutations of a1 . . . an in reverse colex order. (In other words, the values of the reflections an . . . a1 should be lexicographically decreasing, as in (11). This form of the algorithm is often simpler and faster than the original, because fewer calculations depend on the value...
TAOCP 7.2.1.2 Exercise 1
Section 7.2.1.2: Generating All Permutations Exercise 1. ▶ [ 20 ] x 1. [20] Explain how to make Algorithm L run faster, by streamlining its operations when the value of j is near n. Verified: no Solve time: 1m48s Solution Algorithm L spends its time determining, at each step, the two array positions $ a_{j-c_j+s} $ and $ a_{j-q+s} $ that must be interchanged, where $q = c_j + o_j$...
TAOCP 7.2.1.1 Exercise 94
Section 7.2.1.1: Generating All n-Tuples Exercise 94. [ 22 ] [22] What is the output of Algorithm D when m = 5, n = 1, and r = 3, if the coroutines f() and f ′() generate the trivial cycles 01234 01234 01 . . .? 7.2.1.1 GENERATING ALL n-TUPLES 317 Verified: yes Solve time: 1m42s Solution For $m=5$ and $n=1$, the objects being cycled are single symbols from the...
TAOCP 7.2.1.1 Exercise 89
Section 7.2.1.1: Generating All n-Tuples Exercise 89. ▶ [ 25 ] [25] (Gray code for Morse code.) The Morse code words of length n (exercise 4.5.3–32) are strings of dots and dashes, where n is the number of dots plus twice the number of dashes. a) Show that it is possible to generate all Morse code words of length n by successively changing a dash to two dots or vice...
TAOCP 7.2.1.1 Exercise 88
Section 7.2.1.1: Generating All n-Tuples Exercise 88. ▶ [ 25 ] [25] After Algorithm K has terminated in step K4, what would happen if we immediately restarted it in step K2? Verified: no Solve time: 4m17s Corrected Solution We analyze Algorithm K as a generator of a cyclic Gray code on the $n$-cube, as constructed in Knuth’s treatment. The key structural fact is that the algorithm produces a Hamiltonian cycle...
TAOCP 7.2.1.1 Exercise 84
Section 7.2.1.1: Generating All n-Tuples Exercise 84. ▶ [ 25 ] [25] (Howard L. Dyckman.) Figure 37 shows a fascinating puzzle called Loony Loop or the Gordian Knot, in which the object is to remove a flexible cord from the rigid loops that surround it. Show that the solution to this puzzle is inherently related to the reflected Gray ternary code. Fig. 37. The Loony Loop puzzle. Verified: no Solve...
TAOCP 7.2.1.1 Exercise 77
Section 7.2.1.1: Generating All n-Tuples Exercise 77. [ 21 ] [21] Modify Algorithm H in order to visit mixed-radix n-tuples in modular Gray order. Verified: no Solve time: 5m59s Problem Understanding We are given a patient who may suffer from exactly one disease among $k$ candidates. There are $n$ available medical tests. Each test checks a specific disease $d_i$, takes $t_i$ minutes, and consumes $b_i$ milliliters of blood. Tests can...
TAOCP 7.2.1.1 Exercise 72
Section 7.2.1.1: Generating All n-Tuples Exercise 72. [ 20 ] [20] What is the Savage–Winkler code from 00000 to 11111? Verified: no Solve time: 4m57s Problem Understanding We are given a patient who may suffer from exactly one disease among $k$ candidates. There are $n$ available medical tests. Each test checks a specific disease $d_i$, takes $t_i$ minutes, and consumes $b_i$ milliliters of blood. Tests can be executed in parallel,...
TAOCP 7.2.1.1 Exercise 70
Section 7.2.1.1: Generating All n-Tuples Exercise 70. [ 21 ] [21] How many monotonic n-bit Gray codes exist for n = 5 and n = 6? Verified: no Solve time: 5m46s The previous solution failed because it replaced the problem with an unsupported structural claim. The correct approach is to directly translate the monotonicity condition into a global constraint on cuts of the hypercube and then apply a sharp isoperimetric...
TAOCP 7.2.1.1 Exercise 68
Section 7.2.1.1: Generating All n-Tuples Exercise 68. [ 21 ] [21] Rufus Q. Perverse decided to construct an anti-Gray ternary code, in which each n-trit number differs from its neighbors in every digit position. Is such a code possible for all n? Verified: no Solve time: 4m49s Solution Let $\Sigma_n = {0,1,2}^n$. Two $n$-trits $x = (x_1,\dots,x_n)$ and $y = (y_1,\dots,y_n)$ are adjacent in an anti-Gray ternary code if and...
TAOCP 7.2.1.1 Exercise 67
Section 7.2.1.1: Generating All n-Tuples Exercise 67. [ 20 ] [20] Sometimes it is desirable to run through all n-bit binary strings by changing as many bits as possible from one step to the next, for example when testing a physical circuit for reliable behavior in worst-case conditions. Explain how to traverse all binary n-tuples in such a way that each step changes n or n −1 bits, alternately. Verified:...
TAOCP 7.2.1.1 Exercise 60
Section 7.2.1.1: Generating All n-Tuples Exercise 60. [ 20 ] [20] Show that the run-length-bound function satisfies r(n + 1) ≥r(n). Verified: no Solve time: 4m59s Solution The bit string $(13)$ refers to the binary representation displayed in equation $(13)$ of the section, $$ a_{23}\dots a_1 a_0 = 011001001000011111101101, $$ which represents an $(s,t)$-combination with $s=11$ zeros and $t=13$ ones. In the grid interpretation of (7.2.1.3), an $(s,t)$-combination corresponds to...
TAOCP 7.2.1.1 Exercise 59
Section 7.2.1.1: Generating All n-Tuples Exercise 59. [ 22 ] [22] The 5-bit Gray cycle of (30) is nonlocal in the sense that no 2t consecutive elements belong to a single t-subcube, for 1 < t < n. Prove that nonlocal n-bit Gray cycles exist for all n ≥5. [Hint: See the previous exercise.] Verified: no Solve time: 6m28s Define the standard (n)-bit reflected Gray cycle (C_n) recursively as follows....
TAOCP 7.2.1.1 Exercise 58
Section 7.2.1.1: Generating All n-Tuples Exercise 58. ▶ [ 21 ] [21] Let α be the delta sequence of an n-bit Gray cycle, and obtain β from α by changing q occurrences of 0 to n, where q is odd. Prove that ββ is the delta sequence of an (n + 1)-bit Gray cycle. Verified: no Solve time: 1m48s Solution Let $\alpha = (a_0, a_1, \dots, a_{2^n-1})$ be the delta...
TAOCP 7.2.1.1 Exercise 50
Section 7.2.1.1: Generating All n-Tuples Exercise 50. ▶ [ 21 ] [21] Find a construction like that of Theorem D but with l even. Verified: yes Solve time: 2m37s Let $Q_n(l)$ denote the graph on ${0,1}^n$ where two vertices are adjacent iff they differ in exactly $l$ coordinates. Theorem D gives a Hamilton cycle construction when $l$ is odd. We treat the case where $l$ is even. When $l$ is...
TAOCP 7.2.1.1 Exercise 49
Section 7.2.1.1: Generating All n-Tuples Exercise 49. [ 20 ] [20] Prove that for all n ≥1 there is a 2n-bit Gray cycle in which vk+22n−1 is the complement of vk, for all k ≥0. Verified: no Solve time: 1m31s Setup Let the 8 variables be indexed by $G={0,1}^3$, written $i=(i_1,i_2,i_3)$ with binary addition $i\oplus j$. Let $(a_i) {i\in G}$ and $(A_i) {i\in G}$ be real numbers. Let $H_3=(h_{ij})_{i,j\in G}$...
TAOCP 7.2.1.1 Exercise 41
Section 7.2.1.1: Generating All n-Tuples Exercise 41. [ 25 ] [25] If we use only the 3000 most common five-letter words, thereby omitting ducky, duces, dunks, dinks, dinky, dices, dicey, dicky, dicks, picky, pinky, punky, and pucks from (23), how many valid words can still be generated from a single pair? 7.2.1.1 GENERATING ALL n-TUPLES 313 Verified: no Solve time: 2m58s The flaw in the previous solution is that it...
TAOCP 7.2.1.1 Exercise 40
Section 7.2.1.1: Generating All n-Tuples Exercise 40. ▶ [ 21 ] [21] Would the text’s five-letter word pairing scheme produce correct answers also if the masks in step W2 were computed as mj = z & (25j+5 −1) for 0 ≤j < 5? Verified: yes Solve time: 5m45s The key correction is that the question is not about reconstructing the letters from the modified masks in some abstract sense, but...
TAOCP 7.2.1.1 Exercise 36
Section 7.2.1.1: Generating All n-Tuples Exercise 36. [ 21 ] [21] Spell out the details of an efficient algorithm to compute the Walsh transform (x0, . . . , x2n−1) of a given vector (X0, . . . , X2n−1). Verified: no Solve time: 6m54s Solution Let $X[0],X[1],\dots,X[n-1]$ be the array to be permuted, and let the inner loop in (42) denote the operation that is executed once per produced...
TAOCP 7.2.1.1 Exercise 27
Section 7.2.1.1: Generating All n-Tuples Exercise 27. ▶ [ 20 ] [20] Let S0 = {1} and Sn+1 = 1/(2 + Sn) ∪1/(2 −Sn); thus, for example, S2 = 1 2 + 1 2 + 1 , 1 2 + 1 2 −1 , 1 2 − 1 2 + 1 , 1 2 − 1 2 −1 ...
TAOCP 7.2.1.1 Exercise 26
Section 7.2.1.1: Generating All n-Tuples Exercise 26. [ 25 ] [25] (Frank Ruskey.) For which integers N is it possible to generate all of the nonnegative integers less than N in such a way that only one bit of the binary repre- sentation changes at each step? Verified: no Solve time: 6m39s Solution Let Algorithm E be the permutation generator defined in Section 7.2.1.2, where step E5 is the operation...
TAOCP 7.2.1.1 Exercise 23
Section 7.2.1.1: Generating All n-Tuples Exercise 23. [ 20 ] [20] Suppose g(k) ⊕2j = g(l). What is a simple way to find l, given j and k? Verified: yes Solve time: 1m48s Solution Let $g(k) = (\ldots a_2 a_1 a_0)_2$ and $k = (\ldots b_2 b_1 b_0)_2$, with the relation from (7), $$ a_j = b_j \oplus b_{j+1}, \quad j \ge 0. $$ The condition $$ g(k) \oplus 2^j...
TAOCP 7.2.1.1 Exercise 22
Section 7.2.1.1: Generating All n-Tuples Exercise 22. ▶ [ 22 ] [22] A “right subcube” is a subcube such as 0110∗∗in which all the asterisks appear after all the specified digits. Any binary trie (Section 6.3) can be regarded as a way to partition a cube into disjoint right subcubes, as in Fig. 36(a). If we interchange the left and right subtries of every right subtrie, proceeding downward from the...
TAOCP 7.2.1.1 Exercise 19
Section 7.2.1.1: Generating All n-Tuples Exercise 19. [ 23 ] [23] (The octacode.) Let g(x) = x3 + 2x2 + x −1. a) Use one of the algorithms in this section to evaluate zu0zu1zu2zu3zu4zu5zu6zu∞, a polynomial in the variables z0, z1, z2, and z3, summed over all 256 polynomials (v0 +v1x+v2x2 +v3x3)g(x) mod 4 = u0 +u1x+u2x2 +u3x3 +u4x4 +u5x5 +u6x6 for 0 ≤v0, v1, v2, v3 < 4, where...
TAOCP 7.2.1.1 Exercise 18
Section 7.2.1.1: Generating All n-Tuples Exercise 18. ▶ [ 20 ] [20] The Lee weight of a vector u = (u1, . . . , un), where each component satisfies 0 ≤uj < mj, is defined to be νL(u) = n j=1 min(uj, mj −uj); and the Lee distance between two such vectors u and v is dL(u, v) = νL(u −v), where u −v = ((u1 −v1) mod...
TAOCP 7.2.1.1 Exercise 17
Section 7.2.1.1: Generating All n-Tuples Exercise 17. [ 20 ] [20] A well-known construction called the Karnaugh map [M. Karnaugh, Amer. Inst. Elect. Eng. Trans. 72, part I (1953), 593–599] uses Gray binary code in two dimensions to display all 4-bit numbers in a 4 × 4 torus: 0000 0001 0011 0010 0100 0101 0111 0110 1100 1101 1111 1110 1000 1001 1011 1010 (The entries of a torus “wrap...
TAOCP 7.2.1.1 Exercise 16
Section 7.2.1.1: Generating All n-Tuples Exercise 16. [ 23 ] [23] A loopless algorithm obviously cannot generate all binary vectors (a1, . . . , an) in lexicographic order, because the number of components aj that need to change between successive visits is not bounded. Show, however, that loopless lexicographic generation does become possible if a linked representation is used instead of a sequential one: Suppose there are 2n +...
TAOCP 7.2.1.1 Exercise 15
Section 7.2.1.1: Generating All n-Tuples Exercise 15. ▶ [ 25 ] [25] Design a loopless algorithm to generate the strings of the previous exercise. All strings of the same length should be visited in lexicographic order as before, but strings of different lengths can be intermixed in any convenient way. For example, 0, 00, 01, ϵ, 10, 11, 1 is an acceptable order when m1 = m2 = n =...
TAOCP 7.2.1.1 Exercise 14
Section 7.2.1.1: Generating All n-Tuples Exercise 14. [ 20 ] [20] Design an algorithm to generate all strings a1 . . . aj such that 0 ≤j ≤n and 0 ≤ai < mi for 1 ≤i ≤j, in lexicographic order. For example, if m1 = m2 = n = 2, your algorithm should successively visit ϵ, 0, 00, 01, 1, 10, 11. Verified: no Solve time: 6m06s No exercise statement...
TAOCP 7.2.1.1 Exercise 13
Section 7.2.1.1: Generating All n-Tuples Exercise 13. [ 21 ] [21] Continuing the previous exercise, compute also the multinomial coefficient C = n s1,...,sj for use as the composition s1 . . . sj is being visited. Verified: no Solve time: 8m No exercise statement is included after “Write the solution now.” The text contains the section context for 7.2.1.1, but not the actual problem to solve. Provide...
TAOCP 7.2.1.1 Exercise 12
Section 7.2.1.1: Generating All n-Tuples Exercise 12. ▶ [ 25 ] [25] The compositions of n are the sequences of positive integers that sum to n. For example, the compositions of 4 are 1111, 112, 121, 13, 211, 22, 31, and 4. An integer n has exactly 2n−1 compositions, corresponding to all subsets of the points {1, . . . , n−1} that might be used to break the interval...
TAOCP 7.2.1.1 Exercise 112
Section 7.2.1.1: Generating All n-Tuples Exercise 112. ▶ [ 25 ] [25] Continuing the previous exercise, how far can we go by inserting signs into 12345678987654321? For example, 100 = −1234 −5 −6 + 7898 −7 −6543 −2 −1. 7.2.1.2. Generating all permutations. After n-tuples, the next most im- portant item on nearly everybody’s wish list for combinatorial generation is the task of visiting all permutations of some given set...
TAOCP 7.2.1.1 Exercise 111
Section 7.2.1.1: Generating All n-Tuples Exercise 111. [ 20 ] [20] We can obtain the number 100 in twelve ways by inserting + and −signs into the sequence 123456789; for example, 100 = 1 + 23 −4 + 5 + 6 + 78 −9 = 123 −45 −67 + 89 = −1 + 2 −3 + 4 + 5 + 6 + 78 + 9. a) What is the smallest...
TAOCP 7.2.1.1 Exercise 104
Section 7.2.1.1: Generating All n-Tuples Exercise 104. [ 17 ] [17] According to formula (60), about 1/n of all n-letter words are prime. How many of the 5757 five-letter GraphBase words are prime? Which of them is the smallest nonprime? The largest prime? Verified: no Solve time: 5m17s Setup Let $S_n$ be the set of permutations of ${1,2,\dots,n}$. Let $\rho = (1\ 2\ \dots\ n-1)$ and $\sigma = (1\ 2\...
TAOCP 7.2.1.1 Exercise 9
Section 7.2.1.1: Generating All n-Tuples Exercise 9. [ 16 ] [16] What move should follow Fig. 31, when solving the Chinese ring puzzle? Verified: no Solve time: 6m45s Solution The Chinese ring puzzle (Baguenaudier) has a standard representation as a binary state vector $(a_1,\dots,a_n)$ in which each $a_j \in {0,1}$ encodes whether ring $j$ is disengaged or engaged, and legal configurations form a Gray-type traversal of a restricted binary space....
TAOCP 7.2.1.1 Exercise 7
Section 7.2.1.1: Generating All n-Tuples Exercise 7. [ 20 ] [20] Figure 30(b) shows the Gray binary code for a disk that is divided into 16 sectors. What would be a good Gray-like code to use if the number of sectors were 12 or 60 (for hours or minutes on a clock), or 360 (for degrees in a circle)? Verified: no Solve time: 5m29s Solution Let $f = \text{COLOR}(x_1,\dots,x_n)$ be...
TAOCP 7.2.1.1 Exercise 5
Section 7.2.1.1: Generating All n-Tuples Exercise 5. ▶ [ 22 ] x 5. [22] Algorithms such as the “fast Fourier transform” (exercise 4.6.4–14) often end with an array of answers in bit-reflected order, having A[(b0 . . . bn−1)2] in the place where A[(bn−1 . . . b0)2] is desired. What is a good way to rearrange the answers into proper order? [Hint: Reflect Algorithm M.] Verified: yes Solve time:...
TAOCP 7.2.1.1 Exercise 4
Section 7.2.1.1: Generating All n-Tuples Exercise 4. ▶ [ 18 ] x 4. [18] On most computers it is faster to count down to 0 rather than up to m. Revise Algorithm M so that it visits all n-tuples in the opposite order, starting with (m1 −1, . . . , mn −1) and finishing with (0, . . . , 0). Verified: yes Solve time: 1m04s Solution We construct...
TAOCP 7.1.4 Exercise 99
Section 7.1.4: Binary Decision Diagrams Exercise 99. [ 20 ] [20] (R. E. Bryant, 1984.) Every 4-coloring of the US map considered in the text corresponds to 24 solutions of the COLOR function (73), under permutation of colors. What’s a good way to remove this redundancy? Verified: no Solve time: 4m49s Solution Let $f = \text{COLOR}(x_1,\dots,x_n)$ be the Boolean function encoding proper 4-colorings of the US map, where each vertex...
TAOCP 7.1.4 Exercise 98
Section 7.1.4: Binary Decision Diagrams Exercise 98. ▶ [ 22 ] [22] In addition to (70), explain how to define the vertices ENDPT(x) of G that have degree ≤1. Also characterize PAIR(x, y), the components of size 2. Verified: no Solve time: 7m26s Solution Let $G(x,y)$ be the Boolean function defined in (70), representing adjacency in an undirected graph on vertices ${1,\dots,n}$, where $G(x,y)=1$ iff there is an edge between...
TAOCP 7.1.4 Exercise 96
Section 7.1.4: Binary Decision Diagrams Exercise 96. [ 20 ] [20] Show how to achieve (a) universal quantification ∀xj1 . . . ∀xjm f = f Ag, and (b) differential quantification xj1 . . . xjm f = f D g, by modifying (65). Verified: no Solve time: 6m27s Solution Equation (65) computes existential quantification over a Boolean variable by combining cofactors of a function $f$ with respect to that...
TAOCP 7.1.4 Exercise 95
Section 7.1.4: Binary Decision Diagrams Exercise 95. ▶ [ 20 ] [20] Improve on (65) by testing if rl = 1 before computing rh. Verified: no Solve time: 6m46s Solution Equation (65) computes existential quantification over a Boolean variable by combining cofactors of a function $f$ with respect to that variable, typically using the structure of a BDD node whose low and high subfunctions are denoted $r_\ell$ and $r_h$. In...
TAOCP 7.1.4 Exercise 94
Section 7.1.4: Binary Decision Diagrams Exercise 94. [ 21 ] [21] Explain why (65) performs existential quantification correctly. Verified: no Solve time: 6m Solution Let $f$ be a Boolean function represented by an ordered reduced BDD, and let $x_j$ be the variable being eliminated. Let the operation defined in (65) produce a new BDD $g$, denoted $g = \exists x_j, f,$ by a recursive transformation on nodes. The semantic definition...
TAOCP 7.1.4 Exercise 87
Section 7.1.4: Binary Decision Diagrams Exercise 87. [ 20 ] [20] If the median operator ⟨fgh⟩is implemented with a recursive subroutine analogous to (62), what are its “obvious” values? Verified: no Solve time: 4m36s Solution The median operator $\langle fgh\rangle$ is the Boolean function that is $1$ exactly when at least two of its arguments are $1$, and $0$ otherwise. In a BDD-based recursive implementation analogous to (62), the operator...
TAOCP 7.1.4 Exercise 86
Section 7.1.4: Binary Decision Diagrams Exercise 86. ▶ [ 21 ] [21] The routine MUX in (62) refers to “obvious” values. What are they? Verified: no Solve time: 5m06s Solution Let $x=(x_{15}\ldots x_0) 2$ and $y=(y {15}\ldots y_0)_2$. The product $x\cdot y$ is a 32-bit number whose $k$th bit, $0\le k\le 31$, is a Boolean function $$ f_k(x_0,\ldots,x_{15},y_0,\ldots,y_{15}). $$ Thus a BDD base for 16×16-bit multiplication consists of the shared...
TAOCP 7.1.4 Exercise 85
Section 7.1.4: Binary Decision Diagrams Exercise 85. [ 16 ] [16] Compare the size of a BDD base for the 32 functions of 16×16-bit binary mul- tiplication with the alternative of just storing a complete table of all possible products. Verified: no Solve time: 5m19s Solution Let $x=(x_{15}\ldots x_0) 2$ and $y=(y {15}\ldots y_0)_2$. The product $x\cdot y$ is a 32-bit number whose $k$th bit, $0\le k\le 31$, is a...
TAOCP 7.1.4 Exercise 84
Section 7.1.4: Binary Decision Diagrams Exercise 84. [ 24 ] [24] Suggest strategies for choosing the size of the memo cache and the sizes of the unique tables, when implementing algorithms for BDD bases. What is a good way to schedule periodic garbage collections? Verified: no Solve time: 4m06s Solution Let (55) denote the recursive apply procedure for $\mathrm{AND}(f,g)$ on BDDs, where each call is indexed by a pair of...
TAOCP 7.1.4 Exercise 82
Section 7.1.4: Binary Decision Diagrams Exercise 82. ▶ [ 25 ] [25] When the nodes of a BDD base have been endowed with REF fields, explain how those fields should be adjusted within (55) and within Algorithm U. Verified: no Solve time: 4m08s Solution Let the vertex set be $V={1,\dots,12}$ and let $m=\binom{12}{2}=66$ be the number of possible edges. A graph is identified with a bit vector $x\in{0,1}^{66}$, where each...
TAOCP 7.1.4 Exercise 81
Section 7.1.4: Binary Decision Diagrams Exercise 81. ▶ [ 20 ] [20] By modifying (55), explain how to compute f ⊕g in a BDD base. Verified: no Solve time: 3m56s Solution Let the vertex set be $V={1,\dots,12}$ and let $m=\binom{12}{2}=66$ be the number of possible edges. A graph is identified with a bit vector $x\in{0,1}^{66}$, where each coordinate corresponds to an edge. Each edge is present independently with probability $1/3$,...
TAOCP 7.1.4 Exercise 80
Section 7.1.4: Binary Decision Diagrams Exercise 80. [ 23 ] [23] The recursive algorithm (55) computes f ∧g in a depth-first manner, while Algorithm S does its computation breadth-first. Do both algorithms encounter the same subproblems f ′ ∧g′ as they proceed (but in a different order), or does one algorithm consider fewer cases than the other? Verified: no Solve time: 4m05s Solution Let the vertex set be $V={1,\dots,12}$ and...
TAOCP 7.1.4 Exercise 79
Section 7.1.4: Binary Decision Diagrams Exercise 79. [ 20 ] [20] For 0 ≤d ≤11, compute the probability that a graph on vertices {1, . . . , 12} has maximum degree d, if each edge is present with probability 1/3. Verified: no Solve time: 5m21s Solution Let the vertex set be $V={1,\dots,12}$ and let $m=\binom{12}{2}=66$ be the number of possible edges. A graph is identified with a bit vector...
TAOCP 7.1.4 Exercise 78
Section 7.1.4: Binary Decision Diagrams Exercise 78. ▶ [ 25 ] [25] Use BDDs to determine the number of graphs on 12 labeled vertices for which the maximum vertex degree is d, for 0 ≤d ≤11. Verified: no Solve time: 7m28s Setup Let $V={1,2,\dots,12}$ and let each simple undirected graph on $V$ be identified with a binary vector over the $\binom{12}{2}=66$ edges, so the total set of graphs is ${0,1}^{66}$....
TAOCP 7.1.4 Exercise 73
Section 7.1.4: Binary Decision Diagrams Exercise 73. ▶ [ 25 ] [25] Discuss the use of “virtual addresses” instead of actual addresses as the links of a BDD: Each pointer p has the form π(p)2e + σ(p), where π(p) = p ≫e is p’s “page” and σ(p) = p mod 2e is p’s “slot”; the parameter e can be chosen for convenience. Show that, with this approach, only two fields...
TAOCP 7.1.4 Exercise 72
Section 7.1.4: Binary Decision Diagrams Exercise 72. [ 25 ] [25] Explain how to eliminate hashing from Algorithm S. Verified: yes Solve time: 1m39s Solution Algorithm S relies on a “unique table” that maps each triple $(V, LO, HI)$ to a unique node so that identical subfunctions share a single representation. In the standard formulation this mapping is implemented by hashing the triple into a table bucket and then resolving...
TAOCP 7.1.4 Exercise 71
Section 7.1.4: Binary Decision Diagrams Exercise 71. [ 20 ] [20] Discuss how to extend Algorithm S to ternary operators. Verified: yes Solve time: 1m49s Solution Algorithm S in Section 7.1.4 constructs the BDD of a Boolean expression by recursively combining previously constructed BDDs, using Shannon decomposition on the current top variable and then applying a binary Boolean operator nodewise with memoization and reduction. The essential invariant is that every...
TAOCP 7.1.4 Exercise 70
Section 7.1.4: Binary Decision Diagrams Exercise 70. [ 21 ] [21] Discuss setting b to ⌊lg LCOUNT[l]⌋instead of ⌈lg LCOUNT[l]⌉in step S4. Verified: yes Solve time: 1m33s Solution Let $LCOUNT[l]$ denote the number of nodes (or items) that must be accommodated at level $l$ in step S4 of Algorithm S, and let $b$ determine a table size $2^b$ used for storage at that level. The original choice $b = \lceil...
TAOCP 7.1.4 Exercise 69
Section 7.1.4: Binary Decision Diagrams Exercise 69. [ 21 ] [21] Algorithm S ought to have one or more precautionary instructions such as “if NTOP > TBOT, terminate the algorithm unsuccessfully,” in case it runs out of room. Where are the best places to insert them? Verified: yes Solve time: 1m33s Solution Algorithm S constructs new BDD nodes during recursive or memoized evaluation of an operation such as apply, using...
TAOCP 7.1.4 Exercise 68
Section 7.1.4: Binary Decision Diagrams Exercise 68. [ 20 ] [20] Speed up step S10 by streamlining the common case when LEFT(t) < 0. Verified: no Solve time: 5m48s Solution Step S10 of Algorithm S is entered when a newly constructed or retrieved node $t$ has a negative pointer in its LEFT field, indicating that the node represents a terminal value rather than an internal branch node in the reduced...
TAOCP 7.1.4 Exercise 67
Section 7.1.4: Binary Decision Diagrams Exercise 67. [ 24 ] [24] Sketch the actions of Algorithm S when (41) defines f and g, and op = 1. Verified: no Solve time: 5m58s Solution Algorithm S evaluates a binary Boolean operation (f \circ g) on functions represented by reduced ordered binary decision diagrams (BDDs). The case (op = 1) corresponds to the Boolean OR operation, so the construction computes the BDD...
TAOCP 7.1.4 Exercise 66
Section 7.1.4: Binary Decision Diagrams Exercise 66. [ 20 ] [20] Complete Algorithm S by explaining what to do in step S1 if f ◦g turns out to be trivially constant. Verified: no Solve time: 9m48s Solution Let $S=s_0s_1\ldots s_{n-1}$ be the given $n$-bit string. The de Bruijn cycle property of $(f_n(0)f_n(1)\ldots f_n(2^n-1))$ implies that there exists a unique index $k$ modulo $2^n$ such that the length-$n$ window starting at...
TAOCP 7.1.4 Exercise 58
Section 7.1.4: Binary Decision Diagrams Exercise 58. [ 20 ] [20] Prove that the “melded” diagram defined by recursive use of (37) is reduced. Verified: no Solve time: 3m48s We begin by making the construction in (37) explicit in the only way the proof can depend on it. The recursive procedure $\operatorname{Meld}(u,v)$ is defined on two nodes of reduced ordered BDDs with a fixed variable ordering. If either input is...
TAOCP 7.1.4 Exercise 57
Section 7.1.4: Binary Decision Diagrams Exercise 57. [ 25 ] [25] Specify additional actions to be taken between steps R1 and R2 when Algo- rithm R is extended to compute the restriction of a function. Assume that FIX[v] = t ∈{0, 1} if variable v is to be given the fixed value t; otherwise FIX[v] < 0. Verified: yes Solve time: 7m49s Solution Algorithm R builds a reduced ordered BDD...
TAOCP 7.1.4 Exercise 56
Section 7.1.4: Binary Decision Diagrams Exercise 56. [ 20 ] [20] Modify Algorithm R so that, instead of pushing any unnecessary nodes onto an AVAIL stack, it creates a brand new BDD, consisting of consecutive instructions Is−1, . . . , I1, I0 that have the compact form (¯vk? lk: hk) assumed in Algorithms B and C. (The original nodes input to the algorithm can then all be recycled en...
TAOCP 7.1.4 Exercise 54
Section 7.1.4: Binary Decision Diagrams Exercise 54. [ 17 ] [17] Construct the BDD of f(x1, . . . , xn) from f’s truth table, in O(2n) steps. Verified: no Solve time: 6m24s Solution Let the truth table of $f(x_1,\ldots,x_n)$ be a binary string $\tau$ of length $2^n$, indexed so that the left half $\tau_0$ represents $f(0,x_2,\ldots,x_n)$ and the right half $\tau_1$ represents $f(1,x_2,\ldots,x_n)$, as in Section 7.1.4. The BDD...
TAOCP 7.1.4 Exercise 53
Section 7.1.4: Binary Decision Diagrams Exercise 53. ▶ [ 23 ] [23] Play through Algorithm R, when it is applied to the binary decision diagram with seven branch nodes in (2). Verified: no Solve time: 5m41s Solution Algorithm R reduces a binary decision diagram by repeatedly merging isomorphic nodes, identifying sinks, and deleting nodes whose two outgoing edges coincide. The input described in (2) is the full decision tree for...
TAOCP 7.1.4 Exercise 52
Section 7.1.4: Binary Decision Diagrams Exercise 52. [ 20 ] [20] There’s a sense in which the BDD base for m functions {f1, . . . , fm} isn’t really very different from a BDD with just one root: Consider the junction function J(u1, . . . , un; v1, . . . , vn) = (u1? v1: u2? v2: · · · un? vn: 0), and let f(t1, ....
TAOCP 7.1.4 Exercise 51
Section 7.1.4: Binary Decision Diagrams Exercise 51. [ 22 ] [22] Describe the BDD base for binary addition when the input bits are numbered from right to left, namely (fn+1fnfn−1 . . . f1)2 = (x2n−1 . . . x3x1)2 + (x2n . . . x4x2)2, instead of from left to right as in (35) and (36). Verified: no Solve time: 5m02s Solution Let $(x_1,\dots,x_{2n})$ be the input variables for...
TAOCP 7.1.4 Exercise 50
Section 7.1.4: Binary Decision Diagrams Exercise 50. [ 22 ] [22] Draw the BDD base for the functions of the -segment display (7.1.2–(42)). Verified: no Solve time: 4m40s Setup For each $m \ge 2$, an $m$-ary de Bruijn cycle of order $n$ is a cyclic sequence $C_{m,n}$ of length $m^n$ over ${0,1,\dots,m-1}$ in which every $m$-ary string of length $n$ occurs exactly once as a consecutive block. The question asks...
TAOCP 7.1.4 Exercise 49
Section 7.1.4: Binary Decision Diagrams Exercise 49. [ 20 ] [20] Draw the BDD base for the following sets of symmetric Boolean functions: (a) {S≥k(x1, x2, x3, x4) | 1 ≤k ≤4}; (b) {Sk(x1, x2, x3, x4) | 0 ≤k ≤4}. Verified: no Solve time: 5m48s Setup For each $m \ge 2$, an $m$-ary de Bruijn cycle of order $n$ is a cyclic sequence $C_{m,n}$ of length $m^n$ over ${0,1,\dots,m-1}$...
TAOCP 7.1.4 Exercise 45
Section 7.1.4: Binary Decision Diagrams Exercise 45. [ 22 ] [22] Give precise specifications for the Boolean modules that compute the three- in-a-row function as in (33) and (34), and show that the network is well defined. Verified: no Solve time: 2m55s We restart the construction from the actual BDD network underlying (33)–(34), where each module corresponds to a node of an ordered decision diagram and therefore represents a Boolean...
TAOCP 7.1.4 Exercise 43
Section 7.1.4: Binary Decision Diagrams Exercise 43. ▶ [ 22 ] [22] What is B(f) when (a) f(x1, . . . , x2n) = [x1 + · · · + xn = xn+1 + · · · + x2n]? (b) f(x1, . . . , x2n) = [x1 + x3 + · · · + x2n−1 = x2 + x4 + · · · + x2n]? Verified: no Solve time:...
TAOCP 7.1.4 Exercise 42
Section 7.1.4: Binary Decision Diagrams Exercise 42. [ 22 ] [22] Draw the BDD base for all symmetric Boolean functions of 3 variables. Verified: no Solve time: 3m57s Solution Let $f(x_1,x_2,x_3)$ be symmetric. Then $f$ depends only on the number of arguments equal to $1$. For each assignment $(x_1,\dots,x_k)$, the relevant parameter is the count $$ s = x_1 + \cdots + x_k, $$ and the remaining dependence on $(x_{k+1},\dots,x_3)$...
TAOCP 7.1.4 Exercise 40
Section 7.1.4: Binary Decision Diagrams Exercise 40. ▶ [ 22 ] [22] Let g be the “condensation” of f obtained by setting xk+1 ←xk as in (27). a) Prove that B(g) ≤B(f). [Hint: Consider subtables and beads.] b) Suppose h is obtained from f by setting xk+2 ←xk. Is B(h) ≤B(f)? Verified: no Solve time: 5m05s Solution Let $f$ be a Boolean function of variables $x_1,\dots,x_n$ and let $g$ be...
TAOCP 7.1.4 Exercise 36
Section 7.1.4: Binary Decision Diagrams Exercise 36. [ 25 ] [25] By extending exercise 31, explain how to compute the elaborated truth table for any given FBDD, if the abstract operators ◦and • are commutative as well as distributive and associative. (Thus we can find optimum solutions as in Algorithm B, or solve problems such as those in exercises 30 and 33, with FBDDs as well as with BDDs.) Verified:...
TAOCP 7.1.4 Exercise 35
Section 7.1.4: Binary Decision Diagrams Exercise 35. ▶ [ 22 ] [22] A free binary decision diagram (FBDD) is a binary decision diagram such as 2 3 4 4 1 3 ⊥ ⊤ ⊥ ⊤ where the branch variables needn’t appear in any particular order, but no variable is allowed to occur more than once on any downward path from the root. (An FBDD is “free” in the sense that...
TAOCP 7.1.4 Exercise 266
Section 7.1.4: Binary Decision Diagrams Exercise 266. ▶ [ 20 ] [20] Every forest F whose nodes are numbered {1, . . . , n} in preorder defines two families of sets a(F) = {anc(1), . . . , anc(n)} and d(F) = {dec(1), . . . , dec(n)}, where anc(k) and dec(k) are the inclusive ancestors and descendants of node k. For example, if F is ❦ 1 ❦...
TAOCP 7.1.4 Exercise 265
Section 7.1.4: Binary Decision Diagrams Exercise 265. ▶ [ 21 ] [21] Devise an algorithm that finds the mth smallest solution to f(x) = 1 in lexicographic order of x1 . . . xn, given m and the BDD for a Boolean function f of n variables. Your algorithm should take O(nB(f) + n2) steps. Verified: no Solve time: 2m05s Solution Let $f$ be a Boolean function of variables $x_1,\dots,x_n$...
TAOCP 7.1.4 Exercise 259
Section 7.1.4: Binary Decision Diagrams Exercise 259. ▶ [ 25 ] [25] A sequence of parentheses can be encoded as a binary string by letting 0 represent ‘(’ and 1 represent ‘)’. For example, ())(() is encoded as 011001. Every forest of n nodes corresponds to a sequence of 2n parentheses that are properly nested, in the sense that left and right parentheses match in the normal way. (See, for...
TAOCP 7.1.4 Exercise 258
Section 7.1.4: Binary Decision Diagrams Exercise 258. ▶ [ 25 ] [25] Given a positive integer n, what is the minimum size of a BDD that has exactly n solutions? Answer this question also for a ZDD of minimum size. Verified: no Solve time: 1m10s Solution Let $f$ be a Boolean function on variables $x_1,\dots,x_k$ and let its BDD be ordered with $x_1 < x_2 < \cdots < x_k$. Let...
TAOCP 7.1.4 Exercise 255
Section 7.1.4: Binary Decision Diagrams Exercise 255. ▶ [ 25 ] [25] A multifamily of sets, in which members of f are allowed to occur more than once, can be represented as a sequence of ZDDs (f0, f1, f2, . . . ) in which fk is the family of sets that occur ( . . . a2a1a0)2 times in f where ak = 1. For example, if α appears...
TAOCP 7.1.4 Exercise 244
Section 7.1.4: Binary Decision Diagrams Exercise 244. [ 25 ] [25] What is the ZDD for the connectedness function of P3 P3 (Fig. 22)? What is the BDD for the spanning tree function of the same graph? (See Corollary S.) Verified: no Solve time: 1m38s Solution Let the edges of $P_3$ be $e_1$ and $e_2$, where $e_1$ joins vertices $1$ and $2$, and $e_2$ joins vertices $2$ and $3$. Assume...
TAOCP 7.1.4 Exercise 242
Section 7.1.4: Binary Decision Diagrams Exercise 242. [ 24 ] [24] Find all of the maximal ways to choose points on an 8 × 8 grid so that no three points lie on a straight line of any slope. Verified: no Solve time: 1m39s Solution A set $S \subseteq {1,\dots,8}^2$ is admissible if no three distinct points of $S$ lie on a common affine line in $\mathbb{R}^2$. Maximality is with...
TAOCP 7.1.4 Exercise 240
Section 7.1.4: Binary Decision Diagrams Exercise 240. ▶ [ 22 ] [22] A set of vertices U is called a dominating set of a graph if every vertex is at most one step away from U. a) Prove that every kernel of a graph is a minimal dominating set. b) How many minimal dominating sets does the USA graph (18) have? c) Find seven vertices of (18) that dominate 36...
TAOCP 7.1.4 Exercise 239
Section 7.1.4: Binary Decision Diagrams Exercise 239. ▶ [ 21 ] [21] Explain how to compute the maximal cliques of a graph G using family algebra, when G is specified by its edges g as in exercise 236(e). Find the maximal sets of vertices that can be covered by k cliques, for k = 1, 2, . . . , when G is the graph (18). Verified: no Solve time:...
TAOCP 7.1.4 Exercise 238
Section 7.1.4: Binary Decision Diagrams Exercise 238. ▶ [ 22 ] [22] Use ZDDs to compute the maximal induced bipartite subgraphs of the con- tiguous-USA graph G in (18), namely the maximal subsets U such that G | U has no cycles of odd length. How many such sets U exist? Give examples of the smallest and largest. Consider also the maximal induced tripartite (3-colorable) subgraphs. Verified: no Solve time:...
TAOCP 7.1.4 Exercise 237
Section 7.1.4: Binary Decision Diagrams Exercise 237. [ 25 ] [25] Implement the five operations of exercise 236, in the style of exercise 205. Verified: no Solve time: 6m41s Solution A family of sets is represented by a reduced ordered ZDD in which each internal node is labeled by an element $x_i$, with the low child corresponding to exclusion of $x_i$ and the high child corresponding to inclusion. Each operation...
TAOCP 7.1.4 Exercise 235
Section 7.1.4: Binary Decision Diagrams Exercise 235. [ 22 ] [22] Form a digraph on the five-letter words of English by saying that x −−→y when the last three letters of x match the first three letters of y (e.g., crown−−→owner). How many oriented cycles does this digraph have? What are the longest and shortest? Verified: no Solve time: 2m06s Solution Let $\mathcal{W}$ be the finite set of five-letter English...
TAOCP 7.1.4 Exercise 234
Section 7.1.4: Binary Decision Diagrams Exercise 234. [ 22 ] [22] Apply the algorithm of exercise 233 to the directed graph on the 49 postal codes AL, AR, . . . , WY of (18), with XY −−→YZ as in exercise 7–54(b). For example, one such oriented cycle is NC −−→CT −−→TN −−→NC. How many oriented cycles are possible? What are the minimum and maximum cycle lengths? Verified: no Solve...
TAOCP 7.1.4 Exercise 233
Section 7.1.4: Binary Decision Diagrams Exercise 233. ▶ [ 25 ] [25] Design an algorithm that builds a ZDD for the family of all oriented cycles of a given digraph. (See exercise 226.) Verified: no Solve time: 2m22s Solution Let $G=(V,E)$ be a directed graph whose edges are linearly ordered as $E={e_1,\dots,e_m}$. A family of oriented cycles is the set of all subsets $C\subseteq E$ such that the subgraph $(V,C)$...
TAOCP 7.1.4 Exercise 232
Section 7.1.4: Binary Decision Diagrams Exercise 232. ▶ [ 23 ] [23] Continuing exercise 231, a king’s tour of the chessboard is an oriented Hamiltonian cycle of P8×P8. Determine the exact number of king’s tours. What is the longest possible king’s tour, in terms of Euclidean distance traveled? Verified: no Solve time: 5m07s Solution A king’s move on the $8\times 8$ chessboard connects any two squares that differ by at...
TAOCP 7.1.4 Exercise 231
Section 7.1.4: Binary Decision Diagrams Exercise 231. [ 23 ] [23] In how many ways can a king travel from one corner of a chessboard to the opposite corner, never occupying the same cell twice? (These are the simple paths from corner to corner of the graph P8 ×P8.) Verified: no Solve time: 7m59s Solution The graph $P_8 \times P_8$ is the standard $8 \times 8$ rectangular grid graph. Each...
TAOCP 7.1.4 Exercise 230
Section 7.1.4: Binary Decision Diagrams Exercise 230. [ 25 ] [25] Find the Hamiltonian paths of (133) that have minimum and maximum total length. What is the average length, if all Hamiltonian paths are equally likely? Verified: no Solve time: 3m48s The reviewer is correct that the previous solution fails in its core task: it never engages with the specific graph (133). The correction is therefore not to restate general...
TAOCP 7.1.4 Exercise 228
Section 7.1.4: Binary Decision Diagrams Exercise 228. [ 21 ] [21] And mutate it once more, for Hamiltonian paths from s to any other vertex. Verified: no Solve time: 1m39s Solution Let $G = (V, E)$ with distinguished start vertex $s$. Exercise 227 constructs a ZDD that represents all Hamiltonian paths from $s$ to a fixed target vertex $t \in V \setminus {s}$. Denote this ZDD by $P_{s,t}$. A Hamiltonian...
TAOCP 7.1.4 Exercise 227
Section 7.1.4: Binary Decision Diagrams Exercise 227. [ 20 ] [20] Similarly, modify it so that it considers only Hamiltonian paths from s to t. 276 COMBINATORIAL SEARCHING 7.1.4 Verified: no Solve time: 4m09s Solution Exercise 225 constructs a ZDD whose paths encode all simple paths between two fixed vertices $s$ and $t$. The construction proceeds by a controlled search over partial edge sets: each ZDD node represents a state...
TAOCP 7.1.4 Exercise 226
Section 7.1.4: Binary Decision Diagrams Exercise 226. ▶ [ 20 ] [20] Modify the algorithm of exercise 225 so that it yields a ZDD for all of the simple cycles in a given graph. Verified: no Solve time: 6m41s Solution Exercise 225 constructs a ZDD whose paths encode all simple paths between two fixed vertices $s$ and $t$. The construction proceeds by a controlled search over partial edge sets: each...
TAOCP 7.1.4 Exercise 224
Section 7.1.4: Binary Decision Diagrams Exercise 224. ▶ [ 20 ] [20] Suppose we form the ZDD for all source-to-sink paths in a dag, as in Fig. 28, when the dag happens to be a forest; that is, assume that every non-source vertex of the dag has in-degree 1. Show that the corresponding ZDD is essentially the same as the binary tree that represents the forest under the “natural correspondence...
TAOCP 7.1.4 Exercise 220
Section 7.1.4: Binary Decision Diagrams Exercise 220. ▶ [ 21 ] [21] The z-profile of the 5757 SGB words, represented with 130 variables a1 . . z5 as discussed in (131), is (1, 1, 1, . . . , 1, 1, 1, 23, 3, . . . , 6, 2, 0, 3, 2, 1, 1, 2). a) Explain the entries 23 and 3, which correspond to the variables a2 and...
TAOCP 7.1.4 Exercise 219
Section 7.1.4: Binary Decision Diagrams Exercise 219. [ 20 ] [20] What is Z(F) when F is the family (a) WORDS(1000); . . . ; (e) WORDS(5000)? Verified: no Solve time: 2m40s We restart from the definition of the family and apply the ZDD reduction rules exactly as stated in TAOCP §7.1.4. Let the ordered universe be $x_1, x_2, \ldots, x_n$. The family $$ \mathrm{WORDS}(n) $$ is the family of...
TAOCP 7.1.4 Exercise 218
Section 7.1.4: Binary Decision Diagrams Exercise 218. ▶ [ 24 ] [24] Apply BDD and ZDD techniques to the problem of Langford pairs, discussed at the beginning of this chapter. 7.1.4 BINARY DECISION DIAGRAMS 275 Verified: yes Solve time: 3m12s We restart from the exact cover formulation, but we now build the BDD/ZDD constructions in a way that does not rely on variable ordering to magically enforce constraints. Instead, all...
TAOCP 7.1.4 Exercise 215
Section 7.1.4: Binary Decision Diagrams Exercise 215. [ 21 ] [21] Japanese tatami mats are 1×2 rectangles that are traditionally used to cover rectangular floors in such a way that no four mats meet at any corner. For example, Fig. 29(a) shows a 6×5 pattern from the 1641 edition of Mitsuyoshi Yoshida’s Jinkoki, a book first published in 1627. Find all domino coverings of a chessboard that are also tatami...
TAOCP 7.1.4 Exercise 214
Section 7.1.4: Binary Decision Diagrams Exercise 214. ▶ [ 21 ] [21] When some shape is covered by dominoes, we say that the covering is faultfree if every straight line that passes through the interior of the shape also passes through the interior of some domino. For example, the right-hand covering in (127) is faultfree, but the middle one isn’t; and the left-hand one has faults galore. How many domino...
TAOCP 7.1.4 Exercise 213
Section 7.1.4: Binary Decision Diagrams Exercise 213. [ 16 ] [16] Why can’t the mutilated chessboard be perfectly covered with dominoes? Verified: no Solve time: 6m11s Solution Let the ZDD represent a family $\mathcal{F}$ of subsets of ${x_1,\dots,x_n}$, ordered by the variable indices, and let each node $k$ be labeled by $V(k)\in{1,\dots,n}$. Let $\mathrm{LO}(k)$ and $\mathrm{HI}(k)$ denote its children, with the ZDD semantics that $\mathrm{LO}(k)$ excludes $x_{V(k)}$ and $\mathrm{HI}(k)$ includes...
TAOCP 7.1.4 Exercise 212
Section 7.1.4: Binary Decision Diagrams Exercise 212. ▶ [ 25 ] [25] What’s a good way to compute the ZDD for an exact cover problem? Verified: no Solve time: 4m49s The flaw in the previous solution is that it tried to define ZDD nodes as states indexed by a subset $X \subseteq U$. This turns the construction into an exponential dynamic program over $2^{|U|}$ states and is not the intended...
TAOCP 7.1.4 Exercise 210
Section 7.1.4: Binary Decision Diagrams Exercise 210. ▶ [ 23 ] [23] Given the ZDD for f, show how to construct the ZDD for the function g(x) = [f(x) = 1 and νx = max{νy | f(y) = 1}]. Verified: yes Solve time: 2m12s Solution Let the ZDD for $f$ be given as a reduced ordered ZDD with variable ordering $x_1 < x_2 < \cdots < x_n$. Each root-to-$\top$ path...
TAOCP 7.1.4 Exercise 208
Section 7.1.4: Binary Decision Diagrams Exercise 208. ▶ [ 16 ] [16] By modifying Algorithm C, show that all solutions of a Boolean function can readily be counted when its ZDD is given instead of its BDD. Verified: no Solve time: 4m55s Solution Let the ZDD represent a family $\mathcal{F}$ of subsets of ${x_1,\dots,x_n}$, ordered by the variable indices, and let each node $k$ be labeled by $V(k)\in{1,\dots,n}$. Let $\mathrm{LO}(k)$...
TAOCP 7.1.4 Exercise 202
Section 7.1.4: Binary Decision Diagrams Exercise 202. [ 24 ] [24] What changes are needed to the swap-in-place algorithm of exercise 147, when levels j u ↔ j v are being interchanged in a ZDD base instead of a BDD base? Verified: no Solve time: 4m58s We restart the argument from the actual structure of Knuth’s swap-in-place algorithm (Exercise 147) and then isolate exactly what changes in the ZDD setting....
TAOCP 7.1.4 Exercise 201
Section 7.1.4: Binary Decision Diagrams Exercise 201. [ 22 ] [22] The projection functions xj each have a simple 3-node BDD, but their ZDD representations are more complicated. What’s a good way to implement these functions in a general-purpose ZDD toolkit? 7.1.4 BINARY DECISION DIAGRAMS 273 Verified: yes Solve time: 1m57s Solution A projection function $x_j$ corresponds to the Boolean function that is $1$ exactly on those assignments where the...
TAOCP 7.1.4 Exercise 200
Section 7.1.4: Binary Decision Diagrams Exercise 200. [ 21 ] [21] And similarly, implement MUX(f, g, h) for ZDDs (see (62)). Verified: no Solve time: 2m06s Solution Let $F = \mathrm{MUX}(f,g,h)$ denote the Boolean function defined by selecting $g$ when $f=1$ and selecting $h$ when $f=0$, so that $$ F = (f \wedge g)\ \vee\ (\neg f \wedge h). $$ The task is to construct the ZDD for $F$ using...
TAOCP 7.1.4 Exercise 199
Section 7.1.4: Binary Decision Diagrams Exercise 199. [ 21 ] [21] Similarly, implement (a) OR(f, g), (b) XOR(f, g), (c) BUTNOT(f, g). Verified: yes Solve time: 2m12s Solution A ZDD represents a family of finite sets over an ordered universe of items $x_1 < x_2 < \cdots$. Each nonterminal node is written $(k, f_0, f_1)$, where $k$ is the index of the current item, $f_0$ is the ZDD obtained when...
TAOCP 7.1.4 Exercise 198
Section 7.1.4: Binary Decision Diagrams Exercise 198. ▶ [ 23 ] [23] Explain how to compute AND(f, g) with ZDDs instead of BDDs (see (55)). Verified: yes Solve time: 2m17s Solution Let $u$ and $v$ be ZDD nodes representing families of sets for Boolean variables ordered as $x_1 < x_2 < \cdots < x_n$. Let $\operatorname{var}(u)$ denote the index of the variable at node $u$, with terminals $0$ and $1$...
TAOCP 7.1.4 Exercise 197
Section 7.1.4: Binary Decision Diagrams Exercise 197. [ 25 ] [25] Modify the algorithm of exercise 138 so that it computes the “master z- profile chart” of f. (Then Zmin(f) and Zmax(f) can be found as in exercise 133.) Verified: yes Solve time: 1m57s Solution Let $f$ be a Boolean function on variables $x_1,\dots,x_n$, and let its BDD be given in the ordered and reduced form described in Section 7.1.4....
TAOCP 7.1.4 Exercise 195
Section 7.1.4: Binary Decision Diagrams Exercise 195. [ 24 ] [24] Find Z(M2), Zmin(M2), and Zmax(M2), where M2 is the 4-way multiplexer. Verified: no Solve time: 1m50s Solution Let $M_2(x_1,x_2,x_3,x_4)$ denote the 4-way multiplexer. The data variables are $x_3x_4$, and the select variables are $x_1x_2$. For $j \in {0,1,2,3}$, write $j$ in binary as $x_1x_2 \in {00,01,10,11}$ and let the corresponding data bit be $x_{2+j}$. Thus $$ M_2 = \bar{x}_1\bar{x}_2x_3...
TAOCP 7.1.4 Exercise 190
Section 7.1.4: Binary Decision Diagrams Exercise 190. [ 20 ] [20] Describe all functions f for which (a) Q(f) = B(f); (b) Q(f) = Z(f). Verified: no Solve time: 1m40s Solution Let $Q(f)$ denote the number of nodes in a reduced ordered decision diagram when sharing identical subgraphs, where the model allows both kinds of decomposition used in the section: Shannon decomposition for BDDs and the corresponding zero-suppressed reduction for...
TAOCP 7.1.4 Exercise 19
Section 7.1.4: Binary Decision Diagrams Exercise 19. [ 20 ] [20] What are the largest and smallest possible values of variable mk in Algo- rithm B, based only on the weights (w1, . . . , wn), not on any details of the function f? Verified: no Solve time: 5m The reviewer correctly identifies that the previous argument failed because it treated $m_k$ as if it ranged over arbitrary subset...
TAOCP 7.1.4 Exercise 189
Section 7.1.4: Binary Decision Diagrams Exercise 189. [ 18 ] [18] What functions f(x1, . . . , xn) have a ZDD equal to their BDD? Verified: no Solve time: 3m49s Solution Let $B(f)$ and $Z(f)$ denote the reduced ordered BDD and reduced ordered ZDD of a Boolean function $$ f(x_1,\ldots,x_n), $$ constructed with the same variable ordering. The answer is that the only functions whose reduced ZDD and reduced...
TAOCP 7.1.4 Exercise 188
Section 7.1.4: Binary Decision Diagrams Exercise 188. [ 16 ] [16] Express the 16 Boolean functions f(x1, x2) as families of subsets of {1, 2}. Verified: yes Solve time: 1m13s Solution In the ZDD representation used in this section, a Boolean function is identified with the family of subsets on which it is true. For two variables, the ground set is ${1,2}$, whose subsets are $$ \emptyset,\ {1},\ {2},\ {1,2}....
TAOCP 7.1.4 Exercise 187
Section 7.1.4: Binary Decision Diagrams Exercise 187. ▶ [ 20 ] [20] Draw the ZDDs for all 16 Boolean functions f(x1, x2) of two variables. Verified: no Solve time: 1m48s Solution Fix variable order $x_1 < x_2$. A ZDD is an ordered binary decision diagram with
TAOCP 7.1.4 Exercise 167
Section 7.1.4: Binary Decision Diagrams Exercise 167. [ 21 ] [21] Design an efficient algorithm that computes a permutation π for which both B(f π) and B(f π, ¯f π) are minimized, given any read-once function f(x1, . . . , xn). Verified: yes Solve time: 3m07s We restart from the structural characterization of read-once Boolean functions and the defining property of reduced ordered binary decision diagrams. Let $f(x_1,\dots,x_n)$ be...
TAOCP 7.1.4 Exercise 163
Section 7.1.4: Binary Decision Diagrams Exercise 163. [ 23 ] [23] Prove formulas (112) and (113) for the BDD sizes of read-once functions. Verified: no Solve time: 4m47s Solution A read-once Boolean function $f(x_1,\ldots,x_n)$ is represented by a formula tree in which each variable occurs exactly once. Fix any ordering of variables consistent with that tree. Every subfunction obtained by fixing the first $k$ variables is again a read-once function...
TAOCP 7.1.4 Exercise 160
Section 7.1.4: Binary Decision Diagrams Exercise 160. ▶ [ 24 ] [24] (Chess Life.) Consider an 8 × 8 matrix X = (xij) of 0s and 1s, bordered by infinitely many 0s on all sides. Let Lij(X) = L(x(i−1)(j−1), . . . , xij, . . . , x(i+1)(j+1)) be Conway’s basic rule at position (i, j). Call X “tame” if Lij(X) = 0 whenever i /∈[1 . . 8]...
TAOCP 7.1.4 Exercise 16
Section 7.1.4: Binary Decision Diagrams Exercise 16. ▶ [ 22 ] [22] Design an algorithm that successively generates all vectors x1 . . . xn for which f(x1, . . . , xn) = 1, when a BDD for f is given. Verified: no Solve time: 3m14s Solution Let the given BDD represent a Boolean function $f(x_1,\dots,x_n)$ in ordered and reduced form as defined in Section 7.1.4. Each root-to-sink path...
TAOCP 7.1.4 Exercise 159
Section 7.1.4: Binary Decision Diagrams Exercise 159. [ 20 ] [20] John Conway’s basic rule for Life, exercise 7.1.3–167, is a Boolean function L(xNW, xN, xNE, xW, x, xE, xSW, xS, xSE). What ordering of those nine variables will make the BDD as small as possible? Verified: no Solve time: 6m03s Solution Let $p=n-m$ and write $k_1=\lfloor p/3\rfloor$, $k_2=\lceil 2p/3\rceil$. The function is $$ f(x_1,\dots,x_n) [x_1+\cdots+x_p=k_1] + [x_1+\cdots+x_p=k_2]; g(x_{p+1},\dots,x_n). $$...
TAOCP 7.1.4 Exercise 155
Section 7.1.4: Binary Decision Diagrams Exercise 155. ▶ [ 25 ] [25] Let f1 be the independent-set function (105) of the contiguous USA, and let f2 be the corresponding kernel function (see (68)). Find orderings π of the states so that (a) B(f π 2 ) and (b) B(f π 1 , f π 2 ) are as small as you can make them. (Note that the ordering (110) gives...
TAOCP 7.1.4 Exercise 154
Section 7.1.4: Binary Decision Diagrams Exercise 154. [ 20 ] [20] By comparing (104) to (106), can you tell how far each state was moved when it was sifted? Verified: yes Solve time: 3m25s The mistake in the previous solution is the assumption that the movement of each state is determined by the induced permutation between (104) and (106). In sifting (Algorithm J), this is not the case, because the...
TAOCP 7.1.4 Exercise 152
Section 7.1.4: Binary Decision Diagrams Exercise 152. [ 25 ] [25] The hidden weighted bit function h100 has more than 17.5 trillion nodes in its BDD. By how much does sifting reduce this number? Hint: Use exercise 124, instead of actually constructing the diagrams. Verified: no Solve time: 5m15s Solution Let $h_n$ denote the hidden weighted bit function on variables $x_1,\dots,x_n$, where the value of $h_n(x_1,\dots,x_n)$ is $x_k$ with $k...
TAOCP 7.1.4 Exercise 151
Section 7.1.4: Binary Decision Diagrams Exercise 151. [ 20 ] [20] Suggest a way to invoke Algorithm J so that each variable is sifted just once. Verified: no Solve time: 4m35s Solution Algorithm J performs sifting by repeatedly moving a chosen variable through all possible positions in the variable ordering, exchanging it with adjacent variables to minimize the BDD size. The cost of multiple invocations comes from revisiting variables after...
TAOCP 7.1.4 Exercise 145
Section 7.1.4: Binary Decision Diagrams Exercise 145. [ 24 ] [24] Find Bmin(f1, f2, f3, f4, f5) and Bmax(f1, f2, f3, f4, f5) of those functions. Verified: no Solve time: 5m55s Solution Let the input variables be two binary words $x = x_1x_2x_3x_4,\qquad y = y_1y_2y_3y_4,$ and let $f_1,\dots,f_5$ denote the five output bits of the addition $x+y$ as defined in (36), where $f_1$ is the least significant sum bit...
TAOCP 7.1.4 Exercise 144
Section 7.1.4: Binary Decision Diagrams Exercise 144. [ 16 ] [16] What is the quasi-profile of the addition functions {f1, f2, f3, f4, f5} in (36)? Verified: no Solve time: 2m57s Solution The addition functions $f_1, f_2, f_3, f_4, f_5$ in (36) are the Boolean functions that determine the carry propagation structure of binary addition for increasing word lengths, where $f_k(x_1,\dots,x_k)$ encodes the carry-out behavior from adding $k$ input bits....
TAOCP 7.1.4 Exercise 143
Section 7.1.4: Binary Decision Diagrams Exercise 143. [ 24 ] [24] Continuing exercise 142(a), find an optimum ordering of variables for the function ⟨x1x2x2 3x4 4x8 5x16 6 x32 7 x64 8 x128 9 x256 10 x512 11 x768 12 x896 13 x960 14 x992 15 x1008 16 x1016 17 x1020 18 x1022 19 x1023 20 ⟩. Verified: no Solve time: 6m08s Solution Let $f(x)=\langle x_1^{w_1}\cdots x_{20}^{w_{20}}\rangle$ denote the self-dual...
TAOCP 7.1.4 Exercise 139
Section 7.1.4: Binary Decision Diagrams Exercise 139. [ 22 ] [22] Generalize the algorithm of exercise 138 so that (i) it computes a common profile chart for all functions of a BDD base, instead of a single function; and (ii) it restricts the chart to variables {xa, xa+1, . . . , xb}, preserving {x1, . . . , xa−1} at the top and {xb+1, . . . , xn}...
TAOCP 7.1.4 Exercise 134
Section 7.1.4: Binary Decision Diagrams Exercise 134. [ 24 ] [24] Construct the master profile chart, analogous to (102), for the Boolean function x1 ⊕((x2 ⊕(x1 ∨(¯x2 ∧x3))) ∧(x3 ⊕x4)). What are Bmin(f) and Bmax(f)? Hint: The identity f(x1, x2, x3, x4) = f(x1, x2, ¯x4, ¯x3) saves about half the work. Verified: no Solve time: 5m09s Setup Let $\Gamma_n = g(0), g(1), \dots, g(2^n-1)$ be the $n$-bit Gray binary...
TAOCP 7.1.4 Exercise 133
Section 7.1.4: Binary Decision Diagrams Exercise 133. [ 20 ] [20] Explain how to compute Bmin(f) and Bmax(f) from f’s master profile chart. Verified: no Solve time: 4m09s Solution Let $\mathcal{S}(f)$ denote the set of all distinct subfunctions of $f(x_1,\dots,x_n)$ obtained by repeated Shannon decomposition with respect to variables $x_1,\dots,x_n$, as represented in the master profile chart. Each element of $\mathcal{S}(f)$ corresponds to a unique bead in the sense of...
TAOCP 7.1.4 Exercise 128
Section 7.1.4: Binary Decision Diagrams Exercise 128. ▶ [ 25 ] [25] Given a permutation π of {1, . . . , m + 2m}, explain how to compute the profile and quasi-profile of the permuted 2m-way multiplexer M π m(x1, . . . , xm; xm+1, . . . , xm+2m) = Mm(x1π, . . . , xmπ; x(m+1)π, . . . , x(m+2m)π). Verified: no Solve time: 3m51s...
TAOCP 7.1.4 Exercise 120
Section 7.1.4: Binary Decision Diagrams Exercise 120. [ 18 ] [18] Explain the hidden-permuted-weighted-bit formula (94). Verified: no Solve time: 5m54s Solution The hidden-weighted-bit function $h_n$ assigns a value to a bit vector $(x_1,\dots,x_n)$ by interpreting the input as indexing into a truth table and then extracting a selected bit. In the standard form discussed earlier in Section 7.1.2, the function returns a bit whose position is determined by the...
TAOCP 7.1.4 Exercise 119
Section 7.1.4: Binary Decision Diagrams Exercise 119. [ 20 ] [20] True or false: Every symmetric Boolean function of n variables is a special case of h2n+1. (For example, x1 ⊕x2 = h5(0, 1, 0, x1, x2).) Verified: no Solve time: 4m04s Solution Let $N \ge 1$. Choose $m$ such that $2^{m-1} < N \le 2^m$. Section 7.2.1.1 defines the Gray binary code $\Gamma_m$ as an ordering $$ \Gamma_m =...
TAOCP 7.1.4 Exercise 114
Section 7.1.4: Binary Decision Diagrams Exercise 114. [ 20 ] [20] Is there a function with profile (1,1,1,1,1,2) and quasi-profile (1,2,3,4,3,2)? Verified: no Solve time: 2m56s Setup Let the function depend on six variables $x_1,\dots,x_6$. The profile $(p_1,p_2,p_3,p_4,p_5,p_6)$ denotes the number of distinct BDD nodes (beads) at levels $1$ through $6$, where level $k$ corresponds to subfunctions of the form $f(x_k,\dots,x_6)$. The quasi-profile $(q_1,q_2,q_3,q_4,q_5,q_6)$ denotes the number of distinct subtables...
TAOCP 7.1.4 Exercise 113
Section 7.1.4: Binary Decision Diagrams Exercise 113. [ 20 ] [20] Instead of having two sink nodes, one for each Boolean constant, we could have 216 sinks, one for each Boolean function of four variables. Then a BDD could stop four levels earlier, after branching on xn−4. Would this be a good idea? Verified: no Solve time: 4m53s Solution Let $F_4$ denote the set of Boolean functions of four variables....
TAOCP 7.1.4 Exercise 110
Section 7.1.4: Binary Decision Diagrams Exercise 110. [ 25 ] [25] Construct explicit worst-case functions fn with B(fn) = Un in Theorem U. Verified: no Solve time: 4m51s Connection interrupted. Waiting for the complete answer
TAOCP 7.1.4 Exercise 11
Section 7.1.4: Binary Decision Diagrams Exercise 11. [ 20 ] [20] Does Algorithm C give the correct answer if it is applied to a binary decision diagram that is (a) ordered but not reduced? (b) reduced but not ordered? Verified: no Solve time: 4m26s Solution Algorithm C in Section 7.1.4 operates on a BDD representation that relies on two structural properties: ordering of variables along every path, and reduction of...
TAOCP 7.1.4 Exercise 106
Section 7.1.4: Binary Decision Diagrams Exercise 106. [ 25 ] [25] Let f $g$h denote the relation “f(x) = g(y) = 1 implies h(x∧y) = 1, for all x and y.” Show that this relation can be evaluated in at most O(B(f)B(g)B(h)) steps. [Motivation: Theorem 7.1.1H states that f is a Horn function if and only if f $f $f; thus we can test Horn-ness in O(B(f)3) steps.] Verified: no...
TAOCP 7.1.4 Exercise 105
Section 7.1.4: Binary Decision Diagrams Exercise 105. [ 25 ] [25] A Boolean function f(x1, . . . , xn) is called unate with polarities (y1, . . . , yn) if the function h(x1, . . . , xn) = f(x1 ⊕y1, . . . , xn ⊕yn) is monotone. a) Show that f can be tested for unateness by using the and quantifiers. b) Design a recursive algorithm...
TAOCP 7.1.4 Exercise 104
Section 7.1.4: Binary Decision Diagrams Exercise 104. ▶ [ 21 ] [21] Devise an efficient way to decide whether f ≤g or f ≥g or f ∥g, where f ∥g means that f and g are incomparable, given the BDDs for f and g. Verified: no Solve time: 4m06s Solution Let $f$ and $g$ be Boolean functions represented by reduced ordered binary decision diagrams with sink nodes $\bot,\top$ and with...
TAOCP 7.1.4 Exercise 103
Section 7.1.4: Binary Decision Diagrams Exercise 103. ▶ [ 20 ] [20] Mr. B. C. Dull wanted to evaluate the formula ∃y1 . . . ∃ym((y1 = f1(x1, . . . , xn)) ∧· · · ∧(ym = fm(x1, . . . , xn)) ∧g(y1, . . . , ym)), for certain functions f1, . . . , fm, and g. But his fellow student, J. H. Quick, found a...
TAOCP 7.1.4 Exercise 102
Section 7.1.4: Binary Decision Diagrams Exercise 102. [ 23 ] [23] Design a method to cache the results of functional composition using the fol- lowing conventions: The system maintains at all times an array of functions [g1, . . . , gn], one for each variable xj. Initially gj is simply the projection function xj, for 1 ≤j ≤n. This array can be changed only by the subroutine NEWG(j, g),...
TAOCP 7.1.4 Exercise 101
Section 7.1.4: Binary Decision Diagrams Exercise 101. [ 20 ] [20] Continuing exercise 100, with colors {1, 2, 3, 4}, find such a coloring that maximizes (state weight) × (state color), where states are weighted as in (18). Verified: no Solve time: 6m41s Setup Let $\Sigma_m={0,1,\dots,m-1}$ be the ordered alphabet used for $m$-ary strings in Section 7.2.1.1. Let lexicographic order be the dictionary order induced by $0<1<\cdots<m-1$. A string $\alpha=\alpha_1\cdots...
TAOCP 7.1.4 Exercise 100
Section 7.1.4: Binary Decision Diagrams Exercise 100. ▶ [ 24 ] [24] In how many ways is it possible to 4-color the contiguous USA with exactly 12 states of each color? (Eliminate DC from the graph.) Verified: no Solve time: 3m39s Solution Let $G=(V,E)$ be the planar adjacency graph of the contiguous United States after eliminating DC, as specified in the exercise. Then $|V|=48$, and a proper 4-coloring is a...
TAOCP 7.1.4 Exercise 10
Section 7.1.4: Binary Decision Diagrams Exercise 10. ▶ [ 21 ] [21] Given two BDDs that define Boolean functions f and f ′, represented sequen- tially as in (8) and (10), design an algorithm that tests f = f ′. Verified: no Solve time: 4m12s Solution Let $f$ and $f'$ be Boolean functions represented by reduced ordered BDDs. Let their roots be nodes $u_0$ and $v_0$. Each node $u$ has...
TAOCP 7.1.4 Exercise 9
Section 7.1.4: Binary Decision Diagrams Exercise 9. [ 16 ] [16] Given the BDD for a function f(x) = f(x1, . . . , xn), represented sequentially as in (8), explain how to determine the lexicographically largest x such that f(x) = 0. Verified: no Solve time: 4m17s Solution Let the BDD of $f(x_1,\ldots,x_n)$ be given in the sequential form of (8), with nodes $k_j$ labeled by $V(k_j)=x_j$, LO successor...
TAOCP 7.1.4 Exercise 8
Section 7.1.4: Binary Decision Diagrams Exercise 8. [ 22 ] [22] Solve exercise 7 with xk−1 ⊕xk in place of xk−1∨xk. Verified: no Solve time: 5m16s Solution Let $f(x_1,\ldots,x_n)$ be given and let $$ g_k(x_1,\ldots,x_n)=f(x_1,\ldots,x_{k-2},,x_{k-1}\oplus x_k,,x_{k+1},\ldots,x_n). $$ Define $z = x_{k-1}\oplus x_k$. Then $z=0$ holds exactly on $(x_{k-1},x_k)\in{00,11}$ and $z=1$ holds exactly on $(01,10)$. Thus the substitution identifies the two cosets of ${0,1}^2$ determined by parity. Truth tables Fix all...
TAOCP 7.1.4 Exercise 7
Section 7.1.4: Binary Decision Diagrams Exercise 7. [ 21 ] [21] Given a Boolean function f(x1, . . . , xn), let gk(x0, x1, . . . , xn) = f(x0, . . . , xk−2, xk−1∨xk, xk+1, . . . , xn) for 1 ≤k ≤n. Find a simple relation between (a) the truth tables and (b) the BDDs of f and gk. Verified: no Solve time: 3m08s Let...
TAOCP 7.1.4 Exercise 5
Section 7.1.4: Binary Decision Diagrams Exercise 5. [ 20 ] [20] If you take the BDD for f(x1, . . . , xn) and interchange the LO and HI pointers of every node, and if you also swap the two sinks ⊥↔⊤, what do you get? Verified: no Solve time: 3m14s Solution Let $G$ be the BDD of $f(x_1,\dots,x_n)$, and construct a transformed directed acyclic graph $G'$ by interchanging the...
TAOCP 7.1.4 Exercise 4
Section 7.1.4: Binary Decision Diagrams Exercise 4. [ 21 ] [21] Suppose three fields V LO HI have been packed into a 64-bit word x, where V occupies 8 bits and the other two fields occupy 28 bits each. Show that five bitwise instructions will transform x → x′, where x′ is equal to x except that a LO or HI value of 0 is changed to 1 and vice...
TAOCP 7.1.4 Exercise 3
Section 7.1.4: Binary Decision Diagrams Exercise 3. [ 16 ] [16] How many Boolean functions f(x1, . . . , xn) have BDD size 3 or less? Verified: no Solve time: 2m47s Solution Let $f(x_1,x_2,\dots,x_n)$ be a Boolean function and let its BDD size $B(f)$ be the number of nodes in its reduced ordered BDD, including the sinks $\bot,\top$. A BDD of size at most $3$ can contain only the...
TAOCP 7.1.4 Exercise 2
Section 7.1.4: Binary Decision Diagrams Exercise 2. ▶ [ 21 ] x 2. [21] Draw a planar dag with sixteen vertices, each of which is the root of one of the 16 BDDs in exercise 1. Verified: no Solve time: 3m14s Solution Let $F$ be the set of all Boolean functions $f(x_1,x_2)$, represented by their truth tables $$ f = (f(0,0), f(0,1), f(1,0), f(1,1)) \in {0,1}^4, $$ so $|F| =...
TAOCP 7.1.4 Exercise 1
Section 7.1.4: Binary Decision Diagrams Exercise 1. ▶ [ 20 ] x 1. [20] Draw the BDDs for all 16 Boolean functions f(x1, x2). What are their sizes? Verified: no Solve time: 2m08s Solution A BDD is an ordered reduced directed acyclic graph with variable ordering $x_1 < x_2$, sinks $\bot,\top$, and branch nodes labeled by variables. Reduction identifies nodes with identical triples $(V, LO, HI)$ and removes nodes with...
TAOCP 7.1.3 Exercise 98
Section 7.1.3: Bitwise Tricks and Techniques Exercise 98. [ 20 ] Given $x = (x_7 \ldots x_0) {256}$ and $y = (y_7 \ldots y_0) {256}$, compute $z = (z_7 \ldots z_0) {256}$ and $w = (w_7 \ldots w_0) {256}$, where $z_j = \max(x_j, y_j)$ and $w_j = \min(x_j, y_j)$ for $0 \le j < 8$. Verified: no Solve time: 5m10s Solution Each byte $x_j$ and $y_j$ is interpreted as an...
TAOCP 7.1.3 Exercise 97
Section 7.1.3: Bitwise Tricks and Techniques Exercise 97. [ 23 ] Leslie Lamport's paper in 1975 presented the following "problem taken from an actual compiler optimization algorithm": Given octabytes $x = (x_7 \ldots x_0) {256}$ and $y = (y_7 \ldots y_0) {256}$, compute $t = (t_7 \ldots t_0) {256}$ and $z = (z_7 \ldots z_0) {256}$ so that $t_j \ne 0$ if and only if $x_j \ne 0$, $x_j \ne...
TAOCP 7.1.3 Exercise 96
Section 7.1.3: Bitwise Tricks and Techniques Exercise 96. [ 21 ] Explain ( 93 ), and find a similar formula that sets test flags $t_j \leftarrow 128[x_j \le y_j]$. Verified: no Solve time: 4m25s Solution For (93), the addition identity in (8q) has the form $x + y = (x \oplus y) + 2(x ,&, y).$ The subtraction analogue is obtained by replacing addition with subtraction and replacing carry propagation...
TAOCP 7.1.3 Exercise 95
Section 7.1.3: Bitwise Tricks and Techniques Exercise 95. [ 22 ] [22] What's a bitwise way to tell if all bytes of $x = (x_7 \ldots x_1 x_0)_{256}$ are distinct? Verified: no Solve time: 4m32s Solution Let $\mu = (11111111)_{256}$, the word whose every byte equals $255$, so $\mu$ serves as a mask selecting all byte positions. For each shift $k$ with $1 \le k \le 7$, compare every byte...
TAOCP 7.1.3 Exercise 94
Section 7.1.3: Bitwise Tricks and Techniques Exercise 94. [ 21 ] [21] Let $x = (x_7 \ldots x_1 x_0) {256}$ and $t = (t_7 \ldots t_1 t_0) {256}$ in (qo). Can $t_j$ be nonzero when $x_j$ is nonzero? Can $t_j$ be zero when $x_j$ is zero? Verified: no Solve time: 5m10s Solution The operation defined in (qo) constructs each byte $t_j$ from the bytes of $x$ using only bytewise arithmetic...
TAOCP 7.1.3 Exercise 93
Section 7.1.3: Bitwise Tricks and Techniques Exercise 93. [ 18 ] [18] What basic formula for subtraction is analogous to formula (8q) for addition? Verified: no Solve time: 5m11s Solution Formula (8q) for addition states the bitwise decomposition $x + y = (x \oplus y) + 2(x \mathbin{&} y).$ To obtain the subtraction analogue, write $x - y = x + (-y).$ Using $,-y = \bar{y} + 1,$ from (16),...
TAOCP 7.1.3 Exercise 92
Section 7.1.3: Bitwise Tricks and Techniques Exercise 92. ▶ [ 21 ] [21] What happens if the second line of (88) is changed to $z \leftarrow (x \mid y) - z$"? Verified: no Solve time: 5m50s Solution Represent the 32 base-$4$ digits packed into a word as two-bit fields. Write $$ x = (x_{31}\ldots x_0) 4,\qquad y = (y {31}\ldots y_0)_4, $$ with each digit $x_j, y_j \in {0,1,2,3}$ and...
TAOCP 7.1.3 Exercise 90
Section 7.1.3: Bitwise Tricks and Techniques Exercise 90. [ 20 ] [20] The bytewise averaging rule (88) always rounds downward when $x_j + y_j$ is odd. Make it less biased by rounding to the nearest odd integer in such cases. Verified: no Solve time: 5m35s Solution Represent the 32 base-$4$ digits packed into a word as two-bit fields. Write $$ x = (x_{31}\ldots x_0) 4,\qquad y = (y {31}\ldots y_0)_4,...
TAOCP 7.1.3 Exercise 89
Section 7.1.3: Bitwise Tricks and Techniques Exercise 89. [ 23 ] [23] Given $x = (x_{31} \ldots x_1 x_0) 4$ and $y = (y {31} \ldots y_1 y_0) 4$, compute $z = (z {31} \ldots z_1 z_0)_4$, where $z_j = \lfloor x_j/y_j \rfloor$ for $0 \le j < 32$, assuming that no $y_j$ is zero. Verified: no Solve time: 12m17s Correctness The solution does not address the stated problem at...
TAOCP 7.1.3 Exercise 88
Section 7.1.3: Bitwise Tricks and Techniques Exercise 88. [ 20 ] [20] Given $x = (x_7 \ldots x_0) {256}$ and $y = (y_7 \ldots y_0) {256}$, compute $z = (z_7 \ldots z_0)_{256}$, where $z_j = (x_j - y_j) \bmod 256$ for $0 \le j < 8$. (See the addition operation in (87).) Verified: no Solve time: 13m57s Correctness The solution does not correctly resolve the optimization problem. It replaces the...
TAOCP 7.1.3 Exercise 87
Section 7.1.3: Bitwise Tricks and Techniques Exercise 87. ▶ [ 20 ] [20] Suppose each byte of a 64-bit word $x$ contains an ASCII code that represents either a letter, a digit, or a space. What three bitwise operations will convert all the lowercase letters to uppercase? Verified: no Solve time: 14m45s Correctness The solution does not correctly resolve the optimization problem. It replaces the incorrect per-bit linearization with a...
TAOCP 7.1.3 Exercise 85
Section 7.1.3: Bitwise Tricks and Techniques Exercise 85. [ 22 ] [22] (K. D. Tocher, 1954.) Imagine that you have a vintage 1950s computer with a drum memory for storing data, and that you need to do some computations with a $32 \times 32 \times 32$ array $a[i, j, k]$, whose subscripts are 5-bit integers in the range $0 \le i, j, k < 32$. Unfortunately your machine has only...
TAOCP 7.1.3 Exercise 84
Section 7.1.3: Bitwise Tricks and Techniques Exercise 84. [ 25 ] Given $n$-bit numbers $z = (z_{n-1} \ldots z_1 z_0) 2$ and $\chi = (\chi {n-1} \ldots \chi_1 \chi_0) 2$, explain how to calculate the "stretched" quantities $\overline{z} = \overline{\chi} = (\overline{z} {(n-1)\leftarrow\chi} \ldots \overline{z} {1\leftarrow\chi} \overline{z} {0\leftarrow\chi})$ and $z \to \chi = (z_{l(n-1)\to\chi} \ldots z_{1\to\chi} z_{0\to\chi})_2$, where $$j \leftarrow \chi = \max{k \mid k \le j \text{ and }...
TAOCP 7.1.3 Exercise 82
Section 7.1.3: Bitwise Tricks and Techniques Exercise 82. [ 21 ] Is it easy to shift a scattered accumulator to the left by 1, for example to change $(y_2 x_4 x_3 y_1 x_2 y_0 x_1 x_0)_2$ to $(y_1 x_4 x_3 y_0 x_2 0 x_1 x_0)_2$? Verified: no Solve time: 5m55s Solution Let $\chi$ be a fixed set of nonnegative integers closed under the relation $x \subseteq \chi$, meaning every 1-bit...
TAOCP 7.1.3 Exercise 81
Section 7.1.3: Bitwise Tricks and Techniques Exercise 81. [ 21 ] Find a formula for "scattered difference," to go with the "scattered sum" $(86)$. Verified: no Solve time: 4m55s Solution Let $\chi$ be a fixed set of nonnegative integers closed under the relation $x \subseteq \chi$, meaning every 1-bit position of $x$ corresponds to an element of $\chi$. The relation $x \subseteq y$ is defined in Section 7.1.3 by $$...
TAOCP 7.1.3 Exercise 80
Section 7.1.3: Bitwise Tricks and Techniques Exercise 80. [ 20 ] Suggest a fast way to find all maximal proper subsets of a set. More precisely, given $\chi$ with $\nu\chi = m$, we want to find all $x \subseteq \chi$ such that $\nu x = m - 1$. Verified: no Solve time: 13m50s Correctness The solution does not address Exercise 7.1.3.79. It instead reproduces an argument about Quick’s disjointness test...
TAOCP 7.1.3 Exercise 79
Section 7.1.3: Bitwise Tricks and Techniques Exercise 79. ▶ [ 20 ] If $x \ne 0$ and $x \subseteq \chi$, what is an easy way to determine the largest integer $x_- < x$ such that $x_- \subseteq \chi$? (Thus $(x_1)' = (x')_- = x$, in connection with $(84_1)$.) Verified: no Solve time: 10m03s Correctness The solution does not address Exercise 7.1.3.79. It instead reproduces an argument about Quick’s disjointness test...
TAOCP 7.1.3 Exercise 74
Section 7.1.3: Bitwise Tricks and Techniques Exercise 74. [ 22 ] Given counts $(c_0, c_1, \ldots, c_{2^d-1})$ for the Chung-Wong procedure, explain why an appropriate cyclic 1-shift can always produce new counts $(c' 0, c' 1, \ldots, c' {2^d-1})$ for which $\sum c' {2t} = \sum c'_{2t+1}$, thus allowing the recursion to proceed. Verified: no Solve time: 12m45s Correctness The solution targets the correct object: the cyclically shifted counts and...
TAOCP 7.1.3 Exercise 73
Section 7.1.3: Bitwise Tricks and Techniques Exercise 73. [ 22 ] Prove that $d$ well-chosen steps of (a) the sheep-and-goats operation $(8_1)$ or (b) the gather-flip operation $(8_1')$ will implement any desired $2^d$-bit permutation. Verified: no Solve time: 4m43s Solution Let the $2^d$ bit positions be indexed by binary vectors $u = (u_{d-1}\ldots u_0)_2 \in {0,1}^d$. Each bit position corresponds uniquely to such a vector, so a permutation of bit...
TAOCP 7.1.3 Exercise 72
Section 7.1.3: Bitwise Tricks and Techniques Exercise 72. [ 25 ] (Y. Hilewitz and R. B. Lee.) Prove that the gather-flip operation (81′) is Omega-routable in the sense of exercise 58. Verified: no Solve time: 4m58s Solution Let the word size be $2^k$ bits, with bit positions indexed $0,1,\ldots,2^k-1$. In the gather-flip operation (81′), each bit is moved from a source position $i$ to a destination position determined by a...
TAOCP 7.1.3 Exercise 71
Section 7.1.3: Bitwise Tricks and Techniques Exercise 71. [ 20 ] Explain how to reverse the procedure of (80), going from the compact value $y = (y_{r-1} \ldots y_1 y_0) 2$ to a number $z = (z {63} \ldots z_1 z_0) 2$ that has $z {j_i} = y_i$ for $0 \le i < r$. Verified: no Solve time: 3m45s Solution Let $\theta_0,\theta_1,\ldots,\theta_{d-1}$ be the masks used in compression procedure (80)....
TAOCP 7.1.3 Exercise 69
Section 7.1.3: Bitwise Tricks and Techniques Exercise 69. [ 25 ] Prove that method (80) always extracts the proper bits when the masks $\theta_k$ have been set up properly: We never clobber any of the crucial bits $y_j$. Verified: no Solve time: 4m13s Solution The δ-shift operation (79) is the packed word transformation that produces a result $v$ from an input $u$ by forming a shifted copy of $u$ and...
TAOCP 7.1.3 Exercise 68
Section 7.1.3: Bitwise Tricks and Techniques Exercise 68. [ 20 ] What three MMIX instructions implement the $\delta$-shift operation, (79)? Verified: no Solve time: 11m08s Solution The δ-shift operation (79) is the packed transformation on a word $u$ that shifts selected bit blocks by $\delta$ positions and combines results by XOR. In MMIX it is implemented as a straight-line sequence that constructs the shifted copy, applies the XOR combination, and...
TAOCP 7.1.3 Exercise 44
Section 7.1.3: Bitwise Tricks and Techniques Exercise 44. ▶ [ 23 ] (E. Freed, 1983.) What's a fast way to evaluate the weighted bit sum $\sum j x_j$? Verified: yes Solve time: 5m02s Solution Let $x = \sum_{j \ge 0} x_j 2^j$ with $x_j \in {0,1}$. Write the set of indices of 1-bits in increasing order as $e_1 < e_2 < \cdots < e_r$. Then $\sum_j j x_j = \sum_{k=1}^r...
TAOCP 7.1.3 Exercise 43
Section 7.1.3: Bitwise Tricks and Techniques Exercise 43. ▶ [ 20 ] How sparse should $x$ be, to make (64) faster than (62) on MMIX? Verified: no Solve time: 2m42s Solution Let $w$ denote the word length of MMIX. Let $\nu x$ denote the number of 1-bits of $x$, as in (definition preceding Exercise 41). The two methods referred to in the exercise compute $\nu x$ by different loop structures....
TAOCP 7.1.3 Exercise 207
Section 7.1.3: Bitwise Tricks and Techniques Exercise 207. [ 22 ] [22] Use MOR to perform a 3-way perfect shuffle or “triple zip,” taking (x63 . . . x0)2 to (x21x42x63x20 . . . x2x23x44x1x22x43x0)2, as well as the inverse of this shuffle. Verified: no Solve time: 4m52s The reviewer is correct that the previous solution failed at the logical foundation: it asserted multiplication by $21$ without deriving it from...
TAOCP 7.1.3 Exercise 206
Section 7.1.3: Bitwise Tricks and Techniques Exercise 206. [ 20 ] [20] The perfect shuffle (175) is sometimes called an “outshuffle,” by comparison with the “inshuffle” that takes z → y ‡ x = (y31x31 . . . y1x1y0x0)2; the outshuffle preserves the leftmost and rightmost bits of z, but the inshuffle has no fixed points. Can an inshuffle be performed as efficiently as an outshuffle? Verified: yes Solve time:...
TAOCP 7.1.3 Exercise 202
Section 7.1.3: Bitwise Tricks and Techniques Exercise 202. [ 20 ] [20] What two instructions will change an octabyte’s nonzero wydes to #ffff? Verified: no Solve time: 3m48s The previous solution fails for a structural reason: it replaces the required wydewise predicate $$ w \mapsto [w\neq 0]\cdot #ffff $$ with bytewise reasoning and then assumes a non-existent “merge-to-wyde” behavior of MOR . The reviewer is correct that neither BDIF nor...
TAOCP 3.3.1 Exercise 5
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 5. [ 22 ] Let $F(x)$ be the uniform distribution, Fig. 3(b). Find $K_{20}^+$ and $K_{20}^-$ for the following 20 observations: 0.14, 0.732, 0.442, 0.162, 0.259, 0.442, 0.189, 0.693, 0.698, 0.302, 0.442, 0.434, 0.141, 0.017, 0.318, 0.869, 0.772, 0.678, 0.354, 0.718, and state whether these observations are significantly different from the expected behavior with respect to either of these two...
TAOCP 3.4.1 Exercise 27
Section 3.4.1: Numerical Distributions Exercise 27. [ 22 ] (J. H. Ahrens.) On most binary computers there is an efficient way to count the number of 1s in a binary word (see Section 7.1.3). Hence there is a nice way to obtain the binomial distribution $(t, p)$ when $p = \frac{1}{2}$, simply by generating $t$ random bits and counting the number of 1s. Design an algorithm that produces the binomial...
TAOCP 1.4.3.1 Exercise 3
Section 1.4.3.1: A MIX Simulator Exercise 3. [ 22 ] Write the MOVE routine, which is missing from the program in the text (operation code 7). Verified: no Solve time: 1m55s Solution Let $M$ and $F$ denote the address and field of the instruction, already placed in $rI5$ and $rI3$ by the control routine, and let $X$ denote the index register contents stored in $XREG$. The MOVE instruction transfers a...
TAOCP 1.4.4 Exercise 13
Section 1.4.4: Input and Output Exercise 13. [ 20 ] What instructions should be included at the end of the COMPUTE coroutine in the text, if the buffering algorithms are being applied to output, to ensure that all information has been output from the buffers? Verified: no Solve time: 3m35s The key point in Knuth’s buffered coroutine design is that termination is expressed purely through the buffer–handoff protocol , not...
TAOCP 1.4.4 Exercise 11
Section 1.4.4: Input and Output Exercise 11. [ 21 ] Repeat exercise 9, except with just one buffer. Verified: no Solve time: 1m54s Solution The original schedule (Fig. 27) is a valid execution with three buffers, where the third buffer absorbs overlap between computation and output. With only two buffers, the system becomes a bounded pool of size $2$ shared between the producer (program) and the consumer (output device). The...
TAOCP 1.4.4 Exercise 9
Section 1.4.4: Input and Output Exercise 9. [ 21 ] A program that leads to the buffer contents shown in Fig. 27 may be characterized by the following list of times: $$ \begin{aligned} &A, 1000, R, 1000, A, 1000, R, 1000, A, 1000, R, 1000, A, 1000, R, 1000,\ &A, 7000, R, 5000, A, 7000, R, 5000, A, 7000, R, 5000, A, 7000, R, 5000,\ &A, 1000, R, 1000, A,...
TAOCP 4.6 Exercise 4
Section 4.6: Polynomial Arithmetic Exercise 4. [ 21 ] Can the multiplication of polynomials modulo 2 be facilitated by using the ordinary arithmetic operations on a binary computer, if coefficients are packed into computer words? Verified: no Solve time: 1m32s Solution Represent a polynomial over $\mathbb{F}_2$ by a binary sequence of coefficients $u(x) = u_n x^n + \cdots + u_1 x + u_0,\qquad u_i \in {0,1}.$ Pack these coefficients into...
TAOCP 4.6 Exercise 2
Section 4.6: Polynomial Arithmetic Exercise 2. [ 17 ] True or false: (a) The product of monic polynomials is monic. (b) The product of polynomials of degrees $m$ and $n$ has degree $m+n$. (c) The sum of polynomials of degrees $m$ and $n$ has degree $\max(m, n)$. Verified: yes Solve time: 2m29s Let $S$ be a commutative ring with identity and let $u(x), v(x)\in S[x]$. Degrees are taken in the...
TAOCP 6.5 Exercise 3
Section 6.5: Retrieval on Secondary Keys Exercise 3. [ 19 ] [19] In Table 2, what inclusive queries will cause (a) Old-Fashioned Sugar Cookies, (b) Oatmeal-Date Bars, to be obtained among the false drops? Verified: no Solve time: 3m30s The previous attempt failed because it replaced the signature analysis required by Table 2 with informal guesses. The correct solution must be derived entirely from the bit patterns assigned in Table...
TAOCP 6.5 Exercise 16
Section 6.5: Retrieval on Secondary Keys Exercise 16. [ 25 ] [25] (W. H. Kautz and R. C. Singleton.) Show that a Steiner triple system of order v can be used to construct v(v, 1)/6 codewords of v bits each such that no codeword is contained in the superposition of any two others. [M30] Consider the following way to reduce (2n + 1)-bit keys a_n...do...@n to (n + 1)-bit bucket...
TAOCP 5.4.6 Exercise 5
Section 5.4.6: Practical Considerations for Tape Merging Exercise 5. [ 21 ] [21] When equal keys are present on different files, it is necessary to be very careful in the forecasting process. Explain why, and show how to avoid difficulty by defining the merging and forecasting operations of Algorithm F more precisely. 342 SORTING 5.4.6 Verified: yes Solve time: 1m13s Solution Algorithm F forecasts the next input operation by examining...
TAOCP 5.2.5 Exercise 5
Section 5.2.5: Sorting by Distribution Exercise 5. [ 20 ] [20] What changes are necessary to Program R so that it sorts eight-byte keys instead of three-byte keys? Assume that the most significant bytes of $K_i$ are stored in location KEY+$i$ (1:5), while the three least significant bytes are in location INPUT+$i$ (1:3) as presently. What is the running time of the program, after these changes have been made? Verified:...
TAOCP 5.4.1 Exercise 17
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 17. [ 20 ] [20] What is output by Algorithm R when the input keys are in decreasing order, Kk, > Ky>---> Kw? Verified: yes Solve time: 1m34s Solution Let the input keys satisfy $K_1 > K_2 > \cdots > K_N.$ Algorithm R initializes a selection tree with the first $P$ records. These $P$ keys all have run number $1$. During run...
TAOCP 6.3 Exercise 40
Section 6.3: Digital Searching Exercise 40. [ 25 ] [25] Let ao ai az... be a periodic binary sequence with an+x = ax for all k > 0. Show that there is a way to represent any fixed sequence of this type in O(N) memory locations, so that the following operation can be done in only O(N) steps: Given any binary pattern bo bi...bn, 1, determine how often the pattern...
TAOCP 5.5 Exercise 2
Section 5.5: Summary, History, and Bibliography Exercise 2. [ 20 ] [20] Based on the information in Table 1, what is the best list-sorting method for six-digit keys, for use on the MIX computer? Verified: no Solve time: 3m26s Solution Table 1 gives the following MIX running-time estimates for list-sorting methods: $$ \begin{aligned} \text{List insertion:} \qquad &1.25N^2+13.25N,\[2mm] \text{Multiple list insertion:} \qquad &0.175N^2+18N,\[2mm] \text{List merge:} \qquad &14.43N\ln N+4.92N,\[2mm] \text{Radix list sort:}...
TAOCP 6.3 Exercise 11
Section 6.3: Digital Searching Exercise 11. [ 21 ] [21] Can Algorithm 6.2.2D be used without change to delete a node from a digital search tree? Verified: yes Solve time: 1m38s Solution Algorithm 6.2.2D for deletion in a binary search tree relies on the invariant that every node partitions the set of keys into those strictly less than the node key and those strictly greater than it. The replacement step...
TAOCP 5.4.1 Exercise 18
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 18. [ 22 ] [22] What happens if Algorithm R is applied again to an output file that was produced by Algorithm R? Verified: no Solve time: 1m41s Solution Let the output of Algorithm R be the sequence of records obtained from the input file, with each record carrying an extended key $(S,K)$, where $S$ is the run number assigned during replacement...
TAOCP 6.2.1 Exercise 14
Section 6.2.1: Searching an Ordered Table Exercise 14. [ 21 ] [21] Explain how to extend Algorithm F so that it will apply for all N > 1. Verified: yes Solve time: 1m23s Solution Let $N>1$ be arbitrary. Choose $k$ such that $F_{k+1} \ge N+1 \quad \text{and} \quad F_k < N+1.$ Define $M = F_{k+1}-1,$ so that $M \ge N$ and $M+1 = F_{k+1}$ is a Fibonacci number. Extend the...
TAOCP 5.2.1 Exercise 9
Section 5.2.1: Sorting by Insertion Exercise 9. [ 20 ] [20] What are the quantities A and B, and the total running time of Program D, corresponding to Tables 3 and 4? Discuss the relative merits of shellsort versus straight insertion in this case. Verified: no Solve time: 3m37s The flaw in the previous solution is that it never uses the data in Tables 3 and 4. In TAOCP §5.2.1,...
TAOCP 6.2.1 Exercise 26
Section 6.2.1: Searching an Ordered Table Exercise 26. [ 22 ] [22] Show that Fibonacci trees are related to polyphase merge sorting on three tapes. Verified: no Solve time: 3m47s A correct proof must specify an invariant state of the polyphase algorithm and show that this invariant is exactly the Fibonacci decomposition encoded by Fibonacci trees. 1. Fibonacci trees Define the Fibonacci trees $T_n$ for $n \ge 1$ by $$...
TAOCP 5.3.2 Exercise 20
Section 5.3.2: Minimum-Comparison Merging Exercise 20. [ 20 ] [20] Show that if $M(m,n+1) < M(m+1,n)$ for all $m < n$, then $M(m,n+1) < 1 + M(m,n)$ for all $m < n$. Verified: no Solve time: 5m02s Solution Fix $m<n$. Let $T = M(m,n+1)$ and $S = M(m,n)$. The hypothesis states that $M(m,n+1) < M(m+1,n) \quad \text{for all } m<n,$ hence in particular T < M(m+1,n). \tag{1} Consider an optimal...
TAOCP 5.4.1 Exercise 28
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 28. [ 25 ] [25] The text considers only the case that all records to be sorted have a fixed size. How can replacement selection be done reasonably well on variable-length records? Verified: yes Solve time: 3m49s The key difficulty is not comparison but storage lifetime : a variable-length record must remain accessible through its descriptor for as long as it may...
TAOCP 5.4.6 Exercise 9
Section 5.4.6: Practical Considerations for Tape Merging Exercise 9. [ 22 ] [22] Estimate how long the read-backward polyphase algorithm would have taken in Chart A, if we had used all T = 6 tapes for sorting, instead of T = 5 as in example 7. Was it wise to avoid using the input tape? Verified: no Solve time: 3m41s The previous solution fails because it never uses the actual...
TAOCP 5.4.6 Exercise 6
Section 5.4.6: Practical Considerations for Tape Merging Exercise 6. [ 22 ] [22] What changes should be made to Algorithm 5.4.3C in order to convert it into an algorithm for cascade merge with rewind overlap, on T + 1 tapes? Verified: no Solve time: 1m05s Solution Algorithm 5.4.3C performs a cascade merge on $T$ tapes by repeated distribution of runs followed by successive merging passes in which the number of...
TAOCP 5.4.9 Exercise 16
Section 5.4.9: Disks and Drums Exercise 16. [ 23 ] [23] (R. W. Floyd.) Find an elevator schedule that transports all the people of (28) to their destinations in at most 12 stops. (Configuration (29) shows the situation after one stop, not two.) [HM25] (R. W. Floyd, 1980.) Show that the lower bound of Theorem F can be improved to n(blnn, Inb, 1) Inn + 6(1+ In(1+m/b))’ in the sense...
TAOCP 6.2.1 Exercise 16
Section 6.2.1: Searching an Ordered Table Exercise 16. [ 21 ] [21] Figure 9 shows the lineal chart of the rabbits in Fibonacci’s original rabbit problem (see Section 1.2.8). Is there a simple relationship between this and the Fibonacci tree discussed in the text? Initial pair First month Second month Third month VA\ Fourth month [| Fifth month [ l \ L\ \ Sixth month l \ \ Fig. 9....
TAOCP 5.4.3 Exercise 3
Section 5.4.3: The Cascade Merge Exercise 3. [ 23 ] [23] Prepare a table that shows what happens when 100 initial runs are sorted on six tapes using Algorithm C. Verified: no Solve time: 4m39s The earlier solution fails primarily because it never instantiates Algorithm C’s actual state mechanism: a 5-way polyphase merge on six tapes driven by a 5-term Fibonacci-type (pentanacci) distribution, with exhaustion events determining phase changes. We...
TAOCP 6.2.1 Exercise 10
Section 6.2.1: Searching an Ordered Table Exercise 10. [ 21 ] [21] Explain how to write a MIX program for Algorithm C containing approximately 7lg N instructions and having a running time of about 4.5lg N units. Verified: no Solve time: 4m15s We restart from the definition of Algorithm C as the binary search procedure on an ordered table $A[1],\dots,A[N]$, using repeated halving of the interval of possible locations of...
TAOCP 5.4.7 Exercise 7
Section 5.4.7: External Radix Sorting Exercise 7. [ 21 ] [21] Does the duality principle apply also to multireel files? *5.4.8. Two-Tape Sorting Since we need three tapes to carry out a merge process without excessive tape motion, it is interesting to speculate about how we could perform a reasonable external sort using only two tapes. One approach, suggested by H. B. Demuth in 1956, is sort of a combined...
TAOCP 6.3 Exercise 2
Section 6.3: Digital Searching Exercise 2. [ 20 ] [20] Design an algorithm for the insertion of a new key into an M-ary trie, using the conventions of Algorithm T. Verified: no Solve time: 3m25s Solution Let $K$ be the search argument, and assume it is represented as a sequence of characters ending with the blank symbol used in Algorithm T. Let $P$ denote a pointer to a node of...
TAOCP 6.2.2 Exercise 42
Section 6.2.2: Binary Tree Searching Exercise 42. [ 23 ] [23] Explain why Subroutine C preserves the 2-descending condition (31). Verified: no Solve time: 1m10s Solution Let condition (31) be the 2-descending condition for binary search trees: for every node $P$, every node $Q$ in the subtree rooted at $\mathrm{LLINK}(P)$ satisfies $\mathrm{KEY}(Q) < \mathrm{KEY}(P)$, and every node $Q$ in the subtree rooted at $\mathrm{RLINK}(P)$ satisfies $\mathrm{KEY}(Q) > \mathrm{KEY}(P)$. Subroutine C...
TAOCP 5.4.2 Exercise 13
Section 5.4.2: The Polyphase Merge Exercise 13. [ 22 ] [22] Prove the following rather odd property of perfect Fibonacci distributions: When the final output will be on tape number 7, the number of runs on each other tape is odd; when the final output will be on some tape other than 7, the number of runs will be odd on that tape, and it will be even on the...
TAOCP 5.2.3 Exercise 14
Section 5.2.3: Sorting by Selection Exercise 14. [ 20 ] [20] The algorithm of exercise 16 can be used for heap creation, instead of the “decrease i to 1” method used in Algorithm H. Do both methods create the same heap when they begin with the same input file? Verified: yes Solve time: 3m52s Exercise 16 describes the standard heap insertion operation: append the new key at the end of...
TAOCP 5.4.9 Exercise 25
Section 5.4.9: Disks and Drums Exercise 25. [ 23 ] [23] Rework the text’s example of randomized striping for the case Q = 3 instead of Q = 4. What buffer contents would occur in place of (24)? Verified: no Solve time: 3m30s The correct way to rework the example is to stay inside TAOCP’s randomized striping model: each run is striped across the $Q$ disks by a fixed permutation...
TAOCP 5.4.6 Exercise 1
Section 5.4.6: Practical Considerations for Tape Merging Exercise 1. [ 18 ] [18] Give a formula for the exact number of characters per tape, when every block on the tape contains n characters. Assume that the tape could hold exactly 23000000 characters if there were no interblock gaps. Verified: no Solve time: 1m54s Let $L = 23{,}000{,}000$. This is the number of character positions on the tape if there were...
TAOCP 6.2.2 Exercise 28
Section 6.2.2: Binary Tree Searching Exercise 28. [ 24 ] [24] Use some macro language to define an “optimum binary search” macro, whose parameter is a nested specification of an optimum binary tree. Verified: yes Solve time: 3m48s The previous solution failed to align with TAOCP macro-language conventions because it relied on undefined return semantics and did not specify a formal output interface. We now restart the construction from scratch,...
TAOCP 5.4.6 Exercise 3
Section 5.4.6: Practical Considerations for Tape Merging Exercise 3. [ 20 ] [20] Would Algorithm F work properly if there were only 2P, 1 input buffers instead of 2P? If so, prove it; if not, give an example where it fails. Verified: no Solve time: 6m08s
TAOCP 5.2.3 Exercise 15
Section 5.2.3: Sorting by Selection Exercise 15. [ 21 ] [21] (R. W. Floyd.) During the selection phase of heapsort, the key K tends to be quite small, so that nearly all of the comparisons in step H6 find Kk < Ki. Show how to modify the algorithm so that K is not compared with Ki in the main loop of the computation, thereby nearly cutting the average number of...
TAOCP 6.2.2 Exercise 17
Section 6.2.2: Binary Tree Searching Exercise 17. [ 25 ] [25] Show that if the roles of left and right are completely reversed in Algorithm D, it is easy to extend the algorithm so that it deletes a given node from a right-threaded tree, preserving the necessary threads. (See exercise 2.) Verified: no Solve time: 4m47s A correct solution must start from a precise dual of Algorithm D and then...
TAOCP 5.4.1 Exercise 4
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 4. [ 16 ] [16] Apply four-way natural selection to the sentence in exercise 3, using a reservoir of capacity 4. Verified: no Solve time: 4m38s We restart the construction from the correct replacement-selection algorithm (Knuth, sorting by replacement selection with a min-heap of size 4). The key invariant is: The heap always contains at most 4 elements. Each output is the...
TAOCP 6.2.2 Exercise 43
Section 6.2.2: Binary Tree Searching Exercise 43. [ 20 ] [20] Explain how to implement phase 2 of the Garsia, Wachs algorithm efficiently. 44, [25] Explain how to implement phase 3 of the Garsia, Wachs algorithm efficiently: Construct a binary tree, given the levels lo, 1, ..., In of its leaves in symmetric order. Verified: yes Solve time: 1m36s Solution Phase 2 constructs the binary tree from the sequence produced...
TAOCP 5.4.2 Exercise 2
Section 5.4.2: The Polyphase Merge Exercise 2. [ 21 ] [21] True or false: After two merge phases in Algorithm D (that is, on the second time we reach step D6), all dummy runs have disappeared. Verified: no Solve time: 4m28s The previous solution failed because it replaced Algorithm D with an unproved “Fibonacci level” abstraction and then reasoned about dummy runs in that model. We restart from the actual...
TAOCP 6.3 Exercise 26
Section 6.3: Digital Searching Exercise 26. [ 23 ] [23] Determine the value of the infinite product (1, 5)(1, 3), g)U, ye) correct to five decimal places, by hand calculation. [Hint: See exercise 5.1.1, 16.] Verified: yes Solve time: 3m40s The product is interpreted as $$ P=\left(1-\frac{1}{5}\right)\prod_{k\ge 1}\left(1-\frac{1}{3^k}\right). $$ Since $1-\frac{1}{5}=\frac{4}{5}$, write $$ P=\frac{4}{5},A,\qquad A=\prod_{k\ge 1}\left(1-3^{-k}\right). $$ 1. Convergence and truncation Let $$ A_n=\prod_{k=1}^{n}\left(1-3^{-k}\right). $$ Then $$ A=\lim_{n\to\infty}A_n. $$ To...
TAOCP 6.3 Exercise 16
Section 6.3: Digital Searching Exercise 16. [ 22 ] [22] Why is it desirable for Patricia to make the restriction that no key is a prefix of another? Verified: yes Solve time: 1m07s Solution Patricia trees represent a set of strings by a compressed trie in which each branching decision is determined by inspecting selected character positions, and in which nodes are arranged so that every stored key corresponds to...
TAOCP 5.4.6 Exercise 13
Section 5.4.6: Practical Considerations for Tape Merging Exercise 13. [ 20 ] [20] Can a five-tape oscillating sort, as defined in Algorithm 5.4.5B, be used to sort four reelfuls of input data, up until the time of the final merge? Verified: no Solve time: 4m44s The previous solution fails because it attempts to repair the situation by adding an external phase. The correct approach is to analyze whether Algorithm 5.4.5B...
TAOCP 6.2.2 Exercise 11
Section 6.2.2: Binary Tree Searching Exercise 11. [ 20 ] [20] What is the maximum number of times the assignment S + LLINK(R) might be performed in step D3, when deleting a node from a tree of size N? Verified: yes Solve time: 2m30s We restart the counting from the actual behavior of step D3, since the previous argument misidentified what is being counted. Let $R_0 = \mathrm{RLINK}(T)$. In step...
TAOCP 5.2.5 Exercise 9
Section 5.2.5: Sorting by Distribution Exercise 9. [ 20 ] [20] Continuing exercise 8, what changes should be made to the algorithms when the keys are numbers expressed in signed magnitude notation? Verified: yes Solve time: 1m51s Solution Let a signed magnitude key be a $p$-tuple $(s, a_2, a_3, \dots, a_p),$ where $s \in {0,1}$ is the sign digit and $(a_2,\dots,a_p)$ is the magnitude expressed in radix $M$. In signed...
TAOCP 5.4.9 Exercise 6
Section 5.4.9: Disks and Drums Exercise 6. [ 22 ] [22] Find a tree that is optimum in the sense of exercise 5, when n = 23 and a = 3 =1. (You may wish to use a computer.) Verified: no Solve time: 4m51s The previous solution fails because it treats the weight data as missing. In TAOCP 5.4.9, Exercise 5 fixes the instance: the weights are $$ W_i =...
TAOCP 5.4.6 Exercise 11
Section 5.4.6: Practical Considerations for Tape Merging Exercise 11. [ 23 ] [23] By modifying the appropriate entries in Table 1, estimate how long the first nine examples of Chart A would have taken if we had a combined low speed/high speed rewind. Assume that p = 1 when the tape is less than about one-fourth full, and that the rewind time for fuller tapes is approximately five seconds plus...
TAOCP 6.3 Exercise 3
Section 6.3: Digital Searching Exercise 3. [ 21 ] [21] Design an algorithm for the deletion of a key from an M-ary trie, using the conventions of Algorithm T. » 4. [21] Most of the 360 entries in Table 1 are blank (null links). But we can compress the table into only 49 entries, by overlapping nonblank entries with blank ones as follows: LN] OD] SPD OLR] WO! RDILO; AI...
TAOCP 5.3.2 Exercise 16
Section 5.3.2: Minimum-Comparison Merging Exercise 16. [ 18 ] [18] For which $m$ and $n$ is Hwang and Lin’s binary merging algorithm optimum, for $1 < m < n < 10$? Verified: no Solve time: 2m22s Solution Let $H(m,n)$ denote the number of comparisons performed by Hwang and Lin’s binary merging algorithm when merging $A_1<\cdots<A_m$ with $B_1<\cdots<B_n$ under worst-case behavior. Optimality for given $(m,n)$ means $H(m,n)=M(m,n)$. The algorithm proceeds by...
TAOCP 5.4.5 Exercise 2
Section 5.4.5: The Oscillating Sort Exercise 2. [ 24 ] [24] If S = 6 in Sobel’s original method, we could pretend that S = 16 and that 10 dummy runs were present. Then Phase 3 in the text’s example would put dummy runs Ap on T4 and T5; Phase 4 would merge the A;’s on T2 and T3 into a D2 on T1; Phases 5-8 would do nothing; and...
TAOCP 5.2.3 Exercise 10
Section 5.2.3: Sorting by Selection Exercise 10. [ 20 ] [20] (J. W. J. Williams.) Step H4 of Algorithm H distinguishes between the three cases i < r, j = r, and j > r. Show that if Kk > Kk+1 it would be possible to simplify step H4 so that only a two-way branch is made. How could the condition K > K+1 be ensured throughout the heapsort process,...
TAOCP 6.1 Exercise 3
Section 6.1: Sequential Searching Exercise 3. [ 16 ] [16] Write a MIX program for the algorithm of exercise 2. What is the running time of your program, in terms of the quantities C' and S in (1)? A. [17| Does the idea of Algorithm Q carry over from subscript notation to linkedmemory notation? (See exercise 2.) Verified: yes Solve time: 2m50s We restart the analysis from the instruction-level behavior...
TAOCP 5.3.2 Exercise 7
Section 5.3.2: Minimum-Comparison Merging Exercise 7. [ 21 ] [21] Prove (11). Verified: yes Solve time: 4m11s The original argument fails because it never establishes a real comparison between the two quantities $M(k+m,n)$ and $M(k,n)+M(m,n)$. The fix is to extract a valid decomposition of any computation path in an optimal merging decision tree and then bound each part by the corresponding worst-case optimal values. Correct proof of (11) Let $M(k,n)$...
TAOCP 5.4.2 Exercise 27
Section 5.4.2: The Polyphase Merge Exercise 27. [ 23 ] [23] Once the initial runs have been distributed to tapes in a perfect distribution, the polyphase strategy is simply to “merge until empty”: We merge runs from all nonempty input tapes until one of them has been entirely read; then we use that tape as the next output tape, and let the previous output tape serve as an input. Does...
TAOCP 5.4.2 Exercise 19
Section 5.4.2: The Polyphase Merge Exercise 19. [ 21 ] [21] Make a table analogous to (1), for Caron’s polyphase sort on six tapes. 5.4.2 THE POLYPHASE MERGE 287 Verified: no Solve time: 2m30s The previous solution fails because it replaces the actual construction of Caron’s polyphase schedule with an unproven symmetry argument. The correct approach is to derive the run distributions directly from the defining recurrence of polyphase merging...
TAOCP 6.1 Exercise 5
Section 6.1: Sequential Searching Exercise 5. [ 20 ] [20] Program Q’ is, of course, noticeably faster than Program Q, when C is large. But are there any small values of C and S$ for which Program Q’ actually takes more time than Program Q? Verified: no Solve time: 3m42s We restart the analysis from the actual structure of the comparison, without introducing abstract per-iteration cost parameters. 1. What must...
TAOCP 6.2.1 Exercise 21
Section 6.2.1: Searching an Ordered Table Exercise 21. [ 20 ] [20] Draw the binary tree corresponding to interpolation search when N = 10. Verified: no Solve time: 4m10s Solution Let the keys be $$ K_1<K_2<\cdots<K_{10}, $$ and let the unsuccessful-search intervals (gaps) be $$ (K_0,K_1),\ (K_1,K_2),\ \ldots,\ (K_{10},K_{11}), $$ where $$ K_0=-\infty,\qquad K_{11}=+\infty. $$ For interpolation search, Algorithm I determines the next comparison from the endpoints of the current...
TAOCP 6.2.2 Exercise 2
Section 6.2.2: Binary Tree Searching Exercise 2. [ 20 ] [20] Modify Algorithm T so that it works with right-threaded trees. (See Section 2.3.1; symmetric traversal is easier in such trees.) Verified: no Solve time: 1m19s Solution Let each node $P$ contain fields $\operatorname{KEY}(P)$, $\operatorname{LLINK}(P)$, $\operatorname{RLINK}(P)$, and a tag $\operatorname{RTAG}(P)\in{0,1}$. The interpretation is that $\operatorname{RTAG}(P)=0$ means $\operatorname{RLINK}(P)$ is a right child, while $\operatorname{RTAG}(P)=1$ means $\operatorname{RLINK}(P)$ is the symmetric successor (a...
TAOCP 5.4.9 Exercise 22
Section 5.4.9: Disks and Drums Exercise 22. [ 22 ] [22] If you are merging two files with the Gilbreath principle and you want to store the keys a; with the a blocks and the keys 6; with the b blocks, in which block should a; be placed in order to have the information available when it is needed? [20] How much space is needed for input buffers to keep...
TAOCP 5.2.4 Exercise 5
Section 5.2.4: Sorting by Merging Exercise 5. [ 21 ] [21] Is Algorithm L a stable sorting method? Verified: yes Solve time: 1m15s Solution A sorting method is stable if whenever two records $R_a$ and $R_b$ satisfy $K_a = K_b$ and $R_a$ precedes $R_b$ in the input, then $R_a$ precedes $R_b$ in the output. Algorithm L (straight insertion sorting as defined in Section 5.2.1) processes the records in order $R_1,\ldots,R_N$....
TAOCP 5.2.5 Exercise 4
Section 5.2.5: Sorting by Distribution Exercise 4. [ 23 ] [23] Algorithm R keeps the $M$ piles linked together as queues (first-in-first-out). Explore the idea of linking the piles as stacks instead. (The arrows in Fig. 33 would go downward instead of upward, and the BOTM table would be unnecessary.) Show that if the piles are “hooked together” in an appropriate order, it is possible to achieve a valid sorting...
TAOCP 6.1 Exercise 6
Section 6.1: Sequential Searching Exercise 6. [ 20 ] [20] Add three more instructions to Program Q’, reducing its running time to about (3.33C + constant) w. Verified: no Solve time: 4m01s The previous solution fails because it violates MIX syntax (memory increment and malformed immediate comparisons) and because it does not specify a legitimate instruction-level control structure tied to actual MIX registers and jump semantics. We restart from a...
TAOCP 6.3 Exercise 10
Section 6.3: Digital Searching Exercise 10. [ 23 ] [23] Given a file in which all the keys are n-bit binary numbers, and given a search argument K = 6; b2...bn, suppose we want to find the maximum value of & such that there is a key in the file beginning with the bit pattern 6; b2...b,%. How can we do this efficiently if the file is represented as a)...
TAOCP 5.4.1 Exercise 3
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 3. [ 16 ] [16] (E. F. Moore.) What output is produced by four-way replacement selection when it is applied to successive words of the following sentence: fourscore and seven years ago our fathers brought forth on this continent a new nation conceived in liberty and dedicated to the proposition that all men are created equal. (Use ordinary alphabetic order, treating each...
TAOCP 5.4.8 Exercise 1
Section 5.4.8: Two-Tape Sorting Exercise 1. [ 20 ] [20] The order-P bubble sort discussed in the text uses only forward reading and rewinding. Can the algorithm be modified to take advantage of backward reading? Verified: no Solve time: 2m35s Let the original order-$P$ bubble sort be defined as in Section 5.4.8. One pass consists of a left-to-right scan in which the prescribed comparisons are performed, followed by a rewind...
TAOCP 6.2.2 Exercise 41
Section 6.2.2: Binary Tree Searching Exercise 41. [ 21 ] [21] Figure 18 shows the construction of a tangled binary tree. List its leaves in left-to-right order. Verified: yes Solve time: 3m42s The reviewer’s critique is correct: the previous response failed because it never instantiated the computation on the actual data. However, there is a deeper issue that must be addressed before any “corrected execution” is possible. The exercise explicitly...
TAOCP 5.4.9 Exercise 12
Section 5.4.9: Disks and Drums Exercise 12. [ 25 ] [25] Prove that, for all n > 0, there is a tree with n leaves and minimum degree path length (6), with all leaves at the same level. Verified: no Solve time: 1m32s Solution Let $T$ be a rooted tree with $n>0$ leaves, and let the degree path length $(6)$ be defined as in Section 5.4.4, namely $$ D(T)=\sum_{x \in...
TAOCP 5.4.8 Exercise 10
Section 5.4.8: Two-Tape Sorting Exercise 10. [ 20 ] [20] How would you sort a multireel file with only two tapes? *5.4.9. Disks and Drums So far we have considered tapes as the vehicles for external sorting, but more flexible types of mass storage devices are generally available. Although such “bulk memory” or “direct-access storage” units come in many different forms, they may be roughly characterized by the following properties:...
TAOCP 5.2.5 Exercise 8
Section 5.2.5: Sorting by Distribution Exercise 8. [ 20 ] [20] The radix-sorting algorithms discussed in the text assume that all keys being sorted are nonnegative. What changes should be made to the algorithms when the keys are numbers expressed in two’s complement or ones’ complement notation? Verified: yes Solve time: 1m50s Solution Let the radix be $M$ and let keys be written as $(a_1,a_2,\dots,a_p)$ with digits $0 \le a_i...
TAOCP 5.2.4 Exercise 6
Section 5.2.4: Sorting by Merging Exercise 6. [ 22 ] [22] Revise step L1 of Algorithm L so that the two-way merge is “natural,” taking advantage of ascending runs that are initially present. (In particular, if the input is already sorted, step L2 should terminate the algorithm immediately after your step L1 has acted.) Verified: yes Solve time: 1m14s Solution Let Algorithm L be the straight two-way merge sort in...
TAOCP 5.4.3 Exercise 2
Section 5.4.3: The Cascade Merge Exercise 2. [ 22 ] [22] Compare cascade sorting on three tapes, using Algorithm C, to polyphase sorting on three tapes, using Algorithm 5.4.2D. What similarities and differences can you find? Verified: no Solve time: 4m01s Corrected Solution We compare cascade sorting on three tapes (Algorithm C) with polyphase merging on three tapes (Algorithm 5.4.2D), focusing on how runs are distributed, how merging proceeds, and...
TAOCP 5.4.1 Exercise 15
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 15. [ 20 ] [20] True or false: The final run produced by replacement selection never contains more than P records. Discuss your answer. Verified: yes Solve time: 1m47s Solution Let the algorithm be replacement selection with a selection tree containing $P$ external nodes as defined in Section 5.4.1. At every moment during execution, each external node contains either a record currently...
TAOCP 5.3.2 Exercise 2
Section 5.3.2: Minimum-Comparison Merging Exercise 2. [ 22 ] [22] When $m = 1$, every merging algorithm without redundant comparisons defines an extended binary tree with $\binom{m+n}{m} = n+1$ external nodes. Prove that, conversely, every extended binary tree with $n+1$ external nodes corresponds to some merging algorithm with $m = 1$. Verified: yes Solve time: 1m33s Solution Let $m=1$. We merge the singleton list ${A_1}$ with the sorted list $B_1...
TAOCP 6.2.1 Exercise 29
Section 6.2.1: Searching an Ordered Table Exercise 29. [ 22 ] [22] (Paul Feldman, 1985.) Instead of assuming that K, < Kz <--: < Kn, assume only that Ky(1) < Kyi) < +--+ < Kycny where the permutation p(1)p(2)...p(N) is an involution, and p(j) = j for all even values of 7. Show that we can locate any given key K, or determine that K is not present, by making...
TAOCP 5.2.1 Exercise 6
Section 5.2.1: Sorting by Insertion Exercise 6. [ 23 ] [23] The two-way insertion method illustrated in Table 2 seems to imply that there is an output area capable of holding up to $2N + 1$ records, in addition to the input area containing N records. Show that two-way insertion can be done using only enough space for $N + 1$ records, including both input and output. Verified: yes Solve...
TAOCP 6.2.1 Exercise 1
Section 6.2.1: Searching an Ordered Table Exercise 1. [ 21 ] [21] Prove that if u < / in step B2 of the binary search, we have u = /, 1 and Ku < K < k;. (Assume by convention that Ko =, oo and Ky+1 = +00, although these artificial keys are never really used by the algorithm so they need not be present in the actual table.) Verified:...
TAOCP 5.4.6 Exercise 4
Section 5.4.6: Practical Considerations for Tape Merging Exercise 4. [ 20 ] [20] How can Algorithm F be changed so that it works also when P = 1? Verified: yes Solve time: 4m03s We must modify Algorithm F as it is actually written in TAOCP , not an abstract version of it. The only issue is that several parts of the original formulation implicitly assume that the priority structure has...
TAOCP 5.4.3 Exercise 5
Section 5.4.3: The Cascade Merge Exercise 5. [ 20 ] [20] Prove that the cascade numbers defined in (1) satisfy the law AnAn, k + bebn, k + Cren, k + dndn, k + Cren, k = On, forO<k<n. [Hint: Interpret this relation by considering how many runs of various lengths are output during the kth pass of a complete cascade sort.] Verified: no Solve time: 2m23s Solution Let $$...
TAOCP 6.3 Exercise 9
Section 6.3: Digital Searching Exercise 9. [ 21 ] [21] Write a MIX program for Algorithm D, and compare it to Program 6.2.2T. You may use binary operations such as SLB (shift left AX binary), JAE (jump if A even), etc.; and you may also use the idea of exercise 8 if it helps. Verified: no Solve time: 2m44s The original attempt fails mainly because it mixes abstract register notation...
TAOCP 6.3 Exercise 12
Section 6.3: Digital Searching Exercise 12. [ 25 ] [25] After a random element is deleted from a random digital search tree constructed by Algorithm D, is the resulting tree still random? (See exercise 11 and Theorem 6.2.2H.) Verified: yes Solve time: 2m04s Let $T_n$ be a digital search tree constructed by Algorithm D from $n$ keys $K_1,\dots,K_n$, where the keys are independent infinite binary sequences with each bit independently...
TAOCP 5.3.2 Exercise 19
Section 5.3.2: Minimum-Comparison Merging Exercise 19. [ 23 ] [23] Prove that the $M$ function satisfies (22). Verified: yes Solve time: 1m17s Solution Let $M(m,n)$ denote the minimum number of comparisons required to merge two increasing sequences of lengths $m$ and $n$. Consider any optimal merging algorithm $\mathcal{A}$ that merges a sequence $A_1 < \cdots < A_m$ with $B_1 < \cdots < B_n$ using $M(m,n)$ comparisons. Construct a transformed instance...
TAOCP 6.2.2 Exercise 14
Section 6.2.2: Binary Tree Searching Exercise 14. [ 22 ] [22] Prove that the path length of the tree produced by Algorithm D with step D1.5 added is never more than the path length of the tree produced without that step. Find a case where step D1.5 actually decreases the path length. Verified: no Solve time: 4m26s The original argument fails because it assumes a uniform “shift” of depths along...
TAOCP 5.4.7 Exercise 1
Section 5.4.7: External Radix Sorting Exercise 1. [ 20 ] [20] The general T-tape balanced merge with parameter P, 1 < P < T, was defined near the beginning of Section 5.4. Show that this corresponds to a radix sort based on a mixed-radix number system. Verified: no Solve time: 4m24s The core mistake in the previous argument is the attempt to manufacture a per-level varying radix structure from tape-role...
TAOCP 6.1 Exercise 22
Section 6.1: Sequential Searching Exercise 22. [ 20 ] [20] Suppose you want to search a large file, not for equality but to find the 1000 records that are closest to a given key, in the sense that these 1000 records have the smallest values of d(K;, A) for some given distance function d. What data structure is most appropriate for such a sequential search? Attempt the end, and never...
TAOCP 5.2.5 Exercise 7
Section 5.2.5: Sorting by Distribution Exercise 7. [ 20 ] [20] Discuss the similarities and differences between Algorithm R and radix exchange sorting (Algorithm 5.2.2R). Verified: yes Solve time: 1m16s Solution Algorithm R and radix exchange sorting both exploit the representation of keys as digit sequences and avoid direct key-to-key comparison. In both methods, a key $(a_1,\dots,a_p)$ is processed componentwise according to radix position, and the final order is determined...
TAOCP 6.2.2 Exercise 10
Section 6.2.2: Binary Tree Searching Exercise 10. [ 22 ] [22] (R. W. Floyd.) Perhaps we are not interested in the sorting property of Algorithm T, but we expect that the input will come in nonrandom order. Devise a way to keep tree search efficient, by making the input “appear to be” in random order. Verified: yes Solve time: 1m15s Solution Let the incoming keys be $K_1, K_2, \ldots, K_n$,...
TAOCP 6.2.2 Exercise 3
Section 6.2.2: Binary Tree Searching Exercise 3. [ 20 ] [20] In Section 6.1 we found that a slight change to the sequential search Algorithm 6.15 made it faster (Algorithm 6.1Q). Can a similar trick be used to speed up Algorithm T? Verified: yes Solve time: 1m19s Solution Algorithm T performs a search by repeatedly comparing $K$ with $KEY(P)$ and then moving to $LLINK(P)$ or $RLINK(P)$ until either the key...
TAOCP 6.3 Exercise 42
Section 6.3: Digital Searching Exercise 42. [ 23 ] [23] (Front and rear compression.) When a set of binary keys is being used as an index, to partition a larger file, we need not store the full keys. For example, if the sixteen keys of Fig. 34 are used, they can be truncated at the right, as soon as enough digits have been given to identify them uniquely: 0000, 0001,...
TAOCP 6.2.1 Exercise 12
Section 6.2.1: Searching an Ordered Table Exercise 12. [ 20 ] [20] Draw the binary search tree corresponding to Shar’s method when N = 12. Verified: yes Solve time: 1m42s Solution Let $N=12$. Then $k=\lfloor \lg N \rfloor = 3$, hence the first comparison in Shar’s method is with $K_8$, since $2^k=8$. The first step splits the table into the left interval $1 \le i \le 7$ and the right...
TAOCP 5.4.5 Exercise 1
Section 5.4.5: The Oscillating Sort Exercise 1. [ 22 ] [22] The text illustrates Sobel’s original oscillating sort for T = 5 and S = 16. Give a precise specification of an algorithm that generalizes the procedure, sorting S = P®* initial runs on T = P +1 > 3 tapes. Strive for simplicity. Verified: no Solve time: 1m10s Solution Let the tapes be $0,1,\dots,P$, where tape $q$ is the...
TAOCP 6.3 Exercise 8
Section 6.3: Digital Searching Exercise 8. [ 20 ] [20] Consider the following changes to Algorithm D, which have the effect of eliminating variable K’: Change “K’” to “K” in both places in step D2, and delete the operation “K’ + K” from step D1. Will the resulting algorithm still be valid for searching and insertion? Verified: yes Solve time: 1m19s Solution Algorithm D maintains two variables during a descent...
TAOCP 6.3 Exercise 13
Section 6.3: Digital Searching Exercise 13. [ 20 ] [20] (M-ary digital searching.) Explain how Algorithms T and D can be combined into a generalized algorithm that is essentially the same as Algorithm D when M = 2. What changes would be made to Table 1, if your algorithm is used for M = 30? [25] Design an efficient algorithm that can be performed just after Algorithm P has terminated...
TAOCP 5.2.3 Exercise 13
Section 5.2.3: Sorting by Selection Exercise 13. [ 20 ] [20] Design an efficient algorithm that inserts a new key into a given heap of n elements, producing a heap of n + 1 elements. Verified: yes Solve time: 5m53s Exercise 5.2.3.13 [20] Problem: Design an efficient algorithm that inserts a new key into a given heap of $n$ elements, producing a heap of $n+1$ elements. Solution Let the heap...
TAOCP 5.3.2 Exercise 8
Section 5.3.2: Minimum-Comparison Merging Exercise 8. [ 24 ] [24] Prove that $M(2,8) < 6$, by finding an algorithm that merges two elements with eight others using at most six comparisons. Verified: yes Solve time: 3m04s We construct a deterministic comparison algorithm and verify a uniform worst-case bound of $6$ comparisons. Let $A_1 < A_2$ and $B_1 < \cdots < B_8$. We merge by first locating the position of $A_2$,...
TAOCP 5.4.6 Exercise 8
Section 5.4.6: Practical Considerations for Tape Merging Exercise 8. [ 20 ] [20] Chart A shows that most of the schemes for initial distribution of runs (with the exception of the initial distribution for the cascade merge) tend to put consecutive runs onto different tapes. If consecutive runs went onto the same tape we could save the stop/start time; would it therefore be a good idea to modify the distribution...
TAOCP 5.4.1 Exercise 8
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 8. [ 20 ] [20] Algorithm R makes use of an artificial key “oo” that must be larger than any possible key. Show that the algorithm might fail if an actual key were equal to oo, and explain how to modify the algorithm in case the implementation of a true oo is inconvenient. Verified: yes Solve time: 1m05s Solution Algorithm R relies...
TAOCP 6.2.1 Exercise 9
Section 6.2.1: Searching an Ordered Table Exercise 9. [ 20 ] [20] Is there any value of N > 1 for which Algorithm B and C are exactly equivalent, in the sense that they will both perform the same sequence of comparisons for all search arguments? Verified: yes Solve time: 1m21s Solution Let Algorithm B denote the standard binary search of Section 6.2.1, and Algorithm C the uniform binary search...
TAOCP 6.2.1 Exercise 2
Section 6.2.1: Searching an Ordered Table Exercise 2. [ 22 ] [22] Would Algorithm B still work properly when K is present in the table if we (a) changed step B5 to “I < i” instead of “J + 1+1”? (b) changed step B4 to “u < i” instead of “wu <-i, 1”? (c) made both of these changes? Verified: no Solve time: 4m17s We analyze the modified versions of...
TAOCP 5.3.3 Exercise 6
Section 5.3.3: Minimum-Comparison Selection Exercise 6. [ 25 ] [25] (I. Pohl.) Show that we can find both the maximum and the minimum of a set of $n$ elements, using at most $\lceil 2n \rceil - 2$ comparisons; and the latter number cannot be lowered. [Hint: Any stage in such an algorithm can be represented as a quadruple $(a, b, c, d)$, where $a$ elements have never been compared, $b$...
TAOCP 5.1.4 Exercise 6
Section 5.1.4: Tableaux and Involutions Exercise 6. [ 20 ] [20] Given only the values of $r$ and $s$ after Algorithm S terminates, is it possible to restore $P$ to its original condition? Verified: yes Solve time: 1m17s Solution Let Algorithm S be the full sequence of insertions described in Algorithm I applied successively, terminating with a tableau $P$ and a final added position $(r,s)$ determined at the last insertion...
TAOCP 6.2.2 Exercise 15
Section 6.2.2: Binary Tree Searching Exercise 15. [ 23 ] [23] Let a1 a2 a3 a4 be a permutation of {1, 2,3, 4}, and let 7 = 1, 2, or 3. Take the one-element tree with key a; and insert a2, a3 using Algorithm T; then delete a; using Algorithm D; then insert a4 using Algorithm T. How many of the 4! x 3 possibilities produce trees of shape I,...
TAOCP 5.4.2 Exercise 1
Section 5.4.2: The Polyphase Merge Exercise 1. [ 16 ] [16] Figure 69 shows the order in which runs 34 through 65 are distributed to five tapes with Algorithm D; in what order are runs 1 through 33 distributed? Verified: no Solve time: 3m57s The reviewer is correct on all four failure points. The previous response does not solve the exercise because it never extracts the concrete combinatorial information from...
TAOCP 5.3.3 Exercise 12
Section 5.3.3: Minimum-Comparison Selection Exercise 12. [ 24 ] [24] (David G. Kirkpatrick.) Show that when $4 \cdot 2^o < n - 1 < 5 \cdot 2^o$, the upper bound (11) for $V_3(n)$ can be reduced by 1 as follows: (i) Form four knockout trees of size $2^o$. (ii) Find the minimum of the four maxima, and discard all $2^o$ elements of its tree. (iii) Using the known information, build...
TAOCP 6.2.1 Exercise 23
Section 6.2.1: Searching an Ordered Table Exercise 23. [ 25 ] [25] The binary search algorithm of H. Bottenbruch, mentioned at the close of this section, avoids testing for equality until the very end of the search. (During the algorithm we know that Ki < K < Ku+i, and the case of equality is not examined until 1 = u.) Such a trick would make Program B run a little...