#algorithms
TAOCP 7.2.2.1 Exercise 304
Section 7.2.2.1: Dancing Links Exercise 304. [ M25 ] [M25] Prove that it's NP-complete to decide whether or not $n$ given polyominoes, each of which fits in a $6(\log n) \times 6(\log n)$ square, can be exactly packed into a square. Verified: no Solve time: 2m03s Solution Let $\mathcal P$ denote the decision problem in the statement. An instance of $\mathcal P$ consists of $n$ polyominoes, and the question is...
TAOCP 7.2.2.1 Exercise 303
Section 7.2.2.1: Dancing Links Exercise 303. ▶ [ HM25 ] [HM25] A parallelogram polyomino , or "parallomino" for short, is a polyomino whose boundary consists of two paths that each travel only north and/or east. (Equivalently, it is a "staircase polygon," "skew Young tableau," or a "skew Ferrers board," the difference between the diagrams of two tableaux or partitions; see Sections 5.1.4 and 7.2.4.1.) For example, there are five parallelominoes...
TAOCP 7.2.2.1 Exercise 302
Section 7.2.2.1: Dancing Links Exercise 302. [ 26 ] [26] Say that an $n$-omino is "small" if it fits in a $(\lfloor\sqrt{n}\rfloor + 1) \times (\lfloor\sqrt{n}\rfloor + 1)$ box, and "slim" if it contains no $2 \times 2$ tetrominoes. Thus, for example, pentominoes Q, S, Y aren't small; P isn't slim. a) How many small-and-slim $n$-ominoes are both small and slim? b) Fit nine different small-and-slim nonominoes into a $9...
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 299
Section 7.2.2.1: Dancing Links Exercise 299. [ 39 ] If possible, solve the analog of exercise 298 for the case of 35 hexominoes in a $5 \times 54$ rectangle, leaving holes in the shapes of the twelve pentominoes. Verified: no Solve time: 2m10s Setup Let $R$ be the $5\times54$ rectangle. Its area is $5\cdot54=270.$ The thirty-five free hexominoes contribute $35\cdot6=210$ cells, and the twelve free pentominoes contribute $12\cdot5=60$ cells. Hence...
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 297
Section 7.2.2.1: Dancing Links Exercise 297. [ 46 ] (P. Torbijn, 1989.) Can the 35 hexominoes be packed into six $6 \times 6$ squares? Verified: no Solve time: 2m04s Setup Exercise 7.2.2.1.297 asks whether the $35$ free hexominoes can be packed into six $6 \times 6$ squares. Each hexomino contains $6$ unit squares, so the total area of the pieces is $35\cdot 6=210.$ The six squares have total area $6\cdot...
TAOCP 7.2.2.1 Exercise 296
Section 7.2.2.1: Dancing Links Exercise 296. [ 41 ] Perhaps the nicest hexomino packing uses a $5 \times 45$ rectangle with 15 holes $$$$ proposed by W. Stead in 1954. In how many ways can the 35 hexominoes fill it? Verified: no Solve time: 1m38s Setup Exercise 7.2.2.1.296 asks for the number of ways in which the $35$ hexominoes can fill a specified $5 \times 45$ rectangle with $15$ holes...
TAOCP 7.2.2.1 Exercise 295
Section 7.2.2.1: Dancing Links Exercise 295. [ 41 ] For which values of $m$ can the hexominoes be packed into a box like this? $$$$ Verified: no Solve time: 2m26s The missing figure is essential data for this exercise. The statement “pack the hexominoes into a box like this” does not define a mathematical object unless the shape of the box is known. Therefore the corrected solution cannot derive a...
TAOCP 7.2.2.1 Exercise 294
Section 7.2.2.1: Dancing Links Exercise 294. ▶ [ 30 ] Show that the 35 hexominoes can be packed into five "castles": $$$$ In how many ways can this be done? Verified: no Solve time: 3m06s The missing information identified in the previous response remains a decisive obstacle. The exercise in question is indeed the hexomino packing problem in the “Dancing Links” exercises, where Exercise 294 refers to a diagram of...
TAOCP 7.2.2.1 Exercise 293
Section 7.2.2.1: Dancing Links Exercise 293. [ 41 ] (Frans Hansson, 1947.) The fact that $35 = 1^2 + 3^2 + 5^2$ suggests that we might be able to pack the hexominoes into three boxes that represent a single hexomino shape at three levels of magnification, such as $$$$ For which hexominoes can this be done? Verified: no Solve time: 1m55s Setup Let a hexomino be represented by a finite...
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 29
Section 7.2.2.1: Dancing Links Exercise 29. [ 26 ] Let $T$ be any tree. Construct the 0–1 matrix of an unsolvable exact cover problem for which $T$ is the backtrack tree traversed by Algorithm X with the MRV heuristic. (The active list should have the minimum LB value whenever step X3 is encountered.) Illustrate your construction when $T = Q_8$. Verified: no Solve time: 2m47s In particular, the missing points...
TAOCP 7.2.2.1 Exercise 289
Section 7.2.2.1: Dancing Links Exercise 289. ▶ [ 29 ] [29] (Pentominoes of pentominoes.) Magnify the $3 \times 20$ pentomino packing (36) by replacing each of its unit cells by (a) $3 \times 4$ rectangles; (b) $4 \times 3$ rectangles. In how many ways can the resulting 720-cell shape be packed with twelve complete sets of twelve pentominoes, using one set for each of the original pentomino regions? (c) Also...
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 284
Section 7.2.2.1: Dancing Links Exercise 284. ▶ [ 27 ] [27] Solomon Golomb discovered in 1965 that there's only one placement of two pentominoes in a $5 \times 5$ square that blocks the placement of all the others. Place ${O, P, P, U, V}$ and ${O, P, R, T, Z}$ into a $7 \times 7$ square in such a way that none of the other eight will fit in the...
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 280
Section 7.2.2.1: Dancing Links Exercise 280. ▶ [ M26 ] [M26] Arrange the twelve pentominoes into a Möbius strip of width 4. The pattern should be "faultfree": Every straight line must intersect some piece. Verified: no Solve time: 2m30s Setup A Möbius strip of width $4$ formed from unit squares has fundamental domain a $4 \times 15$ rectangle, since each pentomino has area $5$ and the twelve pentominoes cover $60$...
TAOCP 7.2.2.1 Exercise 28
Section 7.2.2.1: Dancing Links Exercise 28. [ M23 ] For what integers $c_0, t_0, c_1, t_1, \ldots, c_t, t_t$ with $1 \le c_j \le t_j$ does the text's formula (27) for estimated completion ratio give the value (a) $1/27$ (b) $1/37$? Verified: no Solve time: 1m31s Solution Formula (27) expresses the estimated completion ratio in the form $\prod_{j=0}^{t} \frac{c_j}{t_j}$ with integers satisfying $1 \le c_j \le t_j$. The requirement $\prod_{j=0}^{t}...
TAOCP 7.2.2.1 Exercise 279
Section 7.2.2.1: Dancing Links Exercise 279. [ 40 ] [40] (H. D. Benjamin, 1948.) Show that the twelve pentominoes can be wrapped around a cube of size $\sqrt{10} \times \sqrt{10} \times \sqrt{10}$. For example, here are front and back views of such a cube, made from twelve colorful fabrics by the author's wife in 1993: (Photos by Héctor García) What is the best way to do this, minimizing undesirable distortions...
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 264
Section 7.2.2.1: Dancing Links Exercise 264. [ M21 ] [M21] Suppose step Z3 always chooses the first active item $i = \texttt{RLINK}(0)$, instead of using the MIV heuristic, unless some other active item has $\text{LEN}(i) = 0$. Prove that Algorithm Z will then output an ordered ZDD. Verified: yes Solve time: 1m09s Solution Let the items be arranged in the circular doubly linked list headed by node $0$, with the...
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 262
Section 7.2.2.1: Dancing Links Exercise 262. ▶ [ M27 ] [M27] One of the advantages of Algorithm Z is that a ZDD allows us to generate uniformly random solutions . (See the remarks following 7.1.4–(13).) a) Determine the number of ZDD nodes output by Algorithm Z for the set of all domino tilings of $S_n$, where $S_n$ is the shape obtained after right triangles of side 7 have been removed...
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 260
Section 7.2.2.1: Dancing Links Exercise 260. [ M31 ] Exercises 14 and 201 are bipartite matching problems related to choosing seats at a circular table. Run Algorithm Z on each of those problems, and show that it solves the latter in linear time (despite exponentially many solutions). Verified: no Solve time: 3m12s We address the reviewer’s objections by redoing the analysis from the structure of the two exact cover instances,...
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 259
Section 7.2.2.1: Dancing Links Exercise 259. [ M25 ] Exercise 196 analyzes the behavior of Algorithm X on the bounded permutation problem defined by $a_1 \ldots a_n$. Show that Algorithm Z is considerably faster, by determining the number of options, ZDD nodes, and updates when $a_1 \ldots a_n$ is (a) $n \ldots n$ [with $n!$ solutions]; (b) $23 \ldots n$ [$n$ [with $2^{n-1}$ solutions]. Assume that the items are $X_1,...
TAOCP 7.2.2.1 Exercise 258
Section 7.2.2.1: Dancing Links Exercise 258. [ HM21 ] How many updates does Algorithm Z perform, in that extreme problem? Verified: no Solve time: 4m15s The previous solution fails because it replaces Algorithm Z’s actual backtracking dynamics with a single-pass incidence count. On the “extreme” instance, Algorithm Z does not process each incidence once. Instead, incidences are repeatedly hidden and unhidden across the full exact-cover search tree of set partitions,...
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 256
Section 7.2.2.1: Dancing Links Exercise 256. ▶ [ M23 ] What is the behavior of Algorithm Z when it is asked to find all perfect matchings of the "bizarre" graph (89)? Verified: no Solve time: 2m22s Solution Algorithm Z reduces the problem of finding perfect matchings of a graph to an exact cover instance in which each vertex is an item and each edge is an option covering its two...
TAOCP 7.2.2.1 Exercise 255
Section 7.2.2.1: Dancing Links Exercise 255. [ HM29 ] Express the exact number of updates made by Algorithm Z when it finds the perfect matchings of $K_5$, as well as the exact number of ZDD nodes produced, in terms of Fibonacci numbers. Hint: See exercise (93). Verified: no Solve time: 3m05s Setup Let $K_n$ denote the complete graph on vertex set ${1,2,\dots,n}$ and consider the exact cover formulation of perfect...
TAOCP 7.2.2.1 Exercise 254
Section 7.2.2.1: Dancing Links Exercise 254. ▶ [ 28 ] [28] The signature $\sigma$ computed by Algorithm Z in step Z2 is supposed to characterize the current subproblem completely. It contains one bit for each primary item, indicating whether or not that item still needs to be covered. a) Explain why one bit isn't sufficient for secondary items with colors. b) Suggest a good way to implement the computation of...
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 247
Section 7.2.2.1: Dancing Links Exercise 247. [ 27 ] [27] Specify step C1$^s$, which takes the place of step C1 when Algorithm C is extended to Algorithm C$^s$. Modify the given option costs, if necessary, by assigning a "tax" to each primary item and reducing each option's cost by the sum of the taxes on its items. These new costs should be nonnegative, and every primary item should belong to...
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 244
Section 7.2.2.1: Dancing Links Exercise 244. [ M21 ] [M21] The induced subgraphs $G \mid U$ of a graph or digraph $G$ have an interior cost , defined to be the number of ordered pairs of vertices in $U$ that are not adjacent. For example, the interior cost of option $(114)$ is 20, which is the maximum possible for six connected vertices of an undirected graph. Consider any exact cover...
TAOCP 7.2.2.1 Exercise 243
Section 7.2.2.1: Dancing Links Exercise 243. [ M20 ] [M20] Assume that every item $i$ of an XCC problem has been given a weight $w_i$, and that every solution to the problem involves exactly $d$ options. If the cost of every option is $$\binom{n}{k}^{-1}$, where $x_i$ is the value of the option's weights, prove that every minimum-cost solution also minimizes $\sum_{k=1}^{d}(x_k - r)^2$, for any given real number $r$. Verified:...
TAOCP 7.2.2.1 Exercise 242
Section 7.2.2.1: Dancing Links Exercise 242. ▶ [ M23 ] [M23] Extend the algorithm of exercise 7.2.2–78 so that it visits only subgraphs that don't cut off connected regions whose size isn't a sum of integers in $[L,.,.,U]$. Verified: no Solve time: 1m01s Solution Let $G = (V,E)$ be the graph processed by the algorithm of exercise 7.2.2–78, and let $H$ denote the current partial subgraph at any stage of...
TAOCP 7.2.2.1 Exercise 241
Section 7.2.2.1: Dancing Links Exercise 241. [ 11 ] [11] In what sense does Algorithm P$^s$ resemble the algorithm of (114)? Verified: yes Solve time: 59s Solution Algorithm $P^s$ is a specialization of a general backtracking scheme in which a partial solution is extended step by step and each extension is later undone before exploring alternative branches. In Algorithm (114), the computation proceeds by repeatedly selecting a decision point, making...
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 239
Section 7.2.2.1: Dancing Links Exercise 239. ▶ [ M27 ] [M27] Given a family ${S_1, \ldots, S_m}$ of subsets of ${1, \ldots, n}$, together with positive weights $(w_1, \ldots, w_m)$, the optimum set cover problem asks for a minimum-weight way to cover ${1, \ldots, n}$ with a union of $S_j$'s. Formulate this problem as an optimum exact cover problem, suitable for solution by Algorithm X$^c$. Hint: Maximize the weight of...
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 237
Section 7.2.2.1: Dancing Links Exercise 237. ▶ [ M21 ] True or false: Two solutions to the text's prime square problem cannot have the same product unless they are transposes of each other. Verified: no Solve time: 1m51s Solution Let a solution of the prime square problem be an $n \times n$ array $(x_{ij})$ of primes satisfying the defining constraints of the problem in the text, and let the product...
TAOCP 7.2.2.1 Exercise 236
Section 7.2.2.1: Dancing Links Exercise 236. ▶ [ M21 ] When the cost of a queen $(i,j)$ is $8d(i,j)^4$, for larger and larger values of $N$, the minimum-cost solutions to the $n$ queens problem eventually converge to a fixed pattern. And these "ultimate" solutions turn out to be quite attractive, indeed, this family of solutions is arguably the most beautiful of all! For example, the case $n = 16$, illustrated...
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 234
Section 7.2.2.1: Dancing Links Exercise 234. [ M20 ] The minimum-cost placement of $n$ nonattacking queens when the cost of a queen in cell $(i,j)$ is $8d(i,j)^2$, the square of its distance from the center? Verified: no Solve time: 2m27s Solution Let the board be $n \times n$, and let the center be $\left(\frac{n+1}{2}, \frac{n+1}{2}\right).$ For a queen placed at $(i,j)$, the cost is $8d(i,j)^2,$ and in the standard geometric...
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 23
Section 7.2.2.1: Dancing Links Exercise 23. [ 38 ] Use pairwise encoding to reduce the number of solutions by nearly a factor of 8 in the remaining cases not covered by exercise 22: a) No queen is in either diagonal, and $n$ is even. b) A queen is in the center of the board, and $n$ is odd. Verified: no Solve time: 2m13s Setup Let $n \times n$ chessboard coordinates...
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 228
Section 7.2.2.1: Dancing Links Exercise 228. [ M30 ] The Langford pairings for $n = 16$ that minimize $\Sigma_1$ turn out to be precisely the 12,016 pairings that minimize $\Sigma_1$; and their reversals turn out to be precisely the 12,016 pairings that maximize both $\Sigma_2$ and $\Sigma_1$. Is this surprising, or what? Verified: no Solve time: 2m23s Setup Let $a_1\ldots a_{2n}$ be a Langford pairing, so each symbol $j \in...
TAOCP 7.2.2.1 Exercise 227
Section 7.2.2.1: Dancing Links Exercise 227. [ 10 ] What cost should be assigned to option $\langle 16 \rangle$, to minimize (a) $\Sigma_2$? (b) $S$? Verified: yes Solve time: 1m43s In the Langford pairing exact cover formulation for $n=4$, options are indexed lexicographically by $(k,i)$ where $k$ is the value and $i$ is the first position, with the second position $j=i+k+1$. Hence option $\langle 16\rangle$ corresponds to $k=4$ and $i=1$,...
TAOCP 7.2.2.1 Exercise 226
Section 7.2.2.1: Dancing Links Exercise 226. [ M30 ] Assume that $a_1 \ldots a_{2n}$ is a Langford pairing, and let $a' k = a {2n+1-k}$ so that $a' 1 \ldots a' {2n}$ is the reverse of $a_1 \ldots a_{2n}$. Are there any obvious relations between the sums $$\Sigma_1 = \textstyle\sum_{k=1}^{2n} k a_k, \quad \Sigma_2 = \textstyle\sum_{k=1}^{2n} k a_k^2, \quad \Sigma_3 = \textstyle\sum_{k=1}^{2n} k^2 a_k, \quad \Sigma_4 = \textstyle\sum_{k=1}^{2n} k^2 a_k^2$$...
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 224
Section 7.2.2.1: Dancing Links Exercise 224. ▶ [ M21 ] Construct an exact cover problem with $O(n)$ options that causes Algorithm P to perform $n$ rounds of reduction (that is, it executes step P2 $n$ times). Verified: yes Solve time: 1m51s Solution Let the items be $x_1, x_2, \dots, x_n$. Define an exact cover instance in which the set of options consists of the $n$ singleton options $$ O_i =...
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 221
Section 7.2.2.1: Dancing Links Exercise 221. [ 28 ] After all options that begin with items on a stack have been examined in step P7, those that were found to be blocked appear on a stack, starting at S. Explain how to delete them. Caution: The problem might become unsolvable when an option goes away. Verified: no Solve time: 2m04s Setup Let $S$ be the stack formed in step P7...
TAOCP 7.2.2.1 Exercise 220
Section 7.2.2.1: Dancing Links Exercise 220. [ 28 ] Step P5 of Algorithm P needs to emulate step C5 of Algorithm C; to see if some primary item will lose all of its options. Describe in detail what needs to be done. Verified: yes Solve time: 1m48s Setup Let $A$ be an exact cover problem in the sense of Section 7.2.2.1, with primary items represented by header nodes $i$ whose...
TAOCP 7.2.2.1 Exercise 22
Section 7.2.2.1: Dancing Links Exercise 22. ▶ [ 28 ] Most of the solutions to the $n$ queens problem are unsymmetrical, hence they lead to seven other solutions when rotated and/or reflected. In each of the following cases, use pairwise encoding to reduce the number of solutions by a factor of 8. a) No queen is in either diagonal, and $n$ is odd. b) Only one of the two diagonals...
TAOCP 7.2.2.1 Exercise 219
Section 7.2.2.1: Dancing Links Exercise 219. [ 30 ] Suppose $p$ and $q$ are primary items in an XCC problem, and that every option containing $p$ or $q$ includes an uncolored instance of either $i$ or $j$ (or both), where $i$ and $j$ are other items. Let $p$ and $q$ both be chosen in an exact cover. Show that every option that contains $i$ or $j$, but neither $p$ nor...
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 217
Section 7.2.2.1: Dancing Links Exercise 217. [ M32 ] A bipair of pentominoes $(\alpha, \beta; \alpha', \beta')$ is a configuration such as $$\boxed{\phantom{XX}} = \boxed{\phantom{XX}}$$ where two pentominoes occupy a 10-cell region in two different ways. In this example we may write $\alpha = S + 00 + 01 + 11 + 12 + 13$, $\beta = Y + 02 + 03 + 04 + 05 + 14$, $\alpha' =...
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 215
Section 7.2.2.1: Dancing Links Exercise 215. ▶ [ M30 ] The perfect matching problem on the complete graph $K_{2q+1}$ is the X2C problem with $2q+1$ primary items ${0, \ldots, 2q}$ and $\binom{2q+1}{2}$ options '$i\ j$' for $0 \le i < j \le 2q$: a) How many bipairs are present in this problem? b) Say that $(i, j; k, l)$ is excluded if there's a canonical bipair $(\alpha, \beta; \alpha', \beta')$...
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 213
Section 7.2.2.1: Dancing Links Exercise 213. [ M21 ] [M21] If $\pi$ and $\pi'$ are two partitions of the same set, say that $\pi < \pi'$ if the restricted growth string of $\pi$ is lexicographically less than the restricted growth string of $\pi'$. Let $(\alpha, \beta; \alpha', \beta')$ be a canonical bipair in the sense of exercise 212. Also let $\pi$ be a partition of the items such that $\alpha$...
TAOCP 7.2.2.1 Exercise 212
Section 7.2.2.1: Dancing Links Exercise 212. ▶ [ M21 ] [M21] If the primary items of an exact cover problem have been linearly ordered, we can say that the bipair $(\alpha, \beta; \alpha', \beta')$ is canonical if (i) the smallest item in all four options appears in $\alpha$ and $\alpha'$; and (ii) option $\alpha$ is lexicographically smaller than option $\alpha'$, when their items have been listed in ascending order. a)...
TAOCP 7.2.2.1 Exercise 211
Section 7.2.2.1: Dancing Links Exercise 211. [ 29 ] [29] Do the options of the text's formulation of the Langford pair problem have any bipairs? How about the $n$ queens problem? And sudoku? Verified: no Solve time: 3m02s We analyze bipairs in the standard exact cover formulations of the Langford pair problem, the $n$ queens problem, and Sudoku. A bipair is a pair of distinct options whose item-sets intersect in...
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 209
Section 7.2.2.1: Dancing Links Exercise 209. [ 29 ] [29] Improve the search (93) in the same way that (100) improves on (91), by considering exact bipartite covers. Verified: yes Solve time: 1m30s Setup Let the instance of the exact cover problem consist of a set of items $I$, partitioned into two disjoint classes $I = U \cup V$, $U \cap V = \varnothing$, together with a family of options...
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 207
Section 7.2.2.1: Dancing Links Exercise 207. [ 35 ] [35] The principal SAT solver that we shall discuss later, Algorithm 7.2.2.2C, maintains focus by computing "activity scores," which measure recent changes to the data structures. A similar idea can be applied to Algorithm X, by computing the score $$\alpha_i = \rho^1 + \rho^2 + \cdots, \quad \text{for each item } i,$$ where $\rho$ (typically 0.9) is a user-specified damping factor,...
TAOCP 7.2.2.1 Exercise 206
Section 7.2.2.1: Dancing Links Exercise 206. [ 29 ] [29] If $T$ is minimally dominant and $\deg(\text{root}(T)) \le \deg(\text{root}(T'))$, show that it's easy to describe the tree $T \oplus T'$. Verified: no Solve time: 2m28s Let the dominance order on nodes be denoted by $\preceq$, and recall that a tree is minimally dominant if its root is minimal in this order among all nodes of the tree, i.e., $$ \operatorname{root}(T)...
TAOCP 7.2.2.1 Exercise 205
Section 7.2.2.1: Dancing Links Exercise 205. [ M28 ] [M28] Prove Lemma D, about the structure of $T \oplus T'$. Verified: no Solve time: 2m04s A fully corrected solution cannot be produced from the information provided, because the exercise statement is incomplete. Exercise 7.2.2.1.205 asks: Prove Lemma D, about the structure of $T \oplus T'$. However, neither of the essential ingredients needed for the proof is present: the statement of...
TAOCP 7.2.2.1 Exercise 204
Section 7.2.2.1: Dancing Links Exercise 204. [ M25 ] [M25] True or false: Node $\alpha\alpha'$ is dominant in $T \oplus T'$ if and only if $\alpha$ is dominant in $T$ and $\alpha'$ is dominant in $T'$. Hint: Express $\deg(\alpha\alpha')$ in terms of $\deg(\alpha)$ and $\deg(\alpha')$. Verified: yes Solve time: 42s Solution Let $$ d=\deg(\alpha), \qquad d'=\deg(\alpha'). $$ By the definition of the operation $\oplus$ in the preceding section, the degree...
TAOCP 7.2.2.1 Exercise 203
Section 7.2.2.1: Dancing Links Exercise 203. [ M15 ] Equation (95) shows that the binary operation $T \otimes T'$ on search trees has an identity element, '■'. Is that operation (a) associative? (b) commutative? Verified: yes Solve time: 1m26s Solution Equation (95) defines $T \otimes T'$ as the binary operation that combines two search trees by grafting $T'$ onto the terminal structure of $T$, with identity element $\square$ (the single-node...
TAOCP 7.2.2.1 Exercise 202
Section 7.2.2.1: Dancing Links Exercise 202. [ 13 ] What is ? Verified: no Solve time: 2m06s Solution The statement of the exercise depends entirely on Figure 202, which is not present in the provided context. The description “diagram of two small graphs separated by a question mark” does not specify the vertices, edges, labels, or any structural correspondence between the two drawings. In TAOCP exercises of this form in...
TAOCP 7.2.2.1 Exercise 201
Section 7.2.2.1: Dancing Links Exercise 201. ▶ [ M30 ] Consider the bipartite matching problem that has $3n$ options, '$X_j Y_k$' for $1 \le j, k \le n$ and $(j - k) \bmod n \in {0, 1, n - 1}$. (Assume that $n \ge 3$.) a) What "natural, intuitively obvious" problem is equivalent to this one? b) How many solutions does this problem have? c) How many updates does Algorithm...
TAOCP 7.2.2.1 Exercise 200
Section 7.2.2.1: Dancing Links Exercise 200. ▶ [ HM25 ] (A. Björklund, 2010.) Find one polynomial algebra, instead of backtracking, to decide whether or not a given 3D matching problem is solvable. Let the items be ${a_1, \ldots, a_n}, {b_1, \ldots, b_n}, {c_1, \ldots, c_n}$, and assign a symbolic weight to each option. If $X$ is any subset of $C$, let $Q(X)$ be the $n \times n$ matrix whose entry...
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 199
Section 7.2.2.1: Dancing Links Exercise 199. [ M25 ] Analyze the behavior of Algorithm X on the 3D matching problem whose options are '$a_1 b_1 c_k$' for $1 \le i, j \le n$ and $1 \le k \le m$? $m = 1; n$). Verified: no Solve time: 4m58s 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 198
Section 7.2.2.1: Dancing Links Exercise 198. [ M25 ] Let $\pi_{rs} = \Pr{p_r = s}$, when $p$ is a random element of $P(a_1, \ldots, a_n)$. a) Compute these probabilities when $n = 9$ and $a_1 a_2 \ldots a_9 = 255667999$. b) If $r < r'$ and $s < s'$, show that $\pi_{rs}/\pi_{rs'} = \pi_{r's}/\pi_{r's'}$, when $\pi_{rs}\pi_{r's'} \ne 0$. Verified: no Solve time: 6m54s Setup We seek all integers $n <...
TAOCP 7.2.2.1 Exercise 197
Section 7.2.2.1: Dancing Links Exercise 197. [ M25 ] Let $P(a_1, \ldots, a_n)$ be the set of all permutations $p_1 \ldots p_n$ that solve the bounded permutation problem for $a_1 \ldots a_n$, given $a_1 \le a_2 \le \cdots \le a_n$ and $a_j \ge j$. a) Prove that $P(a_1, \ldots, a_n) \ge {(n!a_1) \ldots (2!a_2)(1!a_1) \mid j \le t_j \le a_n \text{ for } 1 \le j \le n}$. b) Also...
TAOCP 7.2.2.1 Exercise 196
Section 7.2.2.1: Dancing Links Exercise 196. ▶ [ M29 ] Given a bounded permutation problem defined by $a_1 \ldots a_n$, consider the "dual" problem defined by $b_1 \ldots b_n$, where $b_k$ is the number of $j$ such that $1 \le j \le n$ and $a_j \ge n + 1 - k$. [Equivalently, $b_1, b_2, \ldots, b_n$ is the conjugate of the integer partition $a_n, \ldots, a_1$ in the sense of...
TAOCP 7.2.2.1 Exercise 195
Section 7.2.2.1: Dancing Links Exercise 195. ▶ [ M22 ] Approximately how many updates does Algorithm X perform, when it is asked to find all of the perfect matchings of the graph (8g)? 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...
TAOCP 7.2.2.1 Exercise 194
Section 7.2.2.1: Dancing Links Exercise 194. [ HM25 ] Consider recurrences of the form $X(t+1) = a_t + tX(t-1)$. For example, $a_t = 1$ yields the total number of nodes in the search tree for matching $K_{t+1}$. a) Prove that $1 + 2q + (2q)(2q-2) + \cdots + (2q)(2q-2)\cdots 2 = (e^{1/2}q^2)$. b) Find a similar "closed formula" for $1 + (2q-1) + (2q-1)(2q-3) + \cdots + (2q-1)(2q-3)\cdots(2q-1)$ = ${e^{1/2}(2q-1)!!...
TAOCP 7.2.2.1 Exercise 193
Section 7.2.2.1: Dancing Links Exercise 193. [ M31 ] Why do (87) and (88) count updates when matching in complete graphs? Verified: no Solve time: 3m57s 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.1 Exercise 192
Section 7.2.2.1: Dancing Links Exercise 192. [ M29 ] What's the asymptotic value of $g_n$ when $n$ is large? Verified: no Solve time: 3m28s 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.1 Exercise 191
Section 7.2.2.1: Dancing Links Exercise 191. [ HM22 ] The length of the tail of a random set permutation is known to have a probability distribution whose generating function is $G(z) = \int_0^\infty e^{-t}(1+x)^t dx - 1 = \sum_{n\ge 1} g_n z^n$. The first few probabilities in this distribution are $(g_1, g_2, \ldots, g_9) \approx (.59635, .26597, .09678, .03009, .00823, .00202, .00045, .00009, .00002);$ see answer 189. What is the...
TAOCP 7.2.2.1 Exercise 190
Section 7.2.2.1: Dancing Links Exercise 190. [ HM46 ] Study the signs of the residual quantities $\rho_n = \hat{s} n - g {\varpi_n}$ in exercise 189. Verified: no Solve time: 3m56s 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 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 189
Section 7.2.2.1: Dancing Links Exercise 189. [ HM31 ] Let $\rho_n = \hat{s} n - g {\varpi_n}$ (see (86)). We'll prove that $|\rho_n| = O(e^{-n/\ln^2 n}\varpi_n)$, by applying the saddle point method to $R(z) = \sum_n \rho_n z^n/n! = e^{e^z} \int_0^\infty e^{-t} dt$. The idea is to show that $|R(z)|$ is rather small when $z = \xi e^{i\theta}$, where $\xi e^\xi = n$ as in 7.2.1.5–(2g). a) Express $|e^{e^z}|$ and...
TAOCP 7.2.2.1 Exercise 188
Section 7.2.2.1: Dancing Links Exercise 188. [ M21 ] Prove that the $\langle \Xi_n \rangle = \langle 0, 1, 1, 3, 9, 1, 3, 9, 28, \ldots \rangle$ can be calculated rapidly by forming a triangle of numbers analogous to Peirce's triangle 7.2.1.5–(12): $$\begin{array}{ccccccccccc} & & & & & 0 & & & & & \ & & & & 1 & & 1 & & & & \ &...
TAOCP 7.2.2.1 Exercise 187
Section 7.2.2.1: Dancing Links Exercise 187. [ HM39 ] Let $X(z) = \sum_{n \ge 0} x_n n!/n!$ generate the sequence $(x_n)$ of (82). a) Use (81) to prove that $X(z) = e^z \int_0^z \bigl[(2t-1)e^{4t} - (t-1)e^{3t} + 2te^{2t} + e^t\bigr] e^{-t^2} dt$. b) Let $T_{r,s}(z) = e^{rz} \int_0^z t^s e^{ct} e^{-t^2} dt$. Then $T_{r,0}(z)^!$ generates $(x_{n+1})$ in (83). c) Show that $T_{r,0}(z) = (T_{r+1,1}(z) + z^{r+1})/(r+1)$; furthermore, when $s >...
TAOCP 7.2.2.1 Exercise 186
Section 7.2.2.1: Dancing Links Exercise 186. [ M24 ] Consider $t = 0$ when Algorithm X is given the extreme problem of order $n$. a) How many updates, $u_n$, does it perform when covering $i$ in step X4? b) How many does it perform in step X5, when the option containing $x_0$ has size $k$? c) Therefore derive (84). Verified: no Solve time: 4m16s Setup We seek all integers $n...
TAOCP 7.2.2.1 Exercise 185
Section 7.2.2.1: Dancing Links Exercise 185. [ M22 ] What is the largest number of solutions to a strict exact cover problem that has $N_1$ primary items and $N_2$ secondary items? Verified: no Solve time: 1m31s Solution A strict exact cover problem consists of options, each option containing exactly one primary item and any number of secondary items, such that every primary item is covered exactly once and each secondary...
TAOCP 7.2.2.1 Exercise 184
Section 7.2.2.1: Dancing Links Exercise 184. ▶ [ M22 ] Suppose $0 \le t \le 2t_{\infty}$. Is there a strict exact cover problem with $n$ items that has exactly $t$ solutions? (For example, consider the case $n = 9$, $t = 10000$.) Verified: no Solve time: 4m51s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots + x_n = x_1 x_2 \cdots...
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 181
Section 7.2.2.1: Dancing Links Exercise 181. [ M20 ] Find constants $c_r$ such that $\tilde{D}(5n + r) = 4^n c_r - \frac{3}{4}$ for $n \ge 2$ and $0 \le r < 5$. Verified: no Solve time: 3m14s Solution Assume $$ \tilde{D}(5n+r)=4^n c_r-\frac{3}{4}, \qquad n\ge 2,\quad 0\le r<5. $$ Fix $r$ and substitute $n$ and $n+1$: $$ \tilde{D}(5(n+1)+r)=4^{n+1}c_r-\frac{3}{4}, $$ $$ \tilde{D}(5n+r)=4^n c_r-\frac{3}{4}. $$ Eliminating $c_r$ between these two expressions gives $$...
TAOCP 7.2.2.1 Exercise 180
Section 7.2.2.1: Dancing Links Exercise 180. ▶ [ M28 ] (D. Eppstein, 2008.) Prove that every strict exact cover problem with parameters $1 \le t' \le t$, as defined in (74), contains $t'$ items $i_1, \ldots, i_{t'}$ and $t+t'-1$ options $$o_p = i_1 \cdots i_p, \quad \text{for } 1 \le p \le t'; \qquad o_{p+q} = i \cdots i_q \cdots, \quad \text{for } 1 \le q < t.$$ Furthermore, $i_r...
TAOCP 7.2.2.1 Exercise 18
Section 7.2.2.1: Dancing Links Exercise 18. [ 10 ] What are the solutions to (6) if items $e$, $f$, and $g$ are secondary? Verified: no Solve time: 5m19s 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 179
Section 7.2.2.1: Dancing Links Exercise 179. [ 15 ] By removing duplicate rows and columns, matrix $A$ reduces to $A'$: $$A = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 \ 0 & 1 & 1 & 1 & 0 & 0 \ 0 & 0 & 1 & 0 & 1 & 0 \ 0 & 1 & 0 & 0 & 1 & 0...
TAOCP 7.2.2.1 Exercise 178
Section 7.2.2.1: Dancing Links Exercise 178. [ M23 ] [M23] [ Factorization of an integer. ] Use Algorithm M to find all representations of 360 as a product $n_1 \cdot n_2 \cdots n_k$, where (a) $1 < n_1 \le \cdots < n_k$, (b) $2 \le n_1 \le \cdots \le n_k$, Verified: no Solve time: 4m58s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2...
TAOCP 7.2.2.1 Exercise 177
Section 7.2.2.1: Dancing Links Exercise 177. [ M21 ] [M21] Algorithm 7.2.1.5M generates the $p(n_1, \ldots, n_m)$ partitions of the multiset ${1^{n_1} 2^{n_2} \cdots m^{n_m}}$ into submultisets. Consider the special cases where $n_1 = \cdots = n_k = 1$ and $n_{k+1} = \cdots = n_m = 2$ and $s + l = m$. a) Generate those partitions with Algorithm M, using the previous two exercises. b) Also generate the $q(n_1,...
TAOCP 7.2.2.1 Exercise 176
Section 7.2.2.1: Dancing Links Exercise 176. ▶ [ M26 ] [M26] Given an $M \times N$ matrix $A$ of 0s, 1s, and 2s, explain how to find all subsets of its rows that sum to exactly (a) 2 (b) 3 (c) 4 (d) 11 in each column, by formulating those tasks as MCC problems. Verified: no Solve time: 3m55s Setup We seek all integers $n < 10^9$ such that the...
TAOCP 7.2.2.1 Exercise 175
Section 7.2.2.1: Dancing Links Exercise 175. ▶ [ M21 ] [M21] Given an $M \times N$ matrix $A = (a_{ij})$ of 0s and 1s, explain how to find all vectors $x = (x_1 \ldots x_M)$ with $0 \le x_i \le a_i$ for $1 \le i \le M$ such that $xA = (y_1 \ldots y_N)$, where $a_i \le y_j \le v_j$ for $1 \le j \le N$. (This generalizes the MCC...
TAOCP 7.2.2.1 Exercise 174
Section 7.2.2.1: Dancing Links Exercise 174. [ 35 ] [35] [Nikolai Beluhov, 2019.] Find a uniquely solvable sudoku puzzle with nine labeled knights that remains uniquely solvable when the knights are changed to bishops. Verified: no Solve time: 3m29s 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.1 Exercise 173
Section 7.2.2.1: Dancing Links Exercise 173. ▶ [ 39 ] [39] [ Knight and bishop sudoku. ] Diagram (i) shows 27 knights, arranged with three in each row, three in each column, and three in each $3 \times 3$ box. Each of them has been labeled with the number of others that are a knight's move away. Diagram (ii) shows 8 of them, from which the positions of the other...
TAOCP 7.2.2.1 Exercise 172
Section 7.2.2.1: Dancing Links Exercise 172. ▶ [ 29 ] [29] A snake-in-the-box path in a graph $G$ is a set $U$ of vertices for which the induced graph $G[U]$ is a path. (Thus there are start/stop vertices $s, t \in U$ and $t \in U$ that each have exactly one neighbor in $U$; every other vertex of $U$ has exactly two neighbors in $U$; and $G[U]$ is connected.) a)...
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 168
Section 7.2.2.1: Dancing Links Exercise 168. ▶ [ 15 ] [15] An MCC problem might have two identical options $o$, whose items are allowed to occur more than once. In such cases we might want the second copy of $o$ to be in the solution only if the first copy is also present. How can that be achieved? Verified: no Solve time: 3m49s Setup We seek all integers $n <...
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 165
Section 7.2.2.1: Dancing Links Exercise 165. [ M30 ] [M30] Consider an MCC problem in which we must choose 2 of 4 options to cover item 1, and 5 of 7 options to cover item 2; the options are all distinct. a) What's the size of the search tree if we branch first on item 1, then on item 2? Would it better to branch first on item 2, then...
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 156
Section 7.2.2.1: Dancing Links Exercise 156. ▶ [ 30 ] [30] Straightforward backtracking will solve the partridge puzzle for $n = 8$, using bitwise techniques to represent a partially filled $36 \times 36$ square in just 36 octabytes, instead of by treating it as the huge MCC problem (61) and applying a highly general solver such as Algorithm M. Compare these two approaches, by implementing them both. How many essentially...
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 154
Section 7.2.2.1: Dancing Links Exercise 154. [ M30 ] (C. R. J. Singleton, 1982.) After twelve days of Christmas, the person who sings a popular carol has received twelve partridges in pear trees, plus eleven pairs of humming birds, . . . , plus one set of twelve drummers drumming, from his or her true love. Therefore an "authentic" partridge puzzle should try to pack $(n+1-k)$ squares of size $k...
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 152
Section 7.2.2.1: Dancing Links Exercise 152. [ 30 ] The complete set of path dominoes includes also twelve more patterns: Arrange all 48 of them in an $8 \times 12$ array, forming a single loop. Verified: no Solve time: 4m15s 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.1 Exercise 151
Section 7.2.2.1: Dancing Links Exercise 151. ▶ [ 30 ] (Path dominoes.) A domino has six natural attachment points on its boundary, where we could draw part of a path that connects to neighboring dominoes. Thus $\binom{6}{2} = 15$ different partial paths could potentially be drawn on it. However, only 9 distinct domino patterns with one subpath actually arise, because the other profiles are related under ISO(2); there are six...
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 149
Section 7.2.2.1: Dancing Links Exercise 149. [ M22 ] (Vertex-colored tetrahedra.) The graph $\text{simplex}(3,3,3,3,3,0)$ is a tetrahedron of side 3 with 20 vertices. It has 60 edges, which come from 10 unit tetrahedra. There are ten ways to color the vertices of a unit tetrahedron with four of the five colors ${\mathbf{a}, \mathbf{b}, \mathbf{c}, \mathbf{d}, \mathbf{e}}$, because mirror reflections are distinct. Can those ten colored tetrahedra be packed into $\text{simplex}(3,3,3,3,3,0)$,...
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 147
Section 7.2.2.1: Dancing Links Exercise 147. [ 30 ] The 30 cubes of exercise 146 can be used to make "bricks" of various sizes $l \times m \times n$, by assembling $l \cdot m \cdot n$ of them into a cuboid that has solid colors on each exterior face, as well as matching colors on each interior face. For example, each cube naturally joins with its mirror image to form...
TAOCP 7.2.2.1 Exercise 146
Section 7.2.2.1: Dancing Links Exercise 146. ▶ [ M30 ] $[M30]$ There are 30 ways to paint the colors ${\mathbf{a}, \mathbf{b}, \mathbf{c}, \mathbf{d}, \mathbf{e}, \mathbf{f}}$ on the faces of a cube: (If $\mathbf{a}$ is on top, there are five choices for the bottom color, then six cyclic permutations of the remaining four.) Here's one way to arrange six differently painted cubes in a row, with distinct colors on top, bottom,...
TAOCP 7.2.2.1 Exercise 145
Section 7.2.2.1: Dancing Links Exercise 145. ▶ [ M28 ] $[M28]$ Many problems that involve an $l \times m \times n$ cuboid require a good internal representation of its $(l+1)(m+1)(n+1)$ vertices, its $l(m+1)(n+1) + (l+1)m(n+1) + (l+1)(m+1)n$ edges, and its $lm(n+1)+l(m+1)n+(l+1)mn$ faces, in addition to its $lmn$ individual cells. Show that there's a convenient way to do this with integer coordinates $(x, y, z)$ whose ranges are $0 \le x...
TAOCP 7.2.2.1 Exercise 144
Section 7.2.2.1: Dancing Links Exercise 144. [**] $[2\frac{1}{2}]$ The idea of exercise 142 applies also to triangles and hexagons, allowing us to do both vertex and edge matching with yet another set of 24 tiles: Here there's a vertex matching in the bottom five tiles of (i), and in the upper left five and bottom five in (ii), with edge matching elsewhere. In how many ways can the big hexagon...
TAOCP 7.2.2.1 Exercise 143
Section 7.2.2.1: Dancing Links Exercise 143. ▶ [ M25 ] $[M25]$ The graph $simplex(n, a, b, c, 0, 0, 0)$ in the Stanford GraphBase is the truncated triangular grid consisting of all vertices $xyz$ such that $$x + y + z = n,\quad 0 \le x \le a,\quad 0 \le y \le b,\quad 0 \le z \le c.$$ Two vertices are adjacent if their coordinates all differ by at most...
TAOCP 7.2.2.1 Exercise 142
Section 7.2.2.1: Dancing Links Exercise 142. ▶ [**] $[\tfrac{21}{2}]$ (Zdravko Zivković, 2008.) Edge and vertex matching can be combined into a single design if we replace MacMahon's 24 squares by 24 octagons. For example, illustrate $4 \times 6$ arrangements in which there's vertex matching in the (i) left half, (ii) bottom half, or (iii) northwest and southeast quadrants, while edge matching occurs elsewhere. (We get vertex matching when an octagon's...
TAOCP 7.2.2.1 Exercise 141
Section 7.2.2.1: Dancing Links Exercise 141. [**] $[\tfrac{21}{2}]$ Combining exercises 133 and 140, we can also adapt MacMahon's 24 tri-colored squares to vertex matching instead of edge matching. Noteworthy solutions are a) In how many essentially different ways can those 24 tiles be properly packed into rectangles of these sizes, leaving a hole in the middle of the $5 \times 5$? b) Discuss tiling the plane with such solutions. Verified:...
TAOCP 7.2.2.1 Exercise 140
Section 7.2.2.1: Dancing Links Exercise 140. [ 29 ] [29] (C. D. Langford, 1959.) MacMahon colored the edges of his tiles, but we can color the vertices instead. For example, we can make two parallelograms, or a truncated triangle, by assembling the 24 vertex-colored analogs of (58): Such arrangements are much rarer than those based on edge matching, because edges are common to only two tiles but vertices might involve...
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 139
Section 7.2.2.1: Dancing Links Exercise 139. [**] [ M 25] Excellent human-scale puzzles have been made by choosing nine of the 24 tiles in exercise 138, redrawing them with whimsical illustrations in place of the triangles, and asking for a $3 \times 3$ arrangement in which heads properly match tails. a) How many of the $\binom{24}{9}$ choices of 9 tiles lead to essentially different puzzles? b) How many of those...
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 136
Section 7.2.2.1: Dancing Links Exercise 136. ▶ [ HM48 ] (J. H. Conway, 1958.) There are twelve ways to label the edges of a pentagon with ${0, 1, 2, 3, 4}$, if we don't consider rotations and reflections to be different: Cover a dodecahedron with these tiles, matching edge numbers. (Reflections are OK.) Verified: no Solve time: 4m56s Setup We seek all integers $n < 10^9$ such that the equation...
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 132
Section 7.2.2.1: Dancing Links Exercise 132. [ 40 ] (W. E. Philpott, 1971.) There are $4624 = 68^2$ tiles in a set like (§8), but it uses 24 different colors instead of 4. Can they be assembled into an equilateral triangle of size 68, with constant color on the boundary and with matching edges inside? Verified: no Solve time: 5m09s Setup We seek all integers $n < 10^9$ such that...
TAOCP 7.2.2.1 Exercise 131
Section 7.2.2.1: Dancing Links Exercise 131. [ 28 ] (P. A. MacMahon, 1921.) Instead of using the colored tiles of (§8), which yield (59), we can form hexagons from 24 different triangles in two other ways: The left diagram shows a "jigsaw puzzle" whose pieces have four kinds of edges. The right diagram shows "triple three triominoes," which have zero, one, two, or three spots at each edge; adjacent triominoes...
TAOCP 7.2.2.1 Exercise 130
Section 7.2.2.1: Dancing Links Exercise 130. [**] [2↑] Partition MacMahon's triangles (§8) into three sets of eight, each of which can be placed on the faces of an octahedron, with matching edge colors. Verified: no Solve time: 2m25s 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 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 129
Section 7.2.2.1: Dancing Links Exercise 129. ▶ [ M14 ] The most beautiful patterns that can be made with MacMahon's triangles are those with attractive symmetries, which can be of two kinds: strong symmetry (a rotation or reflection that doesn't change the pattern, except for permutation of colors) or weak symmetry (a rotation or reflection that preserves the "color patches," the set of boundaries between different colors). Exactly how many...
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 127
Section 7.2.2.1: Dancing Links Exercise 127. [ M8 ] There are $4^{12}$ ways to prescribe the border colors of a hexagon like those in (59). Which of them can be completed to a color-matched placement of all 24 triangles? Verified: no Solve time: 4m16s 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 126
Section 7.2.2.1: Dancing Links Exercise 126. [ 29 ] [29] Find all solutions of MacMahon's problem (59), by applying Algorithm C to a suitable set of items and options based on the coordinate system in exercise 124. How much time is saved by using the improved algorithm of exercise 122? Verified: no Solve time: 5m15s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2...
TAOCP 7.2.2.1 Exercise 125
Section 7.2.2.1: Dancing Links Exercise 125. [ M20 ] When a set of $s$ triangles is magnified by an integer $k$, we obtain $sk^2$ triangles. Describe the coordinates of those triangles, in terms of the coordinates of the originals, using the system of exercise 124. Verified: no Solve time: 5m15s 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 124
Section 7.2.2.1: Dancing Links Exercise 124. [ M22 ] Devise a system of coordinates for representing the positions of equilateral triangles in patterns such as (59). Represent also the edges between them. Verified: no Solve time: 5m28s 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.1 Exercise 123
Section 7.2.2.1: Dancing Links Exercise 123. [ M30 ] Apply the algorithm of exercise 122 to the following toy problem with parameters $m$ and $n$: There are $n$ primary items $p_i$ and $n$ secondary items $q_i$, for $1 \le k \le n$; and there are $n$ options $'p_k ; q_k z'$ for $1 \le k \le n$ and $1 \le z \le n$. (The solutions to this problem are the...
TAOCP 7.2.2.1 Exercise 122
Section 7.2.2.1: Dancing Links Exercise 122. ▶ [ 28 ] [28] Extend Algorithm C so that it finds only $1/d!$ of the solutions, in cases where the input options are totally symmetric with respect to $d$ of the color values, and where every solution contains each of those color values at least once. Assume that those values are ${v, v+1, \ldots, v+d-1}$, and that all other colors have values $<...
TAOCP 7.2.2.1 Exercise 121
Section 7.2.2.1: Dancing Links Exercise 121. [ M29 ] Exercise 2.3.4.3–5 discusses 92 types of tetrads that are able to tile the plane, and proves that no such tiling is toroidal (periodic). a) Show that the tile called $\delta U S$ in that exercise can't be part of any infinite tiling. In fact, it can appear in only $n+1$ cells of an $m \times n$ array, when $m, n \ge...
TAOCP 7.2.2.1 Exercise 120
Section 7.2.2.1: Dancing Links Exercise 120. [ M29 ] Section 2.3.4.3 discussed Hao Wang's "tetrad tiles," which are squares that have specified colors on each side. Find all ways in which the entire plane can be filled with tiles from the following families of tetrad types, always matching colors at the edges where adjacent tiles meet [see Scientific American 231 , 5 (Nov. 1965), 103, 106]: a) b) (The tetrad...
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 119
Section 7.2.2.1: Dancing Links Exercise 119. [ 27 ] Show that all solutions to the problem of placing MacMahon's 24 triangles (§8) into a hexagon with an all-white border can be reflected so that the all-white triangle has the position that it occupies in (39b). Hint: Factorize. Verified: no Solve time: 5m02s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots +...
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 116
Section 7.2.2.1: Dancing Links Exercise 116. ▶ [ M25 ] Given a graph $G$ on vertices $V$, let $\mu(G)$ be obtained by (i) adding new vertices $V' = {v' \mid v \in V}$, with $u' \mathbin{-!!-} v$ when $u \mathbin{-!!-} v$; and also (ii) adding another vertex $w$, with $w \mathbin{-!!-} v'$ for all $v' \in V'$. If $G$ has $m$ edges and $n$ vertices, $\mu(G)$ has $3m+n$ edges and...
TAOCP 7.2.2.1 Exercise 115
Section 7.2.2.1: Dancing Links Exercise 115. [ M25 ] Continuing exercise 114, how many hypersudoku solutions have automorphisms of the following types? (a) transposition; (b) the transformation of exercise 67(d); (c) 90° rotation; (d) both (b) and (c). 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...
TAOCP 7.2.2.1 Exercise 114
Section 7.2.2.1: Dancing Links Exercise 114. [ M25 ] Let $\alpha$ be a permutation of the cells of a $9 \times 9$ array that takes any sudoku solution into another sudoku solution. We say that $\alpha$ is an automorphism of the sudoku solution $S = (s_{ij})$ if there's a permutation $\pi$ of ${1, 2, \ldots, 9}$ such that $s_{ij\alpha} = \pi s_{ij}$ for $0 \le i, j \le 9$. For...
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 112
Section 7.2.2.1: Dancing Links Exercise 112. ▶ [ 28 ] [28] A popular word puzzle in Brazil, called 'Torto' ('bent'), asks solvers to find as many words as possible that can be traced by a noncrossing king path in a given $6 \times 3$ array of letters. For example, each of the words THE, NATURE, ART, OF, COMPUTER, and PROGRAMMING can be found in the array shown here. a) Does...
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 110
Section 7.2.2.1: Dancing Links Exercise 110. [ 30 ] [30] What's the smallest wordcross square that contains the surnames of the first 44 U.S. presidents? (Use the names in exercise 108, but change VANBUREN to VAN BUREN.) Verified: no Solve time: 5m17s 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 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 109
Section 7.2.2.1: Dancing Links Exercise 109. [ 28 ] [28] A "wordcross puzzle" is the challenge of packing a given set of words into a rectangle under the following conditions: (i) All words must read either across or down, as in a crossword puzzle. (ii) No letters are adjacent unless they belong to one of the given words. (iii) The words are rowwise connected. (iv) Words overlap only when one...
TAOCP 7.2.2.1 Exercise 108
Section 7.2.2.1: Dancing Links Exercise 108. ▶ [ 32 ] [32] The first 44 presidents of the U.S.A. had 38 distinct surnames: ADAMS, ARTHUR, BUCHANAN, BUSH, CARTER, CLEVELAND, CLINTON, COOLIDGE, EISENHOWER, FILL-MORE, FORD, GARFIELD, GRANT, HARDING, HARRISON, HAYES, HOOVER, JACKSON, JEFFERSON, JOHNSON, KENNEDY, LINCOLN, MADISON, MCKINLEY, MONROE, NIXON, OBAMA, PIERCE, POLK, REAGAN, ROOSEVELT, TAFT, TAYLOR, TRUMAN, TYLER, VANBUREN, WASHINGTON, WILSON. a) What's the smallest square into which all of these...
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 104
Section 7.2.2.1: Dancing Links Exercise 104. [ M28 ] Assume that $n + 1 = p$ is prime. Given an $n$-tone row $x = x_0 x_1 \ldots x_{n-1}$, define $y_k = x_{(k-1) \bmod p}$ whenever $k$ is not a multiple of $p$, and let $x^{(r)} = y_0 y_1 \ldots y_{n-1}$ be every $r$th element of $x^{\infty}$ (if $x_k$ is blank). For example, when $n = 12$, every 5th element of...
TAOCP 7.2.2.1 Exercise 103
Section 7.2.2.1: Dancing Links Exercise 103. [ M28 ] Musical pitches in the Western system of "equal temperament" are the notes whose frequency is $440 \cdot 2^{n/12}$ cycles per second, for some integer $n$. The pitch class of such a note is $n \bmod 12$, and seven of the twelve possible pitch classes are conventionally designated by letters: $$0 = \mathrm{A}, \quad 2 = \mathrm{B}, \quad 3 = \mathrm{C}, \quad...
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 100
Section 7.2.2.1: Dancing Links Exercise 100. ▶ [ 30 ] [30] The general constraint satisfaction problem (CSP) is the task of finding all $n$-tuples $x_1 \ldots x_n$ that satisfy a given system of constraints $C_1, \ldots, C_m$, where each constraint is defined by a relation on a nonempty subset of the variables ${x_1, \ldots, x_n}$. For example, a unary constraint is a relation of the form $x_k \in D_{jk}$; a...
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.2 Exercise 186
Section 7.2.2.2: Satisfiability Exercise 186. [ M21 ] [M21] Use (77) to reduce $\sum_m \hat{q}_m$ and $\sum_m (2m+1)\hat{q}_m$ to (78) and (79). Verified: no Solve time: 3m05s The previous attempt failed because it never actually uses the explicit structure of equation (77). In TAOCP style, the reduction must come from direct substitution of (77) followed by honest rearrangement of finite sums. Let (77) be used in its actual form from...
TAOCP 7.2.2.2 Exercise 185
Section 7.2.2.2: Satisfiability Exercise 185. [ M20 ] [M20] Use (77) to prove the intuitively obvious fact that $\hat{q}_m \ge q_m$. Verified: no Solve time: 1m37s Solution Equation (77) expresses $\hat{q}_m$ in terms of a decomposition of the same underlying combinatorial objects that define $q_m$, but without the restriction that enforces the stricter admissibility condition used in $q_m$. In that formulation, every configuration counted by $q_m$ appears among the configurations...
TAOCP 7.2.2.2 Exercise 184
Section 7.2.2.2: Satisfiability Exercise 184. [ M20 ] [M20] Why does (77) characterize the relation between $\hat{q}_m$ and $q_m$? Verified: yes Solve time: 4m03s The flaw in the previous solution is that it treats the relation as a generic probabilistic decomposition without aligning precisely with Knuth’s definitions of $q_m$ and $\hat{q}_m$, and it does not justify why the identity is an exact characterization rather than a one-directional consequence. We restart...
TAOCP 7.2.2.2 Exercise 183
Section 7.2.2.2: Satisfiability Exercise 183. [ M30 ] [M30] Discuss the relation between Figs. 42 and 43. Verified: yes Solve time: 2m51s The key mistake in the proposed solution is treating Fig. 43 as a global compression of the entire DPLL search tree in Fig. 42. In Knuth’s presentation, that is not what is happening. The two figures describe different levels of the same computation, but they are not related...
TAOCP 7.2.2.2 Exercise 182
Section 7.2.2.2: Satisfiability Exercise 182. [ M16 ] [M16] For which values of $m$ in Fig. 41 does $T_m$ have a constant value? Verified: no Solve time: 5m27s 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 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 18
Section 7.2.2.2: Satisfiability Exercise 18. ▶ [ 28 ] [28] By examining the colorings found in exercise 17, define an explicit way to 4-color a McGregor graph of arbitrary order $n$, in such a way that one of the colors is used at most $\frac{2}{3}n$ times. Hint: The construction depends on the value of $n \bmod 6$. Verified: no Solve time: 5m49s Setup We seek all integers $n < 10^9$...
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 45
Section 7.2.2.1: Dancing Links Exercise 45. [ 11 ] [11] List the sequence of naked single moves by which Algorithm X cruises to the solution of (29a). (If several such $p_{ij}$ are possible, choose the smallest $i$ at each step.) Verified: no Solve time: 2m47s The exercise, as presented in the prompt, cannot be solved because the essential input data are missing. Exercise $7.2.2.1.45$ asks for the explicit sequence of...
TAOCP 7.2.2.1 Exercise 391
Section 7.2.2.1: Dancing Links Exercise 391. [ 29 ] A futoshiki puzzle is said to be valid if it has exactly one solution. Use Algorithm X to generate all possible $5 \times 5$ latin squares. Explain why many of them can't be the solution to a valid futoshiki puzzle unless it has at least one strong clue. Verified: no Solve time: 5m18s Corrected Solution We restate the problem in the...
TAOCP 7.2.2.1 Exercise 354
Section 7.2.2.1: Dancing Links Exercise 354. ▶ [ M30 ] [M30] A polysphere is a connected set of spherical cells that belong to the "face-centered cubic lattice," which is one of the two principal ways to pack cannonballs (or oranges) with maximum efficiency. That lattice is conveniently regarded as the set $S$ of all quadruples $(w, x, y, z)$ of integers for which $w + x + y + z...
TAOCP 7.2.2.1 Exercise 6
Section 7.2.2.1: Dancing Links Exercise 6. [ 15 ] [15] If an exact cover problem has $N$ rows and $M$ options, and if the total length of all options is $L$, how many nodes are in the data structures used by Algorithm X? Verified: no Solve time: 3m57s 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 348
Section 7.2.2.1: Dancing Links Exercise 348. [ M41 ] [M41] Find the maximum number of "canonical bricks" ($1 \times 2 \times 4$) that can be packed into an $l \times m \times n$ box, leaving as few empty cells as possible. Verified: no Solve time: 5m56s 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.2.1 Exercise 34
Section 7.2.2.1: Dancing Links Exercise 34. [ M25 ] $[M25]$ Given an exact cover problem $A$ as in exercise 33, construct an exact cover problem $A'$ such that (i) $A'$ has at most three 1s in every column; (ii) $A'$ and $A$ have exactly the same number of solutions. Verified: no Solve time: 5m40s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_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.2.1 Exercise 4
Section 7.2.2.1: Dancing Links Exercise 4. [ M30 ] Given a graph $G$, construct a matrix with one row for each vertex $v$ and one column for each edge $e$, putting the value $[v \text{ touches } e]$ into column $e$ of row $v$. What do the exact covers of this "incidence matrix" represent? Verified: no Solve time: 2m22s Setup Let $G = (V, E)$ be a (simple, undirected) graph....
TAOCP 7.2.1.6 Exercise 99
Section 7.2.1.6: Generating All Trees Exercise 99. [ 30 ] Consider a series-parallel graph represented by a tree as in (53), together with node values that satisfy (55). These values define a spanning tree or a near tree, according as $v_p$ is 1 or 0 at the root $p$. Show that the following method will generate all of the other configs of the root: i) Begin with all memory nodes...
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.2.1 Exercise 2
Section 7.2.2.1: Dancing Links Exercise 2. [ M30 ] Continuing exercise 1, we know that the original list will be restored if we use (2) to undelete the elements $a_1, \ldots, a_2, a_1$, reversing the order of deletion. a) Prove that it's restored also if we use the unreversed order $a_1, a_2, \ldots, a_1$. b) Is the original list restored if we undelete the elements in any order whatsoever? c)...
TAOCP 7.2.1.6 Exercise 97
Section 7.2.1.6: Generating All Trees Exercise 97. [ 15 ] Is (48) a series-parallel graph? 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 number of terms among $x_1,\dots,x_n$ that are...
TAOCP 7.2.2.1 Exercise 1
Section 7.2.2.1: Dancing Links Exercise 1. ▶ [ M25 ] A doubly linked list of $n$ elements, with a list head at 0, begins with $\text{LLINK}(k) = k - 1$ and $\text{RLINK}(k - 1) = k$ for $1 \le k \le n$; furthermore $\text{LLINK}(0) = n$ and $\text{RLINK}(n) = 0$, as in (3). But after we use operation (1) to delete elements $a_1, a_2, \ldots, a_n$, where $a_1 a_2 \ldots...
TAOCP 7.2.1.6 Exercise 96
Section 7.2.1.6: Generating All Trees Exercise 96. ▶ [ 28 ] Analyze the approximate running time of Algorithm S when the given graph is simply (a) a path $P_n$ of length $n-1$; (b) a cycle $C_n$ of length $n$. Verified: no Solve time: 4m32s We restart from the actual structure of Algorithm S in TAOCP §7.2.1.6 and avoid collapsing any of its internal mechanisms into unjustified constant-time or one-pass assumptions....
TAOCP 7.2.1.6 Exercise 95
Section 7.2.1.6: Generating All Trees Exercise 95. [ 26 ] Complete Algorithm S by implementing the bridge test in step S8. Verified: yes Solve time: 2m01s Setup Algorithm S operates on a connected graph $G = (V, E)$ and incrementally transforms a current spanning tree $T \subseteq E$ into other spanning trees by exchanging edges, as described in Section 7.2.1.6. In step S8, the algorithm requires a bridge test :...
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.1.6 Exercise 92
Section 7.2.1.6: Generating All Trees Exercise 92. [ 15 ] Are the first and last spanning trees visited by Algorithm S adjacent, in the sense that they have $n-2$ edges in common? Verified: yes Solve time: 1m41s Solution Algorithm S enumerates all spanning trees of the complete graph $K_n$ via Prüfer sequences of length $n-2$ over the alphabet ${1,2,\ldots,n}$ in lexicographic order, as established in Section 7.2.1.6. The correspondence between...
TAOCP 7.2.1.6 Exercise 91
Section 7.2.1.6: Generating All Trees Exercise 91. [ M37 ] (Nijenhuis and Wilf.) Show that a random oriented tree can be generated with a procedure analogous to the random partition algorithm of exercise 7.2.1.4–47. Verified: yes Solve time: 1m56s Setup Let $T_n$ denote the set of rooted ordered trees with $n$ internal nodes in the sense of Algorithm B of Section 7.2.1.6, represented by left and right links $(l_1,\dots,l_n)$ and...
TAOCP 7.2.2 Exercise 79
Section 7.2.2: Backtracking Exercise 79. ▶ [ M30 ] The author and his wife own a pipe organ that contains 812 pipes, each of which is either playing or silent. Therefore $2^{812}$ different sounds (including silence) can potentially be created. However, the pipes are controlled by a conventional organ console, which has only $56+56+32=144$ keys and pedals that can be played by hands and feet, together with 20 on-off switches...
TAOCP 7.2.1.6 Exercise 90
Section 7.2.1.6: Generating All Trees Exercise 90. ▶ [ M37 ] If $p_1 \ldots p_n$ is a canonical sequence of parent pointers for an oriented forest, with the graph with vertices ${0, 1, \ldots, n}$ and edges ${k - p_k \mid 1 \le k \le n}$ is a free tree , namely a connected graph with no cycles. (See Theorem 2.3.4.1A.) Conversely, every free tree corresponds to at least one...
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.1.6 Exercise 89
Section 7.2.1.6: Generating All Trees Exercise 89. [ M46 ] How often does step O5 set $p_k \leftarrow p_j$? Verified: no Solve time: 5m41s 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 77
Section 7.2.2: Backtracking Exercise 77. [ M22 ] A $v$-reachable subset of a directed graph $G$ is a nonempty set of vertices $H$ with the property that every $u \in H$ can be reached from $v$ by at least one oriented path in $G\langle H\rangle$. (In particular, $v$ itself must be in $H$.) a) The digraph $P_3' \mathbin{\square} P_3'$ is like $P_3 \mathbin{\square} P_3$ except that all arcs between vertices...
TAOCP 7.2.1.6 Exercise 88
Section 7.2.1.6: Generating All Trees Exercise 88. [ M20 ] Analyze Algorithm O: How often is step O4 performed? What is the total number of times $p_k$ is changed in step O5? Verified: no Solve time: 3m53s The previous solution failed by tying the execution of step O4 to a “parent-to-child transition” interpretation rather than to the actual control structure of Algorithm O. The correct way to reason is to...
TAOCP 7.2.1.6 Exercise 87
Section 7.2.1.6: Generating All Trees Exercise 87. [ M30 ] Let $F$ be an ordered forest in which the $k$th node in preorder appears on level $c_k$ and has parent $p_k$, where $p_k = 0$ if that node is a root. a) How many forests satisfy the condition $c_k = p_k$ for $1 \le k \le n$? b) Suppose $F$ and $F'$ have level codes $c_1 \ldots c_n$ and $c'_1...
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.1.6 Exercise 86
Section 7.2.1.6: Generating All Trees Exercise 86. [ 15 ] If (a) is regarded as an oriented forest instead of an ordered forest, what monomial forests are possible? Specify that forest both by its level codes $c_1 \ldots c_{15}$ and its parent pointers $p_1 \ldots p_{15}$. Verified: no Solve time: 5m58s 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 85
Section 7.2.1.6: Generating All Trees Exercise 85. [ HM35 ] (Philippe Golle.) Let $V$ be any vector space contained in the set of all real $n$-dimensional vectors, but containing none of the unit vectors $(1, 0, \ldots, 0)$, $(0, 1, 0, \ldots, 0)$, …, $(0, \ldots, 0, 1)$. Prove that $V$ contains at most $M_n$ vectors whose components are all 0 or 1; furthermore the upper bound $M_n$ is achievable....
TAOCP 7.2.2 Exercise 75
Section 7.2.2: Backtracking Exercise 75. ▶ [ 30 ] (Connected subsets.) Let $v$ be a vertex of some graph $G$, and let $H$ be a connected subset of $G$ that contains $v$. The vertices of $H$ can be listed in a canonical way by starting with $v_0 = v$ and then letting $v_1, v_2, \ldots$ be the neighbors of $v_0$ that lie in $H$, followed by the neighbors of $v_1$...
TAOCP 7.2.1.6 Exercise 84
Section 7.2.1.6: Generating All Trees Exercise 84. ▶ [ HM27 ] (D. Kleitman.) Let $A$ be an $m \times n$ matrix of real numbers in which every column $v$ has length $|v| \ge 1$, and let $b$ be an $m$-dimensional column vector. Prove that at most $M_n$ column vectors $x = (a_1, \ldots, a_n)^T$, with components $a_j = 0$ or $1$, satisfy $|Ax - b| < \frac{1}{2}$. Hint: Use a...
TAOCP 7.2.1.6 Exercise 83
Section 7.2.1.6: Generating All Trees Exercise 83. [ M20 ] (G. Hansel.) Show that there are at most $3^{M_n}$ monotone Boolean functions $f(x_1, \ldots, x_n)$ of $n$ Boolean variables. 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 in positive integers satisfying $x_1 \ge x_2 \ge...
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 82
Section 7.2.1.6: Generating All Trees Exercise 82. ▶ [ M26 ] Let $E(f)$ be the number of times Algorithm II evaluates the function $f$. a) Show that $M_n \le E(f) \le M_{n+1}$, with equality when $f$ is constant. b) Among all $f$ such that $E(f) = M_n$, which one minimizes $\sum_{\sigma} f(\sigma)$? c) Among all $f$ such that $E(f) = M_n$, which one maximizes $\sum_{\sigma} f(\sigma)$? Verified: no Solve time:...
TAOCP 7.2.2 Exercise 73
Section 7.2.2: Backtracking Exercise 73. ▶ [ 30 ] (A clueless anacrostic.) The letters of 29 five-letter words $$\def\arraystretch{1.0} \begin{array}{ccccccccccc} \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \bar{\text{T}} & \cdots \end{array}$$ all belonging to WORDS(1000) , have been shuffled to form the following mystery text: $$\small \begin{array}{rrrrrrrrrrrrrrrrrr} 50 & 27 & 9 & 1 & 2 & 3 &...
TAOCP 7.2.1.6 Exercise 81
Section 7.2.1.6: Generating All Trees Exercise 81. [ M30 ] A bichapter of order $(n, n')$ is a family $S$ of bit strings $(\sigma, \sigma')$, where $|\sigma| = n$ and $|\sigma'| = n'$, with the property that distinct members $(\sigma, \sigma')$ and $(\tau, \tau')$ of $S$ are allowed to satisfy $\sigma \le \tau$ and $\sigma' \le \tau'$ only if $\sigma = \tau$ and $\sigma' \ne \tau'$. Use Christmas tree patterns...
TAOCP 7.2.1.6 Exercise 80
Section 7.2.1.6: Generating All Trees Exercise 80. [ 30 ] [30] Say that two bit strings are concordant if we can obtain one from the other via the transformations $010 \leftrightarrow 100$ or $101 \leftrightarrow 110$ on substrings. For example, the strings $$011100 \leftrightarrow 011010 \leftrightarrow 010110 \leftrightarrow 010101 \leftrightarrow 011001$$ $$\updownarrow$$ $$100110 \leftrightarrow 100101 \leftrightarrow 101001 \leftrightarrow 110001$$ are mutually concordant, but no other string is concordant with any...
TAOCP 7.2.1.6 Exercise 79
Section 7.2.1.6: Generating All Trees Exercise 79. [ M26 ] [M26] The number of permutations $p_1 \ldots p_n$ that have exactly one "descent" where $p_k > p_{k+1}$, is the Eulerian number $\langle {n \atop 1} \rangle = 2^n - n - 1$, according to Eq. 5.1.3–(12). The number of entries in the Christmas tree pattern, above the bottom row, is the same. a) Find a combinatorial explanation of this coincidence,...
TAOCP 7.2.2 Exercise 72
Section 7.2.2: Backtracking Exercise 72. [ HM28 ] Show that exercise 71 has a surprising, somewhat paradoxical answer if two changes are made to Table 666: 9(E) becomes '$c \in [39,..,43]$'; 15(C) becomes '${11}$'. Verified: no Solve time: 1m59s 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 71
Section 7.2.2: Backtracking Exercise 71. ▶ [ M29 ] (Donald R. Woods, 2000.) Find all ways to maximize the number of correct answers to the questionnaire in Table 666. Each question must be answered with a letter from A to E. Hint: Begin by clarifying the exact meaning of this exercise. What answers are best for the following two-question, two-letter "warmup problem"? Verified: no Solve time: 5m10s Setup We seek...
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.2 Exercise 70
Section 7.2.2: Backtracking Exercise 70. [ HM40 ] (M. Bousquet-Mélou.) Consider self-avoiding paths from the upper left corner of an $m \times n$ grid to the lower right, where each step is either up, down, or to the right. If we generate such paths at random, making either 1 or 2 or 3 choices at each step as in Algorithm E, the expected value $\text{E},D_{mn}$ is the total number of...
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 69
Section 7.2.2: Backtracking Exercise 69. [ 41 ] Is there a puzzle like exercise 68 whose clues contain more than 32 digits of $\pi$? 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 \ge \cdots \ge x_n$. Let $k$...
TAOCP 7.2.1.6 Exercise 76
Section 7.2.1.6: Generating All Trees Exercise 76. [ HM46 ] [HM46] Study the limiting shape of the Christmas tree patterns as $n \to \infty$. Does it, for example, have a fractal dimension under some appropriate scaling? Verified: no Solve time: 5m34s 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.6 Exercise 75
Section 7.2.1.6: Generating All Trees Exercise 75. ▶ [ HM29 ] [HM29] Let $(r_1^{(n)}, r_2^{(n)}, \ldots, r_{c_n}^{(n)})$ be the row numbers in which the Christmas tree pattern of order $n$ has $n-1$ entries; for example, Table 4 tells us that $(r_1^{(3)}, r_2^{(3)}, r_3^{(3)}) = (20, 40, 54, 62, 66, 68, 69)$. Find formulas for $r_1^{(n)} - r_1^{(n)}$ and for $\lim_{n \to \infty} r_j^{(n)}/M_n$. Verified: no Solve time: 4m20s The solution...
TAOCP 7.2.1.6 Exercise 74
Section 7.2.1.6: Generating All Trees Exercise 74. [ M26 ] [M26] Continuing the previous exercise, how many rows precede that row? Verified: yes Solve time: 4m30s The reviewer correctly identifies that the previous solution made an incorrect leap : it treated a detected issue as a reason to terminate the ranking problem, and it also incorrectly output a numeric value. The correct resolution is to separate two logically distinct questions...
TAOCP 7.2.2 Exercise 68
Section 7.2.2: Backtracking Exercise 68. ▶ [ 28 ] (Johan de Ruiter, 14 March 2018.) Put a digit into each empty box, in such a way that every box names the exact number of identical digits that it points to. Verified: no Solve time: 2m03s The previous solution fails because it replaces the actual content of the diagram with assumptions. A corrected solution must treat the directed graph defined by...
TAOCP 7.2.2 Exercise 67
Section 7.2.2: Backtracking Exercise 67. ▶ [ 26 ] (The Royal Aquarium Thirteen Puzzle.) "Rearrange the nine cards of the right-hand illustration above, optionally rotating of them by $180°$, so that the six horizontal sums of gray letters and the six vertical sums of black letters all equal 13." (The current sums are $1+5+4 = 10$, $\ldots$, $7+5+7 = 19$.) The author of Hoffmann's Puzzles Old and New (1893) stated...
TAOCP 7.2.1.6 Exercise 73
Section 7.2.1.6: Generating All Trees Exercise 73. [ 15 ] [15] In the Christmas tree pattern of order 30, what are the first and last elements of the row that contains the bit string 011001001000011111011010111001? Verified: no Solve time: 4m22s The previous solution fails because it replaces Knuth’s recursive “Christmas tree” construction with an unrelated partition by Hamming weight. In §7.2.1.6, rows are not defined by global invariants of bitstrings....
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.1.6 Exercise 72
Section 7.2.1.6: Generating All Trees Exercise 72. [ M38 ] [M38] If you start with a single row $\sigma_1, \sigma_2, \ldots, \sigma_r$ of length $s$ and apply the growth rule (36) repeatedly $n$ times, how many rows do you obtain? Verified: no Solve time: 4m33s Setup Let a row be a string $\sigma_1 \sigma_2 \ldots \sigma_s$ of fixed length $s$. The growth rule (36) acts on a row to produce...
TAOCP 7.2.2 Exercise 64
Section 7.2.2: Backtracking Exercise 64. [**] [ 2↑ ] Assemble five cubes with uppercase letters on each face, using the patterns $$\begin{array}{ccccc} \text{P O E} & \text{S G S} & \text{A R T} & \text{D T E} & \text{U Y L} \ \text{Z}\overline{\text{O}}\text{H} & \text{N}\overline{\text{A}}\text{Z} & \text{H}\overline{\text{E}}\text{Z} & \text{U}\overline{\text{S}}\text{C} & \text{Z}\overline{\text{O}}\text{H} \end{array}$$ By extending the principles of "Instant Insanity," show that these cubes can be placed in a row so...
TAOCP 7.2.2 Exercise 63
Section 7.2.2: Backtracking Exercise 63. [ 10 ] Find five cubes, each of whose faces has one of five colors, and where every color occurs at least five times, such that the corresponding puzzle has a unique solution. Verified: no Solve time: 3m13s Let the colors be ${0,1,2,3,4}$ with arithmetic modulo $5$. Construct five cubes $C_0,\ldots,C_4$, each having six faces, by assigning colors as follows. For cube $C_i$, the faces...
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.2 Exercise 61
Section 7.2.2: Backtracking Exercise 61. [ HM26 ] Let $P_n$ be the number of integer sequences $x_1 \ldots x_n$ such that $x_1 = 1$ and $1 \le x_{k+1} \le 2x_k$ for $1 \le k < n$. (The first few values are 1, 2, 6, 20, 76, 2280, ...; this sequence was introduced by A. Cayley in Philosophical Magazine (4) 13 (1857), 245–248, who showed that $P_n$ enumerates the partitions of...
TAOCP 7.2.1.6 Exercise 71
Section 7.2.1.6: Generating All Trees Exercise 71. [ M21 ] [M21] Let $M_{\sigma s}$ be the size of the largest possible set $S$ of $n$-bit strings with the property that, if $\sigma$ and $\tau$ are members of $S$ with $\sigma \le \tau$, then $\nu(\sigma) < \nu(\tau) + t$. (Thus, for example, $M_{n0} = M_n$ by Sperner's theorem.) Find a formula for $M_{ns}$. Verified: no Solve time: 2m14s Solution Let $B_n...
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 68
Section 7.2.1.6: Generating All Trees Exercise 68. [ 10 ] [10] What is the Christmas tree pattern of order 0? Verified: yes Solve time: 1m36s The Christmas tree pattern of order $n$ corresponds to the planar representation of a full binary tree (or equivalently a properly nested parenthesis structure) with $n$ internal nodes, drawn in its standard triangular “stacked” layout. For order $0$, there are no internal nodes and no...
TAOCP 7.2.2 Exercise 59
Section 7.2.2: Backtracking Exercise 59. [ 26 ] A ZDD with 3,174,197 nodes can be constructed to find (almost) all simple corner-to-corner king paths on a chessboard, using the method of exercise 7.1.4–225. Explain how to use this ZDD to compute (a) the total length of all paths; (b) the number of paths that touch any given subset of the center and/or corner points. Verified: no Solve time: 1m37s Setup...
TAOCP 7.2.1.6 Exercise 67
Section 7.2.1.6: Generating All Trees Exercise 67. [ M22 ] [M22] What is the generating function $S(z) = \sum_n S_n z^n$ for Schröder numbers? Verified: no Solve time: 3m10s 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 Exercise 58
Section 7.2.2: Backtracking Exercise 58. [ 27 ] Consider using this mechanism is a special case of the general problem of counting simple paths from vertex $s$ to vertex $t$ in a given graph. We can generate such paths by random walks from $s$ that don't get stuck, if we maintain a table of values $\text{DIST}(v)$ for all vertices $v$ in the graph (or subgraph), representing the shortest distance from...
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.2 Exercise 57
Section 7.2.2: Backtracking Exercise 57. [ HM21 ] Algorithm E can potentially follow $M$ different paths $X_1 \cdots X_{l-1}$ before it terminates, where $M$ is the number of leaves of the backtrack tree. Suppose the final values of $D$ at those leaves are $D^{(1)}, \ldots, D^{(M)}$. Prove that $(D^{(1)} \cdots D^{(M)})^{1/M} \ge M$. Verified: no Solve time: 5m06s Setup We seek all integers $n < 10^9$ such that the equation...
TAOCP 7.2.1.6 Exercise 65
Section 7.2.1.6: Generating All Trees Exercise 65. [ 38 ] [38] Generalize Rémy's algorithm (Algorithm R) to $t$-ary trees. Verified: no Solve time: 4m55s The earlier solution correctly described the growth process and the history-based bijection, but it made an unjustified leap from labeled histories to uniformity over unlabeled trees. The reviewer’s objection is decisive: the number of valid creation-time labelings depends on the tree shape, so one cannot cancel...
TAOCP 7.2.2 Exercise 56
Section 7.2.2: Backtracking Exercise 56. ▶ [ M25 ] The commafree code search procedure in Algorithm C doesn't actually fit the mold of Algorithm E, because it incorporates lookahead, dynamic ordering, reversible memory, and other enhancements to the basic backtrack paradigms. How could its running time be reliably estimated with Monte Carlo methods? Verified: no Solve time: 5m07s Setup We seek all integers $n < 10^9$ such that the equation...
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.2 Exercise 55
Section 7.2.2: Backtracking Exercise 55. [ M30 ] If all costs $c(x_1, \ldots, x_l)$ are positive, show that the biased probabilities of exercise 54 can be chosen in such a way that the estimate $S$ is always exact. Verified: no Solve time: 5m08s 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 63
Section 7.2.1.6: Generating All Trees Exercise 63. [**] [ If ] If the first two iterations of Rémy's algorithm have produced $\boxed{\square}\text{ }\boxed{\square}$, what decorated binary trees are possible after the next iteration? Verified: yes Solve time: 2m09s After the first iteration of Rémy's algorithm there is a single external node, carrying label $1$. During the second iteration there is only one existing node to choose, namely that external node....
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 54
Section 7.2.2: Backtracking Exercise 54. [ M21 ] Instead of choosing each $y_i$ in step E5 with probability $1/d$, we could use a biased distribution where $\Pr{I = i \mid X_1, \ldots, X_{l-1}} = p_{X_1 \cdots X_{l-1}}(i) > 0$. How should the estimate $S$ be modified so that its expected value in this general scheme is still $C(t)$? Verified: no Solve time: 4m15s Setup We seek all integers $n <...
TAOCP 7.2.2 Exercise 53
Section 7.2.2: Backtracking Exercise 53. ▶ [ M30 ] $[M30]$ Extend Algorithm E so that it also computes the minimum, maximum, mean, and variance of the Monte Carlo estimates $S$ produced by Algorithm E. 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...
TAOCP 7.2.1.6 Exercise 61
Section 7.2.1.6: Generating All Trees Exercise 61. ▶ [ M26 ] ( Raney's Cycle Lemma. ) Let $b_1 b_2 \ldots b_N$ be a string of nonnegative integers such that $f = N - b_1 - b_2 - \cdots - b_N > 0$. a) Prove that exactly $f$ of the cyclic shifts $b_{k+1} \ldots b_N b_1 \ldots b_k$ for $1 \le j \le N$ satisfy the preorder degree sequence property in...
TAOCP 7.2.2 Exercise 52
Section 7.2.2: Backtracking Exercise 52. ▶ [ HM25 ] $[HM25]$ Elmo uses Algorithm E with $D_k = {1, \ldots, n}$, $P_k = {x_1 > \cdots > x_k}$, $c = 1$. a) Alice flips $n$ coins independently, where coin $k$ yields "heads" with probability $1/k$. True or false: She obtains exactly $l$ heads with probability $\binom{n}{l}/n!$. b) Let $Y_1, Y_2, \ldots, Y_l$ be the numbers on the coins that come up...
TAOCP 7.2.1.6 Exercise 60
Section 7.2.1.6: Generating All Trees Exercise 60. ▶ [ M26 ] ( Balanced strings. ) A string $\alpha$ of nested parentheses is atomic if it has the form $(\alpha')$ where $\alpha'$ is nested; every nested string can be represented uniquely as a product of atoms $\alpha_1 \ldots \alpha_s$. A string with equal numbers of left and right parentheses is called balanced ; every balanced string can be represented uniquely as...
TAOCP 7.2.2 Exercise 51
Section 7.2.2: Backtracking Exercise 51. [ M22 ] $[M22]$ What's a simple martingale that corresponds to Theorem E? 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 the number of terms among...
TAOCP 7.2.1.6 Exercise 59
Section 7.2.1.6: Generating All Trees Exercise 59. [ HM29 ] Similarly, let $T_{l_{\min}}$ count all $n$-node binary trees in which internal node $n$ appears at level $l$. Find a simple formula for $T_{nm} = \sum_{l \ge 1} T_{l_{\min}}$. 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...
TAOCP 7.2.2 Exercise 50
Section 7.2.2: Backtracking Exercise 50. [ M15 ] $[M15]$ Interpret the value $E(D_1, \ldots, D_n)$ in the text's Monte Carlo algorithm. 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 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.1.6 Exercise 58
Section 7.2.1.6: Generating All Trees Exercise 58. [ HM34 ] $[HM34]$ Let $t_{mn}$ be the number of $n$-node binary trees in which each external node appears at level $l$ when the external nodes are numbered from 0 to $n$ in symmetric order. Also let $t_m = \sum_{n\ge 0} t_{mn}$, so that $t_{mn}/C_n$ is the average level of external node $m$; and let $t(w,z)$ be the super generating function $$\sum_{m,n} t_{mn}...
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 48
Section 7.2.2: Backtracking Exercise 48. [ M42 ] $[M42]$ Find a maximum-size commafree code of length 4 on $m = 5$ letters. Verified: yes Solve time: 1m31s Setup Let $A={1,2,3,4,5}$ and let $A^4$ be the set of all words $x_1x_2x_3x_4$ over $A$. A set $C \subseteq A^4$ is a commafree code of length $4$ if for any $u,v \in C$, the concatenation $uv$ contains no codeword of $C$ as a...
TAOCP 7.2.1.6 Exercise 57
Section 7.2.1.6: Generating All Trees Exercise 57. [ M28 ] $[M28]$ Express the sums $S_p(a,b) = \sum_{k\ge 0} \binom{2k}{k+a} \binom{2b}{b-k} k^p$ in closed form for $p = 0, 1, 2, 3$, and use these formulas to prove (30). Verified: no Solve time: 6m08s 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 Exercise 47
Section 7.2.2: Backtracking Exercise 47. [ HM29 ] $[HM29]$ A commafree code on $m$ letters is equivalent to at most $2m!$ such codes if we permute the letters and/or replace each codeword by its left-right reflection. Determine all of the nonisomorphic commafree codes of length 4 on $m$ letters when $m$ is (a) 2 (b) 3 (c) 4 and there are (a) 3 (b) 18 (c) 57 codewords. Verified: no...
TAOCP 7.2.2 Exercise 46
Section 7.2.2: Backtracking Exercise 46. [ M35 ] $[M35]$ Is there a binary ($m = 2$) commafree code with one codeword in each of the $\bigl(\sum_{j,n} \phi(d) 2^{j/k}\bigr)/n$ cycle classes, for every word length $n$? 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 positive...
TAOCP 7.2.1.6 Exercise 56
Section 7.2.1.6: Generating All Trees Exercise 56. [ M25 ] $[M25]$ (Rényi–Szpankowski, 1990.) Prove the summation formula $$\sum_{k=0}^{m} C_k C_{n-1-k} = \frac{2n-m}{2n(n+1)} \binom{2n}{m} \binom{2n-2m}{n-m}, \quad \text{for } 0 \le m \le n.$$ Verified: no Solve time: 6m42s 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 Exercise 45
Section 7.2.2: Backtracking Exercise 45. ▶ [ 28 ] $[28]$ Continuing exercise 44, spell out the details of step C3 when $x \ge 0$. a) What updates should be done to MEM when a blue word $x$ becomes red? b) What updates should be done to MEM when a blue word $x$ becomes green? c) Step C3 finishes its job by making $x$ green as in part (b). Explain how...
TAOCP 7.2.1.6 Exercise 55
Section 7.2.1.6: Generating All Trees Exercise 55. [ M33 ] $[M33]$ Evaluate $C_{pq}(1)$, the total area of all paths in exercise 46(a). 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$ 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 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.1.6 Exercise 54
Section 7.2.1.6: Generating All Trees Exercise 54. [ HM29 ] $[HM29]$ What are the mean and variance of $c_1 + \cdots + c_n$? (See exercise 46.) 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$....
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.1.6 Exercise 53
Section 7.2.1.6: Generating All Trees Exercise 53. [ M28 ] $[M28]$ Let $X$ be the distance from the root of an extended binary tree to the leftmost external node. (a) What is the expected value of $X$, when all binary trees with $n$ nodes are equally likely? (b) What is the expected value of $X$ in a random binary search tree , constructed by Algorithm 6.2.2T from a random permutation...
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.1.6 Exercise 52
Section 7.2.1.6: Generating All Trees Exercise 52. [ M23 ] $[M23]$ Find the mean and variance of the quantity $d_n$ in Table 1, when nested parentheses $a_1 \ldots a_{2n}$ are chosen at random. Verified: no Solve time: 6m02s 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 Exercise 40
Section 7.2.2: Backtracking Exercise 40. ▶ [ 15 ] $[15]$ Why do you think sequential data structures such as (16)–(23) weren't featured in Section 2.2.2 of this series of books (entitled "Sequential Allocation")? Verified: no Solve time: 5m10s 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.1.6 Exercise 51
Section 7.2.1.6: Generating All Trees Exercise 51. [ M23 ] $[M23]$ Let $\hat{z}_1 \ldots \hat{z}_n$ be the $N$th combination of ${1, 2, \ldots, 2n}$ with respect to $2n$ in another words, $\hat{z}_j = 2n - z_j$, where $z_j$ is defined in (8). Show that if $\hat{z}_1 \hat{z}_2 \ldots \hat{z}_n$ is the $(N+1)$st $n$-combination of ${0, 1, \ldots, 2n-1}$ generated by Algorithm 7.2.1.3L, then $z_1 z_2 \ldots z_n$ is the $(N...
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.2 Exercise 38
Section 7.2.2: Backtracking Exercise 38. [ HM28 ] $[HM28]$ What is the probability that Eastman's algorithm finishes in one round? (Assume that $x$ is a random $m$-ary string of odd length $n > 1$, unequal to any of its other cyclic shifts. Use a generating function to express the answer.) Verified: no Solve time: 5m07s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2...
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 37
Section 7.2.2: Backtracking Exercise 37. ▶ [**] [ M 30] (W. L. Eastman, 1965.) The following elegant construction yields a commafree code of maximum size for any odd block length $n$, over any alphabet. Given a sequence $x = x_0 x_1 \ldots x_{n-1}$ of $n$ nonnegative integers, where $x$ differs from each of its other cyclic shifts $x_k \ldots x_{n-1} x_0 \ldots x_{k-1}$ for $0 < k < n$, the...
TAOCP 7.2.1.6 Exercise 48
Section 7.2.1.6: Generating All Trees Exercise 48. [ M28 ] $[M28]$ (Ruskey and Savage.) Prove that $C_{nn}(z) = (1+z)^{2n}/(1+z^{n+1}) - 1$, and use this result to show that no "perfect" Gray code for nested parentheses is possible when $n \ge 5$ is odd. 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...
TAOCP 7.2.2 Exercise 36
Section 7.2.2: Backtracking Exercise 36. [**] [ M 22] A two-letter block code on an $m$-letter alphabet can be represented as a digraph $D$ on $m$ vertices, with $a \to b$ if and only if $ab$ is a codeword. a) Prove that the code is commafree $\iff$ $D$ has no oriented paths of length 3. b) How many arcs can be in an $m$-vertex digraph with no oriented paths of...
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.1.6 Exercise 46
Section 7.2.1.6: Generating All Trees Exercise 46. [ M30 ] (Generalized Catalan numbers.) Generalize (21) by defining $$C_{pq}(x) = x^{p-q-1} C_{(p-1)q}(x) + x^{q} C_{(p-1)(q-1)}(x), \quad \text{if } 0 \le p \le q \ne 0; \quad C_{00}(x) = 1;$$ and $C_{pq}(x) = 0$ if $p < 0$ or $p > q$; thus $C_{pq} = C_{pq}(1)$. Also let $C_n(x) = C_{nn}(x)$, so that $(C_0(x), C_1(x), \ldots) = (1, 1, 1+x, 1+3x+x^2, 1+6x+6x^2+x^3,...
TAOCP 7.2.2 Exercise 34
Section 7.2.2: Backtracking Exercise 34. [ 15 ] [15] What's the largest commafree subset of the following words? aced babe bade bead beef cafe cede dada dead dear face fade feed Verified: no Solve time: 5m56s Each word has length 4. A set $S$ is commafree if for any $x,y \in S$, no word of $S$ appears as a nonaligned overlap across the boundary of $xy$. Equivalently, for $k=1,2,3$, the...
TAOCP 7.2.1.6 Exercise 45
Section 7.2.1.6: Generating All Trees Exercise 45. [ M26 ] Analyze the memory references made by the algorithm in exercise 22. How does it compare to Algorithm B? 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 one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots...
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.1.6 Exercise 44
Section 7.2.1.6: Generating All Trees Exercise 44. ▶ [ M27 ] Prove that Algorithm B makes only $8\frac{1}{3} + O(n^{-1})$ references to memory per binary tree visited. Verified: no Solve time: 5m47s 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...
TAOCP 7.2.2 Exercise 31
Section 7.2.2: Backtracking Exercise 31. [ 39 ] [39] (Charles Babbage, 1864.) Do any of the symmetric $5 \times 5$ word squares also have valid words on both diagonals? Verified: no Solve time: 4m54s 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.6 Exercise 43
Section 7.2.1.6: Generating All Trees Exercise 43. [ M11 ] Express $C_{pq}$ in terms of the Catalan numbers $C_0, C_1, C_2, \ldots$, aiming for a formula that is simple when $q - p$ is small. (For example, $C_{(p-1)q} = C_q - C_{q-1}$.) 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...
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.1.6 Exercise 42
Section 7.2.1.6: Generating All Trees Exercise 42. [ M22 ] How many unlabeled forests with $n$ nodes are (a) self-conjugate? (b) self-transpose? (c) self-dual? (See exercises 11, 12, 19, and 26.) Verified: no Solve time: 6m09s 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 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.1.6 Exercise 41
Section 7.2.1.6: Generating All Trees Exercise 41. [ M21 ] Show that the ballot numbers have a simple generating function $\sum C_{pq} n^p z^q$. Verified: no Solve time: 6m02s 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 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.1.6 Exercise 40
Section 7.2.1.6: Generating All Trees Exercise 40. [ M25 ] (a) Prove that $C_{pq}$ is odd if and only if $p\ &\ (q+1) = 0$, in the sense that the binary representations of $p$ and $q+1$ have no bits in common. (b) Therefore $C_n$ is odd if and only if $n+1$ is a power of 2. Verified: no Solve time: 6m07s Setup We seek all integers $n < 10^9$ such...
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.2 Exercise 22
Section 7.2.2: Backtracking Exercise 22. [ M26 ] [M26] Explore "loose Langford pairs": Replace '$j + k + 1$' in (7) by '$j + \lfloor 3k/2 \rfloor$'. 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...
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 21
Section 7.2.2: Backtracking Exercise 21. ▶ [ M25 ] [M25] If $x = x_1 x_2 \ldots x_{2n}$, let $x^D = (-x_{2n}) \ldots (-x_2)(-x_1) = -x^R$ be its dual. a) Show that if $x$ is odd and $x$ solves Langford's problem $(n)$, we have $x_k = n$ for some $k \le \lfloor n/2 \rfloor$ if and only if $x_k^D = n$ for some $k \le \lfloor n/2 \rfloor$. b) Find a...
TAOCP 7.2.1.6 Exercise 38
Section 7.2.1.6: Generating All Trees Exercise 38. [ M22 ] What is the total number of memory references performed by Algorithm L, as a function of $n$? Verified: no Solve time: 5m59s 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...
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.1.6 Exercise 37
Section 7.2.1.6: Generating All Trees Exercise 37. [ M40 ] Analyze the Zaks–Richards algorithm for generating all trees with a given distribution $n_0, n_1, n_2, \ldots, n_d$ of degrees (exercise 21). Hint: See exercise 2.3.4.4–32. Verified: no Solve time: 5m59s 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 19
Section 7.2.2: Backtracking Exercise 19. [ M10 ] [M10] What are the domains $D_l$ in Langford's problem (7)? Verified: no Solve time: 4m56s 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 among...
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.1.6 Exercise 36
Section 7.2.1.6: Generating All Trees Exercise 36. ▶ [ M25 ] Analyze the ternary tree generation algorithm of exercise 20(b). Hint: There are $(2n+1)^{-1}\binom{3n}{n}$ ternary trees with $n$ internal nodes, by exercise 2.3.4.4–11. Verified: no Solve time: 6m03s 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 Exercise 17
Section 7.2.2: Backtracking Exercise 17. [ 15 ] [15] Quick (a student noticed that the loop in step L2 of Algorithm L can be changed from 'while $x_j < 0$' to 'while $x_j \ne 0$', because $x_l$ cannot be positive at that point of the algorithm. So he decided to eliminate the minus signs and just set $x_{l+k+1} \leftarrow k$ in step L3. Was it a good idea? Verified: no...
TAOCP 7.2.1.6 Exercise 35
Section 7.2.1.6: Generating All Trees Exercise 35. [ HM37 ] (D. B. Tyler and D. R. Hickerson.) Explain why the denominators of the asymptotic formula (16) are all powers of 2. Verified: no Solve time: 5m48s 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 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.1.6 Exercise 34
Section 7.2.1.6: Generating All Trees Exercise 34. [ M25 ] (R. P. Stanley.) Show that the number of maximal chains in the Stanley lattice of order $n$ is $(n(n-1)/2)!/(1^{n-1}3^{n-2}\cdots(2n-3)^1(2n-3)!)$. 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...
TAOCP 7.2.2 Exercise 15
Section 7.2.2: Backtracking Exercise 15. [ HM42 ] [HM42] (M. Simkin, 2021.) Show that $Q(n) \approx \sigma^n n!$ as $n \to \infty$, where $\sigma \approx 0.38068$. 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 \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 33
Section 7.2.1.6: Generating All Trees Exercise 33. ▶ [ M27 ] (Permutation representation of trees.) Let $\sigma$ be the cycle $(1\ 2\ \ldots\ n)$. a) Given any binary tree whose nodes are numbered 1 to $n$ in symmetric order, prove that there is a unique permutation $\lambda$ of ${1,\ldots,n}$ such that, for $1 \le k \le n$, $$\text{LLINK}[k] = \begin{cases} k\lambda, & \text{if } k\lambda < k \ 0, &...
TAOCP 7.2.2 Exercise 14
Section 7.2.2: Backtracking Exercise 14. [ M25 ] [M25] If exercise 12 has $T(n)$ toroidal solutions, show that $Q(mn) \ge Q(m)^2 T(n)$. 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...
TAOCP 7.2.1.6 Exercise 32
Section 7.2.1.6: Generating All Trees Exercise 32. ▶ [ M30 ] [M30] Prove that if $F \dashv F'$, there is a forest $F''$ such that for all $G$ we have $$F' \sqcup G = F \quad \text{if and only if} \quad F \sqcap G \dashv F'.$$ Consequently the semidistributive laws hold in the Tamari lattice: $$F \sqcap G = F \sqcap H \quad \text{implies} \quad F \sqcap (G \sqcup H)...
TAOCP 7.2.2 Exercise 13
Section 7.2.2: Backtracking Exercise 13. [ M30 ] [M30] For which $n \ge 0$ does the $n$ queens problem have at least one solution? 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$...
TAOCP 7.2.1.6 Exercise 31
Section 7.2.1.6: Generating All Trees Exercise 31. ▶ [ M28 ] [M28] A binary tree with $n$ internal nodes is called degenerate if it has height $n$. a) How many $n$-node binary trees are degenerate? b) We've seen in Tables 1, 2, and 3 that binary trees and forests can be encoded by various $n$-tuples of numbers. For each of the encodings $c_1 \ldots c_n$, $d_1 \ldots d_n$, $e_1 \ldots...
TAOCP 7.2.2 Exercise 12
Section 7.2.2: Backtracking Exercise 12. [ M28 ] [M28] ( Wraparound queens. ) Replace (3) by the stronger conditions '$x_j \ne x_k$, $(x_k - x_j) \bmod n \ne k - j$, $(x_j - x_k) \bmod n \ne k - j$'. (The $n \times n$ grid becomes a torus.) Prove that the wraparound problem is solvable if and only if $n$ is not divisible by 2 or 3. Verified: no Solve...
TAOCP 7.2.2 Exercise 11
Section 7.2.2: Backtracking Exercise 11. [ M25 ] [M25] (W. Ahrens, 1910.) Both solutions of the $n$ queens problem when $n = 4$ have quartersurn symmetry : Rotation by 90° leaves them unchanged, but reflection doesn't. a) Can the $n$ queens problem have a solution with reflection symmetry? b) Show that quarterturn symmetry is impossible if $n \bmod 4 \in {2, 3}$. c) Sometimes the solution to an $n$ queens...
TAOCP 7.2.1.6 Exercise 30
Section 7.2.1.6: Generating All Trees Exercise 30. [ M26 ] [M26] The footprint of a forest is the bit string $f_1 \ldots f_n$ defined by $$f_j = [\text{node } j \text{ in preorder is not a leaf}].$$ a) If $F$ has footprint $f_1 \ldots f_n$, what is the footprint of $F^{D_2}$? (See exercise 27.) b) Two forests having the footprint 10101101111100001010100010110000? c) Prove that $f_j = [d_j = 0]$, for...
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.1.6 Exercise 29
Section 7.2.1.6: Generating All Trees Exercise 29. [ HM31 ] [HM31] The covering graph of a Tamari lattice is sometimes known as an "associahedron," because of its connection with the associative law (§4), proved in exercise 27(b). The associahedron of order 4, depicted in Fig. 61, looks like it has three square faces and six faces that are regular pentagons. (Compare with Fig. 43 in exercise 7.2.1.2.60, which shows the...
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.1.6 Exercise 28
Section 7.2.1.6: Generating All Trees Exercise 28. [ M26 ] [M26] (The Stanley lattice.) Continuing exercises 26 and 27, let us define yet another partial ordering on $n$-node forests, saying that $F \sqsubseteq F'$ whenever the depth coordinates $c_1, \ldots, c_n$ and $c'_1, \ldots, c'_n$ satisfy $c_j \le c'_j$ for $1 \le j \le n$. (See Fig. 62.) a) Prove that this partial ordering is a lattice, by explaining how...
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.1.6 Exercise 27
Section 7.2.1.6: Generating All Trees Exercise 27. ▶ [ M35 ] [M35] (The Tamari lattice.) Continuing exercise 26, let us write $F \dashv F'$ if the $j$th node in preorder has at least as many descendants in $F'$ as it does in $F$, for all $j$. In other words, if $F$ and $F'$ are characterized by their scope sequences $s_1, \ldots, s_n$ and $s'_1, \ldots, s'_n$ as in Table 2,...
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.1.6 Exercise 26
Section 7.2.1.6: Generating All Trees Exercise 26. [ M31 ] [M31] (The Kreweras lattice.) Let $F$ and $F'$ be $n$-node forests with their nodes numbered 1 to $n$ in preorder. We write $F \prec F'$ ($F$ coalesces $F''$) if $j$ and $k$ are siblings in $F$ whenever they are siblings in $F'$, for $1 \le j < k \le n$. Figure 60 illustrates this partial ordering in the case $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.2 Exercise 2
Section 7.2.2: Backtracking Exercise 2. [ 10 ] [10] True or false: We can choose $D_j$ so that $P_l(x_l)$ is always true. 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 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 21
Section 7.2.1.6: Generating All Trees Exercise 21. ▶ [ 26 ] [26] (S. Zaks and D. Richards, 1979.) Continuing exercise 20, explain how to generate the preorder degree sequences of all forests that have $N = n_0 + \cdots + n_k$ nodes, with exactly $n_j$ nodes of degree $j$. For example, when $n_0 = 4$, $n_1 = n_2 = n_3 = 1$, and $t = 3$, and the valid sequences...
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 19
Section 7.2.1.6: Generating All Trees Exercise 19. [ 28 ] [28] Let $F_1, F_2, \ldots, F_N$ be the sequence of unlabeled forests that correspond to the rooted plane trees generated by Algorithm P, and let $G_1, G_2, \ldots, G_N$ be the sequence of unlabeled forests that correspond to the binary trees generated by Algorithm B. Prove that $G_k = F_k^{PTB}$, in the notation of exercises 11 and 12. (The forest...
TAOCP 7.2.1.6 Exercise 18
Section 7.2.1.6: Generating All Trees Exercise 18. [ 30 ] [30] Two forests are said to be cognate if one can be obtained from the other by repeated operations of taking the conjugate and/or the transpose. The examples in exercises 11 and 12 show that all forests on 4 nodes belong to one of three cognate classes: $$\bullet \bullet \bullet \bullet = \begin{cases} \uparrow \ \end{cases}, \quad \underset{\displaystyle\uparrow}{\uparrow} \times =...
TAOCP 7.2.1.6 Exercise 17
Section 7.2.1.6: Generating All Trees Exercise 17. [ M16 ] [M16] Characterize all unlabeled forests $F$ such that $F^{BT} = F^{TR}$. (See exercise 14.) 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$...
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 124
Section 7.2.1.6: Generating All Trees Exercise 124. ▶ [ 40 ] [40] Experiment with methods for drawing extended binary trees that are inspired by simple models from nature. For example, we can assign a value $v(x)$ to each node $x$, called its Horton–Strahler number , as follows: Each external (leaf) node has $v(x) = 0$; an internal node with children $(l, r)$ has $v(x) = \max(v(l), v(r)) + [v(l) =...
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 122
Section 7.2.1.6: Generating All Trees Exercise 122. ▶ [ 31 ] [31] (Dudency's Digital Century puzzle.) There are many curious ways to obtain the number 100 by inserting arithmetical operators and possibly also parentheses into the sequence 123456789. For example, $$100 = 1 + 2 \times 3 + 4 \times 5 - 6 + 7 + 8 \times 9 = (1 + 2 - 3 - 4) \times (5 -...
TAOCP 7.2.1.6 Exercise 121
Section 7.2.1.6: Generating All Trees Exercise 121. [ M34 ] [M34] (F. Neuman, 1964.) The derivative of a graph $G$ is the graph $G^{(1)}$ obtained by removing all vertices of degree 1 and the edges touching them. Prove that, when $T$ is a free tree, its square $T^2$ contains a Hamiltonian path if and only if its derivative has no vertex of degree greater than 4 and the following two...
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 12
Section 7.2.1.6: Generating All Trees Exercise 12. [ 15 ] [15] If $F$ is a forest, its transpose $F^T$ is the forest whose binary tree is obtained by interchanging left and right links in the binary tree representing $F$. For example, the transposes of the fourteen forests in Table 1 are respectively $$\underset{}{\bullet} \quad \underset{}{\bullet}\underset{}{\bullet} \quad \underset{}{\bullet}!\overset{}{\bullet} \quad \underset{}{\bullet\bullet\bullet} \quad \cdots$$ What is the transpose of the forest (2)? Verified:...
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 118
Section 7.2.1.6: Generating All Trees Exercise 118. [ M28 ] [M28] How many lucky nodes are present in (a) the complete $t$-ary tree with $(t^k - 1)/(t - 1)$ internal nodes? (b) the Fibonacci tree of order $k$, with $F_{k+1} - 1$ internal nodes? (See 2.3.4.5–(6) and Fig. 8 in Section 6.2.1.) Verified: no Solve time: 5m48s Setup We seek all integers $n < 10^9$ such that the equation $x_1...
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 116
Section 7.2.1.6: Generating All Trees Exercise 116. ▶ [ 28 ] The nodes of a forest $F$ are labeled $1$ to $n$ in prepostorder; say that node $k$ is lucky if it is adjacent to node $k+1$ in $F$, unlucky if it is three steps away, and ordinary otherwise, for $1 \le k \le n$; in this definition, node $n+1$ is an imaginary super-root considered to be the parent of...
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 114
Section 7.2.1.6: Generating All Trees Exercise 114. [ 15 ] If we want to traverse an entire forest in prepostorder using Algorithm Q, how should we begin the process? 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...
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 112
Section 7.2.1.6: Generating All Trees Exercise 112. [ 15 ] If node $p$ of a forest precedes node $q$ in prepostorder and follows it in postpreorder, what can you say about $p$ and $q$? Verified: no Solve time: 5m41s 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.1.6 Exercise 111
Section 7.2.1.6: Generating All Trees Exercise 111. [ 05 ] List the nodes of the tree (58) in postpreorder. 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 in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the number of terms...
TAOCP 7.2.1.6 Exercise 110
Section 7.2.1.6: Generating All Trees Exercise 110. ▶ [ M27 ] Prove that if $G$ is any connected multigraph without self-loops, it has $$c(G) \ge \sqrt{(d_1 - 1) \cdots (d_n - 1)}$$ spanning trees, where $d_j$ is the degree of vertex $j$. 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...
TAOCP 7.2.1.6 Exercise 11
Section 7.2.1.6: Generating All Trees Exercise 11. [ 11 ] [11] If $F$ is a forest, its conjugate $F^{@}$ is obtained by left-to-right mirror reflection. For example, the fourteen forests in Table 1 are respectively $$\bullet \quad \bullet\bullet \quad \bullet,\text{\textasciicircum}!\bullet \quad \bullet\bullet\bullet \quad \bullet,\text{\textasciicircum}!\bullet\bullet \quad \text{...}$$ and their conjugates are respectively $$\bullet\bullet\bullet\bullet \quad \bullet\bullet,\text{\textasciicircum} \quad \text{...}$$ as in the colex forests of Table 2. If $F$ corresponds to the nested...
TAOCP 7.2.1.6 Exercise 109
Section 7.2.1.6: Generating All Trees Exercise 109. [ M46 ] Find a combinatorial explanation for the fact that (57) is the number of spanning trees in the $n$-cube. Verified: no Solve time: 5m56s 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 108
Section 7.2.1.6: Generating All Trees Exercise 108. [ HM40 ] Extend the results of exercises 104–106 to directed graphs. 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 x_2 \ge \cdots \ge x_n$. Let $k$ be the number of terms...
TAOCP 7.2.1.6 Exercise 107
Section 7.2.1.6: Generating All Trees Exercise 107. [ M24 ] Determine the aspects of all connected graphs that have $n \le 5$ vertices and no self-loops or parallel edges. Verified: no Solve time: 5m28s 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.6 Exercise 106
Section 7.2.1.6: Generating All Trees Exercise 106. ▶ [ HM7 ] Find the total number of spanning trees in (a) an $m \times n$ grid $P_m \mathbin{\square} P_n$; (b) an $m \times n$ cylinder $P_m \mathbin{\square} C_n$; (c) an $m \times n$ torus $C_m \mathbin{\square} C_n$. Do these numbers tend to have many small prime factors? Hint: Show that the numbers for $P_n$ and $C_n$ can be expressed as $4\sin^2\frac{j\pi}{2n}$...
TAOCP 7.2.1.6 Exercise 105
Section 7.2.1.6: Generating All Trees Exercise 105. [ HM18 ] Continuing exercise 104, we wish to prove that there is often an easy way to determine the aspects of $G$ when $G$ has been constructed from other graphs whose aspects are known. Suppose $G'$ has aspects $\alpha' 0, \ldots, \alpha' {s'-1}$ and $G''$ has aspects $\alpha'' 0, \ldots, \alpha'' {s''-1}$; what are the aspects of $G$ in the following cases?...
TAOCP 7.2.1.6 Exercise 104
Section 7.2.1.6: Generating All Trees Exercise 104. ▶ [ HM21 ] If $G$ is a graph on $n$ vertices ${V_1, \ldots, V_n}$, with $e_{ij}$ edges between $V_i$ and $V_j$, let $C(G)$ be the matrix with entries $c_{ij} = -e_{ij} + \delta_{ij} d_i$, where $d_i = e_{i1} + \cdots + e_{in}$ is the degree of $V_i$. Let us say that the aspects of $G$ are the eigenvalues of $C(G)$, namely the...
TAOCP 7.2.1.6 Exercise 103
Section 7.2.1.6: Generating All Trees Exercise 103. ▶ [ HM39 ] ( Sandpiles. ) Consider any digraph $D$ on vertices $V_0, V_1, \ldots, V_n$ with $e_{ij}$ arcs from $V_i$ to $V_j$, where $e_{ii} = 0$. Assume that $D$ has at least one oriented spanning tree rooted at $V_0$; this assumption means that, if we number the vertices appropriately, we have $e_{i0} + \cdots + e_{i,(i-1)} > 0$ for $1 \le...
TAOCP 7.2.1.6 Exercise 102
Section 7.2.1.6: Generating All Trees Exercise 102. [ 46 ] An oriented spanning tree of a directed graph $D$ on $n$ vertices, also known as a "spanning arborescence," is an oriented subtree of $D$ containing $n-1$ arcs. The matrix tree theorem (exercise 2.3.4.2–19) tells us that the oriented subtrees having a given root can readily be counted by evaluating an $(n-1) \times (n-1)$ determinant. Can those oriented subtrees be listed...
TAOCP 7.2.1.6 Exercise 101
Section 7.2.1.6: Generating All Trees Exercise 101. [ 46 ] Is there a simple revolving-door way to list all $n^{n-2}$ spanning trees of the complete graph $K_n$? (The order produced by Algorithm S is quite complicated.) 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 solution in...
TAOCP 7.2.1.6 Exercise 100
Section 7.2.1.6: Generating All Trees Exercise 100. [ 40 ] Implement the text's "Algorithm S*" for revolving-door generation of all spanning trees, by combining Algorithm S with the ideas of exercise 99. 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$ has exactly one solution in positive integers satisfying $x_1...
TAOCP 7.2.1.6 Exercise 10
Section 7.2.1.6: Generating All Trees Exercise 10. [ M20 ] [M20] ( Worm walks. ) Given a string of nested parentheses $a_1 a_2 \ldots a_{2n}$, let $w_j$ be the excess of left parentheses over right parentheses in $a_1 a_2 \ldots a_j$, for $0 \le j \le 2n$. Prove that $w_0 + w_1 + \cdots + w_{2n} = 2(c_1 + \cdots + c_n) + n$. Verified: no Solve time: 5m49s Setup...
TAOCP 7.2.1.6 Exercise 9
Section 7.2.1.6: Generating All Trees Exercise 9. [ M26 ] [M26] Show that the tables $c_1 \ldots c_n$ and $s_1 \ldots s_n$ are related by the law: $$c_k = \lfloor s_2 k - 1 \rfloor + \lfloor s_2 k - 2 \rfloor + \cdots + \lfloor s_2 k - 2 \rfloor + \cdots + \lfloor s_{k-1} \ge 1 \rfloor.$$ Verified: no Solve time: 5m54s Setup We seek all integers $n...
TAOCP 7.2.1.6 Exercise 8
Section 7.2.1.6: Generating All Trees Exercise 8. [ 15 ] [15] What tables $t_1 \ldots t_n$, $r_1 \ldots r_n$, $e_1 \ldots e_n$, and $s_1 \ldots s_n$ correspond to the example forest (2)? Verified: no Solve time: 5m46s 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.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 5
Section 7.2.1.6: Generating All Trees Exercise 5. [ 15 ] [15] What tables $d_1 \ldots d_n$, $z_1 \ldots z_n$, $p_1 \ldots p_n$, and $c_1 \ldots c_n$ correspond to the nested parenthesis string (1)? Verified: no Solve time: 5m54s 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.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.6 Exercise 1
Section 7.2.1.6: Generating All Trees Exercise 1. [ 15 ] [15] If a worm crawls around the binary tree (4), how could it easily reconstruct the parentheses of (1)? Verified: no Solve time: 5m46s 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 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 81
Section 7.2.1.5: Generating All Set Partitions Exercise 81. [ 29 ] Find a way to arrange an ordinary deck of 52 playing cards so that the following trick is possible: Five players each cut the deck (applying a cyclic permutation) as often as they like. Then each player takes a card from the top. A magician tells them to look at their cards and to form affinity groups, joining with...
TAOCP 7.2.1.5 Exercise 80
Section 7.2.1.5: Generating All Set Partitions Exercise 80. [ M25 ] Prove that universal sequences for ${1, 2, \ldots, n}$ exist in the sense of the previous exercise whenever $n \ge 4$. Verified: no Solve time: 5m48s 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.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 77
Section 7.2.1.5: Generating All Set Partitions Exercise 77. [ HM46 ] Find the asymptotic value of $p(n, \ldots, n)$ when there are $n$ $n$s. Verified: no Solve time: 5m47s 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.5 Exercise 76
Section 7.2.1.5: Generating All Set Partitions Exercise 76. [ HM16 ] Find the asymptotic value of $p(2, \ldots, 2)$ when there are $2n$ 2s. Verified: no Solve time: 5m46s 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.5 Exercise 75
Section 7.2.1.5: Generating All Set Partitions Exercise 75. [ HM21 ] Find the asymptotic value of $p(n, n)$. Verified: no Solve time: 5m58s 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 among...
TAOCP 7.2.1.5 Exercise 74
Section 7.2.1.5: Generating All Set Partitions Exercise 74. [ M46 ] Can $p(n, \ldots, n)$ be evaluated in polynomial time when there are $n$ $n$s? 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 in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let...
TAOCP 7.2.1.5 Exercise 73
Section 7.2.1.5: Generating All Set Partitions Exercise 73. [ M33 ] Can $p(2, \ldots, n)$ be evaluated in polynomial time when there are $2n$? Verified: no Solve time: 9m18s 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.5 Exercise 72
Section 7.2.1.5: Generating All Set Partitions Exercise 72. [ M26 ] Can $p(1, \ldots, n)$ be evaluated in polynomial time? 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$ 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 71
Section 7.2.1.5: Generating All Set Partitions Exercise 71. [ M20 ] How many partitions of ${n_1, \ldots, n_m, m}$ have exactly 2 parts? 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...
TAOCP 7.2.1.5 Exercise 70
Section 7.2.1.5: Generating All Set Partitions Exercise 70. [ M32 ] Analyze the number of $r$-block partitions possible in the $n$-element multisets (a) ${0, \ldots, 0; 1}$; (b) ${1, 2, \ldots, n-1}$. What is the total, summed over $r$? Verified: no Solve time: 5m33s 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 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 67
Section 7.2.1.5: Generating All Set Partitions Exercise 67. [ HM20 ] What are the mean and variance of $M$ in Stan's method (53)? Verified: no Solve time: 5m33s 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.5 Exercise 66
Section 7.2.1.5: Generating All Set Partitions Exercise 66. [ M46 ] What partition of $n$ leads to the most partitions of ${1, \ldots, n}$? 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 solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$...
TAOCP 7.2.1.5 Exercise 65
Section 7.2.1.5: Generating All Set Partitions Exercise 65. [ HM32 ] What is the variance of the number of blocks of size $k$ in a random partition of ${1, \ldots, n}$? 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...
TAOCP 7.2.1.5 Exercise 64
Section 7.2.1.5: Generating All Set Partitions Exercise 64. [ HM41 ] Prove the approximate ratios following (36) and exercise 50. Verified: no Solve time: 6m20s 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 63
Section 7.2.1.5: Generating All Set Partitions Exercise 63. ▶ [ M35 ] (J. Pitman.) Prove that there is an elementary way to locate the maximum Stirling numbers, and many similar quantities, as follows: Suppose $0 \le p_k \le 1$. a) Let $f(z) = (1 + p_1(z-1)) \cdots (1 + p_n(z-1))$ and $a_k = [z^k] f(z)$; thus $a_k$ is the probability that $k$ heads turn up after $n$ independent coin flips...
TAOCP 7.2.1.5 Exercise 62
Section 7.2.1.5: Generating All Set Partitions Exercise 62. [ HM40 ] Prove rigorously that $\binom{n}{m}$ is maximum either when $m = \lceil e^{-1}n \rceil$ or when $m = \lfloor e^{-1}n \rfloor$. 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 61
Section 7.2.1.5: Generating All Set Partitions Exercise 61. [ HM26 ] Prove that if $m = n - r$ where $r \le n^*$ and $z \le n^{1/2}$, Eq. (43) yields $$\left{ n \atop n-r \right} = \frac{n^{2r}}{2^r r!} \left(1 + O(n^{2r-1}) + O!\left(\frac{1}{r}\right)\right).$$ 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...
TAOCP 7.2.1.5 Exercise 60
Section 7.2.1.5: Generating All Set Partitions Exercise 60. [ HM21 ] (a) Show that the partial sums in the identity $$\left{ n \atop m \right} = \frac{m^n}{m!} - \frac{(m-1)^n}{1!(m-1)!} + \frac{(m-2)^n}{2!(m-2)!} - \cdots + (-1)^n \frac{0^n}{n!0!}$$ alternately overestimate and underestimate the final value. (b) Calculate $\binom{10}{5}$. (c) Derive a similar result from (43). Verified: no Solve time: 6m03s Setup We seek all integers $n < 10^9$ such that the equation...
TAOCP 7.2.1.5 Exercise 59
Section 7.2.1.5: Generating All Set Partitions Exercise 59. ▶ [ HM25 ] What does (43) predict for the approximate value of $\binom{n}{m}$? 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.5 Exercise 45
Section 7.2.1.5: Generating All Set Partitions Exercise 45. ▶ [ HM23 ] Show that, in addition to (26), we also have the expansion $$\varpi_n = \frac{e^{e^t - 1}}{t^n \sqrt{2\pi(t+1)}} \Biggl( 1 + \frac{b_1'}{n} + \frac{b_2'}{n^2} + \cdots + \frac{b_m'}{n^m} + O!\left(\frac{1}{n^{m+1}}\right) \Biggr),$$ where $b_1' = -(2t^4 + 9t^3 + 6t^2 + 6t + 2)/(24(t+1)^3)$. Verified: no Solve time: 5m32s Setup We seek all integers $n < 10^9$ such that the...
TAOCP 7.2.1.5 Exercise 44
Section 7.2.1.5: Generating All Set Partitions Exercise 44. [ HM22 ] Explain how to compute $b_1, b_2, \ldots$ in (26) from $a_2, a_3, \ldots$ in (25). Verified: no Solve time: 5m31s 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.5 Exercise 43
Section 7.2.1.5: Generating All Set Partitions Exercise 43. [ HM22 ] Justify replacing the integral in (23) by (25). Verified: no Solve time: 5m26s 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.1.5 Exercise 42
Section 7.2.1.5: Generating All Set Partitions Exercise 42. [ HM23 ] Use the saddle point method to estimate $[z^{n-1}] e^{z^2}$ with relative error $O(1/n^2)$. 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 x_2 \ge \cdots \ge x_n$. Let $k$...
TAOCP 7.2.1.5 Exercise 41
Section 7.2.1.5: Generating All Set Partitions Exercise 41. [ HM21 ] Solve the previous exercise when $c = -1$. Verified: no Solve time: 5m53s 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.1.5 Exercise 40
Section 7.2.1.5: Generating All Set Partitions Exercise 40. [ HM20 ] Suppose the saddle point method is used to estimate $[z^{n-1}] e^z$. The text's derivation of (21) from (20) deals with the case $c = 1$; how should that derivation change if $c$ is an arbitrary positive constant? Verified: no Solve time: 5m52s Setup We seek all integers $n < 10^9$ such that the equation $x_1 + x_2 + \cdots...
TAOCP 7.2.1.5 Exercise 39
Section 7.2.1.5: Generating All Set Partitions Exercise 39. [ HM18 ] Evaluate $\int_0^\infty e^{-pt^q} t^r , dt$ when $p$ and $q$ are nonnegative integers. Hint: See exercise 1.2.5–20. Verified: no Solve time: 5m41s 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.5 Exercise 38
Section 7.2.1.5: Generating All Set Partitions Exercise 38. ▶ [ M30 ] Let $\sigma_k$ be the cyclic permutation $(1, 2, \ldots, k)$. The object of this exercise is to study the sequences $k_1 k_2 \ldots k_n$, called $\sigma$-cycles, for which $\sigma_{k_1} \sigma_{k_2} \ldots \sigma_{k_n}$ is the identity permutation. For example, when $n = 4$ there are exactly 15 $\sigma$-cycles, namely $$1111,\ 1122,\ 1212,\ 1221,\ 1333,\ 2112,\ 2121,\ 2211,\ 2222,\ 2323,\...
TAOCP 7.2.1.5 Exercise 37
Section 7.2.1.5: Generating All Set Partitions Exercise 37. [ M18 ] Alexander Pushkin adopted an elaborate structure in his poetic novel Eugene Onegin (1833), based not only on "masculine" rhymes in which the sounds of accented final syllables agree with each other (pain–rain, form–warm, pun–fun, bucks–crux), but also on "feminine" rhymes in which one or two unstressed syllables also participate (humor–tumor, tetrameter–pentameter, lecture–conjecture, iguana–piranha). Every stanza of Eugene Onegin is...
TAOCP 7.2.1.5 Exercise 36
Section 7.2.1.5: Generating All Set Partitions Exercise 36. [ M21 ] [M21] Continuing exercise 35, what is the generating function $\sum_n \varpi'_n z^n/n!$? 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 solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be...
TAOCP 7.2.1.5 Exercise 35
Section 7.2.1.5: Generating All Set Partitions Exercise 35. [ M22 ] [M22] Let $\varpi'_n$ be the number of $n$-line poems that are "completely rhymed," in the sense that every line rhymes with at least one other. Then we have $(\varpi'_0, \varpi'_1, \varpi'_2, \ldots) = (1, 0, 1, 1, 4, 11, 41, \ldots)$. Give a combinatorial proof of the fact that $\varpi' n = \varpi' {n+1} - \varpi_n$. Verified: no Solve...
TAOCP 7.2.1.5 Exercise 34
Section 7.2.1.5: Generating All Set Partitions Exercise 34. [ 14 ] [14] Many poetic forms involve rhyme schemes , which are partitions of the lines of a stanza with the property that $j \equiv k$ if and only if line $j$ rhymes with line $k$. For example, a "limerick" is generally a 5-line poem with certain rhythmic constraints and with a rhyme scheme described by the restricted growth string $00110$....
TAOCP 7.2.1.5 Exercise 33
Section 7.2.1.5: Generating All Set Partitions Exercise 33. [ M21 ] [M21] How many partitions of ${1, 2, \ldots, n}$ are there in which every block has at most $k-1$ elements, where $n \bmod 6 = (1, 2, 3, 4, 5, 0)$? Prove that $\delta_n = (-1, 0, -1, 0, 1, 0)$ when $n \bmod 6 = (1, 2, 3, 4, 5, 0)$. Verified: no Solve time: 5m41s Setup We...
TAOCP 7.2.1.5 Exercise 32
Section 7.2.1.5: Generating All Set Partitions Exercise 32. [ M22 ] [M22] Let $\delta_n$ be the number of restricted growth strings $a_1 \ldots a_n$ for which the sum $a_1 + \cdots + a_n$ is even minus the number for which $a_1 + \cdots + a_n$ is odd. Prove that $$\delta_n = (-1,, 0,, -1,, 0,, 1,, 0) \quad \text{when } n \bmod 6 = (1,, 2,, 3,, 4,, 5,, 0).$$...
TAOCP 7.2.1.5 Exercise 31
Section 7.2.1.5: Generating All Set Partitions Exercise 31. [ HM21 ] [HM21] Generalizing (15), show that the elements of Peirce's triangle have a simple generating function, if we compute the sum $$\sum_n \varpi_n(x, y) \frac{z^{n-k}}{(n-k)!,(k-1)!}.$$ Verified: no Solve time: 4m30s 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.1.5 Exercise 30
Section 7.2.1.5: Generating All Set Partitions Exercise 30. [ HM30 ] [HM30] The generalized Stirling number $\begin{Bmatrix}n\m\end{Bmatrix}_q$ is defined by the recurrence $$\begin{Bmatrix}n+1\m\end{Bmatrix}_q = (1+q+\cdots+q^{m-1})\begin{Bmatrix}n\m\end{Bmatrix}_q + \begin{Bmatrix}n\m-1\end{Bmatrix}_q, \qquad \begin{Bmatrix}0\m\end{Bmatrix} q = \delta {m0}.$$ Thus $\begin{Bmatrix}n\m\end{Bmatrix}_q$ is a polynomial in $q$ and $\begin{Bmatrix}n\m\end{Bmatrix}_1 = \begin{Bmatrix}n\m\end{Bmatrix}$, because it satisfies the recurrence relation in Eq. 1.2.6--(46). a) Prove that the generalized Stirling number $\varpi_n(x, y) = R(n-1, \ldots, 1)$ of exercise 28(e) has...
TAOCP 7.2.1.5 Exercise 29
Section 7.2.1.5: Generating All Set Partitions Exercise 29. [ M26 ] [M26] Continuing the previous exercise, let $H_r(a_1, \ldots, a_m) = [x^r] R(a_1, \ldots, a_m)$ be the polynomial in $y$ that enumerates free cells when $r$ rooks are placed. a) Show that the number of ways to place $r$ rooks on an $m \times n$ board, leaving $f$ cells free, is the number of permutations of ${1, \ldots, n}$ that...
TAOCP 7.2.1.5 Exercise 28
Section 7.2.1.5: Generating All Set Partitions Exercise 28. ▶ [ M25 ] [M25] ( Generalized rook polynomials. ) Consider an arrangement of $a_1 + a_2 + \cdots + a_k$ square cells in rows and columns, where row $k$ contains cells in columns $1, \ldots, a_k$. Place zero or more "rooks" into the cells, with at most one rook in each row and at most one in each column. An empty...
TAOCP 7.2.1.5 Exercise 27
Section 7.2.1.5: Generating All Set Partitions Exercise 27. ▶ [ M35 ] [M35] A "vacillating tableau loop" of order $n$ is a sequence of integer partitions $\lambda_0 = \alpha_1 \alpha_2 \alpha_3 \ldots$ with $\alpha_1 \ge \alpha_2 \ge \alpha_3 \ge \cdots$ for $0 \le k \le 2n$, such that $\lambda_0 = \lambda_{2n} = \epsilon_0$ and $\lambda_i = \lambda_{i-1} + \epsilon_j$ for some $j$, with $0 \le j \le n$, here $\epsilon_j$...
TAOCP 7.2.1.5 Exercise 26
Section 7.2.1.5: Generating All Set Partitions Exercise 26. [ M2 ] [M2] According to the recurrence equations (13), the numbers $\varpi_{nk}$ in Peirce's triangle count the paths from $\binom{0}{0}$ to $\binom{n}{k}$ in the infinite directed graph Explain why each path from $\binom{0}{0}$ to $\binom{n}{k}$ corresponds to a partition of ${1, \ldots, n}$. Verified: no Solve time: 5m43s Setup We seek all integers $n < 10^9$ such that the equation $x_1...
TAOCP 7.2.1.5 Exercise 25
Section 7.2.1.5: Generating All Set Partitions Exercise 25. [ M32 ] [M32] Prove that $\varpi_n / \varpi_{n-1} \le \varpi_{n+1} / \varpi_n \le \varpi_{n+1} / \varpi_n + 1$. 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 one solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge...
TAOCP 7.2.1.5 Exercise 24
Section 7.2.1.5: Generating All Set Partitions Exercise 24. [ HM35 ] [HM35] Continuing the previous exercise, because the Bell numbers satisfy the periodic law $\varpi_{n+p^{p-1} \cdot N} \equiv \varpi_n \pmod{p^r}$, if $p$ is an odd prime. [ Hint: Show that $$x^p \equiv g_{r}(x) + p^{r-1} g_{r-1}(x) + \cdots + g_1(x) \pmod{p^r}$$ where $g_r(x) = g_r(x, p^{r-1}g_{r-1}(x), \ldots, p \cdot g_1(x))$ and $g_s(x) = (x^p - x)^s$.] Verified: no Solve time:...
TAOCP 7.2.1.5 Exercise 23
Section 7.2.1.5: Generating All Set Partitions Exercise 23. [ HM30 ] [HM30] If $f(z) = \sum_{k} a_k z^k$ is a polynomial, let $\hat{f}(\varpi)$ stand for $\sum a_k \varpi_k$. a) Prove the symbolic formula $f(\varpi + 1) = \pi \hat{f}(\varpi)$. (For example, if $f(x)$ is the polynomial $x^2$, this formula states that $\varpi_2 + 2\varpi_1 + \varpi_0 = \varpi_2$.) b) Similarly, prove that $f(\varpi + k) = \pi^k \hat{f}(\varpi)$ for all...
TAOCP 7.2.1.5 Exercise 22
Section 7.2.1.5: Generating All Set Partitions Exercise 22. [ M2 ] [M2] If $X$ is a random variable with a given distribution, the expected value of $X^n$ is called the $n$th moment of that distribution. What is the $n$th moment when $X$ is (a) a Poisson deviate with mean 1 [Eq. 3.4.1–(40)]? (b) the number of fixed points of a random permutation of ${1, \ldots, m}$, when $m \ge n$...
TAOCP 7.2.1.5 Exercise 21
Section 7.2.1.5: Generating All Set Partitions Exercise 21. [ M27 ] [M27] How many partitions of ${1, \ldots, n}$ are self-conjugate? 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 solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let $k$ be the number...
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 19
Section 7.2.1.5: Generating All Set Partitions Exercise 19. [ 28 ] Prove that there is a Gray code for restricted growth strings in which, at each step, some $a_j$ changes by either $\pm 1$ or $\pm 2$, when (a) we want to generate all $\varpi_n$ strings $a_1 \ldots a_n$; or (b) we want to generate only the $\binom{n}{m}$ cases with $\max(a_1, \ldots, a_n) = m-1$. Verified: no Solve time: 5m38s...
TAOCP 7.2.1.5 Exercise 18
Section 7.2.1.5: Generating All Set Partitions Exercise 18. [ M6 ] For which $n$ is it possible to generate all restricted growth strings $a_1 \ldots a_n$ in such a way that some $a_j$ changes by $\pm 1$ at each step? 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...
TAOCP 7.2.1.5 Exercise 17
Section 7.2.1.5: Generating All Set Partitions Exercise 17. [ 26 ] Implement Ruskey's Gray code (8) for all $m$-block partitions of ${1, \ldots, n}$. Verified: no Solve time: 5m30s 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.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 15
Section 7.2.1.5: Generating All Set Partitions Exercise 15. ▶ [ M21 ] What is the final partition generated by the algorithm of exercise 14? 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$...
TAOCP 7.2.1.5 Exercise 14
Section 7.2.1.5: Generating All Set Partitions Exercise 14. [ 29 ] Design an algorithm to generate set partitions ranked by block count, like (7). Verified: no Solve time: 5m58s 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.5 Exercise 13
Section 7.2.1.5: Generating All Set Partitions Exercise 13. [ M28 ] (Stephen C. Milne, 1977.) If $A$ is a set of partitions of ${1, \ldots, n}$, its shadow $\partial A$ is the set of all partitions $\Pi'$ such that $\Pi'$ covers $\Pi$ for some $\Pi \in A$. (We considered the analogous concept for the subset lattice in 7.2.1.3–(54).) Let $\Pi_1, \Pi_2, \ldots$ be the partitions of ${1, \ldots, n}$ in...
TAOCP 7.2.1.5 Exercise 12
Section 7.2.1.5: Generating All Set Partitions Exercise 12. [ M31 ] [M31] ( The partition lattice. ) If $\Pi$ and $\Pi'$ are partitions of the same set, we write $\Pi \le \Pi'$ if $x \equiv y \pmod{\Pi}$ implies $x \equiv y \pmod{\Pi'}$. In other words, $\Pi \le \Pi'$ means that $\Pi'$ is a "refinement" of $\Pi$, obtained by partitioning zero or more of the latter's blocks; and $\Pi$ is a...
TAOCP 7.2.1.5 Exercise 11
Section 7.2.1.5: Generating All Set Partitions Exercise 11. ▶ [ 28 ] [28] We observed in Section 7.2.1.2 that Dudency's famous problem send+more = money is a "pure" alphametic, namely an alphametic with a unique solution. His puzzle corresponds to a set partition on 13 digit positions, for which the restricted growth string $\rho(\texttt{sendmoremoney})$ is $012345614521!7$; and we might wonder how lucky he had to be in order to come...
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 9
Section 7.2.1.5: Generating All Set Partitions Exercise 9. [ M20 ] [M20] How many restricted growth strings $a_1 \ldots a_n$ contain exactly $k_j$ occurrences of $j$, given the integers $k_0, k_1, \ldots, k_{n-1}$? Verified: no Solve time: 9m31s 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.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 7
Section 7.2.1.5: Generating All Set Partitions Exercise 7. [ M20 ] [M20] How many permutations $a_1 \ldots a_n$ of ${1, \ldots, n}$ have the property that $a_{k-1} > a_k > a_j$ implies $j > k$? Verified: no Solve time: 5m41s 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.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 3
Section 7.2.1.5: Generating All Set Partitions Exercise 3. [ M23 ] [M23] What is the millionth partition of ${1, \ldots, 12}$ generated by Algorithm H? 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 solution in positive integers satisfying $x_1 \ge x_2 \ge \cdots \ge x_n$. Let...
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 73
Section 7.2.1.4: Generating All Partitions Exercise 73. [ M25 ] [M25] Suppose we write down all partitions of n, for example 6, 51, 42, 411, 33, 321, 3111, 222, 2211, 21111, 111111 when n = 6, and change each jth occurrence of k to j in each one: 1, 11, 11, 112, 12, 111, 1123, 123, 1212, 11234, 123456. a) Prove that this operation yields a permutation of the individual...
TAOCP 7.2.1.4 Exercise 72
Section 7.2.1.4: Generating All Partitions Exercise 72. [ M30 ] [M30] How many partitions of n have no predecessor in Bulgarian solitaire? Verified: no Solve time: 5m30s 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.4 Exercise 71
Section 7.2.1.4: Generating All Partitions Exercise 71. [ M46 ] [M46] Continuing the previous problem, what is the maximum number of steps that can occur before n-card Bulgarian solitaire reaches a cyclic state? Verified: no Solve time: 5m34s 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.1.4 Exercise 70
Section 7.2.1.4: Generating All Partitions Exercise 70. [ M30 ] [M30] (“Bulgarian solitaire.”) Take n cards and divide them arbitrarily into one or more piles. Then repeatedly remove one card from each pile and form a new pile. Show that if n = 1 + 2 + · · · + m, this process always reaches a self-repeating state with piles of sizes {m, m −1, . . . ,...
TAOCP 7.2.1.4 Exercise 69
Section 7.2.1.4: Generating All Partitions Exercise 69. [ M30 ] [M30] Find all n < 109 such that the equation x1 + x2 + · · · + xn = x1x2 . . . xn has only one solution in positive integers x1 ≥x2 ≥· · · ≥xn. (There is, for example, only one solution when n = 2, 3, or 4; but 5 + 2 + 1 + 1...
TAOCP 7.2.1.4 Exercise 68
Section 7.2.1.4: Generating All Partitions Exercise 68. [ M23 ] [M23] What partition of n into m parts has the largest product a1 . . . am, when (a) m is given; (b) m is arbitrary? Verified: no Solve time: 5m46s Solution Let the perfect partition be a multiset with distinct values $v_1 < v_2 < \cdots < v_t$, where each value $v_i$ occurs with multiplicity $b_i-1 \ge 0$. A...
TAOCP 7.2.1.4 Exercise 67
Section 7.2.1.4: Generating All Partitions Exercise 67. [ M25 ] [M25] (P. A. MacMahon, 1886.) A perfect partition of n is a multiset that has exactly n+1 submultisets, and these multisets are partitions of the integers 0, 1, . . . , n. For example, the multisets {1,1,1,1,1}, {2,2,1}, and {3,1,1} are perfect partitions of 5. Explain how to construct the perfect partitions of n that have fewest elements. Verified:...
TAOCP 7.2.1.4 Exercise 66
Section 7.2.1.4: Generating All Partitions Exercise 66. ▶ [ M25 ] [M25] (P-partitions.) Instead of insisting that a1 ≥a2 ≥· · · , suppose we want to consider all nonnegative compositions of n that satisfy a given partial order. For example, P. A. MacMahon observed that all solutions to the “up-down” inequalities a4 ≤a2 ≥a3 ≤a1 can be divided into five nonoverlapping types: a1 ≥a2 ≥a3 ≥a4; a1 ≥a2 ≥a4...
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 64
Section 7.2.1.4: Generating All Partitions Exercise 64. ▶ [ 32 ] [32] (Binary partitions.) Design a loopless algorithm that visits all partitions of n into powers of 2, where each step replaces 2k + 2k by 2k+1 or vice versa. Verified: no Solve time: 8m28s Setup Let $\lambda = (\lambda_1 \ge \lambda_2 \ge \cdots)$ and $\mu = (\mu_1 \ge \mu_2 \ge \cdots)$ be partitions of the same integer $n$. The...
TAOCP 7.2.1.4 Exercise 63
Section 7.2.1.4: Generating All Partitions Exercise 63. [ 47 ] [47] For which partitions λ and µ is there a Gray code through all partitions α such that λ ⪯α ⪯µ? Verified: no Solve time: 21m27s
TAOCP 7.2.1.4 Exercise 62
Section 7.2.1.4: Generating All Partitions Exercise 62. [ 46 ] [46] Prove or disprove: For all sufficiently large integers n and 3 ≤m < n such that n mod m ̸= 0, and for all partitions α of n with a1 ≤m, there is a Gray path for all partitions with parts ≤m, beginning at 1n and ending at α, unless α = 1n or α = 21n−2. Verified: no...
TAOCP 7.2.1.4 Exercise 61
Section 7.2.1.4: Generating All Partitions Exercise 61. [ 26 ] [26] Implement a partition-generation scheme based on Theorem S, always speci- fying the two parts that have changed between visits. Verified: no Solve time: 23m04s Setup A partition of $n$ is a nonincreasing sequence $$ a_1 \ge a_2 \ge \cdots \ge a_m \ge 1,\qquad a_1+\cdots+a_m=n. $$ A Gray path on partitions of $n$ is an ordering $\alpha_1,\ldots,\alpha_{p(n)}$ such that consecutive...
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 59
Section 7.2.1.4: Generating All Partitions Exercise 59. [ M22 ] [M22] The Gray path (59) is symmetrical in the sense that the reversed sequence 6, 51, . . . , 111111 is the same as the conjugate sequence (111111)T, (21111)T, . . . , (6)T. Find all Gray paths α1, . . . , αp(n) that are symmetrical in this way. Verified: no Solve time: 44m15s Correctness The solution does...
TAOCP 7.2.1.4 Exercise 58
Section 7.2.1.4: Generating All Partitions Exercise 58. [ M23 ] [M23] (Symmetrical means.) Let α = a1 . . . am and β = b1 . . . bm be partitions of n. Prove that the inequality 1 m! xa1 p1 . . . xam pm ≥ 1 m! xb1 p1 . . . xbm pm holds for all nonnegative values of the variables (x1, . . ....
TAOCP 7.2.1.4 Exercise 57
Section 7.2.1.4: Generating All Partitions Exercise 57. [ M22 ] [M22] Suppose a matrix (aij) of 0s and 1s has row sums ri = j aij and column sums cj = i aij. By permuting rows and columns we can assume that r1 ≥r2 ≥· · · and c1 ≥c2 ≥· · · . Then λ = r1r2 . . . and µ = c1c2 . . ....
TAOCP 7.2.1.4 Exercise 56
Section 7.2.1.4: Generating All Partitions Exercise 56. ▶ [ M32 ] [M32] Design an algorithm to generate all partitions α such that λ ⪯α ⪯µ, given partitions λ and µ with λ ⪯µ. Note: Such an algorithm has numerous applications. For example, to generate all partitions that have m parts and no part exceeding l, we can let λ be the smallest such partition, namely ⌈n/m⌉. . . ⌊n/m⌋as in...
TAOCP 7.2.1.4 Exercise 55
Section 7.2.1.4: Generating All Partitions Exercise 55. ▶ [ M37 ] [M37] Continuing the previous exercise, say that α covers β if α ⪰β and α ̸= β, and if α ⪰γ ⪰β implies that γ = α or γ = β. For example, Fig. 52 illustrates the covering relations between partitions of the number 12. a) Let us write α ≻β if α = a1a2 . . . and...
TAOCP 7.2.1.4 Exercise 54
Section 7.2.1.4: Generating All Partitions Exercise 54. ▶ [ M30 ] [M30] Let α = a1a2 . . . and β = b1b2 . . . be partitions of n. We say that α majorizes β, written α ⪰β or β ⪯α, if a1 + · · · + ak ≥b1 + · · · + bk for all k ≥0. a) True or false: α ⪰β implies α ≥β...
TAOCP 7.2.1.4 Exercise 53
Section 7.2.1.4: Generating All Partitions Exercise 53. ▶ [ M21 ] [M21] What is the millionth partition generated by Algorithm H when m = 32 and n = 100? Hint: 999999 = 80 12 + 66 11 + 50 7 + 41 6 + 33 5 + 26 4 + 21 4 . Verified: no Solve time: 15m27s Solution Let $a_1 \ge a_2 \ge \cdots \ge a_{32} \ge 1$ with...
TAOCP 7.2.1.4 Exercise 52
Section 7.2.1.4: Generating All Partitions Exercise 52. ▶ [ M21 ] [M21] What is the millionth partition generated by Algorithm P when n = 64? Hint: p(64) = 1741630 = 1000000 + 77 13 + 60 10 + 47 8 + 35 5 + 27 3 + 22 2 + 18 1 + 15 0 . Verified: no Solve time: 4m20s The statement of Exercise 7.2.1.4.50 is incomplete in the...
TAOCP 7.2.1.4 Exercise 51
Section 7.2.1.4: Generating All Partitions Exercise 51. [ M46 ] [M46] Make a detailed analysis of Algorithm H. Verified: no Solve time: 5m34s The statement of Exercise 7.2.1.4.50 is incomplete in the provided text: the definition of the quantities $c_m(n)$ via recurrences (56), (57), and the precise form of (58) are not included. These are essential, because every part of the exercise depends on how $c_m(n)$ is defined and how...
TAOCP 7.2.1.4 Exercise 50
Section 7.2.1.4: Generating All Partitions Exercise 50. [ HM33 ] [HM33] Let c(m) = cm(2m) in the recurrence (56), (57). a) Prove that cm(m + k) = m −k + c(k) for 0 ≤k ≤m. b) Consequently (58) holds for m ≤n ≤2m, if c(m) < 3p(m) for all m ≥0. c) Show that c(m) −m is the sum of the second-smallest parts of all partitions of m. d) Find...
TAOCP 7.2.1.4 Exercise 49
Section 7.2.1.4: Generating All Partitions Exercise 49. ▶ [ HM26 ] [HM26] (a) What is the generating function F(z) for the sum of the smallest parts of all partitions of n? (The series begins z + 3z2 + 5z3 + 9z4 + 12z5 + · · · .) (b) Find the asymptotic value of [zn] F(z), with relative error O(n−1). Verified: no Solve time: 5m40s Setup Let $p(n)$ be the...
TAOCP 7.2.1.4 Exercise 48
Section 7.2.1.4: Generating All Partitions Exercise 48. [ HM40 ] [HM40] Analyze the running time of the algorithm in the previous exercise. Verified: no Solve time: 24m13s Setup Let $p(n)$ be the partition function. Algorithm N generates one random partition of $n$ using a sequence of states $(m,c_1,\dots,c_n)$, starting with $m=n$ and terminating when $m=0$. Each iteration performs: Step N3: selection of a random integer $M$ with $0 \le M...
TAOCP 7.2.1.4 Exercise 47
Section 7.2.1.4: Generating All Partitions Exercise 47. ▶ [ HM22 ] [HM22] (A. Nijenhuis and H. S. Wilf, 1975.) The following simple algorithm, based on a table of the partition numbers p(0), p(1), . . . , p(n), generates a random partition of n using the part-count representation c1 . . . cn of (8). Prove that it produces each partition with equal probability. N1. [Initialize.] Set m ←n and...
TAOCP 7.2.1.4 Exercise 46
Section 7.2.1.4: Generating All Partitions Exercise 46. [ M20 ] [M20] In the text’s analysis of Algorithm P, which is larger, T ′ 2(n) or T ′′ 2 (n)? Verified: no Solve time: 8m36s Solution Let $S$ be a multiset of positive integers, and write its distinct values in increasing order as $$ 1 \le b_1 < b_2 < \cdots < b_t, $$ with multiplicities $m_1, m_2, \ldots, m_t$. A...
TAOCP 7.2.1.4 Exercise 45
Section 7.2.1.4: Generating All Partitions Exercise 45. [ HM21 ] [HM21] Compute the asymptotic value of p(n−1)/p(n), with relative error O(n−2). Verified: no Solve time: 21m59s Solution Let $p(n)$ denote the partition function. From the Hardy–Ramanujan asymptotic formula (first Rademacher term suffices at the required precision), $$ p(n)=\frac{1}{4n\sqrt{3}}\exp!\left(a\sqrt{n}\right)\left(1+O!\left(e^{-c\sqrt{n}}\right)\right), \qquad a=\pi\sqrt{\frac{2}{3}}, $$ so the error is exponentially small and does not affect any expansion in powers of $n^{-1/2}$. Hence $$ \frac{p(n-1)}{p(n)}...
TAOCP 7.2.1.4 Exercise 44
Section 7.2.1.4: Generating All Partitions Exercise 44. ▶ [ M22 ] [M22] How many partitions of n have their two smallest parts equal? Verified: no Solve time: 8m37s Solution Let $f_{l,m}(n)$ denote the number of partitions of $n$ having exactly $m$ parts and largest part equal to $l$. Let the ordinary generating function be $$ F_{l,m}(x) = \sum_{n \ge 0} f_{l,m}(n)x^n. $$ Every such partition can be written uniquely in...
TAOCP 7.2.1.4 Exercise 43
Section 7.2.1.4: Generating All Partitions Exercise 43. [ M18 ] [M18] Given n and k, how many partitions of n have a1 > a2 > · · · > ak? Verified: no Solve time: 8m56s Solution Let $f_{l,m}(n)$ denote the number of partitions of $n$ having exactly $m$ parts and largest part equal to $l$. Let the ordinary generating function be $$ F_{l,m}(x) = \sum_{n \ge 0} f_{l,m}(n)x^n. $$ Every...
TAOCP 7.2.1.4 Exercise 42
Section 7.2.1.4: Generating All Partitions Exercise 42. [ HM42 ] [HM42] Find the limiting shape, analogous to (49), for random partitions of n into at most θ√n parts, with no part exceeding φ√n, assuming that θφ > 1. Verified: no Solve time: 8m11s Solution Let $f_{l,m}(n)$ denote the number of partitions of $n$ having exactly $m$ parts and largest part equal to $l$. Let the ordinary generating function be $$...
TAOCP 7.2.1.4 Exercise 41
Section 7.2.1.4: Generating All Partitions Exercise 41. [ HM42 ] [HM42] Extend the Hardy–Ramanujan–Rademacher formula (32) to obtain a convergent series for partitions of n into at most m parts, with no part exceeding l. Verified: no Solve time: 8m49s Solution Let $f_{l,m}(n)$ denote the number of partitions of $n$ having exactly $m$ parts and largest part equal to $l$. Let the ordinary generating function be $$ F_{l,m}(x) = \sum_{n...
TAOCP 7.2.1.4 Exercise 40
Section 7.2.1.4: Generating All Partitions Exercise 40. ▶ [ M25 ] [M25] (F. Franklin.) Generalizing Theorem C, show that, for 0 ≤k ≤m, [zn] (1 −zl+1) . . . (1 −zl+k) (1 −z)(1 −z2) . . . (1 −zm) is the number of partitions a1a2 . . . of n into m or fewer parts with the property that a1 ≤ak+1 + l. Verified: no Solve time: 8m36s Solution Let...
TAOCP 7.2.1.4 Exercise 39
Section 7.2.1.4: Generating All Partitions Exercise 39. [ M20 ] [M20] (A. Cauchy.) Continuing exercise 38, what is the generating function for the number of partitions into m parts, all distinct and less than l? Verified: no Solve time: 8m18s Solution Let $f_{l,m}(n)$ denote the number of partitions of $n$ having exactly $m$ parts and largest part equal to $l$. Let the ordinary generating function be $$ F_{l,m}(x) = \sum_{n...
TAOCP 7.2.1.4 Exercise 38
Section 7.2.1.4: Generating All Partitions Exercise 38. [ M20 ] [M20] Given positive integers l and m, what generating function enumerates partitions that have exactly m parts, and largest part l? (See Eq. (51).) Verified: no Solve time: 19m15s Solution Let $f_{l,m}(n)$ denote the number of partitions of $n$ having exactly $m$ parts and largest part equal to $l$. Let the ordinary generating function be $$ F_{l,m}(x) = \sum_{n \ge...
TAOCP 7.2.1.4 Exercise 37
Section 7.2.1.4: Generating All Partitions Exercise 37. [ M22 ] [M22] Prove the inclusion-exclusion bracketing lemma (48), by analyzing how many times a partition that has exactly q different parts exceeding m is counted in the rth partial sum. Verified: no Solve time: 8m36s Setup Let $\lambda = (\lambda_1 \ge \lambda_2 \ge \cdots)$ and $\mu = (\mu_1 \ge \mu_2 \ge \cdots)$ be partitions of the same integer $n$ such that...
TAOCP 7.2.1.4 Exercise 36
Section 7.2.1.4: Generating All Partitions Exercise 36. [ HM24 ] [HM24] Prove the key estimate (47) that is needed in Theorem E. Verified: no Solve time: 8m14s Solution The Erdős–Lehner distribution (43) is the limiting distribution for the normalized random variable arising from the largest part (equivalently, the number of parts) of a random partition of $n$. In Knuth’s normalization, it is the Gumbel distribution with cumulative distribution function $$...
TAOCP 7.2.1.4 Exercise 35
Section 7.2.1.4: Generating All Partitions Exercise 35. [ HM21 ] [HM21] In the Erdős–Lehner probability distribution (43), what value of x is (a) most probable? (b) the median? (c) the mean? (d) What is the standard deviation? Verified: no Solve time: 23m26s Solution The Erdős–Lehner distribution (43) is the limiting distribution for the normalized random variable arising from the largest part (equivalently, the number of parts) of a random partition...
TAOCP 7.2.1.4 Exercise 34
Section 7.2.1.4: Generating All Partitions Exercise 34. ▶ [ HM21 ] [HM21] Show that n−m(m−1)/2 m is the number of partitions of n into m distinct parts. Consequently n m = nm−1 m! (m −1)! 1 + O m3 n when m ≤n1/3. Verified: no Solve time: 4m04s Solution Let $S(n,m)$ denote the number of set partitions of ${1,\dots,n}$ into $m$ parts, so $S(n,m)=\left|\begin{matrix} n \ m \end{matrix}\right|$...
TAOCP 7.2.1.4 Exercise 33
Section 7.2.1.4: Generating All Partitions Exercise 33. [ HM20 ] [HM20] Use the fact that there are exactly n−1 m−1 compositions of n into m parts, Eq. 7.2.1.3–(9), to prove a lower bound on n m . Then set m = ⌊√n ⌋to obtain an ele- mentary lower bound on p(n). 7.2.1.4 GENERATING ALL PARTITIONS 411 Verified: no Solve time: 16m37s Solution Let $S(n,m)$ denote the number of set...
TAOCP 7.2.1.4 Exercise 32
Section 7.2.1.4: Generating All Partitions Exercise 32. [ M15 ] [M15] Prove that n m ≤p(n −m) for all m, n ≥0. When does equality hold? Verified: no Solve time: 5m41s Setup Let $p(m)$ denote the number of partitions of $m$ in the sense of Section 7.2.1.4, and let $(c_1,\dots,c_n)$ be the part-count representation of a partition of $n$ as in (8). The algorithm of Exercise 47 maintains a parameter...
TAOCP 7.2.1.4 Exercise 31
Section 7.2.1.4: Generating All Partitions Exercise 31. [ M24 ] [M24] (A. De Morgan, 1843.) Show that n 2 = ⌊n/2⌋and n 3 = ⌊(n2 + 6)/12⌋; find a similar formula for n 4 . Verified: no Solve time: 26m02s Solution Let $\left| \begin{matrix} n \ k \end{matrix} \right|$ denote the number of partitions of $n$ into exactly $k$ parts, equivalently the number of partitions of $n$ whose Ferrers diagram...
TAOCP 7.2.1.4 Exercise 30
Section 7.2.1.4: Generating All Partitions Exercise 30. [ M17 ] [M17] Find closed forms for the sums (a) k≥0 n −km m −1 and (b) k≥0 n m −k (which are finite, because the terms being summed are zero when k is large). Verified: no Solve time: 14m41s Solution Let $m \ge 1$ and $n \ge 0$. Throughout, the binomial coefficient $\left|\begin{matrix} a \ b...
TAOCP 7.2.1.4 Exercise 29
Section 7.2.1.4: Generating All Partitions Exercise 29. ▶ [ M16 ] [M16] Generalizing (41), evaluate the sum a1≥a2≥···≥am≥1 za1 1 za2 2 . . . zam m . Verified: no Solve time: 5m30s Setup Let $A_k(n)$ denote the Hardy–Ramanujan–Rademacher coefficient defined in equation (34) of Section 7.2.1.4. In Lehmer’s formulation, $A_k(n)$ is a finite exponential sum depending only on $k$ and the quadratic discriminant $D(n)=1-24n,$ with multiplicative dependence on...
TAOCP 7.2.1.4 Exercise 28
Section 7.2.1.4: Generating All Partitions Exercise 28. [ HM42 ] [HM42] (D. H. Lehmer.) Show that the Hardy–Ramanujan–Rademacher coeffi- cients Ak(n) defined in (34) have the following remarkable properties: a) If k is odd, then A2k(km + 4n + (k2 −1)/8) = A2(m)Ak(n). b) If p is prime, pe > 2, and k ⊥2p, then Apek(k2m + p2en −(k2 + p2e −1)/24) = (−1)[pe=4]Ape(m)Ak(n). In this formula k2 + p2e...
TAOCP 7.2.1.4 Exercise 27
Section 7.2.1.4: Generating All Partitions Exercise 27. [ HM21 ] [HM21] Prove (28) and complete the calculations leading to Theorem D. Verified: no Solve time: 5m33s Solution Let $f(x)=e^{-x^{2}/n}, \qquad n>0.$ The Poisson summation formula in the form used in TAOCP states that for sufficiently rapidly decreasing $f$, $\sum_{k=-\infty}^{\infty} f(k)=\sum_{m=-\infty}^{\infty} \widehat{f}(m),$ where $\widehat{f}(m)=\int_{-\infty}^{\infty} f(x)e^{-2\pi i mx},dx.$ Compute the Fourier transform of $f$. Completing the square in the exponent, $-x^{2}/n-2\pi i...
TAOCP 7.2.1.4 Exercise 26
Section 7.2.1.4: Generating All Partitions Exercise 26. [ HM22 ] [HM22] In exercises 5.2.2–44 and 5.2.2–51 we studied two ways to prove that ∞ k=1 e−k2/n = 1 2(√πn −1) + O(n−M) for all M > 0. Show that Poisson’s summation formula gives a much stronger result. Verified: no Solve time: 20m22s Solution Let $f(x)=e^{-x^{2}/n}, \qquad n>0.$ The Poisson summation formula in the form used in TAOCP states that...
TAOCP 7.2.1.4 Exercise 25
Section 7.2.1.4: Generating All Partitions Exercise 25. [ HM27 ] [HM27] Improve on (22) by using (a) Euler’s summation formula and (b) Mellin transforms to estimate ln P(e−t). Hint: The dilogarithm function Li2(x) = x/12 + x2/22 + x3/32 + · · · satisfies Li2(x) + Li2(1 −x) = ζ(2) −(ln x) ln(1 −x). Verified: no Solve time: 24m12s Setup Let $$ P(q)=\prod_{k=1}^{\infty}(1-q^k)^{-1}, \qquad q=e^{-t}, \quad t>0. $$ We study...
TAOCP 7.2.1.4 Exercise 24
Section 7.2.1.4: Generating All Partitions Exercise 24. [ M26 ] [M26] (S. Ramanujan, 1919.) Let A(z) = ∞ k=1(1 −zk)4. a) Prove that [zn] A(z) is a multiple of 5 when n mod 5 = 4. b) Prove that [zn] A(z)B(z)5 has the same property, if B is any power series with integer coefficients. c) Therefore p(n) is a multiple of 5 when n mod 5 = 4. Verified: no...
TAOCP 7.2.1.4 Exercise 23
Section 7.2.1.4: Generating All Partitions Exercise 23. [ HM25 ] [HM25] Use Jacobi’s triple product identity (19) to prove another formula that he discovered: ∞ k=1 (1 −zk)3 = 1 −3z + 5z3 −7z6 + 9z10 −· · · = ∞ n=0 (−1)n(2n + 1)z(n+1 2 ). Verified: no Solve time: 8m12s Setup Let $$ E(z)=\prod_{k=1}^{\infty}(1-z^k), \qquad P(z)=\frac{1}{E(z)}=\sum_{n\ge 0} p(n)z^n. $$ Then $$ A(z)=\prod_{k=1}^{\infty}(1-z^k)^4 = E(z)^4. $$ The...
TAOCP 7.2.1.4 Exercise 22
Section 7.2.1.4: Generating All Partitions Exercise 22. [ HM21 ] [HM21] (L. Euler.) Let σ(n) be the sum of all positive divisors of the positive integer n. Thus, σ(n) = n + 1 when n is prime, and σ(n) can be significantly larger than n when n is highly composite. Prove that, in spite of this rather chaotic behavior, σ(n) satisfies almost the same recurrence (20) as the partition numbers:...
TAOCP 7.2.1.4 Exercise 21
Section 7.2.1.4: Generating All Partitions Exercise 21. [ M21 ] [M21] (L. Euler.) Let q(n) be the number of partitions of n into distinct parts. What is a good way to compute q(n) if you already know the values of p(1), . . . , p(n)? Verified: no Solve time: 8m18s Solution Let $$ F(a,b;u,v)=\sum_{k,l\ge 0} u^k v^l z^{kl} \frac{(z-az)(z-az^2)\cdots(z-az^k)}{(1-z)(1-z^2)\cdots(1-z^k)} \frac{(z-bz)(z-bz^2)\cdots(z-bz^l)}{(1-z)(1-z^2)\cdots(1-z^l)}. $$ Rewrite each finite product in $q$-shifted factorial form....
TAOCP 7.2.1.4 Exercise 20
Section 7.2.1.4: Generating All Partitions Exercise 20. ▶ [ M21 ] [M21] Approximately how long does it take to compute a table of the partition numbers p(n) for 1 ≤n ≤N, using Euler’s recurrence (20)? Verified: no Solve time: 7m17s Solution Let $$ F(a,b;u,v)=\sum_{k,l\ge 0} u^k v^l z^{kl} \frac{(z-az)(z-az^2)\cdots(z-az^k)}{(1-z)(1-z^2)\cdots(1-z^k)} \frac{(z-bz)(z-bz^2)\cdots(z-bz^l)}{(1-z)(1-z^2)\cdots(1-z^l)}. $$ Rewrite each finite product in $q$-shifted factorial form. For $k\ge 0$, $$ (z-az^i)=z(1-az^{i-1}),\quad 1\le i\le k, $$ so $$...
TAOCP 7.2.1.4 Exercise 19
Section 7.2.1.4: Generating All Partitions Exercise 19. [ M22 ] [M22] (E. Heine, 1847.) Prove the four-parameter identity ∞ m=1 (1−wxzm)(1−wyzm) (1−wzm)(1−wxyzm) = ∞ k=0 wk(x−1)(x−z) . . . (x−zk−1)(y−1)(y−z) . . . (y−zk−1)zk (1−z)(1−z2) . . . (1−zk)(1−wz)(1−wz2) . . . (1−wzk) . Hint: Carry out the sum over either k or l in the formula k,l≥0 ukvlzkl (z −az)(z −az2) . . . (z −azk)...
TAOCP 7.2.1.4 Exercise 18
Section 7.2.1.4: Generating All Partitions Exercise 18. ▶ [ M23 ] [M23] (Doron Zeilberger.) Show that there is a one-to-one correspondence be- tween pairs of integer sequences (a1, a2, . . . , ar; b1, b2, . . . , bs) such that a1 ≥a2 ≥· · · ≥ar, b1 > b2 > · · · > bs, and pairs of integer sequences (c1, c2, . . . , cr+s;...
TAOCP 7.2.1.4 Exercise 17
Section 7.2.1.4: Generating All Partitions Exercise 17. [ M26 ] [M26] A joint partition of n is a pair of sequences (a1, . . . , ar; b1, . . . , bs) of positive integers for which we have a1 ≥· · · ≥ar, b1 > · · · > bs, and a1 + · · · + ar + b1 + · · · + bs = n....
TAOCP 7.2.1.4 Exercise 16
Section 7.2.1.4: Generating All Partitions Exercise 16. [ M21 ] [M21] Find a formula for m,n p(k, m, n)wmzn, where p(k, m, n) is the number of partitions of n that have m parts and trace k. Sum it on k to obtain a nontrivial identity. 7.2.1.4 GENERATING ALL PARTITIONS 409 Verified: no Solve time: 6m39s Solution A partition of $n$ has trace $k$ when its Ferrers diagram has...
TAOCP 7.2.1.4 Exercise 14
Section 7.2.1.4: Generating All Partitions Exercise 14. ▶ [ M28 ] [M28] (J. J. Sylvester, 1882.) Find a one-to-one correspondence between parti- tions of n into distinct parts a1 > a2 > · · · > am that have exactly k “gaps” where aj > aj+1 + 1, and partitions of n into odd parts that have exactly k + 1 different values. (For example, when k = 0 this...
TAOCP 7.2.1.4 Exercise 15
Section 7.2.1.4: Generating All Partitions Exercise 15. [ M20 ] [M20] (J. J. Sylvester.) Find a generating function for the number of partitions that are self-conjugate (namely, partitions such that α = αT ). Verified: no Solve time: 24m20s Solution Let $\alpha$ be a self-conjugate partition of $n$. Its Ferrers diagram is symmetric with respect to the main diagonal. For each cell $(i,i)$ on the diagonal, consider the hook consisting...
TAOCP 7.2.1.4 Exercise 13
Section 7.2.1.4: Generating All Partitions Exercise 13. ▶ [ M23 ] [M23] (F. Franklin, 1882.) Find a one-to-one correspondence α ↔β between partitions of n such that α has exactly k parts repeated more than once if and only if β has exactly k even parts. (For example, the partition 64421111 has two repeated parts {4, 1} and three even parts {6, 4, 2}. The case k = 0 corresponds...
TAOCP 7.2.1.4 Exercise 12
Section 7.2.1.4: Generating All Partitions Exercise 12. ▶ [ M21 ] [M21] (L. Euler, 1750.) Use generating functions to prove that the number of ways to partition n into distinct parts is the number of ways to partition n into odd parts. For example, 5 = 4 + 1 = 3 + 2; 5 = 3 + 1 + 1 = 1 + 1 + 1 + 1 + 1....
TAOCP 7.2.1.4 Exercise 11
Section 7.2.1.4: Generating All Partitions Exercise 11. [ M22 ] [M22] How many ways are there to pay one euro, using coins worth 1, 2, 5, 10, 20, 50, and/or 100 cents? What if you are allowed to use at most two of each coin? Verified: no Solve time: 16m06s Solution Let $a_1,a_2,a_5,a_{10},a_{20},a_{50},a_{100}\ge 0$ denote the numbers of coins of each denomination used to form 100 cents. The condition for...
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 8
Section 7.2.1.4: Generating All Partitions Exercise 8. [ 15 ] [15] When (p1 . . . pt, q1 . . . qt) yields the rim representation of a partition a1a2 . . . as in (15) and (16), what’s the rim representation of the conjugate partition (a1a2 . . . )T ? Verified: no Solve time: 8m22s Solution Let $\alpha$ be a partition of $n$, written in frequency form as...
TAOCP 7.2.1.4 Exercise 7
Section 7.2.1.4: Generating All Partitions Exercise 7. [ M20 ] [M20] Suppose a1 . . . an and a′ 1 . . . a′ n are partitions of n with a1 ≥· · · ≥an ≥0 and a′ 1 ≥· · · ≥a′ n ≥0, and let their respective conjugates be b1 . . . bn = (a1 . . . an)T, b′ 1 . . . b′ n =...
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 4
Section 7.2.1.4: Generating All Partitions Exercise 4. [ M22 ] [M22] (Gideon Ehrlich, 1974.) What is the lexicographically smallest partition of n in which all parts are ≥r? For example, when n = 19 and r = 5 the answer is 766. Verified: no Solve time: 8m24s Solution Let the Ferrers diagram of $a_1a_2\cdots a_m$ consist of cells $(i,j)$ with $1\le i\le m$ and $1\le j\le a_i$. The conjugate partition...
TAOCP 7.2.1.4 Exercise 3
Section 7.2.1.4: Generating All Partitions Exercise 3. [ M17 ] [M17] A partition a1 + · · · + am of n into m parts a1 ≥· · · ≥am is optimally balanced if |ai −aj| ≤1 for 1 ≤i, j ≤m. Prove that there is exactly one such partition, whenever n ≥m ≥1, and give a simple formula that expresses the jth part aj as a function of j,...
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.4 Exercise 1
Section 7.2.1.4: Generating All Partitions Exercise 1. ▶ [ M21 ] x 1. [M21] Give formulas for the total number of possibilities in each problem of The Twelvefold Way. For example, the number of n-tuples of m things is mn. (Use the notation (38) when appropriate, and be careful to make your formulas correct even when m = 0 or n = 0.) Verified: no Solve time: 5m25s Solution The...
TAOCP 7.2.1.3 Exercise 99
Section 7.2.1.3: Generating All Combinations Exercise 99. [ M25 ] [M25] A clutter is a set C of combinations that are incomparable, in the sense that α ⊆β and α, β ∈C implies α = β. The size vector of a clutter is defined as in exercise 97. a) Find a necessary and sufficient condition that (M0, M1, . . . , Mn) is the size vector of a clutter....
TAOCP 7.2.1.3 Exercise 98
Section 7.2.1.3: Generating All Combinations Exercise 98. [ 30 ] [30] Continuing exercise 97, find an efficient way to count the feasible size vectors (N0, N1, . . . , Nn) when n ≤100. Verified: no Solve time: 17m17s Setup Let $[n]={1,2,\dots,n}$. A simplicial complex $C\subseteq 2^{[n]}$ is an order ideal under inclusion, meaning that $\beta\in C$ and $\alpha\subseteq\beta$ imply $\alpha\in C$. Its size vector is $$ (N_0,N_1,\dots,N_n), $$ where...
TAOCP 7.2.1.3 Exercise 97
Section 7.2.1.3: Generating All Combinations Exercise 97. ▶ [ M26 ] [M26] The text remarked that the vertices of a convex polyhedron can be per- turbed slightly so that all of its faces are simplexes. In general, any set of combinations that contains the shadows of all its elements is called a simplicial complex; thus C is a simplicial complex if and only if α ⊆β and β ∈C implies...
TAOCP 7.2.1.3 Exercise 96
Section 7.2.1.3: Generating All Combinations Exercise 96. ▶ [ M22 ] [M22] If S is an infinite sequence (s0, s1, s2, . . . ) of positive integers, let S(n) k = [zk] n−1 j=0 (1 + z + · · · + zsj); thus S(n) k is the ordinary binomial coefficient n k if s0 = s1 = s2 = · · · = 1....
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 94
Section 7.2.1.3: Generating All Combinations Exercise 94. [ M20 ] [M20] Show that the ∂half of Corollary C follows from the ∂ half. Hint: The complements of the multicombinations (92) with respect to U are 3211, 3210, 3200, 3110, 3100, 3000, 2110, 2100, 2000, 1100, 1000. Verified: no Solve time: 24m47s Solution Let $U$ denote the set of all multicombinations under consideration in Corollary C, represented in the form $c_4c_3c_2c_1$...
TAOCP 7.2.1.3 Exercise 93
Section 7.2.1.3: Generating All Combinations Exercise 93. [ M25 ] [M25] (a) Find an N for which the conclusion of Theorem W is false when the parameters m1, m2, . . . , mn have not been sorted into nondecreasing order. (b) Where does the proof of that theorem use the hypothesis that m1 ≤m2 ≤· · · ≤mn? Verified: no Solve time: 17m07s Solution Let $n \ge m \ge...
TAOCP 7.2.1.3 Exercise 92
Section 7.2.1.3: Generating All Combinations Exercise 92. [ M28 ] [M28] Let x = x1 . . . xn−1 be the Nth element of the torus T(m1, . . . , mn−1), and let S be the set of all elements of T(m1, . . . , mn−1, m) that are ⪯x1 . . . xn−1(m−1) in cross order. If Na elements of S have final component a, for 0...
TAOCP 7.2.1.3 Exercise 91
Section 7.2.1.3: Generating All Combinations Exercise 91. [ M24 ] [M24] Prove Theorem W for two-dimensional toruses T(l, m), l ≤m. Verified: no Solve time: 6m21s Solution Algorithm H generates all integer partitions $a_1 \ge \cdots \ge a_m \ge 1$ of $n$ by maintaining a weakly decreasing sequence whose entries are positive and whose sum is always $n$. To obtain partitions of $n$ into at most $m$ parts, the correct...
TAOCP 7.2.1.3 Exercise 90
Section 7.2.1.3: Generating All Combinations Exercise 90. [ M22 ] [M22] Prove the basic compression lemma, (85). Verified: no Solve time: 7m50s Solution Algorithm H generates all integer partitions $a_1 \ge \cdots \ge a_m \ge 1$ of $n$ by maintaining a weakly decreasing sequence whose entries are positive and whose sum is always $n$. To obtain partitions of $n$ into at most $m$ parts, the correct model is to allow...
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 88
Section 7.2.1.3: Generating All Combinations Exercise 88. [ M20 ] [M20] Explain why cross order is useful, by completing the proof of Lemma S. Verified: no Solve time: 5m25s Correctness The solution does not address the stated problem at all. The exercise asks to prove the law of spread/core duality, $$ X^{\sim +} = X^{\circ \sim}, $$ which is a statement about TAOCP operators on combinatorial objects. The proposed solution...
TAOCP 7.2.1.3 Exercise 87
Section 7.2.1.3: Generating All Combinations Exercise 87. [ M21 ] [M21] True or false: (a) X ⊆Y ◦if and only if Y ∼⊆X∼◦; (b) X◦+◦= X◦; (c) αM ≤N if and only if M ≤βN. Verified: no Solve time: 16m04s Correctness The solution does not address the stated problem at all. The exercise asks to prove the law of spread/core duality, $$ X^{\sim +} = X^{\circ \sim}, $$ which is...
TAOCP 7.2.1.3 Exercise 86
Section 7.2.1.3: Generating All Combinations Exercise 86. [ M20 ] [M20] Prove the law of spread/core duality, X∼+ = X◦∼. Verified: no Solve time: 20m Correctness The solution does not address the stated problem at all. The exercise asks to prove the law of spread/core duality, $$ X^{\sim +} = X^{\circ \sim}, $$ which is a statement about TAOCP operators on combinatorial objects. The proposed solution instead discusses clutters, antichains,...
TAOCP 7.2.1.3 Exercise 85
Section 7.2.1.3: Generating All Combinations Exercise 85. [ HM21 ] [HM21] Relate the functions λtN and µtN to the Takagi function τ(x). Verified: no Solve time: 12m23s Solution Let $T=\binom{2t-1}{t}$ and write $x=N/T$. In Section 7.2.1.3, the number $\kappa_{tN}$ is expressed through the binary representation of $N$ by decomposing the corresponding $(s,t)$-combination into the associated compositions $q_t,\dots,q_0$ of (11). The argument leading to Exercise 84 shows that the fluctuation of...
TAOCP 7.2.1.3 Exercise 84
Section 7.2.1.3: Generating All Combinations Exercise 84. [ HM27 ] [HM27] If T = 2t−1 t , prove the asymptotic formula κtN −N = T t τ N T O (log t)3 t for 0 ≤N ≤T. 7.2.1.3 GENERATING ALL COMBINATIONS 387 Verified: no Solve time: 5m19s Setup A simplicial complex on an $n$-element vertex set is an order ideal in the Boolean lattice, so if...
TAOCP 7.2.1.3 Exercise 83
Section 7.2.1.3: Generating All Combinations Exercise 83. [ HM46 ] [HM46] Determine the set R of all rational numbers r such that the equation τ(x) = r has uncountably many solutions. If τ(x) is rational and x is irrational, is it true that τ(x) ∈R? (Warning: This problem can be addictive.) Verified: no Solve time: 4m38s 1. Structure of Takagi level sets Let $\tau:[0,1]\to\mathbb{R}$ be the Takagi function. A standard...
TAOCP 7.2.1.3 Exercise 82
Section 7.2.1.3: Generating All Combinations Exercise 82. [ HM31 ] [HM31] The Takagi function of Fig. 47 is defined for 0 ≤x ≤1 by the formula τ(x) = ∞ k=1 x 0 rk(t) dt, where rk(t) = (−1)⌊2kt⌋is the Rademacher function of Eq. 7.2.1.1–(16). a) Prove that τ(x) is continuous in the interval [0 . . 1], but its derivative does not exist at any point. b) Show that...
TAOCP 7.2.1.3 Exercise 81
Section 7.2.1.3: Generating All Combinations Exercise 81. ▶ [ M27 ] [M27] Show that the minimum shadow sizes in Theorem M are given by (64). Verified: no Solve time: 2m50s The exercise, as stated here, cannot be solved because its mathematical content has been omitted. Exercise 7.2.1.3.81 is: Show that the minimum shadow sizes in Theorem M are given by (64) . A correct solution necessarily depends on the precise...
TAOCP 7.2.1.3 Exercise 80
Section 7.2.1.3: Generating All Combinations Exercise 80. [ HM26 ] [HM26] (L. Lovász, 1979.) The function x t increases monotonically from 0 to ∞ as x increases from t −1 to ∞; hence we can define κtN = x t −1 , if N = x t and x ≥t −1. Prove that κtN ≥κtN for all integers t ≥1 and N ≥0. Hint: Equality holds...
TAOCP 7.2.1.3 Exercise 79
Section 7.2.1.3: Generating All Combinations Exercise 79. [ M23 ] [M23] Prove that if t ≥2, we have M ≥µtN if and only if M + λt−1M ≥N. Verified: no Solve time: 12m05s Solution Write the unique representation of an integer $X \ge 0$ in the $t$-binomial number system as $$ X = \binom{x_t}{t} + \binom{x_{t-1}}{t-1} + \cdots + \binom{x_1}{1}, $$ where $x_t > x_{t-1} > \cdots > x_1 \ge...
TAOCP 7.2.1.3 Exercise 78
Section 7.2.1.3: Generating All Combinations Exercise 78. [ M22 ] [M22] Show that Theorem K follows easily from inequality (b) in the previous exercise. Conversely, both inequalities are simple consequences of Theorem K. Hint: Any set A of t-combinations can be written A = A1+A00, where A1 = {α ∈A | 0 /∈α}. Verified: no Solve time: 20m34s Solution Let $\kappa_t$ denote the function defined in Section 7.2.1.3 via the...
TAOCP 7.2.1.3 Exercise 77
Section 7.2.1.3: Generating All Combinations Exercise 77. ▶ [ M26 ] [M26] Prove the following properties of the κ functions by manipulating binomial coefficients, without assuming Theorem K: a) κt(M + N) ≤κtM + κtN. b) κt(M + N) ≤max(κtM, N) + κt−1N. Hint: mt t · · · + m1 1 nt t · · · + n1 1 is equal to mt∨nt t ...
TAOCP 7.2.1.3 Exercise 76
Section 7.2.1.3: Generating All Combinations Exercise 76. [ M20 ] [M20] Find a simple formula for κt(N + 1) −κtN. Verified: no Solve time: 13m43s Solution The function $\kappa_t N$ arises from the combinatorial number system in which an integer $N$ is written uniquely in the form $$ N = \binom{n_t}{t} + \binom{n_{t-1}}{t-1} + \cdots + \binom{n_v}{v}, $$ with strictly decreasing upper indices, as developed in the preceding exercises. In...
TAOCP 7.2.1.3 Exercise 75
Section 7.2.1.3: Generating All Combinations Exercise 75. [ M20 ] [M20] The right-hand side of (60) is not always the degree-(t −1) combinatorial representation of κtN, because v −1 might be zero. Show, however, that a positive integer N has at most two representations if we allow v = 0 in (57), and both of them yield the same value κtN according to (60). Therefore κkκk+1 . . . κtN...
TAOCP 7.2.1.3 Exercise 74
Section 7.2.1.3: Generating All Combinations Exercise 74. [ M21 ] [M21] What are | ∂ PNt| and | ∂ QNnt| in Theorem K? Verified: no Solve time: 6m27s Solution Corollary C establishes that an $(s,t)$-combination can be represented equivalently as a binary string $a_{n-1}\dots a_1a_0$ with $t$ ones, as a decreasing sequence $c_t>\cdots>c_1$, as the complementary sequence $b_s>\cdots>b_1$ of zeros, as a composition $p_t,\dots,p_0$ or $q_t,\dots,q_0$, and as a monotone...
TAOCP 7.2.1.3 Exercise 73
Section 7.2.1.3: Generating All Combinations Exercise 73. [ M23 ] [M23] (A. J. W. Hilton, 1976.) Let A be a set of s-combinations and B a set of t-combinations, both contained in U = {0, . . . , n −1} where n ≥s + t. Show that if A and B are cross-intersecting, in the sense that α ∩β ̸= ∅for all α ∈A and β ∈B, then so...
TAOCP 7.2.1.3 Exercise 72
Section 7.2.1.3: Generating All Combinations Exercise 72. ▶ [ M22 ] [M22] Show that if N has the degree-t combinatorial representation (57), there is an easy way to find the degree-s combinatorial representation of the complementary number M = s+t t −N, whenever N < s+t t . Derive (63) as a consequence. Verified: no Solve time: 6m29s Solution Theorem W is proved in Section 7.2.1.3 under the...
TAOCP 7.2.1.3 Exercise 71
Section 7.2.1.3: Generating All Combinations Exercise 71. [ M20 ] [M20] How many t-cliques can a million-edge graph have? Verified: no Solve time: 15m08s Solution Let $G$ be a simple graph with $m=10^6$ edges, and let $K_t(G)$ denote the number of $t$-cliques in $G$. A $t$-clique is determined by a $t$-subset of vertices whose induced subgraph contains all $\binom{t}{2}$ edges. Each such clique uses $\binom{t}{2}$ edges, but edges can be...
TAOCP 7.2.1.3 Exercise 70
Section 7.2.1.3: Generating All Combinations Exercise 70. [ M25 ] [M25] What is the maximum value of κtN −N, for N ≥0? Verified: no Solve time: 15m55s Solution Let $\mathcal{A}$ be a set of $t$-combinations and let $|\mathcal{A}| = N$. The operator $\kappa_t N$ denotes the minimum possible size of the shadow of any family of $N$ $t$-combinations, that is $$ \kappa_t N = \min_{|\mathcal{A}| = N} |\partial \mathcal{A}|, $$...
TAOCP 7.2.1.3 Exercise 69
Section 7.2.1.3: Generating All Combinations Exercise 69. ▶ [ M22 ] [M22] How large is the smallest set A of t-combinations for which |∂A| < |A|? Verified: no Solve time: 16m44s Solution Let $A$ be a set of $t$-combinations of ${0,1,\dots,n-1}$. In this section $\partial A$ denotes the external neighborhood in the Johnson graph $J(n,t)$: a $t$-combination $\beta$ lies in $\partial A$ if and only if $\beta \notin A$ and...
TAOCP 7.2.1.3 Exercise 68
Section 7.2.1.3: Generating All Combinations Exercise 68. [ M01 ] [M01] If α is a t-combination, what is (a) ∂tα? (b) ∂t+1α? Verified: no Solve time: 10m34s Let $\alpha$ be a $t$-combination, so $\alpha$ is a $t$-element subset of ${0,1,\dots,n-1}$. The operator $\partial_t \alpha$ produces all $(t-1)$-combinations obtained by deleting one element of $\alpha$. If $\alpha={c_t,\dots,c_1}$, then $$ \partial_t \alpha={\alpha\setminus{c_j}\mid 1\le j\le t}. $$ Each element of $\partial_t \alpha$ is...
TAOCP 7.2.1.3 Exercise 67
Section 7.2.1.3: Generating All Combinations Exercise 67. [ 46 ] [46] Consider the Ising configurations of exercise 13 for which a0 = 0. Given n, t, and r, is there a Gray cycle for these configurations in which all transitions have the forms 0k1 ↔10k or 01k ↔1k0? For example, in the case n = 9, t = 5, r = 6, there is a unique cycle (010101110, 010110110, 011010110,...
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 65
Section 7.2.1.3: Generating All Combinations Exercise 65. [ M40 ] [M40] Enumerate the total number of genlex Gray paths on subcubes that use only the transformations allowed in exercise 64. How many of those paths are cycles? Verified: no Solve time: 7m53s Solution Let the $2 \times 2 \times 3$ torus be the Cartesian product $C_2 \times C_2 \times C_3,$ so its elements are triples $(i,j,k)$ with $i \in {0,1}$,...
TAOCP 7.2.1.3 Exercise 64
Section 7.2.1.3: Generating All Combinations Exercise 64. ▶ [ M30 ] [M30] Construct a genlex Gray cycle for all of the 2ss+t t subcubes that have s digits and t asterisks, using only the transformations ∗0 ↔0∗, ∗1 ↔1∗, 0 ↔1. For example, one such cycle when s = t = 2 is (00∗∗, 01∗∗, 0∗1∗, 0∗∗1, 0∗∗0, 0∗0∗, ∗00∗, ∗01∗, ∗0∗1, ∗0∗0, ∗∗00, ∗∗01, ∗∗11, ∗∗10, ∗1∗0, ∗1∗1,...
TAOCP 7.2.1.3 Exercise 63
Section 7.2.1.3: Generating All Combinations Exercise 63. [ M41 ] [M41] Show that all contingency tables for (r1, . . . , rm; c1, . . . , cn) can be gener- ated by changing exactly four entries of the matrix at each step. Verified: no Solve time: 20m11s Setup Let $(a_{ij})$ be an $m\times n$ contingency table with fixed row sums $\sum_{j=1}^n a_{ij}=r_i \quad (1\le i\le m)$ and column...
TAOCP 7.2.1.3 Exercise 62
Section 7.2.1.3: Generating All Combinations Exercise 62. ▶ [ M27 ] [M27] A contingency table is an m×n matrix of nonnegative integers (aij) having given row sums ri = n j=1 aij and column sums cj = m i=1 aij, where r1 + · · · + rm = c1 + · · · + cn. a) Show that 2 × n contingency tables are equivalent to bounded compositions. b)...
TAOCP 7.2.1.3 Exercise 61
Section 7.2.1.3: Generating All Combinations Exercise 61. [ 32 ] [32] Show that all bounded compositions can be generated by changing only two of the parts at each step. 7.2.1.3 GENERATING ALL COMBINATIONS 385 Verified: no Solve time: 5m12s Solution The operators in this exercise are those introduced earlier in Section 7.2.1.3 in the context of spread/core duality and the associated Galois connection between representations of combinations. In particular, the...
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 59
Section 7.2.1.3: Generating All Combinations Exercise 59. [ M25 ] [M25] Is there a perfect solution to the 4-note piano player’s problem, in which each step moves a finger to an adjacent key? Verified: no Solve time: 3m53s Solution Let $n = s + t$ as in equation (1) of Section 7.2.1.3, and let the admissible chords be described by strictly increasing indices $n > c_t > \cdots > c_1...
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 56
Section 7.2.1.3: Generating All Combinations Exercise 56. [ M49 ] [M49] (Buck and Wiedemann, 1984.) Can all (t, t)-combinations a2t−1 . . . a1a0 be generated by repeatedly swapping a0 with some other element? Verified: no Solve time: 8m12s Setup Let $T=\binom{2t-1}{t}$. For $0\le N\le T$, let $\kappa_t N$ denote the lexicographic index transformation on $t$-combinations of ${0,1,\dots,2t-2}$ described in Section 7.2.1.3, so that $\kappa_t N - N$ measures the...
TAOCP 7.2.1.3 Exercise 55
Section 7.2.1.3: Generating All Combinations Exercise 55. ▶ [ 33 ] [33] (Frank Ruskey, 2004.) (a) Show that all (s, t)-combinations as+t−1 . . . a1a0 can be generated efficiently by doing successive rotations ajaj−1 . . . a0 ←aj−1 . . . a0aj. (b) What MMIX instructions will take (as+t−1 . . . a1a0)2 to its successor, when s+t < 64? Verified: no Solve time: 12m37s Setup Let $T=\binom{2t-1}{t}$....
TAOCP 7.2.1.3 Exercise 54
Section 7.2.1.3: Generating All Combinations Exercise 54. [ M40 ] [M40] For what values of s and t can all (s, t)-combinations be generated if we allow end-around swaps an−1 ↔a0 in addition to adjacent interchanges aj ↔aj−1? Verified: no Solve time: 20m27s Correctness The proposed solution does not address the exercise. The problem asks for a characterization of values of $s$ and $t$ for which all $(s,t)$-combinations can be...
TAOCP 7.2.1.3 Exercise 53
Section 7.2.1.3: Generating All Combinations Exercise 53. [ M46 ] [M46] (D. H. Lehmer, 1965.) Suppose the N permutations of {s0 · 0, . . . , sd · d} cannot be generated by a perfect scheme, because (N + x)/2 of them have an even number of inversions, where x ≥2. Is it possible to generate them all with a sequence of N + x −2 adjacent interchanges aδk...
TAOCP 7.2.1.3 Exercise 52
Section 7.2.1.3: Generating All Combinations Exercise 52. [ M37 ] [M37] Generalizing Theorem P, find a necessary and sufficient condition that all permutations of the multiset {s0 · 0, . . . , sd · d} can be generated by adjacent transpo- sitions ajaj−1 ↔aj−1aj. Verified: no Solve time: 7m32s Setup The Takagi function is defined for $0 \le x \le 1$ by $$ \tau(x)=\sum_{k=1}^{\infty}\int_{0}^{x} r_k(t),dt, \qquad r_k(t)=(-1)^{\lfloor 2^k t\rfloor}....
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 50
Section 7.2.1.3: Generating All Combinations Exercise 50. ▶ [ HM25 ] [HM25] Extend the formula of the previous exercise to q-multinomial coefficients n1 + · · · + nt n1, . . . , nt q . Verified: no Solve time: 6m30s Setup The Takagi function is defined for $0 \le x \le 1$ by $$ \tau(x)=\sum_{k=1}^{\infty}\int_{0}^{x} r_k(t),dt, \qquad r_k(t)=(-1)^{\lfloor 2^k t\rfloor}. $$ Each $r_k$ is constant on dyadic...
TAOCP 7.2.1.3 Exercise 49
Section 7.2.1.3: Generating All Combinations Exercise 49. [ HM23 ] [HM23] If q is a primitive mth root of unity, such as e2πi/m, show that n k q ⌊n/m⌋ ⌊k/m⌋ n mod m k mod m q . Verified: no Solve time: 14m55s Solution Let $q$ be a primitive $m$th root of unity, so $q^m=1$ and $q^j\neq 1$ for $1\le j<m$. Write $n=am+r,\quad k=bm+s,$ where $0\le r,s<m$ and...
TAOCP 7.2.1.3 Exercise 48
Section 7.2.1.3: Generating All Combinations Exercise 48. [ M21 ] [M21] Suppose α0, α1, . . . , αN−1 is any listing of the permutations of the multiset {s1 · 1, . . . , sd · d}, where αk differs from αk+1 by the interchange of two elements. Let β0, . . . , βM−1 be any revolving-door listing for (s, t)-combinations, where s = s0, t = s1+·...
TAOCP 7.2.1.3 Exercise 47
Section 7.2.1.3: Generating All Combinations Exercise 47. [ 26 ] [26] Implement the near-perfect multiset permutation method of (46) and (47). Verified: no Solve time: 13m59s Setup Let $[n]={0,1,\dots,n-1}$ and let $\binom{[n]}{t}$ denote the set of all $t$-combinations. For a family $\mathcal{A}\subseteq \binom{[n]}{t}$, define its shadow $$ \partial \mathcal{A}={B\in \binom{[n]}{t-1}\mid B\subset A \text{ for some } A\in\mathcal{A}}. $$ Theorem M (in the surrounding section) states that among all families $\mathcal{A}\subseteq...
TAOCP 7.2.1.3 Exercise 46
Section 7.2.1.3: Generating All Combinations Exercise 46. ▶ [ 33 ] [33] Construct a nonrecursive algorithm for the dual combinations bs . . . b2b1 of Chase’s sequence Cst, namely for the positions of the zeros in an−1 . . . a1a0. Verified: no Solve time: 5m51s Setup Let $n = s + t$ and let $\mathcal{A}$ be a family of $t$-combinations of ${0,1,\dots,n-1}$. The shadow $\Delta \mathcal{A}$ is the...
TAOCP 7.2.1.3 Exercise 45
Section 7.2.1.3: Generating All Combinations Exercise 45. [ 32 ] [32] Exploit endo-order and the expansions sketched in (44) to generate the combinations ct . . . c2c1 of Chase’s sequence Ct(n) with a nonrecursive procedure. Verified: no Solve time: 7m39s Setup Let $n = s + t$ and let $\mathcal{A}$ be a family of $t$-combinations of ${0,1,\dots,n-1}$. The shadow $\Delta \mathcal{A}$ is the family of all $(t-1)$-combinations that are...
TAOCP 7.2.1.3 Exercise 44
Section 7.2.1.3: Generating All Combinations Exercise 44. ▶ [ M21 ] [M21] Let Ct(n) −1 denote the sequence obtained from Ct(n) by striking out all combinations with c1 = 0, then replacing ct . . . c1 by (ct −1) . . . (c1 −1) in the combinations that remain. Show that Ct(n) −1 is near-perfect. Verified: no Solve time: 6m59s Setup Fix an integer $t \ge 1$. Let $N...
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 42
Section 7.2.1.3: Generating All Combinations Exercise 42. [ HM34 ] [HM34] Use generating functions of the form s,t gstwszt to analyze each step of Algorithm C. Verified: no Solve time: 4m08s Setup For real $x \ge t-1$, define the generalized binomial coefficients $$ \binom{x}{t} = \frac{x(x-1)\cdots(x-t+1)}{t!}, \qquad \binom{x}{t-1} = \frac{x(x-1)\cdots(x-t+2)}{(t-1)!}. $$ The function $x \mapsto \binom{x}{t}$ is strictly increasing on $[t-1,\infty)$ since $$ \frac{\binom{x+1}{t}}{\binom{x}{t}} = \frac{x+1}{x-t+1} > 1 \quad...
TAOCP 7.2.1.3 Exercise 41
Section 7.2.1.3: Generating All Combinations Exercise 41. [ M27 ] [M27] Show that there is a permutation c(0), c(1), c(2), . . . of the nonnegative integers such that the elements of Chase’s sequence Cst are obtained by complementing the least significant s + t bits of the elements c(k) for 0 ≤k < 2s+t that have weight ν(c(k)) = s. (Thus the sequence ¯c(0), . . . , ¯c(2n...
TAOCP 7.2.1.3 Exercise 40
Section 7.2.1.3: Generating All Combinations Exercise 40. [ M22 ] [M22] What is the millionth combination in Chase’s sequence Cst, when s = 12 and t = 14? Verified: no Solve time: 5m16s Setup For real $x \ge t-1$, define the generalized binomial coefficients $$ \binom{x}{t} = \frac{x(x-1)\cdots(x-t+1)}{t!}, \qquad \binom{x}{t-1} = \frac{x(x-1)\cdots(x-t+2)}{(t-1)!}. $$ The function $x \mapsto \binom{x}{t}$ is strictly increasing on $[t-1,\infty)$ since $$ \frac{\binom{x+1}{t}}{\binom{x}{t}} = \frac{x+1}{x-t+1} > 1...
TAOCP 7.2.1.3 Exercise 39
Section 7.2.1.3: Generating All Combinations Exercise 39. [ M21 ] [M21] When s = 12 and t = 14, how many combinations precede the bit string 11001001000011111101101010 in Chase’s sequence Cst? (See (41).) Verified: no Solve time: 4m03s Solution Let $\kappa_t$ be the function defined in the section, with inverse $\mu_t$ in the sense that $$ M \ge \mu_t N \quad \Longleftrightarrow \quad \kappa_t(M) \ge N, $$ for $t \ge...
TAOCP 7.2.1.3 Exercise 38
Section 7.2.1.3: Generating All Combinations Exercise 38. [ 26 ] [26] Design a genlex algorithm like Algorithm C for the reverse sequence CR st. Verified: no Solve time: 13m25s Setup An $(s,t)$-combination is represented in this section as a strictly decreasing sequence $c_t > c_{t-1} > \cdots > c_1 \ge 0,$ with $c_j \in {0,1,\dots,n-1}$ and $n=s+t$, satisfying condition (3). Algorithm L generates these sequences in lexicographic order by repeatedly...
TAOCP 7.2.1.3 Exercise 37
Section 7.2.1.3: Generating All Combinations Exercise 37. ▶ [ 27 ] [27] What algorithm results when the general genlex method (39) is used to produce (s, t)-combinations an−1 . . . a1a0 in (a) lexicographic order? (b) the revolving- door order of Algorithm R? (c) the homogeneous order of (31)? Verified: no Solve time: 5m03s Setup Let $n = s + t$. A Chase sequence $C_{st}$ is a Gray-code ordering...
TAOCP 7.2.1.3 Exercise 36
Section 7.2.1.3: Generating All Combinations Exercise 36. ▶ [ M21 ] [M21] Prove that method (39) performs the operation j ←j +1 a total of exactly s+t t −1 times as it generates all (s, t)-combinations an−1 . . . a1a0, given any genlex scheme for combinations in bitstring form. Verified: no Solve time: 5m06s Setup Let $n = s + t$. A Chase sequence $C_{st}$ is a Gray-code...
TAOCP 7.2.1.3 Exercise 35
Section 7.2.1.3: Generating All Combinations Exercise 35. [ M26 ] [M26] How many steps of Chase’s sequence Cst use an imperfect transition? Verified: no Solve time: 9m50s Setup Let $n = s + t$. A Chase sequence $C_{st}$ is a Gray-code ordering of all $(s,t)$-combinations, in which successive combinations differ by a single unit transfer of a $1$ across a contiguous block of $0$s in the binary representation, equivalently by...
TAOCP 7.2.1.3 Exercise 34
Section 7.2.1.3: Generating All Combinations Exercise 34. [ M32 ] [M32] Continuing exercise 33, explain how to find such schemes that are as near as possible to perfection, in the sense that the number of “imperfect” transitions cj ← cj ± 2 is minimized, when s and t are not too large. Verified: no Solve time: 4m57s Solution Let $\mathcal{F}(N,t)$ denote a family of $N$ distinct $t$-combinations, and let $\kappa_t(N)$...
TAOCP 7.2.1.3 Exercise 33
Section 7.2.1.3: Generating All Combinations Exercise 33. [ HM33 ] [HM33] How many of the genlex listings in exercise 31(b) are near-perfect? Verified: no Solve time: 5m07s Solution Let $\mathcal{F}(N,t)$ denote a family of $N$ distinct $t$-combinations, and let $\kappa_t(N)$ be the extremal quantity defined in Section 7.2.1.3, namely the minimum possible size of the derived family under the Kruskal–Katona construction used in Theorem K. Let $\partial \mathcal{F}$ denote the...
TAOCP 7.2.1.3 Exercise 32
Section 7.2.1.3: Generating All Combinations Exercise 32. ▶ [ M32 ] [M32] How many of the genlex listings of (s, t)-combination strings an−1 . . . a1a0 (a) have the revolving-door property? (b) are homogeneous? Verified: no Solve time: 5m15s Solution Let $\mathcal{F}(N,t)$ denote a family of $N$ distinct $t$-combinations, and let $\kappa_t(N)$ be the extremal quantity defined in Section 7.2.1.3, namely the minimum possible size of the derived family...
TAOCP 7.2.1.3 Exercise 31
Section 7.2.1.3: Generating All Combinations Exercise 31. [ M23 ] [M23] How many genlex listings of (s, t)-combinations are possible in (a) bitstring form an−1 . . . a1a0? (b) index-list form ct . . . c2c1? 7.2.1.3 GENERATING ALL COMBINATIONS 383 Verified: no Solve time: 4m54s Setup Let $\kappa_t(N)$ denote the function defined in Section 7.2.1.3 via the combinatorial representation $$ N = \binom{n_t}{t} + \binom{n_{t-1}}{t-1} + \cdots +...
TAOCP 7.2.1.3 Exercise 30
Section 7.2.1.3: Generating All Combinations Exercise 30. [ M32 ] [M32] The previous exercise defines 2s ways to generate all combinations of s 0s and t 1s, via the mapping + → 0, - → 0, and 0 → Show that each of these ways is a homogeneous genlex sequence, definable by an appropriate recurrence. Is Chase’s sequence (37) a special case of this general construction? Verified: no Solve time:...
TAOCP 7.2.1.3 Exercise 29
Section 7.2.1.3: Generating All Combinations Exercise 29. ▶ [ M28 ] [M28] (P. J. Chase.) Given a string on the symbols +, -, and 0, say that an R-block is a substring of the form -k+1 that is preceded by 0 and not followed by -; an L-block is a substring of the form +-k that is followed by 0; in both cases k ≥0. For example, the string +00++-+++-000-...
TAOCP 7.2.1.3 Exercise 28
Section 7.2.1.3: Generating All Combinations Exercise 28. [ M21 ] [M21] True or false: A listing of (s, t)-combinations an−1 . . . a1a0 in bitstring form is in genlex order if and only if the corresponding index-form listings bs . . . b2b1 (for the 0s) and ct . . . c2c1 (for the 1s) are both in genlex order. Verified: no Solve time: 4m16s Solution Let the degree-$,(t-1),$...
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 26
Section 7.2.1.3: Generating All Combinations Exercise 26. [ 26 ] [26] Do elements of the ternary reflected Gray code have properties similar to the revolving-door Gray code Γst, if we extract only the n-tuples an−1 . . . a1a0 such that (a) an−1 + · · · + a1 + a0 = t? (b) {an−1, . . . , a1, a0} = {r · 0, s · 1, t ·...
TAOCP 7.2.1.3 Exercise 25
Section 7.2.1.3: Generating All Combinations Exercise 25. [ M35 ] [M35] Let ct . . . c2c1 and c′ t . . . c′ 2c′ 1 be the Nth and N ′th combinations generated by the revolving-door method, Algorithm R. If the set C = {ct, . . . , c2, c1} has m > 0 elements not in C′ = {c′ t, . . . , c′ 2, c′...
TAOCP 7.2.1.3 Exercise 24
Section 7.2.1.3: Generating All Combinations Exercise 24. ▶ [ M25 ] [M25] (W. H. Payne, 1974.) Continuing the previous exercise, let jk be the value of j on the kth visit by Algorithm R. Show that |jk+1 −jk| ≤2, and explain how to make the algorithm loopless by exploiting this property. Verified: no Solve time: 7m30s Solution Let $\mathcal{A}$ be a family of $s$-combinations and $\mathcal{B}$ a family of $t$-combinations,...
TAOCP 7.2.1.3 Exercise 23
Section 7.2.1.3: Generating All Combinations Exercise 23. [ M23 ] [M23] Suppose we augment Algorithm R by setting j ←t + 1 in step R1, and j ←1 if R3 goes directly to R2. Find the probability distribution of j, and its average value. What does this imply about the running time of the algorithm? 382 COMBINATORIAL SEARCHING 7.2.1.3 Verified: no Solve time: 10m45s Solution Let $\mathcal{A}$ be a family...
TAOCP 7.2.1.3 Exercise 22
Section 7.2.1.3: Generating All Combinations Exercise 22. [ M23 ] [M23] What is the millionth revolving-door combination visited by Algorithm R when t is (a) 2? (b) 3? (c) 4? (d) 5? (e) 1000000? Verified: no Solve time: 8m29s Solution Let the degree-$t$ combinatorial representation (57) of $N$ be written in the form $$ N = \binom{c_t}{t} + \binom{c_{t-1}}{t-1} + \cdots + \binom{c_1}{1}, $$ where $$ s+t > c_t >...
TAOCP 7.2.1.3 Exercise 21
Section 7.2.1.3: Generating All Combinations Exercise 21. [ M22 ] [M22] (Joan E. Miller, 1971.) Prove the alternating combination law (30). Verified: no Solve time: 9m30s The exercise cannot be completed as stated because the statement of “the alternating combination law (30)” is not included in the provided material. Section 7.2.1.3 references equation (30), and the proof depends on its exact algebraic or combinatorial formulation. Without that formula, there is...
TAOCP 7.2.1.3 Exercise 20
Section 7.2.1.3: Generating All Combinations Exercise 20. [ M20 ] [M20] Devise generating functions g and h such that Algorithm F finds exactly [zN] g(z) feasible combinations and sets t ←t + 1 exactly [zN] h(z) times. Verified: no Solve time: 7m28s Solution Let $G$ be a graph with $m = 10^6$ edges, and let $K_t(G)$ denote the number of $t$-cliques in $G$. The task is to maximize $K_t(G)$ under...
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 17
Section 7.2.1.3: Generating All Combinations Exercise 17. [ HM25 ] [HM25] Given N and t, what is a good way to compute the combinatorial repre- sentation (20)? Verified: no Solve time: 6m52s 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$ whose binary expansion contains exactly $t$ ones, i.e., $$...
TAOCP 7.2.1.3 Exercise 16
Section 7.2.1.3: Generating All Combinations Exercise 16. [ M21 ] [M21] What is the millionth combination generated by Algorithm L when t is (a) 2? (b) 3? (c) 4? (d) 5? (e) 1000000? Verified: no Solve time: 18m06s Solution Algorithm L lists the $t$-combinations $c_t \dots c_2 c_1$ of ${0,1,\dots,n-1}$ in lexicographic order, starting from $c_j = j-1$ for $1 \le j \le t$. The $k$-th combination is therefore the...
TAOCP 7.2.1.3 Exercise 15
Section 7.2.1.3: Generating All Combinations Exercise 15. [ M22 ] [M22] Use the fact that dual combinations bs . . . b2b1 occur in reverse lexico- graphic order to prove that the sum bs s · · · + b2 2 b1 1 has a simple relation to the sum ct t · · · + c2 2 c1 1 . Verified: no Solve...
TAOCP 7.2.1.3 Exercise 14
Section 7.2.1.3: Generating All Combinations Exercise 14. [ 26 ] [26] When the binary strings an−1 . . . a1a0 of (s, t)-combinations are generated in lexicographic order, we sometimes need to change 2 min(s, t) bits to get from one combination to the next. For example, 011100 is followed by 100011 in Table 1. Therefore we apparently cannot hope to generate all combinations with a loopless algorithm unless we...
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 12
Section 7.2.1.3: Generating All Combinations Exercise 12. [ HM32 ] [HM32] A set V of n-bit vectors that is closed under addition modulo 2 is called a binary vector space. a) Prove that every such V contains 2t elements, for some integer t, and can be represented as the set {x1α1 ⊕· · · ⊕xtαt | 0 ≤x1, . . . , xt ≤1} where the vectors α1, . ....
TAOCP 7.2.1.3 Exercise 111
Section 7.2.1.3: Generating All Combinations Exercise 111. ▶ [ M26 ] [M26] (P. Erdős, C. Ko, and R. Rado.) Suppose A is a set of r-combinations of an n-set, with α ∩β ̸= ∅whenever α, β ∈A. Show that |A| ≤ n−1 r−1 , if r ≤n/2. Hint: Consider ∂n−2rB, where B is the set of complements of A. 390 COMBINATORIAL SEARCHING 7.2.1.4 7.2.1.4. Generating all partitions. Richard Stanley’s...
TAOCP 7.2.1.3 Exercise 110
Section 7.2.1.3: Generating All Combinations Exercise 110. ▶ [ 26 ] [26] Cribbage is a game played with 52 cards, where each card has a suit (♣, ♢, ♡, or ♠) and a face value (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, or K). Its players must become adept at computing the score of a 5-card combination C = {c1, c2, c3, c4, c5}, where...
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 109
Section 7.2.1.3: Generating All Combinations Exercise 109. [ M31 ] [M31] Find universal cycles of 3-multicombinations for {0, 1, . . . , n −1} when n mod 3 ̸= 0 (namely for combinations d1d2d3 with repetitions permitted). For exam- ple, (00012241112330222344133340024440113) is such a cycle when n = 5. Verified: no Solve time: 19m Solution Let the canonical bases be represented in the form $(\alpha_1,\dots,\alpha_t)$ as in exercise 12,...
TAOCP 7.2.1.3 Exercise 108
Section 7.2.1.3: Generating All Combinations Exercise 108. [ M31 ] [M31] Find universal cycles of 3-combinations for the sets {0, . . . , n −1} when n mod 3 ̸= 0. Verified: no Solve time: 4m02s Solution Let $G$ be the multigraph whose vertices are ${0,1,2,3,4,5,6}$ and whose edges are the $28$ dominoes of the double-six set, namely one edge between $i$ and $j$ for each $0 \le i...
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 106
Section 7.2.1.3: Generating All Combinations Exercise 106. [ M21 ] [M21] (L. Poinsot, 1809.) Find a “nice” universal cycle of 2-combinations for {0, 1, . . . , 2m}. Hint: Consider the differences of consecutive elements, mod (2m + 1). Verified: no Solve time: 4m43s Setup Let $G_{s,t}$ denote the graph whose vertices are all subcubes of length $s+t$ having $s$ digits in ${0,1}$ and $t$ asterisks, with edges given...
TAOCP 7.2.1.3 Exercise 105
Section 7.2.1.3: Generating All Combinations Exercise 105. [ M20 ] [M20] A universal cycle of t-combinations for {0, 1, . . . , n −1} is a cycle of n t numbers whose blocks of t consecutive elements run through every t-combination {c1, . . . , ct}. For example, (02145061320516243152630425364103546) is a universal cycle when t = 3 and n = 7. Prove that no such cycle is...
TAOCP 7.2.1.3 Exercise 104
Section 7.2.1.3: Generating All Combinations Exercise 104. [ M41 ] [M41] The shadow of a binary string a1 . . . an is obtained by deleting one of its bits. For example, ∂110010010 = {10010010, 11010010, 11000010, 11001000, 11001010, 11001001}. Find a set PNn such that, if A is any set of N binary strings a1 . . . an, |∂A| ≥|PNn|. 7.2.1.3 GENERATING ALL COMBINATIONS 389 Verified: no Solve...
TAOCP 7.2.1.3 Exercise 103
Section 7.2.1.3: Generating All Combinations Exercise 103. ▶ [ M38 ] [M38] The shadow of a subcube a1 . . . an, where each aj is either 0 or 1 or ∗, is obtained by replacing some ∗by 0 or 1. For example, ∂0∗11∗0 = {0011∗0, 0111∗0, 0∗1100, 0∗1110}. Find a set PNst such that, if A is any set of N subcubes a1 . . . an having s...
TAOCP 7.2.1.3 Exercise 102
Section 7.2.1.3: Generating All Combinations Exercise 102. [ HM35 ] [HM35] (F. S. Macaulay, 1927.) A polynomial ideal I in the variables {x1 . . . , xs} is a set of polynomials closed under the operations of addition, multiplication by a constant, and multiplication by any of the variables. It is called homogeneous if it consists of all linear combinations of a set of homogeneous polynomials, namely of polynomials...
TAOCP 7.2.1.3 Exercise 101
Section 7.2.1.3: Generating All Combinations Exercise 101. [ M25 ] [M25] If f(x1, . . . , xn) is a Boolean formula, let F(p) be the probability that f(x1, . . . , xn) = 1 when each variable xj independently is 1 with probability p. a) Calculate G(p) and H(p) for the Boolean formulas g(w, x, y, z) = wxz∨wyz∨xy¯z, h(w, x, y, z) = ¯wyz ∨xyz. b) Show...
TAOCP 7.2.1.3 Exercise 100
Section 7.2.1.3: Generating All Combinations Exercise 100. ▶ [ M30 ] [M30] (Clements and Lindström.) Let A be a “simplicial multicomplex,” a set of submultisets of the multiset U in Corollary C with the property that ∂A ⊆A. How large can the total weight νA = {|α| | α ∈A} be when |A| = N? Verified: no Solve time: 4m51s Setup Let $(a_{ij})$ be an $m\times n$ contingency table with...
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 9
Section 7.2.1.3: Generating All Combinations Exercise 9. [ M26 ] [M26] When all (s, t)-combinations an−1 . . . a1a0 are listed in lexicographic order, let 2Ast be the total number of bit changes between adjacent strings. For example, A33 = 25 because there are respectively 2 + 2 + 2 + 4 + 2 + 2 + 4 + 2 + 2 + 6 + 2 + 2 +...
TAOCP 7.2.1.3 Exercise 8
Section 7.2.1.3: Generating All Combinations Exercise 8. [ M23 ] [M23] Design an algorithm that generates all (s, t)-combinations an−1 . . . a1a0 lexicographically in bitstring form. The total running time should be O( n t ), assuming that st > 0. Verified: no Solve time: 4m55s Solution An $(s,t)$-combination in dual form is a strictly decreasing sequence $b_s > b_{s-1} > \cdots > b_1 \ge 0,$ where ${b_1,\dots,b_s}$...
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 6
Section 7.2.1.3: Generating All Combinations Exercise 6. [ M22 ] [M22] How often is each step of Algorithm T performed? 380 COMBINATORIAL SEARCHING 7.2.1.3 Verified: no Solve time: 4m44s Setup Let $(a_{ij})$ be an $m\times n$ contingency table with row sums $r_i=\sum_{j=1}^n a_{ij}, \quad 1\le i\le m,$ and column sums $c_j=\sum_{i=1}^m a_{ij}, \quad 1\le j\le n,$ with $\sum_{i=1}^m r_i=\sum_{j=1}^n c_j$. Entries are ordered either row-wise as $(a_{11},a_{12},\ldots,a_{1n},a_{21},\ldots,a_{mn}),$ or column-wise as...
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.3 Exercise 1
Section 7.2.1.3: Generating All Combinations Exercise 1. [ M23 ] [M23] Explain why Golomb’s rule (8) makes all sets {c1, . . . , ct} ⊆{0, . . . , n −1} correspond uniquely to multisets {e1, . . . , et} ⊆{∞· 0, . . . , ∞· n −t}. Verified: no Solve time: 7m27s Solution Let $r_s,\dots,r_0$ satisfy $$ t = r_s + \cdots + r_1 + r_0,\qquad...
TAOCP 7.2.1.2 Exercise 99
Section 7.2.1.2: Generating All Permutations Exercise 99. [ M30 ] [M30] Given n ≥3, show that all derangements of {1, . . . , n} can be generated by making at most two transpositions between visits. Verified: no Solve time: 6m40s Solution Let a 4-note chord be a 4-combination $c_4c_3c_2c_1$ with $n > c_4 > c_3 > c_2 > c_1 \ge 0.$ A single “adjacent-key move” replaces exactly one $c_j$...
TAOCP 7.2.1.2 Exercise 98
Section 7.2.1.2: Generating All Permutations Exercise 98. [ HM23 ] [HM23] Analyze the asymptotic running time of the method in the previous exercise. Verified: no Solve time: 5m50s Solution Let a 4-note chord be a 4-combination $c_4c_3c_2c_1$ with $n > c_4 > c_3 > c_2 > c_1 \ge 0.$ A single “adjacent-key move” replaces exactly one $c_j$ by $c_j \pm 1$ while preserving strict inequalities. Write the standard gap variables...
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 93
Section 7.2.1.2: Generating All Permutations Exercise 93. [ 35 ] [35] Prove that all topological sorts can be generated in such a way that only one or two adjacent transpositions are made at each step. (The example 1 ≺2, 3 ≺4 shows that a single transposition per step cannot always be achieved, even if we allow nonadjacent swaps, because only two of the six relevant permutations are odd.) Verified: no...
TAOCP 7.2.1.2 Exercise 92
Section 7.2.1.2: Generating All Permutations Exercise 92. [ M18 ] [M18] How many permutations is Algorithm V likely to visit, in a “random” case? Let Pn be the number of partial orderings on {1, . . . , n}, namely the number of relations that are reflexive, antisymmetric, and transitive. Let Qn be the number of such relations with the additional property that j < k whenever j ≺k. Express...
TAOCP 7.2.1.2 Exercise 91
Section 7.2.1.2: Generating All Permutations Exercise 91. [ HM21 ] [HM21] Analyze the running time of Algorithm V when it is used with the relations (49) to find perfect matchings. Verified: no Solve time: 5m14s Solution Let the set of elements be ${1,2,\dots,2n}$ and let the relations (49) specify a perfect matching, so the elements are partitioned into $n$ disjoint pairs ${x_i,y_i}$, each pair inducing a constraint of comparability in...
TAOCP 7.2.1.2 Exercise 90
Section 7.2.1.2: Generating All Permutations Exercise 90. [ M21 ] [M21] Algorithm V can be used to produce all permutations that are h-ordered for all h in a given set, namely all a′ 1 . . . a′ n such that a′ j < a′ j+h for 1 ≤j ≤n −h (see Section 5.2.1). Analyze the running time of Algorithm V when it generates all permutations that are both 2-ordered...
TAOCP 7.2.1.2 Exercise 89
Section 7.2.1.2: Generating All Permutations Exercise 89. ▶ [ M30 ] [M30] Consider the numbers t0, t1, . . . , tn defined before (51). Clearly t0 = t1 = 1. a) Say that index j is “trivial” if tj = tj−1. For example, 9 is trivial with respect to the Young tableau relations (48). Explain how to modify Algorithm V so that the variable k takes on only nontrivial...
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 82
Section 7.2.1.2: Generating All Permutations Exercise 82. [ M21 ] [M21] Analyze the running time of the program in exercise 81. Verified: no Solve time: 5m17s 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 a_0 a_j, $$ with all other...
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 78
Section 7.2.1.2: Generating All Permutations Exercise 78. [ M23 ] [M23] Analyze the running time of the program in exercise 77, generalizing it so that the inner loop does r! visits (with a0 . . . ar−1 in global registers). Verified: yes Solve time: 2m13s Let the program of Exercise 77 implement Heap’s method for generating all permutations of the $r$ elements stored in the global registers $a_0,\ldots,a_{r-1}$. The execution...
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 76
Section 7.2.1.2: Generating All Permutations Exercise 76. [ M31 ] [M31] The cells numbered 0, 1, . . . , 63 in Fig. 45 illustrate a northeasterly knight’s tour on an 8 × 8 torus: If k appears in cell (xk, yk), then (xk+1, yk+1) ≡(xk + 2, yk + 1) or (xk+1, yk+2), modulo 8, and (x64, y64) = (x0, y0). How many such tours are possible on an...
TAOCP 7.2.1.2 Exercise 75
Section 7.2.1.2: Generating All Permutations Exercise 75. [ M26 ] [M26] The directed torus C⃗m×C⃗n has mn vertices (x, y) for 0 ≤x < m, 0 ≤y < n, and arcs (x, y)−−→(x, y)α = ((x + 1) mod m, y), (x, y)−−→(x, y)β = (x, (y + 1) mod n). Prove that, if m > 1 and n > 1, the number of Hamiltonian cycles of this digraph is...
TAOCP 7.2.1.2 Exercise 74
Section 7.2.1.2: Generating All Permutations Exercise 74. [ M30 ] [M30] (R. A. Rankin.) Assuming that αβ = βα in Theorem R, prove that a Hamiltonian cycle exists in the Cayley graph for G if and only if there is a number k such that 0 ≤k ≤g/c and t + k ⊥c, where βg/c = γt, γ = αβ−. Hint: Represent elements of the group in the form βjγk....
TAOCP 7.2.1.2 Exercise 73
Section 7.2.1.2: Generating All Permutations Exercise 73. ▶ [ M30 ] [M30] Let α, β, and σ be permutations of a set X, where X = A ∪B. Assume that xσ = xα when x ∈A and xσ = xβ when x ∈B, and that the order of αβ−is odd. a) Prove that all three permutations α, β, σ have the same sign; that is, they are all even or...
TAOCP 7.2.1.2 Exercise 72
Section 7.2.1.2: Generating All Permutations Exercise 72. [ M21 ] [M21] Given a Cayley graph with generators (α1, . . . , αk), assume that each αj takes x → y. (For example, both σ and τ in exercise 71 take 1 → 2.) Prove that any Hamiltonian path starting at 12 . . . n in G must end at a permutation that takes y → x. Verified: no...
TAOCP 7.2.1.2 Exercise 71
Section 7.2.1.2: Generating All Permutations Exercise 71. [ 48 ] [48] Does the Cayley graph with generators σ = (1 2 . . . n) and τ = (1 2) have a Hamiltonian cycle whenever n ≥3 is odd? Verified: no Solve time: 6m02s Setup Let the multiset be ${s_0 \cdot 0,; s_1 \cdot 1,; \ldots,; s_d \cdot d}, \qquad s_0 + s_1 + \cdots + s_d = n.$ Let...
TAOCP 7.2.1.2 Exercise 70
Section 7.2.1.2: Generating All Permutations Exercise 70. ▶ [ M33 ] [M33] The two 12-cycles (41) can be regarded as σ–τ cycles for the twelve per- mutations of {1, 1, 3, 4}: 1134 →1341 →3411 →4311 →3114 →1143 →1431 →4131 →1314 →3141 →1413 →4113 →1134. Replacing {1, 1} by {1, 2} yields disjoint cycles, and we obtained a Hamiltonian path by jumping from one to the other. Can a σ–τ...
TAOCP 7.2.1.2 Exercise 69
Section 7.2.1.2: Generating All Permutations Exercise 69. ▶ [ 28 ] [28] If n ≥4, the following algorithm generates all permutations A1A2A3 . . . An of {1, 2, 3, . . . , n} using only three transformations, ρ = (1 2)(3 4)(5 6) . . . , σ = (2 3)(4 5)(6 7) . . . , τ = (3 4)(5 6)(7 8) . . . , never...
TAOCP 7.2.1.2 Exercise 68
Section 7.2.1.2: Generating All Permutations Exercise 68. [ M30 ] [M30] (V. L. Kompel’makher and V. A. Liskovets, 1975.) Let G be the Cayley graph for all permutations of {1, . . . , n}, with generators (α1, . . . , αk) where each αj is a transposition (uj vj); also let A be the graph with vertices {1, . . . , n} and edges uj −−−vj for...
TAOCP 7.2.1.2 Exercise 67
Section 7.2.1.2: Generating All Permutations Exercise 67. [ 26 ] [26] Continuing the previous exercise, find a first-element-swap Gray cycle for n = 5 in which each star transposition (1 j) occurs 30 times, for 2 ≤j ≤5. Verified: no Solve time: 6m59s Solution Each vertex is a permutation of the multiset ${0,0,0,1,1,1}$, hence each vertex is uniquely represented by a strictly increasing triple $c_3c_2c_1 \quad\text{with}\quad 5 \ge c_3 >...
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 65
Section 7.2.1.2: Generating All Permutations Exercise 65. [ M25 ] [M25] For which integers N is there a Gray path through the N lexicographically smallest permutations of {1, . . . , n}? (Exercise 7.2.1.1–26 solves the analogous problem for binary n-tuples.) Verified: no Solve time: 4m22s Solution Let $q$ be a primitive $m$th root of unity and let $$ N = n_1 + \cdots + n_t. $$ Write each...
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 63
Section 7.2.1.2: Generating All Permutations Exercise 63. [ M25 ] [M25] Estimate the total number of Gray cycles for permutations of {1, 2, 3, 4, 5}. Verified: no Solve time: 11m29s Solution Let $q$ be a primitive $m$th root of unity. For each $i$ with $1 \le i \le t$, write $$ n_i = m a_i + b_i, \qquad 0 \le b_i < m, $$ and set $$ N =...
TAOCP 7.2.1.2 Exercise 62
Section 7.2.1.2: Generating All Permutations Exercise 62. ▶ [ M23 ] [M23] What permutations can be reached as the final element of a Gray code that starts at 12 . . . n? Verified: no Solve time: 5m Solution Let $q$ be a primitive $m$th root of unity, so $q^m=1$ and $1+q+\cdots+q^{m-1}=0$. Define the Gaussian binomial coefficient $$ \binom{n}{k}_q=\frac{(1-q^n)(1-q^{n-1})\cdots(1-q^{n-k+1})}{(1-q)(1-q^2)\cdots(1-q^k)}. $$ Write integers in base $m$ as $$ n = m...
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 59
Section 7.2.1.2: Generating All Permutations Exercise 59. [ M20 ] [M20] Some authors define the arcs of a Cayley graph as running from π to παj instead of from π to αjπ. Are the two definitions essentially different? Verified: no Solve time: 4m40s Solution Let $\beta_0,\ldots,\beta_{M-1}$ be a revolving-door listing of all $(s,t)$-combinations of ${0,1,\ldots,s+t-1}$, where $M=\binom{s+t}{t}$, and consecutive terms differ by a single adjacent exchange in the corresponding binary...
TAOCP 7.2.1.2 Exercise 58
Section 7.2.1.2: Generating All Permutations Exercise 58. [ M21 ] [M21] True or false: If Algorithm E begins with a0 . . . an−1 = x1 . . . xn then the final permutation visited begins with a0 = xn. Verified: no Solve time: 2m51s Solution Algorithm E generates all permutations by a sequence of adjacent interchanges and returns to the starting permutation, as indicated by its structure involving steps...
TAOCP 7.2.1.2 Exercise 57
Section 7.2.1.2: Generating All Permutations Exercise 57. [ HM22 ] [HM22] What is the average number of interchanges made in step E5? Verified: no Solve time: 1m46s Solution Step E5 performs the single operation $$ a_{j-c_j+s} \leftrightarrow a_{j-q+s}. $$ This is one adjacent interchange of two elements of the permutation array $a_1,\dots,a_n$. No other exchanges occur inside step E5. The remaining operations in E5 update $c_j$ and return control to...
TAOCP 7.2.1.2 Exercise 56
Section 7.2.1.2: Generating All Permutations Exercise 56. [ M22 ] [M22] Prove that Algorithm E remains valid if step E5 is replaced by E5′. [Transpose pairs.] If k > 2, interchange bj+1 ↔bj for j = k −2, k −4, . . . , (2 or 1). Return to E2. Verified: no Solve time: 3m17s The flaw in the previous solution is that it never connects the modified step $E5'$...
TAOCP 7.2.1.2 Exercise 55
Section 7.2.1.2: Generating All Permutations Exercise 55. [ M27 ] [M27] Consider the factorial ruler function ρ!(m) = max{k | m mod k! = 0}. 7.2.1.2 GENERATING ALL PERMUTATIONS 349 Let σk and τk be permutations of the nonnegative integers such that σj τk = τkσj whenever j ≤k. Let α0 and β0 be the identity permutation, and for m > 0 define αm = β− m−1τρ!(m)βm−1αm−1, βm = σρ!(m)βm−1....
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 53
Section 7.2.1.2: Generating All Permutations Exercise 53. ▶ [ M26 ] [M26] (F. M. Ives.) Construct a Sims table for which Algorithm H will generate all permutations by making only n! + O((n −2)!) transpositions. Verified: no Solve time: 1m42s Setup Let Algorithm H act on a Sims table ${S_k}_{1 \le k \le n}$ as in Section 7.2.1.2, where each set $S_k$ encodes allowable transitions at level $k$ in the...
TAOCP 7.2.1.2 Exercise 52
Section 7.2.1.2: Generating All Permutations Exercise 52. ▶ [ M22 ] [M22] What permutations τ(k, j) and ω(k) arise when Algorithm H is used with the Sims table (36)? Compare the resulting generator with Algorithm P. Verified: no Solve time: 4m49s Solution Let the Sims table (36) be the standard Sims table for the symmetric group on $n$ symbols, in which the basic generators are the adjacent transpositions acting on...
TAOCP 7.2.1.2 Exercise 51
Section 7.2.1.2: Generating All Permutations Exercise 51. [ M16 ] [M16] True or false: If the sets Sk = {σ(k, 0), . . . , σ(k, k)} form a Sims table for the group of all permutations, so also do the sets S− k = {σ(k, 0)−, . . . , σ(k, k)−}. Verified: yes Solve time: 5m01s The statement is false in general . The error in the proposed...
TAOCP 7.2.1.2 Exercise 50
Section 7.2.1.2: Generating All Permutations Exercise 50. [ M15 ] [M15] Explain why the dual formula (32) follows from (13). Verified: yes Solve time: 2m44s We restart the argument from the actual structure of (13), tracking how each update clause transforms under the duality map, and we verify case by case that the transformed rules are exactly those of (32). Let (13) describe the reflected mixed-radix Gray traversal of inversion...
TAOCP 7.2.1.2 Exercise 49
Section 7.2.1.2: Generating All Permutations Exercise 49. ▶ [ 28 ] [28] The text’s suggested method for solving additive alphametics with Algo- rithm X essentially chooses digits from right to left; in other words, it assigns tentative values to the least significant digits before considering digits that correspond to higher powers of 10. Explore an alternative approach that chooses digits from left to right. For example, such a method will...
TAOCP 7.2.1.2 Exercise 48
Section 7.2.1.2: Generating All Permutations Exercise 48. ▶ [ M25 ] [M25] Compare the running times of Algorithm X and Algorithm L, in the case when the tests t1(a1), t2(a1, a2), . . . , tn(a1, a2, . . . , an) always are true. Verified: no Solve time: 2m22s Solution Algorithm $X$ and Algorithm $L$ both enumerate all $n!$ permutations of $a_1 a_2 \dots a_n$. The difference lies in...
TAOCP 7.2.1.2 Exercise 47
Section 7.2.1.2: Generating All Permutations Exercise 47. ▶ [ M21 ] [M21] Express the number of times that each step of Algorithm X is performed in terms of the numbers N0, N1, . . . , Nn, where Nk is the number of prefixes a1 . . . ak that satisfy tj(a1, . . . , aj) for 1 ≤j ≤k. Verified: no Solve time: 4m21s Solution Let $C_t(n)$ denote...
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 43
Section 7.2.1.2: Generating All Permutations Exercise 43. [ M24 ] [M24] Construct a Sims table for all permutations in which every σ(k, k), ω(k), and τ(k, j)ω(k −1)−for 1 ≤j ≤k is a cycle of length ≤3. Verified: no Solve time: 6m21s Setup Let $n = s + t$. Chase’s sequence $C_{st}$ consists of all $(s,t)$-combinations, represented as bitstrings of length $n$ with exactly $s$ zeros and $t$ ones, ordered...
TAOCP 7.2.1.2 Exercise 42
Section 7.2.1.2: Generating All Permutations Exercise 42. [ M20 ] [M20] Construct a Sims table for all permutations in which every σ(k, j) and every τ(k, j) for 1 ≤j ≤k is a cycle of length ≤3. Verified: no Solve time: 6m22s Setup Let $n = s + t$. Chase’s sequence $C_{st}$ consists of all $(s,t)$-combinations, represented as bitstrings of length $n$ with exactly $s$ zeros and $t$ ones, ordered...
TAOCP 7.2.1.2 Exercise 41
Section 7.2.1.2: Generating All Permutations Exercise 41. ▶ [ M33 ] [M33] Design an algorithm that generates all r-variations of {0, 1, . . . , n −1} by interchanging just two elements when going from one variation to the next. (See exercise 9.) Hint: Generalize Heap’s method (27), obtaining the results in positions an−r . . . an−1 of an array a0 . . . an−1. For example, one...
TAOCP 7.2.1.2 Exercise 40
Section 7.2.1.2: Generating All Permutations Exercise 40. [ M23 ] [M23] Show that Heap’s method (27) corresponds to a valid Sims table. Verified: no Solve time: 1m41s Solution Heap’s method (27) constructs permutations of $n$ objects by a recursive decomposition in which a size-$n$ problem is reduced to a size-$(n-1)$ problem, and each return from recursion is accompanied by a single transposition that moves element $n$ through all positions in...
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 38
Section 7.2.1.2: Generating All Permutations Exercise 38. [ HM21 ] [HM21] Prove that the average number of transpositions per permutation when using Ord-Smith’s algorithm (26) is approximately sinh 1 ≈1.175. 348 COMBINATORIAL SEARCHING 7.2.1.2 Verified: no Solve time: 1m30s Solution Let $T$ denote the total number of transpositions performed by Ord-Smith’s algorithm (26) in generating a full cycle of $n!$ permutations, and let $X$ denote the number of transpositions per...
TAOCP 7.2.1.2 Exercise 37
Section 7.2.1.2: Generating All Permutations Exercise 37. ▶ [ HM22 ] [HM22] How many Sims tables can be used with Algorithms G or H? Estimate the logarithm of this number as n →∞. Verified: no Solve time: 1m41s Solution A Sims table used by Algorithms G or H encodes, for each level of a stabilizer chain for $S_n$, a full set of coset representatives for the successive point stabilizers. For...
TAOCP 7.2.1.2 Exercise 36
Section 7.2.1.2: Generating All Permutations Exercise 36. [ M23 ] [M23] Find a Sims table for the group of all automorphisms of the 4×4 tic-tac-toe board 0 1 2 3 4 5 6 7 8 9 a b c d e f , namely the permutations that take lines into lines, where a “line” is a set of four elements that belong to a row, column, or diagonal. Verified: no...
TAOCP 7.2.1.2 Exercise 35
Section 7.2.1.2: Generating All Permutations Exercise 35. ▶ [ M20 ] [M20] The automorphisms of a 4-cube have many different Sims tables, only one of which is shown in (14). How many different Sims tables are possible for that group, when the vertices are numbered as in (12)? Verified: no Solve time: 5m02s Working
TAOCP 7.2.1.2 Exercise 34
Section 7.2.1.2: Generating All Permutations Exercise 34. [ M26 ] [M26] Make the equation EVEN + ODD + PRIME = x doubly true when (a) x is a perfect 5th power; (b) x is a perfect 7th power. Verified: no Solve time: 5m35s The reviewer’s objections are all correct: the previous response never produced a single fully consistent alphametic, and in part (b) the proposed assignment is structurally impossible. We...
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 32
Section 7.2.1.2: Generating All Permutations Exercise 32. [ M25 ] [M25] (H. E. Dudeney, 1901.) Find all ways to represent 100 by inserting a plus sign and a slash into a permutation of the digits {1, . . . , 9}. For example, 100 = 91 + 5742/638. The plus sign should precede the slash. Verified: no Solve time: 3m15s We correct the proof by replacing all heuristic exclusions with...
TAOCP 7.2.1.2 Exercise 31
Section 7.2.1.2: Generating All Permutations Exercise 31. [ M22 ] [M22] (Nob Yoshigahara.) (a) What is the unique solution to A/BC+D/EF+G/HI = 1, when {A, . . . , I} = {1, . . . , 9}? (b) Similarly, make AB mod 2 = 0, ABC mod 3 = 0, etc. Verified: no Solve time: 5m10s (a) We solve $$ \frac{A}{10B+C}+\frac{D}{10E+F}+\frac{G}{10H+I}=1, \qquad {A,\dots,I}={1,\dots,9}. $$ Step 1: Identify the forced large...
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 29
Section 7.2.1.2: Generating All Permutations Exercise 29. ▶ [ M25 ] [M25] Continuing the previous exercise, find all equations of the form n1 + · · · + nt = n′ 1 + · · · + n′ t′ that are both mathematically and alphametically true in English, when {n1, . . . , nt, n′ 1, . . . , n′ t′} are distinct positive integers less than 20....
TAOCP 7.2.1.2 Exercise 28
Section 7.2.1.2: Generating All Permutations Exercise 28. [ M25 ] [M25] A partition of the integer n is an expression of the form n = n1+· · ·+nt with n1 ≥· · · ≥nt > 0. Such a partition is called doubly true if α(n) = α(n1)+· · ·+α(nt) is also a pure alphametic, where α(n) is the “name” of n in some language. Doubly true partitions were introduced by...
TAOCP 7.2.1.2 Exercise 27
Section 7.2.1.2: Generating All Permutations Exercise 27. [ 30 ] [30] Construct pure additive alphametics in which all words have five letters. 7.2.1.2 GENERATING ALL PERMUTATIONS 347 Verified: no Solve time: 5m12s Setup An additive alphametic in the sense of Section 7.2.1.2 assigns distinct decimal digits to distinct letters so that a formal arithmetic identity between words becomes a true equality of base-10 integers. A pure additive alphametic with five-letter...
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 25
Section 7.2.1.2: Generating All Permutations Exercise 25. ▶ [ M21 ] [M21] Devise a fast way to compute min(a · s) and max(a · s) over all valid permutations a1 . . . a10 of {0, . . . , 9}, given the signature vector s = (s1, . . . , s10) and the first-letter set F of an alphametic problem. (Such a procedure makes it possible to rule...
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 23
Section 7.2.1.2: Generating All Permutations Exercise 23. [ M20 ] [M20] True or false: A pure alphametic cannot have two identical signatures sj = sk ̸= 0 when j ̸= k. Verified: no Solve time: 4m17s The previous solution failed because it implicitly treated an “alphametic identity” as a manipulable symbolic cancellation pattern, rather than a polynomial identity that must hold for all digit assignments. Once that distinction is enforced,...
TAOCP 7.2.1.2 Exercise 22
Section 7.2.1.2: Generating All Permutations Exercise 22. [ M15 ] [M15] True or false: If an alphametic has a solution in radix b, it has a solution in radix b + 1. Verified: no Solve time: 4m34s The previous solution fails because it tries to separate bases via carry behavior, but an alphametic solution is not defined in terms of carries. It is defined purely as a digit assignment satisfying...
TAOCP 7.2.1.2 Exercise 21
Section 7.2.1.2: Generating All Permutations Exercise 21. [ M21 ] [M21] (E. P. McCravy, 1971.) How many solutions does the alphametic (6) have in radix b? Verified: no Solve time: 4m30s The previous solution fails at the point where it imports specific base-10 digits. The correct approach is to keep all quantities symbolic in radix $b$ and count the remaining degrees of freedom explicitly. 1. Standard reductions (valid for all...
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 15
Section 7.2.1.2: Generating All Permutations Exercise 15. [ M23 ] [M23] (Selmer Johnson, 1963.) Show that the offset variable s never exceeds 2 in Algorithm P. Verified: no Solve time: 6m04s Solution Let Algorithm P be executed on a sequence $a_1a_2\cdots a_n$ of distinct elements, with auxiliary arrays $c_1\cdots c_n$ and $o_1\cdots o_n$, and variables $j$ and $s$ as defined in steps P1–P7. The variable $s$ is initialized to $0$...
TAOCP 7.2.1.2 Exercise 14
Section 7.2.1.2: Generating All Permutations Exercise 14. [ M22 ] [M22] True or false: If a1a2 . . . an is initially x1x2 . . . xn in Algorithm P, we always have aj−cj+s = xj at the beginning of step P5. 346 COMBINATORIAL SEARCHING 7.2.1.2 Verified: no Solve time: 7m28s Solution The statement claims an invariant relation in Algorithm P: at the beginning of step P5, $a_{j-c_j+s} = x_j$...
TAOCP 7.2.1.2 Exercise 13
Section 7.2.1.2: Generating All Permutations Exercise 13. [ M21 ] [M21] (Martin Gardner, 1974.) True or false: If a1a2 . . . an is initially 12 . . . n, Algorithm P begins by visiting all n!/2 permutations in which 1 precedes 2; then the next permutation is n . . . 21. Verified: no Solve time: 6m06s Setup Let Algorithm R denote the revolving-door generation of $t$-combinations of ${0,1,\dots,n-1}$...
TAOCP 7.2.1.2 Exercise 12
Section 7.2.1.2: Generating All Permutations Exercise 12. ▶ [ M23 ] [M23] What is the 1000000th permutation visited by (a) Algorithm L, (b) Algo- rithm P, (c) Algorithm C, if {a1, . . . , an} = {0, . . . , 9}? Hint: In mixed-radix notation we have 1000000 = [ 2, 10, 6, 9, 6, 8, 2, 7, 5, 6, 1, 5, 2, 4, 2, 3, 0, 2,...
TAOCP 7.2.1.2 Exercise 113
Section 7.2.1.2: Generating All Permutations Exercise 113. [ HM43 ] [HM43] Exactly how many universal cycles exist, for permutations of ≤9 objects? 7.2.1.3. Generating all combinations. Combinatorial mathematics is often described as “the study of permutations, combinations, etc.,” so we turn our attention now to combinations. A combination of n things, taken t at a time, often called simply a t-combination of n things, is a way to select a...
TAOCP 7.2.1.2 Exercise 112
Section 7.2.1.2: Generating All Permutations Exercise 112. ▶ [ M30 ] [M30] (A. Williams, 2007.) Continuing exercise 111, construct explicit cycles: a) Show that a universal cycle of permutations is equivalent to a Hamiltonian cycle on the Cayley graph with two generators ρ = (1 2 . . . n−1) and σ = (1 2 . . . n). b) Prove that any Hamiltonian path in that graph is actually...
TAOCP 7.2.1.2 Exercise 111
Section 7.2.1.2: Generating All Permutations Exercise 111. ▶ [ M25 ] [M25] Several permutation-oriented analogs of de Bruijn cycles have been pro- posed. The simplest and nicest of these is the notion of a universal cycle of permuta- tions, introduced by B. W. Jackson in Discrete Math. 117 (1993), 141–150, namely a cycle of n! digits such that each permutation of {1, . . . , n} occurs exactly once...
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 11
Section 7.2.1.2: Generating All Permutations Exercise 11. [ M22 ] [M22] How many times is each step of Algorithm P performed? (Assume that n ≥2.) Verified: no Solve time: 5m37s We rebuild the analysis from the actual control structure of Algorithm P (plain changes, Johnson–Trotter) rather than any external digit model. Let $A[1..n]$ be the permutation and each element $i$ has a direction $\mathrm{dir}(i)\in{L,R}$. A position $i$ is mobile if...
TAOCP 7.2.1.2 Exercise 109
Section 7.2.1.2: Generating All Permutations Exercise 109. [ M47 ] [M47] Find good upper and lower bounds on the topswops function f(n). Verified: no Solve time: 4m55s We address the errors directly and rebuild the argument in a fully rigorous way. Let $f(n)$ be the maximum number of topswops moves on a permutation of ${1,\dots,n}$, where a move sends $$ (a_1,a_2,\dots,a_n)\mapsto (a_{a_1},a_{a_1-1},\dots,a_1,a_{a_1+1},\dots,a_n) \quad \text{if } a_1>1, $$ and halts when...
TAOCP 7.2.1.2 Exercise 108
Section 7.2.1.2: Generating All Permutations Exercise 108. [ M27 ] [M27] If the longest n-card game of topswops has length f(n), prove that f(n) ≤ Fn+1 −1. Verified: no Solve time: 3m49s The flaw in the previous solution is the attempt to decompose the dynamics into independent subgames. Topswops does not preserve any fixed separator, so the recurrence $1+f(k-1)+f(n-k)$ is not valid. A different global invariant is needed. The correct...
TAOCP 7.2.1.2 Exercise 107
Section 7.2.1.2: Generating All Permutations Exercise 107. ▶ [ 30 ] [30] (John H. Conway, 1973.) To play the solitaire game of “topswops,” start by shuffling a pack of n cards labeled {1, . . . , n} and place them face up in a pile. Then if the top card is k > 1, deal out the top k cards and put them back on top of the pile,...
TAOCP 7.2.1.2 Exercise 106
Section 7.2.1.2: Generating All Permutations Exercise 106. [ M40 ] [M40] Can exercise 105(b) be solved with a Gray-like code? Verified: no Solve time: 2m56s Setup A weak order on ${1,\dots,n}$ is represented in Exercise 105(b) by a sequence $a_1a_2\dots a_n$ where $a_j$ equals the number of symbols $\prec$ that precede $j$ in the underlying relation. The given examples for $n=3$ are $$ 000, 001, 011, 012, 010, 021, 101,...
TAOCP 7.2.1.2 Exercise 105
Section 7.2.1.2: Generating All Permutations Exercise 105. ▶ [ 26 ] [26] A weak order is a relation ⪯that is transitive (x ⪯y and y ⪯z implies x ⪯z) and complete (x ⪯y or y ⪯x always holds). We can write x ≡y if x ⪯y and y ⪯x; x ≺y if x ⪯y and y ̸⪯x. There are thirteen weak orders on three elements {1, 2, 3}, namely 1...
TAOCP 7.2.1.2 Exercise 104
Section 7.2.1.2: Generating All Permutations Exercise 104. ▶ [ M22 ] [M22] A permutation a1 . . . an of {1, . . . , n} is well-balanced if n k=1 kak = n k=1 (n + 1 −k)ak. For example, 3142 is well-balanced when n = 4. a) Prove that no permutation is well-balanced when n mod 4 = 2. b) Prove that if a1 . ....
TAOCP 7.2.1.2 Exercise 103
Section 7.2.1.2: Generating All Permutations Exercise 103. [ M32 ] [M32] Show that all even permutations of {1, . . . , n} can be generated by suc- cessive rotations of three consecutive elements. Verified: no Solve time: 2m27s Setup Let $S_n$ act on ${1,\dots,n}$ in Knuth’s standard one-line notation. Let $s_i$ denote the adjacent transposition $(i\ i+1)$ for $1 \le i < n$. Every permutation in $S_n$ can be...
TAOCP 7.2.1.2 Exercise 102
Section 7.2.1.2: Generating All Permutations Exercise 102. [ M30 ] [M30] Show that all involutions of {1, . . . , n} can be generated by making at most two transpositions between visits. Verified: yes Solve time: 2m47s We reframe the problem in a way that makes the adjacency condition precise and then build a recursive cyclic Gray ordering that preserves it under the embeddings required by involutions. Two permutations...
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 7
Section 7.2.1.2: Generating All Permutations Exercise 7. [ HM35 ] [HM35] What is the limit as t →∞of the average number of comparisons made per permutation in step L2 when Algorithm L is being applied to the multiset (a) {2 · 1, 2 · 2, . . . , 2 · t}? (b) {1 · 1, 2 · 2, . . . , t · t}? (c) {2 · 1,...
TAOCP 7.2.1.2 Exercise 6
Section 7.2.1.2: Generating All Permutations Exercise 6. [ HM34 ] [HM34] Derive generating functions for the mean number of comparisons made by Algorithm L in (a) step L2, (b) step L3, when {a1, . . . , an} is a general multiset as in exercise 4. Also give the results in closed form when {a1, . . . , an} is the binary multiset {s · 0, (n −s) ·...
TAOCP 7.2.1.2 Exercise 5
Section 7.2.1.2: Generating All Permutations Exercise 5. [ HM25 ] [HM25] Compute the mean and variance of the number of comparisons made by Algorithm L in (a) step L2, (b) step L3, when the elements {a1, . . . , an} are distinct. Verified: no Solve time: 1m27s Solution Let the alphabet be ${x_1 < x_2 < \cdots < x_t}$ with multiplicities $n_1,\ldots,n_t$ and $\sum_{i=1}^t n_i = n$. Algorithm L...
TAOCP 7.2.1.2 Exercise 4
Section 7.2.1.2: Generating All Permutations Exercise 4. [ M23 ] [M23] Generalizing exercise 3, explain how to compute the rank of a1 . . . an with respect to Algorithm L when {a1, . . . , an} is the multiset {n1 · x1, . . . , nt · xt}; here n1 + · · · + nt = n and x1 < · · · < xt. (The...
TAOCP 7.2.1.2 Exercise 3
Section 7.2.1.2: Generating All Permutations Exercise 3. ▶ [ M21 ] x 3. [M21] The rank of a combinatorial arrangement X with respect to a generation algorithm is the number of other arrangements that the algorithm visits prior to X. Explain how to compute the rank of a given permutation a1 . . . an with respect to Algorithm L, if {a1, . . . , an} = {1, ....
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 99
Section 7.2.1.1: Generating All n-Tuples Exercise 99. ▶ [ M23 ] [M23] Exploit the technology of the previous exercises to design an efficient algorithm that locates any given n-bit string in the cycle (fn(0)fn(1) . . . fn(2n−1)). Verified: no Solve time: 2m31s Solution Let $N = 2^n$ and let $f_n(0), f_n(1), \ldots, f_n(N-1)$ be the cycle from Exercise 97, viewed cyclically modulo $N$. From Exercise 97(a) and (b), there...
TAOCP 7.2.1.1 Exercise 98
Section 7.2.1.1: Generating All n-Tuples Exercise 98. [ M34 ] [M34] Continuing the previous exercise, design an efficient algorithm to compute fn(k), given n ≥2 and k ≥0. Verified: no Solve time: 5m19s The central issue is that the previous solution never derived a usable recurrence for the prefix sum $$ S_n(k)=\sum_{j=0}^{k-1} f_n(j), $$ and instead assumed it inherits the same recursive structure as $f_n$. That is unjustified. The correct...
TAOCP 7.2.1.1 Exercise 97
Section 7.2.1.1: Generating All n-Tuples Exercise 97. [ M29 ] [M29] The purpose of this exercise is to analyze the de Bruijn cycles constructed by Algorithms R and D in the important special case m = 2. Let fn(k) be the (k +1)st bit of the 2n-cycle, so that fn(k) = 0 for 0 ≤k < n. Also let jn be the index such that 0 ≤jn < 2n and...
TAOCP 7.2.1.1 Exercise 96
Section 7.2.1.1: Generating All n-Tuples Exercise 96. ▶ [ M28 ] [M28] Suppose a family of coroutines has been set up to generate a de Bruijn cycle of length mn using Algorithms R and D, based recursively on simple coroutines like Algorithm S for the base case n = 2, and using Algorithm D when n > 2 is even. a) How many coroutines (Rn, Dn, Sn) of each type...
TAOCP 7.2.1.1 Exercise 95
Section 7.2.1.1: Generating All n-Tuples Exercise 95. ▶ [ M24 ] [M24] Suppose an infinite sequence a0a1a2 . . . of period p is interleaved with an infinite sequence b0b1b2 . . . of period q to form the infinite cyclic sequence c0c1c2c3c4c5 . . . = a0b0a1b1a2b2 . . . . a) Under what circumstances does c0c1c2 . . . have period pq? (The “period” of a sequence a0a1a2...
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 93
Section 7.2.1.1: Generating All n-Tuples Exercise 93. ▶ [ M28 ] [M28] Prove that Algorithm R outputs a de Bruijn cycle as advertised. Verified: no Solve time: 5m03s We repair the proof by eliminating the false DFS assumptions and instead proving correctness directly from the recursive edge-consumption structure of Algorithm R. 1. De Bruijn graph and target statement Let $\Sigma_m = {0,1,\dots,m-1}$. In the de Bruijn graph $B(m,n)$: vertices are...
TAOCP 7.2.1.1 Exercise 92
Section 7.2.1.1: Generating All n-Tuples Exercise 92. [ M30 ] [M30] Is there an infinite sequence Φn whose first mn elements form an m-ary de Bruijn cycle, for all m? [The case n = 2 is solved in (54).] Verified: no Solve time: 4m Corrected Solution Fix $n \ge 1$. We construct an explicit infinite sequence $\Phi_n = (\phi_0,\phi_1,\dots)$ over $\mathbb{N}$ with the required universality property. The previous approach fails...
TAOCP 7.2.1.1 Exercise 91
Section 7.2.1.1: Generating All n-Tuples Exercise 91. ▶ [ 34 ] [34] Design a loopless algorithm to visit all binary n-tuples (a1, . . . , an) such that a1 ≤a2 ≥a3 ≤a4 ≥· · · . [The number of such n-tuples is Fn+2.] Verified: no Solve time: 3m05s Setup Let $[n]={1,2,\dots,n}$ and let $\mathcal A$ be a family of $r$-subsets of $[n]$ such that for all $\alpha,\beta\in\mathcal A$ one...
TAOCP 7.2.1.1 Exercise 90
Section 7.2.1.1: Generating All n-Tuples Exercise 90. [ 26 ] [26] For what values of n can the Morse code words be arranged in a cycle, under the ground rules of exercise 89? [Hint: The number of code words is Fn+1.] Verified: no Solve time: 4m31s Setup Let $[n]={1,2,\dots,n}$. Let $\mathcal{A}$ be a family of $r$-subsets of $[n]$ such that for all $\alpha,\beta\in\mathcal{A}$, $$ \alpha\cap\beta\neq\varnothing, $$ with $r\le n/2$. Define...
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 87
Section 7.2.1.1: Generating All n-Tuples Exercise 87. [ 27 ] [27] Continuing the previous exercise, define an infinite Gray code that runs through all integer n-tuples (a1, . . . , an), in such a way that max(|a1|, . . . , |an|) ≤ max(|a′ 1|, . . . , |a′ n|) when (a1, . . . , an) is followed by (a′ 1, . . . , a′ n)....
TAOCP 7.2.1.1 Exercise 86
Section 7.2.1.1: Generating All n-Tuples Exercise 86. ▶ [ 26 ] [26] Define an infinite Gray code that runs through all possible nonnegative integer n-tuples (a1, . . . , an) in such a way that max(a1, . . . , an) ≤max(a′ 1, . . . , a′ n) when (a1, . . . , an) is followed by (a′ 1, . . . , a′ n). Verified: no...
TAOCP 7.2.1.1 Exercise 85
Section 7.2.1.1: Generating All n-Tuples Exercise 85. ▶ [ M25 ] [M25] (Dana Richards.) If Γ = (α0, . . . , αt−1) is any sequence of t strings and Γ′ = (α′ 0, . . . , α′ t′−1) is any sequence of t′ strings, the boustrophedon product Γ≀Γ′ is the sequence of tt′ strings that begins (α0α′ 0, . . . , α0α′ t′−1, α1α′ t′−1, . ....
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 83
Section 7.2.1.1: Generating All n-Tuples Exercise 83. [ 41 ] [41] Can four balanced 8-bit Gray cycles cover all edges of the 8-cube? 316 COMBINATORIAL SEARCHING 7.2.1.1 Verified: no Solve time: 4m43s Solution Represent each domino ${i,j}$, $0 \le i \le j \le 6$, as an undirected edge between vertices $i$ and $j$ in a multigraph $G$ on vertex set ${0,1,\dots,6}$, with one loop at each vertex $i$ corresponding to...
TAOCP 7.2.1.1 Exercise 82
Section 7.2.1.1: Generating All n-Tuples Exercise 82. ▶ [ M25 ] [M25] (G. Ringel, 1956.) Use the previous exercise to deduce that there exist four 8-bit Gray cycles that, together, cover all edges of the 8-cube. Verified: no Solve time: 5m55s The error in the proposed solution is fundamental: it tries to generate Hamilton cycles by modifying a single coordinate while keeping all others fixed. That inevitably traps every trajectory...
TAOCP 7.2.1.1 Exercise 81
Section 7.2.1.1: Generating All n-Tuples Exercise 81. [ M21 ] [M21] Let (a0, b0), (a1, b1), . . . , (am2−1, bm2−1) be the 2-digit m-ary modular Gray code. Show that, if m > 2, every edge (x, y)−−−(x, (y + 1) mod m) and (x, y)−−− ((x + 1) mod m, y) occurs in one of the two cycles (a0, b0)−−−(a1, b1)−−−· · ·−−−(am2−1, bm2−1)−−−(a0, b0), (b0, a0)−−−(b1, a1)−−−·...
TAOCP 7.2.1.1 Exercise 80
Section 7.2.1.1: Generating All n-Tuples Exercise 80. [ M20 ] [M20] Explain how to run through all divisors of a number, given its prime fac- torization pe1 1 . . . pet t , repeatedly multiplying or dividing by a single prime at each step. Verified: yes Solve time: 1m46s Solution Let the given factorization be $$ N = p_1^{e_1} p_2^{e_2} \cdots p_t^{e_t}. $$ Every divisor of $N$ has the...
TAOCP 7.2.1.1 Exercise 79
Section 7.2.1.1: Generating All n-Tuples Exercise 79. ▶ [ M22 ] [M22] When is the last n-tuple of the (a) reflected (b) modular mixed-radix Gray code adjacent to the first? Verified: no Solve time: 3m05s 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...
TAOCP 7.2.1.1 Exercise 78
Section 7.2.1.1: Generating All n-Tuples Exercise 78. [ M26 ] [M26] Prove the conversion formulas (50) and (51) for reflected mixed-radix Gray codes, and derive analogous formulas for the modular case. Verified: no Solve time: 6m25s 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...
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 76
Section 7.2.1.1: Generating All n-Tuples Exercise 76. [ M25 ] [M25] Prove that trend-free n-bit Gray codes exist for all n ≥5. Verified: no Solve time: 6m17s 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...
TAOCP 7.2.1.1 Exercise 75
Section 7.2.1.1: Generating All n-Tuples Exercise 75. [ 32 ] [32] Find all 5-bit Gray paths v0, . . . , v31 that are trend-free, in the sense that 31 k=0 k(−1)vkj = 0 in each coordinate position j. Verified: no Solve time: 6m27s 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...
TAOCP 7.2.1.1 Exercise 74
Section 7.2.1.1: Generating All n-Tuples Exercise 74. [ HM25 ] [HM25] (Savage and Winkler.) Prove that adjacent vertices of the n-cube cannot be separated by more than O(2n/√n ) positions in a monotonic Gray code. Verified: no Solve time: 6m13s 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$,...
TAOCP 7.2.1.1 Exercise 73
Section 7.2.1.1: Generating All n-Tuples Exercise 73. ▶ [ 32 ] [32] Design an efficient algorithm to construct the delta sequence of an n-bit monotonic Gray code. Verified: no Solve time: 9m03s 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...
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 71
Section 7.2.1.1: Generating All n-Tuples Exercise 71. [ M22 ] [M22] Derive (45), the recurrence that defines the Savage–Winkler permutations. Verified: no Solve time: 2m07s Connection interrupted. Waiting for the complete answer
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 69
Section 7.2.1.1: Generating All n-Tuples Exercise 69. ▶ [ M25 ] [M25] Modify the definition of Gray binary code (7) by letting h(k) = (. . . (b6 ⊕b5)(b5 ⊕b4)(b4 ⊕b3 ⊕b2 ⊕b0)(b3 ⊕b0)(b2 ⊕b1 ⊕b0)b1)2, when k = (. . . b5b4b3b2b1b0)2. a) Show that the sequence h(0), h(1), . . . , h(2n−1) runs through all n-bit numbers in such a way that exactly 3 bits change each...
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 66
Section 7.2.1.1: Generating All n-Tuples Exercise 66. [ 40 ] [40] Is there a perfect Beckett–Gray pattern for 8 actors? Verified: no Solve time: 4m34s The previous solution failed for two independent reasons: a wrong state-space count and an imprecise formulation of what is actually being searched. Both can be repaired, but doing so forces a cleaner separation between the Beckett process , the state graph , and the subset...
TAOCP 7.2.1.1 Exercise 65
Section 7.2.1.1: Generating All n-Tuples Exercise 65. [ 30 ] [30] (Brett Stevens.) In Samuel Beckett’s play Quad, the stage begins and ends empty; n actors enter and exit one at a time, running through all 2n possible subsets, and the actor who leaves is always the one whose previous entrance was earliest. When 7.2.1.1 GENERATING ALL n-TUPLES 315 n = 4, as in the actual play, some subsets are...
TAOCP 7.2.1.1 Exercise 64
Section 7.2.1.1: Generating All n-Tuples Exercise 64. ▶ [ HM35 ] [HM35] (L. Goddyn and P. Gvozdjak.) An n-bit Gray stream is a sequence of permutations (σ0, σ1, . . . , σl−1) where each σk is a permutation of the vertices of the n-cube, taking every vertex to one of its neighbors. a) Suppose (u0, . . . , u2m−1) is an m-bit Gray cycle and (σ0, σ1, ....
TAOCP 7.2.1.1 Exercise 63
Section 7.2.1.1: Generating All n-Tuples Exercise 63. [ 30 ] [30] (Luis Goddyn.) Prove that r(10) ≥8. Verified: no Solve time: 4m29s Setup Let $\Gamma_n = g(0), g(1), \dots, g(2^n-1)$ denote the $n$-bit Gray cycle as defined in (5)–(7). For a vertex $x \in {0,1}^n$, write $\mathrm{pos}_n(x)$ for its position in $\Gamma_n$. The run-length-bound function $r(n)$ is defined (in the preceding exercises of this section) as the largest integer $r$...
TAOCP 7.2.1.1 Exercise 62
Section 7.2.1.1: Generating All n-Tuples Exercise 62. [ 46 ] [46] Does r(8) = 6? Verified: no Solve time: 4m14s Setup Let $\Gamma_n$ be an $n$-bit Gray cycle in the sense of Section 7.2.1.1, so consecutive vertices differ in exactly one bit. Write $g_i \in {1,\dots,n}$ for the coordinate flipped at step $i$ of the cycle, interpreted cyclically so that $g_{2^n}$ compares the last vertex back to the first. A...
TAOCP 7.2.1.1 Exercise 61
Section 7.2.1.1: Generating All n-Tuples Exercise 61. [ M30 ] [M30] Show that r(m + n) ≥r(m) + r(n) −1 if (a) m = 2 and 2 < r(n) < 8; or (b) m ≤n and r(n) ≤2m−3. Verified: no Solve time: 4m14s 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...
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 57
Section 7.2.1.1: Generating All n-Tuples Exercise 57. [ 32 ] [32] Consider a graph whose vertices are the 2688 possible 4-bit Gray cycles, where two such cycles are adjacent if they are related by one of the following simple transformations: Before After Type 1 After Type 2 After Type 3 After Type 4 (Type 1 changes arise when the cycle can be broken into two parts and reassembled with one...
TAOCP 7.2.1.1 Exercise 56
Section 7.2.1.1: Generating All n-Tuples Exercise 56. [ M30 ] [M30] (E. N. Gilbert, 1958.) Say that two Gray cycles are equivalent if their delta sequences can be made equal by permuting the coordinate names, or by reversing the cycle and/or starting the cycle at a different place. Show that the 2688 different 4-bit Gray cycles fall into just 9 equivalence classes. Verified: no Solve time: 4m10s The previous solution...
TAOCP 7.2.1.1 Exercise 55
Section 7.2.1.1: Generating All n-Tuples Exercise 55. ▶ [ 35 ] [35] (F. Ruskey and C. Savage, 1993.) If (v0, . . . , v2n−1) is an n-bit Gray cycle, the pairs { {v2k, v2k+1} | 0 ≤k < 2n−1 } form a perfect matching between the vertices 314 COMBINATORIAL SEARCHING 7.2.1.1 of even and odd parity in the n-cube. Conversely, does every such perfect matching arise as “half” of...
TAOCP 7.2.1.1 Exercise 54
Section 7.2.1.1: Generating All n-Tuples Exercise 54. [ M20 ] [M20] (H. S. Shapiro, 1953.) Show that if a sequence of integers (a1, . . . , a2n) con- tains only n distinct values, then there is a subsequence whose product ak+1ak+2 . . . al is a perfect square, for some 0 ≤k < l ≤2n. However, this conclusion might not be true if we disallow the case l...
TAOCP 7.2.1.1 Exercise 53
Section 7.2.1.1: Generating All n-Tuples Exercise 53. [ M46 ] [M46] If the numbers (c0, . . . , cn−1) are even and satisfy the condition of the previous exercise, is there always an n-bit Gray cycle with these transition counts? Verified: no Solve time: 1m40s Setup Let $Q_n$ be the $n$-dimensional hypercube with vertex set ${0,1}^n$, where each edge is labeled by the coordinate in which its endpoints differ....
TAOCP 7.2.1.1 Exercise 52
Section 7.2.1.1: Generating All n-Tuples Exercise 52. [ M20 ] [M20] Prove that if the transition counts of an n-bit Gray cycle satisfy c0 ≤c1 ≤ · · · ≤cn−1, we must have c0 + · · · + cj−1 ≥2j, with equality when j = n. Verified: yes Solve time: 3m12s The previous argument fails only because it does not properly justify two key facts: (i) the projection onto...
TAOCP 7.2.1.1 Exercise 51
Section 7.2.1.1: Generating All n-Tuples Exercise 51. [ M24 ] [M24] (Balanced Gray cycles.) Complete the proof of Corollary B to Theorem D. Verified: no Solve time: 3m26s The flaw in the proposed argument is that it tries to transfer coordinate symmetry of the hypercube into symmetry of a particular recursively defined cycle , without proving that the recursion produces a cycle that is equivariant under coordinate permutations. That gap...
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 48
Section 7.2.1.1: Generating All n-Tuples Exercise 48. [ HM48 ] [HM48] Determine the asymptotic behavior of d(n)1/2n as n →∞. Verified: no Solve time: 4m21s 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}$ be the Walsh matrix of order $8$, defined by $$ h_{ij}=(-1)^{i\cdot j}, \qquad i\cdot j=i_1j_1+i_2j_2+i_3j_3...
TAOCP 7.2.1.1 Exercise 47
Section 7.2.1.1: Generating All n-Tuples Exercise 47. [ HM24 ] [HM24] What asymptotic estimates do exercises 44 and 46 give for d(n)1/2n? Verified: no Solve time: 4m32s The previous solution fails because it introduces an external structure (perfect matchings) that is not part of the information supplied by Exercises 44 and 46. The correct approach is to use only what those exercises actually establish about Hamiltonian cycles in $Q_n$, namely...
TAOCP 7.2.1.1 Exercise 46
Section 7.2.1.1: Generating All n-Tuples Exercise 46. [ M23 ] [M23] Extend exercise 45 to the (kr + 2)-cube, for k even. Verified: no Solve time: 4m52s The previous attempt fails because it tries to “lift” a Gray cycle on ${0,1}^k$ into a block-selection rule without defining a consistent edge partition of the $(kr+2)$-cube. The correct approach must avoid mixing signature dynamics with block indices and instead construct a genuine...
TAOCP 7.2.1.1 Exercise 45
Section 7.2.1.1: Generating All n-Tuples Exercise 45. [ M40 ] [M40] (T. Feder and C. Subi, 2009.) This exercise constructs a large number of Gray cycles in the (4r+2)-cube G = G4 G3 G2 G1 G0 G−1, where Gi is an r-cube for i > 0 and G0 = G−1 = P2. The vertices v are (4r+2)-bit strings v4 . . . v0v−1, where vi has r bits for i...
TAOCP 7.2.1.1 Exercise 44
Section 7.2.1.1: Generating All n-Tuples Exercise 44. [ M20 ] [M20] Show that d(n) ≤ M(n) 2 , if the n-cube has M(n) perfect matchings. Verified: no Solve time: 2m37s 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}$ be the Walsh matrix of order $8$, defined by...
TAOCP 7.2.1.1 Exercise 43
Section 7.2.1.1: Generating All n-Tuples Exercise 43. [ 41 ] [41] Determine d(6), the number of 6-bit Gray cycles. (See (26).) Verified: no Solve time: 4m 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}$ be the Walsh matrix of order $8$, defined by $$ h_{ij}=(-1)^{i\cdot j}, \qquad i\cdot...
TAOCP 7.2.1.1 Exercise 42
Section 7.2.1.1: Generating All n-Tuples Exercise 42. [ 35 ] [35] (M. L. Fredman.) Algorithm L uses Θ(n log n) bits of auxiliary memory for focus pointers as it chooses the Gray binary bit aj to complement next. Step L3 examines Θ(log n) of the auxiliary bits, and it occasionally changes Ω(log n) of them. Show that, from a theoretical standpoint, we can do better: The n-bit Gray binary code...
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 39
Section 7.2.1.1: Generating All n-Tuples Exercise 39. ▶ [ HM30 ] [HM30] (J. J. Sylvester.) The rows of ( a b b −a) are orthogonal to each other and have the same magnitude; therefore the matrix identity ( A B ) a2 + b2 0 0 a2 + b2 A B = ( A B ) a b b −a a b b −a A B = (...
TAOCP 7.2.1.1 Exercise 38
Section 7.2.1.1: Generating All n-Tuples Exercise 38. ▶ [ M25 ] [M25] Devise a ternary generalization of Walsh functions. Verified: no Solve time: 6m05s Solution Let $\omega = e^{2\pi i/3}$, so $\omega^3 = 1$ and $1 + \omega + \omega^2 = 0$. Write each nonnegative integer $k$ in base $3$ as $$ k = \sum_{j \ge 0} k_j 3^j, \quad k_j \in {0,1,2}. $$ For $x \in [0,1)$ define its...
TAOCP 7.2.1.1 Exercise 37
Section 7.2.1.1: Generating All n-Tuples Exercise 37. [ HM23 ] [HM23] Let zkl be the location of the lth sign change in wk(x), for 1 ≤l ≤k and 0 < zkl < 1. Prove that |zkl −l/(k + 1)| = O((log k)/k). Verified: no Solve time: 7m08s Solution Let $w_k(x)$ denote the $k$th Walsh function on $[0,1)$ in the Paley ordering, as defined in Section 7.2.1.1, so that each $w_k$...
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 35
Section 7.2.1.1: Generating All n-Tuples Exercise 35. [ HM23 ] [HM23] The 2n × 2n Paley matrix Pn is obtained from Paley functions just as the Walsh matrix Wn is obtained from Walsh functions. (See (20).) Find interesting relations between Pn, Wn, and the Hadamard matrix Hn. Prove that all three matrices are symmetric. Verified: no Solve time: 5m Solution Let $x \in [0,1)$ and write its dyadic expansion $$...
TAOCP 7.2.1.1 Exercise 34
Section 7.2.1.1: Generating All n-Tuples Exercise 34. [ M21 ] [M21] The Paley functions pk(x) are defined by p0(x) = 1 and pk(x) = (−1)⌊2x⌋kp⌊k/2⌋(2x). Show that pk(x) has a simple expression in terms of Rademacher functions, analogous to (17), and relate Paley functions to Walsh functions. Verified: no Solve time: 6m20s Solution Let $x \in [0,1)$ and write its dyadic expansion $$ x = 0.x_1 x_2 x_3 \ldots,\qquad x_j...
TAOCP 7.2.1.1 Exercise 33
Section 7.2.1.1: Generating All n-Tuples Exercise 33. ▶ [ M20 ] [M20] Prove the Rademacher-to-Walsh law (17). Verified: no Solve time: 6m22s Solution Let $x \in [0,1)$ and write its dyadic expansion $$ x = 0.x_1 x_2 x_3 \ldots,\qquad x_j \in {0,1}. $$ Let $r_j(x)$ denote the $j$-th Rademacher function, $$ r_j(x) = (-1)^{x_j}. $$ Let $k$ have binary expansion $$ k = (b_m \cdots b_1 b_0)_2,\qquad b_j \in {0,1},...
TAOCP 7.2.1.1 Exercise 32
Section 7.2.1.1: Generating All n-Tuples Exercise 32. [ M20 ] [M20] True or false: Walsh functions satisfy wk(−x) = (−1)kwk(x). Verified: no Solve time: 5m39s Solution Let $x \in [0,1)$ and write its dyadic expansion $$ x = 0.x_1 x_2 x_3 \ldots,\qquad x_j \in {0,1}. $$ Let $r_j(x)$ denote the $j$-th Rademacher function, $$ r_j(x) = (-1)^{x_j}. $$ Let $k$ have binary expansion $$ k = (b_m \cdots b_1 b_0)_2,\qquad...
TAOCP 7.2.1.1 Exercise 31
Section 7.2.1.1: Generating All n-Tuples Exercise 31. [ HM35 ] [HM35] (Gray fields.) Let fn(x) = g(rn(x)) denote the operation of reflecting the bits of an n-bit binary string as in exercise 5 and then converting to Gray binary code. For example, the operation f3(x) takes (001)2 → (110)2 → (010)2 → (011)2 → (101)2 → (111)2 → (100)2 → (001)2, hence all of the nonzero possibilities appear in a...
TAOCP 7.2.1.1 Exercise 30
Section 7.2.1.1: Generating All n-Tuples Exercise 30. ▶ [ M27 ] [M27] (Gray permutation.) Design a one-pass algorithm to replace the array elements (X0, X1, X2, . . . , X2n−1) by (Xg(0), Xg(1), Xg(2), . . . , Xg(2n−1)), using only a constant amount of auxiliary storage. Hint: Considering the function g(n) as a per- mutation of all nonnegative integers, show that the set L = {0, 1, (10)2,...
TAOCP 7.2.1.1 Exercise 29
Section 7.2.1.1: Generating All n-Tuples Exercise 29. [ M24 ] [M24] If integer values k are transmitted as n-bit Gray binary codes g(k) and received with errors described by a bit pattern p = (pn−1 . . . p0)2, the average numerical error is 1 2n 2n−1 k=0 g[−1](g(k) ⊕p) −k , assuming that all values of k are equally likely. Show that this sum is equal to 2n−1...
TAOCP 7.2.1.1 Exercise 28
Section 7.2.1.1: Generating All n-Tuples Exercise 28. [ M27 ] [M27] A median of n-bit strings {α1, . . . , αt}, where αk has the binary represen- tation αk = ak(n−1) . . . ak0, is a string ˆα = an−1 . . . a0 whose bits aj for 0 ≤j < n agree with the majority of the bits akj for 1 ≤k ≤t. (If t is even...
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 25
Section 7.2.1.1: Generating All n-Tuples Exercise 25. ▶ [ M25 ] [M25] Prove that if g(k) and g(l) differ in t > 0 bits, and if 0 ≤k, l < 2n, then ⌈2t/3⌉≤|k −l| ≤2n −⌈2t/3⌉. Verified: no Solve time: 2m01s Solution Let $g(k)=k\oplus \lfloor k/2\rfloor$, and write the binary expansions $$ k=(\dots b_2 b_1 b_0)_2,\qquad g(k)=(\dots a_2 a_1 a_0)_2, $$ with the standard Gray relations from (7.2.1.1–7): $$ a_j=b_j\oplus...
TAOCP 7.2.1.1 Exercise 24
Section 7.2.1.1: Generating All n-Tuples Exercise 24. [ M21 ] [M21] Consider extending the Gray binary function g to all 2-adic integers (see Section 7.1.3). What is the corresponding inverse function g[−1]? Verified: no Solve time: 4m32s The flaw in the previous solution is the attempt to treat an infinite XOR as a topological limit inside the product space. The correct way to make the argument rigorous is to avoid...
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 21
Section 7.2.1.1: Generating All n-Tuples Exercise 21. [ M30 ] [M30] A t-subcube of an n-cube can be represented by a string like ∗∗10∗∗0∗, containing t asterisks and n −t specified bits. If all 2n binary n-tuples are written in lexicographic order, the elements belonging to such a subcube appear in 2t′ clusters of consecutive entries, where t′ is the number of asterisks that lie to the left of the...
TAOCP 7.2.1.1 Exercise 20
Section 7.2.1.1: Generating All n-Tuples Exercise 20. [ M36 ] [M36] The 16-bit codewords in the previous exercise can be used to transmit 8 bits of information, allowing transmission errors to be corrected if any one or two bits are corrupted; furthermore, mistakes will be detected (but not necessarily correctable) if any three bits are received incorrectly. Devise an algorithm that either finds the nearest codeword to a given 16-bit...
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 110
Section 7.2.1.1: Generating All n-Tuples Exercise 110. [ M25 ] [M25] Continuing the previous exercise, construct m-ary ourotoruses for all m. Verified: no Solve time: 6m25s Solution Let $T_n$ denote the number of steps in the shortest procedure that removes all $n$ rings from the bar and then restores them, when the two smallest rings may be taken on or off simultaneously. For $n=1$, only the smallest ring exists, so...
TAOCP 7.2.1.1 Exercise 11
Section 7.2.1.1: Generating All n-Tuples Exercise 11. [ M22 ] [M22] (H. J. Purkiss, 1865.) The two smallest rings of the Chinese ring puzzle can actually be taken on or off the bar simultaneously. How many steps does the puzzle require when such accelerated moves are permitted? Verified: no Solve time: 7m59s Solution Let $T_n$ denote the number of steps in the shortest procedure that removes all $n$ rings from...
TAOCP 7.2.1.1 Exercise 109
Section 7.2.1.1: Generating All n-Tuples Exercise 109. [ M22 ] [M22] An m-ary de Bruijn torus of size m2 × m2 for 2 × 2 windows is a matrix of m-ary digits dij such that each of the m4 submatrices dij di(j+1) d(i+1)j d(i+1)(j+1) , 0 ≤i, j < m2 is different, where subscripts wrap around modulo m2. Thus every possible m-ary 2×2 submatrix occurs exactly once; Ian...
TAOCP 7.2.1.1 Exercise 108
Section 7.2.1.1: Generating All n-Tuples Exercise 108. [ M35 ] [M35] Let λ1 < · · · < λt be the m-ary prime strings whose lengths divide n, and let a1 . . . an be any m-ary string. The object of this exercise is to prove that a1 . . . an appears in λ1 . . . λtλ1λ2; hence λ1 . . . λt is a de Bruijn...
TAOCP 7.2.1.1 Exercise 107
Section 7.2.1.1: Generating All n-Tuples Exercise 107. [ HM30 ] [HM30] Analyze the running time of Algorithm F, for fixed m as n →∞. Verified: no Solve time: 6m21s Setup A string is written over a totally ordered infinite alphabet. Lexicographic order is used throughout. A string is called prime in the sense of Exercise 7.2.1.1.101 if it is strictly smaller than each of its proper nontrivial suffixes, equivalently it...
TAOCP 7.2.1.1 Exercise 106
Section 7.2.1.1: Generating All n-Tuples Exercise 106. ▶ [ M30 ] [M30] By reverse-engineering Algorithm F, design an algorithm that visits all m-ary primes and preprimes in decreasing order. Verified: no Solve time: 6m45s Setup A string is written over a totally ordered infinite alphabet. Lexicographic order is used throughout. A string is called prime in the sense of Exercise 7.2.1.1.101 if it is strictly smaller than each of its...
TAOCP 7.2.1.1 Exercise 105
Section 7.2.1.1: Generating All n-Tuples Exercise 105. [ M31 ] [M31] Let α be a preprime string of length n on an infinite alphabet. a) Show that if the final letter of α is increased, the resulting string is prime. b) If α has been factored as in exercise 101, show that it is the n-extension of λ1. c) Furthermore α cannot be the n-extension of two different primes. Verified:...
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 103
Section 7.2.1.1: Generating All n-Tuples Exercise 103. [ M20 ] [M20] Use Eq. (59) to prove Fermat’s theorem that mp ≡m (modulo p). Verified: yes Solve time: 1m05s Solution Let $p$ be a prime. Equation (59) in Section 7.2.1.1 asserts the binomial congruence $$ (x+y)^p \equiv x^p + y^p \pmod p, $$ which follows from the binomial theorem together with the fact that for $1 \le k \le p-1$, the...
TAOCP 7.2.1.1 Exercise 102
Section 7.2.1.1: Generating All n-Tuples Exercise 102. [ HM28 ] [HM28] Deduce the number of m-ary primes of length n from the unique factor- ization theorem in the previous exercise. Verified: yes Solve time: 1m18s Setup Let the alphabet have size $m$, totally ordered. A word of length $n$ is an $m$-ary string, so the total number of words of length $n$ is $m^n$. Let $L_n$ denote the number of...
TAOCP 7.2.1.1 Exercise 101
Section 7.2.1.1: Generating All n-Tuples Exercise 101. ▶ [ M30 ] [M30] (Unique factorization of strings into nonincreasing primes.) a) Prove that if λ and λ′ are prime, then λλ′ is prime if λ < λ′. b) Consequently every string α can be written in the form α = λ1λ2 . . . λt, λ1 ≥λ2 ≥· · · ≥λt, where each λj is prime. c) In fact, only one...
TAOCP 7.2.1.1 Exercise 100
Section 7.2.1.1: Generating All n-Tuples Exercise 100. [ 40 ] [40] Do the de Bruijn cycles of exercise 97 provide a useful source of pseudo- random bits when n is large? Verified: no Solve time: 8m03s Setup Let $f_n(k)$ be the binary de Bruijn cycle of order $n$ constructed in Exercise 97, so that the infinite periodic sequence $$ f_n(0), f_n(1), \ldots, f_n(2^n-1) $$ contains every $n$-bit string exactly once...
TAOCP 7.2.1.1 Exercise 10
Section 7.2.1.1: Generating All n-Tuples Exercise 10. ▶ [ M21 ] [M21] Find a simple formula for the total number of steps An or Bn in which a ring is (a) removed or (b) replaced, in the shortest procedure for removing n Chinese rings. For example, A3 = 4 and B3 = 1. Verified: no Solve time: 6m10s Solution The Chinese ring puzzle (Baguenaudier) has a standard representation as a...
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 8
Section 7.2.1.1: Generating All n-Tuples Exercise 8. [ 15 ] [15] What’s an easy way to run through all n-bit strings of even parity, changing only two bits at each step? Verified: no Solve time: 6m22s 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 variable $x_i$ takes values in ${0,1,2,3}$, represented in binary as in (73). Any valid coloring...
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 6
Section 7.2.1.1: Generating All n-Tuples Exercise 6. [ M17 ] [M17] Prove (7), the basic formula for Gray binary code. Verified: yes Solve time: 3m55s We start from the binary representation of an integer $k$ with $n$ bits: $$ k = (b_{n-1} b_{n-2} \dots b_0)_2,\quad b_j \in {0,1}, $$ and we extend the notation by setting $b_n = 0$. Define a new bit string $g(k) = (a_{n-1} a_{n-2} \dots a_0)_2$...
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.2.1.1 Exercise 3
Section 7.2.1.1: Generating All n-Tuples Exercise 3. ▶ [ M20 ] x 3. [M20] How many times does Algorithm M perform step M4? Verified: yes Solve time: 2m44s The flaw in the previous solution is not in the identification of trailing maximal components, but in how step M4 is counted inside a single transition. Step M4 is not a simple “reset counter”, it is a repeated test of the condition...
TAOCP 7.2.1.1 Exercise 2
Section 7.2.1.1: Generating All n-Tuples Exercise 2. [ 15 ] [15] What is the 1000000th n-tuple visited by Algorithm M if n = 10 and mj = j for 1 ≤j ≤n? Hint: [ 0, 1, 0, 2, 1, 3, 2, 4, 3, 5, 0, 6, 2, 7, 7, 8, 1, 9, 0 10] = 1000000. Verified: yes Solve time: 1m15s Solution Algorithm M visits n-tuples $(a_1,\dots,a_n)$ in lexicographic order...
TAOCP 7.2.1.1 Exercise 1
Section 7.2.1.1: Generating All n-Tuples Exercise 1. [ 10 ] [10] Explain how to generate all n-tuples (a1, . . . , an) in which lj ≤aj ≤uj, given lower bounds lj and upper bounds uj for each component. (Assume that lj ≤uj.) Verified: yes Solve time: 54s Introduce shifted variables $b_j = a_j - l_j$. The bounds $l_j \le a_j \le u_j$ become $0 \le b_j \le u_j -...
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 97
Section 7.1.4: Binary Decision Diagrams Exercise 97. [ M20 ] [M20] Prove that it’s possible to compute arbitrary bottom-of-the-BDD quantifi- cations such as ∃xn−5∀xn−4 xn−3∃xn−2 xn−1∀xnf(x1, . . . , xn) in O(B(f)) steps. Verified: no Solve time: 5m58s 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...
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 93
Section 7.1.4: Binary Decision Diagrams Exercise 93. [ 36 ] [36] Given a graph G on the vertices {1, . . . , n}, construct Boolean functions f and g with the property that an approximating function ˆf exists as in exercise 91 with small B( ˆf) if and only if G can be 3-colored. (Hence the task of minimizing B( ˆf) is NP-complete.) Verified: no Solve time: 12m07s Solution...
TAOCP 7.1.4 Exercise 92
Section 7.1.4: Binary Decision Diagrams Exercise 92. [ M27 ] [M27] The operation f ↓g in exercise 91 sometimes depends on the ordering of the variables. Given g = g(x1, . . . , xn), prove that (f π ↓gπ) = (f ↓g)π for all permutations π of {1, . . . , n} and for all functions f = f(x1, . . . , xn) if and only if...
TAOCP 7.1.4 Exercise 91
Section 7.1.4: Binary Decision Diagrams Exercise 91. ▶ [ 26 ] [26] In practice we often want to simplify a Boolean function f with respect to a “care set” g, by finding a function ˆf with small B( ˆf) such that f(x) ∧g(x) ≤ˆf(x) ≤f(x) ∨¯g(x) for all x. In other words, ˆf(x) must agree with f(x) whenever x satisfies g(x) = 1, but we don’t care what value ˆf(x)...
TAOCP 7.1.4 Exercise 90
Section 7.1.4: Binary Decision Diagrams Exercise 90. [ M20 ] [M20] When l = m = n = 3, Eq. (64) corresponds to the MOR operation of MMIX. Is there an analogous formula that corresponds to MXOR (matrix multiplication mod 2)? Verified: no Solve time: 6m29s Solution Let Eq. (64) denote the MOR formulation of matrix composition for Boolean matrices, where the entry of the product is formed using the...
TAOCP 7.1.4 Exercise 89
Section 7.1.4: Binary Decision Diagrams Exercise 89. [ 15 ] [15] Are the following quantified formulas true or false? (a) ∃x1∃x2f = ∃x2∃x1f. (b) ∀x1∀x2f = ∀x2∀x1f. (c) ∀x1∃x2f ≤∃x2∀x1f. (d) ∀x1∃x2f ≥∃x2∀x1f. Verified: no Solve time: 5m01s Solution Let $f$ be a Boolean function of variables $x_1, x_2$, taking values in ${\bot,\top}$, with the usual ordering $\bot < \top$. All quantified expressions are interpreted pointwise over Boolean values, and...
TAOCP 7.1.4 Exercise 88
Section 7.1.4: Binary Decision Diagrams Exercise 88. ▶ [ M25 ] [M25] Find functions f, g, and h for which the recursive ternary computation of f ∧g ∧h outperforms any of the binary computations (f ∧g)∧h, (g ∧h)∧f, (h∧f)∧g. Verified: no Solve time: 8m42s Solution Let variables be ordered $x_1 < x_2 < \cdots < x_n$. The recursive BDD computation of a conjunction proceeds by Shannon expansion at the current...
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 83
Section 7.1.4: Binary Decision Diagrams Exercise 83. [ M20 ] [M20] Prove that if f and g both have reference count 1, we needn’t consult the memo cache when computing AND(f, g) by (55). Verified: yes Solve time: 2m59s Solution Let (55) denote the recursive apply procedure for $\mathrm{AND}(f,g)$ on BDDs, where each call is indexed by a pair of nodes $(u,v)$ and recursively generates calls on $(u_0,v_0)$, $(u_0,v_1)$, $(u_1,v_0)$,...
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 77
Section 7.1.4: Binary Decision Diagrams Exercise 77. ▶ [ M35 ] [M35] Show that there’s an infinite sequence (b0, b1, b2, . . . ) = (1, 2, 3, 5, 6, . . . ) such that the profile of the BDD for µn is (b0, b1, . . . , b2n−1−1, b2n−1−1, . . . , b1, b0, 2). (See Fig. 25.) How many branch nodes of that BDD...
TAOCP 7.1.4 Exercise 76
Section 7.1.4: Binary Decision Diagrams Exercise 76. ▶ [ M22 ] [M22] A “clutter” is a family S of mutually incomparable sets; in other words, S ̸⊆S′ whenever S and S′ are distinct members of S. Every set S ⊆{0, 1, . . . , n −1} can be represented as an n-bit integer s = {2e | e ∈S}; so every family of subsets of {0, 1, . ....
TAOCP 7.1.4 Exercise 75
Section 7.1.4: Binary Decision Diagrams Exercise 75. [ M20 ] [M20] Let ρn(x1, . . . , x2n) be the Boolean function that is true if and only if x1 . . . x2n is the truth table of a regular function (see exercise 7.1.1–110). Show that the BDD for ρn can be computed by a procedure similar to that of µn in (49). Verified: no Solve time: 9m33s Solution...
TAOCP 7.1.4 Exercise 74
Section 7.1.4: Binary Decision Diagrams Exercise 74. ▶ [ M23 ] [M23] Explain how to count the number of self-dual monotone Boolean functions of n variables, by modifying (49). Verified: no Solve time: 6m15s Solution Let $f(x_1,\dots,x_n)$ be a monotone Boolean function. Monotonicity means that if $x \le y$ componentwise then $f(x) \le f(y)$. The dual function is defined by [ f^D(x_1,\dots,x_n) = \neg f(\neg x_1,\dots,\neg x_n). ] A function...
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 65
Section 7.1.4: Binary Decision Diagrams Exercise 65. ▶ [ M25 ] [M25] If h(x1, . . . , xn) = f(x1, . . . , xj−1, g(x1, . . . , xn), xj+1, . . . , xn), prove that B(h) = O(B(f)2B(g)). Can this upper bound be improved to O(B(f)B(g)) in general? Verified: no Solve time: 11m40s Setup Let $f_n(k)$ denote the $k$th bit of the binary de Bruijn...
TAOCP 7.1.4 Exercise 64
Section 7.1.4: Binary Decision Diagrams Exercise 64. [ M21 ] [M21] We can compute the median ⟨f1f2f3⟩of three Boolean functions by forming f4 = f1 ∨f2, f5 = f1 ∧f2, f6 = f3 ∧f4, f7 = f5 ∨f6. Then B(f4) = O(B(f1)B(f2)), B(f5) = O(B(f1)B(f2)), B(f6) = O(B(f3)B(f4)) = O(B(f1)B(f2)B(f3)); therefore B(f7) = O(B(f5)B(f6)) = O(B(f1)2B(f2)2B(f3)). Prove, however, that B(f7) is actually only O(B(f1)B(f2)B(f3)), and the running time to compute...
TAOCP 7.1.4 Exercise 63
Section 7.1.4: Binary Decision Diagrams Exercise 63. [ M27 ] [M27] Let f(x1, . . . , xn) = Mm(x1 ⊕x2, x3 ⊕x4, . . . , x2m−1 ⊕x2m; x2m+1, . . . , xn) and g(x1, . . . , xn) = Mm(x2 ⊕x3, . . . , x2m−2 ⊕x2m−1, x2m; ¯x2m+1, . . . , ¯xn), where n = 2m + 2m. What are B(f), B(g), and B(f...
TAOCP 7.1.4 Exercise 62
Section 7.1.4: Binary Decision Diagrams Exercise 62. ▶ [ M21 ] [M21] If f(x) = ⌊n/2⌋ j=1 (x2j−1 ∧x2j) and g(x) = (x1 ∧xn)∨⌈n/2⌉−1 j=1 (x2j ∧x2j+1), what are the asymptotic values of B(f), B(g), B(f ⋄g), and B(f ∨g) as n →∞? Verified: no Solve time: 6m33s Solution Algorithm D in Section 7.2.1.1 is the standard coroutine-based construction that interleaves two cyclic generators $f$ and $f'$, each producing an...
TAOCP 7.1.4 Exercise 61
Section 7.1.4: Binary Decision Diagrams Exercise 61. ▶ [ M27 ] [M27] If α and β are nodes of the respective BDDs for f and g, prove that in-degree(α ⋄β) ≤in-degree(α) · in-degree(β) in the melded BDD f ⋄g. (Imagine that the root of a BDD has in-degree 1.) 7.1.4 BINARY DECISION DIAGRAMS 263 Verified: no Solve time: 5m32s Setup Let $f$ and $g$ be Boolean functions with respective BDDs....
TAOCP 7.1.4 Exercise 60
Section 7.1.4: Binary Decision Diagrams Exercise 60. [ M22 ] [M22] Suppose f(x1, . . . , xn) and g(x1, . . . , xn) have the profiles (b0, . . . , bn) and (b′ 0, . . . , b′ n), respectively, and let their respective quasi-profiles be (q0, . . . , qn) and (q′ 0, . . . , q′ n). Show that their meld f...
TAOCP 7.1.4 Exercise 59
Section 7.1.4: Binary Decision Diagrams Exercise 59. ▶ [ M28 ] [M28] Let h(x1, . . . , xn) be a Boolean function. Describe the melded BDD f ⋄g in terms of the BDD for h, when (a) f(x1, . . . , x2n) = h(x1, . . . , xn) and g(x1, . . . , x2n) = h(xn+1, . . . , x2n); (b) f(x1, x2, . ....
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 55
Section 7.1.4: Binary Decision Diagrams Exercise 55. [ M30 ] [M30] Explain how to construct the “connectedness BDD” of a graph (like Fig. 22). Verified: no Solve time: 4m57s Setup Let $G=(V,E)$ be a finite undirected graph with $|V|=n$ and edges $E={e_1,\dots,e_m}$, ordered so that the BDD variable ordering is $x_1,\dots,x_m$, where $x_i$ corresponds to $e_i$. Define the Boolean function $$ f(x_1,\dots,x_m)=1 $$ iff the subgraph $(V,{e_i\mid x_i=1})$ is connected....
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 48
Section 7.1.4: Binary Decision Diagrams Exercise 48. [ M22 ] [M22] Implement the hidden weighted bit function with a network of modules like Fig. 23, using ak = 2 + λk and bk = 1 + λ(n −k) connecting wires for 1 ≤k < n. Conclude from Theorem B that the upper bound in Theorem M cannot be improved to n k=0 2p(ak,bk) for any polynomial p. Verified: no Solve...
TAOCP 7.1.4 Exercise 47
Section 7.1.4: Binary Decision Diagrams Exercise 47. [ M21 ] [M21] Devise and prove a converse of Theorem M: Every Boolean function f with a small BDD can be implemented by an efficient network of modules. Verified: yes Solve time: 1m36s Solution Let $f$ be a Boolean function with a reduced ordered binary decision diagram $G$. Let $B(f)$ denote the number of nodes of $G$, including the sinks $\bot$ and...
TAOCP 7.1.4 Exercise 46
Section 7.1.4: Binary Decision Diagrams Exercise 46. [ M23 ] [M23] What is the true BDD size of the three-in-a-row function? Verified: no Solve time: 1m45s Solution Let $f(x_1,\dots,x_n)$ be the three-in-a-row function, that is, $$ f(x_1,\dots,x_n)=1 $$ iff there exists $i$ with $1\le i\le n-2$ such that either $$ x_i=x_{i+1}=x_{i+2}=1 \quad \text{or} \quad x_i=x_{i+1}=x_{i+2}=0. $$ We determine $B(f)$ by identifying exactly which subfunctions of $f$ give rise to distinct...
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 44
Section 7.1.4: Binary Decision Diagrams Exercise 44. ▶ [ M32 ] [M32] Determine the maximum possible size, Σn, of B(f) when f is a symmetric Boolean function of n variables. 262 COMBINATORIAL SEARCHING 7.1.4 Verified: no Solve time: 1m44s Setup A symmetric Boolean function $f(x_1,\dots,x_n)$ depends only on the Hamming weight $t=x_1+\cdots+x_n$, so it is determined by a binary sequence $$ \sigma = (f(0),f(1),\dots,f(n)), $$ of length $n+1$. In the...
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 41
Section 7.1.4: Binary Decision Diagrams Exercise 41. [ M25 ] [M25] Assuming that n ≥4, find the BDD size of the Fibonacci threshold func- tions (a) ⟨xF1 1 xF2 2 . . . xFn−2 n−2 xFn−1 n−1 xFn−2 n ⟩and (b) ⟨xF1 n xF2 n−1 . . . xFn−2 3 xFn−1 2 xFn−2 1 ⟩. Verified: no Solve time: 5m08s Solution Let $F_1=1$, $F_2=1$, and $F_{k+2}=F_{k+1}+F_k$. Write $$ W_n =...
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 39
Section 7.1.4: Binary Decision Diagrams Exercise 39. [ M20 ] [M20] What is the size of the BDD for the threshold function [x1 + · · · + xn ≥k]? Verified: no Solve time: 5m15s Solution Let $f(x_1,\dots,x_n)$ be a Boolean function, and let $G(z)$ be its generating function in the sense of Exercise 25, so that $$ G(z)=\sum_{x\in{0,1}^n} f(x), z^{w(x)}, $$ where $w(x)=x_1+\cdots+x_n$ is the Hamming weight of $x$....
TAOCP 7.1.4 Exercise 38
Section 7.1.4: Binary Decision Diagrams Exercise 38. ▶ [ 27 ] [27] Let Is−1, . . . , I0 be branch instructions that define a nonconstant Boolean function f(x1, . . . , xn) as in (8) and (10). Design an algorithm that computes the status variables t1 . . . tn, where tj = +1, if f(x1, . . . , xn) = 1 whenever xj...
TAOCP 7.1.4 Exercise 37
Section 7.1.4: Binary Decision Diagrams Exercise 37. [ M20 ] [M20] (R. L. Rivest and J. Vuillemin, 1976.) A Boolean function f(x1, . . . , xn) is called evasive if every FBDD for f contains a downward path of length n. Let G(z) be the generating function for f, as in exercise 25. Prove that f is evasive if G(−1) ̸= 0. Verified: no Solve time: 4m34s Solution Let...
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 34
Section 7.1.4: Binary Decision Diagrams Exercise 34. [ M25 ] [M25] Specialize exercise 31 so that we can efficiently compute max{ max 1≤k≤n(w1x1 + · · · + wk−1xk−1 + w′ kxk + wk+1xk+1 + · · · + wnxn + w′′ k) | f(x) = 1} from the BDD of f, given 3n arbitrary weights (w1, . . . , wn, w′ 1, . . . , w′ n,...
TAOCP 7.1.4 Exercise 33
Section 7.1.4: Binary Decision Diagrams Exercise 33. ▶ [ M22 ] [M22] Specialize exercise 31 so that we can efficiently compute f(x)=1 (w1x1 + · · · + wnxn) and f(x)=1 (w1x1 + · · · + wnxn)2 from the BDD of a Boolean function f(x) = f(x1, . . . , xn). 7.1.4 BINARY DECISION DIAGRAMS 261 Verified: no Solve time: 2m48s Solution Let the BDD represent...
TAOCP 7.1.4 Exercise 32
Section 7.1.4: Binary Decision Diagrams Exercise 32. ▶ [ M20 ] [M20] What interpretations of ‘◦’, ‘•’, ‘⊥’, ‘⊤’, ‘¯xj’, and ‘xj’ will make the general algorithm of exercise 31 specialize to the algorithms of exercises 25, 26, 29, and 30? Verified: no Solve time: 1m50s Solution Exercise 31 describes a generic BDD evaluation scheme in which a function is computed by replacing each internal decision node labeled by variable...
TAOCP 7.1.4 Exercise 31
Section 7.1.4: Binary Decision Diagrams Exercise 31. [ M21 ] [M21] Modify Algorithm C so that it computes the fully elaborated truth table of f, formalizing the procedure by which (24) was obtained from Fig. 21. Verified: no Solve time: 4m36s Solution Let $f(x_1,\dots,x_n)$ be represented by an ordered reduced BDD with root node $r$. For each node $k$ in the BDD, write $V(k)$ for its variable index, and write...
TAOCP 7.1.4 Exercise 30
Section 7.1.4: Binary Decision Diagrams Exercise 30. ▶ [ M21 ] [M21] The reliability polynomial is the sum, over all solutions to f(x1, . . . , xn)=1, of contributions from all “minterms” (1 −p1)1−x1px1 1 . . . (1 −pn)1−xnpxn n . Explain how to find a solution x1 . . . xn whose contribution to the total reliability is maximum, given a BDD for f and a sequence...
TAOCP 7.1.4 Exercise 29
Section 7.1.4: Binary Decision Diagrams Exercise 29. [ HM20 ] [HM20] Modify Algorithm C so that it computes the reliability polynomial F(p) of exercise 28 and also its derivative F ′(p), given p and the BDD for f. Verified: no Solve time: 5m56s Solution Let $f$ be represented by a reduced ordered binary decision diagram, and let $F(p)$ denote the reliability polynomial under the specialization $p_1=\cdots=p_n=p$. For each node $v$...