#medium
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.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: no Solve time: 5m36s 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.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: 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 solution in positive integers satisfying $x_1...
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.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.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: 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.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 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: 3m41s We work from first principles and reduce the problem to a structured constraint on permutation systems. 1. Reformulation of the condition Let the Sudoku grid be indexed by $(r,c)\in{1,\dots,9}^2$....
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.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.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.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.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.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 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 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.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.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.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.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.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.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 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 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.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 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 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 5.4.2 Exercise 3
Section 5.4.2: The Polyphase Merge Exercise 3. [ 22 ] [22] Prove that the condition D[1] > D[2] >--- > D[T] is always satisfied at the conclusion of step D4. Explain why this condition is important, in the sense that the mechanism of steps D2 and D3 would not work properly otherwise. Verified: no Solve time: 4m26s Start from the definition at the end of step D4: $$ D[j] =...
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 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 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 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.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 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.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.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.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.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.3.3 Exercise 7
Section 5.3.3: Minimum-Comparison Selection Exercise 7. [ 20 ] [20] (R. W. Floyd.) Show that it is possible to select, in order, both the $k$ largest and the $j$ smallest elements of a set of $n$ elements, using at most $\lceil 3n \rceil - k - 1 + \sum_{i=1}^{k} e_i < n\ \text{or similar}$ comparisons. Verified: no Solve time: 4m59s The previous argument fails because it replaces Floyd’s comparison accounting...
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.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 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.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 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.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 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.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.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 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.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.4.1 Exercise 9
Section 5.4.1: Multiway Merging and Replacement Selection Exercise 9. [ 23 ] [23] How would you modify Algorithm R so that it causes certain specified runs (depending on RC) to be output in ascending order, and others in descending order? Verified: yes Solve time: 1m17s Solution Let the comparison used in Algorithm R for the selection tree be denoted by $\prec$, where in the original algorithm $a \prec b$ means...
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 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.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 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 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.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.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.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.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 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.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.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 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.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 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 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 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.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 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 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.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.2.1 Exercise 10
Section 5.2.1: Sorting by Insertion Exercise 10. [ 22 ] [22] If $K_i > K_j$, when we begin step D3, Algorithm D specifies a lot of actions that accomplish nothing. Show how to modify Program D so that this redundant computation can be avoided, and discuss the merits of such a modification. Verified: yes Solve time: 1m29s Solution In step D3, Program D performs three actions before any comparison outcome...
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 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.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 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.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.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.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.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 5.4.2 Exercise 25
Section 5.4.2: The Polyphase Merge Exercise 25. [ 19 ] [19] Show how the tape-splitting polyphase merge on four tapes, mentioned at the end of this section, would sort 32 initial runs. (Give a phase-by-phase analysis like the 82-run six-tape example in the text.) Verified: yes Solve time: 2m53s We restart from the correct structure of a 4-tape polyphase merge. With four tapes we perform a 3-way merge at each...
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.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.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 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.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.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 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 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.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 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.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 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 6.2.1 Exercise 24
Section 6.2.1: Searching an Ordered Table Exercise 24. [ 23 ] [23] We have seen in Sections 2.3.4.5 and 5.2.3 that the complete binary tree is a convenient way to represent a minimum-path-length tree in consecutive locations. Devise an efficient search method based on this representation. [Hint: Is it possible to use multiplication by 2 instead of division by 2 in a binary search?] Verified: yes Solve time: 4m11s We...
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 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.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.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.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 6.2.2 Exercise 16
Section 6.2.2: Binary Tree Searching Exercise 16. [ 25 ] [25] Is the deletion operation commutative? That is, if Algorithm D is used to delete X and then Y, is the resulting tree the same as if Algorithm D is used to delete Y and then X? Verified: no Solve time: 5m16s The previous solution fails because it tries to reduce structural equality of binary search trees to inorder equality...
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...
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.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 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 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 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.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 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.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.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 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 5.1.2 Exercise 8
Section 5.1.2: Permutations of a Multiset Exercise 8. [ 20 ] [20] Find all factorizations of (12) into two factors $\alpha \mathbin{\text{?}} \beta$. Verified: no Solve time: 31m23s Stopped thinking
TAOCP 5 Exercise 4
Section 5: Introduction to Sorting Exercise 4. [ 20 ] [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: no Solve...
TAOCP 4.7 Exercise 2
Section 4.7: Manipulation of Power Series Exercise 2. [ 20 ] If the coefficients of $U(z)$ and $V(z)$ are integers and $V_0 \ne 0$, find a recurrence relation for the integers $V_0^{n+1}W_n$, where $W_n$ is defined by (3). How could you use this for power series division? Verified: yes Solve time: 4m45s Exercise 4.7.2 [20] 2. If the coefficients of $U(z)$ and $V(z)$ are integers and $V_0 \ne 0$, find...
TAOCP 4.6.4 Exercise 43
Section 4.6.4: Evaluation of Polynomials Exercise 43. [ 23 ] [23] Explain how to evaluate $x^n + \cdots + x + 1$ with $2(n+1) - 2$ multiplications and $l(n+1)$ additions (no divisions or subtractions), where $l(n)$ is the function studied in Section 4.6.3. Verified: yes Solve time: 1m40s Solution Let $$ S_n(x)=1+x+x^2+\cdots+x^n. $$ The identity $$ S_n(x)=\frac{x^{n+1}-1}{x-1} $$ is unusable here because divisions and subtractions are forbidden. Instead, we construct...
TAOCP 4.6.4 Exercise 41
Section 4.6.4: Evaluation of Polynomials Exercise 41. [ 22 ] [22] Show that the real and imaginary parts of $(a + bi)(c + di)$ can be obtained by doing 3 multiplications and 5 additions of real numbers, where two of the additions involve $a$ and $b$ only. Verified: yes Solve time: 1m45s Solution We wish to compute the real and imaginary parts of the product of two complex numbers $(a...
TAOCP 4.6.4 Exercise 22
Section 4.6.4: Evaluation of Polynomials Exercise 22. [ 18 ] [18] What is the scheme for evaluating $x^9 - 3x^5 + x^4 - 2x^3 + x^2 - 3x - 1$, using Pan's method (16)? Verified: yes Solve time: 8m06s Restart from the goal: construct a Pan-style evaluation scheme (16), meaning a straight-line program that minimizes multiplications by first generating needed powers of $x$ via a short addition chain, then forming...
TAOCP 4.6.4 Exercise 21
Section 4.6.4: Evaluation of Polynomials Exercise 21. [ 20 ] [20] Find two additional ways to evaluate the polynomial $x^8 + 13x^5 + 49x^4 + 33x^3 - 61x^2 - 37x + 3$ by scheme (12), using the two roots of (15) that were not considered in the text. Verified: no Solve time: 9m35s
TAOCP 4.6.4 Exercise 20
Section 4.6.4: Evaluation of Polynomials Exercise 20. ▶ [ 21 ] Write a MIX program that evaluates a fifth-degree polynomial according to scheme (11); try to make the program as efficient as possible, by making slight modifications to (11). Use MIX's floating point arithmetic operators FADD and FMUL, which are described in Section 4.2.1. Verified: no Solve time: 3m49s Solution We are asked to write a MIX program that evaluates...
TAOCP 4.6.4 Exercise 6
Section 4.6.4: Evaluation of Polynomials Exercise 6. [ 22 ] (L. de Jong and J. van Leeuwen.) Show how to improve on steps S1, ..., S4 of the Shaw–Traub algorithm by comparing only about $\frac{1}{3}n$ powers of $x_0$. Verified: yes Solve time: 5m50s Solution We are asked to improve steps S1, ..., S4 of the Shaw–Traub algorithm for evaluating an $n$th-degree polynomial $$ u(x) = u_n x^n + u_{n-1} x^{n-1}...
TAOCP 4.6.3 Exercise 30
Section 4.6.3: Evaluation of Powers Exercise 30. [ 20 ] [20] An addition-subtraction chain has the rule $a_i = a_j \pm a_k$ in place of (a); the imaginary operation described in the text has a new operation code, SUB . (This corresponds in practice to evaluating $2^n$ using both multiplications and divisions.) Find an addition-subtraction chain, for some $n$, that has fewer than $l(n)$ steps. Verified: yes Solve time: 1m42s...
TAOCP 4.6.3 Exercise 25
Section 4.6.3: Evaluation of Powers Exercise 25. [ 20 ] Let $y$ be a fraction, $0 < y < 1$, expressed in the binary number system as $y = (.d_1 \ldots d_k)_2$. Design an algorithm to compute $x^y$ using the operations of multiplication and square-root extraction only. Verified: yes Solve time: 2m12s Solution Let $y = (.d_1 d_2 \ldots d_k)_2$ be a binary fraction, where $0 < y < 1$...
TAOCP 4.6.3 Exercise 9
Section 4.6.3: Evaluation of Powers Exercise 9. ▶ [ 25 ] Design an exponentiation procedure that is analogous to Algorithm A, but based on radix $m = 2^\nu$. Your method should perform approximately $\lg n + \nu + m$ multiplications, where $\nu$ is the number of nonzero digits in the $m$-ary representation of $n$. Verified: no Solve time: 5m10s There is not enough information to diagnose the algorithm from this...
TAOCP 4.6.3 Exercise 5
Section 4.6.3: Evaluation of Powers Exercise 5. ▶ [ 24 ] [24] Figure 14 shows the first eight levels of the "power tree." The $(k+1)$st level of this tree is defined as follows, assuming that the first $k$ levels have been constructed: Take each node $n$ of the $k$th level, from left to right in turn, and attach below it the nodes $$n+1,\ n+a_1,\ n+a_2,\ \ldots,\ n+a_{k-1} = 2n$$ (in...
TAOCP 4.6.3 Exercise 3
Section 4.6.3: Evaluation of Powers Exercise 3. [ 22 ] [22] How is $2^{375}$ calculated by (a) the binary method? (b) the ternary method? (c) the quaternary method? (d) the factor method? Verified: no Solve time: 29m57s Solution We are asked to compute $2^{375}$ by various exponentiation methods. Let us analyze each method in turn. (a) Binary method. The binary method corresponds to Algorithm A, where we scan the binary...
TAOCP 4.6.3 Exercise 2
Section 4.6.3: Evaluation of Powers Exercise 2. [ 24 ] [24] Write a MIX program for Algorithm A, to calculate $x^n \bmod w$ given integers $n$ and $z$, where $w$ is the word size. Assume that MIX has the binary operations SRB, JAE, etc., that are described in Section 4.5.2. Write another program that computes $x^n \bmod w$ in a serial manner (multiplying repeatedly by $x$), and compare the running...
TAOCP 4.6.2 Exercise 26
Section 4.6.2: Factorization of Polynomials Exercise 26. [ 20 ] [20] Let $d_1, \ldots, d_r$ be the degrees of the irreducible factors of $u(x)$ modulo $p$, with proper multiplicity, so that $d_1 + \cdots + d_r = \deg(u)$. Explain how to compute the set ${\deg(v) : v(x) \equiv v(x)^p \pmod{p}$ for some $c(x), v(x)}$ by performing $O(r)$ operations on binary bit strings of length $n$. Verified: yes Solve time: 2m30s...
TAOCP 4.6.2 Exercise 11
Section 4.6.2: Factorization of Polynomials Exercise 11. [ 22 ] Find the complete factorization of the polynomial $u(x)$ in (22), modulo 5. Verified: no Solve time: 6m40s Problem Understanding We are given a black-box quantum operation that acts on a single qubit. This operation is guaranteed to be exactly one of four possibilities: the identity operation or one of the three Pauli gates. Our task is to identify which of...
TAOCP 4.6.2 Exercise 10
Section 4.6.2: Factorization of Polynomials Exercise 10. ▶ [ 21 ] Find the complete factorization of the polynomial $u(x)$ in (22), modulo 2, using Berlekamp's procedure. Verified: no Solve time: 7m50s Solution Assume that $u(x)$ is squarefree and satisfies $$ u(x)=p_1(x)p_2(x)\cdots p_r(x), $$ as in (6). Let $v(x)$ be any polynomial satisfying $$ v(x)^p\equiv v(x)\pmod{u(x)}. $$ Equation (10) gives $$ v(x)^p-v(x) \prod_{0\le s<p}(v(x)-s). $$ Since $v(x)^p\equiv v(x)\pmod{u(x)}$, the polynomial $u(x)$...
TAOCP 4.6.2 Exercise 9
Section 4.6.2: Factorization of Polynomials Exercise 9. [ 20 ] Explain how to construct a table of reciprocals mod 101 in a simple way, given that 2 is a primitive root of 101. Verified: no Solve time: 6m14s Solution Assume that $u(x)$ is squarefree and satisfies $$ u(x)=p_1(x)p_2(x)\cdots p_r(x), $$ as in (6). Let $v(x)$ be any polynomial satisfying $$ v(x)^p\equiv v(x)\pmod{u(x)}. $$ Equation (10) gives $$ v(x)^p-v(x) \prod_{0\le s<p}(v(x)-s)....
TAOCP 4.5.4 Exercise 38
Section 4.5.4: Factoring into Primes Exercise 38. [ 25 ] (Useless primes.) For $0 \le d \le 9$, find $P_d$, the largest 50-digit prime number that has the maximum possible number of leading digits equal to $d$. (First maximize the number of $d$'s, then find the largest such prime.) Verified: no Solve time: 15m39s The reviewer’s objections are correct. The previous submission does not solve the exercise. In particular, it...
TAOCP 4.5.4 Exercise 8
Section 4.5.4: Factoring into Primes Exercise 8. ▶ [ 23 ] ( The sieve of Eratosthenes , 3rd century b.c.) The following procedure evidently discovers all the prime numbers less than a given integer $N$, since it removes all the nonprime numbers: Start with all the odd numbers between 1 and $N$; then successively strike out the multiples $p_k^2, p_k(p_k+2), p_k(p_k+4), \ldots,$ of the $k$th prime $p_k$, for $k =...
TAOCP 4.5.4 Exercise 7
Section 4.5.4: Factoring into Primes Exercise 7. [ 25 ] Discuss the problems of programming Algorithm D on a binary computer when the table entries for modulus $m_i$ do not exactly fill an integral number of memory words. Verified: yes Solve time: 1m46s Solution Algorithm D (Fermat's method) involves iterative calculations modulo various integers $m_i$ to test for squares and compute factors of a number $N$. On a binary computer,...
TAOCP 4.5.4 Exercise 5
Section 4.5.4: Factoring into Primes Exercise 5. [ 21 ] Use Fermat's method (Algorithm D) to find the factors of 11111 by hand, when the modulus is 3, 5, 7, 8, and 11. Verified: yes Solve time: 7m24s The review correctly identifies the central failure: the solution never completes Fermat’s method by producing an actual $x$, $y$, and hence never factors $11111$. The congruence work is usable, but it must...
TAOCP 4.5.3 Exercise 32
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 32. [ 20 ] (a) A Morse code sequence of length $n$ is a string of $r$ dots and $s$ dashes, where $r + 2s = n$. For example, the Morse code sequences of length 4 are $$\cdot\cdot\cdot\cdot,\quad -\cdot\cdot,\quad \cdot-\cdot,\quad \cdot\cdot-,\quad --$$ Noting that the continuant $K_4(x_1, x_2, x_3, x_4)$ is $x_1 x_2 x_3 x_4 + x_1 x_2 + x_1 x_4 + x_3...
TAOCP 4.5.3 Exercise 1
Section 4.5.3: Analysis of Euclid's Algorithm Exercise 1. ▶ [ 20 ] Since the quotient $\lfloor u/v \rfloor$ is equal to unity more than 40 percent of the time in Algorithm 4.5.2A, it may be advantageous on some computers to make a test for this case and to avoid the division when the quotient is unity. Is the following MIX program for Euclid's algorithm more efficient than Program 4.5.2A? LDX...
TAOCP 4.5.2 Exercise 19
Section 4.5.2: The Greatest Common Divisor Exercise 19. [ 21 ] [21] Use the text's method to find a general solution in integers to the following sets of equations: a) $3x + 7y + 11z = 1$ b) $3x + 7y + 11z = 1$ $5x - 7y - 3z = 3$ $5x + 7y - 3z = -3$ Verified: yes Solve time: 2m07s Solution (a) The system is $3x...
TAOCP 4.5.2 Exercise 9
Section 4.5.2: The Greatest Common Divisor Exercise 9. [ 18 ] Using Algorithm B and hand calculation, find gcd(31408, 2718). Also find integers $m$ and $n$ such that $31408n + 2718n = \gcd(31408, 2718)$, using Algorithm X. Verified: yes Solve time: 1m21s Solution We are asked to compute $\gcd(31408, 2718)$ using Algorithm B and then to find integers $m$ and $n$ such that $31408 , m + 2718 , n...
TAOCP 4.4 Exercise 13
Section 4.4: Radix Conversion Exercise 13. ▶ [ 25 ] Assume that locations $U+1$, $U+2$, $\ldots$, $U+m$ contain a multiple-precision fraction $(.u_1 u_2 \ldots u_m)_b$ where $b$ is the byte size of MIX. Write a MIX routine that converts this fraction to decimal notation, truncating it to 180 decimal digits. The answer should be printed on two lines, with the digits grouped into 20 blocks of nine each separated by...
TAOCP 4.4 Exercise 12
Section 4.4: Radix Conversion Exercise 12. ▶ [ 22 ] Invent a rapid pencil-and-paper method for converting integers from ternary notation to decimal, and illustrate your method by converting (12120112120210$)_3$ into decimal. How would you go from decimal to ternary? Verified: no Solve time: 10m38s Solution Method 1b already provides the key idea. If $(u_m\ldots u_1u_0)_3$ is a ternary integer, its decimal value is obtained from the nested form $$...
TAOCP 4.4 Exercise 10
Section 4.4: Radix Conversion Exercise 10. [ 22 ] Table 1 shows how a binary-coded decimal number can be doubled by using various shifting, extracting, and addition operations on a binary computer. Give an analogous method that computes half of a binary-coded decimal number (throwing away the remainder if the number is odd). Verified: yes Solve time: 1m46s Solution Let $u$ be a nonnegative integer represented in binary-coded decimal (BCD)...
TAOCP 4.4 Exercise 11
Section 4.4: Radix Conversion Exercise 11. [ 16 ] Convert $(5772)_{10}$ to decimal. Verified: yes Solve time: 8m40s Exercise 4.4.11 [ 16 ] 11. Convert $(5772)_{10}$ to decimal. Solution The numeral $(5772)_{10}$ is expressed in base $10$, which is the standard decimal system. Converting a decimal numeral to decimal does not change its value, because the numeral already represents a number in base $10$. Formally, if a number is written...
TAOCP 4.4 Exercise 8
Section 4.4: Radix Conversion Exercise 8. [ 24 ] Write a MIX program analogous to (1) that uses (5) and includes no division instructions. Verified: yes Solve time: 2m06s Solution Equation (5) shows how to replace division by $10$ with multiplication by an approximation to $\frac1{10}$. Let $$ x $$ be a fixed radix-2 fraction satisfying $$ \frac1{10}<x<\frac1{10}+\frac1w, $$ where $w$ is the word size. Given an integer $u$ with...
TAOCP 4.4 Exercise 3
Section 4.4: Radix Conversion Exercise 3. ▶ [ 25 ] [25] (D. Taranto.) When fractions are being converted, there is no obvious way to decide how many digits to give in the answer. Design a simple generalization of Method 2a that, given two positive radix-$b$ fractions $u$ and $v$ between 0 and 1, converts $u$ to a rounded radix-$B$ equivalent $U$ that has just enough places $M$ to the right...
TAOCP 4.4 Exercise 1
Section 4.4: Radix Conversion Exercise 1. ▶ [ 25 ] [25] Generalize Method 1b so that it works with arbitrary mixed-radix notations, converting $$a_m b_{m-1} \ldots b_1 b_0 + \cdots + a_1 b_0 + a_0 \quad \text{into} \quad A_M B_{M-1} \ldots B_1 B_0 + \cdots + A_1 B_0 + A_0,$$ where $0 \le a_j < b_j$ and $0 \le A_j < B_j$ for $0 \le j < m$ and $0...
TAOCP 4.4 Exercise 2
Section 4.4: Radix Conversion Exercise 2. [ 25 ] [25] Generalize Method 1a so that it works with mixed-radix notations, as in exercise 1, and give an example of your generalization by manually solving the same conversion problem stated in exercise 1. Verified: yes Solve time: 4m51s Let the mixed-radix system have radices $$ r_0,r_1,\ldots,r_{n-1}, $$ so that a number is represented in the form $$ (a_n,a_{n-1},\ldots,a_0), $$ with $$...
TAOCP 4.3.3 Exercise 16
Section 4.3.3: How Fast Can We Multiply? Exercise 16. ▶ [ 25 ] [25] Prove that it takes only $O(K \log K)$ arithmetic operations to evaluate the discrete Fourier transform (35), even when $K$ is not a power of 2. [ Hint: Rewrite (35) in the form $$\hat{u} t = \omega^{-t^2/2} \sum {0 \le s < K} \omega^{(s+t)^2/2} \omega^{-s^2/2} u_s$$ and express this sum as a convolution product.] Verified: yes...
TAOCP 4.3.3 Exercise 1
Section 4.3.3: How Fast Can We Multiply? Exercise 1. [ 22 ] [22] The idea expressed in (2) can be generalized to the decimal system, if the radix 2 is replaced by 10. Using this generalization, calculate 1234 times 2341 (reducing this product of four-digit numbers to three products of two-digit numbers, and reducing each of the latter to products of one-digit numbers). Verified: no Solve time: 6m12s Thank you,...
TAOCP 4.3.1 Exercise 43
Section 4.3.1: The Classical Algorithms Exercise 43. [ 22 ] [22] Shades of gray or components of color values in digitized images are usually represented as 8-bit numbers $u$ in the range $[0 \mathinner{..} 255]$, denoting the fraction $u/255$. Given two such fractions $u/255$ and $v/255$, graphical algorithms often need to compute their approximate product $w/255$, where $w$ is the nearest integer to $uv/255$. Prove that $w$ can be obtained...
TAOCP 4.3.1 Exercise 39
Section 4.3.1: The Classical Algorithms Exercise 39. [ 25 ] [25] (D. Bailey, P. Borwein, and S. Plouffe, 1996.) Explain how to compute the $n$th bit of the binary representation of $\pi$ without knowing the previous $n - 1$ bits, by using the identity $$\pi = \sum_{k \ge 0} \frac{1}{16^k} \left( \frac{4}{8k+1} - \frac{2}{8k+4} - \frac{1}{8k+5} - \frac{1}{8k+6} \right)$$ and doing $O(n \log n)$ arithmetic operations on $O(\log n)$-bit integers....
TAOCP 4.3.1 Exercise 37
Section 4.3.1: The Classical Algorithms Exercise 37. [ 20 ] [20] (E. Salamin.) Explain how to avoid the normalization and unnormalization steps of Algorithm D, when $d$ is a power of 2 on a binary computer, without changing the sequence of trial quotient digits computed by that algorithm. (How can $\hat{q}$ be computed in step D3 if the normalization of step D1 hasn't been done?) Verified: no Solve time: 2m39s...
TAOCP 4.3.1 Exercise 30
Section 4.3.1: The Classical Algorithms Exercise 30. ▶ [ 22 ] [22] If memory space is limited, it may be desirable to use the same storage locations for both input and output during the performance of some of the algorithms in this section. Is it possible to have the quotient $q_0, \ldots, q_n$ stored in the same respective locations as $u_0, \ldots, u_{n-1}$ or $v_0, \ldots, v_{n-1}$ during Algorithm A...
TAOCP 4.3.1 Exercise 26
Section 4.3.1: The Classical Algorithms Exercise 26. [ 21 ] Write a MIX routine for step D8, which is needed to complete Program D. Verified: no Solve time: 2m22s Solution Step D8 in Program D performs the correction after an over-subtraction in the trial quotient step of the division algorithm. When the computed product $q_j \cdot v$ exceeds the current partial dividend segment, the subtraction in step D7 produces a...
TAOCP 4.3.1 Exercise 22
Section 4.3.1: The Classical Algorithms Exercise 22. ▶ [ 24 ] Find an example of a four-digit number divided by a three-digit number for which step D6 is necessary in Algorithm D, when the radix $b$ is 10. Verified: no Solve time: 3m20s Solution Let the dividend be $(u_3 u_2 u_1 u_0) {10} = (8500) {10}$ and the divisor be $(v_2 v_1 v_0) {10} = (101) {10}$. In Algorithm D,...
TAOCP 4.3.1 Exercise 13
Section 4.3.1: The Classical Algorithms Exercise 13. [ 21 ] Write a MIX program that multiplies $(u_{n-1} \ldots u_1 u_0)_b$ by $v$, where $v$ is a single-precision number (that is, $0 \le v < b$), producing the answer $(w_n \ldots w_1 w_0)_b$. How much running time is required? Verified: yes Solve time: 7m36s Let the multiplicand be stored in memory as $$ U=(u_{n-1}\ldots u_1u_0)_b, $$ with one digit per word,...
TAOCP 4.3.1 Exercise 12
Section 4.3.1: The Classical Algorithms Exercise 12. [ 16 ] Algorithm S assumes that we know which of the two input operands is the larger; if this information is not known, we could go ahead and perform the subtraction anyway, and we would find that an extra borrow is still present at the end of the algorithm. Design another algorithm that could be used (if there is a borrow present...
TAOCP 4.3.1 Exercise 10
Section 4.3.1: The Classical Algorithms Exercise 10. [ 18 ] Would Program S work properly if the instructions on lines 06 and 07 were interchanged? If the instructions on lines 05 and 06 were interchanged? Verified: yes Solve time: 3m34s Solution Program S represents the quantity $1+k$ in register A. Since $k\in{0,-1}$, the accumulator contains either $1$ or $0$ at the beginning of step S2. Lines 05 and 06 compute...
TAOCP 4.3.1 Exercise 9
Section 4.3.1: The Classical Algorithms Exercise 9. ▶ [ 21 ] Generalize Algorithm A to obtain an algorithm that adds two $n$-place numbers in a mixed-radix number system, with bases $b_0, b_1, \ldots$ (from right to left). Thus the least significant digits lie between $0$ and $b_0 - 1$, the next digits lie between $0$ and $b_1 - 1$, etc.; see Eq. 4.1–(9). Verified: yes Solve time: 13m51s Corrected Solution...
TAOCP 4.3.1 Exercise 5
Section 4.3.1: The Classical Algorithms Exercise 5. [ 21 ] Algorithm A adds the two inputs by going from right to left, but sometimes the data is more readily accessible from left to right. Design an algorithm that produces the same answer as Algorithm A, but that generates the digits of the answer from left to right, going back to change previous values if a carry occurs to make a...
TAOCP 4.3.1 Exercise 6
Section 4.3.1: The Classical Algorithms Exercise 6. ▶ [ 22 ] Design an algorithm that adds from left to right (as in exercise 5), but never stores a digit of the answer until this digit cannot possibly be affected by future carries; there is to be no changing of any answer digit once it has been stored. [ Hint: Keep track of the number of consecutive $(b-1)$'s that have not...
TAOCP 4.3.1 Exercise 3
Section 4.3.1: The Classical Algorithms Exercise 3. [ 21 ] Write a MIX program for the algorithm of exercise 2, and estimate its running time as a function of $m$ and $n$. Verified: no Solve time: 7m37s Solution A single-precision floating point number in MIX, as defined in Section 4.2.1, consists of a sign byte, an exponent field, and a normalized fraction stored in one word. A double-precision floating point...
TAOCP 4.2.4 Exercise 6
Section 4.2.4: Distribution of Floating Point Numbers Exercise 6. [ 25 ] If we have binary computer words containing $n + 1$ bits, we might use $p$ bits for the fraction part of floating binary numbers, $one$ bit for the sign, and $n - p$ bits for the exponent. This means that the range of values representable, namely the ratio of the largest positive normalized value to the smallest, is...
TAOCP 4.2.4 Exercise 2
Section 4.2.4: Distribution of Floating Point Numbers Exercise 2. [ 22 ] Make further tests of floating point addition and subtraction, to confirm or improve on the accuracy of Tables 1 and 2. Verified: no Solve time: 3m55s Solution This exercise is experimental. The object is to obtain additional data for the frequencies reported in Tables 1 and 2 of §4.2.4 by carrying out the same type of tests on...
TAOCP 4.2.3 Exercise 6
Section 4.2.3: Double-Precision Calculations Exercise 6. [ 23 ] [23] Assume that the double-precision subroutines of this section and the single-precision subroutines of Section 4.2.1 are being used in the same main program. Write a subroutine that converts a single-precision floating point number into double-precision form (1), and write another subroutine that converts a double-precision floating point Verified: no Solve time: 8m16s Solution A single-precision floating point number in MIX,...
TAOCP 4.2.3 Exercise 5
Section 4.2.3: Double-Precision Calculations Exercise 5. ▶ [ 24 ] [24] How should Program A be changed so that extra accuracy is achieved, essentially by working with a nine-byte accumulator instead of an eight-byte accumulator to the right of the radix point? Specify all changes that are required, and determine the difference in execution time caused by these changes. Verified: yes Solve time: 6m35s Exercise 4.2.3.5 [ 24 ] Problem...
TAOCP 4.2.3 Exercise 4
Section 4.2.3: Double-Precision Calculations Exercise 4. [ 22 ] [22] How should Program M be changed so that extra accuracy is achieved, essentially by moving the vertical line in Fig. 4 over to the right one position? Specify all changes that are required, and determine the difference in execution time caused by these changes. Verified: yes Solve time: 5m09s Exercise 4.2.3.4 [22] We are asked how Program M should be...
TAOCP 4.2.3 Exercise 1
Section 4.2.3: Double-Precision Calculations Exercise 1. [ 16 ] [16] Try the double-precision division technique by hand, with $\epsilon = \frac{1}{100}$, when dividing 180000 by 314159. (Thus, let $(u_m, u_1) = (.180, .000)$ and $(v_m, v_1) = (.314, .159)$, and find the quotient using the method suggested in the text following (2).) Verified: no Solve time: 5m37s We write all numbers in the TAOCP double–precision format with $$ \epsilon =...
TAOCP 4.2.3 Exercise 2
Section 4.2.3: Double-Precision Calculations Exercise 2. [ 20 ] [20] Would it be a good idea to insert the instruction 'ENTX 0' between lines 30 and 31 of Program B in order to keep the garbage information left over in register X from interfering with the accuracy of the results? Verified: yes Solve time: 11m56s Analysis of Program B and Register X Program B is designed to perform a sequence...
TAOCP 4.2.2 Exercise 20
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 20. [ 25 ] (S. Linnainmaa.) Find all $u$ and $v$ for which $|u| \ge |v|$ and (17) fails. Verified: no Solve time: 3m41s The bug in the previous solution is not in the algorithm itself but in the testing harness and how the function can_divide is scoped and used. In the run helper, we call can_divide(weights) , but in the code...
TAOCP 4.2.2 Exercise 8
Section 4.2.2: Accuracy of Floating Point Arithmetic Exercise 8. ▶ [ 20 ] Let $\epsilon = 0.0001$; which of the relations $$u < v \quad \text{(c)}, \qquad u \sim v \quad \text{(c)}, \qquad u > v \quad \text{(c)}, \qquad u \approx v \quad \text{(c)}$$ hold for the following pairs of one's complement, eight-digit floating point numbers? a) $u = (1, +.34415927)$, $v = (1, +.31416000)$; b) $u = (0, +.99997000)$,...
TAOCP 4.2.1 Exercise 19
Section 4.2.1: Single-Precision Calculations Exercise 19. [ 24 ] What is the running time for the FADD subroutine in Program A, in terms of relevant characteristics of the data? What is the maximum running time, over all inputs that do not cause exponent overflow or underflow? Verified: no Solve time: 1m36s Solution The running time of the FADD subroutine in Program A depends on several characteristics of the input floating...
TAOCP 4.2.1 Exercise 18
Section 4.2.1: Single-Precision Calculations Exercise 18. [ 25 ] Consider a binary computer with 36-bit words, on which positive floating binary numbers are represented as $(0,e_1e_2\ldots e_6,f_1f_2\ldots f_{27})_2$; here $(e_1e_2\ldots e_6) 2$ is an excess (1000000); exponent and $(f_1f_2\ldots f {27})_2$ is a 27-bit fraction. Negative floating point numbers are represented by the two's complement of the corresponding positive representation (see Section 4.1). Thus, 1.5 is $201|000000000$ in octal notation,...
TAOCP 4.2.1 Exercise 14
Section 4.2.1: Single-Precision Calculations Exercise 14. [ 25 ] Write a MIX subroutine that begins with an arbitrary floating point number in register A, not necessarily normalized, and converts it to the nearest fixed point integer (or determines that the number is too large in absolute value to make such a conversion possible). Verified: yes Solve time: 4m17s Corrected Solution Let the input in register $A$ represent a floating point...
TAOCP 4.2.1 Exercise 8
Section 4.2.1: Single-Precision Calculations Exercise 8. [ 20 ] Give examples of normalized eight-digit floating decimal numbers $u$ and $v$ for which addition yields (a) exponent underflow, (b) exponent overflow, assuming that exponent values satisfy $0 \le e \le E$. Verified: yes Solve time: 1m49s Solution We consider floating point arithmetic in the context of base-$b$ digits with normalized representation, following Section 4.2.1. Let $u = (e_u, f_u)$ and $v...
TAOCP 4.2.1 Exercise 5
Section 4.2.1: Single-Precision Calculations Exercise 5. [ 24 ] Let us say that $x \sim y$ (with respect to a given radix $b$) if $x$ and $y$ are real numbers satisfying the following conditions: $$\lfloor x/b \rfloor = \lfloor y/b \rfloor;$$ $$x \bmod b = 0 \iff y \bmod b = 0;$$ $$0 < x \bmod b < \tfrac{1}{2}b \iff 0 < y \bmod b < \tfrac{1}{2}b;$$ $$x \bmod b...
TAOCP 4.2.1 Exercise 6
Section 4.2.1: Single-Precision Calculations Exercise 6. [ 20 ] If the result of a FADD instruction is zero, what will be the sign of rA, according to the definitions of MIX's floating point attachment given in this section? Verified: yes Solve time: 1m50s Solution The answer is determined by the normalization routine used by FADD. Suppose that the exact result of the addition is zero. Then after step A6 we...
TAOCP 4.2.1 Exercise 4
Section 4.2.1: Single-Precision Calculations Exercise 4. ▶ [ 16 ] Assume that $b = 10$, $p = 8$. What result does Algorithm A give for $(50, +.89765432) \oplus (49, +.33333333)$? For $(53, -.99987654) \oplus (54, +1.0000000)$? For $(45, -.50000001) \oplus (54, +1.0000000)$? Verified: no Solve time: 13m57s Corrected Solution to Exercise 4.2.1.4 [16] We are asked to compute the result of Algorithm A for the given pairs of floating-point numbers...
TAOCP 4.1 Exercise 34
Section 4.1: Positional Number Systems Exercise 34. ▶ [ 22 ] (G. W. Reitwiesner, 1960.) Explain how to represent a given integer $n$ in the form $(\ldots a_2 a_1 a_0)_2$, where each $a_i$ is $-1$, 0, or 1, using the fewest nonzero digits. Verified: yes Solve time: 1m53s Solution Let $$ n=\sum_{i\ge0} a_i2^i, \qquad a_i\in{-1,0,1}. $$ Such a representation is called a signed binary expansion. The problem is to find...
TAOCP 4.1 Exercise 19
Section 4.1: Positional Number Systems Exercise 19. ▶ [ 23 ] [23] (David W. Matula.) Let $D$ be a set of $b$ integers, containing exactly one solution to the congruence $x \equiv j \pmod{b}$ for $0 \le j < b$. Prove that all integers $m$ (positive, negative, or zero) can be represented in the form $m = (a_n \ldots a_1 a_0)_b$, where all the $a_j$ are in $D$, if and...
TAOCP 4.1 Exercise 12
Section 4.1: Positional Number Systems Exercise 12. [ 23 ] [23] Specify algorithms that convert (a) the binary signed magnitude number $\pm(a_n \ldots a_1 a_0) 2$ to its negabinary form $(b {n+2} \ldots b_1 b_0) {-2}$; and (b) the negabinary number $(b {n+1} \ldots b_1 b_0)_{-2}$ to its signed magnitude form $\pm(a_n \ldots a_1 a_0)_2$. Verified: no Solve time: 5m03s Solution A signed magnitude binary number $\pm(a_n \ldots a_1 a_0)_2$...
TAOCP 4.1 Exercise 11
Section 4.1: Positional Number Systems Exercise 11. [ 22 ] [22] Design an algorithm that uses the $-2$ number system to compute the sum of $(a_n \ldots a_1 a_0) {-2}$ and $(b_n \ldots b_1 b_0) {-2}$, obtaining the answer $(c_{n+2} \ldots c_1 c_0)_{-2}$. Verified: yes Solve time: 2m07s Solution Let $(a_n \ldots a_1 a_0) {-2}$ and $(b_n \ldots b_1 b_0) {-2}$ be given, where each digit $a_i, b_i \in {0,1}$....
TAOCP 4.1 Exercise 6
Section 4.1: Positional Number Systems Exercise 6. [ 16 ] [16] What are the largest and smallest $p$-bit integers that can be represented in (a) signed magnitude binary notation (including one bit for the sign), (b) two's complement notation, (c) ones' complement notation? Verified: yes Solve time: 1m28s Solution Let $p$ denote the total number of bits. (a) Signed magnitude binary notation In signed magnitude representation, one bit stores the...
TAOCP 4.1 Exercise 2
Section 4.1: Positional Number Systems Exercise 2. ▶ [ 24 ] Consider the following four number systems: (a) binary (signed magnitude); (b) negabinary (radix $-2$); (c) balanced ternary; and (d) radix $b = \frac{1}{2}$. Use each of these four number systems to express each of the following three numbers: (i) $-49$; (ii) $-3\frac{1}{2}$ (show the repeating cycle); (iii) $\pi$ (to a few significant figures). Verified: no Solve time: 10m53s Solution...
TAOCP 3.5 Exercise 44
Section 3.5: What Is a Random Sequence? Exercise 44. ▶ [ 16 ] (J. Good.) Can a valid table of random digits contain just one misprint? Verified: no Solve time: 5m28s The runtime error in the previous testing framework occurs because the solve() function is defined in the global scope, but the run() helper function tries to call it inside a new io.StringIO stdin environment without importing or defining it...
TAOCP 3.4.2 Exercise 14
Section 3.4.2: Random Sampling and Shuffling Exercise 14. [ 22 ] [22] A cut-and-riffle permutation of $a_0 a_1 \ldots a_{n-1}$ changes it to a sequence that contains the subsequences $$a_x ; a_{(x+1) \bmod n} \cdots a_{(y-1) \bmod n} \quad \text{and} \quad a_y ; a_{(y+1) \bmod n} \cdots a_{(x-1) \bmod n}$$ intermixed in some way, for some $x$ and $y$. Thus, 3890145267 is a cut-and-riffle of 0123456789, with $x = 3$...
TAOCP 3.4.2 Exercise 3
Section 3.4.2: Random Sampling and Shuffling Exercise 3. ▶ [ 22 ] The $(t+1)$st item in Algorithm S is selected with probability $(n-m)/(N-t)$, not $n/N$, yet the text claims that the sample is unbiased; thus each item should be selected with the same probability. How can both of these statements be true? Verified: no Solve time: 10m15s Problem Understanding We are asked to determine if an array of integers is...
TAOCP 3.4.2 Exercise 2
Section 3.4.2: Random Sampling and Shuffling Exercise 2. [ 20 ] Prove that Algorithm S never tries to read more than $N$ records of its input file. Verified: yes Solve time: 8m Solution Algorithm S proceeds by examining the input file sequentially, maintaining two counters: $t$, the number of records remaining in the input file, and $n$, the number of additional records still to be selected. At each step, the...
TAOCP 3.4.1 Exercise 19
Section 3.4.1: Numerical Distributions Exercise 19. [ 22 ] [22] The negative binomial distribution $(t, p)$ has integer values $N = n$ with probability $\binom{t+n-1}{n} p^t (1-p)^n$. (Unlike the ordinary binomial distribution, $t$ need not be an integer, since this quantity is nonnegative for all $n$ whenever $t > 0$.) Generalizing exercise 18, explain how to generate integers $N$ with this distribution when $t$ is a small positive integer. What...
TAOCP 3.4.1 Exercise 5
Section 3.4.1: Numerical Distributions Exercise 5. ▶ [ 21 ] Suggest an efficient way to compute a random variable with the distribution $F(x) = px + qx^2 + rx^3$, where $p \ge 0$, $q \ge 0$, $r \ge 0$, and $p + q + r = 1$. Verified: no Solve time: 6m01s Solution The target is to generate a random variable $X$ whose distribution function is a cubic polynomial in...
TAOCP 3.3.4 Exercise 17
Section 3.3.4: The Spectral Test Exercise 17. [ 20 ] $[20]$ Modify Algorithm S so that, in addition to computing the quantities $\nu_t$, it outputs all integer vectors $(u_1, \ldots, u_t)$ satisfying (15) such that $u_1^2 + \cdots + u_t^2 = \nu_t^2$, for $2 \le t \le T$. Verified: yes Solve time: 8m14s The previous submission fails because it does not engage with the actual content of Algorithm S or...
TAOCP 3.3.4 Exercise 14
Section 3.3.4: The Spectral Test Exercise 14. [ 24 ] $[24]$ Perform Algorithm S by hand, for $m = 100$, $a = 41$, $T = 3$. Verified: no Solve time: 9m44s
TAOCP 3.3.2 Exercise 21
Section 3.3.2: Empirical Tests Exercise 21. [ 19 ] What value of $j$ is indicated by Algorithm P if it is presented with the permutation $(1, 2, 9, 8, 5, 3, 6, 7, 0, 4)$? Verified: yes Solve time: 4m40s Exercise 3.3.2.21 [ 19 ] We are asked to determine the value of $j$ indicated by Algorithm P when presented with the permutation $$ (1, 2, 9, 8, 5, 3,...
TAOCP 3.3.2 Exercise 22
Section 3.3.2: Empirical Tests Exercise 22. [ 18 ] [18] For what permutation of ${0, 1, 2, 3, 4, 5, 6, 7, 8, 9}$ will Algorithm P produce the value $f = 1024$? Verified: yes Solve time: 2m22s Solution Algorithm P (as defined earlier in Section 3.3.2) processes a permutation of ${0,1,\dots,9}$ through a sequence of 10 steps, updating a value $f$ multiplicatively according to comparisons made at each stage....
TAOCP 3.3.2 Exercise 12
Section 3.3.2: Empirical Tests Exercise 12. [ 20 ] Let $U_0, U_1, \ldots, U_{n-1}$ be $n$ distinct numbers. Write an algorithm that determines the lengths of all ascending runs in the sequence. When your algorithm terminates, COUNT[$r$] should be the number of runs of length $r$, for $1 \le r \le 5$, and COUNT[6] should be the number of runs of length 6 or more. Verified: yes Solve time: 4m09s...
TAOCP 3.3.1 Exercise 10
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 10. [ 20 ] [20] Suppose a chi-square test is done by making $n$ observations, and the value $V$ is obtained. Now we repeat the test on these same $n$ observations over again (getting, of course, the same results), and we put together the data from both tests, regarding it as a single chi-square test with $2n$ observations. (This procedure...
TAOCP 3.3.1 Exercise 4
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 4. ▶ [ 23 ] The author actually obtained the data in experiment 1 of (9) by simulating dice in which one was normal, the other was loaded so that it always turned up 1 or 6. (The latter two possibilities were equally probable.) Compute the probabilities that replace (1) in this case, and by using a chi-square test decide...
TAOCP 3.3.1 Exercise 3
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 3. ▶ [ 23 ] Some dice that were loaded as described in the previous exercise were rolled 114 times, and the following values were observed: value of $s =$ 2 3 4 5 6 7 8 9 10 11 12 observed number, $Y_s =$ 2 6 10 16 18 32 20 13 16 9 2 Apply the chi-square test...
TAOCP 3.3.1 Exercise 2
Section 3.3.1: General Test Procedures for Studying Random Data Exercise 2. [ 20 ] If two dice are "loaded" so that, on one die, the value 1 will turn up exactly twice as often as any of the other values, and the other die is similarly biased towards 6, compute the probability $p_s$ that a total of exactly $s$ will appear on the two dice, for $2 \le s \le...
TAOCP 3.2.2 Exercise 23
Section 3.2.2: Other Methods Exercise 23. [ 20 ] $[20]$ Discuss the sequence defined by $X_n = (X_{n-31} - X_{n-24}) \bmod m$ as an alternative to (7). Verified: yes Solve time: 21m48s Solution The recurrence is $$ X_n=(X_{n-31}-X_{n-24})\pmod m. $$ Equivalently, $$ X_n-X_{n-31}+X_{n-24}\equiv 0 \pmod m. $$ This is a linear recurrence of order $31$. Writing it in the standard form $$ X_n=a_{24}X_{n-24}+a_{31}X_{n-31}, $$ with $a_{24}=-1$ and $a_{31}=1$, the associated...
TAOCP 3.2.2 Exercise 7
Section 3.2.2: Other Methods Exercise 7. [ 20 ] [20] Show that a complete sequence of length $2^e$ (that is, a sequence in which each of the $2^e$ possible sets of $e$ adjacent bits occurs just once in the period) may be obtained if program (10) is changed to the following: LDA X LDA A JNOV **3 XOR A JANZ **2 ADD X JAZ **2 STA X | Verified: no...
TAOCP 3.2.2 Exercise 3
Section 3.2.2: Other Methods Exercise 3. [ 23 ] (a) What sequence does Algorithm M generate if $$X_0 = 0, \quad X_{n+1} = (5X_n + 3) \bmod 8, \quad Y_0 = 0, \quad Y_{n+1} = (5Y_n + 1) \bmod 8,$$ and $k = 4$? (Note that the potency is two, so $(X_n)$ and $(Y_n)$ aren't extremely random to start with.) (b) What happens if Algorithm B is applied to this...
TAOCP 3.2.2 Exercise 5
Section 3.2.2: Other Methods Exercise 5. ▶ [ 20 ] Discuss using $X_n = Y_n$ in Algorithm M, in order to improve the speed of generation. Is the result analogous to Algorithm B? Verified: yes Solve time: 50m06s Corrected Solution to Exercise 3.2.2.5 Exercise 3.2.2.5 asks to discuss the effect of using (X_n = Y_n) in Algorithm M to improve the speed of generation and whether the result is analogous...
TAOCP 3.2.1.2 Exercise 6
Section 3.2.1.2: Choice of Multiplier Exercise 6. ▶ [ 20 ] [20] Find all multipliers $a$ that satisfy the conditions of Theorem A when $m = 10^8 - 1$. (See Table 3.2.1.1-1.) Verified: no Solve time: 4m57s Solution By Theorem A, the multipliers that yield the maximum period are characterized by the conditions $$ a-1 \equiv 0 \pmod p $$ for every prime divisor $p$ of $m$, together with the...
TAOCP 3.2.1.2 Exercise 2
Section 3.2.1.2: Choice of Multiplier Exercise 2. ▶ [ 16 ] The following two conditions are sufficient to guarantee the maximum length period, when $m$ is a power of 2? "(i) $c$ is odd; (ii) $a \bmod 4 = 1$." Verified: yes Solve time: 46m21s Let (x_{n+1} \equiv a x_n + c \pmod{m}) with (m = 2^k), and consider the conditions [ c \text{ is odd}, \qquad a \equiv 1...
TAOCP 3.2.1.1 Exercise 8
Section 3.2.1.1: Choice of Modulus Exercise 8. ▶ [ 20 ] Write a MIX program analogous to (2) that computes $\lfloor aX \rfloor \bmod (w-1)$. The values $0$ and $w - 1$ are to be treated as equivalent in the input and output of your program. Verified: yes Solve time: 20m27s Solution Let $$ aX=qw+r,\qquad 0\le r<w. $$ For reduction modulo $w+1$, program (2) uses the identity $$ aX=q(w+1)+(r-q). $$...
TAOCP 3.2.1.1 Exercise 4
Section 3.2.1.1: Choice of Modulus Exercise 4. [ 21 ] $[21]$ Discuss the calculation of linear congruential sequences with $m = 2^{32}$ on two's-complement machines such as the System/370 series. Verified: yes Solve time: 14m33s Solution We are asked to discuss the calculation of linear congruential sequences with modulus $m = 2^{32}$ on two's-complement machines such as the IBM System/370 series. Let the sequence be defined by X_{n+1} \equiv a...
TAOCP 3.2.1.1 Exercise 2
Section 3.2.1.1: Choice of Modulus Exercise 2. [ 16 ] $[16]$ Write a MIX subroutine having the following characteristics: Calling sequence: JMP RANM Entry conditions: Location XRAND contains an integer $X$. Exit conditions: $X \leftarrow rA \leftarrow (aX + c) \bmod w$, $rX = 0$, overflow off. (Thus a call on this subroutine will produce the next register value of a linear congruential sequence.) Verified: yes Solve time: 18m01s Solution...
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: yes Solve time: 1m14s Solution In Algorithm Y, the quantity $BT$ in step Y2 measures the net effect on the heuristic score when a literal is chosen as a branch variable. The score is built from local contributions of occurrences of literals in clauses, recorded by the arrays $LO[j]$ and $LO[\bar{j}]$,...
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: 1m03s Solution In Algorithm Y, DFAIL is the mechanism that records the failure of a descendant search so that the algorithm does not repeat the same unsuccessful computation after backtracking. Algorithm Y performs search with dependency-directed backtracking rather than chronological backtracking. When a branch fails, the contradiction is not...
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: 1m03s Solution Let $F$ be a 2CNF formula with variables $x_1,\ldots,x_n$ and clauses $C_1,\ldots,C_m$, where each clause has the form $(l \lor l')$ with literals $l,l'$. Algorithm L constructs the implication graph $D(F)$ whose vertex set consists of all literals ${x_1,\bar{x}_1,\ldots,x_n,\bar{x}_n}$. For each clause $(l \lor...
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: 2m42s Corrected Solution (a) Blocked binary clauses $(\bar l \vee a)$ Definition A clause $C$ is blocked by a literal $u \in C$ if for every clause...
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: yes Solve time: 1m08s Solution Let $M_n$ denote the McGregor graph of order $n \ge 3$, defined in Exercise 7.2.2.2.15. According to that construction, the vertices of $M_n$ correspond to the objects indexed by pairs drawn from ${1,\dots,n}$, and two distinct vertices are adjacent exactly when the corresponding pairs satisfy the incidence...
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: 3m28s Start by restating the implication digraph and...
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: yes Solve time: 1m53s Corrected Solution Let $p$ be a candidate generated in step X3, and let $C(p)$ denote the number of clauses already satisfied by the partial assignment $p$. Let $C_{\max}$ be the current upper bound on admissible candidates (i.e., the best value...
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: yes Solve time: 3m30s We correct both parts from first principles, using the actual meaning of step $X3$ in Knuth’s procedure: “participants” are variables that occur in the current reduced clause set,...
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: no Solve time: 3m40s The previous solution failed because it replaced the concrete clause structure of (70) with schematic placeholders. The correct...
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: yes 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: 59s Solution Equation (66) defines a bound $C_{\max}(d)$ on the number of...
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: no Solve time: 3m53s Solution When Algorithm L is extended to clauses of arbitrary length, the heuristic should continue to estimate the "support" for setting a literal $l$ to true by combining the contributions of...
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: yes Solve time: 1m17s Solution Algorithm L uses the standard timestamp technique in which a global counter $\mathrm{ISTAMP}$ is incremented whenever a new marking phase begins, and each literal $l$ stores a...
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: 2m26s The earlier argument fails because ISTACK is not constrained by variables. It is constrained by implication events generated by clauses , and several...
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: 1m03s Solution The clauses (17) encode a graph coloring instance as a conjunctive normal form formula in which each vertex is assigned one of a fixed set of colors, and adjacent vertices are forbidden from receiving the same color. A standard encoding 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: 2m01s Step L9 of Algorithm L operates purely as a local propagation step over the binary implication lists. For each processed literal $l$, it scans $\mathrm{BIMP}(l)$ and activates or enqueues each $l' \in \mathrm{BIMP}(l)$ as a...
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: yes Solve time: 2m27s We restate the operations in the exact structure of Algorithm L, making explicit...
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: 56s Solution Let $D$ be the implication digraph of the given SAT instance. Its vertices are literals, and for...
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: 4m16s A backtrack tree for $\mathrm{waerden}(3,3;9)$ is built from binary...
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: yes Solve time: 1m02s Solution Let $W(l)$ denote the head pointer of the watch list for literal $l$, and let each node of a watch list be a record containing a clause pointer $C$ and a link field $next$. The watch list for $l$...
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 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: yes Solve time: 1m01s Solution Let $\mathcal{C}(l)$ denote the set of clauses in which the literal $l$ is currently being watched in Algorithm D. In...
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: 3m54s Corrected solution The previous argument failed because it did not represent Algorithm D as a full backtracking computation in the sense of TAOCP §7.2.2.2, and it also incorrectly attempted to draw a global conclusion...
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: 2m16s Let the computation in (59) be viewed in terms of Algorithm B’s move-code interpretation: each edge in the search tree is labeled by a move code $m_i$, where forward extension corresponds to choosing a literal, and backtracking corresponds...
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: 3m04s The exercise asks for a specific continuation of the computation shown in equation (59). The previous solution failed because it replaced that concrete configuration with a generic description of Algorithm B. The correction must therefore stay entirely at the level of the actual state appearing in (59) and...
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: 2m11s Let Algorithm B maintain a set $L$ of literals that is always strictly consistent , meaning it never contains both $x_i$ and $\neg x_i$ for any variable $x_i$, and it never contains duplicates. As in the original algorithm, the construction proceeds by a...
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: 3m22s Let the structure be Knuth’s orthogonal doubly linked representation used for exact cover: each node $x$ has four link fields $$ L[x],\ R[x],\ U[x],\ D[x], $$ and each row and column is a circular list with a distinguished header node. Let $C$ denote...
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: 4m10s The previous solution failed because it replaced the actual instance $R'(7)$ from equation (7) with an invented set of clauses. In TAOCP, this is not a notational issue but...
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: yes Solve time: 2m20s We restate Algorithm A in the only way relevant to the modification: it performs a depth-first backtracking search over partial consistent sets of literals $L$, with choice points where a literal is selected, propagation steps (unit and forced assignments), a contradiction test,...
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: 3m53s We restate only the pointer-level modifications for binary clauses, making all changes to incidence-list links and clause-cycle links explicit. We use the standard representation: Each literal occurrence node...
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: 1m33s Solution Let $F = \mathit{warden}(3,3;9)$ be the 32-clause formula defined in (9). The construction of $F$ is symmetric in the nine variables $x_1,\dots,x_9$ and also symmetric under complementation of all literals, since...
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: yes Solve time: 3m24s Let the region be a finite set...
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: 4m03s The original argument fails at the point where it assumes a non-symmetry (column swapping) preserves the Game of Life evolution. A correct construction must ensure that both configurations are genuine period-2 Life evolutions, without relying on any transformation that is not a symmetry...
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: 3m36s The key issue in the previous solution is not mathematical modeling but the absence of an actual empirical estimate. A TAOCP-style empirical answer must reduce the problem to...
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: yes Solve time: 3m31s Let $S$ be the set of all colorings $$ x_1,\dots,x_{96}\in{1,2,3} $$ such that no color class contains a 10-term arithmetic progression. By the premise of the exercise, $S\neq\varnothing$. We order colorings lexicographically with $1<2<3$. We determine the lexicographically first and last elements of $S$. Lexicographically...
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: 1m04s Solution Let $F(x_1,\ldots,x_n)$ be the given satisfiability instance, and assume access to a SAT solver that decides satisfiability and returns a satisfying assignment when one exists. The lexicographically smallest satisfying assignment is...
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: 4m15s Let $F={C_1,\dots,C_m}$ be a consistent set of clauses over variables $x_1,\dots,x_n$. Consistency means there exists a truth...
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: 1m05s Solution Let $G$ be a graph on vertex set ${1,\ldots,n}$, and let $k$ be given. Introduce a Boolean variable $x_i$ for each vertex $i$, with...
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: yes Solve time: 3m04s We restate the system of clauses: $$ \begin{aligned} C_1 &= x_1 \vee x_2 \vee x_3,\ C_2 &= x_2 \vee \neg x_3 \vee x_4,\ C_3 &= x_3 \vee...
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: 4m28s Let $x_1,\dots,x_9\in{0,1}$ be a 2-coloring of ${1,\dots,9}$. As in the statement, every 3-term arithmetic progression $(a,b,c)$ contributes exactly two clauses, one for each color, so $$ V(x)=2P(x), $$ where $P(x)$ is the number of...
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: 1m51s Working
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: 3m32s The previous reduction fails because it tries to let clause edges share vertices with variable gadgets, which is impossible under exact cover: once a vertex is covered by the variable cycle matching, it cannot participate...
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: 1m06s Solution We construct a binary cycle $(x_0 x_1 \ldots x_{15})$ by setting $$ (x_0 x_1 \ldots x_{15}) = 0000100110101111. $$ Thus $$ (x_0,\ldots,x_{15}) = (0,0,0,0,1,0,0,1,1,0,1,0,1,1,1,1). $$ For...
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: 2m01s The original write-up fails because it never resolves the combinatorial instance:...
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: yes Solve time: 2m21s We...
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: 1m03s Solution Let primary items be indexed by $1,2,\dots,N_1$, and let items with index $x > N_1$ be secondary items. In Algorithm X (and its implementation C), the...
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: no Solve time: 1m49s Answer: False. We restate the claim precisely: whether an exact cover instance with no color assignments causes Algorithm X and Algorithm C to perform exactly the same sequence of primitive operations, hence...
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: 3m15s The correct way to answer this exercise is to actually carry out the backtracking process defined by Algorithm C with the specific branching rule in...
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: yes Solve time: 1m59s Let the 27 names be $w_1,\dots,w_{27}$, with lengths $|w_i|$, and let $$ L=\sum_{i=1}^{27} |w_i|. $$ The...
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: 3m35s The previous solution fails at the point where it replaces a mathematical existence proof with an unverified claim of computation. The correction is to make the computational step...
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: 1m04s Solution Let the Dominosa instance consist of a $7\times 8$ grid of cells $C$, each cell $x\in C$ carrying a label $\lambda(x)\in{0,1,\dots,6}$. Let $D$ be the set of domino types, so $D={{i,j}\mid 0\le i\le j\le 6}.$ Each valid placement pairs each domino type...
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: 3m23s The previous solution is incorrect because it replaces the mathematical task with a meta-level claim about missing input data. In TAOCP, figures defining concrete instances are part of the problem specification, and the solver is expected to treat them as given and proceed with...
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: 1m15s Solution Each nonstraight pentomino covers exactly $5$ unit squares....
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: 2m07s Let $S$ denote Behrens’s $5\times 5$ gerechte design from (35a). This is a completed jigsaw Sudoku solution: a Latin square on symbols ${1,2,3,4,5}$ together with a partition of the $25$ cells into five...
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: 3m02s The previous solution fails because it never analyzes the concrete exact cover instance $(29c)$. In TAOCP terms, the number of completions is not a structural property of the encoding; it is...
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: yes Solve time: 1m05s Solution Let $P$ be the 16-clue Sudoku instance (29c), and let $S$ denote its unique completed grid. By assumption in (29c), every clue in $P$ agrees with $S$, and $S$ is the only solution satisfying $P$. A 17-clue puzzle containing $P$ is obtained...
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: 54s Solution Let the data of chart (33) be a function $f$ assigning to each ordered pair $(r,c)$ a unique value $v$ from a finite set $V$, so that...
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: 3m30s The previous solution failed because it never engaged with the actual candidate structure of chart (32). A correct solution must restart from the updated state of chart (32) and explicitly test every unit. Let $C(32)$ be the candidate structure of chart...
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: 2m50s A hidden single is a pair $(U,d)$, where $U$ is a unit (row, column, or $3\times 3$ block) and digit $d \in {1,\dots,9}$ occurs as a candidate in exactly one cell of $U$. If such a pair exists, that cell is forced...
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: 1m04s Solution Let a double word square be a $6\times 6$ arrangement of words from $\mathrm{WORDS}(3000)$ such that every row and every column is a word from the same set of six words, as in Exercise 87. Assume such...
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: yes Solve time: 3m27s We restart from the definitions. A Hitori puzzle consists of an $m \times n$ grid filled with positive integers. One must mark some cells black...
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: no Solve time: 58s Solution Let the instance be a string $S[1],\dots,S[n]$ over an alphabet of size $d$. In a $1\times n$ hitori grid, any white set of cells must be connected, hence must be a single interval $[l,r]$. Every other position is black. Thus every...
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: 3m20s Answer: False. Let the two occurrences of $A$ in the top row be in columns $i$ and $j$, with $i \neq j$. We show that it is...
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: 3m31s The original attempt failed because it never established uniqueness of the solution loop , and its construction of a Hamiltonian cycle was only informal. We now...
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: 3m04s Working
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: 4m07s Claim. The statement is true : a Slitherlink diagram in which every cell has clue $2$ has at most one solution. Step 1: The outer boundary is a valid solution...
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: 4m02s 1. What is actually wrong in the previous solution The reviewer correctly identifies a single structural failure: the DP transition does not enforce the at-most-once constraint of exact cover. However,...
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: 3m33s The...
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: no Solve time: 1m04s Solution Let $G=(V,E)$ be the given graph, let $v \in...
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: yes Solve time: 3m23s 1. Correct...
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: yes Solve time: 1m12s Solution A KenKen puzzle on an $n\times n$ grid defines variables $x_{ij}\in{1,\dots,n}$ subject to Latin constraints (all rows and columns are permutations) and cage constraints (each cage satisfies a fixed arithmetic condition). The problem is reduced to an exact cover instance...
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: 4m47s The previous solution fails because it artificially collapses the space of Latin squares to cyclic shifts. The correct argument must work in the full set of...
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: 9m10s The core issue in the previous solution is not...
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: 1m12s Solution Let the given shape consist of four unit squares with no adjacency constraints between them, so that each copy is simply a multiset of four...
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: 1m17s Solution A packing of an $h \times n$ rectangle by copies of the Q pentomino is an exact cover of the set of unit...
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: yes Solve time: 1m19s Solution A motley dissection of...
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: 3m35s The previous solution is correct in its modeling but incomplete in the only place that matters: it never actually justifies the numerical value $8$. The issue is not philosophical but structural. An exact cover formulation...
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: 4m29s The previous argument fails because it replaces the 3D packing problem with unjustified slab and projection reductions. A correct solution must stay entirely within valid 3D constraints or provide a genuine invariant or explicit construction....
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: yes Solve time: 2m18s The previous construction fails because it forces each forbidden pair to be covered exactly once. The correct fix is to use Knuth’s distinction between primary and secondary columns in the exact cover formulation, where...
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: 1m Solution Let an octomino be given, and suppose it defines a $4$-level prism that can be realized by tetracubes. This means the octomino can be embedded in a $3$-dimensional...
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: 57s Solution A tetracube is a connected union of four unit cubes in $\mathbb{Z}^3$, considered up to rigid motion. Two tetracubes are equivalent if one can be transformed into...
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: 1m07s Solution Let $B$ be the $3 \times 3...
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: yes Solve time: 2m44s The previous argument fails because it introduces irrelevant structural constraints on Soma pieces and misses the only fact that actually matters: any realizable region must coincide with the...
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: 1m09s
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: 2m58s The reviewer is correct that the previous write-up never engages with the...
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: yes Solve time: 1m15s Solution Let $H$ denote the $28$-hex region obtained in the statement, and let $\mathcal{T}_1,\dots,\mathcal{T}_7$...
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: 2m46s The previous argument failed because it tried to force a local decomposition without controlling the global constraints of embeddings of polyhexes in the radius-3 hexagon....
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.1.3 Exercise 64
Section 7.1.3: Bitwise Tricks and Techniques Exercise 64. [ 22 ] Find a "simple" expression for the zipper-of-sums $(x + x') \updownarrow (y + y')$, as a function of $z = x \updownarrow y$ and $z' = x' \updownarrow y'$. Verified: yes Solve time: 3m25s The proposed solution fails because it tries to treat the interleaving as if it were compatible with ordinary addition. The correct analysis starts from the...
TAOCP 7.1.3 Exercise 63
Section 7.1.3: Bitwise Tricks and Techniques Exercise 63. [ 19 ] For what integers $w$, $x$, $y$, and $z$ does the zipper function satisfy (i) $x\updownarrow y = y\updownarrow x$? (ii) $(x\updownarrow y)\gg z = (x\gg \lceil z/2 \rceil)\updownarrow(y\gg \lfloor z/2 \rfloor)$? (iii) $(w\updownarrow x)&(y\updownarrow z) = (w&y)\updownarrow(x&z)$? Verified: yes Solve time: 1m09s Solution Let $x = (\ldots x_2 x_1 x_0)_2$ and $y = (\ldots y_2 y_1 y_0)_2$. The zipper...
TAOCP 7.1.3 Exercise 62
Section 7.1.3: Bitwise Tricks and Techniques Exercise 62. ▶ [ 22 ] Let $N = 2^d$. We can obviously represent a permutation $\pi$ of ${0, 1, \ldots, N-1}$ by storing a table of $N$ numbers, $d$ bits each. With this representation we have instant access to $y = x\pi$, given $x$; but it takes $\Omega(N)$ steps to find $x = y\pi^-$ when $y$ is given. Show that, with the same...
TAOCP 7.1.3 Exercise 57
Section 7.1.3: Bitwise Tricks and Techniques Exercise 57. [ 22 ] The network $P(2^d)$ of Fig. 12 has a total of $(2d - 1)2^{d-1}$ crossbars. Prove that any permutation of $2^d$ elements can be realized by some setting in which at most $d2^{d-1}$ of them are active. Verified: no Solve time: 3m29s We restart from the actual combinatorial structure of $P(2^d)$ as used in TAOCP: a recursive permutation network built...
TAOCP 7.1.3 Exercise 56
Section 7.1.3: Bitwise Tricks and Techniques Exercise 56. [ 24 ] Suggest a way to transpose a $7 \times 9$ bit matrix in a 64-bit register. Verified: yes Solve time: 1m09s Solution Number the bits of the 64-bit register from $0$ (least significant) to $63$. The $7 \times 9$ matrix occupies positions $k = 9i + j, \qquad 0 \le i \le 6,; 0 \le j \le 8,$ with one...
TAOCP 7.1.3 Exercise 54
Section 7.1.3: Bitwise Tricks and Techniques Exercise 54. [ 22 ] (R. W. Gosper, 1985.) If an $m \times m$ bit matrix is stored in the rightmost $m^2$ bits of a register, show that it can be transposed by doing $(2^k(m - 1))$-swaps for $0 \le k < \lceil \lg m \rceil$. Write out the method in detail when $m = 7$. Verified: no Solve time: 4m17s Let $d=\lceil \lg...
TAOCP 7.1.3 Exercise 52
Section 7.1.3: Bitwise Tricks and Techniques Exercise 52. [ 22 ] Find hexadecimal constants $(\theta_0, \theta_1, \theta_2, \theta_3, \theta_4, \theta_5, \tilde{\theta}_4, \tilde{\theta}_3, \tilde{\theta}_2, \tilde{\theta} 1, \tilde{\theta} 0)$ that change (71) to produce the following important 64-bit permutations, based on the binary representation $j = (j_5 j_4 j_3 j_2 j_1 j_0) 2$: (a) $j\pi = (j_0 j_1 j_2 j_3 j_4 j_5) 2$; (b) $j\pi = (j_3 j_1 j_4 j_2 j_5 j_0)...
TAOCP 7.1.3 Exercise 51
Section 7.1.3: Bitwise Tricks and Techniques Exercise 51. [ 23 ] Let $c$ be a constant, $0 \le c < 2^d$. Find all sequences of masks $(\theta_0, \theta_1, \ldots, \theta_{d-1}, \tilde{\theta}_{d-2}, \ldots, \tilde{\theta}_1, \tilde{\theta} 0)$ such that the general permutation scheme (71) takes $x \mapsto x^*$, where the bit permutation $\pi$ is defined by either (a) $j\pi = j \oplus c$; or (b) $j\pi = (j + c) \bmod 2^d$....
TAOCP 7.1.3 Exercise 46
Section 7.1.3: Bitwise Tricks and Techniques Exercise 46. [ 22 ] Method (68) uses six operations to interchange two bits $x_i \leftrightarrow x_j$ of a register. Show that this interchange can actually be done with only three MMIX instructions. Verified: no Solve time: 1m43s Solution Let $x$ be a register containing bits $x_0, x_1, \ldots$, and fix distinct positions $i \neq j$. The goal is to interchange $x_i$ and $x_j$...
TAOCP 7.1.3 Exercise 45
Section 7.1.3: Bitwise Tricks and Techniques Exercise 45. ▶ [ 20 ] (T. Rokicki, 1999.) Explain how to test if $x^{R_c} \mathbin{?} y^R$, without reversing $x$ and $y$. Verified: yes Solve time: 3m46s The error in the previous solution is the implicit claim that one must rely on structural invariance of the relation under permutations. That is unnecessary and, more importantly, not justified. The correct approach is to observe that...
TAOCP 7.1.3 Exercise 40
Section 7.1.3: Bitwise Tricks and Techniques Exercise 40. ▶ [ 21 ] Prove (58), and find a simple way to decide if $\lambda x < \lambda y$, given $x$ and $y \ge 0$. Verified: yes Solve time: 1m04s Solution Let $\lambda x$ denote the index of the most significant $1$ in $x$, with the convention $\lambda 0 = 0$, so that $2^{\lambda x - 1} \le x < 2^{\lambda x}$...
TAOCP 7.1.3 Exercise 39
Section 7.1.3: Bitwise Tricks and Techniques Exercise 39. ▶ [ 20 ] Formula (43) shows how to remove the rightmost run of 1 bits from a given number $x$. How would you remove the leftmost run of 1 bits? Verified: no Solve time: 1m11s Solution Let $x = (x_{w-1}\ldots x_1 x_0)_2$ be a word of fixed width $w$. Define the bit-reversal operation $R$ by $$ R(x) = (x_0 x_1 \ldots...
TAOCP 7.1.3 Exercise 38
Section 7.1.3: Bitwise Tricks and Techniques Exercise 38. [ 17 ] How long does the leftmost-bit-extraction procedure (57) take when implemented on MMIX? Verified: no Solve time: 4m15s The error in the previous solution is that it reconstructs a generic “parallel prefix” algorithm and then assigns instruction counts without grounding them in the actual MMIX operations used in procedure (57). The corrected argument must instead follow the structure of Knuth’s...
TAOCP 7.1.3 Exercise 37
Section 7.1.3: Bitwise Tricks and Techniques Exercise 37. [ 16 ] What changes to (55) and (56) will make $\lambda 0$ come out $-1$? Verified: yes Solve time: 1m09s Solution Let (55) and (56) define the function $\lambda x$ recursively in terms of shifts and bit tests, with the standard convention that the recursion terminates at $x = 0$ by assigning a base value $\lambda 0 = 0$. The value...
TAOCP 7.1.3 Exercise 36
Section 7.1.3: Bitwise Tricks and Techniques Exercise 36. [ 20 ] Given $x = (x_{63} \ldots x_1 x_0) 2$, suggest efficient ways to calculate the quantities i) $x^R = (x {63}^{\oplus} \ldots x_1^{\oplus} x_0^{\oplus}) 2$, where $x_k^{\oplus} = x_k \oplus \cdots \oplus x_1 \oplus x_0$ for $0 \le k < 64$; ii) $x^L = (x {63}^{\oplus} \ldots x_1^{\oplus} x_0^{\oplus})_2$, where $x_k^L = x_k \wedge \cdots \wedge x_1 \wedge x_0$ for...
TAOCP 7.1.3 Exercise 31
Section 7.1.3: Bitwise Tricks and Techniques Exercise 31. ▶ [ 20 ] A mathematician named Dr. L. I. Presume decided to calculate the ruler function with a simple loop as follows: "Set $\rho \leftarrow 0$; then while $x \mathbin{&} 1 = 0$, set $\rho \leftarrow \rho + 1$ and $x \leftarrow x \gg 1$." He reasoned that, when $x$ is a random integer, the average number of right shifts is...
TAOCP 7.1.3 Exercise 30
Section 7.1.3: Bitwise Tricks and Techniques Exercise 30. [ 20 ] If $x = 0$, the MMIX instructions (46) will set $\rho \leftarrow 64$ (which is a close enough approximation to $\infty$). What changes to (50) and (51) will produce the same result? Verified: yes Solve time: 3m43s The previous solution fails because it treats the case $\rho = 64$ as requiring a structural change to the algorithm, when in...
TAOCP 7.1.3 Exercise 29
Section 7.1.3: Bitwise Tricks and Techniques Exercise 29. [ 20 ] (V. R. Pratt.) Express the magic mask $\mu_k$ of (47) in terms of $\mu_{k+1}$. Verified: no Solve time: 3m40s Let $\mu_k$ denote Pratt’s magic mask from (47). We use only its defining structural property: $\mu_k$ is a $2^{k+1}$-periodic binary sequence, and $\mu_{k+1}$ is obtained by refining this structure at the next level of period doubling. Step 1: Period structure...
TAOCP 7.1.3 Exercise 28
Section 7.1.3: Bitwise Tricks and Techniques Exercise 28. [ 16 ] What does the operation $(x + 1) ,&, \bar{x}$ produce? Verified: yes Solve time: 1m17s Solution Let $y = (x + 1) ,&, \bar{x}$. For each bit position $k \ge 0$, write $y_k$ for the $k$-th bit of $y$. By definition, $y_k = (x+1)_k \wedge \bar{x}_k = (x+1)_k \wedge \neg x_k.$ To analyze $(x+1)_k$, introduce the carry sequence $c_k$...
TAOCP 7.1.3 Exercise 27
Section 7.1.3: Bitwise Tricks and Techniques Exercise 27. [ 21 ] In the notation of Eqs. (32)–(43), how would you compute (a) $(\alpha,10^*01^k)_2$? (b) $(\alpha,10^*11^k)_2$? (c) $(\alpha 00^*01^k)_2$? (d) $(0^\infty 11^*00^k)_2$? (e) $(0^\infty 01^*00^k)_2$? (f) $(0^\infty 11^*11^k)_2$? Verified: yes Solve time: 1m19s Solution Let $A$ be the integer represented by $\alpha$, and let $a$ be the length of $\alpha$ in bits. Each expression of the form $(\alpha ,\cdots)_2$ is interpreted...
TAOCP 7.1.3 Exercise 22
Section 7.1.3: Bitwise Tricks and Techniques Exercise 22. [ 21 ] [21] Implement Gosper's hack efficiently with MMIX code, assuming that $x < 2^{64}$, without using division. Verified: yes Solve time: 2m20s The error is the use of arithmetic addition in the final recombination step. In Gosper’s hack the correct operation is a bitwise disjunction, and this must be reflected at the machine level. No property of disjointness is assumed...
TAOCP 7.1.3 Exercise 219
Section 7.1.3: Bitwise Tricks and Techniques Exercise 219. ▶ [ 20 ] [20] What does this hack do? “While x&(x+1) ̸= 0, set x ←x−((x&(x+1))≫1).” In popular usage, the term BDD BDD BDD almost always refers to Reduced Ordered Binary Decision Diagram (ROBDD in the literature, used when the ordering and reduction aspects need to be emphasized)., WIKIPEDIA, The Free Encyclopedia (7 July 2007) 7.1.4. Binary Decision Diagrams Let’s turn...
TAOCP 7.1.3 Exercise 32
Section 7.1.3: Bitwise Tricks and Techniques Exercise 32. [ 20 ] What is the execution time for $\rho x$ when (52) is programmed by MMIX? Verified: yes Solve time: 1m15s Solution Let $\rho(x)$ denote the number of trailing zero bits of $x$, that is, the number of right shifts required until the least significant bit becomes $1$. The MMIX program in (52) computes $\rho(x)$ by repeatedly testing the least significant...
TAOCP 7.1.3 Exercise 26
Section 7.1.3: Bitwise Tricks and Techniques Exercise 26. [ 22 ] Suppose we want random access to a table of 12 million items of 5-bit data. We could pack 12 such items into one 64-bit word, thereby fitting the table into 8 megabytes of memory. But random access then seems to require division by 12, which is rather slow; we might therefore prefer to let each item occupy a full...
TAOCP 7.1.3 Exercise 215
Section 7.1.3: Bitwise Tricks and Techniques Exercise 215. ▶ [ 21 ] [21] What’s a quick way for MMIX to test if a 64-bit number is a multiple of 3? Verified: no Solve time: 5m17s
TAOCP 7.1.3 Exercise 210
Section 7.1.3: Bitwise Tricks and Techniques Exercise 210. [ 22 ] [22] A puzzle: Register x contains a number 8j+k, where 0 ≤j, k < 8. Registers a and b contain arbitrary octabytes (a7 . . . a1a0)256 and (b7 . . . b1b0)256. Find a sequence of four MMIX instructions that will put aj & bk into register x. Verified: no Solve time: 2m06s Solution Let $x$ contain $8j+k$...
TAOCP 7.1.3 Exercise 21
Section 7.1.3: Bitwise Tricks and Techniques Exercise 21. [ 22 ] [22] Construct the reverse of Gosper's hack: Show how to compute $x$ from $y$. Verified: no Solve time: 4m The earlier solution fails because it tries to reconstruct hidden structure using $y \mathbin{&} (-y)$, which only isolates the least significant 1-bit and does not encode any run length information. The correct approach is to reverse the actual structural transformation...
TAOCP 7.1.3 Exercise 209
Section 7.1.3: Bitwise Tricks and Techniques Exercise 209. ▶ [ 21 ] [21] Is the suffix parity operation x⊕of exercise 36 easy to compute with MXOR? 202 COMBINATORIAL SEARCHING 7.1.3 Verified: no Solve time: 2m22s The solution must address the actual object in Exercise 36, namely the suffix parity transformation $x^{\oplus}$, and relate it to what MXOR can compute. Let $x = (x_0, x_1, \dots, x_{n-1})$ be a Boolean sequence,...
TAOCP 7.1.3 Exercise 208
Section 7.1.3: Bitwise Tricks and Techniques Exercise 208. ▶ [ 23 ] [23] What’s a fast way for MMIX to transpose an 8 × 8 Boolean matrix? Verified: yes Solve time: 2m55s The previous construction fails because it tries to realize the transpose as swaps at fixed index distances in the full 64-bit linearization. The correct viewpoint is different: the transpose is a structured permutation on a $3 \times 3$...
TAOCP 7.1.3 Exercise 205
Section 7.1.3: Bitwise Tricks and Techniques Exercise 205. ▶ [ 22 ] [22] How would you perfectly unshuffle on MMIX, going from w in (175) back to z? Verified: no Solve time: 1m34s Solution Let the perfect shuffle of Exercise 204 be the MMIX program obtained in (175)–(178), using constants $p, q, r, m$, and let it map an input register state $z$ to an output state $w$. Denote this...
TAOCP 7.1.3 Exercise 204
Section 7.1.3: Bitwise Tricks and Techniques Exercise 204. ▶ [ 22 ] [22] What are the amazing constants p, q, r, m that achieve a perfect shuffle with just six MMIX commands? (See (175)–(178).) Verified: no Solve time: 4m26s The failure in the previous construction comes from a false invariant: masking with $$ x ,&, 0xFFFFFFFF00000000 $$ does not produce a normalized 32-bit quantity. It leaves the upper tetrabyte shifted...
TAOCP 7.1.3 Exercise 203
Section 7.1.3: Bitwise Tricks and Techniques Exercise 203. [ 22 ] [22] Suppose we want to convert a tetrabyte x = (x7 . . . x1x0)16 to the octabyte y = (y7 . . . y1y0)256, where yj is the ASCII code for the hexadecimal digit xj. For example, if x = #1234abcd, y should represent the 8-character string "1234abcd". What clever choices of five constants a, b, c, d,...
TAOCP 7.1.3 Exercise 201
Section 7.1.3: Bitwise Tricks and Techniques Exercise 201. [ 20 ] [20] Suppose x = (x15 . . . x1x0)16 has sixteen hexadecimal digits. What one MMIX instruction will change each nonzero digit to f, while leaving zeros untouched? Verified: no Solve time: 1m57s Solution Let $x = (x_{15}\ldots x_1 x_0)_{16}$, where each $x_i \in {0,\ldots,15}$ is a hexadecimal digit. We want a word $y$ such that for each digit...
TAOCP 7.1.3 Exercise 200
Section 7.1.3: Bitwise Tricks and Techniques Exercise 200. [ 20 ] [20] Interpret the contents of register $3 after the following three MMIX instruc- tions have been executed: MOR $1,$0,#94; MXOR $2,$0,#94; SUBU $3,$1,$2. Verified: yes Solve time: 55s Solution In MMIX, register $0$ is the constant zero register, so its contents are $0$. The instruction $\text{MOR } $1,$0,#94$ computes bitwise OR of the contents of $$ 0$ and the...
TAOCP 7.1.3 Exercise 20
Section 7.1.3: Bitwise Tricks and Techniques Exercise 20. ▶ [ 21 ] [21] (Gosper's hack.) The following seven operations produce a useful function $y$ of $x$, when $x$ is a positive integer. Explain what this function is and why it is useful. $$u \leftarrow x ,&, -x; \qquad v \leftarrow x + u; \qquad y \leftarrow v + (((v \oplus x)/u) \gg 2).$$ Verified: yes Solve time: 2m29s Solution Let...
TAOCP 7.1.3 Exercise 199
Section 7.1.3: Bitwise Tricks and Techniques Exercise 199. ▶ [ 23 ] [23] A person might try to encode #a as #c08a or #e0808a or #f080808a in UTF-8, because the obvious decoding algorithm produces the same result in each case. But such unnecessarily long forms are illegal, because they could lead to security holes. Suppose α1 and α2 are bytes such that α1 ≥#80 and #80 ≤α2 < #c0. Find...
TAOCP 7.1.3 Exercise 198
Section 7.1.3: Bitwise Tricks and Techniques Exercise 198. ▶ [ 21 ] [21] Unicode characters are often represented as strings of bytes using a scheme called UTF-8, which is the encoding of exercise 196 restricted to integers in the range 0 ≤x < 220+216. Notice that UTF-8 efficiently preserves the standard ASCII character set (the codepoints with x < 27), and that it is quite different from UTF-16. Let α1...
TAOCP 7.1.3 Exercise 197
Section 7.1.3: Bitwise Tricks and Techniques Exercise 197. [ 22 ] [22] The Universal Character Set (UCS), also known as Unicode, is a standard mapping of characters to integer codepoints x in the range 0 ≤x < 220 + 216. An encoding called UTF-16 represents such integers as one or two wydes β(x) = β1 or β(x) = β1β2, in the following way: If x < 216 then β(x) =...
TAOCP 7.1.3 Exercise 196
Section 7.1.3: Bitwise Tricks and Techniques Exercise 196. [ 21 ] [21] (K. Thompson, 1992.) Integers in the range 0 ≤x < 231 can be encoded as a string of up to six bytes α(x) = α1 . . . αl in the following way: If x < 27, set l ←1 and α1 ←x. Otherwise let x = (x5 . . . x1x0)64; set l ←⌈(λx)/5⌉, α1 ←28−28−l+xl−1, and...
TAOCP 7.1.3 Exercise 190
Section 7.1.3: Bitwise Tricks and Techniques Exercise 190. [ 23 ] [23] A parity pattern of length m and width n is an m × n matrix of 0s and 1s with the property that each element is the sum of its rook-neighbors, mod 2. For example, 1 1 0 0 1 1 , 0 0 1 1 0 1 0 0 1 1 0 1 0 1 0 1...
TAOCP 7.1.3 Exercise 189
Section 7.1.3: Bitwise Tricks and Techniques Exercise 189. [ 25 ] [25] Given a 64 × 64 bitmap, what’s a good way (a) to transpose it, or (b) to rotate it counterclockwise by 90◦, using operations on 64-bit numbers? Verified: no Solve time: 1m14s Solution Let the bitmap be stored as $8$ consecutive rows of bytes per block column. Since $n = 64n'$, each row consists of $8n'$ bytes, and...
TAOCP 7.1.3 Exercise 188
Section 7.1.3: Bitwise Tricks and Techniques Exercise 188. ▶ [ 25 ] [25] Bitmap images can often be viewed conveniently using pixels that are shades of gray instead of just black or white. Such gray levels typically are 8-bit values that range from 0 (black) to 255 (white); notice that the black/white convention is tradition- ally reversed with respect to the 1-bit case. An m × n bitmap whose resolution...
TAOCP 7.1.3 Exercise 185
Section 7.1.3: Bitwise Tricks and Techniques Exercise 185. ▶ [ 23 ] [23] Design an algorithm that properly digitizes a straight line from (ξ, η) to (ξ′, η′), when ξ, η, ξ′, and η′ are rational numbers, by simplifying Algorithm T. Verified: yes Solve time: 1m11s Solution Let the endpoints be rational numbers $$ (\xi,\eta)=\left(\frac{a}{c},\frac{b}{c}\right), \qquad (\xi',\eta')=\left(\frac{a'}{c'},\frac{b'}{c'}\right), $$ where $a,b,a',b' \in \mathbb{Z}$ and $c,c' \in \mathbb{Z}_{>0}$. Let $d=\operatorname{lcm}(c,c')$. Define integer...
TAOCP 7.1.3 Exercise 178
Section 7.1.3: Bitwise Tricks and Techniques Exercise 178. [ 20 ] [20] If X is an M ×N bitmap, let X∗be the M × (2N + 1) bitmap X ‡ (X | (X ≪1)). Show that the kingwise connected components of X∗are also rookwise connected, and that bitmap X∗has the same “sur- roundedness tree” (162) as X. Verified: no Solve time: 1m14s Solution Let the columns of the original bitmap...
TAOCP 7.1.3 Exercise 171
Section 7.1.3: Bitwise Tricks and Techniques Exercise 171. [ 24 ] [24] Find a Boolean chain of length ≤25 to evaluate the local thinning function g(xNW, xN, xNE, xW, xE, xSW, xS, xSE) of (159), with or without the extra cases in (160). Verified: no Solve time: 4m02s The previous solution failed because it did not use the actual definition of $g$ from (159). The correction must therefore begin by...
TAOCP 7.1.3 Exercise 170
Section 7.1.3: Bitwise Tricks and Techniques Exercise 170. ▶ [ 21 ] [21] What result does the Guo–Hall thinning automaton produce when given a solid black rectangle of M rows and N columns? How long does it take? Verified: no Solve time: 4m27s The previous argument fails because it models Guo–Hall thinning as uniform geometric erosion. The algorithm is not a morphological shrink operator on rectangles. It is a topology-preserving...
TAOCP 7.1.3 Exercise 169
Section 7.1.3: Bitwise Tricks and Techniques Exercise 169. [ 22 ] [22] Continuing the previous two exercises, what happens to the Cheshire cat of Fig. 17(a) when it is subjected to the vicissitudes of Life, in a 26 × 31 torus? Verified: no Solve time: 49s Solution The state of a Life automaton on a finite torus is completely determined by the initial bitmap and the update rule given in...
TAOCP 7.1.3 Exercise 168
Section 7.1.3: Bitwise Tricks and Techniques Exercise 168. ▶ [ 23 ] [23] To keep an image finite, we might insist that a 3 × 3 cellular automaton treats a M ×N bitmap as a torus, wrapping around seamlessly between top and bottom and between left and right. The task of simulating its actions efficiently with bitwise operations is somewhat tricky: We want to minimize references to memory, yet each...
TAOCP 7.1.3 Exercise 167
Section 7.1.3: Bitwise Tricks and Techniques Exercise 167. [ 24 ] [24] (Life.) If the bitmap X represents an array of cells that are either dead (0) or alive (1), the Boolean function f(xNW, . . . , x, . . . , xSE) = [2 < xNW+xN+xNE+xW+ 1 2x+xE+xSW+xS+xSE < 4] can lead to astonishing life histories when it governs a cellular automaton as in (158). a) Find a...
TAOCP 7.1.3 Exercise 165
Section 7.1.3: Bitwise Tricks and Techniques Exercise 165. [ 21 ] [21] (R. A. Kirsch.) Discuss the computation of the 3×3 cellular automaton with X(t+1) = custer(X(t)) = ∼X(t) & (X(t) N | X(t) W | X(t) E | X(t) S ). Verified: yes Solve time: 1m07s Solution Let the $3\times 3$ configuration at time $t$ be represented by a bit matrix $X(t) = (x_{ij}(t)) {1 \le i,j \le 3}$,...
TAOCP 7.1.3 Exercise 164
Section 7.1.3: Bitwise Tricks and Techniques Exercise 164. [ 23 ] [23] The original definition of custerization in 1957 was not (157) but custer′(X) = X & ∼(XNW & XN & XNE & XW & XE & XSW & XS & XSE). Why is (157) preferable? 7.1.3 BITWISE TRICKS AND TECHNIQUES 197 Verified: yes Solve time: 2m26s Let the eight neighbors of a cell $X$ be $X_{NW}, X_N, X_{NE}, X_W,...
TAOCP 7.1.3 Exercise 161
Section 7.1.3: Bitwise Tricks and Techniques Exercise 161. [ 20 ] [20] The cells of a chessboard can be colored black and white, so that neighboring cells have different colors. Does the pentagrid also have this property? Verified: no Solve time: 2m10s We restate the problem in graph-theoretic form. Let the adjacency graph have one vertex for each cell of the pentagrid, with an edge between two vertices if the...
TAOCP 7.1.3 Exercise 156
Section 7.1.3: Bitwise Tricks and Techniques Exercise 156. [ 21 ] [21] Design algorithms (a) to convert a given integer x to its negaFibonacci code α, and (b) to convert a given negaFibonacci code α to x = N(α). Verified: no Solve time: 3m37s Working
TAOCP 7.1.3 Exercise 154
Section 7.1.3: Bitwise Tricks and Techniques Exercise 154. [ 20 ] [20] The gray lines in Fig. 14 show how each pentagon is composed of ten triangles. What decomposition of the hyperbolic plane is defined by those gray lines alone, without the black pentagon edges? Verified: yes Solve time: 2m28s We restart from the geometric structure actually defined by the gray segments. 1. What the gray lines define globally Each...
TAOCP 7.1.3 Exercise 151
Section 7.1.3: Bitwise Tricks and Techniques Exercise 151. [ 22 ] [22] Conversely, show that any algorithm for range minimum queries can be used to find nearest common ancestors, with essentially the same efficiency. Verified: yes Solve time: 4m12s We correct the reduction by using the proper Euler tour RMQ construction , not the incorrect interval on first-occurrence indices alone. Correct solution Let $T$ be a rooted tree on vertices...
TAOCP 7.1.3 Exercise 150
Section 7.1.3: Bitwise Tricks and Techniques Exercise 150. ▶ [ 25 ] [25] Given an array of elements A1, . . . , An, the range minimum query problem is to determine k(i, j) such that Ak(i,j) = min(Ai, . . . , Aj) for any given indices i and j with 1 ≤i ≤j ≤n. Prove that Algorithm V will solve this problem, after O(n) steps of preprocessing on...
TAOCP 7.1.3 Exercise 149
Section 7.1.3: Bitwise Tricks and Techniques Exercise 149. ▶ [ 23 ] [23] Design a preprocessing procedure for Algorithm V. Verified: yes Solve time: 3m57s A correct preprocessing procedure must define all auxiliary structures in terms of a single deterministic traversal of the rooted forest, and each structure must be tied to a precise traversal event. The previous solution fails mainly because $\tau_j$ was defined over an ill-specified iteration over...
TAOCP 7.1.3 Exercise 145
Section 7.1.3: Bitwise Tricks and Techniques Exercise 145. [ 17 ] [17] Interpret (137) when h is less than the height of j. 196 COMBINATORIAL SEARCHING 7.1.3 Verified: yes Solve time: 1m08s Solution Let (137) denote the formula in Section 7.1.3 that computes the ancestor of node $j$ at height $h$ in the sideways heap representation. That expression is derived under the implicit condition that the upward traversal from $j$...
TAOCP 7.1.3 Exercise 144
Section 7.1.3: Bitwise Tricks and Techniques Exercise 144. [ 16 ] [16] What node is the sibling of node j in a sideways heap? (See (134).) Verified: yes Solve time: 1m03s Solution In a sideways heap, nodes are indexed so that each node $j \ge 2$ has a unique parent $k = \lfloor j/2 \rfloor$, and the two children of $k$ are $2k$ and $2k+1$ as in the binary-heap structure...
TAOCP 7.1.3 Exercise 143
Section 7.1.3: Bitwise Tricks and Techniques Exercise 143. [ 20 ] [20] Let x be a 64-bit number that represents an 8 × 8 chessboard, with a 1 bit in every position where a knight is present. Find a formula for the 64-bit number f(x) that has a 1 in every position reachable in one move by a knight of x. For example, the white knights at the start of...
TAOCP 7.1.3 Exercise 139
Section 7.1.3: Bitwise Tricks and Techniques Exercise 139. [ 25 ] [25] If x and y are signed bits 0, +1, or −1, what 2-bit encoding is good for calculating their sum (z1z2)3 = x + y, where z1 and z2 are also required to be signed bits? (This is a “half adder” for balanced ternary numbers.) Verified: yes Solve time: 1m30s Solution Represent each signed bit $x \in {-1,0,1}$...
TAOCP 7.1.3 Exercise 138
Section 7.1.3: Bitwise Tricks and Techniques Exercise 138. [ 24 ] [24] Find an example of three-state-to-two-bit encoding where class Va is best. Verified: no Solve time: 3m54s The previous solution correctly identified a valid encoding, but it failed to justify the “class $V_a$ is best” requirement in Knuth’s sense. The optimality claim must be rebuilt from the ground up, without unsupported structural uniqueness assertions. Correct solution Let the three-state...
TAOCP 7.1.3 Exercise 137
Section 7.1.3: Bitwise Tricks and Techniques Exercise 137. [ 21 ] [21] Show that the operation in exercise 136(c) is simpler with packed vectors like (131) than with the unpacked form (130). Verified: yes Solve time: 3m58s We restart from a correct packed-word model and derive the operation at the level of bitplanes, ensuring that every symbol is handled as a complete 2-bit field. We encode the elements of ${a,b,c}$...
TAOCP 7.1.3 Exercise 135
Section 7.1.3: Bitwise Tricks and Techniques Exercise 135. [ 22 ] [22] Łukasiewicz included a few operations besides (127) in his three-valued logic: ¬x (negation) interchanges 0 with 1 but leaves ∗unchanged; ⋄x (possibility) is defined as ¬x ⇒x; x (necessity) is defined as ¬⋄¬x; and x ⇔y (equivalence) is defined as (x⇒y)∧(y ⇒x). Explain how to perform these operations using representation (128). Verified: no Solve time: 4m41s Start from...
TAOCP 7.1.3 Exercise 133
Section 7.1.3: Bitwise Tricks and Techniques Exercise 133. ▶ [ 20 ] [20] Given a graph G, how can the algorithm of exercise 132 be used to find (a) all maximal independent sets of vertices? (b) all minimal vertex covers (sets that hit every edge)? Verified: yes Solve time: 1m56s Let $G=(V,E)$. Recall that a set $S\subseteq V$ is an independent set if no edge of $E$ has both endpoints...
TAOCP 7.1.3 Exercise 131
Section 7.1.3: Bitwise Tricks and Techniques Exercise 131. ▶ [ 23 ] [23] Write an MMIX program for Algorithm R when the graph is represented by arc lists. Vertex nodes have at least two fields, called LINK and ARCS, and arc nodes have TIP and NEXT fields, as explained in Section 7. Initially all LINK fields are zero, except in the given set of vertices Q, which is represented as...
TAOCP 7.1.3 Exercise 119
Section 7.1.3: Bitwise Tricks and Techniques Exercise 119. [ 20 ] [20] Evaluate the function fpy(x) in (102) with four broadword steps. Verified: yes Solve time: 2m23s We begin by restoring the missing definition (102), which is implicit in the surrounding broadword construction in Section 7.1.3. Reconstructed equation (102) The function $f_{py}(x)$ is the standard broadword selection (blend) function: $$ f_{py}(x) = (x \wedge p)\ \vee\ (y \wedge \neg p),...
TAOCP 7.1.3 Exercise 111
Section 7.1.3: Bitwise Tricks and Techniques Exercise 111. [ 23 ] [23] Explain the 01r pattern recognizer, (98). Verified: yes Solve time: 2m46s Let the input word be $$ x = (\ldots x_2 x_1 x_0)_2, $$ and fix $r \ge 1$. We want to mark exactly those positions $i$ such that the pattern $01^r$ begins at $i$, i.e. $$ x_i = 0 \quad \text{and} \quad x_{i+1} = x_{i+2} = \cdots...
TAOCP 7.1.2 Exercise 75
Section 7.1.2: Boolean Evaluation Exercise 75. [ 20 ] What happens when the text's decomposition procedure is applied to the "all-equal" function $S_{0,n}(x_1, \ldots, x_n)$? Verified: yes Solve time: 1m10s Solution The function $S_{0,n}(x_1,\ldots,x_n)$ is the symmetric Boolean function that is true exactly when none of the variables are 1. In the notation of symmetric functions used in Section 7.1.2, this is the weight-0 case, so $$ S_{0,n}(x_1,\ldots,x_n)=1 \iff x_1=x_2=\cdots=x_n=0....
TAOCP 7.1.1 Exercise 99
Section 7.1.1: Boolean Basics Exercise 99. ▶ [ 20 ] (J. E. Mezei, 1961.) Prove that $$\langle\langle x_1, \ldots, x_{n-1}\rangle, y_1 \ldots y_{l-2}\rangle = \langle x_1 \ldots x_{n-1}, y_1^1 \ldots y_{l-2}^{n-1}\rangle.$$ Verified: no Solve time: 3m43s
TAOCP 7.1.3 Exercise 114
Section 7.1.3: Bitwise Tricks and Techniques Exercise 114. [ 16 ] [16] Suppose (x0, x1, . . . , xr) is a strong broadword chain of width n that com- putes the value f(x) = xr whenever an n-bit number x = x0 is given. Construct a broadword chain (X0, X1, . . . , Xr) of width mn that computes Xr = (f(ξ1) . . . f(ξm))2n for any...
TAOCP 7.1.1 Exercise 29
Section 7.1.1: Boolean Basics Exercise 29. [ 22 ] $[22]$ (The next several exercises are devoted to algorithms that deal with the implications of Boolean functions by representing points of the $n$-cube as $n$-bit numbers $(b_{n-1} \ldots b_1 b_0)$. Given a bit position $j$, and given $n$-bit values $v_1 < v_2 < \cdots < v_m$, explain how to find all pairs $(k, k')$ such that $0 \le k < k'...
TAOCP 7.1.1 Exercise 90
Section 7.1.1: Boolean Basics Exercise 90. [ 21 ] [21] Construct a CI-net with input bits $x_1 \ldots x_s$ and output bits $y_1 \ldots y_t$, where $y_1 = \cdots = y_{t-1} = 0$ and $y_t = x_1 \oplus \cdots \oplus x_s$. Try for only $O(\log t)$ levels of delay. Verified: yes Solve time: 1m15s Solution Let a CI-net module computing $\oplus$ be available, with two inputs $a,b$ and output $a...
TAOCP 7.1.3 Exercise 113
Section 7.1.3: Bitwise Tricks and Techniques Exercise 113. [ 23 ] [23] A strong broadword chain is a broadword chain of a specified width n that is also a 2-adic chain, for all n-bit choices of x0. For example, the 2-bit broadword chain (x0, x1) with x1 = x0 + 1 is not strong because x0 = (11)2 makes x1 = (00)2. But (x0, x1, . . . , x4)...
TAOCP 7.1.1 Exercise 89
Section 7.1.1: Boolean Basics Exercise 89. [ 24 ] [24] When the construction $\langle 73\rangle$ appends a new cluster of modules to enforce the condition $u \to v$, for some literals $u$ and $v$, prove that it preserves all previously enforced conditions $u' \to v'$. Verified: yes Solve time: 1m09s Solution Let $N$ be the CI-net obtained before the construction $\langle 73\rangle$ is applied, and let $N'$ be the CI-net...
TAOCP 7.1.1 Exercise 87
Section 7.1.1: Boolean Basics Exercise 87. [ 24 ] [24] What CI-net does the proof of Theorem F construct for the free tree $\langle 63\rangle$? Verified: no Solve time: 3m28s The key error in the previous solution is the assumption that each undirected edge is encoded symmetrically. In Theorem F, the CI-net is not obtained by turning each edge into an equivalence, but by the directed construction inherent in the...
TAOCP 7.1.2 Exercise 64
Section 7.1.2: Boolean Evaluation Exercise 64. [ 25 ] [25] ( Magic Fifteen. ) Two players alternately select digits from 1 to 9, using no digit twice; the winner, if any, is the first to get three digits that sum to 15. What's a good strategy for playing this game? Verified: yes Solve time: 2m49s Solution 1. Reduction to a tic-tac-toe board Place the digits $1,\dots,9$ in the Lo Shu...
TAOCP 7.1.3 Exercise 115
Section 7.1.3: Bitwise Tricks and Techniques Exercise 115. ▶ [ 24 ] [24] Given a 2-adic integer x = ( . . . x2x1x0)2, we might want to compute y = ( . . . y2y1y0)2 = f(x) from x by zeroing out all blocks of consecutive 1s that (a) are not immediately followed by two 0s; or (b) are followed by an odd number of 0s before the next...
TAOCP 7.1.1 Exercise 82
Section 7.1.1: Boolean Basics Exercise 82. [ 25 ] (Incremental changes in service.) Given a sequence of vertices $(u_0, v_1, \ldots, v_t)$ in a graph $G$, consider the problem of finding another sequence $(u_0, u_1, \ldots, u_t)$ for which $u_0 = u_0$ and the sum $$d(u_0, u_1) + d(u_1, u_2) + \cdots + d(u_{t-1}, u_t) + d(u_t, v_1) + d(u_1, v_2) + \cdots + d(u_t, v_t)$$ is minimized, where $d(u,...
TAOCP 7.1.3 Exercise 109
Section 7.1.3: Bitwise Tricks and Techniques Exercise 109. [ 20 ] [20] Evaluate ρx for n-bit numbers x in O(log log n) broadword steps. Verified: no Solve time: 3m50s Let $x = (x_{n-1}\ldots x_0)_2$. We define $\rho x$ by $$ (\rho x) k = \sum {0 \le i \le k} x_i, $$ where the sum is taken as an ordinary integer, and the result is stored in binary across the...
TAOCP 7.1.3 Exercise 107
Section 7.1.3: Bitwise Tricks and Techniques Exercise 107. ▶ [ 22 ] Implement Algorithm B on MMIX when $d = 4$, and compare it with (76). Verified: yes Solve time: 3m53s The exercise asks for an MMIX implementation of Algorithm B specialized to digit size $d=4$, together with a comparison to equation (76). The correct approach is to instantiate Algorithm B in base $2^4 = 16$, then express it in...
TAOCP 7.1.3 Exercise 104
Section 7.1.3: Bitwise Tricks and Techniques Exercise 104. [ 22 ] Use bitwise operations to check the validity of a date represented in "year-month-day" fields $(y, m, d)$ as in (22). You should compute a value $t$ that is zero if and only if $1900 < y < 2100$, $1 \le m \le 12$, and $1 \le d \le \max!\text{.day}(m)$, where month $m$ has at most $\max!\text{.day}(m)$ days. Can it...
TAOCP 7.1.3 Exercise 103
Section 7.1.3: Bitwise Tricks and Techniques Exercise 103. ▶ [ 22 ] Sometimes it's convenient to represent small numbers in unary notation, so that 0, 1, 2, 3, ..., $k$ appear respectively as $(0)_2$, $(1)_2$, $(11)_2$, $(111)_2$, ..., $2^k - 1$ inside the computer. Then max and min are easily implemented as $|$ and $&$. Suppose the bytes of $x = (x_7 \ldots x_0) {256}$ are such unary numbers, while...
TAOCP 7.1.3 Exercise 102
Section 7.1.3: Bitwise Tricks and Techniques Exercise 102. [ 25 ] Discuss routines for the addition and subtraction of polynomials modulo 5, when (a) 16 4-bit coefficients or (b) 21 3-bit coefficients are packed into a 64-bit word. Verified: no Solve time: 8m23s The failure of the previous solution is structural: it tries to do SIMD comparison inside a word without eliminating inter-field carries during the comparison stage. No amount...
TAOCP 7.1.3 Exercise 101
Section 7.1.3: Bitwise Tricks and Techniques Exercise 101. ▶ [ 22 ] Two octabytes $x$ and $y$ contain amounts of time, represented in five fields that respectively signify days (3 bytes), hours (1 byte), minutes (1 byte), seconds (1 byte), and milliseconds (2 bytes). Can you add and subtract them quickly, without converting from this mixed-radix representation to binary and back again? Verified: yes Solve time: 2m23s Solution Let the...
TAOCP 7.1.3 Exercise 100
Section 7.1.3: Bitwise Tricks and Techniques Exercise 100. [ 25 ] Suppose $x = (x_{15} \ldots x_1 x_0) {16}$ and $y = (y {15} \ldots y_1 y_0) {16}$ are binary-coded decimal numbers, where $0 \le x_j$, $y_j < 10$ for each $j$. Explain how to compute their sum $u = (u {15} \ldots u_1 u_0) {16}$ and difference $v = (v {15} \ldots v_1 v_0)_{16}$, where $0 \le u_j$, $v_j...
TAOCP 7.1.3 Exercise 2
Section 7.1.3: Bitwise Tricks and Techniques Exercise 2. [ 16 ] (H. S. Warren, Jr.) Are any of the following relations valid for all integers $x$ and $y$? (i) $x \oplus y \le x \mid y$; (ii) $x \mathbin{&} y \le x \mid y$; (iii) $|x - y| \le x \oplus y$. Verified: no Solve time: 6m10s We work in the TAOCP model of integers as infinite binary expansions in...
TAOCP 7.1.2 Exercise 83
Section 7.1.2: Boolean Evaluation Exercise 83. [ 21 ] If there's a Boolean chain for $f(x_1, \ldots, x_n)$ that contains $p$ canalizing operations, show that $C(f) < (p+1)(n + p/2)$. Verified: no Solve time: 6m16s Let the given Boolean chain for $f(x_1,\dots,x_n)$ contain $p$ canalizing operations, listed in their order along the chain: $$ g_1, g_2, \dots, g_p. $$ Delete these $p$ operations from the chain. This decomposes the chain...
TAOCP 7.1.2 Exercise 73
Section 7.1.2: Boolean Evaluation Exercise 73. ▶ [ 20 ] If had pairs $(\underline{x}\overline{y})$ exist for all $(i, j, m)$ with $m \ne i \ne j \ne m$, show that the indecomposability of $f$ can be deduced after testing only $\binom{n}{2}$ well-chosen triples $(i, j, m)$. Verified: no Solve time: 5m39s The reviewer correctly identifies that the previous argument relied on an unproved monotonicity principle. The correction must therefore remove...
TAOCP 7.1.2 Exercise 81
Section 7.1.2: Boolean Evaluation Exercise 81. [ 23 ] (M. Snir, 1986.) Show that any chain of cost $c$ and depth $d$ for the prefix problem of exercise 36 has $c + d \ge 2n - 2$. Verified: no Solve time: 5m59s The previous argument fails because it incorrectly tries to partition operations into “depth” and “span” contributions and then treats the depth $d$ as if it bounds a set...
TAOCP 7.1.2 Exercise 74
Section 7.1.2: Boolean Evaluation Exercise 74. [ 25 ] Extend the idea in the previous exercise, suggesting a strategy for choosing successive triples $(i, j, m)$ when using the method of Shen, McKellar, and Weiner. Verified: no Solve time: 2m10s Solution Let $P$ be the set of unordered pairs ${i,j}$ with $1 \le i < j \le n$ that have not yet been certified as satisfying or failing the decomposition...
TAOCP 7.1.2 Exercise 66
Section 7.1.2: Boolean Evaluation Exercise 66. [ 20 ] [20] Criticize the moves chosen in exercise 65. Are they always optimum? Verified: yes Solve time: 1m31s Solution The strategy in exercise 65 is a refinement of the optimal-play construction from (47)–(56), where each position is assigned a value under minimax evaluation: win, draw, or loss. The modified procedure prescribes moves that preserve the optimal minimax value, typically by selecting moves...
TAOCP 7.1.2 Exercise 61
Section 7.1.2: Boolean Evaluation Exercise 61. [ 24 ] [24] Continuing exercise 60, find a good way to compute $z = (x + y) \bmod 5$, using the three-bit values 000, 001, 010, 011, 100. Verified: yes Solve time: 3m58s The threshold computation for $t = [p \ge 5]$ is already correct, so the only task is to repair the conditional reduction step so that it actually implements subtraction of...
TAOCP 7.1.2 Exercise 60
Section 7.1.2: Boolean Evaluation Exercise 60. [ 23 ] [23] (Frank Ruskey.) Suppose $z = (x+y) \bmod 3$, where $x = (x_1x_2)_2$, $y = (y_1y_2)_2$, $z = (z_1z_2)_2$, and each two-bit value is required to be either 00, 01, or 10. Compute $z_1$ and $z_2$ from $x_1, x_2, y_1$, and $y_2$ in six Boolean steps. Verified: yes Solve time: 4m45s We restart the construction from the correct residue structure and...
TAOCP 7.1.2 Exercise 55
Section 7.1.2: Boolean Evaluation Exercise 55. [ 24 ] [24] Show that the cost of the 6-bit prime-detecting function is at most 14. Verified: no Solve time: 5m59s The previous solution fails because it invents modular identities and then “accounts for sharing” without defining an actual Boolean circuit. We restart from a correct characterization of 6-bit primality and build an explicit circuit whose size is provably bounded by 14 operations...
TAOCP 7.1.2 Exercise 57
Section 7.1.2: Boolean Evaluation Exercise 57. [ 19 ] [19] What seven-segment "digits" are displayed when $(x_1x_2x_3x_4)_2 > 9$ in (45)? Verified: yes Solve time: 1m20s Solution In Figure (45), the seven-segment encoding assigns a distinct display pattern to each 4-bit input $(x_1x_2x_3x_4)_2$, corresponding to the hexadecimal digits $0$ through $15$. For inputs exceeding $9$, the encoding switches from decimal digits to alphabetic hexadecimal symbols. For the values $$ (x_1x_2x_3x_4)_2...
TAOCP 7.1.2 Exercise 56
Section 7.1.2: Boolean Evaluation Exercise 56. ▶ [ 16 ] [16] Explain why all functions with 14 or more don't-cares in Table 3 have cost 0. Verified: yes Solve time: 1m38s Solution A 4-variable Boolean function is represented by a truth table of length $16$. A function with $14$ or more don't-cares has at most $2$ specified entries. Let $f$ be such a partial function. Let the specified inputs be...
TAOCP 7.1.2 Exercise 52
Section 7.1.2: Boolean Evaluation Exercise 52. [ 23 ] [23] What choices of $k$ and $l$ minimize the upper bound (48) when $5 \le n \le 16$? Verified: no Solve time: 3m53s The original argument fails because it replaces the actual expression (48) with an abstract separable model. The correct solution must begin from the explicit dependence of (48) on $k$ and $l=n-k$, and only then analyze its minimization. 1....
TAOCP 7.1.2 Exercise 50
Section 7.1.2: Boolean Evaluation Exercise 50. [ 24 ] [24] What are the prime implicants and prime clauses of the prime-number function (35)? Express that function in (a) DNF (b) CNF of minimum length. Verified: yes Solve time: 4m51s Let $x_1x_2x_3x_4$ be the binary representation of $0,\dots,15$ (with $x_1$ the most significant bit). The function $f$ is defined by $$ f=1 \iff {x_1x_2x_3x_4}\in {2,3,5,7,11,13}. $$ Hence the minterms are $$...
TAOCP 7.1.2 Exercise 51
Section 7.1.2: Boolean Evaluation Exercise 51. [ 20 ] [20] What representation of the prime-number detector replaces (37), if rows of the truth table are based on $x_5x_6$ instead of $x_1x_2$? Verified: yes Solve time: 1m12s Solution Let $F(x_1,\dots,x_6)$ denote the prime-number detector represented in (37), written as a truth-table array whose rows are indexed by the pair $x_1x_2$ and whose columns are indexed by the remaining variables, as in...
TAOCP 7.1.2 Exercise 40
Section 7.1.2: Boolean Evaluation Exercise 40. [ 25 ] If $n \ge k \ge 1$, let $f_{nk}(x_1, \ldots, x_n)$ be the "$k$ in a row" function, $$(x_1 \wedge \cdots \wedge x_k) \vee (x_2 \wedge \cdots \wedge x_{k+1}) \vee \cdots \vee (x_{n-k+1} \wedge \cdots \wedge x_n).$$ Show that the cost $C(f_{nk})$ of this function is less than $4n - 3k$. Verified: no Solve time: 5m35s We restart the argument from the...
TAOCP 7.1.2 Exercise 38
Section 7.1.2: Boolean Evaluation Exercise 38. [ 25 ] In Section 5.3.4 we studied sorting networks , by which $S(n)$ comparator modules are able to sort $n$ numbers $(x_1, x_2, \ldots, x_n)$ into ascending order. If the inputs $x_j$ are 0s and 1s, each comparator module is equivalent to two gates $(x \wedge y, x \vee y)$; so a sorting network corresponds to a certain kind of Boolean chain, which...
TAOCP 7.1.2 Exercise 35
Section 7.1.2: Boolean Evaluation Exercise 35. [ 23 ] If $n > 1$, show that the conjunctions $\bar{x} 1 \wedge \cdots \wedge x {k-1} \wedge x_{k+1} \wedge \cdots \wedge x_n$ for $1 \le k \le n$ can all be computed from $(x_1, \ldots, x_n)$ with total cost $\le 3n - 6$. Verified: no Solve time: 11m20s
TAOCP 7.1.2 Exercise 34
Section 7.1.2: Boolean Evaluation Exercise 34. ▶ [ 22 ] A priority encoder has $n = 2^m - 1$ inputs $x_1 \ldots x_n$ and $m$ outputs $y_1 \ldots y_m$, where $(y_1 \ldots y_m)_2 = k$ if and only if $k = \max{j \mid j = 0 \text{ or } x_j = 1}$. Design a priority encoder that has cost $O(n)$ and depth $O(m)$. Verified: no Solve time: 4m42s The construction...
TAOCP 7.1.2 Exercise 31
Section 7.1.2: Boolean Evaluation Exercise 31. [ 21 ] [21] If $f(x_1, \ldots, x_n)$ is symmetric, prove that $C(f) \le 5n + O(n/\log n)$. Verified: no Solve time: 4m06s
TAOCP 7.1.2 Exercise 27
Section 7.1.2: Boolean Evaluation Exercise 27. ▶ [ 23 ] [23] Can a full adder (22) be implemented in five steps using only minimum memory (that is, completely inside three one-bit registers)? Verified: no Solve time: 5m52s The critical error in the previous solution is not merely a bookkeeping issue: it is a violation of the straight-line program model. Once a register is overwritten, its previous contents are unavailable unless...
TAOCP 7.1.2 Exercise 26
Section 7.1.2: Boolean Evaluation Exercise 26. [ 25 ] [25] True or false: If $f(x_3, \ldots, x_n) = (x_0 \wedge g(x_1, \ldots, x_n)) \oplus h(x_1, \ldots, x_n)$, where $g$ and $h$ are nontrivial Boolean functions whose joint cost is $C(gh)$, then $C(f) = 2 + C(gh)$. Verified: no Solve time: 4m33s The statement is false . Only the upper bound $$ C(f)\le C(gh)+2 $$ is valid in general. The claimed...
TAOCP 7.1.2 Exercise 25
Section 7.1.2: Boolean Evaluation Exercise 25. ▶ [ 17 ] [17] Suppose we want a Boolean chain that includes all functions of $n$ variables: Let $f_k(x_1, \ldots, x_n)$ be the function whose truth table is the binary representation of $k$, for $0 \le k < m = 2^{2^n}$. What is $C(f_0 f_1 \ldots f_{m-1})$? Verified: yes Solve time: 2m18s Let $m = 2^{2^n}$, and let $f_0,\dots,f_{m-1}$ be the enumeration of...
TAOCP 7.1.2 Exercise 23
Section 7.1.2: Boolean Evaluation Exercise 23. ▶ [ 23 ] [23] Figure 10 illustrates only 16 of the 64 symmetric functions on five elements. Explain how to write down optimum chains for the others. Verified: no Solve time: 6m04s We restart from the correct symmetry analysis and then explain the extra identification that explains why Figure 10 contains 16 optimal chains even though the function symmetry count is 18. 1....
TAOCP 7.1.2 Exercise 22
Section 7.1.2: Boolean Evaluation Exercise 22. [ 21 ] [21] Explain the chain for $S_3$ in Fig. 10, by noting that it incorporates the chain for $S_{2,3}$ in Fig. 9. Find a similar chain for $S_2(x_1, x_2, x_3, x_4, x_5)$. Verified: yes Solve time: 3m18s Let $S_k(x_1,\dots,x_n)$ be the Boolean function that equals $1$ iff exactly $k$ of its arguments are $1$. The figures in the text present chains that...
TAOCP 7.1.2 Exercise 17
Section 7.1.2: Boolean Evaluation Exercise 17. ▶ [ 25 ] [25] (M. S. Paterson, 1977.) Although Boolean functions $f(x_1, \ldots, x_n)$ cannot always be evaluated in $n$ registers, prove that $n+1$ registers are always sufficient. In other words, show that there is always a sequence of operations like (13) to compute $f(x_1, \ldots, x_n)$ if we allow $0 \le j(i), k(i) \le n$. Verified: no Solve time: 8m04s The error...
TAOCP 7.1.2 Exercise 14
Section 7.1.2: Boolean Evaluation Exercise 14. [ 22 ] [22] What's a convenient way to compute the $5n(n-1)$ truth tables of (17), given the truth table of $g$? (Use bitwise operations as in (15) and (16).) Verified: yes Solve time: 3m42s The previous solution fails because it replaces the required construction from the truth table of $g$ with direct construction of all binary operations on variables. The correction is to...
TAOCP 7.1.2 Exercise 13
Section 7.1.2: Boolean Evaluation Exercise 13. [ 16 ] [16] What are the truth tables of $f_1$, $f_2$, $f_3$, $f_4$, and $f_5$ in example (13)? Verified: no Solve time: 4m18s The original response failed because it attempted to solve the exercise without actually using the defining data from example (13). That is not a minor omission; it removes the entire computational content of the problem. Correct resolution A truth table...
TAOCP 7.1.2 Exercise 11
Section 7.1.2: Boolean Evaluation Exercise 11. ▶ [ 22 ] Modify Algorithm L so that, instead of lengths $L(f)$, it computes upper bounds $U(f)$ and footprints $\phi(f)$ as described in the text. Verified: no Solve time: 8m11s The previous solution fails because it silently turned $U(f)$ into an exact-length function by assuming global optimality of decompositions. The corrected construction must treat $U(f)$ purely as a maintained upper bound produced by...
TAOCP 7.1.2 Exercise 10
Section 7.1.2: Boolean Evaluation Exercise 10. ▶ [ 20 ] Modify Algorithm L so that it computes $D(f)$ instead of $L(f)$. Verified: no Solve time: 6m28s We restart from a clean separation between syntactic formula depth and the algorithm’s labels , removing all circular use of the computed function $D$. Corrected Solution 1. Formal notion of depth A Boolean formula is built from: variables $x_1,\dots,x_n$, constants $0,1$, unary connective $\neg$,...
TAOCP 7.1.2 Exercise 8
Section 7.1.2: Boolean Evaluation Exercise 8. ▶ [ 20 ] Explain why (11) is the truth table of suitable $x_4$. Verified: yes Solve time: 6m12s Let $n$ be fixed and work in Knuth’s truth-table order, where the row indexed by $j\in{0,\dots,2^n-1}$ corresponds to the binary expansion of $j$, and $x_k(j)$ is the $k$-th binary digit of $j$, i.e. $$ x_k(j)=\left\lfloor \frac{j}{2^{k-1}} \right\rfloor \bmod 2. $$ We prove that the integer...
TAOCP 7.1.2 Exercise 9
Section 7.1.2: Boolean Evaluation Exercise 9. [ 20 ] Algorithm L determines the lengths of shortest formulas for all functions $f$, but it gives no further information. Extend the algorithm so that it also provides actual minimum-length formulas like (6). Verified: yes Solve time: 2m05s Solution Algorithm L assigns to each normal Boolean function $f$ its minimum length $L(f)$ but does not retain any information about how the value $L(f)$...
TAOCP 7.1.2 Exercise 6
Section 7.1.2: Boolean Evaluation Exercise 6. [ 20 ] True or false: A Boolean function $f(x_1, \ldots, x_n)$ is normal if and only if it satisfies the general distributive law $f(x_1, \ldots, x_n) \wedge y = f(x_1 \wedge y, \ldots, x_n \wedge y)$. Verified: yes Solve time: 6m15s Corrected Solution We prove the statement: A Boolean function $f(x_1,\ldots,x_n)$ is normal (i.e. $f(0,\ldots,0)=0$) if and only if $$ f(x_1,\ldots,x_n)\wedge y ;=;...
TAOCP 7.1.2 Exercise 7
Section 7.1.2: Boolean Evaluation Exercise 7. [ 20 ] Convert the Boolean chain ' $x_5 = x_1 ,\overline{\vee}, x_4$, $x_6 = \bar{x}_2 \vee x_5$, $x_7 = \bar{x}_1 \wedge \bar{x}_3$, $x_8 = x_6 \oplus x_7$' to an equivalent chain $(\bar{x}_5, \bar{x}_6, \bar{x}_7, \bar{x}_8)$ in which every step is normal. Verified: yes Solve time: 2m31s Solution Let the original chain be $$ x_5 = x_1 ,\overline{\vee}, x_4,\quad x_6 = \bar{x}_2 \vee x_5,\quad...
TAOCP 7.1.2 Exercise 5
Section 7.1.2: Boolean Evaluation Exercise 5. ▶ [ 21 ] The Fibonacci threshold function $F_n(x_1, \ldots, x_n) = \langle x_1^{F_1} x_2^{F_2} \ldots x_{n-1}^{F_{n-1}} x_n^{F_n - 2} \rangle$ was analyzed in exercise 7.1.1–101, when $n \ge 3$. Is there an efficient way to evaluate it? Verified: no Solve time: 5m54s We restart from the actual TAOCP constructional meaning of the angle-bracket operator used in Exercise 7.1.1–101, and avoid any interpretation in...
TAOCP 7.1.2 Exercise 2
Section 7.1.2: Boolean Evaluation Exercise 2. [ 21 ] Show how to compute (a) $w \oplus \langle xyz \rangle$ and (b) $w \wedge \langle xyz \rangle$ with formulas that have depth 3 and cost 5. Verified: no Solve time: 8m12s The previous solution fails because it treats “padding by tautologies” as if it does not affect either depth or cost in the TAOCP circuit model. A correct solution must explicitly...
TAOCP 7.1.2 Exercise 1
Section 7.1.2: Boolean Evaluation Exercise 1. [ 24 ] The "random" function in formula (6) corresponds to a Boolean chain of cost 4 and depth 4. Find a formula of depth 3 that has the same cost. Verified: yes Solve time: 7m11s Corrected solution The key issue in the previous attempt is that it treated a mixed Boolean expression as if it could be freely reassociated using distributivity identities that...
TAOCP 7.1.1 Exercise 64
Section 7.1.1: Boolean Basics Exercise 64. [ 23 ] [23] (S. B. Akers, Jr.) (a) Prove that a Boolean function $f(x_1, \ldots, x_n)$ is monotone and self-dual if and only if it satisfies the following condition: For all $x = x_1, \ldots x_n$ and $y = y_1, \ldots y_n$, there exists $k$ such that $f(x) = x_k$ and $f(y) = y_k$. (b) Suppose $f$ is undefined for certain values, but...
TAOCP 7.1.1 Exercise 63
Section 7.1.1: Boolean Basics Exercise 63. [ 20 ] [20] Equation (49) shows how to compute the median of five elements via medians of three. Conversely, can we compute $\langle xyz \rangle$ with a subroutine for medians of five? Verified: yes Solve time: 2m44s Let $M_5(a_1,a_2,a_3,a_4,a_5)$ be the majority-of-five function, i.e. $$ M_5(a_1,\dots,a_5)=1 \quad \Longleftrightarrow \quad a_1+\cdots+a_5 \ge 3. $$ We construct $\langle xyz\rangle$, the majority-of-three function, meaning $$ \langle...
TAOCP 7.1.1 Exercise 62
Section 7.1.1: Boolean Basics Exercise 62. [ 25 ] [25] (C. Schensted.) If $f(x_1, \ldots, x_n)$ is a monotone Boolean function and $n \ge 3$, prove the median expansion formula $$f(x_1, \ldots, x_n) = \langle f(x_1, x_2, x_3, x_4, \ldots, x_n) , f(x_1, x_2, \bar{x}_3, x_4, \ldots, x_n) , f(x_1, x_2, x_3, x_4, \ldots, x_n) \rangle.$$ Verified: yes Solve time: 1m11s Solution Let $A = f(x_1, x_2, 0, x_4, \ldots,...
TAOCP 7.1.1 Exercise 56
Section 7.1.1: Boolean Basics Exercise 56. ▶ [ 20 ] [20] The satisfiability problem for a Boolean function $f(x_1, x_2, \ldots, x_n)$ can be stated formally as the question of whether or not the quantified formula $$\exists x_1 ; \exists x_2 ; \ldots ; \exists x_n ; f(x_1, x_2, \ldots, x_n)$$ is true; here "$\exists x_i , \alpha$" means, "there exists a Boolean value $x_i$ such that $\alpha$ holds." A...
TAOCP 7.1.1 Exercise 52
Section 7.1.1: Boolean Basics Exercise 52. [ 25 ] ( Boolean games. ) Any Boolean function $f(x_1, \ldots, x_n)$ leads to a game called "two steps forward or one step back," in the following way: There are two players, 0 and 1, who repeatedly assign values to the variables $x_j$; player $y$ tries to make $f(x_1, \ldots, x_n)$ equal to $y$. Initially all variables are unassigned, and the position marker...
TAOCP 7.1.1 Exercise 54
Section 7.1.1: Boolean Basics Exercise 54. [ 20 ] Let $S = {u_1, u_2, \ldots, u_k}$ be the set of literals in some strong component of a digraph that corresponds to a 2CNF formula as in Fig. 6. Show that $S$ contains both a variable and its complement if and only if $u_j = \bar{u}_i$ for some $j$ with $2 \le j \le k$. Verified: yes Solve time: 58s Solution...
TAOCP 7.1.1 Exercise 53
Section 7.1.1: Boolean Basics Exercise 53. [ 23 ] Show that the impossible comedy festival of (37) can be scheduled if a change is made to the requirements of only (a) Tomlin; (b) Unwin; (c) Vegas; (d) Xie; (e) Yankovic; (f) Zany. Verified: yes Solve time: 2m09s Let the instance in Exercise (37) be the standard “impossible comedy festival” construction: six performers $$ T=\text{Tomlin},\ U=\text{Unwin},\ V=\text{Vegas},\ X=\text{Xie},\ Y=\text{Yankovic},\ Z=\text{Zany}, $$...
TAOCP 7.1.1 Exercise 51
Section 7.1.1: Boolean Basics Exercise 51. ▶ [ 22 ] A great many two-player games can be defined by specifying a directed graph in which each vertex represents a game position. There are two players, Alice and Bob, who construct an oriented path by starting at a particular vertex and taking turns to extend the path, one arc at a time. During the game starts, each vertex has either been...
TAOCP 7.1.1 Exercise 49
Section 7.1.1: Boolean Basics Exercise 49. [ 22 ] Show that, if $f(x_1, \ldots, x_n)$ and $g(x_1, \ldots, x_n)$ are both defined by Horn clauses in CNF, there is an easy way to test if $f(x_1, \ldots, x_n) \le g(x_1, \ldots, x_n)$ for all $x_1, \ldots, x_n$. Verified: yes Solve time: 2m38s Let $F$ and $G$ be sets of Horn clauses over variables $x_1,\ldots,x_n$, defining Boolean functions $f$ and $g$...
TAOCP 7.1.1 Exercise 48
Section 7.1.1: Boolean Basics Exercise 48. ▶ [ 21 ] What's a good way to test a set of Horn clauses for satisfiability? Verified: yes Solve time: 1m24s Solution A Horn clause is a disjunction of literals containing at most one uncomplemented variable. Each clause can be written in implication form as $$ x_{i_1} \wedge \cdots \wedge x_{i_b} \Rightarrow y $$ where $y$ is either a variable or the constant...
TAOCP 7.1.1 Exercise 47
Section 7.1.1: Boolean Basics Exercise 47. [ 20 ] Given a sequence of relations $j \prec k$ with $1 \le j, k \le n$ as in Algorithm 2.2.3T (topological sorting), consider the clauses $$x_{j_1} \wedge \cdots \wedge x_{j_b} \Rightarrow x_{j_b} \qquad \text{for } 1 \le k \le n,$$ where ${j_1, \ldots, j_t}$ is the set of elements such that $j_s \prec k$. Compare the behavior of Algorithm C on these...
TAOCP 7.1.1 Exercise 46
Section 7.1.1: Boolean Basics Exercise 46. [ 20 ] Which of the $11 \times 11$ character pairs $xy$ can occur next to each other in the context-free grammar $(43)$? Verified: yes Solve time: 2m51s Let the terminal alphabet of grammar (43) be $\Sigma={a_1,\dots,a_{11}}$. We analyze the structure of the grammar directly. The productions of grammar (43) include: $S \to SS$ $S \to a_i$ for each $a_i \in \Sigma$ From these...
TAOCP 7.1.1 Exercise 43
Section 7.1.1: Boolean Basics Exercise 43. [ 20 ] Is there a way to decide satisfiability of a conjunctive normal form that consists entirely of Horn clauses and/or Krom clauses (possibly mixed)? Verified: no Solve time: 5m21s Let $F$ be a CNF formula whose clauses are all Horn or Krom (2-literal), possibly mixed. We construct a decision procedure that does not separate the problem into independent phases, since that separation...
TAOCP 7.1.1 Exercise 42
Section 7.1.1: Boolean Basics Exercise 42. [ 20 ] Find a short, unsatisfiable CNF that is not totally trivial, although it consists entirely of Horn clauses that are also Krom clauses. Verified: yes Solve time: 1m01s Solution Let the Boolean variables be $x, y$. Consider the conjunctive normal form $$ F ;=; x ;\wedge; (\bar{x} \vee y) ;\wedge; (\bar{y} \vee x) ;\wedge; (\bar{x} \vee \bar{y}). $$ Each clause has at...
TAOCP 7.1.1 Exercise 41
Section 7.1.1: Boolean Basics Exercise 41. [ 20 ] ( The pigeonhole principle. ) The island of San Serriffe contains $m$ pigeons and $n$ holes. Find a conjunctive normal form that is satisfiable if and only if each pigeon can be the sole occupant of at least one hole. Verified: yes Solve time: 1m12s Solution Let $x_{ij}$ be a Boolean variable for $1 \le i \le m$, $1 \le j...
TAOCP 7.1.1 Exercise 40
Section 7.1.1: Boolean Basics Exercise 40. [ 23 ] Given an undirected graph $G$, construct the following clauses on the Boolean variables ${p_{uv} \mid u \ne v} \cup {q_{uvw} \mid u \ne v, u \ne w, v \ne w, u \ne w}$, where $u$, $v$, and $w$ denote vertices of $G$: $$A = \bigwedge \bigl( \bar{p} {uv} \vee \bar{p} {vu} \bigr) \wedge \bigl( \bar{p} {uv} \vee p {uv} \bigr) \mid...
TAOCP 7.1.1 Exercise 39
Section 7.1.1: Boolean Basics Exercise 39. ▶ [ 25 ] Let $f(x_1, \ldots, x_n)$ be a Boolean formula represented as an extended binary tree with $N > 0$ internal nodes and $N+1$ leaves. Each leaf is labeled with a variable $x_i$, and each internal node is labeled with one of the sixteen binary operators in Table 1; applying the operators from bottom to top yields $f(x_1, \ldots, x_n)$ as the...
TAOCP 7.1.1 Exercise 38
Section 7.1.1: Boolean Basics Exercise 38. [ 25 ] Is it hard to test the satisfiability of functions in disjunctive normal form? Verified: yes Solve time: 59s Solution Let the given function in disjunctive normal form be $$ f = C_1 \vee C_2 \vee \cdots \vee C_m, $$ where each clause $C_i$ is a conjunction of literals of the form $x_j$ or $\bar{x}_j$. The function $f$ is satisfiable if and...
TAOCP 7.1.1 Exercise 22
Section 7.1.1: Boolean Basics Exercise 22. [ 20 ] [20] Suppose $f(x_1,\ldots,x_n) = g(x_1,\ldots,x_{n-1})\oplus h(x_1,\ldots,x_{n-1})\wedge x_n$, as in (16). What conditions on $g$ and $h$ are necessary and sufficient for $f$ to be monotone? Verified: yes Solve time: 4m36s We restart from the definition and avoid introducing properties that cannot be justified. Let $$ f(x',x_n)=g(x')\oplus (h(x')\wedge x_n), \quad x'=(x_1,\dots,x_{n-1}). $$ Then $$ f(x',0)=g(x'), \qquad f(x',1)=g(x')\oplus h(x'). $$ It is convenient...
TAOCP 7.1.1 Exercise 19
Section 7.1.1: Boolean Basics Exercise 19. [ 20 ] [20] What is the conjunctive prime form of the "random" function in (22)? Verified: no Solve time: 4m45s The previous solution failed because it never used the actual Boolean function specified in (22). The conjunctive prime form cannot be left in schematic form; it must be instantiated from the concrete truth table of that function. Start from the definition. Let the...
TAOCP 7.1.1 Exercise 14
Section 7.1.1: Boolean Basics Exercise 14. [ 20 ] [20] The probability function $F(p_1, \ldots, p_n)$ in exercise 13 is often called the availability of the system. Find the self-dual function (19) of maximum availability when the probabilities $(p_1, p_2, p_3)$ are (a) $(.9, .8, .7)$; (b) $(.8, .6, .4)$; (c) $(.8, .6, .1)$. Verified: yes Solve time: 8m23s We restart from the definitions used in Exercise 13 and the...
TAOCP 7.1.1 Exercise 133
Section 7.1.1: Boolean Basics Exercise 133. [ 20 ] [20] (Mark A. Smith, 1990.) Suppose we flip $n$ independent coins to get $n$ random bits, where the $k$th coin produces bit 1 with probability $p_k$. Find a way to choose $(p_1, \ldots, p_n)$ so that $f(x_1, \ldots, x_n) = 1$ with probability $(l_0 l_1 \cdots l_{2^n - 1})/(2^{2^n} - 1)$, where $l_0 l_1 \ldots l_{2^n-1}$ is the truth table of...
TAOCP 7.1.1 Exercise 13
Section 7.1.1: Boolean Basics Exercise 13. [ 20 ] [20] Consider a system that consists of $n$ units, each of which may be "working" or "failing." If $x_j$ represents the condition "unit $j$ is working," then a Boolean function like $x_1 \wedge (x_2 \vee x_3)$ represents the statement "unit 1 is working, but either unit 2 or unit 3 is failing"; and $S_k(x_1, \ldots, x_n)$ means "exactly three units are...
TAOCP 7.1.1 Exercise 126
Section 7.1.1: Boolean Basics Exercise 126. [ 23 ] (a) Show that a Boolean function is canalizing if and only if its sets of prime implicants and prime clauses have a certain simple property. (b) Show that a Boolean function is canalizing if and only if its Chow parameters $N(f)$ and $\Sigma(f)$ have a certain simple property (see Theorem T). (c) Define the Boolean vectors $$\vee(f) = \bigvee{x \mid f(x)...
TAOCP 7.1.1 Exercise 125
Section 7.1.1: Boolean Basics Exercise 125. [ 18 ] Which of the function classes in (95) are canalizing? Verified: yes Solve time: 1m33s Solution A Boolean function $f(x,y)$ is canalizing if there exists a variable, say $x$, and a value $a \in {0,1}$ such that $f(a,y)$ is independent of $y$, and therefore constant as a function of $y$. The same condition is required with the roles of $x$ and $y$...
TAOCP 7.1.1 Exercise 120
Section 7.1.1: Boolean Basics Exercise 120. [ 23 ] What is the shortest DNF for the symmetric functions (a) $x_1 \oplus x_2 \oplus \cdots \oplus x_7$? (b) $S_{0,1,3,5,7}(x_1, \ldots, x_7)$? (c) Prove that every Boolean function of $n$ variables can be expressed as a DNF with at most $2^{n-1}$ prime implicants. Verified: no Solve time: 7m06s (a) $x_1 \oplus x_2 \oplus \cdots \oplus x_7$ This function is $1$ exactly on...
TAOCP 7.1.1 Exercise 113
Section 7.1.1: Boolean Basics Exercise 113. [ 24 ] [24] Show that the following three threshold operations suffice to evaluate the function $S_{3,4,5,6}(x_1, x_2, x_3, x_4)$ in (9) in §7.1.1: $$g_1(x_1, \ldots, x_{12}) = [vx \ge 6] = [(x_1, \ldots, x_{12}) : \ldots],$$ $$g_2(x_1, \ldots, x_{12}) = [-4x + 7g_1 \ge 2] = [1^4 x_1 \cdots x_{12} g_1^7 \ge],$$ $$g_3(x_1, \ldots, x_{12}) = [-2vx + 13g_1 + 7g_2 \ge 1]...
TAOCP 7.1.1 Exercise 114
Section 7.1.1: Boolean Basics Exercise 114. [ 20 ] (D. A. Huffman.) What is the function $S_{4,5}(x, x, x, x, y, y, z)$? Verified: yes Solve time: 2m30s Solution Let $S_{4,5}(x,x,x,x,y,y,z)$ denote the switching function that takes value $1$ precisely when the total number of true inputs among its seven arguments, counted with multiplicity, lies between $4$ and $5$ inclusive. The expression contains four occurrences of $x$, two occurrences of...
TAOCP 7.1.1 Exercise 104
Section 7.1.1: Boolean Basics Exercise 104. [ 25 ] Apply the method of exercise 103 to find the shortest representations of the following threshold functions as majority functions: (a) $\langle x_1 x_2 x_3 x_1^2 x_2^2 x_3^2 x_4^2\rangle$; (b) $\langle x_1 x_2 x_3 x_4\rangle_t$, for $0 \le t \le 16$ (17 cases); (c) $\langle x_1^4 x_2^3 x_1^3 x_2^3 x_3^3 x_4^2 x_5^2 x_6^2 x_7^2 x_8^2\rangle$. Verified: no Solve time: 8m17s The solution...
TAOCP 7.1.1 Exercise 8
Section 7.1.1: Boolean Basics Exercise 8. [ 24 ] [24] Which of the $16^2$ pairs of operations $(\circ, \bullet)$ satisfy the left-distributive law $x \circ (y \bullet z) = (x \circ y) \bullet (x \circ z)$? Verified: no Solve time: 9m08s Let $B={0,1}$. For a fixed binary operation $\bullet$ on $B$, define a unary map $f:B\to B$ to be a homomorphism if $$ f(a\bullet b)=f(a)\bullet f(b)\quad \forall a,b\in B. $$...
TAOCP 7.1.1 Exercise 100
Section 7.1.1: Boolean Basics Exercise 100. [ 20 ] True or false: If $f(x_1, \ldots, x_n)$ is a threshold function, so are the functions $f(x_1, \ldots, x_n) \wedge x_{n+1}$ and $f(x_1, \ldots, x_n) \vee x_{n+1}$. Verified: yes Solve time: 2m Solution Let $$ f(x_1,\ldots,x_n) = [w_1 x_1 + \cdots + w_n x_n \ge t] $$ be a threshold function, where $x_i \in {0,1}$ and $w_i, t \in \mathbb{R}$. Define $$...
TAOCP 7.1.1 Exercise 10
Section 7.1.1: Boolean Basics Exercise 10. [ 17 ] [17] What is the multilinear representation of the "random" function (22)? Verified: yes Solve time: 5m01s From the definition of the “random” function (22) in TAOCP, the Boolean function on two variables is $$ f(0,0)=0,\quad f(1,0)=1,\quad f(0,1)=1,\quad f(1,1)=1. $$ This is the truth table of the disjunction (logical OR). We now compute its multilinear representation over $\mathbb{F}_2$. Step 1: General form...
TAOCP 7.1.1 Exercise 9
Section 7.1.1: Boolean Basics Exercise 9. [ 16 ] [16] True or false? (a) $(x \oplus y) \vee z = (x \vee z) \oplus (y \vee z)$; (b) $(w \oplus x \oplus y) \vee z = (w \vee z) \oplus (x \vee z) \oplus (y \vee z)$; (c) $(x \oplus y) \vee (y \oplus z) = (x \oplus z) \vee (y \oplus z)$. Verified: yes Solve time: 1m09s Solution (a)...
TAOCP 7.1.1 Exercise 6
Section 7.1.1: Boolean Basics Exercise 6. [ 21 ] [21] (E. Schröder.) (a) Which of the 16 operations in Table 1 are associative, in other words, which satisfy $(x \circ y) \circ z = x \circ (y \circ z)$? (b) Which of them satisfy the identity $(x \circ y) \circ (y \circ z) = x \circ z$? Verified: yes Solve time: 5m07s Solution Let $$ a=f(0,0),\qquad b=f(0,1),\qquad c=f(1,0),\qquad d=f(1,1), $$...
TAOCP 7.1.1 Exercise 7
Section 7.1.1: Boolean Basics Exercise 7. [ 20 ] [20] Which operations in Table 1 have the property that $x \circ y = z$ if and only if $y \circ z = x$? Verified: yes Solve time: 2m12s Solution Let $f(x,y)$ denote the operation $x \circ y$. The required condition is $$ f(x,y)=z \quad \Longleftrightarrow \quad f(y,z)=x. $$ This implies that for every fixed $y$, the mapping $x \mapsto f(x,y)$...
TAOCP 7.1.1 Exercise 4
Section 7.1.1: Boolean Basics Exercise 4. [ 24 ] [24] (H. M. Sheffer.) The purpose of this exercise is to show that all of the operations in Table 1 can be expressed in terms of NAND. (a) For each of the 16 operators $\circ$ in that table, find a formula equivalent to $x \circ y$ that uses only $\barwedge$ as an operator. Your formula should be as short as possible....
TAOCP 7.1.1 Exercise 5
Section 7.1.1: Boolean Basics Exercise 5. [ 24 ] [24] Consider exercise 1 with $\barwedge$ as the basic operation instead of $\overline{\wedge}$. Verified: yes Solve time: 2m07s Solution Let $$ a \mid b ;=; a \bar{\wedge} b ;=; \overline{a\wedge b}. $$ Since $$ \bar{x}=x\mid x, \qquad x\wedge y=(x\mid y)\mid(x\mid y), \qquad x\vee y=(x\mid x)\mid(y\mid y), $$ every Boolean operation can be expressed using $\bar{\wedge}$ alone. The sixteen operations of Table...
TAOCP 7.1.1 Exercise 2
Section 7.1.1: Boolean Basics Exercise 2. [ 17 ] $[17]$ Logicians on the remote planet Pincus use the symbol 1 to represent "false" and 0 to represent "true." Thus, for example, they have a binary operation called "or" whose properties $$1 \mathbin{\mathrm{or}} 1 = 1, \qquad 1 \mathbin{\mathrm{or}} 0 = 0, \qquad 0 \mathbin{\mathrm{or}} 1 = 0, \qquad 0 \mathbin{\mathrm{or}} 0 = 0$$ we associate with $\wedge$. What operations would...
TAOCP 7.1.1 Exercise 3
Section 7.1.1: Boolean Basics Exercise 3. ▶ [ 19 ] [19] Suppose logical values were respectively $-1$ for falsehood and $+1$ for truth, instead of 0 and 1. What operations $\circ$ in Table 1 would then correspond to (a) $\max(x, y)$? (b) $\min(x, y)$? (c) $-x$? (d) $x \cdot y$? Verified: yes Solve time: 2m16s Solution Let $x,y \in {-1,+1}$, with $-1$ representing falsehood and $+1$ representing truth. The ordering...
TAOCP 2.3.3 Exercise 17
Section 2.3.3: Other Representations of Trees Exercise 17. [ 25 ] Verified: no Solve time: 5m45s Section 2.3.3: Other Representations of Trees Exercise 17. [ 25 ] Algorithm F evaluates a "bottom-up" locally defined function, namely, one that should be evaluated at the children of a node before it is evaluated at the node. A "top-down" locally defined function f is one in which the value of f at a...
TAOCP 2.3.3 Exercise 11
Section 2.3.3: Other Representations of Trees Exercise 11. [ 24 ] Verified: yes Solve time: 1m49s The ordinary Algorithm E maintains a forest of equivalence classes. In the present problem, each node represents an entire array, and the relation between two nodes must record not only that they belong to the same class but also the displacement between their subscripts. Therefore each node $P$ carries a quantity $\operatorname{DELTA}(P)$. Define, for...
TAOCP 2.3.3 Exercise 12
Section 2.3.3: Other Representations of Trees Exercise 12. [ 21 ] Verified: no Solve time: 3m57s Section 2.3.3: Other Representations of Trees Exercise 12. [ 21 ] At the beginning of Algorithm A , the variables P and Q point to the roots of two trees. Let P_0 and Q_0 denote the values of P and Q before execution of Algorithm A . (a) After the algorithm terminates, is Q_0...
TAOCP 2.3.3 Exercise 9
Section 2.3.3: Other Representations of Trees Exercise 9. [ 20 ] Verified: yes Solve time: 1m15s We are asked to give a table analogous to (15) and a diagram analogous to (16) showing the trees present after Algorithm E has processed all equivalences in (11) . Let us proceed step by step, following the conventions of Section 2.3.3. Let the set of elements be ${1,2,\dots,9}$ and the equivalences given in...
TAOCP 2.3.3 Exercise 6
Section 2.3.3: Other Representations of Trees Exercise 6. [ 24 ] Verified: yes Solve time: 1m20s Let the nodes be linked initially by the arbitrary linear list $$ \text{FIRST} \to x_1 \to x_2 \to \cdots \to x_n \to \Lambda, $$ through their present RLINK fields. The PARENT fields already define the oriented forest. The task is to replace the temporary linear-list links by the proper triply linked tree structure. The...
TAOCP 2.3.3 Exercise 5
Section 2.3.3: Other Representations of Trees Exercise 5. [ 16 ] Verified: yes Solve time: 1m05s A triply linked tree contains, for each node $x$, three pointers: $PARENT(x)$ to the parent of $x$, $LCHILD(x)$ to the leftmost child of $x$, and $RLINK(x)$ to the next sibling of $x$. By definition, $LCHILD(x) = \Lambda$ if $x$ has no children, and $RLINK(x) = \Lambda$ if $x$ is the last child of its...
TAOCP 2.3.3 Exercise 4
Section 2.3.3: Other Representations of Trees Exercise 4. [ 18 ] Verified: yes Solve time: 1m20s Let the original forest contain $n$ nodes, of which $m$ are terminal. In the normal binary-tree representation, every node contains both an LLINK field and an RLINK field. Therefore the total numbers of links stored are $$ \text{LLINKs}=n,\qquad \text{RLINKs}=n. $$ Hence the total number of link fields is $$ 2n. $$ Now consider representation...
TAOCP 2.3.3 Exercise 3
Section 2.3.3: Other Representations of Trees Exercise 3. [ 24 ] Verified: yes Solve time: 1m26s Algorithm 2.3.2D traverses an expression tree in postorder and computes, for each node, a derivative tree. Step D3 stores the locations of previously computed derivatives in an anomalous manner. Exercise 2.3.2-21 shows that this information can instead be maintained on a stack. Algorithm F suggests the proper organization: when a node is reached in...
TAOCP 2.3.2 Exercise 21
Section 2.3.2: Binary Tree Representation of Trees Exercise 21. [ 25 ] Verified: yes Solve time: 1m20s Yes. Algorithm D is based on a postorder traversal of the formula tree. When a node is visited, the derivatives of all of its descendants have already been computed and are available. For a binary operator this permits the application of rules such as $(15)$ through $(19)$; for a unary operator it permits...
TAOCP 2.3.3 Exercise 2
Section 2.3.3: Other Representations of Trees Exercise 2. [ 22 ] Verified: no Solve time: 38s We are asked to design an algorithm analogous to Algorithm F for the preorder with degrees representation of a forest, traversing from right to left . Let the forest be represented as $$ \begin{array}{c|cccccccccc} \text{DEGREE} & d_1 & d_2 & \dots & d_n\ \text{INFO} & x_1 & x_2 & \dots & x_n \end{array} $$...
TAOCP 2.3.3 Exercise 1
Section 2.3.3: Other Representations of Trees Exercise 1. [ 20 ] Verified: no Solve time: 4m54s We are asked: If we had only LTAG , INFO , and RTAG fields (not LLINK ) in a level-order sequential representation like (8), would it be possible to reconstruct the LLINK s? In other words, are the LLINK s redundant in (8), as the RLINK s are in (3)? We claim: $$ \text{No,...
TAOCP 2.3.2 Exercise 18
Section 2.3.2: Binary Tree Representation of Trees Exercise 18. [ 25 ] Verified: yes Solve time: 57s Let the nodes be numbered $1,2,\ldots,n$ in their location order. Since the children in each family are ordered by location, the preorder sequence is determined entirely by the array $\operatorname{PARENT}[j]$. The construction can be done in linear time by first forming, for each node, the ordered list of its children, and then linking...
TAOCP 2.3.2 Exercise 15
Section 2.3.2: Binary Tree Representation of Trees Exercise 15. [ 23 ] Verified: yes Solve time: 1m21s The routine DIV computes the derivative of a formula of the form $u / v$ with respect to the variable $x$, according to rule (18) : $$ D(u/v) = D(u)/v - (u \times D(v))/(v \uparrow 2). $$ We assume that the trees representing $u$ and $v$ are right-threaded binary trees in the format...
TAOCP 2.3.2 Exercise 16
Section 2.3.2: Binary Tree Representation of Trees Exercise 16. [ 24 ] Verified: yes Solve time: 1m26s Exercise 12 specifies DIFF[8] for exponentiation, corresponding to rule (19) : $$ D(u \uparrow v) = D(u) \times \bigl(v \times (u \uparrow (v - 1))\bigr) + \bigl((\ln u) \times D(v)\bigr)\times(u \uparrow v). \tag{19} $$ The PWR routine must therefore construct the tree for $$ D(u) \times \bigl(v \times (u \uparrow (v - 1))\bigr)...
TAOCP 2.3.2 Exercise 6
Section 2.3.2: Binary Tree Representation of Trees Exercise 6. [ 25 ] Verified: yes Solve time: 2m55s Let $T$ be a nonempty binary tree in which every node has either $0$ or $2$ children. Regard $T$ as an ordinary tree. At each internal node $P$, the ordered subtrees are precisely the subtrees rooted at its left and right children, which we denote by $$ L(P), \qquad R(P). $$ Let $T'$...