To put all the good stuff into one book is patently impossible,
and attempting even to be reasonably comprehensive
about certain aspects of the subject is likely to lead to runaway growth.
— GERALD B. FOLLAND, “Editor’s Corner” (2005)
The title of Volume 4 is Combinatorial Algorithms , and when I proposed it I was strongly inclined to add a subtitle: The Kind of Programming I Like Best . My editors have decided to tone down such exuberance, but the fact remains that programs with a combinatorial flavor have always been my favorites.
On the other hand I’ve often been surprised to find that, in many people’s minds, the word “combinatorial” is linked with computational difficulty. Indeed, Samuel Johnson, in his famous dictionary of the English language (1755), said that the corresponding noun “is now generally used in an ill sense.” Colleagues tell me tales of woe, in which they report that “the combinatorics of the situation defeated us.” Why is it that, for me, combinatorics arouses feelings of pure pleasure, yet for many others it evokes pure panic?
It’s true that combinatorial problems are often associated with humongously large numbers. Johnson’s dictionary entry also included a quote from Ephraim Chambers, who had stated that the total number of words of length 24 or less, in a 24-letter alphabet, is 1,391,724,288,887,252,999,425,128,493,402,200. The corresponding number when we replace 24 by 10 in Chambers’s statement is 11,111,111,110; and it’s only 3905 when we reduce the parameter to 5. Thus a “combinatorial explosion” certainly does occur as the size of the problem grows from 5 to 10 to 24 and beyond.
Computing machines have become tremendously more powerful throughout my life. As I write these words, I know that they are being processed by a “laptop” whose speed is more than 100,000 times faster than the trusty IBM Type 650 computer to which I’ve dedicated these books. My current machine’s memory capacity is also more than 100,000 times greater. Tomorrow’s computers will be even faster and more capacious. But these amazing advances have not diminished people’s craving for answers to combinatorial questions; quite the contrary. Our once-unimaginable ability to compute so rapidly has raised our expectations, and whetted our appetite for more — because the size of a combinatorial problem can increase more than 100,000-fold when a parameter n increases by just 1.
Combinatorial algorithms can be defined informally as techniques for the high-speed manipulation of combinatorial objects such as permutations or graphs. We typically try to find patterns or arrangements that are the best possible ways to satisfy certain constraints. The number of such problems is vast, and the art of writing such programs is especially important and appealing because a single good idea can save years or even centuries of computer time.
Indeed, the fact that good algorithms for combinatorial problems can have a terrific payoff has led to terrific advances in the state of the art. Many problems that once were thought to be intractable can now be polished off with ease, and many algorithms that once were known to be good have now become better. Starting about 1970, computer scientists began to experience a phenomenon that we called “Floyd’s Lemma”: Problems that seemed to need n 3 operations could actually be solved in O ( n 2 ); problems that seemed to require n 2 could be handled in O ( n log n ); and n log n was often reducible to O ( n ). More difficult problems saw a reduction in running time from O (2 n ) to O (1.5 n ) to O (1.3 n ), etc. Other problems remained difficult in general, but they were found to have important special cases that are much simpler. Many combinatorial questions that I once thought would never be answered during my lifetime have now been resolved, and those breakthroughs have been due mainly to improvements in algorithms rather than to improvements in processor speeds.
By 1975, such research was advancing so rapidly that a substantial fraction of the papers published in leading journals of computer science were devoted to combinatorial algorithms. And the advances weren’t being made only by people in the core of computer science; significant contributions were coming from workers in electrical engineering, artificial intelligence, operations research, mathematics, physics, statistics, and other fields. I was trying to complete Volume 4 of The Art of Computer Programming , but instead I felt like I was sitting on the lid of a boiling kettle: I was confronted with a combinatorial explosion of another kind, a prodigious explosion of new ideas!
This series of books was born at the beginning of 1962, when I naïvely wrote out a list of tentative chapter titles for a 12-chapter book. At that time I decided to include a brief chapter about combinatorial algorithms, just for fun. “Hey look, most people use computers to deal with numbers, but we can also write programs that deal with patterns.” In those days it was easy to give a fairly complete description of just about every combinatorial algorithm that was known. And even by 1966, when I’d finished a first draft of about 3000 handwritten pages for that already-overgrown book, fewer than 100 of those pages belonged to Chapter 7 . I had absolutely no idea that what I’d foreseen as a sort of “salad course” would eventually turn out to be the main dish.
The great combinatorial fermentation of 1975 has continued to churn, as more and more people have begun to participate. New ideas improve upon the older ones, but rarely replace them or make them obsolete. So of course I’ve had to abandon any hopes that I once had of being able to surround the field, to write a definitive book that sets everything in order and provides one-stop shopping for everyone who has combinatorial problems to solve. The array of applicable techniques has mushroomed to the point where I can almost never discuss a subtopic and say, “Here’s the final solution: end of story.” Instead, I must restrict myself to explaining the most important principles that seem to underlie all of the efficient combinatorial methods that I’ve encountered so far. At present I’ve accumulated more than twice as much raw material for Volume 4 as for all of Volumes 1–3 combined.
This sheer mass of material implies that the once-planned “Volume 4” must actually become several physical volumes. You are now looking at Volume 4A. Volumes 4B and 4C will exist someday, assuming that I’m able to remain healthy; and (who knows?) there may also be Volumes 4D, 4E, . . .; but surely not 4Z.
My plan is to go systematically through the files that I’ve amassed since 1962 and to tell the stories that I believe are still waiting to be told, to the best of my ability. I can’t aspire to completeness, but I do want to give proper credit to all of the pioneers who have been responsible for key ideas; so I won’t scrimp on historical details. Furthermore, whenever I learn something that I think is likely to remain important 50 years from now, something that can also be explained elegantly in a paragraph or two, I can’t bear to leave it out. Conversely, difficult material that requires a lengthy proof is beyond the scope of these books, unless the subject matter is truly fundamental.
OK, it’s clear that the field of Combinatorial Algorithms is vast, and I can’t cover it all. What are the most important things that I’m leaving out? My biggest blind spot, I think, is geometry, because I’ve always been much better at visualizing and manipulating algebraic formulas than objects in space. Therefore I don’t attempt to deal in these books with combinatorial problems that are related to computational geometry, such as close packing of spheres, or clustering of data points in n -dimensional Euclidean space, or even the Steiner tree problem in the plane. More significantly, I tend to shy away from polyhedral combinatorics, and from approaches that are based primarily on linear programming, integer programming, or semidefinite programming. Those topics are treated well in many other books on the subject, and they rely on geometrical intuition. Purely combinatorial developments are easier for me to understand.
I also must confess a bias against algorithms that are efficient only in an asymptotic sense, algorithms whose superior performance doesn’t begin to “kick in” until the size of the problem exceeds the size of the universe. A great many publications nowadays are devoted to algorithms of that kind. I can understand why the contemplation of ultimate limits has intellectual appeal and carries an academic cachet; but in The Art of Computer Programming I tend to give short shrift to any methods that I would never consider using myself in an actual program. (There are, of course, exceptions to this rule, especially with respect to basic concepts in the core of the subject. Some impractical methods are simply too beautiful and/or too insightful to be excluded; others provide instructive examples of what not to do.)
Furthermore, as in earlier volumes of this series, I’m intentionally concentrating almost entirely on sequential algorithms, even though computers are increasingly able to carry out activities in parallel. I’m unable to judge what ideas about parallelism are likely to be useful five or ten years from now, let alone fifty, so I happily leave such questions to others who are wiser than I. Sequential methods, by themselves, already test the limits of my own ability to discern what the artful programmers of tomorrow will want to know.
The main decision that I needed to make when planning how to present this material was whether to organize it by problems or by techniques. Chapter 5 in Volume 3, for example, was devoted to a single problem, the sorting of data into order; more than two dozen techniques were applied to different aspects of that problem. Combinatorial algorithms, by contrast, involve many different problems, which tend to be attacked with a smaller repertoire of techniques. I finally decided that a mixed strategy would work better than any pure approach. Thus, for example, these books treat the problem of finding shortest paths in Section 7.3, and problems of connectivity in Section 7.4.1.3; but many other sections are devoted to basic techniques, such as the use of Boolean algebra ( Section 7.1 ), backtracking (Section 7.2.2), matroid theory (Section 7.6), or dynamic programming (Section 7.7). The famous Traveling Salesrep Problem, and other classic combinatorial tasks related to coloring and packing, have no sections of their own, but they come up several times in different places as they are treated by different methods.
I’ve mentioned great progress in the art of combinatorial computing, but I don’t mean to imply that all combinatorial problems have actually been tamed. When the running time of a computer program goes ballistic, its programmers shouldn’t expect to find a silver bullet for their needs in this book. The methods described here will often work a great deal faster than the first approaches that a programmer tries; but let’s face it: Combinatorial problems get huge very quickly. We can even prove rigorously that a certain small, natural problem will never have a feasible solution in the real world, although it is solvable in principle (see the theorem of Stockmeyer and Meyer in Section 7.1.2 ). In other cases we cannot prove as yet that no decent algorithm for a given problem exists, but we know that such methods are unlikely, because any efficient algorithm would yield a good way to solve thousands of other problems that have stumped the world’s greatest experts (see the discussion of NP-completeness in Section 7.9).
Experience suggests that new combinatorial algorithms will continue to be invented, for new combinatorial problems and for newly identified variations or special cases of old ones. Experience also suggests that people’s appetite for such algorithms will continue to grow. The art of computer programming continually reaches new heights when programmers are faced with challenges such as these. Yet today’s methods are also likely to remain relevant.
Most of this book is self-contained, although there are frequent tie-ins with the topics discussed in Volumes 1–3. Low-level details of machine language programming have been covered extensively in those volumes, so the algorithms in the present book are usually specified only at an abstract level, independent of any machine. However, some aspects of combinatorial programming are heavily dependent on low-level details that didn’t arise before; in such cases, all examples in this book are based on the MMIX computer, which supersedes the MIX machine that was defined in early editions of Volume 1. Details about MMIX appear in a paperback supplement to that volume called The Art of Computer Programming , Volume 1, Fascicle 1, containing Sections 1.3.1´, 1.3.2´, etc.; they’re also available on the Internet, together with downloadable assemblers and simulators.
Another downloadable resource, a collection of programs and data called The Stanford GraphBase , is cited extensively in the examples of this book. Readers are encouraged to play with it, in order to learn about combinatorial algorithms in what I think will be the most efficient and most enjoyable way.
Many of the computer programs that I wrote while preparing this material are listed on the following webpage:
[https://www-cs-faculty.stanford.edu/~knuth/programs.html](https://www-cs-faculty.stanford.edu/~knuth/programs.html)
In particular, you can download BDD14 and BDD15, which are the experimental toolkits that I played with when studying BDDs and ZDDs, respectively, in Section 7.1.4 . Another example is a program called SIMPATH, for exercise 7.1.4 – 225 .
Incidentally, while writing the introductory material at the beginning of Chapter 7 , I was pleased to note that it was natural to mention some work of my Ph.D. thesis advisor, Marshall Hall, Jr. (1910–1990), as well as some work of his thesis advisor, Oystein Ore (1899–1968), as well as some work of his thesis advisor, Thoralf Skolem (1887–1963). Skolem’s advisor, Axel Thue (1863–1922), was already present in Chapter 6.
I’m immensely grateful to the hundreds of readers who have helped me to ferret out numerous mistakes that I made in the early drafts of this volume, which were originally posted on the Internet and subsequently printed in paperback fascicles. In particular, the extensive comments of Thorsten Dahlheimer, Marc van Leeuwen, and Udo Wermuth have been especially influential. But I fear that other errors still lurk among the details collected here, and I want to correct them as soon as possible. Therefore I will cheerfully award $2.56 to the first finder of each technical, typographical, or historical error. The taocp webpage cited on page iv contains a current listing of all corrections that have been reported to me.
D. E. K.
Stanford, California
October 2010
In my preface to the first edition,
I begged the reader not to draw attention to errors.
I now wish I had not done so
and am grateful to the few readers who ignored my request.
— STUART SUTHERLAND, The International Dictionary of Psychology (1996)
Naturally, I am responsible for the remaining errors—
although, in my opinion, my friends could have caught a few more.
— CHRISTOS H. PAPADIMITRIOU, Computational Complexity (1994)
I like to work in a variety of fields
in order to spread my mistakes more thinly.
— VICTOR KLEE (1999)
A note on references. Several oft-cited journals and conference proceedings have special code names, which appear in the Index and Glossary at the close of this book. But the various kinds of IEEE Transactions are cited by including a letter code for the type of transactions, in boldface preceding the volume number. For example, ‘ IEEE Trans. C-35 ’ means the IEEE Transactions on Computers , volume 35. The IEEE no longer uses these convenient letter codes, but the codes aren’t too hard to decipher: ‘ EC ’ once stood for “Electronic Computers,” ‘ IT ’ for “Information Theory,” ‘ SE ’ for “Software Engineering,” and ‘ SP ’ for “Signal Processing,” etc.; ‘ CAD ’ meant “Computer-Aided Design of Integrated Circuits and Systems.”
A cross-reference such as ‘exercise 7.10–00’ points to a future exercise in Section 7.10 whose number is not yet known.
A note on notations. Simple and intuitive conventions for the algebraic representation of mathematical concepts have always been a boon to progress, especially when most of the world’s researchers share a common symbolic language. The current state of affairs in combinatorial mathematics is unfortunately a bit of a mess in this regard, because the same symbols are occasionally used with completely different meanings by different groups of people; some specialists who work in comparatively narrow subfields have unintentionally spawned conflicting symbolisms. Computer science—which interacts with large swaths of mathematics—needs to steer clear of this danger by adopting internally consistent notations whenever possible. Therefore I’ve often had to choose among a number of competing schemes, knowing that it will be impossible to please everyone. I have tried my best to come up with notations that I believe will be best for the future, often after many years of experimentation and discussion with colleagues, often flip-flopping between alternatives until finding something that works well. Usually it has been possible to find convenient conventions that other people have not already coopted in contradictory ways.
Appendix B is a comprehensive index to all of the principal notations that are used in the present book, inevitably including several that are not (yet?) standard. If you run across a formula that looks weird and/or incomprehensible, chances are fairly good that Appendix B will direct you to a page where my intentions are clarified. But I might as well list here a few instances that you might wish to watch for when you read this book for the first time:
• Hexadecimal constants are preceded by a number sign or hash mark. For example, # 123 means (123) 16 .
• The “monus” operation x
y , sometimes called dot-minus or saturating subtraction, yields max(0, x – y ).
• The median of three numbers { x , y , z } is denoted by
xyz
.
• A set such as { x }, which consists of a single element, is often denoted simply by x in contexts such as X ∪ x or X \ x .
• If n is a nonnegative integer, the number of 1-bits in n ’s binary representation is νn . Furthermore, if n > 0, the leftmost and rightmost 1-bits of n are respectively 2 λn and 2 ρn . For example, ν 18 = 2, λ 18 = 4, ρ 18 = 1.
• The Cartesian product of graphs G and H is denoted by G □ H . For example, C m □ C n denotes an m × n torus, because C n denotes a cycle of n vertices.