Note to readers: Please ignore these sidenotes; they're just hints to myself for preparing the index, and they're often aky! KNUTH THE ART OF COMPUTER PROGRAMMING VOLUME 4 PRE-FASCICLE 5B INTRODUCTION TO BACKTRACKING DONALD E. KNUTH ADDISON{WESLEY November 12, 2016 Stanford University 6 77 Internet Stanford GraphBase MMIX Internet page http://www- s-fa ulty.stanford.edu/~knuth/tao p.html ontains urrent information about this book and related books. See also http://www- s-fa ulty.stanford.edu/~knuth/sgb.html for information about The Stanford GraphBase, in luding downloadable software for dealing with the graphs used in many of the examples in Chapter 7. See also http://www- s-fa ulty.stanford.edu/~knuth/mmixware.html for downloadable software to simulate the MMIX omputer. Copyright 2016 by Addison{Wesley All rights reserved. No part of this publi ation may be reprodu ed, stored in a retrieval system, or transmitted, in any form, or by any means, ele troni , me hani al, photoopying, re ording, or otherwise, without the prior onsent of the publisher, ex ept that the oÆ ial ele troni le may be used to print single opies for personal (not ommer ial) use. Zeroth printing (revision -79), 12 November 2016 November 12, 2016 CARROLL Internet PREFACE Begin at the beginning, and do not allow yourself to gratify a mere idle uriosity by dipping into the book, here and there. This would very likely lead to your throwing it aside, with the remark \This is mu h too hard for me!," and thus losing the han e of adding a very large item to your sto k of mental delights. | LEWIS CARROLL, in Symboli Logi (1896) This booklet ontains draft material that I'm ir ulating to experts in the eld, in hopes that they an help remove its most egregious errors before too many other people see it. I am also, however, posting it on the Internet for ourageous and/or random readers who don't mind the risk of reading a few pages that have not yet rea hed a very mature state. Beware: This material has not yet been proofread as thoroughly as the manus ripts of Volumes 1, 2, 3, and 4A were at the time of their rst printings. And alas, those arefully- he ked volumes were subsequently found to ontain thousands of mistakes. Given this aveat, I hope that my errors this time will not be so numerous and/or obtrusive that you will be dis ouraged from reading the material arefully. I did try to make the text both interesting and authoritative, as far as it goes. But the eld is vast; I annot hope to have surrounded it enough to orral it ompletely. So I beg you to let me know about any de ien ies that you dis over. To put the material in ontext, this portion of fas i le 5 previews the opening pages of Se tion 7.2.2 of The Art of Computer Programming, entitled \Ba ktra k programming." The pre eding se tion, 7.2.1, was about \Generating basi ombinatorial patterns" | namely tuples, permutations, ombinations, partitions, and trees. Now it's time to onsider the non-basi patterns, the ones that have a mu h less uniform stru ture. For these we generally need to make tentative hoi es and then we need to ba k up when those hoi es need revision. Several subse tions (7.2.2.1, 7.2.2.2, et .) will follow this introdu tory material. The explosion of resear h in ombinatorial algorithms sin e the 1970s has meant that I annot hope to be aware of all the important ideas in this eld. I've tried my best to get the story right, yet I fear that in many respe ts I'm woefully ignorant. So I beg expert readers to steer me in appropriate dire tions. November 12, 2016 iii iv PREFACE Please look, for example, at the exer ises that I've lassed as resear h problems (rated with diÆ ulty level 46 or higher), namely exer ises 14, : : : ; I've also impli itly mentioned or posed additional unsolved questions in the answers to exer ises 6, 8, 42, 45, : : : . Are those problems still open? Please inform me if you know of a solution to any of these intriguing questions. And of ourse if no solution is known today but you do make progress on any of them in the future, I hope you'll let me know. I urgently need your help also with respe t to some exer ises that I made up as I was preparing this material. I ertainly don't like to re eive redit for things that have already been published by others, and most of these results are quite natural \fruits" that were just waiting to be \plu ked." Therefore please tell me if you know who deserves to be redited, with respe t to the ideas found in exer ises 31(b), 33, 44, 50, 51, 62, 66, 67, 75, 100, : : : . Furthermore I've redited exer ises : : : to unpublished work of : : : . Have any of those results ever appeared in print, to your knowledge? I've got a histori al question too: Have you any idea who originated the idea of \stamping" in data stru tures? (See 7.2.2{(26). This on ept is quite di erent from the so- alled time stamps in persistent data stru tures, and quite di erent from the so- alled time stamps in depth- rst sear h algorithms, and quite di erent from the so- alled time stamps in ryptology, although many programmers do use the name \time stamp" for those kinds of stamp.) It's a te hnique that I've seen often, in programs that have ome to my attention during re ent de ades, but I wonder if it ever appeared in a book or paper that was published before, say, 1980. Spe ial thanks are due to : : : for their detailed omments on my early attempts at exposition, as well as to numerous other orrespondents who have ontributed ru ial orre tions. I happily o er a \ nder's fee" of $2.56 for ea h error in this draft when it is rst reported to me, whether that error be typographi al, te hni al, or histori al. The same reward holds for items that I forgot to put in the index. And valuable suggestions for improvements to the text are worth 32/ ea h. (Furthermore, if you nd a better solution to an exer ise, I'll a tually do my best to give you immortal glory, by publishing your name in the eventual book: ) Cross referen es to yet-unwritten material sometimes appear as `00'; this impossible value is a pla eholder for the a tual numbers to be supplied later. Happy reading! Stanford, California D. E. K. 99 Umbruary 2016 November 12, 2016 stamping Knuth MPR Part of the Prefa e to Volume 4B During the years that I've been preparing Volume 4, I've often run a ross basi te hniques of probability theory that I would have put into Se tion 1.2 of Volume 1 if I'd been lairvoyant enough to anti ipate them in the 1960s. Finally I realized that I ought to olle t most of them together in one pla e, near the beginning of Volume 4B, be ause the story of these developments is too interesting to be broken up into little pie es s attered here and there. Therefore this volume begins with a spe ial se tion entitled \Mathemati al Preliminaries Redux," and future se tions use the abbreviation `MPR' to refer to its equations and its exer ises. November 12, 2016 v MATHEMATICAL PRELIMINARIES REDUX Many parts of this book deal with dis rete probabilities, namely with a nite or ountably in nite set of atomi events !, ea h of whi h has a given probability Pr(!), where X 0 Pr(!) 1 and Pr(!) = 1: (1) !2 ::: For the omplete text of the spe ial MPR se tion, please see Pre-Fas i le 5a. In identally, Se tion 7.2.2 intentionally begins on a left-hand page, and its illustrations are numbered beginning with Fig. 68, be ause Se tion 7.2.1 ended on a right-hand page and its nal illustration was Fig. 67. The editor has de ided to treat Chapter 7 as a single unit, even though it will be split a ross several physi al volumes. November 12, 2016 1 2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) Nowhere to go but out, Nowhere to ome but ba k. | BEN KING, in The Sum of Life ( . 1893) When you ome to one legal road that's blo ked, you ba k up and try another. | PERRY MASON, in The Case of the Bla k-Eyed Blonde (1944) No one I think is in my tree. | JOHN LENNON, in Strawberry Fields Forever (1967) 7.2.2. Ba ktra k Programming Now that we know how to generate simple ombinatorial patterns su h as tuples, permutations, ombinations, partitions, and trees, we're ready to ta kle more exoti patterns that have subtler and less uniform stru ture. Instan es of almost any desired pattern an be generated systemati ally, at least in prin iple, if we organize the sear h arefully. Su h a method was hristened \ba ktra k" by R. J. Walker in the 1950s, be ause it is basi ally a way to examine all fruitful possibilities while exiting gra efully from situations that have been fully explored. Most of the patterns we shall deal with an be ast in a simple, general framework: We seek all sequen es x1 x2 : : : xn for whi h some property Pn (x1 ; x2 ; : : : ; xn ) holds, where ea h item xk belongs to some given domain Dk of integers. The ba ktra k method, in its most elementary form, onsists of inventing intermediate \ uto " properties P l (x1 ; : : : ; xl ) for 1 l < n, su h that P l (x1 ; : : : ; xl ) is true whenever P l+1 (x1 ; : : : ; xl+1 ) is true; (1) P l (x1 ; : : : ; xl ) is fairly easy to test, if P l 1 (x1 ; : : : ; xl 1 ) holds. (2) (We assume that P0 () is always true. Exer ise 1 shows that all of the basi patterns studied in Se tion 7.2.1 an easily be formulated in terms of domains Dk and uto properties P l .) Then we an pro eed lexi ographi ally as follows: Algorithm B (Basi ba ktra k ). Given domains Dk and properties P l as above, this algorithm visits all sequen es x1 x2 : : : xn that satisfy Pn (x1 ; x2 ; : : : ; xn ). B1. [Initialize.℄ Set l 1, and initialize the data stru tures needed later. B2. [Enter level l .℄ (Now P l 1 (x1 ; : : : ; xl 1 ) holds.) If l > n, visit x1 x2 : : : xn and go to B5. Otherwise set xl min D l , the smallest element of D l . B3. [Try xl .℄ If P l (x1 ; : : : ; xl ) holds, update the data stru tures to fa ilitate testing P l+1 , set l l + 1, and go to B2. B4. [Try again.℄ If xl 6= max D l , set xl to the next larger element of D l and return to B3. B5. [Ba ktra k.℄ Set l l 1. If l > 0, downdate the data stru tures by undoing the hanges re ently made in step B3, and return to B4. (Otherwise stop.) The main point is that if P l (x1 ; : : : ; xl ) is false in step B3, we needn't waste time trying to append any further values xl+1 : : : xn . Thus we an often rule out huge regions of the spa e of all potential solutions. A se ond important point is that very little memory is needed, although there may be many, many solutions. November 12, 2016 KING MASON Gardner ES LENNON ba ktra k Walker domain uto properties: logi al propositions (relations) P0 () lexi ographi ally 7.2.2 BACKTRACK PROGRAMMING 3 For example, let's onsider the lassi problem of n queens : In how many ways an n queens be pla ed on an n n board so that no two are in the same row, olumn, or diagonal? We an suppose that one queen is in ea h row, and that the queen in row k is in olumn xk , for 1 k n. Then ea h domain Dk is f1; 2; : : : ; ng; and Pn (x1 ; : : : ; xn ) is the ondition that xj 6= xk and jxk xj j 6= k j; for 1 j < k n. (3) (If xj = xk and j < k, two queens are in the same olumn; if jxk xj j = k j , they're in the same diagonal.) This problem is easy to set up for Algorithm B, be ause we an let property P l (x1 ; : : : ; xl ) be the same as (3) but restri ted to 1 j < k l. Condition (1) is lear; and so is ondition (2), be ause P l requires testing (3) only for k = l when P l 1 is known. Noti e that P1 (x1 ) is always true in this example. One of the best ways to learn about ba ktra king is to exe ute Algorithm B by hand in the spe ial ase n = 4 of the n queens problem: First we set x1 1. Then when l = 2 we nd P2 (1; 1) and P2 (1; 2) false; hen e we don't get to l = 3 until trying x2 3. Then, however, we're stu k, be ause P3 (1; 3; x) is false for 1 x 4. Ba ktra king to level 2, we now try x2 4; and this allows us to set x3 2. However, we're stu k again, at level 4; and this time we must ba k up all the way to level 1, be ause there are no further valid hoi es at levels 3 and 2. The next hoi e x1 2 does, happily, lead to a solution without mu h further ado, namely x1 x2 x3 x4 = 2413. And one more solution (3142) turns up before the algorithm terminates. The behavior of Algorithm B is ni ely visualized as a tree stru ture, alled a sear h tree or ba ktra k tree. For example, the ba ktra k tree for the four queens problem has just 17 nodes, 1 3 4 2 2 3 4 1 1 4 3 2 4 1 3 2 ; (4) orresponding to the 17 times step B2 is performed. Here xl is shown as the label of an edge from level l 1 to level l of the tree. (Level l of the algorithm a tually orresponds to the tree's level l 1, be ause we've hosen to represent patterns using subs ripts from 1 to n instead of from 0 to n 1 in this dis ussion.) The pro le (p0 ; p1 ; : : : ; pn ) of this parti ular tree | the number of nodes at ea h level | is (1; 4; 6; 4; 2); and we see that the number of solutions, pn = p4 , is 2. Figure 68 shows the orresponding tree when n = 8. This tree has 2057 nodes, distributed a ording to the pro le (1; 8; 42; 140; 344; 568; 550; 312; 92). Thus the early uto s fa ilitated by ba ktra king have allowed us to nd all 92 solutions by examining only 0.01% of the 88 = 16;777;216 possible sequen es x1 : : : x8 . (And 88 is only 0.38% of the 648 = 4;426;165;368 ways to put eight queens on the board.) November 12, 2016 n queens{ diagonal ba ktra k tree pro le 4 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 data stru tures{ mems downdating vs updating+ undoes Fig. 68. The problem of pla ing eight nonatta king queens has this ba ktra k tree. Noti e that, in this ase, Algorithm B spends most of its time in the vi inity of level 5. Su h behavior is typi al: The ba ktra k tree for n = 16 queens has 1,141,190,303 nodes, and its pro le is (1, 16, 210, 2236, 19688, 141812, 838816, 3998456, 15324708, 46358876, 108478966, 193892860, 260303408, 253897632, 171158018, 72002088, 14772512), on entrated near level 12. Data stru tures. Ba ktra k 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 qui kly as possible in step B3. One way to implement Algorithm B for the n queens problem is to avoid auxiliary data stru tures and simply to make a bun h of sequential omparisons in that step: \Is xl xj 2 fj l; 0; l j g for some j < l?" Assuming that we a ess memory whenever referring to xj , given a trial value xl in a register, su h an implementation performs approximately 112 billion memory a esses when n = 16; that's about 98 mems per node. We an do better by introdu ing three simple arrays. Property P l in (3) says essentially that the numbers xk are distin t, and so are the numbers xk + k, and so are the numbers xk k. Therefore we an use auxiliary Boolean arrays a1 : : : an , b1 : : : b2n 1, and 1 : : : 2n 1 , where aj means `some xk = j ', bj means `some xk + k 1 = j ', and j means `some xk k + n = j '. Those arrays are readily updated and downdated if we ustomize Algorithm B as follows: B1*. [Initialize.℄ Set a1 : : : an 0 : : : 0, b1 : : : b2n 1 0 : : : 0, 1 : : : 2n 1 0 : : : 0, and l 1. B2*. [Enter level l .℄ (Now P l 1 (x1 ; : : : ; xl 1 ) holds.) If l > n, visit x1 x2 : : : xn and go to B5*. Otherwise set t 1. B3*. [Try t.℄ If at = 1 or bt+l 1 = 1 or t l+n = 1, go to B4*. Otherwise set at 1, bt+l 1 1, t l+n 1, xl t, l l + 1, and go to B2*. B4*. [Try again.℄ If t < n, set t t + 1 and return to B3*. B5*. [Ba ktra k.℄ Set l l 1. If l > 0, set t xl , t l+n 0, bt+l 1 0, at 0, and return to B4*. (Otherwise stop.) Noti e how step B5* neatly undoes the updates that step B3* had made, in the reverse order. Reverse order for downdating is typi al of ba ktra k algorithms, November 12, 2016 7.2.2 BACKTRACK PROGRAMMING 5 although there is some exibility; we ould, for example, have restored at before bt+l 1 and t l+n , be ause those arrays are independent. The auxiliary arrays a, b, make it easy to test property P l at the beginning of step B3*, but we must also a ess memory when we update them and downdate them. Does that ost 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 ould keep the bit ve tors a, b, entirely in registers, on a ma hine with 64-bit registers, assuming that n 32. Then there would be just two memory a esses per node, namely to store xl t and later to fet h t xl ; however, quite a lot of in-register omputation would be ome ne essary. Walker's method. The 1950s-era programs of R. J. Walker organized ba ktra king in a somewhat di erent way. Instead of letting xl run through all elements of D l , he al ulated and stored the set (5) Sl x 2 D l P l (x1 ; : : : ; xl 1 ; x) holdsg upon entry to ea h node at level l. This omputation an often be done eÆ iently all at on e, instead of pie emeal, be ause some uto properties make it possible to ombine steps that would otherwise have to be repeated for ea h x 2 D l . In essen e, he used the following variant of Algorithm B: Algorithm W (Walker's ba ktra k ). Given domains Dk and uto s P l as above, this algorithm visits all sequen es x1 x2 : : : xn that satisfy Pn (x1 ; x2 ; : : : ; xn ). W1. [Initialize.℄ Set l 1, and initialize the data stru tures needed later. W2. [Enter level l .℄ (Now P l 1 (x1 ; : : : ; xl 1 ) holds.) If l > n, visit x1 x2 : : : xn and go to W4. Otherwise determine the set Sl as in (5). W3. [Try to advan e.℄ If Sl is nonempty, set xl min Sl , update the data stru tures to fa ilitate omputing Sl+1 , set l l + 1, and go to W2. W4. [Ba ktra k.℄ Set l l 1. If l > 0, downdate the data stru tures by undoing hanges made in step W3, set Sl Sl n xl , and retreat to W3. Walker applied this method to the n queens problem by omputing Sl = U n Al n Bl n Cl , where U = Dl = f1; : : : ; ng and Al = fxj j 1 j < lg; Bl = fxj + j l j 1 j < lg; Cl = fxj j + l j 1 j < lg: (6) He represented these auxiliary sets by bit ve tors a, b, , analogous to (but di erent from) the bit ve tors of Algorithm B* above. Exer ise 9 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 orresponding 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 omputed independently by several people during the nineteenth entury. Small ases were relatively easy; but when T. B. Sprague November 12, 2016 registers Walker uto properties visits bitwise operations histori al notes+ Sprague 6 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 had nished omputing Q(11) he remarked that \This was a very heavy pie e of work, and o upied most of my leisure time for several months. : : : It will, I imagine, be s ar ely possible to obtain results for larger boards, unless a number of persons o-operate in the work." [See Pro . Edinburgh Math. So . 17 (1899), 43{ 68; Sprague was the leading a tuary of his day.℄ Nevertheless, H. Onnen went on to evaluate Q(12) = 14;200 | an astonishing feat of hand al ulation | in 1910. [See W. Ahrens, Math. Unterhaltungen und Spiele 2, se ond edition (1918), 344.℄ All of these hard-won results were on rmed in 1960 by R. J. Walker, using the SWAC omputer at UCLA and the method of exer ise 9. Walker also omputed Q(13); but he ouldn't go any further with the ma hine available to him at the time. The next step, Q(14), was omputed by Mi hael D. Kennedy at the University of Tennessee in 1963, ommandeering an IBM 1620 for 120 hours. S. R. Bun h 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 omputer, but with an improved method. Computers and algorithms have ontinued to get better, of ourse, and su h results are now obtained almost instantly. Hen e larger and larger values of n lie at the frontier. The whopping value Q(27) = 234,907,967,154,122,528, found in 2016 by Thomas B. Preuer and Matthias R. Engelhardt, probably won't be exeeded for awhile! [See J. Signal Pro essing Systems 89 (2017), to appear. This distributed omputation o upied a dynami luster of diverse FPGA devi es for 383 days; those devi es provided a total peak of more than 7000 ustom-designed hardware solvers to handle 2,024,110,796 independent subproblems.℄ Permutations and Langford pairs. Every solution x1 : : : xn to the n queens problem is a permutation of f1; : : : ; ng, and many other problems are permutation-based. Indeed, we've already seen Algorithm 7.2.1.2X, whi h is an elegant ba ktra k pro edure spe i ally designed for spe ial kinds of permutations. When that algorithm begins to hoose the value of xl , it makes all of the appropriate elements f1; 2; : : : ; ngnfx1; : : : ; xl 1 g onveniently a essible in a linked list. We an get further insight into su h data stru tures by returning to the problem of Langford pairs, whi h was dis ussed at the very beginning of Chapter 7. That problem an be reformulated as the task of nding all permutations of f1; 2; : : : ; ng [ f 1; 2; : : : ; ng with the property that xj = k implies xj+k+1 = k; for 1 j 2n and 1 k n. (7) For example, when n = 4 there are two solutions, namely 23421314 and 41312432. (As usual we nd it onvenient to write 1 for 1, 2 for 2, et .) Noti e that if x = x1 x2 : : : x2n is a solution, so is its \dual" xR = ( x2n ) : : : ( x2 )( x1 ). Here's a Langford-inspired adaptation of Algorithm 7.2.1.2X, with the former notation modi ed slightly to mat h Algorithms B and W: We want to maintain pointers p0 p1 : : : pn su h that, if the positive integers not already present in x1 : : : xl 1 are k1 < k2 < < kt when we're hoosing xl , we have the linked list (8) p0 = k1 ; pk1 = k2 ; : : : ; pkt 1 = kt ; pkt = 0: Su h a ondition turns out to be easy to maintain. November 12, 2016 Sprague Onnen Ahrens Walker SWAC omputer UCLA Kennedy University of Tennessee IBM 1620 Bun h University of Illinois IBM System 360-75 Bitner Preuer Engelhardt distributed omputation FPGA permutation data stru tures Langford pairs dual linked list 7.2.2 BACKTRACK PROGRAMMING 7 (Langford pairs ). This algorithm visits all solutions x1 : : : x2n to (7) in lexi ographi order, using pointers p0 p1 : : : pn that satisfy (8), and also using an auxiliary array y1 : : : y2n for ba ktra king. L1. [Initialize.℄ Set x1 : : : x2n 0 : : : 0, pk k +1 for 0 k < n, pn 0, l 1. L2. [Enter level l .℄ Set k p 0. If k = 0, visit x1 x2 : : : x2n and go to L5. Otherwise set j 0, and while xl < 0 set l l + 1. L3. [Try xl = k .℄ (At this point we have k = pj .) If l + k + 1 > 2n, go to L5. Otherwise, if x l+k+1 = 0, set xl k, x l+k+1 k , yl j , pj pk , l l + 1, and return to L2. L4. [Try again.℄ (We've found all solutions that begin with x1 : : : xl 1 k or something smaller.) Set j k and k pj , then go to L3 if k 6= 0. L5. [Ba ktra k.℄ Set l l 1. If l > 0 do the following: While xl < 0, set l l 1. Then set k xl , xl 0, x l+k+1 0, j yl , pj k, and go ba k to L4. Otherwise terminate the algorithm. Careful study of these steps will reveal how everything ts together ni ely. Noti e that, for example, step L3 removes k from the linked list (8) by simply setting pj pk . That step also sets x l+k+1 k, in a ordan e with (7), so that we an skip over position l + k + 1 when we en ounter it later in step L2. The main point of Algorithm L is the somewhat subtle way in whi h step L5 undoes the deletion operation by setting pj k. The pointer pk still retains the appropriate link to the next element in the list, be ause pk has not been hanged by any of the intervening updates. (Think about it.) This is the germ of an idea alled \dan ing links" that we will explore in Se tion 7.2.2.1. To draw the sear h tree orresponding to a run of Algorithm L, we an label the edges with the positive hoi es of xl as we did in (4), while labeling the nodes with any previously set negative values that are passed over in step L2. For instan e the tree for n = 4 is Algorithm L 1 2 1 3 2 3 2 4 3 1 1 2 3 1 4 4 213 1 1 2 4 3 21 2 4 4 132 1 2 13 2 3 3 : (9) 1 1 2 314 432 Solutions appear at depth n in this tree, even though they involve 2n values x1 x2 : : : x2n . Algorithm L sometimes makes false starts and doesn't realize the problem until probing further than ne essary. Noti e that the value xl = k an appear only when l + k + 1 2n; hen e if we haven't seen k by the time l rea hes 2n k 1, we're for ed to hoose xl = k. For example, the bran h 121 in (9) needn't be pursued, be ause 4 must appear in fx1 ; x2 ; x3 g. Exer ise 20 explains how to in orporate this uto prin iple into Algorithm L. When n = 17, it redu es the number of nodes in the sear h tree from 1.29 trillion to 330 billion, November 12, 2016 undoes deletion operation dan ing links sear h tree uto prin iple 8 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 and redu es the running time from 25.0 teramems to 8.1 teramems. (The amount of work has gone from 19.4 mems per node to 24.4 mems per node, be ause of the extra tests for uto s, yet there's a signi ant overall redu tion.) Furthermore, we an \break the symmetry" by ensuring that we don't onsider both a solution and its dual. This idea, exploited in exer ise 21, redu es the sear h tree to just 160 billion nodes and osts just 3.94 teramems | that's 24.6 mems per node. Word re tangles. Let's look next at a problem where the sear h domains Dl are mu h larger. An m n word re tangle is an array of n-letter words* whose olumns are m-letter words. For example, status lowest utopia making sledge (10) is a 5 6 word re tangle whose olumns all belong to WORDS(5757), the olle tion of 5-letter words in the Stanford GraphBase. To nd su h patterns, we an suppose that olumn l ontains the xl th most ommon 5-letter word, where 1 xl 5757 for 1 l 6; hen e there are 5757 6 = 36,406,369,848,837,732,146,649 ways to hoose the olumns. In (10) we have x1 : : : x6 = 1446 185 1021 2537 66 255. Of ourse very few of those hoi es will yield suitable rows; but ba ktra king will hopefully help us to nd all solutions in a reasonable amount of time. We an set this problem up for Algorithm B by storing the n-letter words in a trie (see Se tion 6.3), with one trie node of size 26 for ea h l-letter pre x of a legitimate word, 0 l n. For example, su h a trie for n = 6 represents 15727 words with 23667 nodes. The pre x st orresponds 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, st , : : : , stx, stz. A slightly di erent onvention is used for pre xes of length n 1; for example, the entries for node 580, ` orne', 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 ornea, orned, ornel, orner, and ornet are ranked 3879, 3878, 9602, 171, and 5013 in the list of 6-letter words. * Whenever ve-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 The OÆ ial SCRABBLE R Players Di tionary, fourth edition (Hasbro, 2005), be ause those words have been in orporated into many widely available omputer games. Su h words have been ranked a ording to the British National Corpus of 2007 | where `the' o urs 5,405,633 times and the next-most ommon word, `of', o urs roughly half as often (3,021,525). The OSPD4 list in ludes respe tively (101, 1004, 4002, 8887, 15727, 23958, 29718, 29130, 22314, 16161, 11412) words of lengths (2, 3, : : : , 12), of whi h (97, 771, 2451, 4474, 6910, 8852, 9205, 8225, 6626, 4642, 3061) o ur at least six times in the British National Corpus. November 12, 2016 break the symmetry dual word re tangles{ footnote doesn't show up here n-letter words WORDS 5-letter words Stanford GraphBase trie 7.2.2 BACKTRACK PROGRAMMING 9 Suppose x1 and x2 spe ify the 5-letter olumn-words slums and total as in (10). Then the trie tells us that the next olumn-word x3 must have the form 1 2 3 4 5 where 1 2 fa; e; i; o; r; u; yg, 2 2= fe; h; j; k; y; zg, 3 2 fe; m; o; tg, 4 2= fa; b; og, and 5 2 fa; e; i; o; u; yg. (There are 221 su h words.) Let al1 : : : alm be the trie nodes orresponding to the pre xes of the rst l olumns of a partial solution to the word re tangle problem. This auxiliary array enables Algorithm B to nd all solutions, as explained in exer ise 24. It turns out that there are exa tly 625,415 valid 5 6 word re tangles, a ording to our onventions; and the method of exer ise 24 needs about 19 teramems of omputation to nd them all. In fa t, the pro le of the sear h tree is (1; 5757; 2458830; 360728099; 579940198; 29621728; 625415); (13) indi ating for example that just 360,728,099 of the 5757 3 = 190,804,533,093 hoi es for x1 x2 x3 will lead to valid pre xes of 6-letter words. With are, exer ise 24's running time an be signi antly de reased, on e we realize that every node of the sear h tree for 1 l n requires testing 5757 possibilities for xl in step B3. If we build a more elaborate data stru ture for the 5-letter words, so that it be omes easy to run though all words that have a spe i letter in a spe i position, we an re ne the algorithm so that the average number of possibilities per level that need to be investigated be omes only (5757:0; 1697:9; 844:1; 273:5; 153:5; 100:8); (14) the total running time then drops to 1.15 teramems. Exer ise 25 has the details. And exer ise 28 dis usses a method that's faster yet. Commafree odes. Our next example deals entirely with four -letter words. But it's not obs ene; it's an intriguing question of oding theory. The problem is to nd a set of four-letter words that an be de oded even if we don't put spa es or other delimiters between them. If we take any message that's formed from words of the set by simply on atenating them together, likethis, and if we look at any seven onse utive letters : : : x1 x2 x3 x4 x5 x6 x7 : : : , exa tly one of the four-letter substrings x1 x2 x3 x4 , x2 x3 x4 x5 , x3 x4 x5 x6 , x4 x5 x6 x7 will be a odeword. Equivalently, if x1 x2 x3 x4 and x5 x6 x7 x8 are odewords, then x2 x3 x4 x5 and x3 x4 x5 x6 and x4 x5 x6 x7 aren't. (For example, iket isn't.) Su h a set is alled a \ ommafree ode" or a \self-syn hronizing blo k ode" of length four. Commafree odes were introdu ed by F. H. C. Cri k, J. S. GriÆth, and L. E. Orgel [Pro . National A ad. S i. 43 (1957), 416{421℄, and studied further by S. W. Golomb, B. Gordon, and L. R. Wel h [Canadian Journal of Mathemati s 10 (1958), 202{209℄, who onsidered the general ase of m-letter alphabets and nletter words. They onstru ted optimum ommafree odes for all m when n = 2, 3, 5, 7, 9, 11, 13, and 15; and optimum odes for all m were subsequently found also for n = 17, 19, 21, : : : (see exer ise 32). We will fo us our attention on the four-letter ase here (n = 4), partly be ause that ase is still very far from being resolved, but mostly be ause the task of nding su h odes is espe ially instru tive. Indeed, our dis ussion will lead us naturally to an understanding of several signi ant te hniques that are important for ba ktra k programming in general. November 12, 2016 teramems pro le sear h tree data stru ture ommafree odes{ 4-letter odewords four-letter words oding theory on atenating odewords self-syn hronizing blo k ode blo k ode Cri k GriÆth Orgel Golomb Gordon Wel h 10 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 To begin, we an see immediately that a ommafree odeword annot be \periodi ," like dodo or gaga. Su h a word already appears within two adja ent opies of itself. Thus we're restri ted to aperiodi words like item, of whi h there are m4 m2 . Noti e further that if item has been hosen, we aren't allowed to in lude any of its y li shifts temi, emit, or mite, be ause they all appear within itemitem. Hen e the maximum number of odewords in our ommafree ode annot ex eed (m4 m2 )=4. For example, onsider the binary ase, m = 2, when this maximum is 3. Can we hoose three four-bit \words," one from ea h of the y li lasses [0001℄ = f0001; 0010; 0100; 1000g; [0011℄ = f0011; 0110; 1100; 1001g; (15) [0111℄ = f0111; 1110; 1101; 1011g; so that the resulting ode is ommafree? Yes: One solution in this ase is simply to hoose the smallest word in ea h lass, namely 0001, 0011, and 0111. (Alert readers will re all that we studied the smallest word in the y li lass of any aperiodi string in Se tion 7.2.1.1, where su h words were alled prime strings and where some of the remarkable properties of prime strings were proved.) That tri k doesn't work when m = 3, however, when there are (81 9)=4 = 18 y li lasses. Then we annot in lude 1112 after we've hosen 0001 and 0011. Indeed, a ode that ontains 0001 and 1112 an't ontain either 0011 or 0111. We ould systemati ally ba ktra k through 18 levels, hoosing x1 in [0001℄ and x2 in [0011℄, et ., and reje ting ea h x l as in Algorithm B whenever we dis over that fx1 ; x2 ; : : : ; x l g isn't ommafree. For example, if x1 = 0010 and we try x2 = 1001, this approa h would ba ktra k be ause x1 o urs inside x2 x1 . But a nave strategy of that kind, whi h re ognizes failure only after a bad hoi e has been made, an be vastly improved. If we had been lever enough, we ould have looked a little bit ahead, and never even onsidered the hoi e x2 = 1001 in the rst pla e. Indeed, after hoosing x1 = 0010, we an automati ally ex lude all further words of the form 001, su h as 2001 when m 3 and 3001 when m 4. Even better pruning o urs if, for example, we've hosen x1 = 0001 and x2 = 0011. Then we an immediately rule out all words of the forms 1 or 0, be ause x1 1 in ludes x2 and 0x2 in ludes x1 . Already we ould then dedu e, in the ase m 3, that lasses [0002℄, [0021℄, [0111℄, [0211℄, and [1112℄ must be represented by 0002, 0021, 0111, 0211, and 2111, respe tively; ea h of the other three possibilities in those lasses has been wiped out! Thus we see the desirability of a lookahead me hanism. Dynami ordering of hoi es. Furthermore, we an see from this example that it's not always good to hoose x1 , then x2 , then x3 , and so on when trying to satisfy a general property Pn (x1 ; x2 ; : : : ; xn ) in the setting of Algorithm B. Maybe the sear h tree will be mu h smaller if we rst hoose x5 , say, and then turn next to some other xj , depending on the parti ular value of x5 that was sele ted. Some orderings might have mu h better uto properties than others, and every bran h of the tree is free to hoose its variables in any desired order. November 12, 2016 periodi aperiodi y li shifts prime strings lookahead dynami ordering{ sear h rearrangement, see dynami ordering uto properties 7.2.2 BACKTRACK PROGRAMMING 11 Indeed, our ommafree oding problem for ternary 4-tuples doesn't di tate any parti ular ordering of the 18 lasses that would be likely to keep the sear h tree small. Therefore, instead of alling those hoi es x1 , x2 , : : : , x18 , it's better to identify them by the various lass names, namely x0001 , x0002 , x0011 , x0012 , x0021 , x0022 , x0102 , x0111 , x0112 , x0121 , x0122 , x0211 , x0212 , x0221 , x0222 , x1112 , x1122 , x1222 . (Algorithm 7.2.1.1F is a good way to generate those names.) At every node of the sear h tree we then an hoose a onvenient variable on whi h to bran h, based on previous hoi es. After beginning with x0001 0001 at level 1 we might de ide to try x0011 0011 at level 2; and then, as we've seen, the hoi es x0002 0002, x0021 0021, x0111 0111, x0211 0211, and x1112 2111 are for ed, so we should make them at levels 3 through 7. Furthermore, after those for ed moves are made, it turns out that they don't for e any others. But only two hoi es for x0012 will remain, while x0122 will have three. Therefore it will probably be wiser to bran h on x0012 rather than on x0122 at level 8. (In identally, it also turns out that there is no ommafree ode with x0001 = 0001 and x0011 = 0011, ex ept when m = 2.) It's easy to adapt Algorithms B and W to allow dynami ordering. Every node of the sear h tree an be given a \frame" in whi h we re ord the variable being set and the hoi e that was made. This hoi e of variable and value an be alled a \move" made by the ba ktra k pro edure. Dynami ordering an be helpful also after ba ktra king has taken pla e. If we ontinue the example above, where x0001 = 0001 and we've explored all ases in whi h x0011 = 0011, we aren't obliged to ontinue by trying another value for x0011 . We do want to remember that 0011 should no longer be onsidered legal, until x0001 hanges; but we ould de ide to explore next a ase su h as x0002 = 2000 at level 2. In fa t, x0002 = 2000 is qui kly seen to be impossible in the presen e of 0001 (see exer ise 34). An even more eÆ ient hoi e at level 2, however, is x0012 = 0012, be ause that bran h immediately for es x0002 = 0002, x0022 = 0022, x0122 = 0122, x0222 = 0222, x1222 = 1222, and x0011 = 1001. Sequential allo ation redux. The hoi e of a variable and value on whi h to bran h is a deli ate tradeo . We don't want to devote more time to planning than we'll save by having a good plan. If we're going to bene t from dynami ordering, we'll need eÆ ient data stru tures that will lead to good de isions without mu h deliberation. On the other hand, elaborate data stru tures need to be updated whenever we bran h to a new level, and they need to be downdated whenever we return from that level. Algorithm L illustrates an eÆ ient me hanism based on linked lists; but sequentially allo ated lists are often even more appealing, be ause they are a hefriendly and they involve fewer a esses to memory. Assume then that we wish to represent a set of items as an unordered sequential list. The list begins in a ell of memory pointed to by HEAD, and TAIL points just beyond the end of the list. For example, HEAD November 12, 2016 TAIL 3 9 1 4 (16) frame move sequential lists{ downdating vs updating a he-friendly unordered sequential list ells of memory MEM, an array of \ ells"{ 12 7.2.2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) is one way to represent the set f1; 3; 4; 9g. The number of items urrently 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 an easily delete MEM[P℄: TAIL TAIL 1; if P 6= TAIL, set MEM[P℄ MEM[TAIL℄: (18) (We've ta itly 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 over ow.) We an't delete an item from a list without knowing its MEM lo ation. 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) be omes the following, if M = 10: HEAD TAIL 3 9 1 4 (19) IHEAD (Shaded ells have unde ned ontents.) With this setup, insertion (17) be omes MEM[TAIL℄ x; MEM[IHEAD + x℄ TAIL; TAIL TAIL + 1; (20) and TAIL will never ex eed HEAD + M. Similarly, deletion of x be omes P MEM[IHEAD + x℄; TAIL TAIL 1; if P 6= TAIL, set y MEM[TAIL℄, MEM[P℄ y, MEM[IHEAD + y℄ P. (21) For example, after deleting `9' from (19) we would obtain this: HEAD TAIL 3 4 1 (22) IHEAD In more elaborate situations we also want to test whether or not a given item x is present. If so, we an keep more information in the inverse list. A parti ularly useful variation arises when the list that begins at IHEAD ontains a omplete permutation of the values fHEAD; HEAD + 1; : : : ; HEAD + M 1g, and the memory ells beginning at HEAD ontain the inverse permutation | although only the rst TAIL HEAD elements of that list are onsidered to be \a tive." For example, in our ommafree ode problem with m = 3, we an begin by putting items representing the M = 18 y le lasses [0001℄, [0002℄, : : : , [1222℄ into memory ells HEAD through HEAD + 17. Initially they're all a tive, with November 12, 2016 empty insert over ow inverse list inverse permutation a tive 7.2.2 BACKTRACK PROGRAMMING 13 TAIL = HEAD + 18 and MEM[IHEAD + ℄ = HEAD + for 0 < 18. Then whenever we de ide to hoose a odeword for lass , we delete from the a tive list by using a souped-up version of (21) that maintains full permutations: P MEM[IHEAD + ℄; TAIL TAIL 1; if P 6= TAIL, set y MEM[TAIL℄, MEM[TAIL℄ , MEM[P℄ y, MEM[IHEAD + ℄ TAIL; MEM[IHEAD + y ℄ P: (23) Later on, after ba ktra king to a state where we on e again want to be onsidered a tive, we simply set TAIL TAIL + 1, be ause will already be in pla e! Lists for the ommafree problem. The task of nding all four-letter ommafree odes is not diÆ ult when m = 3 and only 18 y le lasses are involved. But it already be omes hallenging when m = 4, be ause we must then deal with (44 42)=4 = 60 lasses. Therefore we'll want to give it some areful thought as we try to set it up for ba ktra king. The example s enarios for m = 3 onsidered above suggest that we'll repeatedly want to know the answers to questions su h as, \How many words of the form 02 are still available for sele tion as odewords?" Redundant data stru tures, oriented to queries of that kind, appear to be needed. Fortunately, we shall see that there's a ni e way to provide them, using sequential lists as in (19){(23). In Algorithm C below, ea h of the m4 four-letter words is given one of three possible states during the sear h for ommafree odes. A word is green if it's part of the urrent set of tentative odewords. It is red if it's not urrently a andidate for su h status, either be ause it is in ompatible with the existing green words or be ause the algorithm has already examined all s enarios in whi h it is green in their presen e. Every other word is blue, and sort of in limbo; the algorithm might or might not de ide to make it red or green. All words are initially blue | ex ept for the m2 periodi words, whi h 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 urrent state of word x is kept in MEM[ ℄, using one of the arbitrary internal odes 2 (GREEN), 0 (RED), or 1 (BLUE). The most important feature of the algorithm is that every blue word x = x1 x2 x3 x4 is potentially present in seven di erent lists, alled 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 mat hing x1 , x1 x2 , x1 x2 x3 ; S1(x), S2(x), S3(x) are the blue words mat hing x4 , x3 x4 , x2 x3 x4 ; CL(x) hosts the blue words in fx1 x2 x3 x4 ; x2 x3 x4 x1 ; x3 x4 x1 x2 ; x4 x1 x2 x3 g. These seven lists begin respe tively in MEM lo ations P1OFF+p1 ( ), P2OFF+p2( ), P3OFF + p3 ( ), S1OFF + s1 ( ), S2OFF + s2 ( ), S3OFF + s3 ( ), and CLOFF +4 l( ); here (P1OFF, P2OFF, P3OFF, S1OFF, S2OFF, S3OFF, CLOFF) are respe tively (2m4 , 5m4 , 8m4, 11m4, 14m4 , 17m4, 20m4). We de ne p1 ((x1 x2 x3 x4 )m ) = (x1 )m , p2 ((x1 x2 x3 x4 )m ) = (x1 x2 )m , p3 ((x1 x2 x3 x4 )m ) = (x1 x2 x3 )m , s1 ((x1 x2 x3 x4 )m ) = (x4 )m , s2 ((x1 x2 x3 x4 )m ) = (x3 x4 )m , s3 ((x1 x2 x3 x4 )m ) = (x2 x3 x4 )m ; and nally l((x1 x2 x3 x4 )m ) is an internal number between 0 and (m4 m2 )=4 1 assigned November 12, 2016 delete data stru tures periodi radix m 14 7.2.2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) re e tion symmetry breaking losed Table 1 0 10 20 30 40 50 60 70 80 90 a0 b0 0 d0 e0 f0 100 110 120 130 140 150 0 1 RED BLUE 20 0001 0010 25 50 0001 0010 53 80 0001 81 b8 0010 0110 b4 e4 1100 e1 112 LISTS USED BY ALGORITHM C (m = 2), ENTERING LEVEL 1 2 3 4 5 6 7 8 BLUE BLUE RED RED BLUE BLUE RED 21 22 23 24 0011 0110 0111 1100 2d 51 52 54 55 0011 0110 0111 1001 56 5a 82 83 86 87 0010 0011 0110 0111 1001 84 84 88 89 b0 b9 b1 bb 1100 1110 0001 be e8 e e9 ed 0001 1001 1101 0010 e7 eb 114 116 11 11e 0001 1001 0010 0011 1011 1100 110 114 115 118 119 140 141 144 145 148 0001 0010 0011 0110 1100 1001 0111 142 148 14 9 a b d e f BLUE RED BLUE BLUE BLUE BLUE RED 29 2 28 2b 2a 1001 1110 1101 1011 P1 58 1011 P2 5 5e 5d 1100 1110 1101 5f 88 8a 8 8d 8e 1011 1100 1101 1110 8b 8e 8f ba bd b2 b b3 0011 1001 0111 1101 1011 e5 0110 1110 59 ee e0 0011 ef 113 117 118 1101 0110 11b 11e 147 14b 146 1110 1101 1011 e6 ea 0111 1011 11a 11d 1110 0111 11f 14a 149 This table shows MEM lo ations 0000 through 150f, using hexade imal notation. (For example, MEM[40d℄=5e; see exer ise 36.) Blank entries are unused by the algorithm. to ea h lass. The seven MEM lo ations where x appears in these seven lists are respe tively kept in inverse lists that begin in MEM lo ations P1OFF m4 + , P2OFF m4 + , : : : , CLOFF m4 + . And the TAIL pointers, whi h indi ate the urrent list sizes as in (19){(23), are respe tively kept in MEM lo ations P1OFF + m4 + , P2OFF + m4 + , : : : , CLOFF + m4 + . (Whew; got that?) This vast apparatus, whi h o upies 22m4 ells of MEM, is illustrated in Table 1, at the beginning of the omputation for the ase m = 2. Fortunately it's not really as ompli ated as it may seem at rst. Nor is it espe ially vast: After all, 22m4 is only 13,750 when m = 5. (A lose inspe tion of Table 1 reveals in identally that the words 0100 and 1000 have been olored red, not blue. That's be ause we an assume without loss of generality that lass [0001℄ is represented either by 0001 or by 0010. The other two ases are overed by left-right re e tion of all odewords.) Algorithm C nds these lists invaluable when it is de iding where next to bran h. But it has no further use for a list in whi h one of the items has be ome green. Therefore it de lares su h lists \ losed"; and it saves most of the work of list maintenan e by updating only the lists that remain open. A losed list is represented internally by setting its TAIL pointer to HEAD 1. For example, Table 2 shows how the lists in MEM will have hanged just after x = 0010 has been hosen to be a tentative odeword. The elements f0001; 0010; 0011; 0110; 0111g of P1(x) are e e tively hidden, be ause the tail pointer MEM[30℄ = 1f = 20 1 marks that list as losed. (Those list elements a November 12, 2016 P3 S1 S2 S3 CL 7.2.2 BACKTRACK PROGRAMMING 15 undoing{ Floyd ompiler Table 2 LISTS USED BY ALGORITHM C (m = 2), ENTERING LEVEL 2 0 1 2 3 0 RED RED GREEN BLUE 10 20 30 1f 40 50 60 4f 70 80 90 80 81 a0 b9 b0 0 af d0 e e0 1100 f0 e1 100 116 110 120 110 112 130 144 140 150 13f 4 RED 5 6 7 8 RED BLUE BLUE RED 0110 0111 56 84 1101 e5 54 55 86 87 0110 0111 88 bb ed 11 11e 0011 1011 113 118 145 148 0011 0110 1100 147 9 RED a b d RED BLUE BLUE BLUE 29 28 2b 1100 1011 1110 1101 2 58 5 5e 1011 1100 1110 59 5f 8a 8 8d 1011 1100 1101 88 8b 8e b8 ba 1011 0011 1101 0111 b ee e0 e4 0011 0111 e7 ef 117 118 11a 1100 1101 0110 1110 119 11b 11e 14b 146 14a 0111 1110 1101 1011 14 e f BLUE RED 2a 5d 1101 8e 1110 8f 1011 11d 0111 11f 149 tually do still appear in MEM lo ations 200 through 204, 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.) We're almost ready to nalize the details of Algorithm C and to get on with the sear h for ommafree odes, but a big problem still remains: The state of omputation at every level of the sear h involves all of the marvelous lists that we've just spe i ed, and those lists aren't tiny. They o upy more than 5000 ells of MEM when m = 4, and they an hange substantially from level to level. We ould make a new opy of the entire state, whenever we advan e to a new node of the sear h tree. But that's a bad idea, be ause we don't want to perform thousands of memory a esses per node. A mu h better strategy would be to sti k with a single instan e of MEM, and to update and downdate the lists as the sear h progresses, if we ould only think of a simple way to do that. And we're in lu k: There is su h a way, rst formulated by R. W. Floyd in his lassi paper \Nondeterministi algorithms" [JACM 14 (1967), 636{644℄. Floyd's original idea, whi h required a spe ial ompiler to generate forward and ba kward versions of every program step, an in fa t be greatly simpli ed when all of the hanges in state are on ned to a single MEM array. All we need to do is to repla e every assignment operation of the form `MEM[a℄ v' by the November 12, 2016 P2 P3 S1 The word 0010 has be ome green, thus losing its seven lists and making 0001 red. The logi of Algorithm C has also made 1001 red. Hen e 0001 and 1001 have been deleted from the open lists in whi h they formerly appeared (see exer ise 37). A general me hanism for doing and undoing. P1 S2 S3 CL 16 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 slightly more umbersome operation store(a; v) : Set UNDO[u℄ a; MEM[a℄ , MEM[a℄ v, and u u + 1. (24) Here UNDO is a sequential sta k that holds (address, value) pairs; in our appliation we ould say `UNDO[u℄ (a 16) + MEM[a℄', be ause the ell addresses and values never ex eed 16 bits. Of ourse we'll also need to he k that the sta k 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 hanges to MEM sin e the time when u had rea hed a parti ular value u0, we simply do this: unstore(u0 ) : While u > u0, set u u 1, (a; v) UNDO[u℄, and MEM[a℄ v. (25) In our appli ation the unsta king operation `(a; v) UNDO[u℄' here ould be implemented by saying `a UNDO[u℄ 16, v UNDO[u℄ & # ffff'. A useful re nement of this reversible-memory te hnique 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 sta k when the same memory address is updated more than on e in the same round. store(a; v) : If STAMP[a℄ 6= , 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 ea h address in MEM. It's initially all zero, and is initially 1. Whenever we ome to a fallba k point, where the urrent sta k pointer will be remembered as the value u0 for some future undoing, we \bump" the urrent stamp by setting + 1. Then (26) will ontinue to do the right thing. (In programs that run for a long time, we must be areful when integer over ow auses to be bumped to zero; see exer ise 38.) Noti e that the ombination of (24) and (25) will perform ve memory a esses for ea h assignment and its undoing. The ombination of (26) and (25) will ost seven mems for the rst assignment to MEM[a℄, but only two mems for every subsequent assignment to the same address. So (26) wins, if multiple assignments ex eed one-time-only assignments. Ba ktra king through ommafree odes. OK, we're now equipped with enough basi knowhow to write a pretty good ba ktra k program for the problem of generating all ommafree four-letter odes. Algorithm C below in orporates one more key idea, whi h is a lookahead me hanism that is spe i to ommafree ba ktra king; we'll all it the \poison list." Every item on the poison list is a pair, onsisting of a suÆx and a pre x that the ommafree rule forbids from o urring together. Every green word x1 x2 x3 x4 | that is, every word that will be a nal odeword in the urrent bran h of our ba ktra k sear h | ontributes three items to the poison list, namely (x1 x2 x3 ; x4 ); (x1 x2 ; x3 x4 ); and (x1 ; x2 x3 x4 ): (27) November 12, 2016 UNDO sta k reversible-memory stamping fallba k point bump over ow lookahead poison list 7.2.2 BACKTRACK PROGRAMMING 17 If there's a green word on both sides of a poison list entry, we're dead: The ommafree ondition fails, and we must ba ktra k. If there's a green word on one side but not the other, we an kill o all blue words on the other side by making them red. And if either side of a poison list entry orresponds to an empty list, we an remove this entry from the poison list be ause it will never a e t the out ome. (Blue words be ome red or green, but red words stay red.) For example, onsider the transition from Table 1 to Table 2. When word 0010 be omes green, the poison list re eives its rst three items: (001; 0); (00; 10); (0; 010): The rst of these kills o the 001 list, be ause 0 ontains the green word 0010. That makes 1001 red. The last of these, similarly, kills o the 010 list; but that list is empty when m = 2. The poison list now redu es to a single item, (00; 10), whi h remains poisonous be ause list 00 ontains the blue word 1100 and 10 ontains the blue word 1011. We'll maintain the poison list at the end of MEM, following the CL lists. It obviously will ontain at most 3(m4 m2)=4 entries, and in fa t 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 ells per entry so that TAIL will hange by 2 instead of by 1. The value of TAIL will be stored in MEM at key times so that temporary hanges to it an be undone. The ase m = 4, in whi h ea h odeword onsists of four quaternary digits f0; 1; 2; 3g, is parti ularly interesting, be ause an early ba ktra k program by Lee Laxdal found that no su h ommafree ode an make use of all 60 of the y le lasses [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 lasses must be omitted; and it found several valid 57-word sets. Further details were never published, be ause the proof that 58 odewords are impossible depended on what Jiggs alled a \quite time- onsuming" omputation. Be ause size 60 is impossible, our algorithm annot simply assume that a move su h as 1001 is for ed when the other words 0011, 0110, 1100 of its lass have been ruled out. We must also onsider the possibility that lass [0011℄ is entirely absent from the ode. Su h onsiderations add an interesting further twist to the problem, and Algorithm C des ribes one way to ope with it. Algorithm C (Four-letter ommafree odes ). Given an alphabet size m 7 and a goal g in the range L m(m 1) g L, where L = (m4 m2 )=4, this algorithm nds all sets of g four-letter words that are ommafree and in lude either 0001 or 0010. It uses an array MEM of M = b23:5m4 16-bit numbers, as well as several more auxiliary arrays: ALF of size 163m; STAMP of size M ; X, C, S, and U of size L + 1; FREE and IFREE of size L; and a suÆ iently large array alled UNDO whose maximum size is diÆ ult to guess. C1. [Initialize.℄ Set ALF[(ab d)16℄ (ab d)m for 0 a; b; ; d < m. Set STAMP[k ℄ 0 for 0 k < M and 0. Put the initial pre x, suÆx, and lass lists into MEM, as in Table 1. Also reate an empty poison list by November 12, 2016 inverse list Laxdal Jiggs stamping++ 18 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 setting MEM[PP℄ POISON, where POISON = 22m4 and PP = POISON 1. Set FREE[k℄ IFREE[k℄ k for 0 k < L. Then set l 1, x # 0001, 0, s L g, f L, u 0, and go to step C3. (Variable l is the level, x is a trial word, is its lass, s is the \sla k," f is the number of free lasses, and u is the size of the UNDO sta k.) C2. [Enter level l .℄ If l > L, visit the solution x1 : : : xL and go to C6. Otherwise hoose a andidate word x and lass as des ribed in exer ise 39. C3. [Try the andidate.℄ 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 stru tures to make x green, as des ribed in exer ise 40, es aping to C5 if trouble arises. C4. [Make the move.℄ Set X[l ℄ x, C[l℄ , S[l℄ s, p IFREE[ ℄, f f 1. If p 6= f , set y FREE[f ℄, FREE[p℄ y, IFREE[y℄ p, FREE[f ℄ , IFREE[ ℄ 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 exer ise 40). Go to C2. C6. [Ba ktra k.℄ Set l l 1, and terminate if l = 0. Otherwise set x X[l℄, C[l℄, f f 1. If x < 0, repeat this step ( lass was omitted from the ode). Otherwise set s S[l℄ and go ba k to C5. Exer ises 39 and 40 provide the instru tive details that esh 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 nd the 1152 solutions for g = 57; see exer ise 44. There are roughly (14, 240, 3700, 38000) thousand nodes in the respe tive sear h trees, with most of the a tivity taking pla e on levels 30 10. The height of the UNDO sta k never ex eeds 2804, and the poison list never ontains more than 12 entries at a time. Running time estimates. Ba ktra k programs are full of surprises. Sometimes they produ e instant answers to a supposedly diÆ ult problem. But sometimes they spin their wheels endlessly, trying to traverse an astronomi ally large sear h tree. And sometimes they deliver results just about as fast as we might expe t. Fortunately, we needn't sit in the dark. There's a simple Monte Carlo algorithm by whi h we an often tell in advan e whether or not a given ba ktra k strategy will be feasible. This method, based on random sampling, an a tually be worked out by hand before writing a program, in order to help de ide whether to invest further time while following a parti ular approa h. In fa t, the very a t of arrying out this pleasant pen il-and-paper method often suggests useful uto strategies and/or data stru tures that will be valuable later when a program is being written. For example, the author developed Algorithm C above after rst doing some arm hair experiments with random hoi es of potential ommafree odewords, and noti ing that a family of lists su h as those in Tables 1 and 2 would be quite helpful when making further hoi es. To illustrate the method, let's onsider the n queens problem again, as represented in Algorithm B* above. When n = 8, we an obtain a de ent \ballpark November 12, 2016 Running time estimates of run time{ Monte Carlo algorithm random sampling pen il-and-paper method uto strategies data stru tures author 7.2.2 q q q q 19 BACKTRACK PROGRAMMING q q q ^001^001^00^00^ (a) 8 5 4 3 1 1 1 0 q q q q q q q 8 5 3 3 2 1 1 0 ^101^101^111^110^000^10^10^1^ (b) q q q q q q ^100^111^100^01^1^0^ () 8 5 4 2 1 2 0 q q q q q q ^100^100^11^11^00^0^ (d) Fig. 69. Four random attempts to solve the 8 queens problem. Su h experiments help to estimate the size of the ba ktra k tree in Fig. 68. The bran hing degrees are shown at the right of ea h diagram, while the random bits used for sampling appear below. Cells have been shaded in gray if they are atta ked by one or more queens in earlier rows. estimate" of the size of Fig. 68 by examining only a few random paths in that sear h tree. We start by writing down the number D1 8, be ause there are eight ways to pla e the queen in row 1. (In other words, the root node of the sear h tree has degree 8.) Then we use a sour e of random numbers | say the binary digits of mod 1 = (:001001000011 : : : )2 | to sele t one of those pla ements. Eight hoi es are possible, so we look at three of those bits; we shall set X1 2, be ause 001 is the se ond of the eight possibilities (000, 001, : : : , 111). Given X1 = 2, the queen in row 2 an't go into olumns 1, 2, or 3. Hen e ve possibilities remain for X2 , and we write down D2 5. The next three bits of lead us to set X2 5, sin e 5 is the se ond of the available olumns (4, 5, 6, 7, 8) and 001 is the se ond value of (000, 001, : : : , 100). If had ontinued with 101 or 110 or 111 instead of 001, we would in identally have used the \reje tion method" of Se tion 3.4.1 and moved to the next three bits; see exer ise 47. Continuing in this way leads to D3 4, X3 1; then D4 3, X4 4. (Here we used the two bits 00 to sele t X3 , and the next two bits 00 to sele t X4 .) The remaining bran hes are for ed: D5 1, X5 7; D6 1, X6 3; D7 1, X7 6; and we're stu k when we rea h level 8 and nd D8 0. These sequential random hoi es are depi ted in Fig. 69(a), where we've used them to pla e ea h queen su essively into an unshaded ell. Parts (b), ( ), and (d) of Fig. 69 orrespond in the same way to hoi es based on the binary digits of e mod 1, mod 1, and mod 1. Exa tly 10 bits of , 20 bits of e, 13 bits of , and 13 bits of were used to generate these examples. In this dis ussion the notation Dk stands for a bran hing degree, not for a domain of values. We've used upper ase letters for the numbers D1 , X1 , D2 , et ., be ause those quantities are random variables. On e we've rea hed Dl = 0 at some level, we're ready to estimate the overall ost, by impli itly assuming that the path we've taken is representative of all root-to-leaf paths in the tree. The ost of a ba ktra k program an be assessed by summing the individual amounts of time spent at ea h node of the sear h tree. Noti e that every node on level l of that tree an be labeled uniquely by a sequen e x1 : : : x l 1 , whi h de nes the path from the root to that node. Thus our goal is to estimate the sum of all (x1 : : : x l 1 ), where (x1 : : : x l 1 ) is the ost asso iated with node x1 : : : x l 1 . November 12, 2016 8 5 4 3 2 1 0 sear h tree root node degree pi as sour e+ reje tion method e, as sour e phi, as sour e gamma, as sour e random variables ost fun tion 20 7.2.2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) For example, the four queens problem is represented by the sear h tree (4), and its ost is the sum of 17 individual osts () + (1) + (13) + (14) + (142) + (2) + (24) + + (413) + (42): (28) If C (x1 : : : x l ) denotes the total ost of the subtree rooted at x1 : : : x l , then (d) C (x1 : : : x l ) = (x1 : : : x l ) + C (x1 : : : x l x(1) (29) l+1 ) + + C (x1 : : : x l x l+1 ) (d) when the hoi es for x l+1 at node x1 : : : x l are fx(1) l+1 ; : : : ; x l+1 g. For instan e in (4) we have C (1) = (1) + C (13) + C (14); C (13) = (13); and C () = () + C (1) + C (2) + C (3) + C (4) is the overall ost (28). In these terms a Monte Carlo estimate for C () is extremely easy to ompute: Theorem E. Given D1 , X1 , D2 , X2 , : : : as above, the ost of ba ktra king is C () = E () + D1 ( (X1 ) + D2 ( (X1 X2 ) + D3 ( (X1 X2 X3 ) + ))) : (30) Proof. Node x1 : : : x l , with bran h degrees d1 , : : : , d l above it, is rea hed with probability 1=d1 : : : d l ; so it ontributes d1 : : : d l (x1 : : : x l )=d1 : : : d l = (x1 : : : x l ) to the expe ted value in this formula. For example, the tree (4) has six root-to-leaf paths, and they o ur with respe tive probabilities 1/8, 1/8, 1/4, 1/4, 1/8, 1/8. The rst one ontributes 1/8 times ()+4( (1) +2( (13))), namely ()=8 + (1)=2 + (13), to the expe ted value. The se ond ontributes ()=8 + (1)=2 + (14) + (142); and so on. A spe ial ase of Theorem E, with all (x1 : : : x l ) = 1, tells us how to estimate the total size of the tree, whi h is often a ru ial quantity: Corollary E. The number of nodes in the sear h tree, given D1 , D2 , : : : , is (31) E(1 + D1 + D1 D2 + ) = E 1 + D1 1 + D2 (1 + D3 (1 + )) : For example, Fig. 69 gives us four estimates for the size of the tree in Fig. 68, using the numbers Dj at the right of ea h 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 respe tively 2689, 1489, 2609. None of them is extremely far from the true number, 2057, although we an't expe t to be so lu ky all the time. The detailed study in exer ise 51 shows that the estimate (31) in the ase of 8 queens turns out to be quite well behaved: p (32) min 489; ave 2057; max 7409; dev 1146640 1071 : The analogous problem for 16 queens has a mu h less homogeneous sear h 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 orre t order of magnitude. (For example, ten independent experiments predi ted .632, .866, .237, 1.027, 4.006, .982, .143, .140, 3.402, and .510 billion nodes, respe tively. The mean of these is 1.195.) A thousand trials with n = 64 suggest that the problem of 64 queens will have about 3 1065 nodes in its tree. November 12, 2016 subtree size of the tree standard deviation 7.2.2 BACKTRACK PROGRAMMING 21 Let's formulate this estimation pro edure pre isely, so that it an be performed onveniently by ma hine as well as by hand: Algorithm E (Estimated ost of ba ktra k ). Given domains Dk and properties P l as in Algorithm B, together with node osts (x1 : : : x l ) as above, this algorithm omputes the quantity S whose expe ted value is the total ost C () in (30). It uses an auxiliary array y1 y2 : : : whose size should be max(jD1 j; : : : ; jDn j). E1. [Initialize.℄ Set l D 1, S 0, and initialize any data stru tures needed. E2. [Enter level l .℄ (At this point P l 1 (X1 ; : : : ; Xl 1 ) holds.) Set S S+ D (X1 : : : Xl 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 (X1 ; : : : ; Xl 1 ; x) holds, set yd x and d d + 1. E4. [Try again.℄ If x 6= max D l , set x to the next larger element of D l and return to step E3. E5. [Choose and try.℄ If d = 0, terminate. Otherwise set D D d and X l yI , where I is a uniformly random integer in f0; : : : ; d 1g. Update the data stru tures to fa ilitate testing P l+1 , set l l + 1, and go ba k to E2. Although Algorithm E looks rather like Algorithm B, it never ba ktra ks. Of ourse we an't expe t this algorithm to give de ent estimates in ases where the ba ktra k tree is wildly errati . The expe ted value of S , namely E S , is indeed the true ost; but the probable values of S might be quite di erent. An extreme example of bad behavior o urs if property P l is the simple ondition `x1 > > x l ' and all domains are f1; : : : ; ng. Then there's only one solution, x1 : : : xn = n : : : 1; and ba ktra king is a parti ularly stupid way to nd it! The sear h tree for this somewhat ridi ulous problem is, nevertheless, quite interesting. It is none other than the binomial tree Tn of Eq. 7.2.1.3{(21), whi h has nl nodes on level l + 1 and 2n nodes in total. If we set all osts to 1, the expe ted value of S is therefore 2n = en ln 2 . But2 exer ise 50 proves that S will almost always be mu h smaller, less than e(ln n) ln ln n . Furthermore the average value of l when Algorithm E terminates with respe t to Tn is only Hn +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 re nements of Algorithm E are possible. For example, exer ise 52 shows that the hoi es in step E5 need not be uniform. We shall dis uss improved estimation te hniques in Se tion 7.2.2.9, after having seen numerous examples of ba ktra king in pra ti e. *Estimating the number of solutions. Sometimes we know that a problem has more solutions than we ould ever hope to generate, yet we still want to know roughly how many there are. Algorithm E will tell us the approximate number, in ases where the ba ktra k pro ess never rea hes a dead end | that is, if it never terminates with d = 0 in step E5. There may be another riterion for su essful termination in step E2 even though l might still be n. The expe ted nal value of D is exa tly the total number of solutions, be ause every solution X1 : : : X l onstru ted by the algorithm is obtained with probability 1=D. November 12, 2016 binomial tree estimating solutions 22 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 For example, suppose we want to know the number of di erent paths by whi h a king an go from one orner of a hessboard to the opposite orner, without revisiting any square. One su h path, hosen at random using the bits of for guidan e as we did in Fig. 69(a), is shown here. Starting in the upper left orner, we have 3 hoi es for the rst move. Then, after moving to the right, there are 3 4 2 4 hoi es for the se ond move. And so on. 6 6 6 We never make a move that would dis onne t us from the goal; in parti ular, two of 1 5 4 5 7 the moves are a tually for ed. (Exer ise 58 explains one way to avoid fatal mistakes.) 4 4 4 4 The probability of obtaining this parti 5 4 5 5 6 1 ular path is exa tly 31 41 16 61 21 16 71 : : : 12 = 1=D, 3 where D = 3 4 6 6 2 6 7 2 = 4 3 5 6 5 3 4 12 24 34 410 59 66 71 8:7 1020 . Thus we an reasonably guess, at least tentatively, 4 5 6 5 that there are 1021 su h paths, more or less. 2 Of ourse that guess, based on a single 2 2 random sample, rests on very shaky grounds. But we know that the average value MN = (D(1) + + D(N ) )=N of N guesses, in N independent experiments, will almost surely approa h the orre t number. How large should N be, before we an have any on den e in the results? The a tual values of D obtained from random king paths tend to vary all over the map. Figure 70 plots typi al results, as N varies from 1 to 10000. For ea h value of N we an follow the advi e of statisti s textbooks and alpulate the sample varian e VN = SN =(N 1) as in Eq. 4.2.2{(16); then MN VN =N is the textbook estimate. The top diagram in Fig. 70 shows these \error bars" in gray, surrounding bla k dots for MN . This sequen e MN does appear to settle down after N rea hes 3000 or so, and to approa h a value near 5 1025. That's mu h higher than our rst guess, but it has lots of eviden e to ba k it up. On the other hand, the bottom hart in Fig. 70 shows the distribution of the logarithms of the 10000 values of D that were used to make the top hart. Almost half of those values were totally negligible | less than 1020. About 75% of them were less than 1024. But some of them* ex eeded 1028. Can we really rely on a result that's based on su h haoti behavior? Is it really right to throw away most of our data and to trust almost entirely on observations that were obtained from omparatively few rare events? Yes, we're okay! Some of the justi ation appears in exer ise MPR{124, whi h is based on theoreti al work by P. Dia onis and S. Chatterjee. In the paper ited with that exer ise, they defend a simple measure of quality, max(D(1) ; : : : ; D(N )) ; QN = max(D(1) ; : : : ; D(N ) )=(NMN ) = (34) D(1) + + D(N ) k * Four of the a tual values that led to Fig. 70 were larger than 1028 ; the largest, 2:11028 , ame from a path of length 57. The smallest estimate, 19361664, ame from a path of length 10. November 12, 2016 king hessboard simple paths paths, simple for guidan e statisti s sample varian e varian e error bars dis arded data Dia onis Chatterjee 7.2.2 BACKTRACK PROGRAMMING 23 81025 ZDD Hamiltonian paths knight 25 510 21025 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 0.5 0.0 1025 20 10 Estimates of the number of king paths, based on up to 10000 random trials. The middle graph shows the orresponding quality measures of Eq. (34). The lower graph shows the logarithms of the individual estimates D( ) , after they've been sorted. Fig. 70. k arguing that a reasonable poli y in most experiments su h as these is to stop sampling when QN gets small. (Values of this statisti QN have been plotted in the middle of Fig. 70.) Furthermore we an estimate other properties of the solutions to a ba ktra k problem, instead of merely ounting those solutions. For example, the expe ted value of lD on termination of the random king's path algorithm is the total length of su h paths. The data underlying Fig. 70 suggests that this total is (2:66 :14) 1027; hen e the average path length appears to be about 53. The samples also indi ate that about 34% of the paths pass through the enter; about 46% tou h the upper right orner; about 22% tou h both orners; and about 7% pass through the enter and both orners. For this parti ular problem we don't a tually need to rely on estimates, be ause the ZDD te hnology of Se tion 7.1.4 allows us to ompute the true values. (See exer ise 59.) The total number of simple orner-to- orner king paths on a hessboard is exa tly 50,819,542,770,311,581,606,906,543; this value lies almost within the error bars of Fig. 70 for all N 250, ex ept for a brief interval near N = 1400. And the total length of all these paths turns out to be exa tly 2,700,911,171,651,251,701,712,099,831, whi h is a little higher than our estimate. The true average length is therefore 53:15. The true probabilities of hitting the enter, a given orner, both orners, and all three of those spots are respe tively about 38.96%, 50.32%, 25.32%, and 9.86%. The total number of orner-to- orner king paths of the maximum length, 63, is 2,811,002,302,704,446,996,926. This is a number that an not be estimated well by a method su h as Algorithm E without additional heuristi s. The analogous problem for orner-to- orner knight paths, of any length, lies a bit beyond ZDD te hnology be ause many more ZDD nodes are needed. Using Algorithm E we an estimate that there are about (8:6 1:2) 1019 su h paths. November 12, 2016 24 7.2.2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) Imagine an instan e of ba ktra king that is equivalent to solving two independent subproblems. For example, we might be looking for all sequen es x = x1 x2 : : : xn that satisfy Pn (x1 ; x2 ; : : : ; xn ) = F (x1 ; x2 ; : : : ; xn ), where F (x1 ; x2 ; : : : ; xn ) = G(x1 ; : : : ; xk ) ^ H (xk+1 ; : : : ; xn ): (35) Then the size of the ba ktra k tree is essentially the produ t of the tree sizes for G and for H , even if we use dynami ordering. Hen e it's obviously foolish to apply the general setup of (1) and (2). We an do mu h better by nding all solutions to G rst, then nding all solutions to H , thereby redu ing the amount of omputation to the sum of the tree sizes. Again we've divided and onquered, by fa toring the ompound problem (35) into separate subproblems. We dis ussed a less obvious appli ation of problem fa torization near the beginning of Chapter 7, in onne tion with latin squares: Re all that E. T. Parker sped up the solution of 7{(6) by more than a dozen orders of magnitude, when he dis overed 7{(7) by essentially fa toring 7{(6) into ten subproblems whose solutions ould readily be ombined. In general, ea h solution x to some problem F often implies the existen e of solutions x(p) = p (x) to various simpler problems Fp that are \homomorphi images" of F . And if we're lu ky, the solutions to those simpler problems an be ombined and \lifted" to a solution of the overall problem. Thus it pays to be on the lookout for su h simpli ations. Let's look at another example. F. A. S hossow invented a tantalizing puzzle [U.S. Patent 646463 (3 April 1900)℄ that \went viral" in 1967 when a marketing genius de ided to rename it Instant Insanity . The problem is to take four ubes su h as Fa toring the problem. R | } Cube 4 | | ~ } } | | | } | ~ Cube 3 } ~~ ~ ~ Cube 2 Cube 1 (36) where ea h fa e 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 ba k sides. The pla ement in (36) is in orre t, be ause there are two | s (and no ) on top. But we get a solution if we rotate ea h ube by 90Æ. There are 24 ways to pla e ea h ube, be ause any of the six fa es an be on top and we an rotate four ways while keeping the top un hanged. So the total number of pla ements is 244 = 331776. But this problem an be fa tored in an ingenious way, so that all solutions an be found qui kly by hand! [See F. de Carteblan he, Eureka 9 (1947), 9{11.℄ The idea is that any solution to the puzzle gives us two ea h of f|; }; ~; g, if we look only at the top and bottom or only at the front and ba k. That's a mu h easier problem to solve. For this purpose a ube an be hara terized by its three pairs of markings on opposite fa es; in (36) these fa e-pairs are respe tively f|}; |; ~g; f||; |~; }g; f~~; |}; }g; f|~; ~; }g: (37) November 12, 2016 fa torization of problems{ independent subproblems ba ktra k tree tree sizes dynami ordering divide and onquer paradigm latin squares Parker homomorphi images lifted S hossow Patent Tantalizer, see Instant InsanityTM Armbruster Instant InsanityTM Carteblan he 7.2.2 BACKTRACK PROGRAMMING 25 Whi h of the 34 = 81 ways to hoose one fa e-pair from ea h ube will give us f|; |; }; }; ~; ~; ; g? They an all be dis overed in a minute or two, by listing the nine possibilities for ubes (1; 2) and the nine for (3; 4). We get just three, (|}; |~; }; ~); (~; |~; |}; }); (~; }; |}; |~): (38) Noti e furthermore that ea h solution an be \halved" so that one ea h of f|; }; ~; g appears on both sides, by swapping fa e-pairs; we an hange (38) to (}|; |~; }; ~); (~; |~; }|; }); (~; }; }|; |~): (39) Ea h of these solutions to the opposite-fa e subproblem an be regarded as a 2-regular graph, be ause every vertex of the multigraph whose edges are (say) } |, | ~, }, ~ has exa tly two neighbors. A solution to Instant Insanity will give us two su h 2-regular fa tors, one for top-and-bottom and one for front-and-ba k. Furthermore those two fa tors will have disjoint edges: We an't use the same fa e-pair in both. Therefore problem (36) an be solved only by using the rst and third fa tor in (39). Conversely, whenever we have two disjoint 2-regular graphs, we an always use them to position the ubes as desired, thus \lifting" the fa tors to a solution of the full problem. Exer ise 75 illustrates another kind of problem fa torization. R I may de ide to insert a (small?) amount of additional material here, as I prepare se tions 7.2.2.1{7.2.2.9. Histori al notes. The origins of ba ktra k programming are obs ure. Equivalent ideas must have o urred to many people, yet there was hardly any reason to write them down until omputers existed. We an be reasonably sure that James Bernoulli used su h prin iples in the 17th entury, when he su essfully solved the \Tot tibi sunt dotes" problem that had eluded so many others (see Se tion 7.2.1.7), be ause tra es of the method exist in his exhaustive list of solutions. Ba ktra k programs typi ally traverse the tree of possibilities by using what is now alled depth- rst sear h, a general graph exploration pro edure that E douard Lu as redited to a student named Tremaux [Re reations Mathematiques 1 (Paris: Gauthier-Villars, 1882), 47{50℄. The eight queens problem was rst proposed by Max Bezzel [S ha hzeitung 3 (1848), 363; 4 (1849), 40℄ and by Franz Nau k [Illustrirte Zeitung 14, 361 (1 June 1850), 352; 15, 377 (21 September 1850), 182℄, perhaps independently. C. F. Gauss saw the latter publi ation, and wrote several letters about it to his friend H. C. S huma her. Gauss's letter of 27 September 1850 is espe ially interesting, be ause it explained how to nd all the solutions by ba ktra king| whi h he alled `Tatonniren', from a Fren h term meaning \to feel one's way." He also listed the lexi ographi ally rst solutions of ea h equivalen e lass under re e tion and rotation: 15863724, 16837425, 24683175, 25713864, 25741863, 26174835, 26831475, 27368514, 27581463, 35281746, 35841726, and 36258174. November 12, 2016 2-regular graph disjoint lifting Bernoulli Tot tibi depth- rst sear h Lu as Tremaux eight queens problem Bezzel Nau k Gauss S huma her lexi ographi ally 26 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 Computers arrived a hundred years later, and people began to use them for ombinatorial problems. The time was therefore ripe for ba ktra king to be des ribed as a general te hnique, and Robert J. Walker rose to the o asion [Pro . Symposia in Applied Math. 10 (1960), 91{94℄. His brief note introdu ed Algorithm W in ma hine-oriented form, and mentioned that the pro edure ould readily be extended to nd variable-length patterns x1 : : : xn where n is not xed. The next milestone was a paper by Solomon W. Golomb and Leonard D. Baumert [JACM 12 (1965), 516{524℄, who formulated the general problem arefully and presented a variety of examples. In parti ular, they dis ussed the sear h for maximum ommafree odes, and noted that ba ktra king an be used to nd su essively better and better solutions to ombinatorial optimization problems. They introdu ed ertain kinds of lookahead, as well as the important idea of dynami ordering by bran hing on variables with the fewest remaining hoi es. Other noteworthy early dis ussions of ba ktra k 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 Gas hnig [Report CMU-CS-79-124 (Carnegie Mellon University, 1979), Chapter 4℄. Gas hnig introdu ed te hniques of \ba kmarking" and \ba kjumping" that we shall dis uss later. Monte Carlo estimates of the ost of ba ktra king were rst des ribed brie y 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 mu h more detailed exposition a de ade later, in Math. Comp. 29 (1975), 121{ 136. Su h methods an be onsidered as spe ial ases of so- alled \importan e sampling"; see J. M. Hammersley and D. C. Hands omb, Monte Carlo Methods (London: Methuen, 1964), 57{59. Studies of random self-avoiding walks su h as the king paths dis ussed above were inaugurated by M. N. Rosenbluth and A. W. Rosenbluth, J. Chemi al Physi s 23 (1955), 356{359. Ba ktra k appli ations are ni ely adaptable to parallel programming, beause di erent parts of the sear h tree are often ompletely independent of ea h other; thus disjoint subtrees an be explored on di erent ma hines, with a minimum of interpro ess ommuni ation. Already in 1964, D. H. Lehmer explained how to subdivide a problem so that two omputers of di erent speeds ould work on it simultaneously and nish at the same time. The problem that he onsidered had a sear h tree of known shape (see Theorem 7.2.1.3L); but we an do essentially similar load balan ing even in mu h more ompli ated situations, by using Monte Carlo estimates of the subtree sizes. Although many ideas for parallelizing ombinatorial sear hes have been developed over the years, su h te hniques are beyond the s ope of this book. Readers an nd a ni e introdu tion to a fairly general approa h in the paper by R. Finkel and U. Manber, ACM Transa tions on Programming Languages and Systems 9 (1987), 235{256. M. Alekhnovi h, A. Borodin, J. Buresh-Oppenheim, R. Impagliazzo, A. Magen, and T. Pitassi have de ned priority bran hing trees, a general model of omputation with whi h they were able to prove rigorous bounds on what ba ktra k programs an do, in Computational Complexity 20 (2011), 679{740. November 12, 2016 Walker Golomb Baumert ommafree odes optimization lookahead dynami ordering minimum remaining values Wells Bitner Reingold Gas hnig ba kmarking ba kjumping Monte Carlo estimates Hall Knuth importan e sampling Hammersley Hands omb self-avoiding walks king paths Rosenbluth Rosenbluth parallel programming sear h tree Lehmer load balan ing Finkel Manber Alekhnovi h Borodin Buresh-Oppenheim Impagliazzo Magen Pitassi priority bran hing trees 7.2.2 27 BACKTRACK PROGRAMMING EXERCISES [22 ℄ Explain how the tasks of generating (i) n-tuples, (ii) permutations of distin t items, (iii) ombinations, (iv) integer partitions, (v) set partitions, and (vi) nested parentheses an all be regarded as spe ial ases of ba ktra k programming, by presenting suitable domains D and uto properties P (x1; : : : ; x ) that satisfy (1) and (2). 2. [10 ℄ True or false: We an hoose D1 so that P1 (x1 ) is always true. 3. [16 ℄ Using a hessboard and eight oins to represent queens, one an follow the steps of Algorithm B and essentially traverse the tree of Fig. 68 by hand in about three hours. Invent a tri k to save half of the work. x 4. [20 ℄ Reformulate Algorithm B as a re ursive pro edure alled try (l), having global variables n and x1 : : : x , to be invoked by saying `try (1)'. Can you imagine why the author of this book de ided not to present the algorithm in su h a re ursive form? 5. [20 ℄ Given r , with 1 r n, in how many ways an 7 nonatta king queens be pla ed on an 8 8 hessboard, if no queen is pla ed in row r? 6. [20 ℄ (T. B. Sprague, 1890.) Are there any values n > 5 for whi h the n queens problem has a \framed" solution with x1 = 2, x2 = n, x 1 = 1, and x = n 1? 7. [20 ℄ Are there two 8-queen pla ements with the same x1 x2 x3 x4 x5 x6 ? 8. [21 ℄ Can a 4m-queen pla ement have 3m queens on \white" squares? x 9. [22 ℄ Adapt Algorithm W to the n queens problem, using bitwise operations on n-bit numbers as suggested in the text. 10. [M25 ℄ (W. Ahrens, 1910.) Both solutions of the n queens probQ Q Q Q lem when n = 4 have hiral symmetry : Rotation by 90Æ leaves them Q un hanged, but re e tion doesn't. Q a) Can the n queens problem have a solution with re e tion symmetry? Q Q b) Show that hiral symmetry is impossible when n mod 4 2 f2; 3g. Q QQ ) Sometimes the solution to an n queens problem ontains four queens Q that form the orners of a tilted square, as shown here. Prove that we Q Q an always get another solution by tilting the square the other way (but Q Q leaving the other n 4 queens in pla e). Q QQ d) Let C be the number of hirally symmetri solutions, and suppose Q Q Q of them have x > k for 1 k n=2. Prove that C = 2b 4 . 11. [M28 ℄ (Wraparound queens.) Repla e (3) by the stronger onditions `x 6= x , (x x ) mod n 6= k j , (x x ) mod n 6= k j '. (The n n grid be omes a torus.) Prove that the resulting problem is solvable if and only if n is not divisible by 2 or 3. 12. [M30 ℄ For whi h n 0 does the n queens problem have at least one solution? 13. [M25 ℄ If exer ise 11 has T (n) toroidal solutions, show that Q(mn) Q(m) T (n). 14. [HM47 ℄ Does (ln Q(n))=(n ln n) approa h a positive onstant as n ! 1? 15. [21 ℄ Let H (n) be the number of ways that n queen bees an o upy Q Q an n n honey omb so that no two are in the same line. (For example, Q one of the H (4) = 7 ways is shown here.) Compute H (n) for small n. Q 16. [15 ℄ J. H. Qui k (a student) noti ed that the loop in step L2 of Algorithm L an be hanged from `while x < 0' to `while x 6= 0', be ause x annot be positive at that point of the algorithm. So he de ided to eliminate the minus signs and just set x + +1 k in step L3. Was it a good idea? x 1. k l l n n n n n n k n= n j j k j k k n l l k November 12, 2016 l l n-tuples tuples permutations ombinations integer partitions partitions set partitions nested parentheses parentheses domains uto properties re ursive global variables author re ursion versus iteration Sprague bitwise operations Ahrens hiral symmetry Rotation by 90Æ Wraparound queens broken diagonal, see wraparound torus n queens problem queen bees bees honey omb hexagons Qui k Langford pairs+ 28 7.2.2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 17. [17 ℄ Suppose that n = 4 and Algorithm L has rea hed step L2 with l = 4 and x x x = 241. What are the urrent values of x x x x x , p p p p p , and y y y ? 19. [M19 ℄ What are the domains D in Langford's problem (7)? x 20. [21 ℄ Extend Algorithm L so that it for es x k whenever k 2= fx ; : : : ; x g. x 21. [M25 ℄ If x = x x : : : x , let x = ( x ) : : : ( x )( x ) = x be its dual. a) Show that if n is odd and x solves Langford's problem (7), we have x = n for some k bn=2 if and only if x = n for some k > bn=2 . b) Find a similar rule that distinguishes x from x when n is even. ) Consequently the algorithm of exer ise 20 an be modi ed so that exa tly one of ea h dual pair of solutions fx; x g is visited. 22. [M26 ℄ Explore \loose Langford pairs": Repla e `j + k + 1' in (7) by `j + b3k=2 '. 1 2 3 4 5 6 7 8 0 1 2 3 4 1 2 3 l 1 l 1 2 2n D 2n 2 l 1 R 1 k D k D D 23. [17 ℄ We an often obtain one word re tangle from another by hanging only a letter or two. Can you think of any 5 6 re tangles that almost mat h (10)? 24. [20 ℄ Customize Algorithm B so that it will nd all 5 6 word re tangles. x 25. [25 ℄ Explain how to use orthogonal lists, as in Fig. 13 of Se tion 2.2.6, so that it's easy to visit all 5-letter words whose kth hara ter is , given 1 k 5 and a z. Use those sublists to speed up the algorithm of exer ise 24. 26. [21 ℄ Can you nd ni e word re tangles of sizes 5 7, 5 8, 5 9, 5 10? 27. [22 ℄ What pro le and average node osts repla e (13) and (14) when we ask the algorithm of exer ise 25 for 6 5 word re tangles instead of 5 6? x 28. [23 ℄ The method of exer ises 24 and 25 does n levels of ba ktra king to ll the ells of an m n re tangle one olumn at a time, using a trie to dete t illegal pre xes in the rows. Devise a method that does mn levels of ba ktra king and lls just one ell per level, using tries for both rows and olumns. 29. [15 ℄ What's the largest ommafree subset of the following words? a ed babe bade bead beef afe ede dada dead deaf fa e fade feed x 30. [22 ℄ Let w1 , w2 , : : : , w be four-letter words on an m-letter alphabet. Design an algorithm that a epts or reje ts ea h w , a ording as w is ommafree or not with respe t to the a epted words of fw1 ; : : : ; w 1 g. 31. [M22 ℄ A two-letter blo k ode on an m-letter alphabet an be represented as a digraph D on m verti es, with a ! b if and only if ab is a odeword. a) Prove that the ode is ommafree () D has no oriented paths of length 3. b) How many ar s an be in a digraph with no oriented paths of length r? x 32. [M30 ℄ (W. L. Eastman, 1965.) The following elegant onstru tion yields a ommafree ode of maximum size for any odd blo k length n, over any alphabet. Given a sequen e of x = x0 x1 : : : x 1 of nonnegative integers, where x di ers from ea h of its other y li shifts x : : : x 1 x0 : : : x 1 for 0 < k < n, the pro edure outputs a y li shift x with the property that the set of all su h x is ommafree. We regard x as an in nite periodi sequen e hx i with x = x for all k n. Ea h y li shift then has the form x x +1 : : : x + 1 . The simplest nontrivial example o urs when n = 3, where x = x0 x1 x2 x0 x1 x2 x0 : : : and we don't have x0 = x1 = x2 . In this ase the algorithm outputs x x +1x +2 where x > x +1 x +2 ; and the set of all su h triples learly satis es the ommafree ondition. m j j j n k n k n k k November 12, 2016 k k k k k k n n k k k domains dual loose Langford pairs word re tangle orthogonal lists trie ommafree two-letter blo k ode digraph ommafree Eastman aperiodi words periodi sequen e 7.2.2 BACKTRACK PROGRAMMING 29 One key idea is to think of x as partitioned into t substrings by boundary markers b , where 0 b0 < b1 < < b 1 < n and b = b + n for j t. Then substring y is x j x j +1 : : : x j+1 1 . The number t of substrings is always odd. Initially t = n and b = j for all j ; ultimately t = 1, and x = y0 is the desired output. Eastman's algorithm is based on omparison of adja ent substrings y 1 and y . If those substrings have the same length, we use lexi ographi omparison; otherwise we de lare that the longer substring is bigger. The se ond key idea is the notion of \dips," whi h are substrings of the form z = z1 : : : z where k 2 and z1 z 1 < z . It's easy to see that any string y = y0 y1 : : : in whi h we have y < y +1 for in nitely many i an be fa tored into a sequen e of dips, y = z(0) z(1) : : : , and this fa torization is unique. For example, 3141592653589793238462643383 : : : = 314 15 926 535 89 79 323 846 26 4338 3 : : : : Furthermore, if y is a periodi sequen e, its fa torization into dips is also ultimately periodi , although some of the initial fa tors may not o ur in the period. For example, 123443550123443550123443550 : : : = 12 34 435 501 23 4435 501 23 4435 : : : : Given a periodi , non onstant sequen e y des ribed by boundary markers as above, where the period length t is odd, its periodi fa torization will ontain an odd number of odd-length dips. Ea h 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. a) Play through the algorithm by hand when n = 19 and x = 3141592653589793238. b) Show that the number of rounds is at most blog3 n . ) Exhibit a binary x that a hieves this worst- ase bound when n = 3 . d) Implement the algorithm with full details. (It's surprisingly short!) e) Explain why the algorithm yields a ommafree ode. 33. [HM28 ℄ What is the probability that Eastman's algorithm nishes in one round? (Assume that x is a random m-ary string of odd length n > 1, unequal to any of its other y li shifts. Use a generating fun tion to express the answer.) 4 34. [18 ℄ Why an't a ommafree ode of length (m m2 )=4 ontain 0001 and 2000? x 35. [15 ℄ Why do you think sequential data stru tures su h as (16){(23) weren't featured in Se tion 2.2.2 of this series of books (entitled \Sequential Allo ation")? 36. [17 ℄ What's the signi an e of (a) MEM[40d℄ =5e and (b) MEM[904℄ =84 in Table 1? 37. [18 ℄ Why is (a) MEM[f8℄ = e7 and (b) MEM[a0d℄ = ba in Table 2? 38. [20 ℄ Suppose you're using the undoing s heme (26) and the operation +1 has just bumped the urrent stamp to zero. What should you do? x 39. [25 ℄ Spell out the low-level implementation details of the andidate sele tion pro ess in step C2 of Algorithm C. Use the routine store(a; v) of (26) whenever hanging the ontents of MEM, and use the following sele tion strategy: a) Find a lass with the least number r of blue words. b) If r = 0, set x 1; otherwise set x to a word in lass . ) If r > 1, use the poison list to nd an x that maximizes the number of blue words that ould be killed on the other side of the pre x or suÆx list that ontains x. x 40. [28 ℄ Continuing exer ise 39, spell out the details of step C3 when x 0. a) What updates should be done to MEM when a blue word x be omes red? b) What updates should be done to MEM when a blue word x be omes green? j t j b b j j t b j j k k i k i e November 12, 2016 j substrings boundary markers lexi ographi omparison dips pi, as \random" data worst- ase bound analysis of algs Eastman generating fun tion analysis of algs data stru tures Sequential Allo ation undoing s heme bumped stamp poison list 30 7.2.2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) ) Step C3 begins its job by making x green as in part (b). Explain how it should nish its job by updating the poison list. 42. [M30 ℄ Is there a binary (m = 2) ommafree ode with one odeword in ea h of P the ( n (d)2 )=n y le lasses, for every word length n? 44. [HM29 ℄ A ommafree ode on m letters is equivalent to 2m! su h odes if we permute the letters and/or repla e ea h odeword by its left-right re e tion. Determine all of the nonisomorphi ommafree odes of length 4 on m letters when m is (a) 2 (b) 3 ( ) 4 and there are (a) 3 (b) 18 ( ) 57 odewords. 45. [M42 ℄ Find a maximum-size ommafree ode of length 4 on m = 5 letters. 47. [20 ℄ Explain how the hoi es in Fig. 69 were determined from the \random" bits that are displayed. For instan e, why was X2 set to 1 in Fig. 69(b)? 48. [M15 ℄ Interpret the value E(D1 : : : D ), in the text's Monte Carlo algorithm. 49. [M22 ℄ What's a simple martingale that orresponds to Theorem E? x 50. [HM25 ℄ Elmo uses Algorithm E with D = f1; : : : ; ng, P = [ x1 > >x ℄, = 1. a) Ali e ips n oins independently, where oin k yields \heads" with probability 1=k. True or false: She obtains exa tly l heads with probability =n!. b) Let Y1 , Y2 , : : : , Y be the numbers on the oins that ome up heads. (Thus Y1 = 1, and Y2 = 2 with probability 1=2.) Show that Pr(Ali e obtains Y1 , Y2 , : : : , Y ) = Pr(Elmo obtains X1 = Y , X2 = Y 1 , : : : , X = Y1 ). ) Prove that Ali e q.s. obtains at most (ln n)(ln ln n) heads. d) Consequently Elmo's S is q.s. less than exp((ln n)2 (ln ln n)). x 51. [M30 ℄ Extend Algorithm B so that it also omputes the minimum, maximum, mean, and varian e of the Monte Carlo estimates S produ ed by Algorithm E. 52. [M21 ℄ Instead of hoosing ea h y in step E5 with probability 1=d, we ould use a biased distribution where Pr(I = i j X1 ; : : : ; X 1 ) = p 1 l 1 (y ) > 0. How should the estimate S be modi ed so that its expe ted value in this general s heme is still C ()? 53. [M20 ℄ If all osts (x1 ; : : : ; x ) are positive, show that the biased probabilities of exer ise 52 an be hosen in su h a way that the estimate S is always exa t. x 55. [M25 ℄ The ommafree ode sear h pro edure in Algorithm C doesn't a tually t the mold of Algorithm E, be ause it in orporates lookahead, dynami ordering, reversible memory, and other enhan ements to the basi ba ktra k paradigms. How ould its running time be reliably estimated with Monte Carlo methods? 57. [M20 ℄ Algorithm E an potentially follow M di erent paths X1 : : : X 1 before it terminates, where M is the number of leaves of the ba ktra k tree. Suppose the nal values of D at those leaves are D(1) , : : : , D( ) . Prove that (D(1) : : : D( ) )1 M . 58. [27 ℄ The text's king path problem is a spe ial ase of the general problem of ounting simple paths from vertex s to vertex t in a given graph. We an generate su h paths by random walks from s that don't get stu k, if we maintain a table of values DIST(v) for all verti es v not yet in the path, representing the shortest distan e from v to t through unused verti es. For with su h a table we an simply move at ea h step to a vertex for whi h DIST(v) < 1. Devise a way to update the DIST table dynami ally without unne essary work. 59. [26 ℄ A ZDD with 3,174,197 nodes an be onstru ted for the family of all simple orner-to- orner king paths on a hessboard, using the method of exer ise 7.1.4{225. Explain how to use this ZDD to ompute (a) the total length of all paths; (b) the number of paths that tou h any given subset of the enter and/or orner points. n=d d n l k l l n l l l l l l i l X :::X i l l M November 12, 2016 M =M nonisomorphi symmetries \random" bits Monte Carlo martingale oins Stirling y le numbers q.s. varian e biased distribution ommafree ode lookahead dynami ordering reversible memory simple paths random walks dynami shortest distan es shortest distan es, dynami ZDD orner-to- orner king paths hessboard 7.2.2 BACKTRACK PROGRAMMING 31 x 60. [20 ℄ Experiment with biased random walks (see exer ise 52), weighting ea h non- dead-end king move to a new vertex v by 1 + DIST(v)2 instead of hoosing every su h move with the same probability. Does this strategy improve on Fig. 70? 61. [HM26 ℄ Let P be the number of integer sequen es x1 : : : x su h that x1 = 1 and 1 x +1 2x for 1 k < n. (The rst few values are 1, 2, 6, 26, 166, 1626, : : : ; this sequen e was introdu ed by A. Cayley in Philosophi al Magazine (4) 13 (1857), 245{248, who showed that P enumerates the partitions of 2 1 into powers of 2.) a) Show that P is the number of di erent pro les that are possible for a binary tree of height n. b) Find an eÆ ient way to ompute P for large n. Hint: Consider the more general sequen e P ( ) , de ned similarly but with x1 = m. n ) Use the estimation pro edure of Theorem E to prove that P 2( 2 )=(n 1)!. x 66. [22 ℄ When the fa es of four ubes are olored randomly with four olors, estimate the probability that the orresponding \Instant Insanity" puzzle has a unique solution. How many 2-regular graphs tend to appear during the \fa tored" solution pro ess? 67. [20 ℄ Find ve ubes, ea h of whose fa es has one of ve olors, and where every olor o urs at least ve times, su h that the orresponding puzzle has a unique solution. 70. [24 ℄ Assemble ve ubes with upper ase letters on ea h fa e, using the patterns n k n k n n n n m n n POE SGS ART DTE UYL Z Z H HIZ U C ZOH U W R G 7 5 7 1 2 4 5 5 3 1 4 5 5 3 1 1 3 2 3 5 3 4 7 3 5 4 4 5 4 3 7 3 5 1 2 3 5 5 3 7 2 7 4 2 2 5 8 1 5 4 4 3 1 5 The Fool's Disk 5 3 2 1 3 4 3 2 4 2 4 2 4 By extending the prin iples of Instant Insanity, show that these ubes an be pla ed in a row so that four 5-letter words are visible. (Ea h word's letters should have a onsistent orientation. The letters C and U , H and I , N and Z are related by 90Æ rotation.) x 73. [23 ℄ (The Fool's Disk.) \Rotate the four disks of the lefthand illustration below so that the four numbers on ea h ray sum to 12." (The urrent sums are 4+3+2+4 = 13, et .) Show that this problem fa tors ni ely, so that it an be solved readily by hand. 5 The Royal Aquarium Thirteen Puzzle x 75. [26 ℄ (The Royal Aquarium Thirteen Puzzle.) \Rearrange the nine ards of the righthand illustration above, optionally rotating some of them by 180Æ , so that the six horizontal sums of gray letters and the six verti al sums of bla k letters all equal 13." (The urrent sums are 1 + 5 + 4 = 10, : : : , 7 + 5 + 7 = 19.) The author of Ho mann's Puzzles Old and New (1893) stated that \There is no royal road to the solution. The proper order must be arrived at by su essive transpositions until the onditions are ful lled." Prove that he was wrong: \Fa tor" this problem and solve it by hand. November 12, 2016 biased random walks Cayley binary partitions partitions pro les height n Instant Insanity fa tored speedy s hizophrenia ubes upper ase letters alphabet Instant Insanity 5-letter words Fool's Disk Royal Aquarium Thirteen Puzzle Le Nombre Treize, see Royal Aquarium Thir Ho mann Fa tor 32 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) 7.2.2 Table 666 TWENTY QUESTIONS (SEE EXERCISE 90) 1. The rst 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 onse utive questions with identi al 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 ommon answer is: (A) A (B) B (C) C (D) D (E) E 9. The sum of all question numbers whose answers are orre t and the same as this one is: (A) 2 [59 : : 62℄ (B) 2 [52 : : 55℄ (C) 2 [44 : : 49℄ (D) 2 [61 : : 67℄ (E) 2 [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 exa tly 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) f7g (B) f9g (C) not f11g (D) f13g (E) f15g 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) orre t 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 s ore that an be a hieved on this test is: (A) 18 (B) 19 (C) 20 (D) indeterminate (E) a hievable only by getting this question wrong x 90. [M29 ℄ (Donald R. Woods, 2000.) Find all ways to maximize the number of orre t answers to the questionnaire in Table 666. Ea h question must be answered with a letter from A to E. Hint: Begin by larifying the exa t meaning of this exer ise. What answers are best for the following two-question, two-letter \warmup problem"? 1. 2. (A) Answer 2 is B. (A) Answer 1 is orre t. (B) Answer 1 is A. (B) Either answer 2 is wrong or answer 1 is A, but not both. 91. [HM28 ℄ Show that exer ise 90 has a surprising, somewhat paradoxi al answer if two hanges are made to Table 666: 9(E) be omes `2 [39 : : 43℄'; 15(C) be omes `f11g'. November 12, 2016 Woods questionnaire paradoxi al 7.2.2 33 BACKTRACK PROGRAMMING x 95. [30 ℄ (A .) The letters of 29 ve-letter words lueless ana rosti ; 6 7 8 9 10 ; 11 12 13 14 15 ; 16 17 18 19 20 ; : : : ; 141142143144145 ; ve-letter words 1 2 3 4 5 WORDS(n) mystery text all belonging to WORDS(1000), have been shued to form the following mystery text: English words fa torization 30 29 9 140 12 13 145 90 45 99 26 107 47 84 53 51 27 133 39 137139 66 112 69 14 8 20 91 129 70 Conne ted subsets anoni al grid 16 7 93 19 85 101 76 78 44 10 106 60 118119 24 25 100 1 5 64 11 71 42 122123 pentominoes : ::: spanning tree 103104 63 49 31 121 98 79 80 46 48 134135131 143 96 142120 50 132 33 43 34 40 lexi ographi ally smallest SGB format ( 111 97 113105 38 102 62 65 114 74 82 81 83 136 37 21 61 88 86 55 32 35 117116 23 52 ARCS lueless ana rosti 56 17 18 94 67 128 15 57 58 89 87 109 2 4 6 28 95 3 126 77 144 54 41 75 138 73 124 36 130127141 22 92 72 59 108125110 : ) 68 115 TIP NEXT polyominoes Furthermore, their initial letters 1 , 6 , 11 , 16 , : : : , 141 identify the sour e of that quotation, whi h onsists entirely of ommon English words. What does it say? 96. [21 ℄ The fteenth mystery word in exer ise 95 is ` '. Why does its spe ial 134135131 form lead to a partial fa torization of that problem? x 100. [30 ℄ (Conne ted subsets.) Let v be a vertex of some graph G, and let H be a onne ted subset of G that ontains v. The verti es of H an be listed in a anoni al way by starting with v0 v and then letting v1 , v2 , : : : be the neighbors of v0 that lie in H, followed by the neighbors of v1 that haven't yet been listed, and so on. (We assume that the neighbors of ea h vertex are listed in some xed order.) For example, if G is the 3 3 grid P3 P3 , exa tly 21 of its onne ted ve-element subsets ontain the upper left orner element v. Their anoni al orderings are 0 1 2 4 3 0 1 2 3 4 0 1 3 2 0 1 2 3 0 1 0 1 4 2 3 2 3 1 0 1 4 0 1 0 2 4 0 1 0 2 3 2 4 3 4 1 0 1 4 1 2 0 1 2 0 3 3 2 4 4 4 0 3 4 0 1 2 3 4 1 0 2 3 1 2 3 2 0 3 2 4 4 0 3 1 4 2 3 0 2 4 3 1 4 1 3 4 3 4 0 2 3 1 4 2 if we order the verti es from top to bottom and left to right when listing a vertex's neighbors. (Verti es labeled 0 , 1 , 2 , 3 , 4 indi ate v0 , v1 , v2 , v3 , v4 . Other verti es are not in H.) Noti e that in ea h ase the numbered verti es are impli itly onne ted by a spanning tree whose edges ea h have the form v v for some i < j . The anoni al ordering orresponds to H 's lexi ographi ally smallest spanning tree; for example, the spanning tree in the rst solution is v0 v1 , v0 v2 , v1 v3 , v1 v4 . Furthermore, the 21 solutions appear here in lexi ographi order of their respe tive spanning trees. Design a ba ktra k algorithm to generate all of the n-element onne ted subsets that ontain a spe i ed vertex v, given a graph that is represented in SGB format (whi h has ARCS, TIP, and NEXT elds, as des ribed near the beginning of Chapter 7). 101. [23 ℄ Use the algorithm of exer ise 100 to generate all of the onne ted n-element subsets of a given graph G. How many su h subsets does P P have, for 1 n 9? i j n November 12, 2016 n 34 7.2.2 COMBINATORIAL SEARCHING (F5B: 12 Nov 2016 1636) [M22 ℄ A v-rea hable subset of a dire ted graph G is a nonempty set of verti es H with the property that every u 2 H an be rea hed from v by at least one oriented path in G j H . (In parti ular, v itself must be in H .) a) The digraph P3! P3! is like P3 P3 , ex ept that all ar s between verti es are dire ted downward or to the right. Whi h of the 21 onne ted subsets in exer ise 100 are also v-rea hable from the upper left orner element v of P3! P3!? b) True or false: H is v-rea hable if and only if G j H ontains a dual oriented spanning tree rooted at v. (An oriented tree has ar s u ! p , where p is the parent of the nonroot node u; in a dual oriented tree, the ar s are reversed: p ! u.) ) True or false: If G is undire ted, so that w ! u whenever u ! w, its v-rea hable subsets are the same as the onne ted subsets that ontain v. d) Modify the algorithm of exer ise 100 so that it generates all of the n-element v-rea hable subsets of a digraph G, given n, v, and G. 999. [M00 ℄ this is a temporary exer ise (for dummies) 102. u u u November 12, 2016 v-rea hable subset rea hable subsets grid, oriented oriented grid dual oriented spanning tree oriented tree parent dire ted graph versus undire ted undire ted graph versus dire ted 7.2.2 35 ANSWERS TO EXERCISES SECTION 7.2.2 1. Although many formulations are possible, the following may be the ni est: (i) D is arbitrary (but hopefully nite), and P is always true. (ii) D = f1; 2; : : : ; ng and P = `x 6= x for 1 j < k l'. (iii) For ombinations of n things from N , D = f1; : : : ; N + 1 kg and P = `x1 > > x '. (iv) D = f0; 1; : : : ; bn=k g; P = `x1 x and n (n l ) x x1 + + x n'. (v) For restri ted growth strings, D = f0; : : : ; k 1g and P = `x +1 1+max(x1 ; : : : ; x ) for 1 j < l'. (vi) For indi es of left parentheses (see 7.2.1.6{(8)), D = f1; : : : ; 2k 1g and P = `x1 < < x '. 2. True. (If not, set D1 D1 \ fx j P1 (x)g.) 3. We an restri t D1 to f1; 2; 3; 4g, be ause the re e tion (9 x1 ) : : : (9 x8 ) of every solution x1 : : : x8 is also a solution. (H. C. S huma her made this observation in a letter to Gauss, 24 September 1850.) Noti e that Fig. 68 is left-right symmetri . 4. try (l) = \If l > n, visit x1 : : : x . Otherwise, for x min D , min D + 1, : : : , max D , if P (x1 ; : : : ; x ) all try (l + 1)." This formulation is elegant, and ne for simple problems. But it doesn't give any lue about why the method is alled \ba ktra k"! Nor does it yield eÆ ient ode for important problems whose inner loop is performed billions of times. We will see that the key to eÆ ient ba ktra king is to provide good ways to update and downdate the data stru tures that speed up the testing of property P . The overhead of re ursion an get in the way, and the a tual iterative stru ture of Algorithm B isn't diÆ ult to grasp. 5. Ex luding ases with j = r or k = r from (3) yields respe tively (312, 396, 430, 458, 458, 430, 396, 312) solutions. (With olumn r also omitted there are just (40, 46, 42, 80, 80, 42, 46, 40).) 6. Yes, almost surely for all n > 16. One su h is x1 x2 : : : x17 = 2 17 12 10 7 14 3 5 9 13 15 4 11 8 6 1 16. [See Pro . Edinburgh Math. So . 8 (1890), 43 and Fig. 52.℄ Preuer and Engelhardt found 34,651,355,392 solutions when n = 27. 7. Yes: (42736815; 42736851); also therefore (57263148; 57263184). 8. Yes, at least when m = 4; e.g., x1 : : : x16 = 5 8 13 16 3 7 15 11 6 2 10 14 1 4 9 12. There are no solutions when m = 5, but 7 10 13 20 17 24 3 6 23 11 16 21 4 9 14 2 19 22 1 8 5 12 15 18 works for m = 6. (Are there solutions for all even m 4? C. F. de Jaenis h, Traite des appli ations de l'analyse mathematique au jeu des e he s 2 (1862), 132{133, noted that all 8-queen solutions have four of ea h olor. He proved P that the number of white queens must be even, be ause 4 =1 (x + k) is even.) 9. Let bit ve tors a , b , represent the \useful" elements of the sets in (6), with a = P f2 1 j x 2 A g, b = Pf2 1 j x 2 B \ [1 : : n℄g, = Pf2 1 j x 2 C \ [1 : : n℄g. Then step W2 sets s & a & b & , where is the mask 2 1. In step W3 we an set t s & ( s ), a a 1 + t, b (b 1 + t) 1, (( 1 + t) 1)& ; and it's also onvenient to set s s t at this time, instead of deferring this hange to step W4. (There's no need to store x in memory, or even to ompute x in step W3 as an integer in [1 : : n℄, be ause x an be dedu ed from a a 1 when a solution is found.) 10. (a) Only when n = 1, be ause re e ted queens an apture ea h other. (b) Queens not in the enter must appear in groups of four. ( ) The four queens o upy the same rows, olumns, and diagonals in both ases. (d) In ea h solution ounted by we an independently tilt (or not) ea h of the bn=4 groups of four. [Mathematis he Unterhaltungen und Spiele 1, se ond edition (Leipzig: Teubner, 1910), 249{258.℄ k l j l k k k l l l l k k j l l l j k l n l l l l l l l l m k k l x l l l l x l l l l l l l l l l l l l l l l l n November 12, 2016 l n l l l x l l restri ted growth strings re e tion S huma her Gauss symmetri inner loop ba ktra king, eÆ ient iteration versus re ursion Preuer Engelhardt de Jaenis h mask 36 7.2.2 ANSWERS TO EXERCISES Pn Suppose the x are distin t. Then =1 (x + k) = 2P +1 0 (modulo n). If the 2 numbers (x + k) mod n are also distin t, we have also =1 k +1 . But that is 2 impossible when n is even. Now P suppose further Pthat the numbers P(x 2 k) mod n are distin t. Then we 2 have =1P(x + k)2 =1 ( x k ) =1 k = n(n + 1)(2n + 1)=6. And we P also have =1 (x + k)2 + =1 (x k)2 = 4n(n + 1)(2n + 1)=6 2n=3, whi h is impossible when n is a multiple of 3. [See W. Ahrens, Mathematis he Unterhaltungen und Spiele 2, se ond edition (1918), 364{366, where G. Polya ites a more general result of A. Hurwitz that applies to wraparound diagonals of other slopes.℄ Conversely, if n isn't divisible by 2 or 3, we an let x = n and x = (2k) mod n for 1 k < n. (The rule x = (3k) mod n also works. See E douard Lu as, Re reations Mathematiques 1 (1882), 84{86.) 12. The (n + 1) queens problem learly has a solution with a queen in a orner if and only if the n queens problem has a solution with a queen-free main diagonal. Hen e by the previous answer there's always a solution when n mod 6 2 f0; 1; 4; 5g. Another ni e solution was found by J. Franel [L'Intermediaire des Mathemati iens 1 (1894), 140{141℄ when n mod 6 2 f2; 4g: Let x = (n=2 + 2k 3[2k n ℄) mod n + 1, for 1 k n. With this setup we nd that x x = (k j ) and 1 j < k n implies (1 or 3)(k j ) + (0 or 3) 0 (modulo n); hen e k j = n (1 or 3). But the values of x1 , x2 , x3 , x 2 , x 1 , x give no atta king queens ex ept when n = 2. Franel's solution has empty diagonals, so it provides solutions also for n mod 6 2 f3; 5g. We on lude that only n = 2 and n = 3 are impossible. [A more ompli ated onstru tion for all n > 3 had been given earlier by E. Pauls, in Deuts he S ha hzeitung 29 (1874), 129{134, 257{267. Pauls also explained how to nd all solutions, in prin iple, by building the tree level by level (not ba ktra king).℄ ( ) ( ) be a solution for m queens, and let y1 : : : y be a 13. For 1 j n, let x1 : : : x solution for n toroidal queens. Then X( 1) + = (x( ) 1) n + y (for 1 i m and 1 j n) is a solution for mn queens. [I. Rivin, I. Vardi, and P. Zimmermann, AMM 101 (1994), 629{639, Theorem 2.℄ 14. [Rivin, Vardi, and Zimmermann, in the paper just ited, observe that in fa t the sequen e (ln Q(n))=(n ln n) appears to be in reasing.℄ 15. Let the queen in row k be in ell k . Then we have a \relaxation" of the n queens problem, with jx x j be oming just x x in (3); so we an ignore the b ve tor in Algorithm B* or in exer ise 9. We get n = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 H (n) = 1 1 1 3 7 23 83 405 2113 12657 82297 596483 4698655 40071743 367854835 [N. J. Cavenagh and I. M. Wanless, Dis r. Appl. Math. 158 (2010), 136{146, Table 2.℄ 16. It fails spe ta ularly in step L5. The minus signs, whi h mark de isions that were previously for ed, are ru ial tags for ba ktra king. 17. x4 : : : x8 = 21040, p0 : : : p4 = 33300, and y1 y2 y3 = 130. (If x 0 the algorithm will never look at y ; hen e the urrent state of y4 : : : y8 is irrelevant. But y4 y5 happens to be 20, be ause of past history; y6 , y7 , and y8 haven't yet been tou hed.) 19. We ould say D is f n; : : : ; 2; 1; 1; 2; : : : ; ng, or fk j k 6= 0 and 2 lk 2n l 1g, or anything in between. (But this observation isn't very useful.) 11. k n k k n k n k n k n k n k n k k n k k k k k n k k k k n n j j n j m n j i j k k n j i j j i i l November 12, 2016 Ahrens Polya Hurwitz Lu as Franel Pauls tree breadth rst sear h Rivin Vardi Zimmermann semi-queens Cavenagh Wanless 7.2.2 37 ANSWERS TO EXERCISES 20. First we add a Boolean array a1 : : : a , where a means \k has appeared," as in Algorithm B*. It's 0 : : : 0 in step L1; we set a 1 in step L3, a 0 in step L5. The loop in step L2 be omes \while x < 0, go to L5 if l n 1 and a2 1 = 0, otherwise set l l + 1." After nding l + k + 1 2n in L3, and before testing x + +1 for 0, insert this: \If l n 1 and a2 1 = 0, while l + k +1 6= 2n set j k, k p ." 21. (a) In any solution x = n () x + +1 = n () x = n. (b) x = n 1 for some k n=2 if and only if x = n 1 for some k > n=2. ( ) Let n0 = n [n is even℄. Change `l n 1 and a2 1 = 0' in the modi ed step L2 to `(l = bn=2 and a 0 = 0) or (l n 1 and a2 1 = 0)'. Insert the following before the other insertion into step L3: \If l = bn=2 and a 0 = 0, while k 6= n0 set j k, k p ." And in step L5 | this subtle detail is needed when n is even | go to L5 instead of L4 if l = bn=2 and k = n0 . 22. The solutions 1 1 and 2112 for n = 1 and n = 2 are self-dual; the solutions for n = 4 and n = 5 are 43112342, 2452311435, 4511234253, and their duals. The total number of solutions for n = 1, 2, : : : is 1, 1, 0, 2, 4, 20, 0, 156, 516, 2008, 0, 52536, 297800, 1767792, 0, 75678864, : : : ; there are none when n mod 4 = 3, by a parity argument. Algorithm L needs only obvious hanges. To ompute solutions by a streamlined method like exer ise 21, use n0 = n (0; 1; 2; 0) and substitute `l = bn=4 + (0; 1; 2; 1)' for `l = bn=2 ', when n mod 4 = (0; 1; 2; 3); also repla e `l n 1 and a2 1 = 0' by `l dn=2e and ab(4 +2 2 ) 3 = 0'. The ase n = 15 is proved impossible with 397 million nodes and 9.93 gigamems. 23. slums ! sluff , slump, slurs, slurp , or sluts; (slums; total) ! (slams; tonal). 24. Build the list of 5-letter words and the trie of 6-letter words in step B1; also set a01 a02 a03 a04 a05 00000. Use min D = 1 in step B2 and max D = 5757 in step B4. Testing P in step B3, if word x3 is 1 2 3 4 5 , onsists of forming a 1 : : : a 5 , where a = trie [a( 1) ; ℄ for 1 k 5; but jump to B4 if any a is zero. 25. There are 5 26 singly linked lists, a essed from pointers h , all initially zero. The xth word 1 2 3 4 5 , for 1 x 5757, belongs to 5 lists and has ve pointers l 1 l 2 l 3 l 4 l 5 . To insert it, set l h xk , h xk x, and s xk s xk + 1, for 1 k 5. (Thus s will be theP length of the list a essed from h .) We an store a \signature" 26=1 2 1 [ trie [a; ℄ 6= 0℄ with ea h node a of the trie. For example, the signature for node 260 is 20 +24 +28 +214 +217 +220 +224 = #1124111 , a ording to (11); here a $ 1, : : : , z $ 26. The pro ess of running through all x that mat h a given signature y with respe t to position z, as needed in steps B2 and B4, now takes the following form: (i) Set i 0. (ii) While 2 & y = 0, set i i + 1. (iii) Set x h ( +1) ; go to (vi) if x = 0. (iv) Visit x. (v) Set x l ; go to (iv) if x 6= 0. (vi) Set i i + 1; go to (ii) if 2 y. Let trie [a; 0℄ be the signature of node a. P We hoose z and y = trie [a( 1) ; 0℄ in step B2 so that the number of nodes to visit, 26=1 s [2 1 & y 6= 0℄, is minimum for 1 z 5. For example, when l = 3, x1 = 1446, and x2 = 185 as in (10), that sum for z = 1 is s11 +s15 +s19 +s1(15) +s1(18) +s1(21) +s1(25) = 296+129+74+108+268+75+47 = 997; and the sums for z = 2, 3, 4, 5 are 4722, 1370, 5057, and 1646. Hen e we hoose z = 1 and y = #1124111 ; only 997 words, not 5757, need be tested for x3 . The values y and z are maintained for use in ba ktra king. (In pra ti e we keep x, y, and z in registers during most of the omputation. Then we set x x, y y, z z before in reasing l l + 1 in step B3; and we set x x , y y , z z in n k k k l n l l n k l k k k D n k n D k k n n n l l n k n n l l l lk l l k k l l = l lk k x x x x x x x x x x xk k k k k k k i z i i xz l z z l l l l November 12, 2016 l l l l parity argument trie signature trie bitwise AND 38 7.2.2 ANSWERS TO EXERCISES step B5. We also keep i in a register, while traversing the sublists as above; this value is restored in step B5 by setting it to the zth letter of word x, de reased by 'a'.) 26. Here are the author's favorite 5 7 and 5 8, and the only 5 9's: grandest renoun e episodes basement eyesores smashes partial immense emerged sadness pastelist a iden e mortgagor proreform andesytes varistors agentival oelomate undeleted oysterers No 5 10 word re tangles exist, a ording to our ground rules. 27. (1, 15727, 8072679, 630967290 90962081, 625415) and (15727.0, 4321.6, 1749.7, 450.4, 286.0). Total time 18:3 teramems. (In Se tion 7.2.2.1 we'll study a method that is symmetri al between rows and olumns.) 28. Build a separate trie for the m-letter words; but instead of having trie nodes of size 26 as in (11), it's better to onvert this trie into a ompressed representation that omits the zeros. For example, the ompressed representation of the node for pre x ` orne' in (12) onsists of ve onse utively stored pairs of entries (`a', 3879), (`d', 3878), (`l', 9602), (`r', 171), (`t', 5013), followed by (0; 0). Similarly, ea h shorter pre x with des endants is represented by onse utive pairs ( hara ter, link), followed by (0; 0) to mark the end of the node. Steps B3 and B4 are now very onvenient. Level l orresponds to row i = 1 + (l 1) mod m and olumn j = 1 + b(l 1)=m . For ba ktra king we store the n-trie pointer a l l as before, together with an index x into the ompressed m-trie. This method was suggested by Bernard Gattegno in 1996 (unpublished). It nds all 5 6 word re tangles in just 400 gigamems; and its running time for \transposed" 6 5 re tangles turns out to be slightly less (380 gigamems). Noti e that only one mem is needed to a ess ea h ( hara ter, link) pair in the ompressed trie. 29. Leave out fa e and (of ourse) dada ; the remaining eleven are ne. 0 00 , s , s0 , s00 , for 0 i; j; k < m, ea h apable of storing a 30. Keep tables p , p , p ternary digit. Also keep a table x0 , x1 , : : : of tentatively a epted words. Begin with g 0. Then for ea h input w = ab d, where 0 a;b; ; d < m, set x ab d and 00 +_ 1, s 0 +_ 1, p00 s +_ 1, p also do the following: Set p p +_ 1, p0 p s0 s0 +_ 1, s00 s0000 +_ 1, where x +_ y = min(2; x + y) denotes saturating ternary addition. Then if s 0 p 0 0 0 + s0 0 0 p0 0 0 + s000 0 0 p 0 = 0 for all x = a0 b0 0 d0 , where 0 k g, set g g + 1. Otherwise reje t w and set p p 1, p0 p0 1, 0 0 00 00 00 00 s 1. p p 1, s s 1, s s 1, s 31. (a) The word b appears in message ab d if and only if a ! b, b ! , and ! d. (b) For 0 k < r, put vertex v into lass k if the longest path from v has length k. Given any su h partition, we an in lude all ar s from lass k to lass j < k without in reasing the path lengths. So it's a question of nding the maximum of P p p subje t to p0 + p1 + + p 1 = m. The values p = b(m + j )=r a hieve 0 this (see exer ise 7.2.1.4{68(a)). When r = 3 the maximum simpli es to bm2=3 . 32. (a) The fa tors of the period, 15 926 535 89 79 323 8314, begin at the respe tive boundary points 3, 5, 8, 11, 13, 15, 18 (and then 3 + 19 = 22, et .). Thus round 1 retains boundaries 5, 8, and 15. The se ond-round substrings y0 = 926, y1 = 5358979, y2 = 323831415 have di erent lengths, so lexi ographi omparison is unne essary; the answer is y2 y0 y1 = x15 : : : x33 . (b) Ea h substring onsists of at least three substrings of the previous round. l l i ;j i ij ijk i ij l ijk j g a d d b d a ab ab ab b d a b d a b k d j ab j<k<r d j k November 12, 2016 d b d a ab d ab d d d b d r a a b d j ab ab author Gattegno ompressed trie trie, ompressed saturating ternary addition 7.2.2 39 ANSWERS TO EXERCISES ( ) Let a0 = 0, b0 = 1, a +1 = a a b , b +1 = a b b ; use a or b when n = 3 . (d) We use an auxiliary subroutine `less(i)', whi h returns [ y 1 < y ℄, given i > 0: If b b 1 6= b +1 b , return [ b b 1 < b +1 b ℄. Otherwise, for j = 0, 1, : : : , while b + j < b +1 , if x i 1 + 6= x i + return [ x i 1 + < x i + ℄. Otherwise return 0. The tri ky part of the algorithm is to dis ard initial fa tors that aren't periodi . The se ret is to let i0 be the smallest index su h that y 3 y 2 < y 1 ; then we an be sure that a fa tor begins with y . O1. [Initialize.℄ Set x x for n j < 2n, b j for 0 j < 2n, and t n. 0 0. Find the smallest i > 0 su h that less(i) = 0. O2. [Begin a round.℄ Set t Then nd the smallest j i + 2 su h that less(j 1) = 1 and j t + 2. (If no su h j exists, report an error: The input x was equal to one of its y li shifts.) Set i i0 j mod t. (Now a dip of the period begins at i0 .) O3. [Find the next fa tor.℄ Find the smallest j i + 2 su h that less (j 1) = 1. If j i is even, go to O5. 0 O4. [Retain a boundary.℄ If j < t, set b 0 b ; otherwise set b0 b0 1 for 0t k > 0 and b00 b . Finally set t0 t0 + 1. O5. [Done with round?℄ If j < i0 + t, set i j and return to O3. Otherwise, if t0 = 1, terminate; x begins at item x 00 . Otherwise set t t0 , b b0 for 0 k < t, and b b + n for k t while b < n. Return to O2. (e) Say that a \superdip" is a dip of odd length followed by zero or more dips of even length. Any in nite sequen e y that begins with an odd-length dip has a unique fa torization into superdips. Those superdips an, in turn, be regarded as atomi elements of a higher-level string that an be fa tored into dips. The result x of Algorithm O is an in nite periodi sequen e that allows repeated fa torization into in nite periodi sequen es of superdips at higher and higher levels, until be oming onstant. Noti e that the rst dip of x ends at position i0 in the algorithm, be ause its length isn't 2. Therefore we an prove the ommafree property by observing that, if odeword x00 appears within the on atenation xx0 of two odewords, its superdip fa tors are also superdip fa tors of those odewords. This yields a ontradi tion if any of x, x0, or x00 is a superdip. Otherwise the same observation applies to the superdip fa tors at the next level. [Eastman's original algorithm was essentially the same, but presented in a more ompli ated way; see IEEE Trans. IT-11 (1965), 263{267. R. A. S holtz subsequently dis overed an interesting and totally di erent way to de ne the set of odewords produ ed by Algorithm O, in IEEE Trans. IT-15 (1969), 300{306.℄ 33. Let f (m) be the number of dips of length k for whi h m > z1 and z < m. The + 3 + 3 number of su h sequen es with z2 = j is (m j 1) = (kP 1) ; 2 1 1 summing for 0 j < m gives f (m) = (k 1) + 2 . Thus F (z) = =0 f (m)z = (mz 1)=(1 z) . (The fa t that f0 (m) = 1 in these formulas turns out to be useful!) Algorithm O nishes in one round if and only if some y li shift of x is a superdip. The number of aperiodi x that nish in one round is therefore n [z ℄ G (z), where (1 + mz)(1 z) (1 mz)(1 + z) : G (z) = FF (( zz)) + FF ((zz)) = (1 + mz)(1 z) + (1 mz)(1 + z) P To get the stated probability, divide by n (d)m , the number of aperiodi 3 x. (See Eq. 7.2.1.1{(60). For n = 3, 5, 7, 9 these probabilities are 1, 1, 1, and 1 3= 3 1 .) 34. If so, it ouldn't have 0011, 0110, 1100, or 1001. e e e e e e e e e i i i i i i i b i j b i j i e e i i b j b j i i i i j j n j j t j t k t k k k b k k k m k m j k k m m m m j k m k n m m k k k m k t k m m m m m n=d d n November 12, 2016 m k k superdip Eastman S holtz aperiodi 40 ANSWERS TO EXERCISES 7.2.2 35. That se tion onsidered su h representations of sta ks and queues, but not of unordered sets, be ause large blo ks of sequential memory were either nonexistent or ultra-expensive in olden days. Linked lists were the only de ent option for families of variable-size sets, be ause they ould more readily t in a limited high-speed memory. 36. (a) The blue word x with = d (namely 1101) appears in its P2 list at lo ation 5e. (b) The P3 list for words of the form 010 is empty. (Both 0100 and 0101 are red.) 37. (a) The S2 list of 0010 has be ome losed (hen e 0110 and 1110 are hidden). (b) Word 1101 moved to the former position of 1001 in its S1 list, when 1001 be ame red. (Previously 1011 had moved to the former position of 0001.) 38. In this ase, whi h of ourse happens rarely, it's safe to set all elements of STAMP to zero and set 1. (Do not be tempted to save one line of ode by setting all STAMP elements to 1 and leaving = 0. That might fail when rea hes the value 1!) 39. (a) Set r m + 1. Then for k 0, 1, : : : , f 1, set t FREE[k℄, j MEM[CLOFF + 4t + m4 ℄ (CLOFF + 4t), and if j < r set r j , t; break out of the loop if r = 0. (b) If r > 0 set x MEM[CLOFF + 4 l(ALF[x℄)℄. ( ) If r > 1 set q 0, p0 MEM[PP℄, and p POISON. While p < p0 do the following steps: Set y MEM[p℄, z MEM[p + 1℄, y0 MEM[y + m4 ℄, and z 0 4 MEM[z + m ℄. (Here y and z point to the heads of pre x or suÆx lists; y 0 and z 0 point to the tails.) If y = y0 or z = z0 , delete entry p from the poison list; this means, as in (18), to set p0 p0 2, and if p 6= p0 to store(p; MEM[p0 ℄) and store(p +1; MEM[p0 + 1℄). Otherwise set p p +2; if y0 y z0 z and y0 y > q, set q y0 y and x MEM[z℄; if y0 y < z0 z and z0 z > q, set q z0 z and x MEM[y℄. Finally, after p has be ome equal to p0 , store(PP; p0 ) and set l(ALF[x℄). (Experiments show that this \max kill" strategy for r > 1 slightly outperforms a sele tion strategy based on r alone.) 40. (a) First there's a routine rem( ;Æ; o) that removes an item from a list, following the proto ol (21): Set p Æ + o and q MEM[p + m4 ℄ 1. If q p (meaning that list p isn't losed or being killed), store(p + m4 ; q), set t MEM[ + o m4 ℄; and if t 6= q also set y MEM[q℄, store(t; y), and store(ALF[y℄ + o m4 ; t). Now, to redden x we set ALF[x℄, store( ; RED); then rem( ; p1 ( ); P1OFF), rem( ;p2 ( ); P2OFF), : : : , rem( ;s3 ( ); S3OFF), and rem( ; 4 l( ); CLOFF). (b) A simple routine lose(Æ; o) loses list Æ +o: Set p Æ +o and q MEM[p + m4 ℄; if q 6= p 1, store(p + m4 ; p 1). Now, to green x we set ALF[x℄, store( ; GREEN); then lose(p1 ( ); P1OFF), lose(p2 ( ); P2OFF), : : : , lose(s3 ( ); S3OFF), and lose(4 l( ); CLOFF). Finally, for p r < q (using the p and q that were just set within ` lose'), if MEM[r℄ 6= x redden MEM[r℄. ( ) First set p0 MEM[PP℄ +6, and store(p0 6; p1 ( )+ S1OFF), store(p0 5;s3 ( )+ P3OFF), store(p0 4; p2 ( ) + S2OFF), store(p0 3; s2 ( ) + P2OFF), store(p0 2; p3 ( ) + S3OFF), store(p0 1; s1 ( ) + P1OFF); this adds the three poison items (27). Then set p POISON and do the following while p < p0 : Set y, z, y0 , z0 as in answer 39( ), and delete poison entry p if y = y0 or z = z0 . Otherwise if y0 < y and z00 < z, go to C6 (a poisoned suÆx-pre x pair is present). Otherwise if y0 > y and 0 z > z, set p 0 p + 2. Otherwise if y < y and z0 > z, store(z + m4 ;0 z), redden MEM[ r℄ for z r < z , and delete poison entry p. Otherwise (namely if y > y and z0 < z), store(y + m4 ; y), redden MEM[r℄ for y r < y0 , and delete poison entry p. Finally, after p has be ome equal to p0 , store(PP; p0 ). November 12, 2016 sta ks queues memory onstraints, histori unordered sets Linked lists deletion 7.2.2 ANSWERS TO EXERCISES 41 42. Exer ise 32 exhibits su h odes expli itly for all odd n. The earliest papers on the subje t gave solutions for n = 2, 4, 6, 8. Yoji Niho subsequently found a ode for n = 10 but was unable to resolve the ase n = 12 [IEEE Trans. IT-19 (1973), 580{581℄. This problem an readily be en oded in CNF and given to a SAT solver. The ase n = 10 involves 990 variables and 8.6 million lauses, and is solved by Algorithm 7.2.2.2C in 10.5 gigamems. The ase n = 12 involves 4020 variables and 175 million lauses. After being split into seven independent subproblems (by appending mutually ex lusive unit lauses), it was proved unsatis able by that algorithm after about 86 teramems of omputation. So the answer is \No." The maximum-size ode for n = 12 remains unknown. 44. (a) There are 28 ommafree binary odes of size 3 and length 4; Algorithm C produ es half of them, be ause it assumes that y le lass [0001℄ is represented by 0001 or 0010. They form eight equivalen e lasses, two of whi h are symmetri under the operation of omplementation-and-re e tion; representatives are f0001; 0011; 0111g and f0010; 0011; 1011g. The other six are represented by f0001; 0110; 0111 or 1110g, f0001; 1001; 1011 or 1101g, f0001; 1100; 1101g, f0010; 0011; 1101g. (b) Algorithm C produ es half of the 144 solutions, whi h form twelve equivalen e lasses. Eight are represented by f0001, 0002, 1001, 1002, 2201, 2001, 2002, 2011, 2012, 2102, 2112, 2122 or 2212g and (f0102; 1011; 1012g or f1020; 1101; 2101g) and (f1202; 2202; 2111g or f2021; 2022; 1112g); four are represented by f0001, 0020, 0021, 0022, 1001, 1020, 1021, 1022, 1121 or 1211, 1201, 1202, 1221, 2001, 2201, 2202g and (f1011; 1012; 2221g or f1101; 2101; 1222g). ( ) Algorithm C yields half of the 2304 solutions, whi h form 48 equivalen e lasses. Twelve lasses have unique representatives that omit y le lasses [0123℄, [0103℄, [1213℄, one su h being the ode f0010, 0020, 0030, 0110, 0112, 0113, 0120, 0121, 0122, 0130, 0131, 0132, 0133, 0210, 0212, 0213, 0220, 0222, 0230, 0310, 0312, 0313, 0320, 0322, 0330, 0332, 0333, 1110, 1112, 1113, 2010, 2030, 2110, 2112, 2113, 2210, 2212, 2213, 2230, 2310, 2312, 2313, 2320, 2322, 2330, 2332, 2333, 3110, 3112, 3113, 3210, 3212, 3213, 3230, 3310, 3312, 3313g. The others ea h have two representatives that omit lasses [0123℄, [0103℄, [0121℄, one su h being the ode f0001, 0002, 0003, 0201, 0203, 1001, 1002, 1003, 1011, 1013, 1021, 1022, 1023, 1031, 1032, 1033, 1201, 1203, 1211, 1213, 1221, 1223, 1231, 1232, 1233, 1311, 1321, 1323, 1331, 2001, 2002, 2003, 2021, 2022, 2023, 2201, 2203, 2221, 2223, 3001, 3002, 3003, 3011, 3013, 3021, 3022, 3023, 3031, 3032, 3033, 3201, 3203, 3221, 3223, 3321, 3323, 3331g and its isomorphi image under re e tion and (01)(23). 45. (The maximum size of su h a ode is urrently unknown. Algorithm C isn't fast enough to solve this problem on a single omputer, but a suÆ iently large luster of ma hines and/or an improved algorithm should be able to dis over the answer. The ase m = 3 and n = 6 is also urrently unsolved; a SAT solver shows qui kly that a full set of (36 33 32 + 31 )=6 = 116 odewords annot be a hieved.) 47. The 3-bit sequen es 101, 111, 110 were reje ted before seeing 000. In general, to make a uniformly random hoi e from q possibilities, the text suggests looking at the next t = dlg qe bits b1 : : : b . If (b1 : : : b )2 < q, we use hoi e (b1 : : : b )2 + 1; otherwise we reje t b1 : : : b and try again. [This simple method is optimum when q 4, and the best possible running time for other values of q uses more than half as many bits. But a better s heme is available for q = 5, using only 3 31 bits per hoi e instead of 4 45 ; and for q = 6, one random bit redu es to the ase q = 3. See D. E. Knuth and A. C. Yao, Algorithms and Complexity , edited by J. F. Traub (A ademi Press, 1976), 357{428, x2.℄ t t November 12, 2016 t t Niho CNF SAT solver unit lauses uniformly random reje t Knuth Yao omplexity of al ulation omputational omplexity Traub 42 7.2.2 ANSWERS TO EXERCISES 48. It's the number of nodes on level l + 1 (depth l) of the sear h tree. (Hen e we an estimate the pro le. Noti e that D = D1 : : : D 1 in step E2 of Algorithm E.) 49. Z0 = C (), Z +1 = () + D1 (X1 ) + D1 D2 (X1 X2 ) + + D1 : : : D (X1 : : : X ) + D1 : : : D +1 C (X1 : : : X +1 ). 50. (a) True: The generating fun tion is z (z + 1) : : : (z + n 1)=n!; see Eq. 1.2.10{(9). (b) For instan e, suppose Y1 Y2 : : : Y = 1457 and n = 9. Ali e's probability is 1 1 2 1 1 5 1 7 8 = 13 14 16 19 . Elmo obtains X1 X2 : : : X = 7541 with probability 91 16 14 13 . 1 2 3 4 5 6 7 8 9 ( ) The upper tail inequality (see exer ise 1.2.10{22 with = H ) tells us that Pr(l (ln n)(ln ln n)) exp( (ln n)(ln ln n)(ln ln ln n) + O(ln n)(ln ln n)). P (d) If k n=3 we have =0 2 . By exer ise 1.2.6{67, the number of nodes on the rst (ln n)(ln ln n) levels is therefore at most 2(ne=((ln n)(ln ln n)))(ln )(ln ln ) . 51. The key idea is to introdu e re ursive formulas analogous to (29): m(x1 : : : x ) = (x1 : : : x ) + min(m(x1 : : : x x(1)+1) d; : : : ; m(x1 : : : x x(+1) ) d); M (x1 : : : x ) = (x1 : : : x ) + max(M (x1 : : : x x(1)+1 ) d; : : : ; M (x1 : : : x x(+1) ) d); l l l l l l l l n l l l l k n n j j k l 1 l l 2 X d d l (Cb(x : : : x x )d + 2 (x : : : x ) C (x : : : x x )): 1 i=1 l l l n d l l l Cb(x : : : x ) = (x : : : x ) + 1 n l (i) l+1 1 1 l l (i) l+1 They an be omputed via auxiliary arrays MIN, MAX, KIDS, COST, and CHAT as follows: At the beginning of step B2, set MIN[l℄ 1, MAX[l℄ KIDS[l℄ COST[l℄ CHAT[l℄ 0. Set KIDS[l℄ KIDS[l℄ + 1 just before l l + 1 in step B3. At the beginning of step B5, set m (x1 : : : x 1 ) + KIDS[l℄ MIN[l℄, M (x1 : : : x 1 ) + KIDS[l℄ MAX[l℄, C (x1 : : : x 1 ) + COST[l℄, Cb (x1 : : : x 1)2 + KIDS[l℄ CHAT[l℄ + 2 COST[l℄. Then, after l l 1 is positive, set MIN[l℄ min(m; MIN[l℄), MAX[l℄ max(M; MAX[l℄), COST[l℄ COST[l℄ + C , CHAT[l℄ b. But when l rea hes zero in step B5, return the values m, M , C, C b C 2. CHAT[l℄ + C 52. Let p(i) = p 1 l 1 (y ), and simply set D D=p(I ) instead of D Dd. Then node x1 : : : x is rea hed with probability (x1 : : : x ) = p(x1)p 1 (x2 ) : : : p 1 l (x ), and (x1 : : : x ) has weight 1= (x1 : : : x ) in S ; the proof of Theorem E goes through as before. Noti e that p(I ) is the a posteriori probability of having taken bran h I . (The formulas of answer 51 should now use `=p(i)' instead of `d'; and that algorithm should be modi ed appropriately, no longer needing the KIDS array.) 53. Let p 1 l 1 (y ) = C (x1 : : : x 1 y )=(C (x1 : : : x 1 ) (x1 : : : x 1 )). (Of ourse we generally need to know the ost of the tree before we know the exa t values of these ideal probabilities, so we annot a hieve zero varian e in pra ti e. But the form of this solution shows what kinds of bias are likely to redu e the varian e.) 55. The e e ts of lookahead, dynami ordering, and reversible memory are all aptured easily by a well-designed ost fun tion at ea h node. But there's a fundamental di eren e in step C2, be ause di erent odeword lasses an be sele ted for bran hing at the same node (that is, with the same an estors x1 : : : x 1 ) after C5 has undone the e e ts of a prior hoi e. The level l never surpasses L + 1, but in fa t the sear h tree involves hidden levels of bran hing that are impli itly ombined into single nodes. Thus it's best to view Algorithm C's sear h tree as a sequen e of binary bran hes: Should x be one of the odewords or not? (At least this is true when the \max kill" strategy of answer 39 has sele ted the bran hing variable x. But if r > 1 and the poison list is empty, an r-way bran h is reasonable (or an (r + 1)-way bran h when the sla k is positive), be ause r will be redu ed by 1 and the same lass will be hosen after x has been explored.) l l l X :::X l i l X :::X l i l i l l l November 12, 2016 x :::x x l l l pro le generating fun tion tail inequality umulative binomial distribution re ursive formulas a posteriori ost fun tion sear h tree poison list sla k 7.2.2 ANSWERS TO EXERCISES 43 If x has been sele ted be ause it kills many other potential odewords, we probably should bias the bran h probability as in exer ise 52, giving smaller weight to the \yes" bran h be ause the bran h that in ludes x is less likely to lead to a large subtree. ( ) 57. Let p = 1=D be the probability that Algorithm E terminates at the kth leaf. P Then =1 (1=M ) lg(1=(Mp )) is the Kullba k{Leibler divergen e D(q jj p), where q is P the uniform distribution (see exer ise MPR{121). Hen e 1 =1 lg D( ) lg M . (The result of this exer ise is essentially true in any probability distribution.) 58. Let 1 be any onvenient value n. When vertex v be omes part of the path we will perform a two-phase algorithm. The rst phase identi es all \tarnished" verti es, whose DIST must hange; these are the verti es u from whi h every path to t passes through v. It also forms a queue of \resour e" verti es, whi h are untarnished but adja ent to tarnished ones. The se ond phase updates the DISTs of all tarnished verti es that are still onne ted to t. Ea h vertex has LINK and STAMP elds in addition to DIST. For the rst phase, set d DIST(v), DIST(v) 1 +1, R , T v, LINK(v) , then do the following while T 6= : () Set u T, T S . For ea h w u, if DIST(w) < d do nothing (this happens only when u = v ); if DIST(w) 1 do nothing (w is gone or already known to be tarnished); if DIST(w) = d, make w a resour e (see below); otherwise DIST(w) = d + 1. If w has no neighbor at distan e d, w is tarnished: Set LINK(w) T, DIST(w) 1, T w. Otherwise make w a resour e (see below). Then set u LINK(u), and return to () if u 6= . The queue of resour es will start at R. We will stamp ea h resour e with v so that nothing is added twi e to that queue. To make w a resour e when DIST(w) = d, do the following (unless u = v or STAMP(w) = v): Set STAMP(w) v; if R = , set R RT w; otherwise set LINK(RT) w and RT w. To make w a resour e when DIST(w) = d + 1 and u 6= v and STAMP(w) 6= v , put it rst on sta k S as follows: Set STAMP(w) v; if S = , set S SB w; otherwise set LINK(w) S, S w. Finally, when u = , we append S to R: Nothing needs to be done if S = . Otherwise, if R = , set R S and RT SB; but if R 6= , set LINK(RT) S and RT SB. (These shenanigans keep the resour e queue in order by DIST.) Phase 2 operates as follows: Nothing needs to be done if R = . Otherwise we set LINK(RT) , S , and do the following while R 6= or S 6= : (i) If S = , set d DIST(R). Otherwise set u S, d DIST(u), S ; while u 6= , update the neighbors of u and set u LINK(u). (ii) While R 6= and DIST(R) = d, set u R, R LINK(u), and update the neighbors of u. In both ases \update the neighbors of u" means to look at all w u, and if DIST(w) = 1 to set DIST(w) d + 1, STAMP(w) v, LINK(w) S, and S w. (It works!) 0 59. (a) Compute the generating fun tion g (z ) (see exer ise 7.1.4{209) and then g (1). (b) Let (A;B; C ) denote paths that tou h ( enter, NE orner, SW orner). Reursively ompute eight ounts ( 0 ; : : : ; 7 ) at ea h node, where ounts paths with j = 4[ 2 A℄ + 2[ 2 B ℄ + [ 2 C ℄. At the sink node > we have 0 = 1, e? x : x ) where e is an edge. 1 = = 7 = 0. Other nodes have the form x = ( Two edges go a ross the enter and a e t A; three edges a e t ea h of B and C . Say that those edges have types 4, 2, 1, respe tively; other edges have type 0. Suppose the ounts for x and x are ( 00 ; : : : ; 07 ) and ( 000 ; : : : ; 007 ), and e has type t. Then ount for node x is 0 + [ t = 0℄ 00 + [t & j 6= 0℄( 00 + 00 ). (This pro edure yields the following exa t \Venn diagram" set ounts at the root: 0 = jA \ B \ C j = 7653685384889019648091604; 1 = 2 = jA \ B \ C j = jA \ B \ C j = 7755019053779199171839134; 3 = jA \ B \ C j = 7857706970503366819944024; k k M k k M k k M j l l j h j November 12, 2016 h j j j t bias Kullba k Leibler divergen e generating fun tion Venn diagram 44 7.2.2 ANSWERS TO EXERCISES 4 = jA \ B \ C j = 4888524166534573765995071; 5 = 6 = jA \ B \ C j = jA \ B \ C j = 4949318991771252110605148; 7 = jA \ B \ C j = 5010950157283718807987280.) 60. Yes, the paths are less haoti and the estimates are better: re urren e Cook Kleber Mahler solution 81025 unique Gridgeman 51025 21025 0 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 0.5 0.0 1025 20 10 (a) Let x be the number of nodes at distan e k 1 from the root. (b) Let Q( ) = P (1) + + P ( ) . Then we have the joint re urrenPe P1( ) = 1, P (+1) = Q(2 ) ; in parti ular, Q1( ) = m. And for n 2, we have Q( ) = =1 a for ertain onstants a that an omputed as follows: Set t P ( ) for 1 k n. Then for k = 2, : : : , n set t t t 1 , : : : , t t t 1 . Finally a t for 1 k n. For example, a21 = a22 = 2; a31 = 6, a32 = 14, a33 = 8. The numbers P ( ) have O(n2 + n log m) bits, so this method needs O(n5 ) bit operations to ompute P . ( ) P ( ) orresponds to random paths with X1 = m, D = 2X , X +1 = d2U X e, where ea h U is an independent uniform deviate. Therefore P ( ) = E(D1 : : : D 1 ) is the number of nodes on level nn of an in nite tree. We have X n+1 2 U1 : : : U m, by indu tion; hen e P ( ) E(2( 2 ) U1 2 U2 3 : : : U 1 2 m 1 ) = 2( 2 ) m 1=(n 1)!. [M. Cook and M. Kleber have dis ussed similar sequen es in Ele troni Journal of Combinatori s 7 (2000), #R44. See also K. Mahler's asymptoti formula for binary partitions, in J. London Math. So iety 15 (1940), 115{123, whi h shows that lg P = lg(n 1)! + lg2 + O(1).℄ 2 66. Random trials indi ate that the expe ted number of 2-regular graphs is 3:115, and that the number of disjoint pairs is (0, 1, : : : , 9, and 10) approximately (74.4, 4.3, 8.7, 1.3, 6.2, 0.2, 1.5, 0.1, 2.0, 0.0, and 12.2) per ent of the time. If the ubes are restri ted to ases where ea h olor o urs at least ve times, these numbers hange to 4:89 and (37.3, 6.6, 17.5, 4.1, 16.3, 0.9, 5.3, 0.3, 6.7, 0.2, 5.0). However, the on ept of \unique solution" is tri ky, be ause a 2-regular graph with k y les yields 2 ways to position the ubes. Let's say that a set of ubes has a strongly unique solution if (i) it has a unique disjoint pair of 2-regular graphs, and furthermore (ii) both elements of that pair are n- y les. Su h sets o ur with probability only about 0.3% in the rst ase, and 0.4% in the se ond. [N. T. Gridgeman, in Mathemati s Magazine 44 (1971), 243{252, showed that puzzles with four ubes and four olors have exa tly 434 \types" of solutions.℄ 67. It's easy to nd su h examples at random, as in the se ond part of the previous answer, sin e strongly unique sets o ur about 0.5% of the time (and weakly unique 61. k m n m n n m m n n m n m m nk n n k k k k n k n n k m nk nk k k m n n m n k k k m n k n n k November 12, 2016 n n n n k n k k m n k k n n 7.2.2 ANSWERS TO EXERCISES 45 A sets o ur with probability 8:4%). For example, the pairs of opposite fa es might be (12; 13; 34), (02; 03; 14), (01; 14; 24), (04; 13; 23), (01; 12; 34). (In identally, if we require ea h olor to o ur exa tly six times, every set of ubes that has at least one solution will have at least three solutions, be ause the \hidden" pairs an be hosen in three ways.) 70. Ea h of these ubes an be pla ed in 16 di erent ways that ontribute legitimate letters to all four of the visible words. (A ube whose fa es ontain only letters in fC ; H ; I ; N ; O ; U ; X ; Z g an be pla ed in 24 ways. A ube with a pattern like B D annot be pla ed at all.) We an restri t the rst ube to just two pla ements; thus there are 2 16 16 16 16 = 131072 ways to pla e those ubes without hanging their order. Of these, only 6144 are \ ompatible," in the sense that no rightside-up-only letter appears together with an upside-down-only letter in the same word. The 6144 ompatible pla ements an then ea h be reordered in 5! = 120 ways. One of them, whose words before reordering are GRHTI , NCICY , , , leads to the unique solution. (There's a partial solution with three words out of four. There also are 39 ways to get two valid words, in luding one that has UNTIL adja ent to HOURS , and several with SYRUP opposite ECHOS.) 73. Call the rays N, NE, E, SE, S, SW, W, NW; all the disks 1, 2, 3, 4 from inside to outside. We an keep disk 1 xed. The sum of rays N, S, E, W must be 48. It is 16 (on disk 1) plus 13 or 10 (on disk 2) plus 8 or 13 (on disk 3) plus 11 or 14. So it is attained either as shown, or after rotating disks 2 and 4 lo kwise by 45Æ . (Or we ould rotate any disk by a multiple of 90Æ , sin e that keeps the desired sum invariant.) Next, with optional 90Æ rotations, we must make the sum of rays N + S equal to 24. In the rst solution above it is 9 plus (6 or 7) plus (4 or 4) plus (7 or 4), hen e never 24. But in the other solution it's 9 plus (4 or 6) plus (4 or 4) plus (5 or 9); hen e we must rotate disk 2 lo kwise by 90Æ , and possibly also disk 3. However, 90Æ rotation of disk 3 would make the NE + SW sum equal to 25, so we musn't move it. Finally, to get NE's sum to be 12, via optional rotations by 180Æ , we have 1 plus (2 or 5) plus (1 or 5) plus (3 or 4); we must shift disks 3 and 4. Hurray: That makes all eight rays orre t. Fa toring twi e has redu ed 83 trials to 23 + 23 + 23 . [See George W. Ernst and Mi hael M. Goldstein, JACM 29 (1982), 1{23. Su h puzzles go ba k to the 1800s; three early examples are illustrated on pages 28 of Slo um and Botermans's New Book of Puzzles (1992). One of them, with six rings and six rays, fa tors from 65 trials to 25 + 35 . A ve-ray puzzle would have defeated fa torization.℄ 75. Call the ards 1525, 5113, : : : , 3755. The key observation is that all 12 sums must be odd, so we an rst solve the problem mod 2. For this purpose we may all the ards 1101, 1111, : : : , 1111; only three ards now hange under rotation, namely 1101, 0100, and 1100 (whi h are the mod 2 images of 1525, 4542, and 7384). A se ond observation is that ea h solution gives 6 6 2 others, by permuting rows and/or olumns and/or by rotating all nine ards. Hen e we an assume that the upper left ard is 0011 (8473). Then 0100 (4542) must be in the rst olumn, possibly rotated to 0001 (4245), to preserve parity in the left two bla k sums. We an assume that it's in row 2. In fa t, after retreating from 13 mod 2 to 13, we see that it must be rotated. Hen e the bottom left ard must be either 4725, 7755, or 3755. Similarly we see that 1101 (1525) must be in the rst row, possibly rotated to 0111 (2515); we an put it in olumn 2. It must be rotated, and the top right ard must be 3454 or 3755. This leaves just six s enarios to onsider, and we soon obtain the solution: 8473, 2515, 3454; 4245, 2547, 7452; 7755, 1351, 5537. OUNNI NWRGO November 12, 2016 invariant Ernst Goldstein Slo um Botermans Washington Monument Puzzle, see Fool's D Rotating Century Puzzle, see Fool's Disk Safe Combination Puzzle, see Fool's Disk 46 7.2.2 ANSWERS TO EXERCISES 90. Suppose there are n questions, whose answers ea h lie in a given set S . A student supplies an answer list = a1 : : : a , with ea h a 2 S ; a grader supplies a Boolean ve tor = x1 : : : x . There is a Boolean fun tion f ( ; ) for ea h j 2 f1; : : : ; ng and ea h s 2 S . A graded answer list ( ; ) is valid if and only if F ( ; ) is true, where n j n js F ( ; ) = F (a : : : a ; x : : : x ) = 1 n 1 n ^ ^ n j =1 s 2 ([ a = s ℄ ) x f ( ; )): j j js S The maximum s ore is the largest value of x1 + + x over all graded answer lists ( ; ) that are valid. A perfe t s ore is a hieved if and only if F ( ; 1 : : : 1) holds. Thus, in the warmup problem we have n = 2, S = fA; Bg; f1A = [ a2 = B℄; f1B = [ a1 = A℄; f2A = x1 ; f2B = x2 [ a1 = A℄. The four possible answer lists are: AA: F = (x1 [A = B℄) ^ (x2 x1 ) AB: F = (x1 [B = B℄) ^ (x2 x2 [A = A℄) BA: F = (x1 [B = A℄) ^ (x2 x1 ) BB: F = (x1 [B = A℄) ^ (x2 x2 [B = A℄) Thus AA and BA must be graded 00; AB an be graded either 10 or 11; and BB has no valid grading. Only AB an a hieve the maximum s ore, 2; but 2 isn't guaranteed. In Table 666 we have, for example, f1C =P[ a2 6= A℄ ^ [ a3 = A℄; f4D = [ a1 = D℄ ^ [ a15 = D℄; f12A = [A 1 = B ℄, where = 1 20 [ a = s ℄. It's amusing to note that f14E = [ fA ; : : : ; E g = f2; 3; 4; 5; 6g ℄. The other ases are similar (although often more ompli ated) Boolean fun tions | ex ept for 20D and 20E, whi h are dis ussed further in exer ise 91. Noti e that an answer list that ontains both 10E and 17E must be dis arded: It an't be graded, be ause 10E says `x10 x17 ' while 17E says `x17 x10 '. By suitable ba ktra k programming, we an prove rst that no perfe t s ore is possible. Indeed, if we onsider the answers in the order (3, 15, 20, 19, 2, 1, 17, 10, 5, 4, 16, 11, 13, 14, 7, 18, 6, 8, 12, 9), many ases an qui kly be ruled out. For example, suppose a3 = C. Then we must have a1 6= a2 6= 6= a16 6= a17 = a18 6= a19 6= a20 , and early uto s are often possible. (We might rea h a node where the remaining hoi es for answers 5, 6, 7, 8, 9 are respe tively fC; Dg, fA; Cg, fB; Dg, fA; B; Eg, fB; C; Dg, say. Then if answer 8 is for ed to be B, answer 7 an only be D; hen e answer 6 is also for ed to be A. Also answer 9 an no longer be B.) An instru tive little propagation algorithm will make su h dedu tions ni ely at every node of the sear h tree. On the other hand, diÆ ult questions like 7, 8, 9, are best not handled with ompli ated me hanisms; it's better just to wait until all twenty answers have been tentatively sele ted, and to he k su h hard ases only when the he king is easy and fast. In this way the author's program showed the impossibility of a perfe t s ore by exploring just 52859 nodes, after only 3.4 megamems of omputation. The next task was to try for s ore 19 by asserting that only x is false. This turned out to be impossible for 1 j 18, based on very little omputation whatsoever (espe ially, of ourse, when j = 6). The hardest ase, j = 15, needed just 56 nodes and fewer than 5 kilomems. But then, ta da, three solutions were found: One for j = 19 (185 kilonodes, 11 megamems) and two for j = 20 (131 kilonodes, 8 megamems), namely 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 (i) D C E A B E B C E A B E A E D B D A b B (ii) A E D C A B C D C A C E D B C A D A A (iii) D C E A B A D C D A E D A E D B D B E e n s j j j November 12, 2016 student grader valid dynami ordering propagation algorithm sear h tree author 7.2.2 ANSWERS TO EXERCISES 47 (The in orre t answers are shown here as lower ase letters. The rst two solutions establish the truth of 20B and the falsity of 20E.) 91. Now there's only one list of answers with s ore 19, namely (iii). But that is paradoxi al | be ause it laims 20E is false; hen e the maximum s ore annot be 19! Paradoxi al situations are indeed possible when the global fun tion F of answer 90 is used re ursively within one or more of the lo al fun tions f . Let's explore a bit of re ursive territory by onsidering the following two-question, two-letter example: js 1. 2. (A) Answer 1 is in orre t. (B) Answer 2 is in orre t. (A) Some answers an't be graded onsistently. (B) No answers a hieve a perfe t s ore. Here we have f1A = x1 ; f1B = x2 ; f2A = 9a1 9a2 8x1 8x2 :F (a1 a2 ; x1 x2 ); f2B = 8a1 8a2 :F (a1 a2 ; 11). (Formulas quanti ed by 9a or 8a expand into jS j terms, while 9x or 8x expand into two; for example, 9a 8xg(a;x) = (g(A; 0)^g(A; 1))_(g(B; 0)^g(B; 1)) when S = fA; Bg.) Sometimes the expansion is unde ned, be ause it has more than one \ xed point"; but in this ase there's no problem be ause f2A is true: Answer AA an't be graded, sin e 1A implies x1 x1 . Also f2B is true, be ause both BA and BB imply x1 x2 . Thus we get the maximum s ore 1 with either BA or BB and grades 01. On the other hand the simple one-question, one-letter questionnaire `1. (A) The maximum s ore is 1' has an indeterminate maximum s ore. For in this ase f1A = F (A; 1). We nd that if F (A; 1) = 0, only (A; 0) is a valid grading, so the only possible s ore is 0; similarly, if F (A; 1) = 1, the only possible s ore is 1. OK, suppose that the maximum s ore for the modi ed Table 666 is m. We know that m < 19; hen e (iii) isn't a valid grading. It follows that 20E is true, whi h means that every valid graded list of s ore m has x20 false. And we an on lude that m = 18, be ause of the following two solutions (whi h are the only possibilities with 20C false): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 B A d A B E D C D A E D A E D E D B E A E D C A B C D C A C E D B a C D A A But wait: If m = 18, we an s ore 18 with 20A true and two errors, using (say) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 D e D A B E D e C A E D A E D B D C C A or 47 other answer lists. This ontradi ts m = 18, be ause x20 is true. End of story? No. This argument has impli itly been predi ated on the assumption that 20D is false. What if m is indeterminate? Then a new solution arises 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 D C E A B E D C E A E B A E D B D A d D of s ore 19. With (iii) it yields m = 19! If m is determinate, we've shown that m annot a tually be de ned onsistently; but if m is indeterminate, it's de nitely 19. Question 20 was designed to reate diÆ ulties. [:-)℄ | DONALD R. WOODS (2001) 95. The 29 words spark, often , lu ky, other, month, ought, names , water, games, offer, lying, opens, magi , bri k, lamps, empty, organ, noise, after, raise, drink, draft, ba ks, among, under, mat h, earth, roots, topi yield this: \The su ess or failure of ba ktra k often depends on the skill and ingenuity of the programmer. : : : Ba ktra k programming (as many other types of programming) is somewhat of an art." | Solomon W. Golomb, Leonard D. Baumert. November 12, 2016 re ursively quanti ed xed point indeterminate WOODS Golomb Baumert 48 7.2.2 ANSWERS TO EXERCISES That solution an be found intera tively, using inspired guesses based on a knowledge of English and its ommon two-letter and three-letter words. But ould a omputer that knows ommon English words dis over it without understanding their meanings? We an formulate that question as follows: Let w1 , : : : , w29 be the unknown words from WORDS(1000), and let q1 , : : : , q29 be the unknown words of the quotation. (By oin iden e there happen to be just 29 of ea h.) We an restri t the q's to words that appear, say, 32 times or more in the British National Corpus. That gives respe tively (85, 562, 1863, 3199, 4650, 5631, 5417, 4724, 3657, 2448) hoi es for words of (2, 3, : : : , 11) letters; in parti ular, we allow 3199 possibilities for the ve-letter words q7 , q11 , q21 , q22 , be ause they aren't required to lie in WORDS(1000). Is there a unique ombination of words w and q that meets the given ana rosti onstraints? This is a hallenging problem, whi h we shall dis uss in later se tions. The answer turns out (surprisingly?) to be no ; in fa t, here is the rst solution found by the author's ma hine(!): \The su ess or failure of ba ktra k often depends on roe skill and ingenuity at the programmer. : : : Ba ktra k programming (as la y o al types of programming) as somewhat al an art." (The OSPD4 in ludes `al' as the name of the Indian mulberry tree; the BNC has `al' 3515 times, mostly in unsuitable ontexts, but that orpus is a blunt instrument.) Altogether 720 solutions satisfy the stated onstraints; they di er from the \truth" only in words of at most ve letters. Ana rosti puzzles, whi h are also known by other names su h as double- rosti s, were invented in 1933 by E. S. Kingsley. See E. S. Spiegelthal, Pro eedings of the Eastern Joint Computer Conferen e 18 (1960), 39{56, for an interesting early attempt to solve them | without ba ktra king | on an IBM 704 omputer. 96. Instead of onsidering 1000 possibilities for , it suÆ es to onsider 131132133134135 the 43 pairs xy su h that xyab is in WORDS(1000) and ab is a ommon three-letter word. (Of these pairs ab, ag, : : : , ve, only ar leads to a solution. And indeed, the 720 solutions fa tor into three sets of 240, orresponding to hoosing earth, harsh, or .) Similar redu tions, but not so dramati , o ur with respe t large as 131132133134135 to 137139 , 118119 , 46 48 , and 32 35 . 100. The following algorithm uses an integer utility eld TAG(u) in the representation of ea h vertex u, representing the number of times u has been \tagged." The operations \tag u" and \untag u" stand respe tively for TAG(u) TAG(u) + 1 and TAG(u) TAG(u) 1. Verti es shown as ` ' in the 21 examples have a nonzero TAG eld, indi ating that the algorithm has de ided not to in lude them in this parti ular H . State variables v (a vertex), i (an index), and a (an ar ) are used at level l for 0 l < n. We assume that n > 1. R1. [Initialize.℄ Set TAG(u) 0 for all verti es u. Then set v0 v, i i0 0, a a0 ARCS(v), TAG(v) 1, l 1, and go to R4. R2. [Enter level l.℄ (At this point i = i 1 , v = v , and a = a 1 is an ar from v to v 1 .) If l = n, visit the solution v0 v1 : : : v 1 and set l n 1. R3. [Advan e a.℄ Set a NEXT(a), the next neighbor of v . R4. [Done with level?℄ If a 6= , go to R5. Otherwise if i = l 1, go to R6. Otherwise set i i + 1, v v , a ARCS(v). R5. [Try a.℄ Set u TIP(a) and tag u. If TAG(u) > 1, return to R3. Otherwise set i i, a a, v u, l l + 1, and go to R2. i j l l l l i l November 12, 2016 i l n l l l two-letter three-letter British National Corpus onstraints author OSPD4 double- rosti s Kingsley Spiegelthal IBM 704 utility eld tagged the null pointer 7.2.2 ANSWERS TO EXERCISES 49 [Ba ktra k.℄ Set l l 1, and stop if l = 0. Otherwise set i i . Untag all neighbors of v , for l k > i. Then set a NEXT(a ); while a 6= , untag TIP(a) and set a NEXT(a). Finally set a a and return to R3. This instru tive algorithm di ers subtly from the onventional stru ture of Algorithm B. Noti e in parti ular that TIP(a ) is not untagged in step R6; that vertex won't be untagged and hosen again until some previous de ision has been re onsidered. 101. Let G have N verti es. For 1 k N , perform Algorithm R on the k th vertex v of G, ex ept that step R1 should tag the rst k 1 verti es so that they are ex luded. (A tri ky short ut an be used: If we untag all neighbors of v = v0 after Algorithm R stops, the net e e t will be to tag only v.) The n-omino pla ement ounts 1, 4, 22, 113, 571, 2816, 13616, 64678, 302574 are omputed almost instantly, for small n. (Larger n are dis ussed in Se tion 7.2.3.) 102. (a) All but the 13th and 18th, whi h require an upward or leftward step. (b) True. If u 2 H and u 6= v, let p be any node of H that's one step loser to v. ( ) Again true: The oriented spanning trees are also ordinary spanning trees. (d) The same algorithm works, ex ept that step R4 must return to itself after setting a ARCS(v). (We an no longer be sure that ARCS(v) 6= .) R6. l k l l l u 999. ::: November 12, 2016 ba ktra king, variant stru ture n-omino pla ement INDEX AND GLOSSARY He writes indexes to perfe tion. | OLIVER GOLDSMITH, Citizen of the World (1762) When an index entry refers to a page ontaining a relevant exer ise, see also the answer to that exer ise for further information. An answer page is not indexed here unless it refers to a topi not in luded in the statement of the exer ise. 2-letter blo k odes, 28. 2-letter words of English, 8, 48. 2-regular graphs, 25, 31. 3-letter words of English, 8, 48. 4-letter odewords, 9{18, 28{30. 4-letter words of English, 8, 48. 5-letter words of English, 8{9, 28, 31, 33. 6-letter and k-letter words of English, 8{9, 48. (Euler's onstant), as sour e of \random" data, 19. (the null link), 43, 48. ( ir le ratio), as sour e of \random" data, 19{20, 22, 29. (golden ratio), as sour e of \random" data, 19. A posteriori probability, 42. A tive elements of a list, 12. Ahrens, Joa him Heinri h Lude ke, 6, 27, 36. Alekhnovi h, Mi hael (Misha) Valentinovi h (Alehnoviq, Mihael (Mixa) Valentinoviq), 26. Alphabet, 31. Ana rosti puzzle, 33. Analysis of algorithms, 29, 30. Aperiodi words, 10, 28, 39. ARCS(v) ( rst ar of vertex v), 33, 48. Armbruster, Franz Owen, 24. Ba kjumping, 26. Ba kmarking, 26. Ba ktra k programming, 2{1. eÆ ien y of, 35. history of, 2, 5{6, 25{26. introdu tion to, 2{32. variant stru ture, 48{49. Ba ktra k trees, 3, 4, 7, 9{11, 18{20, 24, 26, 41, 42, 44{46. estimating the size of, 20, 41. Baumert, Leonard Daniel, 26, 47, 51. Bees, queen, 27. Bernoulli, Ja ques (= Jakob = James), 25. Bezzel, Max Friedri h Wilhelm, 25. Biased random walks, 30{31, 43. Binary partitions, 31. Binomial trees, 21. Bitner, James Ri hard, 6, 26. 50 November 12, 2016 Bitwise operations, 5, 27, 37. Blo k odes, 9, 28. Borodin, Allan Bertram, 26. Botermans, Ja obus (= Ja k) Petrus Hermana, 45. Boundary markers, 29. Breadth- rst sear h, 36. Breaking symmetry, 8, 14. British National Corpus, 8, 48. Broken diagonals, see Wraparound. Bumping the urrent stamp, 16, 29. Bun h, Steve Raymond, 6. Buresh-Oppenheim, Joshua, 26. Ca he-friendly data stru tures, 11. Canoni al labeling, 33. Carroll, Lewis (= Dodgson, Charles Lutwidge), iii. Carteblan he, Filet de (pseudonym, most likely of C. A. B. Smith), 24. Cavenagh, Ni holas John, 36. Cayley, Arthur, 31. Cells of memory, 11. Chatterjee, Sourav (esor& *;$;jRI), 22. Chessboard, 2{6, 22{26, 27, 31. Chiral symmetry, 27. Closed lists, 14. Clueless ana rosti , 33. CNF: Conjun tive normal form, 41. Codewords, ommafree, 9{18, 28{30. Coin ipping, 30. Combinations, 27. Commafree odes, 9{18, 26, 28{30. Compilers, 15. Complexity of al ulation, 41. Compressed tries, 38. Computational omplexity, 41. Con atenation, 9. Conne ted subsets, 33. Constraints, 48. Cook, Matthew Makonnen, 44. Corner-to- orner paths, 22{23, 30{31. Cost fun tion, 19, 42. Cri k, Fran is Harry Compton, 9. Cubes, 24{25, 31. Cumulative binomial distribution, 42. Cuto prin iple, 7. Cuto properties, 2, 5, 10, 18, 27. Cy li shifts, 10, 28. GOLDSMITH INDEX AND GLOSSARY Dan ing links, 7. Data stru tures, 4{6, 9, 11{14, 18, 29. de Carteblan he, Filet (pseudonym, most likely of C. A. B. Smith), 24. de Jaenis h, Carl Friedri h Andreevit h (nix, Karl Andreeviq), 35. Degree of a node, 19. Deletion operation, 7, 12{13, 40. Depth- rst sear h, 25. Dia onis, Persi Warren, 22. Diagonal lines (slope 1), 3, 36, see also Wraparound. Digraphs, 28, 34. DIMACS : DIMACS Series in Dis rete Mathemati s and Theoreti al Computer S ien e, inaugurated in 1990. Dips, 29. Dire ted graphs versus undire ted graphs, 34. Dis arded data, 22. Dis rete probabilities, 1. Disjoint sets, 25, 44. Distributed omputations, 6. Divergen e, Kullba k{Leibler, 43. Divide and onquer paradigm, 24. Dodgson, Charles Lutwidge, iii. Domains, 2, 27, 28. Double- rosti s, 48. Downdating versus updating, 4{5, 11, 15. Dual oriented spanning tree, 34. Dual solutions, 6, 8, 28. Dynami ordering, 10{11, 24, 26, 30, 46. Dynami shortest distan es, 30. e, as sour e of \random" data, 19. Eastman, Willard Lawren e, 28, 29, 39. Eight queens problem, 3{4, 19{20, 25{26. Empty lists, 12, 17. Engelhardt, Matthias Rudiger, 6, 35. English words, 8{9, 28, 33. Ernst, George Werner, 45. Error bars, 22. Estimates of run time, 18{21. Estimating the number of solutions, 21{23. Fa torization of problems, 24{25, 31, 33. Fallba k points, 16. Finkel, Raphael Ari, 26. Five-letter words, 8{9, 28. Fixed point of re ursive formula, 47. Floyd, Robert W, 15. Fool's Disk, 31. Four-letter odewords, 9{18, 28{30. FPGA devi es: Field-programmable gate arrays, 6. Frames, 11. Franel, Jerome, 36. November 12, 2016 51 Gardner, Erle Stanley, 2. Gas hnig, John Gary, 26. Gattegno, Bernard, 38. Gau (= Gauss), Johann Frideri h Carl (= Carl Friedri h), 25, 35. Generating fun tions, 29, 42, 43. Gigamem (G): One billion memory a esses, 5. Global variables, 27. Goldsmith, Oliver, 50. Goldstein, Mi hael Milan, 45. Golomb, Solomon Wolf, 9, 26, 47, 51. Gordon, Basil, 9, 51. Graders, 46. Grid graphs, 27, 33. oriented, 34. Gridgeman, Norman Theodore, 44. GriÆth, John Stanley, 9. Hales, Alfred Washington, 51. Hall, Marshall, Jr., 26. Hamilton, William Rowan, paths, 23. Hammersley, John Mi hael, 26. Hands omb, David Christopher, 26. Height of binary trees, 31. Hexagons, 27. Histori al notes, 2, 5{6, 25{26. Ho mann, Louis (pen name of Angelo John Lewis), 24. Homomorphi images, 24. Honey ombs, 27. Hurwitz, Adolf, 36. IBM 704 omputer, 48. IBM 1620 omputer, 6. IBM System 360-75 omputer, 6. Impagliazzo, Russell Graham, 26. Importan e sampling, 26. Independent subproblems, 24. Indeterminate statements, 47. Inner loops, 35. Insertion operation, 12. Instant Insanity, 24{25, 31. Integer partitions, 27, 31. Internet, ii, iii. Invariant relations, 45. Inverse lists, 12{15, 17. Inverse permutations, 12{13. Iteration versus re ursion, 27, 35. Jaenis h, Carl Friedri h Andreevit h de (nix, Karl Andreeviq), 35. Jewett, Robert Israel, 51. Jiggs, B. H. (pen name of Baumert, Hales, Jewett, Imaginary, Golomb, Gordon, and Selfridge), 17. Kennedy, Mi hael David, 6. Kilomem (K): One thousand memory a esses, 44. King, Benjamin Franklin, Jr., 2. 52 INDEX AND GLOSSARY King paths, 22{23, 26, 30{31. Kingsley, Hannah Elizabeth Seelman, 48. Kleber, Mi hael Steven, 44. Knight moves, 23. Knuth, Donald Ervin ( ), i, iv, 18, 26, 27, 38, 41, 46, 48. Kullba k, Solomon, 43. Langford, Charles Dudley, pairs, 6{8, 27{28. Latin squares, 24. Laxdal, Albert Lee, 17. Le Nombre Treize, see Royal Aquarium Thirteen Puzzle. Lehmer, Derri k Henry, 26. Leibler, Ri hard Arthur, 43. Lennon, John Winston Ono, 2. Lewis, Angelo John, 51. Lexi ographi order, 2, 7, 25, 29, 33. Lifting, 24{25. Linked lists, 6{7, 40. Load balan ing, 26. Lookahead, 10, 16, 26, 30. Loose Langford pairs, 28. Lu as, Franois E douard Anatole, 25, 36. Magen, Avner (OBN XEA), 26. Mahler, Kurt, 44. Manber, Udi (XAPN ICER) [not ICE!℄, 26. Martingales, 30. Masks, 35. Mason, Perry, 2. Megamem (M): One million memory a esses, 18. MEM, an array of \ ells," 11{18, 29. Memory onstraints, histori , 40. Mem (): One 64-bit memory a ess, 4. Minimum remaining values heuristi , 26. MMIX omputer, ii. Monte Carlo estimates, 18{23, 26, 30. Moves, 11. MPR: Mathemati al Preliminaries Redux, v, 1. Mystery text, 33. n-letter words of English, 8. n-omino pla ement, 49. n queens problem, 3{6, 18{20, n-tuples, 27. 25{27. Nau k, Franz, 25. Nested parentheses, 27. NEXT(a) (the next ar with the same initial vertex as a), 33, 48. Niho, Yoji Go ( ), 41. Nonisomorphi solutions, 30. Onnen, Hendri k, Sr., 6. Optimization, 26. Orgel, Leslie Eleazer, 9. Oriented grids, 34. November 12, 2016 Oriented trees, 34. Orthogonal lists, 28. OSPD4: OÆ ial SCRABBLE R Di tionary, 8, 48. Over ow of memory, 12, 16. Players P0 (), 2. Paradox, 32. Parallel programming, 46. Parent in a tree, 34. Parentheses, 27. Parity argument, 37, 45. Parker, Ernest Tilden, 24. Partitions, 27, 31. Patents, 24. Paths, simple, 22, 26, 30. Pauls, Emil, 36. Pen il-and-paper method, 18{20. Pentominoes, 33. Periodi sequen es, 28. Periodi words, 10, 13. Permutations, 6, 27. Phi (), as sour e of \random" data, 19. Pi (), as sour e of \random" data, 19{20, 22, 29. Pitassi, Toniann, 26. Poison list, 16{17, 29{30, 42. Polya, Gyorgy (= George), 36. Polyominoes, 33. Preuer, Thomas Bernd, 6, 35. Prime strings, 10. Priority bran hing trees, 26. Probabilities, 1. Pro le of a tree, 3, 9, 28, 31, 42. Propagation algorithm, 46. Properties: Logi al propositions (relations), 2, 27. q.s., 30. Quanti ed Boolean formulas, 47. Queen bees, 27. Queens, see n queens problem. Questionnaires, 31. Queues, 40, 43. Qui k, Jonathan Horatio, 27. Radix m representation, 13. Random bits, 30. Random sampling, 18. Random variables, 19. Random walks, 18{23, 30{31. Rea hable subsets, 34. Re urren e relations, 42, 44. in a Boolean equation, 47. Re ursion versus iteration, 27, 35. Re ursive algorithms, 27, 42. Re e tion symmetry, 14, 27, 35, see also Dual solutions. Registers, 5, 37{38. INDEX AND GLOSSARY Reingold, Edward Martin (CLEBPIIX, MIIG OA DYN WGVI), 26. Reje tion method, 19, 41. Restri ted growth strings, 35. Reversible memory te hnique, 16, 30. Rivin, Igor (Rivin, Igor~ Evgen~eviq), 36. Root node, 19. Rosenbluth, Arianna Wright, 26. Rosenbluth, Marshall Ni holas, 26. Rotating Century Puzzle, see Fool's Disk. Rotation by 90Æ , 27. Royal Aquarium Thirteen Puzzle, 31. Running time estimates, 18{21. Safe Combination Puzzle, see Fool's Disk. Sample varian e, 22. SAT solvers, 41. Saturating ternary addition, 38. S holtz, Robert Arno, 39. S hossow, Frederi k Alvin, 24. S huma her, Heinri h Christian, 25, 35. Sear h rearrangement, see Dynami ordering. Sear h trees, 3, 4, 7, 9{11, 18{20, 24, 26, 36, 41, 42, 44{46. estimating the size, 20, 41. Self-avoiding walks, 22, 26, 30. Self-referen e, 32, 53. Self-syn hronizing blo k odes, 9. Selfridge, John Lewis, 51. Semi-queens, 36. Sequential allo ation, 29. Sequential lists, 11{15. Set partitions, 27. SGB, see Stanford GraphBase. Shortest distan es, dynami , 30. Signature of a trie node, 37. Simple paths, 22, 26, 30. Sla k, 18, 42. Slo um, Gerald Kenneth (= Jerry), 45. Smith, Cedri Austen Bardell, 51. Spanning trees, 33{34. Speedy S hizophrenia, 31. Spiegelthal, Edwin Simeon, 48. Sprague, Thomas Bond, 5, 6, 27. Sta ks, 16, 40. Stamping, iv, 16{19, 29. Standard deviation, 20, 22, 30. Stanford GraphBase, ii, 8. format for digraphs and graphs, 33, 48. Statisti s, 22. Stirling, James, y le numbers, 30. Students, 46. Substrings, 29. Subtrees, 20, 26. Superdips, 39. SWAC omputer, 6. Symmetries, 30, 35. breaking, 8, 14. November 12, 2016 Tagged verti es, 48{49. Tail inequality, 42. Tantalizer, see Instant Insanity. Teramem (T): One trillion memory a esses, 9. TIP(a) ( nal vertex of ar a), 33, 48. Torus, 27. Tot tibi : : : , 25. Traub, Joseph Frederi k, 41. Tremaux, Charles Pierre, 25. Tries, 8{9, 28, 37. ompressed, 38. Tuples, 27. Twenty Questions, 32. Two-letter blo k odes, 28. UCLA: The University of California at Los Angeles, 6. Undire ted graphs versus dire ted graphs, 34. UNDO sta k, 16. Undoing, 4{5, 7, 15{16, 29. Uniformly random numbers, 41. Unique solutions, 44{45. Unit lauses, 41. University of California, 6. University of Dresden, 6. University of Illinois, 6. University of Tennessee, 6. Unordered sequential lists, 11. Unordered sets, 40. Upper ase letters, 31. Utility elds in SGB format, 47. v-rea hable subsets, 34. Valid gradings, 46. Vardi, Ilan, 36. Varian e of a random variable, 22, 30. Venn, John, diagram, 43. Visiting an obje t, 2, 4, 5, 7, 18. Walker, Robert John, 2, 5, 6, 26. Wanless, Ian Murray, 36. Washington Monument Puzzle, see Fool's Disk. Wel h, Lloyd Ri hard, 9. Wells, Mark Brimhall, 26. White squares, 27. Woods, Donald Roy, 32, 47. Word re tangles, 8{9, 28. WORDS(n), the n most ommon ve-letter words of English, 8, 33. Worst- ase bounds, 29. Wraparound, 27. Yao, Andrew Chi-Chih ( ), 41. ZDD: A zero-suppressed de ision diagram, 23, 30. Zimmermann, Paul Vin ent Marie, 36. 53