Nowhere to go but out,

Nowhere to come but back.

— BEN KING, in The Sum of Life (c. 1893)

Lewis back-tracked the original route up the Missouri.

— LEWIS R. FREEMAN, in National Geographic Magazine (1928)

When you come to one legal road that’s blocked,

you back up and try another.

— PERRY MASON, in The Case of the Black-Eyed Blonde (1944)

7.2.2. Backtrack Programming

Now that we know how to generate simple combinatorial patterns such as tuples, permutations, combinations, partitions, and trees, we’re ready to tackle more exotic patterns that have subtler and less uniform structure. Instances of almost any desired pattern can be generated systematically, at least in principle, if we organize the search carefully. Such a method was christened “backtrack” by R. J. Walker in the 1950s, because it is basically a way to examine all fruitful possibilities while exiting gracefully from situations that have been fully explored.

Most of the patterns we shall deal with can be cast in a simple, general framework: We seek all sequences x 1 x 2 ...x n for which some property P n ( x 1 ,x 2 ,...,x n ) holds, where each item x k belongs to some given domain D k of integers. The backtrack method, in its most elementary form, involves the invention of intermediate “cutoff” properties P l ( x 1 ,...,x l ) for 1 ≤ l < n , such that

P l ( x 1 , ... , x l )   is true whenever ⁢ P l + 1 ( x 1 , ... , x l + 1 )   is true; ⁢ ( 1 )

P l ( x 1 , ... , x l )   is fairly easy to test, if ⁢ P l − 1 ( x 1 , ... , x l − 1 )   holds. ⁢ ( 2 )

(We assume that P 0 () is always true. Exercise 1 shows that all of the basic patterns studied in Section 7.2.1 can easily be formulated in terms of domains D k and cutoff properties P l .) Then we can proceed lexicographically as follows:

Algorithm B ( Basic backtrack ). Given domains D k and properties P l as above, this algorithm visits all sequences x 1 x 2 ...x n that satisfy P n ( x 1 ,x 2 ,...,x n ).

B1. [Initialize.] Set l ← 1, and initialize the data structures needed later.

B2. [Enter level l .] (Now P l − 1 ( x 1 ,...,x l − 1 ) holds.) If l > n , visit x 1 x 2 ...x n and go to B5. Otherwise set x l ← min D l , the smallest element of D l .

B3. [Try x l .] If P l ( x 1 ,...,x l ) holds, update the data structures to facilitate testing P l +1 , set l ← l + 1, and go to B2.

B4. [Try again.] If x l ≠ max D l , set x l to the next larger element of D l and return to B3.

B5. [Backtrack.] Set l ← l −1. If l > 0, downdate the data structures by undoing the changes recently made in step B3, and return to B4. (Otherwise stop.)

The main point is that if P l ( x 1 ,...,x l ) is false in step B3, we needn’t waste time trying to append any further values x l +1 ...x n . Thus we can often rule out huge regions of the space of all potential solutions. A second important point is that very little memory is needed, although there may be many, many solutions.

For example, let’s consider the classic problem of n queens : In how many ways can n queens be placed on an n × n board so that no two are in the same row, column, or diagonal? We can suppose that one queen is in each row, and that the queen in row k is in column x k , for 1 ≤ k ≤ n . Then each domain D k is {1, 2 ,...,n }; and P n ( x 1 ,...,x n ) is the condition that

x j ⁢ ≠ x k ⁢ ⁢                           and       | x k − x j | ≠ k − j ,        for 1 ≤ j < k ≤ n . ⁢ ( 3 )

(If x j \= x k and j < k , two queens are in the same column; if | x k − x j | = k − j , they’re in the same diagonal.)

This problem is easy to set up for Algorithm B , because we can let property P l ( x 1 ,...,x l ) be the same as ( 3 ) but restricted to 1 ≤ j < k ≤ l . Condition ( 1 ) is clear; and so is condition ( 2 ), because P l requires testing ( 3 ) only for k = l when P l − 1 is known. Notice that P 1 ( x 1 ) is always true in this example.

One of the best ways to learn about backtracking is to execute Algorithm B by hand in the special case n = 4 of the n queens problem: First we set x 1 ← 1. Then when l = 2 we find P 2 (1, 1) and P 2 (1, 2) false; hence we don’t get to l = 3 until trying x 2 ← 3. Then, however, we’re stuck, because P 3 (1, 3 ,x ) is false for 1 ≤ x ≤ 4. Backtracking to level 2, we now try x 2 ← 4; and this allows us to set x 3 ← 2. However, we’re stuck again, at level 4; and this time we must back up all the way to level 1, because there are no further valid choices at levels 3 and 2. The next choice x 1 ← 2 does, happily, lead to a solution without much further ado, namely x 1 x 2 x 3 x 4 = 2413. And one more solution (3142) turns up before the algorithm terminates.

The behavior of Algorithm B is nicely visualized as a tree structure, called a search tree or backtrack tree . For example, the backtrack tree for the four queens problem has just 17 nodes,

images

corresponding to the 17 times step B2 is performed. Here x l is shown as the label of an edge from level l − 1 to level l of the tree. (Level l of the algorithm actually corresponds to the tree’s level l − 1, because we’ve chosen to represent patterns using subscripts from 1 to n instead of from 0 to n −1 in this discussion.) The profile ( p 0 ,p 1 ,...,p n ) of this particular tree — the number of nodes at each level — is (1, 4, 6, 4, 2); and we see that the number of solutions, p n \= p 4 , is 2.

Figure 68 shows the corresponding tree when n = 8. This tree has 2057 nodes, distributed according to the profile (1, 8, 42, 140, 344, 568, 550, 312, 92). Thus the early cutoffs facilitated by backtracking have allowed us to find all 92 solutions by examining only 0.01% of the 8 8 = 16,777,216 possible sequences x 1 ... x 8 . (And 8 8 is only 0.38% of the ( 64 8 ) \= 4 , 426 , 165 , 368 ways to put eight queens on the board.)

images

Fig. 68. The problem of placing eight nonattacking queens has this backtrack tree.

Notice that, in this case, Algorithm B spends most of its time in the vicinity of level 5 below the root. Such behavior is typical: The tree for n = 16 queens has 1,141,190,303 nodes, and its profile is (1, 16, 210, 2236, 19688, 141812, 838816, 3998456, 15324708, 46358876, 108478966, 193892860, 260303408, 253897632, 171158018, 72002088, 14772512), concentrated near level 12.

Data structures

Backtrack programming is often used when a huge tree of possibilities needs to be examined. Thus we want to be able to test property P l as quickly as possible in step B3.

One way to implement Algorithm B for the n queens problem is to avoid auxiliary data structures and simply to make a bunch of sequential comparisons in that step: “Is x l − x j ∈{ j − l, 0 ,l − j } for some j < l ?” Assuming that we must access memory whenever referring to x j , given a trial value x l in a register, such an implementation performs approximately 112 billion memory accesses when n = 16; that’s about 98 mems per node.

We can do better by introducing three simple arrays. Property P l in ( 3 ) says essentially that the numbers x k are distinct, and so are the numbers x k + k , and so are the numbers x k − k . Therefore we can use auxiliary Boolean arrays a 1 ...a n , b 1 ...b 2 n − 1 , and c 1 ...c 2 n − 1 , where a j means ‘some x k \= j ’, b j means ‘some x k + k − 1 = j ’, and c j means ‘some x k − k + n = j ’. Those arrays are readily updated and downdated if we customize Algorithm B as follows:

B1*. [Initialize.] Set a 1 ...a n ← 0 ... 0, b 1 ...b 2 n − 1 ← 0 ... 0, c 1 ...c 2 n − 1 ← 0 ... 0, and l ← 1.

B2*. [Enter level l .] (Now P l − 1 ( x 1 ,...,x l − 1 ) holds.) If l > n , visit x 1 x 2 ...x n and go to B5*. Otherwise set t ← 1.

B3*. [Try t .] If a t \= 1 or b t + l − 1 = 1 or c t − l + n = 1, go to B4*. Otherwise set a t ← 1, b t + l − 1 ← 1, c t − l + n ← 1, x l ← t , l ← l + 1, and go to B2*.

B4*. [Try again.] If t < n , set t ← t + 1 and return to B3*.

B5*. [Backtrack.] Set l ← l − 1. If l > 0, set t ← x l , c t − l + n ← 0, b t + l − 1 ← 0, a t ← 0, and return to B4*. (Otherwise stop.)

Notice how step B5* neatly undoes the updates that step B3* had made, in the reverse order. Reverse order for downdating is typical of backtrack algorithms, although there is some flexibility; we could, for example, have restored a t before b t + l − 1 and c t − l + n , because those arrays are independent.

The auxiliary arrays a , b , c make it easy to test property P l at the beginning of step B3*, but we must also access memory when we update them and downdate them. Does that cost us more than it saves? Fortunately, no: The running time for n = 16 goes down to about 34 billion mems, roughly 30 mems per node.

Furthermore we could keep the bit vectors a , b , c entirely in registers, on a machine with 64-bit registers, assuming that n ≤ 32. Then there would be just two memory accesses per node, namely to store x l ← t and later to fetch t ← x l . However, quite a lot of in-register computation would become necessary.

Walker’s method

The 1950s-era programs of R. J. Walker organized backtracking in a somewhat different way. Instead of letting x l run through all elements of D l , he calculated and stored the set

S l ← { x ∈ D l |   P l ( x 1 , ... , x ι − 1 , x )   holds } ( 5 )

upon entry to each node at level l . This computation can often be done efficiently all at once, instead of piecemeal, because some cutoff properties make it possible to combine steps that would otherwise have to be repeated for each x ∈ D l . In essence, he used the following variant of Algorithm B :

Algorithm W ( Walker’s backtrack ). Given domains D k and cutoffs P l as above, this algorithm visits all sequences x 1 x 2 ...x n that satisfy P n ( x 1 ,x 2 ,...,x n ).

W1. [Initialize.] Set l ← 1, and initialize the data structures needed later.

W2. [Enter level l .] (Now P l − 1 ( x 1 ,...,x l − 1 ) holds.) If l > n , visit x 1 x 2 ...x n and go to W4. Otherwise determine the set S l as in ( 5 ).

W3. [Try to advance.] If S l is nonempty, set x l ← min S l , update the data structures to facilitate computing S l +1 , set l ← l + 1, and go to W2.

W4. [Backtrack.] Set l ← l −1. If l > 0, downdate by undoing the changes made in step W3, set S l ← S l \ x l , and retreat to W3. (Otherwise stop.)

Walker applied this method to the n queens problem by computing S l \= U \ A l \ B l \ C l , where U = D l \= {1 ,...,n } and

A l \= { x j | 1 ≤ j < l } ,   B l \= { x j + j − l | 1 ≤ j < l } ,   C l \= { x j − j + l | 1 ≤ j < l } . ( 6 )

He represented these auxiliary sets by bit vectors a , b , c , analogous to (but different from) the bit vectors of Algorithm B * above. Exercise 10 shows that the updating in step W3 is easy, using bitwise operations on n -bit numbers; furthermore, no downdating is needed in step W4. The corresponding run time when n = 16 turns out to be just 9.1 gigamems, or 8 mems per node.

Let Q ( n ) be the number of solutions to the n queens problem. Then we have

n

=

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Q(n)

=

1

1

0

0

2

10

4

40

92

352

724

2680

14200

73712

365596

2279184

14772512

and the values for n ≤ 11 were computed independently by several people during the nineteenth century. Small cases were relatively easy; but when T. B. Sprague had finished computing Q (11) he remarked that “This was a very heavy piece of work, and occupied most of my leisure time for several months. ... It will, I imagine, be scarcely possible to obtain results for larger boards, unless a number of persons co-operate in the work.” [See Proc. Edinburgh Math. Soc. 17 (1899), 43–68; Sprague was the leading actuary of his day.] Nevertheless, H. Onnen went on to evaluate Q (12) = 14,200 — an astonishing feat of hand calculation — in 1910. [See W. Ahrens, Math. Unterhaltungen und Spiele 2 , second edition (1918), 344.]

All of these hard-won results were confirmed in 1960 by R. J. Walker, using the SWAC computer at UCLA and the method of exercise 10 . Walker also computed Q (13); but he couldn’t go any further with the machine available to him at the time. The next step, Q (14), was computed by Michael D. Kennedy at the University of Tennessee in 1963, commandeering an IBM 1620 for 120 hours. S. R. Bunch evaluated Q (15) in 1974 at the University of Illinois, using about two hours on an IBM System 360-75; then J. R. Bitner found Q (16) after about three hours on the same computer, but with an improved method.

Computers and algorithms have continued to get better, of course, and such results are now obtained almost instantly. Hence larger and larger values of n lie at the frontier. The whopping value Q (27) = 234,907,967,154,122,528, found at T. U. Dresden in 2016 by Thomas B. Preußer and Matthias R. Engelhardt, probably won’t be exceeded for awhile! [See J. Signal Proc. Syst. 88 88 (2017), 185–201. This distributed computation occupied a dynamic cluster of diverse FPGA devices for 383 days; those devices provided a total peak of more than 7000 custom-designed hardware solvers to handle 2,024,110,796 independent subproblems.]

Permutations and Langford pairs

Every solution x 1 ...x n to the n queens problem is a permutation of {1 ,...,n }, and many other problems are permutation-based. Indeed, we’ve already seen Algorithm 7.2.1.2X, which is an elegant backtrack procedure specifically designed for special kinds of permutations. When that algorithm begins to choose the value of x l , it makes all of the appropriate elements {1, 2 ,...,n }\{ x 1 ,...,x l − 1 } conveniently accessible in a linked list.

We can get further insight into such data structures by returning to the problem of Langford pairs, which was discussed at the very beginning of Chapter 7 . That problem can be reformulated as the task of finding all permutations of {1, 2 ,...,n }∪{−1, −2 ,..., − n } with the property that

x j \= k ⁢                   implies   x j + k + 1 \= − k ,                                         for 1 ≤ j ≤ 2 n ⁢     and 1 ≤ k ≤ n . ( 7 )

For example, when n = 4 there are two solutions, namely 234 2 ¯ 1 3 ¯ 1 ¯ 4 ¯ and 413 1 ¯ 2 4 ¯ 3 ¯ 2 ¯ . (As usual we find it convenient to write 1 ¯ for − 1 , 2 ¯ for −2, etc.) Notice that if x = x 1 x 2 ... x 2 n is a solution, so is its “ dual” − x R = (− x 2 n ) ... (− x 2 )(− x 1 ).

Here’s a Langford-inspired adaptation of Algorithm 7.2.1.2X, with the former notation modified slightly to match Algorithms B and W : We want to maintain pointers p 0 p 1 ...p n such that, if the positive integers not already present in x 1 ...x l − 1 are k 1 < k 2 < ... < k t when we’re choosing x l , we have the linked list

p 0 \= k 1 , p k 1 \= k 2 , ... , p k t − 1 \= k t ,   p k t \= 0. ( 8 )

Such a condition turns out to be easy to maintain.

Algorithm L ( Langford pairs ). This algorithm visits all solutions x 1 ... x 2 n to ( 7 ) in lexicographic order, using pointers p 0 p 1 ... p n that satisfy ( 8 ), and also using an auxiliary array y 1 ... y 2 n for backtracking.

L1. [Initialize.] Set x 1 ... x 2 n ← 0 ... 0, p k ← k +1 for 0 ≤ k < n , p n ← 0, l ← 1.

L2. [Enter level l .] Set k ← p 0 . If k = 0, visit x 1 x 2 ... x 2 n and go to L5. Otherwise set j ← 0, and while x l < 0 set l ← l +1.

L3. [Try x l \= k .] (At this point we have k = p j .) If l + k +1 > 2 n , go to L5. Otherwise, if x l + k +1 = 0, set x l ← k , x l + k +1 ← – k , y l ← j , p j ← p k , l ← l + 1, and return to L2.

L4. [Try again.] (We’ve found all solutions that begin with x 1 ... x l –1 k or something smaller.) Set j ← k and k ← p j , then go to L3 if k ≠ 0.

L5. [ Backtrack.] Set l ← l – 1. If l > 0 do the following: While x l < 0, set l ← l – 1. Then set k ← x l , x l ← 0, x l + k +1 ← 0, j ← y l , p j ← k , and go back to L4. Otherwise terminate the algorithm.

Careful study of these steps will reveal how everything fits together nicely. Notice that, for example, step L3 removes k from the linked list ( 8 ) by simply setting p j ← p k . That step also sets x l + k +1 ← – k , in accordance with ( 7 ), so that we can skip over position l + k + 1 when we encounter it later in step L2.

The main point of Algorithm L is the somewhat subtle way in which step L5 undoes the deletion operation by setting p j ← k . The pointer p k still retains the appropriate link to the next element in the list, because p k has not been changed by any of the intervening updates . (Think about it.) This is the germ of an idea called “ dancing links” that we will explore in Section 7.2.2.1 .

To draw the search tree corresponding to a run of Algorithm L , we can label the edges with the positive choices of x l as we did in ( 4 ), while labeling the nodes with any previously set negative values that are passed over in step L2. For instance the tree for n = 4 is

images

Solutions appear at depth n in this tree, even though they involve 2 n values x 1 x 2 ... x 2 n .

Algorithm L sometimes makes false starts and doesn’t realize the problem until probing further than necessary. Notice that the value x l \= k can appear only when l + k + 1 ≤ 2 n ; hence if we haven’t seen k by the time l reaches 2 n – k – 1, we’re forced to choose x l \= k . For example, the branch 12 1 ¯ in ( 9 ) needn’t be pursued, because 4 must appear in { x 1 ,x 2 ,x 3 }. Exercise 20 explains how to incorporate this cutoff principle into Algorithm L . When n = 17, it reduces the number of nodes in the search tree from 1.29 trillion to 330 billion, and reduces the running time from 25.0 teramems to 8.1 teramems. (The amount of work has gone up from 19.4 mems per node to 24.4 mems per node, because of the extra tests for cutoffs, yet there’s a significant overall reduction.)

Furthermore, we can “break the symmetry” by ensuring that we don’t consider both a solution and its dual. This idea, exploited in exercise 21 , reduces the search tree to just 160 billion nodes and costs just 3.94 teramems — that’s 24.6 mems per node.

Word rectangles

Let’s look next at a problem where the search domains D l are much larger. An m × n word rectangle is an array of n -letter words * whose columns are m -letter words. For example,

* Whenever five-letter words are used in the examples of this book, they’re taken from the 5757 Stanford GraphBase words as explained at the beginning of Chapter 7 . Words of other lengths are taken from The Official SCRABBLE ® Players Dictionary , fourth edition (Hasbro, 2005), because those words have been incorporated into many widely available computer games. Such words have been ranked according to the British National Corpus of 2007 — where ‘the’ occurs 5,405,633 times and the next-most common word, ‘ of ’, occurs roughly half as often (3,021,525). The OSPD4 list includes respectively (101, 1004, 4002, 8887, 15727, 23958, 29718, 29130, 22314, 16161, 11412) words of lengths (2, 3, ..., 12), of which (97, 771, 2451, 4474, 6910, 8852, 9205, 8225, 6626, 4642, 3061) occur at least six times in the British National Corpus.

S T A T U S L O W E S T U T O P I A M A K I N G S L E D G E ( 10 )

is a 5 × 6 word rectangle whose columns all belong to WORDS (5757), the collection of 5-letter words in the Stanford GraphBase. To find such patterns, we can suppose that column l contains the x l th most common 5-letter word, where 1 ≤ x l ≤ 5757 for 1 ≤ l ≤ 6; hence there are 5757 6 = 36,406,369,848,837,732,146,649 ways to choose the columns. In ( 10 ) we have x 1 ... x 6 = 1446 185 1021 2537 66 255. Of course very few of those choices will yield suitable rows; but backtracking will hopefully help us to find all solutions in a reasonable amount of time.

We can set this problem up for Algorithm B by storing the n -letter words in a trie (see Section 6.3), with one trie node of size 26 for each l -letter prefix of a legitimate word, 0 ≤ l ≤ n .

For example, such a trie for n = 6 represents 15727 words with 23667 nodes. The prefix ST corresponds to node number 260, whose 26 entries are

( 484 , 0 , 0 , 0 , 1589 , 0 , 0 , 0 , 2609 , 0 , 0 , 0 , 0 , 0 , 1280 , 0 , 0 , 251 , 0 , 0 , 563 , 0 , 0 , 0 , 1621 , 0 ) ; ( 11 )

this means that STA is node 484, STE is node 1589, ..., STY is node 1621, and there are no 6-letter words beginning with STB , STC , ..., STX , STZ . A slightly different convention is used for prefixes of length n – 1; for example, the entries for node 580, ‘ CORNE ’, are

( 3879 , 0 , 0 , 3878 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 9602 , 0 , 0 , 0 , 0 , 0 , 171 , 0 , 5013 , 0 , 0 , 0 , 0 , 0 , 0 ) , ( 12 )

meaning that CORNEA , CORNED , CORNEL , CORNER , and CORNET are ranked 3879, 3878, 9602, 171, and 5013 in the list of 6-letter words.

Suppose x 1 and x 2 specify the 5-letter column- words SLUMS and TOTAL as in ( 10 ). Then the trie tells us that the next column-word x 3 must have the form c 1 c 2 c 3 c 4 c 5 where c 1 ∈ { A , E , I , O , R , U , Y }, c 2 ∉ { E , H , J , K , Y , Z }, c 3 ∈ { E , M , O , T }, c 4 ∉ { A , B , O }, and c 5 ∈ { A , E , I , O , U , Y }. (There are 221 such words.)

Let a l 1 ... a lm be the trie nodes corresponding to the prefixes of the first l columns of a partial solution to the word rectangle problem. This auxiliary array enables Algorithm B to find all solutions, as explained in exercise 24 . It turns out that there are exactly 625,415 valid 5 × 6 word rectangles, according to our conventions; and the method of exercise 24 needs about 19 teramems of computation to find them all. In fact, the profile of the search tree is

( 1 , 5757 ;   2458830 ,   360728099 ,   579940198 ,   29621728 ,   625415 ) , ( 13 )

indicating for example that just 360,728,099 of the 5757 3 = 190,804,533,093 choices for x 1 x 2 x 3 will lead to valid prefixes of 6-letter words.

With care, exercise 24 ’s running time can be significantly decreased, once we realize that every node of the search tree for 1 ≤ l ≤ n requires testing 5757 possibilities for x l in step B3. If we build a more elaborate data structure for the 5-letter words, so that it becomes easy to run through all words that have a specific letter in a specific position, we can refine the algorithm so that the average number of possibilities per level that need to be investigated becomes only

( 5757.0 ,   1697.9 ⁢   ,   844.1 ,   273.5 ,   153.5 ,   100.8 ) ; ( 14 )

the total running time then drops to 1.15 teramems. Exercise 25 has the details. And exercise 28 discusses a method that’s faster yet.

Commafree codes

Our next example deals entirely with four -letter words. But it’s not obscene; it’s an intriguing question of coding theory. The problem is to find a set of four-letter codewords that can be decoded even if we don’t put spaces or other delimiters between them. If we take any message that’s formed from strings of the set by simply concatenating them together, likethis , and if we look at any seven consecutive letters ... x 1 x 2 x 3 x 4 x 5 x 6 x 7 ..., exactly one of the four-letter substrings x 1 x 2 x 3 x 4 , x 2 x 3 x 4 x 5 , x 3 x 4 x 5 x 6 , x 4 x 5 x 6 x 7 will be a codeword. Equivalently, if x 1 x 2 x 3 x 4 and x 5 x 6 x 7 x 8 are codewords, then x 2 x 3 x 4 x 5 and x 3 x 4 x 5 x 6 and x 4 x 5 x 6 x 7 aren’t. (For example, iket isn’t.) Such a set is called a “commafree code” or a “ self-synchronizing block code” of length four.

Commafree codes were introduced by F. H. C. Crick, J. S. Griffith, and L. E. Orgel [ Proc. National Acad. Sci. 43 (1957), 416–421], and studied further by S. W. Golomb, B. Gordon, and L. R. Welch [ Canadian Journal of Mathematics 10 (1958), 202–209], who considered the general case of m -letter alphabets and n- letter words. They constructed optimum commafree codes for all m when n = 2, 3, 5, 7, 9, 11, 13, and 15; and optimum codes for all m were subsequently found also for n = 17, 19, 21, ... (see exercise 37 ). We will focus our attention on the four-letter case here ( n = 4), partly because that case is still very far from being resolved, but mostly because the task of finding such codes is especially instructive. Indeed, our discussion will lead us naturally to an understanding of several significant techniques that are important for backtrack programming in general.

To begin, we can see immediately that a commafree codeword cannot be “ periodic,” like dodo or gaga . Such a string already appears within two adjacent copies of itself. Thus we’re restricted to aperiodic strings like item , of which there are m 4 – m 2 . Notice further that if item has been chosen, we aren’t allowed to include any of its cyclic shifts temi , emit , or mite , because they all appear within itemitem . Hence the maximum number of codewords in our commafree code cannot exceed ( m 4 – m 2 ) / 4.

For example, consider the binary case, m = 2, when this maximum is 3. Can we choose three four-bit “words,” one from each of the cyclic classes

[ 0001 ] \= { 0001 ,   0010 ,   0100 ,   1000 } , [ 0011 ] \= { 0011 ,   0110 ,   1100 ,   1001 } , [ 0111 ] \= { 0111 ,   1110 ,   1101 ,   1011 } , ( 15 )

so that the resulting code is commafree? Yes: One solution in this case is simply to choose the smallest word in each class, namely 0001, 0011, and 0111. (Alert readers will recall that we studied the smallest word in the cyclic class of any aperiodic string in Section 7.2.1.1, where such words were called prime strings and where some of the remarkable properties of prime strings were proved.)

That trick doesn’t work when m = 3, however, when there are (81 – 9) / 4 = 18 cyclic classes. Then we cannot include 1112 after we’ve chosen 0001 and 0011. Indeed, a code that contains 0001 and 1112 can’t contain either 0011 or 0111.

We could systematically backtrack through 18 levels, choosing x 1 in [0001] and x 2 in [0011], etc., and rejecting each x l as in Algorithm B whenever we discover that { x 1 ,x 2 ,...,x l } isn’t commafree. For example, if x 1 = 0010 and we try x 2 = 1001, this approach would backtrack because x 1 occurs inside x 2 x 1 .

But a naïve strategy of that kind, which recognizes failure only after a bad choice has been made, can be vastly improved. If we had been clever enough, we could have looked a little bit ahead, and never even considered the choice x 2 = 1001 in the first place. Indeed, after choosing x 1 = 0010, we can automatically exclude all further words of the form *001, such as 2001 when m ≥ 3 and 3001 when m ≥ 4.

Even better pruning occurs if, for example, we’ve chosen x 1 = 0001 and x 2 = 0011. Then we can immediately rule out all words of the forms 1*** or ***0, because x 1 1*** includes x 2 and ***0 x 2 includes x 1 . Already we could then deduce, in the case m ≥ 3, that classes [0002], [0021], [0111], [0211], and [1112] must be represented by 0002, 0021, 0111, 0211, and 2111, respectively; each of the other three possibilities in those classes has been wiped out!

Thus we see the desirability of a lookahead mechanism.

Dynamic ordering of choices

Furthermore, we can see from this example that it’s not always good to choose x 1 , then x 2 , then x 3 , and so on when trying to satisfy a general property P n ( x 1 ,x 2 ,...,x n ) in the setting of Algorithm B . Maybe the search tree will be much smaller if we first choose x 5 , say, and then turn next to some other x j , depending on the particular value of x 5 that was selected. Some orderings might have much better cutoff properties than others, and every branch of the tree is free to choose its variables in any desired order.

Indeed, our commafree coding problem for ternary 4-tuples doesn’t dictate any particular ordering of the 18 classes that would be likely to keep the search tree small. Therefore, instead of calling those choices x 1 , x 2 , ..., x 18 , it’s better to identify them by the various class names, namely x 0001 , x 0002 , x 0011 , x 0012 , x 0021 , x 0022 , x 0102 , x 0111 , x 0112 , x 0121 , x 0122 , x 0211 , x 0212 , x 0221 , x 0222 , x 1112 , x 1122 , x 1222 . (Algorithm 7.2.1.1F is a good way to generate those names.) At every node of the search tree we then can choose a convenient variable on which to branch, based on previous choices. After beginning with x 0001 ← 0001 at level 1 we might decide to try x 0011 ← 0011 at level 2; and then, as we’ve seen, the choices x 0002 ← 0002, x 0021 ← 0021, x 0111 ← 0111, x 0211 ← 0211, and x 1112 ← 2111 are forced, so we should make them at levels 3 through 7.

Furthermore, after those forced moves are made, it turns out that they don’t force any others. But only two choices for x 0012 will remain, while x 0122 will have three. Therefore it will probably be wiser to branch on x 0012 rather than on x 0122 at level 8. (Incidentally, it also turns out that there is no commafree code of length ( m 4 – m 2 ) / 4 with x 0001 = 0001 and x 0011 = 0011, except when m = 2.)

It’s easy to adapt Algorithms B and W to allow dynamic ordering. Every node of the search tree can be given a “ frame” in which we record the variable being set and the choice that was made. This choice of variable and value can be called a “move” made by the backtrack procedure.

Dynamic ordering can be helpful also after backtracking has taken place. If we continue the example above, where x 0001 = 0001 and we’ve explored all cases in which x 0011 = 0011, we aren’t obliged to continue by trying another value for x 0011 . We do want to remember that 0011 should no longer be considered legal, until x 0001 changes; but we could decide to explore next a case such as x 0002 ← 2000 at level 2. In fact, x 0002 = 2000 is quickly seen to be impossible in the presence of 0001 (see exercise 39 ). An even more efficient choice at level 2 turns out to be x 0012 ← 0012, because that branch immediately forces x 0002 ← 0002, x 0022 ← 0022, x 0122 ← 0122, x 0222 ← 0222, x 1222 ← 1222, and x 0011 ← 1001.

Sequential allocation redux

The choice of a variable and value on which to branch is a delicate tradeoff. We don’t want to devote more time to planning than we’ll save by having a good plan.

If we’re going to benefit from dynamic ordering, we’ll need efficient data structures that will lead to good decisions without much deliberation. On the other hand, elaborate data structures need to be updated whenever we branch to a new level, and they need to be downdated whenever we return from that level. Algorithm L illustrates an efficient mechanism based on linked lists; but sequentially allocated lists are often even more appealing, because they are cache-friendly and they involve fewer accesses to memory.

Assume then that we wish to represent a set of items as an unordered sequential list. The list begins in a cell of memory pointed to by HEAD , and TAIL points just beyond the end of the list. For example,

images

is one way to represent the set {1, 3, 4, 9}. The number of items currently in the set is TAIL – HEAD ; thus TAIL = HEAD if and only if the list is empty. If we wish to insert a new item x , knowing that x isn’t already present, we simply set

MEM[TAIL] ←   x ,                 TAIL   ←   TAIL +1. ( 17 )

Conversely, if HEAD ≤ P < TAIL , we can easily delete MEM[P] :

TAIL ←   TAIL − 1 ;         if P ≠   TAIL ;  set   MEM[P] ←   MEM[TAIL]. ( 18 )

(We’ve tacitly assumed in ( 17 ) that MEM[TAIL] is available for use whenever a new item is inserted. Otherwise we would have had to test for memory overflow.)

We can’t delete an item from a list without knowing its MEM location. Thus we will often want to maintain an “ inverse list,” assuming that all items x lie in the range 0 ≤ x < M . For example, ( 16 ) becomes the following, if M = 10:

images

(Shaded cells have undefined contents.) With this setup, insertion ( 17 ) becomes

MEM[TAIL] ←   x ,         MEM[IHEAD + x ] ←   TAIL,   TAIL ←   TAIL + 1, ( 20 )

and TAIL will never exceed HEAD + M . Similarly, deletion of x becomes

P ←   MEM[IHEAD + x ][TAIL ← TAIL − 1; if P ≠ TAIL , set   y ⁢ ←   MEM [ TAIL] , MEM[P] ←   y ,   MEM[IHEAD + y ] ←   P. ( 21 )

For example, after deleting ‘9’ from ( 19 ) we would obtain this:

images

In more elaborate situations we also want to test whether or not a given item x is present. If so, we can keep more information in the inverse list. A particularly useful variation arises when the list that begins at IHEAD contains a complete permutation of the values { HEAD , HEAD + 1, ..., HEAD + M – 1}, and the memory cells beginning at HEAD contain the inverse permutation — although only the first TAIL – HEAD elements of that list are considered to be “active.”

For example, in our commafree code problem with m = 3, we can begin by putting items representing the M = 18 cycle classes [0001], [0002], ..., [1222] into memory cells HEAD through HEAD + 17. Initially they’re all active, with TAIL = HEAD + 18 and MEM[IHEAD + c ] = HEAD + c for 0 ≤ c < 18. Then whenever we decide to choose a codeword for class c , we delete c from the active list by using a souped-up version of ( 21 ) that maintains full permutations:

P ← MEM [ IHEAD + c ] ,       TAIL ←   TAIL − 1 ;                   if P ≠ TAIL , set, y ← MEM[TAIL] ,     MEM[TAIL] ← c ,         MEM[P] ←   y , ⁢                                                                                                                   MEM[IHEAD + c ] ← TAIL ,     MEM[IHEAD + y ] ←   P. ( 23 )

Later on, after backtracking to a state where we once again want c to be considered active, we simply set TAIL ← TAIL + 1, because c will already be in place! (This data-structuring technique has been called a sparse-set representation ; see P. Briggs and L. Torczon, ACM Letters Prog. Lang. and Syst. 2 (1993), 59–69.)

Lists for the commafree problem

The task of finding all four-letter comma-free codes of maximum length is not difficult when m = 3 and only 18 cycle classes are involved. But it already becomes challenging when m = 4, because we must then deal with (4 4 – 4 2 ) / 4 = 60 classes. Therefore we’ll want to give it some careful thought as we try to set it up for backtracking.

The example scenarios for m = 3 considered above suggest that we’ll repeatedly want to know the answers to questions such as, “How many words of the form 02** are still available for selection as codewords?” Redundant data structures, oriented to queries of that kind, appear to be needed. Fortunately, we shall see that there’s a nice way to provide them, using sequential lists as in ( 19 )–( 23 ).

In Algorithm C below, each of the m 4 four-letter words is given one of three possible states during the search for commafree codes. A word is green if it’s part of the current set of tentative codewords. It is red if it’s not currently a candidate for such status, either because it is incompatible with the existing green words or because the algorithm has already examined all scenarios in which it is green in their presence. Every other word is blue , and sort of in limbo; the algorithm might or might not decide to make it red or green. All words are initially blue — except for the m 2 periodic words, which are permanently red.

We’ll use the Greek letter α to stand for the integer value of a four-letter word x in radix m . For example, if m = 3 and if x is the word 0102, then α = (0102) 3 = 11. The current state of word x is kept in MEM[ α ] , using one of the arbitrary internal codes 2 ( GREEN ), 0 ( RED ), or 1 ( BLUE ).

The most important feature of the algorithm is that every blue word x = x 1 x 2 x 3 x 4 is potentially present in seven different lists , called P1( x ) , P2( x ) , P3( x ) , S1( x ) , S2( x ) , S3( x ) , and CL( x ) , where

  • P1( x ) , P2( x ) , P3( x ) are the blue words matching x 1 ***, x 1 x 2 **, x 1 x 2 x 3 *;

  • S1( x ) , S2( x ) , S3( x ) are the blue words matching *** x 4 , ** x 3 x 4 , * x 2 x 3 x 4 ;

  • CL( x ) hosts the blue words in { x 1 x 2 x 3 x 4 ,x 2 x 3 x 4 x 1 ,x 3 x 4 x 1 x 2 ,x 4 x 1 x 2 x 3 }.

These seven lists begin respectively in MEM locations P1OFF + p 1 (α), P2OFF + p 2 (α), P3OFF + p 3 (α), S1OFF + s 1 (α), S2OFF + s 2 (α), S3OFF + s 3 (α), and CLOFF + 4 cl (α); here ( P1OFF , P2OFF , P3OFF , S1OFF , S2OFF , S3OFF , CLOFF ) are respectively (2 m 4 , 5 m 4 , 8 m 4 , 11 m 4 , 14 m 4 , 17 m 4 , 20 m 4 ). We define p 1 (( x 1 x 2 x 3 x 4 ) m )= ( x 1 000) m , p 2 (( x 1 x 2 x 3 x 4 ) m ) = ( x 1 x 2 00) m , p 3 (( x 1 x 2 x 3 x 4 ) m ) = ( x 1 x 2 x 3 0) m ; and s 1 (( x 1 x 2 x 3 x 4 ) m )=( x 4 000) m , s 2 (( x 1 x 2 x 3 x 4 ) m )=( x 3 x 4 00) m , s 3 (( x 1 x 2 x 3 x 4 ) m )= ( x 2 x 3 x 4 0) m ; and finally cl (( x 1 x 2 x 3 x 4 ) m ) is an internal number, between 0 and ( m 4 – m 2 ) / 4–1, assigned to each class. The seven MEM locations where x appears in these seven lists are respectively kept in inverse lists that begin in MEM locations P1OFF – m 4 + α, P2OFF – m 4 + α, ..., CLOFF – m 4 + α. And the TAIL pointers, which indicate the current list sizes as in ( 19 )–( 23 ), are respectively kept in MEM locations P1OFF + m 4 + p 1 (α), P2OFF + m 4 + p 2 (α), ..., S3OFF + m 4 + s 3 (α), CLOFF + m 4 +4 cl (α). (Whew; got that?)

This vast apparatus, which occupies 22 m 4 cells of MEM , is illustrated in Table 1 , at the beginning of the computation for the case m = 2. Fortunately it’s not really as complicated as it may seem at first. Nor is it especially vast: After all, 22 m 4 is only 13,750 when m = 5.

Table 1
LISTS USED BY ALGORITHM C ( m = 2), ENTERING LEVEL 1

 

0

1

2

3

4

5

6

7

8

9

a

b

c

d

e

f

0

RED

BLUE

BLUE

BLUE

RED

RED

BLUE

BLUE

RED

BLUE

RED

BLUE

BLUE

BLUE

BLUE

RED

 

10

 

20

21

22

 

 

23

24

 

29

 

2c

28

2b

2a

 

 

20

0001

0010

0011

0110

0111

 

 

 

1100

1001

1110

1101

1011

 

 

 

P1

30

25

 

 

 

 

 

 

 

2d

 

 

 

 

 

 

 

 

40

 

50

51

52

 

 

54

55

 

58

 

59

5c

5e

5d

 

 

50

0001

0010

0011

 

0110

0111

 

 

1001

1011

 

 

1100

1110

1101

 

P2

60

53

 

 

 

56

 

 

 

5a

 

 

 

5f

 

 

 

 

70

 

80

82

83

 

 

86

87

 

88

 

8a

8c

8d

8e

 

 

80

0001

 

0010

0011

 

 

0110

0111

1001

 

1011

 

1100

1101

1110

 

P3

90

81

 

84

 

84

 

88

 

89

 

8b

 

8e

 

8f

 

 

a0

 

b8

b0

b9

 

 

b1

bb

 

ba

 

bd

b2

bc

b3

 

 

b0

0010

0110

1100

1110

 

 

 

 

0001

0011

1001

0111

1101

1011

 

 

S1

c0

b4

 

 

 

 

 

 

 

be

 

 

 

 

 

 

 

 

d0

 

e4

e8

ec

 

 

e9

ed

 

e5

 

ee

e0

e6

ea

 

 

e0

1100

 

 

 

0001

1001

1101

 

0010

0110

1110

 

0011

0111

1011

 

S2

f0

e1

 

 

 

e7

 

 

 

eb

 

 

 

ef

 

 

 

 

100

 

112

114

116

 

 

11c

11e

 

113

 

117

118

11a

11d

 

 

110

 

 

0001

1001

0010

 

0011

1011

1100

 

1101

 

0110

1110

0111

 

S3

120

110

 

114

 

115

 

118

 

119

 

11b

 

11e

 

11f

 

 

130

 

140

141

144

 

 

145

148

 

147

 

14b

146

14a

149

 

 

140

0001

0010

 

 

0011

0110

1100

1001

0111

1110

1101

1011

 

 

 

 

CL

150

142

 

 

 

148

 

 

 

14c

 

 

 

 

 

 

 

 

This table shows MEM locations 000 through 15f , using hexadecimal notation. (For example, MEM[4d] = 5e ; see exercise 41 .) Blank entries are unused by the algorithm.

(A close inspection of Table 1 reveals incidentally that the words 0100 and 1000 have been colored red, not blue. That’s because we can assume without loss of generality that class [0001] is represented either by 0001 or by 0010. The other two cases are covered by left-right reflection of all codewords.)

Algorithm C finds these lists invaluable when it is deciding where next to branch. But it has no further use for a list in which one of the items has become green. Therefore it declares such lists “closed”; and it saves most of the work of list maintenance by updating only the lists that remain open. A closed list is represented internally by setting its TAIL pointer to HEAD – 1.

For example, Table 2 shows how the lists in MEM will have changed just after x = 0010 has been chosen to be a tentative codeword. The elements {0001, 0010, 0011, 0110, 0111} of P1( x ) are effectively hidden, because the tail pointer MEM[30] = 1f = 20 – 1 marks that list as closed. (Those list elements actually do still appear in MEM locations 20 through 24 , just as they did in Table 1 . But there’s no need to look at that list while any word of the form 0*** is green.)

A general mechanism for doing and undoing

We’re almost ready to finalize the details of Algorithm C and to get on with the search for commafree codes, but a big problem still remains: The state of computation at every level of the search involves all of the marvelous lists that we’ve just specified, and those lists aren’t tiny. They occupy more than 5000 cells of MEM when m = 4, and they can change substantially from level to level.

We could make a new copy of the entire state, whenever we advance to a new node of the search tree. But that’s a bad idea, because we don’t want to perform thousands of memory accesses per node. A much better strategy would be to stick with a single instance of MEM , and to update and downdate the lists as the search progresses, if we could only think of a simple way to do that.

And we’re in luck: There is such a way, first formulated by R. W. Floyd in his classic paper “Nondeterministic algorithms” [ JACM 14 (1967), 636–644]. Floyd’s original idea, which required a special compiler to generate forward and backward versions of every program step, can in fact be greatly simplified when all of the changes in state are confined to a single MEM array. All we need to do is to replace every assignment operation of the form ‘ MEM[ a ] ← v ’ by the slightly more cumbersome operation

store ( a , v ) :                     Set     UNDO[ u ] ←   ( a , MEM [ a ] ) ,         MEM[ a ] ←   v , and   u ←   u + 1. ( 24 )

Here UNDO is a sequential stack that holds (address, value) pairs; in our application we could say ‘ UNDO[ u ] ← ( a ≪ 16) + MEM[ a ] ’, because the cell addresses and values never exceed 16 bits. Of course we’ll also need to check that the stack pointer u doesn’t get too large, if the number of assignments has no a priori limit.

Later on, when we want to undo all changes to MEM that were made after the time when u had reached a particular value u 0 , we simply do this:

unstore ( u 0 ) :           While     u ⁢ >   u 0 ,       set   u ⁢   ←   u ⁢ − 1 ,                                                                ( a , v ) ← UNDO [ u ] ,           and MEM [ a ] ← v . ( 25 )

In our application the unstacking operation ‘( a, v ) ← UNDO[ u ] ’ here could be implemented by saying ‘ a ← UNDO[ u ] ≫ 16, v ← UNDO[ u ] & # ffff ’.

A useful refinement of this reversible-memory technique is often advantageous, based on the idea of “ stamping” that is part of the folklore of programming. It puts only one item on the UNDO stack when the same memory address is updated more than once in the same round.

stor e ⁢   ( a , v ) : If STAMP [ a ] ≠ σ , set STAMP[ a ] ← σ ,            UNDO [ u ] ← ( a ,   MEM [ a ] ) ,   and    u ⁢   ←   u + 1. Then set MEM[ a ] ← v . ( 26 )

Here STAMP is an array with one entry for each address in MEM . It’s initially all zero, and σ is initially 1. Whenever we come to a fallback point, where the current stack pointer will be remembered as the value u 0 for some future undoing, we “bump” the current stamp by setting σ ← σ + 1. Then ( 26 ) will continue to do the right thing. (In programs that run for a long time, we must be careful when integer overflow causes σ to be bumped to zero; see exercise 43 .)

Notice that the combination of ( 24 ) and ( 25 ) will perform five memory accesses for each assignment and its undoing. The combination of ( 26 ) and ( 25 ) will cost seven mems for the first assignment to MEM[ a ] , but only two mems for every subsequent assignment to the same address. So ( 26 ) wins, if multiple assignments exceed one-time-only assignments.

Backtracking through commafree codes

OK, we’re now equipped with enough basic knowhow to write a pretty good backtrack program for the problem of generating all commafree four-letter codes.

Algorithm C below incorporates one more key idea, which is a lookahead mechanism that is specific to commafree backtracking; we’ll call it the “ poison list.” Every item on the poison list is a pair, consisting of a suffix and a prefix that the commafree rule forbids from occurring together. Every green word x 1 x 2 x 3 x 4 — that is, every word that will be a final codeword in the current branch of our backtrack search — contributes three items to the poison list, namely

( * x 1 x 2 x 3 ,   x 4 ) ,           ( * * x 1 x 2 , x 3 x 4 * * ) ,                   and    ( * * * x 1 ,   x 2 x 3 x 4 * ) . ( 27 )

If there’s a green word on both sides of a poison list entry, we’re dead: The commafree condition fails, and we mustn’t proceed. If there’s a green word on one side but not the other, we can kill off all blue words on the other side by making them red. And if either side of a poison list entry corresponds to an empty list, we can remove this entry from the poison list because it will never affect the outcome. (Blue words become red or green, but red words stay red.)

For example, consider the transition from Table 1 to Table 2 . When word 0010 becomes green, the poison list receives its first three items:

Table 2
LISTS USED BY ALGORITHM C ( m = 2), ENTERING LEVEL 2

 

0

1

2

3

4

5

6

7

8

9

a

b

c

d

e

f

 

0

RED

RED

GREEN

BLUE

RED

RED

BLUE

BLUE

RED

RED

RED

BLUE

BLUE

BLUE

BLUE

RED

 

10

 

 

 

 

 

 

 

 

 

 

 

29

28

2b

2a

 

 

20

 

 

 

 

 

 

 

 

1100

1011

1110

1101

 

 

 

 

P1

30

1f

 

 

 

 

 

 

 

2c

 

 

 

 

 

 

 

 

40

 

 

 

 

 

 

54

55

 

 

 

58

5c

5e

5d

 

 

50

 

 

 

 

0110

0111

 

 

1011

 

 

 

1100

1110

1101

 

P2

60

4f

 

 

 

56

 

 

 

59

 

 

 

5f

 

 

 

 

70

 

 

 

 

 

 

86

87

 

 

 

8a

8c

8d

8e

 

 

80

 

 

 

 

 

 

0110

0111

 

 

1011

 

1100

1101

1110

 

P3

90

80

 

81

 

84

 

88

 

88

 

8b

 

8e

 

8f

 

 

a0

 

 

 

b9

 

 

 

bb

 

 

 

b8

 

ba

 

 

 

b0

 

 

 

 

 

 

 

 

1011

0011

1101

0111

 

 

 

 

S1

c0

af

 

 

 

 

 

 

 

bc

 

 

 

 

 

 

 

 

d0

 

 

 

ec

 

 

 

ed

 

 

 

ee

e0

e4

 

 

 

e0

1100

 

 

 

1101

 

 

 

 

 

 

 

0011

0111

1011

 

S2

f0

e1

 

 

 

e5

 

 

 

e7

 

 

 

ef

 

 

 

 

100

 

 

 

116

 

 

11c

11e

 

 

 

117

118

11a

11d

 

 

110

 

 

 

 

 

 

0011

1011

1100

 

1101

 

0110

1110

0111

 

S3

120

110

 

112

 

113

 

118

 

119

 

11b

 

11e

 

11f

 

 

130

 

 

 

144

 

 

145

148

 

 

 

14b

146

14a

149

 

 

140

 

 

 

 

0011

0110

1100

 

0111

1110

1101

1011

 

 

 

 

CL

150

13f

 

 

 

147

 

 

 

14c

 

 

 

 

 

 

 

 

The word 0010 has become green, thus closing its seven lists and making 0001 red. The logic of Algorithm C has also made 1001 red. Hence 0001 and 1001 have been deleted from the open lists in which they formerly appeared (see exercise 42 ).

(*001, 0***),   (**00, 10**),   (***0, 010*).

The first of these kills off the *001 list, because 0*** contains the green word 0010. That makes 1001 red. The last of these, similarly, kills off the 010* list; but that list is empty when m = 2. The poison list now reduces to a single item, (**00, 10**), which remains poisonous because list **00 contains the blue word 1100 and 10** contains the blue word 1011.

We’ll maintain the poison list at the end of MEM , following the CL lists. It obviously will contain at most 3( m 4 – m 2 ) / 4 entries, and in fact it usually turns out to be quite small. No inverse list is required; so we shall adopt the simple method of ( 17 ) and ( 18 ), but with two cells per entry so that TAIL will change by ±2 instead of by ±1. The value of TAIL will be stored in MEM at key times so that temporary changes to it can be undone.

The case m = 4, in which each codeword consists of four quaternary digits {0, 1, 2, 3}, is particularly interesting, because an early backtrack program by Lee Laxdal found that no such commafree code can make use of all 60 of the cycle classes [0001], [0002], ..., [2333]. [See B. H. Jiggs, Canadian Journal of Math. 15 (1963), 178–187.] Laxdal’s program also reportedly showed that at least three of those classes must be omitted; and it found several valid 57-word sets. Further details were never published, because the proof that 58 codewords are impossible depended on what Jiggs called a “quite time-consuming” computation.

Because size 60 is impossible, our algorithm cannot simply assume that a move such as 1001 is forced when the other words 0011, 0110, 1100 of its class have been ruled out. We must also consider the possibility that class [0011] is entirely absent from the code. Such considerations add an interesting further twist to the problem, and Algorithm C describes one way to cope with it.

Algorithm C ( Four-letter commafree codes ). Given an alphabet size m ≤ 7 and a goal g in the range L – m ( m – 1) ≤ g ≤ L , where L = ( m 4 – m 2 ) / 4, this algorithm finds all sets of g four-letter words that are commafree and include either 0001 or 0010. It uses an array MEM of M = ⌊23 . 5 m 4 ⌋ 16-bit numbers, as well as several more auxiliary arrays: ALF of size 16 3 m ; STAMP of size M ; X , C , S , and U of size L +1; FREE and IFREE of size L ; and a sufficiently large array called UNDO whose maximum size is difficult to guess.

C1. [Initialize.] Set ALF[ ( abcd ) 16 ] ← ( abcd ) m for 0 ≤ a, b, c, d < m . Set STAMP[ k ] ← 0 for 0 ≤ k < M and σ ← 0. Put the initial prefix, suffix, and class lists into MEM , as in Table 1 . Also create an empty poison list by setting MEM[PP] ← POISON , where POISON = 22 m 4 and PP = POISON – 1. Set FREE[ k ] ← IFREE[ k ] ← k for 0 ≤ k < L . Then set l ← 1, x ← # 0001 , c ← 0, s ← L – g , f ← L , u ← 0, and go to step C3. (Variable l is the level, x is a trial word, c is its class, s is the “ slack,” f is the number of free classes, and u is the size of the UNDO stack.)

C2. [Enter level l .] If l > L , visit the solution x 1 ... x L and go to C6. Otherwise choose a candidate word x and class c as described in exercise 44 .

C3. [Try the candidate.] Set U[ l ] ← u and σ ← σ +1. If x < 0, go to C6 if s = 0 or l = 1, otherwise set s ← s – 1. If x ≥ 0, update the data structures to make x green, as described in exercise 45 , escaping to C5 if trouble arises.

C4. [Make the move.] Set X[ l ] ← x , C[ l ] ← c , S[ l ] ← s , p ← IFREE[ c ] , f ← f –1. If p ≠ f , set y ← FREE[ f ] , FREE[ p ] ← y , IFREE[ y ] ← p , FREE[ f ] ← c , IFREE[ c ] ← f . (This is ( 23 ).) Then set l ← l + 1 and go to C2.

C5. [Try again.] While u > U[ l ] , set u ← u – 1 and MEM[UNDO[ u ] ≫ 16 ] ← UNDO[ u ] & # ffff . (Those operations restore the previous state, as in ( 25 ).) Then σ ← σ + 1 and redden x (see exercise 45 ). Go to C2.

C6. [ Backtrack.] Set l ← l – 1, and terminate if l = 0. Otherwise set x ← X[ l ] , c ← C[ l ] , f ← f +1. If x < 0, repeat this step (class c was omitted from the code). Otherwise set s ← S[ l ] and go back to C5.

Exercises 44 and 45 provide the instructive details that flesh out this skeleton.

Algorithm C needs just 13, 177, and 2380 megamems to prove that no solutions exist for m = 4 when g is 60, 59, and 58. It needs about 22800 megamems to find the 1152 solutions for g = 57; see exercise 47 . There are roughly (14, 240, 3700, 38000) thousand nodes in the respective search trees, with most of the activity taking place on levels 30 ± 10. The height of the UNDO stack never exceeds 2804, and the poison list never contains more than 12 entries at a time.

Running time estimates

Backtrack programs are full of surprises. Sometimes they produce instant answers to a supposedly difficult problem. But sometimes they spin their wheels endlessly, trying to traverse an astronomically large search tree. And sometimes they deliver results just about as fast as we might expect.

Fortunately, we needn’t sit in the dark. There’s a simple Monte Carlo algorithm by which we can often tell in advance whether or not a given backtrack strategy will be feasible. This method, based on random sampling, can actually be worked out by hand before writing a program, in order to help decide whether to invest further time while following a particular approach. In fact, the very act of carrying out this pleasant pencil-and-paper method often suggests useful cutoff strategies and/or data structures that will be valuable later when a program is being written. For example, the author developed Algorithm C above after first doing some armchair experiments with random choices of potential commafree codewords; these dry runs revealed that a family of lists such as those in Tables 1 and 2 would be quite helpful when making further choices.

To illustrate the method, let’s consider the n queens problem again, as represented in Algorithm B * above. When n = 8, we can obtain a decent “ballpark estimate” of the size of Fig. 68 by examining only a few random paths in that search tree. We start by writing down the number D 1 ← 8, because there are eight ways to place the queen in row 1. (In other words, the root node of the search tree has degree 8.) Then we use a source of random numbers — say the binary digits of π mod1 = ( . 001001000011 ... ) 2 — to select one of those placements. Eight choices are possible, so we look at three of those bits; we shall set X 1 ← 2, because 001 is the second of the eight possibilities (000, 001, ..., 111).

Given X 1 = 2, the queen in row 2 can’t go into columns 1, 2, or 3. Hence five possibilities remain for X 2 , and we write down D 2 ← 5. The next three bits of π lead us to set X 2 ← 5, since 5 is the second of the available columns (4, 5, 6, 7, 8) and 001 is the second value of (000, 001, ..., 100). Incidentally, if π had continued with 101 or 110 or 111 instead of 001, we would have used the “ rejection method” of Section 3.4.1 and moved to the next three bits; see exercise 49 .

Continuing in this way leads to D 3 ← 4, X 3 ← 1; then D 4 ← 3, X 4 ← 4. (Here we used the two bits 00 to select X 3 , and the next two bits 00 to select X 4 .) The remaining branches are forced: D 5 ← 1, X 5 ← 7; D 6 ← 1, X 6 ← 3; D 7 ← 1, X 7 ← 6; and we’re stuck when we reach level 8 and find D 8 ← 0.

These sequential random choices are depicted in Fig. 69(a) , where we’ve used them to place each queen successively into an unshaded cell. Parts (b), (c), and (d) of Fig. 69 correspond in the same way to choices based on the binary digits of e mod 1, ϕ mod 1, and γ mod 1. Exactly 10 bits of π, 20 bits of e , 13 bits of ϕ , and 13 bits of γ were used to generate these examples.

images

Fig. 69. Four random attempts to solve the 8 queens problem. Such experiments help to estimate the size of the backtrack tree in Fig. 68 . The branching degrees are shown at the right of each diagram, while the random bits used for sampling appear below. Cells have been shaded in gray if they are attacked by one or more queens in earlier rows.

In this discussion the notation D k stands for a branching degree, not for a domain of values. We’ve used uppercase letters for the numbers D 1 , X 1 , D 2 , etc., because those quantities are random variables. Once we’ve reached D l \= 0 at some level, we’re ready to estimate the overall cost, by implicitly assuming that the path we’ve taken is representative of all root-to-leaf paths in the tree.

The cost of a backtrack program can be assessed by summing the individual amounts of time spent at each node of the search tree. Notice that every node on level l of that tree can be labeled uniquely by a sequence x 1 ... x l – 1 , which defines the path from the root to that node. Thus our goal is to estimate the sum of all c ( x 1 ... x l – 1 ), where c ( x 1 ... x l – 1 ) is the cost associated with node x 1 ... x l – 1 .

For example, the four queens problem is represented by the search tree ( 4 ), and its cost is the sum of 17 individual costs

c ( )   +   c ( 1 )   +   c ( 13 )    +   c ( 14 ) +   c ( 142 )   +   c ( 2 )   +   c ( 24 )   +   . ⁢   . ⁢   ⁢ . ⁢   +    c ( 413 )   +   c ( 42 ) . ( 28 )

If C ( x 1 ... x l ) denotes the total cost of the subtree rooted at x 1 ... x l , then

C ( x 1 ⁢   . ⁢ . ⁢ . ⁢   x l )   \= c ( x 1 ⁢   . ⁢ . ⁢ . ⁢   x l )   +   C ( x 1 ⁢   . ⁢ . ⁢ . ⁢   x l x l + 1 ( 1 ) )   +   . ⁢ . ⁢ . ⁢     +   C ( x 1 ⁢   . ⁢ . ⁢ . ⁢   x l x l + 1 ( d ) ) ( 29 )

when the choices for x l + 1 at node x 1 ... x l are { x l + 1 ( 1 ) ,   . ⁢ . ⁢ . ⁢   , x l + 1 ( d ) } . For instance in ( 4 ) we have C (1) = c (1) + C (13) + C (14); C (13) = c (13); and C () = c () + C (1) + C (2) + C (3) + C (4) is the overall cost ( 28 ).

In these terms a Monte Carlo estimate for C () is extremely easy to compute:

Theorem E. Given D 1 , X 1 , D 2 , X 2 , ... as above, the cost of backtracking is

C ( )   \=   E ( c ( )   +   D 1 ( c ( X 1 )   + D 2 ( c ( X 1 X 2 )   +   D 3 ( c ( X 1 X 2 X 3 )   +   . ⁢   . ⁢   . ) ) ) ) . ( 30 )

Proof. Node x 1 ... x l , with branch degrees d 1 , ... , d l above it, is reached with probability 1 / d 1 ... d l ; so it contributes d 1 ... d l c ( x 1 ... x l ) / d 1 ... d l \= c ( x 1 ... x l ) to the expected value in this formula.

For example, the tree ( 4 ) has six root-to-leaf paths, and they occur with respective probabilities 1/8, 1/8, 1/4, 1/4, 1/8, 1/8. The first one contributes 1/8 times c () + 4( c (1) + 2( c (13))), namely c () / 8 + c (1) / 2 + c (13), to the expected value. The second contributes c () / 8 + c (1) / 2 + c (14) + c (142); and so on.

A special case of Theorem E, with all c ( x 1 ... x l ) = 1, tells us how to estimate the total size of the tree, which is often a crucial quantity:

Corollary E. The number of nodes in the search tree, given D 1 , D 2 , ... , is

E ( 1 + D 1 ⁢   + D 1   D 2 ⁢   +   . ⁢   . ⁢   . )   \=   E ( 1 + D 1 ( 1 + D 2 ( 1 + D 3 ( 1 + . ⁢   . ⁢   . ⁢   ) ) ) ) . ( 31 )

For example, Fig. 69 gives us four estimates for the size of the tree in Fig. 68 , using the numbers D j at the right of each 8 × 8 diagram. The estimate from Fig. 69(a) is 1 + 8 (1 + 5 (1 + 4(1 + 3(1 + 1(1 + 1(1 + 1)))))) = 2129; and the other three are respectively 2689, 1489, 2609. None of them is extremely far from the true number, 2057, although we can’t expect to be so lucky all the time.

The detailed study in exercise 53 shows that the estimate ( 31 ) in the case of 8 queens turns out to be quite well behaved:

(min 489,    ave 2057,     max 7409,       dev 1146640 ⁢   ≈   1071 ). ( 32 )

The analogous problem for 16 queens has a much less homogeneous search tree:

(min 2597105,   ave 1141190303,  max 131048318769,  dev ⁢   ≈  1234000000). ( 33 )

Still, this standard deviation is roughly the same as the mean, so we’ll usually guess the correct order of magnitude. (For example, ten independent experiments predicted .632, .866, .237, 1.027, 4.006, .982, .143, .140, 3.402, and .510 billion nodes, respectively. The mean of these is 1.195.) A thousand trials with n = 64 suggest that the problem of 64 queens will have about 3 × 10 65 nodes in its tree.

Let’s formulate this estimation procedure precisely, so that it can be performed conveniently by machine as well as by hand:

Algorithm E ( Estimated cost of backtrack ). Given domains D k and properties P l as in Algorithm B , together with node costs c ( x 1 ... x l ) as above, this algorithm computes the quantity S whose expected value is the total cost C () in ( 30 ). It uses an auxiliary array y 0 y 1 ... whose size should be ≥ max(| D 1 | , ..., | D n |).

E1. [Initialize.] Set l ← D ← 1, S ← 0, and initialize any data structures needed.

E2. [Enter level l .] (At this point P l −1 ( X 1 , ..., X l −1 ) holds.) Set S ← S + D · c ( X 1 ... X l −1 ). If l > n , terminate the algorithm. Otherwise set d ← 0 and set x ← min D l , the smallest element of D l .

E3. [Test x .] If P l ( X 1 ,..., X l −1 , x ) holds, set y d ← x and d ← d +1.

E4. [Try again.] If x ≠ max D l , set x to the next larger element of D l and return to step E3.

E5. [Choose and try.] Set D ← D · d , and terminate if d = 0. Otherwise set X l ← y I , where I is a uniformly random integer in {0 , ..., d −1}. Update the data structures to facilitate testing P l +1 , set l ← l +1, and go back to E2.

Although Algorithm E looks rather like Algorithm B , it never backtracks.

Of course we can’t expect this algorithm to give decent estimates in cases where the backtrack tree is wildly erratic. The expected value of S , namely E S , is indeed the true cost; but the probable values of S might be quite different.

An extreme example of bad behavior occurs if property P l is the simple condition ‘ x 1 > … > x l ’, and all domains are {1 , ..., n }. Then there’s only one solution, x 1 ... x n \= n ... 1; and backtracking is a particularly stupid way to find it!

The search tree for this somewhat ridiculous problem is, nevertheless, quite interesting. It is none other than the binomial tree T n of Eq. 7.2.1.3–(21), which has ( n l ) nodes on level l + 1 and 2 n nodes in total. If we set all costs to 1, expected value of S is therefore 2 n = e n ln 2 . But exercise 52 proves that S will almost always be much smaller, less than e (ln n ) 2 ln ln n . Furthermore the average value of l when Algorithm E terminates with respect to T n is only H n + 1. When n = 100, for example, the probability that l ≥ 20 on termination is only 0.0000000027, while the vast majority of the nodes are near level 51.

Many refinements of Algorithm E are possible. For example, exercise 54 shows that the choices in step E5 need not be uniform. We shall discuss improved estimation techniques in Section 7.2.2.9, after having seen numerous examples of backtracking in practice.

* Estimating the number of solutions

Sometimes we know that a problem has more solutions than we could ever hope to generate, yet we still want to know roughly how many there are; Algorithm E will tell us the approximate number. Indeed, the expected final value of D is exactly the total number of solutions, because every solution X 1 . . . X l constructed by the algorithm is obtained with probability 1/ D , in a case where D > 0. Note that there may be another criterion for successful termination in step E2, even though l might still be ≤ n .

For example, suppose we want to know the number of different paths by which a king can go from one corner of a chessboard to the opposite corner, without revisiting any square. One such path, chosen at random using the bits of π for guidance as we did in Fig. 69(a) , is shown here. Starting in the upper left corner, we have 3 choices for the first move. Then, after moving to the right, there are 4 choices for the second move. And so on. We never make a move that would disconnect us from the goal; in particular, two of the moves are actually forced. ( Exercise 58 explains one way to avoid fatal mistakes.)

images

The probability of obtaining this particular path is exactly 1 3 1 4 1 6 1 6 1 2 1 6 1 7 ⁢ . ⁢   . ⁢   . ⁢   1 2 ⁢ \= 1 / D , where D = 3 × 4 × 6 × 6 × 2 × 6 × 7 × … × 2 = 1 2 · 2 4 · 3 4 · 4 10 · 5 9 · 6 6 · 7 1 ≈ 8 . 7 × 10 20 . Thus we can reasonably guess, at least tentatively, that there are 10 21 such paths, more or less.

Of course that guess, based on a single random sample, rests on very shaky ground. But we know that the average value M N \= ( D (1) + … + D ( N ) ) / N of N guesses, in N independent experiments, will almost surely approach the correct number.

How large should N be, before we can have any confidence in the results? The actual values of D obtained from random king paths tend to vary all over the map. Figure 70 plots typical results, as N varies from 1 to 10000. For each value of N we can follow the advice of statistics textbooks and calculate the sample variance V N \= S N / ( N − 1) as in Eq. 4.2.2–(16); then M N ⁢   ±   V N / N is the textbook estimate. The top diagram in Fig. 70 shows these “ error bars” in gray, surrounding black dots for M N . This sequence M N does appear to settle down after N reaches 3000 or so, and to approach a value near 5 × 10 25 . That’s much higher than our first guess, but it has lots of evidence to back it up.

On the other hand, the bottom chart in Fig. 70 shows the distribution of the logarithms of the 10000 values of D that were used to make the top chart. Almost half of those values were totally negligible — less than 10 20 . About 75% of them were less than 10 24 . But some of them * exceeded 10 28 . Can we really rely on a result that’s based on such chaotic behavior? Is it really right to throw away most of our data and to trust almost entirely on observations that were obtained from comparatively few rare events?

* Four of the actual values that led to Fig. 70 were larger than 10 28 ; the largest, ≈ 2.1 × 10 28 , came from a path of length 57. The smallest estimate, 19361664, came from a path of length 10.

Images

Fig. 70. Estimates of the number of king paths, based on up to 10000 random trials. The middle graph shows the corresponding quality measures of Eq. ( 34 ). The lower graph shows the logarithms of the individual estimates D ( k ) , after they’ve been sorted.

Yes, we’re okay! Some of the justification appears in exercise MPR–124, which is based on theoretical work by S. Chatterjee and P. Diaconis. In the paper cited with that exercise, they defined a simple measure of quality,

X ^ N ⁢   \= max( D ( 1 ) ,     . ⁢   . ⁢   . ⁢   , D ( N ) / ( N M N )   \=     max ( D ( 1 ) ,   . ⁢   . ⁢   ⁢ . ⁢   ,   D ( N ) ) D ( 1 ) ⁢   +   . ⁢ ⁢ . ⁢   ⁢ . ⁢  +  D ( N ) , ( 34 )

arguing that a reasonable policy in most experiments such as these is to stop sampling when X ^ N gets small. (Values of this statistic X ^ N have been plotted in the middle of Fig. 70 .)

Furthermore we can estimate other properties of the solutions to a backtrack problem, instead of merely counting those solutions. For example, the expected value of lD on termination of the random king’s path algorithm is the total length of such paths. The data underlying Fig. 70 suggests that this total is (2.66 ± . 14) × 10 27 ; hence the average path length appears to be about 53. The samples also indicate that about 34% of the paths pass through the center; about 46% touch the upper right corner; about 22% touch both corners; and about 7% pass through the center and both corners.

For this particular problem we don’t actually need to rely on estimates, because the ZDD technology of Section 7.1.4 allows us to compute the true values. (See exercise 59 .) The total number of simple corner-to-corner king paths on a chessboard is exactly 50,819,542,770,311,581,606,906,543; this value lies almost within the error bars of Fig. 70 for all N ≥ 250, except for a brief interval near N = 1400. And the total length of all these paths turns out to be exactly 2,700,911,171,651,251,701,712,099,831, which is a little higher than our estimate. The true average length is therefore ≈ 53 . 15. The true probabilities of hitting the center, a given corner, both corners, and all three of those spots are respectively about 38.96%, 50.32%, 25.32%, and 9.86%.

The total number of corner-to-corner king paths of the maximum length, 63, is 2,811,002,302,704,446,996,926. This is a number that can not be estimated well by a method such as Algorithm E without additional heuristics.

The analogous problem for corner-to-corner knight paths, of any length, lies a bit beyond ZDD technology because many more ZDD nodes are needed. Using Algorithm E we can estimate that there are about (8 . 6 ± 1 . 2) × 10 19 such paths.

Factoring the problem

Imagine an instance of backtracking that is equivalent to solving two independent subproblems. For example, we might be looking for all sequences x = x 1 x 2 ... x n that satisfy P n ( x 1 , x 2 , ..., x n ) = F ( x 1 , x 2 , ..., x n ), where

F ( x 1   , x 2   , . ⁢   . ⁢   . ⁢   , x n   )   \=   G ( x 1   ,   . ⁢   . ⁢   . ⁢   ,   x k   )   Λ ⁢   ⁢   H ( x k + 1 , . ⁢   . ⁢   . ⁢   ,   x n ) . ( 35 )

Then the size of the backtrack tree is essentially the product of the tree sizes for G and for H , even if we use dynamic ordering. Hence it’s obviously foolish to apply the general setup of ( 1 ) and ( 2 ). We can do much better by finding all solutions to G first, then finding all solutions to H , thereby reducing the amount of computation to the sum of the tree sizes. Again we’ve divided and conquered, by factoring the compound problem ( 35 ) into separate subproblems.

We discussed a less obvious application of problem factorization near the beginning of Chapter 7 , in connection with latin squares: Recall that E. T. Parker sped up the solution of 7–(6) by more than a dozen orders of magnitude, when he discovered 7–(7) by essentially factoring 7–(6) into ten subproblems whose solutions could readily be combined.

In general, each solution x to some problem F often implies the existence of solutions x ( p ) \= ϕ p ( x ) to various simpler problems F p that are “ homomorphic images” of F . And if we’re lucky, the solutions to those simpler problems can be combined and “ lifted” to a solution of the overall problem. Thus it pays to be on the lookout for such simplifications.

Let’s look at another example. F. A. Schossow invented a tantalizing puzzle [ U.S. Patent 646463 (3 April 1900)] that went viral in 1967 when a marketing genius decided to rename it “ Instant Insanity.” The problem is to take four cubes such as

images

where each face has been marked in one of four ways, and to arrange them in a row so that all four markings appear on the top, bottom, front, and back sides. The placement in ( 36 ) is incorrect, because there are two ♣s (and no ♠) on top. But we get a solution if we rotate each cube by 90 ° .

There are 24 ways to place each cube, because any of the six faces can be on top and we can rotate four ways while keeping the top unchanged. So the total number of placements is 24 4 = 331776. But this problem can be factored in an ingenious way, so that all solutions can be found quickly by hand! [See F. de Carteblanche, Eureka 9 (1947), 9–11.] The idea is that any solution to the puzzle gives us two each of images , if we look only at the top and bottom or only at the front and back. That’s a much easier problem to solve.

For this purpose a cube can be characterized by its three pairs of markings on opposite faces; in ( 36 ) these face-pairs are respectively

images

Which of the 3 4 = 81 ways to choose one face-pair from each cube will give us images ? They can all be discovered in a minute or two, by listing the nine possibilities for cubes (1, 2) and the nine for (3, 4). We get just three,

images

Notice furthermore that each solution can be “halved” so that one each of images appears on both sides, by swapping face-pairs; we can change ( 38 ) to

images

Each of these solutions to the opposite-face subproblem can be regarded as a 2-regular graph, because every vertex of the multigraph whose edges are (say) images has exactly two neighbors.

A solution to “ Instant Insanity” will give us two such 2-regular factors, one for top-and-bottom and one for front-and-back. Furthermore those two factors will have disjoint edges: We can’t use the same face-pair in both. Therefore problem ( 36 ) can be solved only by using the first and third factor in ( 39 ).

Conversely, whenever we have two disjoint 2-regular graphs, we can always use them to position the cubes as desired, thus “ lifting” the factors to a solution of the full problem.

Exercise 67 illustrates another kind of problem factorization. We can conveniently think of each subproblem as a “ relaxation” of constraints.

Historical notes

The origins of backtrack programming are obscure. Equivalent ideas must have occurred to many people, yet there was hardly any reason to write them down until computers existed. We can be reasonably sure that James Bernoulli used such principles in the 17th century, when he successfully solved the “ Tot tibi sunt dotes” problem that had eluded so many others (see Section 7.2.1.7), because traces of the method exist in his exhaustive list of solutions.

Backtrack programs typically traverse the tree of possibilities by using what is now called depth-first search, a general graph exploration procedure that Édouard Lucas credited to a telegraph engineer named Trémaux [ Récréations Mathématiques 1 (Paris: Gauthier-Villars, 1882), 47.50].

The eight queens problem was first proposed by Max Bezzel [ Schachzeitung 3 (1848), 363; 4 (1849), 40] and by Franz Nauck [ Illustrirte Zeitung 14 , 361 (1 June 1850), 352; 15 , 377 (21 September 1850), 182], perhaps independently. C. F. Gauss saw the latter publication, and wrote several letters about it to his friend H. C. Schumacher. Gauss’s letter of 27 September 1850 is especially interesting, because it explained how to find all the solutions by backtracking. which he called ‘Tatonniren’, from a French term meaning “to feel one’s way.” He also listed the lexicographically first solutions of each equivalence class under reflection and rotation: 15863724, 16837425, 24683175, 25713864, 25741863, 26174835, 26831475, 27368514, 27581463, 35281746, 35841726, and 36258174.

Computers arrived a hundred years later, and people began to use them for combinatorial problems. The time was therefore ripe for backtracking to be described as a general technique, and Robert J. Walker rose to the occasion [ Proc. Symposia in Applied Math . 10 (1960), 91.94]. His brief note introduced Algorithm W in machine-oriented form, and mentioned that the procedure could readily be extended to find variable-length patterns x 1 ... x n where n is not fixed.

The next milestone was a paper by Solomon W. Golomb and Leonard D. Baumert [ JACM 12 (1965), 516–524], who formulated the general problem carefully and presented a variety of examples. In particular, they discussed the search for maximum commafree codes, and noted that backtracking can be used to find successively better and better solutions to combinatorial optimization problems. They introduced certain kinds of lookahead, as well as the important idea of dynamic ordering by branching on variables with the fewest remaining choices.

Backtrack methods allow special cutoffs when applied to integer programming problems [see E. Balas, Operations Research 13 (1965), 517–546]. A. M. Geoffrion simplified and extended that work, calling it “ implicit enumeration” because many cases aren’t enumerated explicitly [ SIAM Rev. 9 (1967), 178–190].

Other noteworthy early discussions of backtrack programming appear in Mark Wells’s book Elements of Combinatorial Computing (1971), Chapter 4; in a survey by J. R. Bitner and E. M. Reingold, CACM 18 (1975), 651–656; and in the Ph.D. thesis of John Gaschnig [Report CMU-CS-79-124 (Carnegie-Mellon University, 1979), Chapter 4]. Gaschnig introduced techniques of “ backmarking” and “ backjumping” that we s hall discuss later.

Monte Carlo estimates of the cost of backtracking were first described briefly by M. Hall, Jr., and D. E. Knuth in Computers and Computing, AMM 72 , 2, part 2, Slaught Memorial Papers No. 10 (February 1965), 21–28. Knuth gave a much more detailed exposition a decade later, in Math. Comp. 29 (1975), 121– 136. Such methods can be considered as special cases of so-called “ importance sampling”; see J. M. Hammersley and D. C. Handscomb, Monte Carlo Methods (London: Methuen, 1964), 57–59. Studies of random self-avoiding walks such as the king paths discussed above were inaugurated by M. N. Rosenbluth and A. W. Rosenbluth, in J. Chemical Physics 23 (1955), 356–359.

Backtrack applications are nicely adaptable to parallel programming, because different parts of the search tree are often completely independent of each other; thus disjoint subtrees can be explored on different machines, with a minimum of interprocess communication. Already in 1964, D. H. Lehmer explained how to subdivide a problem so that two computers of different speeds could work on it simultaneously and finish at the same time. The problem that he considered had a search tree of known shape (see Theorem 7.2.1.3L); but we can do essentially similar load balancing even in much more complicated situations, by using Monte Carlo estimates of the subtree sizes. Although many ideas for parallelizing combinatorial searches have been developed over the years, such techniques are beyond the scope of this book. Readers can find a nice introduction to a fairly general approach in the paper by R. Finkel and U. Manber, ACM Transactions on Programming Languages and Systems 9 (1987), 235–256.

M. Alekhnovich, A. Borodin, J. Buresh-Oppenheim, R. Impagliazzo, A. Magen, and T. Pitassi have defined priority branching trees , a general model of computation with which they were able to prove rigorous bounds on what backtrack programs can do, in Computational Complexity 20 (2011), 679–740.

Exercises

▸ 1. [ 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 , ..., x l ) that satisfy ( 1 ) and ( 2 ).

2. [ 10 ] True or false: We can choose D 1 so that P 1 ( x 1 ) is always true.

3. [ 20 ] Let T be any tree. Is it possible to define domains D k and cutoff properties P l ( x 1 , ..., x l ) so that T is the backtrack tree traversed by Algorithm B ?

4. [ 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.

▸ 5. [ 20 ] Reformulate Algorithm B as a recursive procedure called try ( l ), having global variables n and x 1 ... x n , to be invoked by saying ‘ try ( 1 )’. Can you imagine why the author of this book decided not to present the algorithm in such a recursive form?

6. [ 20 ] Given r , with 1 ≤ r ≤ 8, in how many ways can 7 nonattacking queens be placed on an 8 × 8 chessboard, if no queen is placed in row r ?

7. [ 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?

8. [ 20 ] Are there two 8-queen placements with the same x 1 x 2 x 3 x 4 x 5 x 6 ?

9. [ 21 ] Can a 4 m -queen placement have 3 m queens on “ white” squares?

▸ 10. [ 22 ] Adapt Algorithm W to the n queens problem, using bitwise operations on n -bit numbers as suggested in the text.

11. [ M25 ] (W. Ahrens, 1910.) Both solutions of the n queens problem when n = 4 have quarterturn symmetry : Rotation by 90 ° leaves them unchanged, but reflection doesn’t.

  1. Can the n queens problem have a solution with reflection symmetry?

  2. Show that quarterturn symmetry is impossible if n mod 4 ∈ {2 , 3}.

  3. Sometimes the solution to an n queens problem contains four queens that form the corners of a tilted square, as shown here. Prove that we can always get another solution by tilting the square the other way (but leaving the other n − 4 queens in place).

  4. Let C n be the number of solutions with 90 ° symmetry, and suppose c n of them have x k > k for 1 ≤ k ≤ n / 2. Prove that C n \= 2 ⌊ n / 4 ⌋ c n .

    images

    images

12. [ M28 ] ( Wraparound queens .) Replace ( 3 ) by the stronger conditions ‘ x j ≠ x k , ( x k − x j )mod n ≠ k − j , ( x j − x k )mod n ≠ k − j ’. (The n × n grid becomes a torus.) Prove that the resulting problem is solvable if and only if n is not divisible by 2 or 3.

13. [ M30 ] For which n ≥ 0 does the n queens problem have at least one solution?

14. [ M25 ] If exercise 12 has T ( n ) toroidal solutions, show that Q ( mn ) ≥ Q ( m ) n T ( n ).

15. [ HM44 ] (M. Simkin, 2021.) Show that Q ( n ) ≈ σ n n ! as n → ∞, where σ ≈ 0.389068.

16. [ 21 ] Let H ( n ) be the number of ways that n queen bees can occupy an n × n honeycomb so that no two are in the same line. (For example, one of the H (4) = 7 ways is shown here.) Compute H ( n ) for small n .

images

17. [ 15 ] J. H. Quick (a student) noticed that the loop in step L2 of Algorithm L can be changed from ‘while x l < 0’ to ‘while x l ≠ 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 ← k in step L3. Was it a good idea?

18. [ 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 4 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 ?

19. [ M10 ] What are the domains D l in Langford’s problem ( 7 )?

▸ 20. [ 21 ] Extend Algorithm L so that it forces x l ← k whenever k ∉ { x 1 , ..., x l −1 } and l ≥ 2 n − k − 1.

▸ 21. [ M25 ] If x = x 1 x 2 ... x 2 n , let x D \= (− x 2 n ) ... (− x 2 ) (− x 1 ) = − x R be its dual.

  1. Show that if n is odd and x solves Langford’s problem ( 7 ), we have x k \= n for some k ≤ ⌊ n/ 2⌋ if and only if x k D \= n for some k > ⌊ n / 2⌋.

  2. Find a similar rule that distinguishes x from x D when n is even.

  3. Consequently the algorithm of exercise 20 can be modified so that exactly one of each dual pair of solutions { x, x D } is visited.

22. [ M26 ] Explore “ loose Langford pairs”: Replace ‘ j + k + 1’ in ( 7 ) by ‘ j + ⌊3 k / 2⌋’.

23. [ 17 ] We can often obtain one word rectangle from another by changing only a letter or two. Can you think of any 5 × 6 word rectangles that almost match ( 10 )?

24. [ 20 ] Customize Algorithm B so that it will find all 5 × 6 word rectangles.

▸ 25. [ 25 ] Explain how to use orthogonal lists , as in Fig. 13 of Section 2.2.6, so that it’s easy to visit all 5-letter words whose k th character is c , given 1 ≤ k ≤ 5 and a ≤ c ≤ z . Use those sublists to speed up the algorithm of exercise 24 .

26. [ 21 ] Can you find nice word rectangles of sizes 5 × 7, 5 × 8, 5 × 9, 5 × 10?

27. [ 22 ] What profile and average node costs replace ( 13 ) and ( 14 ) when we ask the algorithm of exercise 25 for 6 × 5 word rectangles instead of 5 × 6?

▸ 28. [ 23 ] The method of exercises 24 and 25 does n levels of backtracking to fill the cells of an m × 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.

29. [ 20 ] Do any 5 × 6 word rectangles contain fewer than 11 different words?

30. [ 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 N
A L O N E
V E N O M
E N E M Y

because it actually is “meaningful”! Determine the total number of symmetric 5 × 5 word squares, by adapting the method of exercise 28 . How many belong to WORDS (500)?

31. [ 20 ] (Charles Babbage, 1864.) Do any of the symmetric 5 × 5 word squares also have valid words on both diagonals?

32. [ 22 ] How many symmetric word squares of sizes 2 × 2, 3 × 3, ... , are supported by The Official SCRABBLE ® Players Dictionary , fourth edition (Hasbro, 2005)?

33. [ 21 ] Puzzlers who tried to construct word squares by hand found long ago that it was easiest to work from bottom to top. Therefore they used “ reverse dictionaries,” whose words appear in colex order. Does this idea speed up computer experiments?

34. [ 15 ] What’s the largest commafree subset of the following words?

aced babe bade bead beef cafe cede dada dead deaf face fade feed

▸ 35. [ 22 ] Let w 1 , w 2 , ... , 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 , ..., w j −1 }.

36. [ M22 ] A two-letter block code on an m -letter alphabet can be represented as a digraph D on m vertices, with a → b if and only if ab is a codeword.

  1. Prove that the code is commafree ⇔ D has no oriented paths of length 3.

  2. How many arcs can be in an m -vertex digraph with no oriented paths of length r ?

▸ 37. [ M30 ] (W. L. Eastman, 1965.) The following elegant construction yields a comma-free code of maximum size for any odd block length n , over any alphabet. Given a sequence x = x 0 x 1 ... x n −1 of nonnegative integers, where x differs from each of its other cyclic shifts x k ... x n −1 x 0 ...x k −1 for 0 < k < n , the procedure outputs a cyclic shift σx with the property that the set of all such σx is commafree.

We regard x as an infinite periodic sequence 〈 x n 〉 with x k \= x k − n for all k ≥ n . Each cyclic shift then has the form x k x k + 1 ... x k + n −1 . The simplest nontrivial example occurs when n = 3, where x = x 0 x 1 x 2 x 0 x 1 x 2 x 0 ... and we don’t have x 0 = x 1 = x 2 . In this case the algorithm outputs x k x k +1 x k +2 where x k ≥ x k +1 < x k +2 ; and the set of all such triples clearly satisfies the commafree condition.

One key idea is to think of x as partitioned into t substrings by boundary markers b j , where 0 ≤ b 0 < b 1 < … < b t −1 < n and b j \= b j − t + n for j ≥ t . Then substring y j is x b j x b j +1 ... x b j +1 − 1 . The number t of substrings is always odd. Initially t = n and b j \= j for all j ; ultimately t = 1, and σx = y 0 is the desired output.

Eastman’s algorithm is based on comparison of adjacent substrings y j −1 and y j . If those substrings have the same length, we use lexicographic comparison; otherwise we declare that the longer substring is bigger.

The second key idea is the notion of “ dips,” which are substrings of the form z = z 1 ...z k where k ≥ 2 and z 1 … ≥ z k −1 < z k . It’s easy to see that any string y = y 0 y 1 ... in which we have y i < y i +1 for infinitely many i can be factored into a sequence of dips, y = z (0) z (1) ... , and this factorization is unique. For example,

3141592653589793238462643383 . . . = 314 15 926 535 89 79 323 846 26 4338 3 . . . .

Furthermore, if y is a periodic sequence, its factorization into dips is also ultimately periodic, although some of the initial factors may not occur in the period. For example,

123443550123443550123443550 . . . = 12 34 435 501 23 4435 501 23 4435 . . . .

Given a periodic, nonconstant sequence y described by boundary markers as above, where the period length t is odd, its periodic factorization will contain an odd number of odd-length dips. Each round of Eastman’s algorithm simply retains the boundary points at the left of those odd-length dips. Then t is reset to the number of retained boundary points, and another round begins if t > 1.

  1. Play through the algorithm by hand when n = 19 and x = 3141592653589793238.

  2. Show that the number of rounds is at most ⌊log 3 n ⌋.

  3. Exhibit a binary x that achieves this worst-case bound when n = 3 e .

  4. Implement the algorithm with full details. (It’s surprisingly short!)

  5. Explain why the algorithm yields a commafree code.

38. [ 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.)

39. [ 18 ] Why can’t a commafree code of length ( m 4 − m 2 ) / 4 contain 0001 and 2000?

▸ 40. [ 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”)?

41. [ 17 ] What’s the significance of (a) MEM[4d] = 5e and (b) MEM[94] = 84 in Table 1 ?

42. [ 18 ] Why does Table 2 have (a) MEM[f8] = e7 and (b) MEM[ad] = ba ?

43. [ 20 ] Suppose you’re using the undoing scheme (26) and the operation σ ← σ +1 has just bumped the current stamp σ to zero. What should you do?

▸ 44. [ 25 ] Spell out the low-level implementation details of the candidate selection process in step C2 of Algorithm C . Use the routine store( a, v ) of (26) whenever changing the contents of MEM , and use the following selection strategy:

  1. Find a class c with the least number r of blue words.

  2. If r = 0, set x ← −1; otherwise set x to a word in class c .

  3. If r > 1, use the poison list to find an x that maximizes the number of blue words that could be killed on the other side of the prefix or suffix list that contains x .

▸ 45. [ 28 ] Continuing exercise 44 , spell out the details of step C3 when x ≥ 0.

  1. What updates should be done to MEM when a blue word x becomes red?

  2. What updates should be done to MEM when a blue word x becomes green?

  3. Step C3 begins its job by making x green as in part (b). Explain how it should finish its job by updating the poison list.

46. [ M35 ] Is there a binary ( m = 2) commafree code with one codeword in each of the (Σ d\n μ( d )2 n/d )/ n cycle classes, for every word length n ?

47. [ HM29 ] A commafree code on m letters is equivalent to at most 2 m ! 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.

48. [ M42 ] Find a maximum-size commafree code of length 4 on m = 5 letters.

49. [ 20 ] Explain how the choices in Fig. 69 were determined from the “random” bits that are displayed. For instance, why was X 2 set to 1 in Fig. 69(b) ?

50. [ M15 ] Interpret the value E( D 1 ... D l ), in the text’s Monte Carlo algorithm.

51. [ M22 ] What’s a simple martingale that corresponds to Theorem E?

▸ 52. [ HM25 ] Elmo uses Algorithm E with D k \= {1 ,..., n }, P l \= [ x 1 > ... >x l ], c = 1.

  1. Alice flips n coins independently, where coin k yields “heads” with probability 1 /k . True or false: She obtains exactly l heads with probability [ n l ] / n ! .

  2. Let Y 1 , Y 2 , ... , Y l be the numbers on the coins that come up heads. (Thus Y 1 = 1, and Y 2 = 2 with probability 1 / 2.) Show that Pr(Alice obtains Y 1 , Y 2 , ... , Y l ) = Pr(Elmo obtains X 1 = Y l , X 2 = Y l − 1 , ... , X l \= Y 1 ).

  3. Prove that Alice q.s. obtains at most (ln n )(ln ln n ) heads.

  4. Consequently Elmo’s S is q.s. less than exp((ln n ) 2 (ln ln n )).

▸ 53. [ M30 ] Extend Algorithm B so that it also computes the minimum, maximum, mean, and variance of the Monte Carlo estimates S produced by Algorithm E .

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 | X 1 ,..., X l − 1 ) = pX 1 ... X l − 1 ( y i ) > 0. How should the estimate S be modified so that its expected value in this general scheme is still C ()?

55. [ M20 ] If all costs c ( x 1 ,..., 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.

▸ 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?

57. [ HM21 ] Algorithm E can potentially follow M different paths X 1 ... 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) , ... , D ( M ) . Prove that ( D (1) ... D ( M ) ) 1/ M ≥ M .

58. [ 30 ] The text’s king path problem 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 DIST( v ) for all vertices v not yet in the path, representing the shortest distance from v to t through unused vertices. For with such a table we can simply move at each step to a vertex for which DIST( v ) < ∞.

Devise a way to update the DIST table dynamically without unnecessary work.

59. [ 26 ] A ZDD with 3,174,197 nodes can be constructed for the family of 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.

▸ 60. [ 20 ] Experiment with biased random walks (see exercise 54 ), weighting each non-dead-end king move to a new vertex v by 1 + DIST ( v ) 2 instead of choosing every such move with the same probability. Does this strategy improve on Fig. 70 ?

61. [ HM26 ] Let P n be the number of integer sequences x 1 ... x n such that x 1 = 1 and 1 ≤ x k +1 ≤ 2 x k for 1 ≤ k < n . (The first few values are 1, 2, 6, 26, 166, 1626, ... ; this sequence was introduced by A. Cayley in Philosophical Magazine (4) 13 (1857), 245−248, who showed that P n enumerates the partitions of 2 n − 1 into powers of 2.)

  1. Show that P n is the number of different profiles that are possible for a binary tree of height n .

  2. Find an efficient way to compute P n for large n . Hint: Consider the more general sequence P n ( m ) , defined similarly but with x 1 = m .

  3. Use the estimation procedure of Theorem E to prove that P n ≥ 2 ( n 2 ) / ( n − 1 ) !.

▸ 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?

63. [ 20 ] 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.

64. [ 24 ] Assemble five cubes with uppercase letters on each face, using the patterns

images

By extending the principles of “Instant Insanity,” show that these cubes can be placed in a row so that four 5-letter words are visible. (Each word’s letters should have a consistent orientation. The letters C and U , H and I , N and Z are related by 90° rotation.)

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.

▸ 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 4 + 3 + 2 + 4 = 13, etc.) Show that this problem factors nicely, so that it can be solved readily by hand.

images

▸ 67. [ 26 ] ( The Royal Aquarium Thirteen Puzzle .) “Rearrange the nine cards of the right-hand illustration above, optionally rotating some 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, ... , 7 + 5 + 7 = 19.) The author of Hoffmann’s Puzzles Old and New (1893) stated that “There is no royal road to the solution. The proper order must be arrived at by successive transpositions until the conditions are fulfilled.” Prove that he was wrong: “Factor” this problem and solve it by hand.

▸ 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 distinct digits that it points to.

images

69. [ 41 ] Is there a puzzle like exercise 68 whose clues contain more than 32 digits of π ?

70. [ HM40 ] (M. Bousquet-Mélou.) Consider self-avoiding paths from the upper left corner of an m × 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 E D mn is the total number of such paths, m n −1 . But the variance is considerably larger: Construct polynomials P m ( z ) and Q m ( z ) such that we have G m ( z )     \=     ∑ n \= 1 ∞ ( E D m n 2 ) z n ⁢ \=   z P m ( z ) / Q m ( z ) for m ≥ 2. For example, G 3 ( z ) = ( z + z 2 )/(1–9 z –6 z 2 ) = z + 10 z 2 + 96 z 3 + 924 z 4 + 8892 z 5 + . . . . Prove furthermore that E D m n 2 \= Θ ( p m n ) , where ρ m \= 2 m + O (1).

Table 666
TWENTY QUESTIONS (SEE EXERCISE 71 )

1.

The first question whose answer is A is:

 

(A) 1

(B) 2

(C) 3

(D) 4

(E) 5

2.

The next question with the same answer as this one is:

 

(A) 4

(B) 6

(C) 8

(D) 10

(E) 12

3.

The only two consecutive questions with identical answers are questions:

 

(A) 15 and 16

(B) 16 and 17

(C) 17 and 18

(D) 18 and 19

(E) 19 and 20

4.

The answer to this question is the same as the answers to questions:

 

(A) 10 and 13

(B) 14 and 16

(C) 7 and 20

(D) 1 and 15

(E) 8 and 12

5.

The answer to question 14 is:

 

(A) B

(B) E

(C) C

(D) A

(E) D

6.

The answer to this question is:

 

(A) A

(B) B

(C) C

(D) D

(E) none of those

7.

An answer that appears most often is:

 

(A) A

(B) B

(C) C

(D) D

(E) E

8.

Ignoring answers that appear equally often, the least common answer is:

 

(A) A

(B) B

(C) C

(D) D

(E) E

9.

The sum of all question numbers whose answers are correct and the same as this one is:

 

(A) ∈ [59 . . 62]

(B) ∈ [52 . . 55]

(C) ∈ [44 . . 49]

(D) ∈ [59 . . 67]

(E) ∈ [44 . . 53]

10.

The answer to question 17 is:

 

(A) D

(B) B

(C) A

(D) E

(E) wrong

11.

The number of questions whose answer is D is:

 

(A) 2

(B) 3

(C) 4

(D) 5

(E) 6

12.

The number of other questions with the same answer as this one is the same as the number of questions with answer:

 

(A) B

(B) C

(C) D

(D) E

(E) none of those

13.

The number of questions whose answer is E is:

 

(A) 5

(B) 4

(C) 3

(D) 2

(E) 1

14.

No answer appears exactly this many times:

 

(A) 2

(B) 3

(C) 4

(D) 5

(E) none of those

15.

The set of odd-numbered questions with answer A is:

 

(A) {7}

(B) {9}

(C) not {11}

(D) {13}

(E) {15}

16.

The answer to question 8 is the same as the answer to question:

 

(A) 3

(B) 2

(C) 13

(D) 18

(E) 20

17.

The answer to question 10 is:

 

(A) C

(B) D

(C) B

(D) A

(E) correct

18.

The number of prime-numbered questions whose answers are vowels is:

 

(A) prime

(B) square

(C) odd

(D) even

(E) zero

19.

The last question whose answer is B is:

 

(A) 14

(B) 15

(C) 16

(D) 17

(E) 18

20.

The maximum score that can be achieved on this test is:

 

(A) 18

(B) 19

(C) 20

(D) indeterminate

(E) achievable only by getting this question wrong

▸ 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”?

1. (A) Answer 2 is B.

(B) Answer 1 is A.

2. (A) Answer 1 is correct.

(B) Either answer 2 is wrong or answer 1 is A, but not both.

72. [ HM28 ] Show that exercise 71 has a surprising, somewhat paradoxical answer if two changes are made to Table 666 : 9(E) becomes ‘∈ [39 .. 43]’; 15 (C) becomes ‘{11}’.

▸ 73. [ 30 ] ( A clueless anacrostic .) The letters of 29 five-letter words

1 2 3 4 5 ⁢ , 6 7 8 9 10 , 11 12 13 14 15 ,   16 17 18 19 20 ⁢ , . ⁢   . ⁢   . ⁢   , 141 142 143 144 145 ,  

all belonging to WORDS (1000), have been shuffled to form the following mystery text:

30 29 9 ⁢     140 12 13 145 90 45 99 ⁢     26 ⁢ 107 ⁢     47 84 53 51 27 133 39 ⁢     137 139 ⁢     66 112 69 14 ⁢ 8 20 91 129 70 16 ⁢ 7 93 19 85 ⁢     101 76 78 44 10 106 60 ⁢     118 119 ⁢     24 25 100 ⁢     1 5 64 11 71 ⁢     42 122 123 103 104 63 49 31 121 98 79 80 ⁢     ⁢ 46 48 ⁢     134 135 131     143 96 142 120 50 132 33 43 40 . ⁢                         . ⁢   . ⁢   . ⁢   111 97 113 105 38 102 62 65 114 ⁢     74 82 81 83 136 37 21 61 88 86 55 ⁢     ( 32 35 ⁢     ⁢ 117 116 23 53 56 17 18 94 67 ⁢     128 15 57 58 89 ⁢     87 109 ⁢     2 4 6 28 95 3 126 77 144 54 41 ⁢ ) ⁢     68 115 75 138 73 124 36 130 127 141 ⁢     22 92 ⁢     72 59 ⁢     108 125 ⁢ 110 .

Furthermore, their initial letters 1 , 6 , 11 , 16 , . ⁢ . ⁢ . ⁢   , 141 identify the source of that quotation, which consists entirely of common English words. What does it say?

74. [ 21 ] The fifteenth mystery word in exercise 73 is ‘ 134 135 131 ’. Why does its special form lead to a partial factorization of that problem?

▸ 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 , ... be the neighbors of v 0 that lie in H , followed by the neighbors of v 1 that haven’t yet been listed, and so on. (We assume that the neighbors of each vertex are listed in some fixed order.)

For example, if G is the 3 × 3 grid P 3 □ P 3 , exactly 21 of its connected five-element subsets contain the upper left corner element v . Their canonical orderings are

images

if we order the vertices top-to-bottom, left-to-right when listing their neighbors. (Vertices labeled ⓪, ①, ②, ③, ④ indicate ν 0 , ν 1 , ν 2 , ν 3 , ν 4 . Other vertices are not in H .)

Design a backtrack algorithm to generate all of the n -element connected subsets that contain a specified vertex v , given a graph that is represented in SGB format (which has ARCS , TIP , and NEXT fields, as described near the beginning of Chapter 7 ).

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 n □ P n have, for 1 ≤ n ≤ 9?

77. [ M22 ] A v - reachable subset of a directed graph G is a nonempty set of vertices H with the property that every u ∈ H can be reached from v by at least one oriented path in G | H . (In particular, v itself must be in H .)

  1. The digraph P 3 → □ P 3 → is like P 3 □ P 3 , except that all arcs between vertices are directed downward or to the right. Which of the 21 connected subsets in exercise 75 are also v -reachable from the upper left corner element v of P 3 → □ P 3 → ?

  2. True or false: H is v -reachable if and only if G | H contains a dual oriented spanning tree rooted at v . (An oriented tree has arcs u → p u , where p u is the parent of the nonroot node u ; in a dual oriented tree, the arcs are reversed: p u → u .)

  3. True or false: If G is undirected, so that w → u whenever u → w , its v - reachable subsets are the same as the connected subsets that contain v .

  4. Modify the algorithm of exercise 75 so that it generates all of the n -element v- reachable subsets of a digraph G , given n , v , and G .

78. [ 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 ≤ w < U .

▸ 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 to define the connections between keys and pipes. Therefore at most 2 164 different sounds are actually playable! The purpose of this exercise is to determine the exact number of n -pipe playable sounds, for small n .

The keys are binary vectors s = s 0 s 1 ...s 55 and g = g 0 g 1 ...g 55 ; the pedals are p = p 0 p 1 ... p 31 ; the console control switches are c = c 0 c 1 ... c 19 ; and the pipes are r i,j for 0 ≤ i < 16 and 0 ≤ j < 56. Here are the precise rules that define the pipe activity r i,j in terms of the input vectors s , g , p , and c that are governed by the organist:

r i , j \= { c i p j ⁢   v ⁢   c i + 15 p j − 12 , i ∈ { 0 , 1 } ; c i p j , i ∈ { 2 } ; r i , j \= { ( c i ⁢ v ⁢   c i + 1 [ j < 12 ] ) s j * , i ∈ { 3 } ; c i [ j ≥ 12 ] s j * , i ∈ { 4 , 8 } ; c i ⁢ s j * , i ∈ { 5 , 6 , 7 } ; r i , j \= { ( c i ∨ c i + 1 [ j < 12 ] ) g j * , i ∈ { 9 } ; c i [ j ⁢   ≥ 12 ] g j * , i ∈ { 10 } ; c i ⁢   g j * , i ∈ { 11 , 12 } ; ( c 13 ∨ c 14 ) g j * , i ∈ { 13 } ; c 14 ⁢ g j * , i ∈ { 14 , 15 } .

Here p j \= 0 if j < 0 or j ≥ 32; s j * \= s j ∨ c 17 g j ;   g j * \= g j ⁢   ∨ c 19 p j . [In organ jargon, the array of pipes has 16 “ranks”; ranks {0 , 1 , 2}, {3 ,..., 8}, {9 ,..., 15} constitute the Pedal, Swell, and Great divisions. Ranks 3 and 4 share their lower 12 pipes, as do ranks 9 and 10. Ranks 13, 14, and 15 form a “mixture,” c 14 . Unit ranks c 15 and c 16 extend ranks 0 and 1, twelve notes higher. Console switches c 17 , c 18 , c 19 are “couplers” Swell → Great, Swell → Pedal, Great → Pedal, which explain the formulas for s j * and g j * .]

A playable sound S is a set of pairs ( i, j ) such that we have r i,j \= [( i, j ) ∈ S ] for at least one choice of the input vectors s , g , p , c . For example, the first chord of Bach’s Toccata in D minor is the 8-pipe sound {(3 , 33) , (3 , 45) , (4 , 33) , (4 , 45) , (5 , 33) , (5 , 45) , (6 , 33) , (6 , 45)}, which is achievable when s 33 = s 45 = c 3 = c 4 = c 5 = c 6 = 1 and all other inputs are 0. We want to find the number Q n of playable sounds with ॥ S ॥ = n .

  1. There are 16 × 56 variables r i,j but only 812 actual pipes, because some of the ranks are incomplete. For which pairs ( i, j ) is r i,j always false?

  2. True or false: If s ⊆ s ′, g ⊆ g ′, p ⊆ p ′, and c ⊆ c ′, then r ⊆ r ′.

  3. Show that every playable sound is achievable with c 17 = c 18 = c 19 = 0.

  4. Find a 5-pipe playable sound in which just five of the s j , g j , p j , c j are nonzero.

  5. For which i and i ′ are the 2-pipe sounds {( i, 40) , ( i ′ , 50)} playable?

  6. Determine Q 1 by hand, and explain why it is less than 812.

  7. Determine Q 811 by hand.

  8. Determine Q 2 , ... , Q 10 by computer, and compare them to ( 812 2 ) , . ⁢   ⁢ . ⁢   ⁢ . ⁢   ⁢ , ( 812 10 ) .

We hold several threads in our hands,

and the odds are that one or other of them guides us to the truth.

We may waste time in following the wrong one,

but sooner or later we must come upon the right.

— SHERLOCK HOLMES, in The Hound of the Baskervilles (1901)

The following Receipts are not a mere marrow-less collection of

shreds, and patches, and cuttings, and pastings, from obsolete works,

but a bona fide register of practical facts . . .

the author submitting to a labour no preceding cookery-book-maker, perhaps,

ever attempted to encounter; and having not only dressed, but eaten

each Receipt before he set it down in his book.

— WILLIAM KITCHINER, Apicius Redivivus; Or, The Cook’s Oracle (1817)

Just as we hope you will learn from us, we have learned from you,

from the recipes and short cuts and tips and traditions

you have been kind enough to tell us about.

Without your help, truly, this book could not have been written.

— McCall’s Cook Book (1963)

What a dance
do they do
Lordy, how I’m tellin’ you!

— HARRY BARRIS, Mississippi Mud (1927)

Don’t lose your confidence if you slip,
Be grateful for a pleasant trip,
And pick yourself up, dust yourself off, start all over again.

— DOROTHY FIELDS, Pick Yourself Up (1936)

7.2.2.1. Dancing links

One of the chief characteristics of backtrack algorithms is the fact that they usually need to undo everything that they do to their data structures. In this section we’ll study some extremely simple link-manipulation techniques that modify and unmodify the structures with ease. We’ll also see that these ideas have many, many practical applications.

Suppose we have a doubly linked list, in which each node X has a predecessor and successor denoted respectively by LLINK(X) and RLINK(X) . Then we know that it’s easy to delete X from the list, by setting

RLINK(LLINK(X)) ← RLINK(X), LLINK (RLINK(X)) ←   LLINK(X). ( 1 )

At this point the conventional wisdom is to recycle node X , making it available for reuse in another list. We might also want to tidy things up by clearing LLINK(X) and RLINK(X) to ∧, so that stray pointers to nodes that are still active cannot lead to trouble. (See, for example, Eq. 2.2.5–(4), which is the same as ( 1 ) except that it also says ‘ AVAIL ⇐ X ’.) By contrast, the dancing-links trick resists any urge to do garbage collection. In a backtrack application, we’re better off leaving LLINK(X) and RLINK(X) unchanged. Then we can undo operation ( 1 ) by simply setting

RLINK(LLINK(X)) ← X, LLINK (RLINK(X)) ←  X. ( 2 )

For example, we might have a 4-element list, as in 2.2.5–(2):

images

If we use ( 1 ) to delete the third element, ( 3 ) becomes

images

And if we now decide to delete the second element also, we get

images

Subsequent deletion of the final element, then the first, will leave us with this:

images

The list is now empty, and its links have become rather tangled. (See exercise 1 .) But we know that if we proceed to backtrack at this point, using ( 2 ) to undelete elements 1, 4, 2, and 3 in that order, we will magically restore the initial state ( 3 ). The choreography that underlies the motions of these pointers is fun to watch, and it explains the name “dancing links.”

Exact cover problems

We will be seeing many examples where links dance happily and efficiently, as we study more and more examples of backtracking. The beauty of the idea can perhaps be seen most naturally in an important class of problems known as exact covering : We’re given an M × N matrix A of 0s and 1s, and the problem is to find all subsets of rows whose sum is exactly 1 in every column. For example, consider the 6 × 7 matrix

A \= ( 0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1 1 0 1 ) ( 5 )

Each row of A corresponds to a subset of a 7-element universe. A moment’s thought shows that there’s only one way to cover all seven of these columns with disjoint rows, namely by choosing rows 1, 4, and 5. We want to teach a computer how to solve such problems, when there are many, many rows and many columns.

Matrices of 0s and 1s appear frequently in combinatorial problems, and they help us to understand the relations between problems that are essentially the same although they appear to be different (see exercise 5 ). But inside a computer, we rarely want to represent an exact cover problem explicitly as a two-dimensional array of bits, because the matrix tends to be extremely sparse: There normally are very few 1s. Thus we’ll use a different representation, essentially with one node in our data structure for each 1 in the matrix.

Furthermore, we won’t even talk about rows and columns! Some of the exact cover problems we deal with already involve concepts that are called “rows” and “columns” in their own areas of application. Instead we will speak of options and items : Each option is a set of items; and the goal of an exact cover problem is to find disjoint options that cover all the items.

For example, we shall regard ( 5 ) as the specification of six options involving seven items. Let’s name the items a , b , c , d , e , f , g ; then the options are

‘ c ⁢   e ’ ; ‘ a ⁢   d ⁢   g ’ ; ‘ b ⁢   c ⁢   f ’ ; ‘ a ⁢   d ⁢   f ’ ; ‘ b ⁢   g ’ ; ‘ d ⁢   e ⁢   g ’ . ( 6 )

The first, fourth, and fifth options give us each item exactly once.

One of the nicest things about exact cover problems is that every tentative choice we make leaves us with a residual exact cover problem that is smaller — often substantially smaller. For example, suppose we try to cover item a in ( 6 ) by choosing the option ‘ a d g ’: The residual problem has only two options,

‘ c ⁢   e ’ and ‘ b ⁢   c ⁢   f ’ , ( 7 )

because the other four involve the already-covered items. Now it’s easy to see that ( 7 ) has no solution; therefore we can remove option ‘ a d g ’ from ( 6 ). That leaves us with only one option for item a , namely ‘ a d f ’. And its residual,

‘ c ⁢   e ’ and ‘ b ⁢   g ’ , ( 8 )

gives us the solution we were looking for.

Thus we’re led naturally to a recursive algorithm that’s based on the primitive operation of “ covering an item”: To cover item i, we delete all of the options that involve i, from our database of currently active options, and we also delete i from the list of items that need to be covered. The algorithm is then quite simple.

• Select an item i ⁢   that needs to be covered; but terminate ⁢                   successfully if none are left (we've found a solution). • If no active option involve i ,   terminate unsuccessfully                 ( there's no solution ) . ⁢     Otherwise cover item i . • For each just-deleted option O ⁢   that involves i ,   one at a time, do this:                   for each item j ≠ i   in O , cover item j ⁢   ; then                   recursively append O  to each solution of the residual problem. ( 9 )

(Everything that’s covered must later be uncovered, of course, as we’ll see.)

Interesting details arise when we flesh out this algorithm and look at appropriate low-level mechanisms. There’s a doubly linked “horizontal” list of all items that need to be covered; and each item also has its own “vertical” list of all the active options that involve it. For example, the data structures for ( 6 ) are:

images

(In this diagram, doubly linked pointers “wrap around” at the dotted lines.) The horizontal list has LLINK and RLINK pointers; the vertical lists have ULINK and DLINK . Nodes of each vertical list also point to their list header via TOP fields.

The top row of diagram ( 10 ) shows the initial state of the horizontal item list and its associated vertical headers. The other rows illustrate the six options of ( 6 ), which are represented by sixteen nodes within the vertical lists. Those options implicitly form horizontal lists, indicated by light gray lines; but their nodes don’t need to be linked together with pointers, because the option lists don’t change. We can therefore save time and space by allocating them sequentially. On the other hand, our algorithm does require an ability to traverse each option cyclically, in both directions. Therefore we insert spacer nodes between options. A spacer node x is identified by the condition TOP ( x ) ≤ 0; it also has

ULINK ( x ) =   address of the first node in the option before  x ; ULINK ( x ) =   address of the last node in the option after  x . ( 11 )

These conventions lead to the internal memory layout shown in Table 1 . First come the records for individual items; those records have NAME , LLINK , and RLINK fields, where NAME is used in printouts. Then come the nodes, which have TOP , ULINK , and DLINK fields. The TOP field is, however, called LEN in the nodes that serve as item headers, because Algorithm X below uses those fields to store the lengths of the item lists. Nodes 8, 11, 15, 19, 23, 26, and 30 in this example are the spacers. Fields marked ‘—’ are unused and can contain anything.

Table 1
THE INITIAL CONTENTS OF MEMORY CORRESPONDING TO ( 6 ) AND ( 10 )

i:

0

1

2

3

4

5

6

7

NAME ( i ):

—

a

b

c

d

e

f

g

LLINK ( i ):

7

0

1

2

3

4

5

6

RLINK ( i ):

1

2

3

4

5

6

7

0

x :

0

1

2

3

4

5

6

7

LEN ( x ):

—

2

2

2

3

2

2

3

ULINK ( x ):

—

20

24

17

27

28

22

29

DLINK ( x ):

—

12

16

9

13

10

18

14

x :

8

9

10

11

12

13

14

15

TOP ( x ):

0

3

5

−1

1

4

7

−2

ULINK ( x ):

—

3

5

9

1

4

7

12

DLINK ( x ):

10

17

28

14

20

21

25

18

x :

16

17

18

19

20

21

22

23

TOP ( x ):

2

3

6

−3

1

4

6

−4

ULINK ( x ):

2

9

6

16

12

13

18

20

DLINK ( x ):

24

3

22

22

1

27

6

25

x :

24

25

26

27

28

29

30

 

TOP ( x ):

2

7

−5

4

5

7

−6

 

ULINK ( x ):

16

14

24

21

10

25

27

 

DLINK ( x ):

2

29

29

4

5

7

—

 

OK, we’re ready now to spell out precisely what happens inside the computer’s memory when Algorithm X wants to cover the item whose number is i :

cover( i ) = { Set   p ← DLINK ( i ).   (Undeclared variables like p , l , r ⁢     are local. ) While p ≠ i ⁢ ,   hide( p ),  then set p ← DLINK ( P )  and repeat. Set     l ← LLINK ( i ) ,     r ← RLINK ( i ) ,                 RLINK ( i )    ← r ,     LLINK ( r )   ←   l . ( 12 )

  hide( p ) = { Set q ⁢   ← p + 1 , and repeat the following while q ⁢   ≠ p :     Set x ← TOP ( q ),   u ← ULINK ( q ), d ← DLINK ( q );     if x ⁢     ≤ 0 , set q ← u ⁢   ( q ⁢ was a spacer ) ; ⁢     otherwise set DLINK ( u ) ← d ,   ULINK ( d ) ← u ,            LEN ( x ) ← LEN ( x ) − 1 ,   q ← q + 1.     ( 13 )

And — here’s the point — those operations can readily be undone:

uncover ( i )       \=   { Set    l ← LLINK ( i ),   r ← RLINK ( i )                         RLINK ( l ) ← i ,     LLINK ( r ) ← i . Set   p ← ULINK ( i ). While   p ≠ i , unhide( p ), than set p ← ULINK ( p ) and repeat. ( 14 )

unhide( p ) \= { Set q ← p − 1 , and repeat the following while q ≠ p : Set x ← TOP ( q ), u ← ULINK ( q ), d ← DLINK( q ); if x ≤ 0 , set q ← d ⁢ ( q ⁢       was a spaer ) ; otherwise set DLINK ( u ) ← q ,   ULINK ( d )   ←   q ,                         LEN ( x ) ← LEN ( x ) +1, q ← q − 1. ( 15 )

We’re careful here to do everything backwards, using operation ( 2 ) inside ( 14 ) and ( 15 ) to undelete in precisely the reverse order of the way that we’d previously used operation ( 1 ) inside ( 12 ) and ( 13 ) to delete. Furthermore, we’re able to do this in place, without copying, by waltzing through the data structure at the same time as we’re modifying it.

Algorithm X ( Exact cover via dancing links ). This algorithm visits all solutions to a given exact cover problem, using the data structures just described, together with global variables N , Z , i , j , l , and p . It also maintains a list x 0 , x 1 , ..., x T of node pointers for backtracking, where T is large enough to accommodate one entry for each option in a partial solution.

X1. [Initialize.] Set the problem up in memory, as in Table 1 . (See exercise 8 .) Also set N to the number of items, Z to the last spacer address, and l ← 0.

X2. [Enter level l .] If RLINK (0) = 0 (hence all items have been covered), visit the solution that is specified by x 0 x 1 . . . x l −1 and go to X8. (See exercise 13 .)

X3. [Choose i .] At this point the items i 1 , . . . , i t still need to be covered, where i 1 = RLINK (0), i j +1 \= RLINK ( i j ), RLINK ( i t ) = 0. Choose one of them, and call it i . (The MRV heuristic of exercise 9 often works well in practice.)

X4. [Cover i .] Cover item i using ( 12 ), and set x l ← DLINK ( i ).

X5. [Try x l .] If x l \= i , go to X7 (we’ve tried all options for i ). Otherwise set p ← x l + 1, and do the following while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set p ← ULINK ( p ); otherwise cover( j ) and set p ← p + 1. (This covers the items ≠ i in the option that contains x l .) Set l ← l + 1 and return to X2.

X6. [Try again.] Set p ← x l − 1, and do the following while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set p ← DLINK ( p ); otherwise uncover( j ) and set p ← p − 1. (This uncovers the items ≠ i in the option that contains x l , using the reverse of the order in X5.) Set i ← TOP ( x l ), x l ← DLINK ( x l ), and return to X5.

X7. [Backtrack.] Uncover item i using ( 14 ).

X8. [Leave level l .] Terminate if l = 0. Otherwise set l ← l − 1 and go to X6.

The reader is strongly advised to work exercise 11 now — yes, now, really! — in order to experience the dance steps of this instructive algorithm. When the procedure terminates, all of the links will be restored to their original settings.

We’re going to see lots of applications of Algorithm X , and similar algorithms, in this section. Let’s begin by fulfilling a promise that was made on page 2 of Chapter 7 , namely to solve the problem of Langford pairs efficiently by means of dancing links.

The task is to put 2 n numbers {1, 1, 2, 2, ..., n , n } into 2 n slots s 1 s 2 ... s 2 n , in such a way that exactly i numbers fall between the two occurrences of i . It illustrates exact covering nicely, because we can regard the n values of i and the 2 n slots s j as items to be covered. The allowable options for placing the i ’s are then

‘ i ⁢     s j ⁢   s k ’ , for 1 ≤ j ⁢   < k ≤ 2 n , k \= i + j + 1 , 1 ≤ i ≤ n ; ( 16 )

for example, when n = 3 they’re

‘ 1 ⁢   s 1 ⁢   s 3 ’ ,   ‘ 1 s 2   s 4 ’ , ⁢      ‘ 1 s 3   s 5 ’ ,   ‘ 1 s 4   s 6 ’ , ⁢    ‘ 2 s 1   s 4 ’ ,   ‘ 2 s 2   s 5 ’ ,   ‘ 2 s 3   s 6 ’ ,   ‘ 3 s 1   s 5 ’ , ⁢    ‘ 3 s 2   s 6 ’ . ( 17 )

An exact covering of all items is equivalent to placing each pair and filling each slot. Algorithm X quickly determines that ( 17 ) has just two solutions,

‘3 s 1 s 5 ’, ‘2 s 3 s 6 ’, ‘1 s 2 s 4 ’   and   ‘3 s 2 s 6 ’, ‘2 s 1 s 4 ’, ‘1 s 3 s 5 ’,

corresponding to the placements 3 1 2 1 3 2 and 2 3 1 2 1 3. Notice that those placements are mirror images of each other; exercise 15 shows how to save a factor of 2 and eliminate such symmetry, by omitting some of the options in ( 16 ).

With that change, there are exactly 326,721,800 solutions when n = 16, and Algorithm X needs about 1.13 trillion memory accesses to visit them all. That’s pretty good — it amounts to roughly 3460 mems per solution, as the links whirl.

Of course, we’ve already looked at a backtrack procedure that’s specifically tuned to the Langford problem, namely Algorithm 7.2.2L near the beginning of Section 7.2.2 . With the enhancement of exercise 7.2.2–21 , that one handles the case n = 16 somewhat faster, finishing after about 400 billion mems. But Algorithm X can be pleased that its general-purpose machinery isn’t way behind the best custom-tailored method.

Secondary items

Can the classical problem of n queens also be formulated as an exact cover problem? Yes, of course! But the construction isn’t quite so obvious. Instead of setting the problem up as we did in 7.2.2–( 3 ), where we chose a queen placement for each row of the board, we shall now allow both rows and columns to participate equally when making the necessary choices.

There are n 2 options for placing queens, and we want exactly one queen in every row and exactly one in every column. Furthermore, we want at most one queen in every diagonal. More precisely, if x ij is the binary variable that signifies a queen in row i and column j , we want

∑ i \= 1 n ⁢ x i j \= 1 for 1 ≤ j ⁢ ≤ n ; ∑ j \= 1 n ⁢ x i j \= 1 for 1 ≤ i ≤ n ; ( 18 )

∑ { x i j ⁢ |   1 ≤ i , j ≤ n ,   i + j \= s } ≤ 1 for 1 < s ≤ 2 n ; ( 19 )

∑ { x i j ⁢ |   1 ≤ i , j ≤ n ,   i − j \= d } ≤ 1 for − n < d ⁢ <   n . ( 20 )

The inequalities in ( 19 ) and ( 20 ) can be changed to equalities by introducing “ slack variables” u 2 , ..., u 2 n , υ − n +1 , ..., υ n −1 , each of which is 0 or 1:

∑ { x i j ⁢ |   1 ≤ i , j ≤ n ,   i + j \= s } + u s \= 1 for 1 < s ≤ 2   n ; ( 21 )

∑ { x i j ⁢ |   1 ≤ i , j ≤ n ,   i − j \= d } + u d \= 1 for −   n < d <   n . ( 22 )

Thus we’ve shown that the problem of n nonattacking queens is equivalent to the problem of finding n 2 + 4 n − 2 binary variables x ij , u s , υ d for which certain subsets of the variables sum to 1, as specified in ( 18 ), ( 21 ), and ( 22 ).

And that is essentially an exact cover problem, whose options correspond to the binary variables and whose items correspond to the subsets. The items are r i , c j , a s , and b d , representing respectively row i , column j , upward diagonal s , and downward diagonal d . The options are ‘ r i c j a i+j b i−j ’ for queen placements, together with trivial options ‘ a s ’ and ‘ b d ’ to take up any slack.

For example, when n = 4 the n 2 placement options are

‘ r 1 ⁢ ⁢   c 1   a 2 ⁢     b 0 ’ ; ‘ r 1 ⁢ ⁢   c 2   a 3 ⁢     b −   1 ’ ; ‘ r 1 ⁢ ⁢   c 3 ⁢ ⁢   a 4   b − 2 ⁢ ’ ; ‘ r 1 ⁢ ⁢   c 4 ⁢ ⁢   a 5   b − 3 ⁢ ’ ; ‘ r 2 ⁢ ⁢   c 1   a 3 ⁢     b 1 ’ ; ‘ r 2 ⁢ ⁢   c 2   a 4 ⁢     b 0 ’ ; ‘ r 2 ⁢ ⁢   c 3 ⁢ ⁢   a 5   b − 1 ⁢ ’ ; ‘ r 2 ⁢ ⁢   c 4 ⁢ ⁢   a 6   b − 2 ⁢ ’ ; ‘ r 3 ⁢ ⁢   c 1   a 4 ⁢     b 2 ’ ; ‘ r 3 ⁢ ⁢   c 2   a 5 ⁢     b   1 ’ ; ‘ r 3 ⁢ ⁢   c 3 ⁢ ⁢   a 6   b 0 ⁢ ’ ; ’ r 3 ⁢ ⁢   c 4 ⁢ ⁢   a 7   b − 1 ⁢ ’ ; ’ r 4 ⁢ ⁢   c 1   a 5 ⁢     b 3 ’ ; ’ r 4 ⁢ ⁢   c 2   a 6 ⁢     b 2 ’ ; ’ r 4 ⁢ ⁢   c 3 ⁢ ⁢   a 7   b 1 ⁢ ’ ; ‘ r 4 ⁢ ⁢   c 4 ⁢ ⁢   a 8   b 0 ⁢ ’ ; ( 23 )

and the 4 n − 2 slack options (which contain just one item each) are

‘ a 2 ’ ; ‘ a 3 ’ ; ‘ a 4 ’ ; ‘ a 5 ’ ; ‘ a 6 ’ ; ‘ a 7 ’ ; ‘ a 8 ’ ; ‘ b − 3 ’ ; ‘ b − 2 ’ ; ‘ b − 1 ’ ; ‘ b 0 ’ ; ‘ b 1 ’ ; ‘ b 2 ’ ; ‘ b 3 ’ ;   ( 24 )

Algorithm X will solve this small problem easily, although its treatment of the slacks is somewhat awkward (see exercise 16 ).

A closer look shows, however, that a slight change to Algorithm X will allow us to avoid slack options entirely! Let’s divide the items of an exact cover problem into two groups: primary items, which must be covered exactly once, and secondary items, which must be covered at most once. If we simply modify step X1 so that only the primary items appear in the active list, everything will work like a charm. (Think about it.) In fact, the necessary changes to step X1 already appear in the answer to exercise 8 .

Secondary items turn out to be extremely useful in applications. So let’s redefine the exact cover problem, taking them into account: Henceforth we shall assume that an exact cover problem involves N distinct items, of which N 1 are primary and N 2 = N − N 1 are secondary. It is defined by a family of options, each of which is a subset of the items. Every option must include at least one primary item. The task is to find all subsets of the options that (i) contain every primary item exactly once, and (ii) contain every secondary item at most once.

(Options that are purely secondary are excluded from this new definition, because they will never be chosen by Algorithm X as we’ve refined it. If for some reason you don’t like that rule, you can always go back to the idea of slack options. Exercise 19 discusses another interesting alternative.)

The order in which primary items appear in Algorithm X ’s active list can have a significant effect on the running time, because the implementation of step X3 in exercise 9 selects the first item of minimum length. For example, if we consider the primary items of the n queens problem in the natural order r 1 , c 1 , r 2 , c 2 , ..., r n , c n , queens tend to be placed at the top and left before we try to place them at the bottom and right. By contrast, if we use the “ organ-pipe order” r ⌊ n / 2 ⌋ + 1   ⁢ ,   c ⌊ n / 2 ⌋ + 1   ⁢ ,   r ⌊ n / 2 ⌋   , c ⌊ n / 2 ⌋   , r ⌊ n / 2 ⌋   + 2 , c ⌊ n / 2 ⌋   + 2 , r ⌊ n / 2 ⌋   − 1 , c ⌊ n / 2 ⌋   − 1 , . . . , ( r 1 or r n ), ( c 1 or c n ), the queens are placed first in the center, where they prune the remaining possibilities more effectively. For example, the time needed to find all 14772512 ways to place 16 queens is 76 G μ (gigamems) with the natural order, but only 40 G μ with the organ-pipe order.

These running times can be compared with 9 G μ , which we obtained using efficient bitwise operations with Algorithm 7.2.2W . Although that algorithm was specially tuned, the general-purpose dancing links technique runs only five times slower. Furthermore, the setup we’ve used here allows us to solve other problems that wouldn’t be anywhere near as easy with ordinary backtrack. For example, we can limit the solutions to those that contain queens on each of the 2 + 4 l longest diagonals, simply by regarding a s and b d as primary items instead of secondary, for | n +1 − s | ≤ l and | d | ≤ l . (There are 18048 such solutions when n = 16 and l = 4, found with organ-pipe order in 2.7 G μ .)

We can also use secondary items to remove symmetry, so that most of the solutions are found only once instead of eight times (see exercises 22 and 23 ). The central idea is to use a pairwise ordering trick that works also in many other situations. Consider the following family of 2 m options:

α j \= ‘ a ⁢   x 0 ⁢   . ⁢ . ⁢ . ⁢   x j − 1 ’ and β j \= ‘ b ⁢   x j ’ for 0 ≤ j < m , ( 25 )

where a and b are primary while x 0 , x 1 , ..., x m −1 are secondary. For example, when m = 4 the options are

α 0 \= ‘ a ’ ; β 0 \= ‘ b ⁢   x 0 ’ ; α 1 \= ‘ a ⁢   x 0 ’ ; β 1 \= ‘ b ⁢   x 1 ’ ; α 2 \= ‘ a ⁢   x 0 ⁢   x 1 ’ ; β 2 \= ‘ b ⁢   x 2 ’ ; α 3 \= ‘ a ⁢   x 0 ⁢   x 1 ⁢   x 2 ’ ; β 3 \= ‘ b ⁢   x 3 ’ .

It’s not hard to see that there are exactly ( m + 1 2 ) ways to cover both a and b , namely to choose α j and β k with 0 ≤ j ≤ k < m . For if we choose α j , the secondary items x 0 through x j −1 knock out the options β 0 , ..., β j −1 .

This construction involves a total of ( m + 1 2 ) entries in the α options and 2 m entries in the β options. But exercise 20 shows that it’s possible to achieve pairwise ordering with only O ( m log m ) entries in both α ’s and β ’s. For example, when m = 4 it produces the following elegant pattern:

α 0 \= ‘ a ’ ; β 0 \= ‘ b ⁢   y 1 ⁢   y 2 ’ ; α 1 \= ‘ a ⁢   y 1 ’ ; β 1 \= ‘ b ⁢   y 2 ’ ; α 2 \= ‘ a ⁢   ⁢   y 2 ’ ; β 2 \= ‘ b ⁢   y 3 ’ ; α 3 \= ‘ a ⁢    y 3 ⁢   y 2 ’ ; β 3 \= ‘ b ⁢   ’ . ( 26 )

Progress reports

Many of the applications of Algorithm X take a long time, especially when we’re using it to solve a tough problem that is breaking new ground. So we don’t want to just start it up and wait with our fingers crossed, hoping that it will finish soon. We really want to watch it in action and see how it’s doing. How many more hours will it probably run? Is it almost half done?

A simple amendment to step X2 will alleviate such worries. At the beginning of that step, as we enter a new node of the search tree, we can test whether the accumulated running time T has passed a certain threshold Θ, which is initially set to Δ. If T ≥ Θ, we print a progress report and set Θ ← Θ + Δ. (Thus if Δ = ∞, we get no reports; if Δ = 0, we see a report at each node.)

The author’s experimental program measures time in mems, so that he can obtain machine-independent results; and he usually takes Δ = 10 G μ . His program has two main ways to show progress, namely a long form and a short form. The long form gives full details about the current state of the search, based on exercise 12 . For example, here’s the first progress report that it displays when finding all solutions to the 16 queens problem as described above:

Current state (level 15):
 r8 c3 ab ba (4 of 16)
 c8 a8 bn r0 (1 of 13)
 r7 cb ai bj (7 of 10)
 r6 c4 aa bd (2 of 7)
  . . .
 3480159 solutions, 10000000071 mems, and max level 16 so far.

(The computer’s internal encoding for items is different from the conventions we have used; for example, ‘ r8 c3 ab ba ’ stands for what we called ‘ r 9 c 4 a 13 b 5 ’. The first choice at level 0 was to cover item r8 , meaning to put a queen into row 9. The fourth of 16 options for that item has placed it in column 4. Then at level 1 we tried the first of 13 ways to cover item c8 , meaning to put a queen into column 9. And so on. At each level, the leftmost item shown for the option being tried is the one that was chosen in step X3 for branching.)

That’s the long form. The short form, which is the default, produces just one line for each state report:

10000000071mu: 3480159 sols, 4g 1d 7a 27 36 24 23 13 12 12 22 12 ... .19048
20000000111mu: 6604373 sols, 7g cd 6a 88 36 35 44 44 24 11 12 22 .43074
30000000052mu: 9487419 sols, bg cd 9a 68 37 35 24 13 12 12 .68205
40000000586mu: 12890124 sols, fg 6d aa 68 46 35 23 33 23 .90370
Altogether 14772512 solutions, 62296+45565990457 mems, 193032021 nodes.

Two-character codes are used to indicate the current position in the tree; for example, ‘ 4g ’ means branch 4 of 16, then ‘ 1d ’ means branch 1 of 13, etc. By watching these steadily increasing codes — it’s fun! — we can monitor the action.

Each line in the short form ends with an estimate of how much of the tree has been examined, assuming that the search tree structure is fairly consistent. For instance, ‘ .19048 ’ means that we’re roughly 19% done. If we’re currently working at level l on choice c l of t l , this number is computed by the formula

c 0 − 1 t 0 + c 1 − 1 t 0 ⁢   t 1 + ⋅ ⋅ ⋅ + c 1 − 1 t 0 ⁢   t 1 . ⁢   . ⁢   . ⁢   t l + 1 2 t 0 ⁢   t 1 . ⁢   . ⁢   . t l . ( 27 )

Sudoku

A “sudoku square” is a 9 × 9 array that has been divided into 3 × 3 boxes and filled with the digits {1, 2, 3, 4, 5, 6, 7, 8, 9} in such a way that

  • every row contains each of the digits {1, 2, 3, 4, 5, 6, 7, 8, 9} exactly once;

  • every column contains each of the digits {1, 2, 3, 4, 5, 6, 7, 8, 9} exactly once;

  • every box contains each of the digits {1, 2, 3, 4, 5, 6, 7, 8, 9} exactly once.

(Since there are nine cells in each row, each column, and each box, the words ‘exactly once’ can be replaced by ‘at least once’ or ‘at most once’, anywhere in this definition.) Here, for example, are three highly symmetrical sudoku squares:

images

When the square has been only partially specified, the task of completing it — by filling in the blank cells — often turns out to be a fascinating challenge. Howard Garns used this idea as the basis for a series of puzzles that he called Number Place, first published in Dell Pencil Puzzles & Word Games #16 (May 1979), 6. The concept soon spread to Japan, where Nikoli Inc. gave it the name Su Doku ( images , “Unmarried Numbers”) in 1984; and eventually it went viral. By the beginning of 2005, major newspapers had begun to feature daily sudoku puzzles. Today, sudoku ranks among the most popular recreations of all time.

Every sudoku puzzle corresponds to an exact cover problem that has a particularly nice form. Consider, for example, the following three instances:

images

(The clues in (29a) match the first 32 digits of π ; but the clues in (29b) and (29c) disagree with π after awhile.) For convenience, let’s number the rows, columns, and boxes from 0 to 8. Then every sudoku square S = ( s ij ) corresponds naturally to the solution of a master exact cover problem whose 9 · 9 · 9 = 729 options are

‘ p i j ⁢       r i k ⁢     c j k ⁢     b x k ’ for 0 ≤ i , j < 9 , 1 ≤ k ≤ 9 , and   x \= 3 ⌊ i / 3 ⌋ + ⌊ j / 3 ⌋ , ( 30 )

and whose 4 · 9 · 9 = 324 items are p ij , r ik , c jk , b xk . The reason is that option ( 30 ) is chosen with parameters ( i, j, k ) if and only if s ij \= k . Item p ij must be covered by exactly one of the nine options that fill cell ( i, j ); item r ik must be covered by exactly one of the nine options that put k in row i ; ...; item b xk must be covered by exactly one of the nine options that put k in box x . Got it?

My own motive for writing on the subject is partly to justify
the appalling number of hours I have squandered solving Sudoku.

— BRIAN HAYES, in American Scientist (2006)

To find all sudoku squares that contain a given partial specification, we simply remove all of the items p ij , r ik , c jk , b xk that are already covered, as well as all of the options that involve any of those items. For example, (29a) leads to an exact cover problem with 4 · (81 − 32) = 196 items p 00 , p 01 , p 03 , ..., r 02 , r 04 , r 05 , ..., c 01 , c 06 , c 07 , ..., b 07 , b 08 , b 09 , ... ; it has 146 options, beginning with ‘ p 00 r 07 c 07 b 07 ’ and ending with ‘ p 88 r 86 c 86 b 86 ’. These options can be visualized by making a chart that shows every value that hasn’t been ruled out:

images

The active list for item p 00 , say, has options for values {7, 8, 9}; the active list for item r 02 has options for columns {5, 6, 7}; the active list for item c 01 has options for rows {4, 5, 6, 7}; and so on. (Indeed, sudoku experts tend to have charts like this in mind, implicitly or explicitly, as they work.)

Aha! Look at the lonely ‘5’ in the middle! There’s only one option for p 44 ; so we can promote that ‘5’ to ‘5’ . Hence we can also erase the other ‘5’ s that appear in row 4, column 4, or box 4. This operation is called “ forcing a naked single.”

And there’s another naked single in cell (8, 4). Promoting this one from ‘4’ to ‘4’ produces others in cells (7, 4) and (8, 2). Indeed, if the items p ij have been placed first in step X1, Algorithm X will follow a merry path of forced moves that lead immediately to a complete solution of (29a), entirely via naked singles.

Of course sudoku puzzles aren’t always this easy. For example, (29b) has only 17 clues, not 32 ; that makes naked singles less likely. (Puzzle (29b) comes from Gordon Royle’s online collection of approximately 50,000 17-clue sudokus — all of which are essentially different, and all of which have a unique completion despite the paucity of clues. Royle’s collection appears to be nearly complete: Whenever a sudoku fanatic encounters a 17-clue puzzle nowadays, that puzzle almost invariably turns out to be equivalent to one in Royle’s list.)

A massive computer calculation, supervised by Gary McGuire and completed in 2012, has shown that every uniquely solvable sudoku puzzle must contain at least 17 clues . We will see in Section 7.2.3 that exactly 5,472,730,538 nonisomorphic sudoku squares exist. McGuire’s program examined each of them, and showed that comparatively few 16-clue subsets could possibly characterize it. About 16,000 subsets typically survived this initial screening; but they too were shown to fail. All this was determined in roughly 3.6 seconds per sudoku square, thanks to nontrivial and highly optimized bitwise algorithms. [See G. McGuire, B. Tugemann, and G. Civario, Experimental Mathematics 23 (2014), 190–217.]

The 17 clues of puzzle (29b) produce the following chart analogous to ( 31 ):

images

This one has 307 options remaining — more than twice as many as before. Also, as we might have guessed, it has no naked singles. But it still reveals forced moves, if we look more closely! For example, column 3 contains only one instance of ‘3’ ; we can promote it to ‘3’ , and kill all of the other ‘3’ s in row 2 and box 1. This operation is called “forcing a hidden single.”

Similarly, box 2 in ( 32 ) contains only one instance of ‘4’ ; and two other hidden singles are also present (see exercise 47 ). These forced moves cause other hidden singles to appear, and naked singles also arise soon. But after 16 forced promotions have been made, the low-hanging fruit is all gone:

images

Algorithm X readily deduces ( 33 ) from ( 32 ), because it sees naked singles and hidden singles whenever an item p ij or r ik or c jk or b xk has only one remaining option, and because its data structures change easily as the links dance. But when state ( 33 ) is reached, the algorithm resorts to two-way branching, in this case looking first at case ‘7’ of p 16 , then backtracking later to consider case ‘8’ .

A human sudoku expert would actually glance at ( 33 ) and notice that there’s a more intelligent way to proceed, because ( 33 ) contains a “naked pair”: Cells (4, 3) and (4, 8) both contain the same two choices; hence we’re allowed to delete ‘1’ and ‘7’ wherever they appear elsewhere in row 4; and this will produce a naked ‘4’ in column 1. Exercise 49 explores such higher-order deductions in detail.

Fancy logic that involves pairs and triples might well be preferable for earth-lings, but simple backtracking works just fine for machines. In fact, Algorithm X finds the solution to (29b) after exploring a search tree with just 89 nodes, the first 16 of which led it directly to ( 33 ). (It spends about 250 kilomems initializing the data structures in step X1, then 50 more kilomems to complete the task. Much more time would have been needed if it had tried to look for complicated patterns in step X3.) Here’s the solution that it discovers:

c33 b13 p23 r23 (1 of 1)
r13 c13 b03 p11 (1 of 1)
 ˙˙˙
c42 b72 p84 r82 (1 of 1)
p16 r18 c68 b28 (2 of 2)
b27 p18 r17 c87 (1 of 1)
 ˙˙˙
p85 r81 c51 b71 (1 of 1)

After it selects the correct value for column 6 of row 1, the rest is forced.

The dancing links method actually cruises to victory with amazing speed, on almost every known sudoku puzzle. Among several dozen typical specimens — seen by the author since 2005 in newspapers, magazines, books, and webpages worldwide, and subsequently presented to Algorithm X — roughly 70% were found to be solvable entirely by forced moves, based on naked or hidden singles, even though many of those puzzles had been rated ‘diabolical’ or ‘fiendish’ or ‘torturous’! Only 10% of them led to a search tree exceeding 100 nodes, and none of the trees had more than 282 nodes. (See, however, exercise 52 .)

It’s interesting to consider what happens when the algorithm is weakened, so that its forced moves come only from naked singles, which are the easiest deductions for people to make. Suppose we classify the items r ik , c jk , and b xk as secondary , leaving only p ij as primary. (In other words, the specification will require at most one occurrence of each value k , in every row, every column, and every box, but it won’t explicitly insist that every k should be covered.) The search tree for puzzle (29b) then grows to a whopping 41,877 nodes.

Finally, what about puzzle (29c)? That one has only 16 clues, so we know that it cannot have a unique solution. But those 16 clues specify only seven of the nine digits; they give us no way to distinguish 7 from 8 . Algorithm X deduces, with a 129-node search tree, that only two solutions exist. (Of course those two are essentially the same; they’re obtainable from each other by swapping 7 ↔ 8 .)

Puzzlists have invented many intriguing variations on the traditional sudoku challenge, several of which are discussed in the exercises below. Among the best are “ jigsaw sudoku puzzles” (also known as “ geometric sudoku,” “ polyomino sudoku,” “ squiggly sudoku,” etc.), where the boxes have different shapes instead of simply being 3 × 3 subsquares. Consider, for example,

images

In puzzle (34a), which the author designed in 2017 with the help of Bob Harris, one can see for instance that there are only two places to put a ‘ 4 ’ in the top row, because of the ‘ 4 ’ in the next row. This puzzle is an exact cover problem just like ( 30 ), except that x is now a more complicated function of i and j . Similarly, Harris’s classic puzzle (34b) [ Mathematical Wizardry for a Gardner (2009), 55– 57] asks us to put the letters { G , R , A , N , D , T , I , M , E } into each row, column, and irregularly shaped box. Again we use ( 30 ), but with the values of k running through letters instead of digits. [ Hint: Cell (0, 2) must contain ‘ A ’, because column 2 needs an ‘ A ’ somewhere.] Puzzle (34c), The United States Jigsaw Sudoku, is a masterpiece designed and posted online by Thomas Snyder in 2006. It brilliantly uses boxes in the shapes of West Virginia, Kentucky, Wyoming, Alabama, Florida, Nevada, Tennessee, New York (including Long Island), and Virginia — and its clues are postal codes! (See exercise 59 .)

Jigsaw sudoku was invented by J. Mark Thompson, who began to publish such puzzles in 1996 [ GAMES World of Puzzles , #14 (July 1996), 51, 67] under the name Latin Squares. At that time he had not yet heard about sudoku; one of the advantages of his puzzles over normal sudoku was the fact that they can be of any size, not necessarily 9 × 9. Thompson’s first examples were 6 × 6.

The solutions to puzzles of this kind actually have an interesting prehistory: Walter Behrens, a pioneer in the applications of mathematics to agriculture, wrote an influential paper in 1956 that proposed using such patterns in empirical studies of crops that have been treated with various fertilizers [ Zeitschrift für landwirtschaftliches Versuchs- und Untersuchungswesen 2 (1956), 176–193]. He presented dozens of designs, ranging from 4 × 4 to 10 × 10, including

images

Notice that Behrens’s (35b) is actually 9 × 7, so its rows don’t exhibit all 9 possibilities. He required only that no treatment number be repeated in any row or column. Notice also that his (35c) is actually a normal sudoku arrangement; this is the earliest known publication of what is now called a sudoku solution. Following a suggestion of F. Ragaller, Behrens called these designs “ gerechte” (“equitable”) latin squares or latin rectangles, because they assign neighborhood groupings to tracts of land that have been subjected to all n treatments.

All of his designs were partitions of rectangles into connected regions, each with n square cells. We’ll see next that that idea actually turns out to have its own distinguished history of fascinating combinatorial patterns and recreations.

Polyominoes

A rookwise-connected region of n square cells is often called an n-omino , following a suggestion by S. W. Golomb [ AMM 61 (1954), 675–682]. When n = 1, 2, 3, ..., Golomb’s definition gives us monominoes, dominoes, trominoes, tetrominoes, pentominoes, hexominoes, and so on. In general, when n is unspecified, Golomb called such regions polyominoes .

We’ve already encountered small polyominoes, together with their relation to exact covering, in 7.1.4–(130). It’s clear that a domino has only one possible shape. But there are two distinct species of trominoes, one of which is “ straight” (1 × 3) and the other is “ bent,” occupying three cells of a 2 × 2 square. Similarly, the tetrominoes can be classified into five distinct types. (Can you draw all five, before looking at exercise 274 ? Tetris ® players will have no trouble doing this.)

The most piquant polyominoes, however, are almost certainly the pentominoes , of which there are twelve. These twelve shapes have become the personal friends of millions of people, because they can be put together in so many elegant ways. Sets of pentominoes, made from finely crafted hardwoods or from brilliantly colored plastic, are readily available at reasonable cost. Every home really ought to have at least one such set — even though “virtual” pentominoes can easily be manipulated in computer apps — because there’s no substitute for the strangely fascinating tactile experience of arranging these delightful physical objects by hand. Furthermore, we’ll see that pentominoes have much to teach us about combinatorial computing.

If mounted on cardboard, [these pieces]
will form a source of perpetual amusement in the home.

— HENRY E. DUDENEY, The Canterbury Puzzles (1907)

Which English nouns ending in -o pluralize with -s and which with -es?
If the word is still felt as somewhat alien, it takes -s,
while if it has been fully naturalized into English, it takes -es.
Thus, echoes, potatoes, tomatoes, dingoes, embargoes, etc.,
whereas Italian musical terms are altos, bassos, cantos, pianos, solos, etc.,
and there are Spanish words like tangos, armadillos, etc.
I once held a trademark on ‘Pentomino(-es)’, but I now prefer
to let these words be my contribution to the language as public domain.

— SOLOMON W. GOLOMB, letter to Donald Knuth (16 February 1994)

One of the first things we might try to do with twelve pieces of 5 cells each is to pack them into a rectangular box, either 6 × 10 or 5 × 12 or 4 × 15 or 3 × 20. The first three tasks are fairly easy; but a 3 × 20 box presents more of a challenge. Golomb posed this question in his article of 1954, without providing any answer. At that time he was unaware that Frans Hansson had already given a solution many years earlier, in an obscure publication called The Problemist: Fairy Chess Supplement 2 , 12 and 13 (June and August 1935), problem 1844:

images

Hansson had in fact observed that the bracketed pieces “may also be rotated through two right angles, to give the only other possible solution.”

This problem, and many others of a similar kind, can be formulated nicely in terms of exact covering. But before we do this, we need names for the individual pentomino shapes. Everybody agrees that seven of the pentominoes should be named after seven consecutive letters of the alphabet:

images

But two different systems of nomenclature have been proposed for the other five:

images

where Golomb liked to think of the word ‘Filipino’ while Conway preferred to map the twelve pentominoes onto twelve consecutive letters from O to Z. Conway’s scheme tends to work better in computer programs, so we’ll use it here.

The task of 3 × 20 pentomino packing is to arrange pentominoes in such a way that every piece name { O , P , ..., Z } is covered exactly once, and so is every cell ij for 0 ≤ i < 3 and 0 ≤ j < 20. Thus there are 12 + 3 · 20 = 72 items; and there’s an option for each way to place an individual pentomino, namely

‘ 0 ⁢   00 ⁢    01 ⁢   02 ⁢   03 ⁢   04 ’ . ⁢ . ⁢ . ‘ 0 ⁢    2 f 2g 2h 2i 2j ’ ‘ p 00 01 02 10 11 ’ . ⁢ . ⁢ . ‘ z 0j 1h 1i 1j 2h ’ , ( 37 )

if we extend hexadecimal notation so that the “digits” ( a , b , ..., j ) represent (10, 11, ..., 19). In this list, pieces ( O , P , ..., Z ) contribute respectively (48, 220, 136, 144, 136, 72, 110, 72, 72, 18, 136, 72) options, making 1236 altogether. Exercise 266 explains how to generate all of the options for problems like this.

When Algorithm X is applied to ( 37 ), it finds eight solutions, because each of Hansson’s arrangements is obtained with horizontal and/or vertical reflection. We can remove that symmetry by insisting that the V pentomino must appear in its ‘Γ-like’ orientation, as it does in ( 36 ), namely by removing all but 18 of its 72 options. (Do you see why? Think about it.) Without that simplification, a 32,644-node search tree finds 8 solutions in 146 megamems; with it, a 21,805-node search tree finds 2 solutions in 103 megamems.

A closer look shows that we can actually do much better. For example, one of the Γ-like options for V is ‘ V 09 0a 0b 19 29 ’, representing

images

but this placement could never be used, because it asks us to pack pentominoes into the 27 cells at V’s left. Many of the options for other pieces are similarly unusable, because (like ( 38 )) they isolate a region whose area isn’t a multiple of 5.

In fact, if we remove all such options, only 728 of the original 1236 potential placements remain; they include respectively (48, 156, 132, 28, 128, 16, 44, 16, 12, 4, 128, 16) placements of ( O , P , ..., Z ). That gives us 716 options, when we remove also the 12 surviving placements for V that make it non-‘Γ’. When Algorithm X is applied to this reduced set, the search tree for finding all solutions goes down to 1243 nodes, and the running time is only 4.5 megamems.

(There’s also a slightly better way to remove the symmetry: Instead of insisting that piece V looks like ‘Γ’ we can insist that piece X lies in the left half, and that piece Z hasn’t been “flipped over.” This implies that there are (16, 2, 8) potential placements for ( V , X , Z ), instead of (4, 4, 16). The resulting search tree has just 1128 nodes, and the running time is 4.0 M μ .)

Notice that we could have begun with a weaker formulation of this problem: We could merely have asked for pentomino arrangements that use each piece at most once, while covering each cell ij exactly once. That would be essentially the same as saying that the piece names { O , P , ..., Z } are secondary items instead of primary. Then the original set of 1236 options in ( 37 ) would have led to a search tree with 61,851 nodes, and a runtime of 291 M μ . Dually, we could have kept the piece names primary but made the cell names secondary; that would have yielded a 1,086,521,921-node tree, with a runtime of 2.94 T μ ! These statistics are curiously reversed , however, with respect to the reduced set of 716 options obtained by discarding cases like ( 38 ): Then piece names secondary yields 19306 nodes (68 M μ ); cell names secondary yields 11656 nodes (37 M μ ).

In the early days of computing, pentomino problems served as useful benchmarks for combinatorial calculations. Programmers didn’t have the luxury of large random-access memory until much later; therefore techniques such as dancing links, in which more than a thousand options are explicitly listed and manipulated, were unthinkable at the time. Instead, the options for each piece were implicitly generated on-the-fly as needed, and there was no incentive to use fancy heuristics while backtracking. Each branch of the search was essentially based on the available ways to cover the first cell ij that hadn’t yet been occupied.

We can simulate the behavior of those historic methods by running Algorithm X without the MRV heuristic and simply setting i ← RLINK( 0 ) in step X3. An interesting phenomenon now arises: If the cells ij are considered in their natural order — first 00 , then 01 , ..., then 0j , then 10 , ..., finally 2j — the search tree has 1.5 billion nodes. (There are 29 ways to cover 00 ; if we choose ‘ 00 01 02 03 04 0 ’ there are 49 ways to cover 05 ; and so on.) But if we consider the 20 × 3 problem instead of 3 × 20, so that the cells ij for 0 ≤ i < 20 and 0 ≤ j < 3 are processed in order 00 , 01 , 02 , 10 , ..., 2j , the search tree has just 71191 nodes, and all eight solutions are found very quickly. (This speedup is mostly due to having a better “ focus,” which we’ll discuss later.) Again we see that a small change in problem setup can have enormous ramifications.

The best of these early programs were highly tuned, written in assembly language with ingenious uses of macro instructions. Memwise, they were therefore superior to Algorithm X on smallish problems. [See J. G. Fletcher, CACM 8 ,10 (October 1965), cover and 621–623; N. G. de Bruijn, FGbook pages 465–466.] But the MRV heuristic eventually wins, as problems get larger.

Exercises 268 – 323 discuss some of the many intriguing and instructive problems that arise when we explore the patterns that can be made with pentominoes and similar families of planar shapes. Several of these problems are indeed large — beyond the capabilities of today’s machines.

Polycubes

And if you think two-dimensional shapes are fun, you’ll probably enjoy three dimensions even more! A polycube is a solid object formed by taking one or more 1 × 1 × 1 cubes and joining them face-to-face. We call them mono-cubes, dicubes, tricubes, tetracubes, pentacubes, etc.; but we don’t call them “ n -cubes” when they’re made from n unit cubies, because mathematicians have reserved that term for n -dimensional objects.

A new situation arises when n = 4. In two dimensions we found it natural to regard the tetromino ‘ images ’ as identical to its mirror image ‘ images ’, because we could simply flip it over. But the tetracube images is noticeably different from its mirror reflection images , because we can’t change one into the other without going into the fourth dimension. Polycubes that differ from their mirror images are called chiral , a word coined by Lord Kelvin in 1893 when he studied chiral molecules.

The simplest polycubes are cuboids — also called “rectangular parallelepipeds” by people who like long names — which are like bricks of size l × m × n . But things get particularly interesting when we consider noncuboidal shapes. Piet Hein noticed in 1933 that the seven smallest shapes of that kind, namely

images

can be put together to form a 3 × 3 × 3 cube, and he liked the pieces so much that he called them Soma . Notice that the first four pieces are essentially planar, while the other three are inherently three-dimensional. The twists are chiral.

Martin Gardner wrote about the joys of Soma in Scientific American 199 ,3 (September 1958), 182–188, and it soon became wildly popular: More than two million SOMA ® cubes were sold in America alone, after Parker Brothers began to market a well-made set together with an instruction booklet written by Hein.

A minimum number of blocks of simple form are employed. . . .
Experiments and calculations have shown that from the set of seven blocks
it is possible to construct approximately the same number of geometrical
figures as could be constructed from twenty-seven separate cubes.

— PIET HEIN, United Kingdom Patent Specification 420,349 (1934)

The task of packing these seven pieces into a cube is easy to formulate as an exact cover problem, just as we did when packing pentominoes. But this time we have 24 3D-rotations of the pieces to consider, instead of 8 2D-rotations and/or 3D- reflections; so exercise 324 is used instead of exercise 266 to generate the options of the problem. It turns out that there are 688 options, involving 34 items that we can call 1 , 2 , ..., 7 , 000 , 001 , ..., 222 . For example, the first option

‘ 1 ⁢       000 ⁢       001 ⁢     010 ’ ( 40 )

characterizes one of the 144 potential ways to place the “bent” piece 1.

Algorithm X needs just 407 megamems to find all 11,520 solutions to this problem. Furthermore, we can save most of that time by taking advantage of symmetry: Every solution can be rotated into a unique “ canonical” solution in which the “ell” piece 2 has not been rotated; hence we can restrict that piece to only six placements, namely ( 000 , 010 , 020 , 100 ), ( 001 , 011 , 021 , 101 ), ... ,( 102 , 112 , 122 , 202 ) — all shifts of each other. This restriction removes 138 \= 23 24 · 144 options, and the algorithm now finds the 480 canonical solutions in just 20 megamems. (These canonical solutions form 240 mirror-image pairs.)

Factoring an exact cover problem

In fact, we can simplify the Soma cube problem much further, so that all of its solutions can actually be found by hand in a reasonable time, by factoring the problem in a clever way.

Let’s observe first that any solution to an exact cover problem automatically solves infinitely many other problems. Going back to our original formulation in terms of an m × n matrix A = ( a ij ), the task is to find all sets of rows whose sum is 1 in every column, namely to find all binary vectors x 1 ... x m such that ∑ i \= 1 m x i a i j \= 1 ⁢   for 1 ≤ j ≤ n . Therefore if we set b i \= α 1 a i 1 + ··· + α n a in for 1 ≤ i ≤ m , where ( α 1 , ..., α n ) is any n -tuple of coefficients, the vectors x 1 ... x m will also satisfy ∑ i \= 1 m x i b i \= α 1 + ⋅ ⋅ ⋅ + α n ⁢   . By choosing the α ’s intelligently we may be able to learn a lot about the possibilities for x 1 ... x m .

For example, consider again the 6 × 7 matrix A in ( 5 ), and let α 1 = ··· = α 7 = 1. The sum of the entries in each row of A is either 2 or 3; thus we’re supposed to cover 7 things, by burying either 2 or 3 at a time. Without knowing anything more about the detailed structure of A , we can conclude immediately that there’s only one way to obtain a total of 7, namely by selecting 2 + 2 + 3! Furthermore, only rows 1 and 5 have 2 as their sum; we must choose them.

Now here’s a more interesting challenge: “Cover the 64 cells of a chessboard with 21 straight trominoes and one monomino.” This problem corresponds to a big matrix that has 96 + 64 rows and 1 + 64 columns,

    ( M 00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17 20 21 22 23 24 74 75 76 77 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 ... 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 ... 0 0 0 0 . . . . . . . . . . . . . . . . . . . . . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 . . . . . . . . . . . . . . . . . . . . . . . 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 1 ) , ( 41 )

where the first 96 rows specify all possible ways to place a tromino and the other 64 rows specify the possibilities for the monomino. Column ij represents cell ( i, j ); column ‘ M ’ means “monomino.”

The three cells ( i, j ) covered by a straight tromino always lead to distinct values of ( i − j ) mod 3. Therefore, if we add up the 22 columns of ( 41 ) for which ( i − j ) mod 3 = 0, we get 1 in each of the first 96 rows, and 0 or 1 in the other 64 rows. We’re supposed to get a total of 22 in the chosen rows; hence the monomino has to go into a cell ( i, j ) with i ≡ j (modulo 3).

A similar argument, using i + j instead of i − j , shows that the monomino must also go into a cell with i + j ≡ 1 (modulo 3). Therefore i ≡ j ≡ 2. We’ve proved that there are only four possibilities for ( i, j ), namely (2, 2), (2, 5), (5, 2), (5, 5). [ Golomb made this observation in his 1954 paper that introduced polyominoes, after “coloring” the cells of a chessboard with three colors. The general notion of factoring includes all such coloring arguments as special cases.]

Our proof that ( 38 ) is an impossible pentomino placement can also be regarded as an instance of factorization. The residual problem, if ( 38 ) is chosen, has a total of either 0 or 5 in the first 27 columns of each remaining row of the associated matrix. Therefore we can’t achieve a total of 27 from those rows.

Consider now a three-dimensional problem [J. Slothouber and W. Graatsma, Cubics (1970), 108–109]: Can six 1 × 2 × 2 cuboids be packed into a 3 × 3 × 3 box? This is the problem of choosing six rows of the 36 × 27 matrix

( 000 001 002 010 011 012 020 021 022 100 101 102 110 111 112 120 121 122 200 201 202 210 211 212 220 221 222 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 . . . . . . . . . . . . . . . . . . . . . . . 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 ) , ( 42 )

in such a way that all of the column sums are ≤ 1.

The 27 cubies ( i, j, k ) of a 3 × 3 × 3 cube fall into four classes, depending on how many of its coordinates have the middle value 1:

A vertex cubie has no 1s. ( ( 3 0 ) 2 3 \= 8 ⁢   cases. ) An edge cubie has one 1. ( ( 3 1 ) 2 2 \= 12 ⁢   cases. ) A face cubie has two 1s. ( ( 3 2 ) 2 1 \= 6 ⁢   cases. ) ( 43 ) A central cubie has three 1s. ( ( 3 3 ) 2 0 \= 1 ⁢   cases. )

Every symmetry of the cube preserves these classes.

Imagine placing four new columns v , e , f , c at the right of ( 42 ), representing the number of vertex, edge, face, and central cubies of a placement. Then 24 of the rows will have ( v, e, f, c ) = (1, 2, 1, 0), and the other 12 rows will have ( υ, e, f, c ) = (0, 1, 2, 1). If we choose a rows of the first kind and b rows of the second kind, this factorization tells us that we must have

a ≥ 0 , b ≥ 0 ,   a + b \= 6 , a ≤ 8 , 2 a + b ≤ 12 , a + 2 b ≤ 6 , b ≤ 1. ( 44 )

That’s more than enough to prove that b = 0 and a = 6, and thus to find the essentially unique way to pack those six cuboids.

(We could paraphrase this argument as follows, making it more impressive by concealing the low-level algebra that inspired it: “Each 1 × 2 × 2 cuboid occupies at least one face cubie. So each of them must be placed on a different face.”)

With these examples in mind, we’re ready now to apply factorization to the Soma cube. The possible ( v, e, f, c ) values for pieces 1 through 7 in ( 39 ) are:

Piece 1: ( 0 , 1 , 1 , 1 ) , ( 0 , 0 , 2 , 1 ) , ( 0 , 1 , 2 , 0 ) , ( 0 , 2 , 1 , 0 ) , ( 1 , 1 , 1 , 0 ) , ( 1 , 2 , 0 , 0 ) . Piece 2: ( 0 , 1 , 2 , 1 ) , ( 0 , 2 , 2 , 0 ) , ( 1 , 2 , 1 , 0 ) , ( 2 , 2 , 0 , 0 ) . Piece 3: ( 0 , 0 , 3 , 1 ) , ( 0 , 2 , 1 , 1 ) , ( 0 , 3 , 1 , 0 ) , ( 2 , 1 , 1 , 0 ) . Piece 4: ( 0 , 1 , 2 , 1 ) , ( 1 , 2 , 1 , 0 ) . Piece 5: ( 0 , 1 , 2 , 1 ) , ( 0 , 2 , 2 , 0 ) , ( 1 , 1 , 1 , 1 ) , ( 1 , 2 , 1 , 0 ) . Piece 6: ( 0 , 1 , 2 , 1 ) , ( 0 , 2 , 2 , 0 ) , ( 1 , 1 , 1 , 1 ) , ( 1 , 2 , 1 , 0 ) . Piece 7: ( 0 , 2 , 1 , 1 ) , ( 0 , 0 , 3 , 1 ) , ( 1 , 1 , 2 , 0 ) , ( 1 , 3 , 0 , 0 ) . ( 45 )

(This is actually much more information than we need, but it doesn’t hurt.)

Looking only at the totals for v , we see that we must have

(0 or 1) + (0, 1, or 2) + (0 or 2) + (0 or 1) + (0 or 1) + (0 or 1) + (0 or 1) = 8;

and the only way to achieve this is via

(0 or 1) + (1 or 2) + 2 + (0 or 1) + (0 or 1) + (0 or 1) + (0 or 1) = 8,

thus eliminating several options for pieces 2 and 3. More precisely, piece 2 must touch at least one vertex; piece 3 must be placed along an edge.

Looking next at the totals for v + f , which are the “ black” cubies if we color them alternately black and white with black in the corners, we must also have

(1 or 2) + 2 + 3 + 2 + 2 + 2 + (1 or 3) = 14;

and the only way to achieve this is with two from piece 1 and one from piece 7: Piece 1 must occupy two black cubies, and piece 7 must occupy just one.

We have therefore eliminated 200 of the 688 options in the list that begins with ( 40 ). And we also know that exactly five of the pieces 1, 2, 4, 5, 6, 7 occupy as many of the corner vertices as they individually can. This extra information can be encoded by introducing 13 new primary items

* , 1 + , 1 − , 2 + , 2 − , 4 + , 4 − , 5 + , 5 − , 6 + , 6 − , 7 + , 7 − ( 46 )

and six new options

‘ * 1 + 2 − 4 − 5 − 6 − 7 − ’ ‘ * 1 − 2 + 4 − 5 − 6 − 7 − ’ ‘ * 1 − 2 − 4 + 5 − 6 − 7 − ’ ‘ * 1 − 2 − 4 − 5 + 6 − 7 − ’ ‘ * 1 − 2 − 4 − 5 − 6 + 7 − ’ ‘ * 1 − 2 − 4 − 5 − 6 − 7 + ’ ( 47 )

and by appending p + or p - to each of piece p ’s options that do or don’t touch the most corners. For example, this new set of 6 + 488 options for the Soma cube problem includes the following typical ways to place various pieces:

‘1 000 001 011 1+’
‘1 001 011 101 1-’
‘2 000 001 002 010 2+’
‘2 000 001 011 021 2-’
‘3 000 001 002 011’
‘4 000 001 011 012 4+’
‘4 001 011 111 121 4-’
‘5 000 001 010 110 5+’
‘5 001 010 011 101 5-’
‘6 000 001 010 101 6+’
‘6 001 010 011 110 6-’
‘7 000 001 010 100 7+’
‘7 001 010 011 111 7-’

As before, Algorithm X finds 11,520 solutions; but now it needs only 108 megamems to do so. Each of the new options is used in at least 21 of the solutions, hence we’ve removed all of the “fat” in the original set. [This instructive analysis of Soma is due to M. J. T. Guy, R. K. Guy, and J. H. Conway in 1961. See Berlekamp, Conway, and Guy, Winning Ways , second edition (2004), 845–847.]

To reduce the number of solutions, using symmetry, we can force piece 3 to occupy the cells { 000 , 001 , 002 , 011 } (thus saving a factor of 24), and we can remove all options for piece 7 that use a cell ijk with k = 2 (saving an additional factor of 2). From the remaining 455 options, Algorithm X needs just 2 megamems to generate all 240 of the essentially distinct solutions.

The seven Soma pieces are amazingly versatile, and so are the other poly-cubes of small sizes. Exercises 324 – 350 explore some of their remarkable properties, together with historical references.

Color-controlled covering

Take a break! Before reading any further, please spend a minute or two solving the “ word search” puzzle in Fig. 71 . Comparatively mindless puzzles like this one provide a low-stress way to sharpen your word-recognition skills. It can be solved easily — for instance, by making eight passes over the array — and the solution can be found in Fig. 72 on the next page.

Fig. 71. Find the mathematicians * : Put ovals around the following names where they appear in the 15 × 15 array shown here, reading either forward or backward or upward or downward, or diagonally in any direction. After you’ve finished, the leftover letters will form a hidden message. (The solution appears on the next page.)

images

* The journal Acta Mathematica celebrated its 21st birthday by publishing a special Table Générale des Tomes 1–35 , edited by Marcel Riesz (Uppsala, 1913), 179 pp. It contained a complete list of all papers published so far in that journal, together with portraits and brief biographies of all the authors. The 27 mathematicians mentioned in Fig. 71 are those who were subsequently mentioned in Volumes 1, 2, or 3 of The Art of Computer Pro gramming — except for people like MITTAG-LEFFLER or POINCARÉ , whose names contain special characters.

ABEL    HENSEL  MELLIN
BERTRAND  HERMITE  MINKOWSKI
BOREL    HILBERT  NETTO
CANTOR   HURWITZ  PERRON
CATALAN   JENSEN  RUNGE
FROBENIUS  KIRCHHOFF STERN
GLAISHER  KNOPP   STIELTJES
GRAM    LANDAU  SYLVESTER
HADAMARD  MARKOFF  WEIERSTRASS

Our goal in this section is not to discuss how to solve such puzzles; instead, we shall consider how to create them. It’s by no means easy to pack those 27 names into the box in such a way that their 184 characters occupy only 135 cells, with eight directions well mixed. How can that be done with reasonable efficiency?

For this purpose we shall extend the idea of exact covering by introducing color codes . Let’s imagine that each cell ij of the array is to be “colored” with one of the letters { A , ..., Z }. Then the task of creating such a puzzle is essentially to choose from among a vast set of options

‘ ABEL 00 : A 01: B 02: E 03: L ’ ‘ ABEL 00 : A 10: B 20: E 30: L ’ ‘ ABEL 00 : A 11: B 22: E 33: L ’ ‘ ABEL 00 : L 01: E 02: B 03: A ’         . ⁢           . ⁢         . ⁢         . ⁢       . ‘ WEIERSTASS e4: S e5: S e6: A e7: R e8: T e9: S ea: R eb: E ec: I ed: E ee: W ’ ( 48 )

in such a way that the following conditions are satisfied:

  1. Exactly one option must be chosen for each of the 27 mathematicians’ names.

  2. The chosen options must give consistent colors to each of the 15×15 cells ij .

images

Fig. 72. Solution to the puzzle of the hidden mathematicians ( Fig. 71 ). Notice that the central letter R actually participates in six different names:

BERTRAND
GLAISHER
HERMITE
HILBERT
KIRCHHOFF
WEIERSTRASS

The T to its left participates in five.

Here’s what the leftover letters say:

These authors of early papers in Acta Mathematica were cited years later in The Art of Computer Pro gramming .

There also are informal constraints: It’s desirable to have many shared letters between names, and to intermix the various directions, so that the puzzle has plenty of variety and perhaps a few surprises. But conditions (i) and (ii) are the important criteria for a computer to consider; the auxiliary informalities are best handled interactively, with human guidance.

Notice that the color constraints (ii) are significantly different from the name constraints (i). Several distinct options are allowed to specify the color of the same cell, as long as those specifications don’t conflict with each other.

Let us therefore define a new problem, exact covering with colors , or XCC for short. As before, we’re given a set of items, of which N 1 are primary and N − N 1 are secondary. We’re also given a family of M options, each of which includes at least one primary item. The new rule is that a color is assigned to the secondary items of each option. The new task is to find all choices of options such that

  1. every primary item occurs exactly once; and

  2. every secondary item has been assigned at most one color.

The primary items are required; the secondary items are elective.

Color assignments are denoted by a colon; for example, ‘ 00 : A ’ in ( 48 ) means that color A is assigned to the secondary item 00 . When a secondary item of an option is not followed by a colon, it is implicitly assigned a unique color, which doesn’t match its color in any other option. Therefore the ordinary exact cover problems that we’ve been studying so far, in which secondary items don’t explicitly receive colors but cannot be chosen in more than one option, are just special cases of the XCC problem, even though nothing about color was mentioned.

A tremendous variety of combinatorial problems can be expressed readily in the XCC framework. And there’s good news: The dancing links technique works beautifully with such problems! Indeed, we will see that this considerably more general problem can be solved with only a few small extensions to Algorithm X .

The nodes of Algorithm X have just three fields: TOP , ULINK , and DLINK . We now add a fourth field, COLOR ; this field is set to the positive value c when the node represents an item that has explicitly been assigned color c . Consider, for example, the following toy problem with three primary items { p , q , r } and two secondary items { x , y }, where the options are

‘ p q  x  y : A ’ ⁢   ; ‘ p r  x : A  y ’ ⁢   ; ‘ p x : B ’ ⁢     ; ‘ q x : A ’ ⁢     ; ‘ r y : B ’ ⁢     . ( 49 )

Table 2 shows how it would be represented in memory, extending the conventions of Table 1 . Notice that COLOR = 0 when no color has been specified. The COLOR fields of the header nodes (nodes 1–5 in this example) need not be initialized because they’re never examined except in printouts (see the answer to exercise 12 ). The COLOR fields of the spacer nodes (nodes 6, 11, 16, 19, 22, 25) are unimportant, except that they must be nonnegative.

Table 2
THE INITIAL CONTENTS OF MEMORY CORRESPONDING TO ( 49 )

i :

0

1

2

3

4

5

6

NAME ( i ):

—

p

q

r

x

y

—

LLINK ( i ):

3

0

1

2

6

4

5

RLINK ( i ):

1

2

3

0

5

6

4

x :

0

1

2

3

4

5

6

LEN ( x ), TOP( x ):

—

3

2

2

4

3

0

ULINK ( x ):

—

17

20

23

21

24

—

DLINK ( x ):

—

7

8

13

9

10

10

COLOR ( x ):

—

—

—

—

—

—

0

x :

7

8

9

10

11

12

13

TOP ( x ):

1

2

4

5

−1

1

3

ULINK ( x ):

1

2

4

5

7

7

3

DLINK ( x ):

12

20

14

15

15

17

23

COLOR ( x ):

0

0

0

A

0

0

0

x :

14

15

16

17

18

19

20

TOP ( x ):

4

5

−2

1

4

−3

2

ULINK ( x ):

9

10

12

12

14

17

8

DLINK ( x ):

18

24

18

1

21

21

2

COLOR ( x ):

A

0

0

0

B

0

0

x :

21

22

23

24

25

 

 

TOP ( x ):

4

−4

3

5

−5

 

 

ULINK ( x ):

18

20

13

15

23

 

 

DLINK ( x ):

4

24

3

5

—

 

 

COLOR ( x ):

A

0

0

B

0

 

 

It’s easy to see how these COLOR fields can be used to get the desired effect: When an option is chosen, we “purify” any secondary items that it names, by effectively removing all options that have conflicting colors. One slightly subtle point arises, because we don’t want to waste time purifying a list that has already been culled. The trick is to set COLOR ( x ) ← −1 in every node x that’s already known to have the correct color, except in nodes that have already been hidden.

Thus we want to upgrade the original operations cover ( i ) and hide ( p ) in ( 12 ) and ( 13 ), as well as their counterparts uncover ( i ) and unhide ( p ) in ( 14 ) and ( 15 ), in order to incorporate color controls. The changes are simple:

cover'( i ) is like cover( i ), in  12 , but it calls hide'( p )  instead of hide( p );     ( 50 )

hide'( p ) is like hide( p ), in  13 , but it ignores node  q   when  COLOR ( q ) < 0;    ( 51 )

uncover'( i ) is like uncover( i ), but it calls unhide'( p ) instead of unhide( p ); ( 52 )

unhide'( p ) is like unhide( p ), but it ignores node q ⁢   when COLOR ( q )   <   0. ( 53 )

Our colorful algorithm also introduces two new operations and their inverses:

commit( p , j ) = { If COLOR ( p ) = 0, cover' ( j ) if COLOR ( p ) > 0, purify ( p ) ( 54 )

purify( p )= { Set c ⁢   ← COLOR ( p ) ,   i ⁢   ← TOP ( p ) , COLOR ( i ) ←   c ,   q ⁢   ← DLINK ( i ). While q ⁢   ≠ i , ⁢ do the following and set q ⁢   ← DLINK   ( q ):                          If COLOR ( q ) = c ,   set COLOR ( q ) ←   − 1 ;                           otherwise hide'( q ). ( 55 )

uncommit ( p , j ) = { If COLOR ( p ) = 0, uncover'(j); if COLOR ( p ) > 0, unpurify ( p ). ( 56 )

unpurify ( p ) = { Set c ⁢ ← COLOR ( p ) ,   i ⁢ ← TOP   ( p ) ,   q ⁢ ← ULINK ( i ). While   q ≠ i ,   do the following and set q ⁢   ← ULINK ( q ) :                               If COLOR ( q ) < 0, set COLOR ( q ) ← c ;                             otherwise unhide'( q ). ( 57 )

Otherwise Algorithm C is almost word-for-word identical to Algorithm X .

Algorithm C ( Exact covering with colors ). This algorithm visits all solutions to a given XCC problem, using the same conventions as Algorithm X .

C1. [Initialize.] Set the problem up in memory, as in Table 2 . (See exercise 8 .) Also set N to the number of items, Z to the last spacer address, and l ← 0.

C2. [Enter level l .] If RLINK( 0 ) = 0 (hence all items have been covered), visit the solution that is specified by x 0 x 1 ... x l −1 and go to C8. (See exercise 13 .)

C3. [Choose i .] At this point the items i 1 , ..., i t still need to be covered, where i 1 = RLINK (0), i j +1 \= RLINK ( i j ), RLINK ( i t ) = 0. Choose one of them, and call it i . (The MRV heuristic of exercise 9 often works well in practice.)

C4. [Cover i .] Cover item i using ( 50 ), and set x l ← DLINK ( i ).

C5. [Try x l .] If x l \= i , go to C7 (we’ve tried all options for i ). Otherwise set p ← x l + 1, and do the following while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set p ← ULINK ( p ); otherwise commit( p, j ) and set p ← p + 1. (This commits the items ≠ i in the option that contains x l .) Set l ← l +1 and return to C2.

C6. [Try again.] Set p ← x l − 1, and do the following while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set p ← DLINK ( p ); otherwise uncommit( p, j ) and set p ← p − 1. (This uncommits the items ≠ i in the option that contains x l , using the reverse order.) Set i ← TOP ( x l ), x l ← DLINK ( x l ), and return to C5.

C7. [ Backtrack.] Uncover item i using ( 52 ).

C8. [Leave level l .] Terminate if l = 0. Otherwise set l ← l − 1 and go to C6.

Algorithm C applies directly to several problems that we’ve already discussed in previous sections. For example, it readily generates word rectangles, as well as intriguing patterns of words that have more intricate structure (see exercises 87 – 93 ). We can use it to find all de Bruijn cycles, and their two-dimensional counterparts (see exercises 94 – 97 ). The extra generality of exact covering options also invites us to impose additional constraints for special applications. Furthermore, Algorithm C facilitates experiments with the tetrad tiles that we studied in Section 2.3.4.3 (see exercises 120 and 121 ).

The great combinatorialist P. A. MacMahon introduced several families of colorful geometric patterns that continued to fascinate him throughout his life. For example, in U.K. Patent 3927 of 1892, written with J. R. J. Jocelyn, he considered the 24 different triangles that can be made with four colors on their edges,

images

and showed two ways in which they could be arranged to form a hexagon with matching colors at adjacent edges and with solid colors on the outer boundary:

images

(Notice that chiral pairs, like images and images in ( 58 ), are considered to be distinct; MacMahon’s tiles can be rotated, but they can’t be “flipped over.”)

Four suitable colours are black, white, red, and blue,
as they are readily distinguishable at night.

— P. A. MACMAHON, New Mathematical Pastimes (1921)

Let’s assume that the boundary is supposed to be all white, as in pattern (59b). There are millions of ways to satisfy this condition; but every really distinct solution is counted 72 times, because the hexagon has 12 symmetries under rotation and reflection, and because the three nonwhite colors can be permuted in 3! = 6 ways. We can remove the hexagon symmetries by fixing the position of the all-white triangle (see exercise 119 ). And the color symmetries can be removed by using an interesting extension of Algorithm C , which reduces the number of solutions by a factor of d ! when the options are symmetrical with respect to d colors (see exercise 122 ). In this way all of the solutions — can you guess how many? — can actually be found with only 4.5 G μ of computation (see exercise 126 ).

MacMahon went on to study many other matching problems with these triangles, as well as with similar sets of tiles that are based on squares, hexagons, and other shapes. He also considered three-dimensional arrangements of colored cubes, which are supposed to match where they touch. Exercises 127 – 148 are devoted to some of the captivating questions that have arisen from this work.

Introducing multiplicity

We’ve now seen from numerous examples that Algorithm C — which extends Algorithm X and solves arbitrary XCC problems — is enormously versatile. In fact, there’s a sense in which every constraint satisfaction problem is a special case of an XCC problem (see exercise 100 ).

But we can extend Algorithm C even further, again without substantial changes, so that it goes well beyond the original notion of exact covering. For example, let’s consider Robert Wainwright’s “ partridge puzzle” (1981), which was inspired by the well-known fact that the sum of the first n cubes is a perfect square:

1 3 + 2 3 + ⋅ ⋅ ⋅ + n 3 \= N 2 , where   N ⁢ \=   1 + 2 + ⋅ ⋅ ⋅ + n . ( 60 )

Wainwright wondered if this relation could be verified geometrically, by taking one square of size 1 × 1, two squares of size 2 × 2, ..., n squares of size n × n , and packing them all into a big square of size N × N . (We know from exercise 1.2.1–8 that 4 k squares of each size k × k can be packed into a 2 N × 2 N square. But Wainwright hoped for a more direct corroboration of ( 60 ).) He proved the task impossible for 2 ≤ n ≤ 5, but found a perfect packing when n = 12; so he thought of the 12 days of Christmas, and named his puzzle accordingly (see exercise 154 ).

This partridge puzzle is easily expressed in terms of options that involve n items # k for 1 ≤ k ≤ n , as well as N 2 items ij for 0 ≤ i, j < N . By analogy with what we did with pentominoes in ( 37 ), the options are

‘ # k ⁢ i j ⁢     i ( j \= 1 ) . ⁢     . ⁢   ⁢   ⁢ . ⁢       i ( j + k − 1 )     ( i + 1 ) j ( i + 1 )     ( j + 1 )   . ⁢   ⁢ . ⁢   ⁢ . ⁢     ( i + k − 1 ) ( j + k − 1 ) ’ ( 61 )

for 1 ≤ k ≤ n and 0 ≤ i, j ≤ N − k . (Exactly ( N + 1 − k ) 2 options involve # k , and each of them names 1 + k 2 items.) For example, the options when n = 2 are

‘#1 00’, ‘#1 01’, ‘#1 02’, ‘#1 10’, ‘#1 11’, ‘#1 12’, ‘#1 20’, ‘#1 21’, ‘#1 22’,

  ‘#2 00 01 10 11’, ‘#2 01 02 11 12’, ‘#2 10 11 20 21’, ‘#2 11 12 21 22’.

As before, we want to cover each of the N 2 cells ij exactly once. But there’s a difference: We now want to cover primary item # k exactly k times, not just once.

That’s a rather big difference. But in Algorithm M below, we’ll see that the dancing links approach can handle it. For example, that algorithm can show that the partridge puzzle has no perfect packings for n = 6 or n = 7; but it finds thousands of surprising solutions when n = 8, such as

images

When we first defined exact cover problems, near the beginning of this section, we considered M × N matrices of 0s and 1s, such as ( 5 ). In matrix terms, the task was to find all subsets of the rows whose sum is 11 ... 1. Algorithm M is going to do much more: It will find all subsets of rows whose sum is v 1 v 2 ... v N , where v 1 v 2 ... v N is any desired vector of multiplicities.

In fact, Algorithm M will go further yet, by allowing intervals [ u j .. v j ] to be prescribed for each multiplicity. It will actually solve the general MCC problem , “multiple covering with colors,” which is defined as follows: There are N items, of which N 1 are primary and N − N 1 are secondary. Each primary item j for 1 ≤ j ≤ N 1 is assigned an interval [ u j .. _v j ] of permissible values, where 0 ≤ _u j ≤ v j and v j > 0. There also are M options, each of which includes at least one primary item. A color is assigned to the secondary items of each option; a “blank” color is understood to represent a unique color that appears nowhere else. The task is to find all subsets of options such that

  1. each primary item j occurs at least u j times and at most v j times;

  2. every secondary item has been assigned at most one color.

Thus every XCC problem is the special case u j \= v j \= 1 of an MCC problem.

Indeed, the MCC problem is extremely general! For example, its special case u j \= 1 and v j \= M , without secondary items, is the classical not-necessarily-exact cover problem , in which we simply require each item to appear in at least one option. Section 7.2.2.6 will be entirely devoted to the cover problem.

Let’s confine our attention here to a few more examples of the MCC problem, in preparation for Algorithm M . In the first place, we can tackle a refined version of Wainwright’s partridge puzzle: “Pack at most k squares of size k × k , for 1 ≤ k ≤ n , into an N × N square, without overlapping, so that as many as possible of the N 2 cells are covered.” (As before, N = 1 + 2 + ··· + n .) We know from (6 2 ) that the entire square can be covered when n = 8; but smaller cases are another story. Solutions for 2 ≤ n ≤ 5 are readily found by hand:

images

And to prove that every packing for n = 5 must leave at least 13 cells vacant, Algorithm M will show that the MCC problem ( 61 ) has no solutions when items #1, #2, #3 are respectively given the multiplicities [0 .. 13], [0 .. 2], [0 .. 3] instead of 1, 2, 3. Exercise 157 constructs optimum packings when n = 6 and n = 7, thereby settling all small cases of the partridge puzzle.

Next, let’s consider an MCC problem of quite a different kind: “Place m queens so that they control all cells of an n × n chessboard.” (The classic 5-queens problem — which should be distinguished from the ‘5 queens problem’ considered earlier — is the special case m = 5, n = 8.) Exercise 7.1.4–241 discusses the history of this problem, which goes back to a remarkable book by de Jaenisch (1863).

We can solve it, MCC-wise, by introducing n 2 + 1 primary items, namely the pairs ij for 0 ≤ i, j < n and the special item #, together with n 2 options:

‘ # ⁢   i j ⁢     i 1 ⁢   j 1 ⁢   i 2 ⁢   j 2 ⁢   . ⁢   . ⁢   . ⁢   i t j t ’ ⁢         for 0 ≤ i ,   j < n , ( 64 )

where i 1 j 1 , i 2 j 2 , ..., i t j t are the cells attacked by a queen on ij . Each cell ij is assigned the multiplicity [1 .. m ]; item # gets multiplicity m .

From this specification Algorithm M will readily find all 4860 solutions to the 5-queens problem, after 13 gigamems of computation. For example, it begins with 22 ways to cover the corner cell 00. If it puts a queen there, it has 22 ways to cover cell 17; and so on. The branching factor at each step tends to decrease rapidly after three queens have been placed.

The beauty of the MCC setup in ( 64 ) is that we can solve many related problems by making simple changes to the specifications. For example, by retaining only the 36 options for 1 ≤ i, j ≤ 6, we could find the 284 solutions that place no queens at the edges of the board. Or by removing the 16 options for 2 ≤ i, j ≤ 5, we would discover that exactly 880 of the solutions place no queens near the middle. Exactly 88 solutions avoid the central two rows and the central two columns. Exactly 200 solutions put all five queens on “black” cells (with i + j even). Exactly 90 avoid the upper left and lower right quadrants. Exactly 2 solutions (can you find them?) place all five queens in the top half of the board.

By changing the multiplicities in the bottom row from [1 .. 5] to 1, we get 18 solutions for which every cell in that row is attacked just once. Or, changing the central 16 multiplicities to [2 .. 5] yields 48 solutions for which every cell near the center is attacked at least twice. Changing all the cell multiplicities to [1 .. 4] reduces the number of solutions from 4860 to 3248; changing them all to [1 .. 3] reduces it to 96. Exercise 161 illustrates several of the less obvious possibilities.

images

The examples of MCC problems that we’ve seen so far have involved primary items only. Secondary items, and their color controls, add new dimensions and extend the range of applications enormously. Consider, for example, the word rectangles that we investigated briefly in Section 7.2.2 . Here’s a 4 × 5 word rectangle that uses only nine distinct letters of the alphabet:

L A B E L
A B I D E .
S L A I N
T E S T S

Can we find one that uses only eight distinct letters, while sticking to common words? (More precisely, is there such a rectangle whose columns are chosen from the most common 1000 four-letter words of English, and whose rows belong to WORDS (2000), the curated collection from the Stanford GraphBase?)

The answer is yes, and in fact there are six solutions:

S T R U T T E A S E E A T E N P R E S S E A S E D A G I L E S E N S E E D G E D W A D E D A R E N A S E E D S H A R S H R A D A R A R E N A S E A T S H A R S H L L A M A E A G E R S T E A M T E S T S S C A R S C O C O A A R R A Y R E E D S ( 66 )

One way to find them, and to show that seven distinct letters is impossible, is to set up an MCC problem in which the primary items are A 0 , A 1 , A 2 , A 3 , D 0 , D 1 , D 2 , D 3 , D 4 , # A , # B , ..., # Z , #; they all have multiplicity 1 except that # has multiplicity 8. There also are secondary items A , B , ..., Z , and ij for 0 ≤ i < 4, 0 ≤ j < 5. The letter-counting is handled by 2 · 26 short options:

‘ # A A:0 ’ , ‘ # A A:1 ’ , ‘ # B B:0 ’ ,   ‘ # B B:1 ’ ;   . ⁢   . ⁢   . ⁢   ; ‘ # Z Z:0 ’ , ‘ # Z Z:1 # ’ . ( 67 )

Then each legal 5-letter word c 1 c 2 c 3 c 4 c 5 yields four options, ‘ A i i 0: c 1 i 1: c 2 i 2: c 3 i 3: c 4 i 4: c 5 c 1 :1 c 2 :1 c 3 :1 c 4 :1 c 5 :1’, for 0 ≤ i < 4; each legal 4-letter word c 1 c 2 c 3 c 4 yields five options, ‘ D j 0 j : c 1 1 j : c 2 2 j : c 3 3 j : c 4 c 1 :1 c 2 :1 c 3 :1 c 4 :1’, for 0 ≤ j < 5. (Letters that occur more than once in a word are listed only once.)

For example, one of the options chosen for the first solution in ( 66 ) is ‘ A 3 30: P 31: R 32: E 33: S 34: S P :1 R :1 E :1 S :1’; it forces the options ‘# P P :1 #’, ‘# R R :1 #’, ‘# E E :1 #’, ‘# S S :1 #’ to be chosen too, thus contributing four to the number of chosen options that contain #.

By the way, when Algorithm M is applied to these options, it’s important to use the “ nonsharp preference heuristic” discussed in exercise 10 and its answer. Otherwise the algorithm will foolishly make binary branches on the items # A , ..., # Z , before trying out actual words. A 1000-way branch on D 0 is much better than a 2-way branch on # Q , in this situation.

*A new dance step

In order to implement multiplicity, we need to update the data structures in a new way. Suppose, for example, that there are five options available for some primary item p , and suppose they are represented in nodes a , b , c , d , and e . Then p ’s vertical list of active options has the following links:

x : p a b c d e ULINK ( x ) e p a b c d DLINK ( x ) a ⁢ b c d e p ( 68 )

If the multiplicity of p is 3, there are ( 5 3 ) \= 10 ways to choose three of the five options; but we do not want to make a 10-way branch! Instead, each branch of Algorithm M below chooses only the first of the options that will appear in the solution. Then it reduces the problem recursively; the reduced problem will have a shorter list for p , from which two further options should be selected. Since we must choose either a , b , or c as the first option, the algorithm will therefore begin with a 3-way branch. For example, if b is chosen to be first, the reduced problem will ask for two of options { c, d, e } to be chosen eventually.

The algorithm will recursively find all solutions to that reduced problem. But it won’t necessarily begin by branching again on the same item, p ; some other item, q , might well have become more significant. For instance, the choice of b might have assigned color values that make LEN ( q ) ≤ 1. (The choice of b might also have made c , d , and/or e illegal.)

The main point is that, after we’ve chosen the first of three options for p in the original problem, we have not “covered” p as we did in Algorithms X and C . Item p remains on an equal footing with all other active items of the reduced problem, so we need to modify ( 68 ) accordingly.

The operation of reducing the problem by removing an option from an item list, in the presence of multiplicity, is called “ tweaking” that option. For example, just after the algorithm has chosen b as the first option for p , it will have tweaked both a and b . This operation is deceptively simple:

tweak ( x ,   p ) = { hide'( x ) and set   d ← DLINK ( x ) ,   DLINK ( p ) ← d , ULINK ( d ) ← p ,   LEN ( p ) ← LEN ( p ) − 1. ( 69 )

(See ( 51 ). We will tweak( x, p ) only when x = DLINK ( p ) and p = ULINK ( x ).) Notice that tweaking x does more than hiding x , but it does less than covering p .

Eventually the algorithm will have tried each of a , b , and c as p ’s first option, and it will want to backtrack and undo the tweaking. The actions tweak( a, p ), tweak( b, p ), tweak( c, p ) will have clobbered most of the original uplinks in ( 68 ):

x : p a b c d e ULINK ( x ) e p p p p d DLINK ( x ) d ⁢ b c d e p ( 70 )

Unfortunately, this residual data isn’t sufficient for us to restore the original state, because we’ve lost track of node a . But if we had recorded the value of a when we began, we would be in good shape, because a pointer to node a together with the DLINK s in ( 70 ) would now lead us to node b , then to c , and then to d .

Therefore the algorithm maintains an array FT[ l ] , to remember the locations of the “ first tweaks” that were made at every level l . And it adds a new dance step, “ untweaking,” to its repertoire of link manipulations:

untweak( l )   \= { Set a ⁢ ← FT [ l ], p ← ( a ≤ N ?   a : TOP ( a ) ) ,   x ← a ,   y ← p ; set z ← DLINK ( p ), DLINK ( p ) ← x ,   k ⁢ ← 0 ; while x ⁢ ≠ z ,     set ULINK ( x ) ←   y ⁢     and k ← k + 1 ,                      unhide'( x ), and set y ← x ,   x ← DLINK ( x ); finally set ULINK ( z ) ← y ⁢   and LEN ( p ) ← LEN( p ) +   k . ( 71 )

(See exercise 163 . This computation relies on a surprising fact proved in exercise 2(a) , namely that unhiding can safely be done in the same order as hiding.)

The same mechanism can be used when the specified multiplicity is an interval instead of a single number. For example, suppose item p in the example above is required to occur in either 2, 3, or 4 options, not exactly 3. Then the first option chosen must be a , b , c , or d ; and the reduced problem will ask p to occur in either 1, 2, or 3 of the options that remain. Eventually the algorithm will resort to untweaking, after a , b , c , and d have all been tweaked and explored.

Similarly, if p ’s multiplicity has been specified to be either 0, 1, or 2, the algorithm below will tweak each of a through e in turn. It will also run though all solutions that omit all of p ’s options, before finally untweaking and backtracking.

A special case arises, however, when p ’s multiplicity has been specified to be either 0 or 1. In such cases we’re not allowed to choose options b , c , d , or e after option a has been chosen. Therefore it’s important to invoke cover′( p ), as in Algorithm C , instead of hiding one option at a time. (See ( 50 ).) The individual options of p are then tweaked, to remove them one by one from the active list; this tweaking uses the special operation tweak′( x, p ), which is like tweak( x, p ) in ( 69 ) except that it omits the operation hide′( x ), because hiding was already done when p was covered. Finally, the case of 0-or-1 multiplicity eventually concludes by invoking the routine untweak′( l ), which is like untweak( l ) in ( 71 ) except that (i) it omits unhide′( x ), and (ii) it calls uncover′( p ) after restoring LEN ( p ).

We’re ready now to write Algorithm M , except that we need a way to represent the multiplicities in the data structures. For this purpose every primary item has two new fields, SLACK and BOUND . Suppose the desired multiplicity of p is in the interval [ u..v; ], where 0 ≤ u ≤ v and v ≠ 0; Algorithms X and C correspond to the case u = v = 1. Then we set

SLACK( p )    ← v ⁢     −   u , BOUND ( p ) ← v ( 72 )

when the algorithm begins. The value of SLACK ( p ) will never be changed. But BOUND ( p ) will decrease dynamically, as we reduce the problem, so that we will never choose more options for p than its current bound.

Algorithm M ( Covering with multiplicities and colors ). This algorithm visits all solutions to a given MCC problem, extending Algorithms X and C .

M1. [Initialize.] Set the problem up in memory as in step C1 of Algorithm C , with the addition of multiplicity specifications ( 72 ). Also set N to the number of items, N 1 to the number of primary items, Z to the last spacer address, and l ← 0.

M2. [Enter level l .] If RLINK (0) = 0 (hence all items have been covered), visit the solution that is specified by x 0 x 1 ... x l −1 and go to M9. (See exercise 164 .)

M3. [Choose i .] At this point the items i 1 , ..., i t still need to be covered, where i 1 = RLINK (0), i j +1 \= RLINK ( i j ), RLINK ( i t ) = 0. Choose one of them, and call it i . (The MRV heuristic of exercise 166 often works well in practice.) If the branching degree θ i is zero (see exercise 166 ), go to M9.

M4. [Prepare to branch on i .] Set x l ← DLINK ( i ) and BOUND ( i ) ← BOUND ( i )−1. If BOUND ( i ) is now zero, cover item i using ( 50 ). If BOUND ( i ) ≠ 0 or SLACK ( i ) ≠ 0, set FT [ l ] ← x l .

M5. [Possibly tweak x l .] If BOUND ( i ) = SLACK ( i ) = 0, go to M6 if x l ≠ i , to M8 if x l \= i . (That case is like Algorithm C .) Otherwise if LEN ( i ) ≤ BOUND ( i ) − SLACK ( i ), go to M8 (list i is too short). Otherwise if x l ≠ i , call tweak( x l ,i ) (see ( 69 )), or tweak′( x l ,i ) if BOUND ( i ) = 0. Otherwise if BOUND ( i ) ≠ 0, set p ← LLINK ( i ), q ← RLINK ( i ), RLINK ( p ) ← q , LLINK ( q ) ← p .

M6. [Try x l .] If x l ≠ i , set p ← x l + 1, and do the following while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set p ← ULINK ( p ); otherwise if j ≤ N 1 , set BOUND ( j ) ← BOUND ( j ) − 1, p ← p + 1, and cover′( j ) if BOUND ( j ) is now 0; otherwise commit( p, j ) and set p ← p + 1. (This loop covers or commits the items ≠ i in the option that contains x l .) Set l ← l + 1 and return to M2.

M7. [Try again.] Set p ← x l − 1, and do the following steps while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set p ← DLINK ( p ); otherwise if j ≤ N 1 , set BOUND ( j ) ← BOUND ( j ) + 1, p ← p − 1, and uncover′( j ) if BOUND ( j ) is now 1; otherwise uncommit( p, j ) and set p ← p − 1. (This loop uncovers or uncommits the items ≠ i in the option that contains x l , using the reverse order.) Set x l ← DLINK ( x l ) and return to M5.

M8. [Restore i .] If BOUND ( i ) = SLACK ( i ) = 0, uncover item i using ( 52 ). Otherwise call untweak( l ) (see ( 71 )), or untweak′( l ) if BOUND ( i ) = 0. Set BOUND ( i ) ← BOUND ( i ) +1.

M9. [Leave level l .] Terminate if l = 0. Otherwise set l ← l − 1. If x l ≤ N , set i ← x l , p ← LLINK ( i ), q ← RLINK ( i ), RLINK ( p ) ← LLINK ( q ) ← i , and go to M8. (That reactivates i .) Otherwise set i ← TOP ( x l ) and go to M7.

* Analysis of Algorithm X

Now let’s get quantitative, and see what we can actually prove about the running time of these algorithms.

For simplicity, we’ll ignore color constraints and look only at Algorithm X , as it finds all solutions to an exact cover problem, where the problem is specified in terms of an M × N matrix A of 0s and 1s such as ( 5 ).

We’ll assume that the problem is strict , in the sense that no two rows of the matrix are identical, and no two columns of the matrix are identical. For if two or more rows or columns are equal, we need keep only one of them; it’s easy to relate all solutions of the original problem A to the solutions of this reduced problem A ′. (See exercise 179 .)

Our first goal will be to find an upper bound on the number of nodes in the search tree, as a function of the number of rows of A (the number of options). This upper bound grows exponentially, because the exact cover problem can have lots of solutions; but we’ll see that it can’t actually be extremely large.

For this purpose we’ll define the doomsday function D ( n ), which will have the property that the search tree for every strict exact cover problem with n options has at most D ( n ) nodes, when Algorithm X uses the MRV heuristic in step X3.

The search tree has a root node labeled with the original matrix A , and its other nodes are defined recursively: When a node at level l is labeled with a subproblem for which step X3 makes a t -way branch, that node has t subtrees, whose roots are labeled by the reduced problem that remains after step X4 has covered item i and after step X5 has optionally covered one or more other items j , for t different choices of x l .

Here, for example, is the complete search tree when A is the matrix of ( 5 ):

images

(Each matrix and submatrix in this diagram has been framed with a light-gray border. The node at bottom left illustrates a 0×1 submatrix, where the algorithm had to backtrack because it had no way to cover the remaining column. The node at bottom right illustrates a 0×0 submatrix, which happens to be a solution to the 1 × 2 problem above it.) We can, if we like, reduce all of the submatrices by eliminating repeated columns, although Algorithm X doesn’t do this; then we get strict exact cover problems at every node of the search tree:

images

A t -way branch implies that the matrix A has a certain structure. We know that there’s some column, say i 1 = i , that has 1 in exactly t rows, say o 1 , ..., o t , and that every column contains at least t 1s. When we branch on row o p , for 1 ≤ p ≤ t , the reduced problem that defines the p th subtree will retain all but s p of the rows of A , where s p is the number of rows that intersect o p . We can order the rows so that s 1 ≤ ··· ≤ s t . For example, in ( 73 ) we have t = 2 and s 1 = s 2 = 4.

A nice thing now happens: There’s always a unique index 0 ≤ t ′ ≤ t such that

s p \= t + p − 1 , for 1 ≤ p ⁢ ≤ t ′ . ( 74 )

That is, either s 1 > t and t ′ = 0; or s 1 = t and t ′ = 1 and either ( t = 1 or s 2 > t + 1); or s 1 = t and s 2 = t + 1 and t ′ = 2 and either ( t = 2 or s 3 > t + 2); or ... ; or s 1 = t and s 2 = t + 1 and ... and s t \= 2 t − 1 and t ′ = t .

Suppose, for example, that t = 4 and s 1 = 4; we must prove that s 2 ≥ 5. Since s 1 = 4, row o 1 doesn’t intersect any rows except { o 1 , ..., o t }; consequently option o 1 consists of the single item ‘ i 1 ’. Hence option o 2 must contain at least two items, ‘ i 1 i 2 ...’, otherwise the problem wouldn’t be strict. This new item appears in at least 4 options, however, one of which is different from o 1 . Option o 2 therefore intersects 5 or more options (including itself). QED.

Similarly, if t = 4 and s 1 = 4 and s 2 = 5, exercise 180 proves that s 3 ≥ 6, and indeed it proves that even more is true. For example, if t = t ′ = 4, so that ( s 1 , s 2 , s 3 , s 4 ) = (4, 5, 6, 7) as demanded by ( 74 ), exercise 180 proves the existence of options o 5 , o 6 , o 7 that have a particularly simple form:

o 1 ⁢   \=   ‘ i 1 ’ ; o 2 ⁢ \= ‘ i 1 ⁢   i 2 ’ ;   o 3 \= ‘ i 1 ⁢   i 2 ⁢   ⁢ i 3 ’ ; o 4 \= ‘ i 1 ⁢   i 2 ⁢   ⁢ i 3   ⁢ i 4 ’ ;   ⁢ o 5 \= ‘ i 2 ⁢   i 3 ⁢   ⁢ i 4   ⁢ . ⁢   . ⁢   . ⁢   ’ ;     o 6 \= ‘ i 3 ⁢   ⁢   ⁢ i 4   ⁢ . ⁢   . ⁢   . ⁢   ’ ; o 7 \= ‘ ⁢   ⁢ i 4   ⁢ . ⁢   . ⁢   . ⁢   ’ . ( 75 )

Okay, we’re ready now to construct the promised “doomsday function” D ( n ). It starts out very tame,

D ( 0 ) \= D ( 1 ) \= 1 ; ( 76 )

and for convenience we set D ( n ) = −∞ if n < 0. When n ≥ 2 the definition is

D ( n ) \= max { d ( n , t , t ′ )   |   1 ≤ t < n ⁢   and   0 ≤ t ′ ≤ t } , ( 77 )

where d ( n, t, t ′) is an upper bound for the size of the search tree over all n -option strict exact cover problems whose parameters ( 74 ) are t and t ′. One such bound,

d ( n , t , 0 ) \= 1 + t ⋅ D ( n − t − 1 ) , ( 78 )

handles the case t ′ = 0, because the search tree in that case is a t -way branch

images

and each subproblem A p has at most n − t − 1 options.

The formula for t ′ > 0 is more intricate, and less obvious:

d ( n , t , t ′ ) \= t ′ + t ′ ⋅ D ( n − t − t ′ + 1 ) + ( t − t ′ ) ⋅ D ( n − t − t ′ − 1 ) ,     if 1 ≤ t ′ ≤ t . ( 80 )

It can be justified by the structure theory of exercise 180 , using the fact that each of the first t ′ − 1 branches is immediately followed by a 1-way branch. For example, the search tree looks like this when t = 5 and t ′ = 3:

images

Here A 1 ′ is the only way to cover i 2 in A 1 , and A 2 ′ is the only way to cover i 3 in A 2 . The strict problems A 1 ′ , A 2 ′ , and A 3 have at most n − 7 options; A 4 and A 5 have at most n − 9. Therefore ( 81 ) has at most 3 + 3 D ( n − 7)+2 D ( n − 9) nodes.

With an easy computer program, ( 76 ), ( 78 ), and ( 80 ) lead to the values

n

=

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

D(n)

=

1

1

2

4

5

6

10

13

17

22

31

41

53

69

94

125

165

213

283

377

501

661

and it turns out that the maximum is attained uniquely when t = 4 and t ′ = 0, for all n ≥ 19. Hence we have D ( n ) = 1+4 D ( n − 5) for all sufficiently large n ; and in fact exercise 181 exhibits a simple formula that expresses D ( n ) exactly.

Theorem E. The search tree of a strict exact cover problem with n options has O (4 n /5 ) = O (1.31951 n ) nodes; it might have as many as Ω(7 n /8 ) = Ω(1.27537 n ).

Proof. The upper bound follows from exercise 181 ; the lower bound follows from the family of problems in exercise 182 .

[David Eppstein presented this theorem to the author as a birthday greeting(!); see [11011110.github.io/blog/2008/01/10/analyzing-algorithm-x.html](http://11011110.github.io/blog/2008/01/10/analyzing-algorithm-x.html) .]

So far we’ve simply been analyzing the number of nodes in Algorithm X ’s search tree. But some nodes might cost much more than others, because they might remove unusually many options from the currently active lists.

Therefore let’s probe deeper, by studying the number of updates that Algorithm X makes to its data structures, namely the number of times that it uses operation ( 1 ) to remove an element from a doubly linked list. (This is also the number of times that it will eventually use operation ( 2 ) to restore an element.) More precisely, the number of updates is the number of times cover( i ) is called, plus the number of times that hide( p ) sets LEN ( x ) ← LEN ( x ) − 1. (See ( 12 ) and ( 13 ).) The total running time of Algorithm X , measured in mems, usually turns out to be roughly 13 times the total number of updates that it makes.

It’s instructive to analyze the number of updates that are made when solving the “ extreme” exact cover problems, which arise when there are n items and 2 n − 1 options: Such problems have the most solutions and the most data, because every nonempty subset of the items is an option. The solutions to these extreme problems are precisely the set partitions — the ϖ n possible ways to partition the items into disjoint blocks, which we studied in Section 7.2.1.5. For example, when n = 3 the options are ‘1’, ‘2’, ‘1 2’, ‘3’, ‘1 3’, ‘2 3’, ‘1 2 3’, and there are ϖ 3 = 5 solutions: ‘1’, ‘2’, ‘3’; ‘1’, ‘2 3’; ‘1 2’, ‘3’; ‘1 3’, ‘2’; ‘1 2 3’.

Any given item can be covered in 2 n −1 ways; and if we cover it with an option of size k , we’re left with the extreme problem on the remaining n − k items. Algorithm X therefore advances 2 n −1 times from level 0 to level 1, after which it essentially calls itself recursively. And at level 0 it performs a certain number of updates, say υ n , regardless of what strategy is used in step X3 to choose an item for branching. Therefore it makes a total of x n updates, where

x n \= v n + ( n − 1 0 ) x n − 1 + ( n − 1 1 ) x n − 2 + ⋅ ⋅ ⋅ + ( n − 1 n − 1 ) x 0 . ( 82 )

The solution to this recurrence is x 0 = v 0 , x 1 = v 0 + v 1 , x 2 = 2 v 0 + v 1 + v 2 , and in general x n \= ∑ k \= 0 n a n k ⁢   v k , where the matrix ( a nk ) is

( 1 0 0 0 0 0 0 . ⁢   . ⁢   . 1 1 0 0 0 0 0 . ⁢ . ⁢ . 2 1 1 0 0 0 0 . ⁢ . ⁢ . 5 3 1 1 0 0 0 . ⁢ . ⁢ . 15 9 4 1 1 0 0 . ⁢ . ⁢ . 52 31 14 5 1 1 0 . ⁢ . ⁢ . 203 121 54 20 6 1 1 . ⁢ . ⁢ . . ⁢ . . . . . . ) \= (     1 0 0 0 0 0 0 . ⁢ . ⁢ . ⁢ − 1 1 0 0 0 0 0 . ⁢ . ⁢ . − 1 − 1 1 0 0 0 0 . ⁢ . ⁢   . − 1 − 2 − 1 1 0 0 0 . ⁢ . ⁢ . − 1 − 3 − 3 − 1 1 0 0 . ⁢ . ⁢ . − 1 − 4 − 6 − 4 − 1 1 0 . ⁢ . ⁢ . − 1 − 5 − 10 − 10 − 5 − 1 1 . ⁢ . ⁢ . . ⁢ . . . . . . ) − 1 , ( 83 )

with rows and columns numbered from 0. The numbers a n 0 in the left column, which solve ( 82 ) when v n \= δ n 0 , are the familiar Bell numbers ϖ n ; they enumerate the leaves in the search tree. The numbers a n 1 in the next column, which solve ( 82 ) when υ n \= δ n 1 , are the Gould numbers ϖ ^ n ; they enumerate set partitions whose last block or “ tail” is a singleton, when the blocks of the partition are ordered by their least elements. In general, a nk for k > 0 is the number of set partitions whose tail has size k . [See H. W. Gould and J. Quaintance, Applicable Analysis and Discrete Mathematics 1 (2007), 371–385.]

Exercise 186 proves that the actual number of updates at level 0 is

υ n \= ( ( 9 n − 27 ) 4 n − ( 8 n − 32 ) 3 n + ( 36 n − 36 ) 2 n + 72 − 41 δ n ⁢ 0 ) / 72 ; ( 84 )

and exercise 187 exploits relationships between the sequences ⟨ a nk ⟩ to show that

x n \= 22 ϖ n + 12 ⁢   ϖ ^ n − ( 2 3 n − 1 ) 3 n − 5 2 n 2 n − 12 n − 5 − 12 δ n 1 − 18 δ n 0 . ( 85 )

Asymptotically, ϖ ^ n / ϖ n converges rapidly to the “ Euler– Gompertz constant”

g ^ \=     ∞ 0    e − x d x 1 + x \= 0.59634 ⁢    73623 ⁢   23194 ⁢   07434 ⁢   10784 ⁢   99369 ⁢   27937 ⁢   60742 − ( 86 )

(see exercise 189 ). Thus x n ≈ (22 + 12ĝ)ϖ n ≈ 29.156ϖ n , and we’ve proved that Algorithm X performs approximately 29.156 updates per solution to the extreme exact cover problem, on average . That’s encouraging: One might suspect that the list manipulations needed to deal with 2 n options of average length n /2 would cost substantially more, but the dancing-links approach turns out to be within a constant factor of Section 7.2.1.5’s highly tuned methods for set partitions.

*Analysis of matching problems

Among the simplest exact cover problems are the ones whose options don’t contain many items. For example, a so-called X2C problem (“exact cover with 2-sets”) is the special case where every option has exactly 2 items; an X3C problem has 3 items per option; and so on. We’ve seen in ( 30 ) that sudoku is an X4C problem.

Let’s take a close look at the simplest case, the X2C problems. Despite their simplicity, we’ll see that such problems actually include many cases of interest. Every X2C problem corresponds in an obvious way to a graph G , whose vertices υ are the items and whose edges u — υ are the pairs of items that occur together in an option ‘ u υ ’. In these terms the X2C problem is the classical task of finding a perfect matching, namely a set of edges that contains each vertex exactly once.

We shall study efficient algorithms for perfect matching in Section 7.5.5 below. But an interesting question faces us now, in the present section: How well does our general-purpose Algorithm X compare to the highly tuned special-purpose algorithms that have been developed especially for matching in graphs?

Suppose, for example, that G is the complete graph K 2 q +1 . In other words, suppose that there are n = 2 q + 1 items {0, 1, . . . , 2 q }, and that there are m \= ( 2 q + 1 2 ) \= ( 2 q + 1 ) q options ‘ i j ’ for 0 ≤ i < j ≤ 2 q . This problem clearly has no solution, because we can’t cover an odd number of points with 2-element sets! But Algorithm X won’t know this (unless we give it a hint by factoring the problem appropriately). Thus it’s interesting to see how long Algorithm X will spin its wheels before giving up on this problem.

In fact the analysis is easy: No matter what item i is chosen in step X3, the algorithm will split nicely into 2 q branches, one for each option ‘ i j ’ with j ≠ i . And each of those branches will be equivalent to the matching problem on the remaining 2 q − 1 items; the remaining options will, in fact, be equivalent to the complete graph K 2 q −1 . Thus the search tree will have 2 q nodes at depth 1, (2 q )(2 q − 2) nodes at depth 2, ..., and (2 q )(2 q − 2) ... (2) = 2 q q ! nodes at depth q . Backtracking will occur at the latter nodes, which are leaves because they correspond to impossible matching in the graph K 1 .

How long does this process take? A closer look (see exercise 193 ) shows that the total number of updates to the data structure will satisfy the recurrence

U ( 2 q + 1 ) \= 1 + 2 q + 4 q 2 + 2 q U ( 2 q − 1 ) , for q > 0;        U ( 1 ) \= 1. ( 87 )

Consequently (see exercise 194 ) the number of updates needed by Algorithm X to discover that K 2 q +1 has no perfect matching turns out to be less than 8.244 times the number of leaves.

There’s better news when Algorithm X is presented with the complete graph K 2 q , because this problem has solutions — lots of them. Indeed, it’s easy to see that K 2 q has exactly (2 q − 1)(2 q − 3) ... (3)(1) = (2 q )! / (2 q q !) perfect matchings. For example, K 8 has 7 · 5 · 3 · 1 = 105 of them. The total number of updates in this case satisfies a recurrence similar to ( 87 ):

U ( 2 q ) \= 1 − 2 q + 4 q 2 + ( 2 q − 1 ) U ( 2 q − 2 ) ,                   for q > 0 ;         U ( 0 ) \= 0. ( 88 )

And exercise 194 proves that this is less than 10.054 updates per matching found.

Armed with these facts, we can work out what happens when the graph

images

is presented to Algorithm X . (This graph has 2 q + 2 r + 2 vertices.) The result, which is revealed in exercise 195 , is both instructive and bizarre.

A 2D matching problem — also called bipartite matching, and 2DM for short — is the special case of an X2C problem in which every option has the form ‘ X j Y k ’, where the items { X 1 , ..., X n } and { Y 1 , ..., Y n } are disjoint sets. Higher-dimensional matching is defined similarly; sudoku is actually a case of 4DM.

Let’s round out our analyses of matching by considering the bounded permutation problem : “Given a sequence of positive integers a 1 ... a n , find all permutations p 1 ... p n of {1, ..., n } such that p j ≤ a j for 1 ≤ j ≤ n .” We can assume that a 1 ≤ ··· ≤ a n , because p 1 ... p n is a permutation; we can also assume that a j ≥ j , otherwise there are no solutions; and we can assume without loss of generality that a n ≤ n . This is easily seen to be a 2DM problem, having exactly a 1 + ··· + a n options, namely ‘ X j Y k ’ for 1 ≤ j ≤ n and 1 ≤ k ≤ a j .

Suppose we branch first on X 1 . Then each of the a 1 subproblems is easily seen to be essentially a bounded permutation problem with n decreased by 1, and with a 1 ... a n replaced by ( a 2 − 1) ... ( a n − 1). Thus a recursive analysis applies, and again we find that the dancing links algorithm does rather well. For example, if a j \= min( j + 1, n ) for 1 ≤ j ≤ n , there are 2 n −1 solutions, and Algorithm X performs only about 12 updates per solution. If a j \= min(2 j, n ) for 1 ≤ j ≤ n , there are ⌊ n + 1 2 ⌋ !   ⌊ n + 2 2 ⌋ ! solutions, and Algorithm X needs only about 4 e − 1 ≈ 9.87 updates per solution. Exercise 196 has the details.

*Maintaining a decent focus

Some backtrack algorithms waste time by trying to solve two or more loosely related problems at once. Consider, for example, the 2DM problem with 7 items {0, 1, ..., 6} and the following 13 options:

‘ 0 ⁢   ⁢   1 ’ ,   ‘ 0 ⁢   ⁢     2 ’ , ‘ ⁢ 1 ⁢   ⁢   4 ’ , ‘ ⁢ 1 ⁢   ⁢   5 ’ , ‘ 1 ⁢   ⁢   6 ’ , ‘ 2 ⁢     4 ’ , ‘ 2 ⁢   ⁢   5 ’ , ‘ 2 ⁢     6 ’ , ‘ 3 ⁢       4 ’ , ‘ 3 ⁢     5 ’ , ‘ 3 ⁢       6 ’ , ‘ 4 ⁢     5 ’ , ‘ 4 ⁢     6 ’ . ( 90 )

Algorithm X , using its MRV heuristic, will branch on item 0, choosing either ‘0 1’ or ‘0 2’; then it will be faced with a three-way branch; and it will eventually conclude that there’s no solution, after implicitly traversing a 19-node search tree,

images

We get an extreme example of bad focus if we take n independent copies of problem ( 90 ), with 7 n items { k 0, k 1, ..., k 6} and 13 n options ‘ k 0 k 1’, ‘ k 0 k 2’, ..., ‘ k 4 k 6’, for 0 ≤ k < n : The algorithm will begin with 2-way branches on each of 00, 10, ..., ( n −1)0; then it will show that each of the 2 n resulting subproblems is unsolvable, making 3-way branches as it begins to study each one. Its search tree, before giving up, will have 10 · 2 n − 1 nodes. By contrast, if we had somehow forced the algorithm to keep its attention on the very first copy of ( 90 ) (the case k = 0), instead of using the MRV heuristic, it would have concluded that there are no solutions after backtracking through only 19 nodes.

Similarly, the simple exact cover problem on items {0, 1, ..., 5} with options

‘ 0 ⁢   ⁢   1 ’ ,   ‘ 0 ⁢   ⁢     2 ’ , ‘ ⁢ 1 ⁢   3 ⁢   ⁢   4 ’ , ‘ ⁢ 1 ⁢   3 ⁢   ⁢   5 ’ , ‘ ⁢ 1 ⁢   4 ⁢   ⁢   5 ’ , ‘ ⁢ 2 ⁢   3 ⁢     4 ’ , ‘ 2 ⁢     3 ⁢   ⁢ 4 ⁢   5 ’ , ‘ 2 ⁢     4 ⁢   5 ’ , ‘ 3 ⁢   ⁢   4 ’ , ‘ 3 ⁢     5 ’ , ‘ 4 ⁢     5 ’ , ( 92 )

has a search tree with 9 nodes, one of which is a solution:

images

Taking n independent copies of ( 92 ) gives us an exact cover problem with a unique solution, whose search tree via Algorithm X and MRV has 8 · 2 n − 7 nodes. But if the algorithm had been able to focus on one problem at a time, it would have discovered the solution with a search tree of only 8 n + 1 nodes.

From a practical standpoint, it must be admitted that the exponential behavior of these badly focused toy examples is worrisome only when n is larger than 30 or so, because 2 n is not scary for modern computers when n is small. Still, we can see that a well-focused approach can give significant advantages. So it will be useful to understand how Algorithm X and its cousins behave in general, when the input actually consists of two independent problems.

Let’s pause a minute to define the search tree precisely. Given an m × n matrix A of 0s and 1s, the search tree T of its associated exact cover problem is simply a solution node ‘▪’ when n = 0; otherwise T is

images

where the item chosen for branching in step X3 has d options, and T k is the search tree for the reduced problem after the items of the k th option have been removed. (With the MRV heuristic, d is the minimum length of all active item lists, and we choose the leftmost item having this value of d .)

The exact cover problem that we get when trying to solve two independent problems given by matrices A and A ′ is the problem that corresponds to the direct sum A ⨁ A ′ (see Eq. 7–(40)). Therefore if T and T ′ are the corresponding search trees, we will write T ⨁ T ′ for the search tree of A ⨁ A ′, under the MRV heuristic. (That tree depends only on T and T ′, not on any other aspects of A or A ′.) If either T or T ′ is simply a solution node, the rule is simple:

T ⊕   ▪ \= T ; ▪ ⊕     T ′ \= T ′ . ( 95 )

Otherwise T and T ′ have the form ( 94 ), and we have

images

Dear reader, please work exercise 202 — which is very easy! — before reading further. That exercise will help you to understand the definition of T ⨁ T ′; and you’ll also see that every node of T ⨁ T ′ is associated with an ordered pair αα ′, where α and α ′ are nodes of T and T ′, respectively. These ordered pairs are the key to the structure of T ⨁ T ′: If α and α ′ appear at levels l > 0 and l ′ > 0 of their trees, so that they are reached from the roots by the respective paths α 0 ─ α 1 ─ ··· ─ α l \= α and a 1 ′ — a 1 ′ — ⋅ ⋅ ⋅ a l ′ ′ \= a ′ , then the parent of αα ′ in T ⨁ T ′ is either α l −1 α ′ or a a l ′ − 1 ′ . Consequently every ancestor α k of α in T , for 0 ≤ k ≤ l , occurs in an ancestor a k ⁢   a k ′ ′ of αα ′ in T ⨁ T ′, for some 0 ≤ k ′ ≤ l ′.

Let deg( α ) be the number of children that node α has in a search tree, except that we define deg( α ) = ∞ when α is a solution node. (Equivalently, deg( α ) is the minimum length of an item list, taken over all active items in the subproblem that corresponds to node α . If α is a solution, there are no active items, hence the minimum is infinite.) Let’s call α a dominant node if its degree exceeds the degree of all its proper ancestors. The root node is always dominant, and so is every solution node. For example, ( 91 ) has three dominant nodes, and ( 93 ) has four.

In these terms, exercise 205 proves a significant fact about direct sums:

Lemma D. Every node of T ⨁ T ′ corresponds to an ordered pair αα ′ of nodes belonging to T and T ′. Either α or α ′ is dominant in its tree, or both are.

Lemma D is good news, focuswise, because the search trees that arise in practice tend to have comparatively few dominant nodes. In such cases the MRV heuristic manages to keep the search reasonably well focused, because T ⨁ T ′ isn’t too large. For example, the search trees for Langford pairs, or for the n queens problem, are “ minimally dominant”: Only their root nodes and their solutions dominate; elsewhere the branching degrees don’t reach new heights.

images

Fig. 73. A 15 × 15 square can be tiled with Y pentominoes, by setting up an exact cover problem with one item for each cell and one option for each placement of a Y. (To eliminate the 8-fold symmetry, only 5 of the 40 options for occupying the center cell were permitted.) Algorithm X ’s first solution, shown here, was found by branching successively on the possible ways to fill the cells marked 0, 1, ..., 9, a, ..., z, A, ..., I .

Let’s look now at a non-contrived example. Figure 73 illustrates a somewhat surprising way to pack 45 Y pentominoes into a 15 × 15 square. [Such tilings were first found in 1973 by J. Haselgrove, at a time when perfect Y-packings were known only for rectangles whose area was even. Her program first ruled out all rectangles of odd area less than 225, as well as the case 9×25, before discovering a 15×15 solution. See JRM 7 (1974), 229.] Notice that the first eight pentominoes in Fig. 73 , those marked 0 through 7 , were placed in or next to the four corners — thus flirting dangerously with the possibility that the algorithm might be trying to solve four independent problems at once! Luckily, the subsequent choices were able to gain and retain focus, because hard-to-fill cells almost always kept popping up near the recent activity. A five-way branch was needed only when placing the pentominoes marked 8 , b , e , g , h , and C in the solution shown.

Focus can sometimes be improved by explicitly preferring some items to others, based on their names; see the “ sharp preference” heuristic of exercise 10 .

Exercise 207 discusses another approach, an experimental modification of Algorithm X , which attempts to improve focus in situations like Fig. 73 by allowing a user to specify the importance of recent activity. The ideas are interesting, but so far they haven’t led to any spectacular successes.

Exploiting local equivalence

A close look at Fig. 73 reveals another phenomenon that is often present in exact cover problems: The tiles marked 8 and b , near the upper right corner, form an ‘H’ shape, which could be reflected left-right to yield another valid tiling. In fact there are three other such H’s in the picture; therefore Fig. 73 actually represents 2 4 = 16 different solutions to the problem, although those solutions are “locally” equivalent.

It turns out that the 15 × 15 tiling problem in Fig. 73 has exactly 212 mutually incongruent solutions, each of which can be rotated and/or reflected to make a set of eight that are congruent to each other; and each of those solutions contains at least two H’s. Algorithm X needs just 92 G μ to find them all. But we can do even better, because of H-equivalence: A slight extension to the options of the exact cover problem will produce only the solutions for which every ‘H’ has just one of its two forms — and so does every images , namely every 90° rotation of an ‘H’. (See exercise 208 .) This modified problem has just 16 solutions, which are obtained with only 26 G μ of computation and compactly represent all 212.

In general, an exact cover might contain four distinct options α , β , α ′, β ′ for which α and β are disjoint, α ′ and β ′ are disjoint, and

α + β \= α ′ + β ′ . ( 97 )

(The ‘+’ sign here is like ‘∪’; it stands for addition of binary vectors, when options are rows of a 0–1 matrix.) In such cases we say that ( α, β ; α ′, β ′) is a bipair . Whenever ( α, β ; α ′, β ′) is a bipair, every solution that contains both α and β leads to another solution that contains both α ′ and β ′, and vice versa. Thus we can avoid considering half of all such solutions if we exclude one of those alternatives. And it’s easy to do that: For example, to exclude all cases that contain both α ′ and β ′, we simply introduce a new secondary item, and append it to options α ′ and β ′.

To illustrate this idea, let’s apply it to the unsolvable toy problem ( 90 ). That problem has many bipairs, but we’ll consider only two of them,

( ‘ 0   1 ’ , ‘ 2   4 ’ ; ‘ 0   2 ’ , ‘ 1   4 ’ ) and ( ‘ 0   1 ’ , ‘ 2   5 ’ ; ‘ 0   2 ’ , ‘ 1   5 ’ ) . ( 98 )

To avoid solutions that contain both ‘0 1’ and ‘2 4’, as well as those that contain both ‘0 2’ and ‘1 5’, we introduce secondary items A and B, and we extend four of the options ( 90 ) to

‘ 0   1  A ’ , ‘ 0   2  B ’ , ‘ 1   5   B ’ , ‘ 2   4  A ’ . ( 99 )

Then the search tree ( 91 ) reduces to

images

and the former focusing problems disappear.

But wait, you say. Both of the bipairs in ( 98 ) involve the options ‘0 1’ and ‘0 2’. Why is it legal to prefer different halves of those overlapping bipairs? Isn’t it possible that we might “paint ourselves into a corner,” if we allow ourselves to make arbitrary decisions about each of several interrelated bipairs?

That’s a good question. Indeed, bad decisions can lead to trouble. Consider, for example, the problem of perfect matching on the complete bipartite graph K 3,3 , which can be coded as an X2C with the nine options ‘ x X ’ for x ∈ { x , y , z } and X ∈ { X , Y , Z }. (The problem of perfect matching on K n,n is equivalent to finding the permutations of n elements; thus K 3,3 has 3! = 6 perfect matchings.)

Every bipair (‘ t u ’, ‘ v w ’; ‘ t w ’, ‘ u v ’) in a perfect matching problem is equivalent to a 4-cycle t ─ u ─ v ─ w ─ t in the given graph. And if we disallow the right halves of the six bipairs

( ‘ x Y ’ , ‘ y X ’ ; ‘ x X ’ , ‘ y Y ’ ) ( ‘ x Y ’ , ‘ y Z ’ ; ‘ x Z ’ , ‘ y Y ’ ) ( ‘ y Y ’ , ‘ z X ’ ; ‘ y X ’ , ‘ z Y ’ ) ( ‘ y Y ’ , ‘ z Z ’ ; ‘ y Z ’ , ‘ z Y ’ ) ( ‘ z Y ’ , ‘ x X ’ ; ‘ z X ’ , ‘ x Y ’ ) ( ‘ z Y ’ , ‘ x Z ’ ; ‘ z Z ’ , ‘ x Y ’ )

we obtain nine options that have no solution:

‘ x X A ’ , ‘ y X B ’ , ‘ z X C ’ , ‘ x Y C D ’ ‘ y Y A E ’ ‘ z Y B F ’ , ‘ x Z E ’ , ‘ y Z F ’ , ‘ z Z D ’ . ( 101 )

Fortunately, however, there’s always a safe and easy way to proceed. We can assign an arbitrary (but fixed) ordering to the set of all options. Then, if for every bipair ( α, β ; α ′, β ′) we always choose the half that contains min( α, β, α ′, β ′), the choices will be consistent.

More precisely, we can express every bipair in the canonical form

( α , β ; α ′ , β ′ ) α < β , α < α ′ ,  and  α ′ < β ′ , ( 102 )

with respect to any fixed ordering of the options. An exact covering is called strong , with respect to a set of such canonical bipairs, if its options don’t include both α ′ and β ′ for any bipair in that set.

Theorem S. If an exact cover problem has a solution, it has a strong solution.

Proof. Every solution ∑ corresponds to a binary vector x = x 1 ... x M , where x j \= [option j is in ∑ ]. If ∑ isn’t strong, with respect to a given set of canonical bipairs, it violates at least one of those bipairs, say ( α, β ; α ′, β ′). Thus there are indices j , k , j ′, k ′ with j < k , j < j ′, and j ′ < k ′ such that α , β , α ′, β ′ are respectively the j th, k th, j ′th, k ′th options, and such that x j′ \= x k′ \= 1. By ( 97 ), x j \= x k \= 0; and we obtain another solution ∑ ′ by setting x j ′ ← x k ′ ← 1 , x j ′ ′ ← x k ′ ′ ← 0 , otherwise x i ′ \= x i . This vector x ′ is lexicographically greater than x ; so we’ll eventually obtain a strong solution by repeating the process.

In particular, we’re allowed to exclude both ‘0 1’ and ‘2 4’, as well as both ‘0 2’ and ‘1 5’, with respect to the bipairs ( 98 ), because we can choose an ordering in which options ‘1 4’ and ‘2 5’ precede the other options ‘0 1’, ‘0 2’, ‘1 5’, ‘2 4’.

Another convenient way to make consistent choices among related bipairs is based on ordering the primary items, instead of the options. (See exercise 212 ).

It’s interesting to apply this theory to the problem of perfect matching in the complete graph K 2 q +1 . We showed in ( 87 ) above that Algorithm X needs a long time — Ω(2 q q !) mems — to discover that this problem has no solution. But bipairs come to the rescue.

Indeed, K 2 q +1 has lots of bipairs, Θ( q 4 ) of them. A straightforward application of Theorem S, using the natural order ‘0 1’ < ‘0 2’ < ··· < ‘(2 q −1) 2 q ’ on the ( 2 q + 1 2 ) options, solves the problem in Θ( q 4 ) mems, by using just Θ( q 3 ) of the bipairs. And a more clever way to order the options allows us to solve it in only Θ( q 2 ) mems, using just Θ( q 2 ) well-chosen bipairs. The search tree can in fact be reduced to just 2 q + 1 nodes — which is optimum! Exercise 215 explains all.

* Preprocessing the options

Sometimes the input to an XCC problem can be greatly simplified, because we can eliminate many of its options and/or items. The general idea of “preprocessing,” which transforms one combinatorial problem into an equivalent but hopefully simpler one, is an important paradigm, which is often called kernelization for reasons that we shall discuss later.

Algorithm P below is a case in point. It takes any sequence of items and options that would be acceptable to Algorithm X or to Algorithm C , and produces another such sequence with the same number of solutions. Any solution of the new problem can in fact be converted to a solution of the original one, if desired.

The algorithm is based entirely on two general principles, used repeatedly until they no longer apply:

  • An option can be removed if it blocks all uses of some primary item.

  • An item can be removed if some primary item always forces it to appear.

More precisely, let o be a generic option ‘ i 1 i 2 [: c 2 ] ... i t [: c t ]’, where i 1 is primary and the other t − 1 items might have color controls. When Algorithm C deals with option o , it covers i 1 in step C4 and commits the other items in step C5, thereby removing all options that aren’t compatible with o . If this process causes some primary item p to lose its last remaining option, we say that p is “blocked” by o . In such a case o is useless, and we can remove it. For example, ‘ deg ’ can be removed from ( 6 ), because it blocks a ;‘1 s 4 s 6 ’ can be removed from ( 17 ), because it blocks s 3 ; then ‘1 s 1 s 3 ’ and ‘2 s 2 s 5 ’ also go away, because they block s 4 .

That was the first principle mentioned above, the one that removes options. The item-removing principle is similar, but more dramatic when it applies: Let p be a primary item, and suppose that p ’s options all contain an uncolored instance of some other item, i . In such a case we say that p “forces” i ; and we can remove item i , because p must be covered in every solution and it carries i along. For example, a forces d in ( 6 ). Hence we can remove item d , shortening the second and fourth options to just ‘ a g ’ and ‘ a f ’.

These two principles, blocking and forcing, are by no means a complete catalog of transformations that could be used to preprocess exact cover problems. For example, they are incapable of discovering the fact that ( 38 ) is a useless option in the pentomino problem, nor do they discover the simplifications that we deduced by factoring the Soma cube problem. (See the discussion before ( 46 ).) Exercise 219 discusses yet another way to discard superfluous options.

A “perfect” and “complete” preprocessor would in fact be able to recognize any problem that has at most one solution. We can’t hope to achieve that, so we’ve got to stop somewhere. We shall limit ourselves to the removal of blocking and forcing, because those transformations can be done in polynomial time, and because no other easily recognizable simplifications are apparent.

Algorithm P discovers all such simplifications by systematically traversing the given items and options, using the same data structures that were enjoyed by Algorithm C . It cycles through all items i , trying first to remove i by studying what happens when i is covered. If that fails, it studies what happens when the items of options that begin with i are committed. It needs some small variations of the former ‘cover’ and ‘ hide’ operations (compare with ( 12 )–( 15 ), ( 50 )–( 53 )):

cover ″ ( i ) \= { Set  p ← DLINK ( i ) .   While  p ≠ i ,      hide ″ ( p )  unless  COLOR ( p ) ≠ 0 ,      then set  p ← DLINK ( p )  and repeat . ( 103 )

hide ″ ( p ) \= { Do operation hide ( p ) ;  but also set  S ← x ,     whenever  LEN ( x )  has been set to  0  and  x ≤ N 1 . ( 104 )

uncover ″ ( i ) \= { Set  p ← ULINK ( i ) .   While  p ≠ i ,      unhide ( p )  unless COLOR ( p ) ≠ 0 ,      then set  p ← ULINK ( p )  and repeat . ( 105 )

Algorithm P ( Preprocessing for exact covering ). This algorithm reduces a given XCC problem until no instances of blocking or forcing are present. It uses the data structures of Algorithm C , together with new global variables C and S .

P1. [Initialize.] Set the problem up in memory, as in step C1 of Algorithm C . (Again there are N items, of which N 1 are primary.) Also set C ← 1. If there’s an item i ≤ N 1 with LEN ( i ) = 0, go to P9.

P2. [Begin a round.] If C = 0, go to P10. Otherwise set C ← 0, i ← 1.

P3. [Is item i active?] If i > N , return to P2. Otherwise if LEN ( i ) = 0, go to P8.

P4. [Cover i .] Set S ← 0. Use ( 103 ) to cover item i . Then go to P7 if S ≠ 0; otherwise set x ← DLINK ( i ).

P5. [Try x .] If node x isn’t the leftmost remaining node of its option, or if COLOR ( x ) ≠ 0, go to P6. Otherwise use the method of exercise 220 to test whether this option blocks some primary item. If so, set C ← 1, TOP ( x ) ← S , and S ← x .

P6. [Try again.] Set x ← DLINK ( x ), and return to P5 if x ≠ i . Otherwise uncover item i using ( 105 ); use the method of exercise 221 to delete all options that were stacked in step P5; and go to P8.

P7. [Remove item i .] Uncover item i (which is forced by the primary item S ). Then use the method of exercise 222 to delete or shorten every option that uses item i . Finally, set C ← 1, DLINK ( i ) ← ULINK ( i ) ← i , LEN ( i ) ← 0.

P8. [Loop on i .] Set i ← i + 1 and return to P3.

P9. [Collapse.] Set N ← 1 and delete all options. (The problem is unsolvable.)

P10. [Finish.] Output the reduced problem, whose items are those for which LEN ( i ) > 0 or i = N = 1, and terminate. (See exercise 223 .)

How effective is Algorithm P ? Well, sometimes it spins its wheels and finds absolutely nothing to simplify. For example, the options ( 16 ) for n Langford pairs contain no instances of blocking or forcing when n > 5. Neither do the options for the n queens problem when n > 3. There’s no “excess fat” in those specifications. In MacMahon’s triangle problem ( exercise 126 ), Algorithm P needs just 20 megamems to remove 576 of the 1537 options; but the options that it removes don’t really matter, because Algorithm C traverses exactly the same search tree, with or without them.

We do gain 10% when we try to pack pentominoes into a 6 × 10 box ( exercise 271 ): Without preprocessing, Algorithm X needs 4.11 G μ to discover all 2339 solutions to that classic task. But Algorithm P needs just 0.19 G μ to remove 235 of the 2032 options, after which Algorithm X finds the same 2339 solutions in 3.52 G μ ; so the total time has been reduced to 3.71 G μ . The similar problem of packing the one-sided pentominoes into a 6 × 15 box has an even bigger payoff: It has 3308 options without preprocessing, and 15.5 T μ are needed to process them. But after preprocessing — which costs a mere 260 M μ — there are 3157 options, and the running time has decreased to 13.1 T μ .

The simplifications discovered by Algorithm P for those pentomino problems involve only blocking (see exercise 225 ). But more subtle reductions occur in the Y pentomino problem of Fig. 73 . For example, cell 20 is forced by cell 10, in that problem; and in round 2, cell 00 is forced by cell 22. In round 4, cell 61 is blocked by the option ‘50 51 52 53 62’ — a surprising discovery! Unfortunately, however, those clever reductions have little effect on the overall running time.

Preprocessing really shines on the problem of exercise 114 , which asks for all sudoku solutions that are self-equivalent when reflected about their main diagonal. In this case Algorithm P is presented with 5410 options that involve intricate color controls, on 585 primary items and 90 secondary items. It rapidly reduces them to just 2426 options, on 506 primaries and 90 secondaries; and Algorithm C needs only 287 G μ to process the reduced options and to discover the 30,258,432 solutions. That’s 7.5 times faster than the 2162 G μ it would have needed without reduction.

Thus, preprocessing is a mixed bag. It might win big, or it might be a waste of time. We can hedge our bets by allocating a fixed budget — for instance by deciding that Algorithm P will be allowed to run at most a minute or so. Its data structures are in a “safe” state at the beginning of step P3; therefore we can jump from there directly to step P10 if we don’t want to run to completion.

Of course, preprocessing can also be applied to the subproblems that arise in the midst of a longer computation. A careful balancing of different strategies might be the key to solving problems that are especially tough.

Minimum- cost solutions

Many of the exact cover problems that we’ve been studying have few solutions, if any. In such cases our joy is to discover the rare gems. But in many other cases the problems have solutions galore; and for such problems we’ve focused our attention so far on the task of minimizing the amount of time per solution, assuming that all of the solutions are interesting.

A new perspective arises when each option of our problem has been assigned a nonnegative cost . Then it becomes natural to seek solutions of minimum cost. And ideally we’d like to do this without examining very many of the high-cost solutions at all; they’re basically useless, but a low-cost solution might be priceless.

Fortunately there’s a reasonably simple way to modify our algorithms, so that they will indeed find minimum-cost solutions rather quickly. But before we look at the details of those modifications, it will be helpful to look at several examples of what is possible.

Consider, for instance, the problem of Langford pairs from this point of view. We observed near the very beginning of Chapter 7 that there are 2 L 16 = 653,443,600 ways to place the 32 numbers {1, 1, 2, 2, ..., 16, 16} into an array a 1 a 2 ... a 32 so that exactly i entries lie between the two occurrences of i , for 1 ≤ i ≤ 16. And we claimed that the pairing displayed in 7–(3), namely

2   3   4   2   1   3   1   4   16   13   15   5   14   7   9   6   11   5   12   10   8   7   6   13   9   16   15   14   11   8   10   12 , ( 106 )

is one of 12,016 solutions that maximize the sum Σ 1 \= Σ k \= 1 32 k a k . Consequently the reverse of that pairing, namely

12   10   8   11   14   15   16   9   13   6   7   8   10   12   5   11   6   9   7   14   5   15   13   16   4   1   3   1   2   4   3   2 , ( 107 )

is one of 12,016 solutions that minimize ∑ 1 . We noted in ( 16 ) above that Langford pairs are the solutions to a simple exact cover problem, whose options ‘ i s j s k ’ represent the assignments a j \= i and a k \= i . Therefore, if we associate the cost $( ji + ki ) with option ‘ i s j s k ’, the minimum-cost solutions will be precisely the Langford pairings that minimize ∑ 1 . (See exercise 226 .)

One way to minimize the total cost is, of course, to visit all solutions and to compute the individual sums. But there’s a better way: The min-cost variant of Algorithm X below, which we shall call Algorithm X $ , finds a solution of cost $3708 and proves its minimality after only 60 gigamems of computation. That’s more than 36 times faster than the use of plain vanilla Algorithm X , which needs 2.2 teramems to run through the full set of solutions.

Moreover, Algorithm X $ doesn’t stop there. It actually will compute the K solutions of least cost, for any given value of K . For example, if we take K = 12500, it needs just 70 gigamems to discover the 12,016 solutions of cost $3708, together with 484 solutions of the next-lowest cost (which happens to be $3720).

The news is even better when we try to minimize Σ 2 \= Σ k \= 1 32   k 2 a k instead of ∑ 1 . Algorithm X $ needs just 28 G μ to prove that the minimum ∑ 2 is $68880. And better yet is the fact, obtained in only 10 G μ , that the minimum of Σ k \= 1 32   k a k 2 is $37552, obtainable uniquely by the remarkable Langford pairing

16   14   15   9   6   13   5   7   12   10   11   6   5   9   8   7   14   16   15   13   10   12   11   8   4   1   3   1   2   4   3   2 , ( 108 )

which also happens to minimize both ∑ 1 and ∑ 2 ! (See exercise 229 .)

Another classic combinatorial task, the 16 queens problem, provides another instructive example. We know from previous discussions that there are exactly 14,772,512 ways to place 16 nonattacking queens on a 16 × 16 board. We also know that Algorithm X needs about 40 G μ of computation to visit them all, when we give it options like ( 23 ).

Let’s suppose that the cost of placing a queen in cell ( i, j ) is the distance from that cell to the center of the board. (If we number the rows and columns from 1 to 16, that distance d ( i, j ) is ( i − 17 / 2 ) 2 + ( j − 17 / 2 ) 2 ; it varies from d ( 8 , 8 ) \= 1 / 2  to   d ( 1 , 1 ) \= 15 / 2 .) Thus we want to concentrate the queens near the center as much as possible, although many of them must lie at or near the edges because there must be one queen in each row and one queen in each column.

images

Fig. 74. Optimum solutions to the 16 queens problem, placing them (a) as close as possible to the center, or (b, c) as far as possible from it.

Figure 74(a) shows how to minimize the total cost — and this answer actually turns out to be unique, except for rotation and reflection. Similarly, Figs. 74(b) and 74(c) show the placements that maximize the cost. (Curiously, those solutions are obtainable from each other by reflecting the middle eight rows, without changing the top four or the bottom four.) Algorithm X $ , with K = 9, discovers and proves the optimality of those placements in just (a) 3.7 G μ ; (b, c) 0.8 G μ .

The modifications that convert Algorithm X to Algorithm X $ also convert Algorithm C into Algorithm C $ . Therefore we can find minimum-cost solutions to XCC problems, which go well beyond ordinary exact cover problems.

For example, here’s a toy problem that now becomes tractable: Put ten different 5-digit prime numbers into the rows and columns of a 5 × 5 array, in such a way that their product is as small as possible. (A 5-digit prime number is one of the 8363 primes between 10007 and 99991, inclusive.) One such “prime square,” made up entirely of primes that are less than 30000, is

[ 2 1 2 1 1 2 0 1 0 1 1 1 0 0 3 1 1 0 6 9 1 1 1 1 3 ] . ( 109 )

To set this up as an XCC problem, introduce ten primary items { a 1 , a 2 , a 3 , a 4 , a 5 } and { d 1 , d 2 , d 3 , d 4 , d 5 } that represent “across” and “down,” together with 25 secondary items ij for 1 ≤ i, j ≤ 5 that represent cells of the array, together with 8363 additional secondary items p 1 p 2 p 3 p 4 p 5 , one for eligible prime p = p 1 p 2 p 3 p 4 p 5 . The options for placing p in row i or column j are then

‘ a i i 1 : p 1 i 2 : p 2 i 3 : p 3 i 4 : p 4 i 5 : p 5 p 1 p 2 p 3 p 4 p 5 ’ ; ‘ d j 1 j : p 1 2 j : p 2 3 j : p 3 4 j : p 4 5 j : p 5 p 1 p 2 p 3 p 4 p 5 ’ . ( 110 )

For example, ‘ a 4 41:1 42:1 43:0 44:6 45:9 11069’ enables the prime 11069 in ( 109 ).

This is a good example where preprocessing is helpful, because the primes that are usable in a 1 and d 1 must not contain a 0; furthermore, the primes that are usable in a 5 and d 5 must contain only the digits {1, 3, 7, 9}. Algorithm P discovers those facts on its own, without being told anything special about number theory. It reduces the 83630 options of ( 110 ) to only 62900; and those reductions provide useful clues for the choices of items on which to branch.

The Monte Carlo estimate of exercise 86 tells us that there are roughly 6 × 10 14 different ways to fit ten primes into a 5 × 5 array — a vast number. We probably don’t need to look at too many of those possibilities, yet it isn’t easy to decide which of them can safely be left unexamined.

To minimize the product of the primes, we assign the cost $(ln p ) to each of the options in ( 110 ). (This works because the logarithm of a product is the sum of the logarithms of the factors.) More precisely, we use the cost $ ⌊ C ln p ⌋, where C is large enough to make truncation errors negligible, but not large enough to cause arithmetic overflow, because Algorithm C $ wants all costs to be integers.

Every solution has the same cost as its transpose. Thus we can get the best five prime squares by asking Algorithm C $ to compute the K = 10 least-cost solutions, each of which occurs twice. Here they are, with the best at the left:

[ 1 1 1 1 3 1 0 1 0 3 1 1 0 0 3 3 1 7 6 9 1 1 1 7 1 ] [ 1 1 1 1 3 1 0 1 0 3 1 1 0 0 3 3 1 7 7 1 1 1 1 9 7 ] [ 1 1 1 1 3 1 0 0 0 7 1 1 0 0 3 3 1 6 9 9 1 1 1 1 7 ] [ 1 1 1 1 3 1 0 0 0 7 1 1 0 0 3 3 1 6 6 3 1 1 1 7 7 ] [ 1 1 1 1 3 1 0 0 0 7 1 1 0 0 3 3 1 6 6 3 1 1 1 9 7 ] . ( 111 )

The running time, 440 G μ , would have been 1270 G μ without preprocessing; so the 280 G μ spent in preprocessing paid off. But the five greatest-cost solutions,

[ 9 9 9 8 9 8 8 9 9 7 9 8 6 8 9 9 9 7 9 3 9 9 9 9 1 ] [ 9 9 9 8 9 8 9 8 9 9 9 6 7 9 9 9 8 7 3 7 9 9 9 9 1 ] [ 9 9 9 8 9 8 8 9 9 7 9 8 8 9 7 9 9 5 7 1 9 9 9 7 1 ] [ 9 9 9 8 9 8 8 9 9 7 9 8 8 9 7 9 9 5 8 1 9 9 9 9 1 ] [ 9 9 9 8 9 8 8 9 9 7 9 8 8 9 7 9 9 5 7 7 9 9 9 7 1 ] ( 112 )

(greatest at the right), can be found in just 22 G μ , without preprocessing.

Let’s turn now from purely mathematical problems to some “organic” scenarios that are more typical of the real world. The USA’s 48 contiguous states

images

define an interesting planar graph that has already supplied us with a variety of instructive examples. This graph G has 48 vertices and 105 edges. Suppose we want to partition it into eight connected subgraphs of six vertices each. What’s the minimum number of edges whose removal will do that?

Well, exercise 7.2.2–76 has told us how to list all of the connected subsets of six states, and there happen to be 11505 of them. That gives us 11505 options for an exact cover problem on 48 items, whose solutions are precisely the potential partitions of interest. The total number of solutions turns out to be 4,536,539; and Algorithm X is able to visit them all, at a cost of 807 gigamems.

But let’s try to do better, using Algorithm X $ . Every induced subgraph G | U has an exterior cost , which is the number of edges from U to vertices not in U . When we partition a graph by removing edges, every such edge contributes to the exterior cost of two of the components that remain; hence the number of removed edges is exactly half the sum of the exterior costs. The best partition therefore corresponds to the minimum-cost solution to our exact cover problem, if we assign the exterior costs to each option. For example, one of the 11505 options is

‘ ND SD NE KS OK TX ’ , ( 114 )

and we assign a cost of $19 to that option.

Algorithm X $ now obligingly finds, in just 3.2 gigamems, that the optimum solution costs $72. Hence we get the desired partition by removing only 36 edges.

Before we look at the answer, let’s stare at the problem a bit longer, because we still haven’t discovered the best way to solve it! A closer examination shows that option ( 114 ) is useless, because it could never actually appear in any solution: It cuts the graph into two pieces, with 11 states to the left and 31 states to the right. (We encountered a similar situation earlier in ( 38 ).) In fact, 4961 of the 11505 options turn out to be unusable, for essentially the same reason. The state of Maine ( ME ), for example, belongs to 25 connected subgraphs of order 6; but we can easily see that the only way to get ME into the final partition is to group it with the other five states of New England ( NH , VT , MA , CT , RI ). Exercise 242 explains how to detect and reject the useless options quickly.

The remaining problem, which has 6544 options, is solved by Algorithm X in 327 G μ and by Algorithm X $ in just 1046 M μ .

Essentially the same methods will partition the graph nicely into six connected clusters of order eight. This time the exact cover problem has 40520 options after reduction, and a total of 4,177,616 solutions. But Algorithm X $ needs less than 2 G μ to determine the minimum cost, which is $54.

Here are examples of the optimum partitions found, 8 × 6 and 6 × 8:

images

In each case the optimum can actually be achieved in two ways: On the left, one could swap the affiliations of VA and WV ; on the right, a more complicated cyclic shuffle ( MI NE LA VA ) could be used.

It’s also instructive to solve a different kind of problem, namely to use census data and to partition G based on the population of each state. For example, let’s try to find eight connected clusters that each contain nearly the same number of people. The total population, P , of the 48 states was officially 306084180 in 2010. We want each cluster to represent P /8 people, or as close to P /8 as we can get. That’s about 38 million people per cluster.

The algorithm of exercise 242 will find and reduce all connected subgraphs whose total population x satisfies L ≤ x < U , for any given bounds L and U . If we take L > ⌊ P /9⌋ (which is about 34 million) and U ≤ ⌈ P /7⌉ (which is about 44 million), those candidate subgraphs will define an exact cover problem for which every solution uses exactly eight options, because 9 x > P and 7 x < P .

That algorithm proves that G contains 1,926,811 connected sets of states whose population lies in [34009354 .. 43726312); and it prunes away 1,571,057 of them, leaving 355,754. But that’s overkill. This problem has enough flexibility that its final solution can be expected to contain only sets whose population is quite close to 38 million. Therefore we might as well restrict ourselves to the range [37000000 .. 39000000) instead. There are 34,111 such options; surely they should be enough to solve our problem.

Well, that’s very plausible, but unfortunately it doesn’t work: Those 34,111 options have no solution, because Algorithm X can’t use them to cover NY ( New York)! Notice that NY is an articulation point of G . The population of New York is about 19.4 million, and the combined population of the six New England states is about 14.3 million. Whatever option covers New York had better cover all of New England too, otherwise New England is stranded. So that makes 19.4 + 14.3 = 33.7 million people. New York’s only other neighbors are New Jersey (8.8 million) and Pennsylvania (12.7 million); adding either of them will put us over 42 million.

So we’re clearly not going to be able to cover New York with a cluster that’s close to the desired 38 million. We’ll either need a lightweight one (New York plus New England) or a heavyweight one (with New Jersey too). Let’s throw those two options in with the other 34,111.

Notice that this problem is quite different from the others we’ve been discussing, because its options vary greatly in size. One of the options contains just one state, CA (California), whose population is the largest (37.3 million); others contain up to fifteen states, almost spanning the continent from DE to NV .

Now we assign the cost $( x 2 ) to each option with population x , because the minimum-cost solutions will then minimize the squared deviations ( x 1 − P /8) 2 + ··· +( x 8 − P /8) 2 . (See exercise 243 .) This works well; and Algorithm X $ needs only 3.3 gigamems to find the optimum solution below. The seven options not involving New York all contain between 37.3 and 38.1 million people.

A similar analysis, partitioning into six equipopulated clusters instead of eight, gives in 1.1 G μ a minimum-cost solution whose six populations are all in the range [50650000 .. 51150000]. Both solutions are illustrated here, with the area of each vertex proportional to its population:

images

In both cases the solution is unique. (And in both cases, let’s face it, the solution is also pretty weird. Partitions like this could only be concocted by a computer. Exercise 246 discusses approximate solutions that are less eccentric.)

*Implementing the min-cost cutoffs

OK, we’ve now seen lots of reasons why Algorithms X $ and C $ are desirable. But how exactly can we obtain those algorithms by extending Algorithm X and C ? It will suffice to describe Algorithm C $ .

The mission of Algorithm C $ is to find the K min-cost solutions. More precisely, it should discover K solutions whose total cost is as small as possible, with the understanding that different solutions might have the same cost. Let’s imagine, for example, a problem that has exactly ten solutions, and that their costs are $3, $1, $4, $1, $5, $9, $2, $6, $5, $3, in the order that Algorithm C would discover them. Algorithm C $ won’t differ from Algorithm C until it has found K solutions, because those K might turn out to be the best. After K are known, however, it will be harder to please: It will accept a new solution only if that solution is better than one of the best K it knows. Thus if K = 3, say, the accepted solutions will have costs $3, $1, $4, $1, $2; Algorithm C $ won’t find the other five.

To implement that behavior, we maintain a BEST table, which contains the K least costs known so far. That table is “ heap ordered,” with

BEST [ ⌊ j / 2 ⌋ ] ≥ BEST [ j ] for 1 ≤ ⌊ j / 2 ⌋ < j ≤ K ( 117 )

(see Eq. 5.2.3–(3)). In particular, BEST[ 1 ] will be the greatest of the least K costs, and we call it the cutoff value , T . Algorithm C $ will reject any solution whose cost is T or more. Initially, BEST[ j ] = ∞ for 1 ≤ j ≤ K ; then every new solution of cost c < T will be “sifted” into the BEST table as in Algorithm 5.2.3H. The successive cutoff values in the example above, if K = 3, would therefore be ∞, ∞, 4, 3, 3, 3, 2, 2, 2, 2. If K = 4 they’d be ∞, ∞, ∞, 4, 4, 4, 3, 3, 3, 3.

Algorithm C $ adds a COST field to every node, thereby making each node 64 bits larger than before. Step C1 $ stores the cost of every option, assumed to be a nonnegative integer, in each node belonging to that option.

The costs in every list of options created by step C1 $ are ordered , so that

COST ( x ) ≤ COST ( y ) if y \= DLINK ( x ) , ( 118 )

whenever neither x nor y is a header node. Therefore, if p is primary and belongs to t options, we have COST ( x 1 ) ≤ COST ( x 2 ) ≤ ··· ≤ COST ( x t ), where x 1 = DLINK ( p ), x j +1 \= DLINK ( x j ) for 1 ≤ j < t , and p = DLINK ( x t ). This fact will allow us to ignore options that are too expensive to be part of a min-cost solution.

For this purpose we generalize the basic operations of covering, purifying, uncovering, and unpurifying (see ( 50 )–( 57 )), by including a threshold parameter ϑ : Their loops in Algorithm C $ now say ‘While q ≠ i and COST ( q ) < ϑ ’ instead of simply ‘While q ≠ i ’. We also change the uncovering and unpurifying operations, so that they now go downward using DLINK s instead of upward using ULINK s. Furthermore, we make the unhiding operation of ( 15 ) go from left to right, with q increasing, just as hiding does in ( 13 ). (These conventions clearly flout the rules by which we established the validity of dancing links in the first place! But we’re lucky, because they’re justified by the theory in exercise 2 .)

At level l of the search, Algorithm C $ has constructed a partial solution, consisting of l options represented by nodes x 0 ... x l −1 . Let C l be their total cost. In step C4 $ we set x l ← DLINK ( i ), then cover item i using the threshold value ϑ 0 = T − C l − COST ( x l ). (Item i will have been chosen so that ϑ 0 > 0.) The covering process will now proceed faster than before, if ϑ 0 is fairly low, because it won’t bother to hide options that could not be in an accepted solution. We need to remember the value of ϑ 0 , so that exactly the same threshold will be used when backtracking; therefore step C4 $ sets TH0[ l ] ← ϑ 0 , and step C7 $ uses TH0[ l ] as the threshold for uncovering item i , where TH0 is an auxiliary array.

The cutoff value T decreases as computation proceeds. Therefore the threshold ϑ = T − C l − COST ( x l ) used in step C5 $ for covering and purification might be different each time. Step C5 $ should go directly to C7 $ if ϑ ≤ 0. Otherwise it sets TH [ l ] ← ϑ in that step, and uses TH [ l ] for undoing in step C6 $ , where TH is another auxiliary array.

Step C3 $ , which chooses the item on which to branch at level l , is of course crucially important. If some primary item i has no options, or if the cost of its least expensive option is so high that it can’t lead to a solution better than we’ve already found, step C3 $ should jump immediately to step C8 $ . Otherwise, many strategies are worthy of investigation, and there’s room here to discuss only the method that was used in the author’s experiments: Good results were obtained by choosing an i with the fewest not-too-costly options, as in the MRV heuristic. In case of ties, the author’s implementation chose an i whose least expensive option cost the most . (That item must be covered sooner or later, so there’s no way to avoid paying that much. We probably have a better chance of reaching a cutoff quickly if we maximize our chances of failure.) Exercise 248 has full details.

Many applications of Algorithm C $ have special features that allow us to prune unproductive branches from the search tree long before they would be cut off by the methods discussed so far. For example, every option in our “ square of primes” problem has exactly one primary item (see ( 110 )). In such cases, we know that every solution obtained by extending x 0 ... x l − 1 must cost at least

C l + COST ( DLINK ( i 1 ) ) + ... + COST ( DLINK ( i t ) ) , ( 119 )

because of ( 118 ), where i 1 , ..., i t are the primary items still active. If this total is T or more, step C3 $ can proceed immediately to step C8 $ .

Similarly, in the n queens problem, every option has exactly two primary items, one of the form R i and one of the form C j . The active items i 1 , ..., i t must therefore contain t /2 of each. Let C R and C C be Σ COST ( DLINK ( i j )), summed over those types. If either C l + C R or C l + C C is ≥ T , step C3 $ can jump to C8 $ .

In our first problem for the contiguous USA, every option has exactly 6 items. (See ( 114 ).) Hence the number of active items, t , is always a multiple of 6. Exercise 249 presents a nice algorithm to find the least possible cost of t /6 future options; step C3 $ of Algorithm C $ uses that method to find early cutoffs.

The Langford pair problem has options with three items, one of which is a digit. The pentomino problems have options with six items, one of which is a piece name. In both cases, Algorithm C $ can obtain suitable lower bounds for early cutoffs by combining the strategies already mentioned. (See exercise 250 .)

Finally, Algorithm C $ uses one other important technique: It gains traction by preprocessing the costs . Notice that if p is a primary item, and if the cost of every option that includes p is c or more, we could decrease the cost of all those options by c, without changing the set of min-cost solutions . That’s true because p is going to appear exactly once in every solution. We can think of c as an unavoidable tax or “cover charge,” which must be paid “up front.”

In general there are many ways to preprocess the costs without changing the underlying problem. Properly transformed costs can help the algorithm’s heuristics to make much more intelligent choices. Exercise 247 discusses the simple method that was used for step C1 $ in the author’s experiments discussed earlier.

* Dancing with ZDDs

The solutions visited by Algorithm X in step X2 can be represented naturally in the form of a decision tree, as we discussed in Section 7.1.4. For example, here’s a decision tree for the solutions to the problem of covering the eight cells of a 3 × 3 board with four dominoes, after the corner cell 22 has been removed:

images

This diagram uses the standard ZDD conventions: Every branch node names an option. A solid line means that the option is taken, while a broken line means that it is not. The terminal nodes ⊥ and ⊤ represent failure and success. This problem has four solutions, corresponding to the four paths from the root to ⊤.

We learned in Section 7.1.4 that ZDDs can readily be manipulated, and that a small ZDD can sometimes characterize a large family of solutions. If we’re lucky, we can save a huge amount of time and energy by simply generating an appropriate ZDD instead of visiting the solutions one by one.

Such economies arise when the same subproblem occurs repeatedly. For example, two branches come together in ( 120 ) at the node marked ‘*’; this happens because the problem that remains after placing two dominoes ‘00 10’ and ‘01 11’ is the same as the residual problem after placing ‘00 01’ and ‘10 11’. “We’ve been there and done that.” Hence we needn’t recapitulate our former actions, if we’ve already built a subZDD to remember what we did. (Those two pairs of domino placements form a “ bipair,” as discussed earlier; but the ZDD idea is considerably more general and powerful.)

Let’s look more closely at the underlying details. The exact cover problem solved by ( 120 ) has eight items 00, 01, 10, 02, 11, 20, 12, 21, representing cells to be covered; and it has the following ten options, representing domino placements:

1 : 00 01 3 : 01 02 5 : 02 12 7 : 10 20 9 : 11 21 2 : 00 10 4 : 01 11 6 : 10 11 8 : 11 12 10 : 20 21 ( 121 )

The ZDD ( 120 ) is internally represented as a sequence of branching instructions,

I 12 \= ( 2 ¯ ? 8 : 11 ) , I 11 \= ( 4 ¯ ? 10 : 3 ) , I 10 \= ( 3 ¯ ? 0 : 9 ) , I 9 \= ( 8 ¯ ? 0 : 2 ) , I 8 \= ( 1 ¯ ? 0 : 7 ) , I 7 \= ( 7 ¯ ? 4 : 6 ) , I 6 \= ( 5 ¯ ? 0 : 5 ) , I 5 \= ( 9 ¯ ? 0 : 1 ) , I 4 \= ( 6 ¯ ? 0 : 3 ) , I 3 \= ( 5 ¯ ? 0 : 2 ) , I 2 \= ( 10 ¯ ? 0 : 1 ) , ( 122 )

where 0 and 1 stand for ⊥ and ⊤. (See, for instance, 7.1.4–(8).) “If we don’t take option 2, go to instruction 8; but if we do take it, continue with instruction 11.”

A few modifications to Algorithm X will transform it from a solution-visiting method into a constructor of ZDDs. In fact, color controls can be handled too:

Algorithm Z ( Dancing with ZDDs ). Given an XCC problem as in Algorithm C , this algorithm outputs a free ZDD for the sets of options that satisfy it. The ZDD instructions { I 2 , ..., I s } have the form ( o ¯ j ? l j : h j ) illustrated in ( 122 ), and I s is the root. (But if the problem has no solutions, the algorithm terminates with s = 1, and the root is 0.) The data structures of Algorithm C are extended by a “ memo cache” consisting of signatures S[ j ] and ZDD pointers Z[ j ] . Algorithm C ’s table of choices x 0 x 1 ... is joined by two new auxiliary tables m 0 m 1 ... and z 0 z 1 ... , indexed by the current level l .

Z1. [Initialize.] Set the problem up in memory, as in step C1 of Algorithm C . Also set N to the number of items, Z to the last spacer address, l ← 0, S [0] ← 0, Z [0] ← 1, m ← 1, s ← 1.

Z2. [Enter level l .] Form a “signature” σ that characterizes the current subproblem (see below). If σ = S [ t ] for some t (this is a “ cache hit”), set ζ ← Z [ t ] and go to Z8. Otherwise set S [ m ] ← σ, m l ← m , z l ← 0, and m ← m + 1.

Z3. [Choose i .] At this point items i 1 , ..., i t still need to be covered, as in step C3 of Algorithm C . Choose one of them, and call it i .

Z4. [ Cover i .] Cover item i using ( 12 ), and set x l ← DLINK ( i ).

Z5. [Try x l .] If x l \= i , go to Z7. Otherwise set p ← x l + 1, and do the following while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set p ← ULINK ( p ); otherwise commit( p, j ) and set p ← p + 1. Set l ← l + 1 and return to Z2.

Z6. [Try again.] Set p ← x l − 1, and do the following while p ≠ x l : Set j ← TOP ( p ); if j ≤ 0, set o ← 1 − j and p ← DLINK ( p ); otherwise uncommit( p, j ) and set p ← p − 1. If ζ ≠ 0, set s ← s + 1, output I s \= ( o ¯ ? z l : ζ ) , and set z l ← s . Set i ← TOP ( x l ), x l ← DLINK ( x l ), and return to Z5.

Z7. [Backtrack.] Uncover item i using ( 14 ). Then set Z [ m l ] ← z l and ζ ← z l .

Z8. [Leave level l .] Terminate if l = 0. Otherwise set l ← l − 1 and go to Z6.

Important: The ‘commit’ and ‘uncommit’ operations in steps Z5 and Z6 should modify ( 54 )–( 57 ), by calling cover( j ), hide( q ), uncover( j ), and unhide( q ) instead of cover′( j ), hide′( q ), uncover′( j ), and unhide′( q ). These changes cause every step of Algorithm Z to be slightly different from the corresponding step of Algorithm C . (Yet only step Z2 has changed substantially.)

Exercise 253 shows that a few more changes will make Algorithm Z compute the total number of solutions, instead of (or in addition to) outputting a ZDD.

The keys to Algorithm Z ’s success are the signatures computed in step Z2. This computation is easy if there are no secondary items: The signature σ is then simply a bit vector of length N , containing 1 in every position i where item i is still active. The computation is, however, somewhat subtler in the presence of secondary items; exercise 254 has the details.

It’s instructive to analyze some special cases. For example, suppose Algorithm Z is asked to find the perfect matchings of the complete graph K N . This problem has N primary items {1, ..., N }, and ( N 2 ) options ‘ j k ’ for 1 ≤ j < k ≤ N . We noted earlier, in the discussion preceding ( 87 ), that every item list on level l has exactly N − 1 − 2 l options, regardless of the choices made in step Z3. If we always choose the smallest uncovered item, step Z2 will compute exactly ( N − l l ) different signatures on level l , namely the signatures in which items {1, ..., l } are covered and so are l of the other items { l + 1, ..., N }. Hence the total number of cache entries is Σ l \= 0 N ( N − l l ) \= F N + 1 , a Fibonacci number(!). (See exercise 1.2.8–16.) Moreover, the main loops in steps Z5 and Z6 are executed ( N − 1 − 2 l ) ( N − l l ) times at level l , since steps Z3 and Z4 are executed ( N − l l ) times.

In fact, when N is even, the ZDD that is output for all perfect matchings of K N turns out to have exactly Σ l \= 0 N ( N − 1 − 2 l ) ( N − l l ) + 2 nodes, which is approximately N 5 F N + 1 . Exercise 255 shows that the total running time to compute this ZDD is Θ( N 2 F N ) = Θ( N 2 ϕ N ); and the same estimate holds also when N is odd and the ZDD has only one node ‘⊥’. This is much smaller than the time needed by Algorithm X , which is Θ (( N/e ) N /2 ).

More concretely, Algorithm X computes the 2,027,025 perfect matchings of K 16 in about 360 megamems, using about 6 kilobytes of memory. Algorithm Z needs only about 2 megamems to characterize those matchings with a 10,228-node ZDD; but it uses 2.5 megabytes of memory. For K 32 there are 191,898, 783,962,510,625 perfect matchings, and the difference is even more dramatic: Algorithm X costs about 34 thousand petamems and 25 kilobytes; Algorithm Z costs about 16 gigamems and 85 megabytes, for a ZDD with 48 meganodes.

This example illustrates several important points: ( 1 ) Algorithm Z can greatly reduce the running time of Algorithm X (or Algorithm C ), trading time for space. ( 2 ) These improvements can also be achieved for problems that have no solutions, like matchings of K 2 q +1 . ( 3 ) The number of nodes in the ZDD that is output might greatly exceed the number of memos in Algorithm Z ’s cache.

Let’s take a closer look at the ZDDs. The output for N = 8 is, schematically,

images

where, for example, ‘ 00101101 ’ represents nodes for the signature 00101101.

A signature represents a subproblem. If that subproblem has at least one solution, the ZDD for the full problem will have a subZDD that specifies all solutions of the subproblem. And if the signature is in cache location S [ t ], the root of the corresponding subZDD will be stored in Z [ t ], at the end of step Z7.

This subZDD has a very special structure, illustrated in ( 123 ). Suppose we branch on item i when working on signature σ; and suppose solutions are found for options o 1 , o 2 , ..., o k in the list for item i . Then there will be subZDDs rooted at ζ 1 , ζ 2 , ..., ζ k , associated with the subproblems whose signatures are σ \ o 1 , σ \ o 2 , ..., σ \ o k . The net effect of steps Z3–Z6 is to construct a subZDD for σ that essentially begins with k conditional instructions:

o k ? ζ k : ... o 2 ? ζ 2 : o 1 ? ζ 1 : ⊥ . ( 124 )

(The ZDD is constructed from bottom to top, so it tests o k first.)

For example, ‘ 00101101 ’ in ( 123 ) is the root of the subZDD for the subproblem that needs to cover {3, 5, 6, 8}. We branch on item 3, whose list has the three options ‘3 5’, ‘3 6’, ‘3 8’. Three branch instructions are output,

I θ \= ( 3   5 ¯ ? 0 : γ   ) , I η \= ( 3   6 ¯ ? θ : β   ) , I ζ \= ( 3   8 ¯ ? η : α   ) ;

here γ , β , α are the subZDDs for signatures 00000101, 00001001, 00001100, respectively. The subZDD for 00101101 begins at ζ .

Thus ( 123 ) illustrates a ZDD with 1 · 7 + 7 · 5 + 15 · 3 + 10 · 1 + 2 = 99 nodes. Notice that the dotted links always go either to ⊥ or to an “invisible” node, which is one of the k − 1 subsidiary nodes in a chain of branches such as ( 124 ). Every invisible node has a single parent. But there is one visible node for each successful signature, and a visible node may have many parents.

Exercises 256 – 262 discuss a number of examples where Algorithm Z gives spectacular improvements over Algorithm X (and over Algorithm C when colors are involved). Many additional examples could also be given. But most of the exact cover problems we’ve been considering in our examples do not have an abundance of common subproblems, so they reap little benefit from the memo cache.

For example, consider our old standby, the problem of Langford pairs. Algorithm X needs 15 G μ to show that there are no solutions when n = 14; Algorithm Z reduces this slightly, to 11 G μ . Algorithm X needs 1153 G μ to list the 326,721,800 solutions for n = 16; Algorithm Z computes that number in 450 G μ ; but it needs 20 gigabytes of memory, and produces a ZDD of 500 million nodes!

Similarly, Algorithm Z is not the method of choice for the n queens problem, or for word-packing problems, although it does yield modest speedups more often than one might suspect. Exercise 263 surveys some typical examples.

Summary

We began this section by observing that simple properties of linked lists can enhance the efficiency of backtracking, especially when applied to exact cover problems (XC). Then we noticed that a wide variety of combinatorial tasks, going well beyond matching, turn out to be special cases of exact covering.

The most important “takeaway,” however, has been the fact that color codes lead to a significant generalization of the classical exact cover problem. Indeed, the general XCC problem, “exact covering with colors,” has a truly extraordinary number of applications. The exercises below exhibit dozens and dozens of instructive problems that are quite naturally describable in terms of “ options,” which involve “ items” that may or may not be colored in certain ways. We’ve discussed Algorithm X (for XC problems) and Algorithm C (for XCC problems); and the good news is that those algorithms are almost identical.

Furthermore, we’ve seen how to extend Algorithm C in several directions: Algorithm M handles the general MCC problem, which allows items to be covered with different ranges of multiplicities. Algorithm C $ associates a cost with each option, and finds XCC solutions of minimum total cost. Algorithm Z produces XCC solutions as ZDDs, which can be manipulated and optimized in other ways.

Historical notes

The basic idea of ( 2 ) was introduced by H. Hitotumatu and K. Noshita [ Information Processing Letters 8 (1979), 174–175], who applied it to the n queens problem. Algorithm 7.2.1.2X, which was published by J. S. Rohl in 1983, can be regarded as a simplified version of dancing links, for cases when singly linked lists suffice. (Indeed, as Rohl observed, the n queens problem is such a case.) Its extension to exact cover problems in general, as in Algorithm X above, was the subject of the author’s tribute to C. A. R. Hoare in Millennial Perspectives in Computer Science (2000), 187–214, where numerous examples were given. [That paper was subsequently reprinted with additions and corrections as Chapter 38 of FGbook .] His original implementation, called DLX, used a more complex data structure than ( 10 ), involving nodes with four-way links.

Knuth extended Algorithm X to Algorithm C in November 2000, while thinking about two-dimensional de Bruijn sequences. A special case of Algorithm M , in which all multiplicities are fixed, followed in August 2004, when he was thinking about packing various sizes of bricks into boxes. The current form of Algorithm M was developed in January 2017, after he’d studied an independent generalization of Algorithm X that Wei-Hwa Huang had written in 2007.

The first computer programs for exact cover problems were developed independently by J. F. Pierce [ Management Science 15 (1968), 191–209] and by R. S. Garfinkel and G. L. Nemhauser [ Operations Research 17 (1969), 848–856]. In both cases the given options each had an associated cost, and the goal was to obtain minimum-cost solutions instead of arbitrary solutions. Both algorithms were similar, although they used different ways to prune nonoptimum choices: Items were chosen for branching according to a fixed, precomputed order, and options were represented as bit vectors. An option was never removed from its item list; it would repeatedly be rejected if its bits intersected with previously chosen items. ( Caution: Literature from the operations research community traditionally reverses the roles of rows and columns in matrices like ( 5 ). For them, items are rows and options are columns, even though bit vectors look like rows.)

The concept of “ dancing with ZDDs” was introduced by M. Nishino, N. Yasuda, S. Minato, and M. Nagata, in the AAAI Conference on Artificial Intelligence 31 (2017), 868–874, where they presented the special case of Algorithm Z in which all items are primary.

The history of XCC solving is clearly still in its infancy, and much more work needs to be done. For example, many applications will benefit from improved ways to choose an item for branching — especially in step M3 of Algorithm M , where only a few strategies have been explored so far. It’s important to maintain a good “ focus”; furthermore, techniques of “ factoring” can dramatically prune away unproductive branches, as shown for example in exercise 343 .

Algorithm M deserves to be extended to Algorithm M $ , and perhaps also to produce ZDD output. A further generalization would be to allow each item of each option to have an associated weight. (Thus the associated matrix, analogous to ( 5 ), would not consist merely of 0s and 1s.)

Hence we can expect to see many continued advances in XCC solving.

Exercises—First Set

▸ 1. [ M25 ] A doubly linked list of n elements, with a list head at 0, begins with LLINK ( k ) = k − 1 and RLINK ( k − 1) = k for 1 ≤ k ≤ n ; furthermore LLINK (0) = n and RLINK ( n ) = 0, as in ( 3 ). But after we use operation ( 1 ) to delete elements a 1 , a 2 , ..., a n , where a 1 a 2 ... a n is a permutation of {1, 2, ..., n }, the list will be empty and the links will be entangled as in ( 4 ).

  1. Show that the final settings of LLINK and RLINK can be described in terms of the binary search tree that is obtained when the keys a n , ..., a 2 , a 1 (in reverse order) are inserted by Algorithm 6.2.2T into an initially empty tree.

  2. Say that permutations a 1 a 2 ... a n and b 1 b 2 ... b n are equivalent if they both yield the same LLINK and RLINK values after deletion. How many distinct equivalence classes arise, for a given value of n ?

  3. How many of those equivalence classes contain just one permutation?

2. [ M30 ] Continuing exercise 1 , we know that the original list will be restored if we use ( 2 ) to undelete the elements a n , ..., a 2 , a 1 , reversing the order of deletion.

  1. Prove that it’s restored also if we use the unreversed order a 1 , a 2 , ..., a n (!).

  2. Is the original list restored if we undelete the elements in any order whatsoever?

  3. What if we delete only k of the elements, say a 1 , ..., a k , then undelete them in exactly the same order a 1 , ..., a k . Is the list always restored?

3. [ 20 ] An m × 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 4 = x 3 + x 5 = x 1 + x 3 = x 2 + x 4 + x 6 = x 1 + x 6 = x 3 + x 4 = x 2 + x 5 + x 6 = 1,

where each x k \= [choose row k ] is either 0 or 1.

  1. What is the general solution to those seven equations?

  2. Why is this approach to exact cover problems almost never useful in practice?

4. [ M20 ] Given a graph G , construct a matrix with one row for each vertex v and one column for each edge e , putting the value [ e touches υ ] into column e of row υ . What do the exact covers of this “ incidence matrix” represent?

5. [ 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 then to partition the universe into such subsets. In geometric contexts, an exact cover is often called a tiling .

Equivalently, we can use the terminology of hypergraphs, speaking of hyperedges (rows) that consist of vertices (columns); then the exact cover problem is to find a perfect matching, also called a perfect packing, namely a set of nonoverlapping hyperedges that hit every vertex.

Such problems generally have duals , which arise when we transpose the rows and columns of the input matrix. What is the dual of the exact cover problem, in hypergraph terminology?

6. [ 15 ] If an exact cover problem has N items 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 ?

7. [ 16 ] Why is TOP (23) = −4 in Table 1 ? Why is DLINK (23) = 25?

8. [ 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 option, one option per line.

9. [ 18 ] Explain how to branch in step X3 on an item i for which LEN ( i ) is minimum. If several items have that minimum length, i itself should also be minimum. (This choice is often called the “minimum remaining values” ( MRV) heuristic.)

10. [ 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 ) ≤ 1 or no other choices exist. (This tactic is called the “ sharp preference” heuristic.)

▸ 11. [ 19 ] 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?

▸ 12. [ 21 ] 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 2 of 3 in the list for item d .)

13. [ 16 ] When Algorithm X finds a solution in step X2, how can we use the values of x 0 x 1 ... x l −1 to figure out what that solution is?

▸ 14. [ 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 couples adjacent?”

  1. Suppose the women have already been seated, and let the vacant seats be ( S 0 , S 1 , ..., S n −1 ). Let M j be the spouse of the woman between seats S j and S ( j +1) mod n . Formulate the ménage problem as an exact cover problem with items S j and M j .

  2. Apply Algorithm X to find the solutions for n ≤ 10. Approximately how many mems are needed per solution, with and without the MRV heuristic?

15. [ 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’ll get only half as many solutions; the others will be the reversals of the solutions found.

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?

17. [ 16 ] Repeat exercise 16 , but consider a j and b j to be secondary items and omit the slack options ( 24 ). Consider the primary items in order r 3 , c 3 , r 2 , c 2 , r 4 , c 4 , r 1 , c 1 .

18. [ 10 ] What are the solutions to ( 6 ) if items e , f , and g are secondary ?

▸ 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 c f ’.)

▸ 20. [ 25 ] Generalize ( 26 ) to a pairwise ordering of options ( α 0 , ..., α m −1 ; β 0 , ..., β m −1 ) that uses at most ⌈lg m ⌉ of the secondary items y 1 , ..., y m −1 in each option. (Option α j should be compatible with option β k if and only if 0 ≤ j ≤ k < m .) Hint : Think of binary notation, and use y j at most 2 ρj times within each of the α ’s and β ’s.

21. [ 22 ] Extend exercise 20 to k -wise ordering of km options α j i , for 1 ≤ i ≤ k and 0 ≤ j < m . The solutions should be ( α j 1 i , ... , α j k k ) with 0 ≤ j 1 ≤ … ≤ j k < m . Again there should be at most ⌈lg m ⌉ secondary items in each option.

▸ 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.

images

  1. No queen is in either diagonal, and n is odd.

  2. Only one of the two diagonals contains a queen.

  3. There are two queens in the two diagonals.

23. [ 28 ] Use pairwise encoding to reduce the number of solutions by nearly a factor of 8 in the remaining cases not covered by exercise 22 :

  1. No queen is in either diagonal, and n is even.

  2. A queen is in the center of the board, and n is odd.

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°.

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.

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?

27. [ 22 ] Introduce secondary items cleverly into the options ( 16 ), so that only planar solutions to Langford’s problem are obtained. (See exercise 7 – 8 .)

28. [ M22 ] For what integers c 0 , t 0 , c 1 , t 1 , ..., c l , t l with 1 ≤ c j ≤ t j does the text’s formula ( 27 ) for estimated completion ratio give the value (a) 1/2? (b) 1/3?

▸ 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. (A unique item should have the minimum LEN value whenever step X3 is encountered.) Illustrate your construction when images

30. [ 25 ] 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 ?

31. [ M21 ] The running time of Algorithm X depends on the order of primary items in the active list, as well as on the order of options in the individual item lists. Explain how to randomize the algorithm so that (a) every item list is in random order after step X1; (b) step X3 chooses randomly among items with the minimum LEN .

32. [ M21 ] The solution to an exact cover problem with M options can be regarded as a binary vector x = x 1 ... x M , with x k \= [choose option k ]. The distance between two solutions x and x ′ can then be defined as the Hamming distance d ( x, x ′) = ν ( x ⨁ x ′), the number of places where x and x ′ differ. The diversity of the problem is the minimum distance between two of its solutions. (If there’s at most one solution, the diversity is ∞.)

  1. Is it possible to have diversity 1?

  2. Is it possible to have diversity 2?

  3. Is it possible to have diversity 3?

  4. Prove that the distance between solutions of a uniform exact cover problem — that is, a problem having the same number of items in each option — is always even.

  5. Most of the exact cover problems that arise in applications are at least quasi-uniform , in the sense that they have a nonempty subset of primary items such that the problem is uniform when restricted to only those items. (For example, every polyomino or polycube packing problem is quasi-uniform, because every option specifies exactly one piece name.) Can such problems have odd distances?

33. [ M16 ] Given an exact cover problem, specified by a 0–1 matrix A , efficiently construct an exact cover problem A ′ that has exactly one more solution than A does. [Consequently it is NP-hard to determine whether an exact cover problem with at least one solution has more than one solution.] Assume that A contains no all-zero rows.

34. [ 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.

35. [ M21 ] Continuing exercise 34 , construct A ′ having exactly three 1s per column.

▸ 36. [ 25 ] Let i k \= TOP ( x k ) be the item on which branching occurs at level k in Algorithm X . Modify that algorithm so that it finds the solution for which i 0 x 0 i 1 x 1 i 2 x 2 ... is smallest in lexicographic order. (It’s easy to do this by simply setting i ← RLINK (0) in step X3. But there’s a much faster way, by using the MRV heuristic most of the time.)

What is the lexicographically first solution to the 32 queens problem?

37. [ M46 ] (N. J. A. Sloane, 2016.) Let ⟨ q n ⟩ be the lexicographically smallest solution to the ∞ queens problem. (This sequence begins 1, 3, 5, 2, 4, 9, 11, 13, 15, 6, 8, 19, 7, 22, 10, 25, 27, 29, 31, 12, 14, 35, 37, 39, 41, 16, 18, 45, ..., and it clearly has strange regularities and irregularities.)

  1. Prove that every positive integer occurs in the sequence.

  2. Prove that q n is either nϕ + O (1) or n/ϕ + O (1).

▸ 38. [ M25 ] Devise an efficient way to compute the sequence ⟨ q n ⟩ of exercise 37 .

▸ 39. [ M21 ] Experiment with exact cover problems that are defined by m random options on n items. (Each option is generated independently, with repetitions permitted.)

  1. Use a fixed probability p that item i is included in any given option.

  2. Let every option be a random sample of r distinct items.

▸ 40. [ 21 ] If we merely want to count the number of solutions to an exact cover problem, without actually constructing them, a completely different approach based on bitwise manipulation instead of list processing is sometimes useful.

The following naïve algorithm illustrates the idea: We’re given an m × n matrix of 0s and 1s, represented as n -bit vectors r 1 , ..., r m . The algorithm works with a (potentially huge) database of pairs ( s j , c j ), where s j is an n -bit number representing a set of items, and c j is a positive integer representing the number of ways to cover that set exactly. Let p be the n -bit mask that represents the primary items.

N1. [Initialize.] Set N ← 1, s 1 ← 0, c 1 ← 1, k ← 1.

N2. [Done?] If k > m , terminate; the answer is Σ j \= 1 N c j [ s j & p \= p ] .

N3. [Append r k where possible.] Set t ← r k . For N ≥ j ≥ 1, if s j & t = 0, insert ( s j + t, c j ) into the database (see below).

N4. [Loop on k .] Set k ← k + 1 and return to N2.

To insert ( s, c ) there are two cases: If s = s i for some ( s i , c i ) already present, we simply set c i ← c i + c . Otherwise we set N ← N + 1, s N ← s , c N ← c .

Show that this algorithm can be significantly improved by using the following trick: Set u k ← r k & f ¯ k , where f k \= r k +1 | ··· | r m is the bitwise OR of all future rows. If u k ≠ 0, we can remove any entry from the database for which s j does not contain u k & p . We can also exploit the nonprimary items of u k to compress the database further.

41. [ 25 ] Implement the improved algorithm of the previous exercise, and compare its running time to that of Algorithm X when applied to the n queens problem.

42. [ M21 ] Explain how the method of exercise 40 could be extended to give representations of all solutions, instead of simply counting them.

43. [ M20 ] Give formulas for the entries a ij , b ij , c ij of the sudoku squares in ( 28 ).

44. [ M04 ] Could the clues of a sudoku puzzle be the first 33 digits of π ? (See (29a).)

45. [ 14 ] 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 ij at each step.)

46. [ 19 ] List all of the hidden single sudoku moves that are present in chart ( 31 ).

47. [ 19 ] What hidden singles are present in ( 32 ), after ‘ 3 ’ is placed in cell (2, 3)?

▸ 48. [ 24 ] Chart ( 33 ) essentially plots rows versus columns. Show that the same data could be plotted as either (a) rows versus values; or (b) values versus columns.

▸ 49. [ 24 ] Any solution to an exact cover problem will also solve the “relaxed” subproblems that are obtained by removing some of the items. For example, we might relax a sudoku problem ( 30 ) by removing all items c jk and b xk , as well as r ik with i ≠ i 0 . Then we’re left with a subproblem in which every option contains just two items, ‘ p i 0 j r i 0 k ’, for certain pairs ( j, k ). In other words, we’re left with a 2D matching problem.

Consider the bipartite graph with u j −−− υ k whenever a sudoku option contains ‘ p i 0 j r i 0 k ’. For example, the graph for i 0 = 4 in ( 33 ) is illustrated below. A perfect matching of this graph must take u 3 and u 8 to either υ 7 or υ 1 , hence the edges from other u ’s to those υ ’s can be deleted; that’s called a “naked pair” in row i 0 . Dually, υ 5 and υ 8 must be matched to either u 2 or u 7 , hence the edges from other υ ’s to those u ’s can be deleted; that’s called a “hidden pair” in row i 0 .

images

In general, q of the u ’s form a naked q-tuple if their neighbors include only q of the υ ’s; and q of the υ ’s form a hidden q-tuple if their neighbors include only q of the u ’s.

  1. These definitions have been given for rows. Show that naked and hidden q -tuples can be defined analogously for (i) columns, (ii) boxes.

  2. Prove that if the bipartite graph has r vertices in each part, it has a hidden q -tuple if and only if it has a naked ( r − q )-tuple.

  3. Find all the naked and hidden q -tuples of ( 33 ). What options do they rule out?

  4. Consider deleting items p ij and b xk , as well as all r ik and c jk for k ≠ k 0 . Does this lead to further reductions of ( 33 )?

50. [ 20 ] How many uniquely solvable 17-clue puzzles contain the 16 clues of (29c)?

51. [ 22 ] In how many ways can (29c) be completed so that every row, every column, and every box contains a permutation of the multiset {1, 2, 3, 4, 5, 6, 7, 7, 9}?

52. [ 40 ] Try to find a sudoku puzzle that’s as difficult as possible for Algorithm X .

53. [ M26 ] Beginners to sudoku might want to cut their teeth on a miniature variant called shidoku , which features 4 × 4 squares divided into four 2 × 2 boxes.

  1. Prove that every uniquely solvable shidoku problem has at least four clues.

  2. Two shidoku problems are equivalent if we can get from one to the other by permuting rows and columns in such a way that boxes are preserved, and/or by 90° rotation, and/or by permuting the numbers. Show that exactly 13 essentially different 4-clue shidoku problems have a unique solution.

▸ 54. [ 35 ] ( Minimal clues. ) Puzzle (29a) contains more clues than necessary to make the sudoku solution unique. (For example, the final ‘ 95 ’ could be omitted.) Find all subsets X of those 32 clues for which (i) the solution is unique, given X ; yet also (ii) for every x ∈ X , the solution is not unique, given X \ x .

55. [ 34 ] (G. McGuire.) Prove that at least 18 clues are necessary, in any sudoku puzzle whose unique answer is (28a). Also find 18 clues that suffice. Hint: At least two of the nine appearances of { 1, 4, 7 } in the top three rows must be among the clues.

Similarly, find a smallest-possible set of clues whose unique answer is (28b).

56. [ 47 ] What is the largest number of clues in a minimal sudoku puzzle?

57. [ 22 ] Every sudoku solution has at most 27 horizontal trios and 27 vertical trios, namely the 3-digit sets that appear within a single row or column of a box. For example, (28a) has nine horizontal trios { 1, 2, 3 }, { 2, 3, 4 }, ..., { 9, 1, 2 } and three vertical trios { 1, 4, 7 }, { 2, 5, 8 }, { 3, 6, 9 }; (28b) has just three of each. The solution to (29a) has 26 horizontal trios and 23 vertical trios; { 3, 6, 8 } occurs once horizontally, twice vertically.

Let T be the 27 trios {{ A, B, C } | A ∈ { 1, 2, 3 }, B ∈ { 4, 5, 6 }, C ∈ { 7, 8, 9 }}. Find all sudoku solutions whose horizontal trios and vertical trios are both equal to T .

▸ 58. [ 22 ] (A. Thoen and A. van de Wetering, 2019.) Find all sudoku solutions for which the 1s, 2s, ..., 7s also solve the nine queens problem.

59. [ 20 ] Solve the jigsaw sudokus in ( 34 ). How large is Algorithm X ’s search tree?

60. [ 20 ] ( The Puzzlium Sudoku ABC .) Complete these hexomino-shaped boxes:

images

61. [ 21 ] Turn Behrens’s 5 × 5 gerechte design (35a) into a jigsaw sudoku puzzle, by erasing all but five of its 25 entries.

▸ 62. [ 34 ] For n ≤ 7, generate all of the ways in which an n × n square can be packed with n nonstraight n -ominoes. (These are the possible arrangements of boxes in a square jigsaw sudoku.) How many of them are symmetric? Hint: See exercise 7.2.2–76 .

63. [ 29 ] In how many different ways can Behrens’s 9 × 9 array (35c) be regarded as a gerechte latin square? (In other words, how many decompositions of that square into nine boxes of size 9 have a complete “ rainbow” {1, 2, 3, 4, 5, 6, 7, 8, 9} in each box? None of the boxes should simply be an entire row or an entire column.)

64. [ 23 ] ( Clueless jigsaw sudoku. ) A jigsaw sudoku puzzle can be called “clueless” if its solution is uniquely determined by the entries in a single row or column, because such clues merely assign names to the n individual symbols that appear. For example, the first such puzzle to be published, discovered in 2000 by Oriel Maxime, is shown here.

images

  1. Find all clueless sudoku jigsaw puzzles of order n ≤ 6.

  2. Prove that such puzzles exist of all orders n ≥ 4.

65. [ 24 ] Find the unique solutions to the following examples of jigsaw sudoku:

images

▸ 66. [ 30 ] Arrange the following sets of nine cards in a 3 × 3 array so that they define a sudoku problem with a unique solution. (Don’t rotate them.)

images

▸ 67. [ 22 ] Hypersudoku extends normal sudoku by adding four more (shaded) boxes in which a complete “ rainbow” {1, 2, 3, 4, 5, 6, 7, 8, 9} is required to appear:

images

(Such puzzles, introduced by P. Ritmeester in 2005, are featured by many newspapers.)

  1. Show that a hypersudoku solution actually has 18 rainbow boxes, not only 13.

  2. Use that observation to solve hypersudoku puzzles efficiently by extending ( 30 ).

  3. How much does that observation help when solving (i) and (ii)?

  4. True or false: A hypersudoku solution remains a hypersudoku solution if the four 4 × 4 blocks that touch its four corners are simultaneously rotated 180°, while also flipping the middle half-rows and middle half-columns (keeping the center fixed).

68. [ 28 ] A polyomino is called convex if it contains all of the cells between any two of its cells that lie in the same row or the same column. (This happens if and only if it has the same perimeter as its minimum bounding box does, because each row and column contribute 2.) For example, all of the pentominoes (3 6 ) are convex, except for ‘U’.

  1. Generate all ways to pack n convex n -ominoes into an n × n box, for n ≤ 7.

  2. In how many ways can nine convex nonominoes be packed into a 9 × 9 box, when each of them is small enough to fit into a 4 × 4? (Consider also the symmetries.)

▸ 69. [ 30 ] Diagram (i) below shows the 81 communities of Bitland, and their nine electoral districts. The voters in each community are either Big-Endian (B) or Little-Endian (L). Each district has a representative in Bitland’s parliament, based on a majority vote.

Notice that there are five Ls and four Bs in every district, hence the parliament is 100% Little-Endian. Everybody agrees that this is unfair. So you have been hired as a computer consultant, to engineer the redistricting.

A rich bigwig secretly offers to pay you a truckload of money if you get the best possible deal for his side. You could gerrymander the districts as in diagram (ii), thereby obtaining seven Big-Endian seats. But that would be too blatantly biased.

images

Show that seven wins for B are actually obtainable with nine districts that do respect the local neighborhoods of Bitland quite decently, because each of them is a convex nonomino that fits in a 4 × 4 square (see exercise 68 ).

70. [ 21 ] Dominosa is a solitaire game in which you “shuffle” the 28 pieces images , images of double-six dominoes and place them at random into a 7 × 8 frame. Then you write down the number of spots in each cell, put the dominoes away, and try to reconstruct their positions based only on that 7 × 8 array of numbers. For example,

images

  1. Show that another placement of dominoes also yields the same matrix of numbers.

  2. What domino placement yields the array

( 3 3 6 5 1 5 1 5 6 5 6 1 2 3 2 4 2 4 3 3 3 6 2 0 4 1 6 1 4 4 6 0 3 0 3 0 1 1 4 4 2 6 2 5 0 5 0 0 2 5 0 5 4 2 1 6 ) ?

▸ 71. [ 20 ] Show that Dominosa reconstruction is a special case of 3D MATCHING.

72. [ M22 ] Generate random instances of Dominosa, and estimate the probability of obtaining a 7 × 8 matrix with a unique solution. Use two models of randomness: (i) Each matrix whose elements are permutations of the multiset {8 × 0, 8 × 1, ..., 8 × 6} is equally likely; (ii) each matrix obtained from a random shuffle of the dominoes is equally likely.

73. [ 46 ] What’s the maximum number of solutions to an instance of Dominosa?

74. [ 22 ] (M. Keller, 1987.) Is there a uniquely solvable Dominosa array for which every domino matches two adjacent cells of the array in either three or four places?

▸ 75. [ M24 ] A grope is a set G together with a binary operation ∘, in which the identity x ∘ ( y ∘ x ) = y is satisfied for all x ∈ G and y ∈ G .

  1. Prove that the identity ( x ∘ y ) ∘ x = y also holds, in every grope.

  2. Which of the following “ multiplication tables” define a grope on {0, 1, 2, 3}?

    0123 1032 2301 3210 ; 0321 3210 2103 1032 ; 0132 1023 3210 2301 ; 0231 3102 1320 2031 ; 0312 2130 3021 1203 .

    (In the first example, x ∘ y = x ⊕ y ; in the second, x ∘ y = (− x − y ) mod 4. The last two have x ∘ y = x ⊕ f ( x ⊕ y ) for certain functions f .)

  3. For all n , construct a grope whose elements are {0, 1, ..., n − 1}.

  4. Consider the exact cover problem that has n 2 items xy for 0 ≤ x, y < n and the following n +( n 3 − n )/3 options:

    1. ‘ xx ’, for 0 ≤ x < n ;

    2. ‘ xx xy yx ’, for 0 ≤ x < y < n ;

    3. ‘ xy yz zx ’, for 0 ≤ x < y,z < n .

    Show that its solutions are in one-to-one correspondence with the multiplication tables of gropes on the elements {0, 1, ..., n − 1}.

  5. Element x of a grope is idempotent if x ∘ x = x . If k elements are idempotent and n − k are not, prove that k ≡ n 2 (modulo 3).

76. [ 21 ] Modify the exact cover problem of exercise 75(d) in order to find the multiplication tables of (a) all idempotent gropes — gropes such that x ∘ x = x for all x ; (b) all commutative gropes — gropes such that x ∘ y = y ∘ x for all x and y ; (c) all gropes with the identity element 0 — gropes such that x ∘ 0 = 0 ∘ x = x for all x .

77. [ M21 ] Given graphs G and H , each with n vertices, use Algorithm X to decide whether or not G is isomorphic to a subgraph of H . (In such a case we say that G is embedded in H .)

78. [ 16 ] Show that it’s quite easy to pack the 27 mathematicians’ names of Fig. 71 into a 12 × 15 array, with all names reading correctly from left to right. (Of course that would be a terrible word search puzzle.)

79. [ M20 ] How many options are in ( 48 ), when they are completely listed?

80. [ 19 ] Play through Algorithm C by hand, using exercise 9 in step C3 and the input in Table 2 , until first reaching a solution. What are the contents of memory then?

81. [ 21 ] True or false: An exact cover problem that has no color assignments has exactly the same running time on Algorithms X and C .

82. [ 21 ] True or false: It’s possible to save memory references in Algorithms X and C by not updating the LEN fields in the hide/unhide operations when x > N 1 .

▸ 83. [ 20 ] Algorithm C can be extended in the following curious way: Let p be the primary item that is covered first, and suppose that there are k ways to cover it. Suppose further that the j th option for p ends with a secondary item s j , where { s 1 , ..., s k } are distinct. Modify the algorithm so that, whenever a solution contains the j th option for p , it leaves items { s 1 , ..., s j −1 } uncovered. (In other words, the modified algorithm will emulate the behavior of the unmodified algorithm on a much larger instance, in which the j th option for p contains all of s 1 , s 2 , ..., s j .)

▸ 84. [ 25 ] Number the options of an XCC problem from 1 to M . A minimax solution is one whose maximum option number is as small as possible. Explain how to modify Algorithm C so that it determines all of the minimax solutions (omitting any that are known to be worse than a solution already found).

85. [ 22 ] Sharpen the algorithm of exercise 84 so that it produces exactly one minimax solution — unless, of course, there are no solutions at all.

▸ 86. [ M25 ] Modify Algorithm C so that, instead of finding all solutions to a given XCC problem, it gives a Monte Carlo estimate of the number of solutions and the time needed to find them, using Theorem 7.2.2E. (Thus the modified algorithm is to Algorithm C as Algorithm 7.2.2E is to Algorithm 7.2.2B .)

87. [ 20 ] A double word square is an n × n array whose rows and columns contain 2 n different words. Encode this problem as an XCC problem. Can you save a factor of 2 by not generating the transpose of previous solutions? Does Algorithm C compete with the algorithm of exercise 7.2.2–28 (which was designed explicitly to handle such problems)?

88. [ 21 ] Instead of finding all of the double word squares, we usually are more interested in finding the best one, in the sense of using only words that are quite common. For example, it turns out that a double word square can be made from the words of WORDS (1720) but not from those of WORDS (1719). Show that it’s rather easy to find the smallest W such that WORDS ( W ) supports a double word square, via dancing links.

89. [ 24 ] What are the best double word squares of sizes 2 × 2, 3 × 3, ..., 7 × 7, in the sense of exercise 88 , with respect to The Official _SCRABBLE_ ® Players Dictionary? [Exercise 7.2.2–32 considered the analogous problem for symmetric word squares.]

▸ 90. [ 22 ] A word stair of period p is a cyclic arrangement of words, offset stepwise, that contains 2 p distinct words across and down. They exist in two varieties, left and right:

images

What are the best five-letter word stairs, in the sense of exercise 88 , for 1 ≤ p ≤ 10? Hint: You can save a factor of 2 p by assuming that the first word is the most common.

91. [ 40 ] For given W , find the largest p such that WORDS ( W ) supports a word stair of period p . (There are two questions for each W , examining stairs to the {left, right}.)

92. [ 24 ] Some p -word cycles define two-way word stairs that have 3 p distinct words:

images

What are the best five-letter examples of this variety, for 1 ≤ p ≤ 10?

93. [ 22 ] Another periodic arrangement of 3 p words, perhaps even nicer than that of exercise 92 and illustrated here for p = 3, lets us read them diagonally up or down, as well as across. What are the best five-letter examples of this variety, for 1 ≤ p ≤ 10? (Notice that there is 2 p -way symmetry.)

images

94. [ 20 ] (É. Lucas.) Find a binary cycle ( x 0 x 1 ... x 15 ) for which the 16 quadruples x k x ( k +1) mod 16 x ( k +3) mod 16 x ( k +4) mod 16 for 0 ≤ k < 16 are distinct.

▸ 95. [ 20 ] Given 0 ≤ p < q ≤ n , explain how to use color controls and Algorithm C to find all cycles ( x 0 x 1 ... x m −1 ) of 0s and 1s, where m \= Σ k \= p q ( n k ) , with the property that the m binary vectors { x 0 x 1 ... x n −1 , x 1 x 2 ... x n , ..., x m −1 x 0 ... x n −2 } are distinct and have weight between p and q . (In other words, all n -bit binary vectors y = y 1 ... y n with p ≤ νy ≤ q occur exactly once in the cycle. We studied the special case of de Bruijn cycles , for which p = 0 and q = n , in Section 7.2.1.1.)

For example, when n = 7, p = 0, and q = 3, the cycle

( 0000000100000110000101000101100010010101001001100100011010000111 )

exhibits all binary 7-tuples with a majority of 0s. When n = 7, p = 3, q = 4, the cycle

( 0000111000101100011010010101011010100110011011001011100100111010001111 )

shows all 7-tuples obtainable by removing the first bit of an 8-tuple with four 0s, four 1s.

Exactly how many cycles exist, when ( n, p, q ) = (7, 0, 3) or (7, 3, 4)? How long does it take for Algorithm C to find them?

96. [ M20 ] Find an 8 × 8 binary torus whose sixty-four 2 × 3 subrectangles are distinct.

97. [ M21 ] Find all 9 × 9 ternary ouro toruses D = ( d i,j ) that are symmetrical, in the sense that d ( i +3) mod 9, ( j +3) mod 9 \= ( d i,j + 1) mod 3. (See exercise 7.2.1.1–109.)

98. [ 25 ] Prove that the exact cover problem with color controls is NP-complete, even if every option consists of only two items.

99. [ 20 ] True or false: Every XCC problem can be reformulated as an ordinary exact cover problem with the same solutions and the same number of options.

▸ 100. [ 20 ] The general constraint satisfaction problem ( CSP) is the task of finding all n -tuples x 1 ... x n that satisfy a given system of constraints C 1 , ..., C m , where each constraint is defined by a relation on a nonempty subset of the variables { x 1 , ..., x n }.

For example, a unary constraint is a relation of the form x k ∈ D k ; a binary constraint is a relation of the form ( x j , x k ) ∈ D jk ; a ternary constraint is a relation of the form ( x i , x j , x k ) ∈ D ijk ; and so on.

  1. Find all x 1 x 2 x 3 x 4 x 5 for which 0 ≤ x 1 ≤ x 2 ≤ x 3 ≤ x 4 ≤ x 5 ≤ 2 and x 1 + x 3 + x 5 = 3.

  2. Formulate the problem of part (a) as an XCC problem.

  3. Explain how to formulate any CSP as an XCC problem.

▸ 101. [ 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 hosts a diplomat.

  • The Norwegian’s house is the leftmost.

  • The milk drinker lives in the middle house.

  • The painter comes from Japan.

  • The coffee-lover’s house is green.

  • The dog’s owner is from Spain.

  • The violinist drinks orange juice.

  • The white house is just left of the green one.

  • The Norwegian lives next to the blue house.

  • The horse lives next to the diplomat.

  • The Ukrainian drinks tea.

  • The sculptor breeds snails.

  • The nurse lives next to the fox.

Who trains the zebra, and who prefers to drink just plain water?”

▸ 102. [ 25 ] Explain how to find all solutions to a Japanese arrow puzzle with Algorithm C . (See exercise 7.2.2–68 .)

▸ 103. [ M28 ] Musical pitches in the Western system of “ equal temperament” are the notes whose frequency is 440 · 2 n /12 cycles per second, for some integer n . The pitch class of such a note is n mod 12, and seven of the twelve possible pitch classes are conventionally designated by letters:

0 \= A , 2 \= B , 3 \= C , 5 \= D , 7 \= E , 8 \= F , 10 \= G.

The other classes are named by appending sharp (♯) or flat (♭) signs, to go up or down by 1; thus 1 = A♯ = B♭, 4 = C♯ = D♭, ... , 11 = G♯ = A♭.

Arnold Schoen berg popularized a composition technique that he called a twelve-tone row, which is simply a permutation of the twelve pitch classes. For example, his student Alban Berg featured the motif

images

which is the twelve-tone row 8 7 3 0 10 5 11 4 6 9 1 2, in the first movement of his Lyric Suite (1926), and in another composition he had written in 1925.

In general we can say that an n -tone row x = x 0 x 1 ... x n −1 is a permutation of {0, 1, ..., n −1}. Two n -tone rows x and x ′ are considered to be equivalent if they differ only by a transposition — that is, if x k ′ \= ( x k + d ) mod n for some d and for 0 ≤ k < n . Thus, the number of inequivalent n -tone rows is exactly ( n − 1)!.

  1. Berg’s 12-tone row above has the additional property that the intervals between adjacent notes, ( x k − x k −1 )mod n , are {1, ..., n − 1}. Prove that an n -tone row can have this all-interval property only if n is even and x n −1 \= ( x 0 + n /2) mod n .

  2. Use Algorithm C to find n -tone rows with the all-interval property. How many inequivalent solutions arise, when 2 ≤ n ≤ 12?

  3. Any all-interval n -tone row leads easily to several others. For example, if x = x 0 x 1 ... x n −1 is a solution, so is its reversal x R \= x n −1 ... x 1 x 0 ; and so is cx = ( cx 0 mod n )( cx 1 mod n ) ... ( cx n −1 mod n ) whenever c ⊥ n . Prove that the cyclic shift x Q \= x k ... x n −1 x 0 ... x k −1 is also a solution, when x k − x k −1 \= ± n /2.

  4. True or false: In part (c) we always have x RQ \= x QR .

  5. The 12-tone row of Alban Berg shown above is symmetrical, because it is equivalent to x R . Other kinds of symmetry are also possible; for example, the row x = 0 1 3 7 2 5 11 10 8 4 9 6 is equivalent to − x Q . How many symmetrical all-interval n -tone rows exist, for n ≤ 12?

104. [ M28 ] Assume that n +1 = p is prime. Given an n -tone row x = x 0 x 1 ... x n −1 , define y k \= x ( k −1) mod p , and let x ( r ) \= y r y 2 r ... y nr be the n -tone row consisting of “every r th element of x ” (where x n is considered to be blank). For example, when n = 12, every 5th element of x is the sequence x (5) = x 4 x 9 x 1 x 6 x 11 x 3 x 8 x 0 x 5 x 10 x 2 x 7 .

An n -tone row is called perfect if it is equivalent to x ( r ) for 1 ≤ r ≤ n . For example, the amazing 12-tone row 0 1 4 2 9 5 11 3 8 10 7 6 is perfect.

  1. Prove that a perfect n -tone row has the all-interval property.

  2. Prove that a perfect n -tone row also satisfies x ≡ x R .

105. [ 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 × 6 square, leaving one cell untouched.

106. [ 22 ] Also pack two copies of ONE, TWO, THREE, FOUR, FIVE into a 5 × 5 square.

▸ 107. [ 25 ] Pack as many of the following words as possible into a 9 × 9 array, simultaneously satisfying the rules of both word search and sudoku:

ACRE COMPARE CORPORATE MACRO MOTET ROAM ART COMPUTER CROP META PARAMETER TAME

▸ 108. [ 32 ] The first 44 presidents of the U.S.A. had 38 distinct surnames: ADAMS, ARTHUR, BUCHANAN, BUSH, CARTER, CLEVELAND, CLINTON, COOLIDGE, EISENHOWER, FILLMORE, 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 .

  1. What’s the smallest square into which all of these names can be packed, using word search conventions, and requiring all words to be connected via overlaps?

  2. What’s the smallest rectangle , under the same conditions?

▸ 109. [ 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 rookwise connected. (iv) Words overlap only when one is vertical and the other is horizontal. For example, the eleven words ZERO , ONE , ..., TEN can be placed into an 8 × 8 square under constraints (i) and (ii) as shown; but (iii) is violated, because there are three different components.

images

Explain how to encode a wordcross puzzle as an XCC problem. Use your encoding to find a correct solution to the problem above. Do those eleven words fit into a smaller rectangle, under conditions (i), (ii), and (iii)?

110. [ 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 .)

111. [ 21 ] Find all 8 × 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. They should have no words of other lengths. (Crossword puzzle diagrams are black-and-white grids such as those in exercise 1.3.2–23.)

▸ 112. [ 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 × 3 array of letters. For example, each of the words THE, MATURE, ART, OF, COMPUTER , and PROGRAMMING can be found in the array shown here.

images

  1. Does that array contain other common words of eight or more letters?

  2. Create a 6 × 3 array that contains TORTO, WORDS, SOLVER , and many other interesting English words of five or more letters. (Let your imagination fly.)

  3. Is it possible to pack ONE, TWO, THREE , ..., EIGHT, NINE, TEN into a Torto array?

▸ 113. [ 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, LISTS, GIVEN, LINKS, QUEUE, GRAPH, TIMES, BLOCK, VALUE, TABLE, FIELD, EMPTY, ABOVE, POINT, THREE, UNTIL, HENCE, QUITE, DEQUE ? (Each of these words appears more than 50 times in Chapter 2.)

114. [ M25 ] Let α be a permutation of the cells of a 9 × 9 array that takes any sudoku solution into another sudoku solution. We say that α is an automorphism of the sudoku solution S = ( s ij ) if there’s a permutation π of {1, 2, ..., 9} such that s ( ij )α \= s ij π for 0 ≤ i, j < 9. For example, the permutation that takes ij into ( ij ) α = ji , commonly called transposition , is an automorphism of ( 28b ), with respect to the permutation π = ( 24 )( 37 )( 68 ); but it is not an automorphism of ( 28a ) or ( 28c ).

Show that Algorithm C can be used to find all sudoku solutions that have a given automorphism α , by defining an appropriate XCC problem.

How many sudoku solutions have transposition as an automorphism?

115. [ M25 ] Continuing exercise 114 , how many hypersudoku solutions have auto-morphisms of the following types? (a) transposition; (b) the transformation of exercise 67(d) ; (c) 90° rotation; (d) both (b) and (c).

▸ 116. [ M25 ] Given a graph G on vertices V , let μ ( G ) be obtained by (i) adding new vertices V ′ = { v ′ | v ∈ V }, with u ′ — v when u — v ; and also (ii) adding another vertex w , with w — v ′ for all v ′ ∈ V ′. (If G has m edges and n vertices, μ ( G ) has 3 m + n edges and 2 n + 1 vertices.) The Mycielski graphs M c are defined for all c ≥ 2 by setting M 2 = K 2 and M c +1 \= μ ( M c ); they have 7 18 3 c − 3 4 2 c + 1 2 edges and 3 4 2 c − 1 vertices:

images

  1. Prove that each M c is triangle-free (contains no subgraph K 3 ).

  2. Prove that the chromatic number χ ( M c ) = c .

  3. Prove that each M c is in fact “ χ -critical”: Removing any edge decreases χ .

▸ 117. [ 24 ] ( Graph coloring. ) Suppose we want to find all possible ways to label the vertices of graph G with d colors; adjacent vertices should have different colors.

  1. Formulate this as an exact cover problem, with one primary item for each vertex and with d secondary items for each edge.

  2. Sometimes G ’s edges are conveniently specified by giving a family { C 1 , ..., C r } of cliques, where each C j is a subset of vertices; then u — v if and only if u ∈ C j and v ∈ C j for some j . (For example, the 728 edges of the queen graph Q 8 can be specified by just 8+8+13+13 = 42 cliques — one clique for each row, column, and diagonal.) Modify the construction of (a) so that there are only rd secondary items.

  3. In how many ways can Q 8 be 9-colored? (Compare method (a) to method (b).)

  4. Each solution to the coloring problem that uses k different colors is obtained d k ¯ \= d ( d − 1 ) ... ( d − k + 1 ) times, because of the symmetry between colors. Modify (a) and (b) so that each essentially different solution is obtained just once, when the symmetry-breaking technique of exercise 122 is used.

  5. In how many ways can the Mycielski graph M c be c -colored, for 2 ≤ c ≤ 5?

  6. Use Algorithm C to verify that M c can’t be ( c − 1)-colored, for 2 ≤ c ≤ 5.

  7. Try ( c − 1)-coloring M c when a random edge is removed, for 2 ≤ c ≤ 5.

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.

119. [ 21 ] Show that all solutions to the problem of placing MacMahon’s 24 triangles ( 58 ) into a hexagon with all-white border can be rotated and reflected so that the all-white triangle has the position that it occupies in (59b). Hint: Factorize.

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]:

images

(The tetrad tiles must not be rotated or flipped.) Hint: Algorithm C will help.

▸ 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).

  1. Show that the tile called βUS in that exercise can’t be part of any infinite tiling. In fact, it can appear in only n + 1 cells of an m × n array, when m, n ≥ 4.

  2. Show that, for all k ≥ 1, there’s a unique (2 k − 1) × (2 k − 1) tiling for which the middle tile is δRD . (Consequently, by the infinity lemma, there’s a unique tiling of the entire plane in which δRD is placed at the origin.)

  3. Similarly, show that there are exactly (2, 2, 3, 57) tilings of size (2 k − 1) × (2 k − 1) whose middle tile is respectively ( δRU , δLD , δLU , δSU ), for all k ≥ 3.

  4. How many tilings of the infinite plane have ( δRU , δLD , δLU , δSU ) at the origin?

▸ 122. [ 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, ..., v + d − 1}, and that all other colors have values < v . Hint : Modify the algorithm so that the first such color it assigns is always v , then v + 1, etc.

123. [ M20 ] Apply the algorithm of exercise 122 to the following toy problem with parameters m and n : There are n primary items p k and n secondary items q k , for 1 ≤ k ≤ n ; and there are mn options, ‘ p k q k : j ’ for 1 ≤ j ≤ m and 1 ≤ k ≤ n . (The solutions to this problem are the mappings of {1, ..., n } into {1, ..., m }, which may also be regarded as the partitions of {1, ..., n } into parts labeled {1, ..., m }.) Algorithm C will obviously find m n \= { n 1 } m 1 ¯ + { n 1 } m 2 ¯ + ... + { n m } m m ¯ solutions. But the modified algorithm finds only the “ unlabeled” partitions, of which there are { n 1 } + { n 2 } + ... + { n m } .

▸ 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.

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 term of the coordinates of the originals, using the system of exercise 124 .

126. [ 23 ] 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 ?

127. [ M28 ] 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?

▸ 128. [ 25 ] Eleven of MacMahon’s triangles ( 58 ) involve only the first three colors (not black). Arrange them into a pleasant pattern that tiles the entire plane when replicated.

▸ 129. [ M34 ] 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).

images

Exactly how many essentially different symmetrical patterns are possible, in a hexagon?

130. [ 21 ] Partition MacMahon’s triangles ( 58 ) into three sets of eight, each of which can be placed on the faces of an octahedron, with matching edge colors.

images

131. [ 28 ] (P. A. MacMahon, 1921.) Instead of using the colored tiles of ( 58 ), which yield ( 59 ), we can form hexagons from 24 different triangles in two other ways:

images

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 should have a total of three spots where they meet.

  1. In how many ways can that jigsaw puzzle make a hexagon? (All pieces are white.)

  2. How many triomino arrangements have that pattern of dots at the edges?

132. [ 40 ] (W. E. Philpott, 1971.) There are 4624 = 68 2 tiles in a set that’s like ( 58 ), 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?

133. [ 21 ] (P. A. MacMahon, 1921.) A set of 24 square tiles can be constructed, analogous to the triangular tiles of ( 58 ), if we restrict ourselves to just three colors. For example, they can be arranged in a 4 × 6 rectangle as shown, with all-white border. In how many ways can this be done?

images

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?

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 × 2 × 2 cube, matching colors wherever adjacent.

▸ 136. [ HM28 ] (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:

images

Cover a dodecahedron with these tiles, matching edge numbers. (Reflections are OK.)

137. [ 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.

images

  1. Show that this puzzle has a unique solution, with that particular set of seven. (Reflections of the nuts are not OK!)

  2. Can those seven nuts form the same shape, but with the label numbers summing to 7 where they meet ({1, 6}, {2, 5}, or {3, 4})?

  3. Hex nuts can be decorated with {1, 2, 3, 4, 5, 6} in 5! = 120 different ways. If seven of them are chosen at random, what’s the approximate probability that they define a puzzle with a unique solution, under matching condition (a)?

  4. Find seven hex nuts that have a unique solution under both conditions (a) and (b).

138. [ 25 ] ( Heads and tails .) Here’s a set of 24 square tiles that MacMahon missed(!):

images

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

images

where the 4 × 6 arrangement will tile the plane; the 5 × 5 arrangement has a special “joker” tile in the middle, containing all four heads.

  1. How many 4 × 6 arrangements will tile the plane? (Consider symmetries.)

  2. Notice that the half-objects at the top, bottom, left, and right of the 5 × 5 arrangement match the heads in the middle. How many such arrangements are possible?

  3. Devise a 5 × 5 arrangement that will tile the plane, in conjunction with the 5 × 5 pattern shown above. Hint: Use an “anti-joker” tile, which contains all four tails.

139. [ M25 ] 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 × 3 arrangement in which heads properly match tails.

  1. How many of the ( 24 9 ) choices of 9 tiles lead to essentially different puzzles?

  2. How many of those puzzles have exactly k solutions, for k = 0, 1, 2, ... ?

140. [ 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 ):

images

Such arrangements are much rarer than those based on edge matching, because edges are common to only two tiles but vertices might involve up to six.

  1. In how many essentially distinct ways can those shapes be formed?

  2. The first parallelogram is a scaled-up version of the “straight hexiamond” images , with dimensions doubled. How many of the other eleven scaled-up hexiamond shapes can be assembled from Langford’s tiles? (See exercises 125 and 309 .)

  3. Each of the seven tetrahexes also yields an interesting shape that consists of 24 triangles. (See exercise 316 .) How do Langford’s tiles behave in those shapes?

141. [ 24 ] 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

images

  1. 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 × 5?

  2. Discuss tiling the plane with such solutions.

▸ 142. [ 23 ] ( Zdravko Zivkovic, 2008.) Edge and vertex matching can be combined into a single design if we replace MacMahon’s 24 squares by 24 octagons. For example,

images

illustrate 4 × 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 center is images , edge matching when it’s images .) How many 4 × 6 arrangements satisfy (i), (ii), and (iii), respectively?

▸ 143. [ 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 , 0 ≤ x ≤ a , 0 ≤ y ≤ b , and 0 ≤ z ≤ c . Two vertices are adjacent if their coordinates all differ by at most 1. The boundary edges always define a convex polygon. For example, simplex (7, 7, 5, 3, 0, 0, 0) is illustrated here.

images

  1. What simplex graphs correspond to the three shapes in exercise 140 ?

  2. The examples in (a) have 24 interior triangles, but simplex (7, 7, 5, 3, 0, 0, 0) has 29. Can any other convex polygons be made from 24 triangles, connected edgewise?

  3. Design an efficient algorithm that lists all possible convex polygons that can be formed from exactly N triangles, given N . Hint: Every convex polygon in a triangular grid can be characterized by the six numbers in its boundary path x 0 x 1 x 2 x 3 x 4 x 5 , which moves x k steps in direction (60 k )° for k = 0, 1, ..., 5. For example, the boundary path of simplex (7, 7, 5, 3, 0, 0, 0) is 503412.

  4. Can every convex polygon in a triangular grid be described by a simplex graph?

144. [ 24 ] 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:

images

Here there’s vertex matching in the bottom five tiles of (i), and in the upper left five and bottom five of (ii), with edge matching elsewhere. In how many ways can the big hexagon be made from these 24 little hexagons, under constraints (i) and (ii)?

▸ 145. [ M20 ] Many problems that involve an l × m × 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 ≤ x ≤ 2 l , 0 ≤ y ≤ 2 m , 0 ≤ z ≤ 2 n .

▸ 146. [ M30 ] There are 30 ways to paint the colors { a, b, c, d, e, f } on the faces of a cube:

images

(If 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, front, and back (as in “ Instant Insanity”), and with the further proviso that adjacent cubes have matching colors where they share a face:

images

  1. Explain why any such arrangement also has the same color at the left and right.

  2. Invent a way to name each cube, distinguishing it from the other 29.

  3. How many essentially different arrangements like (*) are possible?

  4. Can all 30 cubes be used to make five such arrangements simultaneously?

147. [ 30 ] The 30 cubes of exercise 146 can be used to make “ bricks” of various sizes l × m × n , by assembling l · m · 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 a 1 × 1 × 2 brick. Two such bricks can then join up to make a 1 × 2 × 2; the one illustrated here has a in front, b in the back, c at the left and right, d at the top, and e at the bottom.

images

  1. Assemble all 30 cubes into a magnificent brick of size 2 × 3 × 5.

  2. Compile a catalog of all the essentially different bricks that can be made.

148. [ 24 ] Find all the 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).

149. [ M22 ] ( Vertex-colored tetrahedra .) The graph simplex (3, 3, 3, 3, 3, 0, 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 { a, b, c, d, e }, because mirror reflections are distinct. Can those ten colored tetrahedra be packed into simplex (3, 3, 3, 3, 3, 0, 0), with matching colors at every vertex?

150. [ 23 ] 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 several different ways.”

images

(The desired square is 8 × 8.) In exactly how many different ways can it be solved?

▸ 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 ( 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 15 possibilities are reduced under 180° rotation to six pairs, plus three patterns that have central symmetry. Similarly, there are 27 distinct domino patterns that contain two partial paths (where the paths might cross each other). An 8 × 9 arrangement, which nicely illustrates all 36 of the possibilities, is shown; notice that its path is a Hamiltonian cycle, consisting of a single loop .

images

  1. Only two of the dominoes in the arrangement above are in horizontal position. Find a single-loop 8 × 9 arrangement that has 18 horizontals and 18 verticals.

  2. Similarly, find an arrangement that has the maximum number of horizontals.

152. [ 30 ] The complete set of path dominoes includes also twelve more patterns:

images

Arrange all 48 of them in an 8 × 12 array, forming a single loop.

153. [ 25 ] Here are six of the path dominoes, plus a “start” piece and a “stop” piece:

images

  1. Place them within a 4 × 5 array so that they define a path from “start” to “stop.”

  2. 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?

  3. Design an eight-piece puzzle that’s like (a), but it involves four of the two-subpath dominoes instead of only two. (Your puzzle should have a unique solution.)

154. [ M30 ] (C. R. J. Singleton, 1996.) 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 × k , for 1 ≤ k ≤ n , into a box that contains P ( n ) = n · 1 2 +( n − 1) · 2 2 + ··· +1 · n 2 cells. For which values of n is P ( n ) a perfect square?

155. [ 20 ] That “authentic” partridge puzzle has a square solution when n = 6.

  1. Exactly how many different solutions does it have in that case?

  2. 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 ( 62 ) the scores are 165 and 67.) What solutions to (a) have the maximum and minimum affinity scores?

▸ 156. [ 30 ] Straightforward backtracking will solve the partridge puzzle for n = 8, using bitwise techniques to represent a partially filled 36 × 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 different solutions does that partridge puzzle have?

157. [ 22 ] Complete the study of small partridges by extending (6 3 ) to n = 6 and 7.

158. [ 23 ] Another variation of the partridge puzzle when 2 ≤ n ≤ 7 asks for the smallest rectangular area that will contain k nonoverlapping squares of size k × k for 1 ≤ k ≤ n . For example, here are solutions for n = 2, 3, and 4:

images

(To show optimality for n = 4 one must prove that rectangles of sizes 6 × 17, 8 × 13, 5 × 21, and 7 × 15 are too small.) Solve this puzzle for n = 5, 6, and 7.

▸ 159. [ 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 4 ).

160. [ 21 ] The 5-queens problem leads to an interesting graph, whose vertices are the 4860 solutions, with u — 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”?

▸ 161. [ 23 ] Three restricted queen-domination problems are prominent in the literature:

  1. No two queens of a solution attack each other.

  2. Each queen of a solution is attacked by at least one of the others.

  3. The queens of a solution form a clique.

(The third and fourth examples in (6 5 ) are instances of types (ii) and (i).)

Explain how to formulate each of these variants as an MCC problem, analogous to ( 64 ). How many solutions of each type are present in the 5-queens problem?

162. [ 24 ] Say that a 𝒬 n is an n × n array of n nonattacking queens. Sometimes a 𝒬 n contains a 𝒬 m for m < n ; for example, eight of the ten possible 𝒬 5 ’s contain a 𝒬 4 , and the 𝒬 17 illustrated here contains both a 𝒬 4 and a 𝒬 5 .

What is the smallest n such that at least one 𝒬 n contains (a) two 𝒬 4 ’s? (b) three 𝒬 4 ’s? (c) four 𝒬 4 ’s? (d) five 𝒬 4 ’s? (e) two Q5’s? (f) three 𝒬 5 ’s? (g) four 𝒬 5 ’s? (h) two 𝒬 6 ’s? (i) three 𝒬 6 ’s?

images

163. [ 20 ] Explain the peculiar rule for setting p in ( 71 ).

164. [ 17 ] When Algorithm M finds a solution x 0 x 1 ... x l −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 .

165. [ 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 don’t interact.

  1. What’s the size of the search tree if we branch first on item 1, then on item 2? Would it be better to branch first on item 2, then on item 1?

  2. Generalize part (a) to the case when item 1 needs p of p + d options, while item 2 needs q of q + d options, where q > p and d > 0.

166. [ 21 ] Extend answer 9 to the more general situation that arises in Algorithm M :

  1. Let θ 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 θ p as a function of LEN ( p ), SLACK ( p ), and BOUND ( p ).

  2. Suppose θ p \= θ p′ and SLACK ( p ) = SLACK ( p ′) = 0, but LEN ( p ) < LEN ( p ′). Should we prefer to branch on p or on p ′, based on exercise 165 ?

167. [ 24 ] 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 v p for p ’s multiplicity is ≥ M p . Does the precise value of this upper bound affect the behavior of Algorithm M ? (In other words, does v p \= ∞ lead to the same running time as v p \= M p ?)

▸ 168. [ 15 ] An MCC problem might have two identical options α , whose items are allowed to occur more than once. In such cases we might want the second copy of α to be in the solution only if the first copy is also present. How can that be achieved?

▸ 169. [ 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.

170. [ 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.)

▸ 171. [ 35 ] 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.

▸ 172. [ 29 ] A snake 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 ∈ U and t ∈ 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.)

images

For example, let G = P 4 ⊠ P 4 be the graph of king moves on a 4 × 4 board. The set of kings illustrated at the right is not a snake path in G ; but it becomes one if we remove the king in the corner.

  1. Use Algorithm M to discover all of the longest snake paths that are possible on an 8 × 8 chessboard, when G is the graph of all (i) king moves; (ii) knight moves; (iii) bishop moves; (iv) rook moves; (v) queen moves.

  2. Similarly, a snake cycle is a set for which G | U is a cycle. (In other words, that induced graph is connected and 2-regular.) What are the longest possible snake cycles for those five chess pieces?

▸ 173. [ 30 ] ( Knight and bishop sudoku .) Diagram (i) shows 27 knights, arranged with three in each row, three in each column, and three in each 3 × 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 19 can be deduced. Diagrams (iii) and (iv) are analogous, but for bishops instead of knights: (iii) solves puzzle (iv). (i)

images

  1. Explain how to find all completions of such diagrams using Algorithm M .

  2. Find the unique completions of the following puzzles:

    images

  3. Compose additional puzzles like those of (b), in which all clues have the same numerical labels. Try to use as few clues as possible.

  4. Construct a uniquely solvable knight sudoku puzzle that has only three clues.

174. [ 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.

▸ 175. [ M22 ] Given an M × N matrix A = ( a ij ) of 0s and 1s, explain how to find all vectors x = ( x 1 ... x M ) with 0 ≤ x i ≤ r i for 1 ≤ i ≤ M such that xA = ( y 1 ... y N ), where u j ≤ y j ≤ v j for 1 ≤ j ≤ N . (This generalizes the MCC problem by allowing the i th option to be repeated up to r i times.)

▸ 176. [ M25 ] Given an M × N matrix A = ( a ij ) 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.

177. [ M21 ] Algorithm 7.2.1.5M generates the p ( n 1 , ..., n m ) partitions of the multiset { n 1 · x 1 , ..., n m · x m } into submultisets. Consider the special cases where n 1 = ··· = n s \= 1 and n s +1 \= ··· = n s + t \= 2 and s + t = m .

  1. Generate those partitions with Algorithm M , using the previous two exercises.

  2. Also generate the q ( n 1 , ..., n m ) multipartitions into distinct multisets.

178. [ M22 ] ( Factorizations of an integer. ) Use Algorithm M to find all representations of 360 as a product n 1 · n 2 · ... · n t , where (a) 1 < n 1 < ··· < n t ; (b) 2 ≤ n 1 ≤ ··· ≤ n t .

179. [ 15 ] By removing duplicate rows and columns, matrix A reduces to A ′:

A \= ( 1 0 0 0 0 0 0 1 1 1 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 0 0 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1 1 0 0 ) ; A ′ \= ( 1 0 0 0 0 1 1 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 0 1 1 1 1 0 ) .

Derive the exact covers of A from the exact covers of A ′.

▸ 180. [ M28 ] (D. Eppstein, 2008.) Prove that every strict exact cover problem with parameters 1 ≤ t ′ ≤ t , as defined in ( 74 ), contains t ′ items i 1 , ..., i t′ and t + t ′−1 options

o p \= ‘ i 1 ... i p ’ , for 1 ≤ p ≤ t ' ; o t ' + q \= ‘ ... i t ' ... ’ , for 1 ≤ q < t .

Furthermore, i r ∈ o t′+q if and only if 1 ≤ q < t + r − t ′, for 1 ≤ r ≤ t ′.

181. [ M20 ] Find constants c r such that D ( 5 n + r ) \= 4 n c r − 1 3 for n ≥ 3 and 0 ≤ r < 5.

182. [ 21 ] (D. Eppstein, 2008.) Find a strict exact cover problem with 8 options, whose search tree contains 16 nodes and 7 solutions.

183. [ 46 ] Let D ^ ( 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 lim sup n → ∞ D ^ ( n ) 1 / n ?

▸ 184. [ M22 ] Suppose 0 ≤ t ≤ ϖ n . Is there a strict exact cover problem with n items that has exactly t solutions? (For example, consider the case n = 9, t = 10000.)

185. [ M23 ] What is the largest number of solutions to a strict exact cover problem that has N 1 primary items and N 2 secondary items?

186. [ M24 ] Consider l = 0 when Algorithm X is given the extreme problem of order n .

  1. How many updates, u n , does it perform when covering i in step X4?

  2. How many does it perform in step X5, when the option containing x 0 has size k ?

  3. Therefore derive ( 84 ).

187. [ HM29 ] Let X ( z ) = ∑ n x n z n /n ! generate the sequence ⟨ x n ⟩ of ( 82 ).

  1. Use ( 84 ) to prove that X ( z ) \= e e z ∫ 0 z ( ( 2 t − 1 ) e 4 t − ( t − 1 ) e 3 t + 2 t e 2 t + e t ) e − e t d t .

  2. Let T r , s ( z ) \= e e z ∫ 0 z t r e s t e − e t d t . Prove that T r ,0 ( z )/ r ! generates ⟨ a n,r +1 ⟩ in ( 83 ).

  3. Show that T r ,0 ( z ) = ( T r +1, 1 ( z )+ z r +1) /( r + 1); furthermore, when s > 0,

    T r , s ( z ) \= ( Σ k \= 0 r ( − 1 ) k r k ¯ s k + 1 ( T r − k , s + 1 ( z ) + z r − k e s z ) ) − ( − 1 ) r r ! s r + 1 e e z − 1 .

  4. Therefore X ( z ) = 22 e e z −1 + 12 T 0,0 ( z ) − (2 z − 1) e 3 z − 5 ze 2 z − (12 z + 5) e z − 12 z − 18.

▸ 188. [ M21 ] Prove that the Gould numbers 〈 ϖ n ^ 〉 \= 〈 0 , 1 , 1 , 3 , 9 , 31 , 121 , 523 , 2469 , ... 〉 can be calculated rapidly by forming a triangle of numbers analogous to Peirce’s triangle 7.2.1.5–(12):

images

Here the entries ϖ ^ n 1 , ϖ ^ n 2 , ... , ϖ ^ n n of the n th row obey the simple recurrence

ϖ ^ n k \= ϖ ^ ( n − 1 ) k + ϖ ^ n ( k + 1 ) , if 1 ≤ k < n ; ϖ ^ n n \= ϖ ^ ( n − 1 ) 1 , if n > 2 ;

and initially ϖ ^ 11 \= 0 , ϖ ^ 22 \= 1 . Hint: Give a combinatorial interpretation of ϖ ^ n k .

189. [ HM34 ] Let ρ n \= ϖ ^ n − g ^ ϖ n (see ( 86 )). We’ll prove that | ρ n | = O ( e − n /ln 2 n ϖ n ), by applying the saddle point method to R ( z ) \= Σ n ρ n z n / n ! \= e e z ∫ z ∞ e − e t d t . The idea is to show that | R(z) | is rather small when z = ξ e iθ where ξ e ξ = n as in 7.2.1.5–(24).

  1. Express | e e z | and | e −e z | in terms of x and y when z = x + iy .

  2. If 0 ≤ θ ≤ π 2 , y \= ξ sin θ ≤ 3 2 , 0 < c 1 < cos 3 2 , prove | R ( ξ e i θ ) | \= O ( exp ( e ξ − c 1 e ξ ) ) .

  3. If 0 ≤ θ ≤ π 2 , y \= ξ sin θ ≤ 3 2 , 0 < c 2 < 9 8 , prove | R ( ξ e i θ ) | \= O ( exp ( e ξ − c 2 e ξ / ξ ) ) .

  4. Consequently ρ n −1 /ϖ n −1 \= O ( e − n /ln 2 n ) , as desired.

190. [ HM46 ] Study the signs of the residual quantities ρ n \= ϖ n − g ^ ϖ n in exercise 189 .

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 ) \= ∫ 0 ∞ e − x ( 1 + x ) z d x − 1 \= Σ k \= 1 ∞ g ^ k z k . (The first few probabilities in this distribution are ( g ^ 1 , g ^ 2 , ... , g ^ 9 ) ≈ ( .59635 , .26597 , .09678 , .03009 , .00823 , .00202 , .00045 , .00009 , .00002 ); see answer 189 .) What is the average length? What is the variance?

192. [ HM29 ] What’s the asymptotic value of g ^ n when n is large?

193. [ M21 ] Why do ( 87 ) and ( 88 ) count updates when matching in complete graphs?

194. [ HM23 ] 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 .

  1. Prove that 1 + 2 q +(2 q )(2 q − 2) + ··· +(2 q )(2 q − 2) ... (2) = ⌊ e 1/2 2 q q !⌋.

  2. Find a similar “closed formula” for 1 + (2 q − 1) + (2 q − 1)(2 q − 3) + ··· + (2 q − 1) · (2 q − 3) ... ( 3 )( 1 ). Hint: Use the fact that e x erf ( x ) \= Σ n ≥ 0 x n + 1 / 2 / ( n + 1 / 2 ) ! .

  3. Estimate the solution U (2 q + 1) of ( 87 ) to within O (1).

  4. Similarly, give a good approximation to the solution U (2 q ) of ( 88 ).

▸ 195. [ M22 ] Approximately how many updates does Algorithm X perform, when it is asked to find all of the perfect matchings of the graph ( 89 )?

▸ 196. [ M29 ] Given a bounded permutation problem defined by a 1 ... a n , consider the “ dual” problem defined by b 1 ... b n , where b k is the number of j such that 1 ≤ j ≤ n and a j ≥ n + 1 − k . [Equivalently, b n ...b 1 is the conjugate of the integer partition a n ...a 1 , in the sense of Section 7.2.1.4.]

  1. What is the dual problem when n = 9 and a 1 ... a 9 = 246677889?

  2. Prove that the solutions to the dual problem are essentially the inverses of the permutations that solve the original problem.

  3. If Algorithm X begins with an a 1 -way branch on item X 1 , how many updates does it perform while preparing for the subproblems at depth 1 of its search tree?

  4. How many solutions does a bounded permutation problem have, given a 1 ... a n ?

  5. Give a formula for the total number of updates, assuming that the algorithm always branches on X j at depth j − 1 of the search tree.

  6. Evaluate the formula of (e) when a j \= n for 1 ≤ j ≤ n (that is, all permutations).

  7. Evaluate the formula of (e) when a j \= min( j +1, n ) for 1 ≤ j ≤ n .

  8. Evaluate the formula of (e) when a j \= min(2 j, n ) for 1 ≤ j ≤ n .

  9. Show, however, that the assumption in (e) is not always correct. How can the total updates be calculated correctly in general?

197. [ M25 ] Let P ( a 1 , ..., a n ) be the set of all permutations p 1 ... p n that solve the bounded permutation problem for a 1 ... a n , given a 1 ≤ a 2 ≤ ··· ≤ a n and a j ≥ j .

  1. Prove that P ( a 1 , ..., a n ) = {( nt n ) ... (2 t 2 )(1 t 1 ) | j ≤ t j ≤ a n for 1 ≤ j ≤ n }.

  2. Also prove that P ( a 1 , ..., a n ) = { σ nt n ... σ 2 t 2 σ 1 t 1 | j ≤ t j ≤ a n for 1 ≤ j ≤ n }, where σ st is the ( t +1 − s )- cycle ( t t −1 ... s +1 s ).

  3. Let C ( p ) be the number of cycles in the permutation p , and let I ( p ) be the number of inversions. Find the generating functions

    C ( a 1 , ... , a n ) \= Σ p ∈ P ( a 1 , ... a n ) z C ( p ) and I ( a 1 , ... , a n ) \= Σ p ∈ P ( a 1 , ... a n ) z I ( p ) .

198. [ M25 ] Let π rs \= Pr( p r \= s ), when p is a random element of P ( a 1 , ..., a n ).

  1. Compute these probabilities when n = 9 and a 1 a 2 ... a 9 = 255667999.

  2. If r < r ′ and s < s ′, show that π rs /π rs′ \= π r′ s /π r′ s′ , when π rs′ π r′ s′ ≠ 0.

199. [ M25 ] Analyze the behavior of Algorithm X on the 3D matching problem whose options are ‘ a i b j c k ’ for 1 ≤ i, j ≤ n and 1 ≤ k ≤ ( i ≤ m ? m − 1: n ).

▸ 200. [ HM25 ] (A. Björklund, 2010.) We can use polynomial algebra, instead of backtracking, to decide whether or not a given 3D matching problem is solvable. Let the items be { a 1 , ..., a n }, { b 1 , ..., b n }, { c 1 , ..., c n }, and assign a symbolic variable to each option. If X is any subset of C , let Q ( X ) be the n × n matrix whose entry in row i and column j is the sum of the variables for all options ‘ a i b j c k ’ with c k ∉ X .

For example, suppose n = 3. The seven options t : ‘ a 1 b 1 c 2 ’, u : ‘ a 1 b 2 c 1 ’, v : ‘ a 2 b 3 c 2 ’, w : ‘ a 2 b 3 c 3 ’, x : ‘ a 3 b 1 c 3 ’, y : ‘ a 3 b 2 c 1 ’, z : ‘ a 3 b 2 c 2 ’ yield the matrices

X \= ∅ { c 3 } { c 2 } { c 2 , c 3 } { c 1 } { c 1 , c 3 } { c 1 , c 2 } Q ( X ) \= ( t u 0 0 0 v + w x y + z 0 ) ( t u 0 0 0 v 0 y + z 0 ) ( 0 u 0 0 0 w x y 0 ) ( 0 u 0 0 0 0 0 y 0 ) ( t 0 0 0 0 v + w x z 0 ) ( t 0 0 0 0 v 0 z 0 ) ( 0 0 0 0 0 w x 0 0 )

(and Q ( C ) is always zero). The determinant of Q (∅) is u ( v + w ) x − t ( v + w )( y + z ).

  1. If the given problem has r solutions, prove that the polynomial

    S \= Σ X ⊂ C ( − 1 ) | X | det Q ( X )

    is the sum of r monomials, each with coefficient ±1. (In the given example it is uvx − twy .) Hint: Consider the case where all possible options are present.

  2. Use this fact to design a randomized algorithm that decides q.s. whether or not a matching exists, in O (2 n n 4 ) steps.

▸ 201. [ M30 ] Consider the bipartite matching problem that has 3 n options, ‘ X j Y k ’ for 1 ≤ j, k ≤ n and ( j − k )mod n ∈ {0, 1, n − 1}. (Assume that n ≥ 3.)

  1. What “natural, intuitively obvious” problem is equivalent to this one?

  2. How many solutions does this problem have?

  3. How many updates does Algorithm X make when finding all solutions, if the items are ordered X 1 , Y 1 , ..., X n , Y n , and if exercise 9 is used in step X3?

202. [ 13 ] What is images

203. [ M15 ] Equation ( 95 ) shows that the binary operation T ⊕ T ′ on search trees has an identity element, ‘◼’. Is that operation (a) associative? (b) commutative?

204. [ M25 ] True or false: Node αα ′ is dominant in T ⊕ T ′ if and only if α is dominant in T and α ′ is dominant in T ′. Hint: Express deg( αα ′) in terms of deg( α ) and deg( α ′).

205. [ M28 ] Prove Lemma D, about the structure of T ⊕ T ′.

206. [ 20 ] If T is minimally dominant and deg(root( T )) ≤ deg(root( T ′)), show that it’s easy to describe the tree T ⊕ T ′.

207. [ 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

α i \= ρ t 1 + ρ t 2 + ... , for each item   i ,

where ρ (typically 0.9) is a user-specified damping factor, and where i ’s list of active options was modified at times t − t 1 , t − t 2 , ...; here t denotes the current “time,” as measured by some convenient clock. When step X3 chooses an item for branching, the MRV heuristic of exercise 9 rates i by its degree λ i \= LEN ( i ); the new heuristic replaces this by

λ i ′ \= { λ i , if λ i ≤ 1 ; 1 + λ i / ( 1 + μ α i ) , if λ i ≤ 2.

Here μ is another user-specified parameter. If μ = 0, decisions are made as before; but larger and larger values of μ cause greater and greater attention to be given to the recently active items, even if they have a somewhat large degree of branching.

  1. For example, suppose α i \= 1, α j \= 1/2, and μ = 1. Which item will be preferable, i or j , if LEN ( i ) = LEN ( j ) + 1 and 0 ≤ LEN ( j ) ≤ 4?

  2. What modifications to Algorithm X will implement this scheme?

  3. What values of ρ and μ will avoid exponential growth, when applied to n independent copies of the toy problems ( 90 ) and ( 92 )?

  4. Does this method save time in the Y pentomino problem of Fig. 73 ?

▸ 208. [ 21 ] Modify the exact cover problem of Fig. 73 so that none of the Y pentominoes that occur in an ‘H’ or images have been flipped over. Hint: To prevent the flipped-over Y’s marked 8 and b from occurring simultaneously, use the options ‘ 1c 2c 3c 4c 3b V 1b ’ and ‘ 1a 2a 3a 4a 2b V 1b ’, where V 1b is a secondary item.

209. [ 20 ] Improve the search tree ( 93 ) in the same way that ( 100 ) improves on ( 91 ), by considering two bi pairs of ( 92 ).

210. [ 21 ] A “ bitriple” ( α, β, γ ; α ′, β ′, γ ′) is analogous to a bipair, but with ( 97 ) replaced by α + β + γ = α ′ + β ′ + γ ′. How can we modify an exact cover problem so that it excludes all solutions in which options α ′, β ′, and γ ′ are simultaneously present?

211. [ 20 ] Do the options of the text’s formulation of the Langford pair problem have any bipairs? How about the n queens problem? And sudoku?

▸ 212. [ M21 ] If the primary items of an exact cover problem have been linearly ordered, we can say that the bipair ( α, β ; α ′, β ′) is canonical if (i) the smallest item in all four options appears in α and α ′; and (ii) option α is lexicographically smaller than option α ′, when their items have been listed in ascending order.

  1. Prove that Theorem S applies to exact coverings that are strong according to this definition of canonicity. Hint: Show that it’s a special case of the text’s definition.

  2. Does such an ordering justify the choices made in ( 99 )?

213. [ M21 ] If π and π ′ are two partitions of the same set, say that π < π ′ if the restricted growth string of π is lexicographically less than the restricted growth string of π ′. Let ( α, β ; α ′, β ′) be a canonical bipair in the sense of exercise 212 . Also let π be a partition of the items such that α and β are two of its parts, and let π ′ be the same partition but with α ′ and β ′ substituted. Is π < π ′?

▸ 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 all strong solutions?

▸ 215. [ M30 ] The perfect matching problem on the complete graph K 2 q +1 is the X2C problem with 2 q +1 primary items {0, ..., 2 q } and ( 2 q + 1 2 ) options ‘ i j ’ for 0 ≤ i < j ≤ 2 q .

  1. How many bipairs are present in this problem?

  2. Say that ( i, j, k, l ) is excluded if there’s a canonical bipair ( α, β ; α ′, β ′) for which α ′ = ‘ i j ’ and β ′ = ‘ k l ’. Prove that, regardless of the ordering of the options, the number of excluded quadruples is 2/3 of the number of bipairs.

  3. What quadruples are excluded when the options are ordered lexicographically?

  4. We reduce the amount of search by introducing a secondary item ( i, j, k, l ) for each excluded quadruple, and appending it to the options for ‘ i j ’ and ‘ k l ’. Describe the search tree when this has been done for the quadruples of (c).

  5. Show that only Θ( q 3 ) excluded quadruples suffice to obtain that search tree.

  6. Order the options cleverly so that the search tree has only 2 q + 1 nodes.

  7. How many excluded quadruples suffice to obtain that search tree?

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).

217. [ M32 ] A bipair of pentominoes ( α, β ; α ′, β ′) is a configuration such as

images

where two pentominoes occupy a 10-cell region in two different ways. In this example we may write α = S + 00 + 01 + 11 + 12 + 13, β = Y + 02 + 03 + 04 + 05 + 14, α ′ = S + 04 + 05 + 12 + 13 + 14, β ′ = Y + 00 + 01 + 02 + 03 + 11; hence α + β = α ′ + β ′ as in ( 97 ).

Compile a complete catalog of all bipairs that are possible with distinct pentominoes. In particular, show that each of the twelve pentominoes participates in at least one such bipair. (It’s difficult to do this by hand without missing anything. One good approach is to exploit the equation α − α ′ = −( β − β ′): First find all the delta values ±( α − α ′) for each of the twelve pentominoes individually; then study all deltas that are shared by two or more of them. For example, the S and Y pentominoes both have 00 + 01 − 04 − 05 + 11 − 14 among their deltas.)

▸ 218. [ 20 ] Why must i be uncolored, in the definition of “ forcing” for Algorithm P ?

219. [ 20 ] Suppose p and q are primary items of 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; yet p and q never occur in the same option. Prove that every option that contains i or j , but neither p nor q , can be removed without changing the problem.

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.

221. [ 23 ] After all options that begin with item i have been examined in step P5, 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.

222. [ 22 ] Before item i is deleted in step P7, it should be removed from every option that contains S , by changing the corresponding node into a spacer. All options that involve i but not S should also be deleted. Spell out the low-level details of this process.

223. [ 20 ] Implement the output phase of Algorithm P (step P10).

▸ 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).

225. [ 21 ] Why does Algorithm P remove 235 options in the 6 × 10 pentomino problem, but only 151 options in the “one-sided” 6 × 15 case?

226. [ M20 ] Assume that a 1 ... a 2 n is a Langford pairing, and let a k ′ \= a 2 n + 1 − k so that a 1 ′ ... a 2 n ′ is the reverse of a 1 ... a 2 n . Are there any obvious relations between the sums

Σ 1 \= Σ k \= 1 2 n k a k , Σ 1 ′ \= Σ k \= 1 2 n k a k ′ Σ 2 \= Σ k \= 1 2 n k 2 a k , Σ 2 ′ \= Σ k \= 1 2 n k 2 a k ′ , ?

What about the analogous sums S \= Σ k \= 1 2 n k a k 2 and S ′ \= Σ k \= 1 2 n k ( a k ′ ) 2 ?

227. [ 10 ] What cost should be assigned to option ( 16 ), to minimize (a) ∑ 2 ? (b) S ?

228. [ M30 ] The Langford pairings for n = 16 that minimize ∑ 2 turn out to be precisely the 12,016 pairings that minimize ∑ 1 ; and their reversals turn out to be precisely the 12,016 pairings that maximize both ∑ 2 and ∑ 1 . Is this surprising, or what?

▸ 229. [ 25 ] What Langford pairings for n = 16 are lexicographically smallest and largest?

230. [ 20 ] Explain how Algorithm X $ , which minimizes the sum of option costs, can also be used to maximize that sum, in problems like that of Fig. 74 .

231. [ 21 ] What’s the maximum SCRABBLE ® -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?

images images

For example, WATCH|AGILE|RADAR|TREND scores 26+10+7+18+14+9+5+7+24 points.

232. [ 20 ] The costs supplied to Algorithm X $ must be nonnegative integers; but d ( i, j ) in the 16 queens problem of Fig. 74 is never an integer. Is it OK to use $⌊ d ( i, j )⌋ instead of $ d ( i, j ) for the cost of placing a queen in cell ( i, j )?

233. [ 20 ] Minimize and maximize the product of the 16 queen distances, not the sum.

234. [ M20 ] What is the minimum-cost placement of n nonattacking queens when the cost of a queen in cell ( i, j ) is $ d ( i, j ) 2 , the square of its distance from the center?

▸ 235. [ 21 ] Solve the problem of Fig. 74 using the (integer) costs $4 d ( i, j ) 4 .

▸ 236. [ M41 ] When the cost of a queen in cell ( i, j ) is $ d ( i, j ) N , for larger and larger values of N , the minimum-cost solutions to the n queens problem eventually converge to a fixed pattern. And those “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 here, can actually be discovered by hand, with a few moments of concentrated thought. Notice that it is doubly symmetric and nicely “rounded.”

images

Discover such optimum placements for as many n as you can ( not by hand).

▸ 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.

238. [ 24 ] Find 3× n arrays filled with distinct 3-digit and n -digit primes, for 3 ≤ n ≤ 7, having the minimum and maximum possible product.

▸ 239. [ M27 ] Given a family { S 1 , ..., S m } of subsets of {1, ..., n }, together with positive weights ( w 1 , ..., w m ), the optimum set cover problem asks for a minimum-weight way to cover {1, ..., n } with a union of S j ’s. Formulate this problem as an optimum exact cover problem, suitable for solution by Algorithm X $ . Hint: Maximize the weight of all sets that do not participate in the cover.

240. [ 16 ] What usable 6-state options include MT and TX in the USA-partition problem?

241. [ 21 ] Does preprocessing by Algorithm P remove the useless option ( 114 )?

▸ 242. [ 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 ).

243. [ 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 $( x 2 ), where x is the sum of the option’s weights, prove that every minimum-cost solution also minimizes Σ k \= 1 d ( x k − r ) 2 , for any given real number r .

244. [ M21 ] The induced subgraphs G | 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 problem whose items are the vertices of G , and whose options all contain exactly t items. True or false: A solution that minimizes the sum of the interior costs also minimizes the sum of the exterior costs, as defined in the text.

245. [ 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.

246. [ 22 ] The left-hand graph partition in ( 116 ) 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] million, plus two options that include New York, New England, and possibly New Jersey. The options of the right-hand example would again be the connected subsets with population in [50.5 .. 51.5] million.)

Consider also minimizing the interior costs, as defined in exercise 244 .

247. [ 23 ] Specify step C1 $ , which takes the place of step C1 when Algorithm C is extended to Algorithm C $ . 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 at least one option whose cost is now zero. Be sure to obey condition ( 118 ).

248. [ 22 ] Let ϑ = T − C l in step C3$, 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 probably belongs to the fewest options of cost < ϑ . Instead of taking the time to make a complete search, assume conservatively that there are LEN ( i ) such items, after verifying that item i has at least L of them, where L is a parameter.

249. [ 21 ] A set of dk costs, with 0 ≤ c 1 ≤ c 2 ≤ ··· ≤ c dk , is said to be bad if c k + c 2 k + ··· + c dk ≥ θ . 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, k = 2, and θ = 16. If costs appear in the order (3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8), your algorithm should stop after seeing the 2.

250. [ 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 with a character in Z . (For example, Z = { p, r, c, b } in the sudoku options ( 30 ); z = 1 in options ( 110 ). In the options ( 16 ) for Langford pairs, we could change the name of each numeric item i to ‘! i ’, then let Z = {!} and z = 2.) Explain how to use these hints to supply an early-cutoff test at the beginning of step C3$, as explained in the text.

251. [ 18 ] If a given problem is solvable, when does Algorithm Z first discover that fact?

▸ 252. [ 20 ] Algorithm Z produces the ZDD ( 120 ) from the options ( 121 ) if step Z3 simply chooses the leftmost item i 1 = RLINK (0) instead of using the MRV heuristic. What ZDD would have been obtained if the method of exercise 9 had been used instead?

▸ 253. [ 21 ] Extend Algorithm Z so that it reports the total number of solutions.

▸ 254. [ 28 ] The signature σ 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.

  1. Explain why one bit isn’t sufficient for secondary items with colors.

  2. Suggest a good way to implement the computation of σ.

  3. Algorithm C uses the operations hide′ and unhide′ in ( 50 )–( 57 ), in order to avoid unnecessary accesses to memory in nodes for secondary items. Explain why Algorithm Z does not want to use those optimizations. Hint: Algorithm Z needs to know whether the option list for a secondary item is empty.

  4. When the list for item i is purified, its options of the wrong color are removed from other lists. But they remain on list i , in order to be unpurified later. How then can Algorithm Z know when list i is no longer relevant to the current subproblem?

255. [ HM29 ] Express the exact number of updates made by Algorithm Z when it finds the perfect matchings of K N , as well as the exact number of ZDD nodes produced, in terms of Fibonacci numbers. Hint: See exercise 193 .

▸ 256. [ M23 ] What is the behavior of Algorithm Z when it is asked to find all perfect matchings of the “bizarre” graph ( 89 )?

▸ 257. [ 21 ] How does Algorithm Z do on the “ extreme” exact cover problem, with n items and 2 n − 1 options? (See the discussion preceding ( 82 ).)

  1. What signatures are formed in step Z2?

  2. Draw the schematic ZDD, analogous to ( 123 ), when n = 4.

258. [ HM21 ] How many updates does Algorithm Z perform, in that extreme problem?

259. [ M25 ] Exercise 196 analyzes the behavior of Algorithm X on the bounded permutation problem defined by a 1 ... a n . Show that Algorithm Z is considerably faster, by determining the number of memos, ZDD nodes, and updates when a 1 a 2 ... a n −1 a n is (a) n n...n n [with n ! solutions]; (b) 2 3 ... n n [with 2 n −1 solutions]. Assume that the items are X 1 , X 2 , ..., X n , Y 1 , Y 2 , ..., Y n , in that order.

260. [ M21 ] Exercises 14 and 201 are bipartite matching problems related to choosing seats at a circular table. Test Algorithm Z empirically on those problems, and show that it solves the latter in linear time (despite exponentially many solutions).

▸ 261. [ 23 ] Let G be a directed acyclic graph, with source vertices S and sink vertices T .

  1. Use Algorithm C (or Z ) to find all sets of m vertex-disjoint paths from S to T .

  2. Also find all such sets of paths from s k to t k for 1 ≤ k ≤ m , given s k and t k .

  3. Apply (a) to find all sets of n − 1 disjoint paths that enter an n × n square at the north or east edge, proceed by south and/or west steps, and exit at the south or west edge, avoiding the corners. (A random 16 × 16 example is shown.)

    images

  4. Apply (b) to find all vertex-disjoint, downward paths of eight knights that start on the top row of a chessboard and end on the bottom row in reverse order.

▸ 262. [ M23 ] 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).)

  1. 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 from each corner of a 16 × n rectangle:

    images

    How many tilings are possible for S 16 (the Aztec diamond of order 8)? For S 32 ?

  2. Similarly, determine the ZDD size for the family of all diamond tilings of T n — the grid simplex ( n + 16, n + 8, 16, n + 8, 0, 0, 0), a hexagon of sides (8, 8, n , 8, 8, n ):

    images

263. [ 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 × 6 “word search” problem of exercise 105 ; (g) the kakuro problem in exercise 431 .

264. [ M21 ] Suppose step Z3 always chooses the first active item i = RLINK (0), instead of using the MRV heuristic, unless some other active item has LEN ( i ) = 0. Prove that Algorithm Z will then output an ordered ZDD.

▸ 265. [ 22 ] Prove that Algorithm Z will never produce identical ZDD nodes ( o ¯ i ? l i : h i ) \= ( o ¯ j ? l j : h j ) for i ≠ j , if all items are primary. But secondary items can cause duplicates.

Exercises—Second Set

Thousands of fascinating recreational problems have been based on polyominoes and their polyform cousins (the polycubes, polyiamonds, polyhexes, polysticks, ... ). The following exercises explore “the cream of the crop” of such classic puzzles, as well as a few gems that were not discovered until recently.

In most cases the point of the exercise is to find a good way to discover all solutions, usually by setting up an appropriate exact cover problem that can be solved without taking an enormous amount of time.

▸ 266. [ 25 ] Sketch the design of a utility program that will create sets of options by which an exact cover solver will fill a given shape with a given set of polyominoes.

267. [ 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.

images

▸ 268. [ 21 ] There are 1010 ways to pack the twelve pentominoes into a 5 × 12 box, not counting reflections. What’s a good way to find them all, using Algorithm X ?

269. [ 21 ] How many of those 1010 packings decompose into 5 × k and 5 × (12 − k )?

270. [ 21 ] In how many ways can the eleven nonstraight pentominoes be packed into a 5 × 11 box, not counting reflections as different? (Reduce symmetry cleverly.)

271. [ 20 ] There are 2339 ways to pack the twelve pentominoes into a 6 × 10 box, not counting reflections. What’s a good way to find them all, using Algorithm X ?

272. [ 23 ] Continuing exercise 271 , explain how to find special kinds of packings:

  1. Those that decompose into 6 × k and 6 × (10 − k ).

  2. Those that have all twelve pentominoes touching the outer boundary.

  3. Those with all pentominoes touching that boundary except for V, which doesn’t.

  4. Same as (c), with each of the other eleven pentominoes in place of V.

  5. Those with the minimum number of pentominoes touching the outer boundary.

  6. Those that are characterized by Arthur C. Clarke’s description, as quoted below.

That is, the X pentomino should touch only the F (aka R), the N (aka S), the U, and the V — no others.

Very gently, he replaced the titanite cross
in its setting between the F, N, U, and V pentominoes.

— ARTHUR C. CLARKE, Imperial Earth (1976)

273. [ 25 ] All twelve pentominoes fit into a 3 × 20 box only in two ways, shown in (3 6 ).

  1. How many ways are there to fit eleven of them into that box?

  2. In how many solutions to (a) are the five holes nonadjacent , kingwise?

  3. In how many ways can eleven pentominoes be packed into a 3 × 19 box?

274. [ 21 ] There are five different tetrominoes , namely

images

In how many essentially different ways can each of them be packed into an 8 × 8 square together with the twelve pentominoes?

275. [ 21 ] If an 8 × 8 checkerboard is cut up into thirteen pieces, representing 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 do not?

276. [ 18 ] Design a nice, simple tiling pattern that’s based on the five tetrominoes.

277. [ 25 ] How many of the 6 × 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?

▸ 278. [ 32 ] Use the catalog of bipairs in exercise 217 to reduce the number of 6 × 10 pentomino packings, listing strong solutions only (see Theorem S). How much time is saved?

279. [ 40 ] (H. D. Benjamin, 1948.) Show that the twelve pentominoes can be wrapped around a cube of size 10 × 10 × 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:

images

(Photos by Héctor García)

What is the best way to do this, minimizing undesirable distortions at the corners?

▸ 280. [ 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.

▸ 281. [ 20 ] The white cells of a (2 n +1)×(2 n +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

images

except that (ii) has a “ hole” of order 3/2. Thus (i) has 61 cells, and (ii) has 80.

  1. Find all ways to pack (i) with the twelve pentominoes and one monomino.

  2. Find all ways to pack (ii) with the 12 pentominoes and 5 tetrominoes.

Speed up the process by not producing solutions that are symmetric to each other.

▸ 282. [ 22 ] (Craig S. Kaplan.) A polyomino can sometimes be surrounded by non-overlapping 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) largest fences for each of the twelve pentominoes. (Some of these patterns are unique, and quite pretty.)

283. [ 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.”

▸ 284. [ 27 ] Solomon Golomb discovered in 1965 that there’s only one placement of two pentominoes in a 5 × 5 square that blocks the placement of all the others.

images

Place (a) {O, P, U, V} and (b) {P, R, T, U} into a 7 × 7 square in such a way that none of the other eight will fit in the remaining spaces.

285. [ 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:

images

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?

286. [ 21 ] If you want to pack the twelve pentominoes into a 6 × 10 box without turning any pieces over, 2 6 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?

▸ 287. [ 23 ] A princess asks you to pack an m × n box with pentominoes, rewarding you with $ c · ( ni + j ) if you’ve covered cell ( i, j ) with piece c , where c = (1, 2, ..., 12) for pieces (O, P, ..., Z). (The most valuable packing will be “closest to alphabetic order.”)

Use Algorithm X $ to maximize your bounty when packing boxes of sizes 4 × 15, 5 × 12, 6 × 10, 10 × 6, 12 × 5, and 15 × 4. Consider also the princess’s circle-shaped subset of a 9 × 9 box, where you are to cover only the 60 cells whose distances from the center are between 1 and 18 . How do the running times of Algorithm X $ compare to the amounts of time that Algorithm X would take to find all solutions?

288. [ 21 ] Similarly, pack the one-sided pentominoes optimally into 9 × 10 and 10 × 9.

▸ 289. [ 29 ] ( Pentominoes of pentominoes .) Magnify the 3 × 20 pentomino packing (3 6 ) by replacing each of its unit cells by (a) 3 × 4 rectangles; (b) 4 × 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 partition the 720-cell shape below into 3 × 20 approximately square 12-cell regions, by assigning each gray cell to an adjacent region. (This shape has been superimposed on a grid whose 12 × 12 regions are perfectly square.) Minimize the total perimeter of the 60 resulting regions, and try for a pleasantly symmetrical solution.

images

Use your partition to present a scaled-up version of (3 6 ), again with 12 complete sets.

290. [ 21 ] When te trominoes 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?

291. [ 24 ] ( 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:

images

Which of the ( 12 3 ) sets of three pentominoes always allow this to be done?

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 × 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 proved that the hexominoes actually can’t be packed into any rectangle! Nevertheless, Benjamin continued to play with them, eventually discovering that they fit nicely into the triangle shown here.

images

Prove Kadner’s theorem. Hint: See exercise 275 .

293. [ 24 ] (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

images

For which hexominoes can this be done?

▸ 294. [ 30 ] Show that the 35 hexominoes can be packed into five “ castles”:

images

In how many ways can this be done?

295. [ 41 ] For which values of m can the hexominoes be packed into a box like this?

images

296. [ 41 ] Perhaps the nicest hexomino packing uses a 5 × 45 rectangle with 15 holes

images

proposed by W. Stead in 1954. In how many ways can the 35 hexominoes fill it?

297. [ 24 ] (P. Torbijn, 1989.) Can the 35 hexominoes be packed into six 6 × 6 squares?

▸ 298. [ 22 ] In how many ways can the twelve pentominoes be placed into an 8 × 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.

images

299. [ 39 ] If possible, solve the analog of exercise 298 for the case of 35 hexominoes in a 5 × 54 rectangle, leaving holes in the shapes of the twelve pentominoes .

▸ 300. [ 24 ] In how many ways can the twelve pentominoes be arranged in a 10 × 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 is really interesting?

301. [ 25 ] Here’s one way to place the twelve pentominoes into a 5 × 5 square, covering the cells of rows (1, 2, 3, 4, 5) exactly (2, 3, 2, 3, 2) times:

images

  1. How many such placements are possible?

  2. Suppose we’ve placed O first, P next, Q next, ..., Z last, when making the arrangement above. Then Z is above W is above V is above U is above P is above O ; hence the pentominoes have been stacked up on six levels. Show that a different order of placement would require only four levels.

  3. Find a solution to (a) that needs only three levels.

  4. Find a solution to (a) that can’t be achieved with only four levels.

302. [ 26 ] Say that an n -omino is “ small” if it fits in a ( n + 1 ) × ( n + 1 ) box, and “ slim” if it contains no 2 × 2 tetromino. Thus, for example, pentominoes O, Q, S, Y aren’t small; P isn’t slim.

  1. How many nonominoes are both small and slim?

  2. Fit nine different small-and-slim nonominoes into a 9 × 9 box.

  3. Use a solution to (b) as the basis of a jigsaw sudoku puzzle with a unique solution. The clues of your puzzle should be the initial digits of π .

▸ 303. [ HM35 ] 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.1.4.) For example, there are five parallominoes whose boundary paths have length 4:

images

  1. Find a one-to-one correspondence that maps the set of ordered trees with m leaves and n nodes into the set of parallominoes with width m and height n − m .

  2. Study the generating function G ( w , x , y ) \= Σ parallominoes w area x width y height .

  3. Prove that the parallominoes whose width-plus-height is n have total area 4 n −2 .

  4. Part (c) suggests that we might be able to pack all of those parallominoes into a 2 n −2 × 2 n −2 square, without rotating them or flipping them over. Such a packing is clearly impossible when n = 3 or n = 4; but is it possible when n = 5 or n = 6?

304. [ M25 ] Prove that it’s NP-complete to decide whether or not n given polyominoes, each of which fits in a Θ(log n ) × Θ(log n ) square, can be exactly packed into a square.

305. [ 28 ] When a square grid is scaled by 1 / 2 and rotated 45°, we can place half of its vertices on top of the original ones; the other “ odd-parity” vertices then correspond to the centers of the original square cells.

Using this idea we can glue a small domino of area 1 over portions of an ordinary domino of area 2, thereby obtaining ten distinct two-layer pieces called the windmill dominoes :

images

images

  1. Arrange four windmill dominoes so that the upper layer resembles a windmill.

  2. Place all ten windmill dominoes inside a 4 × 5 box, without overlapping.

  3. Similarly, pack them all into a 2 × 10 box.

  4. Place them so that the upper layer fills a ( 4 / 2 ) × ( 5 / 2 ) rectangle.

  5. Similarly, fit the upper layer into a ( 2 / 2 ) × ( 10 / 2 ) rectangle.

In each case (a)–(e), use Algorithm X to count the total number of possible placements. Also look at the output and choose arrangements that are especially pleasing.

▸ 306. [ 32 ] (S. Grabarchuk, 1996.) In how many ways can the ten windmill dominoes be arranged so that the 20 large squares define a snake cycle, in the sense of exercise 172(b) , and so do the 20 small squares? (For example, arrangements like

images

satisfy one snake condition but not the other.)

307. [ M21 ] If a (3 m +1)×(3 n +2) box is packed with 3 mn +2 m + n straight trominoes and one domino, where must the domino be placed?

308. [ 22 ] A polyiamond is a connected set of triangles in a triangular grid, inspired by the diamond images — just as a polyomino is a connected set of squares in a square grid, inspired by the domino images . Thus we can speak of moniamonds, diamonds, triamonds, etc.

  1. Extend exercise 266 to the triangular grid, using the coordinate system of exercise 124 . How many base placements do each of the tetriamonds have?

  2. Find all ways to pack the pentiamonds into a convex polygon (see exercise 143 ).

  3. Similarly, find all such ways to pack the one-sided pentiamonds.

309. [ 24 ] The hexiamonds are particularly appealing, because — like pentominoes — there are 12 of them. Here they are, with letter names suggested by J. H. Conway:

images

  1. How many base placements do they have?

  2. In how many ways can they be packed into convex polygons, as in exercise 308 ?

310. [ 23 ] What’s the smallest m for which the 12 hexiamonds fit without overlap in

images

Find a pleasant way to place them inside of that smallest box.

▸ 311. [ 30 ] ( Hexiamond wallpaper. ) Place the twelve hexiamonds into a region of N triangles, so that (i) shifted copies of the region fill the plane; (ii) the hexiamonds of the resulting infinite pattern do not touch each other, even at vertices; (iii) N is minimum.

312. [ 22 ] The following shape can be folded, to cover the faces of an octahedron:

images

Fill it with hexiamonds so that they cross the folded edges as little as possible.

▸ 313. [ 29 ] ( Hexiamonds of hexiamonds .) A “ whirl,” shown here, is an interesting dodeciamond that tiles the plane in a remarkably beautiful way.

images

If each triangle ‘Δ’ of a hexiamond is replaced by a whirl, in how many ways can the resulting 72-triangle shape be packed with the full set of hexiamonds? ( Exercise 289 discusses the analogous problem for pentominoes.)

Consider also using “flipped whirls,” the left-right reflections of each whirl.

▸ 314. [ 28 ] (G. Sicherman, 2008.) Can the four pentiamonds be used to make two 10-iamonds of the same shape? Formulate this question as an exact cover problem.

315. [ 20 ] A polyhex is a connected shape formed by pasting hexagons together at their edges, just as polyominoes are made from squares and polyiamonds are made from triangles. For example, there’s one monohex and one dihex, but there are three trihexes. Chemists have studied polyhexes since the 19th century, and named the small ones:

images

(Groups of six carbon atoms can bond together in a nearly planar fashion, forming long chains of hexagons, with hydrogen atoms attached. But the correspondence between polyhexes and polycyclic aromatic hydrocarbons is not exact.)

Represent the individual hexagons of an infinite grid by Cartesian-like coordinates

images

where 1 ¯ \= − 1 , 2 ¯ \= − 2 , etc. Extending exercises 266 and 308(a) , explain how to find the base placements of a polyhex, given the coordinates of its cells when placed on this grid.

316. [ 20 ] Show that the complete set of trihexes and tetrahexes can be packed nicely into a rosette that consists of 37 concentric hexagons. In how many ways can it be done?

317. [ 22 ] ( Tetrahexes of tetrahexes .) If we replace each hexagon of a tetrahex by a rosette of seven hexagons, we get a 28-hex. In how many ways can that scaled-up shape be packed with the seven distinct tetrahexes? (See exercises 289 and 313 .)

▸ 318. [ 20 ] Let’s say that the T-grid is the set of all hexagons xy with x ≢ y (modulo 3):

images

Show that there’s a one-to-one correspondence between the hexagons of the T-grid and the triangles of the infinite triangular grid, in which every polyiamond corresponds to a polyhex. (Therefore the study of polyiamonds is a special case of the study of polyhexes!) Hint: Exercise 124 discusses a coordinate system for representing triangles.

319. [ 21 ] After polyominoes, polyiamonds, and polyhexes, the next most popular polyforms are the polyaboloes , originally proposed by S. J. Collins in 1961. These are the shapes obtainable by attaching isosceles right triangles at their edges; for example, there are three diaboloes: images Notice that any n -abolo corresponds to a 2 n -abolo, when it has been scaled up by 2 .

The 14 tetraboloes can be named by using rough resemblances to hexiamonds:

images

Show that the study of polyaboloes can be reduced to the study of (slightly generalized) polyominoes, just as exercise 318 reduces polyiamonds to polyhexes.

▸ 320. [ M28 ] Explain how to enumerate all of the N -aboloes that are convex . How many of the convex 56-aboloes can be packed by the fourteen tetraboloes?

321. [ 24 ] (T. H. O’Beirne, 1962.) In how many ways can a square be formed from the eight one-sided tetraboloes and their mirror images?

322. [ 23 ] The polysticks provide us with another intriguing family of shapes that can be combined in interesting ways. An “ n -stick” is formed by joining n horizontal and/or vertical unit line segments together near grid points. For example, there are two disticks and five tristicks; and of course there’s only one monostick. They’re shown here in white, surrounded by the sixteen tetrasticks in black.

images

Polysticks introduce yet another twist into polyform puzzles, because we must not allow different pieces to cross each other when we pack them into a container. Extend exercise 266 to polysticks, so that Algorithm X can deal with them conveniently.

323. [ M25 ] We’ve now seen polyominoes, polyiamonds, polyhexes, ..., polysticks, each of which have contributed new insights; and many other families of “polyforms” have in fact been studied. Let’s close our survey with polyskews , a relatively new family that seems worthy of further exploration. Polyskews are the shapes that arise when we join squares alternately with rhombuses, in checkerboard fashion. For example, here are the ten tetraskews:

images

There are two monoskews, one diskew, and five triskews.

  1. Explain how to draw such skewed pixel diagrams.

  2. Show that polyskews, like polyaboloes, can be reduced to polyominoes.

  3. In how many ways do the tetraskews make a skewed rectangle?

▸ 324. [ 20 ] Extend exercise 266 to three dimensions. How many base placements do each of the seven Soma pieces have?

325. [ 27 ] The Somap is the graph whose vertices are the 240 distinct solutions to the Soma cube problem, with u — v if and only if u can be obtained from an equivalent of v by changing the positions of at most three pieces. The strong Somap is similar, but it has u — v only when a change of just two pieces gets from one to the other.

  1. What are the degree sequences of the Somap graphs?

  2. How many connected components do they have? How many bicomponents?

▸ 326. [ M25 ] Use factorization to prove that Fig. 75 ’s W-wall cannot be built.

327. [ 24 ] Figure 75(a) shows some of the many “low-rise” (2-level) shapes that can be built from the seven Soma pieces. Which of them is hardest (has the fewest solutions)? Which is easiest? Answer those questions also for the 3-level prism shapes in Fig. 75(b) .

▸ 328. [ M23 ] Generalizing the first four examples of Fig. 75 , study the set of all shapes obtainable by deleting three cubies from a 3 × 5 × 2 box. (Two examples are shown here.) How many essentially different shapes are possible? Which shape is easiest? Which shape is hardest?

images

329. [ 22 ] Similarly, consider (a) all shapes that consist of a 3 × 4 × 3 box with just three cubies in the top level; (b) all 3-level prisms that fit into a 3 × 4 × 3 box.

330. [ 25 ] How many of the 1285 nonominoes define a prism that can be realized by the Soma pieces? Do any of those packing problems have a unique solution?

331. [ M40 ] Make empirical tests of Piet Hein’s belief that the number of shapes achievable with seven Soma pieces is approximately the number of 27-cubie polycubes.

332. [ 20 ] (B. L. Schwartz, 1969.) Show that the Soma pieces can make shapes that appear to have more than 27 cubies, because of holes hidden inside or at the bottom:

images

In how many ways can each of these three trick shapes be constructed?

333. [ 22 ] Show that the seven Soma pieces can also make structures such as

images

which are “ self-supporting” via gravity. (You may need to place a small book on top.)

images

Fig. 75. Gallery of noteworthy polycubes that contain 27 cubies. All of them can be built from the seven Soma pieces, except for the W-wall. Many constructions are also stable when tipped on edge and/or when turned upside down. (See exercises 326 – 334 .)

▸ 334. [ M32 ] Impossible structures can be built, if we insist only that they look genuine when viewed from the front (like façades in Hollywood movies)! Find all solutions to

images

that are visually correct. (To solve this exercise, you need to know that the illustrations here use the skew projection ( x, y, z ) ↦ (30 x − 42 y , 14 x + 10 y + 45 z ) u from three dimensions to two, where u is a scale factor.) All seven Soma pieces must be used.

335. [ 30 ] The earliest known example of a polycube puzzle is the “ Cube Diabolique,” manufactured in late nineteenth century France by Charles Watilliaux; it contains six flat pieces of sizes 2, 3, ..., 7:

images

  1. In how many ways do these pieces make a 3 × 3 × 3 cube?

  2. Are there six polycubes, of sizes 2, 3, ..., 7, that make a cube in just one way?

336. [ 21 ] ( The L-bert Hall .) Take two cubies and drill three holes through each of them; then glue them together and attach a solid cubie and dowel, as shown. Prove that there’s only one way to pack nine such pieces into a 3 × 3 × 3 box.

images

337. [ 29 ] (Angus Lavery, 1989.) Design a puzzle that consists of nine bent tricubes, whose face squares are either blank or colored with a red or green spot. The goal is to assemble the pieces into a 3 × 3 × 3 cube in two ways:

images

  1. No green spots are visible, and the red spots match a left-handed die.

  2. No red spots are visible, and the green spots match a right-handed die.

338. [ 22 ] Show that there are exactly eight different tetracubes — polycubes of size 4. Which of the following shapes can they make, respecting gravity? How many solutions are possible?

images

339. [ 25 ] How many of the 369 octominoes define a 4-level prism that can be realized by the tetracubes? Do any of those packing problems have a unique solution?

340. [ 30 ] There are 29 pentacubes , conveniently identified with one-letter codes:

images

images

Pieces o through z are called, not surprisingly, the solid pentominoes or flat pentacubes .

  1. What are the mirror images of a, b, c, d, e, f, A, B, C, D, E, F, j, k, l, ..., z?

  2. In how many ways can the solid pentominoes be packed into an a × b × c cuboid?

  3. What “natural” set of 25 pentacubes is able to fill the 5 × 5 × 5 cube?

▸ 341. [ 25 ] The full set of 29 pentacubes can build an enormous variety of elegant structures, including a particularly stunning example called “ Dowler’s Box.” This 7 × 7 × 5 container, first considered by R. W. M. Dowler in 1979, is constructed from five flat slabs. Yet only 12 of the pentacubes lie flat; the other 17 must somehow be worked into the edges and corners.

images

Despite these difficulties, Dowler’s Box has so many solutions that we can actually impose many further conditions on its construction:

  1. Build Dowler’s Box in such a way that the chiral pieces a, b, c, d, e, f and their images A, B, C, D, E, F all appear in horizontally mirror-symmetric positions.

    images

  2. Alternatively, build it so that those pairs are diagonally mirror-symmetric.

  3. Alternatively, place piece x in the center, and build the remaining structure from four congruent pieces that have seven pentacubes each.

342. [ 25 ] The 29 pentacubes can also be used to make the shape shown here, exploiting the curious fact that 3 4 +4 3 = 29 · 5. But Algorithm X will take a long, long time before telling us how to construct it, unless we’re lucky, because the space of possibilities is huge. How can we find a solution quickly?

images

343. [ 40 ] (T. Sillke, 1995.) For each of the twelve pentomino shapes, build the tallest possible tower whose walls are vertical and whose floors all have the given shape, using distinct pentacubes. Hint: Judicious factorization will give tremendous speedup.

344. [ 20 ] In how many distinct ways can a 5 × 5 × 5 cube by packed with 25 solid Y pentominoes? (See Fig. 73 .) Discuss how to remove the 48 symmetries of this problem.

345. [ 20 ] Pack twelve U-shaped dodecacubes into a 4 × 6 × 6 box without letting any two of them form a “ cross.”

images

346. [ M30 ] An ( l, m, n )- tri pod is a cluster of l + m + n + 1 cubies in which three “legs” of lengths l , m , and n are attached to a corner cubie, as in the (1,2,3)- tripod shown here. A “pod” is the special case where the tripod is ( l, m, n ) ∪ {( l ′, m, n ) | 0 ≤ l ′ < l } ∪ {( l, m ′, n ) | 0 ≤ m ′ < m } ∪ {( l, m, n ′) | 0 ≤ n ′ < n }.

images

  1. Prove that, for all m, n ≥ 0, shifted copies of nonoverlapping (1, m,n )-tripods are able to fill all of 3-dimensional space, without rotation or reflection. Hint: Pack N 2 of them into an N × N × N torus, where N = m + n +2.

  2. Show that 7/9 of 3-dimensional space can be packed with shifted (2, 2, 2)-tri pods.

  3. Similarly, at least 65/108 of 3D space can be packed with shifted (3, 3, 3)- tripods.

  4. Let r ( l, m, n ) be the maximum number of pods that can be packed in an l × m × n cuboid. Prove that at least (1+ l + m + n ) r ( l, m, n )/(4 lmn ) of 3-dimensional space can be packed with shifted ( l, m, n )-tripods.

  5. Use Algorithm M to evaluate r ( l, m, n ) for 4 ≤ l ≤ m ≤ n ≤ 6.

▸ 347. [ M21 ] (N. G. de Bruijn, 1961.) Prove that an l × m × n box can be completely filled with 1 × 1 × k bricks only if k is a divisor of l , m , or n . (Consequently, it can be completely filled with a × b × c bricks only if a , b , and c all satisfy this condition.)

348. [ M41 ] Find the maximum number of “canonical bricks” (1 × 2 × 4) that can be packed into an l × m × n box, leaving as few empty cells as possible.

▸ 349. [ M27 ] (D. Hoffman.) Show that 27 bricks of size a × b × c can always be packed into an s × s × s cube, where s = a + b + c . But if s /4 < a < b < c , 28 bricks won’t fit.

350. [ 22 ] Can 28 bricks of size 3 × 4 × 5 be packed into a 12 × 12 × 12 cube?

351. [ M46 ] Can 5 5 hypercuboids of size a × b × c × d × e always be packed into a 5-dimensional hypercube of size ( a + b + c + d + e ) ··· ( a + b + c + d + e )?

352. [ 21 ] In how many ways can the 12 pentominoes be packed into a 2 × 2×3 × 5 box?

353. [ 20 ] A weak polycube is a set of cubies that are loosely connected via common edges, not necessarily via common faces. In other words, we consider cubies to be adjacent when their centers are at most 2 units apart; up to 18 neighbors are possible. Find all the weak polycubes of size 3, and pack them into a symmetrical container.

▸ 354. [ 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 = 0. Each cell of S has 12 neighbors, obtained by adding 1 to one coordinate and subtracting 1 from another.

It’s instructive to view S in two different ways, by slicing it into plane layers that either have constant x + y + z (hence constant w ) or constant y + z (hence constant w + x ):

images

(Here images stands for ( w, x, y, z ).) If we include the layers above and below, we get

images

with each sphere nestling in the gap between the three or four spheres below it. In the “hex layers” on the left, ( w, x, y, z ) lies directly above ( w +3, x − 1, y − 1, z − 1), but doesn’t touch it; in the “quad layers” on the right, ( w, x, y, z ) lies directly above ( w +1, x +1, y − 1, z − 1), but doesn’t touch it.

  1. Show that every polyomino, and every polyhex, may be regarded as a polysphere:

    images

  2. Conversely, every planar polysphere looks like either a polyomino or a polyhex.

  3. Every polysphere {( w 1 , x 1 , y 1 , z 1 ), ..., ( w n , x n , y n , z n )} has a unique base placement { ( w 1 ′ , x 1 ′ , y 1 ′ , z 1 ′ ) , ... , ( w n ′ , x n ′ , y n ′ , z n ′ ) } obtained by subtracting ( w ′, x ′, y ′, z ′) from each ( w k , x k , y k , z k ), where x ′ = min{ x 1 , ..., x n }, y ′ = min{ y 1 , ..., y n }, z ′ = min{ z 1 , ..., z n }, and w ′ + x ′ + y ′ + z ′ = 0. Prove that x k ′ + y k ′ + z k ′ < n .

  4. As with polycubes, we say that polyspheres v and v ′ are equivalent if the base placement of v is also a base placement of some rotation of v ′ in three dimensions. (Reflections of “ chiral” polyspheres are not considered to be equivalent.) Formally speaking, a rotation of S about a line through the origin is an orthogonal 4 × 4 matrix that has determinant 1 and preserves w + x + y + z . Find such matrices for (i) rotation of the hex layers by 120°; (ii) rotation of the quad layers by 90°.

  5. A planar polysphere is equivalent to its reflection, because we can rotate by 180° around a line in its plane. Find suitable 4 × 4 matrices by which we can legally reflect polyspheres that are equivalent to (i) polyominoes; (ii) polyhexes.

  6. Prove that every rotation that takes a polysphere into another polysphere is obtainable as a product of the matrices exhibited in (d) and (e).

355. [ 25 ] The theory in exercise 354 allows us to represent polysphere cells with three integer coordinates xyz , because x , y , and z are nonnegative in base placements. The other variable, w , is redundant (but worth keeping in mind); it always equals − x − y − z .

  1. What’s a good way to find all the base placements of a given polysphere { x 1 y 1 z 1 , x 2 y 2 z 2 , ..., x n y n z n }? Hint: Use exercise 354 to tweak the method of exercise 324 .

  2. Any three points of three-dimensional space lie in a plane. So exercise 354(b) tells us that there are just four trispheres: a tromino, two trihexes, and one that’s both:

    images

    What are their base placements?

  3. According to exercise 354(c) , every base placement of a tetrasphere occurs in the SGB graph simplex (3, 3, 3, 3, 3, 0, 0). Use exercise 7.2.2–75 to find all of the four-element connected subsets of that graph, and identify all of the distinct tetraspheres. How many times does each tetrasphere occur in the graph?

356. [ 23 ] Polysphere puzzles often involve the construction of three kinds of shapes:

images

(An n × n roof or stretched roof is called an “ n -pyramid” or a “stretched n -pyramid.”)

  1. Define each of these configurations by specifying a suitable base placement.

  2. Each of the shapes illustrated is made from 20 spheres, and so is the stretched 4 × 3 roof. Find all multisets of five tetraspheres that suffice to make these shapes.

  3. The 4-pyramid and the stretched 4-pyramid involve 30 spheres. What multisets of ten trispheres are able to make them?

  4. The truncated octahedron , which represents all permutations of {1, 2, 3, 4}, is a noteworthy 24-cell subset of S (see exercise 5.1.1– 10). What multisets of six tetraspheres can build it?

357. [ M40 ] Investigate “ polysplatts,” which are the sets of truncated octahedra that can be built by pasting adjacent faces together (either square or hexagonal).

358. [ HM41 ] Investigate “ polyhexaspheres,” which are the connected sets of spheres in the hexagonal close packing . (This packing differs from that of exercise 354 because each sphere of a hexagonal layer is directly above a sphere that’s 2, not 3, layers below it.)

359. [ 29 ] Nick Baxter devised an innocuous-looking but maddeningly difficult “Square Dissection” puzzle for the International Puzzle Party in 2014, asking that the nine pieces

images

be placed flat into a 65 × 65 square. One quickly checks that 17 × 20+18 × 20+···+24 × 25 = 65 2 ; yet nothing seems to work! Solve his puzzle with the help of Algorithm X .

▸ 360. [ 20 ] The next group of exercises is devoted to the decomposition of rectangles into rectangles, as in the Mondrianesque pattern shown here. The reduction of such a pattern is obtained by distorting it, if necessary, so that it fits into an m × n grid, with each of the vertical coordinates {0, 1, ..., m } used in at least one horizontal boundary and each of the horizontal coordinates {0, 1, ..., n } used in at least one vertical boundary. For example, the illustrated pattern reduces to images , where m = 3 and n = 5. (Notice that the original rectangles needn’t have rational width or height.)

images

A pattern is called reduced if it is equal to its own reduction. Design an exact cover problem by which Algorithm M will discover all of the reduced decompositions of an m × n rectangle, given m and n . How many of them are possible when ( m, n ) = (3, 5)?

361. [ M25 ] The maximum number of subrectangles in a reduced m × n pattern is obviously mn . What is the minimum number?

362. [ 10 ] A reduced pattern is called strictly reduced if each of its subrectangles [ a..b )×[ c..d ) has ( a, b ) ≠ (0, m ) and ( c, d ) ≠ (0, n ) — in other words, if no subrectangle “cuts all the way across.” Modify the construction of exercise 360 so that it produces only strictly reduced solutions. How many 3 × 5 patterns are strictly reduced?

363. [ 20 ] A rectangle decomposition is called faultfree if it cannot be split into two or more rectangles. For example, images is not faultfree, because it has a fault line between rows 2 and 3. (It’s easy to see that every reduced faultfree pattern is strictly reduced, unless m = n = 1.) Modify the construction of exercise 360 so that it produces only faultfree solutions. How many reduced 3 × 5 patterns are faultfree?

364. [ 23 ] True or false: Every faultfree packing of an m × n rectangle by 1 × 3 trominoes is reduced, except in the trivial cases ( m, n ) = (1, 3) or (3, 1).

365. [ 22 ] ( Motley dissections. ) Many of the most interesting decompositions of an m × n rectangle involve strictly reduced patterns whose subrectangles [ a i .. b i )×[ c i .. d i ) satisfy the extra condition

( a i , b i ) ≠ ( a j , b j ) and ( c i , d i ) ≠ ( c j , d j ) when i ≠ j .

Thus no two subrectangles are cut off by the same pair of horizontal or vertical lines. The smallest such “motley dissections” are the 3 × 3 pinwheels, images and images , which are considered to be essentially the same because they are mirror images of each other. There are eight essentially distinct motley rectangles of size 4 × n , namely

images

The two 4 × 4s can each be drawn in 8 different ways, under rotations and reflections. Similarly, most of the 4 × 5s can be drawn in 4 different ways. But the last two have only two forms, because they’re symmetric under 180° rotation. (And the last two are actually equivalent, if we swap the two x coordinates in the middle.)

Design an exact cover problem by which Algorithm M will discover all of the motley dissections of an m × n rectangle, given m and n . (When m = n = 4 the algorithm should find 8 + 8 solutions; when m = 4 and n = 5 it should find 4 + 4 + 4 + 4 + 2 + 2.)

▸ 366. [ 25 ] Improve the construction of the previous exercise by taking advantage of symmetry to cut the number of solutions in half. (When m = 4 there will now be 4 + 4 solutions when n = 4, and 2+2+2+2+1+1 when n = 5.) Hint: A motley dissection is never identical to its left-right reflection, so we needn’t visit both.

367. [ 20 ] The order of a motley dissection is the number of subrectangles it has. There are no motley dissections of order six. Show, however, that there are m × m motley dissections of order 2 m −1 and m ×( m +1) motley dissections of order 2 m , for all m > 3.

368. [ M21 ] (H. Postl, 2017.) Show that an m × n motley dissection of order t can exist only if n < 2 t /3. Hint: Consider adjacent subrectangles.

369. [ 21 ] An m × n motley dissection must have order less than ( m + 1 2 ) , because only ( m + 1 2 ) − 1 intervals [ a i .. b i ) are permitted. What is the maximum order that’s actually achievable by an m × n motley dissection, for m = 5, 6, and 7?

▸ 370. [ 23 ] Explain how to generate all of the m × n motley dissections that have 180° rotational symmetry, as in the last two examples of exercise 365 , by modifying the construction of exercise 366 . (In other words, if [ a..b ) × [ c..d ) is a subrectangle of the dissection, its complement [ m − b..m − a ) × [ n − d..n − c ) must also be one of the subrectangles, possibly the same one.) How many such dissections have size 8 × 16?

371. [ 24 ] Further symmetry is possible when m = n (as in exercise 365 ’s pinwheel).

  1. Explain how to generate all of the n × n motley dissections that have 90°-rotational symmetry. This means that [ a..b ) × [ c..d ) implies [ c..d ) × [ n − b..n − a ).

  2. Explain how to generate all of the n × n motley dissections that are symmetric under reflection about both diagonals. This means that [ a..b ) × [ c..d ) implies [ c..d )×[ a..b ) and [ n − d..n − c )×[ n − b..n − a ), hence [ n − b..n − a )×[ n − d..n − c ).

  3. What’s the smallest n for which symmetric solutions of type (b) exist?

▸ 372. [ M35 ] ( Floorplans .) If a rectangle decomposition satisfies the tatami condition — “no four rectangles meet” — it’s often called a floorplan , and its subrectangles are called rooms . The line segments that delimit rooms are called bounds . Four possibilities arise when room r is adjacent to bound s : Either s ↓ r , r → s , r ↓ s , or s → r , meaning respectively that the top, right, bottom, or left boundary of r is part of s .

For example, the floorplans shown on the next page have 10 rooms {A, B, ..., J}, 7 + 6 bounds { h 0 , ..., h 6 , v 0 , ..., v 5 }, and the following adjacencies: h 0 ↓ A ↓ h 3 ↓ D ↓ h 5 ↓ E ↓ h 6 , h 0 ↓ B ↓ h 1 ↓ C ↓ h 3 ↓ F ↓ h 6 , h 1 ↓ G ↓ h 2 ↓ H ↓ h 4 ↓ I ↓ h 6 , h 2 ↓ J ↓ h 6 ; v 0 → A → v 1 → B → v 5 , v 1 → C → v 3 → H → v 4 , v 0 → D → v 2 → F → v 3 → G → v 5 , v 0 → E → v 2 , v 3 → I → v 4 → J → v 5 .

images

Two floorplans with the same adjacencies are considered to be equivalent. Thus, all four of the floorplans above are essentially the same, even though they look rather different: In particular, room C needn’t overlap room D; we require only C ↓ h 3 ↓ D.

  1. Let r ⇓ r ′ mean that r = r 0 ↓ s 0 ↓ r 1 ↓ ··· ↓ s k −1 ↓ r k \= r ′ for some k > 0; define r ⇒ r ′ similarly. Prove that [ r ⇓ r ′] + [ r ⇒ r ′] + [ r ′ ⇓ r ] + [ r ′ ⇒ r ] = 1, when r ≠ r ′. Hint: Every floorplan has unique diagonal and antidiagonal equivalents, as shown.

  2. A twintree is a data structure whose nodes x have four pointer fields, L0 ( x ), R0 ( x ), L1 ( x ), R1 ( x ). It defines two binary trees T 0 and T 1 on the nodes, where T θ is rooted at ROOT θ and has child links ( L θ , R θ ). These two trees satisfy inorder( T 0 ) = inorder( T 1 )= x 1 ... x n ; L0 ( x k ) = ∧ ⇔ L1 ( x k ) ≠ ∧, for 1 ≤ k < n . R0 ( x k ) = ∧ ⇔ R1 ( x k ) ≠ ∧, for 1 ≤ k < n .

    For each room r , if r ’s top left corner is a ⊤ junction, set L0 ( r ) ← ∧ and L1 ( r ) ← r ′, where r ′ is the room opposite r in that corner; otherwise reverse the roles of L0 and L1 . Similarly, set R0 ( r ) ← ∧ and R1 ( r ) ← r ′ if the bottom right corner of r is a ⊣ junction, or vice versa otherwise. (Use r ′ = ∧ at extreme corners.) Also set ROOT0 and ROOT1 to the bottom-left and top-right rooms. Show that a twintree is created, convenient for representing this floorplan.

373. [ 26 ] A “ perfectly decomposed rectangle” of order t is a faultfree dissection of a rectangle into t subrectangles [ a i .. b i ) × [ c i .. d i ) such that the 2 t dimensions b 1 − a 1 , d 1 − c 1 , ..., b t − a t , d t − c t are distinct. For example, five rectangles of sizes 1 × 2, 3 × 7, 4 × 6, 5 × 10, and 8 × 9 can be assembled to make the perfectly decomposed 13 × 13 square shown here. What are the smallest possible perfectly decomposed squares of orders 5, 6, 7, 8, 9, and 10, having integer dimensions?

images

374. [ M28 ] An “ incomparable dissection” of order t is a decomposition of a rectangle into t subrectangles, none of which will fit inside another. In other words, if the heights and widths of the subrectangles are respectively h 1 × w 1 , ..., h t × w t , we have neither ( h i ≤ h j and w i ≤ w j ) nor ( h i ≤ w j and w i ≤ h j ) when i ≠ j .

  1. True or false: An incomparable dissection is perfectly decomposed.

  2. True or false: The reduction of an incomparable dissection is motley.

  3. True or false: The reduction of an incomparable dissection can’t be a pinwheel.

  4. Prove that every incomparable dissection of order ≤ 7 reduces to the first 4 × 4 motley dissection in exercise 365 ; and its seven regions can be labeled as shown, with h 7 < h 6 < ··· < h 2 < h 1 and w 1 < w 2 < ··· < w 6 < w 7 .

    images

  5. Suppose the reduction of an incomparable dissection is m × n , and suppose its regions have been labeled {1, ..., t }. Then there are numbers x 1 , ..., x n , y 1 , ..., y m such that the widths are sums of the x ’s and the heights are sums of the y ’s. (For example, in (d) we have w 2 = x 1 , h 2 = y 1 + y 2 + y 3 , w 7 = x 2 + x 3 + x 4 , h 7 = y 1 , etc.) Prove that such a dissection exists with w 1 < w 2 < ··· < w t if and only if the linear inequalities w 1 < w 2 < ··· < w t have a positive solution ( x 1 , ..., x n ) and the linear inequalities h 1 > h 2 > ··· > h t have a positive solution ( y 1 , ..., y m ).

375. [ M29 ] Among all the incomparable dissections of order (a) seven and (b) eight, restricted to integer sizes, find the rectangles with the smallest possible semiperimeter (height plus width). Also find the smallest possible squares that have incomparable dissections in integers. Hint: Show that there are 2 t potential ways to mix the h ’s with the w ’s, preserving their order; and find the smallest semiperimeter for each of those cases.

▸ 376. [ M25 ] Find seven different rectangles of area 1/7 that can be assembled into a square of area 1, and prove that the answer is unique.

377. [ M28 ] Two rectangles of shapes h × w and h ′ × w ′ can be concatenated to form a larger rectangle of size ( h + h ′) × w if w = w ′, or of size h × ( w + w ′) if h = h ′.

  1. Given a set S of rectangle shapes, let ∧( S ) be the set of all shapes that can be made from the elements of S by repeated concatenation. Describe ∧({1 × 2, 3 × 1}).

  2. Find the smallest S ⊆ T such that T ⊆ ∧( S ), where T = { h × w | 1 < h < w }.

  3. What’s the smallest S with ∧( S ) = { h × w | h, w > 1 and hw mod 8 = 0}?

  4. Given m and n , solve (c) with ∧( S ) = { h × w | h, w > m and hw mod n = 0}.

▸ 378. [ M30 ] ( A finite basis theorem .) Continuing exercise 377 , prove that any set T of rectangular shapes contains a finite subset S such that T ⊆ ∧( S ).

▸ 379. [ 23 ] What h × w rectangles can be packed with copies of the Q pentomino? Hint: It suffices to find a finite basis for all such rectangles, using the previous exercise.

380. [ 35 ] Solve exercise 379 for the Y pentomino.

381. [ 20 ] Show that 3 n copies of the disconnected shape images can pack a 12 × n rectangle for all sufficiently large values of n .

▸ 382. [ 18 ] There’s a natural way to extend the idea of motley dissection to three dimensions, by subdividing an l × m × n cuboid into subcuboids [ a i .. b i ) × [ c i .. d i ) × [ e i .. f i ) that have no repeated intervals [ a i .. b i ) or [ c i .. d i ) or [ e i .. f i ).

For example, Scott Kim has discovered a remarkable motley 7 × 7 × 7 cube consisting of 23 individual blocks, 11 of which are illustrated here. (Two of them are hidden behind the others.) The full cube is obtained by suitably placing a mirror image of these pieces in front, together with a 1 × 1 × 1 cubie in the center.

images

By studying this picture, show that Kim’s construction can be defined by coordinate intervals [ a i .. b i )×[ c i .. d i )×[ e i .. f i ), with 0 ≤ a i , b i , c i , d i , e i , f i ≤ 7 for 1 ≤ i ≤ 23, in such a way that the pattern is symmetrical under the transformation x y z ⟼ y ¯ z ¯ x ¯ . In other words, if [ a..b ) × [ c..d ) × [ e..f ) is one of the subcuboids, so is [7 − d .. 7 − c ) × [7 − f .. 7 − e ) × [7 − b .. 7 − a ).

383. [ 29 ] Use exercise 382 to construct a perfectly decomposed 92 × 92 × 92 cube, consisting of 23 subcuboids that have 69 distinct integer dimensions. (See exercise 373 .)

384. [ 24 ] By generalizing exercises 365 and 366 , explain how to find every motley dissection of an l × m × n cuboid, using Algorithm M . Note: In three dimensions, the strictness condition ‘( a i ,b i ) ≠ (0, m ) and ( c i , d i ) ≠ (0, n )’ of exercise 362 should become

[ ( a i , b i ) \= ( 0 , l ) ] + [ ( c i , d i ) \= ( 0 , m ) ] + [ ( e i , f i ) \= ( 0 , n ) ] ≤ 1.

What are the results when l = m = n = 7?

385. [ M36 ] (H. Postl, 2017.) Arbitrarily large motley cuboids can be constructed by repeatedly nesting one motley cuboid within another (see answer 367 ). Say that a motley cuboid is primitive if it doesn’t contain a nested motley subcuboid.

Do primitive motley cuboids of size l × m × n exist only when l = m = n = 7?

▸ 386. [ M34 ] A polyomino can have eight different types of symmetry:

images

(Case (i) is often called 8-fold symmetry; case (iii) is often called central symmetry; case (vi) is often called left-right symmetry. Cases (ii), (iv), (v) are 4-fold symmetries; cases (ii) and (iii) are rotation symmetries; cases (iv)–(vii) are reflection symmetries.) In each case an n -omino of that symmetry type has been shown, where n is minimum.

How many symmetry types can a polyiamond or polyhex have? Give example n -iamonds and n -hexes of each type, where n is minimum.

▸ 387. [ M36 ] Continuing exercise 386 , how many symmetry types can a polycube have? Give an example of each type, using the minimum number of cubies. (Note that mirror reflection is not a legal symmetry for a polycube; L-twist ≠ R-twist!)

Exercises—Third Set

The following exercises are based on several intriguing logic puzzles that have recently become popular: futoshiki, kenken, masyu, slitherlink, kakuro, etc. Like sudoku, these puzzles typically involve a hidden pattern, for which only partial information has been revealed. The point of each exercise is usually to set up an appropriate exact cover problem, and to use it either to solve such a puzzle or to create new ones.

▸ 388. [ 21 ] The goal of a futoshiki puzzle is to deduce the entries of a secret latin square, given only two kinds of hints: A “ strong clue” is an explicit entry; a “ weak clue” is a greater-than relation between neighboring entries. The entries are the numbers 1 to n , where n is usually 5 as in the following examples:

images

Solve these puzzles by hand, using sudoku-like principles.

389. [ 20 ] Sketch a simple algorithm that finds simple lower and upper bounds for each entry that is part of a weak clue in a futoshiki puzzle, by repeatedly using the rule that a ≤ x < y ≤ b implies x ≤ b − 1 and y ≥ a + 1. (Your algorithm shouldn’t attempt to give the best possible bounds; that would solve the puzzle! But it should deduce the values of five entries in puzzle (a) of exercise 388 , as well as entry (4, 2) of puzzle (b).)

▸ 390. [ 21 ] Show that every futoshiki puzzle is a special case of an exact cover problem. In fact, show that every such puzzle can be formulated in at least two different ways:

  1. Use a pairwise ordering trick analogous to ( 25 ) or ( 26 ), to encode the weak clues.

  2. Use color controls to formulate an XCC problem suitable for Algorithm C .

391. [ 20 ] A futoshiki puzzle is said to be valid if it has exactly one solution. Use Algorithm X to generate all possible 5 × 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.

▸ 392. [ 25 ] There are 2 6 ( 40 6 ) \= 245656320 ways to construct a 5 × 5 futoshiki puzzle that has six weak clues and no strong ones. How many of them (a) are valid? (b) have no solutions? (c) have more than one solution? Also refine those counts, by considering how many such puzzles of types (a), (b), and (c) have at least one “long path” p < q < r < s < t (like the path that’s present in exercise 388(a) ). Give an example of each case.

393. [ 25 ] There are 5 6 ( 25 6 ) \= 2767187500 ways to construct a 5 × 5 futoshiki puzzle that has six strong clues and no weak ones. How many of them (a) are valid? (b) have no solutions? (c) have more than one solution? Give an example of each case.

394. [ 29 ] Show that every 5 × 5 futoshiki puzzle that has only five clues — strong, weak, or a mixture of both — has at least four solutions. Which puzzles attain this minimum?

395. [ 25 ] Continuing exercise 391 , find a 5 × 5 latin square that cannot be the solution to a valid futoshiki puzzle unless at least three strong clues have been given.

▸ 396. [ 25 ] Inspired by exercise 388(c) , construct a valid 9 × 9 futoshiki puzzle whose diagonal contains the strong clues ( 3, 1, 4, 1, 5, 9, 2, 6, 5 ) in that order. Every other clue should be a weak ‘<’—not a ‘>’, not a ‘∧’, not a ‘∨’.

▸ 397. [ 30 ] ( Save the sheep .) Given a grid in which some of the cells are occupied by sheep, the object of this puzzle is to construct a fence that keeps all the sheep on one side. The fence must begin and end at the edge of the grid, and it must follow the grid lines without visiting any point twice. Furthermore, exactly two edges of each sheep’s square should be part of the fence . For example, consider the following 5 × 5 grids:

images

The four sheep on the left can be “saved” only with the fence shown in the middle. Once you understand why, you’ll be ready to save the four sheep on the right.

  1. Explain how Algorithm C can help to solve puzzles like this, by showing that every solution satisfies a certain XCC problem. Hint: Imagine “coloring” each square with 0 or 1, with 1 indicating the cells on the sheep’s side of the fence.

  2. Devise an interesting 8 × 8 puzzle that has a unique solution and at most 10 sheep.

398. [ 23 ] ( KenKen ® .) A secret latin square whose entries are {1, 2, ..., n } can often be deduced by means of arithmetic. A kenken puzzle specifies the sum, difference, product, or quotient of the entries in some or all of its “ cages,” which are groups of cells indicated by heavy lines, as in the following examples:

images

(When the operation is ‘−’ or ‘÷’, the cage must have just two cells. A one-cell cage, without any operation, is a no-brainer, unless no clue has been specified for it.)

Cages look rather like the boxes of jigsaw sudoku (see ( 34 )); but in fact the rules are quite different: Two entries of the same cage can be equal, if they belong to different rows and different columns. For example, the ‘9×’ in (a) can be achieved only by multiplying the three entries {1, 3, 3}; hence there’s exactly one way to fill that cage.

Solve (a), (b), (c) by hand. Show that one of them is actually not a valid puzzle.

▸ 399. [ 23 ] How can all solutions to a kenken puzzle be obtained with Algorithm X ?

400. [ 21 ] Many clues of a kenken puzzle often turn out to be redundant, in the sense that the contents of one cage might be fully determined by the clues from other cages. For example, it turns out that any one of the clues in puzzle 398(a) could actually be omitted, without permitting a new solution.

Find all subsets of those 11 clues that suffice to determine a unique latin square.

401. [ 22 ] Find all 4 × 4 kenken puzzles whose unique solution is the latin square shown at the right, and whose cages all have two cells. Furthermore, there should be exactly two cages for each of the four operations +, −, ×, ÷.

1234 2143 4312 3421

402. [ 24 ] Solve this 12 × 12 kenken puzzle, using hexadecimal digits from 1 to C :

The five-cell cages of this puzzle have multiplicative clues, associated with the names of the twelve pentominoes:

O , 9240 × P , 5184 × Q , 3168 × R , 720 × S , 15840 × T , 19800 × U , 10560 × V , 4032 × W , 1620 × X , 5040 × Y , 576 × Z , 17248 ×

images

▸ 403. [ 31 ] Inspired by exercises 398(a) and 398(c) , construct a valid 9 × 9 kenken puzzle whose clues exactly match the decimal digits of π , for as many places as you can.

▸ 404. [ 25 ] ( Hidato ® .) A “hidato solution” is an m × n matrix whose entries are a permutation of {1, 2, ..., mn } for which the cells containing k and k + 1 are next to each other, either horizontally, vertically, or diagonally, for 1 ≤ k < mn . (In other words, it specifies a Hamiltonian path of king moves on an m × n board.) A “hidato puzzle” is a subset of those numbers, which uniquely determines the others; the solver is supposed to recreate the entire path from the given clues.

images

For example, consider the 4 × 4 puzzle (i). There’s only one place to put ‘2’. Then there are two choices for ‘4’; but one of them blocks the upper left corner (see (ii)), so we must choose the other. Similarly, ‘6’ must not block any corner. Therefore (iii) is forced; and it’s easy to fill in all of the remaining blanks, thereby obtaining solution (iv).

Explain how to encode such puzzles for solution by Algorithm C .

405. [ 21 ] The preceding exercise needs a subroutine to determine the endpoints of all simple paths of lengths 1, 2, ..., L from a given vertex v in a given graph. That problem is NP-hard; but sketch an algorithm that works well for small L in small graphs.

406. [ 16 ] Show that the following hidato puzzle isn’t as hard as it might look at first:

images

▸ 407. [ 20 ] Here’s a curious 4 × 8 array that is consistent with 52 hidato solutions:

images

Change it to a valid hidato puzzle, by adding one more clue.

408. [ 28 ] (N. Beluhov.) Construct 6 × 6 hidato puzzles that have (a) at most five clues; (b) at least eighteen clues, all of which are necessary.

▸ 409. [ 30 ] Can the first 10 clues of a 10 × 10 hidato puzzle be the first 20 digits of π ?

410. [ 22 ] ( Slitherlink .) Another addictive class of puzzles is based on finding closed paths or “ loops” in a given graph, when the allowable cycles must satisfy certain constraints. For instance, a slitherlink puzzle prescribes the number of loop edges that surround particular cells of a rectangular grid, as in diagram (i) below.

The first step in solving puzzle (i) is to note where the secret edges are definitely absent or definitely present. The 0s prohibit not only the edges immediately next to them but also a few more, because the path can’t enter a dead end. Conversely, the 3 forces the path to go through the upper left corner; we arrive at situation (ii):

images

Some experimentation now tells us which edge must go with the lower 1. We must not form two loops, as in (iii) or (iv). And hurrah: There’s a unique solution, (v).

Which of the following 5 × 5 slitherlink diagrams are valid puzzles? Solve them.

images

411. [ 20 ] True or false: A slitherlink diagram with a numeric clue given in every cell always has at most one solution. Hint: Consider the 2 × 2 case.

▸ 412. [ 22 ] A “ weak solution” to a slitherlink diagram is a set of edges that obeys the numeric constraints, and touches every vertex of the grid either twice or not at all; but it may form arbitrarily many loops. For example, the diagram of exercise 410(i) has six weak solutions, three of which are shown in 410(iii), (iv), and (v).

Show that there’s a nice way to obtain all the weak solutions of a given diagram, by formulating a suitable XCC problem. Hint: Think of the edges as constructed from tiles centered at the vertices, and use even/odd coordinates as in answer 133 .

▸ 413. [ 30 ] Explain how to modify Algorithm C so that the construction of exercise 412 will produce only the true “single-loop” solutions. Your modified algorithm shouldn’t be specific to slitherlink; it should apply also to masyu and other loop-discovery puzzles.

414. [ 25 ] The “strongest possible” answer to exercise 413 would cause the modified Algorithm C to backtrack as soon as the current choice of edge colors is incompatible with any single loop. Show that the algorithm in that answer is not as strong as possible, by examining its behavior on the puzzle at the right.

images

▸ 415. [ M33 ] Exactly 5·(2 25 −1) nonempty slitherlink diagrams of size 5 × 5 are “ homogeneous,” in the sense that all of their clues involve the same digit d ∈ {0, 1, 2, 3, 4}. (See exercise 410(a) –(d).) How many of them are valid puzzles? What are the minimum and maximum number of clues, for each d , in puzzles that contain no redundant clues?

416. [ M30 ] For each d ∈ {0, 1, 2, 3, 4}, construct valid n × n slitherlink diagrams whose nonblank clues are all equal to d , for infinitely many n .

417. [ M46 ] (N. Beluhov, 2018.) Exercise 410 (a, b, d) illustrates three homogeneous slitherlink puzzles that are valid for exactly the same pattern of nonblank clues. Do infinitely many such triples of square puzzles exist?

418. [ M29 ] An m × n slitherlink diagram is said to be symmetrical if cells ( i, j ) and ( m − 1 − i, n − 1 − j ) are both blank or both nonblank, for 0 ≤ i < m and 0 ≤ j < n . (Many grid-based puzzles obey this oft-unwritten rule.)

  1. There are exactly 6 25 ≈ 2.8 × 10 19 slitherlink diagrams of size 5 × 5, since each of the 25 cells can contain either ‘ 0 ’, ‘ 1 ’, ‘ 2 ’, ‘ 3 ’, ‘ 4 ’, or ‘ ’. How many of those diagrams are symmetrical?

  2. How many of the symmetric diagrams in (a) are valid puzzles?

  3. How many of those valid puzzles are minimal , in the sense that the deletion of nonblank clues in ( i, j ) and (4 − i , 4 − j ) would make the solution nonunique?

  4. What is the minimum number of clues in a valid 5 × 5 symmetrical puzzle?

  5. What is the maximum number of clues in a minimal 5 × 5 symmetrical puzzle?

419. [ 30 ] What surprise is concealed in the following symmetrical slitherlink puzzle?

images

420. [ M22 ] Consider an m × n slitherlink with m and n odd, having 2s in the pattern

images

(and possibly other clues). Show that there’s no solution if m mod 4 = n mod 4 = 1.

▸ 421. [ 20 ] ( Masyu .) A masyu (“evil influence”) puzzle, like slitherlink, conceals a hidden loop of straight segments. But there are two important differences. First, the loop passes through the centers of grid cells, instead of following the edges. Second, no numerical quantities are involved; the clues are entirely visual and geometrical.

Clues appear in circles through which the loop must pass: (i) The path must turn 90° at every black circle; but it must travel straight through the two neighboring cells just before and after turning. (ii) The path must not turn 90° when it goes through a white circle; and it must not travel straight through the two neighboring cells just before and after not turning. (Thus it must actually turn, at one or both of those cells. We get at least one turn per clue, and at least one straight segment.)

Consider, for example, a 5 × 5 puzzle with a black clue in cell 02, and with white clues in cells 13, 30, 32, and 43 as shown. The loop clearly will have to include the subpaths 20 — 30 — 40 — 41 and 42 — 43 — 44 — 34 in some order. It also must include either 00 — 01 — 02 — 12 — 22 or 04 — 03 — 02 — 12 — 22, because of the black clue. But the latter alternative is impossible, because it leaves no way to go straight through the white clue in 13. Thus 10 — 00 — 01 — 02 — 12 — 22 is forced; and also 23 — 13 — 03 — 04 — 14 — 24 — 34. (We couldn’t go 24 — 23, because that would close the loop prematurely.) The rest of the path now sort of falls into place.

images

Show that one of the clues in this example puzzle is actually redundant. But if any of the other four clues are absent, show that alternative solutions are possible.

422. [ 21 ] Show that the “weak solutions” to any given masyu puzzle are the solutions to an easily constructed XCC problem, by adapting the solution of exercise 412 .

▸ 423. [ M25 ] For each of the ( m −1) n + m ( n −1) potential edges e in the solution of an m × n masyu puzzle, let x e be the Boolean variable ‘[ e is present]’. The XCC problem constructed in exercise 422 is essentially a set of constraints on those variables.

Explain how to improve that construction dramatically, by exploiting the following special property that is enjoyed by masyu puzzles: Let N , S , E , and W be the edges leading out of a cell that holds a clue. If the clue is black, we have N = ~ S and E = ~ W ; if the clue is white, we have N = S , E = W , and E = ~ N . (Thus every clue reduces the number of independent variables by at least 2.)

▸ 424. [ 36 ] Make an exhaustive study of 6 × 6 masyu, and gather whatever statistics you think are particularly interesting. For example, how many of the 3 36 ≈ 1.5 × 10 17 ways to place white or black clues lead to a valid puzzle? Which of the valid puzzles have the fewest clues? the most clues? the shortest loops? the longest loops? only white clues? only black clues? How many of those puzzles are minimal , in the sense that none of their clues can be removed without allowing a new solution?

How many of the 2 36 ≈ 6.9 × 10 10 ways to occupy cells occur as the pattern of white clues in a valid puzzle? How many of them occur as the pattern of black clues? How many puzzles remain valid when white and black are interchanged? Which 6 × 6 masyu puzzle do you think is most difficult to solve?

425. [ 28 ] The solution to a masyu puzzle is composed of five kinds of “tiles”: images and blank. For example, the 3 × 3 solution shown here contains two tiles of each nonblank type.

images

Find 4 × 4, 5 × 5, and 6 × 6 puzzles whose unique solutions have exactly k tiles of each nonblank type, for every possible k .

▸ 426. [ 31 ] Obtain a valid masyu puzzle from diagram (i) below by changing each ‘ images ’ clue into either ‘ images ’ or ‘ images ’.

images

▸ 427. [ 25 ] Design a 25 × 25 masyu puzzle by adding white clues (only) to diagram (ii) above. All of your clues should preserve the 8-fold symmetry of this pattern.

428. [ M28 ] For infinitely many n , construct a valid n × n masyu puzzle with O ( n ) clues whose loop goes through all four corner cells, where all clues are (a) black; (b) white.

429. [ 21 ] A closed path on a triangular grid may have “ sharp turns,” which change the direction by 120°, or “ slack turns,” which change the direction by 60°, or both. Therefore triangular masyu has three flavors of clues: ‘ images ’ for the sharp turns, ‘ images ’ for the slack turns, and of course ‘ images ’ for the non-turns.

  1. Solve the following homogeneous triangular masyu puzzles:

    images

  2. The following patterns for triangular masyu are clearly impossible to solve. But show that each of them is solvable if the colors { images , images , images } are suitably permuted:

images

▸ 430. [ 26 ] ( Kakuro .) A kakuro puzzle is like a crossword puzzle, except that its “words” are blocks of two or more nonzero digits {1, 2, ..., 9}, not strings of letters. The digits of each block must be distinct, and their sum is given as a clue. Every cell to be filled belongs to exactly one horizontal block and one vertical block.

For example, the mini-kakuro shown here has just three horizontal blocks and three vertical blocks. Notice that the desired sums are indicated to the immediate left or above each block; thus the first horizontal block is supposed to be filled with two digits that sum to 5, so there are four possibilities: 14, 23, 32, 41. The first vertical block should sum to 6; again there are four possibilities, this time 15, 24, 42, 51 (because 33 is forbidden). The second horizontal block has three digits that should sum to 19; it is considerably less constrained. Indeed, there are thirty ways to obtain 19-in-three, namely the permutations of {2, 8, 9} or {3, 7, 9} or {4, 6, 9} or {4, 7, 8} or {5, 6, 8}.

images

  1. Solve the puzzle. Hint: There’s only one possibility for the lower right corner.

  2. Sketch a simple way to build a table of all suitable combinations of n -in- k , for 2 ≤ k ≤ 9 and 2 ≤ n ≤ 45. Which n and k have the most? Hint: Work bitwise.

  3. Generalized kakuro is a related puzzle, for which each block of length k has a specified set of combinations, chosen from among the ( 9 k ) possibilities (regardless of their sum). For example, suppose the three horizontal blocks of mini-kakuro must be filled respectively with permutations of {1, 3}, {3, 5}, or {5, 7}; {1, 3, 5}, {1, 7, 9}, {2, 4, 6}, {6, 8, 9}, or {7, 8, 9}; {2, 4}, {4, 6}, or {6, 8}; and require the same for the three vertical blocks. Find the unique solution to that puzzle.

  4. It would be easy to formulate kakuro as an XCC problem, as we did word squares in exercise 87 , by simply giving one option for each possible placement of a block. But the resulting problem might be gigantic: For example, long blocks are not uncommon in kakuro, and each 9-digit block would have 9! = 362,880 options(!). Show that generalized kakuro can be formulated efficiently as an XCC problem.

▸ 431. [ 30 ] The inventor of kakuro, Jacob E. Funk of Manitoba (who always called his puzzles “ Cross Sums”), published the following challenge on pages 50 and 66 of the August/September 1950 issue of Dell Official Crossword Puzzles :

images

Many ingenious constructions are present here; but unfortunately, he failed to realize that there is more than one solution. Find all solutions, and obtain a valid puzzle by repairing some of his original clues.

▸ 432. [ M25 ] We can’t simply design new kakuro puzzles by randomly filling the blanks and using the resulting sums as the constraints, because the vast majority of feasible sums yield nonunique solutions. Verify this experimentally for the generic diagrams a)

images

In each case determine the exact number of ways to fill the blanks, without repeated digits in any row or column, as well as exactly how many of those filled-in diagrams are uniquely reconstructible from their block sums. Consider also symmetry.

433. [ 26 ] Six of the sum-clues in this little kakuro diagram are unspecified:

images

In how many ways can you obtain valid puzzles by specifying them?

434. [ 30 ] Exactly how many kakuro diagrams are possible in a 9 × 9 grid? (Every row and every column should contain at least one block of empty cells, except that the topmost row and leftmost column are completely black. All blocks must have length ≥ 2. Empty cells needn’t be rookwise connected.) What is the maximum number of blocks?

435. [ 31 ] Design a rectangular kakuro puzzle for which the blocks at the top of the solution are 31, 41, 59, 26, 53, 58, 97 (the first fourteen digits of π ).

▸ 436. [ 20 ] ( Hitori .) Let’s wind up this potpourri of examples by considering a completely different combinatorial challenge. A hitori puzzle (“alone”) is an m × n array in which we’re supposed to cross elements out until three conditions are achieved:

  1. No row or column contains repeated elements.

  2. Adjacent elements cannot be crossed out.

  3. The remaining elements are rookwise connected.

For example, consider the 4 × 5 word rectangle ( α ). Conditions (i) and (ii) can be satisfied in sixteen ways, such as ( β ) and ( γ ). But only ( δ ) satisfies also (iii).

images

A crossed-out cell is said to be black; the other cells are white. While solving a hitori, it’s helpful to circle an entry that is certain to become white. We can initially circle all the “ seeds” — the entries that don’t match any others in their row or column.

For example, puzzle ( α ) has eight seeds. If we decide to blacken a cell, we immediately circle its neighbors (because they cannot also be black). Thus, for instance, we shouldn’t cross out the E in cell (2, 4): That would circle the L in (2, 3), forcing the other L to be black and cutting off the corner E as in ( β ).

images

The precise value of a seed is immaterial to the puzzle; it can be replaced by any other symbol that differs from everything else in its row or column.

We say as usual that a hitori puzzle is valid if it has exactly one solution. Explain why (a) a valid hitori puzzle has exactly one solution with all seeds white; (b) a hitori puzzle that has a unique solution with all seeds white is valid if and only if all the seed cells not adjacent to black in that solution are “ articulation points” for the set of white cells — that is, their removal would disconnect the whites. (See (3, 1) and (3, 2) in ( δ ).)

▸ 437. [ 21 ] A weak solution to a hitori puzzle is a solution for which all seeds are white, and for which properties (i) and (ii) of exercise 436 hold. Given a hitori puzzle, define an XCC problem whose solutions are precisely its weak solutions.

438. [ 30 ] Explain how to modify Algorithm C so that, when given an XCC problem from the construction in answer 437 , it will produce only solutions that satisfy also the connectivity condition (iii). Hint: See exercise 413 ; also consider reachability.

439. [ M20 ] Let G be a graph on the vertices V . A hitori cover of G is a set U ⊆ V such that (i) G | U is connected; (ii) if v /∈ U and u — v then u ∈ U ; (iii) if u ∈ U and if v ∈ U for all u — v , then G | ( U \ u ) is not connected.

  1. Describe a hitori cover in terms of standard graph theory terminology.

  2. Show that the solution of a valid hitori puzzle is a hitori cover of P m □ P n .

440. [ 21 ] True or false: If the letter A occurs exactly twice in the top row of a valid hitori puzzle, exactly one of those occurrences will survive in the solution.

441. [ 18 ] Describe every valid hitori puzzle of size 1 × n on a d -letter alphabet.

▸ 442. [ M33 ] Enumerate all hitori covers of P m □ P n , for 1 ≤ m ≤ n ≤ 9.

▸ 443. [ M30 ] Prove that an m × n hitori cover has at most ( mn + 2)/3 black cells.

444. [ M27 ] Can a valid n × n hitori puzzle involve fewer than 2 n /3 distinct elements? Construct a valid puzzle of size 3 k × 3 k , using only the elements {0, 1, ..., 2 k }.

▸ 445. [ M22 ] It’s surprisingly difficult to construct a valid hitori puzzle that has no seeds. In fact, there are no n × n examples for n ≤ 9 except when n = 6. But it turns out that quite a few seedless 6 × 6 hitori puzzles do exist.

Consider the five hitori covers below. Determine, for each of them, the exact number of valid hitori puzzles with no seeds, having that pattern of white and black cells as the solution. Hint: In some cases the answer is zero.

images

▸ 446. [ 24 ] The digits of e , 2.718281828459045..., are well known to have a curious repeating pattern. In fact, the first 25 digits actually define a valid 5 × 5 hitori puzzle! What is the probability that a random 5 × 5 array of decimal digits will have that property? And what about octal digits? Hexadecimal digits?

447. [ 22 ] (Johan de Ruiter.) Are there any values of m > 1 and n > 1 for which the first mn digits of π define a valid m × n hitori puzzle?

448. [ 22 ] Do any of the 31344 double word squares formed from WORDS (3000) make valid hitori puzzles? (See exercise 87 .)

449. [ 40 ] ( Hidden nuggets .) Johan de Ruiter noticed in 2017 that George Orwell had included a valid hitori puzzle in his novel Nineteen Eighty-Four (part 2, chapter 9):

images

Did Homer, Shakespeare, Tolstoy, and others also create hitori puzzles accidentally?

450. [ 22 ] Use Algorithm X to solve the “ tot tibi sunt dotes” problem of Section 7.2.1.7.

We should “play up” the role of play.

— FRANCIS EDWARD SU , “Mathematics for Human Flourishing” (2017)

He reaps no satisfaction but from low and sensual objects,

or from the indulgence of malignant passions .

— DAVID HUME, The Sceptic (1742)

I can’t get no . . .

— MICK JAGGER and KEITH RICHARDS, Satisfaction (1965)

7.2.2.2. Satisfiability

We turn now to one of the most fundamental problems of computer science: Given a Boolean formula F ( x 1 , ..., x n ), expressed in so-called “ conjunctive normal form” as an AND of ORs, can we “satisfy” F by assigning values to its variables in such a way that F ( x 1 , ..., x n ) = 1? For example, the formula

F ( x 1 , x 2 , x 3 ) \= ( x 1 ∨ x ¯ 2 ) ∧ ( x 2 ∨ x 3 ) ∧ ( x ¯ 1 ∨ x ¯ 3 ) ∧ ( x ¯ 1 ∨ x ¯ 2 ∨ x ¯ 3 ) ( 1 )

is satisfied when x 1 x 2 x 3 = 001. But if we rule that solution out, by defining

G ( x 1 , x 2 , x 3 ) \= F ( x 1 , x 2 , x 3 ) ∧ ( x 1 ∨ x 2 ∨ x ¯ 3 ) , ( 2 )

then G is unsatisfiable: It has no satisfying assignment.

Section 7.1.1 discussed the embarrassing fact that nobody has ever been able to come up with an efficient algorithm to solve the general satisfiability problem, in the sense that the satisfiability of any given formula of size N could be decided in N O (1) steps. Indeed, the famous unsolved question “does P = NP?” is equivalent to asking whether such an algorithm exists. We will see in Section 7.9 that satisfiability is a natural progenitor of every NP-complete problem. *

* At the present time very few people believe that P = NP [see SIGACT News 43 , 2 (June 2012), 53–77]. In other words, almost everybody who has studied the subject thinks that satisfiability cannot be decided in polynomial time. The author of this book, however, suspects that N O(1) -step algorithms do exist, yet that they’re unknowable. Almost all polynomial time algorithms are so complicated that they lie beyond human comprehension, and could never be programmed for an actual computer in the real world. Existence is different from embodiment.

On the other hand enormous technical breakthroughs in recent years have led to amazingly good ways to approach the satisfiability problem. We now have algorithms that are much more efficient than anyone had dared to believe possible before the year 2000. These so-called “ SAT solvers” are able to handle industrial-strength problems, involving millions of variables, with relative ease, and they’ve had a profound impact on many areas of research and development such as computer-aided verification. In this section we shall study the principles that underlie modern SAT-solving procedures.

To begin, let’s define the problem carefully and simplify the notation, so that our discussion will be as efficient as the algorithms that we’ll be considering. Throughout this section we shall deal with variables, which are elements of any convenient set. Variables are often denoted by x 1 , x 2 , x 3 , ..., as in ( 1 ); but any other symbols can also be used, like a, b, c , or even d 74 " ′ . We will in fact often use the numerals 1, 2, 3, ... to stand for variables; and in many cases we’ll find it convenient to write just j instead of x j , because it takes less time and less space if we don’t have to write so many x ’s. Thus ‘2’ and ‘ x 2 ’ will mean the same thing in many of the discussions below.

A literal is either a variable or the complement of a variable. In other words, if v is a variable, both v and v ¯ are literals. If there are n possible variables in some problem, there are 2 n possible literals. If l is the literal x ¯ 2 , which is also written 2 ¯ , then the complement of l , denoted by l ¯ , is x 2 , which is also written 2.

The variable that corresponds to a literal l is denoted by | l |; thus we have | v | \= | v ¯ | \= v for every variable v . Sometimes we write ± v for a literal that is either v or v ¯ . We might also denote such a literal by σ v , where σ is ±1. The literal l is called positive if | l | = l; otherwise | l | \= l ¯ , and l is said to be negative .

Two literals l and l ′ are distinct if l ≠ l ′. They are strictly distinct if | l | ≠ | l ′|. A set of literals { l 1 , ..., l k } is strictly distinct if | l i | ≠ | l j | for 1 ≤ i < j ≤ k .

The satisfiability problem, like all good problems, can be understood in many equivalent ways, and we will find it convenient to switch from one viewpoint to another as we deal with different aspects of the problem. Example ( 1 )isan AND of clauses, where every clause is an OR of literals; but we might as well regard every clause as simply a set of literals, and a formula as a set of clauses. With that simplification, and with ‘ x j ’ identical to ‘ j ’, Eq. ( 1 ) becomes

F \= { { 1 , 2 ¯ } , { 2 , 3 } , { 1 ¯ , 3 ¯ } , { 1 ¯ , 2 ¯ , 3 } } .

And we needn’t bother to represent the clauses with braces and commas either; we can simply write out the literals of each clause. With that shorthand we’re able to perceive the real essence of ( 1 ) and ( 2 ):

F \= { 1 2 ¯ , 23 , 1 ¯ 3 ¯ , 1 ¯ 2 ¯ 3 } , G \= F ∪ { 12 3 ¯ } . ( 3 )

Here F is a set of four clauses, and G is a set of five.

In this guise, the satisfiability problem is equivalent to a covering problem, analogous to the exact cover problems that we considered in Section 7.2.2.1 : Let

T n \= { { x 1 , x ¯ 1 } , { x 2 , x ¯ 2 } , ... , { x n , x ¯ n } } \= { 1 1 ¯ , 2 2 ¯ , ... , n n ¯ } . ( 4 )

“Given a set F = { C 1 , ..., C m }, where each C i is a clause and each clause consists of literals based on the variables { x 1 , ..., x n }, find a set L of n literals that ‘covers’ F ∪ T n , in the sense that every clause contains at least one element of L .” For example, the set F in ( 3 ) is covered by L \= { 1 ¯ , 2 ¯ , 3 } , and so is the set T 3 ; hence F is satisfiable. The set G is covered by { 1 , 1 ¯ , 2 } or { 1 , 1 ¯ , 3 } or ... or { 2 ¯ , 3 , 3 ¯ } , but not by any three literals that also cover T 3 ; so G is unsatisfiable.

Similarly, a family F of clauses is satisfiable if and only if it can be covered by a set L of strictly distinct literals.

If F ′ is any formula obtained from F by complementing one or more variables, it’s clear that F ′ is satisfiable if and only if F is satisfiable. For example, if we replace 1 by 1 ¯ and 2 by 2 ¯ in ( 3 ) we obtain

F ′ \= { 1 ¯ 2 , 2 ¯ 3 , 1 3 ¯ , 123 } , G ′ \= F ′ ∪ { 1 ¯ 2 ¯ 3 ¯ } .

In this case F ′ is trivially satisfiable, because each of its clauses contains a positive literal: Every such formula is satisfied by simply letting L be the set of positive literals. Thus the satisfiability problem is the same as the problem of switching signs (or “ polarities”) so that no all-negative clauses remain.

Another problem equivalent to satisfiability is obtained by going back to the Boolean interpretation in ( 1 ) and complementing both sides of the equation. By De Morgan’s laws 7.1.1–(11) and (12) we have

F ¯ ( x 1 , x 2 , x 3 ) \= ( x ¯ 1 ∧ x 2 ) ∨ ( x ¯ 2 ∧ x ¯ 3 ) ∨ ( x 1 ∨ x 3 ) ∨ ( x 1 ∧ x 2 ∧ x ¯ 3 ) ; ( 5 )

and F is unsatisfiable ⇔ F \= 0 ⇔ F ¯ \= 1 \= ⇔ F ¯ is a tautology. Consequently F is satisfiable if and only if F ¯ is not a tautology: The tautology problem and the satisfiability problem are essentially the same. *

* Strictly speaking, TAUT is coNP-complete, while SAT is NP-complete; see Section 7.9.

Since the satisfiability problem is so important, we simply call it SAT. And instances of the problem such as ( 1 ), in which there are no clauses of length greater than 3, are called 3SAT. In general, k SAT is the satisfiability problem restricted to instances where no clause has more than k literals.

Clauses of length 1 are called unit clauses , or unary clauses. Binary clauses, similarly, have length 2; then come ternary clauses, quaternary clauses, and so forth. Going the other way, the empty clause , or nullary clause, has length 0 and is denoted by ; it is always unsatisfiable. Short clauses are very important in algorithms for SAT, because they are easier to deal with than long clauses. But long clauses aren’t necessarily bad; they’re much easier to satisfy than the short ones.

A slight technicality arises when we consider clause length: The binary clause ( x 1 ∨ x ¯ 2 ) in ( 1 ) is equivalent to the ternary clause ( x 1 ∨ x 1 ⁢   ∨ x ¯ 2 ) as well as to ( x 1 ∨ x ¯ 2 ⁢   ∨ x ¯ 2 ) and to longer clauses such as ( x 1 ∨ x 1 ⁢   ∨ x 1 ∨ x ¯ 2 ) ; so we can regard it as a clause of any length ≥2. But when we think of clauses as sets of literals rather than ORs of literals, we usually rule out multisets such as 11 2 ¯ or 1 2 ¯ 2 ¯ that aren’t sets; in that sense a binary clause is not a special case of a ternary clause. On the other hand, every binary clause ( x ∨ y ) can be replaced by two ternary clauses, ( x ∨ y ∨ z ) ∧ ( x ∨ y ∨ z ¯ ) , if z is another variable; and every k -ary clause is equivalent to the AND of two ( k + 1)-ary clauses. Therefore we can assume, if we like, that k SAT deals only with “ k -clauses” whose length is exactly k .

A clause is tautological (always satisfied) if it contains both v and v ¯ for some variable v . Tautological clauses can be denoted by ℘ (see exercise 7.1.4–222). They never affect a satisfiability problem; so we usually assume that the clauses input to a SAT-solving algorithm consist of strictly distinct literals.

When we discussed the 3SAT problem briefly in Section 7.1.1, we took a look at formula 7.1.1–(32), “the shortest interesting formula in 3CNF.” In our new shorthand, it consists of the following eight unsatisfiable clauses:

R \= { 12 3 ¯ , 23 4 ¯ , 341 , 4 1 ¯ 2 , 1 ¯ 2 ¯ 3 , 2 ¯ 3 ¯ 4 , 3 ¯ 4 ¯ 1 ¯ , 4 ¯ 1 2 ¯ } . ( 6 )

This set makes an excellent little test case, so we will refer to it frequently below. (The letter R reminds us that it is based on R. L. Rivest’s associative block design 6.5–(13).) The first seven clauses of R, namely

R ′ \= { 12 3 ¯ , 23 4 ¯ , 341 , 4 1 ¯ 2 , 1 ¯ 2 ¯ 3 , 2 ¯ 3 ¯ 4 , 3 ¯ 4 ¯ 1 ¯ } , ( 7 )

also make nice test data; they are satisfied only by choosing the complements of the literals in the omitted clause, namely { 4 , 1 ¯ , 2 } . More precisely, the literals 1 ¯ , and 2 are necessary and sufficient to cover R ′; we can also include either 3 or 3 ¯ in the solution. Notice that ( 6 ) is symmetric under the cyclic permutation 1 → 2 → 3 → 4 → 1 ¯ → 2 ¯ → 3 ¯ → 4 ¯ → 1 of literals. Thus, omitting any clause of ( 6 ) gives a satisfiability problem equivalent to ( 7 ).

A simple example

SAT solvers are important because an enormous variety of problems can readily be formulated Booleanwise as ANDs of ORs. Let’s begin with a little puzzle that leads to an instructive family of example problems: Find a binary sequence x 1 ... x 8 that has no three equally spaced 0 s and no three equally spaced 1 s . For example, the sequence 01001011 almost works; but it doesn’t qualify, because x 2 , x 5 , and x 8 are equally spaced 1s.

If we try to solve this puzzle by backtracking manually through all 8-bit sequences in lexicographic order, we see that x 1 x 2 = 00 forces x 3 = 1. Then x 1 x 2 x 3 x 4 x 5 x 6 x 7 = 0010011 leaves us with no choice for x 8 . A minute or two of further hand calculation reveals that the puzzle has just six solutions, namely

00110011 , 01011010 , 01100110 , 10011001 , 10100101 , 11001100. ( 8 )

Furthermore it’s easy to see that none of those solutions can be extended to a suitable binary sequence of length 9. We conclude that every binary sequence x 1 ... x 9 contains three equally spaced 0 s or three equally spaced 1 s.

Notice now that the condition x 2 x 5 x 8 ≠ 111 is the same as the Boolean clause ( x ¯ 2 ∨ x ¯ 5 ∨ x ¯ 8 ) , namely 2 ¯ 5 ¯ 8 ¯ . Similarly x 2 x 5 x 8 ≠ 000 is the same as 258. So we have just verified that the following 32 clauses are unsatisfiable:

123 , 234 , ... , 789 , 135 , 246 , ... , 579 , 147 , 258 , 369 , 159 1 ¯ 2 ¯ 3 ¯ , 2 ¯ 3 ¯ 4 ¯ , ... , 7 ¯ 8 ¯ 9 ¯ , 1 ¯ 3 ¯ 5 ¯ , 2 ¯ 4 ¯ 6 ¯ , ... , 5 ¯ 7 ¯ 9 ¯ , 1 ¯ 4 ¯ 7 ¯ , 2 ¯ 5 ¯ 8 ¯ , 3 ¯ 6 ¯ 9 ¯ , 1 ¯ 5 ¯ 9 ¯ . ( 9 )

This result is a special case of a general fact that holds for any given positive integers j and k : If _n is sufficiently large, every binary sequence x 1 ... _x n contains either j equally spaced 0 s or k equally spaced 1 s (or both). The smallest such n is denoted by W (j, k) in honor of B. L. van der Waerden, who proved an even more general result (see exercise 2.3.4.3–6): If n is sufficiently large, and if k 0 , ..., k b −1 are positive integers, every b-ary sequence x 1 ... x n contains k a equally spaced a ’ s for some digit a , 0 ≤ a < b . The least such n is W ( k 0 , ..., k b − 1 ).

Let us accordingly define the following set of clauses when j, _k , n_ > 0:

w a e r d e n ( j , k ; n ) \= { ( x i ∨ x i + d ∨ ... ∨ x i + ( j − 1 ) d ) | 1 ≤ i ≤ n − ( j − 1 ) d , d ≥ 1 } ∪ { x ¯ i ∨ x ¯ i + d ∨ ... ∨ x ¯ i + ( k − 1 ) d | 1 ≤ i ≤ n − ( k − 1 ) d , d ≥ 1 } . ( 10 )

The 32 clauses in ( 9 ) are waerden (3, 3; 9); and in general waerden ( j, k; n ) is an appealing instance of SAT, satisfiable if and only if n < W ( j , k ).

It’s obvious that W (1, k ) = k and W (2, k )= 2 k − [ k even]; but when j and k exceed 2 the numbers W(j, k) are quite mysterious. We’ve seen that W (3, 3) = 9, and the following nontrivial values are currently known:

k \= 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 W ( 3 , k ) \= 9 18 22 32 46 58 77 97 114 135 160 186 218 238 279 312 349 W ( 4 , k ) \= 18 35 55 73 109 146 309 ? ? ? ? ? ? ? ? ? ? W ( 5 , k ) \= 22 55 178 206 260 ? ? ? ? ? ? ? ? ? ? ? ? W ( 6 , k ) \= 32 73 206 1132 ? ? ? ? ? ? ? ? ? ? ? ? ?

V. Chvátal inaugurated the study of W(j, k) by computing the values for j+k ≤ 9 as well as W (3, 7) [ Combinatorial Structures and Their Applications (1970), 31– 33]. Most of the large values in this table have been calculated by state-of-the-art SAT solvers [see M. Kouril and J. L. Paul, Experimental Math. 17 (2008), 53– 61; M. Kouril, Integers 12 (2012), A46:1–A46:13]. The table entries for j = 3 suggest that we might have W (3, k ) < k 2 when k > 4, but that isn’t true: SAT solvers have also been used to establish the lower bounds

k \= 20 21 22 23 24 25 26 27 28 29 30 W ( 3 , k ) ≥ 389 416 464 516 593 656 727 770 827 868 903

(which might in fact be the true values for this range of k ); see T. Ahmed, O. Kullmann, and H. Snevily [ Discrete Applied Math. 174 (2014), 27–51].

Notice that the literals in every clause of waerden ( j, k; n ) have the same sign: They’re either all positive or all negative. Does this “monotonic” property make the SAT problem any easier? Unfortunately, no: Exercise 10 proves that any set of clauses can be converted to an equivalent set of monotonic clauses.

Exact covering

The exact cover problems that we solved with “ dancing links” in Section 7.2.2.1 can easily be reformulated as instances of SAT and handed off to SAT solvers. For example, let’s look again at Langford pairs, the task of placing two 1s, two 2s, ..., two n ’s into 2 n slots so that exactly k slots intervene between the two appearances of k , for each k . The corresponding exact cover problem when n = 3 has nine items and eight options ( see 7.2.2.1 – (17) ):

′ d 1 s 1 s 3 ′ , ′ d 1 s 2 s 4 ′ ′ d 1 s 3 s 5 ′ ′ d 1 s 4 s 6 ′ ′ d 2 s 1 s 4 ′ ′ d 2 s 2 s 5 ′ ′ d 2 s 3 s 6 ′ ′ d 3 s 1 s 5 ′ . ( 11 )

The items are d i for 1 ≤ i ≤ 3 and s j for 1 ≤ j ≤ 6; the option ‘ d i s j s k ’ means that digit i is placed in slots j and k . Left-right symmetry allows us to omit the option ‘ d 3 s 2 s 6 ’ from this specification.

We want to select options of ( 11 ) so that each item appears just once. Let the Boolean variable x j mean ‘select option j’, for 1 ≤ j ≤ 8; the problem is then to satisfy the nine constraints

S 1 ( x 1 , x 2 , x 3 , x 4 ) ∧ S 1 ( x 5 , x 6 , x 7 ) ∧ S 1 ( x 8 ) ∧ S 1 ( x 1 , x 5 , x 8 ) ∧ S 1 ( x 2 , x 6 ) ∧ S 1 ( x 1 , x 3 , x 7 ) ∧ S 1 ( x 2 , x 4 , x 5 ) ∧ S 1 ( x 3 , x 6 , x 8 ) ∧ S 1 ( x 4 , x 7 ) , ( 12 )

one for each item. (Here, as usual, S 1 ( y 1 , ..., y p ) denotes the symmetric function [ y 1 + ... + y p = 1].) For example, we must have x 5 + x 6 + x 7 = 1, because item d 2 appears in options 5, 6, and 7 of ( 11 ).

One of the simplest ways to express the symmetric Boolean function S 1 as an AND of ORs is to use 1 + ( p 2 ) clauses:

S 1 ( y 1 , ... , y p ) \= ( y 1 ∨ ... ∨ y p ) ∧ ∧ ( y ¯ j ∨ y ¯ k ) . ( 13 )

“At least one of the y ’s is true, but not two.” Then ( 12 ) becomes, in shorthand,

{ 1234 , 1 ¯ 2 ¯ , 1 ¯ 3 , 1 ¯ 4 ¯ , 2 ¯ 3 ¯ , 2 ¯ 4 ¯ , 3 ¯ 4 ¯ , 567 , 5 ¯ 6 ¯ , 5 ¯ 7 ¯ , 6 ¯ 7 ¯ , 8 , 158 , 1 ¯ 5 ¯ , 1 ¯ 8 ¯ , 5 ¯ 8 ¯ , 26 , 2 6 ¯ , 137 , 1 ¯ 3 ¯ , 1 ¯ 7 ¯ , 3 ¯ 7 ¯ , ⁢ 245 , 2 ¯ 4 ¯ , 2 ¯ 5 ¯ , 4 ¯ 5 ¯ , 368 , 3 ¯ 6 ¯ , 3 ¯ 8 ¯ , 6 ¯ 8 ¯ , 47 , 4 ¯ 7 ¯ } ; ( 14 )

we shall call these clauses langford ( 3 ). (Notice that only 30 of them are actually distinct, because 1 ¯ 3 ¯ and 2 ¯ 4 ¯ appear twice.) Exercise 13 defines langford ( n ); we know from exercise 7 – 1 that langford ( n ) is satisfiable ⇔ n mod 4 = 0 or 3.

The unary clause 8 in ( 14 ) tells us immediately that x 8 = 1. Then from the binary clauses 1 ¯ 8 ¯ , 5 ¯ 8 ¯ , 3 ¯ 8 ¯ , 6 ¯ 8 ¯ we have x 1 = x 5 = x 3 = x 6 = 0. The ternary clause 137 then implies x 7 = 1; finally x 4 = 0 (from 4 ¯ 7 ¯ ) and x 2 = 1 (from 1234). Options 8, 7, and 2 of ( 11 ) now give us the desired Langford pairing 312132.

Incidentally, the function S 1 ( y 1 , y 2 , y 3 , y 4 , y 5 ) can also be expressed as

( y 1 ∨ y 2 ∨ y 3 ∨ y 4 ∨ y 5 ) ∧ ( y ¯ 1 ∨ y ¯ 2 ) ∧ ( y ¯ 1 ∨ y ¯ 3 ) ∧ ( y ¯ 1 ∨ t ¯ ) ∧ ( y ¯ 2 ∨ y ¯ 3 ) ∧ ( y ¯ 2 ∨ t ¯ ) ∧ ( y ¯ 3 ∨ t ¯ ) ∧ ( t ∨ y ¯ 4 ) ∧ ( t ∨ y ¯ 5 ) ∧ ( y ¯ 4 ∨ y ¯ 5 ) ,

where t is a new variable. In general, if p gets big, it’s possible to express S 1 ( y 1 , ..., y p ) with only 3 p −5 clauses instead of ( p 2 ) + 1 , by using ⌊ ( p − 3 ) / 2 ⌋ new variables as explained in exercise 12 . When this alternative encoding is used to represent Langford pairs of order n , we’ll call the resulting clauses langford ( n ).

Do SAT solvers do a better job with the clauses langford ( n ) or langford ( n )? Stay tuned: We’ll find out later.

Coloring a graph

The classical problem of coloring a graph with at most d colors is another rich source of benchmark examples for SAT solvers. If the graph has n vertices V , we can introduce nd variables v j , for v ∊ V and 1 ≤ j ≤ d , signifying that v has color j ; the resulting clauses are quite simple:

( v 1 ∨ v 2 ∨ ... ∨ v d )   for  v ∈ V ⁢     ( “ every vertex has at least one color” ) ; ( 15 )

( u ¯ j ∨ v ¯ j )   for  u — v , 1 ≤ j ≤ d ⁢     ( “ adjacent vertices have different colors” ) . ( 16 )

We could also add n ( d 2 ) additional so-called exclusion clauses

( v ¯ i ∨ v ¯ j )   for  v ∈ V , 1 ≤ i < j ≤ d ⁢     ( “ every vertex has at most one color” ) ; ( 17 )

but they’re optional, because vertices with more than one color are harmless. Indeed, if we find a solution with v 1 = v 2 = 1, we’ll be extra happy, because it gives us two legal ways to color vertex v. (See exercise 14 .)

images

Fig. 76. The McGregor graph of order 10. Each region of this “map” is identified by a two-digit hexadecimal code. Can you color the regions with four colors, never using the same color for two adjacent regions?

Martin Gardner astonished the world in 1975 when he reported [ Scientific American 232 , 4 (April 1975), 126–130] that a proper coloring of the planar map in Fig. 76 requires five distinct colors, thereby disproving the longstanding four-color conjecture. (In that same column he also cited several other “facts” supposedly discovered in 1974: (i) e π 163 is an integer; (ii) pawn-to-king-rook-4 (‘h4’) is a winning first move in chess; (iii) the theory of special relativity is fatally flawed; (iv) Leonardo da Vinci invented the flush toilet; and (v) Robert Ripoff devised a motor that is powered entirely by psychic energy. Thousands of readers failed to notice that they had been April Fooled!)

The map in Fig. 76 actually can be 4-colored; you are hereby challenged to discover a suitable way to do this, before turning to the answer of exercise 18 . Indeed, the four-color conjecture became the Four Color Theorem in 1976, as mentioned in Section 7. Fortunately that result was still unknown in April of 1975; otherwise this interesting graph would probably never have appeared in print. McGregor’s graph has 110 vertices (regions) and 324 edges (adjacencies between regions); hence ( 15 ) and ( 16 ) yield 110 + 1296 = 1406 clauses on 440 variables, which a modern SAT solver can polish off quickly.

We can also go much further and solve problems that would be extremely difficult by hand. For example, we can add constraints to limit the number of regions that receive a particular color. Randal Bryant exploited this idea in 2010 to discover that there’s a four-coloring of Fig. 76 that uses one of the colors only 7 times (see exercise 17 ). His coloring is, in fact, unique, and it leads to an explicit way to 4-color the McGregor graphs of all orders n ≥ 3 ( exercise 18 ).

Such additional constraints can be generated in many ways. We could, for instance, append ( 110 8 ) clauses, one for every choice of 8 regions, specifying that those 8 regions aren’t all colored 1. But no, we’d better scratch that idea: ( 110 8 ) \= 409 , 705 , 619 , 895 . Even if we restricted ourselves to the 74,792,876,790 of 8 regions that are independent , we’d be dealing with far too many clauses.

An interesting SAT-oriented way to ensure that x 1 + ... + x n is at most r , which works well even when n and r are rather large, was found by C. Sinz [ LNCS 3709 (2005), 827–831]. His method introduces ( n − r ) r new variables s j k for 1 ≤ j ≤ n − r and 1 ≤ k ≤ r . If F is any satisfiability problem and if we add the ( n − r − 1) r + ( n − r )( r + 1) clauses

( s ¯ j k ∨ s j + 1 k ) , for 1  ≤ j < n − r ⁢      and 1 ≤ k ≤ r , ( 18 )

( x ¯ j + k ∨ s j − k ∨ s j k + 1 ) , for 1  ≤ j ≤ n − r ⁢      and 0  ≤ k ≤ r , ( 19 )

where s j − k is omitted when k = 0 and s j k + 1 is omitted when k = r , then the new set of clauses is satisfiable if and only if F is satisfiable with x 1 +...+ x n ≤ r. (See exercise 26 .) With this scheme we can limit the number of red-colored regions of McGregor’s graph to at most 7 by appending 1538 clauses in 721 new variables.

Another way to achieve the same goal, which turns out to be even better, has been proposed by O. Bailleux and Y. Boufkhad [ LNCS 2833 (2003), 108– 122]. Their method is a bit more difficult to describe, but still easy to implement: Consider a complete binary tree that has n −1 internal nodes numbered 1 through n − 1, and n leaves numbered n through 2 n − 1; the children of node k , for 1 ≤ k < n , are nodes 2 k and 2 k +1 (see 2.3.4.5–(5)). We form new variables b j k for 1 < k < n and 1 ≤ j ≤ t k , where t k is the minimum of r and the number of leaves below node k. Then the following clauses, explained in exercise 27 , do the job:

( b ¯ i 2 k ∨ b ¯ j 2 k + 1 ∨ b i + j k ) ,  for 0  ≤ i ≤ t 2 k , 0 ≤ j ≤ t 2 k + 1 ,   1 ≤ i + j ≤ r + 1 , 1 < k < n ; ( 20 )

( b ¯ i 2 ∨ b ¯ j 3 ) ,   for 0  ≤ i ≤ t 2 , 0 ≤ j ≤ t 3 , i + j \= r + 1 . ( 21 )

In these formulas we let t k = 1 and b 1 k \= x k − n + 1 for n ≤ k < 2 n ; all literals b ¯ 0 k and b r + 1 k are to be omitted. Applying ( 20 ) and ( 21 ) to McGregor’s graph, with n = 110 and r = 7, yields just 1216 new clauses in 399 new variables.

The same ideas apply when we want to ensure that x 1 +...+ x n is at least r , because of the identity S ≥ r ( x 1 , ... , x n ) \= S ≤ n − r ( x ¯ 1 , ... , x ¯ n ) . And exercise 30 considers the case of equality, when our goal is to make x 1 + ... + x n = r . We’ll discuss other encodings of such cardinality constraints below.

Factoring integers

Next on our agenda is a family of SAT instances with quite a different flavor. Given an ( m + n ) -bit binary integer z = ( z m + n ... z 2 z 1 ) 2 , do there exist integers x = ( x m ... x 1 ) 2 and y = ( y n ... y 1 ) 2 such that z = x × y ? For example, if m = 2 and n = 3, we want to invert the binary multiplication

y 3 y 2 y 1 × x 2 x 1 ¯ a 3 a 2 a 1 b 3 b 2 b 1 ¯ c 3 c 2 c 1 ¯ z 5 z 4 z 3 z 2 ⁢   z 1 ( a 3 a 2 a 1 ) 2 \= ( y 3 y 2 y 1 ) 2 × x 1 ( b 3 b 2 b 1 ) 2 \= ( y 3 y 2 y 1 ) 2 × x 2 z 1 \= a 1 ( c 1 z 2 ) 2 \= a 2 + b 1 ( c 2 z 3 ) 2 \= a 3 + b 2 + c 1 ( c 3 z 4 ) 2 \= b 3 + c 2 z 5 \= c 3 ( 22 )

when the z bits are given. This problem is satisfiable when z = 21 = (10101) 2 , in the sense that suitable binary values x 1 , x 2 , y 1 , y 2 , y 3 , a 1 , a 2 , a 3 , b 1 , b 2 , b 3 , c 1 , c 2 , c 3 do satisfy these equations. But it’s unsatisfiable when z = 19 = (10011) 2 .

Arithmetical calculations like ( 22 ) are easily expressed in terms of clauses that can be fed to a SAT solver: We first specify the computation by constructing a Boolean chain, then we encode each step of the chain in terms of a few clauses. One such chain, if we identify a 1 with z 1 and c 3 with z 5 , is

z 1 ← x 1 ∧ y 1 , b 1 ← x 2 ∧ y 1 , z 2 ← a 2 ⊕ b 1 , s ← a 3 ⊕ b 2 , z 3 ← s ⊕ c 1 , z 4 ← b 3 ⊕ c 2 , a 2 ← x 1 ∧ y 2 , b 2 ← x 2 ∧ y 2 , c 1 ← a 2 ∧ b 1 , p ← a 3 ∧ b 2 , q ← s ∧ c 1 , z 5 ← b 3 ∧ c 2 , a 3 ← x 1 ∧ y 3 , b 3 ← x 2 ∧ y 3 , c 2 ← p ∨ q , ( 23 )

using a “ full adder” to compute c 2 z 3 and “ half adders” to compute c 1 z 2 and c 3 z 4 (see 7.1.2–(23) and (24)). And that chain is equivalent to the 49 clauses

( x 1 ∨ z ¯ 1 ) ∧ ( y 1 ∨ z ¯ 1 ) ∧ ( x ¯ 1 ∨ y ¯ 1 ∨ z 1 ) ∧ ... ∧ ( b ¯ 3 ∨ c ¯ 2 ∨ z ¯ 4 ) ∧ ( b 3 ∨ z ¯ 5 ) ∧ ( c 2 ∨ z ¯ 5 ) ∧ ( b ¯ 3 ∨ c ¯ 2 ∨ z 5 )

obtained by expanding the elementary computations according to simple rules:

t ← u ∧ v ⁢  becomes  ( u ∨ t ¯ ) ∧ ( v ∨ t ¯ ) ∧ ( u ¯ ∨ v ¯ ∨ t ) ; t ← u ∨ v ⁢  becomes  ( u ¯ ∨ t ) ∧ ( v ¯ ∨ t ) ∧ ( u ∨ v ∨ t ¯ ) ; ( 24 ) t ← u ⊕ v ⁢  becomes  ( u ¯ ∨ v ∨ t ) ∧ ( u ∨ v ¯ ∨ t ) ∧ ( u ∨ v ∨ t ¯ ) ∧ ( u ¯ ∨ v ¯ ∨ t ¯ ) .

To complete the specification of this factoring problem when, say, z = (10101) 2 , we simply append the unary clauses ( z 5 ) ∧ ( z ¯ 4 ) ( z 3 ) ∧ ( z ¯ 2 ) ∧ ( z 1 ) .

Logicians have known for a long time that computational steps can readily be expressed as conjunctions of clauses. Rules such as ( 24 ) are now called Tseytin encoding , after Gregory Tseytin (1966). Our representation of a small five-bit factorization problem in 49+5 clauses may not seem very efficient; but we will see shortly that m-bit by n-bit factorization corresponds to a satisfiability problem with fewer than 6mn variables, and fewer than 20mn clauses of length 3 or less.

Even if the system has hundreds or thousands of formulas,

it can be put into the conjunctive normal form “piece by piece”,

without any “multiplying out.”

— MARTIN DAVIS and HILARY PUTNAM (1958)

Suppose m ≤ n . The easiest way to set up Boolean chains for multiplication is probably to use a scheme that goes back to John Napier’s Rabdologiæ (Edinburgh, 1617), pages 137–143, as modernized by Luigi Dadda [ Alta Frequenza 34 (1965), 349–356]: First we form all mn products x i ∧ y j , putting every such bit into bin [ i + j ], which is one of m + n “bins” that hold bits to be added for a particular power of 2 in the binary number system. The bins will contain respectively (0, 1, 2, ..., m , m , ..., m , ..., 2, 1) bits at this point, with n −m+1 occurrences of “ m ” in the middle. Now we look at bin [k] for k = 2, 3, .... If bin [k] contains a single bit b , we simply set z k − 1 ← b . If it contains two bits { b,b ′}, we use a half adder to compute z k − 1 ← b ⊕ b ′ ,   c ← b ∧ b ′ , and we put the carry bit c into bin [ k + 1]. Otherwise bin [ k ] contains t ≥ 3 bits; we choose any three of them, say { b, b', b" }, and remove them from the bin. With a full adder we then compute r ← b ⊕ b ′ ⊕ b " and c ← 〈 bb ′ b ″〉, so that b + b ′ + b ″ = r + 2 c ; and we put r into bin [k] , c into bin [ k +1]. This decreases t by 2, so eventually we will have computed z k − 1 . Exercise 41 quantifies the exact amount of calculation involved.

This method of encoding multiplication into clauses is quite flexible, since we’re allowed to choose any three bits from bin [ k ] whenever four or more bits are present. We could use a first-in-first-out strategy, always selecting bits from the “rear” and placing their sum at the “front”; or we could work last-in-first-out, essentially treating bin [ k ] as a stack instead of a queue. We could also select the bits randomly, to see if this makes our SAT solver any happier. Later in this section we’ll refer to the clauses that represent the factoring problem by calling them factor\ fifo ( _m , n , z ), factor_lifo ( m , n , z ), or factor_rand ( m , n , z , s ), respectively, where s is a seed for the random number generator used to generate them.

It’s somewhat mind-boggling to realize that numbers can be factored without using any number theory! No greatest common divisors, no applications of Fermat’s theorems, etc., are anywhere in sight. We’re providing no hints to the solver except for a bunch of Boolean formulas that operate almost blindly at the bit level. Yet factors are found.

Of course we can’t expect this method to compete with the sophisticated factorization algorithms of Section 4.5.4. But the problem of factoring does demonstrate the great versatility of clauses. And its clauses can be combined with other constraints that go well beyond any of the problems we’ve studied before.

Fault testing

Lots of things can go wrong when computer chips are manufactured in the “real world,” so engineers have long been interested in constructing test patterns to check the validity of a particular circuit. For example, suppose that all but one of the logical elements are functioning properly in some chip; the bad one, however, is stuck: Its output is constant, always the same regardless of the inputs that it is given. Such a failure is called a single-stuck-at fault.

Figure 77 illustrates a typical digital circuit in detail: It implements the 15 Boolean operations of ( 23 ) as a network that produces five output signals z 5 z 4 z 3 z 2 z 1 from the five inputs y 3 y 2 y 1 x 2 x 1 . In addition to having 15 AND, OR, and XOR gates, each of which transforms two inputs into one output, it has 15 “fanout” gates (indicated by dots at junction points), each of which splits one input into two outputs. As a result it comprises 50 potentially distinct logical signals, one for each internal “wire.” Exercise 47 shows that a circuit with m outputs, n inputs, and g conventional 2-to-1 gates will have g + m − n fanout gates and 3 g +2 m − n wires. A circuit with w wires has 2w possible single-stuck-at faults, namely w faults in which the signal on a wire is stuck at 0 and w more on which it is stuck at 1.

images

Fig. 77. A circuit that corresponds to ( 23 ).

Table 1 shows 101 scenarios that are possible when the 50 wires of Fig. 77 are activated by one particular sequence of inputs, assuming that at most one stuck-at fault is present. The column headed OK shows the correct behavior of the Boolean chain (which nicely multiplies x = 3 by y = 6 and obtains z = 18). We can call these the “ default” values, because, well, they have no faults. The other 100 columns show what happens if all but one of the 50 wires have error-free signals; the two columns under b 2 1 , for example, illustrate the results when the rightmost wire that fans out from gate b 2 is stuck at 0 or 1. Each row is obtained bitwise from previous rows or inputs, except that the boldface digits are forced. When a boldface value agrees with the default, its entire column is correct; otherwise errors might propagate. All values above the bold diagonal match the defaults.

Table 1
SINGLE-STUCK-AT FAULTS IN FIGURE 77 WHEN x 2 x 1 = 11, y 3 y 2 y 1 = 110

images

If we want to test a chip that has n inputs and m outputs, we’re allowed to apply test patterns to the inputs and see what outputs are produced. Close inspection shows, for instance, that the pattern considered in Table 1 doesn’t detect an error when q is stuck at 1, even though q should be 0, because all five output bits z 5 z 4 z 3 z 2 z 1 are correct in spite of that error. In fact, the value of c 2 ← p ∨ q is unaffected by a bad q , because p = 1 in this example. Similarly, the fault “ x 1 2 stuck at 0” doesn’t propagate into z 1 ← x 1 2 ∧ y 1 1 because y 1 1 \= 0 . Altogether 44 faults, not 50, are discovered by this particular test pattern.

All of the relevant repeatable faults, whether they’re single-stuck-at or wildly complicated, could obviously be discovered by testing all 2 n possible patterns. But that’s out of the question unless n is quite small. Fortunately, testing isn’t hopeless, because satisfactory results are usually obtained in practice if we do have enough test patterns to detect all of the detectable single-stuck-at faults. Exercise 49 shows that just five patterns suffice to certify Fig. 77 by this criterion.

The detailed analysis in exercise 49 also shows, surprisingly, that one of the faults, namely “ s 2 stuck at 1,” cannot be detected! Indeed, an erroneous s 2 can propagate to an erroneous q only if c 1 2 \= 1 , and that forces x 1 = x 2 = y 1 = y 2 = 1; only two possibilities remain, and neither y 3 = 0 nor y 3 = 1 reveals the fault. Consequently we can simplify the circuit by removing gate q ; the chain ( 23 ) becomes shorter, with “ q ← s ∧ c 1 , c 2 ← p ∨ q ” replaced by “ c 2 ← p ∨ c 1 .”

Of course Fig. 77 is just a tiny little circuit, intended only to introduce the concept of stuck-at faults. Test patterns are needed for the much larger circuits that arise in real computers; and we will see that SAT solvers can help us to find them. Consider, for example, the generic multiplier circuit prod (m, n) , which is part of the Stanford GraphBase. It multiplies an m -bit number x by an n -bit number y , producing an ( m + n )-bit product z . Furthermore, it’s a so-called “ parallel multiplier,” with delay time O(log(m + n)) ; thus it’s much more suited to hardware design than methods like the factor_fifo schemes that we considered above, because those circuits need Ω( m + n ) time for carries to propagate.

Let’s try to find test patterns that will smoke out all of the single-stuck-at faults in prod (32, 32), which is a circuit of depth 33 that has 64 inputs, 64 outputs, 3660 AND gates, 1203 OR gates, 2145 XOR gates, and (therefore) 7008 fanout gates and 21,088 wires. How can we guard it against 42,176 different faults?

Before we construct clauses to facilitate that task, we should realize that most of the single-stuck-at faults are easily detected by choosing patterns at random, since faults usually cause big trouble and are hard to miss. Indeed, choosing x = # 3243F6A8 and y = # 885A308D more-or-less at random already eliminates 14,733 cases; and ( x, y ) = (# 2B7E1516 , # 28AED2A6 ) eliminates 6,918 more. We might as well keep doing this, because bitwise operations such as those in Table 1 are fast. Experience with the smaller multiplier in Fig. 77 suggests that we get more effective tests if we bias the inputs, choosing each bit to be 1 with probability .9 instead of .5 (see exercise 49 ). A million such random inputs will then generate, say, 243 patterns that detect all but 140 of the faults.

Our remaining job, then, is essentially to find 140 needles in a haystack of size 2 64 , after having picked 42,176 − 140 = 42,036 pieces of low-hanging fruit. And that’s where a SAT solver is useful. Consider, for example, the analogous but simpler problem of finding a test pattern for “q stuck at 0” in Fig. 77 . We can use the 49 clauses F derived from ( 23 ) to represent the well-behaved circuit; and we can imagine corresponding clauses F ′ that represent the faulty computation, using “primed” variables z 1 ′ , a 2 ′ , ... , z 5 ′ . Thus F ′ begins with ( x 1 ∨ z ¯ 1 ′ ) ∧ ( y 1 ∨ z ¯ 1 ′ ) and ends with ( b ¯ 3 ′ ∨ c ¯ 2 ′ ∨ z 5 ′ ) ; it’s like F except that the clauses representing q ′ ← s ′ ∧ c 1 ′ in ( 23 ) are changed to simply q ¯ ′ (meaning that q ′ is stuck at 0). Then the clauses of F and F ′, together with a few more clauses to state that z 1 ≠ z 1 ′ or ... or z 5 ≠ z 5 ′ , will be satisfiable only by variables for which ( y 3 y 2 y 1 ) 2 × ( x 2 x 1 ) 2 is a suitable test pattern for the given fault.

This construction of F ′ can obviously be simplified, because z 1 ′ is identical to z 1 ; any signal that differs from the correct value must be located “downstream” from the one-and-only fault. Let’s say that a wire is tarnished if it is the faulty wire or if at least one of its input wires is tarnished. We introduce new variables g ′ only for wires g that are tarnished. Thus, in our example, the only clauses F ′ that are needed to extend F to a faulty companion circuit are q ¯ ′ and the clauses that correspond to c 2 ′ ← p ∨ q ′ , z 4 ′ ← b 3 ⊕ c 2 ′ , z 5 ′ ← b 3 ∧ c 2 ′ . .

Moreover, any fault that is revealed by a test pattern must have an active path of wires, leading from the fault to an output; all wires on this path must carry a faulty signal. Therefore Tracy Larrabee [ IEEE Trans. CAD-11 (1992), 4–15] decided to introduce additional “sharped” variables g # for each tarnished wire, meaning that g lies on the active path. The two clauses

( g ¯ # ∨ g ∨ g ′ ) ∧ ( g ¯ # ∨ g ¯ ∨ g ¯ ′ ) ( 25 )

ensure that g ≠ g ′ whenever g is part of that path. Furthermore we have ( v ¯ # ∨ g # ) whenever g is an AND, OR, or XOR gate with tarnished input v . Fanout gates are slightly tricky in this regard: When wires g 1 and g 2 fan out from a tarnished wire g, we need variables g 1# and g 2# as well as g # ; and we introduce the clause

( g ¯ # ∨ g 1 # ∨ g 2 # ) ( 26 )

to specify that the active path takes at least one of the two branches.

According to these rules, our example acquires the new variables q # , c 2 # , c 2 1 # , c 2 2 # , z 4 # , z 5 # , and the new clauses

( q ¯ # ∨ q ∨ q ′ ) ∧ ( q ¯ # ∨ q ¯ ∨ q ¯ ′ ) ∧ ( q ¯ # ∨ c 2 # ) ∧ ( c ¯ 2 # ∨ c 2 ∨ c 2 ′ ) ∧ ( c ¯ 2 # ∨ c ¯ 2 ∨ c ¯ 2 ′ ) ∧ ( c ¯ 2 # ∨ c 2 1 # ∨ c 2 2 # ) ∧ ( c ¯ 2 1 # ∨ z 4 # ) ∧ ( z ¯ 4 # ∨ z 4 ∨ z 4 ′ ) ∧ ( z ¯ 4 # ∨ z ¯ 4 ∨ z ¯ 4 ′ ) ∧ ( c ¯ 2 2 # ∨ z 5 # ) ∧ ( z ¯ 5 # ∨ z 5 ∨ z 5 ′ ) ∧ ( z ¯ 5 # ∨ z ¯ 5 ∨ z ¯ 5 ′ ) .

The active path begins at q, so we assert the unit clause ( q # ); it ends at a tarnished output, so we also assert ( z 4 # ∨ z 5 # ) . The resulting set of clauses will find a test pattern for this fault if and only if the fault is detectable. Larrabee found that such active-path variables provide important clues to a SAT solver and significantly speed up the solution process.

Returning to the large circuit prod (32, 32), one of the 140 hard-to-test faults is “ W 21 26 stuck at 1,” where W 21 26 denotes the 26th extra wire that fans out from the OR gate called W 21 in §75 of the Stanford GraphBase program GB_GATES; W 21 26 is an input to gate b 40 40 ← d 40 19 ∧ W 21 26 in §80 of that program. Test patterns for that fault can be characterized by a set of 23,194 clauses in 7,082 variables (of which only 4 variables are “primed” and 4 are “sharped”). Fortunately the solution ( x, y ) = ( #7F13FEDD , #5FE57FFE ) was found rather quickly in the author’s experiments; and this pattern also killed off 13 of the other cases, so the score was now “14 down and 126 to go”!

The next fault sought was “ A 5 36 , 2 stuck at 1,” where A 5 36 , 2 is the second extra wire to fan out from the AND gate A 5 36 in §72 of GB GATES (an input to R 11 36 ← A 5 36 , 2 ∧ R 1 35 , 2 ). This fault corresponds to 26,131 clauses on 8,342 variables; but the SAT solver took a quick look at those clauses and decided almost instantly that they are unsatisfiable . Therefore the fault is undetectable, and the circuit prod (32, 32) can be simplified by setting R 11 36 ← R 1 35 , 2 . A closer look showed, in fact, that clauses corresponding to the Boolean equations

x \= y ∧ z , y \= v ∧ w , z \= t ∧ u , u \= v ⊕ w

were present (where t \= R 13 44 , u \= A 58 45 , v \= R 4 44 , w \= A 14 45 , x \= R 23 46 , y \= R 13 45 , z \= R 19 45 ); these clauses force x = 0. Therefore it was not surprising to find that the list of unresolved faults also included R 23 46 , R 23 46 , 1 , and R 23 46 , 2 stuck at 0. Altogether 26 of the 140 faults undetected by random inputs turned out to be absolutely undetectable; and only one of these, namely “ Q 26 46 stuck at 0,” required a nontrivial proof of undetectability.

Some of the 126−26 = 100 faults remaining on the to-do list turned out to be significant challenges for the SAT solver. While waiting, the author therefore had time to take a look at a few of the previously found solutions, and noticed that those patterns themselves were forming a pattern! Sure enough, the extreme portions of this large and complicated circuit actually have a fairly simple structure, stuck-at-fault-wise. Hence number theory came to the rescue: The factorization #87FBC059 × F0F87817 = 2 63 − 1 solved many of the toughest challenges, some of which occur with probability less than 2 −34 when 32-bit numbers are multiplied; and the “Aurifeuillian” factorization (2 31 − 2 16 +1)(2 31 +2 16 +1) = 2 62 + 1, which the author had known for more than forty years (see Eq. 4.5.4– (15)), polished off most of the others.

The bottom line (see exercise 51 ) is that all 42,150 of the detectable single- stuck-at faults of the parallel multiplication circuit prod (32, 32) can actually be detected with at most 196 well-chosen test patterns.

Learning a Boolean function

Sometimes we’re given a “black box” that evaluates a Boolean function f( x 1 , ..., x N ). We have no way to open the box, but we suspect that the function is actually quite simple. By plugging in various values for x = x 1 ... x N , we can observe the box’s behavior and possibly learn the hidden rule that lies inside. For example, a secret function of N = 20 Boolean variables might take on the values shown in Table 2 , which lists 16 cases where f(x) = 1 and 16 cases where f(x)= 0.

Suppose we assume that the function has a DNF ( disjunctive normal form) with only a few terms. We’ll see in a moment that it’s easy to express such an assumption as a satisfiability problem. And when the author constructed clauses corresponding to Table 2 and presented them to a SAT solver, he did in fact learn almost immediately that a very simple formula is consistent with all of the data:

f ( x 1 , ... , x 20 ) \= x ¯ 2 x ¯ 3 x ¯ 10 ∨ x ¯ 6 x ¯ 10 x ¯ 12 ∨ x 8 x ¯ 13 x ¯ 15 ∨ x ¯ 8 x 10 x ¯ 12. ( 27 )

Table 2
VALUES TAKEN ON BY AN UNKNOWN FUNCTION

Cases where f ( x ) = 1 Cases where f ( x ) = 0
x 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9      . . .      x 20 x 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9      . . .      x 20
1 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 11 0 1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0 0 10 1 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 1 10 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 1 1 00 1 1 0 0 0 1 0 1 0 0 0 1 0 1 1 1 0 0 00 0 0 0 1 1 0 1 1 1 0 0 0 0 0 1 1 1 0 01 1 0 1 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 00 0 1 0 0 1 0 0 1 1 1 0 0 0 0 0 1 0 0 01 0 0 0 1 0 1 0 0 1 1 0 0 1 1 1 1 1 0 01 1 0 0 0 1 1 1 0 1 0 0 0 0 0 0 0 0 1 00 0 0 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 00 1 1 0 0 0 1 1 1 0 1 1 0 0 0 1 0 0 1 11 0 0 1 1 0 1 1 0 0 1 0 0 0 1 0 0 1 0 10 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 00 1 1 1 1 0 0 1 1 0 0 0 1 1 1 0 0 0 1 10 1 0 0 0 0 0 0 0 1 0 0 1 1 0 1 1 1 0 1 1 0 1 0 1 1 0 1 1 1 1 1 1 0 0 0 0 1 0 10 1 0 0 0 1 0 1 1 0 0 0 1 0 1 0 0 0 1 01 0 1 1 1 0 1 1 0 1 0 0 1 0 1 0 1 0 0 11 0 1 0 1 0 1 0 1 1 1 1 1 1 0 1 1 1 0 00 1 0 1 0 1 1 0 0 0 1 0 0 0 0 0 0 0 1 00 1 1 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 01 1 1 1 0 0 0 1 1 1 0 1 1 0 0 0 1 0 1 11 0 0 1 1 1 0 0 0 1 0 1 1 0 0 0 0 0 1 11 1 0 0 1 1 1 0 0 0 1 0 1 1 0 1 0 0 1 10 1 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0 0 11 1 1 0 0 0 0 1 0 0 1 1 0 1 1 0 0 1 0 00 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 0 1 0 00 0 1 1 0 0 1 1 1 1 1 1 1 0 1 1 1 1 0 01 1 0 0 1 0 0 1 0 0 1 1 1 0 0 1 1 1 0 11 1 0 0 1 1 1 0 0 0 1 0 0 1 0 0 1 0 0 11 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 0 1

This formula was discovered by constructing clauses in 2 MN variables p i,j and q i,j for 1 ≤ i ≤ M and 1 ≤ j ≤ N , where M is the maximum number of terms allowed in the DNF (here M = 4) and where

p i , j \= [ term  i ⁢     contains  x j ] , q i , j \= [ term  i ⁢     contains  x ¯ j ] . ( 28 )

If the function is constrained to equal 1 at P specified points, we also use auxiliary variables z i,k for 1 ≤ i ≤ M and 1 ≤ k ≤ P , one for each term at every such point.

Table 2 says that f (1, 1, 0, 0, ..., 1) = 1, and we can capture this specification by constructing the clause

( z 1 , 1 ∨ z 2 , 1 ∨ ... ∨ z M , 1 ) ( 29 )

together with the clauses

( z ¯ i , 1 ∨ q ¯ i , 1 ) ∧ ( z ¯ i , 1 ∨ q ¯ i , 2 ) ∧ ( z ¯ i , 1 ∨ p ¯ i , 3 ) ∧ ( z ¯ i , 1 ∨ p ¯ i , 4 ) ∧ ... ∧ ( z ¯ i , 1 ∨ q ¯ i , 20 ) ( 30 )

for 1 ≤ i ≤ M . Translation: ( 29 ) says that at least one of the terms in the DNF must evaluate to true; and ( 30 ) says that, if term i is true at the point 1100 ... 1, it cannot contain x ¯ 1 or x ¯ 2 or x 3 or x 4 or ... or x ¯ 20 .

Table 2 also tells us that f (1, 0, 1, 0, ..., 1) = 0. This specification corresponds to the clauses

( q i , 1 ∨ p i , 2 ∨ q i , 3 ∨ p i , 4 ∨ ... ∨ q i , 20 ) ( 31 )

for 1 ≤ i ≤ M . (Each term of the DNF must be zero at the given point; thus either x ¯ 1 or x 2 or x ¯ 3 or x 4 or ... or x ¯ 20 must be present for each value of i .)

In general, every case where f ( x ) = 1 yields one clause like ( 29 ) of length M , plus MN clauses like ( 30 ) of length 2. Every case where f ( x ) = 0 yields M clauses like ( 31 ) of length N . We use q i,j when x j = 1 at the point in question, and p i,j when x j = 0, for both ( 30 ) and ( 31 ). This construction is due to A. P. Kamath, N. K. Karmarkar, K. G. Ramakrishnan, and M. G. C. Resende [ Mathematical Programming 57 (1992), 215–238], who presented numerous examples. From Table 2 , with M = 4, N = 20, and P = 16, it generates 1360 clauses of total length 3904 in 224 variables; a SAT solver then finds a solution with p 1 , 1 = q 1 , 1 = p 1 , 2 = 0, q 1 , 2 = 1, ..., leading to ( 27 ).

The simplicity of ( 27 ) makes it plausible that the SAT solver has indeed psyched out the true nature of the hidden function f(x) . The chance of agreeing with the correct value 32 times out of 32 is only 1 in 2 32 , so we seem to have overwhelming evidence in favor of that equation.

But no: Such reasoning is fallacious. The numbers in Table 2 actually arose in a completely different way, and Eq. ( 27 ) has essentially no credibility as a predictor of f(x) for any other values of x ! (See exercise 53 .) The fallacy comes from the fact that short- DNF Boolean functions of 20 variables are not at all rare; there are many more than 2 32 of them.

D. Morgenstern has found a much simpler formula that also matches Table 2 :

f ( x 1 , ... , x 20 ) \= x ¯ 4 x 10 x ¯ 12 ∨ x ¯ 6 x ¯ 10 x ¯ 12 ∨ x 9 x ¯ 10 x 11 .

But it’s actually further than ( 27 ) from the “true”f that’s revealed in exercise 53 .

On the other hand, when we do know that the hidden function f(x) has a DNF with at most M terms (although we know nothing else about it), the clauses ( 29 )–( 31 ) give us a nice way to discover those terms, provided that we also have a sufficiently large and unbiased “ training set” of observed values.

For example, let’s assume that ( 27 ) actually is the function in the box. If we examine f(x) at 32 random points x , we don’t have enough data to make any deductions. But 100 random training points will almost always home in on the correct solution ( 27 ). This calculation typically involves 3942 clauses in 344 variables; yet it goes quickly, needing only about 100 million accesses to memory.

One of the author’s experiments with a 100-element training set yielded

f ^ ( x 1 , ... , x 20 ) \= x ¯ 2 x ¯ 3 x ¯ 10 ∨ x 3 x ¯ 6 x ¯ 10 x ¯ 12 ∨ x 8 x ¯ 13 x 15 ∨ x ¯ 8 x 10 x ¯ 12 , ( 32 )

which is close to the truth but not quite exact. ( Exercise 59 proves that f ^ ( x ) is equal to f(x) more than 97% of the time.) Further study of this example showed that another nine training points were enough to deduce f(x) uniquely, thus obtaining 100% confidence (see exercise 61 ).

Bounded model checking

Some of the most important applications of SAT solvers in practice are related to the verification of hardware or software, because designers generally want some kind of assurance that particular implementations correctly meet their specifications.

A typical design can usually be modeled as a transition relation between Boolean vectors X = x 1 ... x n that represent the possible states of a system. We write X → X ′ if state X at time t can be followed by state X ′ at time t + 1. The task in general is to study sequences of state transitions

X 0 → X 1 → X 2 → ... → X r , ( 33 )

and to decide whether or not there are sequences that have special properties. For example, we hope that there’s no such sequence for which X 0 is an “ initial state” and X r is an “error state”; otherwise there’d be a bug in the design.

images

Fig. 78. Conway’s rule ( 35 ) defines these three successive transitions.

Questions like this are readily expressed as satisfiability problems: Each state X t is a vector of Boolean variables x t 1 ... x tn , and each transition relation can be represented by a set of m clauses T ( X t , X t +1 ) that must be satisfied. These clauses T ( X , X ′) involve 2 n variables { x 1 , ... , x n , x 1 ′ , ... , x n ′ } , together with q auxiliary variables { y 1 , ..., y q } that might be needed to express Boolean formulas in clause form as we did with the Tseytin encodings in ( 24 ). Then the existence of sequence ( 33 ) is equivalent to the satisfiability of mr clauses

T ( X 0 , X 1 ) ∧ T ( X 1 , X 2 ) ∧ T ( X r − 1 , X r ) ( 34 )

in the n ( r +1)+ qr variables { x tj | 0 ≤ t ≤ r , 1 ≤ j ≤ n } ∪ { y tk | 0≤ t < r , 1≤ k ≤ q }. We’ve essentially “unrolled” the sequence ( 33 ) into r copies of the transition relation, using variables x tj for state X t and y tk for the auxiliary quantities in T ( X t , X t +1 ). Additional clauses can now be added to specify constraints on the initial state X 0 and/or the final state X r , as well as any other conditions that we want to impose on the sequence.

This general setup is called “bounded model checking,” because we’re using it to check properties of a model (a transition relation), and because we’re considering only sequences that have a bounded number of transitions, r .

John Conway’s fascinating Game of Life provides a particularly instructive set of examples that illustrate basic principles of bounded model checking. The states X of this game are two-dimensional bitmaps, corresponding to arrays of square cells that are either alive ( 1 ) or dead (0). Every bitmap X has a unique successor X ′, determined by the action of a simple 3 × 3 cellular automaton: Suppose cell x has the eight neighbors { x NW , x N , x NE , x W , x E , x SW , x S , x SE }, and let ν = x NW + x N + x NE + x W + x E + x SW + x S + x SE be the number of neighbors that are alive at time t . Then x is alive at time t + 1 if and only if either (a) ν = 3, or (b) ν = 2 and x is alive at time t . Equivalently, the transition rule

x ′ \= [ 2 < x NW + x N + x NE + x W + 1 2 x + x E + x SW + x S + x SE < 4 ] ( 35 )

holds at every cell x . (See, for example, Fig. 78 , where the live cells are black.)

Conway called Life a “ no-player game,” because it involves no strategy: Once an initial state X 0 has been set up, all subsequent states X 1 , X 2 , ... are completely determined. Yet, in spite of the simple rules, he also proved that Life is inherently complicated and unpredictable, indeed beyond human comprehension, in the sense that it is universal: Every finite, discrete, deterministic system, however complex, can be simulated faithfully by some finite initial state X 0 of Life. [See Berlekamp, Conway, and Guy, Winning Ways (2004), Chapter 25.]

In exercises 7.1.4–160 through 162, we’ve already seen some of the amazing Life histories that are possible, using BDD methods. And many further aspects of Life can be explored with SAT methods, because SAT solvers can often deal with many more variables. For example, Fig. 78 was discovered by using 7×15 = 105 variables for each state X 0 , X 1 , X 2 , X 3 . The values of X 3 were obviously predetermined; but the other 105 × 3 = 315 variables had to be computed, and BDDs can’t handle that many. Moreover, additional variables were introduced to ensure that the initial state X 0 would have as few live cells as possible.

Here’s the story behind Fig. 78 , in more detail: Since Life is two-dimensional, we use variables x ij instead of x j to indicate the states of individual cells, and x tij instead of x tj to indicate the states of cells at time t. We generally assume that x tij = 0 for all cells outside of a given finite region, although the transition rule ( 35 ) can allow cells that are arbitrarily far away to become alive as Life goes on. In Fig. 78 the region was specified to be a 7 × 15 rectangle at each unit of time. Furthermore, configurations with three consecutive live cells on a boundary edge were forbidden, so that cells “outside the box” wouldn’t be activated.

The transitions T ( X t , X t +1 ) can be encoded without introducing additional variables, but only if we introduce 190 rather long clauses for each cell not on the boundary. There’s a better way, based on the binary tree approach underlying ( 20 ) and ( 21 ) above, which requires only about 63 clauses of size ≤ 3, together with about 14 auxiliary variables per cell. This approach (see exercise 65 ) takes advantage of the fact that many intermediate calculations can be shared. For example, cells x and x W have four neighbors { x NW , x N , x SW , x S } in common; so we need to compute x NW + x N + x SW + x S only once, not twice.

The clauses that correspond to a four-step sequence X 0 → X 1 → X 2 → X 3 → X 4 leading to X 4 = images turn out to be unsatisfiable without going outside of the 7 × 15 frame. (Only 10 gigamems of calculation were needed to establish this fact, using Algorithm C below, even though roughly 34000 clauses in 9000 variables needed to be examined!) So the next step in the preparation of Fig. 78 was to try X 3 = images ; and this trial succeeded. Additional clauses, which permitted X 0 to have at most 39 live cells, led to the solution shown, at a cost of about 17 gigamems; and that solution is optimum, because a further run (costing 12 gigamems) proved that there’s no solution with at most 38.

Let’s look for a moment at some of the patterns that can occur on a chessboard, an 8 × 8 grid. Human beings will never be able to contemplate more than a tiny fraction of the 2 64 states that are possible; so we can be fairly sure that “Lifenthusiasts” haven’t already explored every tantalizing configuration that exists, even on such a small playing field.

One nice way to look for a sequence of interesting Life transitions is to assert that no cell stays alive more than four steps in a row. Let us therefore say that a mobile Life path is a sequence of transitions X 0 → X 1 → ... → X r with the additional property that we have

( x ¯ t i j ∨ x ¯ ( t + 1 ) i j ∨ x ¯ ( t + 2 ) i j ∨ x ¯ ( t + 3 ) i j ∨ x ¯ ( t + 4 ) i j ) , for 0 ≤ t ≤ r − 4. ( 36 )

To avoid trivial solutions we also insist that X r is not entirely dead. For example, if we impose rule ( 36 ) on a chessboard, with x tij permitted to be alive only if 1 ≤ i, j ≤ 8, and with the further condition that at most five cells are alive in each generation, a SAT solver can quickly discover interesting mobile paths such as

images

which last quite awhile before leaving the board. And indeed, the five-celled object that moves so gracefully in this path is R. K. Guy’s famous glider (1970), which is surely the most interesting small creature in Life’s universe. The glider moves diagonally, recreating a shifted copy of itself after every four steps.

Interesting mobile paths appear also if we restrict the population at each time to {6, 7, 8, 9, 10} instead of {1, 2, 3, 4, 5}. For example, here are some of the first such paths that the author’s solver came up with, having length r = 8:

images

These paths illustrate the fact that symmetry can be gained, but never lost, as Life evolves deterministically. Marvelous designs are spawned in the process. In each of these sequences the next bitmap, X 9 , would break our ground rules: The population immediately after X 8 grows to 12 in the first and last examples, but shrinks to 5 in the second-from-last; and the path becomes immobile in the other two. Indeed, we have X 5 = X 7 in the second example, hence X 6 = X 8 and X 7 = X 9 , etc. Such a repeating pattern is called an oscillator of period 2. The third example ends with an oscillator of period 1, known as a “ still life.”

What are the ultimate destinations of these paths? The first one becomes still, with X 69 = X 70 ; and the fourth becomes very still, with X 12 = 0! The fifth is the most fascinating of the group, because it continues to produce ever more elaborate valentine shapes, then proceeds to dance and sparkle, until finally beginning to twinkle with period 2 starting at time 177. Thus its members X 2 through X 7 qualify as “ Methuselahs,” defined by Martin Gardner as “Life patterns of population less than 10 that do not become stable within 50 generations.” (A repetitive pattern, like the glider or an oscillator, is called stable .)

SAT solvers are basically useless for the study of Methuselahs, because the state space becomes too large. But they are quite helpful when we want to illuminate many other aspects of Life, and exercises 66 – 85 discuss some notable instances. We will consider one more instructive example before moving on, namely an application to “ eaters.” Consider a Life path of the form

images

where the gray cells form a still life and the cells of X 1 , X 2 , X 3 are unknown. Thus X 4 = X 5 and X 0 = X 5 + glider. Furthermore we require that the still life X 5 does not interact with the glider’s parent, images ; see exercise 77 . The idea is that a glider will be gobbled up if it happens to glide into this particular still life, and the still life will rapidly reconstitute itself as if nothing had happened.

Algorithm C almost instantaneously (well, after about 100 megamems) finds

images

the four-step eater that was first observed in action by R. W. Gosper in 1971.

Applications to mutual exclusion

Let’s look now at how bounded model checking can help us to prove that algorithms are correct. (Or incorrect.) Some of the most challenging issues of verification arise when we consider parallel processes that need to synchronize their concurrent behavior. To simplify our discussion it will be convenient to tell a little story about Alice and Bob.

Alice and Bob are casual friends who share an apartment. One of their joint rooms is special: When they’re in that critical room, which has two doors, they don’t want the other person to be present. Furthermore, being busy people, they don’t want to interrupt each other needlessly. So they agree to control access to the room by using an indicator light, which can be switched on or off.

The first protocol they tried can be characterized by symmetrical algorithms:

A 0 .   M a y b e   g o   t o   A 1 . B 0 .   M a y b e   g o   t o   B 1 . A 1 .   I f   l   g o   t o   A 1 ,   e l s e   t o   A 2 . B 1 .   I f   l   g o   t o   B 1 ,   e l s e   t o   B 2 . A 2 .   S e t   l   ←   1 ,   g o   t o   A 3 . B 2 .   S e t   l   ←   1 ,   g o   t o   B 3 . ( 40 ) A 3 .   C r i t i c a l ,   g o   t o   A 4 . B 3 .   C r i t i c a l ,   g o   t o   B 4 . A 4 .   S e t   l   ←   0 ,   g o   t o   A 0 . B 4 .  S e t  l → 0 ,  g o  t o  B 0 .

At any instant of time, Alice is in one of five states, {A0, A1, A2, A3, A4}, and the rules of her program show how that state might change. In state A0 she isn’t interested in the critical room; but she goes to A1 when she does wish to use it. She reaches that objective in state A3. Similar remarks apply to Bob. When the indicator light is on ( l = 1), they wait until the other person has exited the room and switched the light back off ( l = 0).

Alice and Bob don’t necessarily operate at the same speed. But they’re allowed to dawdle only when in the “maybe” state A0 or B0. More precisely, we model the situation by converting every relevant scenario into a discrete sequence of state transitions. At every time t = 0, 1, 2, ..., either Alice or Bob (but not both) will perform the command associated with their current state, thereby perhaps changing to a different state at time t + 1. This choice is nondeterministic.

Only four kinds of primitive commands are permitted in the procedures we shall study, all of which are illustrated in ( 40 ): ( 1 ) “Maybe go to s ”; ( 2 ) “Critical, go to s ”; ( 3 ) “Set v ← b , go to s ”; and ( 4 ) “If v go to s 1 , else to s 0 ”. Here s denotes a state name, v denotes a shared Boolean variable, and b is 0 or 1.

Unfortunately, Alice and Bob soon learned that protocol ( 40 ) is unreliable: One day she went from A1 to A2 and he went from B1 to B2, before either of them had switched the indicator on. Embarrassment (A3 and B3) followed.

They could have discovered this problem in advance, if they’d converted the state transitions of ( 40 ) into clauses for bounded model checking, as in ( 33 ), then applied a SAT solver. In this case the vector X t that corresponds to time t consists of Boolean variables that encode each of their current states, as well as the current value of l. We can, for example, have eleven variables A0 t , A1 t , A2 t , A3 t , A4 t , B0 t , B1 t , B2 t , B3 t , B4 t , l t , together with ten binary exclusion clauses ( A0 t ¯ ⁢ ∨   A1 t ¯ ) , ( A0 t ¯ ⁢ ∨   A2 t ¯ ) , ... , ( A3 t ¯ ⁢ ∨   A4 t ¯ ) to ensure that Alice is in at most one state, and with ten similar clauses for Bob. There’s also a variable @ t , which is true or false depending on whether Alice or Bob executes their program step at time t . (We say that Alice was “bumped” if @ t = 1, and Bob was bumped if @ t = 0.)

If we start with the initial state X 0 defined by unit clauses

A0 0 ∧ A1 0 ¯ ∧ A2 0 ¯ ∧ A3 0 ¯ ∧ A4 0 ¯ ∧ B0 0 ∧ B1 0 ¯ ∧ B2 0 ¯ ∧ B3 0 ¯ ∧ B4 0 ¯ ∧ l ¯ 0 , ( 41 )

the following clauses for 0 ≤ t < r (discussed in exercise 87 ) will emulate the first r steps of every legitimate scenario defined by ( 40 ):

( @ t ∨ A0 ¯ t ∨ A0 t + 1 ) ( @ t ¯ ∨ A0 t ¯ ∨ A0 t + 1 ∨ A1 t + 1 ) ( @ t ∨ B 0 t ¯ ∨ B 0 t + 1 ∨ B 1 t + 1 ) ( @ t ∨ A1 ¯ t ∨ A1 t + 1 ) ( @ t ¯ ∨ A1 t ¯ ∨ l t ¯ ∨ A1 t + 1 ) ( @ t ∨ B1 t ¯ ∨ l ¯ t ∨ B 1 t + 1 ) ( @ t ∨ A2 ¯ t ∨ A2 t + 1 ) ( @ t ¯ ∨ A1 t ¯ ∨ l t ∨ A2 t + 1 ) ( @ t ∨ B1 t ¯ ∨ l t ∨ B2 t + 1 ) ( @ t ∨ A3 ¯ t ∨ A3 t + 1 ) ( @ t ¯ ∨ A2 t ¯ ∨ A3 t + 1 ) ( @ t ∨ B2 t ¯ ∨ B3 t + 1 ) ( @ t ∨ A4 ¯ t ∨ A4 t + 1 ) ( @ t ¯ ∨ A2 t ¯ ∨ l t + 1 ) ( @ t ∨ B2 t ¯ ∨ l t + 1 ) ( 42 ) ( @ t ¯ ∨ B0 ¯ t ∨ B0 t + 1 ) ( @ t ¯ ∨ A3 t ¯ ∨ A4 t + 1 ) ( @ t ∨ B3 t ¯ ∨ B4 t + 1 ) ( @ t ¯ ∨ B1 ¯ t ∨ B1 t + 1 ) ( @ t ¯ ∨ A4 t ¯ ∨ A0 t + 1 ) ( @ t ∨ B4 t ¯ ∨ B0 t + 1 ) ( @ t ¯ ∨ B2 ¯ t ∨ B2 t + 1 ) ( @ t ¯ ∨ A4 t ¯ ∨ l t + 1 ) ( @ t ∨ B4 t ¯ ∨ l ¯ t + 1 ) ( @ t ¯ ∨ B3 ¯ t ∨ B3 t + 1 ) ( @ t ¯ ∨ l t ∨ A2 t ∨ A4 t ∨ l ¯ t + 1 ) ( @ t ∨ l t ∨ B 2 t ∨ B 4 t ∨ l ¯ t + 1 ) ( @ t ¯ ∨ B4 ¯ t ∨ B4 t + 1 ) ( @ t ¯ ∨ l ¯ t ∨ A2 t ∨ A4 t ∨ l t + 1 ) ( @ t ∨ l ¯ t ∨ B 2 t ∨ B 4 t ∨ l t + 1 )

If we now add the unit clauses (A3 r ) and (B3 r ), the resulting set of 13 + 50 r clauses in 11+12 r variables is readily satisfiable when r = 6, thereby proving that the critical room might indeed be jointly occupied. (Incidentally, standard terminology for mutual exclusion protocols would say that “two threads concurrently execute a critical section ”; but we shall continue with our roommate metaphor.)

Back at the drawing board, one idea is to modify ( 40 ) by letting Alice use the room only when l = 1, but letting Bob in when l = 0:

A0. Maybe go to A1. A1. If  l ⁢     go to A2, else to A1. A2. Critical, go to A3. A3. Set  l ← 0 ,   go to A0. B 0. Maybe go to  B 1. B1. If  l ⁢       go to B1, else to B2. B2. Critical,  go to B3. B3. Set  l ← 1 ,   go to B0. ( 43 )

Computer tests with r = 100 show that the corresponding clauses are unsatisfiable; thus mutual exclusion is apparently guaranteed by ( 43 ).

But ( 43 ) is a nonstarter, because it imposes an intolerable cost: Alice can’t use the room k times until Bob has already done so! Scrap that.

How about installing another light, so that each person controls one of them?

A0. Maybe go to A1. A1. If  b ⁢     go to A1, else to A2. A2. Set  a ← 1 ,   go to A3. A3.   Critical,   go to A4. A4. Set  a ← 0 ,   go to A0.  B 0. Maybe go to  B 1. B1. If  a ⁢       go to B1, else to B2. B2. Set  b ← 1 ,  go to B3. B3. Critical ,   go to B4. B4.     Set  b ← 0 ,   go to  B 0. ( 44 )

No; this suffers from the same defect as ( 40 ). But maybe we can cleverly switch the order of steps 1 and 2:

A0. Maybe go to A1. A1. Set a ← 1 , go to A2. A2. If b ⁢     go to A2, else to A3. A3.   Critical,   go to A4. A4. Set  a ← 0 ,   go to A0.  B 0. Maybe go to  B 1. B1.  Set   b ← 1 ,   go to B2. B2. If a ⁢  go to B2, else to B3. B3. Critical ,   go to B4. B4.     Set  b ← 0 ,   go to  B 0. ( 45 )

Yes! Exercise 95 proves easily that this protocol does achieve mutual exclusion.

Alas, however, a new problem now arises, namely the problem known as “ deadlock” or “ livelock.” Alice and Bob can get into states A2 and B2, after which they’re stuck — each waiting for the other to go critical.

In such cases they could agree to “reboot” somehow. But that would be a cop-out; they really seek a better solution. And they aren’t alone: Many people have struggled with this surprisingly delicate problem over the years, and several solutions (both good and bad) appear in the exercises below. Edsger Dijkstra, in some pioneering lecture notes entitled Cooperating Sequential Processes [Technological University Eindhoven (September 1965), §2.1], thought of an instructive way to improve on ( 45 ):

A0. Maybe go to A1. A1. Set a ← 1 ,   go to A2. A2. If b ⁢     go to   A3, else to A4. A3. Set a ← 0 ,   go to A1. A4. Critical, go to A5. A5. Set a ← 0 ,   go to A0. B 0. Maybe go to B 1. B 1. Set b ← 1 ,   go to B 2. B 2. If a ⁢     go to   B 3, else to B 4. B 3. Set b ← 0 ,   go to B 1. B 4. Critical, go to B 5. B 5. Set b ← 0 ,   go to B 0. ( 46 )

But he realized that this too is unsatisfactory, because it permits scenarios in which Alice, say, might wait forever while Bob repeatedly uses the critical room. (Indeed, if Alice and Bob are in states A1 and B2, she might go to A2, A3, then A1, thereby letting him run to B4, B5, B0, B1, and B2; they’re back where they started, yet she’s made no progress.)

The existence of this problem, called starvation , can also be detected via bounded model checking. The basic idea (see exercise 91 ) is that starvation occurs if and only if there is a loop of transitions

X 0 → X 1 → ... → X p → X p + 1 → ... → X r \= X p ( 47 )

such that (i) Alice and Bob each are bumped at least once during the loop; and (ii) at least one of them is never in a “maybe” or “critical” state during the loop. And those conditions are easily encoded into clauses, because we can identify the variables for time r with the variables for time p, and we can append the clauses

( @ p ¯ ∨ @ p + 1 ¯ ∨ ... ∨ @ r − 1 ¯ ) ∧ ( @ p ∨ @ p + 1 ∨ ... ∨ @ r − 1 ) ( 48 )

to guarantee (i). Condition (ii) is simply a matter of appending unit clauses; for example, to test whether Alice can be starved by ( 46 ), the relevant clauses are A0 p ¯ ∧ A0 p + 1 ¯ ∧ ... ∧ A0 r − 1 ¯ ∧ A4 p ¯ ∧ A4 p + 1 ¯ ∧ ... ∧ A4 r − 1 ¯ .

The deficiencies of ( 43 ), ( 45 ), and ( 46 ) can all be viewed as instances of starvation, because ( 47 ) and ( 48 ) are satisfiable (see exercise 90 ). Thus we can use bounded model checking to find counterexamples to any unsatisfactory protocol for mutual exclusion, either by exhibiting a scenario in which Alice and Bob are both in the critical room or by exhibiting a feasible starvation cycle ( 47 ).

Of course we’d like to go the other way, too: If a protocol has no counterexamples for, say, r = 100, we still might not know that it is really reliable; a counterexample might exist only when r is extremely large. Fortunately there are ways to obtain decent upper bounds on r , so that bounded model checking can be used to prove correctness as well as to demonstrate incorrectness. For example, we can verify the simplest known correct solution to Alice and Bob’s problem, a protocol by G. L. Peterson [ Information Proc. Letters 12 (1981), 115– 116], who noticed that a careful combination of ( 43 ) and ( 45 ) actually suffices:

A0. Maybe go to A1. A1. Set a ← 1 ,   go to A2. A2. Set   l ← 0 ,   go to A3. A3.  If b ⁢ go to A4, else to A5. A4. If   l ⁢ go to A5, else to A3. A5. Critical,   go to A6. A6. Set a ← 0 ,   go to A0. B 0. Maybe go to B1. B1. Set b ← 1 ,   go to B2. B 2. Set   l ← 1 ,   go to B 3. B3.  If a ⁢ go to B4, else to B5. B4. If   l ⁢ go to B3, else to B5. B5. Critical,   go to B6. B6. Set a ← 0 ,   go to B0. ( 49 )

Now there are three signal lights, a, b, and l — one controlled by Alice, one controlled by Bob, and one switchable by both.

To show that states A5 and B5 can’t be concurrent, we can observe that the shortest counterexample will not repeat any state twice; in other words, it will be a simple path of transitions ( 33 ). Thus we can assume that r is at most the total number of states. However, ( 49 ) has 7 × 7 × 2 × 2 × 2 = 392 states; that’s a finite bound, not really out of reach for a good SAT solver on this particular problem, but we can do much better. For example, it’s not hard to devise clauses that are satisfiable if and only if there’s a simple path of length ≤ r (see exercise 92 ), and in this particular case the longest simple path turns out to have only 54 steps.

We can in fact do better yet by using the important notion of invariants, which we encountered in Section 1.2.1 and have seen repeatedly throughout this series of books. Invariant assertions are the key to most proofs of correctness, so it’s not surprising that they also give a significant boost to bounded model checking. Formally speaking, if Φ( X ) is a Boolean function of the state vector X , we say that Φ is invariant if Φ( X ) implies Φ( X ′) whenever X → X ′. For example, it’s not hard to see that the following clauses are invariant with respect to ( 49 ):

Φ ( X ) \= ( A0 ∨ A1 ∨ A2 ∨ A3 ∨ A4 ∨ A5 ∨ A6 ) ∧ ( B0 ∨ B1 ∨ B2 ∨ B3 ∨ B4 ∨ B5 ∨ B6 ) ∧ ( A0 ¯ ∨ a ¯ ) ∧ ( A1 ¯ ∨ a ¯ ) ∧ ( A2 ¯ ∨ a ) ∧ ( A3 ¯ ∨ a ) ∧ ( A4 ¯ ∨ a ) ∧ ( A5 ¯ ∨ a ) ∧ ( A6 ¯ ∨ a ) ∧ ( B0 ¯ ∨ b ¯ ) ∧ ( B1 ¯ ∨ b ¯ ) ∧ ( B2 ¯ ∨ b ) ∧ ( B3 ¯ ∨ b ) ∧ ( B4 ¯ ∨ b ) ∧ ( B5 ¯ ∨ b ) ∧ ( B6 ¯ ∨ b ) . ( 50 )

(The clause A0 ¯ ∨ a ¯ says that a = 0 when Alice is in state A0, etc.) And we can use a SAT solver to prove that Φ is invariant, by showing that the clauses

Φ ( X ) ∧ ( X → X ′ ) ∧ ¬ Φ ( X ′ ) ( 51 )

are unsatisfiable . Furthermore Φ( X 0 ) holds for the initial state X 0 , because ¬Φ( X 0 ) is unsatisfiable. (See exercise 93 .) Therefore Φ( X t ) is true for all t ≥ 0, by induction, and we may add these helpful clauses to all of our formulas.

The invariant ( 50 ) reduces the total number of states by a factor of 4. And the real clincher is the fact that the clauses

( X 0 → X 1 → ... ⁢   X r ) ∧ Φ ( X 0 ) ∧ Φ ( X 1 ) ∧ ... ∧ Φ ( X r ) ∧ A5 r ∧ B5 r , ( 52 )

where X 0 is not required to be the initial state, turn out to be unsatisfiable when r = 3. In other words, there’s no way to go back more than two steps from a bad state, without violating the invariant. We can conclude that mutual exclusion needs to be verified for ( 49 ) only by considering paths of length 2(!). Furthermore, similar ideas ( exercise 98 ) show that ( 49 ) is starvation-free.

Caveat : Although ( 49 ) is a correct protocol for mutual exclusion according to Alice and Bob’s ground rules, it cannot be used safely on most modern computers unless special care is taken to synchronize cache memories and write buffers. The reason is that hardware designers use all sorts of trickery to gain speed, and those tricks might allow one process to see a = 0 at time t + 1 even though another process has set a ← 1 at time t. We have developed the algorithms above by assuming a model of parallel computation that Leslie Lamport has called sequential consistency [ IEEE Trans. C-28 (1979), 690–691].

Digital tomography

Another set of appealing questions amenable to SAT solving comes from the study of binary images for which partial information is given. Consider, for example, Fig. 79 , which shows the “ Cheshire cat” of Section 7.1.3 in a new light. This image is an m × n array of Boolean variables ( x i,j ), with m = 25 rows and n = 30 columns: The upper left corner element, x 1 , 1 , is 0, representing white; and x 1 , 24 = 1 corresponds to the lone black pixel in the top row. We are given the row sums r i \= Σ j \= 1 n x i , j for 1 ≤ i ≤ m and the column sums c i \= Σ i \= 1 m x i , j for 1 ≤ j ≤ n , as well as both sets of sums in the 45° diagonal directions, namely

a d \= Σ i + j \= d + 1 ⁢           x i , j ⁢           and   b d \= Σ i − j \= d − n ⁢           x i , j ⁢                             for  0< d < m + n . ( 53 )

To what extent can such an image be reconstructed from its sums r i , c j , a d , and b d ? Small examples are often uniquely determined by these Xray-like projections (see exercise 103 ). But the discrete nature of pixel images makes the reconstruction problem considerably more difficult than the corresponding continuous problem, in which projections from many different angles are available. Notice, for example, that the classical “8 queens problem” — to place eight nonattacking queens on a chessboard — is equivalent to solving an 8 × 8 digital tomography problem with the constraints r i = 1, c j = 1, a d ≤ 1, and b d ≤ 1.

images

Fig. 79. An array of black and white pixels together with its row sums r i , column sums c j , and diagonal sums a d , b d .

The constraints of Fig. 79 appear to be quite strict, so we might expect that most of the pixels x i,j are determined uniquely by the given sums. For instance, the fact that a 1 = ... = a 5 = 0 tells us that x i,j = 0 whenever i + j ≤ 6; and similar deductions are possible at all four corners of the image. A crude “ballpark estimate” suggests that we’re given a few more than 150 sums, most of which occupy 5 bits each; hence we have roughly 150 × 5 = 750 bits of data, from which we wish to reconstruct 25 × 30 = 750 pixels x i,j . Actually, however, this problem turns out to have many billions of solutions (see Fig. 80 ), most of which aren’t catlike! Exercise 106 provides a less crude estimate, which shows that this abundance of solutions isn’t really surprising.

images

Fig. 80. Extreme solutions to the constraints of Fig. 79 .

A digital tomography problem such as Fig. 79 is readily represented as a sequence of clauses to be satisfied, because each of the individual requirements is just a special case of the cardinality constraints that we’ve already considered in the clauses of ( 18 )–( 21 ). This problem differs from the other instances of SAT that we’ve been discussing, primarily because it consists entirely of cardinality constraints: It is a question of solving 25 + 30 + 54 + 54 = 163 simultaneous linear equations in 750 variables x i,j , where each variable must be either 0 or 1. So it’s essentially an instance of integer programming ( IP), not an instance of satisfiability (SAT). On the other hand, Bailleux and Boufkhad devised clauses ( 20 ) and ( 21 ) precisely because they wanted to apply SAT solvers, not IP solvers, to digital tomography. In the case of Fig. 79 , their method yields approximately 40,000 clauses in 9,000 variables, containing about 100,000 literals altogether.

Figure 80(b) illustrates a solution that differs as much as possible from Fig. 79 . Thus it minimizes the sum x 1 , 24 + x 2 , 5 + x 2 , 6 + ... + x 25 , 21 of the 182 variables that correspond to black pixels, over all 0-or-1-valued solutions to the linear equations. If we use linear programming to minimize that sum over 0 ≤ x i,j ≤ 1, without requiring the variables to be integers, we find almost instantly that the minimum value is ≈ 31.38 under these relaxed conditions; hence every black-and-white image must have at least 32 black pixels in common with Fig. 79 . Furthermore, Fig. 80(b) — which can be computed in a few seconds by widely available IP solvers such as CPLEX — actually achieves this minimum. By contrast, state-of-the-art SAT solvers as of 2013 had great difficulty finding such an image, even when told that a 32-in-common solution is possible.

Parts (a) and (c) of Fig. 80 are, similarly, quite relevant to the current state of the SAT-solving art: They represent hundreds of individual SAT instances, where the first k variables are set to particular known values and we try to find a solution with the next variable either 0 or 1, respectively. Several of the subproblems that arose while computing rows 6 and 7 of Fig. 80(c) turned out to be quite challenging, although resolvable in a few hours; and similar problems, which correspond to different kinds of lexicographic order, apparently still lie beyond the reach of contemporary SAT-oriented methods. Yet IP solvers polish these problems off with ease. (See exercises 109 and 111 .)

If we provide more information about an image, our chances of being able to reconstruct it uniquely are naturally enhanced. For example, suppose we also compute the numbers r i ′ , c j ′ , a d ′ , and b d ′ , which count the runs of 1s that occur in each row, column, and diagonal. (We have r 1 ′ \= 1 , r 2 ′ \= 2 , r 3 ′ \= 4 , and so on.) Given this additional data, we can show that Fig. 79 is the only solution, because a suitable set of clauses turns out to be unsatisfiable. Exercise 117 explains one way by which ( 20 ) and ( 21 ) can be modified so that they provide constraints based on the run counts. Furthermore, it isn’t difficult to express even more detailed constraints, such as the assertion that “column 4 contains runs of respective lengths (6, 1, 3),” as a sequence of clauses; see exercise 438 .

SAT examples — summary

We’ve now seen convincing evidence that simple Boolean clauses — ANDs of ORs of literals — are enormously versatile. Among other things, we’ve used them to encode problems of graph coloring, integer factorization, hardware fault testing, machine learning, model checking, and tomography. And indeed, Section 7.9 will demonstrate that 3SAT is the “poster child” for NP-complete problems in general: Any problem in NP — which is a huge class, essentially comprising all yes-or-no questions of size N whose affirmative answers are verifiable in N O (1) steps — can be formulated as an equivalent instance of 3SAT, without greatly increasing the problem size.

Backtracking for SAT

OK, we’ve seen a dizzying variety of intriguing and important examples of SAT that are begging to be solved. How shall we solve them?

Any instance of SAT that involves at least one variable can be solved systematically by choosing a variable and setting it to 0 or 1. Either of those choices gives us a smaller instance of SAT; so we can continue until reaching either an empty instance — which is trivially satisfiable, because no clauses need to be satisfied — or an instance that contains an empty clause. In the latter case we must back up and reconsider one of our earlier choices, proceeding in the same fashion until we either succeed or exhaust all the possibilities.

For example, consider again the formula F in ( 1 ). If we set x 1 = 0, F reduces to x ¯ 2 ∧ ( x 2 ∨ x 3 ) , because the first clause ( x 1 ∨ x ¯ 2 ) loses its x 1 , while the last two clauses contain x ¯ 1 and are satisfied. It will be convenient to have a notation for this reduced problem; so let’s write

F | x ¯ 1 \= x ¯ 2 ∧ ( x 2 ∨ x 3 ) . ( 54 )

Similarly, if we set x 1 = 1, we obtain the reduced problem

F | x 1 \= ( x 2 ∨ x 3 ) ∧ x ¯ 3 ∧ ( x ¯ 2 ∨ x 3 ) . ( 55 )

F is satisfiable if and only if we can satisfy either ( 54 ) or ( 55 ).

In general if F is any set of clauses and if l is any literal, then F | l (read “ F given l ” or “ F conditioned on l ”) is the set of clauses obtained from F by

  • removing every clause that contains l ; and

  • removing l ¯ from every clause that contains l ¯ .

This conditioning operation is commutative, in the sense that F | l | l ′ = F | l ′ | l when l ′ ≠ l ¯ . If L = { l 1 , ..., l k } is any set of strictly distinct literals, we can also write F | L = F | l 1 |...| l k . In these terms, F is satisfiable if and only if F |L = Ø for some such L, because the literals of L satisfy every clause of F when F |L = Ø.

The systematic strategy for SAT that was sketched above can therefore be formulated as the following recursive procedure B(F) , which returns the special value ⊥ when F is unsatisfiable, otherwise it returns a set L that satisfies F :

B ( F ) \= { If  F \= ∅ ,   return  ∅ . ⁢     ( F ⁢       is trivially satisfiable. ) Otherwise if  ϵ ∈ F ,  return  ⊥ . ⁢     ( F ⁢     is unsatisfiable. ) Otherwise let  l ⁢      be a liternal in  F ⁢     and set  L ⁢     ← B ( F | l ) . If  L ≠ ⊥ , return  L ∪ l . ⁢           Otherwise set  L ← B ( F | l ¯ ) . If  L ≠ ⊥ ,   return  L ∪ l ¯ .  Otherwise return  ⊥ . ( 56 )

Let’s try to flesh out this abstract algorithm by converting it to efficient code at a lower level. From our previous experience with backtracking, we know that it will be crucial to have data structures that allow us to go quickly from F to F | l , then back again to F if necessary, when F is a set of clauses and l is a literal. In particular, we’ll want a good way to find all of the clauses that contain a given literal.

A combination of sequential and linked structures suggests itself for this purpose, based on our experience with exact cover problems: We can represent each clause as a set of cells , where each cell p contains a literal l = L ( p ) together with pointers F ( p ) and B ( p ) to other cells that contain l , in a doubly linked list. We’ll also need C ( p ), the number of the clause to which p belongs. The cells of clause C i will be in consecutive locations START ( i ) + j , for 0 ≤ j < SIZE ( i ).

We will find it convenient to represent the literals x k and x ¯ k , which involve variable x k , by using the integers 2 k and 2 k + 1. With this convention we have

l ¯ \= l ⊕ 1     and    | l | \= x l ≫ 1 . ( 57 )

Our implementation of ( 56 ) will assume that the variables are x 1 , x 2 , ..., x n ; thus the 2 n possible literals will be in the range 2 ≤ l ≤ 2 n + 1.

Cells 0 through 2 n + 1 are reserved for special purposes: Cell l is the head of the list for the occurrences of l in other cells. Furthermore, if l is a literal whose value has not yet been fixed, C ( l ) will be the length of that list, namely the number of currently active clauses in which l appears.

For example, the m = 7 ternary clauses R ′ of ( 7 ) might be represented internally in 2 n + 2 + 3 m = 31 cells as follows, using these conventions:

p \= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 L ( p ) \= − − − − − − − − − − 9 7 3 8 7 5 6 5 3 8 4 3 8 6 2 9 6 4 7 4 2 F ( p ) \= − − 30 21 29 17 26 28 22 25 9 7 3 8 11 5 6 15 12 13 4 18 19 16 2 10 23 20 14 27 24 B ( p ) \= − − 24 12 20 15 16 11 13 10 25 14 18 19 28 17 23 5 21 22 27 3 8 26 30 9 6 29 7 4 2 C ( p ) \= − − 2 3 3 2 3 3 3 2 7 7 7 6 6 6 5 5 5 4 4 4 3 3 3 2 2 2 1 1 1

The literals of each clause appear in decreasing order here; for example, the literals L ( p ) = (8, 4, 3) in cells 19 through 21 represent the clause x 4 ∨ x 2 ∨ x ¯ 1 , which appears as the fourth clause, ‘ 4 1 ¯ 2 ’ in ( 7 ). This ordering turns out to be quite useful, because we’ll always choose the smallest unset variable as the l or l ¯ in ( 56 ); then l or l ¯ will always appear at the right of its clauses, and we can remove it or put it back by simply changing the relevant SIZE fields.

The clauses in this example have START ( i ) = 31 − 3 i for 1 ≤ i ≤ 7, and SIZE ( i ) = 3 when computation begins.

Algorithm A ( Satisfiability by backtracking ). Given nonempty clauses C 1 ∧ ... ∧ C m on n > 0 Boolean variables x 1 ... x n , represented as above, this algorithm finds a solution if and only if the clauses are satisfiable. It records its current progress in an array m 1 ... m n of “moves,” whose significance is explained below.

A1. [Initialize.] Set a ← m and d ← 1. (Here a represents the number of active clauses, and d represents the depth-plus-one in an implicit search tree.)

A2. [Choose.] Set l ← 2d. If C ( l ) ≤ C ( l + 1), set l ← l + 1. Then set m d ← ( l & 1) + 4[ C ( l ⊕ 1) = 0]. (See below.) Terminate successfully if C ( l ) = a .

A3. [Remove l ¯ .] Delete l ¯ from all active clauses; but go to A5 if that would make a clause empty. (We want to ignore l ¯ , because we’re making l true.)

images

Fig. 81. The search tree that is implicitly traversed by Algorithm A , when that algorithm is applied to the eight unsatisfiable clauses R defined in ( 6 ). Branch nodes are labeled with the variable being tested; leaf nodes are labeled with a clause that is found to be contradicted.

A4. [Deactivate l ’s clauses.] Suppress all clauses that contain l . (Those clauses are now satisfied.) Then set a ← a − C ( l ), d ← d + 1, and return to A2.

A5. [Try again.] If m d < 2, set m d ← 3 − m d , l ← 2 d +( m d & 1), and go to A3.

A6. [Backtrack.] Terminate unsuccessfully if d = 1 (the clauses are unsatisfiable). Otherwise set d ← d − 1 and l ← 2d +( m d & 1).

A7. [Reactivate l ’s clauses.] Set a ← a + C ( l ), and unsuppress all clauses that contain l . (Those clauses are now unsatisfied, because l is no longer true.)

A8. [Unremove l ¯ .] Reinstate l ¯ in all the active clauses that contain it. Then go back to A5.

(See exercise 121 for details of the low-level list processing operations that are needed to update the data structures in steps A3 and A4, and to downdate them in A7 and A8.)

The move codes m j of Algorithm A are integers between 0 and 5 that encode the state of the algorithm’s progress as follows:

  • m j = 0 means we’re trying x j = 1 and haven’t yet tried x j = 0.

  • m j = 1 means we’re trying x j = 0 and haven’t yet tried x j = 1.

  • m j = 2 means we’re trying x j = 1 after x j = 0 has failed.

  • m j = 3 means we’re trying x j = 0 after x j = 1 has failed.

  • m j = 4 means we’re trying x j = 1 when x ¯ j doesn’t appear.

  • m j = 5 means we’re trying x j = 0 when x j doesn’t appear.

Codes 4 and 5 refer to so-called “ pure literals”: If no clause contains the literal l ¯ , we can’t go wrong by assuming that l is true.

For example, when Algorithm A is presented with the clauses ( 7 ), it cruises directly to a solution by setting m 1 m 2 m 3 m 4 = 1014; the solution is x 1 x 2 x 3 x 4 = 0101. But when the unsatisfiable clauses ( 6 ) are given, the successive code strings m 1 ... m d in step A2 are

1 , 11 , 110 , 1131 , 121 , 1211 , 1221 , 21 , 211 , 2111 , 2121 , 221 , 2221 , ( 58 )

before the algorithm gives up. (See Fig. 81 .)

It’s helpful to display the current string m 1 ... m d now and then, as a convenient indication of progress; this string increases lexicographically. Indeed, fascinating patterns appear as the 2s and 3s gradually move to the left. (Try it!)

When the algorithm terminates successfully in step A2, a satisfying assignment can be read off from the move table by setting x j ← 1 ⊕ ( m j & 1) for 1 ≤ j ≤ d. Algorithm A stops after finding a single solution; see exercise 122 if you want them all.

Lazy data structures

Instead of using the elaborate doubly linked machinery that underlies Algorithm A , we can actually get by with a much simpler scheme discovered by Cynthia A. Brown and Paul W. Purdom, Jr. [ IEEE Trans. PAMI-4 (1982), 309–316], who introduced the notion of watched literals. They observed that we don’t really need to know all of the clauses that contain a given literal, because only one literal per clause is actually relevant at any particular time.

Here’s the idea: When we work on clauses F |L, the variables that occur in L have known values, but the other variables do not. For example, in Algorithm A , variable x j is implicitly known to be either true or false when j ≤ d, but its value is unknown when j > d. Such a situation is called a partial assignment . A partial assignment is consistent with a set of clauses if no clause consists entirely of false literals. Algorithms for SAT usually deal exclusively with consistent partial assignments; the goal is to convert them to consistent total assignments, by gradually eliminating the unknown values.

Thus every clause in a consistent partial assignment has at least one nonfalse literal; and we can adjust the data so that such a literal appears first, when the clause is represented in memory. Many nonfalse literals might be present, but only one of them is designated as the clause’s “watchee.” When a watched literal becomes false, we can try to find another nonfalse literal to swap into its place.

With such a scheme we need only maintain a relatively short list for every literal l, namely a list W l of all clauses that currently watch l. This list can be singly linked. Hence we need only one link per clause; and we have a total of only 2 n + m links altogether, instead of the two links for each cell that are required by Algorithm A .

Furthermore — and this is the best part! — no updates need to be made to the watch lists when backtracking . The backtrack operations never falsify a nonfalse literal, because they only change values from known to unknown. Perhaps for this reason, data structures based on watched literals are called lazy, in contrast with the “ eager” data structures of Algorithm A .

Let us therefore redesign Algorithm A and make it more laid-back. Our new data structure for each cell p has only one field, L ( p ); the other fields F ( p ), B ( p ), C ( p ) are no longer necessary, nor do we need 2 n + 2 special cells. As before we will represent clauses sequentially, with the literals of C j beginning at START ( j ) for 1 ≤ j ≤ m . The watched literal will be the one in START ( j ); and a new field, LINK ( j ), will be the number of another clause with the same watched literal (or 0, if C j is the last such clause). Variable W l , if nonzero, will point to the first clause that watches literal l . Our new algorithm won’t need SIZE ( j ); instead, we can assume that the final literal of C j is in location START ( j − 1) − 1, provided that we define START (0) appropriately.

The resulting procedure is almost unbelievably short and sweet. It’s surely the simplest SAT solver that can claim to be efficient on problems of modest size:

Algorithm B ( Satisfiability by watching ). Given nonempty clauses C 1 ∧... ∧ C m on n > 0 Boolean variables x 1 ... x n , represented as above, this algorithm finds a solution if and only if the clauses are satisfiable. It records its current progress in an array m 1 ... m n of “moves,” whose significance was explained above.

B1. [ Initialize.] Set d ← 1.

B2. [Rejoice or choose.] If d > n , terminate successfully. Otherwise set m d ← [ W 2 d = 0 or W 2 d +1 ≠ 0] and l ← 2d + m d .

B3. [Remove l ¯ if possible.] For all j such that l ¯ is watched in C j , watch another literal of C j . But go to B5 if that can’t be done. (See exercise 124 .)

B4. [Advance.] Set W l ¯ ← 0 , d ← d + 1 , and return to B2.

B5. [Try again.] If m d < 2, set m d ← 3 − m d , l ← 2 d +( m d & 1), and go to B3.

B6. [Backtrack.] Terminate unsuccessfully if d = 1 (the clauses are unsatisfiable). Otherwise set d ← d − 1 and go back to B5.

Readers are strongly encouraged to work exercise 124 , which spells out the low-level operations that are needed in step B3. Those operations accomplish essentially everything that Algorithm B needs to do.

This algorithm doesn’t use move codes 4 or 5, because lazy data structures don’t have enough information to identify pure literals. Fortunately pure literals are comparatively unimportant in practice; problems that are helped by the pure literal shortcut can usually also be solved quickly without it.

Notice that steps A2 and B2 use different criteria for deciding whether to try x d = 1 or x d = 0 first at each branch of the search tree. Algorithm A chooses the alternative that satisfies the most clauses; Algorithm B chooses to make l true instead of l ¯ if the watch list for l ¯ is empty but the watch list for l is not. (All clauses in which l ¯ is watched will have to change, but those containing l are satisfied and in good shape.) In case of a tie, both algorithms set m d ← 1, which corresponds to x d = 0. The reason is that human-designed instances of SAT tend to have solutions made up of mostly false literals.

Forced moves from unit clauses

The simple logic of Algorithm B works well on many problems that aren’t too large. But its insistence on setting x 1 first, then x 2 , etc., makes it quite inefficient on many other problems, because it fails to take advantage of unit clauses, the clauses of size 1. A unit clause ( l ) forces l to be true; therefore two-way branching is unnecessary whenever a unit clause is present. Furthermore, unit clauses aren’t rare: Far from it. Experience shows that they’re almost ubiquitous in practice, so that the actual search trees often involve only dozens of branch nodes instead of thousands or millions.

The importance of unit clauses was recognized already in the first computer implementation of a SAT solver, designed by Martin Davis, George Logemann, and Donald Loveland [ CACM 5 (1962), 394–397] and based on ideas that Davis had developed earlier with Hilary Putnam [ JACM 7 (1960), 201–215]. They extended Algorithm A by introducing mechanisms that recognize when the size of a clause decreases to 1, or when the number of unsatisfied clauses containing a literal drops to 0. In such cases, they put variables onto a “ ready list,” and assigned those variables to fixed values before doing any further two-way branching. The resulting program was fairly complex; indeed, computer memory was so limited in those days, they implemented branching by writing all the data for the current node of the search tree onto magnetic tape, then backtracking when necessary by restoring the data from the most recently written tape records! The names of these four authors are now enshrined in the term “ DPLL algorithm,” which refers generally to SAT solving via partial assignment and backtracking.

Brown and Purdom, in the paper cited earlier, showed that unit clauses can be detected more simply by using watched literals as in Algorithm B . We can supplement the data structures of that algorithm by introducing indices h 1 ... h n so that the variable whose value is being set at depth d is x h d instead of x d . Furthermore we can arrange the not-yet-set variables whose watch lists aren’t empty into a circular list called the “ active ring”; the idea is to proceed through the active ring, checking to see whether any of its variables are currently in a unit clause. We resort to two-way branching only if we go all around the ring without finding any such units.

For example, let’s consider the 32 unsatisfiable clauses of waerden (3, 3; 9) in ( 9 ). The active ring is initially (1234567), because 8, 8 ¯ , 9, and 9 ¯ aren’t being watched anywhere. There are no unit clauses yet. The algorithm below will decide to try 1 ¯ first; then it will change the clauses 123, 135, 147, and 159 to 213, 315, 417, and 519, respectively, so that nobody watches the false literal 1. The active ring becomes (234567) and the next choice is 2 ¯ ; so 213, 234, 246, and 258 morph respectively into 312, 324, 426, 528. Now, with active ring (34567), the unit clause ‘3’ is detected (because 1 and 2 are false in ‘312’). This precipitates further changes, and the first steps of the computation can be summarized thus:

Active ring x 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 Units Choice Changed clauses ( 1234567 ) − − − − − − − − − 1 ¯ 213 , 315 , 417 , 519 ( 234567 ) 0 − − − − − − − − 2 ¯ 312 , 324 , 426 , 528 ( 34567 ) 0 0 − − − − − − − 3 3 4 ¯ 3 ¯ 5 ¯ , 5 ¯ 3 ¯ 7 ¯ , 639 ¯ ( 4567 ) 0 0 1 − − − − − − 4 ¯ 624 , 714 , 546 , 648 ( 567 ) 0 0 1 0 − − − − − 6 6 9 ¯ 3 ¯ 6 ¯ , 7 ¯ 6 ¯ 8 ¯ ( 975 ) 0 0 1 0 − 1 − − − 9 ¯ 9 ¯ ( 75 ) 0 0 1 0 − 1 − − 0 7 7 8 ¯ 6 ¯ 7 ¯ , 8 ¯ 7 ¯ 9 ¯ ( 85 ) 0 0 1 0 − 1 1 − 0 8 ¯ 8 ( 5 ) 0 0 1 0 − 1 1 0 0 5 , 5 ¯ Backtrack ( 69785 ) 0 0 1 − − − − − − 4 5 ¯ 3 ¯ 4 ¯ , 5 ¯ 4 ¯ 6 ¯ , 6 ¯ 4 ¯ 8 ¯ ( 69785 ) 0 0 1 1 − − − − − 5 ¯ 5 ¯ 456 , 825 , 915 , 657 , 759 ( 59 )

When 6 is found, 7 is also a unit clause; but the algorithm doesn’t see it yet, because variable x 6 is tested first. The active ring changes first to (7 5) after 6 is found, because 5 is cyclically after 6; we want to look at 7 before 5, instead of revisiting more-or-less the same clauses. After 6 has been chosen, 9 is inserted at the left, because the watch list for 9 ¯ becomes nonempty. After backtracking, variables 8, 7, 9, 6 are successively inserted at the left as they lose their forced values.

images

Fig. 82. The search tree that is implicitly traversed by Algorithm D , when that algorithm is applied to the eight unsatisfiable clauses R defined in ( 6 ). Branch nodes are labeled with the variable being tested; leaf nodes are labeled with a clause that is found to be contradicted. When the right child of a branch node is a leaf, the left branch was forced by a conditional unary clause.

The following algorithm represents the active ring by giving a NEXT field to each variable, with x NEXT( _k_ ) the successor of x k . The ring is accessed via “head” and “tail” pointers h and t at the left and right, with h = NEXT ( t ). If the ring is empty, however, t = 0, and h is undefined.

Algorithm D ( Satisfiability by cyclic DPLL ). Given nonempty clauses C 1 ∧ ... ∧ C m on n > 0 Boolean variables x 1 ... x n , represented with lazy data structures and an active ring as explained above, this algorithm finds a solution if and only if the clauses are satisfiable. It records its current progress in an array h 1 ... h n of indices and an array m 0 ... m n of “moves,” whose significance is explained below.

D1. [ Initialize.] Set m 0 ← d ← h ← t ← 0, and do the following for k = n , n −1, ... , 1: Set x k ← −1 (denoting an unset value); if W 2 k ≠ 0 or W 2 k +1 ≠ 0, set NEXT ( k ) ← h, h ← k , and if t = 0 also set t ← k. Finally, if t ≠ 0, complete the active ring by setting NEXT (t) ← h.

D2. [Success?] Terminate if t = 0 (all clauses are satisfied). Otherwise set k ← t.

D3. [Look for unit clauses.] Set h ← NEXT ( k ) and use the subroutine in exercise 129 to compute f ← [2h is a unit] + 2[2h + 1 is a unit]. If f = 3, go to D7. If f = 1 or 2, set m d +1 ← f +3, t ← k , and go to D5. Otherwise, if h ≠ t, set k ← h and repeat this step.

D4. [Two-way branch.] Set h ← NEXT (t) and m d +1 ← [ W 2 h =0 or W 2 h +1 ≠0].

D5. [Move on.] Set d ← d +1, h d ← k ← h. If t = k , set t ← 0; otherwise delete variable k from the ring by setting NEXT (t) ← h ← NEXT ( k ).

D6. [Update watches.] Set b ← ( m d +1) mod 2, x k ← b, and clear the watch list for x ¯ k (see exercise 130 ). Return to D2.

D7. [ Backtrack.] Set t ← k. While m d ≥ 2, set k ← h d , x k ← −1; if W 2 k ≠ 0 or W 2 k +1 ≠ 0, set NEXT ( k ) ← h, h ← k , NEXT (t) ← h; and set d ← d − 1.

D8. [Failure?] If d > 0, set m d ← 3 − m d , k ← h d , and return to D6. Otherwise terminate the algorithm (because the clauses aren’t satisfiable).

The move codes of this algorithm are slightly different from the earlier ones:

  • m j = 0 means we’re trying x h j \= 1 and haven’t yet tried x h j = 0.

  • m j = 1 means we’re trying x h j \= 0 and haven’t yet tried x h j \= 1 .

  • m j = 2 means we’re trying x h j \= 1 after x h j \= 0 has failed.

  • m j = 3 means we’re trying x h j \= 0 after x h j \= 1 has failed.

  • m j = 4 means we’re trying x h j \= 1 because it’s forced by a unit clause.

  • m j = 5 means we’re trying x h j \= 0 because it’s forced by a unit clause.

As before, the number of two-way branch nodes in the implicit search tree is the number of times that m j is set to 0 or 1.

Comparison of the algorithms. OK, we’ve just seen three rudimentary SAT solvers. How well do they actually do? Detailed performance statistics will be given later in this section, after we’ve studied several more algorithms. But a brief quantitative study of Algorithms A , B , and D now will give us some concrete facts with which we can calibrate our expectations before moving on.

Consider, for example, langford (n) , the problem of Langford pairs. This problem is typical of SAT instances where many unit clauses arise during the computation. For example, when Algorithm D is applied to langford ( 5 ) , it reaches a stage where the move codes are

m 1 m 2 ... m d \= 125555555555555114545545 , ( 60 )

indicating only four two-way branches (the 1s and the 2) amongst a sea of forced moves (the 4s and the 5s). We therefore expect Algorithm D to outperform Algorithms A and B , which don’t capitalize on unit clauses.

Sure enough, Algorithm D wins (slightly), even on a small example such as langford ( 5 ), which has 213 clauses, 480 cells, 28 variables. The detailed stats are

Algorithm A : 5379 + 108952 mems, 10552 bytes, 705 nodes.

Algorithm B : 1206 + 30789 mems, 4320 bytes, 771 nodes.

Algorithm D : 1417 + 28372 mems, 4589 bytes, 11 nodes.

(Here “5379+108952 mems” means that 5379 memory accesses were made while initializing the data structures before the algorithm began; then the algorithm itself accessed octabytes of memory 108,952 times.) Notice that Algorithm B is more than thrice as fast as Algorithm A in this example, although it makes 771 two-way branches instead of 705. Algorithm A needs fewer nodes, because it recognizes pure literals; but Algorithm B does much less work per node. Algorithm D , on the other hand, works very hard at each node, yet comes out ahead because its decision-making choices reduce the search to only a few nodes.

These differences become more dramatic when we consider larger problems. For instance, langford ( 9 ) has 1722 clauses, 3702 cells, 104 variables, and we find

Algorithm A : 332.0 megamems, 77216 bytes, 1,405,230 nodes.

Algorithm B : 53.4 megamems, 31104 bytes, 1,654,352 nodes.

Algorithm D : 23.4 megamems, 32057 bytes, 6093 nodes.

And with langford ( 13 )’s 5875 clauses, 12356 cells, 228 variables, the results are

Algorithm A : 2699.1 gigamems, 253.9 kilobytes, 8.7 giganodes.

Algorithm B : 305.2 gigamems, 101.9 kilobytes, 10.6 giganodes.

Algorithm D : 71.7 gigamems, 104.0 kilobytes, 14.0 meganodes.

Mathematicians will recall that, at the beginning of Chapter 7 , we used elementary reasoning to prove the unsatisfiability of langford (4 k + 1) for all k. Evidently SAT solvers have great difficulty discovering this fact, even when k is fairly small. We are using that problem here as a benchmark test, not because we recommend replacing mathematics by brute force! Its unsatisfiability actually enhances its utility as a benchmark, because algorithms for satisfiability are more easily compared with respect to unsatisfiable instances: Extreme variations in performance occur when clauses are satisfiable, because solutions can be found purely by luck. Still, we might as well see what happens when our three algorithms are set loose on the satisfiable problem langford ( 16 ), which turns out to be “no sweat.” Its 11494 clauses, 23948 cells, and 352 variables lead to the statistics

Algorithm A : 11262.6 megamems, 489.2 kilobytes, 28.8 meganodes.

Algorithm B : 932.1 megamems, 196.2 kilobytes, 40.9 meganodes.

Algorithm D : 4.9 megamems, 199.4 kilobytes, 167 nodes.

Algorithm D is certainly our favorite so far, based on the langford data. But it is far from a panacea, because it loses badly to the lightweight Algorithm B on other problems. For example, the 2779 unsatisfiable clauses, 11662 cells, and 97 variables of waerden (3, 10; 97) yield

Algorithm A : 150.9 gigamems, 212.8 kilobytes, 106.7114 meganodes.

Algorithm B : 6.2 gigamems, 71.2 kilobytes, 106.7116 meganodes.

Algorithm D : 1430.4 gigamems, 72.1 kilobytes, 102.7 meganodes.

And waerden (3, 10; 96)’s 2721 satisfiable clauses, 11418 cells, 96 variables give us

Algorithm A : 96.9 megamems, 208.3 kilobytes, 72.9 kilonodes.

Algorithm B : 12.4 megamems, 69.8 kilobytes, 207.7 kilonodes.

Algorithm D : 57962.8 megamems, 70.6 kilobytes, 4447.7 kilonodes.

In such cases unit clauses don’t reduce the search tree size by very much, so we aren’t justified in spending so much time per node.

*Speeding up by working harder

Algorithms A , B , and D are OK on smallish problems, but they cannot really cope with the larger instances of SAT that have arisen in our examples. Significant enhancements are possible if we are willing to do more work and to develop more elaborate algorithms.

Mathematicians generally strive for nice, short, elegant proofs of theorems; and computer scientists generally aim for nice, short, elegant sequences of steps with which a problem can quickly be solved. But some theorems have no short proofs, and some problems cannot be solved efficiently with short programs.

Let us therefore adopt a new attitude, at least temporarily, by fearlessly deciding to throw lots of code at SAT: Let’s look at the bottlenecks that hinder Algorithm D on large problems, and let’s try to devise new methods that will streamline the calculations even though the resulting program might be ten times larger. In this subsection we shall examine an advanced SAT solver, Algorithm L , which is able to outperform Algorithm D by many orders of magnitude on many important problems. This algorithm cannot be described in just a few lines; but it does consist of cooperating procedures that are individually nice, short, elegant, and understandable by themselves.

The first important ingredient of Algorithm L is an improved mechanism for unit propagation. Algorithm D needs only a few lines of code in step D3 to discover whether or not the value of an unknown variable has been forced by previous assignments; but that mechanism isn’t particularly fast, because it is based on indirect inferences from a lazy data structure. We can do better by using “ eager” data structures that are specifically designed to recognize forced values quickly, because high-speed propagation of the consequences of a newly asserted value turns out to be extremely important in practice.

A literal l is forced true when it appears in a clause C whose other literals have become false, namely when the set of currently assigned literals L has reduced C to the unit clause C |L = ( l ). Such unit clauses arise from the reduction of binary clauses. Algorithm L therefore keeps track of the binary clauses ( u ∨ v ) that are relevant to the current subproblem F | L. This information is kept in a so-called “bimp table” BIMP ( l ) for every literal l , which is a list of other literals l ′ whose truth is implied by the truth of l . Indeed, instead of simply including binary clauses within the whole list of given clauses, as Algorithms A , B , and D do, Algorithm L stores the relevant facts about ( u ∨ v ) directly, in a ready-to-use way, by listing u in BIMP ( υ ¯ ) and v in BIMP ( u ¯ ) . Each of the 2 n tables BIMP ( l ) is represented internally as a sequential list of length BSIZE ( l ), with memory allocated dynamically via the buddy system (see exercise 134 ).

Binary clauses, in turn, are spawned by ternary clauses. For simplicity, Algorithm L assumes that all clauses have length 3 or less, because every instance of general SAT can readily be converted to 3SAT form (see exercise 28 ). And for speed, Algorithm L represents the ternary clauses by means of “timp tables,” which are analogous to the bimp tables: Every literal l has a sequential list TIMP ( l ) of length TSIZE ( l ), consisting of pairs p 1 = ( u 1 , v 1 ), p 2 = ( u 2 , v 2 ), ..., such that the truth of l implies that each ( u i ∨ v i ) becomes a relevant binary clause. If ( u ∨ v ∨ w ) is a ternary clause, there will be three pairs p = ( v, w ), p ′ = ( w, u ), and p ″ = ( u, v ), appearing in the respective lists TIMP ( u ¯ ) , TIMP ( υ ¯ ) , and TIMP ( w ¯ ) . Moreover, these three pairs are linked together cyclically, with

Link ( p ) \= p ′ , Link ( p ′ ) \= p " , Link ( p " ) \= p . ( 61 )

Memory is allocated for the timp tables once and for all, as the clauses are input, because Algorithm L does not generate new ternaries during its computations. Individual pairs p are, however, swapped around within these sequential tables, so that the currently active ternary clauses containing u always appear in the first TSIZE ( u ¯ ) positions that have been allocated to TIMP ( u ¯ ) .

For example, let’s consider again the ternary clauses ( 9 ) of waerden (3, 3; 9). Initially there are no binary clauses, so all BIMP tables are empty. Each of the ternary clauses appears in three of the TIMP tables. At level 0 of the search tree we might decide that x 5 = 0; then TIMP ( 5 ¯ ) tells us that we gain eight binary clauses, namely {13, 19, 28, 34, 37, 46, 67, 79}. These new binary clauses are represented by sixteen entries in BIMP tables; BIMP ( 3 ¯ ) , for instance, will now be {1, 4, 7}. Furthermore, we’ll want all of the TIMP pairs that involve either 5 or 5 ¯ to become inactive, because the ternary clauses that contain 5 are weaker than the new binary clauses, and the ternary clauses that contain 5 ¯ are now satisfied. (See exercise 136 .)

As in ( 57 ) above, we shall assume that the variables of a given formula are numbered from 1 to n , and we represent the literals k and k ¯ internally by the numbers 2 k and 2 k +1. Algorithm L introduces a new twist, however, by allowing variables to have many different degrees of truth [see M. Heule, M. Dufour, J. van Zwieten, and H. van Maaren, LNCS 3542 (2005), 345–359]: We say that x k is true with degree D if VAL [ k ] = D , and false with degree D if VAL [ k ] = D + 1, where D is any even number.

The highest possible degree, typically 2 32 − 2 inside a computer, is called RT for “ real truth.” The next highest degree, typically 2 32 − 4, is called NT for “ near truth”; and then comes PT = 2 32 − 6, “ proto truth.” Lower degrees PT − 2, PT − 4, ..., 2 also turn out to be useful. A literal l is said to be fixed in context T if and only if VAL [| l |] ≥ T ; it is fixed true if we also have VAL [| l |] &1 = l &1, and it is fixed false if its complement l ¯ is fixed true.

Suppose, for example, that VAL[2] = RT + 1 and VAL[7] = PT ; hence x 2 is “really false” while x 7 is “proto true.” Then the literal ‘7’, represented internally by l = 14, is fixed true in context PT , but l is not fixed in contexts NT or RT . The literal ‘ 2 ¯ ’, represented internally by l = 5, is fixed true in every context.

Algorithm L uses a sequential stack R 0 , R 1 , ..., to record the names of literals that have received values. The current stack size, E , satisfies 0 ≤ E ≤ n . With those data structures we can use a simple breadth-first search procedure to propagate the binary consequences of a literal l in context T at high speed:

Set  H ← E ;   take account of  l ; while  H < E , set  l ← R H , H ← H + 1 ,   and ( 62 ) take account of  l ′ ⁢       for all  l ′ ⁢     in BIMP ( l ) .

Here “ take account of l ” means “if l is fixed true in context T , do nothing; if l is fixed false in context T , go to step CONFLICT ; otherwise set VAL [| l |] ← T +( l &1), R E ← l, and E ← E + 1.” The step called CONFLICT is changeable.

A literal’s BIMP table might grow repeatedly as computation proceeds. But we can undo the consequences of bad decisions by simply resetting BSIZE ( l ) to the value that it had before those decisions were made. A special variable ISTAMP is increased whenever we begin a new round of decision-making, and each literal l has its private stamp IST ( l ). Whenever BSIZE ( l ) is about to increase, we check if IST ( l ) = ISTAMP . If not, we set

IST ( l ) ← ISTAMP, ISTACK [ I ] ← ( l , BSIZE ( l ) ) , I ← I + 1. ( 63 )

Then the entries on ISTACK make it easy to downdate the BIMP tables when we backtrack. (See step L13 in the algorithm below.)

We’re ready now to look at the detailed steps of Algorithm L , except that one more member of its arsenal of data structures needs to be introduced: There’s an array VAR , which contains a permutation of {1, ..., n }, with VAR [ k ] = x if and only if INX [ x ] = k . Furthermore VAR [ k ] is a “ free variable” — not fixed in context RT — if and only if 0 ≤ k < N . This setup makes it convenient to keep track of the variables that are currently free: A variable becomes fixed by swapping it to the end of the free list and decreasing N (see exercise 137 ); then we can free it later by simply increasing N , without swapping.

Algorithm L ( Satisfiability by DPLL with lookahead ). Given nonempty clauses C 1 ∧ ... ∧ C m of size ≤ 3, on n > 0 Boolean variables x 1 ... x n , this algorithm finds a solution if and only if the clauses are satisfiable. Its family of cooperating data structures is discussed in the text.

L1. [Initialize.] Record all binary clauses in the BIMP array and all ternary clauses in the TIMP array. Let U be the number of distinct variables in unit clauses; terminate unsuccessfully if two unit clauses contradict each other, otherwise record all distinct unit literals in FORCE [k] for 0 ≤ k < U. Set VAR [ k ] ← k + 1 and INX [ k + 1] ← k for 0 ≤ k < n ; and d ← F ← I ← ISTAMP ← 0. (Think d = depth, F = fixed variables, I = ISTACK size.)

L2. [New node.] Set BRANCH [ d ] ← −1. If U = 0, invoke Algorithm X below (which looks ahead for simplifications and also gathers data about how to make the next branch). Terminate happily if Algorithm X finds all clauses satisfied; go to L15 if Algorithm X discovers a conflict; go to L5 if U > 0.

L3. [Choose l .] Select a literal l that’s desirable for branching (see exercise 168 ). If l = 0, set d ← d + 1 and return to L2. Otherwise set DEC [ d ] ← l , BACKF [ d ] ← F , BACKI [ d ] ← I , and BRANCH [ d ] ← 0.

L4. [Try l .] Set U ← 1, FORCE [0] ← l .

L5. [Accept near truths.] Set T ← NT , G ← E ← F, ISTAMP ← ISTAMP +1, and CONFLICT ← L11. Perform the binary propagation routine ( 62 ) for l ← FORCE [0], ..., l ← FORCE [ U − 1]; then set U ← 0.

L6. [Choose a nearly true L .] (At this point the stacked literals R k are “really true” for 0 ≤ k < G, and “nearly true” for G ≤ k < E. We want them all to be really true.) If G = E, go to L10. Otherwise set L ← R G , G ← G +1.

L7. [Promote L to real truth.] Set X ← | L | and VAL [ X ] ← RT + L &1. Remove variable X from the free list and from all TIMP pairs (see exercise 137 ). Do step L8 for all pairs ( u, v ) in TIMP ( L ), then return to L6.

L8. [Consider u ∨ v .] (We have deduced that u or v must be true; five cases arise.) If either u or v is fixed true (in context T , which equals NT ), do nothing. If both u and v are fixed false, go to CONFLICT . If u is fixed false but v isn’t fixed, perform ( 62 ) with l ← v . If v is fixed false but u isn’t fixed, perform ( 62 ) with l ← u . If neither u nor v is fixed, do step L9.

L9. [Exploit u ∨ v .] If v ¯ ∈ BIMP ( u ¯ ) , perform ( 62 ) with l ← u (because u ¯ implies both v and υ ¯ ). Otherwise if v ∈ BIMP ( u ¯ ) , do nothing (because we already have the clause u ∨ ν ). Otherwise if u ¯ ∈ BIMP ( v ¯ ) , perform ( 62 ) with l ← v . Otherwise append v to BIMP ( u ¯ ) and u to BIMP ( υ ¯ ) . (Each change to BIMP means that ( 63 ) might be invoked. Exercise 139 explains how to improve this step by deducing further implications called “ compensation resolvents.”)

L10. [Accept real truths.] Set F ← E . If BRANCH [ d ] ≥ 0, set d ← d + 1 and go to L2. Otherwise go to L3 if d > 0, to L2 if d = 0.

L11. [Unfix near truths.] While E > G, set E ← E − 1 and VAL [| R E |] ← 0.

L12. [Unfix real truths.] While E > F , do the following: Set E ← E − 1 and X ← | R E |; reactivate the TIMP pairs that involve X and restore X to the free list (see exercise 137 ); set VAL [ X ] ← 0.

L13. [Downdate BIMP s.] If BRANCH [ d ] ≥ 0, do the following while I > BACKI [ d ]: Set I ← I − 1 and BSIZE ( l ) ← s, where I STACK [ I ] = ( l, s ).

L14. [Try again?] (We’ve discovered that DEC [ d ] doesn’t work.) If BRANCH [ d ] = 0, set l ← DEC [ d ], DEC [ d ] ← l ← l ¯ , BRANCH [ d ] ← 1, and go back to L4.

L15. [ Backtrack.] Terminate unsuccessfully if d = 0. Otherwise set d ← d − 1, E ← F , F ← BACKF [ d ], and return to L12.

Exercise 143 extends this algorithm so that it will handle clauses of arbitrary size.

*Speeding up by looking ahead

Algorithm L as it stands is incomplete, because step L2 relies on an as-yet-unspecified “ Algorithm X ” before it chooses a literal for branching. If we use the simplest possible Algorithm X , by branching on whatever literal happens to be first in the current list of free variables, the streamlined methods for propagating forced moves in ( 62 ) and ( 63 ) will tend to make Algorithm L run roughly three times as fast as Algorithm D , and that isn’t a negligible improvement. But with a sophisticated Algorithm X we can often gain another factor of 10 or more in speed, on significant problems.

For example, here are some typical empirical statistics:

Problem

Algorithm D

Algorithm L 0

Algorithm L +

waerden (3, 10; 97)

1430 gigamems,
103 meganodes

391 gigamems,
31 meganodes

772 megamems,
4672 nodes

langford ( 13 )

71.7 gigamems,
14.0 meganodes

21.5 gigamems,
10.9 meganodes

45.7 gigamems,
944 kilonodes

rand (3, 420, 100, 0)

184 megamems,
34 kilonodes

34 megamems,
7489 nodes

626 kilomems,
19 nodes

Here Algorithm L 0 stands for Algorithm L with the simplest Algorithm X , while Algorithm L + uses all of the lookahead heuristics that we are about to discuss. The first two problems involve rather large clauses, so they use the extended Algorithm L of exercise 143 . The third problem consists of 420 random ternary clauses on 100 variables. ( Algorithm B , incidentally, needs 80.1 teramems, and a search tree of 4.50 teranodes, to show that those clauses are unsatisfiable.)

The moral of this story is that it’s wise to do 100 times as much computation at every node of a large search tree, if we can thereby decrease the size of the tree by a factor of 1000.

How then can we distinguish a variable that’s good for branching from a variable that isn’t? We shall consider a three-step approach:

  • Preselecting, to identify free variables that appear to be good candidates;

  • Nesting, to allow candidate literals to share implied computations;

  • Exploring, to examine the immediate consequences of hypothetical decisions.

While carrying out these steps, Algorithm X might discover a contradiction (in which case Algorithm L will take charge again at step L15); or the lookahead process might discover that several of the free literals are forced to be true (in which case it places them in the first U positions of the FORCE array). The explorations might even discover a way to satisfy all of the clauses (in which case Algorithm L will terminate and everybody will be happy). Thus, Algorithm X might do much more than simply choose a good variable on which to branch.

The following recommendations for Algorithm X are based on Marijn Heule’s lookahead solver called march, one of the world’s best, as it existed in 2013.

The first stage, preselection, is conceptually simplest, although it also involves some “handwaving” because it depends on necessarily shaky assumptions. Suppose there are N free variables. Experience has shown that we tend to get a good heuristic score h(l) for each literal l, representing the relative amount by which asserting l will reduce the current problem, if these scores approximately satisfy the simultaneous nonlinear equations

h ( l ) \= 0.1 + α ⁢ Σ u ∈ BIMP ( l ) u ⁢       ⁢ not fixed h ^ ( u ) + Σ ( u , υ ) ∈ TIMP ( l ) h ^ ( u ) h ^ ( ν ) . ( 64 )

Here α is a magic constant, typically 3.5; and h ^ ( l ) is a multiple of h ( l ) chosen so that Σ l ⁢     h ^ ( l ) \= 2 N is the total number of free literals. (In other words, the h scores on the right are “normalized” so that their average is 1.)

Any given set of scores h(l) can be used to derive a refined set h ′( l ) by letting

h ′ ( l ) \= 0.1 + α Σ u ∈ BIMP ( l ) u ⁢     not fixed   h ( u ) h ave + Σ ( u , υ ) ∈ TIMP ( l ) h ( u ) h ave h ( υ ) h ave ,   h ave \= 1 2 N Σ l h ( l ) . ( 65 )

Near the root of the search tree, when d ≤ 1, we start with h(l) = 1 for all l and then refine it five times (say). At deeper levels we start with the h(l) values from the parent node and refine them once. Exercise 145 contains an example.

We’ve computed h(l) for all of the free literals l , but we won’t have time to explore them all. The next step is to select free variables CAND [ j ] for 0 ≤ j < C , where C isn’t too large; we will insist that the number of candidates does not exceed

C max \= max ( C 0 , C 1 / d ) , ( 66 )

using cutoff parameters that are typically C 0 = 30, C 1 = 600. (See exercise 148 .)

We start by dividing the free variables into “ participants” and “ newbies”: A participant is a variable such that either x or x ¯ has played the role of u or v in step L8, at some node above us in the search tree; a newbie is a nonparticipant. When d = 0 every variable is a newbie, because we’re at the root of the tree. But usually there is at least one participant, and we want to branch only on participants whenever possible, in order to maintain focus while backtracking.

If we’ve got too many potential candidates, even after restricting consideration to participants, we can winnow the list down by preferring the variables x that have the largest combined score h ( x ) h ( x ¯ ) . Step X3 below describes a fairly fast way to come up with the desired selection of C ≤ C max candidates.

A simple lookahead algorithm can now proceed to compute a more accurate heuristic score H ( l ), for each of the 2 C literals l = CAND [ j ] or l = ¬ CAND [ j ] that we’ve selected for further scrutiny. The idea is to simulate what would happen if l were used for branching, by mimicking steps L4–L9 (at least to a first approximation): Unit literals are propagated as in the exact algorithm, but whenever we get to the part of step L9 that changes the BIMP tables, we don’t actually make such a change; we simply note that a branch on l would imply u ∨ v, and we consider the value of that potential new clause to be h ( u ) h ( v ). The heuristic score H ( l ) is then defined to be the sum of all such clause weights:

H ( l ) \= Σ { h ( u ) h ( υ )       |       asserting  l ⁢     in L4 leads to asserting  u ⁢       ∨ υ ⁢       in L9 } . ( 67 )

For example, the problem waerden (3, 3; 9) of ( 9 ) has nine candidate variables {1, 2, ..., 9} at the root of the search tree, and exercise 145 finds their rough heuristic scores h ( l ). The more discriminating scores H ( l ) turn out to be

H ( 1 ) \= h ( 2 ¯ ) h ( 3 ¯ ) + h ( 3 ¯ ) h ( 5 ¯ ) + h ( 4 ¯ ) h ( 7 ¯ ) + h ( 5 ¯ ) h ( 9 ¯ ) ≈ 168.6 ; H ( 2 ) \= h ( 1 ¯ ) h ( 3 ¯ ) + h ( 3 ¯ ) h ( 4 ¯ ) + h ( 4 ¯ ) h ( 6 ¯ ) + h ( 5 ¯ ) h ( 8 ¯ ) ≈ 157.3 ; H ( 3 ) \= h ( 1 ¯ ) h ( 2 ¯ ) + h ( 2 ¯ ) h ( 4 ¯ ) + h ( 4 ¯ ) h ( 5 ¯ ) + ... + h ( 6 ¯ ) h ( 9 ¯ ) ≈ 233.4 ; H ( 4 ) \= h ( 2 ¯ ) h ( 3 ¯ ) + h ( 3 ¯ ) h ( 5 ¯ ) + h ( 5 ¯ ) h ( 6 ¯ ) + ... + h ( 1 ¯ ) h ( 7 ¯ ) ≈ 231.8 ; H ( 5 ) \= h ( 3 ¯ ) h ( 4 ¯ ) + h ( 4 ¯ ) h ( 6 ¯ ) + h ( 6 ¯ ) h ( 7 ¯ ) + ... + h ( 1 ¯ ) h ( 9 ¯ ) ≈ 284.0.

This problem is symmetrical, so we also have H ( 6 ) \= H ( 6 ¯ ) \= H ( 4 ) \= H ( 4 ¯ ) , etc. The best literal for branching, according to this estimate, is 5 or 5 ¯ .

Suppose we set x 5 false and proceed to look ahead at the reduced problem, with d = 1. At this point there are eight candidates, {1, 2, 3, 4, 6, 7, 8, 9}; and they’re now related also by binary implications, because the original clause ‘357’ has, for instance, been reduced to ‘37’. In fact, the BIMP tables now define the dependency digraph

images

because 3 ¯ → 7 , etc.; and in general the 2 C candidate literals will define a dependency digraph whose structure yields important clues about the current subproblem. We can, for example, use Tarjan’s algorithm to find the strong components of that digraph, as mentioned after Theorem 7.1.1K. If some strong component includes both l and l ¯ , the current subproblem is unsatisfiable. Otherwise two literals of the same component are constrained to have the same value; so we shall choose one literal from each of the S ≤ 2 C strong components, and use those choices as the actual candidates for lookahead.

Continuing our example, at this point we can use a nice trick to save redundant computation, by extracting a subforest of the dependency digraph:

images

The relation 8 ¯ → 2 means that whatever happens after asserting the literal ‘2’ will also happen after asserting ‘ 8 ¯ ’; hence we need not repeat the steps for ‘2’ while studying ‘ 8 ¯ ’. And similarly, each of the other subordinate literals ‘ 1 ¯ ’, ..., ‘ 9 ¯ ’ inherits the assertions of its parent in this hierarchy. Tarjan’s algorithm actually produces such a subforest with comparatively little extra work.

The nested structure of a forest also fits beautifully with “ degrees of truth” in our data structure, if we visit the S candidate literals in preorder of the subforest, and if we successively assert each literal l at the truth degree that corresponds to twice its position in postorder . For instance, ( 69 ) becomes the following arrangement, which we shall call the “ lookahead forest”:

preorder 1 2 8 ¯ 3 4 6 4 ¯ 7 3 ¯ 6 ¯ 9 ¯ 8 2 ¯ 9 1 ¯ 7 ¯ ( 70 ) 2 ⋅ postorder 2 6 4 8 10 14 12 22 16 18 20 26 24 32 28 30

A simulation of steps L4–L9 with l ← 1 and T ← 2 makes x 1 true at degree 2 (we say that it’s “2 fixed” or “2true”); it also computes the score H ( 1 ) ← h ( 2 ¯ ) h ( 3 ¯ ) + h ( 4 ¯ ) h ( 7 ¯ ) , but spawns no other activity if Algorithm Y below isn’t active. Simulation with l ← 2 and T ← 6 then 6fixes 2 and computes H ( 2 ) ← h ( 1 ¯ ) h ( 3 ¯ ) + h ( 3 ¯ ) h ( 4 ¯ ) + h ( 4 ¯ ) h ( 6 ¯ ) ; during this process x 1 ’s value isn’t seen, because it is less than T . Things get interesting when l ← 8 ¯ and T ← 4: Now we 4fix 8 ¯ , and we’re still able to see that x 2 is true because 6 > T . So we save a little computation by inheriting H (2) and setting H ( 8 ¯ ) ← H ( 2 ) + h ( 4 ) h ( 6 ) + h ( 6 ) h ( 7 ) + h ( 7 ) h ( 9 ) .

The real action begins to break through a few steps later, when we set l ← 4 ¯ and T ← 12. Then ( 62 ) will 12fix not only 4 ¯ but also 3, since 4 ¯ → 3 ; and the 12truth of 3 will soon take us to the simulated step L8 with u \= 6 ¯ and υ \= 9 . Aha: We 12fix 9 ¯ , because 6 is 14true. Then we also 12fix the literals 7, 1, ... , and reach a contradiction. This contradiction shows that branching on 4 ¯ will lead to a conflict ; hence the literal 4 must be true, if the current clauses are satisfiable.

Whenever the lookahead simulation of Algorithm X learns that some literal l must be true, as in this example, it places l on the FORCE list and makes l proto true (that is, true in context PT ). A proto true literal will remain fixed true throughout this round of lookahead, because all relevant values of T will be less than PT . Later, Algorithm L will promote proto truth to near truth, and ultimately to real truth — unless a contradiction arises. (And in the case of waerden (3, 3; 9), such a contradiction does in fact arise; see exercise 150 .)

Why does the combination of preorder and postorder work so magically in ( 70 )? It’s because of a basic property of forests in general, which we noted for example in exercise 2.3.2–20: If _u and v are nodes of a forest, u is a proper ancestor of v if and only if u precedes v in preorder and u follows v in postorder . Moreover, when we look ahead at candidate literals in this way, an important invariant relation is maintained on the _R stack, namely that truth degrees never increase as we move from the bottom to the top:

VAL [ | R j − 1 | ] | 1 ⁢     ≥    VAL [ | R j | ] , for  1  ≤ j < E . ( 71 )

Real truths appear at the bottom, then near truths, then proto truths, etc. For example, the stack at one point in the problem above contains seven literals,

j \= 0 1 2 3 4 5 6 R j \= 5 ¯ 6 4 ¯ 3 9 ¯ 7 1 VAL [ | R j | ] \= RT+1 14 13 12 13 12 12

One consequence is that the current visibility of truth values matches the recursive structure by which false literals are purged from ternary clauses.

The second phase of Algorithm X , after preselection of candidates, is called “ nesting,” because it constructs a lookahead forest analogous to ( 70 ). More precisely, it constructs a sequence of literals LL [ j ] and corresponding truth offsets LO [ j ], for 0 ≤ j < S. It also sets up PARENT pointers to indicate the forest structure more directly; for example, with ( 69 ) we would have PARENT ( 8 ¯ ) \= 2 and PARENT (2) = ∧.

The third phase, “ exploration,” now does the real work. It uses the lookahead forest to evaluate heuristics H ( l ) for the candidate literals — and also (if it’s lucky) to discover literals whose values are forced.

The heart of the exploration phase is a breadth-first search based on steps L5, L6, and L8. This routine propagates truth values of degree T and also computes w , the weight of new binary clauses that would be spawned by branching on l :

Set  l 0 ← l , i ← w ← 0 ,   and  G ← E ← F ;     perform   ( 62 ) ; while  G < E ,   set  L ← R G , G ← G + 1 ,   and take  account  of  ( u , υ )  for all   ( u , υ )  in  T I M P ( L ) ; ( 72 ) generate new binary  clauses  ( l ¯ 0 ∨ W k )  for  0 ≤ k < i .

Here “ take account of ( u, v )” means “if either u or v is fixed true (in context T ), do nothing; if both u and v are fixed false, go to CONFLICT ; if u is fixed false but v isn’t fixed, set W i ← v , i ← i + 1, and perform ( 62 ) with l ← v ; if v is fixed false but u isn’t fixed, set W i ← u , i ← i + 1, and perform ( 62 ) with l ← u ; if neither u nor v is fixed, set w ← w + h ( u ) h ( v ).”

Explanation : A ternary clause of the form l ¯ ∨ u ∨ v , where L is fixed true and u is fixed false as a consequence of l 0 being fixed true, is called a “ windfall.” Such clauses are good news, because they imply that the binary clause l ¯ 0 ∨ υ must be satisfied in the current subproblem. Windfalls are recorded on a stack called W , and appended to the BIMP database at the end of ( 72 ).

The exploration phase also exploits an important fact called the autarky principle , which generalizes the notion of “ pure literal” that we discussed above in connection with Algorithm A . An “autarky” for a SAT problem F is a set of strictly distinct literals A = { a 1 , ..., a t } with the property that every clause of F either contains at least one literal of A or contains none of the literals of A ¯ \= { a ¯ 1 , ... , a ¯ 2 } . In other words, A satisfies every clause that A or A ¯ “touches.”

An autarky is a self-sufficient system. Whenever A is an autarky, we can assume without loss of generality that all of its literals are actually true; for if F is satisfiable, the un touched clauses are satisfiable, and A tells us how to satisfy the touched ones. Step X9 of the following algorithm shows that we can detect certain autarkies easily while we’re looking ahead.

Algorithm X ( Lookahead for Algorithm L ). This algorithm, which is invoked in step L2 of Algorithm L , uses the data structures of that algorithm together with additional arrays of its own to explore properties of the current subproblem. It discovers U ≥ 0 literals whose values are forced, and puts them in the FORCE array. It terminates either by (i) satisfying all clauses; (ii) finding a contradiction; or (iii) computing heuristic scores H ( l ) that will allow step L3 to choose a good literal for branching. In case (iii) it might also discover new binary clauses.

X1. [Satisfied?] If F = n , terminate happily (no variables are free).

X2. [Compile rough heuristics.] Set N = n − F . For each free literal l, set VAL [l] ← 0, and use ( 65 ) to compute a rough score h ( l ).

X3. [Preselect candidates.] Let C be the current number of free variables that are “ participants,” and put them into the CAND array. If C = 0, set C ← N and put all free variables into CAND ; terminate happily, however, if all clauses are satisfied (see exercise 152 ). Give each variable x in CAND the rating r ( x ) \= h ( x ) h ( x ¯ ) . Then while C > 2 C max (see ( 66 )), delete all elements of CAND whose rating is less than the mean rating; but terminate this loop if no elements are actually deleted. Finally, if C > C max , reduce C to C max by retaining only top-ranked candidates. (See exercise 153 .)

X4. [Nest the candidates.] Construct a lookahead forest, represented in LL [ j ] and LO [ j ] for 0 ≤ j < S and by PARENT pointers (see exercise 155 ).

X5. [Prepare to explore.] Set U ′ ← j ′ BASE ← j ← 0 and CONFLICT ← X13.

X6. [Choose l for lookahead.] Set l ← LL [ j ] and T ← BASE + LO [ j ]. Set H( l ) ← H( PARENT ( l )), where H (∧) = 0. If l is not fixed in context T , goto X8. Otherwise, if l is fixed false but not proto false, do step X12 with l ← l ¯ .

X7. [Move to next.] If U > U ′, set U ′ ← U and j ′ ← j . Then set j ← j + 1. If j = S , set j ← 0 and BASE ← BASE + 2 S . Terminate normally if j = j′, or if j = 0 and BASE + 2 S ≥ PT (beware of overflow). Otherwise return to X6.

X8. [Compute sharper heuristic.] Perform ( 72 ). Then if w > 0, set H ( l 0 ) ← H ( l 0 ) + w and go to X10.

X9. [Exploit an autarky.] If H ( l 0 ) = 0, do step X12 with l ← l 0 . Otherwise generate the new binary clause l 0 ∨ ¬ PARENT ( l 0 ). ( Exercise 166 explains why.)

X10. [Optionally look deeper.] Perform Algorithm Y below.

X11. [Exploit necessary assignments.] Do step X12 for all literals l ∈ BIMP ( l ¯ 0 ) that are fixed true but not proto true. Then go to X7. (See exercise 167 .)

X12. [Force l .] Set FORCE [ U ] ← l , U ← U + 1, T ′ ← T , and perform ( 72 ) with T ← PT . Then set T ← T ′. (This step is a subroutine, used by other steps.)

X13. [Recover from conflict.] If T < PT , do step X12 with l ← l ¯ 0 and go to X7. Otherwise terminate with a contradiction.

Notice that, in steps X5–X7, this algorithm proceeds cyclically through the forest, continuing to look ahead until completing a pass in which no new forced literals are found. The BASE address of truth values continues to grow, if necessary, but it isn’t allowed to become too close to PT .

*Looking even further ahead

If it’s a good idea to look one step ahead, maybe it’s a better idea to look two steps ahead. Of course that’s a somewhat scary proposition, because our data structures are already pretty stretched; and besides, double lookahead might take way too much time. Nevertheless, there’s a way to pull it off, and to make Algorithm L run even faster on many problems.

Algorithm X looks at the immediate consequences of assuming that some literal l 0 is true. Algorithm Y , which is launched in step X10, goes further out on that limb, and investigates what would happen if another literal, l ^ 0 , were also true. The goal is to detect branches that die off early, allowing us to discover new implications of l 0 or even to conclude that l 0 must be false.

For this purpose Algorithm Y stakes out an area of truth space between the current context T and a degree of truth called “ double truth” or DT , which is defined in step Y2. The size of this area is determined by a parameter Y, which is typically less than 10. The same lookahead forest is used to give relative truth degrees below DT . Double truth is less trustworthy than proto truth, PT ; but literals that are fixed at level DT are known to be conditionally true (“ Dtrue”) or conditionally false (“ Dfalse”) under the hypothesis that l 0 is true.

Going back to our example of waerden (3, 3; 9), the scenario described above was based on the assumption that double lookahead was not done. Actually, however, further activity by Algorithm Y will usually take place after H (1) has been set to h ( 2 ¯ ) h ( 3 ¯ ) + h ( 4 ¯ ) h ( 7 ¯ ) . The value of DT will be set to 130, assuming that Y = 8, because S = 8. Literal 1 will become Dtrue. Looking then at 2 will 6fix 2; and that will 6fix 3 ¯ because of the clause 1 ¯ 2 ¯ 3 ¯ . Then 3 ¯ will 6fix 4 and 7, contradicting 1 ¯ 4 ¯ 7 ¯ and causing 2 to become Dfalse. Other literals also will soon become Dtrue or Dfalse, leading to a contradiction; and that contradiction will allow Algorithm Y to make literal 1 proto false before Algorithm X has even begun to look ahead at literal 2.

The main loop of double lookahead is analogous to ( 72 ), but it’s simpler, because we’re further removed from reality:

Set  l ^ 0 ← l  and  G ← G ← E ← F ;     perform   ( 62 ) ; while  G < E ,   set  L ← R G , G ← G + 1 ,   and ( 73 ) take  account  of  ( u , υ )  for all   ( u , υ )  in  T I M P ( L ) ;

Now “ take account of ( u , v )” means “if either u or v is fixed true (in context T ), or if neither u nor v is fixed, do nothing; if both u and v are fixed false, go to CONFLICT ; if u is fixed false but v isn’t fixed, perform ( 62 ) with l ← v; if v is fixed false but u isn’t fixed, perform ( 62 ) with l ← u .”

Since double-looking is costly, we want to try it only when there’s a fairly good chance that it will be helpful, namely when H ( l 0 ) is large. But how large is large enough? The proper threshold depends on the problem being solved: Some sets of clauses are handled more quickly by double-looking, while others are immune to such insights. Marijn Heule and Hans van Maaren [ LNCS 4501 (2007), 258–271] have developed an elegant feedback mechanism that automatically tunes itself to the characteristics of the problem at hand: Let τ be a “ trigger,” initially 0. Step Y1 allows double-look only if H ( l 0 ) > τ; otherwise τ is decreased to β τ, where β is a damping factor (typically 0.999), so that double-looking will become more attractive. On the other hand if double-look doesn’t find a contradiction that makes l 0 proto false, the trigger is raised to H ( l 0 ) in step Y6.

Algorithm Y ( Double lookahead for Algorithm X ). This algorithm, invoked in step X10, uses the same data structures (and a few more) to look ahead more deeply. Parameters β and Y are explained above. Initially DFAIL ( l ) = 0 for all l .

Y1. [Filter.] Terminate if DFAIL ( l 0 ) = ISTAMP , or if T + 2 S ( Y + 1) > PT . Otherwise, if H ( l 0 ) ≤ τ, set τ ← β τ and terminate.

Y2. [Initialize.] Set BASE ← T − 2, LBASE ← BASE + 2 S · Y , DT ← LBASE + LO [ j ], i ← j ^ ′ ← j ^ ← 0 , E ← F , and CONFLICT ← Y8. Perform ( 62 ) with l ← l 0 and T ← DT .

Y3. [Choose l for double look.] Set l ← LL [ j ^ ] and T ← BASE + LO [ j ^ ]. If l is not fixed in context T , go to Y5. Otherwise, if l is fixed false but not Dfalse, do step Y7 with l ← l ^ .

Y4. [Move to next.] Set j ^ ← j ^ + 1. If j ^ \= S , set j ^ ← 0 and BASE ← BASE + 2 S . Go to Y6 if j ^ ′ = j ^ , or if j ^ \= 0 and BASE = LBASE . Otherwise return to Y3.

Y5. [Look ahead.] Perform ( 73 ), and return to Y4 (if no conflict arises).

Y6. [Finish.] Generate new binary clauses ( l ¯ 0 ∨ W k ) for 0 ≤ k < i . Then set BASE ← LBASE , T ← DT , τ ← H ( l 0 ), DFAIL ( l 0 ) ← ISTAMP , CONFLICT ← X13, and terminate.

Y7. [Assume also l.] Set j ^ ′ ← j ^ , T ′ T , and perform ( 73 ) with T ← DT . Then set T ← T ′, W i ← l ^ 0 , i ← i + 1. (This step is a subroutine.)

Y8. [Recover from conflict.] If T < DT , do step Y7 with l ← ¬ LL [ j ^ ] and go to Y4. Otherwise set BASE ← LBASE , CONFLICT ← X13, and exit to X13.

Some quantitative statistics will help to ground these algorithms in reality: When Algorithm L was let loose on rand (3, 2062, 500, 314), a problem with 500 variables and 2062 random ternary clauses, it proved unsatisfiability after making 684,433,234,661 memory accesses and constructing a search tree of 9,530,489 nodes. Exercise 173 explains what would have happened if various parts of the algorithm had been disabled. None of the other SAT solvers we shall discuss are able to handle such large random problems in a reasonable amount of time.

Random satisfiability

There seems to be no easy way to analyze the satisfiability problem under random conditions. In fact, the basic question “How many random clauses of 3SAT on n variables do we need to consider, on the average, before they can’t all be satisfied?” is a famous unsolved research problem.

From a practical standpoint this question isn’t as relevant as the analogous questions were when we studied algorithms for sorting or searching, because real-world instances of 3SAT tend to have highly non random clauses. Deviations from randomness in combinatorial algorithms often have a dramatic effect on running time, while methods of sorting and searching generally stay reasonably close to their expected behavior. Thus a focus on randomness can be misleading. On the other hand, random SAT clauses do serve as a nice, clean model, so they give us insights into what goes on in Boolean territory. Furthermore the mathematical issues are of great interest in their own right. And fortunately, much of the basic theory is in fact elementary and easy to understand. So let’s take a look at it.

Exercise 180 shows that random satisfiability can be analyzed exactly , when there are at most five variables. We might as well start there, because the “tiny” 5-variable case is still large enough to shed some light on the bigger picture. When there are n variables and k literals per clause, the number N of possible clauses that involve k different variables is clearly 2 k ( n k ) : There are ( n k ) ways to choose the variables, and 2 k ways to either complement or not. So we have, for example, N \= 2 3 ( 5 3 ) \= 80 possible clauses in a 3SAT problem on 5 variables.

Let q m be the probability that m of those clauses, distinct but otherwise selected at random, are satisfiable. Thus q m \= Q m / ( N m ) , where Q m is the number of ways to choose m of the N clauses so that at least one Boolean vector x = x 1 ... x n satisfies them all. Figure 83 illustrates these probabilities when k = 3 and n = 5. Suppose we’re being given distinct random clauses one by one. According to Fig. 83 , the chances are better than 77% that we’ll still be able to satisfy them after 20 different clauses have been received, because q 20 ≈ 0.776. But by the time we’ve accumulated 30 of the 80 clauses, the chance of satisfiability has dropped to q 30 ≈ 0.179; and after ten more we reach q 40 ≈ 0.016.

images

Fig. 83. The probability q m that m distinct clauses of 3SAT on 5 variables are simultaneously satisfiable, for 0 ≤ m ≤ 80.

The illustration makes it appear as if q m = 1 for m < 15, say, and as if q m = 0 for m > 55. But q 8 is actually less than 1, because of ( 6 ); exercise 179 gives the exact value. And q 70 is greater than 0, because Q 70 = 32; indeed, every Boolean vector x satisfies exactly ( 2 k − 1 ) ( n k ) \= ( 1 − 2 − k ) N of the N possible k-clauses, so it’s no surprise that 70 noncontradictory 3-clauses on 5 variables can be found. Of course those clauses will hardly ever be the first 70 received, in a random situation. The actual value of q 70 is 32/1646492110120 ≈ 2 × 10 −11 .

images

Fig. 84. The total number T m of different Boolean vectors x = x 1 ... x 5 that simultaneously satisfy m distinct clauses of 3SAT on 5 variables, for 0 ≤ m ≤ 80.

Figure 84 portrays the same process from another standpoint: It shows in how many ways a random set of m clauses can be satisfied. This value, T m , is a random variable whose mean is indicated in black, surrounded by a gray region that shows the mean plus-or-minus the standard deviation. For example, T 0 is always 32, and T 1 is always 28; but T 2 is either 24, 25, or 26, and it takes these values with the respective probabilities (2200, 480, 480)/3160. Thus the mean for m = 2 is ≈ 24.5, and the standard deviation is ≈ 0.743.

When m = 20, we know from Fig. 83 that T 20 is nonzero more than 77% of the time; yet Fig. 84 shows that T 20 ≈ 1.47 ± 1.17. (Here the notation μ ± σ stands for the mean value μ with standard deviation σ.) It turns out, in fact, that 20 random clauses are uniquely satisfiable , with T 20 = 1, more than 33% of the time; and the probability that T 20 > 4 is only 0.013. With 30 clauses, satisfiability gets dicier and dicier: T 30 ≈ 0.20 ± 0.45; indeed, T 30 is less than 2, more than 98% of the time — although it can be as high as 11 if the clause-provider is being nice to us. By the time 40 clauses are reached, the odds that T 40 exceeds 1 are less than 1 in 4700. Figure 85 shows the probability that T m = 1 as m varies.

images

Fig. 85. Pr( T m = 1), the probability that m distinct clauses of 3SAT on 5 variables are uniquely satisfiable, for 0 ≤ m ≤ 80.

Let P be the number of clauses that have been received when we’re first unable to satisfy them all. Thus we have P = m with probability p m , where p m = q m − 1 − q m is the probability that m − 1 random clauses are satisfiable but m are not. These probabilities are illustrated in Fig. 86 . Is it surprising that Figs. 85 and 86 look roughly the same? (See exercise 183 .)

The expected “ stopping time,” E P , is by definition equal to Σ m   m p m ; and it’s not difficult to see, for example by using the technique of summation by parts (exercise 1.2.7–10), that we can compute it by summing the probabilities in Fig. 83 :

E     P \= Σ m   q m . ( 74 )

images

Fig. 86. The stopping time probabilities, p m , that m distinct clauses of 3SAT on 5 variables have just become unsatisfiable, for 0 ≤ m ≤ 80.

The variance of P , namely E( P − E P ) 2 = (E P 2 ) − (E P ) 2 , also has a simple expression in terms of the q ’s, because

E     P 2 \= Σ m   ( 2 m + 1 ) q m . ( 75 )

In Figs. 83 and 86 we have E P ≈ 25.22, with variance ≈ 35.73.

So far we’ve been focusing our attention on 3SAT problems, but the same ideas apply also to k SAT for other clause sizes k . Figure 87 shows exact results for the probabilities when n = 5 and 1 ≤ k ≤ 4. Larger values of k give clauses that are easier to satisfy, so they increase the stopping time. With five variables the typical stopping times for random 1SAT, 2SAT, 3SAT, and 4SAT turn out to be respectively 4.06 ± 1.19, 11.60 ± 3.04, 25.22 ± 5.98, and 43.39 ± 7.62. In general if P k, n is the stopping time for k SAT on n variables, we let

S k , n \= E       P k , n ( 76 )

be its expected value.

images

Fig. 87. Extension of Fig. 83 to clauses of other sizes.

Our discussions so far have been limited in another way too: We’ve been assuming that m distinct clauses are being presented to a SAT solver for solution. In practice, however, it’s much easier to generate clauses by allowing repetitions, so that every clause is chosen without any dependence on the past history. In other words, there’s a more natural way to approach random satisfiability, by assuming that N m possible ordered sequences of clauses are equally likely after m steps, not that we have ( N m ) equally likely sets of clauses.

Let q ^ m be the probability that m random clauses C 1 ∧ ... ∧ C m are satisfiable, where each C j is randomly chosen from among the N \= 2 k ( n k ) possibilities in a k SAT problem on n variables. Figure 88 illustrates these probabilities in the case k = 3, n = 5; notice that we always have q ^ m ≥ q m . If N is large while m is small, it’s clear that q ^ m will be very close to q m , because repeated clauses are unlikely in such a case. Still, we must keep in mind that q N is always zero, while q ^ m is never zero. Furthermore, the “ birthday paradox” discussed in Section 6.4 warns us that repetitions aren’t as rare as we might expect. The deviations of q ^ m from q m are particularly noticeable in small cases such as the scenario of Fig. 88 .

images

Fig. 88. Random 3SAT on 5 variables when the clauses are sampled with replacement. The probabilities q ^ m are shown with a black line; the smaller probabilities q m of Fig. 83 are shown in gray.

In any event, there’s a direct way to compute q ^ m from the probabilities q t and the value of N (see exercise 184 ):

q ^ m \= Σ t \= 0 N { m t } t ! q t ( N t ) / N m . ( 77 )

And there are surprisingly simple formulas analogous to ( 74 ) and ( 75 ) for the stopping time P ^ , where P ^ m \= q ^ m − 1 − q ^ m , as shown in exercise 186 :

E  P ^ \= Σ m \= 0 N − 1 N N − m q m ; ( 78 )

E  P ^ 2 \= Σ m \= 0 N − 1 N N − m q m ( 1 + 2 ( N N − 1 + ... + N N − m ) ) . ( 79 )

These formulas prove that the expected behavior of P ^ is very much like that of P , if q m is small whenever m/N isn’t small. In the case k = 3 and n = 5, the typical stopping times P ^ \= 30.58 ± 9.56 are significantly larger than those of P ; but we are mostly interested in cases where n is large and where q ^ m is essentially indistinguishable from q m . In order to indicate plainly that the probability q ^ m depends on k and n as well as on m , we shall denote it henceforth by S k ( m, n ):

S k ( m , n ) \= Pr ( m   random  clauses  of  k SAT are  satisfiable ) , ( 80 )

where the m clauses are “sampled with replacement” (they needn’t be distinct). Suitable pseudorandom clauses rand ( k , m , n , seed ) can easily be generated.

Exact formulas appear to be out of reach when n > 5, but we can make empirical tests. For example, extensive experiments on random 3SAT problems by B. Selman, D. G. Mitchell, and H. J. Levesque [ Artificial Intelligence 81 (1996), 17–29] showed a dramatic drop in the chances of satisfiability when the number of clauses exceeds about 4.27 n . This “ phase transition” becomes much sharper as n grows (see Fig. 89 ).

Similar behavior occurs for random k SAT, and this phenomenon has spawned an enormous amount of research aimed at evaluating the so-called satisfiability thresholds

α k \= lim n → ∞   S k , n / n . ( 81 )

images

Fig. 89. Empirical probability data shows that random 3SAT problems rapidly become unsatisfiable when there are more than α 3 n clauses, if n is large enough.

Indeed, we can obtain quite difficult k SAT problems by generating approximately α k n random k -clauses, using empirically observed estimates of α k . If n is large, the running time for random 3SAT with 4.3 n clauses is typically orders of magnitude larger than it is when the number of clauses is 4 n or 4.6 n . (And still tougher problems arise in rare instances when we have, say, 3.9 n clauses that happen to be unsatisfiable .)

Strictly speaking, however, nobody has been able to prove that the so-called constants α k actually exist, for all k! The empirical evidence is overwhelming; but rigorous proofs for k = 3 have so far only established the bounds

lim   inf n → ∞   S 3 , n / n ≥ 3.52 ; lim  sup n → ∞   S 3 , n / n ≤ 4.49. ( 82 )

[See M. Hajiaghayi and G. B. Sorkin, arXiv:math/0310193 [math.CO] (2003), 8 pages; A. C. Kaporis, L. M. Kirousis, and E. G. Lalas, Random Struct. & Alg. 28 (2006), 444–480; J. Díaz, L. Kirousis, D. Mitsche, and X. Pérez-Giménez, Theoretical Comp. Sci. 410 (2009), 2920–2934.] A “ sharp threshold” result has been established by E. Friedgut [ J. American Math. Soc. 12 (1999), 1017–1045, 1053–1054], who proved the existence for k ≥ 2 of functions α k (n) with

lim n → ∞   S k ( ⌊ ( α k ( n ) − ϵ ) n ⌋ , n ) \= 1 , lim n → ∞   S k ( ⌊ ( α k ( n ) + ϵ ) n ⌋ , n ) \= 0 , ( 83 )

when ε is any positive number. But those functions might not approach a limit. They might, for example, fluctuate periodically, like the “ wobble function” that we encountered in Eq. 5.2.2 (47).

The current best guess for α 3, based on heuristics of the “ survey propagation” technique to be discussed below, is that α 3 = 4.26675±0.00015 [S. Mertens, M. Mézard, and R. Zecchina, Random Structures & Algorithms 28 (2006), 340–373]. Similarly, it appears reasonable to believe that α 4 ≈ 9.931, α 5 ≈ 21.12, α 6 ≈ 43.37, α 7 ≈ 87.79. The α ’ s grow as Θ(2 k ) (see exercise 195 ); and they are known to be constant when k is sufficiently large [see J. Ding, A. Sly, and N. Sun, STOC 47 (2015), 59.68].

Analysis of random 2SAT

Although nobody knows how to prove that random 3SAT problems almost always become unsatisfiable when the number of clauses reaches ≈ 4.27 n , the corresponding question for 2SAT does have a nice answer: The satisfiability threshold α 2 equals 1. For example, when the author first tried 1000 random 2SAT problems with a million variables, 999 of them turned out to be satisfiable when there were 960,000 clauses, while all were unsatisfiable when the number of clauses rose to 1,040,000. Figure 90 shows how this transition becomes sharper as n increases.

images

Fig. 90. Empirical satisfaction probabilities for 2SAT with approximately n random clauses. (When n = 100, the probability doesn’t become negligible until more than roughly 180 clauses have been generated.)

The fact that S 2, n ≈ n was discovered in 1991 by V. Chvátal and B. Reed [ FOCS 33 (1992), 620–627], and the same result was obtained independently at about the same time by A. Goerdt and by W. Fernandez de la Vega [see J. Comp. Syst. Sci. 53 (1996), 469–486; Theor. Comp. Sci. 265 (2001), 131–146].

The study of this phenomenon is instructive, because it relies on properties of the digraph that characterizes all instances of 2SAT. Furthermore, the proof below provides an excellent illustration of the “first and second moment principles,” equations MPR–( 21 ) and MPR–( 22 ). Armed with those principles, we’re ready to derive the 2SAT threshold:

Theorem C. Let c be a fixed constant. Then

lim n → ∞ S 2 ( ⌊ cn ⌋ , n ) \= { 1 , if   c < 1 ; 0 , ifc > 1. ( 84 )

Proof. Every 2SAT problem corresponds to an implication digraph on the literals, with arcs l ¯ → l ′ and l ¯ ′ → l for each clause l ∨ l ′. We know from Theorem 7.1.1K that a set of 2SAT clauses is satisfiable if and only if no strong component of its implication digraph contains both x and x ¯ for some variable x . That digraph has 2 m = 2⌊ cn ⌋ arcs and 2n vertices. If it were a random digraph, well-known theorems of Karp (which we shall study in Section 7.4.4) would imply that only O(log n ) vertices are reachable from any given vertex when c < 1, but that there is a unique “ giant strong component” of size Ω( n ) when c > 1.

The digraph that arises from random 2SAT isn’t truly random, because its arcs come in pairs, u → v and v ¯ → u ¯ . But intuitively we can expect that similar behavior will apply to digraphs that are just halfway random. For example, when the author generated a random 2SAT problem with n = 1000000 and m = .99n, the resulting digraph had only two complementary pairs of strong components with more than one vertex, and their sizes were only 2, 2 and 7, 7; so the clauses were easily satisfiable. Adding another .01n clauses didn’t increase the number of nontrivial strong components, and the problem remained satisfiable. But another experiment with m = n = 1000000 yielded a strong component of size 420, containing 210 variables and their complements; that problem was unsatisfiable.

Based on a similar intuition into the underlying structure, Chvátal and Reed introduced the following “ snares and snakes” approach to the proof of Theorem C: Let’s say that an s-chain is any sequence of s strictly distinct literals; thus there are 2 s n s possible s -chains. Every s -chain C corresponds to clauses

( l ¯ 1 ∨ l 2 ) , ( l ¯ 2 ∨ l 3 ) ,   .   .   .   ,   ( l ¯ s − 1 ∨ l s ) ( 85 )

which in turn correspond to two paths

l 1 → l 2 → l 3 → ⋅ ⋅ ⋅ → l s                         and                           l ¯ s → ⋅ ⋅ ⋅ → l ¯ 3 → l ¯ 2 → l ¯ 1 ( 86 )

in the digraph. An s-snare ( C; t, u ) consists of an s -chain C and two indices t and u , where 1 < t ≤ s and 1 ≤ | u | < s ; it specifies the clauses ( 85 ) together with

( l t ∨ l 1 )                                           and                                   ( l ¯ s ∨ l u )   if   u > 0 , ( l ¯ s ∨ l ¯ − u )     if   u < 0 , ( 87 )

representing l ¯ t → l 1 and either l s → l | u | or l s → l ¯ |   u |   . The number of possible s-snares is 2 s +1 ( s − 1) 2 n s . Their clauses are rarely all present when m is small.

Exercise 200 explains how to use these definitions to prove Theorem C in the case c < 1. First we show that every unsatisfiable 2SAT formula contains all the clauses of at least one snare. Then, if we define the binary random variable

X ( C ; t , u ) \= [ all     clauses     of   ( C ; t , u )     are     present ] , ( 88 )

it isn’t difficult to prove that the snares of every s -chain C are unlikely:

E X ( C ; t , u )   ≤ m s + 1 / ( 2 n ( n − 1 ) ) s + 1 . ( 89 )

Finally, letting X be the sum of X ( C ; t , u ) over all snares, we obtain

E     X \= Σ   E     X   ( C ; t , u ) ≤ Σ s ≥ 0 2 s + 1   s ( s − 1 ) n s     ( m 2 n ( n − 1 ) ) s + 1 \= 2 n ( m n − 1 − m ) 3

by Eq. 1.2.9–(20). This formula actually establishes a stronger form of ( 84 ), because it shows that E X is only O ( n −1/4 ) when m = n − n 3/4 > cn . Thus

S 2 ( ⌊ n − n 3 / 4 ⌋ , n ) ≥ Pr ( X \= 0 ) \= 1 − Pr ( X > 0 ) ≥ 1 − O ( n − 1 / 4 ) ( 90 )

by the first moment principle.

The other half of Theorem C can be proved by using the concept of a t-snake , which is the special case ( C ; t , − t ) of a (2 t − 1)-snare. In other words, given any chain ( l 1 ,..., l t ,..., l 2 t −1 ), with s = 2 t − 1 and l t in the middle, a t -snake generates the clauses ( 85 ) together with ( l t ∨ l 1 ) and ( l ¯ s ∨ l ¯ t ) . When t = 5, for example, and ( l 1 ,..., l 2 t −1 ) = ( x 1 ,..., x 9 ), the 2 t = 10 clauses are

51 , 1 ¯ 2 , 2 ¯ 3 , 3 ¯ 4 , 4 ¯ 5 , 5 ¯ 6 , 6 ¯ 7 , 7 ¯ 8 , 8 ¯ 9 , 9 ¯ 5 ¯

images

and they correspond to 20 arcs that loop around to form a strong component as shown here. We will prove that, when c > 1 in ( 84 ), the digraph almost always contains such impediments to satisfiability.

Given a (2 t − 1)-chain C , where the parameter t will be chosen later, let

X C \= [ each   clause   of   ( C ; t , − t )   occurs     exactly     once ] . ( 91 )

The expected value E X C is clearly f (2 t ), where

f ( r ) \= m r − ( 2 n ( n − 1 ) − r ) m − r / ( 2 n ( n − 1 ) ) m ( 92 )

is the probability that r specific clauses occur once each. Notice that

f ( r ) \= ( m 2 n ( n − 1 ) ) r   ( 1 + O ( r 2 m ) + O ( rm n 2 ) ) ; ( 93 )

thus the relative error will be O ( t 2 / n ) if m = Θ( n ) as n → ∞.

Now let X = ∑ X C , summed over all R = 2 2 t −1 n 2 t −1 possible t - snakes C ; thus E X = Rf (2 t ). We want to show that Pr( X > 0) is very nearly 1, using the second moment principle; so we want to show that the expectation E X 2 = (∑ C X C ) (∑ D X D ) = ∑ C ∑ D E X C X D is small. The key observation is that

E      X C X D \= f ( 4 t − r )                               if   C   and   D     have   exactly     r     clauses     in     common . ( 94 )

Let p r be the probability that a randomly chosen t -snake has exactly r clauses in common with the fixed snake ( x 1 ,..., x 2 t −1 ). Then

E   X 2 ( E   X ) 2 \= R 2 Σ r \= 0 2 t   p r     f ( 4 t − r ) R 2 f ( 2 t ) 2 \= Σ r \= 0 2 t p r f ( 4 t − r ) f ( 2 t ) 2 \= Σ r \= 0 2 t p r ( 2 n ( n − 1 ) m ) r ( 1 + O ( t 2 n ) ) . ( 95 )

By studying the interaction of snakes (see exercise 201 ) one can prove that

( 2 n ) r p r \= O ( t 4 / n ) + O ( t ) [ r ≥ t ] + O ( n ) [ r \= 2 t ] , for     1 ≤ r ≤ 2 t . ( 96 )

Finally then, as explained in exercise 202 , we can choose t = ⌊ n 1/5 ⌋ and m = ⌊ n + n 5/6 ⌋, to deduce a sharper form of ( 84 ) when c > 1:

S 2 ( ⌊ n + n 5 / 6 ⌋ , n ) \= O ( n − 1 / 30 ) . ( 97 )

(Deep breath.) Theorem C is proved.

Much more precise results have been derived by B. Bollobás, C. Borgs, J. T. Chayes, J. H. Kim, and D. B. Wilson, in Random Structures & Algorithms 18 (2001), 201–256. For example, they showed that

S 2 ( ⌊ n − n 3 / 4 ⌋ , n ) \= exp ( − Θ ( n − 1 / 4 ) ) ;         S 2 ( ⌊ n + n 3 / 4 ⌋ , n ) \= exp ( − Θ ( n 1 / 4 ) ) . ( 98 )

Resolution

The backtracking process of Algorithms A , B , D , and L is closely connected to a logical proof procedure called resolution . Starting with a family of clauses called “axioms,” there’s a simple rule by which new clauses can be derived from this given set: Whenever both x ∨ A ′ and x ¯ ∨ A " are in our repertoire of clauses, we’re allowed to derive the “resolvent” clause A = A ′ ∨ A ″, denoted by ( x ∨ A ′ ) ◊ ( x ¯ ∨ A " ) . (See exercises 218 and 219 .)

A proof by resolution consists of a directed acyclic graph (dag) whose vertices are labeled with clauses in the following way: (i) Every source vertex is labeled with an axiom. (ii) Every other vertex has in-degree 2. (iii) If the predecessors of vertex v are v ′ and v ″, the label of v is C ( v )= C( v ′) ◊ C ( v ″).

When such a dag has a sink vertex labeled A, we call it a “resolution proof of A”; and if A is the empty clause, the dag is also called a “resolution refutation.”

The dag of a proof by resolution can be expanded to a binary tree, by replicating any vertex that has out-degree greater than 1. Such a tree is said to be regular if no path from the root to a leaf uses the same variable twice to form a resolvent. For example, Fig. 91 is a regular resolution tree that refutes Rivest’s unsatisfiable axioms ( 6 ). All arcs in this tree are directed upwards.

images

Fig. 91. One way to derive ∊ by resolving the inconsistent clauses ( 6 ).

Notice that Fig. 91 is essentially identical to Fig. 82 on page 217 , the backtrack tree by which Algorithm D discovers that the clauses of ( 6 ) are unsatisfiable. In fact this similarity is no coincidence: Every backtrack tree that records the behavior of Algorithm D on a set of unsatisfiable clauses corresponds to a regular resolution tree that refutes those axioms , unless Algorithm D makes an unnecessary branch. (An unnecessary branch occurs if the algorithm tries x ← 0 and x ← 1 without using their consequences to discover an unsatisfiable subset of axioms.) Conversely, every regular refutation tree corresponds to a sequence of choices by which a backtrack-based SAT solver could prove unsatisfiability.

The reason behind this correspondence isn’t hard to see. Suppose both values of x need to be tried in order to prove unsatisfiability. When we set x ← 0 in one branch of the backtrack tree, we replace the original clauses F by F | x ¯ , as in ( 54 ). The key point is that we can prove the empty clause by resolution from F | x ¯ if and only if we can prove x by resolution from F without resolving on x . (See exercise 224 .) Similarly, setting x ← 1 corresponds to changing the clauses from F to F | x .

Consequently, if F is an inconsistent set of clauses that has no short refutation tree, Algorithm D cannot conclude that those clauses are unsatisfiable unless it runs for a long time. Neither can Algorithm L , in spite of enhanced lookahead.

R. Impagliazzo and P. Pudlák [ SODA 11 (2000), 128–136] have introduced an appealing Prover– Delayer game , with which it’s relatively easy to demonstrate that certain sets of unsatisfiable clauses require large refutation trees. The Prover names a variable x, and the Delayer responds by saying either x ← 0 or x ← 1 or x ← ∗. In the latter case the Prover gets to decide the value of x ; but the Delayer scores one point. The game ends when the current assignments have falsified at least one clause. If the Delayer has a strategy that guarantees a score of at least m points, exercise 226 shows that every refutation tree has at least 2 m leaves; hence at least 2 m − 1 resolutions must be done, and every backtrack-based solver needs Ω(2 m ) operations to declare the clauses unsatisfiable.

We can apply their game, for example, to the following interesting clauses:

( x ¯ j ⁢ j ) , for ⁢     1 ⁢ ≤ j ≤ m ; ( 99 ) ( x ¯ j ⁢ j ∨ x ¯ j ⁢ k ∨ x i ⁢ k ) , for ⁢     1 ⁢ ≤ i , j , k ≤ m ; ( 100 ) ( x j 1 ⁢   ∨ x j 2 ∨ ⋅ ⋅ ⋅ ∨   x j ⁢ m ) , for ⁢     1 ⁢   ≤ j ≤ m . ( 101 )

There are m 2 variables x jk , for 1 ≤ j , k ≤ m , which we can regard as the incidence matrix for a binary relation ‘ j ≺ k ’. With this formulation, ( 99 ) says that the relation is irreflexive, and ( 100 ) says that it’s transitive; thus, ( 99 ) and ( 100 ) amount to saying that j ≺ k is a partial ordering . Finally, ( 101 ) says that, for every j , there’s a k with j ≺ k . So these clauses state that there’s a partial ordering on {1,..., m } in which no element is maximal; and they can’t all be satisfied.

We can, however, always score m − 1 points if we’re playing Delayer in that game, by using the following strategy suggested by Massimo Lauria: At every step we know an ordered set S of elements, regarded as “small”; initially S = ∅, and we’ll have S = { j 1 ,..., j s } when our score is s. Suppose the Prover queries x jk , and s < m −2. If j = k , we naturally reply that x jk ← 0. Otherwise, if j ∉ S and k ∉ S , we respond x jk ← ∗; then s ← s + 1, and j s ← j or k according as the Prover specifies x jk ← 1 or x jk ← 0. Otherwise, if j ∈ S and k ∉ S , we respond x jk ← 1; if j ∉ S and k ∈ S , we respond x jk ← 0. Finally, if j = j a ∈ S and k = j b ∈ S , we respond x jk ← [ a < b ]. These responses always satisfy ( 99 ) and ( 100 ). And no clause of ( 101 ) becomes false until the Delayer is finally asked a question with s = m − 2. Then the response x jk ← ∗ gains another point. We’ve proved

Theorem R. Every refutation tree for the clauses ( 99 ), ( 100 ), ( 101 ) represents at least 2 m −1 − 1 resolution steps.

On the other hand, those clauses do have a refutation dag of size O ( m 3 ). Let I j and T ijk stand for the irreflexivity and transitivity axioms ( 99 ) and ( 100 ); and let M jk = x j 1 ∨ ‧‧‧ ∨ x jk , so that ( 101 ) is M jm . Then we have

M i ⁢ m ◊ ⁢     T i ⁢ m ⁢ k \= M i ( m − 1 ) ∨ x ¯ m ⁢ k , for ⁢       1 ≤ i , k < m . ( 102 )

Calling this new clause M i ⁢ m ⁢ k ′ , we can now derive

M j ( m − 1 ) \= ( ( ⋅ ⋅ ⋅ ( ( M m ⁢ m ◊ ⁢ M j ⁢ m 1 ′ ) ◊ ⁢ M j ⁢ m 2 ′ ) ◊ ⋅ ⋅ ⋅ ) ◊ ⁢ M j ⁢ m ( m − 1 ) ′ ) ◊ ⁢ I m ,

for 1 ≤ j < m . Hence ( m − 1) 2 +( m − 1) m resolutions have essentially reduced m to m − 1. Eventually we can therefore derive M 11 ; then M 11 ◊ I 1 = ∊ . [This elegant refutation is due to G. Stålmarck, Acta Informatica 33 (1996), 277–280.]

The method we’ve just used to obtain M j ( m −1) from M mm is, incidentally, a special case of a useful general formula called hyperresolution that is easily proved by induction on r :

( ⋅ ⋅ ⋅ ( ( C 0 ∨ x 1 ∨ ⋅ ⋅ ⋅ ∨ x r ) ◊ ( C 1 ∨ x ¯ 1 ) ) ◊ ⋅ ⋅ ⋅ )   ◊   ( C r ∨ x ¯ r )                                                                                                                                                                                                                                                 \= C 0 ∨ C 1 ∨ ⋅ ⋅ ⋅ ∨   C r . ( 103 )

* Lower bounds for general resolution

Let’s change our perspective slightly: Instead of visualizing a proof by resolution as a directed graph, we can think of it as a “straight line” resolution chain , analogous to the addition chains of Section 4.6.3 and the Boolean chains of Section 7.1.2. A resolution chain based on m axioms C 1 , ... , C m appends additional clauses C m +1 , ... , C m + r , each of which is obtained by resolving two previous clauses of the chain. Formally, we have

C i \= C j ( i ) ◊C k ( i ) , for ⁢ ⁢   m + 1 ≤ i ≤ m + r , ( 104 )

where 1 ≤ j ( i ) < i and 1 ≤ k ( i ) < i . It’s a refutation chain for C 1 , ... , C m if C m + r \= ∊ . The tree in Fig. 91 , for example, yields the refutation chain

12 3 ¯ , 23 4 ¯ , 341 , 4 1 ¯ 2 , 1 ¯ 2 ¯ 3 , 2 ¯ 3 ¯ 4 , 3 ¯ 4 ¯ 1 ¯ , 4 ¯ 1 2 ¯ , 123 , 1 2 ¯ 4 , 1 ¯ 2 4 ¯ , 1 ¯ 2 ¯ 3 ¯ , 12 , 1 2 ¯ , 1 ¯ 2 ¯ , 1 ¯ 2 , 1 , 1 ¯ , ∈

for the axioms ( 6 ); and there are many other ways to refute those axioms, such as

12 3 ¯ , 23 4 ¯ , 341 , 4 1 ¯ 2 , 1 ¯ 2 ¯ 3 , 2 ¯ 3 ¯ 4 , 3 ¯ 4 ¯ 1 ¯ , 4 ¯ 1 2 ¯ , 1 2 ¯ 3 ¯ , 1 3 ¯ , 14 , 3 ¯ 4 ¯ , 24 , 2 4 ¯ , 2 , 1 ¯ 3 , 3 ¯ 4 , 1 4 ¯ , 3 ¯ , 1 , 1 ¯ , ∈ ( 105 )

This chain is quite different from Fig. 91 , and perhaps nicer: It has three more steps, but after forming ‘ 1 2 ¯ 3 ¯ ’ it constructs only very short clauses.

We’ll see in a moment that short clauses are crucial if we want short chains. That fact turns out to be important when we try to prove that certain easily understood families of axioms are inherently more difficult than ( 99 ), ( 100 ), and ( 101 ), in the sense that they can’t be refuted with a chain of polynomial size.

Consider, for example, the well-known “ pigeonhole principle,” which states that m+1 pigeons don’t fit in m pigeon-sized holes. If x jk means that pigeon j occupies hole k , for 0 ≤ j ≤ m and 1 ≤ k ≤ m , the relevant unsatisfiable clauses are

( x j 1 ∨ x j ⁢ 2 ∨ ⋅ ⋅ ⋅ ∨   x j ⁢ m ) , for ⁢     0 ≥ j ≤ m ; ( 106 ) ( x ¯ i ⁢ k ∨ x ¯ j ⁢ k ) , for ⁢       0 ⁢ ≤ i < j ≤ m ⁢       and ⁢     1 ≤ k ≤ m . ( 107 )

(“Every pigeon has a hole, but no hole hosts more than one pigeon.”) These clauses increased the pigeonhole principle’s fame during the 1980s, when Armin Haken [ Theoretical Computer Science 39 (1985), 297–308] proved that they have no short refutation chain. His result marked the first time that any set of clauses had been shown to be intractable for resolution in general.

It is absolutely necessary that two people have equally many hairs .
— JEAN APPIER HANZELET, Recreation Mathematicque (1624)

Haken’s original proof was rather complicated. But simpler approaches were eventually found, culminating in a method by E. Ben-Sasson and A. Wigderson [ JACM 48 (2001), 149–169], which is based on clause length and applies to many other sets of axioms. If α is any sequence of clauses, let us say that its width , written w ( α ), is the length of its longest clause or clauses. Furthermore, if α 0 = ( C 1 ,..., C m ), we write w (α 0 ⊢ ∊ ) for the minimum of w (α) over all refutation chains α = ( C 1 ,..., C m + r ) for α 0 , and ∥ α 0 ⊢ ∊ ∥ for the minimum length r of all such chains. The following lemma is the key to proving lower bounds with Ben-Sasson and Wigderson’s strategy:

Lemma B. ∥ α 0 ⊢ ∊ ∥ ≥ e ( w ( α 0 ⊢ ∊ )−1) 2 /(8 n ) − 2, for clauses in n ≥ w ( α 0 ) 2 variables . Thus there’s exponential growth if we have w ( α 0 ) = O (1) and w ( α 0 ⊢ ∊ ) = Ω( n ).

Proof. Let α = ( C 1 ,..., C m + r ) be a refutation of α 0 with r = ∥ α 0 ⊢ ∊ ∥. We will say that a clause is “fat” if its length is W or more, where W ≥ w ( α 0 ) is a parameter to be set later. If α\ α 0 contains f fat clauses, those clauses contain at least Wf literals; hence some literal l appears in at least Wf /(2 n ) of them.

Now α | l , the chain obtained by replacing each clause C j by C j | l , is a refutation of α 0 | l that contains at most ⌊ ρf ⌋ fat clauses, where ρ = 1 − W /(2 n ). (The clause C j | l will be ℘ if l ∈ C j , thus tautological and effectively absent.)

Suppose f < ρ − b for some integer b . We will prove, by induction on b and secondarily on the total length of all clauses, that there’s a refutation β of α 0 such that w ( β ) ≤ W + b . This assertion holds when b = 0, since W ≥ w ( α 0 ). If b > 0, there’s a refutation β 0 of α 0 | l with w ( β 0 ) ≤ W + b − 1, when we choose l as above, because ρf < ρ 1− b and α | l refutes α 0 | l . Then we can form a resolution chain β 1 that derives l ¯ from α 0 , by inserting l ¯ appropriately into clauses of β 0 . And there’s a simple chain β 2 that derives the clauses of α 0 | l ¯ from α 0 and l ¯ . There’s also a refutation β 3 of α 0 | l ¯ with w ( β 3 ) ≤ W + b , by induction, because α | l ¯ refutes α 0 | l ¯ . Thus the combination β = { β 1 , β 2 , β 3 } refutes α 0 , with

w ( β ) = max( w ( β 0 ) + 1, w ( β 2 ), w ( β 3 )) ≤ max( W + b , w ( α 0 ), W + b ) = W + b .

Finally, exercise 238 chooses W so that we get the claimed bound.

The pigeon axioms are too wide to be inserted directly into Lemma B. But Ben-Sasson and Wigderson observed that a simplified version of those axioms, involving only clauses of 5SAT, is already intractable.

Notice that we can regard the variable x jk as indicating the presence of an edge between a j and b k in a bipartite graph on the vertices A = { a 0 ,..., a m } and B = { b 1 ,..., b m }. Condition ( 106 ) says that each a j has degree ≥ 1, while condition ( 107 ) says that each b k has degree ≤ 1. There is, however, a bipartite graph G 0 on those vertices for which each a j has degree ≤ 5 and such that the following strong “expansion” condition is satisfied:

Every ⁢   subset ⁢     A ′   ⊆     A ⁢     with ⁢       | A ′ | ≤ m / 3000 ⁢       has ⁢   | ∂ A ′ | ⁢     in ⁢   G 0 . ( 108 )

Here ∂ A ′ denotes the bipartite boundary of A ′, namely the set of all b k that have exactly one neighbor in A ′.

Given such a graph G 0 , whose existence is proved ( nonconstructively) in exercise 240 , we can formulate a restricted pigeonhole principle, by which the pigeonhole clauses are unsatisfiable if we also require x ¯ j ⁢ k whenever a j ― b k in G 0 .

Let α ( G 0 ) denote the resulting clauses, which are obtained when axioms ( 106 ) and ( 107 ) are conditioned on all such literals x ¯ j ⁢ k . Then w (α(G 0 )) ≤ 5, and at most 5 m + 5 unspecified variables x jk remain. Lemma B tells us that all refutation chains for α( G 0 ) have length exp Ω( m ) if we can prove that they all have width Ω( m ). Haken’s theorem, which asserts that all refutation chains for ( 106 ) and ( 107 ) also have length exp Ω( m ), will follow, because any short refutation would yield a short refutation of α ( G 0 ) after conditioning on the x ¯ j ⁢ k .

Thus the following result gives our story a happy ending:

Theorem B. The restricted pigeonhole axioms α ( G 0 ) have refutation width

w ( α ( G 0 ) ⊢ ∈ ) ≥ m / 6000 . ( 109 )

Proof. We can assign a complexity measure to every clause C by defining

μ ( C ) \= min { | A ′ |     |     A ′   ⊆   A ⁢   ⁢         and ⁢     α ( A ′ ) ⊢ C } . ( 110 )

Here α ( A ′) is the set of “pigeon axioms” ( 106 ) for a j ∈ A ′, together with all of the “hole axioms” ( 107 ); and α ( A ′) ⊢ C means that clause C can be proved by resolution when starting with only those axioms. If C is one of the pigeon axioms, this definition makes μ ( C ) = 1, because we can let A ′ = { a j }. And if C is a hole axiom, clearly μ ( C ) = 0. The subadditive law

μ ( C ′   ◊ ⁢       C " ) ≤ μ ( C ′ ) + μ ( C " ) ( 111 )

also holds, because a proof of C ′ ◊ C ″ needs at most the axioms of α ( A ′) ∪ α ( A ″) if C ′ follows from α ( A ′) and C ″ follows from α ( A ″).

We can assume that m ≥ 6000. And we must have μ ( ∊ ) > m /3000, because of the strong expansion condition ( 108 ). (See exercise 241 .) Therefore every refutation of α ( G 0 ) must contain a clause C with m /6000 ≤ μ ( C ) < m /3000; indeed, the first clause C j with μ ( C j ) ≥ m /6000 will satisfy this condition, by ( 111 ).

Let A ′ be a set of vertices with | A ′| = μ ( C ) and α ( A ′) ⊢ C . Also let b k be any element of ∂ A ′, with a j its unique neighbor in A ′. Since | A ′ \ a j | < μ ( C ), there must be an assignment of variables that satisfies all axioms of α ( A ′ \ a j ), but falsifies C and the pigeon axiom for j . That assignment puts no two pigeons into the same hole, and it places every pigeon of A ′ \ a j .

Now suppose C contains no literal of the form x j′k or x ¯ j ′ k , for any a j′ ∈ A . Then we could set x j′k ← 0 for all j ′, without falsifying any axiom of α ( A ′ \ a j ); and we could then make the axioms of α ({ a j }) true by setting x jk ← 1. But that change to the assignment would leave C false, contradicting our assumption that α( A ′) ⊢ C . Thus C contains some ± x j′k for each b k ∈ ∂ A ′; and we must have w ( C ) ≥ |∂ A ′| ≥ m /6000.

A similar proof establishes a linear lower bound on the refutation width, hence an exponential lower bound on the refutation length, of almost all random 3SAT instances with n variables and ⌊α n ⌋ clauses, for fixed α as n → ∞ (see exercise 243 ), a theorem of V. Chvátal and E. Szemerédi [ JACM 35 (1988), 759–768].

Historical notes: Proofs by resolution, in the more general setting of first-order logic, were introduced by J. A. Robinson in JACM 12 (1965), 23–41. [They’re also equivalent to G. Gentzen’s “ cut rule for sequents,” Mathematische Zeitschrift 39 (1935), 176–210, III.1.2 1.] Inspired by Robinson’s paper, Gregory Tseytin developed the first nontrivial techniques to prove lower bounds on the length of resolution proofs, based on unsatisfiable graph axioms that are considered in exercise 245 . His lectures of 1966 were published in Volume 8 of the Steklov Mathematical Institute Seminars in Mathematics (1968); see A. O. Slisenko’s English translation, Studies in Constructive Mathematics and Mathematical Logic , part 2 (1970), 115–125.

Tseytin pointed out that there’s a simple way to get around the lower bounds he had proved for his graph-oriented problems, by allowing new kinds of proof steps: Given any set of axioms F , we can introduce a new variable z that doesn’t appear anywhere in F , and add three new clauses G \= { x ⁢ z , y ⁢ z , x ¯ y ¯ z ¯ } ; here x and y are arbitrary literals of F . It’s clear that F is satisfiable if and only if F ∪ G is satisfiable, because G essentially says that z = NAND( x, y ). Adding new variables in this way is somewhat analogous to using lemmas when proving a theorem, or to introducing a memo cache in a computer program.

His method, which is called extended resolution , can be much faster than pure resolution. For example, it allows the pigeonhole clauses ( 106 ) and ( 107 ) to be refuted in only O ( m 4 ) steps (see exercise 237 ). It doesn’t appear to help much with certain other classes of problems such as random 3SAT; but who knows?

SAT solving via resolution

The concept of resolution also suggests alternative ways to solve satisfiability problems. In the first place we can use it to eliminate variables: If F is any set of clauses on n variables, and if x is one of those variables, we can construct a set F ′ of clauses on the other n − 1 variables in such a way that F is satisfiable if and only if F ′ is satisfiable. The idea is simply to resolve every clause of the form x ∨ A ′ with every clause of the form x ¯ ⁢   ∨ A " , and then to discard those clauses.

For example, consider the following six clauses in four variables:

1234 , 1 2 ¯ , 1 ¯ 2 ¯ 3 ¯ , 1 ¯ 3 , 2 3 ¯ , 3 4 ¯ . ( 112 )

We can eliminate the variable x 4 by forming 1234 ⁢   ◊ ⁢ 3 4 ¯ \= 123 . Then we can eliminate x 3 by resolving 123 and 1 ¯ 3 with 1 ¯ 2 ¯ 3 ¯ and 2 3 ¯ :

123 ⁢ ◊ ⁢ 1 ¯ 2 ¯ 3 ¯ \= ℘ ,           123 ⁢ ◊ ⁢ 2 3 ¯ \= 12 ,           1 ¯ 3 ⁢ ◊   1 ¯ 2 ¯ 3 ¯ \= 1 ¯ 2 ¯ ,               1 ¯ 3 ⁢ ◊ ⁢ 2 3 ¯ \= 1 ¯ 2.

Now we’re left with { 12 , 1 2 ¯ , 1 ¯ 2 , 1 ¯ 2 ¯ } , because the tautology ℘ goes away. Eliminating x 2 gives { 1 , 1 ¯ } , and eliminating x 1 gives {∊}; hence ( 112 ) is unsatisfiable.

This method, which was originally proposed for hand calculation by E. W. Samson and R. K. Mueller in 1955, works beautifully on small problems. But why is it valid? There are (at least) two good ways to understand the reason. First, it’s easy to see that F ′ is satisfiable whenever F is satisfiable, because C ′ ◊ C ′ is true whenever C ′ and C ″ are both true. Conversely, if F ′ is satisfied by some setting of the other n − 1 variables, that setting must either satisfy A ′ for all clauses of the form x ∨ A ′, or else it must satisfy A ″ for all clauses of the form x ¯ ∨ A " . (Otherwise neither A ′ nor A ″ would be satisfied, for some A ′ and some A ″, and the clause A ′ ∨ A ″ in F ″ would be false.) Thus at least one of the settings x ← 0 or x ← 1 will satisfy F .

Another good way to understand variable elimination is to notice that it corresponds to the elimination of an existential quantifier (see exercise 248 ).

Suppose p clauses of F contain x and q clauses contain x ¯ . Then the elimination of x will give us at most pq new clauses, in the worst case; so F ′ will have no more clauses than F did, whenever pq ≤ p + q , namely when ( p − 1)( q − 1) ≤ 1. This condition clearly holds whenever p = 0 or q = 0; indeed, we called x a “ pure literal” when such cases arose in Algorithm A . The condition also holds whenever p = 1 or q = 1, and even when p = q = 2.

Furthermore we don’t always get pq new clauses. Some of the resolvents might turn out to be tautologous, as above; others might be subsumed by existing clauses. (The clause C is said to subsume another clause C ′ if C ⊆ C ′ , in the sense that every literal of C appears also in C ′ . In such cases we can safely discard C ′.) And some of the resolvents might also subsume existing clauses.

Therefore repeated elimination of variables doesn’t always cause the set of clauses to explode. In the worst case, however, it can be quite inefficient.

In January of 1972, Stephen Cook showed his students at the University of Toronto a rather different way to employ resolution in SAT-solving. His elegant procedure, which he called “ Method I,” essentially learns new clauses by doing resolution on demand:

Algorithm I ( Satisfiability by clause learning ). Given m nonempty clauses C 1 ∧‧‧‧∧ C m on n Boolean variables x 1 ... x n , this algorithm either proves them unsatisfiable or finds strictly distinct literals l 1 ... l n that satisfy them all. In the process, new clauses may be generated by resolution (and m will then increase).

I1. [Initialize.] Set d ← 0.

I2. [Advance.] If d = n , terminate successfully (the literals { l 1 ,..., l d } satisfy { C 1 ,..., C m }). Otherwise set d ← d + 1, and let l d be a literal strictly distinct from l 1 , ... , l d −1 .

I3. [Find falsified C i .] If none of C 1 , ... , C m are falsified by { l 1 ,..., l d }, go back to I2. Otherwise let C i be a falsified clause.

I4. [Find falsified C j .] (At this point we have l ¯ d ∈ C i ⊆ { l ¯ 1 , ... , l ¯ d } , but no clause is contained in { l ¯ 1 , ... , l ¯ d − 1 } .) Set l d ← l ¯ d . If none of C 1 , ... , C m are falsified by { l 1 ,..., l d }, go back to I2. Otherwise let l ¯ d ∈ C j ⊆ { l ¯ 1 , ... , l ¯ d } .

I5. [Resolve.] Set m ← m +1, C m ← C i ◊ C j . Terminate unsuccessfully if C m is empty. Otherwise set d ← max { t ⁢   | l ¯ t ∈ C m } , i ← m , and return to I4.

In step I5 the new clause C m cannot be subsumed by any previous clause C k for k < m , because C i ⁢   ◊ ⁢   C j ⊆ { l ¯ 1 , ... , l ¯ d − 1 } . Therefore, in particular, no clause is generated twice, and the algorithm must terminate.

This description is intentionally vague when it uses the word “let” in steps I2, I3, and I4: Any available literal l d can be selected in step I2, and any falsified clauses C i and C j can be selected in steps I3 and I4, without making the method fail. Thus Algorithm I really represents a family of algorithms, depending on what heuristics are used to make those selections.

For example, Cook proposed the following way (“ Method IA”) to select l d in step I2: Choose a literal that occurs most frequently in the set of currently unsatisfied clauses that have the fewest unspecified literals. When applied to the six clauses ( 112 ), this rule would set l 1 ← 3 and l 2 ← 2 and l 3 ← 1; then step I3 would find C i \= 1 ¯ 2 ¯ 3 ¯ false. So step I4 would set l 3 ← 1 ¯ and find C j \= 1 2 ¯ false, and step I5 would learn C 7 \= 2 ¯ 3 ¯ . (See exercise 249 for the sequel.)

Cook’s main interest when introducing Algorithm I was to minimize the number of resolution steps; he wasn’t particularly concerned with minimizing the running time. Subsequent experiments by R. A. Reckhow [Ph.D. thesis (Univ. Toronto, 1976), 81–84] showed that, indeed, relatively short resolution refutations are found with this approach. Furthermore, exercise 251 demonstrates that Algorithm I can handle the anti- maximal-element clauses ( 99 )–( 101 ) in polynomial time; thus it trounces the exponential behavior exhibited by all backtrack-based algorithms for this problem (see Theorem R).

On the other hand, Algorithm I does tend to fill memory with a great many new clauses when it is applied to large problems, and there’s no obvious way to deal with those clauses efficiently. Therefore Cook’s method did not appear to be of practical importance, and it remained unpublished for more than forty years.

Conflict driven clause learning

Algorithm I demonstrates the fact that unsuccessful choices of literals can lead us to discover valuable new clauses, thereby increasing our knowledge about the characteristics of a problem. When that idea was rediscovered from another point of view in the 1990s, it proved to be revolutionary: Significant industrial instances of SAT with many thousands or even millions of variables suddenly became feasible for the first time.

The name CDCL solver is often given to these new methods, because they are based on “conflict driven clause learning” rather than on classical backtracking. A CDCL solver shares many concepts with the DPLL algorithms that we’ve already seen; yet it is sufficiently different that we can understand it best by developing the ideas from scratch. Instead of implicitly exploring a search tree such as Fig. 82 , a CDCL solver is built on the notion of a trail , which is a sequence L 0 L 1 ... L F −1 of strictly distinct literals that do not falsify any clause. We can start with F = 0 (the empty trail). As computation proceeds, our task is to extend the current trail until F = n , thus solving the problem, or to prove that no solution exists, by essentially learning that the empty clause is true.

Suppose there’s a clause c of the form l ∨ ā 1 ∨‧‧‧∨ ā k , where a 1 through a k are in the trail but l isn’t. Literals in the trail are tentatively assumed to be true, and c must be satisfied; so we’re forced to make l true. In such cases we therefore append l to the current trail and say that c is its “ reason.” (This operation is equivalent to what we called “ unit propagation” in previous algorithms; those algorithms effectively removed the literals ā 1 , ... , ā k when they became false, thereby leaving l as a “unit” all by itself. But our new viewpoint keeps each clause c intact, and knows all of its literals.) A conflict occurs if the complementary literal l ¯ is already in the trail, because l can’t be both true and false; but let’s assume for now that no conflicts arise, so that l can legally be appended by setting L F ← l and F ← F + 1.

If no such forcing clause exists, and if F < n , we choose a new distinct literal in some heuristic way, and we append it to the current trail with a “reason” of Λ. Such literals are called decisions. They partition the trail into a sequence of decision levels, whose boundaries can be indicated by a sequence of indices with 0 = i 0 ≤ i 1 < i 2 < i 3 < ‧‧‧; literal L t belongs to level d if and only if i d ≤ t < i d +1 . Level 0, at the beginning of the trail, is special: It contains literals that are forced by clauses of length 1, if such clauses exist. Any such literals are unconditionally true. Every other level begins with exactly one decision.

Consider, for example, the problem waerden (3, 3; 9) of ( 9 ). The first items placed on the trail might be

t L t level reason 0 6 ¯ 1 Λ ( a ⁢       decision   ) 1 9 ¯ 2 Λ ( a decision ) 2 3 2 396 ( rearrangement of the clause 369 ) 3 4 ¯ 3 Λ ( a decision ) 4 5 3 546 ( rearrangement of the clause 456 ) 5 8 3 846 ( ⁢   rearrangement of the clause 468 ) 6 2 3 246 7 7 ¯ 3 7 ¯ 5 ¯ 3 ¯ ( rearrangement of the clause 3 ¯ 5 ¯ 7 ¯ ) 8 2 ¯ 3 2 ¯ 5 ¯ 8 ¯ ( a conflict! ) ( 113 )

Three decisions were made, and they started levels at i 1 = 0, i 2 = 1, i 3 = 3. Several clauses have been rearranged; we’ll soon see why. And propagations have led to a conflict, because both 2 and 2 ¯ have been forced. (We don’t actually consider the final entry L 8 to be part of the trail, because it contradicts L 6 .)

If the reason for l includes the literal l ¯ ′ , we say “ l depends directly on l ′.” And if there’s a chain of one or more direct dependencies, from l to l 1 to ‧‧‧ to l k = l ′, we say simply that “ l depends on l ′.” For example, 5 depends directly on 4 ¯ and 6 ¯ in ( 113 ), and 2 ¯ depends directly on 5 and 8; hence 2 ¯ depends on 6 ¯ .

Notice that a literal can depend only on literals that precede it in the trail. Furthermore, every literal l that’s forced at level d > 0 depends directly on some other literal on that same level d ; otherwise l would already have been forced at a previous level. Consequently l must necessarily depend on the d th decision.

The reason for reasons is that we need to deal with conflicts. We will see that every conflict allows us to construct a new clause c ′ that must be true whenever the existing clauses are satisfiable, although c ′ itself does not contain any existing clause. Therefore we can “learn” c ′ by adding it to the existing clauses, and we can try again. This learning process can’t go on forever, because only finitely many clauses are possible. Sooner or later we will therefore either find a solution or learn the empty clause. That will be nice, especially if it happens sooner.

A conflict clause c on decision level d has the form l ¯ ∨ a ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ a ¯ k , where l and all the a ’s belong to the trail; furthermore l and at least one a i belong to level d . We can assume that l is rightmost in the trail, of all the literals in c . Hence l cannot be the d th decision; and it has a reason, say l ∨ a ¯ 1 ′ ∨ ⋅ ⋅ ⋅ ∨ a ¯ k ′ ′ . Resolving c with this reason gives the clause c ′ \= a ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ a ¯ k ∨ a ¯ 1 ′ ∨ ⋅ ⋅ ⋅ ∨ a ¯ k ′ ′ , which includes at least one literal belonging to level d . If more than one such literal is present, then c ′ is itself a conflict clause; we can set c ← c ′ and repeat the process. Eventually we are bound to obtain a new clause c ′ of the form l ¯ ′ ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r , where l ′ is on level d and where b 1 through b r are on lower levels.

Such a c ′ is learnable, as desired, because it can’t contain any existing clauses. (Every subclause of c ′, including c ′ itself, would otherwise have given us something to force at a lower level.) We can now discard levels > d ′ of the trail, where d ′ is the maximum level of b 1 through b r ; and — this is the punch line — we can append l ¯ ′ to the end of level d ′ , with c ′ as its reason. The forcing process now resumes at level d ′, as if the learned clause had been present all along.

For example, after the conflict in ( 113 ), the initial conflict clause is C \= 2 ¯ 5 ¯ 8 ¯ , our shorthand notation for x ¯ 2 ∨ x ¯ 5 ∨ x ¯ 8 ; and its rightmost complemented literal in the trail is 2, because 5 and 8 came earlier. So we resolve c with 246, the reason for 2, and get c ′ \= 4 5 ¯ 6 8 ¯ . This new clause contains complements of three literals from level 3, namely 4 ¯ , 5, and 8; so it’s still a conflict clause. We resolve it with the reason for 8 and get c ′ \= 4 5 ¯ 6 . Again c ′ is a conflict clause. But the result of resolving this conflict with the reason for 5 is c ′ = 46, a clause that is falsified by the literals currently on the trail but has only 4 ¯ at level 3. Good — we have learned ‘46’: In every solution to waerden (3, 3; 9), either x 4 or x 6 must be true.

Thus the sequel to ( 113 ) is

t L t level reason 0 6 ¯ 1 Λ ( a decision ) 1 4 1 46 ( the newly learned clause ) ( 114 )

and the next step will be to begin a new level 2, because nothing more is forced.

Notice that the former level 2 has gone away. We’ve learned that there was no need to branch on the decision variable x 9 , because 6 ¯ already forces 4. This improvement to the usual backtrack regimen is sometimes called “ backjumping,” because we’ve jumped back to a level that can be regarded as the root cause of the conflict that was just discovered.

Exercise 253 explores a possible continuation of ( 114 ); dear reader, please jump to it now. Incidentally, the clause ‘46’ that we learned in this example involves the complements of former decisions 4 ¯ and 6 ¯ ; but exercise 255 shows that newly learned clauses might not contain any decision variables whatsoever.

The process of constructing the learned clause from a conflict is not as difficult as it may seem, because there’s an efficient way to perform all of the necessary resolution steps. Suppose, as above, that the initial conflict clause is l ¯ ∨ a ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ a ¯ k . Then we “stamp” each of the literals a i with a unique number s ; and we also insert ā i into an auxiliary array, which will eventually hold the literals b ¯ 1 , ... , b ¯ r , whenever a i is a literal that received its value on a level d ′ with 0 < d ′ < d . We stamp l too; and we count how many literals of level d have thereby been stamped. Then we repeatedly go back through the trail until coming to a literal L t whose stamp equals s. If the counter is bigger than 1 at this point, and if L t ’s reason is L t ∨ a ¯ 1 ′ ∨ ⋅ ⋅ ⋅ ∨ a ¯ k ′ ′ , we look at each a i ′ , stamping it and possibly putting it into the b array if it had not already been stamped with s . Eventually the count of unresolved literals will decrease to 1; the learned clause is then L ¯ t ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r .

These new clauses might turn out to be quite large, even when we’re solving a problem whose clauses were rather small to start with. For example, Table 3 gives a glimpse of typical behavior in a medium-size problem. It shows the beginning of the trail generated when a CDCL solver was applied to the 2779 clauses of waerden (3, 10; 97), after about 10,000 clauses had been learned. (Recall that this problem tries to find a binary vector x 1 x 2 ... x 97 that has no three equally spaced 0s and no ten equally spaced 1s.) Level 18 in the table has just been launched with the decision L 44 \= 57 ¯ ; and that decision will trigger the setting of many more literals 15, 49, 61, 68, 77, 78, 87, 96 ¯ , ... , eventually leading to a conflict when trying to set L 67 . The conflict clause turns out to have length 22:

Table 3
THE FIRST LEVELS OF A MODERATE-SIZE TRAIL

t

L t

level

reason

t

L t

level

reason

t

L t

level

reason

0

53 ¯

1

Λ

15

70

11

70 36 53

30

08

15

08 46 27

1

55

2

Λ

16

35

12

Λ

31

65

15

65 46 27

2

44

3

Λ

17

39

13

Λ

32

60

15

60 46 53

3

54

4

Λ

18

37 ¯

14

Λ

33

50 ¯

15

∗∗

4

43

5

Λ

19

38

14

38 37 36

34

64

15

64 50 36

5

30

6

Λ

20

47

14

47 37 27

35

22

15

22 50 36

6

34

7

Λ

21

17

14

17 37 27

36

24

15

24 50 37

7

45

8

Λ

22

32

14

32 37 27

37

42

15

42 50 46

8

40

9

Λ

23

69

14

69 37 53

38

48

15

48 50 46

9

27 ¯

10

Λ

24

21

14

21 37 53

39

73

15

73 50 27

10

79

10

79 53 27

25

46 ¯

15

Λ

40

04

15

04 50 27

11

01

10

01 27 53

26

28

15

28 46 37

41

63

15

63 50 37

12

36 ¯

11

Λ

27

41

15

41 46 36

42

33

16

Λ

13

18

11

18 36 27

28

26

15

26 46 36

43

51

17

Λ

14

19

11

19 36 53

29

56

15

56 46 36

44

57 ¯

18

Λ

(Here ∗∗ denotes the previously learned clause 50   ¯ ⁢   26 ¯ ⁢   27   30 ¯ ⁢   32 ¯ ⁢   35 ¯ ⁢   38 ¯ ⁢   40 ¯ ⁢   41 ¯ ⁢   44 ¯ ⁢   45 ¯ ⁢   47 ¯ ⁢   50 ¯ ⁢   55 ¯ ⁢   60 ¯ ⁢   65 ¯ ⁢   70 ¯ .)

53 ⁢   27 ⁢   36 ⁢   70 ¯ ⁢   35 ¯ ⁢   37 ⁢   69 ¯ ⁢   21 ¯ ⁢   46 ⁢   28 ¯ ⁢   56 ¯ ⁢   65 ¯ ⁢   60 ¯ ⁢   50 ⁢   64 ¯ ⁢   24 ¯ ⁢   42 ¯ ⁢   73 ¯ ⁢   63 ¯ ⁢   33 ¯ ⁢   51 ¯ ⁢   57. ( 115 )

(Its literals are shown here in order of the appearance of their complements in the trail.) When we see such a monster clause, we might well question whether we really want to “learn” such an obscure fact!

A closer look, however, reveals that many of the literals in ( 115 ) are redundant. For example, 70 ¯ can safely be deleted, because its reason is ‘70 36 53’; both 36 and 53 already appear in ( 115 ), hence ( 115 ) ◊ (70 36 53) gets rid of 70 ¯ . Indeed, more than half of the literals in this example are redundant, and ( 115 ) can be simplified to the much shorter and more memorable clause

53 ⁢   27 ⁢   36 ⁢   35 ¯ ⁢   37 ⁢   46 ⁢   50 ⁢   33 ¯ ⁢   51 ¯ ⁢   57. ( 116 )

Exercise 257 explains how to discover such simplifications, which turn out to be quite important in practice. For example, the clauses learned while proving waerden (3, 10; 97) unsatisfiable had an average length of 19.9 before simplification, but only 11.2 after; simplification made the algorithm run about 33% faster.

Most of the computation time of a CDCL solver is devoted to unit propagation. Thus we need to know when the value of a literal has been forced by previous assignments, and we hope to know it quickly. The idea of “ lazy data structures,” used above in Algorithm D , works nicely for this purpose, in the presence of long clauses, provided that we extend it so that every clause now has two watched literals instead of one. If we know that the first two literals of a clause are not false, then we needn’t look at this clause until one of them becomes false, even though other literals in the clause might be repeatedly veering between transient states of true, false, and undefined. And when a watchee does become false, we’ll try to swap it with a nonfalse partner that can be watched instead. Propagations or conflicts will arise only when all of the remaining literals are false.

Algorithm C below therefore represents clauses with the following data structures: A monolithic array called MEM is assumed to be large enough to hold all of the literals in all of the clauses, interspersed with control information. Each clause c = l 0 ∨ l 1 ∨ ‧‧‧ ∨ l k −1 with k > 1 is represented by its starting position in MEM , with MEM[ c + j ] = l j for 0 ≤ j < k . Its two watched literals are l 0 and l 1 , and its size k is stored in MEM[ c − 1 ] . Unit clauses, for which k = 1, are treated differently; they appear in level 0 of the trail, not in MEM .

A learned clause c can be distinguished from an initial clause because it has a relatively high number, with MINL ≤ c < MAXL . Initially MAXL is set equal to MINL , the smallest cell in MEM that is available for learned clauses; then MAXL grows as new clauses are added to the repertoire. The set of learned clauses is periodically culled, so that the less desirable ones don’t clutter up memory and slow things down. Additional information about a learned clause c is kept in MEM[ c − 4 ] and MEM[ c − 5 ] , to help with this recycling process (see below).

Individual literals x k and x ¯ k , for 1 ≤ k ≤ n , are represented internally by the numbers 2 k and 2 k + 1 as in ( 57 ) above. And each of these 2 n literals l has a list pointer W l , which begins a linked list of the clauses in which l is watched. We have W l = 0 if there is no such clause; but if W l = c > 0, the next link in this “watch list” is in MEM[ c − 2 ] if l = l 0 , in MEM[ c − 3 ] if l = l 1 . [See Armin Biere, Journal on Satisfiability, Boolean Modeling and Comp. 4 (2008), 75–97.]

For example, the first few cells of MEM might contain the following data when we are representing the clauses ( 9 ) of waerden (3, 3; 9):

i \= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ... MEM [ i ] \= 9 45 3 2 4 6 15 51 3 4 6 8 21 45 3 6 8 10 ...

(Clause 3 is ‘123’, clause 9 is ‘234’, clause 15 is ‘345’, ... , clause 45 is ‘135’, clause 51 is ‘246’, ... ; the watch lists for literals x 1 , x 2 , x 3 , x 4 begin respectively at W 2 = 3, W 4 = 3, W 6 = 9, W 8 = 15.)

The other major data structures of Algorithm C are focused on variables, not clauses. Each variable x k for 1 ≤ k ≤ n has six current attributes S( k ) , VAL( k ) , OVAL( k ) , TLOC( k ) , HLOC( k ) , and ACT( k ) , which interact as follows: S( k ) is the “stamp” that’s used during clause formation. If neither x k nor x ¯ k appears in the current trail, then VAL( k ) = −1, and we say that x k and its two literals are “ free.” But if L t = l is a literal of the trail, belonging to level d , we have

VAL ( | l | ) \= 2 d + ( l & 1 ) ⁢       and ⁢           TLOC ( | l | ) \= t , where ⁢     | l | \= l ≫ 1 , ( 117 )

and we say that l is “true” and l ¯ is “false.” Thus a given literal l is false if and only if VAL( | l | ) is nonnegative and VAL( | l | ) + l is odd. In most cases a watched literal is not false; but there are exceptions to this rule (see exercise 265 ). The “ reason” for literal l ’s current value is kept in variable R l .

The attributes ACT( k ) and HLOC( k ) tell the algorithm how to select the next decision variable. Each variable x k has an activity score ACT( k ) , which heuristically estimates its desirability for branching. All of the free variables, and possibly others, are kept in an array called HEAP , which is arranged so that

ACT ( HEAP [ j ] ) ≤ ACT ⁢   ( HEAP [ ( j − 1 ) ≫ 1 ] ) for ⁢   ⁢   0 < j < h ( 118 )

when it contains h elements (see Section 5.2.3). Thus HEAP[ 0 ] will always be a free variable of maximum activity, if it is free; so it’s the variable that will be chosen to govern the decision when the trail starts to acquire a new level.

Activity scores help the algorithm to focus on recent conflicts. Suppose, for example, that M = 100 conflicts have been resolved, hence 100 clauses have been learned. Suppose further that x j or x ¯ j was stamped while resolving the conflicts numbered 3, 47, 95, 99, and 100; but x k or x ¯ k was stamped during conflicts 41, 87, 94, 95, 96, and 97. We could express their recent activity by computing

ACT ( j ) \= ρ 0 + ρ 1 + ρ 5 + ρ 53 + ρ 97 , ACT ( k ) \= ρ 3 + ρ 4 + ρ 5 + ρ 6 + ρ 13 + ρ 59 ,

where ρ is a damping factor (say ρ = .95), because 100 − 100 = 0, 100 − 99 = 1, 100 − 95 = 5, ... , 100 − 41 = 59. In this particular case j would be considered to be less active than k unless ρ is less than about .8744.

In order to update the activity scores according to this measure, we would have to do quite a bit of recomputation whenever a new conflict occurs: The new scores would require us to multiply all n of the old scores by ρ , then to increase the activity of every newly stamped variable by 1. But there’s a much better way, namely to compute ρ − M \= ρ −100 times the scores shown above:

ACT ( j ) \= ρ − 3 + ρ − 47 + ρ − 95 + ρ − 99 + ρ − 100 , ACT ( k ) \= ρ − 41 + ⋅ ⋅ ⋅ + ρ − 96 + ρ − 97 .

These newly scaled scores, suggested by Niklas Eén, give us the same information about the relative activity of each variable; and they’re updated easily, because we need to do only one addition per stamped variable when resolving conflicts.

The only problem is that the new scores can become really huge, because ρ − M can cause floating point overflow after the number M of conflicts becomes large. The remedy is to divide them all by 10 100 , say, whenever any variable gets a score that exceeds 10 100 . The HEAP needn’t change, since ( 118 ) still holds.

During the algorithm the variable DEL holds the current scaling factor ρ − M , divided by 10 100 each time all of the activities have been rescaled.

Finally, the parity of OVAL( k ) is used to control the polarity of each new decision in step C6. Algorithm C starts by simply making each OVAL( k ) odd, although other initialization schemes are possible. Afterwards it sets OVAL( k ) ← VAL( k ) whenever x k leaves the trail and becomes free, as recommended by D. Frost and R. Dechter [ AAAI Conf. 12 (1994), 301–306] and independently by K. Pipatsrisawat and A. Darwiche [ LNCS 4501 (2007), 294–299], because experience has shown that the recently forced polarities tend to remain good. This technique is called “ sticking” or “ progress saving” or “ phase saving.”

Algorithm C is based on the framework of a pioneering CDCL solver called Chaff, and on an early descendant of Chaff called MiniSAT that was developed by N. Eén and N. Sörensson [ LNCS 2919 (2004), 502–518].

Algorithm C ( Satisfiability by CDCL ). Given a set of clauses on n Boolean variables, this algorithm finds a solution L 0 L 1 ... L n −1 if and only if the clauses are satisfiable, meanwhile discovering M new ones that are consequences of the originals. After discovering M p new clauses, it will purge some of them from its memory and reset M p ; after discovering M f of them, it will flush part of its trail, reset M f , and start over. (Details of purging and flushing will be discussed later.)

C1. [Initialize.] Set VAL( k ) ← OVAL( k ) ← TLOC( k ) ←−1, ACT( k ) ← S( k ) ← 0, R 2 k ← R 2 k +1 ← Λ, HLOC( k ) ← p k − 1, and HEAP[ p k − 1 ] ← k , for 1 ≤ k ≤ n , where p 1 ... p n is a random permutation of {1,..., n }. Then input the clauses into MEM and the watch lists, as described above. Put the distinct unit clauses into L 0 L 1 ... L F −1 ; but terminate unsuccessfully if there are contradictory clauses ( l ) and ( l ¯ ). Set MINL and MAXL to the first available position in MEM . (See exercise 260 .) Set i 0 ← d ← s ← M ← G ← 0, h ← n , DEL ← 1.

C2. [Level complete?] (The trail L 0 ... L F −1 now contains all of the literals that are forced by L 0 ... L G −1 .) Go to C5 if G = F .

C3. [Advance G.] Set l ← L G and G ← G + 1. Then do step C4 for all c in the watch list of l ¯ , unless that step detects a conflict and jumps to C7. If there is no conflict, return to C2. (See exercise 261 .)

C4. [Does c force a unit?] Let l 0 l 1 ... l k −1 be the literals of clause c , where l 1 \= l ¯ . (Swap l 0 ↔ l 1 if necessary.) If l 0 is true, do nothing. Otherwise look for a literal l j with 1 < j < k that is not false. If such a literal is found, move c to the watch list of l j . But if l 2 , ... , l k −1 are all false, jump to C7 if l 0 is also false. On the other hand if l 0 is free, make it true by setting L F ← l 0 , TLOC( | l 0 | ) ← F , VAL( | l 0 | ) ← 2 d +(l 0 & 1), R l 0 ← c , and F ← F +1.

C5. [New level?] If F = n , terminate successfully. Otherwise if M ≥ M p , prepare to purge excess clauses (see below). Otherwise if M ≥ M f , flush literals as explained below and return to C2. Otherwise set d ← d + 1 and i d ← F .

C6. [Make a decision.] Set k ← HEAP[ 0 ] and delete k from the heap (see exercises 262 and 266 ). If VAL( k ) ≥ 0, repeat this step. Otherwise set l ← 2 k + ( OVAL( k ) & 1), VAL( k ) ← 2 d + ( OVAL( k ) & 1), L F ← l , TLOC( |l| ) ← F , R l ← Λ, and F ← F + 1. (At this point F = G + 1.) Go to C3.

C7. [Resolve a conflict.] Terminate unsuccessfully if d = 0. Otherwise use the conflict clause c to construct a new clause l ¯ ′ ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r as described above. Set ACT( | l | ) ← ACT( | l | ) + DEL for all literals l stamped during this process; also set d ′ to the maximum level occupied by { b 1 ,..., b r } in the trail. (See exercise 263 . Increasing ACT( | l | ) may also change HEAP .)

C8. [Backjump.] While F > i d′ +1 , do the following: Set F ← F − 1, l ← L F , k ← | l |, OVAL( k ) ← VAL( k ) , VAL( k ) ← −1, R l ← Λ; and if HLOC( | l | ) < 0 insert k into HEAP (see exercise 262 ). Then set G ← F and d ← d ′.

C9. [Learn.] If d > 0, set c ← MAXL , store the new clause in MEM at position c , and advance MAXL to the next available position in MEM . ( Exercise 263 gives full details.) Set M ← M + 1, L F ← l ¯ ′ , VAL ( | l ′ | ) ← 2 d + ( l ¯ ′ & 1 ) , TLOC( | l ′ | ) ← F , R l , ← c , F ← F + 1, DEL ← DEL / ρ , and return to C3.

The high-level operations on data structures in this algorithm are spelled out in terms of elementary low-level steps in exercises 260 – 263 . Exercises 266 – 271 discuss simple enhancements that were made in the experiments reported below.

Reality check : Although detailed statistics about the performance of Algorithm C on a wide variety of problems will be presented later, a few examples of typical behavior will help now to clarify how the method actually works in practice. Random choices make the running time of this algorithm more variable than it was in Algorithms A , B , D , or L ; sometimes we’re lucky, sometimes we’re not.

In the case of waerden (3, 10; 97), the modest 97-variable-and-2779-clause problem that was considered in Table 3 , nine test runs of Algorithm C established unsatisfiability after making between 250 and 300 million memory accesses; the median was 272 Mμ . (This is more than twice as fast as our best previous time, which was obtained with Algorithm L .) The average number of decisions made — namely the number of times L F ← l was done in step C6 — was about 63 thousand; this compares to 1701 “ nodes” in Algorithm L , step L3, and 100 million nodes in Algorithms A , B , D . About 53 thousand clauses were learned, having an average size of 11.5 literals (after averaging about 19.9 before simplification).

images

Fig. 92. It is not possible to color the edges of the flower snark graph J q with three colors, when q is odd. Algorithm C is able to prove this with amazing speed: Computation times (in megamems) are shown for nine trials at each value of q .

Algorithm C often speeds things up much more dramatically, in fact. For example, Fig. 92 shows how it whips through a sequence of three-coloring problems that are based on “flower snarks.” Exercise 176 defines fsnark ( q ), an interesting set of 42 q + 3 unsatisfiable clauses on 18 q variables. The running time of Algorithms A , B , D , and L on fsnark ( q ) is proportional to 2 q , so it’s way off the chart — well over a gigamem already when q = 19. But Algorithm C polishes off the case q = 99 in that same amount of time (thus winning by 24 orders of magnitude)! On the other hand, no satisfactory theoretical explanation for the apparently linear behavior in Fig. 92 is presently known.

Certificates of unsatisfiability. When a SAT solver reports that a given instance is satisfiable, it also produces a set of distinct literals from which we can easily check that every clause is satisfied. But if its report is negative — UNSAT — how confident can we be that such a claim is true? Maybe the implementation contains a subtle error; after all, large and complicated programs are notoriously buggy, and computer hardware isn’t perfect either. A negative answer can therefore leave both programmers and users unsatisfied, as well as the problem.

We’ve seen that unsatisfiability can be proved rigorously by constructing a resolution refutation, namely a chain of resolution steps that ends with the empty clause ∊ , as in Fig. 91 . But such refutations amount to the construction of a huge directed acyclic graph.

A much more compact characterization of unsatisfiability is possible. Let’s say that the sequence of clauses ( C 1 , C 2 ,..., C t ) is a certificate of unsatisfiability for a family of clauses F if C t = ∊ , and if we have

F ∧ C 1 ∧ ⋅ ⋅ ⋅ ∧ C i − 1 ∧ C ¯ i ⊢ 1 ∈ for ⁢     1 ≤ i ≤ t . ( 119 )

Here the subscript 1 in ‘G ⊢ 1 ∊ ’ means that the clauses G lead to a contradiction by unit propagation ; and if C i is the clause ( a 1 ∨ ‧‧‧ ∨ a k ), then C ¯ i is an abbreviation for the conjunction of unit clauses ( ā 1 ) ∧ ‧‧‧ ∧ ( ā k ).

For example, let F = R be Rivest’s clauses ( 6 ), which were proved unsatisfiable in Fig. 91 . Then (12, 1, 2, ∊ ) is a certificate of unsatisfiability, because

R ∧ 1 ¯ ∧ 2 ¯ ⊢ 1 3 ¯ ⊢ 1     4 ¯ ⊢ 1 ∈ ( using ⁢     12 3 ¯ , 23 4 ¯ , and ⁢   341 ) R ∧ 12 ∧ 1 ¯ ⊢ 1   2 ⊢ 1   4 ¯ ⊢ 1 3 ¯ ⊢ 1 ∈ ( using ⁢   12 , 4 ¯ 1 2 ¯ ,   2 ¯ 3 ¯ 4 ,   and ⁢   341 ) ; R ∧ 12 ∧ 1 ∧ 2 ¯ ⊢ 1   4 ⊢ 1 3 ⊢ 1 ∈ ( using ⁢   4 1 ¯ 2 ,   23 4 ¯ ,   and ⁢   3 ¯ 4 ¯ 1 ¯ ) ; R ∧ 12 ∧ 1 ∧ 2 ⊢ 1 3 ⊢ 1 4 ⊢ 1 ∈ ( using ⁢     1 ¯ 2 ¯ 3 ,   2 ¯ 3 ¯ 4 , and ⁢     3 ¯ 4 ¯ 1 ¯ ) .

A certificate of unsatisfiability gives a convincing proof, since ( 119 ) implies that each C i must be true whenever F , C 1 , ... , C i −1 are true. And it’s easy to check whether or not G ⊢ 1 ∊ , for any given set of clauses G , because everything is forced and no choices are involved. Unit propagation is analogous to water flowing downhill; we can be pretty sure that it has been implemented correctly, even if we don’t trust the CDCL solver that generated the certificate being checked.

E. Goldberg and Y. Novikov [ Proceedings of DATE: Design, Automation and Test in Europe 6 , 1 (2003), 886–891] have pointed out that CDCL solvers actually produce such certificates as a natural byproduct of their operation:

Theorem G. If Algorithm C terminates unsuccessfully, the sequence ( C 1 , C 2 , ..., C t ) of clauses that it has learned is a certificate of unsatisfiability.

Proof. It suffices to show that, whenever Algorithm C has learned the clause C ′ \= l ¯ ′ ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r , unit propagation will deduce ∊ if we append the unit clauses ( l ′) ∧ ( b 1 ) ∧ ‧‧‧ ∧ ( b r ) to the clauses that the algorithm already knows. The key point is that C ′ has essentially been obtained by repeated resolution steps,

C ′ \= ( ... ( ( C ⁢ ◊ ⁢ R l 1 ) ◊ ⁢ R l 2 ) ◊ ⋅ ⋅ ⋅ ) ◊ ⁢ R l s , ( 120 )

where C is the original conflict clause and R l 1 , R l 2 , ... , R l s are the reasons for each literal that was removed while C ′ was constructed in step C7. More precisely, we have C = A 0 and R l i \= l i ∨ A i , where all literals of A 0 ∪ A 1 ∪‧‧‧∪ A s are false (their complements appear in the trail); and

l ¯ i ∈ A 0 ∪ ⋅ ⋅ ⋅ ∪ A i − 1 , ⁢     for ⁢   1 ≤ i ≤ s ; A 0 ∪ A 1 ∪ ⋅ ⋅ ⋅ ∪   A s \= { l ¯ ′ , l ¯ 1 , ... , l ¯ s , b ¯ 1 , ... , b ¯ r } . ( 121 )

Thus the known clauses, plus b 1 , ... , b r , and l ′ , will force l s using clause R l s . And l s −1 will then be forced, using R l s−1 . And so on.

Since the unit literals in this proof are propagated in reverse order l s , l s −1 , ... , l 1 from the resolution steps in ( 120 ), this certificate-checking procedure has become known as “ reverse unit propagation” [see A. Van Gelder, Proc. Int. Symp. on Artificial Intelligence and Math. 10 (2008), 9 pages, online as ISAIM2008 ].

Notice that the proof of Theorem G doesn’t claim that reverse unit propagation will reconstruct the precise reasoning by which Algorithm C learned a clause. Many different downhill paths to ∊ , built from ⊢ 1 steps, usually exist in a typical situation. We merely have shown that every clause learnable from a single conflict does imply the existence of at least one such downhill path.

Many of the clauses learned during a typical run of Algorithm C will be “shots in the dark,” which turn out to have been aimed in unfruitful directions. Thus the certificates in Theorem G will usually be longer than actually necessary to demonstrate unsatisfiability. For example, Algorithm C learns about 53,000 clauses when refuting waerden (3, 10; 97), and about 135,000 when refuting fsnark ( 99 ); but fewer than 50,000 of the former, and fewer than 47,000 of the latter, were actually used in subsequent steps. Exercise 284 explains how to shorten a certificate of unsatisfiability while checking its validity.

An unexpected difficulty arises, however: We might spend more time verifying a certificate than we needed to generate it! For example, a certificate for waerden (3, 10; 97) was found in 272 megamems, but the time needed to check it with straightforward unit-propagations was actually 2.2 giga mems. Indeed, this discrepancy becomes significantly worse in larger problems, because a simple program for checking must keep all of the clauses active in its memory. If there are a million active clauses, there are two million literals being watched; hence every change to a literal will require many updates to the data structures.

The solution to this problem is to provide extra hints to the certificate checker. As we are about to see, Algorithm C does not keep all of the learned clauses in its memory; it systematically purges its collection, so that the total number stays reasonable. At such times it can also inform the certificate checker that the purged clauses will no longer be relevant to the proof.

Further improvements also allow annotated certificates to accommodate stronger proof rules, such as Tseytin’s extended resolution and techniques based on generalized autarkies; see N. Wetzler, M. J. H. Heule, and W. A. Hunt, Jr., LNCS 8561 (2014), 422–429.

Whenever a family of clauses has a certificate of unsatisfiability, a variant of Algorithm C will actually find one that isn’t too much longer. (See exercise 386 .)

* Purging unhelpful clauses

After thousands of conflicts have occurred, Algorithm C has learned thousands of new clauses. New clauses guide the search by steering us away from unproductive paths; but they also slow down the propagation process, because we have to watch them.

We’ve seen that certificates can usually be shortened; therefore we know that many of the learned clauses will probably never be needed again. For this reason Algorithm C periodically attempts to weed out the ones that appear to be more harmful than helpful, by ranking the clauses that have accumulated.

I consider that a man’s brain originally is like a little empty attic, and you have to stock it with such furniture as you choose. . . . the skilled workman is very careful indeed as to what he takes into his brain-attic. . . . It is a mistake to think that that little room has elastic walls and can distend to any extent. . . . It is of the highest importance, therefore, not to have useless facts elbowing out the useful ones.

— SHERLOCK HOLMES, in A Study in Scarlet (1887)

Algorithm C initiates a special clause-refinement process as soon as it has learned M ≥ M p clauses and arrived at a reasonably stable state (step C5). Let’s continue our running example, waerden (3, 10; 97), in order to make the issues concrete. If M p is so huge that no clauses are ever thrown away, a typical run will learn roughly 48 thousand clauses, and do roughly 800 megamems of computation, before proving unsatisfiability. But if M p = 10000, it will learn roughly 50 thousand clauses, and the computation time will go down to about 500 megamems. In the latter case the total number of learned clauses in memory will rarely exceed 10 thousand.

Indeed, let’s set M p = 10000 and take a close look at exactly what happened during the author’s first experiments. Algorithm C paused to reconnoiter the situation after having learned 10002 clauses. At that point only 6252 of those 10002 clauses were actually present in memory, however, because of the clause- discarding mechanism discussed in exercise 271 . Some clauses had length 2, while the maximum size was 24 and the median was 11; here’s a complete histogram:

2 9 49 126 216 371 542 719 882 1094 661 540 414 269 176 111 35 20 10 3 1 1 1.

Short clauses tend to be more useful, because they reduce more quickly to units.

A learned clause cannot be purged if it is the reason for one of the literals on the trail. In our example, 12 of the 6252 fell into this category; for instance, 30 ¯ appeared on level 10 of the trail because ′ 30 ¯ ⁢     33 ¯ ⁢   39 ¯ ⁢   41   42 ¯ ⁢   45 ¯ ⁢   46 ⁢   48 ¯ ⁢   54 ¯ ⁢   57 ¯ ′ had been learned, and we may need to know that clause in a future resolution step.

The purging process will try to remove at least half of the existing learned clauses, so that at most 3126 remain. We aren’t allowed to touch the 12 reason-bound ones; hence we want to forget 3114 of the other 6240. Which of them should we expel?

Among many heuristics that have been tried, the most successful in practice are based on what Gilles Audemard and Laurent Simon have called “ literal block distance” [see Proc. Int. Joint Conference on Artificial Intelligence 21 (2009), 399–404]. They observed that each level of the trail can be considered to be a block of more-or-less related variables; hence a long clause might turn out to be more useful than a short clause, if the literals of the long one all lie on just one or two levels while the literals of the short one belong to three or more.

Suppose all the literals of a clause C = l 1 ∨‧‧‧∨ l r appear in the trail, either positively as l j or negatively as l ¯ j . We can group them by level so that exactly p + q levels are represented, where p of the levels contain at least one positive l j and the other q contain nothing but l ¯ j 's . Then ( p , q ) is the signature of C with respect to the trail, and p + q is the literal block distance. For example, the very first clause learned from waerden (3, 10; 97) in the author’s test run was

11 ¯ ⁢   16 ¯ ⁢   21 ¯ ⁢   26 ¯ ⁢   36 ¯ ⁢   46 ¯ ⁢   51 ¯ ⁢   61 ⁢   66 ⁢       91 ; ( 122 )

later, when it was time to rank clauses for purging, the values and trail levels of those literals were specified by VAL( 11 ) , VAL( 16 ) , ... , VAL( 91 ) , which were

20 21 21 21 20 15 16 8 14 20.

Thus 61 was true on level 8 ≫ 1 = 4; 46 ¯ and 66 were true on level 15 ≫ 1 = 14 ≫ 1 = 7; 51 ¯ was false on level 8; the others were a mixture of true and false on level 10; hence ( 122 ) had p = 3 and q = 1 with respect to the current trail.

If C has signature ( p , q ) and C ′ has signature ( p ′, q ′), where p ≤ p ′ and q ≤ q ′ and ( p , q ) ≠ ( p ′, q ′), we can expect that C is more likely than C ′ to be useful in future propagations. The same conclusion is plausible also when p + q = p ′ + q ′ and p < p ′ , because C ′ won’t force anything until literals from at least p + 1 different levels change sign. These intuitive expectations are borne out by the following detailed data obtained from waerden (3, 10; 97):

( 0 4 17 22 30 54 67 99 17 17 81 191 395 360 404 438 66 6 63 232 463 536 521 386 117 6 0 52 243 291 298 308 112 22 0 0 18 59 86 77 53 7 0 0 0 0 8 3 10 0 0 0 0 0 0 0 1 0 0 0 0 0 0 ) ( 0 1 9 15 21 16 15 3 0 7 26 74 107 82 57 16 1 0 20 74 104 86 61 21 9 0 0 13 40 37 16 14 4 0 0 0 6 10 9 4 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )

The matrix on the left shows how many of the 6240 eligible clauses had a given signature ( p , q ), for 1 ≤ p ≤ 7 and 0 ≤ q ≤ 8; the matrix on the right shows how many would have been used to resolve future conflicts, if none of them had been removed. There were, for example, 536 learned clauses with p = q = 3, of which only 86 actually turned out to be useful. This data is illustrated graphically in Fig. 93 , which shows gray rectangles whose areas correspond to the left matrix, overlaid by black rectangles whose areas correspond to the right matrix. We can’t predict the future, but small ( p , q ) tends to increase the ratio of black to gray.

images

Fig. 93. Learned clauses that have p positive and q all-negative levels. The gray ones will never be used again. Unfortunately, there’s no easy way to distinguish gray from black without being clairvoyant.

An alert reader will be wondering, however, how such signatures were found, because we can’t compute them for all clauses until all variables appear in the trail — and that doesn’t happen until all clauses are satisfied! The answer [see A. Goultiaeva and F. Bacchus, LNCS 7317 (2012), 30–43] is that it’s quite possible to carry out a “ full run” in which every variable is assigned a value, by making only a slight change to the normal behavior of Algorithm C : Instead of resolving conflicts immediately and backjumping, we can carry on after each conflict until all propagations cease, and we can continue to build the trail in the same way until every variable is present on some level. Conflicts may have occurred on several different levels; but we can safely resolve them later, learning new clauses at that time. Meanwhile, a full trail allows us to compute signatures based on VAL fields. And those VAL fields go into the OVAL fields after backjumping, so the variables in each block will tend to maintain their relationships.

The author’s implementation of Algorithm C assigns an eight-bit value

RANGE ( c ) ← min ( ⌊ 16 ( p + α ⁢ q ) , 255 ⌋ ) ( 123 )

to each clause c ; here α is a parameter, 0 ≤ α ≤ 1. We also set RANGE( c ) ← 0 if c is the reason for some literal in the trail; RANGE( c ) ← 256 if c is satisfied at level 0. If there are m j clauses of range j , and if we want to keep at most T clauses in memory, we find the largest j ≤ 256 such that

s j \= m 0 + m 1 + ⋅ ⋅ ⋅ + m j − 1 ≤ T . ( 124 )

Then we retain all clauses for which RANGE( c ) < j , together with T − s j “ tie-breakers” that have RANGE( c ) = j (unless j = 256). When α has the relatively high value 15 16 \= .9375 , this rule essentially preserves as many clauses of small literal block distance as it can; and for constant p + q it favors those with small p .

For example, with α \= 15 16 and the data from Fig. 93 , we save clauses that have p = (1, 2, 3, 4, 5) when q ≤ (5, 4, 3, 2, 0), respectively. This gives us s 95 = 12 + 3069 clauses, just 45 shy of our target T = 3126. So we also choose 45 tie-breakers from among the 59 clauses that have RANGE( c ) = 95, ( p , q )= (5, 1).

Tie-breaking can be done by using a secondary heuristic ACT( c ) , “clause activity,” which is analogous to the activity score of a variable but it is more easily maintained. If clause c has been used to resolve the conflicts numbered 3, 47, 95, 99, and 100, say, then

ACT ( c ) \= ϱ − 3 + ϱ − 47 + ϱ − 95 + ϱ − 99 + ϱ − 100 . ( 125 )

This damping factor ϱ (normally .999) is independent of the factor ρ that is used for variable activities. In the case of Fig. 93 , if the 59 clauses with ( p , q ) = (5, 1) are arranged in order of increasing ACT scores, the gray-and-black pattern is

images

So if we retain the 45 with highest activity, we pick up 8 of the 10 that turn out to be useful. (Clause activities are imperfect predictors, but they are usually somewhat better than this example implies.)

Exercises 287 and 288 present full details of clause purging in accordance with these ideas. One question remains: After we’ve completed a purge, when should we schedule the next one? Successful results are obtained by having two parameters, Δ p and δ p . Initially M p = Δ p ; then after each purge, we set Δ p ← Δ p + δ p and M p ← M p +Δ p . For example, if Δ p = 10000 and δ p = 100, purging will occur after approximately 10000, 20100, 30300, 40600, ... , kΔ p + ( k 2 ) δ p , ... clauses have been learned; and the number of clauses at the beginning of the k th round will be approximately 20000 + 200 k = 2Δ p + 2 kδ p . (See exercise 289 .)

We’ve based this discussion on waerden (3, 10; 97), which is quite a simple problem. Algorithm C ’s gain from clause- purging on larger problems is naturally much more substantial. For example, waerden (3, 13; 160) is only a bit larger than waerden (3, 10; 97). With Δ p = 10000 and δ p = 100, it finishes in 132 gigamems, after learning 9.5 million clauses and occupying only 503 thousand MEM cells. Without purging, it proves unsatisfiability after learning only 7.1 million clauses, yet at well over ten times the cost: 4307 gigamems, and 102 million cells of MEM .

* Flushing literals and restarting

Algorithm C interrupts itself in step C5 not only to purge clauses but also to “flush literals” that may not have been the best choices for decisions in the trail. The task of solving a tough satisfiability problem is a delicate balancing act: We don’t want to get bogged down in the wrong part of the search space; but we also don’t want to lose the fruits of hard work by “throwing out the baby with the bath water.” A nice compromise has been found by Peter van der Tak, Antonio Ramos, and Marijn Heule [ J. Satisfiability, Bool. Modeling and Comp. 7 (2011), 133–138], who devised a useful way to rejuvenate the trail periodically by following trends in the activity scores ACT( k ) .

Let’s go back to Table 3 , to illustrate their method. After learning the clause ( 116 ), Algorithm C will update the trail by setting L 44 ← 57 on level 17; that will force L 45 ← 66 ¯ , because 39, 42, ... , 63 have all become true; and further positive literals 6, 58, 82, 86, 95, 96 will also join the trail in some order. Step C5 might then intervene to suggest that we should contemplate flushing some or all of the F = 52 literals whose values are currently assigned.

The decision literals 53 ¯ , 55, 44, ... , 51 on levels 1, 2, 3, ... , 17 each were selected because they had the greatest current activity scores when their level began. But activity scores are continually being updated, so the old ones might be considerably out of touch with present realities. For example, we’ve just boosted ACT( 53 ) , ACT( 27 ) , ACT( 36 ) , ACT( 70 ) , ... , in the process of learning ( 116 ) — see ( 115 ). Thus it’s quite possible that several of the first 17 decisions no longer seem wise, because those literals haven’t participated in any recent conflicts.

Let x k be a variable with maximum ACT( k ) , among all of the variables not in the current trail. It’s easy to find such a k (see exercise 290 ). Now consider, as a thought experiment, what would happen if we were to jump back all the way to level 0 at this point and start over. Recall that our phase-saving strategy dictates that we would set OVAL( j ) ← VAL( j ) just before setting VAL( j ) ← −1, as the variables become unassigned.

If we now restart at step C6 with d ← 1, all variables whose activity exceeds ACT( k ) will receive their former values (although not necessarily in the same order), because the corresponding literals will enter the trail either as decisions or as forced propagations. History will more-or-less repeat itself, because the old assignments did not cause any conflicts, and because phases were saved.

Therefore we might as well avoid most of this back-and-forth unsetting and resetting, by reusing the trail and jumping back only partway, to the first level where the current activity scores significantly change the picture:

Set ⁢     d ′ ← 0. ⁢   While ⁢     ACT ⁢ ( | L i d ′ + 1 | ) ≥ ACT ( k ) ,   set ⁢       d ′ ← d ′ + 1 . Then ⁢     if ⁢   d ′ < d , jump ⁢   back ⁢   to ⁢   level ⁢   d ′ . ( 126 )

This is the technique called “literal flushing,” because it removes the literals on levels d ′ + 1 through d and leaves the others assigned. It effectively redirects the search into new territory, without being as drastic as a full restart.

In Table 3 , for example, ACT( 49 ) might exceed the activity score of every other unassigned variable; and it might also exceed ACT( 46 ) , the activity of the decision literal 46 ¯ on level 15. If the previous 14 decision-oriented activities ACT( 53 ) , ACT( 55 ) , ... , ACT( 37 ) are all ≥ ACT( 49 ) , we would flush all the literals L 25 , L 26 , ... above level d ′ = 14, and commence a new level 15.

Notice that some of the flushed literals other than 46 ¯ might actually have the largest activities of all. In such cases they will re-insert themselves, before 49 ever enters the scene. Eventually, though, the literal 49 will inaugurate a new level before a new conflict arises. (See exercise 291 .)

Experience shows that flushing can indeed be extremely helpful. On the other hand, it can be harmful if it causes us to abandon a fruitful line of attack. When the solver is perking along and learning useful clauses by the dozen, we don’t want to upset the applecart by rocking the boat. Armin Biere has therefore introduced a useful statistic called agility , which tends to be correlated with the desirability of flushing at any given moment. His idea [ LNCS 4996 (2008), 28– 33] is beautifully simple: We maintain a 32-bit integer variable called AGILITY , initially zero. Whenever a literal l is placed on the trail in steps C4, C6, or C9, we update the agility by setting

AGILITY ← AGILITY − ( AGILITY ≫ 13 ) + ( ( ( OVAL | l | − VAL ( | l | ) ) & 1 ) ≪ 19 ) . ( 127 )

In other words, the fraction AGILITY /2 32 is essentially multiplied by 1 − δ , then increased by δ if the new polarity of l differs from its previous polarity, where δ = 2 −13 ≈ .0001. High agility means that lots of the recent propagations are flipping the values of variables and trying new possibilities; low agility means that the algorithm is basically in a rut, spinning its wheels and getting nowhere.

Table 4
TO FLUSH OR NOT TO FLUSH?

Let a = AGILITY /2 32 when setting M f ← M +Δf , and let ψ = 1/6, θ = 17/16.

If Δ f is

then flush if

If Δf is

then flush if

If Δf is

then flush if

1

a ≤ ψ ≈ .17

32

a ≤ θ 5 ψ ≈ .23

1024

a ≤ θ 10 ψ ≈ .31

2

a ≤ θψ ≈ .18

64

a ≤ θ 6 ψ ≈ .24

2048

a ≤ θ 11 ψ ≈ .32

4

a ≤ θ 2 ψ ≈ .19

128

a ≤ θ 7 ψ ≈ .25

4096

a ≤ θ 12 ψ ≈ .34

8

a ≤ θ 3 ψ ≈ .20

256

a ≤ θ 8 ψ ≈ .27

8192

a ≤ θ 13 ψ ≈ .37

16

a ≤ θ 4 ψ ≈ .21

512

a ≤ θ 9 ψ ≈ .29

16384

a ≤ θ 14 ψ ≈ .39

Armed with the notion of agility, we can finally state what Algorithm C does when step C5 finds M ≥ M f : First M f is reset to M + Δ f , where Δ f is a power of two determined by the “ reluctant doubling” sequence 〈1, 1, 2, 1, 1, 2, 4, 1, ...〉; that sequence is discussed below and in exercise 293 . Then the agility is compared to a threshold, depending on Δ f , according to the schedule in Table 4 . (The parameter ψ in that table can be raised or lowered, if you want to increase or decrease the amount of flushing.) If the agility is sufficiently small, x k is found and ( 126 ) is performed. Nothing changes if the agility is large or if d ′ = d ; otherwise ( 126 ) has flushed some literals, using the operations of step C8.

Monte Carlo algorithms

Let’s turn now to a completely different way to approach satisfiability problems, based on finding solutions by totally heuristic and randomized methods, often called stochastic local search . We often use such methods in our daily lives, even though there’s no guarantee of success. The simplest satisfiability-oriented technique of this kind was introduced by Jun Gu [see SIGART Bulletin 3 , 1 (January 1992), 8–12] and by Christos Papadimitriou [ FOCS 32 (1991), 163–169] as a byproduct of more general studies:

“Start with any truth assignment. While there are unsatisfied clauses, pick any one, and flip a random literal in it.”

Some programmers are known to de bug their code in a haphazard manner, somewhat like this approach; and we know that such “blind” changes are foolish because they usually introduce new bugs. Yet this idea does have merit when it is applied to satisfiability, so we shall formulate it as an algorithm:

Algorithm P ( Satisfiability by random walk ). Given m nonempty clauses C 1 ∧ ‧‧‧ ∧ C m on n Boolean variables x 1 ... x n , this algorithm either finds a solution or terminates unsuccessfully after making N trials.

P1. [Initialize.] Assign random Boolean values to x 1 ...x n . Set j ← 0, s ← 0, and t ← 0. (We know that s clauses are satisfied after having made t flips.)

P2. [Success?] If s = m , terminate successfully with solution x 1 ... x n . Otherwise set j ← ( j mod m )+1. If clause C j is satisfied by x 1 ... x n , set s ← s +1 and repeat this step.

P3. [Done?] If t = N , terminate unsuccessfully.

P4. [Flip one bit.] Let clause C j be ( l 1 ∨ ‧‧‧ ∨ l k ). Choose a random index i ∈ {1,..., k }, and change variable | l i | so that literal l i becomes true. Set s ← 1, t ← t + 1, and return to P2.

Suppose, for example, that we’re given the seven clauses R ′ of ( 7 ). Thus m = 7, n = 4; and there are two solutions, 01∗1. In this case every nonsolution violates a unique clause; for example, 1100 violates the clause 1 ¯ 2 ¯ 3 , so step P4 is equally likely to change 1100 to 0100, 1000, or 1110, only one of which is closer to a solution. An exact analysis (see exercise 294 ) shows that Algorithm P will find a solution after making 8.25 flips, on the average. That’s no improvement over a brute-force search through all 2 n = 16 possibilities; but a small example like this doesn’t tell us much about what happens when n is large.

Papadimitriou observed that Algorithm P is reasonably effective when it’s applied to 2SAT problems, because each flip has roughly a 50-50 chance of making progress in that case. Several years later, Uwe Schöning [ Algorithmica 32 (2002), 615–623] discovered that the algorithm also does surprisingly well on instances of 3SAT, even though the flips when k > 2 in step P4 tend to go “the wrong way”:

Theorem U. If the given clauses are satisfiable, and if each clause has at most three literals, Algorithm P will succeed with probability Ω((3/4) n / n ) after making at most n flips.

Proof. By complementing variables, if necessary, we can assume that 0 ... 0 is a solution; under this assumption, every clause has at least one negative literal. Let X t = x 1 + ‧‧‧ + x n be the number of 1s after t flips have been made. Each flip changes X t by ±1, and we want to show that there’s a nontrivial chance that x t will become 0. After step P1, the random variable X 0 will be equal to q with probability ( q n ) / 2 n .

A clause that contains three negative literals is good news for Algorithm P , because it is violated only when all three variables are 1; a flip will always decrease X t in such a case. Similarly, a violated clause with two negatives and one positive will invoke a flip that makes progress 2/3 of the time. The worst case occurs only when a problematic clause has only one negative literal. Unfortunately, every clause might belong to this worst case, for all we know.

Instead of studying X t , which depends on the pattern of clauses, it’s much easier to study another random variable Y t defined as follows: Initially Y 0 = X 0 ; but Y t +1 \= Y t − 1 only when step P4 flips the negative literal that has the smallest subscript; otherwise Y t +1 \= Y t + 1. For example, after taking care of a violated clause such as x 3 ∨ x ¯ 5 ∨ x ¯ 8 , we have X t +1 = X t +(+1, −1, −1) but Y t +1 = Y t + (+1, −1, +1) in the three possible cases. Furthermore, if the clause contains fewer than three literals, we penalize Y t +1 even more, by allowing it to be Y t − 1 only with probability 1/3. (After a clause such as x 4 ∨ x ¯ 6 , for instance, we put Y t +1 = Y t −1 in only 2/3 of the cases when x 6 is flipped; otherwise Y t +1 \= Y t +1.)

We clearly have X t ≤ Y t for all t . Therefore Pr( X t = 0) ≥ Pr( Y t = 0), after t flips have been made; and we’ve defined things so that it’s quite easy to calculate Pr( Y t = 0), because Y t doesn’t depend on the current clause j :

Pr( Y t +1 \= Y t − 1) = 1/3 and Pr( Y t +1 \= Y t +1) = 2/3 when Y t > 0.

Indeed, the theory of random walks developed in Section 7.2.1.6 tells us how to count the number of scenarios that begin with Y 0 = q and end with Y t = 0, after Y t has increased p times and decreased p + q times while remaining positive for 0 ≤ t < 2 p + q . It is the “ ballot number” of Eq. 7.2.1.6–(23),

C p , p + q − 1 \= q 2 p + q ( 2 p + q p ) . ( 128 )

The probability that Y 0 = q and that Y t = 0 for the first time when t = 2 p + q is therefore exactly

f ( p , q ) \= 1 2 n ( n q ) q 2 p + q ( 2 p + q p ) ( 1 3 ) p + q ( 2 3 ) p . ( 129 )

Every value of p and q gives a lower bound for the probability that Algorithm P succeeds; and exercise 296 shows that we get the result claimed in Theorem U by choosing p = q ≈ n /3.

Theorem U might seem pointless, because it predicts success only with exponentially small probability when N = n . But if at first we don’t succeed, we can try and try again, by repeating Algorithm P with different random choices. And if we repeat it Kn (4/3) n times, for large enough K , we’re almost certain to find a solution unless the clauses can’t all be satisfied.

In fact, even more is true, because the proof of Theorem U doesn’t exploit the full power of Eq. ( 129 ). Exercise 297 carries the analysis further, in a particularly instructive way, and proves a much sharper result:

Corollary W. When Algorithm P is applied K (4/3) n times with N = 2 n to a set of satisfiable ternary clauses, its success probability exceeds 1 − e − K /2 .

If the clauses C 1 ∧‧‧‧∧ C m are unsatisfiable, Algorithm P will never demonstrate that fact conclusively. But if we repeat it 100(4/3) n times and get no solution, Corollary W tells us that the chances of satisfiability are incredibly small (less than 10 −21 ). So it’s a safe bet that no solution exists in such a case.

Thus Algorithm P has a surprisingly good chance of finding solutions “with its eyes closed,” while walking at random in the gigantic space of all 2 n binary vectors; and we can well imagine that even better results are possible if we devise randomized walking methods that proceed with eyes wide open. Therefore many people have experimented with strategies that try to make intelligent choices about which direction to take at each flip-step. One of the simplest and best of these improvements, popularly known as WalkSAT, was devised by B. Selman, H. A. Kautz, and B. Cohen [ Nat. Conf. Artificial Intelligence 12 (1994), 337–343]:

Algorithm W ( WalkSAT ). Given m nonempty clauses C 1 ∧ ‧‧‧ ∧ C m on n Boolean variables x 1 ... x n , and a “ greed-avoidance” parameter p , this algorithm either finds a solution or terminates unsuccessfully after making N trials. It uses auxiliary arrays c 1 ... c n , f 0 ... f m −1 , k 1 ... k m , and w 1 ... w m .

W1. [Initialize.] Assign random Boolean values to x 1 ... x n . Also set r ← t ← 0 and c 1 ... c n ← 0 ... 0. Then, for 1 ≤ j ≤ m , set k j to the number of true literals in C j ; and if k j = 0, set f r ← j , w j ← r , and r ← r + 1; or if k j = 1 and the only true literal of C j is x i or x ¯ i , set c i ← c i + 1. (Now r is the number of unsatisfied clauses, and the f array lists them. The number C i is the “cost” or “ break count” for variable x i , namely the number of additional clauses that will become false if x i is flipped.)

W2. [Done?] If r = 0, terminate successfully with solution x 1 ... x n . Otherwise, if t = N , terminate unsuccessfully.

W3. [Choose j .] Set j ← f q , where q is uniformly random in {0, 1,..., r − 1}. (In other words, choose an unsatisfied clause C j at random, considering every such clause to be equally likely; exercise 3.4.1–3 discusses the best way to compute q .) Let clause C j be ( l 1 ∨ ‧‧‧ ∨ l k ).

W4. [Choose l .] Let c be the smallest cost among the literals { l 1 ,..., l k }. If c = 0, orif c ≥ 1 and U ≥ p where U is uniform in [0 .. 1), choose l randomly from among the literals of cost c . (We call this a “ greedy” choice, because flipping l will minimize the number of newly false clauses.) Otherwise choose l randomly in { l 1 ,..., l k }.

W5. [Flip l .] Change the value of variable | l |, and update r , c 1 ... c n , f 0 ... f r −1 , k 1 ... k m , w 1 ... w m to agree with this new value. ( Exercise 302 explains how to implement steps W4 and W5 efficiently, with computer-friendly changes to the data structures.) Set t ← t + 1 and return to W2.

If, for example, we try to satisfy the seven clauses of ( 7 ) with Algorithm W , as we did earlier with Algorithm P , the choice x 1 x 2 x 3 x 4 = 0110 violates 2 ¯ 3 ¯ 4 ; and c 1 c 2 c 3 c 4 turns out to be 0110 in this situation. So step W4 will choose to flip x 4 , and we’ll have the solution 0111. (See exercise 303 .)

Notice that step W3 focuses attention on clauses whose variables need to change. Furthermore, a literal that appears in the most unsatisfied clauses is most likely to appear in the chosen clause C j .

If no cost-free flip is available, step W4 makes nongreedy choices with probability p . This policy keeps the algorithm from getting stuck in an unsatisfiable region from which there’s no greedy exit. Extensive experiments by S. Seitz, M. Alava, and P. Orponen [ J. Statistical Mechanics (June 2005), P06006:1–27] indicate that the best choice of p is .57 when large random 3SAT problems are being tackled and when N = ∞. For example, with this setting of p , and with m = 4.2 n random 3-literal clauses, Algorithm W works fantastically well: It tends to find solutions after making fewer than 10,000n flips when n = 10 4 , and fewer than 2500n flips when 10 5 ≤ n ≤ 10 6 .

What about the parameter N ? Should we set it equal to 2 n (as recommended for 3SAT problems with respect to Algorithm P ), or perhaps to n 2 (as recommended for 2SAT in exercise 299 ), or to 2500 n (as just mentioned for 3SAT in Algorithm W ), or to something else? When we use an algorithm like WalkSAT, whose behavior can vary wildly depending on random choices and on unknown characteristics of the data, it’s often wise to “cut our losses” and to start afresh with a brand new pattern of random numbers.

Exercise 306 proves that such an algorithm always has an optimum cutoff value N = N ∗ , which minimizes the expected time to success when the algorithm is restarted after each failure. Sometimes N ∗ = ∞ is the best choice, meaning that we should always keep plowing ahead; in other cases N ∗ is quite small.

But N ∗ exists only in theory, and the theory requires perfect knowledge of the algorithm’s behavior. In practice we usually have little or no information about how N should best be specified. Fortunately there’s still an effective way to proceed, by using the notion of reluctant doubling introduced by M. Luby, A. Sinclair, and D. Zuckerman [ Information Proc. Letters 47 (1993), 173–180], who defined the interesting sequence

S 1 , S 2 , ... \= 1 , 1 , 2 , 1 , 1 , 2 , 4 , 1 , 1 , 2 , 1 , 1 , 2 , 4 , 8 , 1 , 1 , 2 , 1 , 1 , 2 , 4 , 1 , 1 , 2 , ... ⁢ . ( 130 )

The elements of this sequence are all powers of 2. Furthermore we have S n +1 \= 2 S n if the number S n has already occurred an even number of times, otherwise S n +1 = 1. A convenient way to generate this sequence is to work with two integers ( u , v ), and to start with ( u 1 , v 1 ) = (1, 1); then

( u n + 1 , v n + 1 ) \= ( u n & − u n \= v n ? ( u n + 1 , 1 ) : ( u n , 2 v n ) ) . ( 131 )

The successive pairs are (1, 1), (2, 1), (2, 2), (3, 1), (4, 1), (4, 2), (4, 4), (5, 1), ... , and we have S n = v n for all n ≥ 1.

The reluctant doubling strategy is to run Algorithm W repeatedly with N = cS 1 , cS 2 , cS 3 , ... , until success is achieved, where c is some constant. Exercise 308 proves that the expected running time X obtained in this way exceeds the optimum by at most a factor of O (log X ). Other sequences besides 〈 S n 〉 also have this property, and they’re sometimes better (see exercise 311 ). The best policy is probably to use 〈 cS n 〉, where c represents our best guess about the value of N ∗ ; in this way we hedge our bets in case c is too small.

The Local Lemma

The existence of particular combinatorial patterns is often established by using a nonconstructive proof technique called the “ probabilistic method,” pioneered by Paul Erdős. If we can show that Pr( X ) > 0, in some probability space, then X must be true in at least one case. For example [ Bull. Amer. Math. Soc. 53 (1947), 292–294], Erdős famously observed that there is a graph G on n vertices such that neither G nor G ¯ contains a k - clique, whenever

( n k ) < 2 k ( k − 1 ) / 2 − 1 . ( 132 )

For if we consider a random graph G, each of whose ( n k ) edges is present with probability 1/2, and if U is any particular subset of k vertices in G , the probability that either G | U or G ¯ ⁢   |   U is a complete graph is clearly 2/2 k ( k −1)/2 . Hence the probability that this doesn’t happen for any of the ( n k ) subsets U is at least 1 − ( n k ) 2 1 − k ( k − 1 ) / 2 . This probability is positive; so such a graph must exist.

The proof just given does not provide any explicit construction. But it does show that we can find such a graph by making at most 1 / ( 1 − ( n k ) 2 1 − k ( k − 1 ) / 2 ) random trials, on the average, provided that n and k are small enough that we are able to test all ( n k ) subgraphs in a reasonable amount of time.

Probability calculations of this kind are often complicated by dependencies between the random events being considered. For example, the presence of a clique in one part of a graph affects the likelihood of many other cliques that share some of the same vertices. But the interdependencies are often highly localized, so that “remote” events are essentially independent of each other. László Lovász introduced an important way to deal with such situations early in the 1970s, and his approach has become known as the “Local Lemma” because it has been used to establish many theorems. First published as a lemma on pages 616–617 of a longer paper [Erdős and Lovász, Infinite and Finite Sets , Colloquia Math. Soc. János Bolyai 10 (1975), 609–627], and subsequently extended to a “lopsided” form [P. Erdős and J. Spencer, Discrete Applied Math. 30 (1991), 151–154], it can be stated as follows:

Lemma L. Let A 1 , ... , A m be events in some probability space. Let G be a graph on vertices {1,..., m }, and let ( p 1 ,..., p m ) be numbers such that

Pr ( A i | A ¯ j 1 ∩ ⋅ ⋅ ⋅ ∩ A ¯ j ⁢ k ) ≤ p i ⁢     whenever ⁢     k ≥ 0 ⁢     and ⁢     i ⁢   — ⁢   j 1 , ... , i ⁢   — ⁢   j k . ( 133 )

Then Pr ( A ¯ 1 ∩ ⋅ ⋅ ⋅ ∩ A ¯ m ) > 0 whenever ( p 1 ,..., p m ) lies in a certain set R ( G ).

In applications we think of the A j as “bad” events, which are undesirable conditions that interfere with whatever we’re trying to find. The graph G is called a “ lopsidependency graph” for our application; this name was coined as an extension of Lovász’s original term “ dependency graph,” for which the strict condition ‘= p i ’ was assumed in place of ‘≤ p i ’ in ( 133 ).

The set R ( G ) of probability bounds for which we can guarantee that all bad events can simultaneously be avoided, given ( 133 ), will be discussed further below. If G is the complete graph K m , so that ( 133 ) simply states that Pr( A i ) ≤ p i , R ( G ) is clearly {( p 1 ,..., p m ) | ( p 1 ,..., p m ) ≥ (0,..., 0) and p 1 + ‧‧‧ + p m < 1}; this is the smallest possible R ( G ). At the other extreme, if G is the empty graph K m ¯ , we get {( p 1 ,..., p m ) | 0 ≤ p j < 1 for 1 ≤ j ≤ m }, the largest possible R ( G ). Adding an edge to G makes R ( G ) smaller. Notice that, if ( p 1 ,..., p m ) is in R ( G ) and 0 ≤ p j ′ ≤ p j for 1 ≤ j ≤ m , then also ( p 1 ′ , ... , p m ′ ) ∈ R ( G ) .

Lovász discovered an elegant local condition that suffices to make Lemma L widely applicable [see J. Spencer, Discrete Math. 20 (1977), 69–76]:

Theorem L. The probability vector ( p 1 ,..., p m ) is in R ( G ) when there are numbers 0 ≤ θ 1 ,..., θ m < 1 such that

p i \= θ i ⁢       Π i − j ⁢   in ⁢ G ( 1 − θ j ) . ⁢ ( 134 )

Proof. Exercise 344(e) proves that Pr ( A ¯ 1 ∩ ⋅ ⋅ ⋅ ∩ A ¯ m ) ≥ ( 1 − θ 1 ) ⁢ ... ⁢ ( 1 − θ m ) .

James B. Shearer [ Combinatorica 5 (1985), 241–245] went on to determine the exact maximum extent of R ( G ) for all graphs G , as we’ll see later; and he also established the following important special case:

Theorem J. Suppose every vertex of _G has degree ≤ _d, where d > 1. Then the probability vector ( p ,..., p ) is in R ( G ) when p ≤ ( d − 1) d −1 / d d .

Proof. See the interesting inductive argument in exercise 317 .

This condition on p holds whenever p ≤ 1/( ed ) (see exercise 319 ).

Further study led to a big surprise: The Local Lemma proves only that desirable combinatorial patterns exist , although they might be rare. But Robin Moser and Gábor Tardos discovered [ JACM 57 (2010), 11:1–11:15] that we can efficiently compute a pattern that avoids all of the bad a j , using an almost unbelievably simple algorithm analogous to WalkSAT!

Algorithm M ( Local resampling ). Given m events { A 1 ,..., A m } that depend on n Boolean variables { x 1 ,..., x n }, this algorithm either finds a vector x 1 ... x n for which none of the events is true, or loops forever. We assume that A j is a function of the variables { x k | k ∈ Ξ j } for some given subset Ξ j ⊆ {1,..., n }.

Whenever the algorithm assigns a value to x k , it sets x k ← 1 with probability ξ k and x k ← 0 with probability 1 − ξ k , where ξ k is another given parameter.

M1. [Initialize.] For 1 ≤ k ≤ n , set x k ← [ U < ξ k ], where U is uniform in [0 .. 1).

M2. [Choose j .] Set j to the index of any event such that A j is true. If no such j exists, terminate successfully, having found a solution x 1 ... x n .

M3. [Resample for A j .] For each k ∈ Ξ j , set x k ← [ U <ξ k ], where U is uniform in [0 .. 1). Return to M2.

(We have stated Algorithm M in terms of binary variables x k purely for convenience. The same ideas apply when each x k has a discrete probability distribution on any set of values, possibly different for each k .)

To tie this algorithm to the Local Lemma, we assume that event A i holds with probability ≤ p i whenever the variables it depends on have the given distribution. For example, if a i is the event “ x 3 ≠ x 5 ” then p i must be at least ξ 3 (1 − ξ 5 )+(1 − ξ 3 )ξ 5 .

We also assume that there’s a graph G on vertices {1,..., m } such that condition ( 133 ) is true, and that i −−− j whenever i ≠ j and Ξ i ∩ Ξ j ≠ ∅. Then G is a suitable dependency graph for { A 1 ,..., A m }, because the events A j 1 , ... , A k can’t possibly influence A i when i ⁢   — ⁢ j 1 , ... , i ⁢   — ⁢   j k . (Those events share no common variables with a i .) We can also sometimes get by with fewer edges by making G a lopsidependency graph; see exercise 351 .

Algorithm M might succeed with any given events, purely by chance. But if the conditions of the Local Lemma are satisfied, success can be guaranteed:

Theorem M. If ( 133 ) holds with probabilities that satisfy condition ( 134 ) of Theorem L, step _M3 is performed for a _j at most θ j /(1 − θ j ) times, on average.

Proof. Exercise 352 shows that this result is a corollary of the more general analysis that is carried out below. The stated upper bound is good news, because θ j is usually quite small.

Traces and pieces

The best way to understand why Algorithm M is so efficient is to view it algebraically in terms of “traces.” The theory of traces is a beautiful area of mathematics in which amazingly simple proofs of profound results have been discovered. Its basic ideas were first formulated by P. Cartier and D. Foata [ Lecture Notes in Math. 85 (1969)], then independently developed from another point of view by R. M. Keller [ JACM 20 (1973), 514–537, 696–710] and A. Mazurkiewicz [“Concurrent program schemes and their interpretations,” DAIMI Report PB 78 (Aarhus University, July 1977)]. Significant advances were made by G. X. Viennot [ Lecture Notes in Math. 1234 (1985), 321–350], who presented many wide-ranging applications and explained how the theory could readily be visualized in terms of what he called “ heaps of pieces.”

Trace theory is the study of algebraic products whose variables are not necessarily commutative. Thus it forms a bridge between the study of strings (in which, for example, acbbaca is quite distinct from baccaab ) and the study of ordinary commutative algebra (in which both of those examples are equal to aaabbcc = a 3 b 2 c 2 ). Each adjacent pair of letters { a , b } either commutes, meaning that ab = ba , or clashes, meaning that ab is different from ba . If, for instance, we specify that a commutes with c but that b clashes with both a and c , then acbbaca is equal to cabbaac, and it has six variants altogether; similarly, there are ten equally good ways to write baccaab .

Formally speaking, a trace is an equivalence class of strings that can be converted to each other by repeatedly interchanging pairs of adjacent letters that don’t clash. But we don’t need to fuss about the fact that equivalence classes are present; we can simply represent a trace by any one of its equivalent strings, just as we don’t distinguish between equivalent fractions such as 1/2 and 3/6.

Every graph whose vertices represent distinct letters defines a family of traces on those letters, when we stipulate that two letters clash if and only if they are adjacent in the graph. For example, the path graph a −−− b −−− c corresponds to the rules stated above. The distinct traces for this graph are

∈ , a , b , c , a ⁢ a , a ⁢ b , a ⁢ c , b ⁢ a ,   b ⁢ b , b ⁢ c , c ⁢ b , c ⁢ c , a ⁢ a ⁢ a ,   a ⁢ a ⁢ b , ... ⁢ , c ⁢ c ⁢ b , c ⁢ c ⁢ c , a ⁢ a ⁢ a ⁢ a , ... ( 135 )

if we list them first by size and then in lexicographic order. (Notice that ca is absent, because ac has already appeared.) The complete graph K n defines traces that are the same as strings, when nothing commutes; the empty graph K n defines traces that are the same as monomials, when everything commutes. If we use the path a −−− b −−− c −−− d −−− e −−− f to define clashes, the traces bcebafdc and efbcdbca turn out to be the same.

Viennot observed that partial commutativity is actually a familiar concept, if we regard the letters as “ pieces” that occupy “ territory.” Pieces clash if and only if their territories overlap; pieces commute if and only if their territories are disjoint. A trace corresponds to stacking the pieces on top of one another, from left to right, letting each new piece “fall” until it either rests on the ground or on another piece. In the latter case, it must rest on the most recent piece with which it clashes. He called this configuration an empilement — a nice French word.

More precisely, each piece a is assigned a nonempty subset T ( a ) of some universe, and we say that a clashes with b if and only if T ( a ) ∩ T ( b ) ≠ ∅. For example, the constraints of the graph a −−− b −−− c −−− d −−− e −−− f arise when we let

T ( a )= {1, 2}, T ( b ) = {2, 3}, T ( c ) = {3, 4}, ..., T ( f ) = {6, 7};

then the traces bcebafdc and efbcdbca both have

images

as their empilement. (Readers who have played the game of Tetris® will immediately understand how such diagrams are formed, although the pieces in trace theory differ from those of Tetris because they occupy only a single horizontal level. Furthermore, each type of piece always falls in exactly the same place; and a piece’s territory T ( a ) might have “holes” — it needn’t be connected.)

Two traces are the same if and only if they have the same empilement. In fact, the diagram implicitly defines a partial ordering on the pieces that appear; and the number of different strings that represent any given trace is the number of ways to sort that ordering topologically (see exercise 324 ).

Every trace α has a length , denoted by | α |, which is the number of letters in any of its equivalent strings. It also has a height , written h ( α ), which is the number of levels in its empilement. For example, | bcebafdc | = 8 and h ( bcebafdc )= 4.

Arithmetic on traces

To multiply traces, we simply concatenate them. If, for example, α = bcebafdc is the trace corresponding to ( 136 ), then αα R = bcebafdccdfabecb has the following empilement:

images

The algorithm in exercise 327 formulates this procedure precisely. A moment’s thought shows that | αβ | = | α | + | β |, h ( αβ ) ≤ h ( α ) + h ( β ), and h ( αα R ) = 2 h ( α ).

Traces can also be divided , in the sense that α = ( αβ )/ β can be determined uniquely when αβ and β are given. All we have to do is remove the pieces of β from the pieces of αβ , one by one, working our way down from the top of the empilements. Similarly, the value of β = α \ ( αβ ) can be computed from the traces α and αβ . (See exercises 328 and 329 .)

Notice that we could rotate diagrams like ( 136 ) and ( 137 ) by 90 degrees, thereby letting the pieces “fall” to the left instead of downwards. (We’ve used a left-to- right approach for similar purposes in Section 5.3.4, Fig. 50.) Or we could let them fall upwards, or to the right. Different orientations are sometimes more natural, depending on what we’re trying to do.

We can also add and subtract traces, thereby obtaining polynomials in variables that are only partially commutative. Such polynomials can be multiplied in the normal way; for example, ( α + β )( γ − δ ) = αγ − αδ + βγ − βδ . Indeed, we can even work with infinite sums, at least formally: The generating function for all traces that belong to the graph a −−− b −−− c is

1 + a + b + c + a ⁢ a + a ⁢ b + a ⁢ c + b ⁢ a + b ⁢ b + b ⁢ c + c ⁢ b + c ⁢ c + a ⁢ a ⁢ a + ⋅ ⋅ ⋅ + c ⁢ c ⁢ c ⁢ + a ⁢ a ⁢ a ⁢ a + ⋅ ⋅ ⋅ . ( 138 )

(Compare with ( 135 ); we now use 1, not , to stand for the empty string.)

The infinite sum ( 138 ) can actually be expressed in closed form: It equals

1 1 − a − b − c ⁢ +   a ⁢ c \= 1 + ( a + b + c − a ⁢ c ) + ( a + b + c − a ⁢ c ) 2 + ⋅ ⋅ ⋅ , ( 139 )

an identity that is correct not only when the variables are commutative, but also in the algebra of traces, when variables commute only when they don’t clash.

In their original monograph of 1969, Cartier and Foata showed that the sum of all traces with respect to any graph can be expressed in a remarkably simple way that generalizes ( 139 ). Let’s define the Möbius function of a trace α with respect to a graph G by the rule

μ G ( α ) \= { 0 , if ⁢   h G ( α ) > 1 ; ( − 1 ) | α | , otherwise . ( 140 )

(The classical Möbius function μ ( n ) for integers, defined in exercise 4.5.2–10, is analogous.) Then the Möbius series for G is defined to be

M G \= Σ α μ G ( α ) α , ( 141 )

where the sum is over all traces. This sum is a polynomial, when G is finite, because it contains exactly one nonzero term for every independent set of vertices in G ; therefore we might call it the Möbius polynomial . For example, when G is the path a −−− b −−− c , we have M G = 1 − a − b − c + ac , the denominator in ( 139 ). Cartier and Foata’s generalization of ( 139 ) has a remarkably simple proof:

Theorem F. The generating function T G for the sum of all traces, with respect to any graph G, is 1/ M G .

Proof. We want to show that M G T G = 1, in the (partially commutative) algebra of traces. This infinite product is ∑ α,β μ G ( α ) αβ = ∑ γ ∑ α,β μ G (α) γ [ γ = αβ ].

Hence we want to show that the sum of μ G ( α ), over all ways to factorize γ = αβ as the product of two traces α and β , is zero whenever γ is nonempty.

But that’s easy. We can assume that the letters are ordered in some arbitrary fashion. Let a be the smallest letter in the bottom level of γ ’s empilement. We can restrict attention to cases where α consists of independent (commuting) letters (pieces), because μ G ( α ) = 0 otherwise. Now if α = aα ′ for some trace α ′ , let β ′ = aβ ; otherwise we must have β = aβ ′ for some trace β ′ , and we let α ′ = aα . In both cases αβ = α ′ β ′ , ( α ′ )′ = α , ( β ′)′ = β , and μ G ( α )+ μ G ( α ′ ) = 0. So we’ve grouped all possible factorizations of γ into pairs that cancel out in the sum.

The Möbius series for any graph can be computed recursively via the formula

M G \= M G \ a − a ⁢ M G \ a * , a * \= { a } ∪ { b ⁢ |     a―b } , ( 142 )

where a is any letter (vertex) of G , because we have a ∉ I or a ∈ I whenever I is independent. For example, if G is the path a −−− b −−− c −−− d −−− e −−− f , then G \ a ∗ = G | { c , d , e , f } is the path c −−− d −−− e −−− f ; repeated use of ( 142 ) yields

M G ⁢ \=   1 − a − b − c − d − e − f + a ⁢ c + a ⁢ d + a ⁢ e + a ⁢ f                                                + b ⁢ d + b ⁢ e + b ⁢ f + c ⁢ e + c ⁢ f + d ⁢ f − a ⁢ c ⁢ e − a ⁢ c ⁢ f − a ⁢ d ⁢ f − b ⁢ d ⁢ f ( 143 )

in this case. Since M G is a polynomial, we can indicate its dependence on the variables by writing M G ( a, b, c, d, e, f ). Notice that M G is always multilinear (that is, linear in each variable); and M G \ a ( b, c, d, e, f ) = M G (0, b,c,d,e,f ).

In applications we often want to replace each letter in the polynomial by a single variable, such as z , and write M G ( z ). The polynomial in ( 143 ) then becomes M G ( z ) = 1−6 z +10 z 2 − 4 z 3 ; and we can conclude from Theorem F that the number of traces of length n with respect to G is [ z n ] 1 / ( 1 − 6 z + 10 z 2 − 4 z 3 ) \= 1 4 ( 2 + 2 ) n + 2 + 1 4 ( 2 − 2 ) n + 2 − 2 n + 1 .

Although ( 142 ) is a simple recurrence for M G , we can’t conclude that M G is easy to compute when G is a large and complicated graph. Indeed, the degree of M G is the size of a maximum independent set in G ; and it’s NP-hard to determine that number! On the other hand, there are many classes of graphs, such as interval graphs and forests, for which M G can be computed in linear time.

If α is any trace, the letters that can occur first in a string that represents it are called the sources of α ; these are the pieces on the bottom level of α ’s empilement, also called its minimal pieces. Dually, the letters that can occur last are the sinks of α , its maximal pieces. A trace that has only one source is called a cone ; in this case all pieces are ultimately supported by a single piece at the bottom. A trace that has only one sink is, similarly, called a pyramid . Viennot proved a nice generalization of Theorem F in his lecture notes:

M G \ A / M G ⁢       is ⁢   the ⁢   sum ⁢   of ⁢   all ⁢   traces ⁢   whose ⁢   sources ⁢   are ⁢   contained ⁢   in ⁢   A . ( 144 )

(See exercise 338 ; Theorem F is the special case where A is the set of all vertices.) In particular, the cones for which a is the only source are generated by

M G \ a / M G − 1 \= a ⁢     M G \ a * / M G . ( 145 )

*Traces and the Local Lemma

Now we’re ready to see why the theory of traces is intimately connected with the Local Lemma. If G is any graph on the vertices {1,..., m }, we say that R ( G ) is the set of all nonnegative vectors ( p 1 ,..., p m ) such that M G ( p 1 ′ , ... , p m 1 ) > 0 whenever 0 ≤ p j ′ ≤ p j for 1 ≤ j ≤ m . This definition of R ( G ) is consistent with the implicit definition already given in Lemma L, because of the following characterization found by J. B. Shearer:

Theorem S

Under condition ( 133 ) of Lemma L, ( p 1 ,..., p m ) ∈ R ( G ) implies

Pr ( A ¯ 1 ∩ ⋅ ⋅ ⋅ ∩ A ¯ m ) ≥ M G ( p 1 , ... , p m ) > 0. ( 146 )

Conversely, if ( p 1 ,..., p m ) ∉ R ( G ) , there are events B 1 , ... , B m such that

            Pr ( B i |   B ¯ j 1 ∩ ⋅ ⋅ ⋅ ∩ B ¯ j ⁢ k ) \= p ⁢ i     whenever ⁢   k ≥ 0 ⁢       and ⁢   i ⁢ — ⁢ j 1 , ... ⁢ , i ⁢   — ⁢ j k , ( 147 )   and ⁢     Pr ( B ¯ 1 ∩ ⋅ ⋅ ⋅ ∩ B ¯ m ) \= 0

Proof. When ( p 1 ,..., p m ) ∈ R ( G ), exercise 344 proves that there’s a unique distribution for events B 1 , ... , B m such that they satisfy ( 147 ) and also

Pr ( ∩ j ∈ J A ¯ j ) ≥ Pr ( ∩ j ∈ J B ¯ j ) \= M G ( p 1 [ 1 ∈ J ] , ... , p m [ m ∈ J ] ) ( 148 )

for every subset J ⊆ {1,..., m }. In this “ extreme” worst-possible distribution, Pr( B i ∩ B j ) = 0 whenever i −−− j in G . Exercise 345 proves the converse.

Given a probability vector ( p 1 ,..., p m ), let

M G *   ( z ) \= M G ( p 1 z , ... , p m z ) . ( 149 )

Theorem F tells us that the coefficient of z n in the power series 1 / M G * ( z ) is the sum of all traces of length n for G . Since this coefficient is nonnegative, we know by Pringsheim’s theorem (see exercise 348 ) that the power series converges for all z < 1 + δ , where 1 + δ is the smallest real root of the polynomial equation M G * ( z ) \= 0 ; this number δ is called the slack of ( p 1 ,..., p m ) with respect to G .

It’s easy to see that ( p 1 ,..., p m ) ∈ R ( G ) if and only if the slack is positive. For if δ ≤ 0, the probabilities ( p 1 ′ , ... , p m ′ ) with p j ′ \= ( 1 + δ ) p j make M G = 0. But if δ > 0, the power series converges when z = 1. And (since it represents the sum of all traces) it also converges to the positive number 1/ M G if any p j is decreased; hence ( p 1 ,..., p m ) lies in R ( G ) by definition. Indeed, this argument shows that, when ( p 1 ,..., p m ) ∈ R ( G ), we can actually increase the probabilities to ((1 + ∊ ) p 1 ,..., (1 + ∊ ) p m ), and they will still lie in R ( G ) whenever ∊ < δ.

Let’s return now to Algorithm M . Suppose the successive bad events A j that step M3 tries to quench are X 1 , X 2 , ... , X N , where N is the total number of times step M3 is performed (possibly N = ∞). To prove that Algorithm M is efficient, we shall show that this random variable N has a small expected value, in the probability space of the independent uniform deviates U that appear in steps M1 and M3. The main idea is that X 1 X 2 ... X N is essentially a trace for the underlying graph; hence we can consider it as an empilement of pieces.

Some simple and concrete examples will help to develop our intuition; we shall consider two case studies. In both cases there are m = 6 events A, B, C, D, E, F , and there are n = 7 variables x 1 ... x 7 . Each variable is a random bit; thus ξ 1 = ‧‧‧ = ξ 7 = 1/2 in the algorithm. Event A depends on x 1 x 2 , while B depends on x 2 x 3 , ... , and F depends on x 6 x 7 . Furthermore, each event occurs with probability 1/4. In Case 1, each event is true when its substring is ‘10’; thus all events are false if and only if x 1 ... x 7 is sorted — that is, x 1 ≤ x 2 ≤ ‧‧‧ ≤ x 7 . In Case 2, each event is true when its substring is ‘11’; thus all events are false if and only if x 1 ... x 7 has no two consecutive 1s.

What happens when we apply Algorithm M to those two cases? One possible scenario is that step M3 is applied N = 8 times, with X 1 X 2 ... X 8 = BCEBAFDC . The actual changes to the bits x 1 ... x 7 might then be

images

(Read x 1 ... x 7 from top to bottom in these diagrams, and scan from left to right. Each module images means “replace the two bad bits at the left by two random bits at the right.” In examples such as this, any valid solution x 1 ... x 7 can be placed at the far right; all values to the left of the modules are then forced.)

Notice that these diagrams are like the empilement ( 136 ), except that they’ve been rotated 90°. We know from ( 136 ) that the same diagram applies to the scenario EFBCDBCA as well as to BCEBAFDC , because they’re the same, as traces. Well ..., not quite! In truth, EFBCDBCA doesn’t give exactly the same result as BCEBAFDC in Algorithm M , if we execute that algorithm as presently written. But the results would be identical if we used separate streams of independent random numbers U k for each variable x k . Thus we can legitimately equate equivalent traces, in the probability space of our random events.

The algorithm runs much faster in practice when it’s applied to Case 1 than when it’s applied to Case 2. How can that be? Both of the diagrams in ( 150 ) occur with the same probability, namely (1/2) 7 (1/4) 8 , as far as the random numbers are concerned. And every diagram for Case 1 has a corresponding diagram for Case 2; so we can’t distinguish the cases by the number of different diagrams. The real difference comes from the fact that, in Case 1, we never have two events to choose from in step M2, unless they are disjoint and can be handled in either order. In Case 2, by contrast, we are deluged at almost every step with events that need to be snuffed out. Therefore the scenario at the right of ( 150 ) is actually quite unlikely; why should the algorithm pick B as the first event to correct, and then C, rather than A? Whatever method is used in step M2, we’ll find that the diagrams for Case 2 will occur less frequently than dictated by the strict probabilities, because of the decreasing likelihood that any particular event will be worked on next, in the presence of competing choices. (See also exercise 353 .)

Worst-case upper bounds on the running time of Algorithm M therefore come from situations like Case 1. In general, the empilement BCEBAFDC in ( 150 ) will occur in a run of Algorithm M with probability at most bcebafdc , if we write ‘ a ’ for the probabilistic upper bound for event A that is denoted by ‘ p i ’ in ( 133 ) when A is a i , and if ‘ b ’, ... , ‘ f ’ are similar for B , ... , F . The reason is that bcebafdc is clearly the probability that those events are produced by the independent random variables x k set by the algorithm, if the layers of the corresponding empilement are defined by dependencies between the variable sets Ξ j . And even if events in the same layer are dependent (by shared variables) yet not lopsidependent (in the sense of exercise 351 ), such events are positively correlated; so the FKG inequality of exercise MPR–61, which holds for the Bernoulli-distributed variables of Algorithm M , shows that bcebafdc is an upper bound. Furthermore the probability that step M2 actually chooses B, C, E, B, A, F, D , and C to work on is at most 1.

Therefore, when ( p 1 ,..., p m ) ∈ R ( G ), Algorithm M ’s running time is maximized when it is applied to events B 1 , ..., B m that have the extreme distribution ( 148 ) of exercise 344 . And we can actually write down the generating function for the running time with respect to those extreme events: We have

Σ N ≥ 0 ⁢   Pr ( Algorithm ⁢   M on B 1 , ... ,   B m ⁢   does ⁢   N ⁢   resamplings )   z N \= M G * ( 1 ) M G * ( z ) , ( 151 )

where M G * ( z ) is defined in ( 149 ), because the coefficient of z N in 1 / M G * ( z ) is the sum of the probabilities of all the traces of length N. Theorem F describes the meaning of 1 / M G * ( 1 ) as a “formal” power series in the variables p i ; we proved it without considering whether or not the infinite sum converges when those variables receive numerical values. But when ( p 1 ,..., p m ) ∈ R ( G ), this series is indeed convergent (it even has a positive “slack”).

This reasoning leads to the following theorem of K. Kolipaka and M. Szegedy [ STOC 43 (2011), 235–243]:

Theorem K. If ( p 1 ,..., p m ) ∈ R ( G ), Algorithm M resamples Ξ j at most

E j \= p j M G \ A j * ( p 1 , ... , p m ) / M G ( p 1 , ... , p m ) ( 152 )

times, on the average. In particular, the expected number of iterations of step M3 is at most E 1 + ‧‧‧ + E m ≤ m / δ , where δ is the slack of ( p 1 ,..., p m ).

Proof. The extreme distribution B 1 , ... , B m maximizes the number of times Ξ j is resampled, and the generating function for that number in the extreme case is

M G ( p 1 , ... , p j − 1 , p j , p j + 1 , ... , p m ) M G ( p 1 , ... , p j − 1 , p j z , p j + 1 , ... , p m ) . ( 153 )

Differentiating with respect to z , then setting z ← 1, gives ( 152 ), because the derivative of the denominator is − p j M G \ A j * ( p 1 , ... , p m ) by ( 141 ).

The stated upper bound on E 1 + ‧‧‧ + E m is proved in exercise 355 .

* Message passing

Physicists who study statistical mechanics have developed a significantly different way to apply randomization to satisfiability problems, based on their experience with the behavior of large systems of interacting particles. From their perspective, a set of Boolean variables whose values are 0 or 1 is best viewed as an ensemble of particles that have positive or negative “spin”; these particles affect each other and change their spins according to local attractions and repulsions, analogous to laws of magnetism. A satisfiability problem can be formulated as a joint probability distribution on spins for which the states of minimum “energy” are achieved precisely when the spins satisfy as many clauses as possible.

In essence, their approach amounts to considering a bipartite structure in which each variable is connected to one or more clauses, and each clause is connected to one or more variables. We can regard both variables and clauses as active agents, who continually tweet to their neighbors in this social network. A variable might inform its clauses that “I think I should probably be true”; but several of those clauses might reply, “I really wish you were false.” By carefully balancing these messages against each other, such local interactions can propagate and build up more and more knowledge of distant connections, often converging to a state where the whole network is reasonably happy.

A particular message-passing strategy called survey propagation [A. Braun-stein, M. Mézard, and R. Zecchina, Random Structures & Algorithms 27 (2005), 201–226] has proved to be astonishingly good at solving random satisfiability problems in the “hard” region just before the threshold of unsatisfiability.

Let C be a clause and let l be one of its literals. A “survey message” η C → l is a fraction between 0 and 1 that represents how urgently C wants l to be true. If η C → l \= 1, the truth of l is desperately needed, lest C be false; but if η C → l \= 0, clause C isn’t the least bit worried about the value of variable | l |. Initially we set each η C → l to a completely random fraction.

We shall consider an extension of the original survey propagation method [see J. Chavas, C. Furtlehner, M. Mézard, and R. Zecchina, J. Statistical Mechanics (November 2005), P11016:1–25; A. Braunstein and R. Zecchina, Physical Review Letters 96 (27 January 2006), 030201:1–4], which introduces additional “ reinforcement messages” η l for each literal l . These new messages, which are initially all zero, represent an external force that acts on l . They help to focus the network activity by reinforcing decisions that have turned out to be fruitful.

Suppose v is a variable that appears in just three clauses: positively in A and B , negatively in C . This variable will respond to its incoming messages η A → v , η B → v , η C → v ¯ , η v and η v ¯ by computing two “ flexibility coefficients,” π v and π v ¯ , using the following formulas:

π v \= ( 1 − η v ) ( 1 − η A → v ) ( 1 − η B → v ) , π v ¯ \= ( 1 − η v ¯ ) ( 1 − η C → v ¯ ) .

If, for instance, η v \= η v ¯ \= 0 while η A → v \= η B → v \= η C → v ¯ \= 2 / 3 , then π v = 1/9, π v ¯ \= 1 / 3 . The π ’s are essentially dual to the η ’s, because high urgency corresponds to low flexibility and vice versa. The general formula for each literal l is

π l \= ( 1 − η l )         Π l ∈ C ⁢       ( 1 − η C → l ) . ( 154 )

Survey propagation uses these coefficients to estimate variable v ’s tendency to be either 1 (true), 0 (false), or ∗ (wild), by computing three numbers

p \= ( 1 − π v ) π v ¯ π v + π v ¯ − π v π v ¯ , q \= ( 1 − π v ¯ ) π v π v + π v ¯ − π v π v ¯ , r \= π v π v ¯ π v + π v ¯ − π v π v ¯ ; ( 155 )

then p + q + r = 1, and ( p , q , r ) is called the “ field” of v , representing respectively (truth, falsity, wildness). The field turns out to be (8/11, 2/11, 1/11) in our example above, indicating that v should probably be assigned the value 1. But if η A → v and η B → v had been only 1/3 instead of 2/3, the field would have been (5/17, 8/17, 4/17), and we would probably want v = 0 in order to satisfy clause C . Figure 94 shows lines of constant p − q as a function of π v and π v ¯ ; the most decisive cases (| p − q | ≈ 1) occur at the lower right and upper left.

images

Fig. 94. Lines of constant bias in a variable’s “field.”

If π v \= π v ¯ \= 0 , there’s no flexibility at all: Variable v is being asked to be both true and false. The field is undefined in such cases, and the survey propagation method hopes that this doesn’t happen.

After each literal l has computed its flexibility, the clauses that involve l or l ¯ can use π l and π l ¯ to refine their survey messages. Suppose, for example, that C is the clause u ∨ v ¯ ∨ w . It will replace the former messages η C → u , η C → v ¯ , η C → w by

η C → u / \= γ v ¯ → C γ w → C , η C → v ¯ ′ \= γ u → C γ w → C , η C → w ′ \= γ u → C γ v ¯ → C ,

where each γ l → C is a “ bias message” received from literal l ,

γ l → C \= ( 1 − π l ¯ ) π l / ( 1 − η C → l ) π l ¯ + ( 1 − π l ¯ )   π l / ( 1 − η C → l ) , ( 156 )

reflecting l ’s propensity to be false in clauses other than C . In general we have

η C → l ′ \= ( Π l ′ ∈ C γ l ′ → C ) / γ l → C . ( 157 )

(Appropriate conventions must be used to avoid division by zero in formulas ( 156 ) and ( 157 ); see exercise 359 .)

New reinforcement messages η l ′ can also be computed periodically, using the formula

η l ′ \= κ ( π l ¯ ∸ π l ) π l + π l ¯ − π l π l ¯ ( 158 )

for each literal l ; here x ∸ y denotes max( x − y , 0), and κ is a reinforcement parameter specified by the algorithm. Notice that η l ¯ ′ > 0 only if η l ¯ ′ \= 0 .

For example, here are messages that might be passed when we want to satisfy the seven clauses of ( 7 ):

l 1 l 2 l 3 η C → l 1 η C → l 2 η C → l 3 γ l 1 → C γ l 2 → C γ l 3 → C l π l η l 1 2 3 ¯ 0 0 0 3 / 5 0 0 1 1 0 1 ¯ 2 ¯ 3 1 / 5 0 0 0 3 / 5 1 / 3 1 ¯ 2 / 5 1 / 2 2 3 4 ¯ 1 / 5 0 0 0 1 / 3 3 / 5 2 2 / 5 1 / 2 2 ¯ 3 ¯ 4 0 0 0 3 / 5 0 0 3 1 0 1 3 4 0 0 1 / 5 3 / 5 1 / 3 0 3 ¯ 2 / 3 1 / 3 1 ¯ 3 ¯ 4 ¯ 0 0 0 0 0 3 / 5 4 2 / 5 1 / 2 1 ¯ 2 4 0 0 0 0 0 0 4 ¯ 1 0 ( 159 )

(Recall that the only solutions to these clauses are 1 ¯ 2 3 4 and 1 ¯ 2 3 ¯ 4.) In this case the reader may verify that the messages of ( 159 ) constitute a “ fixed point”: The η messages determine the π ’s; conversely, we also have η C → l ′ \= η C → l for all clauses C and all literals l , if the reinforcement messages η l remain constant.

Exercise 361 proves that every solution to a satisfiable set of clauses yields a fixed point of the simultaneous equations ( 154 ), ( 156 ), ( 157 ), with the property that η l = [ l is true in the solution].

Experiments with this message-passing strategy have shown, however, that the best results are obtained by using it only for preliminary screening, with the goal of discovering variables whose settings are most critical; we needn’t continue to transmit messages until every clause is fully satisfied. Once we’ve assigned suitable values to the most delicate variables, we’re usually left with a residual problem that can readily be solved by other algorithms such as WalkSAT.

The survey, reinforcement, and bias messages can be exchanged using a wide variety of different protocols. The following procedure incorporates two ideas from an implementation prepared by C . Baldassi in 2012: ( 1 ) The reinforcement strength κ begins at zero, but approaches 1 exponentially. ( 2 ) Variables are rated 1, 0, or ∗ after each reinforcement, according as max( p , q , r ) in their current field is p , q , or r . If every clause then has at least one literal that is true or ∗, message passing will cease even though some surveys might still be fluctuating.

Algorithm S ( Survey propagation ). Given m nonempty clauses on n variables, this algorithm tries to assign values to most of the variables in such a way that the still-unsatisfied clauses will be relatively easy to satisfy. It maintains arrays π l and η l of floating point numbers for each literal l , as well as η C → l for each clause C and each l ∈ C . It has a variety of parameters: ρ (the damping factor for reinforcement), N 0 and N (the minimum and maximum iteration limits), ∊ (the tolerance for convergence), and ψ (the confidence level).

S1. [Initialize.] Set η l ← π l ← 0 for all literals l , and η C → l ← U for all clauses C and l ∈ C , where U is uniformly random in [0 .. 1). Also set i ← 0, ϕ ← 1.

S2. [Done?] Terminate unsuccessfully if i ≥ N . If i is even or i < N 0 , go to S5.

S3. [Reinforce.] Set ϕ ← ρϕ and κ ← 1 − ϕ . Replace η l by η l ′ for all literals l , using ( 158 ); but terminate unsuccessfully if π l \= π l ¯ \= 0 .

S4. [Test pseudo-satisfiability.] Go to S5 if there is at least one clause whose literals l all appear to be false, in the sense that π l ¯ < π l and π l ¯ < 1 2 exercise 358 ). Otherwise go happily to S8.

S5. [Compute the π ’s.] Compute each π l , using ( 154 ); see also exercise 359 .

S6. [Update the surveys.] Set δ ← 0. For all clauses C and literals l ∈ C, compute η C → l ′ using ( 157 ), and set δ ← max ( δ , | η C → l ′ − η C → l | ) , η C → l ← η C → l ′ .

S8. [Loop on i .] If δ ≥ ∊ , set i ← i + 1 and return to S2.

S8. [Reduce the problem.] Assign a value to each variable whose field satisfies | p − q | ≥ ψ . ( Exercise 362 has further details.)

Computational experience — otherwise known as trial and error — suggests suitable parameter values. The defaults ρ = .995, N 0 = 5, N = 1000, ∊ = .01, and ψ = .50 seem to provide a decent starting point for problems of modest size. They worked well, for instance, when the author first tried a random 3SAT problem with 42,000 clauses and 10,000 variables: These clauses were pseudo-satisfiable when i = 143 (although δ ≈ .43 was still rather large); then step S8 fixed the values of 8,282 variables with highly biased fields, and unit propagation gave values to 57 variables more. This process needed only about 218 megamems of calculation. The reduced problem had 1526 2-clauses and 196 3-clauses on 1464 variables (because many other variables were no longer needed); 626 steps of WalkSAT polished it off after an additional 42 kilomems. By contrast, when WalkSAT was presented with the original problem (using p = .57), it needed more than 31 million steps to find a solution after 3.4 gigamems of computation.

Similarly, the author’s first experience applying survey propagation to a random 3SAT problem on n = 10 6 variables with m = 4.2n clauses was a smashing success: More than 800,000 variables were eliminated after only 32.8 gigamems of computation, and WalkSAT solved the residual clauses after 8.5 megamems more. By contrast, pure WalkSAT needed 237 gigamems to perform 2.1 billion steps.

A million-variable problem with 4,250,000 clauses proved to be more challenging. These additional 50,000 clauses put the problem well beyond WalkSAT’s capability; and Algorithm S failed too, with its default parameters. However, the settings ρ = .9999 and N 0 = 9 slowed the reinforcement down satisfactorily, and produced some instructive behavior. Consider the matrix

( 3988 3651 3071 2339 1741 1338 946 702 508 329 5649 5408 4304 3349 2541 2052 1448 1050 666 510 8497 7965 6386 4918 3897 3012 2248 1508 1075 1063 11807 11005 8812 7019 5328 4135 3117 2171 1475 1063 15814 14789 11726 9134 7188 5425 4121 3024 2039 1372 20437 19342 15604 12183 9397 7263 5165 3791 2603 1781 26455 24545 19917 15807 12043 9161 6820 5019 3381 2263 33203 31153 25052 19644 15587 11802 8865 6309 4417 2919 39962 38097 31060 24826 18943 14707 10993 7924 5225 3637 40731 40426 32716 26561 20557 15739 11634 8327 5591 4035 ) ,

which shows the distribution of π v ¯ versus π v (see Fig. 94 ); for example, ‘3988’ at the upper left means that 3988 of the million variables had π v ¯ between 0.0 and 0.1 and π v between 0.9 and 1.0. This distribution, which appeared after δ had been reduced to ≈ 0.0098 by 110 iterations, is terrible — very few variables are biased in a meaningful way. Therefore another run was made with ∊ reduced to .001; but that failed to converge after 1000 iterations. Finally, with ∊ = .001 and N = 2000, pseudo-satisfaction occurred at i = 1373, with the nice distribution

( 406678 1946 1045 979 842 714 687 803 1298 167649 338 2 2 3 0 3 1 4 2 1289 156 1 0 0 0 1 0 2 1 875 118 4 0 0 0 0 0 0 1 743 99 0 0 0 0 0 0 1 0 663 62 0 0 0 0 0 1 0 3 810 41 0 0 0 0 0 0 0 0 1015 55 0 0 0 1 0 1 1 0 1139 63 0 0 1 0 0 0 1 2 1949 116 61 72 41 61 103 120 162 327 406839 )

(although δ was now ≈ 1!). The biases were now pronounced, yet not entirely reliable; the ψ parameter had to be raised, in order to avoid a contradiction when propagating unit literals in the reduced problem. Finally, with ψ = .99, more than 800,000 variables could be set successfully. A solution was obtained after 210 gigamems (including 21 megamems for WalkSAT to finish the job).

Even better results occur when step S8 is allowed to backtrack, resetting less-biased variables when problems arise. See R. Marino, G. Parisi, and F. RicciTersenghi, Nature Communications 7 , 12996 (2016), 1–8.

Success with survey propagation isn’t guaranteed. But hey, when it works, it’s sometimes the only known way to solve a particularly tough problem.

Algorithm S may be viewed as an extension of the “ belief propagation” messages used in the study of Bayesian networks [see J. Pearl, Probabilistic Reasoning in Intelligent Systems (1988), Chapter 4]; it essentially goes beyond Boolean logic on {0, 1} to a three-valued logic on {0, 1, ∗}. Analogous message-passing heuristics had actually been considered much earlier by H. A. Bethe and R. E. Peierls [ Proc. Royal Society of London A150 (1935), 552–575], and independently by R. G. Gallager [ IRE Transactions IT-8 (1962), 21–28]. For further information see M. Mézard and A. Montanari, Information, Physics, and Computation (2009), Chapters 14–22.

* Preprocessing of clauses

A SAT-solving algorithm will often run considerably faster if its input has been transformed into an equivalent but simpler set of clauses. Such transformations and simplifications typically require data structures that would be inappropriate for the main work of a solver, so they are best considered separately.

Of course we can combine a preprocessor and a solver into a single program; and “preprocessing” techniques can be applied again after new clauses have been learned, if we reach a stage where we want to clean up and start afresh. In the latter case the simplifications are called inprocessing . But the basic ideas are most easily explained by assuming that we just want to preprocess a given family of clauses F . Our goal is to produce nicer clauses F′ , which are satisfiable if and only if F is satisfiable.

We shall view preprocessing as a sequence of elementary transformations

F \= F 0 → F 1 → ⋅ ⋅ ⋅ → F r \= F ′ , 160

where each step F j → F j +1 “flows downhill” in the sense that it either (i) eliminates a variable without increasing the number of clauses, or (ii) retains all the variables but decreases the number of literals in clauses. Many different downhill transformations are known; and we can try to apply each of the gimmicks in our repertoire, in some order, until none of them lead to any further progress.

Sometimes we’ll actually solve the given problem, by reaching an F ′ that is either trivially satisfiable (∅) or trivially unsatisfiable (contains ∊ ). But we probably won’t be so lucky unless F was pretty easy to start with, because we’re going to consider only downhill transformations that are quite simple.

Before discussing particular transformations, however, let’s think about the endgame: Suppose F has n variables but F ′ has n ′ < n . After we’ve fed the clauses F′ into a SAT solver and received back a solution, x 1 ′ ... x n ′ ′ , how can we convert it to a full solution x 1 ... x n of the original problem F ? Here’s how: For every transformation F j → F j +1 that eliminates a variable x k , we shall specify an erp rule (so called because it reverses the effect of pre processing). An erp rule for elimination is simply an assignment ‘ l ← E ’, where l is x k or x ¯ k , and E is a Boolean expression that involves only variables that have not been eliminated. We undo the effect of elimination by assigning to x k the value that makes l true if and only if E is true.

For example, suppose two transformations remove x and y with the erp rules

x ¯ ← y ¯ ∨ z , y ← 1.

To reverse these eliminations, right to left, we would set y true, then x ← z ¯ .

As the preprocessor discovers how to eliminate variables, it can immediately write the corresponding erp rules to a file, so that those rules don’t consume memory space. Afterwards, given a reduced solution x 1 ′ ... x n ′ ′ , a postprocessor can read that file in reverse order and provide the unreduced solution x 1 ... x n .

Transformation 1. Unit conditioning

If a unit clause ‘( l )’ is present, we can replace F by F | l and use the erp rule l ← 1. This elementary simplification will be carried out naturally by most solvers; but it is perhaps even more important in a preprocessor, since it often enables further transformations that the solver would not readily see. Conversely, other transformations in the preprocessor might enable unit conditionings that will continue to ripple down.

One consequence of unit conditioning is that all clauses of F ′ will have length two or more, unless F ′ is trivially unsatisfiable.

Transformation 2. Subsumption

If every literal in clause C appears also in another clause C ′, we can remove C ′ . In particular, duplicate clauses will be discarded. No erp rule is needed, because no variable goes away.

Transformation 3. Self-subsumption

If C contains x ¯ and if every other literal in C appears also in another clause C ′, where C ′ contains x , we can delete x from C ′ because C ′ \ x = C ◊ C ′. In other words, the fact that C almost subsumes C ′ allows us at least to strengthen C ′, without actually removing it. Again there’s no erp rule. [Self-subsumption was called “the replacement principle” by J. A. Robinson in JACM 12 (1965), 39.]

Exercise 374 discusses data structures and algorithms by which subsumptions and self-subsumptions can be discovered with reasonable efficiency.

Transformation 4. Downhill resolution

Suppose x appears only in clauses C 1 , ... , C p and x ¯ appears only in C 1 ′ , ... , C q ′ . We’ve observed (see ( 112 )) that variable x can be eliminated if we replace those p + q clauses by the pq clauses { C i ◊ C j ′ | 1 ≤ i ≤ p , 1 ≤ j ≤ q } . The corresponding erp rule (see exercise 367 ) is

either   x ¯ ← Λ i \= 1 p ( C i \ x ) or x ← Λ j \= 1 q ( C j ′ \ x ¯ ) . ( 161 )

Every variable can be eliminated in this way, but we might be flooded with too many clauses. We can prevent this by limiting ourselves to “downhill” cases, in which the new clauses don’t outnumber the old ones. The condition pq ≤ p + q is equivalent to ( p − 1)( q − 1) ≤ 1, as noted above following ( 112 ); the variable is always removed in such cases. But the number of new clauses might be small even when pq is large, because of tautologies or subsumption. Furthermore, N. Eén and A. Biere wrote a fundamental paper on preprocessing [ LNCS 3569 (2005), 61–75], which introduced important special cases that allow many of the pq potential clauses to be omitted; see exercise 369 . Therefore a preprocessor typically tries to elimin ate via resolution whenever min( p , q ) ≤ 10, say, and abandons the attempt only when more than p+q resolvents have been generated.

Many other transformations are possible, although the four listed above have proved to be the most effective in practice. We could, for instance, look for failed literals : If unit propagation leads to a contradiction when we assume that some literal l is true (namely when F ∧ ( l ) ⊢ 1 ∊ ), then we’re allowed to assume that l is false (because the unit clause ( l ¯ ) is certifiable see ( 119 )). This observation and several others related to it were exploited in the lookahead mechanisms of Algorithm Y above. But Algorithm C generally has no trouble finding failed literals all by itself, as a natural byproduct of its mechanism for resolving conflicts. Exercises 378 – 384 discuss other techniques that have been proposed for preprocessing.

Sometimes preprocessing turns out to be dramatically successful. For example, the anti- maximal-element clauses of exercise 228 can be proved unsatisfiable via transformations 1–4 after only about 400 me gamems of work when m = 50. Yet Algorithm C spends 3 gigamems on that untransformed problem when m is only 14; and it needs 11 G μ when m = 15, ... , failing utterly before m = 20.

A more typical example arises in connection with Fig. 78 above: The problem of showing that there’s no 4-step path to images involves 8725 variables, 33769 clauses, and 84041 literals, and Algorithm C requires about 6 gigamems to demonstrate that those clauses are unsatisfiable. Preprocessing needs less than 10 mega mems to reduce that problem to just 3263 variables, 19778 clauses, and 56552 literals; then Algorithm C can handle those with 5 G μ of further work.

On the other hand, preprocessing might take too long, or it might produce clauses that are more difficult to deal with than the originals. It’s totally useless on the waerden or langford problems. (Further examples are discussed below.)

Encoding constraints into clauses

Some problems, like waerden ( j , k ; n ), are inherently Boolean, and they’re essentially given to us as native-born ANDs of ORs. But in most cases we can represent a combinatorial problem via clauses in many different ways, not immediately obvious, and the particular encoding that we choose can have an enormous effect on the speed with which a SAT solver is able to crank out an answer. Thus the art of problem encoding turns out to be just as important as the art of devising algorithms for satisfiability.

Our study of SAT instances has already introduced us to dozens of interesting encodings; and new applications often lead to further ideas, because Boolean algebra is so versatile. Each problem may seem at first to need its own special tricks. But we’ll see that several general principles are available for guidance.

In the first place, different solvers tend to like different encodings: An encoding that’s good for one algorithm might be bad for another.

Consider, for example, the at-most-one constraint, y 1 + ‧‧‧ + y p ≤ 1, which arises in a great many applications. The obvious way to enforce this condition is to assert ( p 2 ) binary clauses ( y ¯ i ∨ y ¯ j ) , for 1 ≤ i < j ≤ p , so that y i = y j = 1 is but those clauses become unwieldy when p is large. The alternative encoding in exercise 12 , due to Marijn Heule, does the same job with only 3 p − 6 binary constraints when p ≥ 3, by introducing a few auxiliary variables a 1 , ... , a ⌊( p −3)/2⌋ . When we formulated Langford’s problem in terms of clauses, via ( 12 ), ( 13 ), and ( 14 ) above, we therefore considered two variants called langford ( n ) and langford′ ( n ), where the former uses the obvious encoding of at-most-one constraints and the latter uses Heule’s method. Furthermore, exercise 7.1.1–55(b) encoded at-most-one constraints in yet another way, having the same number of binary clauses but about twice as many auxiliary variables; let’s give the name langford ″( n ) to the clauses that we get from that scheme.

We weren’t ready to discuss which of the encodings works better in practice, when we introduced langford ( n ) and langford ″ ( n ) above, because we hadn’t yet examined any SAT-solving algorithms. But now we’re ready to reveal the answer; and the answer is: “It depends.” Sometimes langford ′ ( n ) wins over langford ( n ); sometimes it loses. It always seems to beat langford ″( n ). Here, for example, are typical statistics, with runtimes rounded to megamems ( Mμ ) or kilomems ( Kμ ):

variables clauses Algorithm D Algorithm L Algorithm C

langford ( 9 )

104

1722

23 M μ

16 M μ

15 M μ

( UNSAT )

langford ′ ( 9 )

213

801

82 M μ

16 M μ

21 M μ

( UNSAT )

langford ″ ( 9 )

335

801

139 M μ

20 M μ

24 M μ

( UNSAT )

langford ( 13 )

228

5875

71685 M μ

45744 M μ

295571 M μ

( UNSAT )

langford ′ ( 13 )

502

1857

492992 M μ

38589 M μ

677815 M μ

( UNSAT )

langford ″ ( 13 )

795

1857

950719 M μ

46398 M μ

792757 M μ

( UNSAT )

langford ( 16 )

352

11494

5 M μ

52 M μ

301 K μ

( SAT )

langford ′ ( 16 )

796

2928

12 M μ

31 M μ

418 K μ

( SAT )

langford ″ ( 16 )

1264

2928

20 M μ

38 M μ

510 K μ

( SAT )

langford ( 64 )

6016

869650

(huge)

(bigger)

35 M μ

( SAT )

langford ′ ( 64 )

14704

53184

(huger)

(big)

73 M μ

( SAT )

langford ″ ( 64 )

23488

53184

(hugest)

(biggest)

304 M μ

( SAT )

Algorithm D prefers langford to langford ′, because it doesn’t perform unit propagations very efficiently. Algorithm L , which excels at unit propagation, likes langford ′ better. Algorithm C also excels at unit propagation, but it exhibits peculiar behavior: It prefers langford , and on satisfiable instances it zooms in quickly to find a solution; but for some reason it runs very slowly on unsatisfiable instances when n ≥ 10.

Another general principle is that short encodings — encodings with few variables and/or few clauses — are not necessarily better than longer encodings. For example, we often need to use Boolean variables to encode the value of a variable x that actually ranges over d > 2 different values, say 0 ≤ x < d . In such cases it’s natural to use the binary representation x = ( x l −1 ... x 0 ) 2 , where l = ⌈lg d ⌉, and to construct clauses based on the independent bits x j ; but that representation, known as the log encoding , surprisingly turns out to be a bad idea in many cases unless d is large. A direct encoding with d binary variables x 0 , x 1 , ... , x d −1 , where x j = [ x = j ], is often much better. And the order encoding with d − 1 binary variables x 1 , ... , x d −1 , where x j = [ x ≥ j ], is often better yet; this encoding was introduced in 1994 by J. M. Crawford and A. B. Baker [ AAAI Conf. 12 (1994), 1092–1097]. In fact, exercise 408 presents an important applic ation where the order encod ing is the method of choice even when d is 1000 or more! The order encoding is exponentially larger than the log encoding, yet it wins in this application because it allows the SAT solver to deduce consequences rapidly via unit propagation.

Graph coloring problems illustrate this principle nicely. When we tried early in this section to color a graph with d colors, we encoded the color of each vertex with a direct representation, ( 15 ); but we could have used binary notation for those colors. And we could also have used the order encoding, even though the numerical ordering of colors is irrelevant in the problem itself. With a log encoding, exercise 391 exhibits three distinct ways (a, b, c) to enforce the constraint that adjacent vertices have different colors. With the order encoding, exercise 395 explains that it’s easy to handle graph coloring. And there also are four ways to work with the direct encoding, namely (i) to insist on one color per vertex by including the at-most-one exclusion clauses ( 17 ); or (ii) to allow multivalued (multicolored) vertices by omitting those clauses; or (iii) to actually welcome multicolored vertices, by omitting ( 17 ) and forcing each color class to be a kernel, as suggested in answer 14 ; or (iv) to include ( 17 ) but to replace the “ preclusion” clauses ( 16 ) by so-called “ support” clauses as explained in exercise 399 .

These eight options can be compared empirically by trying to arrange 64 colored queens on a chessboard so that no queens of the same color appear in the same row, column, or diagonal. That task is possible with 9 colors, but not with 8. By symmetry we can prespecify the colors of all queens in the top row.

encoding

colors

variables

clauses

Algorithm L

Algorithm C

 

univalued

8

512

7688

3333 M μ

9813 M μ

( UNSAT )

multivalued

8

512

5896

1330 M μ

11997 M μ

( UNSAT )

kernel

8

512

6408

4196 M μ

12601 M μ

( UNSAT )

support

8

512

13512

16796 M μ

20990 M μ

( UNSAT )

log(a)

8

2376

5120

(immense)

20577 M μ

( UNSAT )

log(b)

8

192

5848

(enormous)

15033 M μ

( UNSAT )

log(c)

8

192

5848

(enormous)

15033 M μ

( UNSAT )

order

8

448

6215

43615 M μ

5122 M μ

( UNSAT )

univalued

9

576

8928

2907 M μ

464 M μ

(SAT)

multivalued

9

576

6624

104 M μ

401 M μ

( SAT )

kernel

9

576

7200

93 M μ

87 M μ

( SAT )

support

9

576

15480

2103 M μ

613 M μ

( SAT )

log(a)

9

3168

6776

(gigantic)

1761 M μ

( SAT )

log(b)

9

256

6776

(colossal)

1107 M μ

( SAT )

log(c)

9

256

6584

(mammoth)

555 M μ

( SAT )

order

9

512

7008

(monstrous)

213 M μ

( SAT )

(Each running time shown here is the median of nine runs, made with different random seeds.) It’s clear from this data that the log encodings are completely unsuitable for Algorithm L ; and even the order encoding confuses that algorithm’s heuristics. But Algorithm L shines over Algorithm C with respect to most of the direct encodings. On the other hand, Algorithm C loves the order encoding, especially in the difficult unsatisfiable case.

And that’s not the end of the story. H. Tajima [M.S. thesis, Kobe University (2008)] and N. Tamura noticed that order encoding has another property, which beats all other encodings with respect to graph coloring: Every k - clique of vertices { v 1 ,..., v k } in a graph allows us to append two additional “ hint clauses”

( v ¯ 1 d − k + 1 ∨ ⋅ ⋅ ⋅ ∨ v ¯ k d − k + 1 ) ∧ ( v 1 k − 1 ∨ ⋅ ⋅ ⋅ ∨ v k k − 1 ) ( 162 )

to the clauses for d -coloring — because some vertex of the clique must have a color ≤ d − k , and some vertex must have a color ≥ k − 1. With these additional clauses, the running time to prove unsatisfiability of the 8-coloring problem drops drastically to just 60 M μ with Algorithm L , and to only 13 M μ with Algorithm C . We can even reduce it to just 2 M μ (!) by using that idea twice (see exercise 396 ).

The order encoding has several other nice properties, so it deserves a closer look. When we represent a value x in the range 0 ≤ x < d by the binary variables x j = [ x ≥ j ] for 1 ≤ j < d , we always have

x \= x 1 + x 2 + ⋅ ⋅ ⋅ + x d − 1 ; ( 163 )

hence order encoding is often known as unary representation . The axiom clauses

( x ¯ j + 1 ∨ x j ) for 1 ≤ j < d − 1 ( 164 )

are always included, representing the fact that x ≥ j +1 implies x ≥ j for each j ; these clauses force all the 1s to the left and all the 0s to the right. When d = 2 the unary representation reduces to a one-bit encoding equal to x itself; when d = 3 it’s a two-bit encoding with 00, 10, and 11 representing 0, 1, and 2.

We might not know all of the bits x j of x ’s unary encoding while a problem is in the course of being solved. But if we do know that, say, x 3 = 1 and x 7 = 0, then we know that x belongs to the interval [3 .. 7).

Suppose we know the unary representation of x . Then no calculation is necessary if we want to know the unary representation of y = x + a , when a is a constant, because y j = x j − a . Similarly, z = a − x is equivalent to z j \= x ¯ a + 1 − j ; and w = ⌊ x / a ⌋ is equivalent to w j = x aj . Out-of-bounds superscripts are easy to handle in formulas such as this, because x i = 1 when i ≤ 0 and x i = 0 when i ≥ d . The special case x ¯ \= d − 1 − x is obtained by left-right reflection of x ¯ 1 ... ⁢   x ¯ j − 1 :

( d − 1 − x ) j \= ( x ¯ ) j \= x d − j ¯ . ( 165 )

If we are using the order encoding for two independent variables x and y , with 0 ≤ x , y < d , it’s similarly easy to encode the additional relation x ≤ y + a :

x − y ≤ a ⁢   ⇔     x ≤ y + a ⁢   ⇔     Λ j \= max ( 0 , a + 1 ) min ( d − 1 , d + a ) ( x ¯ j ∨ y j − a ) . ( 166 )

And there are analogous ways to place bounds on the sum, x + y :

x + y ≤ a ⇔ x ≤ y ¯ + a + 1 − d ⇔ Λ j \= max ( 0 , a + 2 − d ) min ( d − 1 , a + 1 ) ( x ¯ j ∨ y ¯ a + 1 − j ) ; ( 167 )

x + y ≥ a ⁢   ⇔     x ¯ ≤ y − a − 1 + d ⁢   ⇔     Λ j \= max ( 1 , a + 1 − d ) min ( d , a ) ( x j ∨ y a + 1 − j ) . ( 168 )

In fact, exercise 405 shows that the general condition ax + by ≤ c can be enforced with at most d binary clauses, when a, b , and c are constant. Any set of such relations, involving at most two variables per constraint, is therefore a 2SAT problem.

Relations between three or more order- encoded variables can also be handled without difficulty, as long as d isn’t too large. For example, conditions such as x + y ≤ z and x + y ≥ z can be expressed with O ( d log d ) clauses of length ≤ 3 (see exercise 407 ). Arbitrary linear inequalities can also be represented, in principle. But of course we shouldn’t expect SAT solvers to compete with algebraic methods on problems that are inherently numerical.

Another constraint of great importance in the encoding of combinatorial problems is the relation of lexicographic order : Given two bit vectors x 1 ... x n and y 1 ... y n , we want to encode the condition ( x 1 ... x n ) 2 ≤ ( y 1 ... y n ) 2 as a conjunction of clauses. Fortunately there’s a nice way to do this with just 3 n − 2 ternary clauses involving n − 1 auxiliary variables a 1 , ... , a n −1 , namely

Λ k \= 1 n − 1 ( ( x ¯ k ∨ y k ∨ a ¯ k − 1 ) ∧ ( x ¯ k ∨ a k ∨ a ¯ k − 1 ) ∧ ( y k ∨ a k ∨ a ¯ k − 1 ) ) ∧ ( x ¯ n ∨ y n ∨ a ¯ n − 1 ) , ( 169 )

where ‘ ā 0 ’ is omitted. For example, the clauses

( x ¯ 1 ∨ y 1 ) ∧ ( x ¯ 1 ∨ a 1 ) ∧ ( y 1 ∨ a 1 ) ∧ ( x ¯ 2 ∨ y 2 ∨ a ¯ 1 ) ∧ ( x ¯ 2 ∨ a 2 ∨ a ¯ 1 ) ∧ ( y 2 ∨ a 2 ∨ a ¯ 1 ) ∧ ( x ¯ 3 ∨ y 3 ∨ a ¯ 2 )

assert that x 1 x 2 x 3 ≤ y 1 y 2 y 3 . And the same formula, but with the final term ( x ¯ n ∨ y n ∨ a ¯ n − 1 ) replaced by ( x ¯ n ∨ a ¯ n − 1 ) ∧ ( y n ∨ a ¯ n − 1 ) , works for the strict comparison x 1 ... x n < y 1 ... y n . These formulas arise by considering the carries that occur when ( x ¯ 1 ⁢   ... ⁢ x ¯ n ) 2 + ( 1 ⁢   or 0 ) is added to ( y 1 ... y n ) 2 . (See exercise 415 .)

The general problem of encoding a constraint on the Boolean variables x 1 , ... , x n is the question of finding a family of clauses F that are satisfiable if and only if f ( x 1 ,..., x n ) is true, where f is a given Boolean function. We usually introduce auxiliary variables a 1 , ... , a m into the clauses of F , unless f can be expressed directly with a short CNF formula; thus the encoding problem is to find a “good” family F such that we have

f ( x 1 , ... , x n ) \= 1 ⁢       ⇔   ∃ a 1 ⁢   ... ⁢   ∃ a m ⁢       Λ C ∈ F C , ( 170 )

where each C is a clause on the variables { a 1 ,..., a m , x 1 ,..., x n }. The variables a 1 , ... , a m can be eliminated by resolution as in ( 112 ), at least in principle, leaving us with a CNF for f — although that CNF might be huge. (See exercise 248 .)

If there’s a simple circuit that computes f , we know from ( 24 ) and exercise 42 that there’s an equally simple “Tseytin encoding” F , with one auxiliary variable for each gate in the circuit. For example, suppose we want to encode the condition x 1 ... x n ≠ y 1 ... y n . The shortest CNF expression for this function f ( x 1 ,..., x n , y 1 ,..., y n ) has 2 n clauses (see exercise 413 ); but there’s a simple circuit ( Boolean chain) with just n + 1 gates:

a 1 ← x 1 ⁢ ⊕ y 1 , … , a n ← x n ⁢ ⊕ y n , f ← a 1 ∨ … ∨ a n .

Using ( 24 ) we get the 4 n clauses

∧ j \= 1 n ( ( x ¯ j ∨ y j ∨ a j ) ∧ ( x j ∨ y ¯ j ∨ a j ) ∧ ( x j ∨ y j ∨ a ¯ j ) ∧ ( x ¯ j ∨ y ¯ j ∨ a ¯ j ) ) , ( 171 )

together with ( a 1 ∨ … ∨ a n ), as a representation of ‘ x 1 ... x n ≠ y 1 ... y n ’.

But this is overkill; D. A. Plaisted and S. Greenbaum have pointed out [ Journal of Symbolic Computation 2 (1986), 293 304] that we can often avoid about half of the clauses in such situations. Indeed, only 2 n of the clauses ( 171 ) are necessary (and sufficient), namely the ones involving a ¯ j :

∧ j \= 1 n ( ( x j ∨ y j ∨ a ¯ j ) ∧ ( x ¯ j ∨ y ¯ j ∨ a ¯ j ) ) . ( 172 )

The other clauses are “ blocked” (see exercise 378 ) and unhelpful. Thus it’s a good idea to examine whether all of the clauses in a Tseytin encoding are really needed. Exercise 416 illustrates another interesting case.

An efficient encoding is possible also when f has a small BDD, and in general whenever f can be computed by a short branching program. Recall the example “ Pi function” introduced in 7.1.1 (22); we observed in 7.1.2 (6) that it can be written ( ( ( x 2 ∧ x ¯ 4 ) ⊕ x ¯ 3 ) ∧ x ¯ 1 ) ⊕ x 2 . Thus it has a twelve-clause Tseytin encoding

( x 2 ∨ a ¯ 1 ) ∧ ( x ¯ 4 ∨ a ¯ 1 ) ∧ ( x ¯ 2 ∨ x 4 ∨ a 1 ) ∧ ( x 3 ∨ a 1 ∨ a 2 ) ∧ ( x ¯ 3 ∨ a ¯ 1 ∨ a 2 ) ∧ ( x ¯ 3 ∨ a 1 ∨ a ¯ 2 ) ∧ ( x 3 ∨ a ¯ 1 ∨ a ¯ 2 ) ∧ ( x ¯ 1 ∨ a ¯ 3 ) ∧ ( a 2 ∨ a ¯ 3 ) ∧ ( x 1 ∨ a ¯ 2 ∨ a 3 ) ∧ ( x 2 ∨ a 3 ) ∧ ( x ¯ 2 ∨ a ¯ 3 ) .

The Pi function also has a short branching program, 7.1.4 (8), namely

I 8 \= ( 1 ¯ ? 7 : 6 ) , I 7 \= ( 2 ¯ ? 5 : 4 ) , I 6 \= ( 2 ¯ ? 0 : 1 ) , I 5 \= ( 3 ¯ ? 1 : 0 ) , I 4 \= ( 3 ¯ ? 3 : 2 ) , I 3 \= ( 4 ¯ ? 1 : 0 ) , I 2 \= ( 4 ¯ ? 1 : 0 ) ,

where the instruction ‘( v ¯ ? l : h )’ means “ If x v \= 0, go to I l , otherwise go to I h ,” except that I 0 and I 1 unconditionally produce the values 0 and 1. We can convert any such branching program into a sequence of clauses, by translating ‘ I j \= ( v ¯ ? l : h ) ’ into

( a ¯ j ∨ x v ∨ a l ) ∧ ( a ¯ j ∨ x ¯ v ∨ a h ) , ( 173 )

where a 0 is omitted, and where any clauses containing a 1 are dropped. We also omit a ¯ t , where I t is the first instruction; in this example t = 8. (These simplifications correspond to asserting the unit clauses ( a ¯ 0 ) ∧ ( a 1 ) ∧ ( a t ) .) The branching program above therefore yields ten clauses,

( x 1 ∨ a 7 ) ∧ ( x ¯ 1 ∨ a 6 ) ∧ ( a ¯ 7 ∨ x 2 ∨ a 5 ) ∧ ( a ¯ 7 ∨ x ¯ 2 ∨ a 4 ) ∧ ( a ¯ 6 ∨ x 2 ) ∧ ( a ¯ 5 ∨ x ¯ 3 ) ∧ ( a ¯ 4 ∨ x 3 ∨ a 3 ) ∧ ( a ¯ 4 ∨ x ¯ 3 ∨ a 2 ) ∧ ( a ¯ 3 ∨ x ¯ 4 ) ∧ ( a ¯ 2 ∨ x 4 ) .

We can readily eliminate a 6 , a 5 , a 3 , a 2 , thereby getting a six-clause equivalent

( x 1 ∨ a 7 ) ∧ ( x ¯ 1 ∨ x 2 ) ∧ ( a ¯ 7 ∨ x 2 ∨ x ¯ 3 ) ∧ ( a ¯ 7 ∨ x ¯ 2 ∨ a 4 ) ∧ ( a ¯ 4 ∨ x 3 ∨ x ¯ 4 ) ∧ ( a ¯ 4 ∨ x ¯ 3 ∨ x 4 ) ;

and a preprocessor will simplify this to the four- clause CNF

( x ¯ 1 ∨ x 2 ) ∧ ( x 2 ∨ x ¯ 3 ) ∧ ( x 1 ∨ x ¯ 2 ∨ x 3 ∨ x ¯ 4 ) ∧ ( x 1 ∨ x ¯ 3 ∨ x 4 ) , ( 174 )

which appeared in exercise 7.1.1 19.

Exercise 417 explains why this translation scheme is valid. The method applies to any branching program whatsoever: The x variables can be tested in any order that is, the v ’s need not be decreasing as in a BDD; moreover, a variable may be tested more than once.

Unit propagation and forcing

The effectiveness of an encoding depends largely on how well that encoding avoids bad partial assignments to the variables. If we’re trying to encode a Boolean condition f ( x 1 , x 2 ,..., x n ), and if the tentative assignments x 1 ← 1 and x 2 ← 0 cause f to be false regardless of the values of x 3 through x n , we’d like the solver to deduce this fact without further ado, ideally by unit propagation once x 1 and x ¯ 2 have been asserted. With a CDCL solver like Algorithm C , a quickly recognized conflict means a relatively short learned clause and that’s a hallmark of progress. Even better would be a situation in which unit propagation, after asserting x 1 , would already force x 2 to be true; and furthermore if unit propagation after x ¯ 2 would also force x ¯ 1 .

Such scenarios aren’t equivalent to each other. For example, consider the clauses F \= ( x ¯ 1 ∨ x 3 ) ∧ ( x ¯ 1 ∨ x 2 ∨ x ¯ 3 ) . Then, using the notation ‘ F ⊦ 1 l ’ to signify that F leads to l via unit propagation, we have F | x 1 ⊦ 1 x 2 , but F | x ¯ 2 ⊬ 1 x ¯ 1 . And with the clauses G \= ( x ¯ 1 ∨ x 2 ∨ x 3 ) ∧ ( x ¯ 1 ∨ x 2 ∨ x ¯ 3 ) we have G | x 1 | x ¯ 2 ⊦ 1 ϵ (see Eq. ( 119 )), but G | x 1 ⊬ 1 x 2 and G | x ¯ 2 ⊦ 1 x ¯ 1 .

Consider now the simple at-most-one constraint on just three variables: f ( x 1 , x 2 , x 3 ) = [ x 1 + x 2 + x 3 ≤ 1]. We can try to represent f by proceeding methodically using the methods suggested above, either by constructing a circuit for f or by constructing f ’s BDD. The first alternative (see exercise 420 ) yields

F \= ( x 1 ∨ x ¯ 2 ∨ a 1 ) ∧ ( x ¯ 1 ∨ x 2 ∨ a 1 ) ∧ ( x 1 ∨ x 2 ∨ a ¯ 1 ) ∧ ( x ¯ 1 ∨ x ¯ 2 ) ∧ ( x ¯ 3 ∨ a ¯ 1 ) ; ( 175 )

the second approach (see exercise 421 ) leads to a somewhat different solution,

G \= ( x 1 ∨ a 4 ) ∧ ( x ¯ 1 ∨ a 3 ) ∧ ( a ¯ 4 ∨ x ¯ 2 ∨ a 2 ) ∧ ( a ¯ 3 ∨ x 2 ∨ a 2 ) ∧ ( a ¯ 3 ∨ x ¯ 2 ) ∧ ( a ¯ 2 ∨ x ¯ 3 ) . ( 176 )

But neither of these encodings is actually very good, because F | x 3 ⊬ 1 x ¯ 1 and G | x 3 ⊬ 1 x ¯ 1 . Much better is the encoding that we get from the general scheme of ( 18 ) and ( 19 ) in the case n = 3, r = 1, namely

S \= ( a ¯ 1 ∨ a 2 ) ∧ ( x ¯ 1 ∨ a 1 ) ∧ ( x ¯ 2 ∨ a 2 ) ∧ ( x ¯ 2 ∨ a ¯ 1 ) ∧ ( x ¯ 3 ∨ a ¯ 2 ) , ( 177 )

where a 1 and a 2 stand for s 1 1 and s 2 1 ; or the one obtained from ( 20 ) and ( 21 ),

B \= ( x ¯ 3 ∨ a 1 ) ∧ ( x ¯ 2 ∨ a 1 ) ∧ ( x ¯ 2 ∨ x ¯ 3 ) ∧ ( a ¯ 1 ∨ x ¯ 1 ) , ( 178 )

where a 1 stands for b 1 2 . With either ( 177 ) or ( 178 ) we have S | x i ⊦ 1 x ¯ j and B | x i ⊦ 1 x ¯ j by unit propagation whenever i ≠ j . And of course the obvious encoding for this particular f is best of all, because n is so small:

O \= ( x ¯ 1 ∨ x ¯ 2 ) ∧ ( x ¯ 1 ∨ x ¯ 3 ) ∧ ( x ¯ 2 ∨ x ¯ 3 ) . ( 179 )

Suppose f ( x 1 ,..., x n ) is a Boolean function that’s represented by a family of clauses F , possibly involving auxiliary variables { a 1 ,..., a m }, as in ( 170 ). We say that F is a forcing representation if we have

F | L ⊦ l implies F | L ⊦ 1 l ( 180 )

whenever L ⋃ l is a set of strictly distinct literals contained in { x 1 ,..., x n , x ¯ 1 , ... x ¯ n }. In other words, if the partial assignment represented by L logically implies the truth of some other literal l , we insist that unit propagation alone should be able to deduce l from F | L . The auxiliary variables { a 1 ,..., a m } are exempt from this requirement; only the potential forcings between primary variables { x 1 ,..., x n } are supposed to be recognized easily when they occur.

( Technical point : If F | L ⊦ ϵ , meaning that F | L is unsatisfiable, we implicitly have F | L ⊦ l for all literals l . In such a case ( 180 ) tells us that F | L ⊦ 1 l and F ⁢   |       L ⊢ 1   l ¯ both hold; hence F | L can then be proved unsatisfiable by unit propagation alone.)

We’ve seen that the clauses S and B in ( 177 ) and ( 178 ) are forcing for the constraint [ x 1 + x 2 + x 3 ≤ 1], but the clauses F and G in ( 175 ) and ( 176 ) are not. In fact, the clauses of ( 18 ) and ( 19 ) that led to ( 177 ) are always forcing, for the general cardinality constraint [ x 1 + … + x n ≤ r ]; and so are the clauses of ( 20 ) and ( 21 ) that led to ( 178 ). (See exercises 429 and 430 .) Moreover, the general at-most-one constraint [ x 1 + … + x n ≤ 1] can be represented more efficiently by Heule’s 3( n −2) binary clauses and ⌊( n − 3)/2⌋ auxiliary variables ( exercise 12 ), or with about n lg n binary clauses and only ⌈lg n ⌉ auxiliary variables ( exercise 394 ); both of those representations are forcing.

In general, we’re glad to know as soon as possible when a variable’s value has been forced by other values, because the variables of a large problem typically participate in many constraints simultaneously. If we know that x can’t be 0 in constraint f , then we can often conclude that some other variable y can’t be 1 in some other constraint g , if x appears in both f and g . There’s lots of feedback.

On the other hand it might be worse to use a large representation F that is forcing than to use a small representation G that isn’t, because additional clauses can make a SAT solver work harder. The tradeoffs are delicate, and they’re difficult to predict in advance.

Every Boolean constraint f ( x 1 ,..., x n ) has at least one forcing representation that involves no auxiliary variables. Indeed, it’s easy to see that the conjunctive prime form F of f the AND of all f ’s prime clauses is forcing.

Smaller representations are also often forcing, even without auxiliaries. For example, the simple constraint [ x 1 ≥ x 2 ≥ … ≥ x n ] has ( n 2 ) prime clauses, namely ( x j ∨ x ¯ k ) for 1 ≤ j < k ≤ n ; but only n − 1 of those clauses, the cases when k = j + 1 as in ( 164 ), are necessary and sufficient for forcing. Exercise 424 presents another, more-or-less random example.

In the worst case, all forcing representations of certain constraints are known to be huge, even when auxiliary variables are introduced (see exercise 428 ). But exercises 431 441 discuss many examples of useful and instructive forcing representations that require relatively few clauses.

We’ve glossed over an interesting technicality in definition ( 180 ), however: A sneaky person might actually construct a representation F that is absolutely useless in practice, even though it meets all of those criteria for forcing. For example, let G ( a 1 ,..., a m ) be a family of clauses that are satisfiable but only when the auxiliary variables a j are set to extremely hard-to-find values. Then we might have f ( x 1 ) = x 1 and F = ( x 1 ) ∧ G ( a 1 ,..., a m )(!). This defect in definition ( 180 ) was first pointed out by M. Gwynne and O. Kullmann [arXiv:1406.7398 [cs.CC] (2014), 67 pages], who have also traced the history of the subject.

To avoid such a glitch, we implicitly assume that F is an honest representation of f , in the following sense: Whenever L is a set of n literals that fully characterizes a solution x 1 ... x n to the constraint f ( x 1 ,..., x n ) = 1, the clauses F | L must be easy to satisfy , using the SLUR algorithm of exercise 444 . That algorithm is efficient because it does not backtrack. All of the examples in exercises 439 444 meet this test of honesty; indeed, the test is automatically passed whenever every clause of F contains at most one negated auxiliary variable.

Some authors have suggested that a SAT solver should branch only on primary variables x i , rather than on auxiliary variables a j , whenever possible. But an extensive study by M. Järvisalo and I. Niemelä [ LNCS 4741 (2007), 348 363; J. Algorithms 63 (2008), 90 113] has shown that such a restriction is not advisable with Algorithm C , and it might lead to a severe slowdown.

Symmetry breaking

Sometimes we can achieve enormous speedup by exploiting symmetries. Consider, for example, the clauses for placing m + 1 pigeons into m holes, ( 106 ) ( 107 ). We’ve seen in Lemma B and Theorem B that Algorithm C and other resolution-related methods cannot demonstrate the unsatisfiability of those clauses without performing exponentially many steps as m grows. However, the clauses are symmetrical with respect to pigeons; independently, they’re also symmetrical with respect to holes: If π is any permutation of {0, 1,..., m } and if ρ is any permutation of {1, 2,..., m }, the transformation x jk ↦ x ( jπ )( kρ ) for 0 ≤ j ≤ m and 1 ≤ k ≤ m leaves the set of clauses ( 106 ) ( 107 ) unchanged. Thus the pigeonhole problem has ( m + 1)! m ! symmetries.

We’ll prove below that the symmetries on the holes allow us to assume safely that the hole-occupancy vectors are lexicographically ordered, namely that

x 0 k x 1 k … x m k ≤ x 0 ( k + 1 ) x 1 ( k + 1 ) ⁢ … x m ( k + 1 ) , for 1 ≤ k < m . ( 181 )

These constraints preserve satisfiability; and we know from ( 169 ) that they are readily expressed as clauses. Without the help of such additional clauses the running time of Algorithm C rises from 19 megamems for m = 7 to 177 M μ for m = 8, and then to 3.5 gigamems and 86 G μ for m = 9 and 10. But with ( 181 ), the same algorithm shows unsatisfiability for m = 10 after only 1 megamem; and for m = 20 and m = 30 after only 284 M μ and 3.6 G μ , respectively.

Even better results occur when we order the pigeon -occupancy vectors:

x j 1 x j 2 … x j m ≤ x ( j + 1 ) 1 x ( j + 1 ) 2 ⁢ … x ( j + 1 ) m , for 0 ≤ j < m . ( 182 )

With these constraints added to ( 106 ) and ( 107 ), Algorithm C polishes off the case m = 10 in just 69 kilo mems. It can even handle m = 100 in 133 M μ . This remarkable improvement was achieved by adding only m 2 − m new variables and 3 m 2 − 2 m new clauses to the original m 2 + m variables and ( m + 1) + ( m 3 + m 2 )/2 clauses of ( 106 ) and ( 107 ). (Moreover, the reasoning that justifies ( 182 ) doesn’t “cheat” by invoking the mathematical pigeonhole principle behind the scenes.)

Actually that’s not all. The theory of columnwise symmetry (see exercise 498 ) also tells us that we’re allowed to add the ( m 2 ) simple binary clauses

( x ( j − 1 ) j ∨ x ¯ ( j − 1 ) k ) for 1 ≤ j < k ≤ m ( 183 )

to ( 106 ) and ( 107 ), instead of ( 182 ). This principle is rather weak in general; but it turns out to be ideally suited to pigeons: It reduces the running time for m = 100 to just 21 megamems, although it needs no auxiliary variables whatsoever!

Of course the status of ( 106 ) ( 107 ) has never been in doubt. Those clauses serve merely as training wheels because of their simplicity; they illustrate the fact that many symmetry-breaking strategies exist. Let’s turn now to a more interesting problem, which has essentially the same symmetries, but with the roles of pigeons and holes played by “ points” and “ lines” instead. Consider a set of m points and n lines, where each line is a subset of points; we will require that no two points appear together in more than one line. (Equivalently, no two lines may intersect in more than one point.) Such a configuration may be called quad-free , because it is equivalent to an m × n binary matrix ( x ij ) that contains no “quad,” namely no 2 × 2 submatrix of 1s; element x ij means that point i belongs to line j . Quad-free matrices are obviously characterized by ( m 2 ) ( n 2 ) clauses,

( x ¯ i j ∨ x ¯ i j ′ ∨ x ¯ i ′ j ∨ x ¯ i ′ j ′ ) , for 1 ≤ i < i ′ ≤ m ⁢   and 1 ≤ j < j ′ ≤ n . ( 184 )

What is the maximum number of 1s in an m × n quad-free matrix? [This question, when m = n , was posed by K. Zarankiewicz, Colloquium Mathematicæ 2 (1951), 301, who also considered how to avoid more general submatrices of 1s.] Let’s call that value Z ( m , n ) − 1; then Z ( m , n ) is the smallest r such that every m × n matrix with r nonzero entries contains a quad.

We’ve actually encountered examples of this problem before, but in a disguised form. For example (see exercise 448 ), a Steiner triple system on v objects exists if and only if v is odd and there is a quad-free matrix with m = v , n = v ( v − 1)/6, and r = v ( v − 1)/2. Other combinatorial block designs have similar characterizations.

Table 5 shows the values of Z ( m , n ) for small cases. These values were discovered by delicate combinatorial reasoning, without computer assistance; so it’s instructive to see how well a SAT solver can compete against real intelligence.

The first interesting case occurs when m = n = 8: One can place 24 markers on a chessboard without forming a quad, but Z (8, 8) = 25 markers is too many. If we simply add the cardinality constraints Σ i \= 1 m Σ j \= 1 n x i j ≥ r to ( 184 ), Algorithm C will quickly find a solution when m = n = 8 and r = 24. But it bogs down when r = 25, requiring about 10 teramems to show unsatisfiability.

Fortunately we can take advantage of m ! n ! symmetries, which permute rows and columns without affecting quads. Exercise 495 shows that those symmetries allow us to add the lexicographic constraints

Table 5
Z ( m, n ), THE MINIMUM NUMBER OF 1S WITH ( 184 ) UNSATISFIABLE

n = 2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

m = 2:

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

m = 3:

5

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

m = 4:

6

8

10

11

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

m = 5:

7

9

11

13

15

16

18

19

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

m = 6:

8

10

13

15

17

19

20

22

23

25

26

28

29

31

32

33

34

35

36

37

38

39

40

41

42

43

m = 7:

9

11

14

16

19

22

23

25

26

28

29

31

32

34

35

37

38

40

41

43

44

45

46

47

48

49

m = 8:

10

12

15

18

20

23

25

27

29

31

33

34

36

37

39

40

42

43

45

46

48

49

51

52

54

55

m = 9:

11

13

16

19

22

25

27

30

32

34

37

38

40

41

43

44

46

47

49

50

52

53

55

56

58

59

m = 10:

12

14

17

21

23

26

29

32

35

37

40

41

43

45

47

48

50

52

53

55

56

58

59

61

62

64

m = 11:

13

15

18

22

25

28

31

34

37

40

43

45

46

48

51

52

54

56

58

60

61

63

64

66

67

69

m = 12:

14

16

19

23

26

29

33

37

40

43

46

49

50

52

54

56

58

61

62

64

66

67

69

71

73

74

m = 13:

15

17

20

24

28

31

34

38

41

45

49

53

54

56

58

60

62

65

67

68

70

72

74

76

79

80

m = 14:

16

18

21

25

29

32

36

40

43

46

50

54

57

59

61

64

66

69

71

73

74

76

79

81

83

85

m = 15:

17

19

22

26

31

34

37

41

45

48

52

56

59

62

65

68

70

73

76

78

79

81

83

86

87

89

m = 16:

18

20

23

27

32

35

39

43

47

51

54

58

61

65

68

71

74

77

81

82

84

86

88

91

92

94

[ References : R. K. Guy, in Theory of Graphs , Tihany 1966, edited by Erdős and Katona (Academic Press, 1968), 119 150; R. J. Nowakowski, Ph.D. thesis (Univ. of Calgary, 1978), 202.]

x i 1 x i 2 ⋅ ⋅ ⋅ x i n ≥ x ( i + 1 ) 1 x ( i + 1 ) 2 ⋅ ⋅ ⋅ x ( i + 1 ) n , for 1 ≤ i < m ; ( 185 )

x 1 j x 2 j ⋅ ⋅ ⋅ x m j ≥ x 1 ( j + 1 ) x 2 ( j + 1 ) ⋅ ⋅ ⋅ x m ( j + 1 ) , for 1 ≤ j < n . ( 186 )

(Increasing order, with ‘≤’ in place of ‘≥’, could also have been used, but decreasing order turns out to be better; see exercise 497 .) The running time to prove un satisfiability when r = 25 now decreases dramatically, to only about 50 megamems. And it falls to 48 M μ if the lexicographic constraints are shortened to consider only the leading 4 elements of a row or column, instead of testing all 8.

The constraints of ( 185 ) and ( 186 ) are useful in satisfiable problems too not in the easy case m = n = 8, when they aren’t necessary, but for example in the case m = n = 13 when r = 52: Then they lead Algorithm C to a solution after about 200 gigamems, while it needs more than 18 teramems to find a solution without such help. (See exercise 449 .)

Satisfiability-preserving maps

Let’s proceed now to the promised theory of symmetry breaking. In fact, we will do more: Symmetry is about permutations that preserve structural properties, but we will consider arbitrary mappings instead. Mappings are more general than permutations, because they needn’t be invertible. If x = x 1 ... x n is any potential solution to a satisfiability problem, our theory is based on transformations τ that map x ↦ x τ \= x 1 ′ … x n ′ , where xτ is required to be a solution whenever x is a solution.

In other words, if F is a family of clauses on n variables and if f ( x ) = [ x satisfies F ], then we are interested in all mappings τ for which f ( x ) ≤ f ( xτ ). Such a mapping is conventionally called an endomorphism of the solutions. * If an endomorphism τ is actually a permutation, it’s called an automorphism . Thus, if there are K solutions to the problem, out of N = 2 n possibilities, the total number of mappings is N N ; the total number of endomorphisms is K K N N − K ; and the total number of automorphisms is K !( N − K )!.

* This word is a bit of a mouthful. But it’s easier to say “endomorphism” than to say “satisfiability-preserving transformation,” and you can use it to impress your friends. The term “conditional symmetry” has also been used by several authors in special cases.

Notice that we don’t require f ( x ) to be exactly equal to f ( xτ ). An endomorphism is allowed to map a nonsolution into a solution, and only K K ( N − K ) N − K mappings satisfy that stronger property. On the other hand, automorphisms always do satisfy f ( x )= f ( xτ ); see exercise 454 .

Here, for instance, is a more-or-less random mapping when n = 4:

images

Exercises 455 and 456 discuss potential endomorphisms of this mapping.

In general there will be one or more cycles , and every element of a cycle is the root of an oriented tree that leads to it. For example, the cycles of ( 187 ) are (0011), (1010 0101 0110), and (1000).

Several different endomorphisms τ 1 , τ 2 , ... , τ p are often known. In such cases it’s helpful to imagine the digraph with 2 n vertices that has arcs from each vertex x to its successors xτ 1 , xτ 2 , ... , xτ p . This digraph will have one or more sink components , which are strongly connected components Y from which there is no escape: If x ∈ Y then xτ k ∈ Y for 1 ≤ k ≤ p . (In the special case where each τ k is an automorphism, the sink components are traditionally called orbits of the automorphism group.) When p = 1, a sink component is the same as a cycle.

The clauses F are satisfiable if and only if f ( x ) = 1 for at least one x . Such an x will lead to at least one sink component Y , all of whose elements will satisfy f ( y ) = 1. Thus it suffices to test satisfiability by checking just one element y in every sink component Y , to see if f ( y ) = 1.

Let’s consider a simple problem based on the “ sweep” of an m × n matrix X = ( x ij ), which is the largest diagonal sum of any t × t submatrix:

sweep ( X ) \= max 1 ≤ i 1 < i 2 < … < i t ≤ m 1 ≤ j 1 < j 2 < … < j t ≤ n ( x i 1 j 1 + x i 2 j 2 + … + x i t j t ) . ( 188 )

When X is binary, sweep( X ) is the length of the longest downward-and-rightward path that passes through its 1s. We can use satisfiability to decide whether such a matrix exists having sweep( X ) ≤ k and Σ i \= 1 m Σ j \= 1 n x i j ≥ r , given m , n , k , and r ; suitable clauses are exhibited in exercise 460 . A solution with m = n = 10, k = 3, and r = 51 appears at the right: It has 51 1s, but no four of them lie in a monotonic southeasterly path.

0000111111
0000100011
0000100111
0001101101
0111111001
1111100001
1010000011
1010000010
1110111110
1111100000

This problem has 2 mn candidate matrices X , and experiments with small m and n suggest several endomorphisms that can be applied to such candidates without increasing the sweep.

  • τ 1 : If x ij \= 1 and x i ( j + 1) \= 0, and if x _i ′j \= 0 for 1 ≤ _i ′ < i , we can set x ij_ ← 0 and _x i ( j +1) ← 1.

  • τ 2 : If x ij \= 1 and x ( i +1) j \= 0, and if x ij′ \= 0 for 1 ≤ j ′ < j , we can set x ij ← 0 and x ( i +1) j ← 1.

  • τ 3 : If the 2 × 2 submatrix in rows { i , i + 1} and columns { j , j + 1} is 11 10 , we can change it to 01 11 .

These transformations are justified in exercise 462 . They’re sometimes applicable for several different i and j ; for instance, τ 3 could be used to change any of eight different 2×2 sub matrices in the example solution. In such cases we make an arbitrary decision, by choosing (say) the lexicographically smallest possible i and j .

The clauses that encode this problem have auxiliary variables besides x ij ; but we can ignore the auxiliary variables when reasoning about endomorphisms.

Each of these endomorphisms either leaves X unchanged or replaces it by a lexicographically smaller matrix. Therefore the sink components of { τ 1 , τ 2 , τ 3 } consist of the matrices X that are fixed points of all three transformations . Hence we’re allowed to append additional clauses, stating that neither τ 1 nor τ 2 nor τ 3 is applicable. For instance, transformation τ 3 is ruled out by the clauses

∧ i \= 1 m − 1 ∧ j \= 1 n − 1 ( x ¯ i j ∨ x ¯ j ( j + 1 ) ∨ x ( i + 1 ) ( j + 1 ) , ( 189 )

which state that the submatrix 11 10 doesn’t appear. The clauses for τ 1 and τ 2 are only a bit more complicated (see exercise 461 ).

These additional clauses give interesting answers in satisfiable instances, although they aren’t really helpful running-time-wise. On the other hand, they’re spectacularly successful when the problem is un satisfiable.

For example, we can show, without endomorphisms, that the case m = n = 10, k = 3, r = 52 is impossible, and hence that any solution for r = 51 is optimum; Algorithm C proves this after about 16 gigamems of work. Adding the clauses for τ 1 and τ 2 , but not τ 3 , increases the running time to 23 G μ ; on the other hand the clauses for τ 3 without τ 1 or τ 2 reduce it to 6 G μ . When we use all three endomorphisms simultaneously, however, the running time to prove unsatisfiability goes down to just 3.5 mega mems, a speedup of more than 4500.

Even better is the fact that the fixed points of { τ 1 , τ 2 , τ 3 } actually have an extremely simple form — see exercise 463 — from which we can readily determine the answer by hand, without running the machine at all! Computer experiments have helped us to guess this result; but once we’ve proved it, we’ve solved infinitely many cases in one fell swoop. Theory and practice are synergistic.

Another interesting example arises when we want to test whether or not a given graph has a perfect matching , which is a set of nonoverlapping edges that exactly touch each vertex. We’ll discuss beautiful, efficient algorithms for this problem in Sections 7.5.1 and 7.5.5; but it’s interesting to see how well a simple-minded SAT solver can compete with those methods.

Perfect matching is readily expressible as a SAT problem whose variables are called ‘ uv ’, one for each edge u — v . Variables ‘ uv ’ and ‘ vu ’ are identical. Whenever the graph contains a 4-cycle v 0 — v 1 — v 2 — v 3 — v 0 , we might include two of its edges { v 0 v 1 , v 2 v 3 } in the matching; but we could equally well have included { v 1 v 2 , v 3 v 0 } instead. Thus there’s an endomorphism that says, “If v 0 v 1 = v 2 v 3 = 1 (hence v 1 v 2 = v 3 v 0 = 0), set v 0 v 1 ← v 2 v 3 ← 0 and v 1 v 2 ← v 3 v 0 ← 1.”

And we can carry this idea further: Let the edges be totally ordered in some arbitrary fashion, and for each edge uv consider all 4-cycles in which uv is the largest edge. In other words, we consider all cycles of the form u — v — u ′ — v ′ — u in which vu ′, u ′ v ′, v ′ u all precede uv in the ordering. If any such cycles exist, choose one of them arbitrarily, and let τ uv be one of two endomorphisms:

τ u v − : " If u v \= u ′ v ′ \= 1 , set     u v ← u ′ v ′ ← 0 and   v u ′ ← v ′ u ← 1. " τ u v + : " If v u ′ \= v ′ u \= 1 , set     u v ← u ′ v ′ ← 1 and   v u ′ ← v ′ u ← 0. "

Either τ u v − or τ u v + is stipulated, for each uv . Exercise 465 proves that a perfect matching is in the sink component of any such family of endomorphisms if and only if it is fixed by all of them. Therefore we need only search for fixed points.

For example, consider the problem of covering an m × n board with dominoes. This is the problem of finding a perfect matching on the grid graph P m ⎕ P n . The graph has mn vertices ( i , j ), with m ( n − 1) “horizontal” edges h ij from ( i , j ) to ( i , j + 1) and ( m − 1) n “vertical” edges v ij from ( i , j ) to ( i + 1, j ). It has exactly ( m − 1)( n − 1) 4-cycles; and if we number the edges from left to right, no two 4-cycles have the same largest edge. Therefore we can construct ( m − 1)( n − 1) endomorphisms, in each of which we’re free to decide whether to allow a particular cycle to be filled by two horizontal dominoes or by two vertical ones.

Let’s stipulate that h ij and h ( i +1) j are allowed together only when i + j is odd; v ij and v i ( j +1) are allowed together only when i + j is even. The nine endomorphisms when m = n = 4 are then

images

And it’s not difficult to see that only one 4 × 4 domino covering is fixed by all nine. Indeed ( exercise 466 ), the solution turns out to be unique for all m and n .

The famous problem of the “ mutilated chessboard” asks for a domino covering when two opposite corner cells have been removed. This problem is unsatisfiable when m and n are both even, by exercise 7.1.4 213. But a SAT solver can’t discover this fact quickly from the clauses alone, because there are many ways to get quite close to a solution; see the discussion following 7.1.4 (130). [S. Dantchev and S. Riis, in FOCS 42 (2001), 220 229, have proved in fact that every resolution refutation of these clauses requires 2 Ω( n ) steps.]

When Algorithm C is presented with mutilated boards of sizes 6 × 6, 8 × 8, 10 × 10, ... , 16 × 16, it needs respectively about 55 K μ , 1.4M μ , 31M μ , 668 M μ , 16.5G μ , and .91 T μ (that’s tera mems) to prove unsatisfiability. The even-odd endomorphisms typified by ( 190 ) come to our rescue, however: They narrow the search space spectacularly, reducing the respective running times to only 15 K μ , 60 K μ , 135 K μ , 250 K μ , 470 K μ , 690 K μ (that’s kilo mems). They even can verify the unsatisfiability of a mutilated 256×256 domino cover after fewer than 4.2G μ of calculation, exhibiting a growth rate of roughly O ( n 3 ).

Endomorphisms can also speed up SAT solving in another important way:

Theorem E. Let p 1 p 2 ... p n be any permutation of {1, 2,..., n }. If the Boolean function f ( x 1 , x 2 ,..., x n ) is satisfiable, then it has a solution such that x p 1 x p 2 ... x p n is lexicographically less than or equal to x p 1 ′ x p 2 ′ … x p n ′ for every endomorphism of f that takes x 1 x 2 … x n ↦ x 1 ′ x 2 ′ … x n ′ .

Proof . The lexicographically smallest solution of f has this property.

Maybe we shouldn’t call this a “theorem”; it’s an obvious consequence of the fact that endomorphisms always map solutions into solutions. But it deserves to be remembered and placed on some sort of pedestal, because we will see that it has many useful applications.

Theorem E is extremely good news, at least potentially, because every Boolean function has a huge number of endomorphisms. (See exercise 457 .) On the other hand, there’s a catch: We almost never know any of those endomorphisms until after we’ve solved the problem! Still, whenever we do happen to know one of the zillions of nontrivial endomorphisms that exist, we’re allowed to add clauses that narrow the search. There’s always a “lex-leader” solution that satisfies x 1 x 2 … x n ≤ x 1 ′ x 2 ′ … x n ′ , if there’s any solution at all.

A second difficulty that takes some of the shine away from Theorem E is the fact that most endomorphisms are too complicated to express neatly as clauses. What we really want is an endomorphism that’s nice and simple, so that lexicographic ordering is equally simple.

Fortunately, such endomorphisms are often available; in fact, they’re usually automorphisms —symmetries of the problem—defined by signed permutations of the variables. A signed permutation represents the operation of permuting variables and/or complementing them; for example, the signed permutation ‘ 4 ¯ 13 2 ¯ ’ stands for the mapping ( x 1 , x 2 , x 3 , x 4 ) ↦ ( x 4 ¯ , x 1 , x 3 , x 2 ¯ ) \= ( x ¯ 4 , x 1 , x 3 , x ¯ 2 ) . This operation transforms the states in a much more regular way than ( 187 ):

images

If σ takes the literal u into v , we write uσ = v ; and in such cases σ also takes u ¯ into v ¯ . Thus we always have u ¯ σ \= u σ ¯ . We also write xσ for the result of applying σ to a sequence x of literals; for example, ( x 1 , x 2 , x 3 , x 4 ) σ \= ( x ¯ 4 , x 1 , x 3 , x ¯ 2 ) . This mapping is a symmetry or automorphism of f ( x ) if and only if f ( x ) = f ( xσ ) for all x . Exercises 474 and 475 discuss basic properties of such symmetries; see also exercise 7.2.1.2 20.

Notice that a signed permutation can be regarded as an unsigned permutation of the 2 n literals { x 1 , … , x n , x ¯ 1 , … , x ¯ n } , and as such it can be written as a product of cycles. For instance, the symmetry 4 ¯ 13 2 ¯ corresponds to the cycles ( 1 4 ¯ 2 ) ( 1 ¯ 4 2 ¯ ) ( 3 ) ( 3 ¯ ) . We can multiply signed permutations by multiplying these cycles in the normal way, just as in Section 1.3.3.

The product στ of two symmetries σ and τ is always a symmetry. Thus in particular, if σ is any symmetry, so are its powers σ 2 , σ 3 , etc. We say that σ has order r if σ , σ 2 , ... , σ r are distinct and σ r is the identity. A signed permutation of order 1 or 2 is called a signed involution ; this important special case arises if and only if σ is its own inverse ( σ 2 = 1).

It’s clearly easier to work with permutations of 2 n literals than to work with permutations of 2 n states x 1 ... x n . The main advantage of a signed permutation σ is that we can test whether or not σ preserves the family F of clauses in a satisfiability problem. If it does, we can be sure that σ also is an automorphism when it acts on all 2 n states. (See exercise 492 .)

Let’s go back to the example waerden (3, 10; 97) that we’ve often discussed above. These clauses have an obvious symmetry, which takes x 1 x 2 ... x 97 ↦ x 97 x 96 ... x 1 . If we don’t break this symmetry, Algorithm C typically verifies unsatisfiability after about 530 M μ of computation. Now Theorem E tells us that we can also assert that x 1 x 2 x 3 ≤ x 97 x 96 x 95 , say; but that symmetry-breaker doesn’t really help at all, because x 1 has very little influence on x 97 . Fortunately, however, Theorem E allows us to choose any permutation p 1 p 2 ... p n on which to base lexicographic comparisons. For example, we can assert that x 48 x 47 x 46 ... ≤ x 50 x 51 x 52 ... provided that we don’t also require x 1 x 2 x 3 ... ≤ x 97 x 96 x 95 ... . (One fixed global ordering must be used, but the endomorphs can be arbitrary.)

Even the simple assertion that x 48 ≤ x 50 , which is the clause ‘ 48 ¯ ⁢ 50 ’, cuts the running time down to about 410 M μ , because this new clause combines nicely with the existing clauses 46 48 50, 48 49 50, 48 50 52 to yield the helpful binary clauses 46 50, 49 50, 50 52. If we go further and assert that x 48 x 47 ≤ x 50 x 51 , the running time improves to 345 M μ . And the next steps x 48 x 47 x 46 ≤ x 50 x 51 x 52 , ... , x 48 x 47 x 46 x 45 x 44 x 43 ≤ x 50 x 51 x 52 x 53 x 54 x 55 take us down to 290 M μ , then 260 M μ , 235 M μ , 220 M μ ; we’ve saved more than half of the running time by exploiting a single reflection symmetry! Only 16 simple additional clauses, namely

48 ¯ ⁢ 50 , 48 ¯ a 1 , 50 a 1 , 47 ¯ 51 a ¯ 1 , 47 ¯ a 2 a ¯ 1 , 51 a 2 a ¯ 1 , 46 ¯ 52 a ¯ 2 , … , 43 ¯ 55 a ¯ 5 ,

are needed to get this speedup, using the efficient encoding of lex order in ( 169 ).

Of course all good things come to an end, and we’ve now reached the point of diminishing returns: Further clauses to assert that x 48 x 47 ... x 42 ≤ x 50 x 51 ... x 56 in the waerden (3, 10; 97) problem turn out to be counterproductive.

A wonderful simplification occurs when a symmetry σ is a signed involution that has comparatively few 2- cycles. Suppose, for example, that σ \= 5 3 ¯ 2 ¯ 41 6 ¯ 9 ¯ 8 ¯ 7 ¯ ; in cycle form this is ( 15 ) ( 1 ¯ 5 ¯ ) ( 2 3 ¯ ) ( 2 ¯ 3 ) ( 4 ) ( 4 ¯ ) ( 6 6 ¯ ) ( 7 9 ¯ ) ( 7 ¯ 9 ) ( 8 8 ¯ ) . Then the lexicographic relation x \= x 1 … x 9 ≤ x 1 ′ … x 9 ′ \= x σ holds if and only if x 1 x 2 x 6 ≤ x 5 x ¯ 3 x ¯ 6 . The reason is clear, once we look closer (see F. A. Aloul, A. Ramani, I. L. Markov, and K. A. Sakallah, IEEE Trans. CAD-22 (2003), 1117 1137, §III.C): The relation x 1 … x 9 ≤ x 1 ′ … x 9 ′ means, in this case, “ x 1 ≤ x 5 ; if x 1 = x 5 then x 2 ≤ x ¯ 3 ; if x 1 = x 5 and x 2 \= x ¯ 3 then x 3 ≤ x ¯ 2 ; if x 1 = x 5 , x 2 \= x ¯ 3 , and x 3 \= x ¯ 2 then x 4 ≤ x 4 ; if x 1 = x 5 , x 2 \= x ¯ 3 , x 3 \= x ¯ 2 , and x 4 = x 4 then x 5 ≤ x 1 ; if x 1 = x 5 , x 2 \= x ¯ 3 , x 3 \= x ¯ 2 , x 4 = x 4 , and x 5 = x 1 then x 6 ≤ x ¯ 6 ; if x 1 = x 5 , x 2 \= x ¯ 3 , x 3 \= x ¯ 2 , x 4 = x 4 , x 5 = x 1 , and x 6 \= x ¯ 6 then we’re done for.” With this expanded description the simplifications are obvious.

In general this reasoning allows us to improve Theorem E as follows:

Corollary E. Let p 1 p 2 ... p n be any permutation of {1, 2,..., n }. For every signed involution σ that is a symmetry of clauses F, we can write σ in cycle form

( p i 1 ± p j 1 ) ( p ¯ i 1 ∓ p j 1 ) ( p i 2 ± p j 2 ) ( p ¯ i 2 ∓ p j 2 ) … ( p i t ± p j t ) ( p ¯ i t ∓ p j t ) ( 192 )

with i 1 ≤ j 1 , i 2 ≤ j 2 , ... , i t ≤ j t , i 1 < i 2 < … < i t , and with ( p ¯ i k ∓ p j k ) omitted when i k \= j k ; and we’re allowed to append clauses to F that assert the lexicographic relation x p i 1 x p i 2 ... x p i q ≤ x ± p j 1 x ± p j 2 ... x ± p j q , where q = t or q is the smallest k with i k \= j k .

In the common case when σ is an ordinary signless involution, all of the signs can be eliminated here; we simply assert that x p i 1 ... x p i t ≤ x p j 1 ... x p j t .

This involution principle justifies all of the symmetry-breaking techniques that we used above in the pigeonhole and quad-free matrix problems. See, for example, the details discussed in exercise 495 .

The idea of breaking symmetry by appending clauses was pioneered by J.-F. Puget [ LNCS 689 (1993), 350–361], then by J. Crawford, M. Ginsberg, E. Luks, and A. Roy [ Int. Conf. Knowledge Representation and Reasoning 5 (1996), 148–159], who considered unsigned permutations only. They also attempted to discover symmetries algorithmically from the clauses that were given as input. Experience has shown, however, that useful symmetries can almost always be better supplied by a person who understands the structure of the underlying problem.

Indeed, symmetries are often “semantic” rather than “syntactic.” That is, they are symmetries of the underlying Boolean function, but not of the clauses themselves. In the Zarankiewicz problem about quad-free matrices, for example, we appended efficient cardinality clauses to ensure that ∑ x ij ≥ r ; that condition is symmetric under row and column swaps, but the clauses are not.

In this connection it may also be helpful to mention the monkey wrench principle : All of the techniques by which we’ve proved quickly that the pigeonhole clauses are unsatisfiable would have been useless if there had been one more clause such as ( x 01 ∨ x 11 ∨ x ¯ 22 ) ; that clause would have destroyed the symmetry!

We conclude that we’re allowed to remove clauses from F until reaching a subset of clauses F 0 for which symmetry-breakers S can be added. If F = F 0 ∪ F 1 , and if F 0 is satisfiable ⇔ F 0 ∪ S is satisfiable, then F 0 ∪ S ⊦ ∈ ⇒ F ⊦ ∈ .

One hundred test cases

And now—ta da!—let’s get to the climax of this long story, by looking at how our SAT solvers perform when presented with 100 moderately challenging instances of the satisfiability problem. The 100 sets of clauses summarized on the next two pages come from a cornucopia of different applications, many of which were discussed near the beginning of this section, while others appear in the exercises below.

Every test case has a code name, consisting of a letter and a digit. Table 6 characterizes each problem and also shows exactly how many variables, clauses, and total literals are involved. For example, the description of problem A1 ends with ‘ 2043 | 24772 ¯ | 55195 | U ’; this means that A1 consists of 24772 clauses on 2043 variables, having 55195 literals altogether, and those clauses are unsatisfiable. Furthermore, since ‘24772’ is underlined, all of A1’s clauses have length 3 or less.

Table 6
CAPSULE SUMMARIES OF THE HUNDRED TEST CASES

A1. Find x = x 1 x 2 ... x 99 with νx = 27 and no three equally spaced 1s. (See exercise 31 .)

2043| 24772 |55195|U

A2. Like A1, but x 1 x 2 ... x 100 .

2071| 25197 |56147|S

B1. Cover a mutilated 10 × 10 board with 49 dominoes, without using extra clauses to break symmetry.

176|572|1300|U

B2. Like B1, but a 12 × 12 board with 71 dominoes.

260|856|1948|U

C1. Find an 8-step Boolean chain that computes ( z 2 z 1 z 0 ) 2 = x 1 + x 2 + x 3 + x 4 . (See exercise 479(a) .)

384|16944|66336|U

C2. Find a 7-step Boolean chain that computes the modified full adder functions z 1 , z 2 , z 3 in exercise 481(b) .

469|26637|100063|U

C3. Like C2, but with 8 steps.

572|33675|134868|S

C4. Find a 9-step Boolean chain that computes z l and z r in the mod-3 addition problem of exercise 480(b) .

678|45098|183834|S

C5. Connect A to A , ... , J to J in Dudeney’s puzzle of exercise 392 , ( iv ).

1980|22518|70356|S

C6. Like C5, but move the J in row 8 from column 4 to column 5.

1980|22518|70356|U

C7. Given binary strings s 1 , ... , s 50 of length 200, randomly generated at distances ≤ r j from some string x , find x (see exercise 502 ).

65719| 577368 |1659623|S

C8. Given binary strings s 1 , ... , s 40 of length 500, inspired by biological data, find a string at distance ≤ 42 from each of them.

123540| 909120 |2569360|U

C9. Like C8, but at distance ≤ 43.

124100| 926200 |2620160|S

D1. Satisfy factor fifo (18 , 19 , 111111111111). (See exercise 41 .)

1940| 6374 |16498|U

D2. Like D1, but factor lifo .

1940| 6374 |16498|U

D3. Like D1, but (19, 19, 111111111111).

2052| 6745 |17461|S

D4. Like D2, but (19, 19, 111111111111).

2052| 6745 |17461|S

D5. Solve ( x 1 ... x 9 ) 2 × ( y 1 ... y 9 ) 2 ≠ ( x 1 ... x 9 ) 2 × ( y 1 ... y 9 ) 2 , with two copies of the same Dadda multiplication circuit.

864|2791|7236|U

E0. Find an Erd˝os discrepancy pattern x 1 ... x 500 (see exercise 482 ).

1603| 9157 |27469|S

E1. Like E0, but x 1 ... x 750 .

2556| 14949 |44845|S

E2. Like E0, but x 1 ... x 1000 .

3546| 21035 |63103|S

F1. Satisfy fsnark ( 99 ). (See exercise 176 .)

1782| 4161 |8913|U

F2. Like F1, but without the clauses ( e ¯ 1 , 3 ∨ f ¯ 99 , 3 ) ∧ ( f ¯ 1 , 1 ∨ e ¯ 2 , 1 ) .

1782| 4159 |8909|S

G1. Win Late Binding Solitaire with the “most difficult winnable deal” in answer 486 .

1242|22617|65593|S

G2. Like G1, but with the most difficult unwinnable deal.

1242|22612|65588|U

G3. Find a test pattern for the fault “ B 43 43 stuck at 0” in prod (16, 32).

3498| 11337 |29097|S

G4. Like G3, but for the fault “ D 34 13 , 9 stuck at 0.”

3502| 11349 |29127|S

G5. Find a 7 × 15 array X 0 leading to X 3 = 𝗟𝗜𝗞𝗘 as in Fig. 78 , having at most 38 live cells.

7150| 28508 |71873|U

G6. Like G5, but at most 39 live cells.

7152| 28536 |71956|S

G7. Like G5, but X 4 = 𝗟𝗜𝗞𝗘 and X 0 can be arbitrary.

8725| 33769 |84041|U

G8. Find a configuration in the Game of Life that proves f ∗ (7, 7) = 28 (see exercise 83 ).

97909| 401836 |1020174|S

K0. Color the 8 × 8 queen graph with 8 colors, using the direct encoding ( 15 ) and ( 16 ), also forcing the colors of all vertices in the top row.

512|5896|12168|U

K1. Like K0, but with the exclusion clauses ( 17 ) also.

512|7688|15752|U

K2. Like K1, but with kernel clauses instead of ( 17 ) (see answer 14 ).

512|6408|24328|U

K3. Like K1, but with support clauses instead of ( 16 ) (see exercise 399 ).

512|13512|97288|U

K4. Like K1, but using the order encoding for colors.

448|6215|21159|U

K5. Like K4, but with the hint clauses ( 162 ) appended.

448|6299|21663|U

K6. Like K5, but with double clique hints ( exercise 396 ).

896|8559|27927|U

K7. Like K1, but with the log encoding of exercise 391(a) .

2376| 5120 |15312|U

K8. Like K1, but with the log encoding of exercise 391(b) .

192|5848|34968|U

L1. Satisfy langford ( 10 ).

130|2437|5204|U

L2. Satisfy langford ′( 10 ).

273|1020|2370|U

L3. Satisfy langford ( 13 ).

228|5875|12356|U

L4. Satisfy langford ′( 13 ).

502|1857|4320|U

L5. Satisfy langford ( 32 ).

1472|102922|210068|S

L6. Satisfy langford ′( 32 ).

3512|12768|29760|S

L7. Satisfy langford ( 64 ).

6016|869650|1756964|S

L8. Satisfy langford ′( 64 ).

14704|53184|124032|S

M1. Color the McGregor graph of order 10 ( Fig. 76 ) with 4 colors, using one color at most 6 times, via the cardinality constraints ( 18 ) and ( 19 ).

1064|2752|6244|U

M2. Like M1, but via ( 20 ) and ( 21 ).

814|2502|5744|U

M3. Like M1, but at most 7 times.

1161|2944|6726|S

M4. Like M2, but at most 7 times.

864|2647|6226|S

M5. Like M4, but order 16 and at most 11 times.

2256|7801|18756|U

M6. Like M5, but at most 12 times.

2288|8080|19564|S

M7. Color the McGregor graph of order 9 with 4 colors, and with at least 18 regions doubly colored (see exercise 19 ).

952|4539|13875|S

M8. Like M7, but with at least 19 regions.

952|4540|13877|U

N1. Place 100 nonattacking queens on a 100 × 100 board.

10000|1151800|2313400|S

O1. Solve a random open shop scheduling problem with 8 machines and 8 jobs, in 1058 units of time.

50846| 557823 |1621693|U

O2. Like O1, but in 1059 units.

50901| 558534 |1623771|S

P0. Satisfy ( 99 ), ( 100 ), and ( 101 ) for m = 20, thereby exhibiting a poset of size 20 with no maximal element.

400|7260|22080|U

P1. Like P0, but with m = 14 and using only the clauses of exercise 228 .

196|847|2667|U

P2. Like P0, but with m = 12 and using only the clauses of exercise 229 .

144|530|1674|U

P3. Like P2, but omitting the clause ( x ¯ 31 ∨ x ¯ 16 ∨ x 36 ) .

144|529|1671|S

P4. Like P3, but with m = 20.

400|2509|7827|S

Q0. Like K0, but with 9 colors.

576|6624|13688|S

Q1. Like K1, but with 9 colors.

576|8928|18296|S

Q2. Like K2, but with 9 colors.

576|7200|27368|S

Q3. Like K3, but with 9 colors.

576|15480|123128|S

Q4. Like K4, but with 9 colors.

512|7008|24200|S

Q5. Like K5, but with 9 colors.

512|7092|24704|S

Q6. Like K6, but with 9 colors.

1024|9672|31864|S

Q7. Like K7, but with 9 colors.

3168|6776|20800|S

Q8. Like K8, but with 9 colors.

256|6776|52832|S

Q9. Like Q8, but with the log encoding of exercise 391(c) .

256|6584|42256|S

R1. Satisfy rand (3, 1061, 250, 314159).

250| 1061 |3183|S

R2. Satisfy rand (3, 1062, 250, 314159).

250| 1062 |3186|U

S1. Find a 4-term disjunctive normal form on { x 1 ,..., x 20 } that differs from ( 27 ) but agrees with it at 108 random training points.

356|4229|16596|S

S2. Like S1, but at 109 points.

360|4310|16760|U

S3. Find a sorting network on nine elements that begins with the comparators [1:6][2:7][3:8][4:9] and finishes in five more parallel rounds. (See exercise 64 .)

5175|85768|255421|U

S4. Like S3, but in six more rounds.

6444|107800|326164|S

T1. Find a 24 × 100 tatami tiling that spells ‘ TATAMI ’ as in exercise 118 .

2874|10527|26112|S

T2. Like T1, but 24 × 106 and the ‘ I ’ should have serifs.

3048|11177|27724|U

T3. Solve the TAOCP problem of exercise 389 with only 4 knight moves.

3752|12069|27548|U

T4. Like T3, but with 5 knight moves.

3756|12086|27598|S

T5. Find the pixel in row 5, column 18 of Fig. 80(c) , the lexicographically last solution to the Cheshire Tom problem.

8837| 39954 |100314|S

T6. Like T5, but column 19.

8837| 39955 |100315|U

T7. Solve the run-count extension of the Cheshire Tom problem (see exercise 117 ).

25734|65670|167263|S

T8. Like T7, but find a solution that differs from Fig. 79 .

25734|65671|167749|U

W1. Satisfy waerden (3, 10; 97).

97|2779|11662|U

W2. Satisfy waerden (3, 13; 159).

159|7216|31398|S

W3. Satisfy waerden (5, 5; 177).

177|7656|38280|S

W4. Satisfy waerden (5, 5; 178).

178|7744|38720|U

X1. Prove that the “taking turns” protocol ( 43 ) gives mutual exclusion for at least 100 steps.

1010|3612|10614|U

X2. Prove that assertions Φ for the four-bit protocol of exercise 101 , analogous to ( 50 ), are invariant.

129|354|926|U

X3. Prove that Bob won’t starve in 36 steps, assuming the Φ of X2.

1652|10552|28971|U

X4. Prove that there’s a simple 36-step path with the four-bit protocol, assuming the Φ of X2.

22199|50264|130404|S

X5. Like X4, but 37 steps.

23388|52822|137034|U

X6. Like X1, but with Peterson’s protocol ( 49 ) instead of ( 43 ).

2218|8020|23222|U

X7. Prove that there’s a simple 54-step path with protocol ( 49 ).

26450|56312|147572|S

X8. Like X7, but 55 steps.

27407|58317|152807|U

images images

Fig. 95. The clauses of these test cases bind the variables together in significantly different ways. (Illustrations by Carsten Sinz.)

Of course we can’t distinguish hard problems from easy ones by simply counting variables, clauses, and literals. The great versatility with which clauses can capture logical relationships means that different sets of clauses can lead to wildly different phenomena. Some of this immense variety is indicated in Fig. 95 , which depicts ten instructive “variable interaction graphs.” Each variable is represented by a ball, and two variables are linked when they appear together in at least one clause. (Some edges are darker than others; see exercise 506 . For further examples of such 3D visualizations, presented also in color, see Carsten Sinz, Journal of Automated Reasoning 39 (2007), 219–243.)

A single SAT solver cannot be expected to excel on all of the many species of problems. Furthermore, nearly all of the 100 instances in Table 6 are well beyond the capabilities of the simple algorithms that we began with: Algorithms A , B , and D are unable to crack any of those test cases without needing more than fifty gigamems of computation, except for the simplest examples—L1, L2, L5, P3, P4, and X2. Algorithm L , the souped-up refinement of Algorithm D , also has a lot of difficulty with most of them. On the other hand, Algorithm C does remarkably well. It polishes off 79 of the given problems in fewer than ten G μ .

Thus the test cases of Table 6 are tough, yet they’re within reach. Almost all of them can be solved in say two minutes, at most, with methods known today.

Complete details can be found in the file SATexamples.tgz on the author’s website, together with many related problems both large and small.

Exactly 50 of these 100 cases are satisfiable. So we’re naturally led to wonder whether Algorithm W (“ WalkSAT”) will handle such cases well. The answer is that Algorithm W sometimes succeeds brilliantly—especially on problems C7, C9, L5, L7, M3, M4, M6, P3, P4, Q0, Q1, R1, S1, where it typically outperforms all the other methods we’ve discussed. In particular it solved S1 in just 1 M μ , in the author’s tests with N = 50 n and p = .4, compared to 25 M μ by the next best method, Algorithm C ; it won by 15 M μ versus Algorithm C ’s 83 M μ on M3, by 83 M μ versus Algorithm L ’s 104 M μ on Q0, by 95 M μ versus Algorithm C ’s 464 M μ on Q1, and by a whopping 104 M μ versus Algorithm C ’s 7036 M μ on C7. That was a surprise. WalkSAT also was reasonably competitive on problem N1. But in all other cases it was nowhere near the method of choice. Therefore we’ll consider only Algorithms L and C in the remainder of this discussion. *

When does a lookahead algorithm like Algorithm L outperform a clause-learning algorithm like Algorithm C ? Figure 96 shows how they compare to each other on our 100 test cases: Each problem is plotted with Algorithm C ’s running time on the vertical axis and Algorithm L ’s on the horizontal axis. Thus Algorithm L is the winner for problems that appear above the dotted line. (This dotted line is “wavy” because times aren’t drawn to scale: The k th fastest running time is shown as k units from the left of the page or from the bottom.)

* There actually are two variants of Algorithm L , because the alternative heuristics of exercise 143 must be used for looking ahead when clauses of length 4 or more are present. We could use exercise 143 even when given all- ternary clauses; but experience shows that we’d tend to lose a factor of 2 or more by doing so. Our references to Algorithm L therefore implicitly assume that exercise 143 is being applied only when necessary.

images

Fig. 96. Comparison of Algorithms C and L on 100 moderately difficult satisfiability problems.

All of these experiments were aborted after 50 G μ , if necessary, since many of these problems could potentially take centuries before running to completion. Thus the test cases for which Algorithm L timed out appear at the right edge of Fig. 96 , and the tough cases for Algorithm C appear at the top. Only E2 and X8 were too hard for both algorithms to handle within the specified cutoff time.

Algorithm L is deterministic: It uses no random variables. However, a slight change (see exercise 505 ) will randomize it, because the inputs can be shuffled as they are in Algorithm C ; and we might as well assume that this change has been made. Then both Algorithms L and C have variable running times. They will find solutions or prove unsatisfiability more quickly on some runs than on others, as we’ve already seen for Algorithm C in Fig. 92 .

To compensate for this variability, each of the runtimes reported in Fig. 96 is the median of nine independent trials. Figure 97 shows all 9×100 of the empirical running times obtained with Algorithm C , sorted by their median values. We can see that many of the problems have near-constant behavior; indeed, the ratio max/min was less than 2 in 38 of the cases. But 10 cases turned out to be highly erratic in these experiments, with max/min > 100; problem P4 was actually solved once after only 323 kilo mems, while another run lasted 339 giga mems!

One might expect satisfiable problems, such as P4, to benefit more from lucky guesses than unsatisfiable problems do; and these experiments strongly support that hypothesis: Of the 21 problems with max/min > 30, all but P0 are satisfiable, and all 32 of the problems with max/min < 1.7 are unsatisfiable. One might also expect the mean running time (the arithmetic average) to exceed the median running time, in problems like this—because bad luck can be significantly bad, though hopefully rare. Yet the mean is actually smaller than the median in 30 cases, about equally distributed between satisfiable and unsatisfiable.

The median is a nice measure because it is meaningful even in the presence of occasional timeouts. It’s also fair, because we are able to achieve the median time, or better, more often than not.

We should point out that input/output has been excluded from these time comparisons. Each satisfiability problem is supposed to appear within a computer’s memory as a simple list of clauses, after which the counting of mems actually begins. We include the cost of initializing the data structures and solving the problem, but then we stop counting before actually outputting a solution.

Some of the test cases in Table 6 and Fig. 96 represent different encodings of the same problem. For example, problems K0–K8 all demonstrate that the 8 × 8 queen graph can’t be colored with 8 colors. Similarly, problems Q0–Q9 all show that 9 colors will suffice. We’ve already discussed these examples above when considering alternative encodings; and we noted that the best solutions, K6 and Q5, are obtained with an extended order encoding and with Algorithm C . Therefore the fact that Algorithm L beats Algorithm C on problems K0, K1, K2, and K3 is somewhat irrelevant; those problems won’t occur in practice.

Problems L5 and L6 compare different ways to handle the at-most-one constraint. L6 is slightly better for Algorithm L , but Algorithm C prefers L5. Similarly, M1 and M2 compare different ways to deal with a more general cardinality constraint. Here M2 turns out to be better, although both are quite easy for Algorithm C and difficult for Algorithm L .

images

Fig. 97. Nine random running times of Algorithm C , sorted by their medians. ( Unsatisfiable cases have solid dots or squares; satisfiable cases are hollow.)

We’ve already noted that Algorithm L shines with respect to random problems such as R1 and R2, and it dominates all competitors even more when unsatisfiable random 3SAT problems get even bigger. Lookahead methods are also successful in waerden problems like W1–W4.

Unsatisfiable Langford problems such as L3 and L4 are definitely bêtes noires for Algorithm C , although not so bad for Algorithm L . Even the world’s fastest CDCL solver, “ Treengeling,” was unable to refute the clauses of langford ( 17 ) in 2013 until it had learned 26.7 billion clauses; this process took more than a week, using a cluster of 24 computers working together. By contrast, the backtrack method of exercise 7.2.2–21 was able to prove unsatisfiability after fewer than 4T μ of computation—that’s about 50 minutes on a single vintage-2013 CPU.

We’ve now discussed every case where Algorithm L trounces Algorithm C , except for D5; and D5 is actually somewhat scandalous! It’s an inherently simple problem that hardware designers call a “ miter”: Imagine two identical circuits that compute some function f ( x 1 ,..., x n ), one with gates g 1 , ... , g m and another with corresponding gates g 1 ′ , … , g m ′ , all represented as in ( 24 ). The problem is to find x 1 ... x n for which the final results g m and g m ′ aren’t equal. It’s obviously unsatisfiable. Furthermore, there’s an obvious way to refute it, by successively learning the clauses ( g ¯ 1 ∨ g 1 ′ ) , ( g ¯ 1 ′ ∨ g 1 ) , ( g ¯ 2 ∨ g ¯ 2 ′ ) , ( g ¯ 2 ′ ∨ g 2 ) , etc. In theory, therefore, Algorithm C will almost surely finish in polynomial time (see exercise 386 ). But in practice, the algorithm won’t discover those clauses without quite a lot of flailing around, unless special-purpose techniques are introduced to help it discover isomorphic gates.

Thus Algorithm C does have an Achilles heel or two. On the other hand, it is the clear method of choice in the vast majority of our test cases, and we can expect it to be the major workhorse for most of the satisfiability problems that we encounter in daily work. Therefore it behooves us to understand its behavior in some detail, not just to look at its total cost as measured in mems.

Table 7
ALGORITHM C ’S EMPIRICAL BEHAVIOR ON THE HUNDRED TEST CASES

name runtime bytes cells nodes learned of size triv disc sub flushes sat?
X2 0+2 Mμ 57 K 9 K 2K 1K 32.0 → 12.0 50% 6% 1% 30 U
K6 0+2 Mμ 314 K 46 K 1K 0K 15.8 → 11.8 22% 4% 3% 6 U
L5 1+1 Mμ 1841 K 210 K 0K 0 K 146.1 → 38.4 51% 23% 0% 0 S
P3 0+2 Mμ 96 K 19 K 2K 1K 18.4 → 12.6 4% 11% 1% 45 S
T1 0+6 Mμ 541 K 35 K 3K 1K 7.4 → 6.8 3% 2% 6% 9 S
T2 0+7 Mμ 574 K 37 K 4K 1K 7.2 → 6.8 1% 2% 4% 6 U
L6 0+8 Mμ 672 K 39 K 1K 0 K 195.9 → 67.8 86% 0% 0% 0 S
P0 0+11 Mμ 376 K 81 K 8K 4K 17.8 → 14.7 3% 10% 10% 28 U
K5 0+13 Mμ 294 K 55 K 3K 2K 18.6 → 12.4 33% 1% 1% 14 U
X1 0+13 Mμ 284 K 38 K 29 K 4K 6.3 → 5.8 0% 3% 8% 53 U
M4 0+24 Mμ 308 K 47 K 6K 4K 20.5 → 16.3 14% 2% 1% 3 S
S1 0+25 Mμ 366 K 72 K 9K 4K 34.0 → 26.7 22% 4% 1% 14 S
G4 0+29 Mμ 759 K 76 K 3K 2K 37.1 → 24.2 26% 0% 0% 1 S
N1 16+14 Mμ 19644 K 2314 K 41 K 0 K 629.3 → 291.7 44% 6% 0% 15 S
B1 0+31 Mμ 251 K 55 K 10 K 7K 13.5 → 11.3 3% 5% 4% 14 U
M2 0+32 Mμ 326 K 53 K 7K 5K 18.2 → 12.8 20% 1% 1% 6 U
L7 12+23 Mμ 14695 K 1758 K 2K 1 K 411.2 → 107.6 66% 4% 0% 0 S
E0 0+40 Mμ 571 K 95 K 5K 3K 30.2 → 19.3 14% 11% 0% 6 S
S4 1+69 Mμ 3291 K 600 K 6K 2K 17.2 → 12.6 19% 1% 1% 8 S
L8 1+72 Mμ 3047 K 224 K 3K 2 K 547.9 → 169.1 87% 0% 0% 0 S
M3 0+83 Mμ 493 K 84 K 13 K 9K 28.4 → 19.2 31% 0% 1% 1 S
Q2 0+87 Mμ 885 K 190 K 11 K 8K 61.7 → 45.8 36% 0% 0% 11 S
X6 0+93 Mμ 775 K 122 K 86 K 17 K 13.5 → 11.4 0% 3% 3% 32 U
F2 0+95 Mμ 714 K 118 K 42 K 22 K 14.3 → 13.1 0% 2% 4% 5 S
X4 1+98 Mμ 3560 K 158 K 24 K 3K 16.2 → 11.4 9% 2% 3% 623 S
X5 1+106 Mμ 3747 K 166 K 23 K 3K 16.5 → 11.0 11% 3% 3% 726 U
M1 0+131 Mμ 483 K 84 K 16 K 12 K 23.2 → 13.4 33% 1% 0% 1 U
Q5 0+143 Mμ 708 K 157 K 13 K 11 K 28.8 → 23.6 21% 2% 2% 6 S
L1 0+157 Mμ 597 K 139 K 21 K 18 K 36.7 → 19.0 60% 3% 0% 30 U
S2 0+176 Mμ 722 K 161 K 29 K 17 K 37.5 → 27.5 33% 3% 1% 8 U
S3 1+201 Mμ 2624 K 471 K 12 K 6K 14.5 → 9.8 21% 1% 2% 1 U
Q4 0+213 Mμ 781 K 175 K 19 K 16 K 29.2 → 23.3 25% 3% 1% 6 S
L2 0+216 Mμ 588 K 136 K 23 K 20 K 36.2 → 17.4 75% 1% 0% 6 U
X3 0+235 Mμ 1000 K 191 K 61 K 25 K 37.7 → 19.3 34% 1% 2% 14 U
G3 0+251 Mμ 1035 K 145 K 12 K 9K 57.9 → 28.1 42% 1% 0% 0 S
Q0 0+401 Mμ 1493 K 342 K 37 K 28 K 63.3 → 40.0 50% 0% 0% 14 S
Q1 0+464 Mμ 1516 K 343 K 41 K 33 K 63.0 → 41.0 45% 0% 0% 14 S
T4 0+546 Mμ 2716 K 544 K 202 K 18 K 218.3 → 61.5 83% 1% 0% 3018 S
Q9 0+555 Mμ 1409 K 343 K 152 K 71 K 26.7 → 20.6 3% 5% 2% 99 S
Q3 0+613 Mμ 1883 K 448 K 27 K 22 K 60.1 → 40.3 41% 1% 1% 7 S
W1 0+626 Mμ 848 K 208 K 71 K 63 K 20.8 → 13.4 5% 14% 1% 28 U
Q6 0+646 Mμ 1211 K 266 K 40 K 35 K 30.4 → 23.2 30% 1% 1% 2 S
M6 0+660 Mμ 1378 K 266 K 80 K 52 K 34.0 → 22.2 33% 1% 1% 59 S
B2 0+668 Mμ 906 K 216 K 96 K 75 K 17.1 → 13.2 4% 5% 2% 16 U
T6 1+668 Mμ 2355 K 291 K 34 K 25 K 41.4 → 19.1 57% 0% 1% 11 U
D4 0+669 Mμ 1009 K 186 K 35 K 28 K 55.7 → 15.9 70% 0% 0% 2 S
M5 0+677 Mμ 1183 K 219 K 73 K 48 K 32.6 → 20.2 37% 1% 1% 139 U
R1 0+756 Mμ 913 K 220 K 87 K 74 K 17.3 → 12.4 3% 8% 0% 9 S
F1 0+859 Mμ 1485 K 311 K 218 K 135 K 17.6 → 15.1 1% 3% 3% 6 U
O2 7+1069 Mμ 18951 K 3144 K 3K 2K 17.0 → 9.5 35% 0% 0% 1 S
Q8 0+1107 Mμ 1786 K 437 K 184 K 109 K 29.4 → 20.2 6% 6% 1% 109 S
C5 0+1127 Mμ 1987 K 419 K 159 K 104 K 24.4 → 16.5 12% 2% 1% 776 S
D2 0+1159 Mμ 962 K 177 K 54 K 45 K 51.8 → 11.5 73% 0% 0% 2 U
C3 0+1578 Mμ 2375 K 571 K 190 K 96 K 49.7 → 23.4 39% 3% 2% 11 S
D1 0+1707 Mμ 1172 K 230 K 76 K 62 K 45.1 → 11.6 73% 0% 0% 2 U
T5 1+1735 Mμ 3658 K 617 K 80 K 59 K 72.5 → 40.9 50% 0% 0% 43 S
Q7 0+1761 Mμ 2055 K 419 K 515 K 118 K 33.9 → 20.3 9% 7% 0% 12 S
D3 0+1807 Mμ 1283 K 254 K 77 K 64 K 57.3 → 14.0 80% 0% 0% 1 S
R2 0+1886 Mμ 1220 K 296 K 173 K 149 K 17.0 → 11.8 3% 9% 0% 14 U
O1 7+2212 Mμ 18928 K 3140 K 5K 3K 17.3 → 8.9 39% 0% 0% 4 U
W3 0+2422 Mμ 1819 K 448 K 191 K 174 K 19.3 → 15.5 2% 12% 1% 18 S
P2 0+2435 Mμ 2039 K 504 K 378 K 301 K 20.9 → 13.7 3% 11% 1% 45 U
C6 0+2792 Mμ 2551 K 560 K 305 K 217 K 27.0 → 17.0 20% 2% 1% 492 U
E1 0+2902 Mμ 2116 K 453 K 180 K 144 K 38.0 → 20.5 21% 18% 0% 2 S
P1 0+3280 Mμ 2726 K 674 K 819 K 549 K 18.2 → 14.4 0% 9% 3% 45 U
G6 1+3941 Mμ 3523 K 647 K 380 K 253 K 31.0 → 17.8 31% 0% 0% 0 S
C9 13+4220 Mμ 35486 K 4923 K 116 K 32 K 11.8 → 9.9 5% 1% 1% 4986 S
C2 0+4625 Mμ 2942 K 712 K 442 K 255 K 46.1 → 18.8 42% 4% 1% 15 U
K4 0+5122 Mμ 1858 K 446 K 267 K 241 K 19.6 → 13.7 19% 2% 1% 5 U
C1 0+5178 Mμ 2532 K 613 K 510 K 311 K 48.9 → 17.0 48% 6% 1% 20 U
G7 1+6070 Mμ 4227 K 771 K 546 K 369 K 32.5 → 17.6 35% 0% 0% 0 U
C8 13+6081 Mμ 35014 K 4823 K 151 K 58 K 15.3 → 10.7 15% 1% 1% 8067 U
T7 1+6467 Mμ 5428 K 544 K 333 K 108 K 26.8 → 15.3 32% 1% 1% 14565 S
C7 8+7029 Mμ 20971 K 3174 K 908 K 32 K 9.5 → 8.4 0% 3% 0% 4965 S
T8 1+7046 Mμ 5322 K 517 K 356 K 117 K 26.9 → 15.0 33% 0% 1% 15026 U
W2 0+7785 Mμ 3561 K 884 K 501 K 432 K 34.7 → 21.3 13% 17% 1% 28 S
G5 1+7799 Mμ 4312 K 844 K 642 K 446 K 33.4 → 17.4 39% 0% 0% 0 U
G1 0+8681 Mμ 5052 K 1221 K 631 K 350 K 61.1 → 34.1 38% 1% 2% 55 S
K1 0+9813 Mμ 2864 K 685 K 405 K 360 K 36.2 → 18.4 53% 2% 0% 13 U
X7 1+11857 Mμ 6235 K 697 K 1955 K 224 K 40.6 → 23.7 35% 0% 1% 31174 S
K0 0+11997 Mμ 3034 K 731 K 493 K 421 K 35.6 → 19.4 45% 2% 0% 14 U
K2 0+12601 Mμ 3028 K 729 K 500 K 427 K 34.8 → 18.0 46% 2% 0% 12 U
A2 0+13947 Mμ 3766 K 843 K 645 K 585 K 34.4 → 15.9 32% 1% 0% 0 S
K8 0+15033 Mμ 2748 K 680 K 821 K 699 K 21.2 → 13.1 8% 15% 1% 93 U
P4 0+16907 Mμ 6936 K 1721 K 1676 K 1314 K 36.5 → 24.0 5% 11% 1% 33 S
A1 0+17073 Mμ 3647 K 815 K 763 K 701 K 30.7 → 14.7 29% 2% 0% 0 U
T3 0+19266 Mμ 10034 K 2373 K 2663 K 323 K 291.8 → 72.9 86% 1% 0% 34265 U
K7 0+20577 Mμ 3168 K 721 K 1286 K 828 K 23.3 → 13.5 9% 15% 0% 9 U
K3 0+20990 Mμ 3593 K 878 K 453 K 407 K 36.7 → 19.0 55% 2% 0% 6 U
W4 0+21295 Mμ 3362 K 834 K 977 K 899 K 19.0 → 14.1 4% 15% 0% 21 U
M8 0+22281 Mμ 4105 K 994 K 992 K 785 K 37.3 → 20.5 43% 1% 1% 6 U
G2 0+23424 Mμ 6910 K 1685 K 1198 K 701 K 68.8 → 34.3 47% 1% 1% 120 U
D5 0+24141 Mμ 3232 K 779 K 787 K 654 K 63.5 → 13.4 78% 0% 0% 2 U
M7 0+24435 Mμ 4438 K 1077 K 1047 K 819 K 40.6 → 23.3 42% 1% 1% 6 S
C4 1+31898 Mμ 8541 K 2108 K 1883 K 1148 K 60.6 → 25.7 42% 4% 1% 12 S
G8 7+35174 Mμ 24854 K 2992 K 4350 K 1101 K 48.0 → 34.7 9% 0% 0% 1523 S
E2 0+53739 Mμ 5454 K 1258 K 2020 K 1658 K 41.5 → 20.8 25% 21% 0% 3 S
X8 2+248789 Mμ 12814 K 2311 K 17005 K 3145 K 56.4 → 22.5 63% 0% 0% 330557 U
L3 0+295571 Mμ 19653 K 4894 K 7402 K 6886 K 70.7 → 31.0 63% 8% 0% 30 U
L4 0+677815 Mμ 22733 K 5664 K 8545 K 7931 K 78.6 → 35.4 86% 0% 0% 5 U

Table 7 summarizes the salient statistics, again listing all cases in order of their median running time (exclusive of input and output). Each running time is actually broken into two parts, ‘ x + y ’, where x is the time to initialize the data structures in step C1 and y is the time for the other steps, both rounded to megamems. For example, the exact median processing time for case L5 was 1,484,489 μ to initialize, then 655,728 μ to find a solution; this is shown as ‘1+1 M μ ’ in the third line of the table. The time for initialization is usually negligible except when there are many clauses, as in problem N1.

The median run of problem L5 also allocated 1,841,372 bytes of memory for data; this total includes the space needed for 210,361 cells in the MEM array, at 4 bytes per cell, together with other arrays such as VAL, OVAL, HEAP, etc. The implementati on considered here keeps unlearned binary clauses in a separate BIMP table, as explained in the answer to exercise 267 .

This run of L5 found a solution after implicitly traversing a search tree with 138 “ nodes.” The number of nodes, or “ decisions,” is the number of times step C6 of the algorithm goes to step C3. It is shown as ‘0 K’ in Table 7 , because the node counts, byte counts, and cell counts are rounded to the nearest thousand.

The number of nodes always exceeds or equals the number of learned clauses, which is the number of conflicts detected at levels d > 0. (See step C7.) In the case of problem L5, only 84 clauses were learned; so again the table reports ‘0 K’. These 84 clauses had average length r + 1 = 146.1; then the simplification process of exercise 257 reduced this average to just 38.4. Nevertheless, the resulting simplified clauses were still sufficiently long that the “trivial” clauses discussed in exercise 269 were sometimes used instead; this substitution happened 43 times (51 %). Furthermore 19 of the learned clauses (23 %) were immediately discarded, using the method of exercise 271 . These percentages show up in the ‘triv’ and ‘disc’ columns of the table.

Sometimes, as in problems D1–D5, a large majority of the learned clauses were replaced by trivial ones; on the other hand, 27 of the 100 cases turned out to be less than 10% trivial in this sense. Table 7 also shows that the discard rate was 5% or more in 26 cases. The ‘sub’ column refers to learned clauses that were “subsumed on the fly” by the technique of exercise 270 ; this optimization is less common, yet it occurs often enough to be worthwhile.

The great variety in our examples is reflected in the variety of behaviors exhibited in Table 7 , although several interesting trends can also be perceived. For example, the number of nodes is naturally correlated with the number of learned clauses, and both statistics tend to grow as the total running time increases. But there are significant exceptions: Two outliers, O1 and O2, have a remarkably high ratio of mems per learned clause, because of their voluminous data.

The penultimate column of Table 7 counts how often Algorithm C decided to restart itself after flushing unproductive literals from its current trail. This quantity does not simply represent the number of times step C5 discovers that M ≥ M f ; it depends also on the current agility level (see ( 127 )) and on the parameter ψ in Table 4 . Some problems, like A1 and A2, had such high agility that they were solved satisfactorily with no restarts whatsoever; but another one, T4, finished in about 500 megamems after restarting more than 3000 times.

The number of “purges” ( recycling phases) is not shown, but it can be estimated from the number of learned clauses (see exercise 508 ). An aggressive purging policy has kept the total number of memory cells comfortably small.

Tuning up the parameters

Table 7 shows that the hardest problem of all for Algorithm C in these experiments, L4, found itself substituting trivial clauses 86% of the time but making only 5 restarts. That test case would probably have been solved much more quickly if the algorithm’s parameters had been specially adjusted for instances of the Langford problem.

Algorithm C , as implemented in the experiments above, has ten major parameters that can be modified by the user on each run:

α , tradeoff between p and q in clause RANGE scores (see Eq. ( 123 ));

ρ , damping factor in variable ACT scores (see after ( 118 ));

ϱ , damping factor in clause ACT scores (see Eq. ( 125 ));

Δ p , initial value of the purging threshold M p (see after ( 125 ));

δ p , amount of gradual increase in M p (see after ( 125 ));

τ , threshold used to prefer trivial clauses (see answer to exercise 269 );

w , full “ warmup” runs done after a restart (see answer to exercise 287 );

p , probability of c hoosing a decision variable at random (see exercise 266 );

P , probability that OVAL ( k ) is initially even;

ψ , agility threshold for flushing (see Table 4 ).

The values for these parameters initially came from seat-of-the-pants guesses

α \= 0.2 , ρ \= 0.95 ,       ϱ \= 0.999 ,     Δ p \= 20000 ,       δ P \= 500 ,                               τ \= 1 ,     w \= 0 ,     p \= 0.02 ,     P \= 0 ,     ψ \= 0.166667 ; ( 193 )

and those defaults gave reasonably good results, so they were used happily for many months (although there was no good reason to believe that they couldn’t be improved). Then finally, after the author had assembled the set of 100 test cases in Table 6 , it was time to decide whether to recommend the default values ( 193 ) or to come up with a better set of numbers.

Parameter optimization for general broad-spectrum use is a daunting task, not only because of significant differences between species of SAT instances but also because of the variability due to random choices when solving any specific instance. It’s hard to know whether a change of parameter will be beneficial or harmful, when running times are so highly erratic. Ouch— Fig. 97 illustrates dramatic variations even when all ten parameters are held fixed, and only the seed for random numbers is changed! Furthermore the ten parameters are not at all independent: An increase in ρ , say, might be a good thing, but only if the other nine parameters are also modified appropriately. How then could any set of defaults be recommended, without an enormous expense of time and money?

Fortunately there’s a way out of this dilemma, thanks to advances in the theory of learning. F. Hutter, H. H. Hoos, K. Leyton-Brown, and T. Stützle have developed a tool called Param ILS intended specifically for making such tuneups [ J. Artificial Intelligence Research 36 (2009), 267–306]; the ‘ILS’ in this name stands for “ iterated local search.” The basic idea is to start with a representative training set of not-too-hard problems, and to carry out random walks in the 10-dimensional parameter space using sophisticated refinements of WalkSAT-like principles. The best parameters discovered during this training session are then evaluated on more difficult problems outside the training set.

In March 2015, Holger Hoos helped the author to tune Algorithm C using ParamILS. The resulting parameters then yielded Fig. 97 , and Table 7 , and many other runtime values discussed above and below. Our training set consisted of 17 problems that usually cost less than 200 M μ with the original parameters ( 193 ), namely {K5, K6, M2, M4, N1, S1, S4, X4, X6} together with stripped-down versions of {A1, C2, C3, D1, D2, D3, D4, K0}. For example, instead of the vector x 1 ... x 100 required by problem A1, we looked only for a shorter vector x = x 1 ... x 62 , now with νx = 20; instead of D1 and D2 we sought 13-bit factors of 31415926; instead of K0 we tried to 9-color the SGB graph jean .

Ten independent training runs with ParamILS gave ten potential parameter settings ( α i , ρ i ,..., ψ i ). We evaluated them on our original 17 benchmarks, together with 25 others that were a bit more difficult: {F1, F2, S2, S3, T4, X5}, plus less-stripped-down variants of {A1, A2, A2, C7, C7, D3, D4, F1, F2, G1, G1, G2, G2, G8, K0, O1, O2, Q0, Q2}. For each of the ten shortlisted parameter settings, we ran each of these 17 + 25 problems with each of the random seeds {1, 2,..., 25}. Finally, hurrah, we had a winner: The parameters ( α , ρ ,..., ψ ) with minimum total running time in this experiment were

α \= 0.4 , ρ \= 0.9 , ϱ \= 0.9995 , Δ p \= 1000 , δ p \= 500 , τ \= 10 , w \= 0 , p \= 0.02 , P \= 0.5 , ψ \= 0.05. ( 194 )

And these are now the recommended de faults for general-purpose use.

How much have we thereby gained? Figure 98 compares the running times of our 100 examples, before and after tuning. It shows that the vast majority—77 of them—now run faster; these are the cases to the right of the dotted line from (1 M μ , 1 M μ ) to (1 T μ , 1 T μ ). Half of the cases experience a speedup exceeding 1.455; 27 of them now run more than twice as fast as they previously did.

Of course every rule has exceptions. The behavior of case P4 has gotten spectacularly worse, almost three orders of magnitude slower! Indeed, we saw earlier in Fig. 97 that this case has an amazingly unstable running time; further peculiarities of P4 are discussed in exercise 511 .

Our other major SAT solver, Algorithm L , also has parameters, notably

α , magic tradeoff coefficient in heuristic scores (see Eq. ( 64 ));

β , damping factor for double-look triggering (see step Y1);

γ , clause weight per literal in heuristic scores (see exercise 175 );

ε , offset in heuristic scores (see answer to exercise 146 );

Θ, maximum heuristic score threshold (see answer to exercise 145 );

Y , maximum depth of double-lookahead (see step Y1).

ParamILS suggests the following default values, which have been used in Fig. 96 :

α \= 3.5 , β \= 0.9998 , γ \= 0.2 , ε \= 0.001 , Θ \= 20.0 , Y \= 1 , ( 195 )

Returning to Fig. 98 , notice that the change from ( 193 ) to ( 194 ) has substantially hindered cases G3 and G4, which are examples of test pattern generation. Evidently such clauses have special characteristics that make them prefer special settings of the parameters. Our main reason for introducing parameters in the first place was, of course, to allow tweaking for different families of clauses.

images

Fig. 98. Median running times of Algorithm C , before and after its parameters were tuned.

Instead of finding values of ( α , ρ ,..., ψ ) that give good results in a broad spectrum of applications, we can clearly use a system like ParamILS to find values that are specifically tailored to a particular class of problems. In fact, this task is easier. For example, Hoos and the author asked for settings of the ten parameters that will tend to make Algorithm C do its best on problems of the form waerden (3, k ; n ). A pair of ParamILS runs, based solely on the easy training cases waerden (3, 9; 77) and waerden (3, 10; 95), suggested the parameters

α \= 0.5 , ρ \= 0.9995 , ϱ \= 0.99 , Δ p \= 100 , δ p \= 10 , τ \= 10 , w \= 8 , p \= 0.01 , P \= 0.5 , ψ \= 0.15 , ( 196 )

and this set indeed works very well. Figure 99 shows typical details, with 7 ≤ k ≤ 14 and with nine independent sample runs for every choice of k and n . Each unsatisfiable instance has n = W (3, k ), as given in the table following ( 10 ) above; each satisfiable instance has n = W (3, k )−1. The fastest run using default parameters ( 194 ) has been paired in Fig. 99 with the fastest run using waerden -tuned parameters ( 196 ); similarly, the second-fastest, ... , second-slowest, and slowest runs have also been paired. Notice that satisfiable instances tend to take an unpredictable amount of time, as in Fig. 97 . In spite of the fact that the new parameters ( 196 ) were found by a careful study of just two simple instances, they clearly yield substantial savings when applied to much, much harder problems of a similar nature. (See exercise 512 for another instructive example.)

images

Fig. 99. Running times of Algorithm C on clauses waerden (3, k ; n ), with and without special tuning.

Exploiting parallelism

Our focus in the present book is almost entirely on sequential algorithms, but we should be aware that the really tough instances of SAT are best solved by parallel methods.

Problems that are amenable to backtracking can readily be decomposed into subproblems that partition the space of solutions. For example, if we have 16 processors available, we can start them off on independent SAT instances in which variables x 1 x 2 x 3 x 4 have been forced to equal 0000, 0001, ... , 1111.

A naïve decomposition of that kind is rarely the best strategy, however. Perhaps only one of those sixteen cases is really challenging. Perhaps some of the processors are slower than others. Perhaps several processors will learn new clauses that the other processors ought to know. Furthermore, the splitting into subproblems need not occur only at the root of the search tree. Careful load-balancing and sharing of information will do much better. These challenges were addressed by a pioneering system called PSATO [H. Zhang, M. P. Bonacina, and J. Hsiang, Journal of Symbolic Computation 21 (1996), 543–560].

A much simpler approach should also be mentioned: We can start up many different solvers, or many copies of the same solver, with different sources of random numbers. As soon as one has finished, we can then terminate the others.

The best parallelized SAT solvers currently available are based on the “ cube and conquer” paradigm, which combines conflict driven clause learning with lookahead techniques that choose branch variables for partitioning; see M. J. H. Heule, O. Kullmann, S. Wieringa, and A. Biere, LNCS 7261 (2012), 50–65. In particular, this approach is excellent for the waerden problems.

Today has proved to be an epoch in my Logical work.

. . . I think of calling it the ‘Genealogical Method.’

— CHARLES L. DODGSON, Diary (16 July 1894)

The method of showing a statement to be tautologous consists merely of constructing a table under it in the usual way and observing that the column under the main connective is composed entirely of ‘T’s.

— W. V. O. QUINE, Mathematical Logic (1940)

A brief history

The classic syllogism “All men are mortal; Socrates is a man; hence Socrates is mortal” shows that the notion of resolution is quite ancient:

¬Man ∨ Mortal; ¬Socrates ∨ Man; ∴ ¬Socrates ∨ Mortal.

Of course, algebraic demonstrations that (¬ x ∨ y )∧(¬ z ∨ x ) implies (¬ z ∨ y ), when x , y , and z are arbitrary Boolean expressions, had to wait until Boole and his 19th-century followers brought mathematics to bear on the subject. The most notable contributor, resolutionwise, was perhaps C. L. Dodgson, who spent the last years of his life working out theories of inference by which complex chains of reasoning could be analyzed by hand. He published Symbolic Logic , Part I, in 1896, addressing it to children and to the young-in-heart by using his famous pen name Lewis Carroll. Section VII.II.§3 of that book explains and illustrates how to eliminate variables by resolution, which he called the Method of Underscoring.

When Dodgson died unexpectedly at the beginning of 1898, his nearly complete manuscript for Symbolic Logic , Part II, vanished until W. W. Bartley III was able to resurrect it in 1977. Part II was found to contain surprisingly novel ideas—especially its Method of Trees, which would have completely changed the history of mechanical theorem proving if it had come to light earlier. In this method, which Carroll documented at length in a remarkably clear and entertaining way, he constructed search trees essentially like Fig. 82 , then converted them into proofs by resolution. Instead of backtracking as in Algorithm D , which is a recursive depth-first method, he worked breadth-first: Starting at the root, he exploited unit clauses when possible, and branched on binary (or even ternary) clauses when necessary, successively filling out all unfinished branches level-by-level in hopes of being able to reuse computations.

Logicians of the 20th century took a different tack. They basically dealt with the satisfiability problem in its equivalent dual form as the tautology problem, namely to decide when a Boolean formula is always true. But they dismissed tautology-checking as a triviality, because it could always be solved in a finite number of steps by just looking at the truth table. Logicians were far more interested in problems that were provably unsolvable in finite time, such as the halting problem—the question of whether or not an algorithm terminates. Nobody was bothered by the fact that an n -variable function has a truth table of length 2 n , which exceeds the size of the universe even when n is rather small.

Practical computations with disjunctive normal forms were pioneered by Archie Blake in 1937, who introduced the “ consensus” of two implicants, which is dual to the resolvent of two clauses. Blake’s work was, however, soon forgotten; E. W. Samson, B. E. Mills, and (independently) W. V. O. Quine rediscovered the consensus operation in the 1950s, as discussed in exercise 7.1.1–31.

The next important step was taken by E. W. Samson and R. K. Mueller [Report AFCRC-TR-55-118 (Cambridge, Mass.: Air Force Cambridge Research Center, 1955), 16 pages], who presented an algorithm for the tautology problem that uses consensus to eliminate variables one by one. Their algorithm therefore was equivalent to SAT solving by successively eliminating variables via resolution. Samson and Mueller demonstrated their algorithm by applying it to the unsatisfiable clauses that we considered in ( 112 ) above.

Independently, Martin Davis and Hilary Putnam had begun to work on the satisfiability problem, motivated by the search for algorithms to deduce formulas in first-order logic—unlike Samson, Mills, and Mueller, who were chiefly interested in synthesizing efficient circuits. Davis and Putnam wrote an u npublished 62-page report “Feasible computational methods in the propositional calculus” (Rensselaer Polytechnic Institute, October 1958) in which a variety of different approaches were considered, such as the removal of unit clauses and pure literals, as well as “ case analysis,” that is, backtracking with respect to the subproblems F | x and F | x ¯ . As an alternative to case analysis, they also discussed eliminating the variable x by resolution. The account of this work that was eventually published [ JACM 7 (1960), 201–215] concentrated on hand calculation, and omitted case analysis in favor of resolution; but when the process was later implemented on a computer, jointly with George Logemann and Donald Loveland [ CACM 5 (1962), 394–397], the method of backtracking through different cases was found to work better with respect to memory requirements. (See Davis’s account of these developments in Handbook of Automated Reasoning (2001), 3–15.)

This early work didn’t actually cause the satisfiability problem to appear on many people’s mental radar screens, however. Far from it; ten years went by before SAT became an important buzzword. The picture changed in 1971, when Stephen A. Cook showed that satisfiability is the key to solving NP-complete problems: He proved that any algorithm to solve a de cision problem in nondeterministic polynomial time can be represented efficiently as a conjunction of ternary clauses to be satisfied. (See STOC 3 (1971), 151–158. We’ll study NP-completeness in Section 7.9.) Thus, a great multitude of hugely important problems could all be solved rather quickly, if we could only devise a decent algorithm for a single problem, 3SAT; and 3SAT seemed almost absurdly simple to solve.

A year of heady optimism following the pub lication of Cook’s paper soon gave way to the realization that, alas, 3SAT might not be so easy after all. Ideas that looked promising in small cases didn’t scale well, as the problem size was increased. Hence the central focus of work on satisfiability largely retreated into theoretical realms, unrelated to programming practice, except for occasional studies that used SAT as a simple model for the behavior of backtracking algorithms in general. Examples of such investigations, pioneered by A. T. Goldberg, P. W. Purdom, Jr., C. A. Brown, J. V. Franco, and others, appear in exercises 213 – 216 . See P. W. Purdom, Jr., and G. N. Haven, SICOMP 26 (1997), 456–483, for a survey of subsequent progress on questions of that kind.

The state of SAT art in the early 90s was well represented by an international programming competition held in 1992 [see M. Buro and H. Kleine Büning, Bulletin EATCS 49 (February 1993), 143–151]. The winning programs in that contest can be regarded as the first successful lookahead solvers on the path from Algorithm A to Algorithm L . Max Böhm “took the gold” by choosing the next branch variable based on lexicographically maximal ( H 1 ( x ),..., H n ( x )), where

H k ( x ) \= h k ( x ) + h k ( x ¯ ) + min ( h k ( x ) , h k ( x ¯ ) ) , h k ⁢ ( x ) \= | { C ∈ F | x ∈ C , | C | \= k } | .

[See M. Böhm and E. Speckenmeyer, Ann. Math. Artif. Intelligence 17 (1996), 381–400. A. Rauzy had independently proposed a somewhat similar branching criterion in 1988; see Revue d’intelligence artificielle 2 (1988), 41–60.] The silver medal went to Hermann Stamm, who used strong components of the dependency digraph to narrow the search at each branch node.

Advances in practical algorithms for satisfiability now began to take off. The benchmark problems of 1992 had been chosen at random, but the DIMACS Implementation Challenge of 1993 featured also a large number of structured instances of SAT. The main purpose of this “challenge” was not to crown a winner, but to bring more than 100 researchers together for a three-day workshop, at which they could compare and share results. In retrospect, the best overall performance at that time was arguably achieved by an elaborate lookahead solver called C-SAT, which introduced techniques for detailed exploration of the first-order effects of candidate literals [see O. Dubois, P. Andre, Y. Boufkhad, and J. Carlier, DIMACS 26 (1996), 415–436]. Further refinements leading towards the ideas in Algorithm L appeared in a Ph.D. thesis by Jon W. Freeman (Univ. of Pennsylvania, 1995), and in the work of Chu Min Li, who introduced double lookahead [see Information Processing Letters 71 (1999), 75–80]. The weighted binary heuristic ( 67 ) was proposed by O. Dubois and G. Dequen, Proc. International Joint Conference on Artificial Intelligence 17 (2001), 248–253.

Meanwhile the ideas underlying Algorithm C began to emerge. Matthew L. Ginsberg [ J. Artificial Intelligence Research 1 (1993), 25–46] showed that efficient backjumping was possible while remembering only at most two learned clauses for each variable. João P. Marques-Silva, in his 1995 thesis directed by Karem A. Sakallah, discovered how to turn unit-propagation conflicts into one or more clauses learned at “ unique implication points,” thus enhancing the potential for backjumping past decisions that didn’t affect the conflict. [See IEEE Trans. C-48 (1999), 506–521.] Similar methods were developed independently by Roberto J. Bayardo, Jr., and Robert C. Schrag [ AAAI Conf. 14 (1997), 203–208], who considered only the special case of new clauses that include the current decision literal, but introduced techniques for purging a learned clause when one of its literals was forced to flip its value. These new methods gave significant speedups on benchmark problems related to industrial applications.

The existence of fast SAT solvers, coupled with Gunnar Stålmarck’s new ideas about applying logic to computer design [see Swedish patent 467076 (1992)], led to the introduction of bounded model checking techniques by Armin Biere, Alessandro Cimatti, Edmund Clarke, and Yunshan Zhu [ LNCS 1579 (1999), 193–207]. Satisfiability techniques had also been introduced to solve classical planning problems in artificial intelligence [Henry Kautz and Bart Selman, Proc. European Conf. Artificial Intelligence 10 (1992), 359–363]. Designers could now verify much larger models than had been possible with BDD methods.

The major breakthroughs appeared in a solver called Chaff [M. W. Moskewicz, C. F. Madigan, Y. Zhao, L. Zhang, and S. Malik, ACM/IEEE Design Automation Conf. 38 (2001), 530–535], which had two especially noteworthy innovations: (i) “ VSIDS” (the Variable State Independent Decaying Sum heuristic), a surprisingly effective way to select decision literals, which also worked well with restarts, and which suggested the even better ACT heuristic of Algorithm C that soon replaced it; also (ii) lazy data structures with two watched literals per clause, which made unit propagation much faster with respect to large learned clauses. (A somewhat similar watching scheme, introduced earlier by H. Zhang and M. Stickel [ J. Automated Reasoning 24 (2000), 277–296], had the disadvantage that it needed to be downdated while backtracking.)

These exciting developments sparked a revival of international SAT competitions, which have been held annually since 2002. The winner in 2002, BerkMin by E. Goldberg and Y. Novikov, has been described well in Discrete Applied Mathematics 155 (2007), 1549–1561. And year after year, these challenging contests have continued to spawn further progress. By 2010, more than twice as many benchmarks could be solved in a given period of time as in 2002, using the programs of 2002 and 2010 on the computers of 2010 [see M. Järvisalo, D. Le Berre, O. Roussel, and L. Simon, AI Magazine 33 ,1 (Spring 2012), 89–94].

The overall champion in 2007 was SATzilla , which was actually not a separate SAT solver but rather a program that knew how to choose intelligently between other solvers on any given instance. SATzilla would first take a few seconds to compute basic features of a problem: the distribution of literals per clause and clauses per literal, the balance between positive and negative occurrences of variables, the proximity to Horn clauses, etc. Samples could quickly be taken to estimate how many unit propagations occur at levels 1, 4, 16, 64, 256, and how many decisions are needed before reaching a conflict. Based on these numbers, and experience with the performance of the other solvers on the previous year’s benchmarks, SATzilla was trained to select the algorithm that appeared most likely to succeed. This “portfolio” approach, which tunes itself nicely to the characteristics of vastly different sets of clauses, has continued to dominate the international competitions ever since. Of course portfolio solvers rely on the existence of “real” solvers, invented independently and bug-free, which shine with respect to particular classes of problems. And of course the winner of competitions may not be the best actual system for practical use. [See L. Xu, F. Hutter, H. H. Hoos, and K. Leyton-Brown, J. Artificial Intelligence Research 32 (2008), 565–606; LNCS 7317 (2012), 228–241; CACM 57 , 5 (May 2014), 98–107.]

Historical notes about details of the algorithms, and about important related techniques such as preprocessing and encoding, have already been discussed above as the algorithms and techniques were described.

One recurring theme appears to be that the behavior of SAT solvers is full of surprises: Some of the most important improvements have been introduced for what has turned out to be the wrong reasons, and a theoretical understanding is still far from adequate.

[The next future breakthrough might come from “variable learning,” as suggested by Tseytin’s idea of extended resolution: Just as clause learning increases the number of clauses, m , we might find good ways to increase the number of variables, n . The subject seems to be far from fully explored.]

Exercises

1. [ 10 ] What are the shortest (a) satisfiable (b) unsatisfiable sets of clauses?

2. [ 20 ] Travelers to the remote planet Pincus have reported that all the healthy natives like to dance, unless they’re lazy. The lazy nondancers are happy, and so are the healthy dancers. The happy nondancers are healthy; but natives who are lazy and healthy aren’t happy. Although the unhappy, unhealthy ones are always lazy, the lazy dancers are healthy. What can we conclude about Pincusians, based on these reports?

3. [ M21 ] Exactly how many clauses are in waerden ( j, k ; n )?

4. [ 22 ] Show that the 32 constraints of waerden (3, 3; 9) in ( 9 ) remain unsatisfiable even if up to four of them are removed.

5. [ HM47 ] Determine the asymptotic behavior of W (3, k ) as k → ∞.

▸ 6. [ HM37 ] Use the Local Lemma to show that W (3, k ) = Ω( k 2 /(log k ) 3 ).

7. [ 21 ] Can one satisfy the clauses { ( x i ∨ x i + 2 d ∨ x i + 2 d + 1 ) | 1 ≤ i ≤ n − 2 d + 1 , d ≥ 0 } ∪ { ( x ¯ i ∨ x ¯ i + 2 d ∨ x ¯ i + 2 d + 1 ) | 1 ≤ i ≤ n − 2 d + 1 , d ≥ 0 } ?

▸ 8. [ 20 ] Define clauses waerden ( k 0 , k 1 ,..., k b −1 ; n ) that are satisfiable if and only if n < W ( k 0 , k 1 ,..., k b −1 ).

9. [ 24 ] Determine the value of W (2, 2, k ) for all k ≥ 0. Hint : Consider k mod 6.

▸ 10. [ 21 ] Show that every satisfiability problem with m clauses and n variables can be transformed into an equivalent monotonic problem with m + n clauses and 2 n variables, in which the first m clauses have only neg ative literals, and the last n clauses are binary with two positive literals.

11. [ 27 ] (M. Tsimelzon, 1994.) Show that a general 3SAT problem with clauses { C 1 ,..., C m } and variables {1,..., n } can be reduced to a 3D matching problem of size 10 m that involves the following cleverly designed triples:

Each clause C j corresponds to 3×10 vertices, namely lj , l ¯ j , | l | j ′, and | l | j ″ for each l ∈ C j , together with wj , xj , yj , and zj , and also j ′ k and j ″ k for 1 ≤ k ≤ 7. If i or ι ¯ occurs in t clauses C j 1 , ... , C j t , there are t “true” triples { i j k , i j k ′ , i j k ″ } and t “false” triples { ι ¯ j k , i j k ′ , i j 1 + ( k ⁢   mod   t ) ″ } , for 1 ≤ k ≤ t . Each clause C j \= ( l 1 ∨ l 2 ∨ l 3 ) also spawns three “satisfiability” triples { l ¯ 1 j , j ′ 1 , j ″ 1 } , { l ¯ 2 j , j ′ 1 , j ″ 2 } , { l ¯ 3 j , j ′ 1 , j ″ 3 } ; six “filler” triples { l 1 j , j ′2, j ″ 1}, { l ¯ 1 j , j ′ 3 , j ″ 1 } , { l 2 j , j ′4, j ″ 2}, { l ¯ 2 j , j ′ 5 , j ″ 2 } , { l 3 j , j ′6, j ″ 3}, { l ¯ 3 j , j ′ 7 , j ″ 3 } ; and twelve “ gadget” triples { wj , j ′2, j ″ 4}, { wj , j ′4, j ″ 4}, { wj , j ′6, j ″ 4}, { xj , j ′2, j ″ 5}, { xj , j ′5, j ″ 5}, { xj , j ′7, j ″ 5}, { yj , j ′3, j ″ 6}, { yj , j ′4, j ″ 6}, { yj , j ′7, j ″ 6}, { zj , j ′3, j ″ 7}, { zj , j ′5, j ″ 7}, { zj , j ′6, j ″ 7}. Thus there are 27 m triples altogether.

For example, Rivest’s satisfiability problem ( 6 ) leads to a 3D matching problem with 216 triples on 240 vertices; the triples that involve vertices 18 and 1 ¯ 8 are {18, 18′, 18 ″ }, { 1 ¯ 8 , 18 ′ , 11 ″ } , { 1 ¯ 8 , 8 ′ 1 , 8 ″ 2 } , {18, 8′4, 8″2}, { 1 ¯ 8 , 8 ′ 5 , 8 ″ 2 } .

12. [ 21 ] (M. J. H. Heule.) Simplify ( 13 ) by exploiting the identity

S ≤ 1 ( y 1 , … , y p ) \= ∃ t ( S ≤ 1 ( y 1 , … y j , t ) ∧ S ≤ 1 ( t ¯ , y j + 1 , … , y p ) ) .

13. [ 24 ] Exercise 7.2.2.1–15 defines an exact cover problem that corresponds to Langford pairs of order n .

  1. What are the constraints analogous to ( 12 ) when n = 4?

  2. Show that there’s a simple way to avoid duplicate binary clauses such as those in ( 14 ), whenever an exact cover problem is converted to clauses using ( 13 ).

  3. Describe the corresponding clauses langford ( 4 ) and langford ′ ( 4 ).

14. [ 22 ] Explain why the clauses ( 17 ) might help a SAT solver to color a graph.

15. [ 24 ] By comparing the McGregor graph of order 10 in Fig. 76 with the McGregor graph of order 3 shown here, give a precise definition of the vertices and edges of the McGregor graph whose order is a given number n ≥ 3. Exactly how many vertices and edges are present in this graph, as a function of n ?

images

16. [ 21 ] Do McGregor graphs have cliques of size 4?

17. [ 26 ] Let f ( n ) and g ( n ) be the smallest and largest values of r such that McGregor’s graph of order n can be 4-colored, and such that some color appears exactly r times. Use a SAT solver to find as many values of f ( n ) and g ( n ) as you can.

▸ 18. [ 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 5 6 n times. Hint : The construction depends on the value of n mod 6.

▸ 19. [ 29 ] Continuing exercise 17 , let h ( n ) be the largest number of regions that can be given two colors simultaneously (without using the clauses ( 17 )). Investigate h ( n ).

20. [ 40 ] In exactly how many ways can McGregor’s map ( Fig. 76 ) be four-colored?

21. [ 22 ] Use a SAT solver to find a minimum-size kernel in the graph of Fig. 76 .

22. [ 20 ] Color the graph C 5 ⊠ C 5 ¯ with the fewest colors. (Two vertices of this graph can receive the same color if and only if they are a king move apart in a 5 × 5 torus.)

23. [ 20 ] Compare the clauses ( 18 ) and ( 19 ) to ( 20 ) and ( 21 ) in the case n = 7, r = 4.

▸ 24. [ M34 ] The clauses obtained from ( 20 ) and ( 21 ) in the previous exercise can be simplified, because we can remove the two that contain the pure literal b 1 2 .

  1. Prove that the literal b 1 2 is always pure in ( 20 ) and ( 21 ), when r > n /2.

  2. Show that b 1 2 might also be pure in some cases when r < n /2.

  3. The clauses obtained from ( 20 ) and ( 21 ) have many pure literals b j k when r has its maximum value n − 1. Furthermore, their removal makes other literals pure. How many clauses will remain in this case after all pure literals have been eliminated?

  4. Show that the complete binary tree with n ≥ 2 leaves is obtained from complete binary trees with n ′ and n ″ = n − n ′ leaves, where either n ′ or n ″ is a power of 2.

  5. Let a ( n , r ) and c ( n , r ) be respectively the number of auxiliary variables b j k and the total number of clauses that remain after all of the pure auxiliary literals have been removed from ( 20 ) and ( 21 ). What are a (2 k , 2 k −1 ) and c (2 k , 2 k −1 )?

  6. Prove that a ( n, r ) = a ( n , n ″ ) = a ( n , n ′) for n ″ ≤ r ≤ n ′, and this common value is max 1≤ r < n a ( n, r ). Also a ( n, r ) = a ( n , n − r ); and c ( n , r ) ≥ c ( n , n − r ) if r ≤ n /2.

25. [ 21 ] Show that ( 18 )–( 19 ) and ( 20 )–( 21 ) are equally effective when r = 2.

26. [ 22 ] Prove that Sinz’s clauses ( 18 ) and ( 19 ) enforce the cardinality constraint x 1 + ··· + x n ≤ r . Hint : Show that they imply s j k \= 1 whenever x 1 + ··· + x j + k −1 ≥ k .

27. [ 20 ] Similarly, prove the correctness of Bailleux and Boufkhad’s ( 20 ) and ( 21 ). Hint : They imply b j k \= 1 whenever the leaves below node k contain j or more 1s.

▸ 28. [ 20 ] What clauses result from ( 18 ) and ( 19 ) when we want to ensure that x 1 + ··· + x n ≥ 1? (This special case converts arbitrary clauses into 3SAT clauses.)

▸ 29. [ 20 ] Instead of the single constraint x 1 + ··· + x n ≤ r , suppose we wish to impose a sequence of constraints x 1 + ··· + x i ≤ r i for 1 ≤ i ≤ n . Can this be done nicely with additional clauses and auxiliary variables?

30. [ 22 ] If auxiliary variables s j k are used as in ( 18 ) and ( 19 ) to make x 1 +···+ x n ≤ r , while s j ′ k are used to make x ¯ 1 + ⋅ ⋅ ⋅ + x ¯ n ≤ n − r , show that we may unify them by taking s k ′ j \= s j k ¯ , for 1 ≤ j ≤ n − r , 1 ≤ k ≤ r . Can ( 20 ) and ( 21 ) be similarly unified?

▸ 31. [ 28 ] Let F t ( r ) be the smallest n for which there is a bit vector x 1 ... x n with x 1 + ··· + x n \= r and with no t equally spaced 1s. For example, F 3 (12) = 30 because of the unique solution 101100011010000000010110001101. Discuss how F t ( r ) might be computed efficiently with the help of a SAT solver.

32. [ 15 ] A list coloring is a graph coloring in which v ’s color belongs to a given set L ( v ), for each vertex v . Represent list coloring as a SAT problem.

33. [ 21 ] A double coloring of a graph is an assignment of two distinct colors to every vertex in such a way that neighboring vertices share no common colors. Similarly, a q -tuple coloring assigns q distinct colors to each vertex. Find double and triple colorings of the cycle graphs C 5 , C 7 , C 9 , ... , using as few colors as possible.

34. [ HM26 ] The fractional coloring number χ * ( G ) of a graph G is defined to be the minimum ratio p / q for which G has a q -tuple coloring that uses p colors.

  1. Prove that χ * ( G ) ≤ χ ( G ), and show that equality holds in McGregor’s graphs.

  2. Let S 1 , ... , S N be all the independent subsets of G ’s vertices. Show that

    χ * ( G ) \= min λ 1 , … , λ N ≥ 0 { λ 1 + ⋅ ⋅ ⋅ + λ N | Σ j \= 1 N λ j [ v ∈ S j ] \= 1 for all vertices v } .

    (This is a fractional exact cover problem.)

  3. What is the fractional coloring number χ * ( C n ) of the cycle graph C n ?

  4. Consider the following greedy algorithm for coloring G : Set k ← 0 and G 0 ← G ; while G k is nonempty, set k ← k + 1 and G k ← G k −1 \ C k , where C k is a maximum independent set of G k −1 . Prove that k ≤ H α ( G ) χ * ( G ), where α ( G ) is the size of G ’s largest independent set; hence χ ( G )/ χ * ( G ) ≤ H α ( G ) \= O (log n ). Hint : Let t v \= 1/| C i | if v ∈ C i , and show that ∑ v∈S t v ≤ H | S | whenever S is an independent set.

35. [ 22 ] Determine χ * ( G ) when G is (a) the graph of the contiguous United States (see 7.2.2.1–(113) ); (b) the graph of exercise 22 .

▸ 36. [ 22 ] A radio coloring of a graph, also known as an L (2, 1) labeling, is an assignment of integer colors to vertices so that the colors of u and v differ by at least 2 when u — v , and by at least 1 when u and v have a common neighbor. (This notion, introduced by Fred Roberts in 1988, was motivated by the problem of assigning channels to radio transmitters, without interference from “close” transmitters and without strong interference from “very close” transmitters.) Find a radio coloring of McGregor’s graph, Fig. 76 , that uses only 16 consecutive colors.

37. [ 20 ] Find an optimum radio coloring of the contiguous USA graph, 7.2.2.1–(113) .

38. [ M25 ] How many consecutive colors are needed for a radio coloring of (a) the n × n square grid P n ⎕ P n ? (b) the vertices {( x, y, z ) | x, y, z ≥ 0, x + y + z = n }, which form a triangular grid with n + 1 vertices on each side?

39. [ M46 ] Find an optimum radio coloring of the n -cube, for some value of n > 6.

40. [ 01 ] Is the factorization problem ( 22 ) unsatisfiable whenever z is a prime number?

41. [ M21 ] Determine the number of Boolean operations ∧, ∨, ⊕ needed to multiply m -bit numbers by n -bit numbers with Dadda’s scheme, when 2 ≤ m ≤ n .

42. [ 21 ] Tseytin encoding analogous to ( 24 ) can be devised also for ternary operations, without introducing any additional variables besides those of the function being encoded. Illustrate this principle by encoding the basic operations x ← t ⊕ u ⊕ v and y ← 〈 tuv 〉 of a full adder directly, instead of composing them from ⊕, ∧, and ∨.

▸ 43. [ 21 ] For which integers n ≥ 2 do there exist odd palindromic binary numbers x = ( x n ... x 1 ) 2 = ( x 1 ... x n ) 2 and y = ( y n ... y 1 ) 2 = ( y 1 ... y n ) 2 such that their product xy = ( z 2 n ... z 1 ) 2 = ( z 1 ... z 2 n ) 2 is also palindromic?

▸ 44. [ 30 ] (Maximum ones.) Find the largest possible value of νx + νy + ν ( xy ), namely the greatest total number of 1 bits, over all multiplications of 32-bit binary x and y .

45. [ 20 ] Specify clauses that constrain ( z t ... z 1 ) 2 to be a perfect square.

46. [ 30 ] Find the largest perfect square less than 2 100 that is a binary palindrome.

▸ 47. [ 20 ] Suppose a circuit such as Fig. 77 has m outputs and n inputs, with g gates that transform two signals into one and h gates that transform one signal into two. Find a relation between g and h , by expressing the total number of wires in two ways.

48. [ 20 ] The small circuit shown here has three inputs, three XOR gates, one fanout gate, eight wires, and one output. Which single-stuck-at faults are detected by each of the eight test patterns pqr ?

images

49. [ 24 ] Write a program that determines exactly which of the 100 single-stuck-at faults of the circuit in Fig. 77 are detected by each of the 32 possible input patterns. Also find all the minimum sets of test patterns that will discover every such fault (unless it’s not detectable).

50. [ 24 ] Demonstrate Larrabee’s method of representing stuck-at faults by describing the clauses that characterize test patterns for the fault “ x 2 1 stuck at 1” in Fig. 77 . (This is the wire that splits off of x 2 and feeds into x 2 3 and x 2 4 , then to b 2 and b 3 ; see Table 1 .)

51. [ 40 ] Study the behavior of SAT solvers on the problem of finding a small number of test patterns for all of the detectable single- stuck-at faults of the circuit prod (32, 32). Can a complete set of patterns for this large circuit be discovered “automatically” (without relying on number theory)?

52. [ 15 ] What clauses correspond to ( 29 ) and ( 30 ) when the second case on the left of Table 2 , f (1, 0, 1, 0,..., 1) = 1, is taken into account?

▸ 53. [ M20 ] The numbers in Table 2 are definitely nonrandom. Can you see why?

▸ 54. [ 23 ] Extend Table 2 using the rule in the previous exercise. How many rows are needed before f ( x ) has no M -term representation in DNF, when M = 3, 4, and 5?

55. [ 21 ] Find an equation analogous to ( 27 ) that is consistent with Table 2 and has every variable complemented. (Thus the resulting function is monotone decreasing.)

▸ 56. [ 22 ] Equation ( 27 ) exhibits a function matching Table 2 that depends on only 8 of the 20 variables. Use a SAT solver to show that we can actually find a suitable f that depends on only five of the x j .

▸ 57. [ 29 ] Combining the previous exercise with the methods of Section 7.1.2, exhibit a function f for Table 2 that can be evaluated with only six Boolean operations(!).

▸ 58. [ 20 ] Discuss adding the clauses p ¯ i , j ∨ q ¯ i , j to ( 29 ), ( 30 ), and ( 31 ).

59. [ M20 ] Compute the exact probability that f ^ ( x ) in ( 32 ) differs from f ( x ) in ( 27 ).

60. [ 24 ] Experiment with the problem of learning f ( x ) in ( 27 ) from training sets of sizes 32 and 64. Use a SAT solver to find a conjectured function, f ^ ( x ) ; then use BDD methods to determine the probability that this f ^ ( x ) differs from f ( x ) for random x .

61. [ 20 ] Explain how to test when a set of clauses generated from a training set via ( 29 )–( 31 ) is satisfiable only by the function f ( x ) in ( 27 ).

62. [ 23 ] Try to learn a secret small-DNF function with N -bit training sets x (0) , x (1) , x (2) , ... , where x (0) is random but each bit of x ( k ) ⊕ x ( k −1) for k > 0 is 1 with probability p . (Thus, if p is small, successive data points will tend to be near each other.) Do such sets turn out to be more efficient in practice than the purely random ones that arise for p = 1/2?

▸ 63. [ 20 ] Given an n -network α = [ i 1 : j 1 ][ i 2 : j 2 ] ... [ i r : j r ], as defined in the exercises for Section 5.3.4, explain how to use a SAT solver to test whether or not α is a sorting network. Hint : Use Theorem 5.3.4Z.

64. [ 26 ] The exact minimum time T ^ ( n ) of a sorting network for n elements is a famous unsolved problem, and the fact that T ^ ( 9 ) \= 7 was first established in 1987 by running a highly optimized program for many hours on a Cray 2 supercomputer.

Show that this result can now be proved with a SAT solver in less than a second(!).

▸ 65. [ 28 ] Describe encodings of the Life transition function ( 35 ) into clauses.

  1. Use only the variables x i j ′ and x ij .

  2. Use auxiliary variables as in the Bailleux and Boufkhad encoding ( 20 )–( 21 ), sharing intermediate results between neighboring cells as discussed in the text.

66. [ 24 ] Use a SAT solver to find short counterparts to Fig. 78 in which (a) X 1 = 𝗟𝗜𝗞𝗘; (b) X 2 = 𝗟𝗜𝗞𝗘. In each case X 0 should have the smallest possible number of live cells.

67. [ 24 ] Find a mobile chessboard path X 0 → X 1 → ... → X 21 with no more than five cells alive in each X t . (The glider in ( 37 ) leaves the board after X 20 .) How about X 22 ?

68. [ 39 ] Find a maximum-length mobile Life path on the chessboard in which 6 to 10 cells are always alive.

69. [ 23 ] Find all (a) still lifes and (b) oscillators of period > 1 that live in a 4×4 board.

70. [ 21 ] The live cells of an oscillator are divided into a rotor (those that change) and a stator (those that stay alive).

  1. Show that the rotor cannot be just a single cell.

  2. Find the smallest example of an oscillator whose rotor is images .

  3. Similarly, find the smallest oscillators of period 3 whose rotors have the following forms: images .

▸ 71. [ 22 ] When looking for sequences of Life transition on a square grid, an a symmetrical solution will appear in eight different forms, because the grid has eight different symmetries. Furthermore, an asymmetrical periodic solution will appear in 8 r different forms, if r is the length of the period.

Explain how to add further clauses so that essentially equivalent solutions will occur only once: Only “ canonical forms” will satisfy the conditions.

72. [ 28 ] Oscillators of period 3 are particularly intriguing, because Life seems so inherently binary.

  1. What are the smallest such oscillators (in terms of bounding box)?

  2. Find period-3 oscillators of sizes 9 × n and 10 × n , with n odd, that have “ fourfold symmetry”: The patterns are unchanged after left-right and/or up-down reflection. (Such patterns are not only pleasant to look at, they also are much easier to find, because we need only consider about one-fourth as many variables.)

  3. What period-3 oscillators with fourfold symmetry have the most possible live cells, on grids of sizes 15 × 15, 15 × 16, and 16 × 16?

  4. The period-3 oscillator shown here has another kind of four-way symmetry, because it’s unchanged after 90° rotation. (It was discovered in 1972 by Robert Wainwright, who called it “ snake dance” because its stator involves four snakes.) What period-3 oscillators with 90° symmetry have the most possible live cells, on grids of sizes 15 × 15 and 16 × 16?

images

▸ 73. [ 21 ] ( Mobile flipflops .) An oscillator of period 2 is called a flipflop , and the Life patterns of mobile flipflops are particularly appealing: Each cell is either blank (dead at every time t ) or type A (alive when t is even) or type B (alive when t is odd). Every nonblank cell (i) has exactly three neighbors of the other type, and (ii) doesn’t have exactly two or three neighbors of the same type.

  1. The blank cells of a mobile flipflop also satisfy a special condition. What is it?

  2. Find a mobile flipflop on an 8 × 8 grid, with top row images .

  3. Find patterns that are mobile flipflops on m × n toruses for various m and n . (Thus, if replicated indefinitely, each one will tile the plane with an infinite mobile flipflop.) Hint : One solution has no blank cells whatsoever; another has blank cells like a checkerboard.

74. [ M28 ] Continuing the previous exercise, prove that no nonblank cell of a finite mobile flipflop has more than one neighbor of its own type. (This fact greatly speeds up the search for finite mobile flipflops.) Can two type A cells be diagonally adjacent?

75. [ M22 ] (Stephen Silver, 2000.) Show that a finite, mobile oscillator of period p ≥ 3 must have some cell that is alive more than once during the cycle.

76. [ 41 ] Construct a mobile Life oscillator of period 3.

77. [ 20 ] “Step X −1 ,” which precedes X 0 in ( 38 ), has the glider configuration images instead of images . What conditions on the still life X 5 will ensure that state X 0 is indeed reached? (We don’t want digestion to begin prematurely.)

78. [ 21 ] Find a solution to the four-step eater problem in ( 38 ) that works on a 7 × n grid, for some n , instead of 8 × 8.

79. [ 23 ] What happens if the glider meets the eater of ( 39 ) in its opposite phase (namely images instead of images )?

80. [ 21 ] To counteract the problem in the previous exercise, find an eater that is symmetrical when reflected about a diagonal, so that it eats both images and images . (You’ll have to go larger than 8 × 8, and you’ll have to wait longer for digestion.)

81. [ 21 ] Conway discovered a remarkable “spaceship,” where X 4 is X 0 shifted up 2:

images

Is there a left-right symmetrical still life that will eat such spaceships?

▸ 82. [ 22 ] ( Light speed .) Imagine Life on an infinite plane, with all cells dead at time 0 except in the lower left quadrant. More precisely, suppose that X t \= ( x tij ) is defined for all t ≥ 0 and all integers −∞ < i,j < +∞, and that x 0 ij \= 0 whenever i > 0 or j > 0.

  1. Prove that x tij \= 0 whenever 0 ≤ t < max( i, j ).

  2. Furthermore x tij \= 0 when 0 ≤ − i ≤ j and 0 ≤ t < i + 2 j .

  3. And x tij \= 0 for 0 ≤ t < 2 i + 2 j , if i ≥ 0 and j ≥ 0. Hint : If x tij \= 0 whenever i ≥ − j , prove that x tij \= 0 whenever i > − j .

83. [ 21 ] According to the previous exercise, the earliest possible time that cell ( i, j ) can become alive, if all initial life is confined to the lower left quadrant of the plane, is at least

f(i,j ) = [i ≥0] + j[j ≥0] + ( i+j )[ i+j ≥0].

For example, when | i | ≤ 5 and | j | ≤ 5 the values of f ( i, j ) are shown at the right.

Let f * ( i, j ) be the actual minimum time at which cell ( i, j ) can be alive, for some such initial state. Devise a set of clauses by which a SAT solver can test whether or not f * ( i 0 , j 0 ) = f ( i 0 , j 0 ), given i 0 and j 0 . (Such clauses make interesting benchmark tests.)

images

84. [ 33 ] Prove that f * ( i, j ) = f ( i, j ) in the following cases when j > 0: (a) i = j , i = j + 1, and i = j − 1. (b) i = 0 and i = −1. (c) i = 1 − j . (d) i = j − 2. (e) i = −2.

▸ 85. [ 39 ] A Garden of Eden is a state of Life that has no predecessor.

  1. If the pattern of 92 cells illustrated here occurs anywhere within a bitmap X , verify that X is a Garden of Eden. (The gray cells can be either dead or alive.)

  2. This “ orphan” pattern, found with a SAT solver’s help, is the smallest that is currently known. Can you imagine how it was discovered?

images

86. [ M23 ] How many Life predecessors does a random 10×10 bitmap have, on average?

87. [ 21 ] Explain why the clauses ( 42 ) represent Alice and Bob’s programs ( 40 ), and give a general recipe for converting such programs into equivalent sets of clauses.

88. [ 18 ] Satisfy ( 41 ) and ( 42 ) for 0 ≤ t < 6, and the 20 × 6 additional binary clauses that exclude multiple states, along with the “embarrassing” unit clauses (A3 6 ) ∧ (B3 6 ).

89. [ 21 ] Here’s a mutual-exclusion protocol once recommended in 1966. Does it work?

A0. Maybe go to A1.

A1. Set a ← 1, go to A2.

A2. If l go to A3, else to A5.

A3. If b go to A3, else to A4.

A4. Set l ← 0, go to A2.

A5. Critical, go to A6.

A6. Set a ← 0, go to A0.

B0. Maybe go to B1.

B1. Set b ← 1, go to B2.

B2. If l go to B5, else to B3.

B3. If a go to B3, else to B4.

B4. Set l ← 1, go to B2.

B5. Critical, go to B6.

B6. Set b ← 0, go to B0.

90. [ 20 ] Show that ( 43 ), ( 45 ), and ( 46 ) permit starvation, by satisfying ( 47 ) and ( 48 ).

91. [ M21 ] Formally speaking, Alice is said to “starve” if there is (i) an infinite sequence of transitions X 0 → X 1 → … starting from the initial state X 0 , and (ii) an infinite sequence @ 0 , @ 1 , ... of Boolean “bumps” that changes infinitely often, such that (iii) Alice is in a “maybe” or “critical” state only a finite number of times. Prove that this can happen if and only if there is a starvation cycle ( 47 ) as discussed in the text.

92. [ 20 ] Suggest O ( r 2 ) clauses with which we can determine whether or not a mutual exclusion protocol permits a path X 0 → X 1 → … → X r of distinct states.

93. [ 20 ] What clauses correspond to the term ¬Φ( X ′) in ( 51 )?

▸ 94. [ 21 ] Suppose we know that ( X 0 → X 1 → … → X r ) ∧ ¬Φ( X r ) is unsatisfiable for 0 ≤ r ≤ k . What clauses will guarantee that Φ is invariant? (The case k = 1 is ( 51 ).)

95. [ 20 ] Using invariants like ( 50 ), prove that ( 45 ) and ( 46 ) provide mutual exclusion.

96. [ 22 ] Find all solutions to ( 52 ) when r = 2. Also illustrate the fact that invariants are extremely helpful, by finding a solution with distinct states X 0 , X 1 , ... , X r and with r substantially greater than 2, if the clauses involving Φ are removed.

97. [ 20 ] Can states A6 and B6 occur simultaneously in Peterson’s protocol ( 49 )?

▸ 98. [ M23 ] This exercise is about proving the nonexistence of starvation cycles ( 47 ).

  1. A cycle of states is called “pure” if one of the players is never bumped, and “ simple” if no state is repeated. Prove that the shortest im pure cycle, if any, is either simple or consists of two simple pure cycles that share a common state.

  2. If Alice is starved by some cycle with protocol ( 49 ), we know that she is never in states A0 or A5 within the cycle. Show that she can’t be in A1, A2, or A6 either.

  3. Construct clauses to test whether there exist states X 0 → X 1 → … → X r , with X 0 arbitrary, such that ( X 0 X 1 ... X k −1 ) is a starvation cycle for some k ≤ r .

  4. Therefore we can conclude that ( 49 ) is starvation-free without much extra work.

99. [ 25 ] Th. Dekker devised the first correct mutual-exclusion protocol in 1965:

A0. Maybe go to A1.

A1. Set a ← 1, go to A2.

A2. If b go to A3, else to A6.

A3. If l go to A4, else to A2.

A4. Set a ← 0, go to A5.

A5. If l go to A5, else to A1.

A6. Critical, go to A7.

A7. Set l ← 1, go to A8.

A8. Set a ← 0, go to A0.

B0. Maybe go to B1.

B1. Set b ← 1, go to B2.

B2. If a go to B3, else to B6.

B3. If l go to B2, else to B4.

B4. Set b ← 0, go to B5.

B5. If l go to B1, else to B5.

B6. Critical, go to B7.

B7. Set l ← 0, go to B8.

B8. Set b ← 0, go to B0.

Use bounded model checking to verify its correctness.

100. [ 22 ] Show that the following protocol can starve one player but not the other:

A0. Maybe go to A1.

A1. Set a ← 1, go to A2.

A2. If b go to A2, else to A3.

A3. Critical, go to A4.

A4. Set a ← 0, go to A0.

B0. Maybe go to B1.

B1. Set b ← 1, go to B2.

B2. If a go to B3, else to B5.

B3. Set b ← 0, go to B4.

B4. If a go to B4, else to B1.

B5. Critical, go to B6.

B6. Set b ← 0, go to B0.

▸ 101. [ 31 ] Protocol ( 49 ) has the potential defect that Alice and Bob might both be trying to set the value of l at the same time. Design a mutual-exclusion protocol in which each of them controls two binary signals, visible to the other. Hint : The method of the previous exercise can be enclosed in another protocol.

102. [ 22 ] If Alice is setting a variable at the same time that Bob is trying to read it, we might want to consider a more stringent model under which he sees either 0 or 1, nondeterministically. (And if he looks k times before she moves to the next step, he might see 2 k possible sequences of bits.) Explain how to handle this model of “ flickering” variables by modifying the clauses of exercise 87 .

103. [ 18 ] (Do this exercise by hand , it’s fun!) Find the 7×21 image whose tomographic sums are ( r 1 ,..., r 7 ) = (1, 0, 13, 6, 12, 7, 19); ( c 1 ,..., c 21 ) = (4, 3, 3, 4, 1, 6, 1, 3, 3, 3, 5, 1, 1, 5, 1, 5, 1, 5, 1, 1, 1); ( a 1 ,..., a 27 ) = (0, 0, 1, 2, 2, 3, 2, 3, 3, 2, 3, 3, 4, 3, 2, 3, 3, 3, 4, 3, 2, 2, 1, 1, 1, 1, 1); ( b 1 ,..., b 27 ) = (0, 0, 0, 0, 0, 1, 3, 3, 4, 3, 2, 2, 2, 3, 3, 4, 2, 3, 3, 3, 3, 3, 4, 3, 2, 1, 1).

104. [ M21 ] For which m and n is it possible to satisfy the digital tomography problem with a d \= b d \= 1 for 0 < d < m + n ? (Equivalently, when can m + n − 1 nonattacking bishops be placed on an m × n board?)

▸ 105. [ M28 ] A matrix whose entries are {−1, 0, +1} is tomographically balanced if its row, column, and diagonal sums are all zero. Two binary images X = ( x ij ) and X ′ \= ( x i j ′ ) clearly have the same row, column, and diagonal sums if and only if X − X ′ is tomographically balanced.

  1. Suppose Y is tomographically balanced and has m rows, n columns, and t occurrences of +1. How many m × n binary matrices X and X ′ satisfy X − X ′ = Y ?

  2. Express the condition “ Y is tomographically balanced” in terms of clauses, with the values {−1, 0, +1} represented respectively by the 2-bit codes {10, 00, 01}.

  3. Count the number T ( m, n ) of tomographically balanced matrices, for m , n ≤ 8.

  4. How many such matrices have exactly four occurrences of +1?

  5. At most how many +1s can a 2 n × 2 n tomographically balanced matrix have?

  6. True or false: The positions of the +1s determine the positions of the −1s.

106. [ M20 ] Determine a generous upper bound on the possible number of different sets of input data { r i , c j , a d , b d } that might be given to a 25 × 30 digital tomography problem, by assuming that each of those sums independently has any of its possible values. How does this bound compare to 2 750 ?

▸ 107. [ 22 ] Basket weavers from the Tonga culture of Inhambane, Mozambique, have developed appealing periodic designs called “ gipatsi patterns” such as this:

images

(Notice that an ordinary pixel grid has been rotated by 45°.) Formally speaking, a gipatsi pattern of period p and width n is a p × n binary matrix ( x i,j ) in which we have x i ,1 \= x i,n \= 1 for 1 ≤ i ≤ p . Row i of the matrix is to be shifted right by i − 1 places in the actual pattern. The example above has p = 6, n = 13, and the first row of its matrix is 1111101111101. Such a pattern has row sums r i \= Σ j \= 1 n x i , j for 1 ≤ i ≤ p and column sums c j \= Σ i \= 1 p x i , j for 1 ≤ j ≤ n , as usual. By analogy with ( 53 ), it also has

a d \= Σ i + j ≡ d ( modulo   p ) x i , j , 1 ≤ d ≤ p ; b d \= Σ 2 i + j ≡ d ( modulo     2 p ) x i , j , 1 ≤ d ≤ 2 p .

  1. What are the tomographic parameters r i , c j , a d , and b d in the example pattern?

  2. Do any other gipatsi patterns have the same parameters?

108. [ 23 ] The column sums c j in the previous exercise are somewhat artificial, because they count black pixels in only a small part of an infinite line. If we rotate the grid at a different angle, however, we can obtain infinite periodic patterns for which each of Fig. 79 ’s four directions encounters only a finite number of pixels.

Design a pattern of period 6 in which parallel lines always have equal tomographic projections, by changing each of the gray pixels in the following diagram to either white or black:

images

▸ 109. [ 20 ] Explain how to find the lexicographically smallest solution x 1 ... x n to a satisfiability problem, using a SAT solver repeatedly. (See Fig. 80(a) .)

110. [ 19 ] What are the lexicographically (first, last) solutions to waerden (3, 10; 96)?

111. [ 40 ] The lexicographically first and last solutions to the “ Cheshire Tom” problem in Fig. 80 are based on the top-to-bottom-and-left-to-right ordering of pixels. Experiment with other pixel orderings—for example, try bottom-to-top-and-right-to-left.

112. [ 46 ] Exactly how many solutions does the tomography problem of Fig. 79 have?

▸ 113. [ 30 ] Prove that the digital tomography problem is NP-complete, even if the marginal sums r , c , a , b are binary: Show that an efficient algorithm to decide whether or not an n × n pixel image ( x ij ) exists, having given 0–1 values of r i \= ∑ j x ij , c j \= ∑ i x ij , a d \= ∑ i + j = d +1 x ij , and b d \= ∑ i − j = d − n x ij , could be used to solve the binary tensor contingency problem of exercise 212(a) .

114. [ 27 ] Each cell ( i, j ) of a given rectangular grid either contains a land mine ( x i,j \= 1) or is safe ( x i,j \= 0). In the game of Minesweeper , you are supposed to identify all of the hidden mines, by probing locations that you hope are safe: If you decide to probe a cell with x i,j \= 1, the mine explodes and you die (at least virtually). But if x i,j \= 0 you’re told the number n i,j of neighboring cells that contain mines, 0 ≤ n i,j ≤ 8, and you live to make another probe. By carefully considering these numeric clues, you can often continue with completely safe probes, eventually touching every mine-free cell.

For example, suppose the hidden mines happen to match the 25 × 30 pattern of the Cheshire cat ( Fig. 79 ), and you start by probing the upper right corner. That cell turns out to be safe, and you learn that n 1,30 = 0; hence it’s safe to probe all three neighbors of (1, 30). Continuing in this vein soon leads to illustration ( α ) below, which depicts information about cells ( i, j ) for 1 ≤ i ≤ 9 and 21 ≤ j ≤ 30; unprobed cells are shown in gray, otherwise the value of n i,j appears. From this data it’s easy to deduce that x 1,24 = x 2,24 = x 3,25 = x 4,25 = ··· = x 9,26 = 1; you’ll never want to probe in those places, so you can mark such cells with X, arriving at state ( β ) since n 3,24 = n 5,25 = 4. Further progress downward to row 17, then leftward and up, leads without difficulty to state ( γ ). (Notice that this process is analogous to digital tomography, because you’re trying to reconstruct a binary array from information about partial sums.)

images

  1. Now find safe probes for all thirteen of the cells that remain gray in ( γ ).

  2. Exactly how much of the Cheshire cat can be revealed without making any unsafe guesses, if you’re told in advance that (i) x 1,1 = 0? (ii) x 1,30 = 0? (iii) x 25,1 = 0? (iv) x 25,30 = 0? (v) all four corners are safe? Hint : A SAT solver can help.

115. [ 25 ] Empirically estimate the probability that a 9×9 game of Minesweeper, with 10 randomly placed mines, can be won with entirely safe probes after the first guess.

116. [ 22 ] Find examples of Life flipflops for which X and X ′ are tomographically equal.

117. [ 23 ] Given a sequence x = x 1 ... x n , let ν (2) x = x 1 x 2 + x 2 x 3 + ··· + x n −1 x n . (A similar sum appears in the serial correlation coefficient, 3.3.2–(23).)

  1. Show that, when x is a binary sequence, the number of runs of 1s in x can be expressed in terms of νx and ν (2) x .

  2. Explain how to encode the condition ν (2) x ≤ r as a set of clauses, by modifying the cardinality constraints ( 20 )–( 21 ) of Bailleux and Boufkhad.

  3. Similarly, encode the condition ν (2) x ≥ r .

118. [ 20 ] A tatami tiling is a covering by dominoes in which no three share a corner:

images

(Notice that images is disallowed, but images would be fine.) Explain how to use a SAT solver to find a tatami tiling that covers a given set of pixels, unless no such tiling exists.

119. [ 18 ] Let F = waerden (3, 3; 9) be the 32 clauses in (9). For which literal l is the reduced formula F | l smallest? Exhibit the resulting clauses.

120. [ M20 ] True or false: F | L \= { C \ L ¯ | C ∈ F and C ∩ L = Ø}, if L ¯ \= { l ¯ | l ∈ L } .

121. [ 21 ] Spell out the changes to the link fields in the data structures, by expanding the higher-level descriptions that appear in steps A3, A4, A7, and A8 of Algorithm A .

▸ 122. [ 21 ] Modify Algorithm A so that it finds all satisfying assignments of the clauses.

123. [ 17 ] Show the contents of the internal data structures L , START , and LINK when Algorithm B or Algorithm D begins to process the seven clauses R ′ of ( 7 ).

▸ 124. [ 21 ] Spell out the low-level link field operations that are sketched in step B3.

▸ 125. [ 20 ] Modify Algorithm B so that it finds all satisfying assignments of the clauses.

126. [ 20 ] Extend the computation in ( 59 ) by one more step.

127. [ 17 ] What move codes m 1 ... m d correspond to the computation sketched in ( 59 ), just before and after backtracking occurs?

128. [ 19 ] Describe the entire computation by which Algorithm D proves that Rivest’s clauses ( 6 ) are unsatisfiable, using a format like ( 59 ). (See Fig. 82 .)

129. [ 20 ] In the context of Algorithm D , design a subroutine that, given a literal l , returns 1 or 0 according as l is or is not being watched in some clause whose other literals are entirely false.

130. [ 22 ] What low-level list processing operations are needed to “clear the watch list for x ¯ k ” in step D6?

▸ 131. [ 30 ] After Algorithm D exits step D3 without finding any unit clauses, it has examined the watch lists of every free variable. Therefore it could have computed the lengths of those watch lists, with little additional cost; and information about those lengths could be used to make a more informed decision about the variable that’s chosen for branching in step D4. Experiment with different branching heuristics of this kind.

▸ 132. [ 22 ] Theorem 7.1.1K tells us that every 2SAT problem can be solved in linear time. Is there a sequence of 2SAT clauses for which Algorithm D takes exponential time?

▸ 133. [ 25 ] The size of a backtrack tree such as Fig. 82 can depend greatly on the choice of branching variable that is made at every node.

  1. Find a backtrack tree for waerden (3, 3; 9) that has the fewest possible nodes.

  2. What’s the largest backtrack tree for that problem?

134. [ 22 ] The BIMP tables used by Algorithm L are sequential lists of dynamically varying size. One attractive way to implement them is to begin with every list having capacity 4 (say); then when a list needs to become larger, its capacity can be doubled.

Adapt the buddy system (Algorithm 2.5R) to this situation. (Lists that shrink when backtracking needn’t free their memory, since they’re likely to grow again later.)

▸ 135. [ 16 ] The literals l ′ in BIMP ( l ) are those for which l → l ′ in the “ implication digraph” of a given satisfiability problem. How can we easily find all of the literals l ″ such that l ″ → l , given l ?

136. [ 15 ] What pairs will be in TIMP ( 3 ¯ ), before and after x 5 is set to zero with respect to the clauses ( 9 ) of waerden (3, 3; 9), assuming that we are on decision level d = 0?

137. [ 24 ] Spell out in detail the processes of (a) removing a variable X from the free list and from all pairs in TIMP lists (step L7 of Algorithm L ), and of (b) restoring it again later (step L12). Exactly how do the data structures change?

▸ 138. [ 20 ] Discuss what happens in step L9 of Algorithm L if we happen to have both v ¯ ∈ BIMP ( u ¯ ) and u ¯ ∈ BIMP ( v ¯ ) .

139. [ 25 ] ( Compensation resolvents .) If w ∈ BIMP ( v ), the binary clause u ∨ v implies the binary clause u ∨ w , because we can resolve u ∨ v with v ¯ ∨ w . Thus step L9 could exploit each new binary clause further, by appending w as well as v to BIMP ( u ¯ ), for all such w . Discuss how to do this efficiently.

140. [ 21 ] The FORCE , BRANCH , BACKF , and BACKI arrays in Algorithm L will obviously never contain more than n items each. Is there a fairly small upper bound on the maximum possible size of ISTACK ?

141. [ 18 ] Algorithm L might increase ISTAMP so often that it overflows the size of the IST ( l ) fields. How can the mechanism of ( 63 ) avoid bugs in such a case?

142. [ 24 ] Algorithms A , B , and D can display their current progress by exhibiting a sequence of move codes m 1 ... m d such as ( 58 ) and ( 60 ); but Algorithm L has no such codes. Show that an analogous sequence m 1 ... m F could be printed in step L2, if desired. Use the codes of Algorithm D ; but extend them to show m j \= 6 (or 7) if R j −1 is a true (or false) literal whose value was found to be forced by Algorithm X , or forced by being a unit clause in the input.

▸ 143. [ 30 ] Modify Algorithm L so that it will apply to nonempty clauses of any size. Call a clause big if its size is greater than 2. Instead of TIMP tables, represent every big clause by ‘ KINX ’ and ‘ CINX ’ tables: Every literal l has a sequential list KINX ( l ) of big clause numbers; every big clause c has a sequential list CINX ( c ) of literals; c is in KINX ( l ) if and only if l is in CINX ( c ). The current number of active clauses containing l is indicated by KSIZE ( l ); the current number of active literals in c is indicated by CSIZE ( c ).

144. [ 15 ] True or false: If l doesn’t appear in any clause, h ′( l ) = 0.1 in ( 65 ).

145. [ 23 ] Starting with h ( l ) = 1 for each of the 18 literals l in waerden (3, 3; 9), find successively refined estimates h ′( l ), h ″( l ), ... , using ( 65 ) with respect to the 32 ternary clauses ( 9 ). Then, assuming that x 5 has been set false as in exercise 136 , and that the resulting binary clauses 13, 19, 28, 34, 37, 46, 67, 79 have been included in the BIMP tables, do the same for the 16 literals that remain at depth d = 1.

146. [ 25 ] Suggest an alternative to ( 64 ) and ( 65 ) for use when Algorithm L has been extended to nonternary clauses as in exercise 143 . (Strive for simplicity.)

147. [ 05 ] Evaluate C max in ( 66 ) for d = 0, 1, 10, 20, 30, using the default C 0 and C 1 .

148. [ 21 ] Equation ( 66 ) bounds the maximum number of candidates using a formula that depends on the current depth d , but not on the total number of free variables. The same cutoffs are used in problems with any number of variables. Why is that a reasonable strategy?

▸ 149. [ 26 ] Devise a data structure that makes it convenient to tell whether a given variable x is a “ participant” in Algorithm L .

150. [ 24 ] Continue the text’s story of lookahead in waerden (3, 3; 9): What happens at depth d = 1 when l ← 7 and T ← 22 (see ( 70 )), after literal 4 has become proto true? (Assume that no double-lookahead is done.)

▸ 151. [ 26 ] The dependency digraph ( 68 ) has 16 arcs, only 8 of which are captured in the subforest ( 69 ). Show that, instead of ( 70 ), we could actually list the literals l and give them offsets o ( l ) in such a way that u appears before v in the list and has o ( u ) > o ( v ) if and only if v → u in ( 68 ). Thus we could capture all 16 dependencies via levels of truth.

152. [ 22 ] Give an instance of 3SAT for which no free “participants” are found in step X3, yet all clauses are satisfied. Also describe an efficient way to verify satisfaction.

153. [ 17 ] What’s a good way to weed out unwanted candidates in step X3, if C > C max ?

154. [ 20 ] Suppose we’re looking ahead with just four candidate variables, { a, b, c, d }, and that they’re related by three binary clauses ( a ∨ b ¯ ) ∧ ( a ∨ c ¯ ) ∧ ( c ∨ d ¯ ) . Find a subforest and a sequence of truth levels to facilitate lookaheads, analogous to ( 69 ) and ( 70 ).

155. [ 32 ] Sketch an efficient way to construct the lookahead forest in step X4.

156. [ 05 ] Why is a pure literal a special case of an autarky?

157. [ 10 ] Give an example of an autarky that is not a pure literal.

158. [ 15 ] If l is a pure literal, will Algorithm X discover it?

159. [ M17 ] True or false: (a) A is an autarky for F if and only if F | A ⊆ F . (b) If A is an autarky for F and A ′ ⊆ A , then A \ A ′ is an autarky for F | A ′.

160. [ 18 ] ( Black and white principle .) Consider any rule by which literals have been colored white, black, or gray in such a way that l is white if and only if l ¯ is black. (For example, we might say that l is white if it appears in fewer clauses than l ¯ .)

  1. Suppose every clause of F that contains a white literal also contains a black literal. Prove that F is satisfiable if and only if its all-gray clauses are satisfiable.

  2. Explain why this metaphor is another way to describe the notion of an autarky.

▸ 161. [ 21 ] ( Black and blue principle .) Now consider coloring literals either white, black, orange, blue, or gray, in such a way that l is white if and only if l ¯ is black, and l is orange if and only if l ¯ is blue. (Hence l is gray if and only if l ¯ is gray.) Suppose further that F is a set of clauses in which every clause containing a white literal also contains either a black literal or a blue literal (or both). Let A = { a 1 ,..., a p } be the black literals and let L = { l 1 ,..., l q } be the blue literals. Also let F ′ be the set of clauses obtained by adding p additional clauses ( l ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ l ¯ q ∨ a j ) to F , for 1 ≤ j ≤ p .

  1. Prove that F is satisfiable if and only if F ′ is satisfiable.

  2. Restate and simplify that result in the case that p = 1.

  3. Restate and simplify that result in the case that q = 1.

  4. Restate and simplify that result in the case that p = q = 1. (In this special case, ( l ¯ ∨ a ) is called a blocked binary clause .)

162. [ 21 ] Devise an efficient way to discover all of the (a) blocked binary clauses ( l ¯ ∨ a ) and (b) size-two autarkies A = { a , a ′} of a given k SAT problem F .

▸ 163. [ M25 ] Prove that the following recursive procedure R ( F ) will solve any n -variable 3SAT problem F with at most O ( ϕ n ) executions of steps R1, R2, or R3:

R1. [Check easy cases.] If F = Ø, return true. If Ø ∈ F , return false. Otherwise let { l 1 ,..., l s } ∈ F be a clause of minimum size s .

R2. [Check autarkies.] If s = 1 or if { l s } is an autarky, set F ← F | l s and return to R1. Otherwise if { l ¯ s , l s − 1 } is an autarky, set F ← F | l ¯ s , l s − 1 and return to R1.

R3. [Recurse.] If R ( F | l s ) is true, return true. Otherwise set F ← F | l ¯ s , s ← s − 1, and go back to R2.

164. [ M30 ] Continuing exercise 163 , bound the running time when F is k SAT.

▸ 165. [ 26 ] Design an algorithm to find the largest positive autarky A for a given F , namely an autarky that contains only positive literals. Hint : Warm up by finding the largest positive autarky for the clauses ( 12 3 ¯ , 12 5 ¯ , 1 ¯ 3 ¯ 4 ¯ , 13 6 ¯ , 1 4 ¯ 5 , 156 , 2 ¯ 35 , 2 4 ¯ 6 , 345 , 3 ¯ 56 } .

166. [ 30 ] Justify the operations of step X9. Hint : Prove that an autarky can be constructed, if w = 0 after ( 72 ) has been performed.

▸ 167. [ 21 ] Justify step X11 and the similar use of X12 in step X6.

168. [ 26 ] Suggest a way to choose the branch literal l in step L3, based on the heuristic scores H ( l ) that were compiled by Algorithm X in step L2. Hint : Experience shows that it’s good to have both H ( l ) and H ( l ¯ ) large.

▸ 169. [ HM30 ] (T. Ahmed, O. Kullmann.) Excellent results have been obtained in some problems when the branch variable in step L3 is chosen to minimize the quantity τ ( H ( l ), H ( l ¯ ) ), where τ ( a, b ) is the positive solution to τ − a + τ − b \= 1. (For example, τ (1, 2) = ϕ ≈ 1.62 and τ ( 2 , 2 ) \= 2 1 / 2 ≈ 1.63 , so we prefer (1, 2) to ( 2 , 2 ) .) Given a list of pairs of positive numbers ( a 1 , b 1 ), ... , ( a s , b s ), what’s an efficient way to determine an index j that minimizes τ ( a j , b j ), without computing logarithms?

170. [ 25 ] (Marijn Heule, 2013.) Show that Algorithm L solves 2SAT in linear time.

171. [ 20 ] What is the purpose of DFAIL in Algorithm Y ?

172. [ 21 ] Explain why ‘+ LO [ j ]’ appears in step Y2’s formula for DT .

173. [ 40 ] Use an implementation of Algorithm L to experiment with random 3SAT problems such as rand (3, 2062, 500, 314). Examine the effects of such things as (i) disabling double lookahead; (ii) disabling “wraparound,” by changing the cases j = S and j ^ \= S in X7 and Y4 so that they simply go to X6 and Y3; (iii) disabling the lookahead forest, by letting all candidate literals have null PARENT ; (iv) disabling compensation resolvents in step L9; (v) disabling “ windfalls” in ( 72 ); (vi) branching on a random free candidate l in L3, instead of using the H scores as in exercise 168 ; or (vii) disabling all lookahead entirely as in “ Algorithm L 0 .”

174. [ 15 ] What’s an easy way to accomplish (i) in the previous exercise?

175. [ 32 ] When Algorithm L is extended to nonternary clauses as in exercise 143 , how should Algorithms X and Y also change? (Instead of using ( 64 ) and ( 65 ) to compute a heuristic for preselection, use the much simpler formula in answer 146 . And instead of using h ( u ) h ( v ) in ( 67 ) to estimate the weight of a ternary clause that will be reduced to binary, consider a simulated reduced clause of size s ≥ 2 to have weight K s ≈ γ s −2 , where γ is a constant (typically 0.2).)

176. [ M25 ] The “ flower snark” J q is a cubic graph with 4 q vertices t j , u j , v j , w j , and 6 q edges t j — t j +1 , t j — u j , u j — v j , u j — w j , v j — w j +1 , w j — v j +1 , for 1 ≤ j ≤ q , with subscripts treated modulo q . Here, for example, are J 5 and its line graph L ( J 5 ):

images

  1. Give labels a j , b j , c j , d j , e j , and f j to the edges of J q , for 1 ≤ j ≤ q . (Thus a j denotes t j — t j +1 and b j denotes t j — u j , etc.) What are the edges of L ( J q )?

  2. Show that χ ( J q ) = 2 and χ ( L ( J q )) = 3 when q is even.

  3. Show that χ ( J q ) = 3 and χ ( L ( J q )) = 4 when q is odd. Note : Let fsnark ( q ) denote the clauses ( 15 ) and ( 16 ) that correspond to 3-coloring L ( J q ), together with ( b 1,1 ) ∧ ( c 1,2 ) ∧ ( d 1,3 ) to set the colors of ( b 1 , c 1 , d 1 ) to (1, 2, 3). These clauses make excellent benchmark tests for SAT solvers.

177. [ HM26 ] Let I q be the number of independent sets of the flower snark line graph L ( J q ). Compute I q for 1 ≤ q ≤ 8, and determine the asymptotic growth rate.

▸ 178. [ M23 ] When Algorithm B is presented with the unsatisfiable clauses fsnark ( q ) of exercise 176 , with q odd, its speed depends critically on the ordering of the variables.

Show that the ru nning time is Θ(2 q ) when the variables are considered in the order

a 1,1 a 1,2 a 1,3 b 1,1 b 1,2 b 1,3 c 1,1 c 1,2 c 1,3 d 1,1 d 1,2 d 1,3 e 1,1 e 1,2 e 1,3 f 1,1 f 1,2 f 1,3 a 2,1 a 2,2 a 2,3 . . . ;

but much, much more time is needed when the order is

a 1,1 b 1,1 c 1,1 d 1,1 e 1,1 f 1,1 a 2,1 b 2,1 c 2,1 d 2,1 e 2,1 f 2,1 . . . a q ,1 b q ,1 c q ,1 d q ,1 e q ,1 f q ,1 a 1,2 b 1,2 c 1,2 . . . .

179. [ 25 ] Show that there are exactly 4380 ways to fill the 32 cells of the 5-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 , 1 * 0 * 0 , * * 001 , * * 110 , * 010 * , * 110 * 0 * * 11 , 1 * * 11.

What does this fact tell you about the value of q 8 in Fig. 83 ?

▸ 180. [ 25 ] Explain how to use BDDs to compute the numbers Q m that underlie Fig. 83 . What is max 0≤ m ≤80 Q m ?

▸ 181. [ 25 ] Extend the idea of the previous exercise so that it is possible to determine the probability distributions T m of Fig. 84 .

182. [ M16 ] For which values of m in Fig. 84 does T m have a constant value?

183. [ M30 ] Discuss the relation between Figs. 85 and 86 .

184. [ M20 ] Why does ( 77 ) characterize the relation between q ^ m and q m ?

185. [ M20 ] Use ( 77 ) to prove the intuitively obvious fact that q ^ m ≥ q m .

186. [ M21 ] Use ( 77 ) to reduce Σ m q ^ m and Σ m ( 2 m + 1 ) q ^ m to ( 78 ) and ( 79 ).

187. [ M20 ] Analyze random satisfiability in the case k = n : What are S n,n and S ^ n , n ?

▸ 188. [ HM25 ] Analyze random 1SAT, the case k = 1: What are S 1, n and S ^ 1 , n ?

189. [ 27 ] Apply BDD methods to random 3SAT problems on 50 variables. What is the approximate BDD size after m distinct clauses have been ANDed together, as m grows?

190. [ M20 ] Exhibit a Boolean function of 4 variables that can’t be expressed in 3CNF. (No auxiliary variables are allowed: Only x 1 , x 2 , x 3 , and x 4 may appear.)

191. [ M25 ] How many Boolean functions of 4 variables can be expressed in 3CNF?

▸ 192. [ HM21 ] Another way to model satisfiability when there are N equally likely clauses is to study S ( p ), the probability of satisfiability when each clause is independently present with probability p .

  1. Express S ( p ) in terms of the numbers Q m \= ( N m ) q m .

  2. Assign uniform random numbers in [0 .. 1) to each clause; then at time t , for 0 ≤ t ≤ N , consider all clauses that have been assigned a number less than t/N . (Approximately t clauses will therefore be selected, when N is large.) Show that S ¯ k , n \= ∫ 0 N S k , n ( t / N ) d t , the expected amount of time during which the chosen clauses remain satisfiable, is very similar to the satisfiability threshold S k,n of ( 76 ).

193. [ HM48 ] Determine the satisfiability threshold ( 81 ) of random 3SAT. Is it true that lim inf n →∞ S 3, n / n = lim sup n →∞ S 3, n / n ? If so, is the limit ≈ 4.2667?

194. [ HM49 ] If α < lim inf n →∞ S 3, n / n , is there a polynomial-time algorithm that is able to satisfy ⌊ αn ⌋ random 3SAT clauses with probability ≥ δ , for some δ > 0?

195. [ HM21 ] (J. Franco and M. Paull, 1983.) Use the first moment principle MPR–(21) to prove that ⌊(2 k ln 2) n ⌋ random k SAT clauses are almost always unsatisfiable. Hint : Let X = ∑ x [ x satisfies all clauses], summed over all 2 n binary vectors x = x 1 ... x n .

▸ 196. [ HM25 ] (D. B. Wilson.) A clause of a s atisfiabi lity problem is “ easy” if it contains one or more variables that don’t appear in any other clauses. Prove that, with probability 1 − O ( n −2 ∈ ), a k SAT problem that has m = ⌊ αn ⌋ random clauses contains (1 − (1 − e − kα ) k ) m + O ( n 1/2+ ∈ ) easy ones. (For example, about 0.000035 n of the 4.27 n clauses in a random 3SAT problem near the threshold will be easy.)

197. [ HM21 ] Prove that the quotient q ( a , b , A , B , n ) \= ( ( a + b ) n a n ) ( ( A + B ) n A n ) / ( ( a + b + A + B ) n ( a + A ) n ) is O ( n −1/2 ) as n → ∞, if a, b, A, B > 0.

▸ 198. [ HM30 ] Use exercises 196 and 197 to show that the phase transition in Fig. 89 is not extremely abrupt: If S 3 ( m , n ) > 2 3 and S 3 ( m ′ , n ) < 1 3 , prove that m ′ \= m + Ω ( n ) .

199. [ M21 ] Let p ( t, m, N ) be the probability that t specified letters each occur at least once within a random m -letter word on an N -letter alphabet.

  1. Prove that p ( t , m , N ) ≤ m t ¯ / N t .

  2. Derive the exact formula p ( t , m , N ) \= Σ k ( t k ) ( − 1 ) k ( N − k ) m / N m .

  3. And p ( t , m , N ) / t ! \= ( t t ) ( m t ) / N t − { t + 1 t } ( m t + 1 ) / N t + 1 + { t + 2 t } ( m t + 2 ) / N t + 2 − … .

▸ 200. [ M21 ] Complete the text’s proof of ( 84 ) when c < 1:

  1. Show that every unsatisfiable 2SAT formula contains clauses of a snare.

  2. Conversely, are the clauses of a snare always unsatisfiable?

  3. Verify the inequality ( 89 ). Hint : See exercise 199 .

201. [ HM29 ] The t - snake clauses specified by a chain ( l 1 ,..., l 2 t −1 ) can be written ( l ¯ i ∨ l i + 1 ) for 0 ≤ i < 2 t , where l 0 \= l ¯ t and subscripts are treated mod 2 t .

  1. Describe all ways to set two of the l ’s so that ( x ¯ 1 ∨ x 2 ) is one of those 2 t clauses.

  2. Similarly, set three l ’s in order to obtain ( x ¯ 1 ∨ x 2 ) and ( x ¯ 2 ∨ x 3 ) .

  3. Also set three to obtain both ( x ¯ 0 ∨ x 1 ) and ( x ¯ t − 1 ∨ x t ) ; here x ¯ 0 ≡ x t and t > 2.

  4. How can the clauses ( x ¯ i ∨ x i + 1 ) for 0 ≤ i < t all be obtained by setting t of the l ’s?

  5. In general, let N ( q, r ) be the number of ways to choose r of the standard clauses ( x ¯ i ∨ x i + 1 ) , which involve exactly q of the variables { x 1 ,..., x 2 t −1 }, and to set q values of { l 1 ,..., l 2 t −1 } in order to obtain the r chosen clauses. Evaluate N (2, 1).

  6. Similarly, evaluate N (3, 2), N ( t, t ), and N (2 t − 1, 2 t ).

  7. Show that the probability p r in ( 95 ) is ≤ Σ q N ( q , r ) / ( 2 q n q ¯ ) .

  8. Therefore the upper bound ( 96 ) is valid.

202. [ HM21 ] This exercise amplifies the text’s proof of Theorem C when c > 1.

  1. Explain the right-hand side of Eq. ( 93 ).

  2. Why does ( 97 ) follow from ( 95 ), ( 96 ), and the stated choices of t and m ?

▸ 203. [ HM33 ] (K. Xu and W. Li, 2000.) Beginning with the n graph-coloring clauses ( 15 ), and optionally the n ( d 2 ) exclusion clauses ( 17 ), consider using randomly generated binary clauses instead of ( 16 ). There are mq random binary clauses, obtained as m independent sets of q clauses each, where every such set is selected by choosing distinct vertices u and v , then choosing q distinct binary clauses ( u ¯ i ∨ v ¯ j ) for 1 ≤ i, j ≤ d . (The number of different possible sequences of random clauses is therefore exactly ( ( n 2 ) ( d 2 q ) ) m and each sequence is equally likely.) This method of clause generation is known as “ Model RB”; it generalizes random 2SAT, which is the case d = 2 and q = 1.

Suppose d = n α and q = pd 2 , where we require 1 2 < α < 1 and 0 ≤ p ≤ 1 2 . Also let m = rn ln d . For this range of the parameters, we will prove that there is a sharp threshold of satisfiability: The clauses are unsatisfiable q.s., as n → ∞, if r ln(1 − p ) + 1 < 0; but they are satisfiable a.s. if r ln(1 − p ) + 1 > 0.

Let X ( j 1 ,..., j n ) = [all clauses are satisfied when each i th variable v has v j i \= 1 ]; here 1 ≤ j 1 ,..., j n ≤ d . Also let X \= Σ 1 ≤ j 1 , ... , j n ≤ d X ( j 1 , ... , j n ) . Then X = 0 if and only if the clauses are unsatisfiable.

  1. Use the first moment principle to prove that X = 0 q.s. when r ln(1 − p )+1 < 0.

  2. Find a formula for p s = Pr( X ( j 1 ,..., j n ) = 1 | X (1,..., 1) = 1), given that exactly s of the colors { j 1 ,..., j n } are equal to 1.

  3. Use (b) and the conditional expectation inequality MPR–(24) to prove that X > 0 a.s. if

    Σ s \= 0 n ( n s ) ( 1 d ) s ( 1 − 1 d ) n − s ( 1 + p 1 − p s 2 n 2 ) m → 1 as   n → ∞ .

  4. Letting t s denote the term for s in that sum, prove that Σ s \= 0 3 n / d t s ≈ 1 .

  5. Suppose r ln(1 − p ) + 1 = ∈ > 0, where ∈ is small. Show that the terms t s first increase, then decrease, then increase, then decrease again, as s grows from 0 to n . Hint: Consider the ratio x s = t s +1 / t s .

  6. Finally, prove that t s is exponentially small for 3 n/d ≤ s ≤ n .

▸ 204. [ 28 ] Figure 89 might suggest that 3SAT problems on n variables are always easy when there are fewer than 2 n clauses. We shall prove, however, that any set of m ternary clauses on n variables can be transformed mechanically into another set of ternary clauses on N = O ( m ) variables in which no variable occurs more than four times. The transformed problem has the same number of solutions as the original one; thus it isn’t any simpler, although (with at most 4 N literals) it has at most 4 3 N clauses.

  1. First replace the original m clauses by m new clauses ( X 1 ∨ X 2 ∨ X 3 ), ... ,( X 3 m −2 ∨ X 3 m −1 ∨ X 3 m ), on 3 m new variables, and show how to add 3 m clauses of size 2 so that the resulting 4 m clauses have exactly as many solutions as the original.

  2. Construct ternary clauses that have a unique solution, yet no variable occurs more than four times.

  3. Use (a) and (b) to prove the N -variable result claimed above.

205. [ 26 ] If F and F ′ are sets of clauses, let F ⊔ F ′ stand for any other set obtained from F ∪ F ′ by replacing one or more clauses C of F by x ∨ C and one or more clauses C ′ of F ′ by x ¯ ∨ C ′ , where x is a new variable. Then F ⊔ F ′ is unsatisfiable whenever F and F ′ are both unsatisfiable. For example, if F = {∈}> and F ′ \= { 1 , 1 ¯ } , then F ⊔ F ′ is either { 2 , 1 2 ¯ , 1 ¯ 2 ¯ } or { 2 , 1 , 1 ¯ 2 ¯ } or { 2 , 1 2 ¯ , 1 ¯ } .

  1. Construct 16 unsatisfiable ternary clauses on 15 variables, where each variable occurs at most four times.

  2. Construct an unsatisfiable 4SAT problem in which every variable occurs at most five times.

206. [ M22 ] A set of clauses is minimally unsatisfiable if it is unsatisfiable, yet becomes satisfiable if any clause is deleted. Show that, if F and F ′ have no variables in common, then F⊔F ′ is minimally unsatisfiable if and only if F and F ′ are minimally unsatisfiable.

207. [ 25 ] Each of the literals { 1 , 1 ¯ , 2 , 2 ¯ , 3 , 3 ¯ , 4 , 4 ¯ } occurs exactly thrice in the eight unsatisfiable clauses ( 6 ). Construct an unsatisfiable 3SAT problem with 15 variables in which each of the 30 literals occurs exactly twice . Hint: Consider { 1 ¯ 2 , 2 ¯ 3 , 3 ¯ 1 , 123 , 1 ¯ 2 ¯ 3 ¯ } .

208. [ 25 ] Via exercises 204(a) and 207 , show that any 3SAT problem can be transformed into an equivalent set of ternary clauses where every literal occurs just twice.

209. [ 25 ] (C. A. Tovey.) Prove that every k SAT formula in which no variable occurs more than k times is satisfiable. (Thus the limits on occurrences in exercises 204 – 208 cannot be lowered, when k = 3 and k = 4.) Hint: Use the theory of bipartite matching.

210. [ M36 ] But the result in the previous exercise can be improved when k is large. Use the Local Lemma to show that every 7SAT problem with at most 13 occurrences of each variable is satisfiable.

211. [ 30 ] (R. W. Irving and M. Jerrum, 1994.) Use exercise 208 to reduce 3SAT to the problem of list coloring a grid graph of the form K N □ K 3 . (Hence the latter problem, which is also called latin rectangle construction , is NP- complete.)

212. [ 32 ] Continuing the previous exercise, we shall reduce grid list coloring to another interesting problem called partial latin square construction . Given three n × n binary matrices ( r ik ), ( c jk ), ( p ij ), the task is to construct an n × n array ( X ij ) such that X ij is blank when p ij = 0, otherwise X ij = k for some k with r ik = c jk = 1; furthermore the nonblank entries must be distinct in each row and column.

  1. Show that this problem is symmetrical in all three coordinates: It’s equivalent to constructing a binary n × n × n tensor ( x ijk ) such that x ∗ jk = c jk , x i ∗ k = r ik , and x ij ∗ = p ij , for 1 ≤ i, j, k ≤ n , where ‘∗’ denotes summing an index from 1 to n . (Therefore it is also known as the binary n × n × n contingency problem , given n 2 row sums, n 2 column sums, and n 2 pile sums.)

  2. A necessary condition for solution is that c ∗ k = r ∗ k , c j ∗ = p ∗ j , and r i ∗ = p i ∗ . Exhibit a small example where this condition is not sufficient.

  3. If M < N , reduce K M □ K N list coloring to the problem of K N □ K N list coloring.

  4. Finally, explain how to reduce K N □ K N list coloring to the problem of constructing an n × n partial latin square, where n = N + Σ I,J | L ( I, J )|. Hint: Instead of considering integers 1 ≤ i, j, k ≤ n , let i , j , k range over a set of n elements. Define p ij = 0 for most values of i and j ; also make r ik = c ik for all i and k .

▸ 213. [ M20 ] Experience with the analyses of sorting algorithms in Chapter 5 suggests that random satisfiability problems might be modeled nicely if we assume that, in each of m independent clauses, the literals x j and x ¯ j occur with respective probabilities p and q , independently for 1 ≤ j ≤ n , where p + q ≤ 1. Why is this not an interesting model as n → ∞, when p and q are constant? Hint: What is the probability that x 1 ... x n = b 1 ... b n satisfies all of the clauses, when b 1 ... b n is a given binary vector?

214. [ HM38 ] Although the random model in the preceding exercise doesn’t teach us how to solve SAT problems, it does lead to interesting mathematics: Let 0 < p < 1 and consider the recurrence

T 0 \= 0 ; T n \= n + 2 Σ k \= 0 n − 1 ( n k ) p k ( 1 − 9 ) n − k T k , for   n > 0.

  1. Find a functional relation satisfied by T ( z ) \= Σ n \= 0 ∞ T n z n / n ! .

  2. Deduce that we have T ( z ) \= z e z Σ m \= 0 ∞ ( 2 p ) m Π k \= 0 m − 1 ( 1 − e − p k ( 1 − p ) z )

  3. Hence, if p ≠ 1/2, we can use Mellin transforms (as in the derivation of 5.2.2–(50)) to show that T n = C p n α (1 + δ( n ) + O (1 /n )) + n/ (1 − 2 p ), where α = 1 / lg(1 /p ), C p is a constant, and δ is a small “ wobble” with δ ( n ) = δ ( pn ).

▸ 215. [ HM28 ] What is the expected profile of the search tree when a simple backtrack procedure is used to find all solutions to a random 3SAT problem with m independent clauses on n variables? (There is a node on level l for every partial solution x 1 ... x l that doesn’t contradict any of the clauses.) Compute these values when m = 200 and n = 50. Also estimate the total tree size when m = αn , for fixed α as n → ;∞.

216. [ HM38 ] (P. W. Purdom, Jr., and C. A. Brown.) Extend the previous exercise to a more sophisticated kind of backtracking, where all choices forced by unit clauses are pursued before two-way branching is done. (The “ pure literal rule” is not exploited, however, because it doesn’t find all solutions.) Prove that the expected tree size is greatly reduced when m = 200 and n = 50. (An upper bound is sufficient.)

217. [ 20 ] True or false: If A and B are arbitrary clauses that are simultaneously satisfiable, and if l is any literal, then the clause C \= ( A ∪ B ) ∖ { l , l ¯ } is also satisfiable. (We’re thinking here of A , B , and C as sets of literals, not as disjunctions of literals.)

218. [ 20 ] Express the formula ( x ∨ A ) ∧ ( x ¯ ∨ B ) in terms of the ternary operator u ? v : w .

▸ 219. [ M20 ] Formulate a general definition of the resolution operator C = C ′ ◊ C ″ that (i) agrees with the text’s definition when C ′ = x ∨ A ′ and C " \= x ¯ ∨ A " ; (ii) applies to arbitrary clauses C′ and C ″; (iii) has the property that C ′ ∧ C ″ implies C ′ ◊ C ″.

220. [ M24 ] We say that clause C subsumes clause C ′, written C ⊆ C ′, if C = ℘ or if C ′ ≠ ℘ and every literal of C appears in C ′.

  1. True or false: C ⊆ C ′ and C ′ ⊆ C ″ implies C ⊆ C ″.

  2. True or false: ( C ∨ α) ◊ ( C ′ ∨ α′) ⊆ ( C ◊ C ′) ∨ α ∨ α′, with ◊ as in exercise 219 .

  3. True or false: C ′ ⊆ C ″ implies C ◊ C ′ ⊆ C ◊ C ″.

  4. The notation C 1 ,..., C m ⊢ C means that a resolution chain C 1 ,..., C m+r exists with C m+r ⊆ C , for some r ≥ 0. Show that we might have C 1 ,..., C m ⊢ C even though C cannot be obtained from { C 1 ,..., C m } by successive resolutions ( 104 ).

  5. Prove that if C 1 ⊆ C 1 ′ , ⋅ ⋅ ⋅ , C m ⊆ C m ′ , and C 1 ′ , ... , C m ′ ⊢ C , then C 1 ,..., C m ⊢ C.

  6. Furthermore C 1 ,..., C m ⊢ C implies C 1 ∨ α 1 ,..., C m ∨ α m ⊢ C ∨ α 1 ∨ ··· ∨ α m .

221. [ 16 ] Draw the search tree analogous to Fig. 81 that is implicitly traversed when Algorithm A is applied to the unsatisfiable clauses { 12 , 2 , 2 ¯ } . Explain why it does not correspond to a resolution refutation that is analogous to Fig. 91 .

222. [ M30 ] (Oliver Kullmann, 2000.) Prove that, for every clause C in a satisfiability problem F , there is an autarky satisfying C if and only if C cannot be used as the label of a source vertex in any resolution refutation of F .

223. [ HM40 ] Step X9 deduces a binary clause that cannot be derived by resolution (see exercise 166 ). Prove that, nevertheless, the running time of Algorithm L on unsatisfiable input will never be less than the length of a shortest treelike refutation.

224. [ M20 ] Given a resolution tree that refutes the axioms F | x ¯ , show how to construct a resolution tree of the same size that either refutes the axioms F or derives the clause { x } from F without resolving on the variable x .

▸ 225. [ M31 ] (G. S. Tseytin, 1966.) If T is any resolution tree that refutes a set of axioms F , show how to convert it to a regular resolution tree T r that refutes F , where T r is no larger than T .

226. [ M20 ] If α is a node in a refutation tree, let C ( α ) be its label, and let ∥ α ∥ denote the number of leaves in its subtree. Show that, given a refutation tree with N leaves, the Prover can find a node with ∥ α ∥ ≤ N/ 2 s for which the current assignment falsifies C ( α ), whenever the Delayer has scored s points in the Prover–Delayer game.

227. [ M27 ] Given an extended binary tree, exercise 7.2.1.6–124 explains how to label each node with its Horton–Strahler number. For example, the nodes at depth 2 in Fig. 91 are labeled 1, because their children have the labels 1 and 0; the root is labeled 3.

Prove that the maximum score that the Delayer can guarantee, when playing the Prover–Delayer game for a set of unsatisfiable clauses F , is equal to the minimum possible Horton–Strahler root label in a tree refutation of F .

▸ 228. [ M21 ] Stålmarck’s refutation of ( 99 )–( 101 ) actually obtains ∊ without using all of the axioms! Show that only about 1/3 of those clauses are sufficient for unsatisfiability.

▸ 229. [ M21 ] Continuing exercise 228 , prove also that the set of clauses ( 99 ), (100′), ( 101 ) is unsatisfiable, where ( 100 ′) denotes ( 100 ) restricted to the cases i ≤ k and j < k .

230. [ M22 ] Show that the clauses with i ≠ j in the previous exercise form a minimal unsatisfiable set: Removing any one of them leaves a satisfiable remainder.

231. [ M30 ] (Sam Buss.) Refute the clauses of exercise 229 with a resolution chain of length O ( m 3 ). Hint: Derive the clauses G ij = ( x ij ∨ x i ( j +1) ∨···∨ x im ) for 1 ≤ i ≤ j ≤ m .

▸ 232. [ M28 ] Prove that the clauses fsnark ( q ) of exercise 176 can be refuted by treelike resolution in O ( q 6 ) steps.

233. [ 16 ] Explain why ( 105 ) satisfies ( 104 ), by exhibiting j ( i ) and k ( i ) for 9 ≤ i ≤ 22.

234. [ 20 ] Show that the Delayer can score at least m points against any Prover who tries to refute the pigeonhole clauses ( 106 ) and ( 107 ).

▸ 235. [ 30 ] Refute those pigeonhole clauses with a chain of length m ( m +3)2 m −2 .

236. [ 48 ] Is the chain in the previous exercise as short as possible?

▸ 237. [ 28 ] Show that a polynomial number of steps suffice to refute the pigeonhole clauses ( 106 ), ( 107 ), if the extended resolution trick is used to append new clauses.

238. [ HM21 ] Complete the proof of Lemma B. Hint: Make r ≤ ρ − b when W = b .

▸ 239. [ M21 ] What clauses α 0 on n variables make ∥ α 0 ⊢ ∊∥ as large as possible?

240. [ HM23 ] Choose integers f ij ∊ {1 ,...,m } uniformly at random, for 1 ≤ i ≤ 5 and 0 ≤ j ≤ m , and let G 0 be the bipartite graph with edges a j −− b k if and only if k ∊ {f 1 j ,..., f 5 j }. Show that Pr( G 0 satisfies the strong expansion condition ( 108 )) ≥ 1/2.

241. [ 20 ] Prove that any set of at most m/ 3000 pigeons can be matched to distinct holes, under the restricted pigeonhole constraints G 0 of Theorem B.

242. [ M20 ] The pigeonhole axioms ( 106 ) and ( 107 ) are equivalent to the clauses ( 15 ) and ( 16 ) that arise if we try to color the complete graph K m +1 with m colors.

Suppose we include further axioms corresponding to ( 17 ), namely

( x ¯ j k ∨ x ¯ j k ′ ) . for 0 ≤ j ≤ m ⁢ and 1 ≤ k < k ′ ≤ m .

Does Theorem B still hold, or do these additional axioms decrease the refutation width?

243. [ HM31 ] (E. Ben-Sasson and A. Wigderson.) Let F be a set of ⌊ αn ⌋ random 3SAT clauses on n variables, where α > 1 /e is a given constant. For any clause C on those variables, define μ ( C ) = min{ | F ′| | F ′ ⊆ F and F ′ ⊢ C }. Also let V ( F ′) denote the variables that occur in a given family of clauses F ′.

  1. Prove that | V ( F ′)| ≥ | F ′| a.s., when F ′ ⊆ F and | F ′| ≤ n/ (2 αe 2 ).

  2. Therefore either F is satisfiable or μ ( ∊ ) > n/ (2 αe 2 ), a.s.

  3. Let n ′ = n/ (1000000 α 4 ), and assume that n ′ ≥ 2. Prove that 2| V ( F ′)|−3| F ′| ≥ n ′/4 q.s., when F ′ ⊆ F and n ′ / 2 ≤ | F ′| < n ′.

  4. Consequently either F is satisfiable or w ( F ⊢ ∊ ) ≥ n ′ / 4, a.s.

244. [ M20 ] If A is a set of variables, let [ A ] 0 or [ A ] 1 stand for the set of all clauses that can be formed from A with an even or odd number of negative literals, respectively; each clause should involve all of the variables. (For example, [{1 , 2 , 3}] 1 = { 12 3 ¯ , 1 2 ¯ 3 , 1 ¯ 23 , 1 ¯ 2 ¯ 3 ¯ } .) If A and B are disjoint, express [ A ∪ B ] 0 in terms of the sets [ A ] 0 , [ A ] 1 , [ B ] 0 , [ B ] 1 .

▸ 245. [ M27 ] Let G be a connected graph whose vertices v ∊ V have each been labeled 0 or 1, where the sum of all labels is odd. We will construct clauses on the set of variables e uv , one for each edge u −− v in G . The axioms are α ( v ) = [ E ( v )] l (v)⊕1 for each v ∊ V (see exercise 244 ), where E ( v ) = { e uv | u −− v } and l ( v ) is the label of v .

For example, vertex 1 of the graph below is shown as a black dot in order to indicate that l (1) = 1, while the other vertices appear as white dots and are labeled l (2) = ··· = l (6) = 0. The graph and its axioms are

images

Notice that, when v has d > 0 neighbors in G , the set α ( v ) consists of 2 d −1 clauses of size d . Furthermore, the axioms of α ( v ) are all satisfied if and only if

images

If we sum this equation over all vertices v , mod 2, we get 0 on the left, because each edge e uv occurs exactly twice (once in E ( u ) and once in E ( v )). But we get 1 on the right. Therefore the clauses α ( G ) = ∪ v α( v ) are unsatisfiable.

  1. The axioms α ( G ) | b and α ( G ) ∣ b ¯ in this example turn out to be α ( G ′) and α ( G ″), where images and images . Explain what happens in general.

  2. Let μ ( C ) = min{ | V ′| | V ′ ⊆ V and ∪ v∊V , α ( v ) ⊢ C }, for every clause C involving the variables e uv . Show that μ ( C ) = 1 for every axiom C ∊ α ( G ). What is μ (∊)?

  3. If V ′ ⊆ V , let ∂V ′ = { e uv | u ∊ V ′ and v ∉ V ′}. Prove that, if ∪ v∊V , α ( v ) ⊢ C and | V ′| = μ ( C ), every variable of ∂V ′ appears in C .

  4. A nonbipartite cubic Ramanujan graph G on m vertices V has three edges v −− vρ , v −− vσ , v −− vτ touching each vertex, where ρ , σ , and τ are permutations with the following properties: (i) ρ = ρ − and τ = σ − ; (ii) G is connected; (iii) if V ′ is any subset of s vertices, and if there are t edges between V ′ and V \ V ′, then we have s/ ( s + t ) ≤ ( s/m + 8)/9. Prove that w ( α ( G ) ⊢ ∊ ) > m/ 78.

▸ 246. [ M28 ] (G. S. Tseytin.) Given a labeled graph G with m edges, n vertices, and N unsatisfiable clauses α ( G ) as in the previous exercise, explain how to refute those clauses with O ( mn + N ) steps of extended resolution.

247. [ 18 ] Apply variable elimination to just five of the six clauses ( 112 ), omitting ‘ 1 2 ¯ ’.

248. [ M20 ] Formally speaking, SAT is the problem of evaluating the quantified formula

∃ x 1 ··· ∃ x n−1 ∃ x n F ( x 1 , …, x n−1 , x n ),

where F is a Boolean function given in CNF as a conjunction of clauses. Explain how to transform the CNF for F into the CNF for F ′ in the reduced problem

∃ x 1 ··· ∃ x n − 1 F ′ ( x 1 ,..., x n − 1 ) , F ′ ( x 1 ,..., x n − 1 ) = F ( x 1 ,..., x n − 1 , 0)∨ F ( x 1 ,..., x n − 1 , 1).

249. [ 18 ] Apply Algorithm I to ( 112 ) using Cook’s Method IA.

250. [ 25 ] Since the clauses R ′ in ( 7 ) are satisfiable, Algorithm I might discover a solution without ever reaching step I4. Try, however, to make the choices in steps I2, I3, and I4 so that the algorithm takes as long as possible to discover a solution.

▸ 251. [ 30 ] Show that Algorithm I can prove the unsatisfiability of the anti-maximal-element clauses ( 99 )–( 101 ) by making O ( m 3 ) resolutions, if suitably clairvoyant choices are made in steps I2, I3, and I4.

252. [ M26 ] Can the unsatisfiability of ( 99 )–( 101 ) be proved in polynomial time by repeatedly performing variable elimination and subsumption?

▸ 253. [ 18 ] What are the next two clauses learned if decision ‘5’ follows next after ( 114 )?

254. [ 16 ] Given the binary clauses { 12 , 1 ¯ 3 , 2 3 ¯ , 2 ¯ 4 ¯ , 3 ¯ 4 } , what clause will a CDCL solver learn first if it begins by deciding that 1 is true?

▸ 255. [ 20 ] Construct a satisfiability problem with ternary clauses, for which a CDCL solver that is started with decision literals ‘1’, ‘2’, ‘3’ on levels 1, 2, and 3 will learn the clause ‘45’ after a conflict on level 3.

256. [ 20 ] How might the clause ‘∗∗’ in Table 3 have been easily learned?

▸ 257. [ 30 ] (Niklas Sörensson.) A literal l ¯ is said to be redundant , with respect to a given clause c and the current trail, if l is in the trail and either (i) l is defined at level 0, or (ii) l is not a decision literal and every false literal in l ’s reason is either in c or (recursively) redundant. (This definition is stronger than the special cases by which ( 115 ) reduces to ( 116 ), because l ¯ itself needn’t belong to c .) If, for example, c \= ( l ¯ ′ ∨ b ¯ 1 ∨ b ¯ 2 ∨ b ¯ 3 ∨ b ¯ 4 ) , let the reason for b 4 be ( b 4 ∨ b ¯ 1 ∨ a ¯ 1 ) , where the reason for a 1 is ( a 1 ∨ b ¯ 2 ∨ a ¯ 2 ) and the reason for a 2 is ( a 2 ∨ b ¯ 1 ∨ b ¯ 3 ) . Then b ¯ 4 is redundant, because ā 2 and ā 1 are redundant.

  1. Suppose c \= ( l ¯ ′ ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r ) is a newly learned clause. Prove that if b ¯ j ∈ c is redundant, some other b ¯ i ∈ c became false on the same level of the trail as b ¯ j did.

  2. Devise an efficient algorithm that discovers all of the redundant literals b ¯ j in a given newly learned clause c \= ( l ¯ ′ ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r ) . Hint: Use stamps.

258. [ 21 ] A non-decision literal l in Algorithm C ’s trail always has a reason R l = ( l 0 ∨ l 1 ∨ ··· ∨ l k − 1 ), where l 0 = l and l ¯ 1 , ... ,   l ¯ k − 1 precede l in the trail. Furthermore, the algorithm discovered this clause while looking at the watch list of l 1 . True or false: l ¯ 2 , ... ,   l ¯ k − 1 precede l ¯ 1 in the trail. Hint: Consider Table 3 and its sequel.

259. [ M20 ] Can ACT ( j ) exceed ACT ( k ) for values of ρ near 0 or 1, but not for all ρ ?

260. [ 18 ] Describe in detail step C1’s setting-up of MEM , the watch lists, and the trail.

261. [ 21 ] The main loop of Algorithm C is the unit-propagation process of steps C3 and C4. Describe the low-level details of link adjustment, etc., to be done in those steps.

262. [ 20 ] What low-level operations underlie changes to the heap in steps C6–C8?

263. [ 24 ] Write out the gory details by which step C7 constructs a new clause and step C9 puts it into the data structures of Algorithm C .

264. [ 20 ] Suggest a way by which Algorithm C could indicate progress by displaying “ move codes” analogous to those of Algorithms A , B , D , and L . (See exercise 142 .)

265. [ 21 ] Describe several circumstances in which the watched literals l 0 and/or l 1 of a clause c actually become false during the execution of Algorithm C .

266. [ 20 ] In order to keep from getting into a rut, CDCL solvers are often designed to make decisions at random, with a small probability p (say p = .02), instead of always choosing a variable of maximum activity. How would this policy change step C6?

▸ 267. [ 25 ] Instances of SAT often contain numerous binary clauses, which are handled efficiently by the unit-propagation loop ( 62 ) of Algorithm L but not by the corresponding loop in step C3 of Algorithm C . (The technique of watched literals is great for long clauses, but it is comparatively cumbersome for short ones.) What additional data structures will speed up Algorithm C ’s inner loop, when binary clauses are abundant?

268. [ 21 ] When Algorithm C makes a literal false at level 0 of the trail, we can remove it from all of the clauses. Such updating might take a long time, if we did it “ eagerly”; but there’s a lazy way out: We can delete a permanently false literal if we happen to encounter it in step C3 while looking for a new literal to watch (see exercise 261 ).

Explain how to adapt the MEM data structure conventions so that such deletions can be done in situ, without copying clauses from one location into another.

269. [ 23 ] Suppose Algorithm C reaches a conflict at level d of the trail, after having chosen the decision literals u 1 , u 2 , ... , u d . Then the “ trivial clause” ( l ¯ ′ ∨ u ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ u ¯ d ′ ) must be true if the given clauses are satisfiable, where l ′and d ′ are defined in step C7.

  1. Show that, if we start with the clause ( l ¯ ′ ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r ) that is obtained in step C7 and then resolve it somehow with zero or more known clauses, we can always reach a clause that subsumes the trivial clause.

  2. Sometimes, as in ( 115 ), the clause that is slated to be learned in step C9 is much longer than the trivial clause. Construct an example in which d = 3, d ′= 1, and r = 10, yet none of b ¯ 1 , ... , b ¯ r are redundant in the sense of exercise 257 .

  3. Suggest a way to improve Algorithm C accordingly.

270. [ 25 ] ( On-the-fly subsumption. ) The intermediate clauses that arise in step C7, immediately after resolving with a reason R l , occasionally turn out to be equal to the shorter clause R l \ l . In such cases we have an opportunity to strengthen that clause by deleting l from it, thus making it potentially more useful in the future.

  1. Construct an example where two clauses can each be subsumed in this way while resolving a single conflict. The subsumed clauses should both contain two literals assigned at the current level in the trail, as well as one literal from a lower level.

  2. Show that it’s easy to recognize such opportunities, and to strengthen such clauses efficiently, by modifying the steps of answer 263 .

▸ 271. [ 25 ] The sequence of learned clauses C 1 , C 2 , ... often includes cases where C i subsumes its immediate predecessor, C i − 1 . In such cases we might as well discard C i − 1 , which appears at the very end of MEM, and store C i in its place, unless C i − 1 is still in use as a reason for some literal on the trail. (For example, more than 8,600 of the 52,000 clauses typically learned from waerden (3, 10; 97) by Algorithm C can be discarded in this way. Such discards are different from the on-the-fly subsumptions considered in exercise 270 , because the subsumed C i − 1 includes only one literal from its original conflict level; furthermore, learned clauses have usually been significantly simplified by the procedure of exercise 257 , unless they’re trivial.)

Design an efficient way to discover when C i − 1 can be safely discarded.

272. [ 30 ] Experiment with the following idea: The clauses of waerden ( j, k ; n ) are symmetrical under reflection, in the sense that they remain unchanged overall if we replace x k by x k R \= x n + 1 − k for 1 ≤ k ≤ n . Therefore, whenever Algorithm C learns a clause C \= ( l ¯ ′ ∨ b ¯ 1 ∨ ⋅ ⋅ ⋅ ∨ b ¯ r ) , it is also entitled to learn the reflected clause C R \= ( l ¯ ′ R ∨ b ¯ 1 R ∨ ⋅ ⋅ ⋅ ∨ b ¯ r R ) .

273. [ 27 ] A clause C that is learned from waerden ( j, k ; n ) is valid also with respect to waerden ( j, k ; n ′) when n ′ > n ; and so are the clauses C + i that are obtained by adding i to each literal of C , for 1 ≤ i ≤ n ′ − n . For example, the fact that ‘35’ follows from waerden (3, 3; 7) allows us to add the clauses 35, 46, 57 to waerden (3, 3; 9).

  1. Exploit this idea to speed up the calculation of van der Waerden numbers.

  2. Explain how to apply it also to bounded model checking.

274. [ 35 ] Algorithm C sets the “ reason” for a literal l as soon as it notices a clause that forces l to be true. Later on, other clauses that force l are often encountered, in practice; but Algorithm C ignores them, even though one of them might be a “ better reason.” (For example, another forcing clause might be significantly shorter.) Explore a modification of Algorithm C that tries to improve the reasons of non-decision literals.

▸ 275. [ 22 ] Adapt Algorithm C to the problem of finding the lexicographically smallest solution to a satisfiability problem, by incorporating the ideas of exercise 109 .

276. [ M15 ] True or false: If F is a family of clauses and L is a set of strictly distinct literals, then F ∧ L ⊢ 1 ∊ if and only if ( F | L ) ⊢ 1 ∊.

277. [ M18 ] If ( C 1 ,..., C t ) is a certificate of unsatisfiability for F , and if all clauses of F have length ≥ 2, prove that some C i is a unit clause.

278. [ 22 ] Find a six-step certificate of unsatisfiability for waerden (3, 3; 9).

279. [ M20 ] True or false: Every unsatisfiable 2SAT problem has a certificate ‘( l, ∊)’.

▸ 280. [ M26 ] The problem cook ( j, k ) consists of all ( n j ) positive j -clauses and all ( n k ) negative k -clauses on {1,..., n }, where n = j + k − 1. For example, cook (2, 3) is

{ 12 , 13 , 14 , 23 , 24 , 34 , 1 ¯ 2 ¯ 3 ¯ , 1 ¯ 2 ¯ 4 ¯ , 1 ¯ 3 ¯ 4 ¯ , 2 ¯ 3 ¯ 4 ¯ }

  1. Why are these clauses obviously unsatisfiable?

  2. Find a totally positive certificate for cook ( j, k ), of length ( n − 1 j − 1 ) .

  3. Prove in fact that Algorithm C always learns exactly ( n − 1 j − 1 ) clauses when it proves the unsatisfiability of cook ( j, k ), if M p = M f = ∞ (no purging or flushing).

281. [ 21 ] Construct a certificate of unsatisfiability that refutes ( 99 ), ( 100 ), ( 101 ).

▸ 282. [ M33 ] Construct a certificate of unsatisfiability for the clauses f snark ( q ) of exercise 176 when q ≥ 3 is odd, using O ( q ) clauses, all having length ≤ 4. Hint: Include the clauses ( ā j,p ∨ ē j,p ), ( a ¯ j , p ∨ f ¯ j , p ) , ( e ¯ j , p ∨ f ¯ j , p ) , and ( a j,p ∨ e j,p ∨ f j,p ) for 1 ≤ j ≤ q , 1 ≤ p ≤ 3.

283. [ HM46 ] Does Algorithm C solve the flower snark problem in linear time? More precisely, let p q ( M ) be the probability that the algorithm refutes fsnark ( q ) while making at most M references to MEM . Is there a constant N such that p q ( N q ) > 1 2 for all q ?

284. [ 23 ] Given F and ( C 1 ,..., C t ), a certificate-checking program tests condition ( 119 ) by verifying that F and clauses C 1 , ... , C i − 1 will force a conflict when they are augmented by the unit literals of C ¯ i . While doing this, it can mark each clause of F ∪ { C 1 ,..., C i − 1 } that was reduced to a unit during the forcing process; then the truth of C i does not depend on the truth of any unmarked clause.

In practice, many clauses of F are never marked at all, hence F will remain unsatisfiable even if we leave them out. Furthermore, many clauses C i are not marked during the verification of any of their successors, { C i +1 ,..., C t }; such clauses C i needn’t be verified, nor need we mark any of the clauses on which they depend.

Therefore we can save work by checking the certificate backwards: Start by marking the final clause C t , which is ∊ and always needs to be verified. Then, for i = t , t − 1, ... , check C i only if it has been marked.

The unit propagations can all be done without recording the “reason” R l that has caused any literal l to be forced. In practice, however, many of the forced literals don’t actually contribute to the conflicts that arise, and we don’t want to mark any clauses that aren’t really involved.

Explain how to use reasons, as in Algorithm C , so that clauses are marked by the verifier only if they actually participate in the proof of a marked clause C i .

285. [ 19 ] Using the data in Fig. 93 , the text observes that Eq. ( 124 ) gives j = 95, s j = 3081, and m j = 59 when α \= 15 16 . What are j , s j , and m j when (a) α \= 9 16 ? (b) α \= 1 2 ? (c) α \= 7 16 ? Also compare the effectiveness of different α ’s by computing the number b j of “black” clauses (those with 0 < RANGE ( c ) < j that proved to be useful).

286. [ M24 ] What choice of signatures-to-keep in Fig. 93 is optimum , in the sense that it maximizes Σ b pq x pq subject to the conditions Σ a pq x pq ≤ 3114, x pq ∊ {0, 1}, and x pq ≥ x p ′ q ′ for 1 ≤ p ≤ p ′ ≤ 7, 0 ≤ q ≤ q ′ ≤ 8? Here a pq and b pq are the areas of the gray and black clauses that have signature ( p, q ), as given by the matrices in the text. [This is a special case of the “knapsack problem with a partial ordering.”]

287. [ 25 ] What changes to Algorithm C are necessary to make it do a “ full run,” and later to learn from all of the conflicts that arose during that run?

288. [ 28 ] Spell out the details of computing RANGE scores and then compressing the database of learned clauses, during a round of purging.

289. [ M20 ] Assume that the k th round of purging begins with y k clauses in memory after k Δ + ( k 2 ) δ clauses have been learned, and that purging removes 1 2 y k of those clauses. Find a closed formula for y k as a function of k .

290. [ 17 ] Explain how to find x k , the unassigned variable of maximum activity that is used for flushing literals. Hint: It’s in the HEAP array.

291. [ 20 ] In the text’s hypothetical scenario about flushing Table 3 back to level 15, why will 49 soon appear on the trail, instead of 49 ¯ ?

292. [ M21 ] How large can AGILITY get after repeatedly executing ( 127 )?

293. [ 21 ] Spell out the details of updating M f to M + Δ f when deciding whether or not to flush. Also compute the agility threshold that’s specified in Table 4 . Hint: See ( 131 ).

294. [ HM21 ] For each binary vector α = x 1 x 2 x 3 x 4 , find the generating function g α ( z ) \= Σ j \= 0 ∞ p α , j z j , where p α,j is the probability that Algorithm P will solve the seven clauses of ( 7 ) after making exactly j flips, given the initial values α in step P1. Deduce the mean and variance of the number of steps needed to find a solution.

295. [ M23 ] Algorithm P often finds solutions much more quickly than predicted by Corollary W. But show that some 3SAT clauses will indeed require Ω((4 / 3) n ) trials.

296. [ HM20 ] Complete the proof of Theorem U by (approximately) maximizing the quantity f ( p, q ) in ( 129 ). Hint: Consider f ( p + 1, q ) /f ( p, q ).

▸ 297. [ HM26 ] (Emo Welzl.) Let G q ( z ) = Σ p C p,p + q − 1 ( z/ 3) p + q (2 z/ 3) p be the generating function for stopping time t = 2 p + q when Y 0 = q in the proof of Theorem U.

  1. Find a closed form for G q ( z ), using formulas from Section 7.2.1.6.

  2. Explain why G q (1) is less than 1.

  3. Evaluate and interpret the quantity G q ′ ( 1 ) / G q ( 1 ) .

  4. Use Markov’s inequality to bound the probability that Y t = 0 for some t ≤ N .

  5. Show that Corollary W follows from this analysis.

298. [ HM22 ] Generalize Theorem U and Corollary W to the case where each clause has at most k literals, where k ≥ 3.

299. [ HM23 ] Continuing the previous exercise, investigate the case k = 2.

▸ 300. [ 25 ] Modify Algorithm P so that it can be implemented with bitwise operations, thereby running (say) 64 independent trials simultaneously.

▸ 301. [ 25 ] Discuss implementing the algorithm of exercise 300 efficiently on MMIX.

302. [ 26 ] Expand the text’s high-level description of steps W4 and W5, by providing low-level details about exactly what the computer should do.

303. [ HM20 ] Solve exercise 294 with Algorithm W in place of Algorithm P .

304. [ HM34 ] Consider the 2SAT problem with n ( n − 1) clauses ( x ¯ j ∨ x k ) for all j ≠ k . Find the generating functions for the number of flips taken by Algorithms P and W . Hint: Exercises 1.2.6–68 and MPR–105 are helpful for finding the exact formulas.

▸ 305. [ HM29 ] Add one more clause, ( x ¯ 1 ∨ x ¯ 2 ) , to the previous exercise and find the resulting generating functions when n = 4. What happens when p = 0 in Algorithm W ?

▸ 306. [ HM32 ] ( Luby, Sinclair, and Zuckerman, 1993.) Consider a “ Las Vegas algorithm” that succeeds or fails; it succeeds at step t with probability p t , and fails with probability p ∞ < 1. Let q t = p 1 + p 2 + ··· + p t and E t = p 1 + 2 p 2 + ··· + tp t ; also let E ∞ = ∞ if p ∞ > 0, otherwise E ∞ \= ∑ t t p t . (The latter sum might be ∞.)

  1. Suppose we abort the algorithm and restart it again, whenever the first N steps have not succeeded. Show that if q N > 0, this strategy will succeed after performing an average of l ( N ) < ∞ steps. What is l ( N )?

  2. Compute l ( N ) when p m \= m n , p ∞ \= n − m n , otherwise p t = 0, where 1 ≤ m ≤ n .

  3. Given the uniform distribution, p t \= 1 n for 1 ≤ t ≤ n , what is l ( N )?

  4. Find all probability distributions such that l ( N ) = l (1) for all N ≥ 1.

  5. Find all probability distributions such that l ( N ) = l ( n ) for all N ≥ n .

  6. Find all probability distributions such that q n +1 \= 1 and l ( n ) ≤ l ( n + 1).

  7. Find all probability distributions such that q 3 = 1 and l (1) < l (3) < l (2).

  8. Let l = inf N ≥1 l ( N ), and let N ∗ be the least positive integer such that l ( N ∗ ) = l , or ∞ if no such integer exists. Prove that N ∗ = ∞ implies l = E ∞ < ∞.

  9. Find N ∗ for the probability distribution p t =[ t>n ] / (( t − n )( t +1− n )), given n ≥ 0.

  10. Exhibit a simple example of a probability distribution for which N ∗ = ∞.

  11. Let L = min t ≥1 t/q t . Prove that l ≤ L ≤ 2 l − 1.

307. [ HM28 ] Continuing exercise 306 , consider a more general strategy defined by an infinite sequence of positive integers ( N 1 , N 2 , ... ): “Set j ← 0; then, while success has not yet been achieved, set j ← j + 1 and run the algorithm with cutoff parameter N j .”

  1. Explain how to compute E X , where X is the number of steps taken before this strategy succeeds.

  2. Let T j = N 1 + ··· + N j . Prove that E ⁢   X \= ∑ j \= 1 ∞ Pr ( T j − 1 < X ≤ T j ) l ( N j ) , if we have q Nj > 0 for all j .

  3. Consequently the steady strategy ( N ∗ , N ∗ , ... ) is best: E X ≥ l ( N ∗ ) = l .

  4. Given n , exercise 306(b) defines n simple probability distributions p ( m ) that have l ( N ∗ ) = n , but the value of N ∗ = m is different in each case. Prove that any sequence ( N 1 , N 2 , ... ) must have E ⁢     X > 1 4 n H n − 1 2 n \= 1 4 l H l − 1 2 l on at least one of those p ( m ) . Hint: Consider the smallest r such that, for each m , the probability is ≥ 1 2 that r trial runs suffice; show that ≥ n/ (2 m ) of { N 1 , ..., N r } are ≥ m .

308. [ M29 ] This exercise explores the “ reluctant doubling” sequence ( 130 ).

  1. What is the smallest n such that S n = 2 a , given a ≥ 0?

  2. Show that { n | S n = 1} = {2 k + 1 − νk | k ≥ 0}; hence the generating function ∑ n z n [ S n = 1] is the infinite product z (1 + z )(1 + z 3 )(1 + z 7 )(1 + z 15 ) ....

  3. Find similar expressions for { n | S n = 2 a } and ∑ n z n [ S n = 2 a ].

  4. Let Σ ( a , b , k ) \= ∑ n \= 1 r ( a , b , k ) S n , where S r ( a,b,k ) is the 2 b k th occurrence of 2 a in 〈 S n 〉. For example, ∑(1, 0, 3) = S 1 + ··· + S 10 = 16. Evaluate ∑( a, b, 1) in closed form.

  5. Show that ∑( a, b, k +1) − ∑( a, b, k ) ≤ ( a + b +2 k − 1)2 a + b , for all k ≥ 1.

  6. Given any probability distribution as in exercise 306(k) , let a = ⌈lg t ⌉ and b = ⌈lg 1 /q t ⌉, where t/q t = L ; thus L ≤ 2 a + b < 4 L . Prove that if the strategy of exercise 307 is used with N j = S j , we have

    E ⁢     X ≤ Σ ( a , b , 1 ) + Σ k ≥ 1 Q k ( Σ ( a , b , k + 1 ) − Σ ( a , b , k ) ) , where   Q \= ( 1 − q 2 a ) 2 b .

  7. Therefore 〈 S n 〉 gives E X < 13 l lg l + 49 l , for every probability distribution.

309. [ 20 ] Exercise 293 explains how to use the reluctant doubling sequence with Algorithm C . Is Algorithm C a Las Vegas algorithm?

310. [ M25 ] Explain how to compute the “ reluctant Fibonacci sequence”

1, 1, 2, 1, 2, 3, 1, 1, 2, 3, 5, 1, 1, 2, 1, 2, 3, 5, 8, 1, 1, 2, 1, 2, 3, 1, 1, 2, 3, 5, 8, 13, 1, ...,

which is somewhat like ( 130 ) and useful as in exercise 308 , but its elements are Fibonacci numbers instead of powers of 2.

311. [ 21 ] Compute approximate values of E X for the 100 probability distributions of exercise 306(b) when n = l = 100, using the method of exercise 307 with the sequences 〈 S n 〉 of exercise 308 and 〈 S n ′ 〉 of exercise 310 . Also consider the more easily generated “ ruler doubling” sequence 〈 R n 〉, where R n = n & − n = 2 ρn . Which sequence is best?

312. [ HM24 ] Let T ( m, n ) = E X when the reluctant doubling method is applied to the probability distribution defined in exercise 306(b) . Express T ( m, n ) in terms of the generating functions in exercise 308(c) .

▸ 313. [ 22 ] Algorithm W always flips a cost-free literal if one is present in C j , without considering the parameter p . Show that such a flip always decreases the number of unsatisfied clauses, r ; but it might increase the distance from x to the nearest solution.

▸ 314. [ 36 ] (H. H. Hoos, 1998.) If the given clauses are satisfiable, and if p > 0, can there be an initial x for which Algorithm W always loops forever?

315. [ M18 ] What value of p is appropriate in Theorem J when d = 1?

316. [ HM20 ] Is Theorem J a consequence of Theorem L?

▸ 317. [ M26 ] Let α ( G ) \= Pr ( A 1 ¯ ∩ ⋅ ⋅ ⋅ ∩ A ¯ m ) under the assumptions of ( 133 ), when p i = p = ( d − 1) d −1 / d d for 1 ≤ i ≤ m and every vertex of G has degree at most d > 1. Prove, by induction on m , that α ( G ) > 0 and that α ( G ) > d − 1 d α ( G \ v ) when v has degree < d .

318. [ HM33 ] (J. B. Shearer.) Prove that Theorem J is the best possible result of its kind: If p > ( d − 1) d −1 / d d and d > 1, there is a graph G of maximum degree d for which ( p,...,p ) ∉ R ( G ). Hint: Consider complete t -ary trees, where t = d − 1.

319. [ HM20 ] Show that pde < 1 implies p ≤ ( d − 1) d −1 / d d .

320. [ M24 ] Given a lopsidependency graph G , the occurrence threshold ρ ( G ) is the smallest value p such that it’s sometimes impossible to avoid all events when each event occurs with probability p . For example, the Möbius polynomial for the path P 3 is 1− p 1 − p 2 − p 3 + p 1 p 3 ; so the occurrence threshold is ϕ −2 , the least p with 1−3 p + p 2 ≤ 0.

  1. Prove that the occurrence threshold for P m is 1 / ( 4 ⁢   cos 2 π m + 2 ) .

  2. What is the occurrence threshold for the cycle graph C m ?

321. [ M24 ] Suppose each of four random events A , B , C , D occurs with probability p , where { A, C } and { B, D } are independent. According to exercise 320(b) with m = 4, there’s a joint distribution of ( A, B, C, D ) such that at least one of the events always occurs, whenever p ≥ ( 2 − 2 ) / 2 ≈ 0.293 . Exhibit such a distribution when p = 3 / 10.

▸ 322. [ HM35 ] (K. Kolipaka and M. Szegedy, 2011.) Surprisingly, the previous exercise cannot be solved in the setting of Algorithm M ! Suppose we have independent random variables ( W, X, Y, Z ) such that A depends on W and X , B depends on X and Y , C depends on Y and Z , D depends on Z and W . Here W equals j with probability w j for all integers j ; X , Y , and Z are similar. This exercise will prove that the constraint A ¯ ∩ B ¯ ∩ C ¯ ∩ D ¯ is always satisfiable, even when p is as large as 0.333.

  1. Express the probability Pr ( A ¯ ∩ B ¯ ∩ C ¯ ∩ D ¯ ) in a convenient way.

  2. Suppose there’s a distribution of W , X , Y , Z with Pr( A ) = Pr( B ) = Pr( C ) = Pr( D ) = p and Pr ( A ¯ ∩ B ¯ ∩ C ¯ ∩ D ¯ ) \= 0 . Show that there are ten values such that

    0 ≤ a , b , c , d , a ′ , b ′ , c ′ , d ′ ≤ 1 , 0 < μ , ν < 1 , μ a + ( 1 − μ ) a ′ ≤ p , μ b + ( 1 − μ ) b ′ ≤ p , ν c + ( 1 − ν ) c ′ ≤ p , v d + ( 1 − ν ) d ′ ≤ p , a + d ≥ 1 o r ⁢   b + c ≥ 1 , a + d ′ ≥ 1 ⁢   o r ⁢   b + c ′ ≥ 1 , a ′ + d ≥ 1 ⁢   o r ⁢     b ′ + c ≥ 1 , a ′ + d ′ ≥ 1 ⁢     o r ⁢     b ′ + c ′ ≥ 1.

  3. Find all solutions to those constraints when p = 1/3.

  4. Convert those solutions to distributions that have Pr ( A ¯ ∩ B ¯ ∩ C ¯ ∩ D ¯ ) \= 0 .

323. [ 10 ] What trace precedes ccb in the list ( 135 )?

▸ 324. [ 22 ] Given a trace α = x 1 x 2 ... x n for a graph G , explain how to find all strings β that are equivalent to α , using Algorithm 7.2.1.2V. How many strings yield ( 136 )?

▸ 325. [ 20 ] An acyclic orientation of a graph G is an assignment of directions to each of its edges so that the resulting digraph has no oriented cycles. Show that the number of traces for G that are permutations of the vertices (with each vertex appearing exactly once in the trace) is the number of acyclic orientations of G .

326. [ 20 ] True or false: If α and β are traces with α = β , then α R = β R . (See ( 137 ).)

▸ 327. [ 22 ] Design an algorithm to multiply two traces α and β , when clashing is defined by territory sets T ( a ) in some universe U . Assume that U is small (say | U | ≤ 64), so that bitwise operations can be used to represent the territories.

328. [ 20 ] Continuing exercise 327 , design an algorithm that computes α/β . More precisely, if β is a right factor of α , in the sense that α = γβ for some trace γ , your algorithm should compute γ ; otherwise it should report that β is not a right factor.

329. [ 21 ] Similarly, design an algorithm that either computes α / β or reports that α isn’t a left factor of β .

▸ 330. [ 21 ] Given any graph G , explain how to define territory sets T ( a ) for its vertices a in such a way that we have a = b or a −−− b if and only if T ( a ) ∩ T ( b ) ≠ ∅. (Thus traces can always be modeled by empilements of pieces.) Under what circumstances is it possible to do this with | T ( a )| = 2 for all a , as in the text’s example ( 136 )?

331. [ M20 ] What happens if the right-hand side of ( 139 ) is expanded without allowing any of the variables to commute with each other?

332. [ 20 ] When a trace is represented by its lexicographically smallest string, no letter in that representative string is followed by a smaller letter with which it commutes. (For example, no c is followed by a in ( 135 ), because we could get an equivalent smaller string by changing ca to ac .)

Conversely, given any ordered set of letters, some of which commute, consider all strings having no letter followed by a smaller letter with which it commutes. Is every such string the lexicographically smallest of its trace?

▸ 333. [ M20 ] ( Carlitz, Scoville, and Vaughan, 1976.) Let D be a digraph on {1, ..., m }, and let A be the set of all strings a j 1 ... a j n such that j i → j i+1 in D for 1 ≤ i < n . Similarly let B be the set of all strings a j 1 ... a jn such that j i ↛ j i+1 for 1 ≤ i < n . Prove that

Σ α ∈ A α \= 1 / Σ β ∈ B ( − 1 ) | β |   β \= Σ k ≥ 0 ( 1 − Σ β ∈ B ( − 1 ) | β | ⁢   β ) k

is an identity in the noncommutative variables { a 1 , ..., a m }. (For example, we have

1 + a + b + a b + b a + a b a + b a b + ⋅ ⋅ ⋅ \= Σ k ≥ 0 ( a + b − a a − b b + a a a + b b b − ⋅ ⋅ ⋅ ) k

in the case m = 2, 1 ↛ 1, 1 → 2, 2 ↛ 1, 2 ↛ 2.)

▸ 334. [ 25 ] Design an algorithm to generate all traces of length n that correspond to a given graph on the alphabet {1, ..., m }, representing each trace by its lexicographically smallest string.

335. [ HM26 ] If the vertices of G can be ordered in such a way that x < y < z and x ― y and y ― z implies x ― z , show that the Möbius series M G can be expressed as a determinant. For example,

images

▸ 336. [ M20 ] If graphs G and H on distinct vertices have the Möbius series M G and M H , what are the Möbius series for (a) G ⊕ H and (b) G −−− H ?

337. [ M20 ] Suppose we obtain the graph G ′ from G by substituting a clique of vertices { a 1 , ..., a k } for some vertex a , then including edges from a j to each neighbor of a for 1 ≤ j ≤ k . Describe the relation between M G ′ and M G .

338. [ M21 ] Prove Viennot’s general identity ( 144 ) for source-constrained traces.

▸ 339. [ HM26 ] (G. Viennot.) This exercise explores factorization of traces into pyramids.

  1. Each letter x j of a given trace α = x 1 ... x n lies at the top of a unique pyramid β j such that β j is a left factor of α . For example, in the trace bcebafdc of ( 136 ), the pyramids β 1 , ... , β 8 are respectively b , bc , e , bcb , bcba , ef , bced , and bcebdc . Explain intuitively how to find these pyramidal left factors from α ’s empilement.

  2. A labeled trace is an assignment of distinct numbers to the letters of a trace; for example, abca might become a 4 b 7 c 6 a 3 . A labeled pyramid is the special case when the pyramid’s top element is required to have the smallest label. Prove that every labeled trace is uniquely factorizable into labeled pyramids whose topmost labels are in ascending order. (For example, b 6 c 2 e 4 b 7 a 8 f 5 d 1 c 3 = b 6 c 2 e 4 d 1 · b 7 a 8 c 3 · f 5 .)

  3. Suppose there are t n traces of length n , and p n pyramids. Then there are T n = n ! t n labeled traces and P n = ( n − 1)! p n labeled pyramids (because only the relative order of the labels is significant). Letting T ( z ) = ∑ n ≥0 T n z n / n ! and P ( z ) = ∑ n ≥1 P n z n / n !, prove that the number of labeled traces of length n whose factorization in part (b) has exactly l pyramids is n ![ z n ] P ( z ) l / l !.

  4. Consequently T ( z ) = e P ( z ) .

  5. Therefore (and this is the punch line!) ln M G ( z ) = ∑ n ≥1 p n z n / n .

▸ 340. [ M20 ] If we assign a weight w ( σ ) to every cyclic permutation σ , then every permutation π has a weight w ( π ) that is the product of the weights of its cycles. For example, if π \= ( 1 2 3 4 5 6 7 3 1 4 2 7 6 5 ) \= ( 1 ⁢     3 ⁢   ⁢ 4 ⁢   ⁢ 2 ) ( 5 ⁢   7 ) ( 6 ) then w ( π ) = w ((1 3 4 2)) w ((5 7)) w (( 6 )).

The permutation polynomial of a set S is the sum of w ( π ) over all permutations of S . Given any n × n matrix A = ( a ij ), show that it’s possible to define appropriate cycle weights so that the permutation polynomial of {1, ..., n } is the determinant of A .

341. [ M25 ] The involution polynomial of a set S is the special case of the permutation polynomial when the cycle weights have the form w jj x for the 1-cycle ( j ) and −w ij for the 2-cycle ( ij ), otherwise w ( σ ) = 0. For example, the involution polynomial of {1, 2, 3, 4} is w 11 w 22 w 33 w 44 x 4 − w 11 w 22 w 34 x 2 − w 11 w 23 w 44 x 2 − w 11 w 24 w 33 x 2 − w 12 w 33 w 44 x 2 − w 13 w 22 w 44 x 2 − w 14 w 22 w 33 x 2 + w 12 w 34 + w 13 w 24 + w 14 w 23 .

Prove that, if w ij > 0 for 1 ≤ i ≤ j ≤ n , the involution polynomial of {1, ..., n } has n distinct real roots. Hint: Show also that, if the roots for {1, ..., n − 1} are q 1 < ··· < q n −1 , then the roots r k for {1, ..., n } satisfy r 1 < q 1 < r 2 < ··· < q n −1 < r n .

342. [ HM25 ] ( Cartier and Foata, 1969.) Let G n be the graph whose vertices are the ∑ k \= 1 n ( n k ) ( k − 1 ) cyclic permutations of subsets of {1, ..., n }, with σ −−− τ when σ and τ intersect. For example, the vertices of G 3 are ( 1 ), ( 2 ), ( 3 ), ( 12 ), ( 13 ), ( 23 ), ( 123 ), ( 132 ); and they’re mutually adjacent except that ( 1 ) ― ( 2 ) , ( 1 ) ― ( 3 ) , ( 1 ) ― ( 23 ) , ( 2 ) ― ( 3 ) , ( 2 ) ― ( 13 ) , ( 12 ) ― ( 3 ) . Find a beautiful relation between M G n and the characteristic polynomial of an n × n matrix.

▸ 343. [ M25 ] If G is any cograph, show that ( p 1 , ..., p m ) ∊ 𝓡 ( G ) if and only if we have M G ( p 1 , ..., p m ) > 0. Exhibit a non-cograph for which the latter statement is not true.

344. [ M33 ] Given a graph G as in Theorem S, let B 1 , ... , B m have the joint probability distribution of exercise MPR–31, with π I = 0 whenever I contains distinct vertices { i, j } with i −−− j , otherwise π I = ∏ i∊I p i .

  1. Show that this distribution is legal (see exercise MPR–32) if ( p 1 , ..., p m ) ∊ R ( G ).

  2. Show that this “ extreme distribution” also satisfies condition ( 147 ).

  3. Let β ( G ) \= Pr ( B ¯ 1 ∩ ⋅ ⋅ ⋅ ∩ B ¯ m ) . If J ⊆ {1, ..., m }, express β ( G | J ) in terms of M G .

  4. Defining α ( G ) as in exercise 317 , with events A j satisfying ( 133 ) and probabilities ( p 1 , ..., p m ) ∊ 𝓡 ( G ), show that α ( G | J ) ≥ β ( G | J ) for all J ⊆ {1, ..., m }.

  5. If p i satisfies ( 134 ), show that β ( G | J ) ≥ ∏ j∊J (1 − θ j ).

345. [ M30 ] Construct unavoidable events that satisfy ( 147 ) when ( p 1 , ..., p m ) ∈ 𝓡 ( G ).

▸ 346. [ HM28 ] Write ( 142 ) as M G = M G \ a (1 − aK a,G ) where K a,G = M G \ a ∗ / M G \ a .

  1. If ( p 1 , ..., p m ) ∊ 𝓡 ( G ), prove that K a,G is monotonic in all of its parameters: It does not increase if any of p 1 , ... , p m are decreased.

  2. Exploit this fact to design an algorithm that computes M G ( p 1 , ..., p m ) and decides whether or not ( p 1 , ..., p m ) ∊ 𝓡 ( G ), given a graph G and probabilities ( p 1 , ..., p m ). Illustrate your algorithm on the graph G = P 3 □ P 2 of exercise 335 .

▸ 347. [ M28 ] A graph is called chordal when it has no induced cycle C k for k > 3. Equivalently (see Section 7.4.2), a graph is chordal if and only if its edges can be defined by territory sets T ( a ) that induce connected subgraphs of some tree. For example, interval graphs and forests are chordal.

  1. Say that a graph is tree-ordered if its vertices can be arranged as nodes of a forest in such a way that

    a ― b ⁢       i m p l i e s ⁢     a ≻ b ⁢     o r ⁢       b ≻ a ; a ≻ b ≻ c ⁢       a n d ⁢     a — c ⁢     i m p l i e s ⁢       a — b . ( * )

    (Here ‘ a ≻ b ’ means that a is a proper ancestor of b in the forest.) Prove that every tree-ordered graph is chordal.

  2. Conversely, show that every chordal graph can be tree-ordered.

  3. Show that the algorithm in the previous exercise becomes quite simple when it is applied to a tree-ordered graph, if a is eliminated before b whenever a ≻ b .

  4. Consequently Theorem L can be substantially strengthened when G is a chordal graph: When G is tree-ordered by ≻, the probability vector ( p 1 , ..., p m ) is in 𝓡 ( G ) if and only if there are numbers 0 ≤ θ 1 , ..., θ m < 1 such that

    p i \= θ i ⁢     Π i — j ⁢   in   G ,   i ≻ j ( 1 − θ j ) .

348. [ HM26 ] (A. Pringsheim, 1894.) Show that any power series f ( z ) \= Σ n \= 0 ∞ a n z n with a n ≥ 0 and radius of convergence ρ , where 0 < ρ < ∞, has a singularity at z = ρ .

▸ 349. [ M24 ] Analyze Algorithm M exactly in the two examples considered in the text (see ( 150 )): For each binary vector x = x 1 ... x 7 , compute the generating function g x ( z ) \= Σ t ⁢   p x , t z t , where p x,t is the probability that step M3 will be executed exactly t times after step M1 produces x . Assume that step M2 always chooses the smallest possible value of j . (Thus the ‘Case 2’ scenario in ( 150 ) will never occur.)

What are the mean and variance of the running times, in (i) Case 1? (ii) Case 2?

350. [ HM26 ] (W. Pegden.) Suppose Algorithm M is applied to the m = n + 1 events

A j = x j for 1 ≤ j ≤ n ; A m = x 1 ∨···∨ x n .

Thus A m is true whenever any of the other A j is true; so we could implement step M2 by never setting j ← m . Alternatively, we could decide to set j ← m whenever possible. Let ( N i , N ii , N iii , N iv , N v ) be the number of resamplings performed when parameter ξ k of the algorithm is (i) 1/2; (ii) 1/(2 n ); (iii) 1/2 n ; (iv) 1/( n + k ); (v) 1/( n + k ) 2 .

  1. Find the asymptotic mean and variance of each N , if j is never equal to m .

  2. Find the asymptotic mean and variance of each N , if j is never less than m .

  3. Let G be the graph on {1, ..., n +1} with edges j −−− ( n + 1) for 1 ≤ j ≤ n , and let p j = Pr( A j ). For which of the five choices of ξ k is ( p 1 , ..., p n +1 ) ∊ 𝓡 ( G )?

▸ 351. [ 25 ] The Local Lemma can be applied to the satisfiability problem for m clauses on n variables if we let A j be the event “ C j is not satisfied.” The dependency graph G then has i −−− j whenever two clauses C i and C j share at least one common variable. If, say, C i is ( x 3 ∨ x ¯ 5 ∨ x 6 ) , then ( 133 ) holds whenever p i ≥ (1 − ξ 3 ) ξ 5 (1 − ξ 6 ), assuming that each x k is true with probability ξ k , independent of the other x ’s.

But if, say, C j is ( x ¯ 2 ∨ x 3 ∨ x 7 ) , condition ( 133 ) remains true even if we don’t stipulate that i −−− j . Variable x 3 appears in both clauses, yet the cases when C j is satisfied are never bad news for C i . We need to require that i −−− j in condition ( 133 ) only when C i and C j are “ resolvable” clauses, namely when some variable occurs positively in one and negatively in the other.

Extend this reasoning to the general setting of Algorithm M , where we have arbitrary events A j that depend on variables Ξ j : Define a lopsidependency graph G for which ( 133 ) holds even though we might have i ― j in some cases when Ξ i ∩ Ξ j ≠ ∅.

352. [ M21 ] Show that E j ≤ θ j /(1 − θ j ) in ( 152 ), when ( 134 ) holds.

353. [ M21 ] Consider Case 1 and Case 2 of Algorithm M as illustrated in ( 150 ).

  1. How many solutions x 1 ... x n are possible? (Generalize from n = 7 to any n .)

  2. How many solutions are predicted by Theorem S?

  3. Show that in Case 2 the lopsi dependency graph is much smaller than the dependency graph. How many solutions are predicted when the smaller graph is used?

354. [ HM20 ] Show that the expected number E N of resampling steps in Algorithm M is at most − M G * ′   ( 1 ) / M G * ( 1 ) .

355. [ HM21 ] In ( 152 ), prove that E j ≤ 1/ δ when ( p 1 , ..., p m ) has positive slack δ . Hint: Consider replacing p j by p j + δp j .

▸ 356. [ M33 ] ( The Clique Local Lemma .) Let G be a graph on {1, ..., m }, and let G | U 1 , ... , G | U t be cliques that cover all the edges of G . Assign numbers θ ij ≥ 0 to the vertices of each U j , such that ∑ j = ∑ i∊U j θ ij < 1. Assume that

Pr ( A i ) \= p i ≤ θ i j ⁢       Π k ≠ j , i ∈ U k ( 1 + θ i k − Σ k ) w h e n e v e r ⁢     1 ≤ i ≤ m ⁢                 a n d ⁢     i ⁢     ∈ U j . ⁢  

  1. Prove that ( p 1 , ..., p m ) ∊ 𝓡 ( G ). Hint: Letting A ¯ S denote ∩ i ∈ S     A ¯ i , show that

    Pr ( A i |   A ¯ S ) ≤ θ i j w h e n e v e r ⁢ 1 ≤ i ≤ m ⁢     a n d ⁢     i ∈ U j ⁢       a n d ⁢       S ∩ U j \= ∅ .

  2. Also E i in ( 152 ) is at most min i −−− j > in G θ ij /(1 − ∑ j ). (See Theorems M and S.)

  3. Improve Theorem L by showing that, if 0 ≤ θ j < 1 2 , then ( p 1 , ..., p m )∊ R ( G ) when

    p i \= θ i ( Π i ― j ⁢   in   G ( 1 − θ j ) ) / max i ― j ⁢   in   G ( 1 − θ j ) .

▸ 357. [ M20 ] Let x \= π v ¯ and y = π v in ( 155 ), and suppose the field of variable v is ( p, q, r ). Express x and y as functions of p , q , and r .

358. [ M20 ] Continuing exercise 357 , prove that r = max( p, q, r ) if and only if x , y ≥ 1 2 .

359. [ 20 ] Equations ( 156 ) and ( 157 ) should actually have been written

γ l → C \= ( 1 − π l ¯ )   ( 1 − η l )   Π l ∈ C ′ ≠ C   ( 1 − η C → l ) π l ¯ + ( 1 − π l ¯ )   ( 1 − η l )   Π l ∈ C ′ ≠ C   ( 1 − η C → l ) a n d ⁢               η C → l ′ \= Π C ∍ l ′ ≠ l γ l ′ → C ,

to avoid division by zero. Suggest an efficient way to implement these calculations.

360. [ M23 ] Find all fixed points of the seven-clause system illustrated in ( 159 ), given that π 1 \= π 2 ¯ \= π 4 ¯ \= 1 . Assume also that η l η l ¯ \= 0 for all l .

▸ 361. [ M22 ] Describe all fixed points η C → l \= η C → l ′ of the equations ( 154 ), ( 156 ), ( 157 ), for which each η C → l and each η l is either 0 or 1.

362. [ 20 ] Spell out the computations needed to finish Algorithm S in step S8.

▸ 363. [ M30 ] ( Lattices of partial assignments .) A partial assignment to the variables of a satisfiability problem is called stable (or “ valid”) if it is consistent and cannot be extended by unit propagation. In other words, it’s stable if and only if no clause is entirely false, or entirely false except for at most one unassigned literal. Variable x k of a partial assignment is called constrained if it appears in a clause where ± x k is true but all the other literals are false (thus its value has a “ reason”).

The 3 n partial assignments of an n -variable problem can be represented either as strings x = x 1 ... x n on the alphabet {0, 1, ∗} or as sets L of strictly distinct literals. For example, the string x = ∗1∗01∗ corresponds to the set L \= { 2 , 4 ¯ , 5 } . We write x ≺ x ′ if x ′ is equal to x except that x k ′ ∈ { 0 , 1 } ; equivalently L ≺ L ′ if L ′ = L ∪ k or L ′ \= L ∪ k ¯ . Also x ⊑ x ′ if there are t ≥ 0 stable partial assignments x ( j ) with

x = x (0) ≺ x (1) ≺ ··· ≺ x ( t ) \= x ′ .

Let p 1 , ... , p n , q 1 , ... , q n be probabilities, with p k + q k = 1 for 1 ≤ k ≤ n . Define the weight W ( x ) of a partial assignment to be 0 if x is unstable, otherwise

W ( x ) = ∏{ p k | x k = ⋆}. ∏{ q k | x k ≠ ∗ and x k is un constrained}.

[E. Maneva, E. Mossel, and M. J. Wainwright, in JACM 54 (2007), 17:1–17:41, studied general message-passing algorithms on partial assignments that are distributed with probability proportional to their weights, in the case p 1 = ··· = p n = p , showing that survey propagation ( Algorithm S ) corresponds to the limit as p → 1.]

  1. True or false: The partial assignment specified by the literals currently on the trail in step C5 of Algorithm C is stable.

  2. What weights W ( x ) correspond to the clauses F in ( 1 )?

  3. Let x be a stable partial assignment with x k = 1, and let x ′ and x ″ be obtained from x by setting x k ′ ← 0 , x k " ← ∗ . True or false: x k is unconstrained in x if and only if (i) x ′ is consistent; (ii) x ′ is stable; (iii) x ″ is stable.

  4. If the only clause is 123 = ( x 1 ∨ x 2 ∨ x 3 ), find all sets L such that L ⊑ { 1 , 2 ¯ , 3 ¯ } .

  5. What are the weights when there’s only a single clause 123 = ( x 1 ∨ x 2 ∨ x 3 )?

  6. Find clauses such that the sets L with L ⊑ {1, 2, 3, 4, 5} are ∅, {4}, {5}, {1, 4}, {2, 5}, {4, 5}, {1, 4, 5}, {2, 4, 5}, {3, 4, 5}, {1, 3, 4, 5}, {2, 3, 4, 5}, {1, 2, 3, 4, 5}.

  7. Let 𝓛 be a family of sets ⊆ {1, ..., n }, closed under intersection, with the property that L ∊ 𝓛 implies L = L (0) ≺ L (1) ≺ ··· ≺ L ( t ) \= {1, ..., n } for some L ( j ) ∊ 𝓛. (The sets in (f) form one such family, with n = 5.) Construct definite Horn clauses with the property that L ∊ 𝓛 if and only if L ⊑ {1, ..., n }.

  8. True or false: If L , L ′, L ″ are stable and L ′ ≺ L , L ″ ≺ L , then L ′ ∩ L ″ is stable.

  9. If L ′ ⊑ L and L ″ ⊑ L , prove that L ′ ∩ L ″ ⊑ L .

  10. Prove that ∑ x ′⊑ x W ( x ′) = ∏{ p k | x k = ∗} whenever x is stable.

▸ 364. [ M21 ] A covering assignment is a stable partial assignment in which every assigned variable is constrained. A core assignment is a covering assignment L that satisfies L ⊑ L ′ for some total assignment L ′.

  1. True or false: The empty partial assignment L = ∅ is always covering.

  2. Find all the covering and core assignments of the clauses F in ( 1 ).

  3. Find all the covering and core assignments of the clauses R ′ in ( 7 ).

  4. Show that every satisfying assignment L ′ has a unique core.

  5. The satisfying assignments form a graph, if two of them are adjacent when they differ by complementing just one literal. The connected components of this graph are called clusters . Prove that the elements of each cluster have the same core.

  6. If L ′ and L ″ have the same core, do they belong to the same cluster?

365. [ M27 ] Prove that the clauses waerden (3, 3; n ) have a nontrivial (i.e., nonempty) covering assignment for all sufficiently large n (although they’re unsatisfiable).

▸ 366. [ 18 ] Preprocess the clauses R ′ of ( 7 ). What erp rules are generated?

▸ 367. [ 20 ] Justify the erp rule ( 161 ) for elimination by resolution.

368. [ 16 ] Show that subsumption and downhill resolution imply unit conditioning: Any preprocessor that does transformations 2 and 4 will also do transformation 1.

▸ 369. [ 21 ] (N. Eén and A. Biere.) Suppose l appears only in clauses C 1 , ... , C p and l ¯ appears only in clauses C 1 ′ , ... , C q ′ , where we have C 1 = ( l ∨ l 1 ∨···∨ l r ) and C j ′ \= ( l ¯ ∨ l ¯ j ) for 1 ≤ j ≤ r . Prove that we can eliminate | l | by using the erp rule l ¯ ← ( l 1 ∨ ⋅ ⋅ ⋅ ∨ l r ) and replacing those p + q clauses by only ( p − 2) r + q others, namely

{ C 1 ◊ C j ′ |   r < j ≤ q } ∪ { C i ◊ C j ′ |   1 < i ≤ p , 1 ≤ j ≤ r } .

(The case r = 1 is especially important. In many applications — for example in the examples of fault testing, tomography, and the “ Life in 4” problem about extending Fig. 78 — more than half of all variable eliminations admit this simplification.)

370. [ 20 ] The clauses obtained by resolution might be needlessly complex even when exercise 369 doesn’t apply. For example, suppose that variable x appears only in the clauses ( x ∨ a ) ∧ ( x ∨ a ¯ ∨ c ) ∧ ( x ¯ ∨ b ) ∧ ( x ¯ ∨ b ¯ ∨ c ¯ ) . Resolution replaces those four clauses by three others: ( a ∨ b ) ∧ ( a ∨ b ¯ ∨ c ¯ ) ∧ ( a ¯ ∨ b ∨ c ) . Show, however, that only two clauses, both binary , would actually suffice in this particular case.

371. [ 24 ] By preprocessing repeatedly with transformations 1–4, and using exercise 369 , prove that the 32 clauses ( 9 ) of waerden (3, 3; 9) are unsatisfiable.

372. [ 30 ] Find a “small” set of clauses that cannot by solved entirely via transformations 1–4 and the use of exercise 369 .

373. [ 25 ] The answer to exercise 228 defines 2 m + ∑ j \= 1 m ( j − 1 ) 2 ≈ m 3 / 3 clauses in m 2 variables that suffice to refute the anti- maximal-element axioms of ( 99 )–( 101 ). Algorithm L needs exponential time to handle these clauses, according to Theorem R; and experiments show that they are bad news for Algorithm C too. Show, however, that preprocessing with transformations 1–4 will rapidly prove them unsatisfiable.

▸ 374. [ 32 ] Design data structures for the efficient representation of clauses within a SAT preprocessor. Also design algorithms that (a) resolve clauses C and C ′ with respect to a variable x ; (b) find all clauses C ′ that are subsumed by a given clause C ; (c) find all clauses C ′ that are self-subsumed by a given clause C and a literal x ¯ ∈ C .

375. [ 21 ] Given | l |, how can one test efficiently whether or not the special situation in exercise 369 applies, using (and slightly extending) the data structures of exercise 374 ?

▸ 376. [ 36 ] After a preprocessor has found a transformation that reduces the current set of clauses, it is supposed to try again and look for further simplifications. (See ( 160 ).) Suggest methods that will avoid unnecessary repetition of previous work, by using (and slightly extending) the data structures of exercise 374 .

377. [ 22 ] (V. Vassilevska Williams.) If G is a graph with n vertices and m edges, construct a 2SAT problem F with 3 n variables and 6 m clauses, such that G contains a triangle (a 3- clique) if and only if F has a failed literal.

378. [ 20 ] ( Blocked clause elimination. ) Clause C = ( l ∨ l 1 ∨ ··· ∨ l q ) is said to be blocked by the literal l if every clause that contains l ¯ also contains either l ¯ 1 or ··· or l ¯ q . Exercise 161(b) proves that clause C can be removed without making an unsatisfiable problem satisfiable. Show that this transformation requires an erp rule, even though it doesn’t eliminate any of the variables. What erp rule works?

▸ 379. [ 20 ] ( Blocked self-subsumption. ) Consider the clause ( a ∨ b ∨ c ∨ d ), and suppose that every clause containing ā but not b ¯ nor c ¯ also contains d . Show that we can then shorten the clause to ( a ∨ b ∨ c ) without affecting satisfiability. Is an erp rule needed?

380. [ 21 ] Sometimes we can use self-subsumption backwards, for example by weakening the clause ( l 1 ∨ l 2 ∨ l 3 ) to ( l 1 ∨···∨ l k ) if each intermediate replacement of ( l 1 ∨· · ·∨ l j ) by ( l 1 ∨···∨ l j −1 ) is justifiable for 3 < j ≤ k . Then, if we’re lucky, the clause ( l 1 ∨· · ·∨ l k ) is weak enough to be eliminated; in such cases we are allowed to eliminate ( l 1 ∨ l 2 ∨ l 3 ).

  1. Show that ( a ∨ b ∨ c ) can be eliminated if it is accompanied by the additional clauses ( a ∨ b ∨ d ¯ ) , ( a ∨ d ∨ e ), ( b ∨ d ∨ e ¯ ) .

  2. Show that ( a ∨ b ∨ c ) can also be eliminated when accompanied by ( a ∨ b ∨ d ¯ ) , ( a ∨ c ¯ ∨ d ¯ ) , ( b ∨ d ∨ e ¯ ) , ( b ∨ c ¯ ∨ e ¯ ) , provided that no other clauses contain c ¯ .

  3. What erp rules, if any, are needed for those eliminations?

381. [ 22 ] Combining exercises 379 and 380 , show that any one of the clauses in

( x ¯ 1 ∨ x 2 ) ∧ ( x ¯ 2 ∨ x 3 ) ∧ ⋅ ⋅ ⋅ ∧ ( x ¯ n − 1 ∨ x n ) ∧ ( x ¯ n ∨ x 1 )

can be removed if there are no other clauses with negative literals. State the erp rules.

382. [ 30 ] Although the techniques in the preceding exercises are computationally difficult to apply, show that a lookahead forest based on the dependency digraph can be used to discover some of those simplifications efficiently.

▸ 383. [ 23 ] ( Inprocessing. ) A SAT solver can partition its database of current clauses into two parts, the “hard” clauses Φ and the “soft” clauses Ψ. Initially Ψ is empty, while Φ is F , the set of all input clauses. Four kinds of changes are subsequently allowed:

  • Learning. We can append a new soft clause C , provided that Φ ∪ Ψ ∪ C is satisfiable whenever Φ ∪ Ψ is satisfiable.

  • Forgetting. We can discard (purge) any soft clause.

  • Hardening. We can reclassify any soft clause and call it hard.

  • Softening. We can reclassify any hard clause C and call it soft, provided that Φ is satisfiable whenever Φ \ C is satisfiable. In this case we also should output any necessary erp rules, which change the settings of variables in such a way that any solution to Φ \ C becomes a solution to Φ.

  1. Prove that, throughout any such procedure, F is satisfiable ⇔ Φ is satisfiable ⇔ Φ ∪ Ψ is satisfiable.

  2. Furthermore, given any solution to Φ, we obtain a solution to F by applying the erp rules in reverse order.

  3. What is wrong with the following scenario? Start with one hard clause, ( x ), and no soft clauses. Reclassify ( x ) as soft, using the erp rule x ← 1. Then append a new soft clause ( x ¯ ).

  4. If C is certifiable for Φ (see exercise 385 ), can we safely learn C ?

  5. If C is certifiable for Φ \ C , can we safely forget C ?

  6. In what cases is it legitimate to discard a clause, hard or soft, that is subsumed by another clause, hard or soft?

  7. In what cases is self- subsumption permissible?

  8. Explain how to eliminate all clauses that involve a particular variable x .

  9. Show that, if z is a new variable, we can safely learn the three new soft clauses ( x ∨ z ), ( y ∨ z ), ( x ¯ ∨ y ¯ ∨ z ¯ ) in Tseytin’s concept of extended resolution.

384. [ 25 ] Continuing the previous exercise, show that we can always safely forget any clause C that contains a literal l for which C ◊ C ′ is certifiable for Φ \ C whenever C ′ ∊ Φ contains l ¯ . What erp rule is appropriate?

385. [ 22 ] Clause C is called certifiable for a set of clauses F if F ∧ C ¯ ⊢ 1 ∈ , as in ( 119 ). It is said to be absorbed by F if it is nonempty and F ∧ C ⁢     \     l ¯ ⊢ 1 l for every l ∊ C , or if it is empty and F ⊢ 1 ∊. (Every clause of F is obviously absorbed by F .)

  1. True or false: If C is absorbed by F , it is certifiable for F .

  2. Which of { 1 ¯ , 1 ¯ 2 , 1 ¯ 23 } are implied by, certifiable for, or absorbed by R ′ in ( 7 )?

  3. If C is certifiable for F and if all clauses of F are absorbed by F ′, prove that C is certifiable for F ′.

  4. If C is absorbed by F and if all clauses of F are absorbed by F ′, prove that C is absorbed by F ′.

▸ 386. [ M31 ] Let Algorithm C 0 be a variant of Algorithm C that (i) makes all decisions at random; (ii) never forgets a learned clause; and (iii) restarts whenever a new clause has been learned. (Thus, step C5 ignores M p and M f ; step C6 chooses l uniformly at random from among the 2( n − F ) currently unassigned literals; step C8 backjumps while F > i 1 , instead of while F > i d ′+1 ; and after step C9 has stored a new clause, with d > 0, it simply sets d ← 0 and returns to C5. The data structures HEAP , HLOC , OVAL , ACT are no longer used.) We will prove that Algorithm C 0 is, nevertheless, quite powerful.

In the remainder of this exercise, F denotes the set of clauses known by Algorithm C 0 , both original and learned; in particular, the unit clauses of F will be the first literals L 0 , L 1 , ... , L i1−1 on the trail. If C is any clause and if l ∊ C , we define

s c o r e ⁢     ( F , C , l ) \= { ∞ | { l ′ |     F ∧ C ⁢   \   l   ¯ ⊢ 1     l ′ } | , ⁢     i f ⁢       F ∧ C ⁢       \       l ¯ ⁢ ⊢ 1   l ; o t h e r w i s e .

Thus score( F, C, l ) represents the total number of literals on the trail after making all the unforced decisions of C ¯ ⁢   \   l ¯ , if no conflict arises. We say that Algorithm C 0 performs a “ helpful round” for C and l if (i) every decision literal belongs to C ¯ ; and (ii) l ¯ is chosen as a decision literal only if the other elements of C ¯ are already in the trail.

  1. Let C be certifiable for F , and suppose that score( F, C, l ) < ∞ for some l ∊ C . Prove that if F ′ denotes F together with a clause learned on a round that’s helpful for C and l , then score( F ′, C,l ) > score( F, C, l ).

  2. Furthermore score( F ′, C, l ) ≥ score( F, C, l ) after an unhelpful round.

  3. Therefore C will be absorbed by the set F ′ of known clauses after at most | C | n helpful rounds have occurred.

  4. If | C | = k , show that Pr(helpful round) ≥ ( k − 1)!/(2 n ) k ≥ 1/(4 n k ).

  5. Consequently, by exercise 385(c) , if there exists a certificate of unsatisfiability ( C 1 , ..., C t ) for a family of clauses F with n variables, Algorithm C 0 will prove F unsatisfiable after learning an average of μ ≤ 4 Σ i \= 1 t ⁢ | C i | n 1 + | C i | clauses. (And it will q.s. need to learn at most μn ln n clauses, by exercise MPR–102.)

▸ 387. [ 21 ] Graph G is said to be embedded in graph G ′ if every vertex v of G corresponds to a distinct vertex v ′ of G ′, where u ′ −−− v ′ in G ′ whenever u −−− v in G . Explain how to construct clauses that are satisfiable if and only if G can be embedded in G ′.

388. [ 20 ] Show that the problems of deciding whether or not a given graph G (a) contains a k - clique, (b) can be k -colored, or (c) has a Hamiltonian cycle can all be regarded as graph embedding problems.

▸ 389. [ 22 ] In this 4 × 4 diagram, it’s possible to trace out the phrase ‘ THE ⊔ ART ⊔ OF ⊔ COMPUTER ⊔ PROGRAMMING ’ by making only king moves and knight moves, except for the final step from N to G .

images

Rearrange the letters so that the entire phrase can be traced.

▸ 390. [ 23 ] Let G be a graph with vertices V , edges E , | E | = m , | V | = n , and s, t ∊ V .

  1. Construct O ( kn ) clauses that are satisfiable if and only if there’s a path of length k or less from s to t , given k .

  2. Construct O ( m ) clauses that are satisfiable if and only if there’s at least one path from s to t .

  3. Construct O ( n 2 ) clauses that are satisfiable if and only if G is connected.

  4. Construct O ( km ) clauses that are un satisfiable if and only if there’s a path of length k or less from s to t , given k .

  5. Construct O ( m ) clauses that are un satisfiable if and only if there’s at least one path from s to t .

  6. Construct O ( m ) clauses that are un satisfiable if and only if G is connected. (This construction is much better than (c), in a sparse graph.)

391. [ M25 ] The values of two integer variables satisfy 0 ≤ x, y < d , and they are to be represented as l -bit quantities x l −1 ... x 0 , y l −1 ... y 0 , where l = ⌈lg d ⌉. Specify three different ways to encode the relation x ≠ y :

  1. Let x = ( x l −1 ... x 0 ) 2 and y = ( y l −1 ... y 0 ) 2 ; and let the encoding enforce the conditions ( x l −1 ... x 0 ) 2 < d , and ( y l −1 ... y 0 ) 2 < d , as well as ensuring that x ≠ y by introducing 2 l + 1 additional clauses in l auxiliary variables.

  2. Like (a), but there are d additional clauses (not 2 l + 1), and no auxiliaries.

  3. All bit patterns x l −1 ... x 0 and y l −1 ... y 0 are valid, but some values might have two different patterns. The encoding has d clauses and no auxiliary variables.

392. [ 22 ] The blank spaces in the following diagrams can be filled with letters in such a way that all occurrences of the same letter are rookwise connected:

images

  1. Demonstrate how to do it. (Puzzle ( i ) is easy; the others less so.)

  2. Similarly, solve the following puzzles — but use kingwise connectedness instead.

    images

  3. Construct clauses with which a SAT solver can solve general puzzles of this kind: Given a graph G and disjoint sets of vertices T 1 , T 2 , ... , T t , a solution should exhibit disjoint connected sets of vertices S 1 , S 2 , ... , S t , with T j ⊆ S j for 1 ≤ j ≤ t .

393. [ 25 ] (T. R. Dawson, 1911.) Show that it’s possible for each white piece in the accompanying chess diagram to capture the corresponding black piece, via a path that doesn’t intersect any of the other paths. How can SAT help to solve this problem?

images

394. [ 25 ] One way to encode the at-most-one constraint S ≤1 ( y 1 , ..., y p ) is to introduce l = ⌈lg p ⌉ auxiliary variables together with the following nl + n − 2 l clauses, which essentially “broadcast” the value of j when y j becomes true:

( y ¯ j ∨ ( − 1 ) b t     a t )             f o r ⁢     1 ≤ j ≤ p ,   1 ≤ t ≤ q \= ⌊ lg ( 2 p − j ) ⌋ ,   where   2 p − j \= ( 1 b 1 ⁢   ... ⁢   b q ) 2 .

For example, the clauses when p = 3 are ( y ¯ 1 ∨ a 1 ) ∧ ( y ¯ 1 ∨ a ¯ 2 ) ∧ ( y ¯ 2 ∨ a 1 ) ∧ ( y ¯ 2 ∨ a 2 ) ∧ ( y ¯ 3 ∨ a ¯ 1 ) .

Experiment with this encoding by applying it to Langford’s problem, using it in place of ( 13 ) whenever p ≥ 7.

395. [ 20 ] What clauses should replace ( 15 ), ( 16 ), and ( 17 ) if we want to use the order encoding for a graph coloring problem?

▸ 396. [ 23 ] ( Double clique hints. ) If x has one of the d values {0, 1, ..., d − 1}, we can represent it binarywise with respect to two different orderings by letting x j = [ x ≥ j ] and x ^ j \= [ x π ≥ j ] for 1 ≤ j < d , where π is any given permutation. For example, if d = 4 and (0 π , 1 π , 2 π , 3 π ) = (2, 3, 0, 1), the representations x 1 x 2 x 3 : x ^ 1 x ^ 2 x ^ 3 of 0, 1, 2, and 3 are respectively 000:110, 100:111, 110:000, and 111:100. This double ordering allows us to encode graph coloring problems by including not only the hints ( 162 ) but also

( v ^ 1 d − k + 1 ¯ ∨ ⋅ ⋅ ⋅ ∨ v ^ k d − k + 1 ¯ ⁢ ) ∧ ( v ^ 1 k − 1 ∨ ⋅ ⋅ ⋅ ∨ v ^ k k − 1 ) ,

whenever the vertices { v 1 , ..., v k } form a k -clique.

Explain how to construct clauses for this encoding, and experiment with coloring the n × n queens graph when (0 π , 1 π , 2 π , 3 π , 4 π,... ) = (0, d −1, 1, d −2, 2, ... ) is the inverse of the organ-pipe permutation.

▸ 397. [ 22 ] (N. Tamura, 2014.) Suppose x 0 , x 1 , ... , x p −1 are integer variables with the range 0 ≤ x i < d , represented in order encoding by Boolean variables x i j \= [ x i ≥ j ] for 0 ≤ i < p and 1 ≤ j < d . Show that the all-different constraint, “ x i ≠ x j for 0 ≤ i < j < p ,” can be nicely encoded by introducing auxiliary integer variables y 0 , y 1 , ... , y d −1 with the range 0 ≤ y j < p , represented in order encoding by Boolean variables y i j \= [ y i ≥ j ] for 1 ≤ i < p and 0 ≤ j < d , and by devising clauses to enforce the condition x i = j ⇒ y j = i . Furthermore, hints analogous to ( 162 ) can be given.

398. [ 18 ] Continuing exercise 397 , what’s an appropriate way to enforce the alldifferent constraint when x 0 , ... , x p −1 are represented in the direct encoding?

▸ 399. [ 23 ] If the variables u and v range over d values {1, ..., d }, it’s natural to encode them directly as sequences u 1 ... u d and v 1 ... v d , where u i = [ u = i ] and v j = [ v = j ], using the at-least-one clauses ( 15 ) and the at-most-one clauses ( 17 ). A binary constraint tells us which pairs ( i, j ) are legal; for example, the graph-coloring constraint says that i ≠ j when i and j are the colors of adjacent vertices in some graph.

One way to specify such a constraint is to assert the preclusion clauses ( u ¯ i ∨ v ¯ j ) for all illegal pairs ( i, j ), as we did for graph coloring in ( 16 ). But there’s also another general way: We can assert the support clauses

Λ i \= 1 d ( u ¯ i ∨ V { v j | ( i , j ) ⁢   is ⁢   legal } ) ∧ Λ j \= 1 d ( v ¯ j ∨ V { u i ⁢ |   ( i , j )     i s ⁢   l e g a l } )

instead. Graph coloring with d colors would then be represented by clauses such as ( ū 3 ∨ v 1 ∨ v 2 ∨ v 4 ∨ ··· ∨ v d ), when u and v are adjacent.

  1. Suppose t of the d 2 pairs ( i, j ) are legal. How many preclusion clauses are needed? How many support clauses?

  2. Prove that the support clauses are always at least as strong as the preclusion clauses, in the sense that all consequences of the preclusion clauses under unit propagation are also consequences of the support clauses under unit propagation, given any partial assignment to the binary variables { u 1 , ..., u d , v 1 , ..., v d }.

  3. Conversely, in the case of the graph-coloring constraint, the preclusion clauses are also at least as strong as the support clauses (hence equally strong).

  4. However, exhibit a binary constraint for which the support clauses are strictly stronger than the preclusion clauses.

400. [ 25 ] Experiment with preclusion clauses versus support clauses by applying them to the n queens problem. Use Algorithms L , C , and W for comparison.

401. [ 16 ] If x has the unary representation x 1 x 2 ... x d −1 , what is the unary representation of (a) y = ⌈ x /2⌉? (b) z = ⌊( x +1)/3⌋?

402. [ 18 ] If x has the unary representation x 1 x 2 ... x d −1 , encode the further condition that x is (a) even; (b) odd.

403. [ 20 ] Suppose x , y , z have the order encoding, with 0 ≤ x, y, z < d . What clauses enforce (a) min( x, y ) ≤ z ? (b) max( x, y ) ≤ z ? (c) min( x, y ) ≥ z ? (d) max( x, y ) ≥ z ?

▸ 404. [ 21 ] Continuing exercise 403 , encode the condition | x − y | ≥ a , for a given constant a ≥ 1, using d +1 − a clauses of length ≤ 4 and no auxiliary variables.

▸ 405. [ M23 ] The purpose of this exercise is to encode the constraint ax + by ≤ c , when a , b , c are integer constants, assuming that x , y are order-encoded with range [0 ..d ).

  1. Prove that it suffices to consider cases where a, b, c > 0.

  2. Exhibit a suitable encoding for the special case 13 x − 8 y ≤ 7, d = 8.

  3. Exhibit a suitable encoding for the special case 13 x − 8 y ≥ 1, d = 8.

  4. Specify an encoding that works for general a , b , c , d .

406. [ M24 ] Order-encode (a) xy ≤ a and (b) xy ≥ a , when a is an integer constant.

▸ 407. [ M22 ] If x , y , z are order-encoded, with 0 ≤ x, y < d and 0 ≤ z < 2 d −1, the clauses

Λ k \= 1 2 d − 2 ⁢       Λ j \= max ( 0 , k + 1 − d ) k ( x ¯ j ∨ y ¯ k − j ∨ z k )

are satisfiable if and only if x + y ≤ z ; this is the basic idea underlying ( 20 ). Another way to encode the same relation is to introduce new order-encoded variables u and v , and to construct clauses for the relations ⌊ x/ 2⌋ + ⌊ y/ 2⌋ ≤ u and ⌈ x/ 2⌉ + ⌈ y/ 2⌉ ≤ v , recursively using methods for numbers less than ⌈ d/ 2⌉ and ⌊ d/ 2⌋ + 1. Then we can finish the job by letting z 1 = v 1 , z 2 d −2 \= v d ( d even) or u d −1 ( d odd), and appending the clauses

( u ¯ j ∨ z 2 j ) ∧ ( v ¯ j + 1 ∨ z 2 j ) ∧ ( u ¯ j ∨ v ¯ j + 1 ∨ z 2 j + 1 ) , f o r ⁢       1 ≤ j ≤ d − 2.

  1. Explain why the alternative method is valid.

  2. For what values of d does that method produce fewer clauses?

  3. Consider analogous methods for the relation x + y ≥ z .

▸ 408. [ 25 ] ( Open shop scheduling. ) Consider a system of m machines and n jobs, together with an m × n matrix of nonnegative integer weights W = ( w ij ) that represent the amount of uninterrupted time on machine i that is needed by job j .

The open shop scheduling problem seeks a way to get all the work done in t units of time, without assigning two jobs simultaneously to the same machine and without having two machines simultaneously assigned to the same job. We want to minimize t , which is called the “ makespan” of the schedule.

For example, suppose m = n = 3 and W \= ( 703 172 235 ) . A “greedy” algorithm, which repeatedly fills the lexicographically smallest time slot ( t, i, j ) such that w ij > 0 but neither machine i nor job j have yet been scheduled at time t , achieves a makespan of 12 with the following schedule:

images

  1. Is 12 the optimum makespan for this W ?

  2. Prove that the greedy algorithm always produces a schedule whose makespan is less than ( max i \= 1 m Σ j \= 1 n w i j ) + ( max j \= 1 n ⁢ Σ i \= 1 m ⁢   w i j ) , unless W is entirely zero.

  3. Suppose machine i begins to work on job j at time s ij , when w ij > 0. What conditions should these starting times satisfy, in order to achieve the makespan t ?

  4. Show that the order encoding of these variables s ij yields SAT clauses that nicely represent any open shop scheduling problem.

  5. Let ⌊ W/k ⌋ be the matrix obtained by replacing each element w ij of W by ⌊ w ij / k ⌋. Prove that if the open shop scheduling problem for ⌊ W/k ⌋ and t is unsatisfiable, so is the problem for W and kt .

▸ 409. [ M26 ] Continuing exercise 408 , find the best makespans in the following cases:

  1. m = 3, n = 3 r + 1; w 1 j \= w 2( r + j ) \= w 3(2 r +j) \= a j for 1 ≤ j ≤ r ; w 1 n \= w 2 n \= w 3 n \= ⌊( a 1 + ··· + a r )/2⌋; otherwise w ij = 0. (The positive integers a j are given.)

  2. m = 4, n = r +2; w 1 j \= ( r +1) a j and w 2 j \= 1 for 1 ≤ j ≤ r ; w 2( n −1) \= w 2 n \= ( r +1) ⌊( a 1 + ··· + a r )/2⌋; w 3( n −1) \= w 4 n \= w 2 n + r ; otherwise w ij = 0.

  3. m = n ; w jj = n − 2, w jn = w nj = 1 for 1 ≤ j < n ; otherwise w ij = 0.

  4. m = 2; w 1 j \= a j and w 2 j \= b j for 1 ≤ j ≤ n , where a 1 +··· + a n = b 1 + ··· + b n = s and a j + b j ≤ s for 1 ≤ j ≤ n .

410. [ 24 ] Exhibit clauses for the constraint 13 x −8 y ≤ 7 when x and y are log- encoded as 3-bit integers x = ( x 2 x 1 x 0 ) 2 and y = ( y 2 y 1 y 0 ) 2 . (Compare with exercise 405(b) .)

▸ 411. [ 25 ] If x = ( x m ... x 1 ) 2 , y = ( y n ... y 1 ) 2 , and z = ( z m + n ... z 1 ) 2 stand for binary numbers, the text explains how to encode the relation xy = z with fewer than 20 mn clauses, using Napier– Dadda multiplication. Explain how to encode the relations xy ≤ z and xy ≥ z with fewer than 9 mn and 11 mn clauses, respectively.

412. [ 40 ] Experiment with the encoding of somewhat large numbers by using a radix- d representation in which each digit has the order encoding.

413. [ M22 ] Find all CNF formulas for the function ( x 1 ⊕ y 1 ) ∨ ··· ∨ ( x n ⊕ y n ).

414. [ M20 ] How many clauses will remain after the auxiliary variables a 1 , ... , a n −1 of ( 169 ) have been eliminated by resolution?

▸ 415. [ M27 ] Generalize ( 169 ) to an encoding of lexicographic order on d -ary vectors, ( x 1 ... x n ) d ≤ ( y 1 ... y n ) d , where each x k \= x k 1 ⁢   + ⋅ ⋅ ⋅ + x k d − 1 and y k \= y k 1 ⁢   + ⋅ ⋅ ⋅ + y k d − 1 has the order encoding. What modifications to your construction will encode the strict relation x 1 ... x n < y 1 ... y n ?

416. [ 20 ] Encode the condition ‘if x 1 ... x n = y 1 ... y n then u 1 ... u m = v 1 ... v m ’, using 2 m +2 n +1 clauses and n +1 auxiliary variables. Hint: 2 n of the clauses are in ( 172 ).

417. [ 21 ] Continuing exercise 42 , what is the Tseytin encoding of the ternary mux operation ‘ s ← t ? u : v ’ ? Use it to justify the translation of branching programs via ( 173 ).

418. [ 23 ] Use a branching program to construct clauses that are satisfiable if and only if ( x ij ) is an m × n Boolean matrix whose rows satisfy the hidden weighted bit function h n and whose columns satisfy the complementary function h ¯ m . In other words,

r i \= Σ j \= 1 n x i j , c j \= Σ i \= 1 m x i j ⁢       a n d ⁢     x i r i \= 1 ,     x c j j \= 0 ,   a s s u ⁢ min g ⁢   that   x i 0 \= x 0 j \= 0.

419. [ M21 ] If m, n ≥ 3, find (by hand) all solutions to the problem of exercise 418 such that (a) ∑ x ij = m + 1 (the minimum); (b) ∑ x ij = mn − n − 1 (the maximum).

420. [ 18 ] Derive ( 175 ) mechanically (that is, “without thinking”) from the Boolean chain s ← x 1 ⊕ x 2 , c ← x 1 ∧ x 2 , t ← s ⊕ x 3 , c ′ ← s ∧ x 3 , requiring c = c ′ = 0.

421. [ 18 ] Derive ( 176 ) mechanically from the branching program I 5 \= ( 1 ¯ ? 4 : 3 ) , I 4 \= ( 2 ¯ ? 1 : 2 ) , I 3 \= ( 2 ¯ ? 2 : 0 ) , I 2 \= ( 3 ¯ ? 1 : 0 ) , beginning at I 5 .

422. [ 11 ] What does unit propagation deduce when the additional clause ( x 1 ) or ( x 2 ) is appended to (a) F in ( 175 )? (b) G in ( 176 )?

423. [ 22 ] A representation F that satisfies a condition like ( 180 ) but with l replaced by can be called “ weakly forcing.” Exercise 422 shows that ( 175 ) and ( 176 ) are weakly forcing. Does the BDD of every function define a weakly forcing encoding, via ( 173 )?

▸ 424. [ 20 ] The dual of the Pi function has the prime clauses { 1 ¯ 2 ¯ 3 ¯ , 1 ¯ 3 ¯ 4 ¯ , 2 3 ¯ 4 ¯ , 234 , 12 } (see 7.1.1–(30)). Can any of them be omitted from a forcing representation?

425. [ 18 ] A clause with exactly one positive literal is called a definite Horn clause, and Algorithm 7.1.1C computes the “ core” of such clauses. If F consists of definite Horn clauses, prove that x is in the core if and only if F ⊢ 1 x , if and only if F ∧ ( x ¯ ) ⊢ 1 ∈ .

▸ 426. [ M20 ] Suppose F is a set of clauses that represent f ( x 1 , ..., x n ) using auxiliary variables { a 1 , ..., a m } as in ( 170 ), where m > 0. Let G be the clauses that result after variable a m has been eliminated as in ( 112 ).

  1. True or false: If F is forcing then G is forcing.

  2. True or false: If F is not forcing then G is not forcing.

427. [ M30 ] Exhibit a function f ( x 1 , ..., x n ) for which every set of forcing clauses that uses no auxiliary variables has size Ω(3 n / n 2 ), although f can actually be represented by a polynomial number of forcing clauses when auxiliary variables are introduced. Hint: See exercise 7.1.1–116.

428. [ M27 ] A generic graph G on vertices {1, ..., n } can be characterized by ( n 2 ) Boolean variables X = { x ij | 1 ≤ i < j ≤ n }, where x ij = [ i −−− j in G ]. Properties of G can therefore be regarded as Boolean functions, f ( X ).

  1. Let f nd ( X ) = [ χ ( G ) ≤ d ]; that is, f nd is true if and only if G has a d -coloring. Construct clauses F nd that represent the function f nd ( X ) ∨ y , using auxiliary variables Z = { z jk | 1 ≤ j ≤ n , 1 ≤ k ≤ d } that mean “vertex j has color k .”

  2. Let G nd be a forcing representation of the Boolean function F nd ( X, y, Z ), and suppose that G nd has M clauses in N variables. (These N variables should include the ( n 2 ) + 1 + n d variables of F nd , along with an arbitrary number of additional auxiliaries.) Explain how to construct a monotone Boolean chain of cost O ( MN 2 ) for the function f ¯ n d (see exercise 7.1.2–84), given the clauses of G nd .

Note: Noga Alon and Ravi B. Boppana, Combinatorica 7 (1987), 1–22, proved that every monotone chain for this function has length exp Ω(( n/ log n ) 1/3 ) when d +1 = ⌊( n / lg n ) 2/3 /4⌋. Hence M and N cannot both be of polynomial size.

429. [ 22 ] Prove that Bailleux and Boufkhad’s clauses ( 20 ), ( 21 ) are forcing: If any r of the x ’s have been set to 1, then unit propagation will force all the others to 0.

430. [ 25 ] Similarly, Sinz’s clauses ( 18 ) and ( 19 ) are forcing.

▸ 431. [ 20 ] Construct efficient, forcing clauses for the relation x 1 + ··· + x m ≤ y 1 + ··· + y n .

432. [ 24 ] Exercise 404 gives clauses for the relation | x − y | ≥ a . Are they forcing?

▸ 433. [ 25 ] Are the lexicographic-constraint clauses in ( 169 ) forcing?

434. [ 21 ] Let L l be the language defined by the regular expression 0 ∗ 1 l 0 ∗ ; in other words, the binary string x 1 ... x n is in L l if and only if it consists of zero or more 0s followed by exactly l 1s followed by zero or more 0s.

  1. Explain why the following clauses are satisfiable if and only if x 1 ... x n ∊ L l :
    (i) ( p ¯ k ∨ x ¯ k ) , ( p ¯ k ∨ p k − 1 ) , and ( p ¯ k − 1 ∨ x k ∨ p k ) for 1 ≤ k ≤ n , also ( p 0 ); (ii) ( q ¯ k ∨ x ¯ k ) , ( q ¯ k ∨ q k + 1 ) , and ( q ¯ k + 1 ∨ x k ∨ q k ) for 1 ≤ k ≤ n , also ( q n +1 ); (iii) ( r ¯ k ∨ p k − 1 ) ∧ Λ 0 ≤ d < l ( r ¯ k ∨ x k + d ) ∧ ( r ¯ k ∨ q k + 1 ) for 1 ≤ k ≤ n +1 − l , also ( r 1 ∨ ··· ∨ r n +1− l ).

  2. Show that those clauses are forcing when l = 1 but not when l = 2.

▸ 435. [ 28 ] Given l ≥ 2, construct a set of O ( n log l ) clauses that characterize the language L l of exercise 434 and are forcing.

436. [ M32 ] ( Nondeterministic finite-state automata .) A regular language L on the alphabet A can be defined in the following well-known way: Let Q be a finite set of “states,” and let I ⊆ Q and O ⊆ Q be designated “ input states” and “ output states.” Also let T ⊆ Q × A × Q be a set of “transition rules.” Then the string x 1 ... x n is in L if and only if there’s a sequence of states q 0 , q 1 , ... , q n such that q 0 ∊ I , ( q k −1 , x k , q k ) ∊ T for 1 ≤ k ≤ n , and q n ∊ O .

Given such a definition, where A = {0, 1}, use auxiliary variables to construct clauses that are satisfiable if and only if x 1 ... x n ∊ L . The clauses should be forcing, and there should be at most O ( n | T |) of them.

As an example, write out the clauses for the language L 2 = 0 ∗ 1 2 0 ∗ of exercise 434 .

437. [ M21 ] Extend exercise 436 to the general case where A has more than two letters.

438. [ 21 ] Construct a set of forcing clauses that are satisfiable if and only if a given binary string x 1 ... x n contains exactly t runs of 1s, having lengths ( l 1 , l 2 , ..., l t ) from left to right. (Equivalently, the string x 1 ... x n should belong to the language defined by the regular expression 0 ∗ 1 l 1 0 + 1 l 2 0 + ... 0 + 1 l t 0 ∗ .)

▸ 439. [ 30 ] Find efficient forcing clauses for the constraint that x 1 + ··· + x n = t and that there are no two consecutive 1s. (This is the special case l 1 = ··· = l t = 1 of the previous exercise, but a much simpler construction is possible.)

440. [ M33 ] Extend exercise 436 to context free languages , which can be defined by a set S ⊆ N and by production rules U and W of the following well-known forms: U ⊆ { P → a | P ∊ N, a ∊ A } and W ⊆ { P → QR | P, Q, R ∊ N }, where N is a set of “ nonterminal symbols.” A string x 1 ... x n with each x j ∊ A belongs to the language if and only if it can be produced from a nonterminal symbol P ∊ S .

441. [ M35 ] Show that any threshold function f ( x 1 , ..., x n ) = [ w 1 x 1 + ··· + w n x n ≥ t ] has a forcing representation whose size is polynomial in log | w 1 | + ··· + log | w n |.

▸ 442. [ M27 ] The unit propagation relation ⊢ 1 can be generalized to k th order propagation ⊢ k as follows: Let F be a family of clauses and let l be a literal. If ( l 1 , l 2 , ..., l p ) is a sequence of literals, we write L q − \= { l 1 , ... , l q − 1 , l ¯ q } for 1 ≤ q ≤ p . Then

F ⊢ 0 l ⇔ ∈ ∊ F ; F ⊢ k + 1   l ⇔ F | L 1 − ⊢ k ∈ , F ⁢ |   L 2 − ⊢ k     ∈ ,   ... ⁢ , a n d ⁢   F |   L p − ⊢ k ∈ ⁢                       for some strictly distinct literals l 1 , l 2 , ... ⁢   , l p ⁢   with l p \= l ; F ⊢ k   ∈ ⇔ F ⊢ k   l ⁢     and F ⊢ k       l ¯ ⁢       for some literal l .

  1. Verify that ⊢ 1 corresponds to unit propagation according to this definition.

  2. Describe ⊢ 2 informally, using the concept of “ failed literals.”

  3. Prove that F ⊢ k or F ⊢ k     l ¯ implies F | l ⊢ k ∊ for all literals l , and furthermore that F ⊢ k ∊ implies F ⊢ k +1 ∊, for all k ≥ 0.

  4. True or false: F ⊢ k l implies F ⊢ k +1 l .

  5. Let L k ( F ) = { l | F ⊢ k l }. What is L k ( R ′), where R ′ appears in ( 7 ) and k ≥ 0? f) Given k ≥ 1, explain how to compute L k ( F ) and F | L k ( F ) in O ( n 2 k −1 m ) steps, when F has m clauses in n variables.

443. [ M24 ] ( A hierarchy of hardness .) Continuing the previous exercise, a family of clauses F is said to belong to class UC k if it has the property that

F | L ⊢ ∊ implies F | L ⊢ k ∊ for all sets of strictly distinct literals L .

(“Whenever a partial assignment yields unsatisfiable clauses, the inconsistency can be detected by k th order propagation.”) And F is said to belong to class PC k if

F | L ⊢ l implies F | L ⊢ k l for all sets of strictly distinct literals L ∪ l .

  1. Prove that PC 0 ⊂ UC 0 ⊂ PC 1 ⊂ UC 1 ⊂ PC 2 ⊂ UC 2 ⊂ ··· , where the set inclusions are strict (each class is contained in but unequal to its successor).

  2. Describe all families F that belong to the smallest class, PC 0 .

  3. Give interesting examples of families in the next smallest class, UC 0 .

  4. True or false: If F contains n variables, F ∊ PC n .

  5. True or false: If F contains n variables, F ∊ UC n −1 .

  6. Where do the clauses R ′ of ( 7 ) fall in the hierarchy?

444. [ M26 ] The following single lookahead unit resolution algorithm, called SLUR, returns either ‘sat’, ‘unsat’, or ‘maybe’, depending on whether a given set F of clauses is satisfiable, unsatisfiable, or beyond its ability to decide via easy propagations:

E1. [Propagate.] If F ⊢ 1 , terminate (‘unsat’). Otherwise set F ← F |{ l | F ⊢ 1 l }.

E2. [Satisfied?] If F = ∅, terminate (‘sat’). Otherwise set l to any literal within F .

E3. [Lookahead and propagate.] If F |     l ⁢     ⊬ 1 ∈ , set F ← F | l |{ l ′ | F | l ⊢ 1 l ′} and return to E2. Otherwise if F |     l ¯ ⁢     ⊬ 1 ∈ , F ← F | l ¯ | { l ′   | F |   l ¯ ⁢   ⊢ 1     l ′ ⁢ } and return to E2. Otherwise terminate (‘maybe’).

Notice that this algorithm doesn’t backtrack after committing itself in E2 to either l or l ¯ .

  1. If F consists of Horn clauses, possibly renamed (see exercise 7.1.1–55), prove that SLUR will never return ‘maybe’, regardless of how it chooses l in step E2.

  2. Find four clauses F on three variables such that SLUR always returns ‘sat’, although F is not a set of possibly renamed Horn clauses.

  3. Prove that SLUR never returns ‘maybe’ if and only if F ∊ UC 1 (see exercise 443 ).

  4. Explain how to implement SLUR in linear time with respect to total clause length.

▸ 445. [ 22 ] Find short certificates of unsatisfiability for the pigeonhole clauses ( 106 )–( 107 ), when they are supplemented by (a) ( 181 ); (b) ( 182 ); (c) ( 183 ).

446. [ M10 ] What’s the maximum number of edges in a subgraph of K m,n that has girth ≥ 6? (Express your answer in terms of Z ( m, n ).)

▸ 447. [ 22 ] Determine the maximum number of edges in a girth-8 subgraph of K 8,8 .

448. [ M25 ] What is Z ( m, n ) when m is odd and n = m ( m −1)/6? Hint: See 6.5–(16).

449. [ 21 ] Exhibit n × n quad-free matrices that contain the maximum number of 1s and obey the lexicographic constraints ( 185 ), ( 186 ), for 8 ≤ n ≤ 16.

450. [ 25 ] Prove that there is essentially only one 10 × 10 quad-free system of points and lines with 34 incidences. Hint: First show that every line must contain either 3 points or 4 points; hence every point must belong to either 3 lines or 4 lines.

▸ 451. [ 28 ] Find a way to color the squares of a 10 × 10 board with three colors, so that no rectangle has four corners of the same color. Prove furthermore that every such “ nonchromatic rectangle” board has the color distribution {34, 34, 32}, not {34, 33, 33}. But show that if any square of the board is removed, a nonchromatic rectangle is possible with 33 squares of each color.

452. [ 34 ] Find a nonchromatic rectangle with four colors on an 18 × 18 board.

453. [ M23 ] An m × n matrix X = ( x ij ) is said to be decomposable if it has row indices R ⊆ {1, ..., m } and column indices C ⊆ {1, ..., n } such that 0 < | R | + | C | < m + n , with x ij = 0 whenever ( i ∊ R and j ∉ C ) or ( i ∉ R and j ∊ C ). It represents a bipartite graph on the vertices { u 1 , ..., u m } and { v 1 , ..., v n }, if [ u i −−− v j ] = [ x ij ≠ 0].

  1. Prove that X is indecomposable if and only if its bipartite graph is connected.

  2. The direct sum X ′ ⊕ X ′ of matrices X ′ and X ″, where X ′ is m ′ × n ′ and X ″ is m ″ × n ″, is the ( m ′ + m ″) × ( n ′ + n ″) “ block diagonal” matrix X that has X ′ in its upper left corner, X ″ in the lower right corner, and zeros elsewhere (see 7–( 40 )). True or false: If the rows and columns of X ′ and X ″ are nonnegative and lexicographically ordered as in ( 185 ) and ( 186 ), so are the rows and columns of X .

  3. Let X be any nonnegative matrix whose rows and columns are lexicographically nonincreasing, as in ( 185 ) and ( 186 ). True or false: X is decomposable if and only if X is a direct sum of smaller matrices X ′ and X ″.

454. [ 15 ] If τ is an endomorphism for the solutions of f , show that f ( x ) = f ( xτ ) for every cyclic element x (every element that’s in a cycle of τ ).

455. [ M20 ] Suppose we know that ( 187 ) is an endomorphism of some given clauses F on the variables { x 1 , x 2 , x 3 , x 4 }. Can we be sure that F is satisfiable if and only if F ∧ C is satisfiable, when (a) C \= 1 ¯ 2 4 ¯ , i.e., C \= ( x ¯ 1 ∨ x 2 ∨ x ¯ 4 ) ? (b) C \= 2 3 ¯ 4 ¯ ? (c) C = 123? (d) C \= 1 3 ¯ 4 ?

456. [ M21 ] For how many functions f ( x 1 , x 2 , x 3 , x 4 ) is ( 187 ) an endomorphism?

457. [ HM19 ] Show that every Boolean f ( x 1 , x 2 , x 3 , x 4 ) has more than 51 quadrillion endomorphisms, and an n -variable function has more than 2 2 n ( n −1) .

458. [ 20 ] The simplification of clauses by removing an autarky can be regarded as the exploitation of an endomorphism. Explain why.

▸ 459. [ 20 ] Let X ij denote the submatrix of X consisting of the first i rows and the first j columns. Show that the numbers sweep( X ij ) satisfy a simple recurrence, from which it’s easy to compute sweep( X ) = sweep( X mn ).

460. [ 21 ] Given m , n , k , and r , construct clauses that are satisfied by an m × n binary matrix X = ( x ij ) if and only if sweep( X ) ≤ k and ∑ i,j x ij ≥ r .

461. [ 20 ] What additional clauses will rule out non- fixed points of τ 1 and τ 2 ?

462. [ M22 ] Explain why τ 1 , τ 2 , and τ 3 preserve satisfiability in the sweep problem.

▸ 463. [ M21 ] Show that X is a fixed point of τ 1 , τ 2 , and τ 3 if and only if its rows and columns are nondecreasing. Therefore the maximum of νX = ∑ i,j x ij over all binary matrices of sweep k is a simple function of m , n , and k .

▸ 464. [ M25 ] Transformations τ 1 and τ 2 don’t change the text’s example 10×10 matrix. Prove that they will never change any 10 × 10 matrix of sweep 3 that has ν X = 51.

465. [ M21 ] Justify the text’s rule for simultaneous endomorphisms in the perfect matching problem: Any perfect matching must lead to one that’s fixed by every τ uv .

466. [ M23 ] Prove that when mn is even, the text’s even-odd rule ( 190 ) for endomorphisms of m × n domino coverings has exactly one fixed point.

467. [ 20 ] Mutilate the 7×8 and 8×7 boards by removing the upper right and lower left cells. What domino coverings are fixed by all the even-odd endomorphisms like ( 190 )?

468. [ 20 ] Experiment with the mutilated chessboard problem when the even-odd endomorphisms are modified so that (a) they use the same rule for all i and j ; or (b) they each make an independent random choice between horizontal and vertical.

▸ 469. [ M25 ] Find a certificate of unsatisfiability ( C 1 , C 2 , ..., C t ) for the fact that an 8 × 8 chessboard minus cells (1, 8) and (8, 1) cannot be exactly covered by dominoes h ij and v ij that are fixed under all of the even-odd endomorphisms. Each C k for 1 ≤ k < t should be a single positive literal. (Therefore the clauses for this problem belong to the relatively simple class PC 2 in the hierarchy of exercise 443 .)

▸ 470. [ M22 ] Another class of endomorphisms, one for every 4- cycle, can also be used in perfect matching problems: Let the vertices (instead of the edges) be totally ordered in some fashion. Every 4-cycle can be written v 0 −−− v 1 −−− v 2 −−− v 3 −−− v 0 , with v 0 > v 1 > v 3 and v 0 > v 2 ; the corresponding endomorphism changes any solution for which v 0 v 1 = v 2 v 3 = 1 by setting v 0 v 1 ← v 2 v 3 ← 0 and v 1 v 2 ← v 3 v 0 ← 1. Prove that every perfect matching leads to a fixed point of all these transformations.

471. [ 16 ] Find all fixed points of the mappings in exercise 470 when the graph is K 2n .

472. [ M25 ] Prove that even-odd endomorphisms such as ( 190 ) in the domino covering problem can be regarded as instances of the endomorphisms in exercise 470 .

▸ 473. [ M23 ] Generalize exercise 470 to endomorphisms for the unsatisfiable clauses of Tseytin’s graph parity problems in exercise 245 .

474. [ M20 ] A signed permutation σ is a symmetry of f ( x ) if and only if f ( x ) = f ( xσ ) for all x , and it is an antisymmetry if and only if we have f ( x ) \= f ¯ ( x σ ) for all x .

  1. How many signed permutations of n elements are possible?

  2. Write 75 1 ¯ 4 ¯ 2 ¯ 6 3 ¯ in cycle form, as an unsigned permutation of { 1 , ... , 7 ,   1 ¯ , ... , 7 ¯ } .

  3. For how many functions f of four variables is 4 ¯ 13 2 ¯ a symmetry?

  4. For how many functions f of four variables is 4 ¯ 13 2 ¯ an antisymmetry?

  5. For how many f ( x 1 , ..., x 7 ) is 75 1 ¯ 4 ¯ 2 ¯ 6 3 ¯ a symmetry or antisymmetry?

475. [ M22 ] Continuing exercise 474 , a Boolean function is called asymmetric if the identity is its only symmetry; it is totally asymmetric if it is asymmetric and has no anti symmetries.

  1. If f is totally asymmetric, how many functions are equivalent to f under the operations of permuting variables, complementing variables, and/or complementing the function?

  2. According to (a) and 7.1.1–(95), the function ( x ∨ y ) ∧ ( x ⊕ z ) is not totally asymmetric. What is its nontrivial symmetry?

  3. Prove that if f is not asymmetric, it has an automorphism of prime order p .

  4. Show that if ( u v w ) ( u ¯ v ¯ w ¯ ) is a symmetry of f , so is ( u v ) ( u ¯ v ¯ ) .

  5. Make a similar statement if f has a symmetry of the form ( u v w x y ) ( u ¯ v ¯ w ¯ x ¯ y ¯ ) .

  6. Conclude that, if n ≤ 5, the Boolean function f ( x 1 , ..., x n ) is totally asymmetric if and only if no signed involution is a symmetry or antisymmetry of f .

  7. However, exhibit a counterexample to that statement when n = 6.

476. [ M23 ] For n ≤ 5, find Boolean functions of n variables that are (a) asymmetric but not totally asymmetric; (b) totally asymmetric. Furthermore, your functions should be the easiest to evaluate (in the sense of having a smallest possible Boolean chain), among all functions that qualify. Hint: Combine exercises 475 and 477 .

▸ 477. [ 23 ] ( Optimum Boolean evaluation. ) Construct clauses that are satisfiable if and only if there is an r -step normal Boolean chain that computes m given functions g 1 , ... , g m on n variables. (For example, if n = 3 and g 1 = 〈 x 1 x 2 x 3 〉, g 2 = x 1 ⊕ x 2 ⊕ x 3 , such clauses with r = 4 and 5 enable a SAT solver to discover a “ full adder” of minimum cost; see 7.1.2–(1) and 7.1.2–(22).) Hint: Represent each bit of the truth tables.

▸ 478. [ 23 ] Suggest ways to break symmetry in the clauses of exercise 477 .

▸ 479. [ 25 ] Use SAT technology to find optimum circuits for the following problems:

  1. Compute z 2 , z 1 , and z 0 , when x 1 + x 2 + x 3 + x 4 = ( z 2 z 1 z 0 ) 2 (see 7.1.2–(27)).

  2. Compute z 2 , z 1 , and z 0 , when x 1 + x 2 + x 3 + x 4 + x 5 = ( z 2 z 1 z 0 ) 2 .

  3. Compute all four symmetric functions S 0 , S 1 , S 2 , S 3 of { x 1 , x 2 , x 3 }.

  4. Compute all five symmetric functions S 0 , S 1 , S 2 , S 3 , S 4 of { x 1 , x 2 , x 3 , x 4 }.

  5. Compute the symmetric function S 3 ( x 1 , x 2 , x 3 , x 4 , x 5 , x 6 ).

  6. Compute the symmetric function S 0,4 ( x 1 , ..., x 6 ) = [( x 1 + ··· + x 6 ) mod 4 = 0].

  7. Compute all eight minterms of { x 1 , x 2 , x 3 } (see 7.1.2–(30)).

480. [ 25 ] Suppose the values 0, 1, 2 are encoded by the two-bit codes x l x r = 00, 01, and 1∗, respectively, where 10 and 11 both represent 2. (See Eq. 7.1.3–(120).)

  1. Find an optimum circuit for mod 3 addition: z l z r = ( x l x r + y l y r )mod 3.

  2. Find an optimum circuit that computes z l z r = ( x 1 + x 2 + x 3 + y l y r )mod 3.

  3. Conclude that [ x 1 + ··· + x n ≡ a (modulo 3)] can be computed in < 3 n steps.

▸ 481. [ 28 ] An ordered bit pair xy can be encoded by another ordered bit pair ⟦ xy ⟧ = ( x ⊕ y ) y without loss of information, because ⟦ xy ⟧ = uv implies ⟦ uv ⟧ = xy .

  1. Find an optimum circuit that computes (⟦ zz ′⟧) 2 = x 1 + x 2 + x 3 .

  2. Let ν ⟦ uv ⟧ = ( u ⊕ v )+ v , and note that ν ⟦00⟧ = 0, ν ⟦01⟧ = 2, ν ⟦1∗⟧ = 1. Find an optimum circuit that, given x 1 ... x 5 , computes z 1 z 2 z 3 such that we have ν ⟦ x 1 x 2 ⟧ + ν ⟦ x 3 x 4 ⟧ + x 5 = 2 ν ⟦ z 1 z 2 ⟧ + z 3 .

  3. Use that circuit to prove by induction that the “ sideways sum” ( z ⌊lg n ⌋ ... z 1 z 0 ) 2 = x 1 + x 2 + ··· + x n can always be computed with fewer than 4 . 5 n gates.

▸ 482. [ 26 ] ( Erd˝os discrepancy patterns. ) The binary sequence y 1 ... y t is called strongly balanced if we have | Σ j \= 1 k ( 2 y i − 1 ) | ≤ 2 for 1 ≤ k ≤ t .

  1. Show that this balance condition needs to be checked only for odd k ≥ 3.

  2. Describe clauses that efficiently characterize a strongly balanced sequence.

  3. Construct clauses that are satisfied by x 1 x 2 ... x n if and only if x d x 2d ... x ⌊ n/d ⌋ d is strongly balanced for 1 ≤ d ≤ n .

483. [ 21 ] Symmetry between colors was broken in the coloring problems of Table 6 by assigning fixed colors to a large clique in each graph. But many graphs have no large clique, so a different strategy is necessary. Explain how to encode the “ restricted growth string” principle (see Section 7.2.1.5) with appropriate clauses, given an ordering v 1 v 2 ... v n of the vertices: The color of v j must be at most one greater than the largest color assigned to { v 1 , ..., v j −1 }. (In particular, v 1 always has color 1.)

Experiment with this scheme by applying it to the Mycielski graphs of exercise 7.2.2.1–116 .

484. [ 22 ] ( Graph quenching. ) A graph with vertices ( v 1 , ..., v n ) is called “ quenchable” if either (i) n = 1; or (ii) there’s a k such that v k −−− v k +1 and the graph on ( v 1 , ..., v k −1 , v k +1 , ..., v n ) can be quenched; or (iii) there’s an l such that v l −−− v l +3 and the graph on ( v 1 , ..., v l −1 , v l +3 , v l +1 , v l +2 , v l +4 , ..., v n ) can be quenched.

  1. Find a 4-element graph that is quenchable although v 3 ― v 4 .

  2. Construct clauses that are satisfiable if and only if a given graph is quenchable. Hint: Use the following three kinds of variables for this model-checking problem: x t,i,j = [ v i −−− v j at time t ], for 1 ≤ i < j ≤ n − t ; q t,k = [a quenching move of type (ii) leads to time t + 1]; s t,l = [a quenching move of type (iii) leads to time t + 1].

▸ 485. [ 23 ] Sometimes successive transitions in the previous exercise are commutative: For example, the effect of q t,k and q t +1, k +1 is the same as q t , k +2 and q t +1, k . Explain how to break symmetry in such cases, by allowing only one of the two possibilities.

486. [ 21 ] ( Late Binding Solitaire .) Shuffle a deck and deal out 18 cards; then try to reduce these 18 piles to a single pile, using a sequence of “captures” in which one pile is placed on top of another pile. A pile can capture only the pile to its immediate left, or the pile found by skipping left over two other piles. Furthermore a capture is permitted only if the top card in the capturing pile has the same suit or the same rank as the top card in the captured pile. For example, consider the following deal:

J ♡ 5 ♡ 10♣ 8 ♢ J ♣ A ♣ K ♠ A ♡ 4♣ 8♠ 5♠ 5 ♢ 2 ♢ 10♠ A♠ 6♡ 3♡ 10♢

Eleven captures are initially possible, including 5 ♡ × J ♡, A ♣×× 10 ♣, and 5 ♢ × 5 ♠. Some captures then make others possible, as in 8 ♠ ×× K ♠×× 8 ♢.

If captures must be made “greedily” from left to right as soon as possible, this game is the same as the first 18 steps of a classic one-player game called “ Idle Year,” and we wind up with five piles [see Dick’s Games of Patience (1883), 50–52]. But if we cleverly hold back until all 18 cards have been dealt, we can do much better.

Show that one can win from this position, but not if the first move is A ♣× J ♣.

▸ 487. [ 27 ] There are ( 64 8 ) \= 4426165368 ways to place eight queens on a chessboard. Long ago, W. H. Turton asked which of them causes the maximum number of vacant squares to remain unattacked. [See W. W. Rouse Ball, Mathematical Recreations and Problems , third edition (London: Macmillan, 1896), 109–110.]

Every subset S of the vertices of a graph has three boundary sets defined thus:

∂ S \= the set of all edges with exactly one endpoint ∈ S ; ∂ o u t   S \= the set of all vertices ∉ S ⁢   with at least one neighbor ∈ S ; ∂ i n   S \= the set of all vertices ∈ S ⁢ with at least one neighbour ∉ S .

Find the minimum and maximum sizes of ∂S , ∂ out S , and ∂ in S , over all 8-element sets S in the queen graph Q 8 (exercise 7.1.4–241). Which set answers Turton’s question?

▸ 488. [ 24 ] ( Peaceable armies of queens .) Prove that armies of nine white queens and nine black queens can coexist on a chessboard without attacking each other, but armies of size 10 cannot, by devising appropriate sets of clauses and applying Algorithm C . Also examine the effects of symmetry breaking. (This problem has sixteen symmetries, because we can swap colors and/or rotate and/or reflect the board.) How large can coexisting armies of queens be on n × n boards, for n ≤ 11?

489. [ M21 ] Find a recurrence for T n , the number of signed involutions on n elements.

▸ 490. [ 15 ] Does Theorem E hold also when p 1 p 2 ... p n is any signed permutation?

▸ 491. [ 22 ] The unsatisfiable clauses R in ( 6 ) have the signed permutation 234 1 ¯ as an automorphism. How can this fact help us to verify their unsatisfiability?

492. [ M20 ] Let τ be a signed mapping of the variables { x 1 , ..., x n }; for example, the signed mapping ′ 4 ¯ 13 3 ¯ ′ stands for the operation ( x 1 , x 2 , x 3 , x 4 ) ↦ ( x 4 ¯ , x 1 , x 3 , x 3 ¯ ) \= ( x ¯ 4 , x 1 , x 3 , x ¯ 3 ) . When a signed mapping is applied to a clause, some of the resulting literals might coincide; or two literals might become complementary, making a tautology. When τ \= 4 ¯ 13 3 ¯ , for instance, we have ( 123 ) τ \= 4 ¯ 13 , ( 13 4 ¯ ) τ \= 4 ¯ 3 , ( 13 4 ¯ ) τ \= ℘ .

A family F of clauses is said to be “closed” under a signed mapping τ if Cτ is subsumed by some clause of F whenever C ∊ F . Prove that τ is an endomorphism of F in such a case.

493. [ 20 ] The problem waerden (3, 3; 9) has four symmetries, because we can reflect and/or complement all the variables. How can we speed up the proof of unsatisfiability by adding clauses to break those symmetries?

494. [ 21 ] Show that if ( u v w ) ( u ¯ v ¯ w ¯ ) is a symmetry of some clauses F , we’re allowed to break symmetries as if ( u v ) ( u ¯ v ¯ ) , ( u w ) ( u ¯ w ¯ ) , and ( v w ) ( v ¯ w ¯ ) were also symmetries. For example, if i < j < k and if ( i j k ) ( i ¯ j ¯ k ¯ ) is a symmetry, we can assert ( x ¯ i ∨ x j ) ∧ ( x ¯ j ∨ x k ) with respect to the global ordering p 1 ... p n = 1 ... n . What are the corresponding binary clauses when the symmetry is (i) ( i j k ¯ ) ( i ¯ j ¯ k ) ? (ii) ( i j ¯ k ) ( i ¯ j k ¯ ) ? (iii) ( i j ¯ k ¯ ) ( i ¯ j ¯ k ) ?

495. [ M22 ] Spell out the details of how we can justify appending clauses to assert ( 185 ) and ( 186 ), using Corollary E, whenever we have an m × n problem whose variables x ij possess both row and column symmetry. (In other words we assume that x ij ↦ x ( iπ )( jρ ) is an automorphism for all permutations π of {1, ..., m } and ρ of {1, ..., n }.)

▸ 496. [ M20 ] B. C. Dull reasoned as follows: “The pigeonhole clauses have row and column symmetry. Therefore we can assume that the rows are lexicographically increasing from top to bottom, and the columns are lexicographically increasing from right to left. Consequently the problem is easily seen to be unsatisfiable.” Was he correct?

497. [ 22 ] Use BDD methods to determine the number of 8 × 8 binary matrices that have both rows and columns in nondecreasing lexicographic order. How many of them have exactly r 1s, for r = 24, r = 25, r = 64 − 25 = 39, and r = 64 − 24 = 40?

498. [ 22 ] Justify adding the symmetry-breakers ( 183 ) to the pigeonhole clauses.

499. [ 21 ] In the pigeonhole problem, is it legitimate to include the clauses ( 183 ) together with clauses that enforce lexicographic row and column order?

500. [ 16 ] The precocious student J. H. Quick decided to extend the monkey wrench principle, arguing that if F 0 ∪ S ⊢ l then the original clauses F can be replaced by F | l . But he soon realized his mistake. What was it?

501. [ 22 ] Martin Gardner introduced an interesting queen placement problem in Scientific American 235 , 4 (October 1976), 134–137: “Place r queens on an m × n chess-board so that (i) no three are in the same row, column, or diagonal; (ii) no empty square can be occupied without breaking rule (i); and (iii) r is as small as possible.” Construct clauses that are satisfiable if and only if there’s a solution to conditions (i) and (ii) with at most r queens. (A similar problem was considered in exercise 7.1.4–242.)

502. [ 16 ] ( Closest strings .) Given binary strings s 1 , ... , s m of length n , and threshold parameters r 1 , ... , r m , construct clauses that are satisfiable by x = x 1 ... x n if and only if x differs from s j in at most r j positions, for 1 ≤ j ≤ m .

503. [ M20 ] ( Covering strings .) Given s j and r j as in exercise 502 , show that every string of length n is within r j bits of some s j if and only if the closest string problem has no solution with parameters r j ′ \= n − 1 − r j .

▸ 504. [ M21 ] The problem in exercise 502 can be proved NP-complete as follows:

  1. Let w j be the string of length 2 n that is entirely 0 except for 1s in positions 2 j − 1 and 2 j , and let w n + j \= w ¯ j , for 1 ≤ j ≤ n . Describe all binary strings of length 2 n that differ from each of w 1 , ... , w 2 n in at most n bit positions.

  2. Given a clause ( l 1 ∨ l 2 ∨ l 3 ) with strictly distinct literals l 1 , l 2 , l 3 ∈ { x 1 , ... , x n ,   x ¯ 1 , ... , x ¯ n } , let y be the string of length 2 n that is entirely zero except that it has 1 in position 2 k − 1 when some l i is x ¯ k , and 1 in position 2 k when some l i is x k . In how many bit positions does a string that satisfies (a) differ from y ?

  3. Given a 3SAT problem F with m clauses and n variables, use (a) and (b) to construct strings s 1 , ... , s m+2n of length 2 n such that F is satisfiable if and only if the closest string problem is satisfiable with r j = n +[ j> 2 n ].

  4. Illustrate your construction in (c) by exhibiting the closest string problems that correspond to the simple 3SAT problems R and R ′ in ( 6 ) and ( 7 ).

505. [ 21 ] Experiment with making Algorithm L nondeterministic, by randomizing the initial order of VAR in step L1 just as HEAP is initialized randomly in step C1. How does the modified algorithm perform on, say, problems D3, K0, and W2 of Table 6 ?

506. [ 22 ] The weighted variable interaction graph of a family of clauses has one vertex for each variable and the weight ∑2/(| c |(| c |− 1)) between vertices u and v , where the sum is over all clauses c that contain both ± u and ± v . Figure 95 indicates these weights indirectly, by making the heavier edges darker.

  1. True or false: The sum of all edge weights is the total number of clauses.

  2. Explain why the graph for test case B2 has exactly 6 edges of weight 2. What are the weights of the other edges in that graph?

▸ 507. [ 21 ] (Marijn Heule.) Explain why “ windfalls” (see ( 72 )) help Algorithm L to deal with miter problems such as D5.

508. [ M20 ] According to Table 7 , Algorithm C proved problem T3 to be unsatisfiable after learning about 323 thousand clauses. About how many times did it enter a purging phase in step C7?

509. [ 20 ] Several of the “ training set” tasks used when tuning Algorithm C ’s parameters were taken from the 100 test cases of Table 6 . Why didn’t this lead to a problem of “ overfitting” (namely, of choosing parameters that are too closely associated with the trainees)?

510. [ 18 ] When the data points A1, A2, ... , X8 were plotted in Fig. 98 , one by one, they sometimes covered parts of previously plotted points, because of overlaps. What test cases are partially hidden by (a) T2? (b) X6? (c) X7?

511. [ 22 ] Problem P4 in Table 6 is a strange set of clauses that lead to extreme behavior of Algorithm C in Figs. 97 and 98 ; and it causes Algorithm L to “time out” in Fig. 96 .

  1. The preprocessing algorithm of the text needs about 1.5 megamems to convert those 2509 clauses in 400 variables into just 2414 clauses in 339 variables. Show empirically that Algorithm L makes short work of the resulting 2414 clauses.

  2. How efficient is Algorithm C on those preprocessed clauses?

  3. What is the behavior of WalkSAT on P4, with and without preprocessing?

512. [ 29 ] Find parameters for Algorithm C that will find an Erd˝os discrepancy pattern x 1 x 2 ... x n rapidly when n = 500. (This is problem E0 in Table 6 .) Then compare the running times of nine random runs with your parameters versus nine random runs with ( 194 ), when n = 400, 500, 600, ... , 1100, 1160, and 1161.

513. [ 24 ] Find parameters for Algorithm L that tune it for rand (3, m,n , seed ).

514. [ 24 ] The timings quoted in the text for Algorithm W , for problems in Table 6 , are based on the median of nine runs using the parameters p = . 4 and N = 50 n , restarting from scratch if necessary until a solution is found. Those parameters worked fine in most cases, unless Algorithm W was unsuited to the task. But problem C9 was solved more quickly with p = . 6 and N = 2500 n (943 M μ versus 9 . 1G μ ).

Find values of p and N/n that give near-optimum performance for problem C9.

▸ 515. [ 23 ] ( Hard sudoku .) Specify SAT clauses with which a designer of sudoku puzzles can meet the following specifications: (i) If cell ( i, j ) of the puzzle is blank, so is cell (10− i , 10− j ), for 1 ≤ i, j ≤ 9. (ii) Every row, every column, and every box contains at least one blank. (Here “box” means one of sudoku’s nine special 3 × 3 subarrays.) (iii) No box contains an all-blank row or an all-blank column. (iv) There are at least two ways to fill every blank cell, without conflicting with nonblank entries in the same row, column, or box. (v) If a row, column, or box doesn’t already contain k , there are at least two places to put k into that row, column, or box, without conflict. (vi) If the solution has a 2 × 2 subarray of the form k l l k , those four cells must not all be blank.

(Condition (i) is a feature of “classic” sudoku puzzles. Conditions (iv) and (v) ensure that the corresponding exact cover problem has no forced moves; see Section 7.2.2.1 . Condition (vi) rules out common cases with non-unique solutions.)

516. [ M49 ] Prove or disprove the strong exponential time hypothesis : “If τ < 2, there is an integer k such that no randomized algorithm can solve every k SAT problem in fewer than τ n steps, where n is the number of variables.”

517. [ 25 ] Given clauses C 1 , ... , C m , the one-per-clause satisfiability problem asks if there is a Boolean assignment x 1 ... x n such that every clause is satisfied by a unique literal. In other words, we want to solve the simultaneous equations ∑ C j = 1 for 1 ≤ j ≤ m , where ∑ C is the sum of the literals of clause C .

  1. Prove that this problem is NP-complete, by reducing 3SAT to it.

  2. Prove that this problem, in turn, can be reduced to its special case “ one-in-three satisfiability,” where every given clause is required to be ternary.

518. [ M32 ] Given a 3SAT problem with m clauses and n variables, we shall construct a (6 m + n ) × (6 m + n ) matrix M of integers such that the permanent , per M , is zero if and only if the clauses are unsatisfiable. For example, the solvable problem ( 7 ) corresponds to the 46 × 46 matrix indicated here; each shaded box stands for a fixed 6 × 6 matrix A that corresponds to a clause.

images

Each A has three “inputs” in columns 1, 3, 5 and three “outputs” in rows 2, 4, 6. The first n rows and the last n columns correspond to variables. Outside of the A s, all entries are either 0 or 2; and the 2s link variables to clauses, according to a scheme much like the data structures in several of the algorithms in this section: Let I ij and O ij denote the j th input and output of clause i , for 1 ≤ i ≤ m and 1 ≤ j ≤ 3. Then, if literal l appears in t ≥ 0 clauses i 1 < ··· < i t , as element j 1 , ... , j t , we put ‘2’ in column I i k +1 j k +1 of row O i k j k for 0 ≤ k ≤ t ( O i 0 j is row | l |, I i t +1 j is column 6 m +| l |).

  1. Find a 6 × 6 matrix A = ( a ij ), whose elements are either 0, 1, or −1, such that

    per ( a 11 a 12 a 13 a 14 a 15 a 16 a 21 + 2 p a 22 a 23 + 2 q a 24 a 25 + 2 r a 26 a 31 a 32 a 33 a 34 a 35 a 36 a 41 + 2 u a 42 a 43 + 2 v a 44 a 45 + 2 w a 46 a 51 a 52 a 53 a 54 a 55 a 56 a 61 + 2 x a 62 a 63 + 2 y a 64 a 65 + 2 z a 66 ) \= 16 ( p e r ( p + 1 q r u v + 1 w x y z + 1 ) − 1 ) .

    Hint: There’s a solution with lots of symmetry.

  2. In which of the rows and columns of M does ‘2’ occur twice? once? not at all?

  3. Conclude that per M = 2 4 m + n s , when the 3SAT problem has exactly s solutions.

519. [ 20 ] Table 7 shows inconclusive results in a race for factoring between factor fifo and factor lifo . What is the comparable performance of factor_rand ( m, n, z , 314159)?

▸ 520. [ 24 ] Every instance of SAT corresponds in a natural way to an integer programming feasibility problem: To find, if possible, integers x 1 , ... , x n that satisfy the linear inequalities 0 ≤ x j ≤ 1 for 1 ≤ j ≤ n and

l 1 + l 2 + ⋅ ⋅ ⋅ + l k ≥ 1 for each clause           C \= ( l 1 ∨ l 2 ∨ ⋅ ⋅ ⋅ ∨ l k ) .

For example, the inequality that corresponds to the clause ( x 1 ∨ x ¯ 3 ∨ x ¯ 4 ∨ x 7 ) is x 1 +(1− x 3 )+(1− x 4 )+ x 7 ≥ 1; i.e., x 1 − x 3 − x 4 + x 7 ≥ −1.

Sophisticated “ IP solvers” have been developed by numerous researchers for solving general systems of integer linear inequalities, based on techniques of “ cutting planes” in high-dimensional geometry. Thus we can solve any satisfiability problem by using such general-purpose software, as an alternative to trying a SAT solver.

Study the performance of the best available IP solvers, with respect to the 100 sets of clauses in Table 6 , and compare it to the performance of Algorithm C in Table 7 .

521. [ 30 ] Experiment with the following idea, which is much simpler than the clause- purging method described in the text: “Forget a learned clause of length k with probability p k ,” where p 1 ≥ p 2 ≥ p 3 ≥ ··· is a tunable sequence of probabilities.

▸ 522. [ 26 ] ( Loopless shadows .) A cyclic path within the cube P 3 □ P 3 □ P 3 is shown here, together with the three “shadows” that appear when it is projected onto each coordinate plane. Notice that the shadow at the bottom contains a loop, but the other two shadows do not. Does this cube contain a cycle whose three shadows are entirely without loops? Use SAT technology to find out.

523. [ 30 ] Prove that, for any m or n , no cycle of the graph P m □ P n □ P 2 has loopless shadows.

▸ 524. [ 22 ] Find all Hamiltonian paths of the cube P 3 □ P 3 □ P 3 that have loopless shadows.

images

▸ 525. [ 40 ] Find the most difficult 3SAT problem you can that has at most 100 variables.

526. [ M25 ] (David S. Johnson, 1974.) If F has m clauses, all of size ≥ k , prove that some assignment leaves at most m /2 k clauses unsatisfied.

Behold once more with serious labor here
Haue I refurnisht out this little frame,
Repaird some parts, defectiue here and there,
And passages new added to the same.

— SAMUEL DANIEL, Certaine small Workes Heretofore Divulged (1607)