everything - Algorithms Department of Computer Science...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Algorithms Department of Computer Science University of Illinois at Urbana-Champaign Instructor: Jeff Erickson Teaching Assistants: • Spring 1999: Mitch Harris and Shripad Thite • Summer 1999 (IMCS): Mitch Harris • Summer 2000 (IMCS): Mitch Harris • Fall 2000: Chris Neihengen, Ekta Manaktala, and Nick Hurlburt • Spring 2001: Brian Ensink, Chris Neihengen, and Nick Hurlburt • Summer 2001 (I2CS): Asha Seetharam and Dan Bullok • Fall 2002: Erin Wolf, Gio Kao, Kevin Small, Michael Bond, Rishi Talreja, Rob McCann, and Yasutaka Furakawa • Spring 2004: Dan Cranston, Johnathon Fischer, Kevin Milans, and Lan Chen • Fall 2005: Erin Chambers, Igor Gammer, and Aditya Ramani • Fall 2006: Dan Cranston, Nitish Korula, and Kevin Milans • Spring 2007: Kevin Milans • Fall 2008: Reza Zamani-Nasab • Spring 2009: Alina Ene, Ben Moseley, and Amir Nayyeri c Copyright 1999–2009 Jeff Erickson. Last update August 16, 2009. This work may be freely copied and distributed, either electronically or on paper. It may not be sold for more than the actual cost of reproduction, storage, or transmittal. This work is licensed under a Creative Commons Attribution-NonCommercial-Share Alike 3.0 United States License. For license details, see http://creativecommons.org/licenses/by-nc-sa/3.0/us/. For the most recent edition of this work, see http://www.uiuc.edu/~jeffe/teaching/algorithms/. Shall I tell you, my friend, how you will come to understand it? Go and write a book on it. — Henry Home, Lord Kames (1696–1782), to Sir Gilbert Elliot You know, I could write a book. And this book would be thick enough to stun an ox. — Laurie Anderson, “Let X=X”, Big Science (1982) I’m writing a book. I’ve got the page numbers done, so now I just have to fill in the rest. — Stephen Wright About These Notes This course packet includes lecture notes, homework questions, and exam questions from algorithms courses I taught at the University of Illinois at Urbana-Champaign in Spring 1999, Fall 2000, Spring 2001, Fall 2002, Spring 2004, Fall 2005, Fall 2006, Spring 2007, Fall 2008, and Spring 2009. These lecture notes and my videotaped lectures were also offered over the web in Summer 1999, Summer 2000, Summer 2001, Fall 2002, and Fall 2005 as part of the UIUC computer science department’s online master’s program. Lecture notes were posted to the course web site a few days (on average) after each lecture. Homeworks, exams, and solutions were also distributed over the web. I wrote a significant fraction of these lecture notes in Spring 1999; I revise them and add a few new notes every time I teach the course. The recurrences ‘pre-lecture’ is very loosely based on a handout written by Ari Trachtenberg, based on a paper by George Lueker, from an earlier semester (Fall 1998?) taught by Ed Reingold, but I have essentially rewritten it from scratch. Most (but not all) of the exercises at the end of each lecture note have been used at least once in a homework assignment, discussion section, or exam. You can also find a near-complete collection of homeworks and exams from past semesters of my class online at http://www.uiuc.edu/~jeffe/teaching/ algorithms/. A large fraction of these exercises were contributed by some amazing teaching assistants: Aditya Ramani, Alina Ene, Amir Nayyeri, Asha Seetharam, Ben Moseley, Brian Ensink, Chris Neihengen, Dan Bullok, Dan Cranston, Johnathon Fischer, Ekta Manaktala, Erin Wolf Chambers, Igor Gammer, Gio Kao, Kevin Milans, Kevin Small, Lan Chen, Michael Bond, Mitch Harris, Nick Hurlburt, Nitish Korula, Reza Zamani-Nasab, Rishi Talreja, Rob McCann, Shripad Thite, and Yasu Furakawa. Stars indicate more challenging problems; many of these appeared on qualifying exams for the algorithms PhD stduents at UIUC. A small number of really hard problems are marked with a larger star; one or two open problems are indicated by enormous stars. Please do not ask me for solutions to the exercises. If you’re a student, seeing the solution will rob you of the experience of solving the problem yourself, which is the only way to learn the material. If you’re an instructor, you shouldn’t assign problems that you can’t solve yourself! (I do not always follow my own advice; some of these problems have serious bugs.) Prerequisites For the most part, these notes assume that the reader has mastered the material covered in the first two years of a typical undergraduate computer science curriculum. (Mastery is not the same thing as ‘exposure’ or ‘a good grade’; this is why I start every semester with Homework Zero.) Specific prerequisites include: • Proof techniques: direct proof, indirect proof, proof by contradiction, combinatorial proof, and induction (including its “strong”, “structural”, and “recursive” forms). Lecture 0 requires induction, and whenever Lecture n − 1 requires induction, so does Lecture n. • Discrete mathematics: Boolean algebra, predicate logic, sets, functions, relations, recursive definitions, trees (as abstract objects, not just data structures), graphs • Elementary discrete probability: expectation, linearity of expectation, independence • Iterative programming concepts: variables, conditionals, iteration, subroutines, indirection (addresses/pointers/references), recursion. Programming experience in any language that supports pointers and recursion is a plus. • Fundamental data structures: arrays/vectors, linked lists, search trees, heaps • Fundamental abstract data types: dictionaries, stacks, queues, priority queues; the difference between this list and the previous list. • Fundamental algorithms: searching, sorting (selection, insertion, merge, heap, quick, anything but bubble), pre-/post-/inorder tree traversal, depth/breadth first search • Basic algorithm analysis: Asymptotic notation (o, O, Θ, Ω, ω), translating loops into sums and recursive calls into recurrences, evaluating simple sums and recurrences. • Mathematical maturity: facility with abstraction, formal (especially recursive) definitions, and (especially inductive) proofs; following mathematical arguments; recognizing syntactic, semantic, and/or logical nonsense; writing the former rather than the latter Some of this material is covered briefly, but more as a reminder than a good introduction. In the future, I hope to write review notes that cover more of these topics. Alas, the future has not yet arrived. Acknowledgments The lecture notes and exercises draw heavily on the creativity, wisdom, and experience of thousands of algorithms students, teachers, and researchers. In particular, I am immensely grateful to the almost 1200 Illinois students who have used these notes as a primary reference, offered useful (if sometimes painful) criticism, and suffered through some truly awful first drafts. I’m also grateful for the contributions and feedback from teaching assistants, all listed above. Naturally, these notes owe a great deal to the people who taught me this algorithms stuff in the first place: Bob Bixby and Michael Perlman at Rice; David Eppstein, Dan Hirshberg, and George Lueker at UC Irvine; and Abhiram Ranade, Dick Karp, Manuel Blum, Mike Luby, and Raimund Seidel at UC Berkeley. I’ve also been helped tremendously by many discussions with faculty colleagues at UIUC—Chandra Chekuri, Edgar Ramos, Herbert Edelsbrunner, Jason Zych, Lenny Pitt, Mahesh Viswanathan, Margaret Fleck, Shang-Hua Teng, Steve LaValle, and especially Ed Reingold and Sariel Har-Peled. I stole the first iteration of the overall course structure, and the idea to write up my own lecture notes, from Herbert Edelsbrunner. “Johnny’s” multi-colored crayon homework was found under the TA office door among the other Fall ¯ 2000 Homework 1 submissions. The square Kufi rendition of the name “al-Khwarizm¯ on the back of ı” the cover page is mine. The following sources have been invaluable references. (This list is incomplete!) • Alfred V Aho, John E. Hopcroft, and Jeffrey D. Ullman. The Design and Analysis of Computer . Algorithms. Addison-Wesley, 1974. (I used this textbook as an undergrad at Rice, and again as a masters student at UC Irvine.) • Sara Baase and Allen Van Gelder. Computer Algorithms: Introduction to Design and Analysis. Addison-Wesley, 2000. • Mark de Berg, Marc van Kreveld, Mark Overmars, and Otfried Schwarzkopf. Computational Geometry: Algorithms and Applications. Springer-Verlag, 1997. • Thomas Cormen, Charles Leiserson, Ron Rivest, and Cliff Stein. Introduction to Algorithms, second edition. MIT Press/McGraw-Hill, 2000. (This was my recommended textbook until 2005. I also used the first edition as a teaching assistant at Berkeley.) • Sanjoy Dasgupta, Christos H. Papadimitriou, and Umesh V Vazirani. Algorithms. McGraw-Hill, . 2006. (This is the current recommended textbook for the undergraduate section of my class.) • Michael R. Garey and David S. Johnson. Computers and Intractability: A Guide to the Theory of NP-Completeness. W. H. Freeman, 1979. • Michael T. Goodrich and Roberto Tamassia. Algorithm Design: Foundations, Analysis, and Internet Examples. John Wiley & Sons, 2002. • Dan Gusfield. Algorithms on Strings, Trees, and Sequences: Computer Science and Molecular Biology. Cambridge University Press, 1997. • Jon Kleinberg and Éva Tardos. Algorithm Design. Addison-Wesley, 2005. (This is the current recommended textbook for the graduate section of my class.) • Donald Knuth. The Art of Computer Programming, volumes 1–3. Addison-Wesley, 1997. (My parents gave me these for Christmas when I was 14. I actually read it much later.) • Udi Manber. Introduction to Algorithms: A Creative Approach. Addison-Wesley, 1989. (I used this textbook as a teaching assistant at Berkeley.) • Rajeev Motwani and Prabhakar Raghavan. Randomized Algorithms. Cambridge University Press, 1995. • Ian Parberry. Problems on Algorithms. Prentice-Hall, 1995. (This book is out of print, but it can be downloaded for ‘free’ from http://www.eng.unt.edu/ian/books/free/license.html .) • Alexander Schrijver. Combinatorial Optimization: Polyhedra and Efficiency. Springer, 2003. • Robert Sedgewick. Algorithms. Addison-Wesley, 1988. (This book and its sequels have by far the best algorithm illustrations I’ve seen anywhere.) • Robert Endre Tarjan. Data Structures and Network Algorithms. SIAM, 1983. • Robert J. Vanderbei. Linear Programming: Foundations and Extensions. Springer, 2001. • Class notes from my own algorithms classes at Berkeley, especially those taught by Dick Karp and Raimund Seidel. • The Source of All Knowledge (Google) and The Source of All Lies (Wikipedia). Caveat Lector! With few exceptions, each of these notes contains far too much material to cover in a single lecture. In a typical 75-minute lecture, I tend to cover 4 to 5 pages of material—a bit more if I’m lecturing to graduate students than to undergraduates. Your mileage may vary! (Arguably, that means that as I continue to add material, the label “lecture notes” becomes less and less accurate.) Despite several rounds of revision, these notes still contain lots of mistakes, errors, bugs, gaffes, omissions, snafus, kludges, typos, mathos, grammaros, thinkos, brain farts, nonsense, garbage, cruft, junk, and outright lies, all of which are entirely Steve Skiena’s fault. I revise and update these notes every time I teach the course, so please let me know if you find a bug. (Steve is unlikely to care.) Whenever I teach the algorithms class, I award extra credit points to the first student to post an explanation and correction of any error in the lecture notes to the course newsgroup. Obviously, the number of extra credit points depends on the severity of the error and the quality of the correction. If I’m not teaching the course, encourage your instructor to set up a similar extra-credit scheme, and forward the bug reports to Steve me! Of course, any other feedback is also welcome! Enjoy! — Jeff It is traditional for the author to magnanimously accept the blame for whatever deficiencies remain. I don’t. Any errors, deficiencies, or problems in this book are somebody else’s fault, but I would appreciate knowing about them so as to determine who is to blame. — Steven S. Skiena, The Algorithm Design Manual (1997) c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. Algorithms Lecture 0: Introduction We should explain, before proceeding, that it is not our object to consider this program with reference to the actual arrangement of the data on the Variables of the engine, but simply as an abstract question of the nature and number of the operations required to be perfomed during its complete solution. — Ada Augusta Byron King, Countess of Lovelace, translator’s notes for Luigi F. Menabrea, “Sketch of the Analytical Engine invented by Charles Babbage, Esq.” (1843) You are right to demand that an artist engage his work consciously, but you confuse two different things: solving the problem and correctly posing the question. — Anton Chekhov, in a letter to A. S. Suvorin (October 27, 1888) The more we reduce ourselves to machines in the lower things, the more force we shall set free to use in the higher. — Anna C. Brackett, The Technique of Rest (1892) The moment a man begins to talk about technique that’s proof that he is fresh out of ideas. — Raymond Chandler 0 Introduction 0.1 What is an algorithm? An algorithm is an explicit, precise, unambiguous, mechanically-executable sequence of elementary instructions. For example, here is an algorithm for singing that annoying song ‘99 Bottles of Beer on the Wall’, for arbitrary values of 99: B OTTLESOFBEER(n): For i ← n down to 1 Sing “i bottles of beer on the wall, i bottles of beer,” Sing “Take one down, pass it around, i − 1 bottles of beer on the wall.” Sing “No bottles of beer on the wall, no bottles of beer,” Sing “Go to the store, buy some more, n bottles of beer on the wall.” The word ‘algorithm’ does not derive, as algorithmophobic classicists might guess, from the Greek root algos (αλγoς), meaning ‘pain’. Rather, it is a corruption of the name of the 9th century Persian ¯ ¯ ¯¯ ¯ ¯ mathematician Abu ’Abd Allah Muhammad ibn Musa al-Khwarizm¯ 1 Al-Khwarizm¯ is perhaps best ı. ı . ¯ ¯ known as the writer of the treatise Al-Kitab al-mukhtasar f¯ ¯ ab al-˘abr wa’l-muqabala2 , from which ıhıs¯ g . the modern word algebra derives. The word algorithm is a corruption of the older word algorism (by false connection to the Greek arithmos (αριθ µoς), meaning ‘number’, and perhaps the aforementioned αλγoς), used to describe the modern decimal system for writing and manipulating numbers—in ¯ particular, the use of a small circle or sifr to represent a missing quantity—which al-Khwarizm¯ brought ı into Persia from India. Thanks to the efforts of the medieval Italian mathematician Leonardo of Pisa, better known as Fibonacci, algorism began to replace the abacus as the preferred system of commercial calculation3 in Europe in the late 12th century, although it was several more centuries before cyphers became truly ubiquitous. (Counting boards were used by the English and Scottish royal exchequers well into the 1600s.) Thus, until very recently, the word algorithm referred exclusively to pencil-and-paper 1 ¯ ¯ ‘Mohammad, father of Adbdulla, son of Moses, the Kwarizmian’. Kwarizm is an ancient city, now called Khiva, in the Khorezm Province of Uzbekistan. 2 ‘The Compendious Book on Calculation by Completion and Balancing’. 3 from the Latin word calculus, meaning literally ‘small rock’, referring to the stones on a counting board, or abacus 1 Algorithms Lecture 0: Introduction methods for numerical calculations. People trained in the reliable execution of these methods were called—you guessed it—computers. 0.2 A Few Simple Examples Multiplication by compass and straightedge. Although they have only been an object of formal study for a few decades, algorithms have been with us since the dawn of civilization, for centuries before ¯ Al-Khwarizm¯ and Fibonacci popularized the cypher. Here is an algorithm, popularized (but almost ı certainly not discovered) by Euclid about 2500 years ago, for multiplying or dividing numbers using a ruler and compass. The Greek geometers represented numbers using line segments of the appropriate length. In the pseudo-code below, CIRCLE( p, q) represents the circle centered at a point p and passing through another point q. Hopefully the other instructions are obvious.4 Z 〈〈Construct the line perpendicular to and passing through P .〉〉 RIGHTANGLE( , P ): Choose a point A ∈ A, B ← INTERSECT(CIRCLE( P, A), ) C , D ← INTERSECT(CIRCLE(A, B ), CIRCLE(B , A)) return LINE(C , D) 〈〈Construct a point Z such that |AZ | = |AC ||AD|/|AB |.〉〉 MULTIPLYORDIVIDE(A, B , C , D): α ← RIGHTANGLE(LINE(A, C ), A) E ← INTERSECT(CIRCLE(A, B ), α) F ← INTERSECT(CIRCLE(A, D), α) β ← RIGHTANGLE(LINE( E , C ), F ) γ ← RIGHTANGLE(β , F ) return INTERSECT(γ, LINE(A, C )) C D B β α F A E γ Multiplying or dividing using a compass and straightedge. This algorithm breaks down the difficult task of multiplication into a series of simple primitive operations: drawing a line between two points, drawing a circle with a given center and boundary point, and so on. These primitive steps are quite non-trivial to execute on a modern digital computer, but this algorithm wasn’t designed for a digital computer; it was designed for the Platonic Ideal Classical Greek Mathematician, wielding the Platonic Ideal Compass and the Platonic Ideal Straightedge. In this example, Euclid first defines a new primitive operation, constructing a right angle, by (as modern programmers would put it) writing a subroutine. Multiplication by duplation and mediation. Here is an even older algorithm for multiplying large numbers, sometimes called (Russian) peasant multiplication. A variant of this method was copied into the Rhind papyrus by the Egyptian scribe Ahmes around 1650 BC, from a document he claimed was (then) about 350 years old. This was the most common method of calculation by Europeans before Fibonacci’s introduction of Arabic numerals; it was still being used in Russia, along with the Julian Euclid and his students almost certainly drew their constructions on an αβαξ, a table covered in sand (or very small rocks). Over the next several centuries, the Greek abax evolved into the medieval European abacus. 4 2 Algorithms Lecture 0: Introduction calendar, well into the 20th century. This algorithm was also commonly used by early digital computers that did not implement integer multiplication directly in hardware. x y 123 61 30 15 7 3 1 PEASANTMULTIPLY( x , y ): prod ← 0 while x > 0 if x is odd prod ← prod + y x ← x /2 y← y+y return p +456 +912 1824 +3648 +7296 +14592 +29184 prod 0 = 456 = 1368 = 5016 = 12312 = 26904 = 56088 The peasant multiplication algorithm breaks the difficult task of general multiplication into four simpler operations: (1) determining parity (even or odd), (2) addition, (3) duplation (doubling a number), and (4) mediation (halving a number, rounding down).5 Of course a full specification of this algorithm requires describing how to perform those four ‘primitive’ operations. Peasant multiplication requires (a constant factor!) more paperwork to execute by hand, but the necessary operations are easier (for humans) to remember than the 10 × 10 multiplication table required by the American grade school algorithm.6 The correctness of peasant multiplication follows from the following recursive identity, which holds for any non-negative integers x and y : if x = 0 0 x·y= x /2 · ( y + y ) A bad example. if x is even x /2 · ( y + y ) + y if x is odd Consider “Martin’s algorithm”:7 BECOMEAMILLIONAIREANDNEVERPAYTAXES: Get a million dollars. Don’t pay taxes. If you get caught, Say “I forgot.” Pretty simple, except for that first step; it’s a doozy. A group of billionaire CEOs would consider this an algorithm, since for them the first step is both unambiguous and trivial, but for the rest of us poor slobs, Martin’s procedure is too vague to be considered an algorithm. On the other hand, this is a perfect example of a reduction—it reduces the problem of being a millionaire and never paying taxes to the ‘easier’ problem of acquiring a million dollars. We’ll see reductions over and over again in this class. As hundreds of businessmen and politicians have demonstrated, if you know how to solve the easier problem, a reduction tells you how to solve the harder one. 5 The version of this algorithm actually used in ancient Egypt does not use mediation or parity, but it does use comparisons. To avoid halving, the algorithm pre-computes two tables by repeated doubling: one containing all the powers of 2 not exceeding x , the other containing the same powers of 2 multiplied by y . The powers of 2 that sum to x are then found by greedy subtraction, and the corresponding entries in the other table are added together to form the product. 6 American school kids learn a variant of the lattice multiplication algorithm developed by Indian mathematicians and described by Fibonacci in Liber Abaci. The two algorithms are equivalent if the input numbers are represented in binary. 7 S. Martin, “You Can Be A Millionaire”, Saturday Night Live, January 21, 1978. Appears on Comedy Is Not Pretty, Warner Bros. Records, 1979. 3 Algorithms Lecture 0: Introduction Martin’s algorithm, like many of our previous examples, is not the kind of algorithm that computer scientists are used to thinking about, because it is phrased in terms of operations that are difficult for computers to perform. In this class, we’ll focus (almost!) exclusively on algorithms that can be reasonably implemented on a computer. In other words, each step in the algorithm must be something that either is directly supported by common programming languages (such as arithmetic, assignments, loops, or recursion) or is something that you’ve already learned how to do in an earlier class (like sorting, binary search, or depth first search). Congressional apportionment. Here is another good example of an algorithm that comes from outside the world of computing. Article I, Section 2 of the US Constitution requires that Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers. . . . The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative. . . . Since there are a limited number of seats available in the House of Representatives, exact proportional representation is impossible without either shared or fractional representatives, neither of which are legal. As a result, several different apportionment algorithms have been proposed and used to round the fractional solution fairly. The algorithm actually used today, called the Huntington-Hill method or the method of equal proportions, was first suggested by Census Bureau statistician Joseph Hill in 1911, refined by Harvard mathematician Edward Huntington in 1920, adopted into Federal law (2 U.S.C. §§2a and 2b) in 1941, and survived a Supreme Court challenge in 1992.8 The input array P [1 .. n] stores the populations of the n states, and R is the total number of representatives. Currently, n = 50 and R = 435. APPORTIONCONGRESS( P [1 .. n], R): H ← NEWMAXHEAP for i ← 1 to n r [i ] ← 1 INSERT H , i , P [i ]/ 2 R←R−n while R > 0 s ← EXTRACTMAX(H ) r [s ] ← r [s ] + 1 INSERT H , i , P [i ]/ R←R−1 r [i ]( r [i ] + 1) return r [1 .. n] Note that this description assumes that you know how to implement a max-heap and its basic operations NEWMAXHEAP, INSERT, and EXTRACTMAX. (The actual law doesn’t make those assumptions, of course.) Moreover, the correctness of the algorithm doesn’t depend at all on how these operations are implemented. The Census Bureau implements the max-heap as an unsorted array stored in a column of an Excel spreadsheet; you should have learned a more efficient solution in your undergraduate data structures class. 8 Overruling an earlier ruling by a federal district court, the Supreme Court unanimously held that any apportionment method adopted in good faith by Congress is constitutional (United States Department of Commerce v. Montana). The current congressional apportionment algorithm is described in gruesome detail at the U.S. Census Department web site http://www.census.gov/population/www/censusdata/apportionment/computing.html. A good history of the apportionment problem can be found at http://www.thirty-thousand.org/pages/Apportionment.htm. A report by the Congressional Research Service describing various apportionment methods is available at http://www.rules.house.gov/archives/RL31074.pdf. 4 Algorithms 0.3 Lecture 0: Introduction Writing down algorithms Computer programs are concrete representations of algorithms, but algorithms are not programs; they should not be described in a particular programming language. The whole point of this course is to develop computational techniques that can be used in any programming language.9 The idiosyncratic syntactic details of C, Java, Python, Ruby, Erlang, OcaML, Scheme, Visual Basic, Smalltalk, Javascript, Forth, TEX, COBOL, Intercal, or Brainfuck10 are of little or no importance in algorithm design, and focusing on them will only distract you from what’s really going on.11 What we really want is closer to what you’d write in the comments of a real program than the code itself. On the other hand, a plain English prose description is usually not a good idea either. Algorithms have a lot of structure—especially conditionals, loops, and recursion—that are far too easily hidden by unstructured prose. Like any natural languags, English is full of ambiguities, subtleties, and shades of meaning, but algorithms must be described as accurately as possible. Finally and more seriously, many people have a tendency to describe loops informally: “Do this first, then do this second, and so on.” As anyone who has taken one of those ‘What comes next in this sequence?’ tests already knows, specifying what happens in the first few iterations of a loop doesn’t say much about what happens in later iterations.12 Phrases like ‘and so on’ or ‘repeat this for all n’ are good indicators that the algorithm should have been described in terms of loops or recursion, and the description should have specified a generic iteration of the loop. Similarly, the appearance of the phrase ‘and so on’ in a proof almost always means the proof should have been done by induction. The best way to write down an algorithm is using pseudocode. Pseudocode uses the structure of formal programming languages and mathematics to break the algorithm into one-sentence steps, but those sentences can be written using mathematics, pure English, or an appropriate mixture of the two. Exactly how to structure the pseudocode is a personal choice, but the overriding goal should be clarity and precision. Here are the guidelines I follow: • Use standard imperative programming keywords (if/then/else, while, for, repeat/until, case, return) and notation (variable ← value, Array[index], function(argument), bigger > smaller, etc.). Keywords should be standard English words: write ‘else if’ instead of ‘elif’. • Use standard mathematical notation for standard mathematical stuff. For example, write x and a b and π instead of sqrt( x ) and power(a, b) and pi. (One exception: never use ∀ to represent a for-loop!) • Avoid mathematical notation if English is clearer. For example, ‘Insert a into X ’ is often preferable to INSERT(X , a) or X ← X ∪ {a}. 9 See http://www.ionet.net/~timtroyr/funhouse/beer.html for implementations of the B OTTLESOFBEER algorithm in over 200 different programming languages. 10 Brainfuck is the well-deserved name of a programming language invented by Urban Müller in 1993. Brainfuck programs are written entirely using the punctuation characters <>+-,., each representing a different operation (roughly: shift left, shift right, increment, decrement, input, output, begin loop, end loop). See http://esolangs.org/wiki/Brainfuck for a complete language description; links to several interpreters, compilers, and sample programs; and lots of related shit. 11 This is, of course, a matter of religious conviction. Linguists argue incessantly over the Sapir-Whorf hypothesis, which states (more or less) that people think only in the categories imposed by their languages. According to an extreme formulation of this principle, some concepts in one language simply cannot be understood by speakers of other languages, not just because of technological advancement—How would you translate ‘jump the shark’ or ‘blog’ into Aramaic?—but because of inherent structural differences between languages and cultures. For a more skeptical view, see Steven Pinker’s The Language Instinct. There is admittedly some strength to this idea when applied to different programming paradigms. (What’s the Y combinator, again? How do templates work? What’s an Abstract Factory?) Fortunately, those differences are generally too subtle to have much impact in this class. 12 See http://www.research.att.com/~njas/sequences/. 5 Algorithms Lecture 0: Introduction • Indent everything carefully and consistently; the block structure should be visible from across the room. This is especially important for nested loops and conditionals. Don’t use unnecessary syntactic sugar (like braces or begin/end tags). • Don’t typeset keywords in a different font or style. Changing type style emphasizes the keywords, making the reader think the syntactic sugar is important. It isn’t! • Each statement should fit on one line, and each line should contain either exactly one statement or exactly one structuring element (for, while, if). (I sometimes make an exception for short and similar statements like i ← i + 1; j ← j − 1; k ← 0.) • Use short, mnemonic algorithm and variable names. Absolutely never use pronouns! A good description of an algorithm reveals the internal structure, hides irrelevant details, and can be implemented easily and correctly by any competent programmer in their favorite programming language, even if they don’t understand why the algorithm works. Good pseudocode, like good code, makes the algorithm much easier to understand and analyze; it also makes mistakes much easier to spot. The algorithm descriptions in these lecture notes are (hopefully) good examples of what we want to see on your homeworks and exams. 0.4 Analyzing algorithms It’s not enough just to write down an algorithm and say ‘Behold!’ We also need to convince ourselves (and our graders) that the algorithm does what it’s supposed to do, and that it does it efficiently. Correctness: In some application settings, it is acceptable for programs to behave correctly most of the time, on all ‘reasonable’ inputs. Not in this class; we require algorithms that are correct for all possible inputs. Moreover, we must prove that they’re correct; trusting our instincts, or trying a few test cases, isn’t good enough.13 Sometimes correctness is fairly obvious, especially for algorithms you’ve seen in earlier courses. On the other hand, ‘obvious’ is all too often a synonym for ‘wrong’. Many of the algorithms we will discuss in this course will require some extra work to prove. Correctness proofs almost always involve induction. We like induction. Induction is our friend.14 But before we can formally prove that our algorithm does what we want it to do, we have to formally state what we want it to do! Usually problems are given to us in real-world terms, not with formal mathematical descriptions. It’s up to us, the algorithm designers, to restate these problems in terms of mathematical objects that we can prove things about—numbers, arrays, lists, graphs, trees, and so on. We also need to determine if the problem statement makes any hidden assumptions, and state those assumptions explicitly. (For example, in the song “n Bottles of Beer on the Wall”, n is always a positive integer.) Restating the problem formally is not only required for proofs; it is also one of the best ways to really understand what the problems is asking for. The hardest part of answering any question is figuring out the right way to ask it! It is important to remember the distinction between a problem and an algorithm. A problem is a task to perform, like “Compute the square root of x ” or “Sort these n numbers” or “Keep n algorithms students awake for t minutes”. An algorithm is a set of instructions to follow if you want to accomplish this task. The same problem may have hundreds of different algorithms; the same algorithm may solve hundreds of different problems. 13 14 I say we take off and nuke the entire site from orbit. It’s the only way to be sure. If induction is not your friend, you will have a hard time in this course. 6 Algorithms Lecture 0: Introduction Running time: The most common way of ranking different algorithms for the same problem is by how fast they run. Ideally, we want the fastest possible algorithm for our problem. In many application settings, it is acceptable for programs to run efficiently most of the time, on all ‘reasonable’ inputs. Not in this class; we require algorithms that always run efficiently, even in the worst case. But how do we measure running time? As a specific example, how long does it take to sing the song B OTTLESOFBEER(n)? This is obviously a function of the input value n, but it also depends on how quickly you can sing. Some singers might take ten seconds to sing a verse; others might take twenty. Technology widens the possibilities even further. Dictating the song over a telegraph using Morse code might take a full minute per verse. Downloading an mp3 over the Web might take a tenth of a second per verse. Duplicating the mp3 in a computer’s main memory might take only a few microseconds per verse. What’s important here is how the singing time changes as n grows. Singing B OTTLESOFBEER(2n) takes about twice as long as singing B OTTLESOFBEER(n), no matter what technology is being used. This is reflected in the asymptotic singing time Θ(n). We can measure time by counting how many times the algorithm executes a certain instruction or reaches a certain milestone in the ‘code’. For example, we might notice that the word ‘beer’ is sung three times in every verse of B OTTLESOFBEER, so the number of times you sing ‘beer’ is a good indication of the total singing time. For this question, we can give an exact answer: B OTTLESOFBEER(n) uses exactly 3n + 3 beers. There are plenty of other songs that have non-trivial singing time. This one is probably familiar to most English-speakers: NDAYSOFCHRISTMAS(gifts[2 .. n]): for i ← 1 to n Sing “On the i th day of Christmas, my true love gave to me” for j ← i down to 2 Sing “ j gifts[ j ]” if i > 1 Sing “and” Sing “a partridge in a pear tree.” The input to NDAYSOFCHRISTMAS is a list of n − 1 gifts. It’s quite easy to show that the singing time is n Θ(n2 ); in particular, the singer mentions the name of a gift i =1 i = n(n + 1)/2 times (counting the partridge in the pear tree). It’s also easy to see that during the first n days of Christmas, my true love n i gave to me exactly i =1 j =1 j = n(n + 1)(n + 2)/6 = Θ(n3 ) gifts. Other songs that take quadratic time to sing are “Old MacDonald had a Farm”, “There Was an Old Lady Who Swallowed a Fly”, “The House that Jack Built”, “Green Grow the Rushes O”, “Eh, Compare!”, “Alouette”, “Echad Mi Yodea”, “Chad Gadya”, and “Ist das nicht ein Schnitzelbank?”15 For further details, consult your nearest preschooler. OLDMACDONALD(animals[1 .. n], noise[1 .. n]): for i ← 1 to n Sing “Old MacDonald had a farm, E I E I O” Sing “And on this farm he had some animals[i ], E I E I O” Sing “With a noise[i ] noise[i ] here, and a noise[i ] noise[i ] there” Sing “Here a noise[i ], there a noise[i ], everywhere a noise[i ] noise[i ]” for j ← i − 1 down to 1 Sing “noise[ j ] noise[ j ] here, noise[ j ] noise[ j ] there” Sing “Here a noise[ j ], there a noise[ j ], everywhere a noise[ j ] noise[ j ]” Sing “Old MacDonald had a farm, E I E I O.” 15 Wakko: Ist das nicht Otto von Schnitzelpusskrankengescheitmeyer? Yakko and Dot: Ja, das ist Otto von Schnitzelpusskrankengescheitmeyer!! 7 Algorithms Lecture 0: Introduction ALOUETTE(lapart[1 .. n]): Chantez « Alouette, gentille alouette, alouette, je te plumerais. » pour tout i de 1 á n Chantez « Je te plumerais lapart[i ]. Je te plumerais lapart[i ]. » pour tout j de i − 1 á bas á 1 Chantez « Et lapart[ j ] ! Et lapart[ j ] ! » Chantez « Ooooooo! » Chantez « Alouette, gentille alluette, alouette, je te plumerais. » For a slightly more complicated example, consider the algorithm APPORTIONCONGRESS. Here the running time obviously depends on the implementation of the max-heap operations, but we can certainly bound the running time as O(N + RI + (R − n) E ), where N is the time for a NEWMAXHEAP, I is the time for an INSERT, and E is the time for an EXTRACTMAX. Under the reasonable assumption that R > 2n (on average, each state gets at least two representatives), this simplifies to O(N + R( I + E )). The Census Bureau uses an unsorted array of size n, for which N = I = Θ(1) (since we know a priori how big the array is), and E = Θ(n), so the overall running time is Θ(Rn). This is fine for the federal government, but if we want to be more efficient, we can implement the heap as a perfectly balanced n-node binary tree (or a heap-ordered array). In this case, we have N = Θ(1) and I = R = O(log n), so the overall running time is Θ(R log n). Sometimes we are also interested in other computational resources: space, randomness, page faults, inter-process messages, and so forth. We use the same techniques to analyze those resources as we use for running time. 0.5 A Longer Example: Stable Marriage Every year, thousands of new doctors must obtain internships at hospitals around the United States. During the first half of the 20th century, competition among hospitals for the best doctors led to earlier and earlier offers of internships, sometimes as early as the second year of medical school, along with tighter deadlines for acceptance. In the 1940s, medical schools agreed not to release information until a common date during their students’ fourth year. In response, hospitals began demanding faster decisions. By 1950, hospitals would regularly call doctors, offer them an internship, and demand an immediate response. Interns were forced to gamble if their third-choice hospital called first—accept and risk losing a better opportunity later, or reject and risk having no position at all. Finally, a central clearinghouse for internship assignments, now called the National Resident Matching Program, was established in the early 1950s. Each year, doctors submit a ranked list of all hospitals where they would accept an internship, and each hospital submits a ranked list of doctors they would accept as interns. The NRMP then computes an assignment of interns to hospitals that satisfies the following stability requirement. For simplicity, let’s assume that there are n doctors and n hospitals; each hospital offers exactly one internship; each doctor ranks all hospitals and vice versa; and finally, there are no ties in the doctors’ and hospitals’ rankings.16 We say that a matching of doctors to hospitals is unstable if there are two doctors α and β and two hospitals A and B , such that • α is assigned to A, and β is assigned to B ; • α prefers B to A, and B prefers α to β . In other words, α and B would both be happier with each other than with their current assignment. The goal of the Resident Match is a stable matching, in which no doctor or hospital has an incentive to cheat the system. At first glance, it is not clear that a stable matching exists! 16 In reality, most hospitals offer multiple internships, Each doctor ranks only a subset of the hospitals and vice versa, and there are typically more internships than interested doctors. And then it starts getting complicated. 8 Algorithms Lecture 0: Introduction In 1952, the NRMP adopted the “Boston Pool” algorithm to assign interns, so named because it had been previously used by a regional clearinghouse in the Boston area. The algorithm is often inappropriately attributed to David Gale and Lloyd Shapley, who formally analyzed the algorithm and first proved that it computes a stable matching in 1962.17 The algorithm proceeds in rounds until every position has been filled. In each round: 1. Some unassigned hospital offers its position to the best doctor (according to the hospital’s preference list) who has not already rejected it. 2. Each doctor is always assigned to the best hospital (according to the doctor’s preference list) that has made her an offer so far. Thus, whenever a doctor receives an offer that she likes more than her current assignment, her assignment changes. For example, suppose there are three doctors α, β , γ, following preference lists: αβγδAB AABDδβ BDABγδ CCCCβα DBDAαγ δ, and three hospitals A, B , C , D with the C δ α β γ D γ β α δ The algorithm might proceed as follows: 1. A makes an offer to δ. 2. B makes an offer to β . 3. C makes an offer to δ, who rejects her earlier offer from A. 4. D makes an offer to γ. (From this point on, because there is only one unmatched hospital, the algorithm has no more choices.) 5. A makes an offer to γ, who rejects her earlier offer from D. 6. D makes an offer to β , who rejects her earlier offer from B . 7. B makes an offer to δ, who rejects her earlier offer from C . 8. C makes an offer to α. At this point we have the assignment (α, C ), (β , D), (γ, A), (δ, B ). You can verify by brute force that this matching is stable, despite the fact that no doctor was hired by her favorite hospital, and no hospital hired its favorite doctor. Analyzing the algorithm is straightforward. Since each hospital makes an offer to each doctor at most once, the algorithm requires at most n2 rounds. In an actual implementation, each doctor and hospital can be identified by a unique integer between 1 and n, and the preference lists can be represented as two arrays DocPref[1 .. n][1 .. n] and HosPref[1 .. n][1 .. n], where DocPref[α][ r ] represents the r th hospital 17 Gale and Shapely used the metaphor of college admissions. The “Gale-Shapely algorithm” is a prime instance of Stigler’s Law of Eponymy: No scientific discovery is named after its original discoverer. In his 1980 paper that gives the law its name, the statistician Stephen Stigler claimed that this law was first proposed by sociologist Robert K. Merton, although similar sentiments were previously attributed to Vladimir Arnol’d in the 1970’s (“Discoveries are rarely attributed to the correct person.”), Carl Boyer in 1968 (“Clio, the muse of history, often is fickle in attaching names to theorems!”), Alfred North Whitehead in 1917 (“Everything of importance has been said before by someone who did not discover it.”), and even Stephen’s father George Stigler in 1966 (“If we should ever encounter a case where a theory is named for the correct man, it will be noted.”)! The law was dubbed the Zeroth Theorem of the History of Science by historian Ernst Peter Fischer in 2006 (“[E]ine Entdeckung (Regel, Gesetzmässigkeit, Einsicht), die nach einer Person benannt ist, nicht von dieser Person herrührt.”) . 9 Algorithms Lecture 0: Introduction in doctor α’s preference list, and HosPref[A][ r ] represents the r th doctor in hospital A’s preference list. With the input in this form, the Boston Pool algorithm can be implemented to run in O(n2 ) time; we leave the details as an exercise for the reader. A somewhat harder exercise is to prove that there are inputs (and choices of who makes offers when) that force Ω(n2 ) rounds before the algorithm terminates. But why is it correct? Gale and Shapely prove that the Boston Pool algorithm always computes a stable matching as follows. The algorithm must terminate, because each hospital makes an offer to each doctor at most once. When the algorithm terminates, every internship has been filled. Now suppose in the final matching, doctor α is assigned to hospital A but prefers B . Since every doctor accepts the best offer she receives, α received no offer she liked more than A. In particular, B never made an offer to α. On the other hand, B made offers to every doctor they like more than β . Thus, B prefers β to α, and so there is no instability. Surprisingly, the correctness of the algorithm does not depend on which hospital makes its offer in which round. In fact, there is a stronger sense in which the order of offers doesn’t matter—no matter which unassigned hospital makes an offer in each round, the algorithm always computes the same matching! Let’s say that α is a feasible doctor for A if there is a stable matching that assigns doctor α to hospital A, and let best(A) be the highest-ranked feasible doctor on A’s preference list. Lemma 1. The Boston Pool algorithm assigns best(A) to A, for every hospital A. Proof: In the final matching, no hospital A is assigned a doctor they prefer to best(A), because that would create an instability (by definition of ‘feasible’ and ‘best’). A hospital A can only be assigned a doctor they like less than best(A) if best(A) rejects their offer. So consider the first round where some hospital A is rejected by its best choice α = best(A). In that round, α got an offer from another hospital B that α ranks higher than A. Now, B must rank α at least as highly as its best choice best(B ), because this is the first round where a hospital is rejected by its best choice. Thus, B must rank α higher than any doctor that is feasible for B . Now consider the stable matching where α is assigned to A. On the one hand, α prefers B to A. On the other hand, because this is a stable matching, B is assigned a doctor β that is feasible for B , which implies that B prefers α to β . But this means the matching is unstable, and we have a contradiction. In other words, from the hospitals’ point of view, the Boston Pool algorithm computes the best possible stable matching. It turns out that this is also the worst possible matching from the doctors’ viewpoint! Let worst(α) be the lowest-ranked feasible hospital on doctor α’s preference list. Lemma 2. The Boston Pool algorithm assigns α to worst(α), for every doctor α. Proof: Suppose the Boston Pool algorithm assigns doctor α to hospital A; the previous lemma implies that α = best(A). To prove the lemma, we need to show that A = worst(α). Consider an arbitrary stable matching where A is not matched with α but with another doctor β . Then A must prefer α to β . Since this matching is stable, α must therefore prefer her current assignment to A. This argument works for any stable assignment, so α prefers every other feasible match to A; in other words, A = worst(α). In 1998, the National Residency Matching Program reversed its matching algorithm, so that potential residents offer to work for hospitals, and each hospital accepts its best offer. Thus, the new algorithm computes the best possible stable matching for the doctors, and the worst possible stable matching for the hospitals. As far as I know, the precise effect of this change on the patients is an open problem. 10 Algorithms 0.6 Lecture 0: Introduction Why are we here, anyway? This class is ultimately about learning two skills that are crucial for all computer scientists. 1. Reasoning: How to think about abstract computation. 2. Communication: How to talk about abstract computation. The first goal of this course is to help you develop algorithmic intuition. How do various algorithms really work? When you see a problem for the first time, how should you attack it? How do you tell which techniques will work at all, and which ones will work best? How do you judge whether one algorithm is better than another? How do you tell whether you have the best possible solution? These are not easy questions. Anyone who says differently is selling something. Our second main goal is to help you develop algorithmic language. It’s not enough just to understand how to solve a problem; you also have to be able to explain your solution to somebody else. I don’t mean just how to turn your algorithms into working code—despite what many students (and inexperienced programmers) think, ‘somebody else’ is not just a computer. Nobody programs alone. Code is read far more often than it is written, or even compiled. Perhaps more importantly in the short term, explaining something to somebody else is one of the best ways to clarify your own understanding. As Albert Einstein (or was it Richard Feynman?) apocryphally put it, “You do not really understand something unless you can explain it to your grandmother." Along the way, you’ll pick up a bunch of algorithmic facts—mergesort runs in Θ(n log n) time; the amortized time to search in a splay tree is O(log n); greedy algorithms usually don’t produce optimal solutions; the traveling salesman problem is NP-hard—but these aren’t the point of the course. You can always look up mere facts in a textbook or on the web, provided you have enough intuition and experience to know what to look for. That’s why we let you bring cheat sheets to the exams; we don’t want you wasting your study time trying to memorize all the facts you’ve seen. You’ll also practice a lot of algorithm design and analysis skills—finding useful (counter)examples, developing induction proofs, solving recurrences, using big-Oh notation, using probability, giving problems crisp mathematical descriptions, and so on. These skills are incredibly useful, and it’s impossible to develop good intuition and good communication skills without them, but they aren’t the main point of the course either. At this point in your educational career, you should be able to pick up most of those skills on your own, once you know what you’re trying to do. Unfortunately, there is no systematic procedure—no algorithm—to determine which algorithmic techniques are most effective at solving a given problem, or finding good ways to explain, analyze, optimize, or implement a given algorithm. Like many other activities (music, writing, juggling, acting, martial arts, sports, cooking, programming, teaching, etc.), the only way to master these skills is to make them your own, through practice, practice, and more practice. You can only develop good problemsolving skills by solving problems. You can only develop good communication skills by communicating. Good intuition is the product of experience, not its replacement. We can’t teach you how to do well in this class. All we can do (and what we will do) is lay out some fundamental tools, show you how to use them, create opportunities for you to practice with them, and give you honest feedback, based on our own hard-won experience and intuition. The rest is up to you. Good algorithms are extremely useful, elegant, surprising, deep, even beautiful. But most importantly, algorithms are fun!! I hope this course will inspire at least some you to come play! 11 Algorithms Lecture 0: Introduction Boethius the algorist versus Pythagoras the abacist. from Margarita Philosophica by Gregor Reisch (1503) 12 Algorithms Lecture 0: Introduction Exercises 0. Describe and analyze an algorithm that determines, given a legal arrangement of standard pieces on a standard chess board, which player will win at chess from the given starting position if both players play perfectly. [Hint: There is a one-line solution!] 1. The traditional Devonian/Cornish drinking song “The Barley Mow” has the following pseudolyrics18 , where container[i ] is the name of a container that holds 2i ounces of beer. One version of the song uses the following containers: nipperkin, gill pot, half-pint, pint, quart, pottle, gallon, half-anker, anker, firkin, half-barrel, barrel, hogshead, pipe, well, river, and ocean. (Every container in this list is twice as big as its predecessor, except that a firkin is actually 2.25 ankers, and the last three units are just silly.) BARLEYMOW(n): “Here’s a health to the barley-mow, my brave boys,” “Here’s a health to the barley-mow!” “We’ll drink it out of the jolly brown bowl,” “Here’s a health to the barley-mow!” “Here’s a health to the barley-mow, my brave boys,” “Here’s a health to the barley-mow!” for i ← 1 to n “We’ll drink it out of the container[i ], boys,” “Here’s a health to the barley-mow!” for j ← i downto 1 “The container[ j ],” “And the jolly brown bowl!” “Here’s a health to the barley-mow!” “Here’s a health to the barley-mow, my brave boys,” “Here’s a health to the barley-mow!” (a) Suppose each container name container[i ] is a single word, and you can sing four words a second. How long would it take you to sing BARLEYMOW(n)? (Give a tight asymptotic bound.) (b) If you want to sing this song for n > 20, you’ll have to make up your own container names. To avoid repetition, these names will get progressively longer as n increases19 . Suppose container[n] has Θ(log n) syllables, and you can sing six syllables per second. Now how long would it take you to sing BARLEYMOW(n)? (Give a tight asymptotic bound.) (c) Suppose each time you mention the name of a container, you actually drink the corresponding amount of beer: one ounce for the jolly brown bowl, and 2i ounces for each container[i ]. Assuming for purposes of this problem that you are at least 21 years old, exactly how many ounces of beer would you drink if you sang BARLEYMOW(n)? (Give an exact answer, not just an asymptotic bound.) 2. Describe and analyze the Boston Pool stable matching algorithm in more detail, so that the worst-case running time is O(n2 ), as claimed earlier in the notes. 18 19 Pseudolyrics are to lyrics as pseudocode is to code. “We’ll drink it out of the hemisemidemiyottapint, boys!” 13 Algorithms Lecture 0: Introduction 3. Consider a generalization of the stable matching problem, where some doctors do not rank all hospitals and some hospitals do not rank all doctors, and a doctor can be assigned to a hospital only if each appears in the other’s preference list. In this case, there are three additional unstable situations: • A hospital prefers an unmatched doctor to its assigned match. • A doctor prefers an unmatched hospital to its assigned match. • An unmatched doctor and an unmatched hospital each appear in the other’s preference list. Describe and analyze an efficient algorithm that computes a stable matching in this setting. Note that a stable matching may leave some doctors and hospitals unmatched, even though their preference lists are non-empty. For example, if every doctor lists Harvard as their only acceptable hospital, and every hospital lists Doogie as their only acceptable intern, then only Doogie and Harvard will be matched. 4. Recall that the input to the Huntington-Hill apportionment algorithm APPORTIONCONGRESS is an array P [1 .. n], where P [i ] is the population of the i th state, and an integer R, the total number of representatives to be allotted. The output is an array r [1 .. n], where r [i ] is the number of representatives allotted to the i th state by the algorithm. n Let P = i =1 P [i ] denote the total population of the country, and let ri∗ = R · P [i ]/ P denote the ideal number of representatives for the i th state. (a) Prove that r [i ] ≥ ri∗ for all i . (b) Describe and analyze an algorithm that computes exactly the same congressional apportionment as APPORTIONCONGRESS in O(n log n) time. (Recall that the running time of APPORTIONCONGRESS depends on R, which could be arbitrarily larger than n.) (c) If a state’s population is small relative to the other states, its ideal number ri∗ of representatives could be close to zero; thus, tiny states are over-represented by the HuntingtonHill apportionment process. Surprisingly, this can also be true of very large states. Let α = (1 + 2)/2 ≈ 1.20710678119. Prove that for any > 0, there is an input to APPORTION∗ CONGRESS with maxi P [i ] = P [1], such that r [1] > (α − ) r1 . (d) Can you improve the constant α in the previous question? c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 14 Algorithms Lecture 1: Recursion Our life is frittered away by detail. Simplify, simplify. — Henry David Thoreau The control of a large force is the same principle as the control of a few men: it is merely a question of dividing up their numbers. — Sun Zi, The Art of War (c. 400 C.E.), translated by Lionel Giles (1910) Nothing is particularly hard if you divide it into small jobs. — Henry Ford 1 Recursion 1.1 Simplify and delegate Reduction is the single most common technique used in designing algorithms. Reducing one problem X to another problem (or set of problems) Y means to write an algorithm for X , using an algorithm or Y as a subroutine or black box. For example, the congressional apportionment algorithm described in Lecture 0 reduces the problem of apportioning Congress to the problem of maintaining a priority queue under the operations INSERT and EXTRACTMAX. Those data structure operations are black boxes; the apportionment algorithm does not depend on any specific implementation. Conversely, when we design a particular priority queue data structure, we typically neither know nor care how our data structure will be used. Whether or not the Census Bureau plans to use our code to apportion Congress is completely irrelevant to our design choices. As a general rule, when we design algorithms, we may not know—and we should not care—how the basic building blocks we use are implemented, or how your algorithm might be used as a basic building block to solve a bigger problem. A particularly powerful kind of reduction is recursion, which can be defined loosely as follows: • If the given instance of the problem is small or simple enough, just solve it. • Otherwise, reduce the problem to one or more simpler instances of the same problem. If the self-reference is confusing, it’s helpful to imagine that someone else is going to solve the simpler problems, just as you would assume for other types of reductions. Your only task is to simplify the original problem, or to solve it directly when simplification is either unnecessary or impossible. The Recursion Fairy will magically take care of the simpler subproblems.1 There is one mild technical condition that must be satisfied in order for any recursive method to work correctly, namely, that there is no infinite sequence of reductions to ‘simpler’ and ‘simpler’ subproblems. Eventually, the recursive reductions must stop with an elementary base case that can be solved by some other method; otherwise, the recursive algorithm will never terminate. This finiteness condition is almost always satisfied trivially, but we should always be wary of ‘obvious’ recursive algorithms that actually recurse forever.2 1 I used to refer to ‘elves’ instead of the Recursion Fairy, referring to the traditional fairy tale about an old shoemaker who leaves his work unfinished when he goes to bed, only to discover upon waking that elves have finished everything overnight. Someone more entheogenically experienced than I might recognize them as Terence McKenna’s ‘self-adjusting machine elves’. 2 All too often, ‘obvious’ is a synonym for ‘false’. 1 Algorithms 1.2 Lecture 1: Recursion Tower of Hanoi The Tower of Hanoi puzzle was first published by the mathematician François Éduoard Anatole Lucas in 1883, under the pseudonym ‘N. Claus (de Siam)’ (an anagram of ‘Lucas d’Amiens’). The following year, Henri de Parville described the puzzle with the following remarkable story:3 In the great temple at Benares beneath the dome which marks the centre of the world, rests a brass plate in which are fixed three diamond needles, each a cubit high and as thick as the body of a bee. On one of these needles, at the creation, God placed sixty-four discs of pure gold, the largest disc resting on the brass plate, and the others getting smaller and smaller up to the top one. This is the Tower of Bramah. Day and night unceasingly the priests transfer the discs from one diamond needle to another according to the fixed and immutable laws of Bramah, which require that the priest on duty must not move more than one disc at a time and that he must place this disc on a needle so that there is no smaller disc below it. When the sixty-four discs shall have been thus transferred from the needle on which at the creation God placed them to one of the other needles, tower, temple, and Brahmins alike will crumble into dust, and with a thunderclap the world will vanish. Of course, being good computer scientists, we read this story and immediately substitute n for the hardwired constant sixty-four. How can we move a tower of n disks from one needle to another, using a third needles as an occasional placeholder, never placing any disk on top of a smaller disk? The Tower of Hanoi puzzle The trick to solving this puzzle is to think recursively. Instead of trying to solve the entire puzzle all at once, let’s concentrate on moving just the largest disk. We can’t move it at the beginning, because all the other disks are covering it; we have to move those n − 1 disks to the third needle before we can move the nth disk. And then after we move the nth disk, we have to move those n − 1 disks back on top of it. So now all we have to figure out is how to. . . STOP!! That’s it! We’re done! We’ve successfully reduced the n-disk Tower of Hanoi problem to two instances of the (n − 1)-disk Tower of Hanoi problem, which we can gleefully hand off to the Recursion Fairy (or, to carry the original story further, to the junior monks at the temple). recursion recursion The Tower of Hanoi algorithm; ignore everything but the bottom disk 3 This English translation is from W. W. Rouse Ball and H. S. M. Coxeter’s book Mathematical Recreations and Essays. 2 Algorithms Lecture 1: Recursion Our algorithm does make one subtle but important assumption: There is a largest disk. In other words, our recursive algorithm works for any n ≥ 1, but it breaks down when n = 0. We must handle that base case directly. Fortunately, the monks at Benares, being good Buddhists, are quite adept at moving zero disks from one needle to another. The base case for the Tower of Hanoi algorithm. There is no spoon. While it’s tempting to think about how all those smaller disks get moved—in other words, what happens when the recursion is unfolded—it’s not necessary. In fact, for more complicated problems, unfolding the recursive calls is merely distracting. Our only task is to reduce the problem to one or more simpler instances, or to solve the problem directly if such a reduction is impossible. Our algorithm is trivially correct when n = 0. For any n ≥ 1, the Recursion Fairy correctly moves (or more formally, the inductive hypothesis implies that our algorithm correctly moves) the top n − 1 disks, so our algorithm is clearly correct. Here’s the recursive Hanoi algorithm in more typical pseudocode. HANOI(n, src, dst, tmp): if n > 0 HANOI(n, src, tmp, dst) move disk n from src to dst HANOI(n, tmp, dst, src) Let T (n) denote the number of moves required to transfer n disks—the running time of our algorithm. Our vacuous base case implies that T (0) = 0, and the more general recursive algorithm implies that T (n) = 2 T (n − 1) + 1 for any n ≥ 1. The annihilator method lets us quickly derive a closed form solution T (n ) = 2n − 1. In particular, moving a tower of 64 disks requires 264 − 1 = 18,446,744,073,709,551,615 individual moves. Thus, even at the impressive rate of one move per second, the monks at Benares will be at work for approximately 585 billion years before tower, temple, and Brahmins alike will crumble into dust, and with a thunderclap the world will vanish. 1.3 MergeSort Mergesort is one of the earliest algorithms proposed for sorting. According to Donald Knuth, it was suggested by John von Neumann as early as 1945. 1. Divide the array A[1 .. n] into two subarrays A[1 .. m] and A[m + 1 .. n], where m = n/2 . 2. Recursively mergesort the subarrays A[1 .. m] and A[m + 1 .. n]. 3. Merge the newly-sorted subarrays A[1 .. m] and A[m + 1 .. n] into a single sorted list. Input: Divide: Recurse: Merge: S S I A O O N E R R O G T T S I I I R L N N T M G G A N E E E O A Mergesort example. 3 X X G P A A L S M M M R P P P T L L X X Algorithms Lecture 1: Recursion The first step is completely trivial—we only need to compute the median index m—and we can delegate the second step to the Recursion Fairy. All the real work is done in the final step; the two sorted subarrays A[1 .. m] and A[m + 1 .. n] can be merged using a simple linear-time algorithm. Here’s a complete specification of the Mergesort algorithm; for simplicity, we separate out the merge step as a subroutine. MERGE(A[1 .. n], m): i ← 1; j ← m + 1 for k ← 1 to n if j > n B [k] ← A[i ]; else if i > m B [k] ← A[ j ]; else if A[i ] < A[ j ] B [k] ← A[i ]; else B [k] ← A[ j ]; MERGESORT(A[1 .. n]): if (n > 1) m ← n/ 2 MERGESORT(A[1 .. m]) MERGESORT(A[m + 1 .. n]) MERGE(A[1 .. n], m) i ← i+1 j ← j+1 i ← i+1 j ← j+1 for k ← 1 to n A[k] ← B [k] To prove that the algorithm is correct, we use our old friend induction. We can prove that MERGE is correct using induction on the total size of the two subarrays A[i .. m] and A[ j .. n] left to be merged into B [k .. n]. The base case, where at least one subarray is empty, is straightforward; the algorithm just copies it into B . Otherwise, the smallest remaining element is either A[i ] or A[ j ], since both subarrays are sorted, so B [k] is assigned correctly. The remaining subarrays—either A[i + 1 .. m] and A[ j .. n], or A[i .. m] and A[ j + 1 .. n]—are merged correctly into B [k + 1 .. n] by the inductive hypothesis.4 This completes the proof. Now we can prove MERGESORT correct by another round of straightforward induction. The base cases n ≤ 1 are trivial. Otherwise, by the inductive hypothesis, the two smaller subarrays A[1 .. m] and A[m + 1 .. n] are sorted correctly, and by our earlier argument, merged into the correct sorted output. What’s the running time? Since we have a recursive algorithm, we’re going to get a recurrence of some sort. MERGE clearly takes linear time, since it’s a simple for-loop with constant work per iteration. We get the following recurrence for MERGESORT: T (1) = O(1), T ( n) = T n/ 2 +T n/ 2 + O(n). Aside: Domain Transformations. Except for the floor and ceiling, this recurrence falls firmly into the “all levels equal” case of the recursion tree method, or its corollary, the Master Theorem. If we simply ignore the floor and ceiling, the method suggests the solution T (n) = O(n log n). We can easily check that this answer is correct using induction, but there is a simple method for solving recurrences like this directly, called domain transformation. First we overestimate the time bound, once by pretending that the two subproblem sizes are equal, and again to eliminate the ceiling: T ( n) ≤ 2 T n/ 2 + O(n) ≤ 2 T (n/2 + 1) + O(n). Now we define a new function S (n) = T (n + α), where α is a constant chosen so that S (n) satisfies the familiar recurrence S (n) ≤ 2S (n/2) + O(n). To figure out the appropriate value for α, we compare two 4 “The inductive hypothesis” is just a technical nickname for our friend the Recursion Fairy. 4 Algorithms Lecture 1: Recursion versions of the recurrence for T (n + α): S (n) ≤ 2S (n/2) + O(n) =⇒ T (n + α) ≤ 2 T (n/2 + α) + O(n) T (n) ≤ 2 T (n/2 + 1) + O(n) =⇒ T (n + α) ≤ 2 T ((n + α)/2 + 1) + O(n + α) For these two recurrences to be equal, we need n/2 + α = (n + α)/2 + 1, which implies that α = 2. The recursion tree method tells us that S (n) = O(n log n), so T (n) = S (n − 2) = O((n − 2) log(n − 2)) = O(n log n). We can use domain transformations to remove floors, ceilings, and lower order terms from any recurrence. But now that we realize this, we don’t need to bother grinding through the details ever again! 1.4 Quicksort Quicksort was discovered by Tony Hoare in 1962. In this algorithm, the hard work is splitting the array into subsets so that merging the final result is trivial. 1. Choose a pivot element from the array. 2. Split the array into three subarrays containing the items less than the pivot, the pivot itself, and the items bigger than the pivot. 3. Recursively quicksort the first and last subarray. Input: Choose a pivot: Partition: Recurse: S O R T I N G E X A M P L S O R T I N G E X A M P L M A A E E G G I I L L M N N R O X P O S S R P T T X A Quicksort example. Here’s a more formal specification of the Quicksort algorithm. The separate PARTITION subroutine takes the original position of the pivot element as input and returns the post-partition pivot position as output. QUICKSORT(A[1 .. n]): if (n > 1) Choose a pivot element A[ p] k ← PARTITION(A, p) QUICKSORT(A[1 .. k − 1]) QUICKSORT(A[k + 1 .. n]) PARTITION(A[1 .. n], p): if ( p = n) swap A[ p] ↔ A[n] i ← 0; j ← n while (i < j ) repeat i ← i + 1 until (i = j or A[i ] ≥ A[n]) repeat j ← j − 1 until (i = j or A[ j ] ≤ A[n]) if (i < j ) swap A[i ] ↔ A[ j ] if (i = n) swap A[i ] ↔ A[n] return i Just as we did for mergesort, we need two induction proofs to show that QUICKSORT is correct—weak induction to prove that PARTITION correctly partitions the array, and then straightforward strong induction to prove that QUICKSORT correctly sorts assuming PARTITION is correct. I’ll leave the gory details as an exercise for the reader. 5 Algorithms Lecture 1: Recursion The analysis is also similar to mergesort. PARTITION runs in O(n) time: j − i = n at the beginning, j − i = 0 at the end, and we do a constant amount of work each time we increment i or decrement j . For QUICKSORT, we get a recurrence that depends on k, the rank of the chosen pivot: T (n) = T (k − 1) + T (n − k) + O(n) If we could choose the pivot to be the median element of the array A, we would have k = n/2 , the two subproblems would be as close to the same size as possible, the recurrence would become T ( n) = 2 T n/ 2 − 1 + T n/ 2 + O(n) ≤ 2 T (n/2) + O(n), and we’d have T (n) = O(n log n) by the recursion tree method. In fact, it is possible to locate the median element in an unsorted array in linear time. However, the algorithm is fairly complicated, and the hidden constant in the O() notation is quite large. So in practice, programmers settle for something simple, like choosing the first or last element of the array. In this case, k can be anything from 1 to n, so we have T (n) = max T (k − 1) + T (n − k) + O(n) 1≤k≤n In the worst case, the two subproblems are completely unbalanced—either k = 1 or k = n—and the recurrence becomes T (n) ≤ T (n − 1) + O(n). The solution is T (n) = O(n2 ). Another common heuristic is ‘median of three’—choose three elements (usually at the beginning, middle, and end of the array), and take the middle one as the pivot. Although this is better in practice than just choosing one element, we can still have k = 2 or k = n − 1 in the worst case. With the median-of-three heuristic, the recurrence becomes T (n) ≤ T (1) + T (n − 2) + O(n), whose solution is still T (n) = O(n2 ). Intuitively, the pivot element will ‘usually’ fall somewhere in the middle of the array, say between n/10 and 9n/10. This suggests that the average-case running time is O(n log n). Although this intuition is correct, we are still far from a proof that quicksort is usually efficient. We will formalize this intuition about average cases in a later lecture. 1.5 The Pattern Both mergesort and and quicksort follow the same general three-step pattern of all divide and conquer algorithms: 1. Divide the problem into several smaller independent subproblems. 2. Delegate each subproblem to the Recursion Fairy to get a sub-solution. 3. Combine the sub-solutions together into the final solution. If the size of any subproblem falls below some constant threshold, the recursion bottoms out. Hopefully, at that point, the problem is trivial, but if not, we switch to a different algorithm instead. Proving a divide-and-conquer algorithm correct usually involves strong induction. Analyzing the running time requires setting up and solving a recurrence, which often (but unfortunately not always!) can be solved using recursion trees (or, if you insist, the Master Theorem), perhaps after a simple domain transformation. 6 Algorithms 1.6 Lecture 1: Recursion Median Selection So, how do we find the median element of an array in linear time? The following algorithm was discovered by Manuel Blum, Bob Floyd, Vaughan Pratt, Ron Rivest, and Bob Tarjan in the early 1970s. Their algorithm actually solves the more general problem of selecting the kth largest element in an array, using the following recursive divide-and-conquer strategy. The subroutine PARTITION is the same as the one used in QUICKSORT. SELECT(A[1 .. n], k): if n ≤ 25 use brute force else m ← n/5 for i ← 1 to m B [i ] ← SELECT(A[5i − 4 .. 5i ], 3) mom ← SELECT(B [1 .. m], m/2 ) 〈〈Brute force!〉〉 〈〈Recursion!〉〉 r ← PARTITION(A[1 .. n], mom) if k < r return SELECT(A[1 .. r − 1], k) else if k > r return SELECT(A[ r + 1 .. n], k − r ) else return mom 〈〈Recursion!〉〉 〈〈Recursion!〉〉 If the input array is too large to handle by brute force, we divide it into n/5 blocks, each containing exactly 5 elements, except possibly the last. (If the last block isn’t full, just throw in a few ∞s.) We find the median of each block by brute force and collect those medians into a new array. Then we recursively compute the median of the new array (the median of medians — hence ‘mom’) and use it to partition the input array. Finally, either we get lucky and the median-of-medians is the kth largest element of A, or we recursively search one of the two subarrays. The key insight is that these two subarrays cannot be too large or too small. The median-of-medians is larger than n/5 /2 − 1 ≈ n/10 medians, and each of those medians is larger than two other elements in its block. In other words, the median-of-medians is larger than at least 3n/10 elements in the input array. Symmetrically, mom is smaller than at least 3n/10 input elements. Thus, in the worst case, the final recursive call searches an array of size 7n/10. We can visualize the algorithm’s behavior by drawing the input array as a 5 × n/5 grid, which each column represents five consecutive elements. For purposes of illustration, imagine that we sort every column from top down, and then we sort the columns by their middle element. (Let me emphasize that the algorithm doesn’t actually do this!) In this arrangement, the median-of-medians is the element closest to the center of the grid. Visualizing the median of medians The left half of the first three rows of the grid contains 3n/10 elements, each of which is smaller than the median-of-medians. If the element we’re looking for is larger than the median-of-medians, 7 Algorithms Lecture 1: Recursion our algorithm will throw away everything smaller than the median-of-median, including those 3n/10 elements, before recursing. A symmetric argument applies when our target element is smaller than the median-of-medians. Discarding approximately 3/10 of the array We conclude that the worst-case running time of the algorithm obeys the following recurrence: T (n) ≤ O(n) + T (n/5) + T (7n/10). The recursion tree method implies the solution T (n) = O(n). A finer analysis reveals that the hidden constants are quite large, even if we count only comparisons; this is not a practical algorithm for small inputs. (In particular, mergesort uses fewer comparisons in the worst case when n < 4,000,000.) Selecting the median of 5 elements requires 6 comparisons, so we need 6n/5 comparisons to set up the recursive subproblem. We need another n − 1 comparisons to partition the array after the recursive call returns. So the actual recurrence is T (n) ≤ 11n/5 + T (n/5) + T (7n/10). The recursion tree method implies the upper bound T ( n) ≤ 1.7 5 i 9 11n i ≥0 10 = 11n 5 · 10 = 22n. Multiplication Adding two n-digit numbers takes O(n) time by the standard iterative ‘ripple-carry’ algorithm, using a lookup table for each one-digit addition. Similarly, multiplying an n-digit number by a one-digit number takes O(n) time, using essentially the same algorithm. What about multiplying two n-digit numbers? At least in the United States, every grade school student (supposedly) learns to multiply by breaking the problem into n one-digit multiplications and n additions: 31415962 × 27182818 251327696 31415962 251327696 62831924 251327696 31415962 219911734 62831924 853974377340916 8 Algorithms Lecture 1: Recursion We could easily formalize this algorithm as a pair of nested for-loops. The algorithm runs in O(n2 ) time—altogether, there are O(n2 ) digits in the partial products, and for each digit, we spend constant time. We can get a more efficient algorithm by exploiting the following identity: (10m a + b)(10m c + d ) = 102m ac + 10m ( bc + ad ) + bd Here is a divide-and-conquer algorithm that computes the product of two n-digit numbers x and y , based on this formula. Each of the four sub-products e, f , g , h is computed recursively. The last line does not involve any multiplications, however; to multiply by a power of ten, we just shift the digits and fill in the right number of zeros. MULTIPLY( x , y, n): if n = 1 return x · y else m ← n/ 2 a ← x /10m ; b ← x mod 10m d ← y /10m ; c ← y mod 10m e ← MULTIPLY(a, c , m) f ← MULTIPLY( b, d , m) g ← MULTIPLY( b, c , m) h ← MULTIPLY(a, d , m) return 102m e + 10m ( g + h) + f You can easily prove by induction that this algorithm is correct. The running time for this algorithm is given by the recurrence T (n) = 4 T ( n/2 ) + O(n), T (1) = 1, which solves to T (n) = O(n2 ) by the recursion tree method (after a simple domain transformation). Hmm. . . I guess this didn’t help after all. But there’s a trick, first published by Anatoli˘ Karatsuba in 1962.5 We can compute the middle ı coefficient bc + ad using only one recursive multiplication, by exploiting yet another bit of algebra: ac + bd − (a − b)(c − d ) = bc + ad This trick lets use replace the last three lines in the previous algorithm as follows: FASTMULTIPLY( x , y, n): if n = 1 return x · y else m ← n/ 2 a ← x /10m ; b ← x mod 10m d ← y /10m ; c ← y mod 10m e ← FASTMULTIPLY(a, c , m) f ← FASTMULTIPLY( b, d , m) g ← FASTMULTIPLY(a − b, c − d , m) return 102m e + 10m (e + f − g ) + f 5 However, the same basic trick was used non-recursively by Gauss in the 1800s to multiply complex numbers using only three real multiplications. 9 Algorithms Lecture 1: Recursion The running time of Karatsuba’s FASTMULTIPLY algorithm is given by the recurrence T (n) ≤ 3 T ( n/2 ) + O(n), T (1) = 1. After a domain transformation, we can plug this into a recursion tree to get the solution T (n) = O(nlg 3 ) = O(n1.585 ), a significant improvement over our earlier quadratic-time algorithm.6 Of course, in practice, all this is done in binary instead of decimal. We can take this idea even further, splitting the numbers into more pieces and combining them in more complicated ways, to get even faster multiplication algorithms. Ultimately, this idea leads to the development of the Fast Fourier transform, a more complicated divide-and-conquer algorithm that can be used to multiply two n-digit numbers in O(n log n) time.7 We’ll talk about Fast Fourier transforms in detail in another lecture. 1.8 Exponentiation Given a number a and a positive integer n, suppose we want to compute a n . The standard naïve method is a simple for-loop that does n − 1 multiplications by a: SLOWPOWER(a, n): x←a for i ← 2 to n x ← x ·a return x This iterative algorithm requires n multiplications. Notice that the input a could be an integer, or a rational, or a floating point number. In fact, it doesn’t need to be a number at all, as long as it’s something that we know how to multiply. For example, the same algorithm can be used to compute powers modulo some finite number (an operation commonly used in cryptography algorithms) or to compute powers of matrices (an operation used to evaluate recurrences and to compute shortest paths in graphs). All we really require is that a belong to a multiplicative group.8 Since we don’t know what kind of things we’re multiplying, we can’t know how long a multiplication takes, so we’re forced analyze the running time in terms of the number of multiplications. There is a much faster divide-and-conquer method, using the following simple recursive formula: an = a n/2 ·a n/2 . What makes this approach more efficient is that once we compute the first factor a the second factor a n/2 using at most one more multiplication. n/2 , we can compute Karatsuba actually proposed an algorithm based on the formula (a + c )( b + d ) − ac − bd = bc + ad . This algorithm also runs in O(nlg 3 ) time, but the actual recurrence is a bit messier: a − b and c − d are still m-digit numbers, but a + b and c + d might have m + 1 digits. The simplification presented here is due to Donald Knuth. 7 This fast algorithm for multiplying integers using FFTs was discovered by Arnold Schönhange and Volker Strassen in 1971. The O(n log n) running time requires the standard assumption that O(log n)-bit integer arithmetic can be performed in constant time; the number of bit operations is O(n log n log log n). 8 A multiplicative group (G , ⊗) is a set G and a function ⊗ : G × G → G , satisfying three axioms: 1. There is a unit element 1 ∈ G such that 1 ⊗ g = g ⊗ 1 for any element g ∈ G . 2. Any element g ∈ G has a inverse element g −1 ∈ G such that g ⊗ g −1 = g −1 ⊗ g = 1 3. The function is associative: for any elements f , g , h ∈ G , we have f ⊗ ( g ⊗ h) = ( f ⊗ g ) ⊗ h. 6 10 Algorithms Lecture 1: Recursion FASTPOWER(a, n): if n = 1 return a else x ← FASTPOWER(a, n/2 ) if n is even return x · x else return x · x · a The total number of multiplications satisfies the recurrence T (n) ≤ T ( n/2 ) + 2, with the base case T (1) = 0. After a domain transformation, recursion trees give us the solution T (n) = O(log n). Incidentally, this algorithm is asymptotically optimal—any algorithm for computing a n must perform at least Ω(log n) multiplications. In fact, when n is a power of two, this algorithm is exactly optimal. However, there are slightly faster methods for other values of n. For example, our divide-and-conquer algorithm computes a15 in six multiplications (a15 = a7 · a7 · a; a7 = a3 · a3 · a; a3 = a · a · a), but only five multiplications are necessary (a → a2 → a3 → a5 → a10 → a15 ). Nobody knows of an efficient algorithm that always uses the minimum possible number of multiplications. Exercises 1. (a) Professor George O’Jungle has a 27-node binary tree, in which every node is labeled with a unique letter of the Roman alphabet or the character &. Preorder and postorder traversals of the tree visit the nodes in the following order: • Preorder: I Q J H L E M V O T S B R G Y Z K C A & F P N U D W X • Postorder: H E M L J V Q S G Y R Z B T C P U D N F W & X A K O I Draw George’s binary tree. (b) Describe and analyze a recursive algorithm for reconstructing a binary tree, given its preorder and postorder node sequences. (c) Describe and analyze a recursive algorithm for reconstructing a binary tree, given its preorder and inorder node sequences. 2. Consider a 2n × 2n chessboard with one (arbitrarily chosen) square removed. (a) Prove that any such chessboard can be tiled without gaps or overlaps by L-shaped pieces, each composed of 3 squares. (b) Describe and analyze an algorithm to compute such a tiling, given the integer n and two n-bit integers representing the row and column of the missing square. The output is a list of the positions and orientations of (4n − 1)/3 tiles. Your algorithm should run in O(4n ) time. 3. Prove that the recursive Tower of Hanoi algorithm is exactly equivalent to each of the following non-recursive algorithms; in other words, prove that all three algorithms move the same disks, to and from the same needles, in the same order. The needles are labeled 0, 1, and 2, and our problem is to move a stack of n disks from needle 0 to needle 2 (as shown on page 2). (a) Follow these four rules: • Never move the same disk twice in a row. 11 Algorithms Lecture 1: Recursion • If n is even, always move the smallest disk forward (· · · → 0 → 1 → 2 → 0 → · · · ). • If n is odd, always move the smallest disk backward (· · · → 0 → 2 → 1 → 0 → · · · ). • When there is no move that satisfies the other rules, the puzzle is solved. (b) Let ρ (n) denote the smallest integer k such that n/2k is not an integer. For example, ρ (42) = 2, because 42/21 is an integer but 42/22 is not. (Equivalently, ρ (n) is one more than the position of the least significant 1 bit in the binary representation of n.) The function ρ (n) is sometimes called the ‘ruler’ function, because its behavior resembles the marks on a ruler: 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 5, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1, 6, 1, 2, 1, 3, 1, . . . . Here’s the non-recursive algorithm in one line: In step i , move disk ρ (i ) forward if n − i is even, backward if n − i is odd. When this rule requires us to move disk n + 1, the algorithm ends. 4. Consider the following restricted variants of the Tower of Hanoi puzzle. In each problem, the needles are numbered 0, 1, and 2, as in problem 3, and your task is to move a stack of n disks from needle 1 to needle 2. (a) Suppose you are forbidden to move any disk directly between needle 1 and needle 2; every move must involve needle 0. Describe an algorithm to solve this version of the puzzle in as few moves as possible. Exactly how many moves does your algorithm make? (b) Suppose you are only allowed to move disks from needle 0 to needle 2, from needle 2 to needle 1, or from needle 1 to needle 0. Equivalently, Suppose the needles are arranged in a circle and numbered in clockwise order, and you are only allowed to move disks counterclockwise. Describe an algorithm to solve this version of the puzzle in as few moves as possible. Exactly how many moves does your algorithm make? 0 1 5 2 6 3 7 4 8 A top view of the first eight moves in a counterclockwise Towers of Hanoi solution (c) Finally, suppose your only restriction is that you may never move a disk directly from needle 1 to needle 2. Describe an algorithm to solve this version of the puzzle in as few moves as possible. How many moves does your algorithm make? [Hint: This is considerably harder to analyze than the other two variants.] 12 Algorithms Lecture 1: Recursion 5. Most graphics hardware includes support for a low-level operation called blit, or block transfer, which quickly copies a rectangular chunk of a pixel map (a two-dimensional array of pixel values) from one location to another. This is a two-dimensional version of the standard C library function memcpy(). Suppose we want to rotate an n × n pixel map 90◦ clockwise. One way to do this, at least when n is a power of two, is to split the pixel map into four n/2 × n/2 blocks, move each block to its proper position using a sequence of five blits, and then recursively rotate each block. Alternately, we could first recursively rotate the blocks and then blit them into place. CA DB AB CD AB CD BD AC Two algorithms for rotating a pixel map. Black arrows indicate blitting the blocks into place; white arrows indicate recursively rotating the blocks. The first rotation algorithm (blit then recurse) in action. (a) Prove that both versions of the algorithm are correct when n is a power of two. (b) Exactly how many blits does the algorithm perform when n is a power of two? (c) Describe how to modify the algorithm so that it works for arbitrary n, not just powers of two. How many blits does your modified algorithm perform? (d) What is your algorithm’s running time if a k × k blit takes O(k2 ) time? (e) What if a k × k blit takes only O(k) time? 6. An inversion in an array A[1 .. n] is a pair of indices (i , j ) such that i < j and A[i ] > A[ j ]. The n number of inversions in an n-element array is between 0 (if the array is sorted) and 2 (if the array is sorted backward). Describe and analyze an algorithm to count the number of inversions in an n-element array in O(n log n) time. [Hint: Modify mergesort.] 13 Algorithms Lecture 1: Recursion 7. (a) Prove that the following algorithm actually sorts its input. STOOGESORT(A[0 .. n − 1]) : if n = 2 and A[0] > A[1] swap A[0] ↔ A[1] else if n > 2 m = 2 n/ 3 STOOGESORT(A[0 .. m − 1]) STOOGESORT(A[n − m .. n − 1]) STOOGESORT(A[0 .. m − 1]) (b) Would STOOGESORT still sort correctly if we replaced m = 2n/3 with m = 2n/3 ? Justify your answer. (c) State a recurrence (including the base case(s)) for the number of comparisons executed by STOOGESORT. (d) Solve the recurrence, and prove that your solution is correct. [Hint: Ignore the ceiling.] (e) Prove that the number of swaps executed by STOOGESORT is at most n 2 . 8. Suppose you are given a stack of n pancakes of different sizes. You want to sort the pancakes so that smaller pancakes are on top of larger pancakes. The only operation you can perform is a flip—insert a spatula under the top k pancakes, for some integer k between 1 and n, and flip them all over. Flipping the top three pancakes. (a) Describe an algorithm to sort an arbitrary stack of n pancakes using as few flips as possible. Exactly how many flips does your algorithm perform in the worst case? (b) Now suppose one side of each pancake is burned. Describe an algorithm to sort an arbitrary stack of n pancakes, so that the burned side of every pancake is facing down, using as few flips as possible. Exactly how many flips does your algorithm perform in the worst case? 9. You are a contestant on the hit game show “Beat Your Neighbors!” You are presented with an m × n grid of boxes, each containing a unique number. It costs $100 to open a box. Your goal is to find a box whose number is larger than its neighbors in the grid (above, below, left, and right). If you spend less money than any of your opponents, you win a week-long trip for two to Las Vegas and a year’s supply of Rice-A-RoniTM , to which you are hopelessly addicted. (a) Suppose m = 1. Describe an algorithm that finds a number that is bigger than any of its neighbors. How many boxes does your algorithm open in the worst case? (b) Suppose m = n. Describe an algorithm that finds a number that is bigger than any of its neighbors. How many boxes does your algorithm open in the worst case? (c) Prove that your solution to part (b) is optimal up to a constant factor. 14 Algorithms Lecture 1: Recursion 10. (a) Suppose we are given two sorted arrays A[1 .. n] and B [1 .. n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in Θ(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B ; if k = n, your algorithm should return the median of A ∪ B .) You can assume that the arrays contain no duplicate elements. [Hint: First solve the special case k = n.] (b) Now suppose we are given three sorted arrays A[1 .. n], B [1 .. n], and C [1 .. n], and an integer k. Describe an algorithm to find the kth smallest element in A in O(log n) time. (c) Finally, suppose we are given a two dimensional array A[1 .. m][1 .. n] in which every row A[i ][ ] is sorted, and an integer k. Describe an algorithm to find the kth smallest element in A as quickly as possible. How does the running time of your algorithm depend on m? [Hint: Use the linear-time SELECT algorithm as a subroutine.] 11. (a) Describe and analyze an algorithm to sort an array A[1 .. n] by calling a subroutine SQRTSORT(k), which sorts the subarray A k + 1 .. k + n in place, given an arbitrary integer k between 0 and n − n as input. (To simplify the problem, assume that n is an integer.) Your algorithm is only allowed to inspect or modify the input array by calling SQRTSORT; in particular, your algorithm must not directly compare, move, or copy array elements. How many times does your algorithm call SQRTSORT in the worst case? (b) Prove that your algorithm from part (a) is optimal up to constant factors. In other words, if f (n) is the number of times your algorithm calls SQRTSORT, prove that no algorithm can sort using o( f (n)) calls to SQRTSORT. (c) Now suppose SQRTSORT is implemented recursively, by calling your sorting algorithm from part (a). For example, at the second level of recursion, the algorithm is sorting arrays roughly of size n1/4 . What is the worst-case running time of the resulting sorting algorithm? (To k simplify the analysis, assume that the array size n has the form 22 , so that repeated square roots are always integers.) 12. Suppose we have n points scattered inside a two-dimensional box. A kd-tree recursively subdivides the points as follows. First we split the box into two smaller boxes with a vertical line, then we split each of those boxes with horizontal lines, and so on, always alternating between horizontal and vertical splits. Each time we split a box, the splitting line partitions the rest of the interior points as evenly as possible by passing through a median point inside the box (not on its boundary). If a box doesn’t contain any points, we don’t split it any more; these final empty boxes are called cells. A kd-tree for 15 points. The dashed line crosses the four shaded cells. 15 Algorithms Lecture 1: Recursion (a) How many cells are there, as a function of n? Prove your answer is correct. (b) In the worst case, exactly how many cells can a horizontal line cross, as a function of n? Prove your answer is correct. Assume that n = 2k − 1 for some integer k. (c) Suppose we have n points stored in a kd-tree. Describe and analyze an algorithm that counts the number of points above a horizontal line (such as the dashed line in the figure) as quickly as possible. [Hint: Use part (b).] (d) Describe an analyze an efficient algorithm that counts, given a kd-tree storing n points, the number of points that lie inside a rectangle R with horizontal and vertical sides. [Hint: Use part (c).] 13. You are at a political convention with n delegates, each one a member of exactly one political party. It is impossible to tell which political party any delegate belongs to; in particular, you will be summarily ejected from the convention if you ask. However, you can determine whether any two delegates belong to the same party or not by introducing them to each other—members of the same party always greet each other with smiles and friendly handshakes; members of different parties always greet each other with angry stares and insults. (a) Suppose a majority (more than half) of the delegates are from the same political party. Describe an efficient algorithm that identifies a member (any member) of the majority party. (b) Now suppose exactly k political parties are represented at the convention and one party has a plurality: more delegates belong to that party than to any other. Present a practical procedure to pick a person from the plurality political party as parsimoniously as possible. (Please.) 14. The median of a set of size n is its n/2 th largest element, that is, the element that is as close as possible to the middle of the set in sorted order. In this lecture, we saw a fairly complicated algorithm to compute the median in O(n) time. During your lifelong quest for a simpler linear-time median-finding algorithm, you meet and befriend the Near-Middle Fairy. Given any set X , the Near-Middle Fairy can find an element m ∈ X that is near the middle of X in O(1) time. Specifically, at least a third of the elements of X are smaller than m, and at least a third of the elements of X are larger than m. Describe and analyze a simple recursive algorithm to find the median of a set in O(n) time if you are allowed to ask the Near-Middle Fairy for help. 15. For this problem, a subtree of a binary tree means any connected subgraph. A binary tree is complete if every internal node has two children, and every leaf has exactly the same depth. Describe and analyze a recursive algorithm to compute the largest complete subtree of a given binary tree. Your algorithm should return the root and the depth of this subtree. The largest complete subtree of this binary tree has depth 2. 16 Algorithms Lecture 1: Recursion 16. Consider the following classical recursive algorithm for computing the factorial n! of a non-negative integer n: FACTORIAL(n): if n = 0 return 0 else return n · FACTORIAL(n − 1) (a) How many multiplications does this algorithm perform? (b) How many bits are required to write n! in binary? Express your answer in the form Θ( f (n)), for some familiar function f (n). [Hint: Use Stirling’s approximation: n! ≈ 2πn · (n/e)n .] (c) Your answer to (b) should convince you that the number of multiplications is not a good estimate of the actual running time of FACTORIAL. The grade-school multiplication algorithm takes O(k · l ) time to multiply a k-digit number and an l -digit number. What is the running time of FACTORIAL if we use this multiplication algorithm as a subroutine? (d) The following algorithm also computes n!, but groups the multiplication differently: FACTORIAL2(n, m): 〈〈Compute n!/(n − m)!〉〉 if m = 0 return 1 else if m = 1 return n else return FACTORIAL2(n, m/2 ) · FACTORIAL2(n − m/2 , m/2 ) What is the running time of FACTORIAL2(n, n) if we use grade-school multiplication? [Hint: Ignore the floors and ceilings.] (e) Describe and analyze a variant of Karastuba’s algorithm that can multiply any k-digit number and any l -digit number, where k ≥ l , in O(k · l lg 3−1 ) = O(k · l 0.585 ) time. (f) What are the running times of FACTORIAL(n) and FACTORIAL2(n, n) if we use the modified Karatsuba multiplication from part (e)? c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 17 Algorithms Non-Lecture A: Fast Fourier Transforms Calvin: Here’s another math problem I can’t figure out. What’s 9+4? Hobbes: Ooh, that’s a tricky one. You have to use calculus and imaginary numbers for this. Calvin: IMAGINARY NUMBERS?! Hobbes: You know, eleventeen, thirty-twelve, and all those. It’s a little confusing at first. Calvin: How did YOU learn all this? You’ve never even gone to school! Hobbes: Instinct. Tigers are born with it. — “Calvin and Hobbes” (January 6, 1998) It needs evaluation So let the games begin A heinous crime, a show of force A murder would be nice, of course — “Bad Horse’s Letter”, Dr. Horrible’s Sing-Along Blog (2008) A Fast Fourier Transforms A.1 Polynomials In this lecture we’ll talk about algorithms for manipulating polynomials: functions of one variable built from additions subtractions, and multiplications (but no divisions). The most common representation for a polynomial p( x ) is as a sum of weighted powers of a variable x : n aj x j. p( x ) = j =0 The numbers a j are called coefficients. The degree of the polynomial is the largest power of x ; in the example above, the degree is n. Any polynomial of degree n can be specified by a sequence of n + 1 coefficients. Some of these coefficients may be zero, but not the nth coefficient, because otherwise the degree would be less than n. Here are three of the most common operations that are performed with polynomials: • Evaluate: Give a polynomial p and a number x , compute the number p( x ). • Add: Give two polynomials p and q, compute a polynomial r = p + q, so that r ( x ) = p( x ) + q( x ) for all x . If p and q both have degree n, then their sum p + q also has degree n. • Multiply: Give two polynomials p and q, compute a polynomial r = p · q, so that r ( x ) = p( x ) · q( x ) for all x . If p and q both have degree n, then their product p · q has degree 2n. Suppose we represent a polynomial of degree n as an array of n + 1 coefficients P [0 .. n], where P [ j ] is the coefficient of the x j term. We learned simple algorithms for all three of these operations in high-school algebra: EVALUATE( P [0 .. n], x ): X ← 1 〈〈X = x j 〉〉 y ←0 for j ← 0 to n y ← y + P [ j] · X X ←X·x return y ADD( P [0 .. n], Q[0 .. n]): for j ← 0 to n R[ j ] ← P [ j ] + Q [ j ] return R[0 .. n] 1 MULTIPLY( P [0 .. n], Q[0 .. m]): for j ← 0 to n + m R[ j ] ← 0 for j ← 0 to n for k ← 0 to m R[ j + k] ← R[ j + k] + P [ j ] · Q[k] return R[0 .. n + m] Algorithms Non-Lecture A: Fast Fourier Transforms EVALUATE uses O(n) arithmetic operations.1 This is the best we can hope for, but we can cut the number of multiplications in half using Horner’s rule: p( x ) = a0 + x (a1 + x (a2 + . . . + x an )). HORNER( P [0 .. n], x ): y ← P [ n] for i ← n − 1 downto 0 y ← x · y + P [i ] return y The addition algorithm also runs in O(n) time, and this is clearly the best we can do. The multiplication algorithm, however, runs in O(n2 ) time. In the previous lecture, we saw a divide and conquer algorithm (due to Karatsuba) for multiplying two n-bit integers in only O(nlg 3 ) steps; precisely the same algorithm can be applied here. Even cleverer divide-and-conquer strategies lead to multiplication algorithms whose running times are arbitrarily close to linear—O(n1+ ) for your favorite value e > 0—but with great cleverness comes great confusion. These algorithms are difficult to understand, even more difficult to implement correctly, and not worth the trouble in practice thanks to large constant factors. A.2 Alternate Representations Part of what makes multiplication so much harder than the other two operations is our input representation. Coefficients vectors are the most common representation for polynomials, but there are at least two other useful representations. A.2.1 Roots The Fundamental Theorem of Algebra states that every polynomial p of degree n has exactly n roots r1 , r2 , . . . rn such that p( r j ) = 0 for all j . Some of these roots may be irrational; some of these roots may by complex; and some of these roots may be repeated. Despite these complications, this theorem implies a unique representation of any polynomial of the form n p( x ) = s (x − rj) j =1 where the r j ’s are the roots and s is a scale factor. Once again, to represent a polynomial of degree n, we need a list of n + 1 numbers: one scale factor and n roots. Given a polynomial in this root representation, we can clearly evaluate it in O(n) time. Given two polynomials in root representation, we can easily multiply them in O(n) time by multiplying their scale factors and just concatenating the two root sequences. Unfortunately, if we want to add two polynomials in root representation, we’re pretty much out of luck. There’s essentially no correlation between the roots of p, the roots of q, and the roots of p + q. We could convert the polynomials to the more familiar coefficient representation first—this takes O(n2 ) I’m going to assume in this lecture that each arithmetic operation takes O(1) time. This may not be true in practice; in fact, one of the most powerful applications of FFTs is fast integer multiplication. One of the fastest integer multiplication algorithms, due to Schönhage and Strassen, multiplies two n-bit binary numbers using O(n log n log log n log log log n log log log log n · · · ) bit operations. The algorithm uses an n-element Fast Fourier Transform, which requires several O(log n)-nit integer multiplications. These smaller multiplications are carried out recursively (of course!), which leads to the cascade of logs in the running time. Needless to say, this is a can of worms. 1 2 Algorithms Non-Lecture A: Fast Fourier Transforms time using the high-school algorithms—but there’s no easy way to convert the answer back. In fact, for most polynomials of degree 5 or more in coefficient form, it’s impossible to compute roots exactly.2 A.2.2 Samples Our third representation for polynomials comes from a different consequence of the Fundamental Theorem of Algebra. Given a list of n + 1 pairs {( x 0 , y0 ), ( x 1 , y1 ), . . . , ( x n , yn ) }, there is exactly one polynomial p of degree n such that p( x j ) = y j for all j . This is just a generalization of the fact that any two points determine a unique line, since a line is (the graph of) a polynomial of degree 1. We say that the polynomial p interpolates the points ( x j , y j ). As long as we agree on the sample locations x j in advance, we once again need exactly n + 1 numbers to represent a polynomial of degree n. Adding or multiplying two polynomials in this sample representation is easy, as long as they use the same sample locations x j . To add the polynomials, just add their sample values. To multiply two polynomials, just multiply their sample values; however, if we’re multiplying two polynomials of degree n, we need to start with 2n + 1 sample values for each polynomial, since that’s how many we need to uniquely represent the product polynomial. Both algorithms run in O(n) time. Unfortunately, evaluating a polynomial in this representation is no longer trivial. The following formula, due to Lagrange, allows us to compute the value of any polynomial of degree n at any point, given a set of n + 1 samples. n−1 n−1 (x − xk) yj k= j = p( x ) = yj ( x − x k ) k= j ( x j − x k ) k= j ( x j − x k ) k= j j =0 j =0 Hopefully it’s clear that formula actually describes a polynomial, since each term in the rightmost sum is written as a scaled product of monomials. It’s also not hard to check that p( x j ) = y j for all j . As I mentioned earlier, the fact that this is the only polynomial that interpolates the points {( x j , y j )} is an easy consequence of the Fundamental Theorem of Algebra. We can easily transform Lagrange’s formula into an O(n2 )-time algorithm. A.2.3 Summary We find ourselves in the following frustrating situation. We have three representations for polynomials and three basic operations. Each representation allows us to almost trivially perform a different pair of operations in linear time, but the third takes at least quadratic time, if it can be done at all! evaluate multiply coefficients O ( n) O ( n) O (n 2 ) roots + scale O ( n) ∞ O ( n) samples A.3 add O (n 2 ) O ( n) O ( n) Converting Between Representations What we need are fast algorithms to convert quickly from one representation to another. That way, when we need to perform an operation that’s hard for our default representation, we can switch to a different representation that makes the operation easy, perform that operation, and then switch back. 2 This is where numerical analysis comes from. 3 Algorithms Non-Lecture A: Fast Fourier Transforms This strategy immediately rules out the root representation, since (as I mentioned earlier) finding roots of polynomials is impossible in general, at least if we’re interested in exact results. So how do we convert from coefficients to samples and back? Clearly, once we choose our sample positions x j , we can compute each sample value y j = p( x j ) in O(n) time from the coefficients using Horner’s rule. So we can convert a polynomial of degree n from coefficients to samples in O(n2 ) time. The Lagrange formula gives us an explicit conversion algorithm from the sample representation back to the more familiar coefficient representation. If we use the naïve algorithms for adding and multiplying polynomials (in coefficient form), this conversion takes O(n3 ) time. We can improve the cubic running time by observing that both conversion problems boil down to computing the product of a matrix and a vector. The explanation will be slightly simpler if we assume the polynomial has degree n − 1, so that n is the number of coefficients or samples. Fix a sequence j x 0 , x 1 , . . . , x n−1 of sample positions, and let V be the n × n matrix where vi j = x i (indexing rows and columns from 0 to n − 1): n 2 1 x0 x0 · · · x 0 −1 n 2 1 x1 x1 · · · x 1 −1 n 2 x2 x2 · · · x 2 −1 . V = 1 . . . . .. . . . . . . . . . n−1 2 1 x n−1 x n−1 · · · x n−1 The matrix V is called a Vandermonde matrix. The vector of coefficients a = (a0 , a1 , . . . , an−1 ) and the vector of sample values y = ( y0 , y1 , . . . , yn−1 ) are related by the matrix equation V a = y, or in more detail, 1 1 1 . . . 1 x0 2 x0 ··· n x 0 −1 x1 2 x1 ··· x2 . . . 2 x2 . . . ··· .. . n x 1 −1 a1 y1 n x 2 −1 a2 = y2 . . . . . . . . . . n−1 yn−1 x n−1 an−1 x n−1 2 x n−1 · · · a0 y0 Given this formulation, we can clearly transform any coefficient vector a into the corresponding sample vector y in O(n2 ) time. Conversely, if we know the sample values y , we can recover the coefficients by solving a system of n linear equations in n unknowns; this takes O(n3 ) time if we use Gaussian elimination. But we can speed this up by implicitly hard-coding the sample positions into the algorithm, To convert from samples to coefficients, we can simply multiply the sample vector by the inverse of V , again in O(n2 ) time. a = V −1 y Computing V −1 would take O(n3 ) time if we had to do it from scratch using Gaussian elimination, but because we fixed the set of sample positions in advance, the matrix V −1 can be written directly into the algorithm.3 So we can convert from coefficients to sample value and back in O(n2 ) time, which is pointless, because we can add, multiply, or evaluate directly in either representation in O(n2 ) time. But wait! Actually, it is possible to invert an n × n matrix in o(n3 ) time, using fast matrix multiplication algorithms that closely resemble Karatsuba’s sub-quadratic divide-and-conquer algorithm for integer/polynomial multiplication. 3 4 Algorithms Non-Lecture A: Fast Fourier Transforms There’s a degree of freedom we haven’t exploited—We get to choose the sample positions! Our conversion algorithm may be slow only because we’re trying to be too general. Perhaps, if we choose a set of sample points with just the right kind of recursive structure, we can do the conversion more quickly. In fact, there is a set of sample points that’s perfect for the job. A.4 The Discrete Fourier Transform Given a polynomial of degree n − 1, we’d like to find n sample points that are somehow as symmetric as possible. The most natural choice for those n points are the nth roots of unity; these are the roots of the polynomial x n − 1 = 0. These n roots are spaced exactly evenly around the unit circle in the complex plane.4 Every nth root of unity is a power of the primitive root ωn = e2πi /n = cos 2π n + i sin 2π n . A typical nth root of unity has the form j ωn = e(2πi /n) j = cos 2π n j + i sin 2π n j. These complex numbers have several useful properties for any integers n and k: • There are only n different nth roots of unity: ωk = ωk mod n . n n k • If n is even, then ωn+n/2 = −ωk ; in particular, ωn/2 = −ω0 = −1. n n n • 1/ωk = ω−k = ωk = (ωn )k , where the bar represents complex conjugation: a + bi = a − bi n n n • ωn = ωk . Thus, every nth root of unity is also a (kn)th root of unity. kn If we sample a polynomial of degree n − 1 at the nth roots of unity, the resulting list of sample values is called the discrete Fourier transform of the polynomial (or more formally, of the coefficient vector). Thus, given an array P [0 .. n − 1] of coefficients, the discrete Fourier transform computes a new vector P ∗ [0 .. n − 1] where n−1 j P ∗ [ j ] = p(ωn ) = jk P [ k ] · ωn k =0 We can obviously compute P ∗ in O(n2 ) time, but the structure of the nth roots of unity lets us do better. But before we describe that faster algorithm, let’s think about how we might invert this transformation. Recall that transforming coefficients into sample values is a linear transformation; the sample vector is the product of a Vandermonde matrix V and the coefficient vector. For the discrete Fourier transform, each entry in V is an nth root of unity; specifically, jk v jk = ωn 4 In this lecture, i always represents the square root of −1. Computer scientists are used to thinking of i as an integer index into a sequence, an array, or a for-loop, but we obviously can’t do that here. The physicist’s habit of using j = −1 just delays ◦ the problem (How do physicists write quaternions?), and typographical tricks like I or i or Mathematica’s ı are just stupid. ı 5 Algorithms Non-Lecture A: Fast Fourier Transforms for all integers j and k. Thus, 1 1 1 1 1 ω 2 ωn ω3 n n 1 ω2 ω4 ω6 n n n V = 1 ω3 ω6 ω9 n n n . . . . . . . . . . . . n−1 2(n−1) 3(n−1) 1 ωn ωn ωn ··· ··· ··· ··· .. . ··· 1 ωn−1 n ω2(n−1) n ω3(n−1) n . . . 2 ω(n−1) n To invert the discrete Fourier transform, converting sample values back to coefficients, we just have to multiply P ∗ by the inverse matrix V −1 . But the following amazing fact implies that this is almost the same as multiplying by V itself: Claim: V −1 = V /n Proof: Let W = V /n. We just have to show that M = V W is the identity matrix. We can compute a single entry in M as follows: n−1 n−1 v jl · w l k = m jk = l =0 j −k If j = k, then ωn jl ωn lk · ωn / n = l =0 = ω0 = 1, so n m jk = 1 n−1 n 1= l =0 1 n −1 n n n jl ωn −lk l =0 1 n −1 j − k l = (ω ) n l =0 n = 1, and if j = k, we have a geometric series n −1 j (ωn−k )l = m jk = l =0 j −k (ωn )n − 1 j −k ωn −1 = (ωn ) j −k − 1 n j −k ωn −1 = 1 j −k − 1 j −k ωn −1 = 0. That’s it! jk − jk In other words, if W = V −1 then w jk = v jk /n = ωn /n = ωn /n. What this means for us computer scientists is that any algorithm for computing the discrete Fourier transform can be easily modified to compute the inverse transform as well. A.5 Divide and Conquer! The symmetry in the roots of unity also allow us to compute the discrete Fourier transform efficiently using a divide and conquer strategy. The basic structure of the algorithm is almost the same as MergeSort, and the O(n log n) running time will ultimately follow from the same recurrence. The Fast Fourier Transform algorithm, popularized by Cooley and Tukey in 19655 , assumes that n is a power of two; if necessary, we can just pad the coefficient vector with zeros. 5 Actually, the FFT algorithm was previously published by Runge and König in 1924, and again by Yates in 1932, and again by Stumpf in 1937, and again by Danielson and Lanczos in 1942. So of course it’s often called the Coley-Tukey algorithm. But the algorithm was first used by Gauss in the 1800s for calculating the paths of asteroids from a finite number of equally-spaced observations. By hand. Fourier himself always did it the hard way. Cooley and Tukey apparently developed their algorithm to help detect Soviet nuclear tests without actually visiting Soviet nuclear facilities, by interpolating off-shore seismic readings. Without their rediscovery of the FFT algorithm, the nuclear test ban treaty would never have been ratified, and we’d all be speaking Russian, or more likely, whatever language radioactive glass speaks. 6 Algorithms Non-Lecture A: Fast Fourier Transforms Let p( x ) be a polynomial of degree n − 1, represented by an array P [0 .. n − 1] of coefficients. The FFT algorithm begins by splitting p into two smaller polynomials u and v , each with degree n/2 − 1. The coefficients of u are precisely the the even-degree coefficients of p; the coefficients of v are the odd-degree coefficients of p. For example, if p( x ) = 3 x 3 − 4 x 2 + 7 x + 5, then u( x ) = −4 x + 5 and v ( x ) = 3 x + 7. These three polynomials are related by the equation p( x ) = u( x 2 ) + x · v ( x 2 ). In particular, if x is an nth root of unity, we have p(ωk ) = u(ω2k ) + ωk · v (ω2k ). n n n n Now we can exploit those roots of unity again. Since n is a power of two, n must be even, so we k mod n/2 have ω2k = ωk /2 = ωn/2 . In other words, the values of p at the nth roots of unity depend on the n n values of u and v at (n/2)th roots of unity. k mod n/2 p(ωk ) = u(ωn/2 n k mod n/2 ) + ωk · v (ωn/2 n ). But those are just coefficients in the DFTs of u and v ! We conclude that the DFT coefficients of P are defined by the following recurrence: P ∗ [k] = U ∗ [k mod n/2] + ωk · V ∗ [k mod n/2] n Once the Recursion Fairy give us U ∗ and V ∗ , we can compute P ∗ in linear time. The base case for the recurrence is n = 1: if p( x ) has degree 0, then P ∗ [0] = P [0]. Here’s the complete FFT algorithm, along with its inverse. FFT( P [0 .. n − 1]): if n = 1 return P INVERSEFFT( P ∗ [0 .. n − 1]): if n = 1 return P for j ← 0 to n/2 − 1 U [ j ] ← P [2 j ] V [ j ] ← P [2 j + 1] for j ← 0 to n/2 − 1 U ∗ [ j ] ← P ∗ [2 j ] V ∗ [ j ] ← P ∗ [ 2 j + 1] U ∗ ← FFT(U [0 .. n/2 − 1]) V ∗ ← FFT(V [0 .. n/2 − 1]) U ← INVERSEFFT(U [0 .. n/2 − 1]) V ← INVERSEFFT(V [0 .. n/2 − 1]) π π ωn ← cos( 2n ) + i sin( 2n ) ω←1 π π ωn ← cos( 2n ) − i sin( 2n ) ω←1 for j ← 0 to n/2 − 1 P ∗[ j] ← U ∗[ j] + ω · V ∗[ j] ∗ P [ j + n/ 2] ← U ∗ [ j ] − ω · V ∗ [ j ] ω ← ω · ωn for j ← 0 to n/2 − 1 P [ j] ← 2(U [ j ] + ω · V [ j ]) P [ j + n/2] ← 2(U [ j ] − ω · V [ j ]) ω ← ω · ωn return P ∗ [0 .. n − 1] return P [0 .. n − 1] The overall running time of this algorithm satisfies the recurrence T (n) = Θ(n) + 2 T (n/2), which as we all know solves to T (n) = Θ(n log n). A.6 Fast Multiplication Given two polynomials p and q, each represented by an array of coefficients, we can multiply them in Θ(n log n) arithmetic operations as follows. First, pad the coefficient vectors and with zeros until the 7 Algorithms Non-Lecture A: Fast Fourier Transforms size is a power of two greater than or equal to the sum of the degrees. Then compute the DFTs of each coefficient vector, multiply the sample values one by one, and compute the inverse DFT of the resulting sample vector. FFTMULTIPLY( P [0 .. n − 1], Q[0 .. m − 1]): ← lg(n + m) for j ← n to 2 − 1 P [ j] ← 0 for j ← m to 2 − 1 Q[ j ] ← 0 P ∗ ← F F T (P ) Q∗ ← F F T (Q) for j ← 0 to 2 − 1 R∗ [ j ] ← P ∗ [ j ] · Q∗ [ j ] return INVERSEFFT(R∗ ) A.7 Inside the FFT FFTs are often implemented in hardware as circuits. To see the recursive structure of the circuit, let’s connect the top-level inputs and outputs to the inputs and outputs of the recursive calls. On the left we split the input P into two recursive inputs U and V . On the right, we combine the outputs U ∗ and V ∗ to obtain the final output P ∗ . 000 FFT(n/2) V* 110 001 001 011 101 101 011 111 V 110 110 P* 010 101 P 100 100 100 U* 010 010 011 FFT(n/2) 000 001 U 000 111 111 bit reversal permutation butterfly network The recursive structure of the FFT algorithm. If we expand this recursive structure completely, we see that the circuit splits naturally into two parts. The left half computes the bit-reversal permutation of the input. To find the position of P [k] in this permutation, write k in binary, and then read the bits backward. For example, in an 8-element bit-reversal permutation, P [3] = P [0112 ] ends up in position 6 = 1102 . The right half of the FFT circuit is a butterfly network. Butterfly networks are often used to route between processors in massively-parallel computers, since they allow any processor to communicate with any other in only O(log n) steps. 8 Algorithms Non-Lecture A: Fast Fourier Transforms Caveat Lector! This presentation is appropriate for graduate students or undergrads with strong math backgrounds, but it leaves most undergrads confused. You may find it less confusing to approach the material in the opposite order, as follows: First, any polynomial can be split into even-degree and odd-degree parts: p( x ) = peven ( x 2 ) + x · podd ( x 2 ). We can evaluate p( x ) by recursively evaluating peven ( x 2 ) and podd ( x 2 ) and doing O(1) arithmetic operations. Now suppose our task is to evaluate the degree-n polynomial p( x ) at n different points x , as quickly as possible. To exploit the even/odd recursive structure, we must choose the n evaluation points carefully. Call a set X of n values delicious if one of the following conditions holds: • X has one element. • The set X 2 = { x 2 | x ∈ X } has only n/2 elements and is delicious. Clearly the size of any delicious set is a power of 2. If someone magically handed us a delicious set X , we could compute { p( x ) | x ∈ X } in O(n log n) time using the even/odd recursive structure. Bit reversal permutation, blah blah blah, butterfly network, yadda yadda yadda. If n is a power of two, then the set of integers {0, 1, . . . , n − 1} is delicious, provided we perform all arithmetic modulo n . But that only tells us p( x ) mod n, and we want the actual value of p( x ). Of course, we can use larger moduli: {0, c , 2c , . . . , (n − 1)c } is delicious mod cn. We can avoid modular arithmetic entirely by using complex roots of unity—the set {e2πi (k/n) | k = 0, 1, . . . , n − 1} is delicious! The sequence of values p(e2πi (k/n) ) is called the discrete Fourier transform of p. Finally, to invert this transformation from coefficients to values, we repeat exactly the same procedure, using the same delicious set but in the opposite order. Blardy blardy, linear algebra, hi dee hi dee hi dee ho. Exercises 1. For any two sets X and Y of integers, the Minkowski sum X + Y is the set of all pairwise sums { x + y | x ∈ X , y ∈ Y }. (a) Describe an analyze and algorithm to compute the number of elements in X + Y in O(n2 log n) time. [Hint: The answer is not always n2 .] (b) Describe and analyze an algorithm to compute the number of elements in X + Y in O( M log M ) time, where M is the largest absolute value of any element of X ∪ Y . [Hint: What’s this lecture about?] 2. (a) Describe an algorithm that determines whether a given set of n integers contains two elements whose sum is zero, in O(n log n) time. (b) Describe an algorithm that determines whether a given set of n integers contains three elements whose sum is zero, in O(n2 ) time. (c) Now suppose the input set X contains only integers between − 10000n and 10000n. Describe an algorithm that determines whether X contains three elements whose sum is zero, in O(n log n) time. [Hint: Hint.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 9 Algorithms Lecture 2: Backtracking ’Tis a lesson you should heed, Try, try again; If at first you don’t succeed, Try, try again; Then your courage should appear, For, if you will persevere, You will conquer, never fear; Try, try again. — Thomas H. Palmer, The Teacher’s Manual: Being an Exposition of an Efficient and Economical System of Education Suited to the Wants of a Free People (1840) When you come to a fork in the road, take it. — Yogi Berra 2 Backtracking In this lecture, I want to describe another recursive algorithm strategy called backtracking. A backtracking algorithm tries to build a solution to a computational problem incrementally. Whenever the algorithm needs to decide between two alternatives to the next component of the solution, it simply tries both options recursively. 2.1 n -Queens The prototypical backtracking problem is the classical n-Queens Problem, first proposed by German chess enthusiast Max Bezzel in 1848 for the standard 8 × 8 board, and both solved and generalized to larger boards by Franz Nauck in 1850. The problem is to place n queens on an n × n chessboard, so that no two queens can attack each other. For readers not familiar with the rules of chess, this means that no two queens are in the same row, column, or diagonal. ♕ ♛ ♕ ♛ ♕ ♕ ♕ ♛ ♕ ♛ ♕ ♕ One solution to the 8 queens problem, represented by the array [4,7,3,8,2,5,1,6] Obviously, in any solution to the n-Queens problem, there is exactly one queen in each column. So we will represent our possible solutions using an array Q[1 .. n], where Q[i ] indicates the square in column i that contains a queen, or 0 if no queen has yet been placed in column i . To find a solution, we will put queens on the board row by row, starting at the top. A partial solution is an array Q[1 .. n] whose first r − 1 entries are positive and whose last n − r + 1 entries are all zeros, for some integer r . The following recursive algorithm recursively enumerates all complete n-queens solutions that are consistent with a given partial solution. The input parameter r is the first empty row. Thus, to compute all n-queens solutions with no restrictions, we would call RECURSIVENQUEENS(Q[1 .. n], 1). 1 Algorithms Lecture 2: Backtracking RECURSIVENQUEENS(Q[1 .. n], r ): if r = n + 1 print Q else for j ← 1 to n legal ← TRUE for i ← 1 to r − 1 if (Q[i ] = j ) or (Q[i ] = r − j + i ) or (Q[i ] = r + i − j ) legal ← FALSE if legal Q[ r ] ← j RECURSIVENQUEENS(Q[1 .. n], r + 1) Like most recursive algorithms, the execution of a backtracking algorithm can be illustrated using a recursion tree. The root of the recursion tree corresponds to the original invocation of the algorithm; edges in the tree correspond to recursive calls. A path from the root down to any node shows the history of a partial solution to the n-Queens problem, as queens are added to successive rows. The leaves correspond to partial solutions that cannot be extended, either because there is already a queen on every row, or because every position in the next empty row is in the same row, column, or diagonal as an existing queen. ♕ ♕ ♕ ♛ ♕ ♕ ♕ ♛ ♕ ♛ ♕ ♕ ♕ ♕ ♛ ♕ ♛ ♕ ♛ ♕ ♛ ♕ ♕ ♕ ♕ ♛ ♕ ♛ ♕ ♛ ♕ ♕ ♕ ♛ ♕ ♛ ♕ ♛ ♕ ♕ ♛ ♕ ♛ ♕ ♕ ♕ ♕ ♛ ♕ ♕ ♛ ♕ ♛ ♕ The complete recursion tree for our algorithm for the 4-queens problem. 2.2 Subset Sum Let’s consider a more complicated problem, called SUBSETSUM: Given a set X of positive integers and target integer T , is there a subset of elements in X that add up to T ? Notice that there can be more than one such subset. For example, if X = {8, 6, 7, 5, 3, 10, 9} and T = 15, the answer is TRUE, thanks to 2 Algorithms Lecture 2: Backtracking the subsets {8, 7} or {7, 5, 3} or {6, 9} or {5, 10}. On the other hand, if X = {11, 6, 5, 1, 7, 13, 12} and T = 15, the answer is FALSE. There are two trivial cases. If the target value T is zero, then we can immediately return TRUE, because the elements of the empty set add up to zero.1 On the other hand, if T < 0, or if T = 0 but the set X is empty, then we can immediately return FALSE. For the general case, consider an arbitrary element x ∈ X . (We’ve already handled the case where X is empty.) There is a subset of X that sums to T if and only if one of the following statements is true: • There is a subset of X that includes x and whose sum is T . • There is a subset of X that excludes x and whose sum is T . In the first case, there must be a subset of X \ { x } that sums to T − x ; in the second case, there must be a subset of X \ { x } that sums to T . So we can solve SUBSETSUM(X , T ) by reducing it to two simpler instances: SUBSETSUM(X \ { x }, T − x ) and SUBSETSUM(X \ { x }, T ). Here’s how the resulting recusive algorithm might look if X is stored in an array. SUBSETSUM(X [1 .. n], T ): if T = 0 return TRUE else if T < 0 or n = 0 return FALSE else return SUBSETSUM(X [2 .. n], T ) ∨ SUBSETSUM(X [2 .. n], T − X [1]) Proving this algorithm correct is a straightforward exercise in induction. If T = 0, then the elements of the empty subset sum to T , so TRUE is the correct output. Otherwise, if T is negative or the set X is empty, then no subset of X sums to T , so FALSE is the correct output. Otherwise, if there is a subset that sums to T , then either it contains X [1] or it doesn’t, and the Recursion Fairy correctly checks for each of those possibilities. Done. The running time T (n) clearly satisfies the recurrence T (n) ≤ 2 T (n − 1) + O(1), so the running time is T (n) = O(2n ) by the annihilator method. Here is a similar recursive algorithm that actually constructs a subset of X that sums to T , if one exists. This algorithm also runs in O(2n ) time. CONSTRUCTSUBSET(X [1 .. n], T ): if T = 0 return ∅ if T < 0 or n = 0 return NONE Y ← CONSTRUCTSUBSET(X [2 .. n], T ) if Y = NONE return Y Y ← CONSTRUCTSUBSET(X [2 .. n], T − X [1]) if Y = NONE return Y ∪ {X [1]} return NONE These two algorithms are examples of a general algorithmic technique called backtracking. You can imagine the algorithm searching through a binary tree of recursive possibilities like a maze, trying to 1 There’s no base case like the vacuous base case! 3 Algorithms Lecture 2: Backtracking find a hidden treasure ( T = 0), and backtracking whenever it reaches a dead end ( T < 0 or n = 0). For some problems, there are tricks that allow the recursive algorithm to recognize some branches as dead ends without exploring them directly, thereby speeding up the algorithm; two such problems are described later in these notes. Alas, SUBSETSUM is not one of the those problems; in the worst case, our algorithm explicitly considers every subset of X .2 2.3 Longest Increasing Subsequence Now suppose we are given a sequence of integers, and we want to find the longest subsequence whose elements are in increasing order. More concretely, the input is an array A[1 .. n] of integers, and we want to find the longest sequence of indices 1 ≤ i1 < i2 < · · · ik ≤ n such that A[i j ] < A[i j +1 ] for all j . To derive a recursive algorithm for this problem, we start with a recursive definition of the kinds of objects we’re playing with: sequences and subsequences. A sequence of integers is either empty or an integer followed by a sequence of integers. This definition suggests the following strategy for devising a recursive algorithm. If the input sequence is empty, there’s nothing to do. Otherwise, we should figure out what to do with the first element of the input sequence, and let the Recursion Fairy take care of everything else. We can formalize this strategy somewhat by giving a recursive definition of subsequence (using array notation to represent sequences): The only subsequence of the empty sequence is the empty sequence. A subsequence of A[1 .. n] is either a subsequence of A[2 .. n] or A[1] followed by a subsequence of A[2 .. n]. We’re not just looking for just any subsequence, but a longest subsequence with the property that elements are in increasing order. So let’s try to add those two conditions to our definition. (I’ll omit the familiar vacuous base case.) The LIS of A[1 .. n] is either the LIS of A[2 .. n] or A[1] followed by the LIS of A[2 .. n] with elements larger than A[1], whichever is longer. This definition is correct, but it’s not quite recursive—we’re defining ‘longest increasing subsequence’ in terms of the different ‘longest increasing subsequence with elements larger than x ’, which we haven’t properly defined yet. Fortunately, this second object has a very similar recursive definition. (Again, I’m omitting the vacuous base case.) If A[1] ≤ x , the LIS of A[1 .. n] with elements larger than x is the LIS of A[2 .. n] with elements larger than x . Otherwise, the LIS of A[1 .. n] with elements larger than x is either the LIS of A[2 .. n] with elements larger than x or A[1] followed by the LIS of A[2 .. n] with elements larger than A[1], whichever is longer. 2 Indeed, because SUBSETSUM is NP-hard, there is almost certainly no way to solve it in subexponential time. 4 Algorithms Lecture 2: Backtracking The longest increasing subsequence without restrictions can now be redefined as the longest increasing subsequence with elements larger than −∞. Rewriting this recursive definition into pseudocode gives us the following recursive algorithm. LIS(A[1 .. n]): return LISBIGGER(−∞, A[1 .. n]) LISBIGGER(prev, A[1 .. n]): if n = 0 return 0 else max ← LISBIGGER(prev, A[2 .. n]) if A[1] > pr ev L ← 1 + LISBIGGER(A[1], A[2 .. n]) if L > max max ← L return max The running time of this algorithm satisfies the recurrence T (n) ≤ 2 T (n − 1) + O(1), so as usual the annihilator method implies that T (n) = O(2n ). We really shouldn’t be surprised by this running time; in the worst case, the algorithm examines each of the 2n subsequences of the input array. The following alternative strategy avoids defining a new object with the ‘larger than x ’ constraint. We still only have to decide whether to include or exclude the first element A[1]. We consider the case where A[1] is excluded exactly the same way, but to consider the case where A[1] is included, we remove any elements of A[2 .. n] that are larger than A[1] before we recurse. This new strategy gives us the following algorithm: FILTER(A[1 .. n], x ): j←1 for i ← 1 to n if A[i ] > x B [ j ] ← A[i ]; j ← j + 1 return B [1 .. j ] LIS(A[1 .. n]): if n = 0 return 0 else max ← LIS(prev, A[2 .. n]) L ← 1 + LIS(A[1], FILTER(A[2 .. n], A[1])) if L > max max ← L return max The FILTER subroutine clearly runs in O(n) time, so the running time of LIS satisfies the recurrence T (n) ≤ 2 T (n − 1) + O(n), which solves to T (n) ≤ O(2n ) by the annihilator method. This upper bound pessimistically assumes that FILTER never actually removes any elements; indeed, if the input sequence is sorted in increasing order, this assumption is correct. 2.4 Optimal Binary Search Trees Our next example combines recursive backtracking with the divide-and-conquer strategy. Hopefully you remember that the cost of a successful search in a binary search tree is proportional to the number of ancestors of the target node.3 As a result, the worst-case search time is proportional to the depth of the tree. To minimize the worst-case search time, the height of the tree should be as small as possible; ideally, the tree is perfectly balanced. In many applications of binary search trees, it is more important to minimize the total cost of several searches than to minimize the worst-case cost of a single search. If x is a more ‘popular’ search target 3 An ancestor of a node v is either the node itself or an ancestor of the parent of v . A proper ancestor of v is either the parent of v or a proper ancestor of the parent of v . 5 Algorithms Lecture 2: Backtracking than y , we can save time by building a tree where the depth of x is smaller than the depth of y , even if that means increasing the overall depth of the tree. A perfectly balanced tree is not the best choice if some items are significantly more popular than others. In fact, a totally unbalanced tree of depth Ω(n) might actually be the best choice! This situation suggests the following problem. Suppose we are given a sorted array of n keys A[1 .. n] and an array of corresponding access frequencies f [1 .. n]. Over the lifetime of the search tree, we will search for the key A[i ] exactly f [i ] times. Our task is to build the binary search tree that minimizes the total search time. Before we think about how to solve this problem, we should first come up with a good recursive definition of the function we are trying to optimize! Suppose we are also given a binary search tree T with n nodes. Let vi denote the node that stores A[i ], and let r be the index of the root node. Ignoring constant factors, the cost of searching for A[i ] is the number of nodes on the path from the root vr to vi . Thus, the total cost of performing all the binary searches is given by the following expression: n Cost( T, f [1 .. n]) = f [i ] · #nodes between vr and vi i =1 Every search path includes the root node vr . If i < r , then all other nodes on the search path to vi are in the left subtree; similarly, if i > r , all other nodes on the search path to vi are in the right subtree. Thus, we can partition the cost function into three parts as follows: r −1 Cost( T, f [1 .. n]) = f [i ] · #nodes between left( vr ) and vi i =1 n + f [i ] i =1 n + f [i ] · #nodes between right( vr ) and vi i = r +1 Now the first and third summations look exactly like our original expression (*) for Cost( T, f [1 .. n]). Simple substitution gives us our recursive definition for Cost: n Cost( T, f [1 .. n]) = Cost(left( T ), f [1 .. r − 1]) + f [i ] + Cost(right( T ), f [ r + 1 .. n]) i =1 The base case for this recurrence is, as usual, n = 0; the cost of performing no searches in the empty tree is zero. Now our task is to compute the tree Topt that minimizes this cost function. Suppose we somehow magically knew that the root of Topt is vr . Then the recursive definition of Cost( T, f ) immediately implies that the left subtree left( Topt ) must be the optimal search tree for the keys A[1 .. r − 1] and access frequencies f [1 .. r − 1]. Similarly, the right subtree right( Topt ) must be the optimal search tree for the keys A[ r + 1 .. n] and access frequencies f [ r + 1 .. n]. Once we choose the correct key to store at the root, the Recursion Fairy will automatically construct the rest of the optimal tree for us. More formally, let OptCost( f [1 .. n]) denote the total cost of the optimal search tree for the given frequency counts. We immediately have the following recursive definition. n OptCost( f [1 .. n]) = min 1≤ r ≤n OptCost( f [1 .. r − 1]) + f [i ] + OptCost( f [ r + 1 .. n]) i =1 6 Algorithms Lecture 2: Backtracking Again, the base case is OptCost( f [1 .. 0]) = 0; the best way to organize no keys, which we will plan to search zero times, is by storing them in the empty tree! This recursive definition can be translated mechanically into a recursive algorithm, whose running time T (n) satisfies the recurrence n T (n) = Θ(n) + T ( k − 1) + T ( n − k ) . k =1 n The Θ(n) term comes from computing the total number of searches i =1 f [i ]. Yeah, that’s one ugly recurrence, but it’s actually easier to solve than it looks. To transform it into a more familiar form, we regroup and collect identical terms, subtract the recurrence for T (n − 1) to get rid of the summation, and then regroup again. n−1 T (n) = Θ(n) + 2 T (k) k =0 n−2 T (n − 1) = Θ(n − 1) + 2 T (k) k =0 T (n) − T (n − 1) = Θ(1) + 2 T (n − 1) T (n) = 3 T (n − 1) + Θ(1) The solution T (n ) = Θ(3n ) now follows from the annihilator method. Let me emphasize that this recursive algorithm does not examine all possible binary search trees. The number of binary search trees with n nodes satisfies the recurrence n−1 N ( n) = N ( r − 1) · N ( n − r ) , r =1 which has the closed-from solution N (n) = Θ(4n / n). Our algorithm saves considerable time by searching independently for the optimal left and right subtrees. A full enumeration of binary search trees would consider all possible pairings of left and right subtrees; hence the product in the recurrence for N (n). Exercises 1. (a) Let A[1 .. m] and B [1 .. n] be two arbitrary arrays. A common subsequence of A and B is both a subsequence of A and a subsequence of B . Give a simple recursive definition for the function lcs(A, B ), which gives the length of the longest common subsequence of A and B . (b) Let A[1 .. m] and B [1 .. n] be two arbitrary arrays. A common supersequence of A and B is another sequence that contains both A and B as subsequences. Give a simple recursive definition for the function scs(A, B ), which gives the length of the shortest common supersequence of A and B . (c) Call a sequence X [1 .. n] oscillating if X [i ] < X [i + 1] for all even i , and X [i ] > X [i + 1] for all odd i . Give a simple recursive definition for the function los(A), which gives the length of the longest oscillating subsequence of an arbitrary array A of integers. (d) Give a simple recursive definition for the function sos(A), which gives the length of the shortest oscillating supersequence of an arbitrary array A of integers. 7 Algorithms Lecture 2: Backtracking (e) Call a sequence X [1 .. n] accelerating if 2 · X [i ] < X [i − 1] + X [i + 1] for all i . Give a simple recursive definition for the function lxs(A), which gives the length of the longest accelerating subsequence of an arbitrary array A of integers. For more backtracking exercises, see the next two lecture notes! c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 8 Algorithms Non-Lecture B: Efficient Exponential-Time Algorithms Wouldn’t the sentence “I want to put a hyphen between the words Fish and And and And and Chips in my Fish-And-Chips sign.” have been clearer if quotation marks had been placed before Fish, and between Fish and and, and and and And, and And and and, and and and And, and And and and, and and and Chips, as well as after Chips?1 — unknown, from the original FreeBSD ‘fortune’ file B Efficient Exponential-Time Algorithms In another lecture note, we discuss the class of NP-hard problems. For every problem in this class, the fastest algorithm anyone knows has an exponential running time. Moreover, there is very strong evidence (but alas, no proof) that it is impossible to solve any NP-hard problem in less than exponential time—it’s not that we’re all stupid; the problems really are that hard! Unfortunately, an enormous number of problems that arise in practice are NP-hard; for some of these problems, even approximating the right answer is NP-hard. Suppose we absolutely have to find the exact solution to some NP-hard problem. A polynomial-time algorithm is almost certainly out of the question; the best running time we can hope for is exponential. But which exponential? An algorithm that runs in O(1.5n ) time, while still unusable for large problems, is still significantly better than an algorithm that runs in O(2n ) time! For most NP-hard problems, the only approach that is guaranteed to find an optimal solution is recursive backtracking. The most straightforward version of this approach is to recursively generate all possible solutions and check each one: all satisfying assignments, or all vertex colorings, or all subsets, or all permutations, or whatever. However, most NP-hard problems have some additional structure that allows us to prune away most of the branches of the recursion tree, thereby drastically reducing the running time. B.1 3SAT Let’s consider the mother of all NP-hard problems: 3SAT. Given a boolean formula in conjunctive normal form, with at most three literals in each clause, our task is to determine whether any assignment of values of the variables makes the formula true. Yes, this problem is NP-hard, which means that a polynomial algorithm is almost certainly impossible. Too bad; we have to solve the problem anyway. The trivial solution is to try every possible assignment. We’ll evaluate the running time of our 3SAT algorithms in terms of the number of variables in the formula, so let’s call that n. Provided any clause appears in our input formula at most once—a condition that we can easily enforce in polynomial time—the overall input size is O(n3 ). There are 2n possible assignments, and we can evaluate each assignment in O(n3 ) time, so the overall running time is O(2n n3 ). Since polynomial factors like n3 are essentially noise when the overall running time is exponential, from now on I’ll use poly(n) to represent some arbitrary polynomial in n; in other words, poly(n) = nO(1) . For example, the trivial algorithm for 3SAT runs in time O(2n poly(n)). We can make this algorithm smarter by exploiting the special recursive structure of 3CNF formulas: A 3CNF formula is either nothing or a clause with three literals ∧ a 3CNF formula 1 If you ever decide to read this sentence out loud, be sure to pause briefly between ‘Fish and and’ and ‘and and and And’, ‘and and and And’ and ‘and And and and’, ‘and And and and’ and ‘and and and And’, ‘and and and And’ and ‘and And and and’, and ‘and And and and’ and ‘and and and Chips’! 1 Algorithms Non-Lecture B: Efficient Exponential-Time Algorithms Suppose we want to decide whether some 3CNF formula Φ with n variables is satisfiable. Of course this is trivial if Φ is the empty formula, so suppose Φ = ( x ∨ y ∨ z) ∧ Φ for some literals x , y, z and some 3CNF formula Φ . By distributing the ∧ across the ∨s, we can rewrite Φ as follows: Φ = ( x ∧ Φ ) ∨ ( y ∧ Φ ) ∨ (z ∧ Φ ) For any boolean formula Ψ and any literal x , let Ψ| x (pronounced “sigh given eks") denote the simpler boolean formula obtained by assuming x is true. It’s not hard to prove by induction (hint, hint) that x ∧ Ψ = x ∧ Ψ| x , which implies that Φ = ( x ∧ Φ | x ) ∨ ( y ∧ Φ | y ) ∨ (z ∧ Φ |z ). Thus, in any satisfying assignment for Φ, either x is true and Φ | x is satisfiable, or y is true and Φ | y is satisfiable, or z is true and Φ |z is satisfiable. Each of the smaller formulas has at most n − 1 variables. If we recursively check all three possibilities, we get the running time recurrence T (n) ≤ 3 T (n − 1) + poly(n), whose solution is O(3n poly(n)). So we’ve actually done worse! But these three recursive cases are not mutually exclusive! If Φ | x is not satisfiable, then x must be false in any satisfying assignment for Φ. So instead of recursively checking Φ | y in the second step, we ¯ ¯ can check the even simpler formula Φ | x y . Similarly, if Φ | x y is not satisfiable, then we know that y ¯y must be false in any satisfying assignment, so we can recursively check Φ | x ¯ z in the third step. 3SAT(Φ): if Φ = ∅ return TRUE ( x ∨ y ∨ z) ∧ Φ ← Φ if 3SAT(Φ| x ) return TRUE ¯ if 3SAT(Φ| x y ) return TRUE ¯y return 3SAT(Φ| x ¯ z ) The running time off this algorithm obeys the recurrence T (n) = T (n − 1) + T (n − 2) + T (n − 3) + poly(n), where poly(n) denotes the polynomial time required to simplify boolean formulas, handle control flow, move stuff into and out of the recursion stack, and so on. The annihilator method gives us the solution T (n) = O(λn poly(n)) = O(1.83928675522n ) where λ ≈ 1.83928675521 . . . is the largest root of the characteristic polynomial r 3 − r 2 − r − 1. (Notice that we cleverly eliminated the polynomial noise by increasing the base of the exponent ever so slightly.) We can improve this algorithm further by eliminating pure literals from the formula before recursing. ¯ A literal x is pure in if it appears in the formula Φ but its negation x does not. It’s not hard to prove (hint, 2 Algorithms Non-Lecture B: Efficient Exponential-Time Algorithms hint) that if Φ has a satisfying assignment, then it has a satisfying assignment where every pure literal is ¯ true. If Φ = ( x ∨ y ∨ z ) ∧ Φ has no pure literals, then some in Φ contains the literal x , so we can write ¯ Φ = ( x ∨ y ∨ z) ∧ ( x ∨ u ∨ v) ∧ Φ ¯ for some literals u and v (each of which might be y , ¯ , z , or z ). It follows that the first recursive formula y Φ| x has contains the clause (u ∨ v ). We can recursively eliminate the variables u and v just as we tested the variables y and x in the second and third cases of our previous algorithm: ¯ Φ| x = (u ∨ v ) ∧ Φ | x = (u ∧ Φ | xu) ∨ ( v ∧ Φ | x u v ). Here is our new faster algorithm: 3SAT(Φ): if Φ = ∅ return TRUE if Φ has a pure literal x return 3SAT(Φ| x ) ¯ ( x ∨ y ∨ z) ∧ ( x ∨ u ∨ v) ∧ Φ ← Φ if 3SAT(Φ| xu) return TRUE ¯ if 3SAT(Φ| x u v ) return TRUE ¯ if 3SAT(Φ| x y ) return TRUE ¯y return 3SAT(Φ| x ¯ z ) The running time T (n) of this new algorithm satisfies the recurrence T (n) = 2 T (n − 2) + 2 T (n − 3) + poly(n), and the annihilator method implies that T (n) = O(µn poly(n)) = O(1.76929235425n ) where µ ≈ 1.76929235424 . . . is the largest root of the characteristic polynomial r 3 − 2 r − 2. Naturally, this approach can be extended much further. As of 2004, the fastest (deterministic) algorithm for 3SAT runs in O(1.473n ) time2 , but there is absolutely no reason to believe that this is the best possible. B.2 Maximum Independent Set Now suppose we are given an undirected graph G and are asked to find the size of the largest independent set, that is, the largest subset of the vertices of G with no edges between them. Once again, we have an obvious recursive algorithm: Try every subset of nodes, and return the largest subset with no edges. Expressed recursively, the algorithm might look like this. 2 Tobias Brueggemann and Walter Kern. An improved deterministic local search algorithm for 3-SAT. Theoretical Computer Science 329(1–3):303–313, 2004. 3 Algorithms Non-Lecture B: Efficient Exponential-Time Algorithms MAXIMUMINDSETSIZE(G ): if G = ∅ return 0 else v ← any node in G withv ← 1 + MAXIMUMINDSETSIZE(G \ N ( v )) withoutv ← MAXIMUMINDSETSIZE(G \ { v }) return max{withv, withoutv}. Here, N ( v ) denotes the neighborhood of v : The set containing v and all of its neighbors. Our algorithm is exploiting the fact that if an independent set contains v , then by definition it contains none of v ’s neighbors. In the worst case, v has no neighbors, so G \ { v } = G \ N ( v ). Thus, the running time of this algorithm satisfies the recurrence T (n) = 2 T (n − 1) + poly(n) = O(2n poly(n)). Surprise, surprise. This algorithm is mirroring a crude recursive upper bound for the number of maximal independent sets in a graph; an independent set is maximal if every vertex in G is either already in the set or a neighbor of a vertex in the set. If the graph is non-empty, then every maximal independent set either includes or excludes each vertex. Thus, the number of maximal independent sets satisfies the recurrence M (n) ≤ 2 M (n − 1), with base case M (1) = 1. The annihilator method gives us M (n) ≤ 2n − 1. The only subset that we aren’t counting with this upper bound is the empty set! We can improve this upper bound by more carefully examining the worst case of the recurrence. If v has no neighbors, then N ( v ) = { v }, and both recursive calls consider a graph with n − 1 nodes. But in this case, v is in every maximal independent set, so one of the recursive calls is redundant. On the other hand, if v has at least one neighbor, then G \ N ( v ) has at most n − 2 nodes. So now we have the following recurrence. M (n) ≤ max M ( n − 1) M ( n − 1) + M ( n − 2) = O(1.61803398875n ) The upper bound is derived by solving each case separately using the annihilator method and taking the worst of the two cases. The first case gives us M (n) = O(1); the second case yields our old friends the Fibonacci numbers. We can improve this bound even more by examining the new worst case: v has exactly one neighbor w . In this case, either v or w appears in any maximal independent set. Thus, instead of recursively searching in G \ { v }, we should recursively search in G \ N (w ), which has at most n − 1 nodes. On the other hand, if G has no nodes with degree 1, then G \ N ( v ) has at most n − 3 nodes. M ( n − 1) 2 M ( n − 2) M (n) ≤ max = O(1.46557123188n ) M ( n − 1) + M ( n − 3) The base of the exponent is the largest root of the characteristic polynomial r 3 − r 2 − 1. The second case n implies a bound of O( 2 ) = O(1.41421356237n ), which is smaller. We can apply this improvement technique one more time. If G has a node v with degree 3 or more, then G \ N ( v ) has at most n − 4 nodes. Otherwise (since we have already considered nodes of degree 0 and 1), every node in the graph has degree 2. Let u, v, w be a path of three nodes in G (possibly with u adjacent to w ). In any maximal independent set, either v is present and u, w are absent, or u is present and its two neighbors are absent, or w is present and its two neighbors are absent. In all three cases, we recursively count maximal independent sets in a graph with n − 3 nodes. 4 Algorithms Non-Lecture B: Efficient Exponential-Time Algorithms M ( n − 1) 2 M ( n − 2) M (n) ≤ max = O(3n/3 ) = O(1.44224957031n ) M (n − 1) + M (n − 4) 3 M ( n − 3) The third case implies a bound of O(1.3802775691n ), where the base is the largest root of r 4 − r 3 − 1. Unfortunately, we cannot apply the same improvement trick again. A graph consisting of n/3 triangles (cycles of length three) has exactly 3n/3 maximal independent sets, so our upper bound is tight in the worst case. Now from this recurrence, we can derive an efficient algorithm to compute the largest independent set in G in O(3n/3 poly(n)) = O(1.44224957032n ) time. MAXIMUMINDSETSIZE(G ): if G = ∅ return 0 else if G has a node v with degree 0 return 1 + MAXIMUMINDSETSIZE(G \ { v }) else if G has a node v with degree 1 w ← v ’s neighbor withv ← 1 + MAXIMUMINDSETSIZE(G \ N ( v )) withw ← 1 + MAXIMUMINDSETSIZE(G \ N (w )) return max{withv, withw} 〈〈n − 1〉〉 〈〈n − 2〉〉 〈〈≤ n − 2〉〉 else if G has a node v with degree greater than 2 withv ← 1 + MAXIMUMINDSETSIZE(G \ N ( v )) 〈〈≤ n − 4〉〉 withoutv ← MAXIMUMINDSETSIZE(G \ { v }) 〈〈≤ n − 1〉〉 return max{withv, withoutv} else 〈〈every node in G has degree 2〉〉 v ← any node; u, w ← v ’s neighbors withu ← 1 + MAXIMUMINDSETSIZE(G \ N (u)) withv ← 1 + MAXIMUMINDSETSIZE(G \ N ( v )) withw ← 1 + MAXIMUMINDSETSIZE(G \ N (w )) return max{withu, withv, withw} 〈〈≤ n − 3〉〉 〈〈≤ n − 3〉〉 〈〈≤ n − 3〉〉 Exercises 1. Describe an algorithm to solve 3SAT in time O(φ n poly(n)), where φ = (1 + 5)/2 ≈ 1.618034. [Hint: Prove that in each recursive call, either you have just eliminated a pure literal, or the formula has a clause with at most two literals. What recurrence leads to this running time?] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 5 Algorithms Lecture 3: Dynamic Programming Those who cannot remember the past are doomed to repeat it. — George Santayana, The Life of Reason, Book I: Introduction and Reason in Common Sense (1905) The 1950s were not good years for mathematical research. We had a very interesting gentleman in Washington named Wilson. He was secretary of Defense, and he actually had a pathological fear and hatred of the word ‘research’. I’m not using the term lightly; I’m using it precisely. His face would suffuse, he would turn red, and he would get violent if people used the term ‘research’ in his presence. You can imagine how he felt, then, about the term ‘mathematical’. The RAND Corporation was employed by the Air Force, and the Air Force had Wilson as its boss, essentially. Hence, I felt I had to do something to shield Wilson and the Air Force from the fact that I was really doing mathematics inside the RAND Corporation. What title, what name, could I choose? — Richard Bellman, on the origin of his term ‘dynamic programming’ (1984) If we all listened to the professor, we may be all looking for professor jobs. — Pittsburgh Steelers’ head coach Bill Cowher, responding to David Romer’s dynamic-programming analysis of football strategy (2003) 3 Dynamic Programming 3.1 Fibonacci Numbers The Fibonacci numbers Fn , named after Leonardo Fibonacci Pisano1 , the mathematician who popularized ‘algorism’ in Europe in the 13th century, are defined as follows: F0 = 0, F1 = 1, and Fn = Fn−1 + Fn−2 for all n ≥ 2. The recursive definition of Fibonacci numbers immediately gives us a recursive algorithm for computing them: RECFIBO(n): if (n < 2) return n else return RECFIBO(n − 1) + RECFIBO(n − 2) How long does this algorithm take? Except for the recursive calls, the entire algorithm requires only a constant number of steps: one comparison and possibly one addition. If T (n) represents the number of recursive calls to RECFIBO, we have the recurrence T (0) = 1, T (1) = 1, T (n) = T (n − 1) + T (n − 2) + 1. This looks an awful lot like the recurrence for Fibonacci numbers! The annihilator method gives us an asymptotic bound of Θ(φ n ), where φ = ( 5 + 1)/2 ≈ 1.61803398875, the so-called golden ratio, is the largest root of the polynomial r 2 − r − 1. But it’s fairly easy to prove (hint, hint) the exact solution T (n ) = 2 Fn +1 − 1. In other words, computing Fn using this algorithm takes more than twice as many steps as just counting to Fn ! Another way to see this is that the RECFIBO is building a big binary tree of additions, with nothing but zeros and ones at the leaves. Since the eventual output is Fn , our algorithm must call RECRIBO(1) (which returns 1) exactly Fn times. A quick inductive argument implies that RECFIBO(0) is called exactly Fn−1 times. Thus, the recursion tree has Fn + Fn−1 = Fn+1 leaves, and therefore, because it’s a full binary tree, it must have 2 Fn+1 − 1 nodes. 1 literally, “Leonardo, son of Bonacci, of Pisa” 1 Algorithms 3.2 Lecture 3: Dynamic Programming Memo(r)ization and Dynamic Programming The obvious reason for the recursive algorithm’s lack of speed is that it computes the same Fibonacci numbers over and over and over. A single call to RECURSIVEFIBO(n) results in one recursive call to RECURSIVEFIBO(n − 1), two recursive calls to RECURSIVEFIBO(n − 2), three recursive calls to RECURSIVEFIBO(n − 3), five recursive calls to RECURSIVEFIBO(n − 4), and in general, Fk−1 recursive calls to RECURSIVEFIBO(n − k), for any 0 ≤ k < n. For each call, we’re recomputing some Fibonacci number from scratch. We can speed up the algorithm considerably just by writing down the results of our recursive calls and looking them up again if we need them later. This process is called memoization.2 MEMFIBO(n): if (n < 2) return n else if F [n] is undefined F [n] ← MEMFIBO(n − 1) + MEMFIBO(n − 2) return F [n] If we actually trace through the recursive calls made by MEMFIBO, we find that the array F [ ] is filled from the bottom up: first F [2], then F [3], and so on, up to F [n]. Once we see this pattern, we can replace the recursion with a simple for-loop that fills the array in order, instead of relying on the complicated recursion to do it for us. This gives us our first explicit dynamic programming algorithm. ITERFIBO(n): F [0] ← 0 F [1] ← 1 for i ← 2 to n F [ i ] ← F [ i − 1] + F [ i − 2] return F [n] ITERFIBO clearly takes only O(n) time and O(n) space to compute Fn , an exponential speedup over our original recursive algorithm. We can reduce the space to O(1) by noticing that we never need more than the last two elements of the array: ITERFIBO2(n): prev ← 1 curr ← 0 for i ← 1 to n next ← curr + prev prev ← curr curr ← next return curr (This algorithm uses the non-standard but perfectly consistent base case F−1 = 1 so that ITERFIBO2(0) returns the correct value 0.) Even this algorithm can be improved further. There’s a faster algorithm defined in terms of matrix multiplication, using the following wonderful fact: 01 11 2 x y = y x+y “My name is Elmer J. Fudd, millionaire. I own a mansion and a yacht.” 2 Algorithms Lecture 3: Dynamic Programming In other words, multiplying a two-dimensional vector by the matrix 0 1 does exactly the same thing 11 as one iteration of the inner loop of ITERFIBO2. This might lead us to believe that multiplying by the matrix n times is the same as iterating the loop n times: 01 11 n 1 = 0 Fn−1 . Fn A quick inductive argument proves this fact. So if we want the nth Fibonacci number, we just have to compute the nth power of the matrix 0 1 . 11 If we use repeated squaring, computing the nth power of something requires only O(log n) multiplications. In this case, that means O(log n) 2 × 2 matrix multiplications, each of which reduces to a constant number of integer multiplications and additions. Thus, we can compute Fn in only O(log n) integer arithmetic operations. This is an exponential speedup over the standard iterative algorithm, which was already an exponential speedup over our original recursive algorithm. Right? 3.3 Uh. . . wait a minute. Well, not exactly. Fibonacci numbers grow exponentially fast. The nth Fibonacci number is approximately n log10 φ ≈ n/5 decimal digits long, or n log2 φ ≈ 2n/3 bits. So we can’t possibly compute Fn in logarithmic time — we need Ω(n) time just to write down the answer! I’ve been cheating by assuming we can do arbitrary-precision arithmetic in constant time. If we use fast Fourier transforms, multiplying two n-digit numbers takes O(n log n) time. Thus, the matrix-based algorithm’s actual running time is given by the recurrence T (n) = T ( n/2 ) + O(n log n), which solves to T (n) = O(n log n) by the Master Theorem. Is this slower than our “linear-time” iterative algorithm? No! Addition isn’t free, either. Adding two n-digit numbers takes O(n) time, so the running time of the iterative algorithm is O(n2 ). (Do you see why?) Our matrix algorithm really is faster than our iterative algorithm, but not exponentially faster. In the original recursive algorithm, the extra cost of arbitrary-precision arithmetic is overwhelmed by the huge number of recursive calls. The correct recurrence is T (n) = T (n − 1) + T (n − 2) + O(n), which still has the solution O(φ n ), by the annihilator method. 3.4 The Pattern: Smart Recursion In a nutshell, dynamic programming is recursion without repetition. Developing a dynamic programming algorithm almost always requires two distinct stages. 1. Formulate the problem recursively. Write down a recursive formula or algorithm for the whole problem in terms of the answers to smaller subproblems. This is the hard part. 2. Build solutions to your recurrence from the bottom up. Write an algorithm that starts with the base cases of your recurrence and works its way up to the final solution, by considering intermediate subproblems in the correct order. This stage can be broken down into several smaller, relatively mechanical steps: 3 Algorithms Lecture 3: Dynamic Programming (a) Identify the subproblems. What are all the different ways can your recursive algorithm call itself, starting with some initial input? For example, the argument to RECFIBO is always an integer between 0 and n. (b) Choose a data structure to memoize intermediate results. For most problems, each recursive subproblem can be identified by a few integers, so you can use a multidimensional array. For some problems, however, a more complicated data structure is required. (c) Analyze running time and space. The number of possible distinct subproblems determines the space complexity of your memoized algorithm. To compute the time complexity, add up the running times of all possible subproblems, ignoring the recursive calls. For example, if we already know Fi −1 and Fi −2 , we can compute Fi in O(1) time, so computing the first n Fibonacci numbers takes O(n) time. (d) Identify dependencies between subproblems. Except for the base cases, every recursive subproblem depends on other subproblems—which ones? Draw a picture of your data structure, pick a generic element, and draw arrows from each of the other elements it depends on. Then formalize your picture. (e) Find a good evaluation order. Order the subproblems so that each subproblem comes after the subproblems it depends on. Typically, this means you should consider the base cases first, then the subproblems that depends only on base cases, and so on. More formally, the dependencies you identified in the previous step define a partial order over the subproblems; in this step, you need to find a linear extension of that partial order. Be careful! (f) Write down the algorithm. You know what order to consider the subproblems, and you know how to solve each subproblem. So do that! If your data structure is an array, this usually means writing a few nested for-loops around your original recurrence. Of course, you have to prove that each of these steps is correct. If your recurrence is wrong, or if you try to build up answers in the wrong order, your algorithm won’t work! Dynamic programming algorithms store the solutions of intermediate subproblems, often but not always in some kind of array or table. Many algorithms students make the mistake of focusing on the table (because tables are easy and familiar) instead of the much more important (and difficult) task of finding a correct recurrence. Dynamic programming is not about filling in tables; it’s about smart recursion. As long as we memoize the correct recurrence, an explicit table isn’t really necessary, but if the recursion is incorrect, nothing works. 3.5 Edit Distance The edit distance between two words—sometimes also called the Levenshtein distance—is the minimum number of letter insertions, letter deletions, and letter substitutions required to transform one word into another. For example, the edit distance between FOOD and MONEY is at most four: FOOD → MOOD → MON∧D → MONED → MONEY A better way to display this editing process is to place the words one above the other, with a gap in the first word for every insertion, and a gap in the second word for every deletion. Columns with two different characters correspond to substitutions. Thus, the number of editing steps is just the number of columns that don’t contain the same character twice. 4 Algorithms Lecture 3: Dynamic Programming F M O O O N E D Y It’s fairly obvious that you can’t get from FOOD to MONEY in three steps, so their edit distance is exactly four. Unfortunately, this is not so easy in general. Here’s a longer example, showing that the distance between ALGORITHM and ALTRUISTIC is at most six. Is this optimal? A A L L G O T R R U I I S T T H I M C To develop a dynamic programming algorithm to compute the edit distance between two strings, we first need to develop a recursive definition. Our gap representation for edit sequences has a crucial “optimal substructure” property. Suppose we have the gap representation for the shortest edit sequence for two strings. If we remove the last column, the remaining columns must represent the shortest edit sequence for the remaining substrings. We can easily prove this by contradiction. If the substrings had a shorter edit sequence, we could just glue the last column back on and get a shorter edit sequence for the original strings. Once we figure out what should go in the last column, the Recursion Fairy will magically give us the rest of the optimal gap representation. So let’s recursively define the edit distance between two strings A[1 .. m] and B [1 .. n], which we denote by Edit(A[1 .. m], B [1 .. n]). If neither string is empty, there are three possibilities for the last column in the shortest edit sequence: • Insertion: The last entry in the bottom row is empty. In this case, the edit distance is equal to Edit(A[1 .. m − 1], B [1 .. n]) + 1. The +1 is the cost of the final insertion, and the recursive expression gives the minimum cost for the other columns. • Deletion: The last entry in the top row is empty. In this case, the edit distance is equal to Edit(A[1 .. m], B [1 .. n − 1]) + 1. The +1 is the cost of the final deletion, and the recursive expression gives the minimum cost for the other columns. • Substitution: Both rows have characters in the last column. If the characters are the same, the substitution is free, so the edit distance is equal to Edit(A[1 .. m − 1], B [1 .. n − 1]). If the characters are different, then the edit distance is equal to Edit(A[1 .. m − 1], B [1 .. n − 1]) + 1. The edit distance between A and B is the smallest of these three possibilities:3 Edit(A[1 .. m], B [1 .. n − 1]) + 1 Edit(A[1 .. m], B [1 .. n]) = min Edit(A[1 .. m − 1], B [1 .. n]) + 1 Edit(A[1 .. m − 1], B [1 .. n − 1]) + A[m] = B [n] This recurrence has two easy base cases. The only way to convert the empty string into a string of n characters is by performing n insertions. Similarly, the only way to convert a string of m characters into the empty string is with m deletions, Thus, if denotes the empty string, we have Edit(A[1 .. m], ) = m, Edit( , B [1 .. n]) = n. Both of these expressions imply the trivial base case Edit( , ) = 0. 3 Once again, I’m using Iverson’s bracket notation P to denote the indicator variable for the logical proposition P , which has value 1 if P is true and 0 if P is false. 5 Algorithms Lecture 3: Dynamic Programming Now notice that the arguments to our recursive subproblems are always prefixes of the original strings A and B . Thus, we can simplify our notation considerably by using the lengths of the prefixes, instead of the prefixes themselves, as the arguments to our recursive function. So let’s write Edit(i , j ) as shorthand for Edit(A[1 .. i ], B [1 .. j ]). This function satisfies the following recurrence: if j = 0 Edit(i , j ) = i j if i = 0 Edit(i − 1, j ) + 1, min Edit(i , j − 1) + 1, Edit(i − 1, j − 1) + A[i ] = B [ j ] otherwise The edit distance between the original strings A and B is just Edit(m, n). This recurrence translates directly into a recursive algorithm. Just out of curiosity, we can analyze the running time of this algorithm by solving the following recurrence:4 T ( m , n) = O(1) if n = 0 or m = 0, T (m, n − 1) + T (m − 1, n) + T (n − 1, m − 1) + O(1) otherwise. I don’t know of a general closed-form solution for this mess, but we can derive an upper bound by defining a new function O(1) T (N ) = max T (n, m) = if N = 0, 2 T (N − 1) + T (N − 2) + O(1) otherwise. n + m= N The annihilator method implies that T (N ) = O((1 + 2)N ). Thus, the running time of our recursive edit-distance algorithm is at most T (n + m) = O((1 + 2)n+m ). We can dramatically reduce the running time of this algorithm down by memoization. Because each recursive subproblem can be identified by two indices i and j , we can store the intermediate values in a two-dimensional array Edit[0 .. m , 0 .. n ]. Note that the index ranges start at zero to accommodate the base cases. Since there are Θ(mn) entries in the table, our memoized algorithm uses Θ(mn ) space. Since each entry in the table can be computed in Θ(1) time once we know its predecessors, our memoized algorithm runs in Θ(mn ) time. It’s not immediately clear what order the recursive algorithm fills the rest of the table; all we can tell from the recurrence is that each entry Edit[i , j ] is filled in after the neighboring entries directly above, directly to the left, and both above and to the left. But just this partial information is enough to give us an explicit evaluation order. If we fill in our table in the standard row-major order—row by row from top down, each row from left to right—then whenever we reach an entry in the table, the entries it depends on are already available. j i Dependencies in the memoization table for edit distance, and a legal evaluation order 4 You can skip this step. Since we’re going to memoize this algorithm, the running time of the non-memoized algorithm is not particularly important. In particular, we won’t ask you do do this on homeworks or exams. 6 Algorithms Lecture 3: Dynamic Programming Putting everything together, we obtain the following dynamic programming algorithm: EDITDISTANCE(A[1 .. m], B [1 .. n]): for j ← 1 to n Edit[0, j ] ← j for i ← 1 to m Edit[i , 0] ← i for j ← 1 to n if A[i ] = B [ j ] Edit[i , j ] ← min Edit[i − 1, j ] + 1, Edit[i , j − 1] + 1, Edit[i − 1, j − 1] else Edit[i , j ] ← min Edit[i − 1, j ] + 1, Edit[i , j − 1] + 1, Edit[i − 1, j − 1] + 1 return Edit[m, n] Here’s the resulting table for ALGORITHM → ALTRUISTIC. Bold numbers indicate places where characters in the two strings are equal. The arrows represent the predecessor(s) that actually define each entry. Each direction of arrow corresponds to a different edit operation: horizontal=deletion, vertical=insertion, and diagonal=substitution. Bold diagonal arrows indicate “free” substitutions of a letter for itself. Any path of arrows from the top left corner to the bottom right corner of this table represents an optimal edit sequence between the two strings. (There can be many such paths.) Moreover, since we can compute these arrows in a post-processing phase from the values stored in the table, we can reconstruct the actual optimal editing sequence in O(n + m) additional time. A L T R U I S T I C ALGORITHM 0 → 1 →2→ 3 → 4 → 5 →6→ 7 → 8 → 9 ↓ 1 0 →1→ 2 → 3 → 4 →5→ 6 → 7 → 8 ↓↓ 2 1 0→ 1 → 2 → 3 → 4 → 5 → 6 → 7 ↓↓↓ 3 2 1 1 → 2 → 3 → 4 →4→ 5 → 6 ↓↓↓↓ 4 3 2 2 2 2 →3→ 4 → 5 → 6 ↓↓↓↓↓↓ 5 4 3 3 3 3 3→ 4 → 5 → 6 ↓↓↓↓↓↓ 6 5 4 4 4 4 3→ 4 → 5 → 6 ↓↓↓↓↓↓↓ 7655554456 ↓↓↓↓↓↓↓ 8 7 6 6 6 6 5 4→ 5 → 6 ↓↓↓↓↓↓↓↓ 9 8 7 7 7 7 6 5 5→ 6 ↓↓↓↓↓↓↓↓↓ 10 9 8 8 8 8 7 6 6 6 The edit distance between ALGORITHM and ALTRUISTIC is indeed six. There are three paths through this table from the top left to the bottom right, so there are three optimal edit sequences: A A L L G T A A L L G A A L L G T O R R U I I T T S H I M C O T R R U I I S T T H I M C O R R U I I S T T H I M C 7 Algorithms 3.6 Lecture 3: Dynamic Programming Warning: Greed is Stupid If we’re very very very very lucky, we can bypass all the recurrences and tables and so forth, and solve the problem using a greedy algorithm. The general greedy strategy is look for the best first step, take it, and then continue. While this approach seems very natural, it almost never works; optimization problems that can be solved correctly by a greedy algorithm are very rare. Nevertheless, for many problems that should be solved by dynamic programming, many students’ first intuition is to apply a greedy strategy. For example, a greedy algorithm for the edit distance problem might look for the longest common substring of the two strings, match up those substrings (since those substitutions don’t cost anything), and then recursively look for the edit distances between the left halves and right halves of the strings. If there is no common substring—that is, if the two strings have no characters in common—the edit distance is clearly the length of the larger string. If this sounds like a stupid hack to you, pat yourself on the back. It isn’t even close to the correct solution. Everyone should tattoo the following sentence on the back of their hands, right under all the rules about logarithms and big-Oh notation: Greedy algorithms almost never work! Use dynamic programming instead! What, never? No, never! What, never? Well. . . hardly ever.5 A different lecture note describes the effort required to prove that greedy algorithms are correct, in the rare instances when they are. You will not receive any credit for any greedy algorithm for any problem in this class without a formal proof of correctness. We’ll push through the formal proofs for several greedy algorithms later in semester. 3.7 Dynamic Programming on Trees So far, all of our dynamic programming example use a multidimensional array to store the results of recursive subproblems. However, as the next example shows, this is not always the most appropriate date structure to use. A independent set in a graph is a subset of the vertices that have no edges between them. Finding the largest independent set in an arbitrary graph is extremely hard; in fact, this is one of the canonical NP-hard problems described in another lecture note. But from some special cases of graphs, we can find the largest independent set efficiently. In particular, when the input graph is a tree (a connected and acyclic graph) with n vertices, we can compute the largest independent set in O(n) time. In the recursion notes, we saw a recursive algorithm for computing the size of the largest independent set in an arbitrary graph: MAXIMUMINDSETSIZE(G ): if G = ∅ return 0 v ← any node in G withv ← 1 + MAXIMUMINDSETSIZE(G \ N ( v )) withoutv ← MAXIMUMINDSETSIZE(G \ { v }) return max{withv, withoutv}. 5 Greedy hardly ever ever works! Then give three cheers, and one cheer more, for the hardy Captain of the Pinafore! Then give three cheers, and one cheer more, for the Captain of the Pinafore! 8 Algorithms Lecture 3: Dynamic Programming Here, N ( v ) denotes the neighborhood of v : the set containing v and all of its neighbors. As we observed in the other lecture notes, this algorithm has a worst-case running time of O(2n poly(n)), where n is the number of vertices in the input graph. Now suppose we require that the input graph is a tree; we will call this tree T instead of G from now on. We need to make a slight change to the algorithm to make it truly recursive. The subgraphs T \ { v } and T \ N ( v ) are forests, which may have more than one component. But the largest independent set in a disconnected graph is just the union of the largest independent sets in its components, so we can separately consider each tree in these forests. Fortunately, this has the added benefit of making the recursive algorithm more efficient, especially if we can choose the node v such that the trees are all significantly smaller than T . Here is the modified algorithm: MAXIMUMINDSETSIZE( T ): if T = ∅ return 0 v ← any node in T withv ← 1 for each tree T in T \ N ( v ) withv ← withv + MAXIMUMINDSETSIZE( T ) withoutv ← 0 for each tree T in T \ { v } withoutv ← withoutv + MAXIMUMINDSETSIZE( T ) return max{withv, withoutv}. Now let’s try to memoize this algorithm. Each recursive subproblem considers a subtree (that is, a connected subgraph) of the original tree T . Unfortunately, a single tree T can have exponentially many subtrees, so we seem to be doomed from the start! Fortunately, there’s a degree of freedom that we have not yet exploited: We get to choose the vertex v . We need a recipe—an algorithm!—for choosing v in each subproblem that limits the number of different subproblems the algorithm considers. To make this work, we impose some additional structure on the original input tree. Specifically, we declare one of the vertices of T to be the root, and we orient all the edges of T away from that root. Then we let v be the root of the input tree; this choice guarantees that each recursive subproblem considers a rooted subtree of T . Each vertex in T is the root of exactly one subtree, so now the number of distinct subproblems is exactly n. We can further simplify the algorithm by only passing a single node instead of the entire subtree: MAXIMUMINDSETSIZE( v ): withv ← 1 for each grandchild x of v withv ← withv + MAXIMUMINDSETSIZE( x ) withoutv ← 0 for each child w of v withoutv ← withoutv + MAXIMUMINDSETSIZE(w ) return max{withv, withoutv}. What data structure should we use to store intermediate results? The most natural choice is the tree itself! Specifically, for each node v , we store the result of MAXIMUMINDSETSIZE( v ) in a new field v. MIS. (We could use an array, but then we’d have to add a new field to each node anyway, pointing to the corresponding array entry. Why bother?) What’s the running time of the algorithm? The non-recursive time associated with each node v is proportional to the number of children and grandchildren of v ; this number can be very different from 9 Algorithms Lecture 3: Dynamic Programming one vertex to the next. But we can turn the analysis around: Each vertex contributes a constant amount of time to its parent and its grandparent! Since each vertex has at most one parent and at most one grandparent, the total running time is O(n). What’s a good order to consider the subproblems? The subproblem associated with any node v depends on the subproblems associated with the children and grandchildren of v . So we can visit the nodes in any order, provided that all children are visited before their parent. In particular, we can use a straightforward post-order traversal. Here is the resulting dynamic programming algorithm. Yes, it’s still recursive. I’ve swapped the evaluation of the with- v and without- v cases; we need to visit the kids first anyway, so why not consider the subproblem that depends directly on the kids first? MAXIMUMINDSETSIZE( v ): withoutv ← 0 for each child w of v withoutv ← withoutv + MAXIMUMINDSETSIZE(w ) withv ← 1 for each grandchild x of v withv ← withv + x . MIS v. MIS ← max{withv, withoutv} return v. MIS Another option is to store two values for each rooted subtree: the size of the largest independent set that includes the root, and the size of the largest independent set that excludes the root. This gives us an even simpler algorithm, with the same O(n) running time. MAXIMUMINDSETSIZE( v ): v. MISno ← 0 v. MISyes ← 1 for each child w of v v. MISno ← v. MISno + MAXIMUMINDSETSIZE(w ) v. MISyes ← v. MISyes + w. MISno return max{ v. MISyes, v. MISno} 3.8 Optimal Binary Search Trees In an earlier lecture, we developed a recursive algorithm for the optimal binary search tree problem. We are given a sorted array A[1 .. n] of search keys and an array f [1 .. n] of frequency counts, where f [i ] is the number of searches to A[i ]. Our task is to construct a binary search tree for that set such that the total cost of all the searches is as small as possible. We developed the following recurrence for this problem: n OptCost( f [1 .. n]) = min 1≤ r ≤n OptCost( f [1 .. r − 1]) + f [i ] + OptCost( f [ r + 1 .. n]) i =1 To put this recurrence in more standard form, fix the frequency array f , and let OptCost(i , j ) denote the total search time in the optimal search tree for the subarray A[i .. j ]. To simplify notation a bit, let F (i , j ) denote the total frequency count for all the keys in the interval A[i .. j ]: j F (i , j ) = f [k] k=i 10 Algorithms Lecture 3: Dynamic Programming We can now write OptCost(i , j ) = if j < i 0 F (i , j ) + min OptCost(i , r − 1) + OptCost( r + 1, j ) i≤r ≤ j otherwise The base case might look a little weird, but all it means is that the total cost for searching an empty set of keys is zero. The algorithm will be somewhat simpler and more efficient if we precompute all possible values of F (i , j ) and store them in an array. Computing each value F (i , j ) using a separate for-loop would O(n3 ) time. A better approach is to turn the recurrence F (i , j ) = f [i ] if i = j F ( i , j − 1) + f [ j ] otherwise into the following O(n2 )-time dynamic programming algorithm: INITF( f [1 .. n]): for i ← 1 to n F [ i , i − 1] ← 0 for j ← i to n F [i , j ] ← F [i , j − 1] + f [i ] This will be used as an initialization subroutine in our final algorithm. So now let’s compute the optimal search tree cost OptCost(1, n) from the bottom up. We can store all intermediate results in a table OptCost[1 .. n, 0 .. n]. Only the entries OptCost[i , j ] with j ≥ i − 1 will actually be used. The base case of the recurrence tells us that any entry of the form OptCost[i , i − 1] can immediately be set to 0. For any other entry OptCost[i , j ], we can use the following algorithm fragment, which comes directly from the recurrence: COMPUTEOPTCOST(i , j ): OptCost[i , j ] ← ∞ for r ← i to j t mp ← OptCost[i , r − 1] + OptCost[ r + 1, j ] if OptCost[i , j ] > t mp OptCost[i , j ] ← t mp OptCost[i , j ] ← OptCost[i , j ] + F [i , j ] The only question left is what order to fill in the table. Each entry OptCost[i , j ] depends on all entries OptCost[i , r − 1] and OptCost[ r + 1, j ] with i ≤ k ≤ j . In other words, every entry in the table depends on all the entries directly to the left or directly below. In order to fill the table efficiently, we must choose an order that computes all those entries before OptCost[i , j ]. There are at least three different orders that satisfy this constraint. The one that occurs to most people first is to scan through the table one diagonal at a time, starting with the trivial base cases OptCost[i , i − 1]. The complete algorithm looks like this: OPTIMALSEARCHTREE( f [1 .. n]): INITF( f [1 .. n]) for i ← 1 to n OptCost[i , i − 1] ← 0 for d ← 0 to n − 1 for i ← 1 to n − d COMPUTEOPTCOST(i , i + d ) return OptCost[1, n] 11 Algorithms Lecture 3: Dynamic Programming We could also traverse the array row by row from the bottom up, traversing each row from left to right, or column by column from left to right, traversing each columns from the bottom up. These two orders give us the following algorithms: OPTIMALSEARCHTREE2( f [1 .. n]): INITF( f [1 .. n]) for i ← n downto 1 OptCost[i , i − 1] ← 0 for j ← i to n COMPUTEOPTCOST(i , j ) return OptCost[1, n] OPTIMALSEARCHTREE3( f [1 .. n]): INITF( f [1 .. n]) for j ← 0 to n OptCost[ j + 1, j ] ← 0 for i ← j downto 1 COMPUTEOPTCOST(i , j ) return OptCost[1, n] Three different evaluation orders for the table OptCost[i , j ]. No matter which of these orders we actually use, the resulting algorithm runs in Θ(n 3 ) time and uses Θ(n 2 ) space. We could have predicted these space and time bounds directly from the original recurrence. OptCost(i , j ) = if j = i − i 0 F (i , j ) + min OptCost(i , r − 1) + OptCost( r + 1, j ) i≤r ≤ j otherwise First, the function has two arguments, each of which can take on any value between 1 and n, so we probably need a table of size O(n2 ). Next, there are three variables in the recurrence (i , j , and r ), each of which can take any value between 1 and n, so it should take us O(n3 ) time to fill the table. 3.9 More Examples We’ve already seen two other examples of recursive algorithms that we can significantly speed up via dynamic programming. 3.9.1 Subset Sum Recall that the Subset Sum problem asks, given a set X of positive integers (represented as an array X [1 .. n] and an integer T , whether any subset of X sums to T . In that lecture, we developed a recursive algorithm which can be reformulated as follows. Fix the original input array X [1 .. n] and the original target sum T , and define the boolean function S (i , t ) = some subset of X [i .. n] sums to t . Our goal is to compute S (1, T ), using the recurrence TRUE S (i , t ) = if t = 0, FALSE if t < 0 or i > n, S (i + 1, t ) ∨ S (i + 1, t − X [i ]) otherwise. 12 Algorithms Lecture 3: Dynamic Programming Observe that there are only nT possible values for the input parameters that lead to the interesting case of this recurrence, so storing the results of all such subproblems requires O (mn ) space. If S (i + 1, t ) and S (i + 1, t − X [i ]) are already known, we can compute S (i , t ) in constant time, so memoizing this recurrence gives us and algorithm that runs in O (n T ) time.6 To turn this into an explicit dynamic programming algorithm, we only need to consider the subproblems S (i , t ) in the proper order: SUBSETSUM(X [1 .. n], T ): S [n + 1, 0] ← TRUE for t ← 1 to T S [n + 1, t ] ← FALSE for i ← n downto 1 S [i , 0] = TRUE for t ← 1 to X [i ] − 1 S [i , t ] ← S [i + 1, t ] 〈〈Avoid the case t < 0〉〉 for t ← X [i ] to T S [i , t ] ← S [i + 1, t ] ∨ S [i + 1, t − X [i ]] return S [1, T ] This direct algorithm clearly always uses O (n T ) time and space. In particular, if T is significantly larger than 2n , this algorithm is actually slower than our naïve recursive algorithm. Dynamic programming isn’t always an improvement! 3.9.2 Longest Increasing Subsequence We also developed a recurrence for the longest increasing subsequence problem. Fix the original input array A[1 .. n] with a sentinel value A[0] = −∞. Let L (i , j ) denote the length of the longest increasing subsequence of A[ j .. n] with all elements larger than A[i ]. Our goal is to compute L (0, 1) − 1. (The −1 removes the sentinel −∞.) For any i < j , our recurrence can be stated as follows: if j > n 0 L (i , j ) = L (i , j + 1) max{ L (i , j + 1), 1 + L ( j , j + 1)} if A[i ] ≥ A[ j ] otherwise The recurrence suggests that our algorithm should use O(n2 ) time and space, since the input parameters i and j each can take n different values. To get an explicit dynamic programming algorithm, we only need to ensure that both L (i , j + 1) and L ( j , j + 1) are considered before L (i , j ), for all i and j . LIS(A[1 .. n]): A[0] ← −∞ for i ← 0 to n L [ i , n + 1] ← 0 〈〈Add a sentinel〉〉 〈〈Base cases〉〉 for j ← n downto 1 for i ← 0 to j − 1 if A[i ] ≥ A[ j ] L [ i , j ] ← L [ i , j + 1] else L [i , j ] ← max{ L [i , j + 1], 1 + L [ j , j + 1]} return L [0, 1] − 1 〈〈Don’t count the sentinel〉〉 Even though SubsetSum is NP-complete, this bound does not imply that P=NP, because T is not necessary bounded by a polynomial function of the input size. 6 13 Algorithms Lecture 3: Dynamic Programming As predicted, this algorithm clearly uses O (n 2 ) time and space. We can reduce the space to O(n) by only maintaining the two most recent columns of the table, L [·, j ] and L [·, j + 1]. This is not the only recursive strategy we could use for computing longest increasing subsequences. Here is another recurrence that gives us the O(n) space bound for free. Let L (i ) denote the length of the longest increasing subsequence of A[i .. n] that starts with A[i ]. Our goal is to compute L (0) − 1. To define L (i ) recursively, we only need to specify the second element in subsequence; the Recursion Fairy will do the rest. L (i ) = 1 + max L ( j ) | j > i and A[ j ] > A[i ] Here, I’m assuming that max ∅ = 0, so that the base case is L (n) = 1 falls out of the recurrence automatically. Memoizing this recurrence requires O(n) space, and the resulting algorithm runs in O(n2 ) time. To transform this into a dynamic programming algorithm, we only need to guarantee that L ( j ) is computed before L (i ) whenever i < j . LIS2(A[1 .. n]): A[0] = −∞ 〈〈Add a sentinel〉〉 for i ← n downto 0 L [i ] ← 1 for j ← i + 1 to n if A[ j ] > A[i ] and 1 + L [ j ] > L [i ] L [i ] ← 1 + L [ j ] return L [0] − 1 〈〈Don’t count the sentinel〉〉 Exercises 1. Suppose you are given an array A[1 .. n] of integers. Describe and analyze an algorithm that finds the largest sum of of elements in a contiguous subarray A[i .. j ]. For example, if the array contains the numbers (−6, 12, −7, 0, 14, −7, 5), then the largest sum of any contiguous subarray is 19 = 12 − 7 + 0 + 14. −6 12 −7 0 14 −7 5 19 2. This series of exercises asks you to develop efficient algorithms to find optimal subsequences of various kinds. A subsequence is anything obtained from a sequence by extracting a subset of elements, but keeping them in the same order; the elements of the subsequence need not be contiguous in the original sequence. For example, the strings C, DAMN, YAIOAI, and DYNAMICPROGRAMMING are all subsequences of the sequence DYNAMICPROGRAMMING. (a) Let A[1 .. m] and B [1 .. n] be two arbitrary arrays. A common subsequence of A and B is another sequence that is a subsequence of both A and B . Describe an efficient algorithm to compute the length of the longest common subsequence of A and B . (b) Let A[1 .. m] and B [1 .. n] be two arbitrary arrays. A common supersequence of A and B is another sequence that contains both A and B as subsequences. Describe an efficient algorithm to compute the length of the shortest common supersequence of A and B . (c) Call a sequence X [1 .. n] of numbers oscillating if X [i ] < X [i + 1] for all even i , and X [i ] > X [i + 1] for all odd i . Describe an efficient algorithm to compute the length of the longest oscillating subsequence of an arbitrary array A of integers. 14 Algorithms Lecture 3: Dynamic Programming (d) Describe an efficient algorithm to compute the length of the shortest oscillating supersequence of an arbitrary array A of integers. (e) Call a sequence X [1 .. n] of numbers accelerating if 2 · X [i ] < X [i − 1] + X [i + 1] for all i . Describe an efficient algorithm to compute the length of the longest accelerating subsequence of an arbitrary array A of integers. (f) Recall that a sequence X [1 .. n] of numbers is increasing if X [i ] < X [i + 1] for all i . Describe an efficient algorithm to compute the length of the longest common increasing subsequence of two given arrays of integers. For example, 〈1, 4, 5, 6, 7, 9〉 is the longest common increasing subsequence of the sequences 〈3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3〉 and 〈1, 4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5〉. 3. You and your eight-year-old nephew Elmo decide to play a simple card game. At the beginning of the game, the cards are dealt face up in a long row. Each card is worth a different number of points. After all the cards are dealt, you and Elmo take turns removing either the leftmost or rightmost card from the row, until all the cards are gone. At each turn, you can decide which of the two cards to take. The winner of the game is the player that has collected the most points when the game ends. Having never taken an algorithms class, Elmo follows the obvious greedy strategy—when it’s his turn, Elmo always takes the card with the higher point value. Your task is to find a strategy that will beat Elmo whenever possible. (It might seem mean to beat up on a little kid like this, but Elmo absolutely hates it when grown-ups let him win.) (a) Prove that you should not also use the greedy strategy. That is, show that there is a game that you can win, but only if you do not follow the same greedy strategy as Elmo. (b) Describe and analyze an algorithm to determine, given the initial sequence of cards, the maximum number of points that you can collect playing against Elmo. 4. A palindrome is any string that is exactly the same as its reversal, like I, or DEED, or RACECAR, or AMANAPLANACATACANALPANAMA. (a) Describe and analyze an algorithm to find the length of the longest subsequence of a given string that is also a palindrome. For example, the longest palindrome subsequence of MAHDYNAMICPROGRAMZLETMESHOWYOUTHEM is MHYMRORMYHM, so given that string as input, your algorithm should output the number 11. (b) Any string can be decomposed into a sequence of palindromes. For example, the string BUBBASEESABANANA (‘Bubba sees a banana.’) can be broken into palindromes in the following ways (and many others): BUB + BASEESAB B + U + BB + A + SEES B + U + BB + A + SEES B+U+B+B+A+S+E+E+S + + + + ANANA ABA + NAN + A A + B + ANANA A+B+A+N+A+N+A Describe and analyze an efficient algorithm to find the smallest number of palindromes that make up a given input string. For example, given the input string BUBBASEESABANANA, your algorithm would return the integer 3. 15 Algorithms Lecture 3: Dynamic Programming 5. Suppose you have a subroutine QUALITY that can compute the ‘quality’ of any given string A[1 .. k] in time O(k). For example, the quality of a string might be 1 if the string is a Québecois curse word, and 0 otherwise. Given an arbitrary input string T [1 .. n], we would like to break it into contiguous substrings, such that the total quality of all the substrings is as large as possible. For example, the string SAINTCIBIOREDESACRAMENTDECRISSE can be decomposed into the substrings SAINT + CIBIORE + DE + SACRAMENT + DE + CRISSE, of which three (or possibly four) are sacres. Describe an algorithm that breaks a string into substrings of maximum total quality, using the QUALITY subroutine. 6. Suppose you are given an m × n bitmap, represented by an array M [1 .. m, 1 .. n] whose entries are all 0 or 1. A solid block is a subarray of the form M [i .. i , j .. j ] in which every bit is equal to 1. Describe and analyze an efficient algorithm to find a solid block in M with maximum area. 7. Consider two horizontal lines 1 and 2 in the plane. Suppose we are given (the x -coordinates of) n distinct points a1 , a2 , . . . , an on 1 and n distinct points b1 , b2 , . . . , bn on 2 . (The points ai and b j are not necessarily indexed in order from left to right, or in the same order.) Design an algorithm to compute the largest set S of non-intersecting line segments satisfying to the following restrictions: (a) Each segment in S connects some point ai to the corresponding point bi . (b) No two segments in S intersect. 8. You are a bus driver with a soda fountain machine in the back and a bus full of very hyper students, who are drinking more soda as they ride along the highway. Your goal is to drop the students off as quickly as possible. More specifically, each minute that a student is on your bus, he drinks another ounce of soda. Your goal is to drop the students off quickly, so that in total they drink as little soda as possible. You know how many students will get off of the bus at each exit. Your bus begins partway along the highway (probably not at either end), and moves at a constant speed of 37.4 miles per hour. You must drive the bus along the highway; however, you may drive forward to one exit then backward to an exit in the other direction, switching as often as you like. (You can stop the bus, drop off students, and turn around instantaneously.) Describe an efficient algorithm to drop the students off so that they drink as little soda as possible. The input to the algorithm should be: the bus route (a list of the exits, together with the travel time between successive exits), the number of students you will drop off at each exit, and the current location of your bus (which you may assume is an exit). 9. In a previous life, you worked as a cashier in the lost Antarctican colony of Nadira, spending the better part of your day giving change to your customers. Because paper is a very rare and valuable resource in Antarctica, cashiers were required by law to use the fewest bills possible whenever they gave change. Thanks to the numerological predilections of one of its founders, the currency 16 Algorithms Lecture 3: Dynamic Programming of Nadira, called Dream Dollars, was available in the following denominations: $1, $4, $7, $13, $28, $52, $91, $365.7 (a) The greedy change algorithm repeatedly takes the largest bill that does not exceed the target amount. For example, to make $122 using the greedy algorithm, we first take a $91 bill, then a $28 bill, and finally three $1 bills. Give an example where this greedy algorithm uses more Dream Dollar bills than the minimum possible. (b) Describe and analyze a recursive algorithm that computes, given an integer k, the minimum number of bills needed to make k Dream Dollars. (Don’t worry about making your algorithm fast; just make sure it’s correct.) (c) Describe a dynamic programming algorithm that computes, given an integer k, the minimum number of bills needed to make k Dream Dollars. (This one needs to be fast.) 10. What excitement! The Champaign Spinners and the Urbana Dreamweavers have advanced to meet each other in the World Series of Basket-weaving! The World Champions will be decided by a best-of- 2n − 1 series of head-to-head weaving matches, and the first to win n matches will take home the coveted Golden Basket (for example, a best-of-7 series requiring four match wins, but we will keep the generalized case). We know that for any given match there is a constant probability p that Champaign will win, and a subsequent probability q = 1 − p that Urbana will win. Let P (i , j ) be the probability that Champaign will win the series given that they still need i more victories, whereas Urbana needs j more victories for the championship. P (0, j ) = 1 for any j , because Champaign needs no more victories to win. Similarly, P (i , 0) = 0 for any i , as Champaign cannot possibly win if Urbana already has. P (0, 0) is meaningless. Champaign wins any particular match with probability p and loses with probability q, so P (i , j ) = p · P (i − 1, j ) + q · P (i , j − 1) for any i ≥ 1 and j ≥ 1. Describe and analyze an efficient algorithm that computes the probability that Champaign will win the series (that is, calculate P (n, n)), given the parameters n, p, and q as input. 11. Vankin’s Mile is a solitaire game played on an n × n square grid. The player starts by placing a token on any square of the grid. Then on each turn, the player moves the token either one square to the right or one square down. The game ends when player moves the token off the edge of the board. Each square of the grid has a numerical value, which could be positive, negative, or zero. The player starts with a score of zero; whenever the token lands on a square, the player adds its value to his score. The object of the game is to score as many points as possible. For example, given the grid below, the player can score 8 − 6 + 7 − 3 + 4 = 10 points by placing the initial token on the 8 in the second row, and then moving down, down, right, down, down. (This is not the best possible score for these values.) 7 For more details on the history and culture of Nadira, including images of the various denominations of Dream Dollars, see http://www.dream-dollars.com. 17 Algorithms Lecture 3: Dynamic Programming -1 7 -4 -9 5 -2 -7 4 7 -8 1 10 -5 8 -6 0 -6 -6 7 ⇓ ⇓ 7⇒ -3 ⇓ -6 4 ⇓ -3 -9 Describe and analyze an efficient algorithm to compute the maximum possible score for a game of Vankin’s Mile, given the n × n array of values as input. 12. A shuffle of two strings X and Y is formed by interspersing the characters into a new string, keeping the characters of X and Y in the same order. For example, ‘bananaananas’ is a shuffle of ‘banana’ and ‘ananas’ in several different ways. banana ananas ban ana ana nas b an an a na a na s The strings ‘prodgyrnamammiincg’ and ‘dyprongarmammicing’ are both shuffles of ‘dynamic’ and ‘programming’: d y nam ic pro g r ammi n g dy nam ic pro g r amm ing Given three strings A[1 .. m], B [1 .. n], and C [1 .. m + n], describe and analyze an algorithm to determine whether C is a shuffle of A and B . 13. Suppose we want to display a paragraph of text on a computer screen. The text consists of n words, where the i th word is pi pixels wide. We want to break the paragraph into several lines, each exactly P pixels long. Depending on which words we put on each line, we will need to insert different amounts of white space between the words. The paragraph should be fully justified, meaning that the first word on each line starts at its leftmost pixel, and except for the last line, the last character on each line ends at its rightmost pixel. There must be at least one pixel of white-space between any two words on the same line. Define the slop of a paragraph layout as the sum over all lines, except the last, of the cube of the number of extra white-space pixels in each line (not counting the one pixel required between every adjacent pair of words). Specifically, if a line contains words i through j , then the amount of j extra white space on that line is P − j + i − k=i pk . Describe a dynamic programming algorithm to print the paragraph with minimum slop. 14. Let P be a set of points in the plane in convex position. Intuitively, if a rubber band were wrapped around the points, then every point would touch the rubber band. More formally, for any point p in P , there is a line that separates p from the other points in P . Moreover, suppose the points are indexed P [1], P [2], . . . , P [n] in counterclockwise order around the ‘rubber band’, starting with the leftmost point P [1]. This problem asks you to solve a special case of the traveling salesman problem, where the salesman must visit every point in P , and the cost of moving from one point p ∈ P to another point q ∈ P is the Euclidean distance | pq|. 18 Algorithms Lecture 3: Dynamic Programming (a) Describe a simple algorithm to compute the shortest cyclic tour of P . (b) A simple tour is one that never crosses itself. Prove that the shortest tour of P must be simple. (c) Describe and analyze an efficient algorithm to compute the shortest tour of P that starts at the leftmost point P [1] and ends at the rightmost point P [ r ]. 15. Describe and analyze an algorithm to solve the traveling salesman problem in O(2n poly(n)) time. Given an undirected n-vertex graph G with weighted edges, your algorithm should return the weight of the lightest cycle in G that visits every vertex exactly once, or ∞ if G has no such cycles. [Hint: The obvious recursive algorithm takes O(n!) time.] 16. Recall that a subtree of a (rooted, ordered) binary tree T consists of a node and all its descendants. Design and analyze an efficient algorithm to compute the largest common subtree of two given binary trees T1 and T2 ; this is the largest subtree of T1 that is isomorphic to a subtree in T2 . The contents of the nodes are irrelevant; we are only interested in matching the underlying combinatorial structure. Two binary trees, with their largest common subtree emphasized 17. Suppose we need to distribute a message to all the nodes in a rooted tree. Initially, only the root node knows the message. In a single round, any node that knows the message can forward it to at most one of its children. Design an algorithm to compute the minimum number of rounds required for the message to be delivered to all nodes. A message being distributed through a tree in five rounds. 18. A company is planning a party for its employees. The employees in the company are organized into a strict hierarchy, that is, a tree with the company president at the root. The organizers of the party have assigned a real number to each employee measuring how ‘fun’ the employee is. In order to keep things social, there is one restriction on the guest list: an employee cannot attend the party if their immediate supervisor is present. On the other hand, the president of the company must attend the party, even though she has a negative fun rating; it’s her company, after all. Give an algorithm that makes a guest list for the party that maximizes the sum of the ‘fun’ ratings of the guests. 19 Algorithms Lecture 3: Dynamic Programming 19. Scientists have branched out from the bizarre planet of Yggdrasil to study the vodes which have settled on Ygdrasil’s moon, Xryltcon. All vodes on Xryltcon are descended from the first vode to arrive there, named George. Each vode has a color, either cyan, magenta, or yellow, but breeding patterns are not the same as on Yggdrasil; every vode, regardless of color, has either two children (with arbitrary colors) or no children. George and all his descendants are alive and well, and they are quite excited to meet the scientists who wish to study them. Unsurprisingly, these vodes have had some strange mutations in their isolation on Xryltcon. Each vode has a weirdness rating; weirder vodes are more interesting to the visiting scientists. (Some vodes even have negative weirdness ratings; they make other vodes more boring just by standing next to them.) Also, Xryltconian society is strictly governed by a number of sacred cultural traditions. • No cyan vode may be in the same room as its non-cyan children (if it has any). • No magenta vode may be in the same room as its parent (if it has one). • Each yellow vode must be attended at all times by its grandchildren (if it has any). • George must be present at any gathering of more than fifty vodes. The scientists have exactly one chance to study a group of vodes in a single room. You are given the family tree of all the vodes on Xryltcon, along with the weirdness value of each vode. Design and analyze an efficient algorithm to decide which vodes the scientists should invite to maximize the sum of the weirdness values of the vodes in the room. Be careful to respect all of the vodes’ cultural taboos. 20. Oh, no! You have been appointed as the gift czar for Giggle, Inc.’s annual mandatory holiday party! The president of the company, who is certifiably insane, has declared that every Giggle employee must receive one of three gifts: (1) an all-expenses-paid six-week vacation anywhere in the world, (2) an all-the-pancakes-you-can-eat breakfast for two at Jumping Jack Flash’s Flapjack Stack Shack, or (3) a burning paper bag full of dog poop. Corporate regulations prohibit any employee from receiving the same gift as his/her direct supervisor. Any employee who receives a better gift than his/her direct supervisor will almost certainly be fired in a fit of jealousy. How do you decide what gifts everyone gets if you want to minimize the number of people that get fired? 1 2 3 3 2 1 3 2 1 1 3 3 2 1 2 3 3 3 1 2 2 1 A tree labeling with cost 9. Bold nodes have smaller labels than their parents. This is not the optimal labeling for this tree. More formally, suppose you are given a rooted tree T , representing the company hierarchy. You want to label each node in T with an integer 1, 2, or 3, such that every node has a different 20 Algorithms Lecture 3: Dynamic Programming label from its parent.. The cost of an labeling is the number of nodes that have smaller labels than their parents. Describe and analyze an algorithm to compute the minimum cost of any labeling of the given tree T . (Your algorithm does not have to compute the actual best labeling—just its cost.) 21. Every year, as part of its annual meeting, the Antarctican Snail Lovers of Upper Glacierville hold a Round Table Mating Race. Several high-quality breeding snails are placed at the edge of a round table. The snails are numbered in order around the table from 1 to n. During the race, each snail wanders around the table, leaving a trail of slime behind it. The snails have been specially trained never to fall off the edge of the table or to cross a slime trail, even their own. If two snails meet, they are declared a breeding pair, removed from the table, and whisked away to a romantic hole in the ground to make little baby snails. Note that some snails may never find a mate, even if the race goes on forever. For every pair of snails, the Antarctican SLUG race organizers have posted a monetary reward, to be paid to the owners if that pair of snails meets during the Mating Race. Specifically, there is a two-dimensional array M [1 .. n, 1 .. n] posted on the wall behind the Round Table, where M [i , j ] = M [ j , i ] is the reward to be paid if snails i and j meet. Describe and analyze an algorithm to compute the maximum total reward that the organizers could be forced to pay, given the array M as input. 1 8 2 8 7 1 6 7 3 52 4 6 3 4 5 The end of a typical Antarctican SLUG race. Snails 6 and 8 never find mates. The organizers must pay M [3, 4] + M [2, 5] + M [1, 7]. 22. Ribonucleic acid (RNA) molecules are long chains of millions of nucleotides or bases of four different types: adenine (A), cytosine (C), guanine (G), and uracil (U). The sequence of an RNA molecule is a string b[1 .. n], where each character b[i ] ∈ {A, C , G , U } corresponds to a base. In addition to the chemical bonds between adjacent bases in the sequence, hydrogen bonds can form between certain pairs of bases. The set of bonded base pairs is called the secondary structure of the RNA molecule. We say that two base pairs (i , j ) and (i , j ) with i < j and i < j overlap if i < i < j < j or i < i < j < j . In practice, most base pairs are non-overlapping. Overlapping base pairs create so-called pseudoknots in the secondary structure, which are essential for some RNA functions, but are more difficult to predict. Suppose we want to predict the best possible secondary structure for a given RNA sequence. We will adopt a drastically simplified model of secondary structure: 21 Algorithms • • • • Lecture 3: Dynamic Programming Each base can be paired with at most one other base. Only A-U pairs and C-G pairs can bond. Pairs of the form (i , i + 1) and (i , i + 2) cannot bond. Overlapping base pairs cannot bond. The last restriction allows us to visualize RNA secondary structure as a sort of fat tree. (a) Describe and analyze an algorithm that computes the maximum possible number of bonded base pairs in a secondary structure for a given RNA sequence. (b) A gap in a secondary structure is a maximal substring of unpaired bases. Large gaps lead to chemical instabilities, so secondary structures with smaller gaps are more likely. To account for this preference, let’s define the score of a secondary structure to be the sum of the squares of the gap lengths.8 Describe and analyze an algorithm that computes the minimum possible score of a secondary structure for a given RNA sequence. CU G A U U U G C A U G C U A GU A C CA C UACUCAU G C UUACA CAUGAGUA AAUGU U A A A G U C U U C U A A A A U CU C GG A U U Example RNA secondary structure with 21 base pairs, indicated by heavy red lines. Gaps are indicated by dotted curves. This structure has score 22 + 22 + 82 + 12 + 72 + 42 + 72 = 187 23. Let = {A1 , A2 , . . . , An } be a finite set of strings over some fixed alphabet Σ. An edit center for is a string C ∈ Σ∗ such that the maximum edit distance from C to any string in is as small as possible. The edit radius of is the maximum edit distance from an edit center to a string in . A set of strings may have several edit centers, but its edit radius is unique. EditRadius( ) = min max Edit(A, C ) ∗ C ∈Σ A∈ EditCenter( ) = arg min max Edit(A, C ) C ∈ Σ∗ A∈ (a) Describe and analyze an efficient algorithm to compute the edit radius of three given strings. (b) Describe and analyze an efficient algorithm to approximate the edit radius of an arbitrary set of strings within a factor of 2. (Computing the edit radius exactly is NP-hard unless the number of strings is fixed.) 8 This score function has absolutely no connection to reality; I just made it up. Real RNA structure prediction requires much more complicated scoring functions. 22 Algorithms Lecture 3: Dynamic Programming 24. Let D[1 .. n] be an array of digits, each an integer between 0 and 9. An digital subsequence of D is an sequence of positive integers composed in the usual way from disjoint substrings of D. For example, 3, 4, 5, 6, 8, 9, 32, 38, 46, 64, 83, 279 is an increasing digital subsequence of the first several digits of π: 3 , 1, 4 , 1, 5 , 9, 2, 6 , 5, 3, 5, 8 , 9 , 7, 9, 3, 2 , 3, 8 , 4, 6 , 2, 6, 4 , 3, 3, 8, 3 , 2, 7, 9 The length of a digital subsequence is the number of integers it contains, not the number of digits; the preceding example has length 12. Describe and analyze an efficient algorithm to compute the longest increasing digital subsequence of D. [Hint: Be careful about your computational assumptions. How long does it take to compare two k-digit numbers?] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 23 Algorithms Non-Lecture C: Advanced Dynamic Programming Tricks Ninety percent of science fiction is crud. But then, ninety percent of everything is crud, and it’s the ten percent that isn’t crud that is important. — [Theodore] Sturgeon’s Law (1953) C Advanced Dynamic Programming Tricks Dynamic programming is a powerful technique for efficiently solving recursive problems, but it’s hardly the end of the story. In many cases, once we have a basic dynamic programming algorithm in place, we can make further improvements to bring down the running time or the space usage. We saw one example in the Fibonacci number algorithm. Buried inside the naïve iterative Fibonacci algorithm is a recursive problem—computing a power of a matrix—that can be solved more efficiently by dynamic programming techniques—in this case, repeated squaring. C.1 Saving Space: Divide and Conquer Just as we did for the Fibonacci recurrence, we can reduce the space complexity of our edit distance algorithm from O(mn) to O(m + n) by only storing the current and previous rows of the memoization table. This ‘sliding window’ technique provides an easy space improvement for most (but not all) dynamic programming algorithm. Unfortunately, this technique seems to be useful only if we are interested in the cost of the optimal edit sequence, not if we want the optimal edit sequence itself. By throwing away most of the table, we apparently lose the ability to walk backward through the table to recover the optimal sequence. However, if we throw away most of the rows in the table, it seems we no longer have enough information to reconstruct the actual editing sequence. Now what? Fortunately for memory-misers, in 1975 Dan Hirshberg discovered a simple divide-and-conquer strategy that allows us to compute the optimal edit sequence in O(mn) time, using just O(m + n) space. The trick is to record not just the edit distance for each pair of prefixes, but also a single position in the middle of the editing sequence for that prefix. Specifically, the optimal editing sequence that transforms A[1 .. m] into B [1 .. n] can be split into two smaller editing sequences, one transforming A[1 .. m/2] into B [1 .. h] for some integer h, the other transforming A[m/2 + 1 .. m] into B [h + 1 .. n]. To compute this breakpoint h, we define a second function Half(i , j ) as follows: ∞ if i < m/2 j if i = m/2 Half(i , j ) = Half(i − 1, j ) if i > m/2 and Edit(i , j ) = Edit(i − 1, j ) + 1 Half(i , j − 1) if i > m/2 and Edit(i , j ) = Edit(i , j − 1) + 1 Half(i − 1, j − 1) otherwise A simple inductive argument implies that Half(m, n) is the correct value of h. We can easily modify our earlier algorithm so that it computes Half(m, n) at the same time as the edit distance Edit(m, n), all in O(mn) time, using only O(m) space. Now, to compute the optimal editing sequence that transforms A into B , we recursively compute the optimal subsequences. The recursion bottoms out when one string has only constant length, in which case we can determine the optimal editing sequence by our old dynamic programming algorithm. 1 Algorithms Non-Lecture C: Advanced Dynamic Programming Tricks Overall the running time of our recursive algorithm satisfies the following recurrence: if m ≤ 1 O(n) T (m, n) = O(m) if n ≤ 1 O(mn) + T (m/2, h) + T (m/2, n − h) otherwise It’s easy to prove inductively that T (m, n) = O(mn), no matter what the value of h is. Specifically, the entire algorithm’s running time is at most twice the time for the initial dynamic programming phase. T (m, n) ≤ αmn + T (m/2, h) + T (m/2, n − h) ≤ αmn + 2αmh/2 + 2αm(n − h)/2 [inductive hypothesis] = 2αmn A similar inductive argument implies that the algorithm uses only O(n + m) space. Hirschberg’s divide-and-conquer trick can be applied to almost any dynamic programming problem to obtain an algorithm to construct an optimal structure (in this case, the cheapest edit sequence) within the same space and time bounds as computing the cost of that optimal structure (in this case, edit distance). For this reason, we will almost always ask you for algorithms to compute the cost of some optimal structure, not the optimal structure itself. C.2 Saving Time: Sparseness In many applications of dynamic programming, we are faced with instances where almost every recursive subproblem will be resolved exactly the same way. We call such instances sparse. For example, we might want to compute the edit distance between two strings that have few characters in common, which means there are few “free” substitutions anywhere in the table. Most of the table has exactly the same structure. If we can reconstruct the entire table from just a few key entries, then why compute the entire table? To better illustrate how to exploit sparseness, let’s consider a simplification of the edit distance problem, in which substitutions are not allowed (or equivalently, where a substitution counts as two operations instead of one). Now our goal is to maximize the number of “free” substitutions, or equivalently, to find the longest common subsequence of the two input strings. Let LCS(i , j ) denote the length of the longest common subsequence of two fixed strings A[1 .. m] and B [1 .. n]. This function can be defined recursively as follows: if i = 0 or j = 0 0 LCS(i , j ) = LCS(i − 1, j − 1) max LCS(i , j − 1), LCS(i − 1, j ) if A[i ] = B [ j ] otherwise This recursive definition directly translates into an O(mn)-time dynamic programming algorithm. Call an index pair (i , j ) a match point if A[i ] = B [ j ]. In some sense, match points are the only ‘interesting’ locations in the memoization table; given a list of the match points, we could easily reconstruct the entire table. 2 Algorithms Non-Lecture C: Advanced Dynamic Programming Tricks « A L T R U I S T I C » «AL 000 011 012 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 GOR I T HMS » 0 1 2 2 2 2 2 2 2 0 1 2 2 2 2 2 2 2 0 1 2 2 0 1 2 2 33 33 34 34 34 0000 1111 2222 3333 3333 3333 4444 4445 5555 0 1 2 3 3 3 4 5 5 012223455555 012223455555 012223455556 The LCS memoization table for ALGORITHMS and ALTRUISTIC; the brackets « and » are sentinel characters. More importantly, we can compute the LCS function directly from the list of match points using the following recurrence: if i = j = 0 0 LCS(i , j ) = max LCS(i , j ) | A[i ] = B [ j ] and i < i and j < j + 1 max LCS(i , j ) | A[i ] = B [ j ] and i ≤ i and j ≤ j if A[i ] = B [ j ] otherwise (Notice that the inequalities are strict in the second case, but not in the third.) To simplify boundary issues, we add unique sentinel characters A[0] = B [0] and A[m + 1] = B [n + 1] to both strings. This ensures that the sets on the right side of the recurrence equation are non-empty, and that we only have to consider match points to compute LCS(m, n) = LCS(m + 1, n + 1) − 1. If there are K match points, we can actually compute them all in O(m log m + n log n + K ) time. Sort the characters in each input string, but remembering the original index of each character, and then essentially merge the two sorted arrays, as follows: FINDMATCHES(A[1 .. m], B [1 .. n]): for i ← 1 to m: I [i ] ← i for j ← 1 to n: J [ j ] ← j sort A and permute I to match sort B and permute J to match i ← 1; j ← 1 while i < m and j < n if A[i ] < B [ j ] i ← i+1 else if A[i ] > B [ j ] j ← j+1 else 〈〈Found a match!〉〉 ii ← i while A[ii ] = A[i ] jj ← j while B [ j j ] = B [ j ] report ( I [ii ], J [ j j ]) jj ← jj +1 ii ← i + 1 i ← ii ; j ← j j 3 Algorithms Non-Lecture C: Advanced Dynamic Programming Tricks To efficiently evaluate our modified recurrence, we once again turn to dynamic programming. We consider the match points in lexicographic order—the order they would be encountered in a standard row-major traversal of the m × n table—so that when we need to evaluate LCS(i , j ), all match points (i , j ) with i < i and j < j have already been evaluated. SPARSELCS(A[1 .. m], B [1 .. n]): Match[1 .. K ] ← FINDMATCHES(A, B ) Match[K + 1] ← (m + 1, n + 1) 〈〈Add end sentinel〉〉 Sort M lexicographically for k ← 1 to K (i , j ) ← Match[k] LCS[k] ← 1 〈〈From start sentinel〉〉 for ← 1 to k − 1 (i , j ) ← Match[ ] if i < i and j < j LCS[k] ← min{LCS[k], 1 + LCS[ ]} return LCS[K + 1] − 1 The overall running time of this algorithm is O(m log m + n log n + K 2 ). So as long as K = o( mn), this algorithm is actually faster than naïve dynamic programming. C.3 Saving Time: Monotonicity Recall the optimal binary search tree problem from the previous lecture. Given an array F [1 .. n] of access frequencies for n items, the problem it to compute the binary search tree that minimizes the cost of all accesses. A straightforward dynamic programming algorithm solves this problem in O(n3 ) time. As for longest common subsequence problem, the algorithm can be improved by exploiting some structure in the memoization table. In this case, however, the relevant structure isn’t in the table of costs, but rather in the table used to reconstruct the actual optimal tree. Let OptRoot[i , j ] denote the index of the root of the optimal search tree for the frequencies F [i .. j ]; this is always an integer between i and j . Donald Knuth proved the following nice monotonicity property for optimal subtrees: If we move either end of the subarray, the optimal root moves in the same direction or not at all. More formally: OptRoot[i , j − 1] ≤ OptRoot[i , j ] ≤ OptRoot[i + 1, j ] for all i and j . This (nontrivial!) observation leads to the following more efficient algorithm: FASTEROPTIMALSEARCHTREE( f [1 .. n]): INITF( f [1 .. n]) for i ← 1 downto n OptCost[i , i − 1] ← 0 OptRoot[i , i − 1] ← i for d ← 0 to n for i ← 1 to n COMPUTECOSTANDROOT(i , i + d ) return OptCost[1, n] COMPUTECOSTANDROOT(i , j ): OptCost[i , j ] ← ∞ for r ← OptRoot[i , j − 1] to OptRoot[i + 1, j ] tmp ← OptCost[i , r − 1] + OptCost[ r + 1, j ] if OptCost[i , j ] > tmp OptCost[i , j ] ← tmp OptRoot[i , j ] ← r OptCost[i , j ] ← OptCost[i , j ] + F [i , j ] It’s not hard to see that the loop index r increases monotonically from 1 to n during each iteration of the outermost for loop of FASTEROPTIMALSEARCHTREE. Consequently, the total cost of all calls to COMPUTECOSTANDROOT is only O(n2 ). If we formulate the problem slightly differently, this algorithm can be improved even further. Suppose we require the optimum external binary tree, where the keys A[1 .. n] are all stored at the leaves, and 4 Algorithms Non-Lecture C: Advanced Dynamic Programming Tricks intermediate pivot values are stored at the internal nodes. An algorithm due to Te Ching Hu and Alan Tucker1 computes the optimal binary search tree in this setting in only O(n log n) time! Exercises 1. Describe an algorithm to compute the edit distance between two strings A[1 .. m] and B [1 ... n] in O(m log m + n log n + K 2 ) time, where K is the number of match points. [Hint: Use the FINDMATCHES algorithm on page 3 as a subroutine.] 2. (a) Describe an algorithm to compute the longest increasing subsequence of a string X [1 .. n] in O(n log n) time. (b) Using your solution to part (a) as a subroutine, describe an algorithm to compute the longest common subsequence of two strings A[1 .. m] and B [1 ... n] in O(m log m + n log n + K log K ) time, where K is the number of match points. 3. Describe an algorithm to compute the edit distance between two strings A[1 .. m] and B [1 ... n] in O(m log m + n log n + K log K ) time, where K is the number of match points. [Hint: Combine your answers for problems 1 and 2(b).] 4. Let T be an arbitrary rooted tree, where each vertex is labeled with a positive integer. A subset S of the nodes of T is heap-ordered if it satisfies two properties: • S contains a node that is an ancestor of every other node in S . • For any node v in S , the label of v is larger than the labels of any ancestor of v in S . 3 1 4 9 2 3 2 5 3 1 5 5 8 9 8 4 2 7 7 9 6 3 6 9 A heap-ordered subset of nodes in a tree. (a) Describe an algorithm to find the largest heap-ordered subset S of nodes in T that has the heap property in O(n2 ) time. 1 T. C. Hu and A. C. Tucker, Optimal computer search trees and variable length alphabetic codes, SIAM J. Applied Math. 21:514–532, 1971. For a slightly simpler algorithm with the same running time, see A. M. Garsia and M. L. Wachs, A new algorithms for minimal binary search trees, SIAM J. Comput. 6:622–642, 1977. The original correctness proofs for both algorithms are rather intricate; for simpler proofs, see Marek Karpinski, Lawrence L. Larmore, and Wojciech Rytter, Correctness of constructing optimal alphabetic trees revisited, Theoretical Computer Science, 180:309-324, 1997. 5 Algorithms Non-Lecture C: Advanced Dynamic Programming Tricks (b) Modify your algorithm from part (a) so that it runs in O(n log n) time when T is either a linked list. [Hint: This special case is equivalent to a problem you’ve seen before.] (c) Describe an algorithm to find the largest subset S of nodes in T that has the heap property, in O(n log n) time. [Hint: Find an algorithm to merge two sorted lists of lengths k and in k+ O(log k ) time.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 6 Algorithms Lecture 4: Greedy Algorithms The point is, ladies and gentleman, greed is good. Greed works, greed is right. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit. Greed in all its forms, greed for life, money, love, knowledge has marked the upward surge in mankind. And greed—mark my words—will save not only Teldar Paper but the other malfunctioning corporation called the USA. — Michael Douglas as Gordon Gekko, Wall Street (1987) There is always an easy solution to every human problem— neat, plausible, and wrong. — H. L. Mencken, “The Divine Afflatus”, New York Evening Mail (November 16, 1917) 4 4.1 Greedy Algorithms Storing Files on Tape Suppose we have a set of n files that we want to store on a tape. In the future, users will want to read those files from the tape. Reading a file from tape isn’t like reading from disk; first we have to fast-forward past all the other files, and that takes a significant amount of time. Let L [1 .. n] be an array listing the lengths of each file; specifically, file i has length L [i ]. If the files are stored in order from 1 to n, then the cost of accessing the kth file is k cost(k) = L [ i ]. i =1 The cost reflects the fact that before we read file k we must first scan past all the earlier files on the tape. If we assume for the moment that each file is equally likely to be accessed, then the expected cost of searching for a random file is n E[cost] = cost(k) k =1 n n k = L [i ] k = 1 i =1 n . If we change the order of the files on the tape, we change the cost of accessing the files; some files become more expensive to read, but others become cheaper. Different file orders are likely to result in different expected costs. Specifically, let π(i ) denote the index of the file stored at position i on the tape. Then the expected cost of the permutation π is n k E[cost(π)] = k = 1 i =1 L [π(i )] n . Which order should we use if we want the expected cost to be as small as possible? The answer is intuitively clear; we should store the files in order from shortest to longest. So let’s prove this. Lemma 1. E[cost(π)] is minimized when L [π(i )] ≤ L [π(i + 1)] for all i . Proof: Suppose L [π(i )] > L [π(i + 1)] for some i . To simplify notation, let a = π(i ) and b = π(i + 1). If we swap files a and b, then the cost of accessing a increases by L [ b], and the cost of accessing b decreases by L [a]. Overall, the swap changes the expected cost by ( L [ b] − L [a])/n. But this change is an improvement, because L [ b] < L [a]. Thus, if the files are out of order, we can improve the expected cost by swapping some mis-ordered adjacent pair. 1 Algorithms Lecture 4: Greedy Algorithms This example gives us our first greedy algorithm. To minimize the total expected cost of accessing the files, we put the file that is cheapest to access first, and then recursively write everything else; no backtracking, no dynamic programming, just make the best local choice and blindly plow ahead. If we use an efficient sorting algorithm, the running time is clearly O(n log n), plus the time required to actually write the files. To prove the greedy algorithm is actually correct, we simply prove that the output of any other algorithm can be improved by some sort of swap. Let’s generalize this idea further. Suppose we are also given an array f [1 .. n] of access frequencies for each file; file i will be accessed exactly f [i ] times over the lifetime of the tape. Now the total cost of accessing all the files on the tape is n Σcost(π) = k f [π(k)] · n L [π(i )] i =1 k =1 k = f [π(k)] · L [π(i )] . k =1 i = 1 Now what order should store the files if we want to minimize the total cost? We’ve already proved that if all the frequencies are equal, then we should sort the files by increasing size. If the frequencies are all different but the file lengths L [i ] are all equal, then intuitively, we should sort the files by decreasing access frequency, with the most-accessed file first. In fact, this is not hard to prove by modifying the proof of Lemma 1. But what if the sizes and the frequencies are both different? In this case, we should sort the files by the ratio L / f . Lemma 2. Σcost(π) is minimized when L [π(i )] F [π(i )] ≤ L [π(i + 1)] F [π(i + 1)] for all i . Proof: Suppose L [π(i )]/ F [π(i )] > L [π(i + 1)]/ F [π(i + i )] for some i . To simplify notation, let a = π(i ) and b = π(i + 1). If we swap files a and b, then the cost of accessing a increases by L [ b], and the cost of accessing b decreases by L [a]. Overall, the swap changes the total cost by L [ b] F [a] − L [a] F [a]. But this change is an improvement, since L [a] F [a] > L [ b] F [ b] =⇒ L [ b] F [a] − L [a] F [a] < 0. Thus, if the files are out of order, we can improve the total cost by swapping some mis-ordered adjacent pair. 4.2 Scheduling Classes The next example is slightly less trivial. Suppose you decide to drop out of computer science at the last minute and change your major to Applied Chaos. The Applied Chaos department has all of its classes on the same day every week, referred to as “Soberday" by the students (but interestingly, not by the faculty). Every class has a different start time and a different ending time: AC 101 (‘Toilet Paper Landscape Architecture’) starts at 10:27pm and ends at 11:51pm; AC 666 (‘Immanentizing the Eschaton’) starts at 4:18pm and ends at 7:06pm, and so on. In the interests of graduating as quickly as possible, you want to register for as many classes as you can. (Applied Chaos classes don’t require any actual work.) The University’s registration computer won’t let you register for overlapping classes, and no one in the department knows how to override this ‘feature’. Which classes should you take? More formally, suppose you are given two arrays S [1 .. n] and F [1 .. n] listing the start and finish times of each class. Your task is to choose the largest possible subset X ∈ {1, 2, . . . , n} so that for any pair i , j ∈ X , either S [i ] > F [ j ] or S [ j ] > F [i ]. We can illustrate the problem by drawing each class as a rectangle whose left and right x -coordinates show the start and finish times. The goal is to find a largest subset of rectangles that do not overlap vertically. 2 Algorithms Lecture 4: Greedy Algorithms A maximal conflict-free schedule for a set of classes. This problem has a fairly simple recursive solution, based on the observation that either you take class 1 or you don’t. Let B4 be the set of classes that end before class 1 starts, and let L8 be the set of classes that start later than class 1 ends: B4 = {i | 2 ≤ i ≤ n and F [i ] < S [1]} L8 = {i | 2 ≤ i ≤ n and S [i ] > F [1]} If class 1 is in the optimal schedule, then so are the optimal schedules for B4 and L8 , which we can find recursively. If not, we can find the optimal schedule for {2, 3, . . . , n} recursively. So we should try both choices and take whichever one gives the better schedule. Evaluating this recursive algorithm from the bottom up gives us a dynamic programming algorithm that runs in O(n2 ) time. I won’t bother to go through the details, because we can do better.1 Intuitively, we’d like the first class to finish as early as possible, because that leaves us with the most remaining classes. If this greedy strategy works, it suggests the following very simple algorithm. Scan through the classes in order of finish time; whenever you encounter a class that doesn’t conflict with your latest class so far, take it! The same classes sorted by finish times and the greedy schedule. We can write the greedy algorithm somewhat more formally as follows. (Hopefully the first line is understandable.) GREEDYSCHEDULE(S [1 .. n], F [1 .. n]): sort F and permute S to match count ← 1 X [count ] ← 1 for i ← 2 to n if S [i ] > F [X [count ]] count ← count + 1 X [count ] ← i return X [1 .. count ] 1 But you should still work out the details yourself. The dynamic programming algorithm can be used to find the “best” schedule for any definition of “best”, but the greedy algorithm I’m about to describe only works that “best” means “biggest”. Also, you need the practice. 3 Algorithms Lecture 4: Greedy Algorithms This algorithm clearly runs in O(n log n) time. To prove that this algorithm actually gives us a maximal conflict-free schedule, we use an exchange argument, similar to the one we used for tape sorting. We are not claiming that the greedy schedule is the only maximal schedule; there could be others. (See the figures on the previous page.) All we can claim is that at least one of the maximal schedules is the one that the greedy algorithm produces. Lemma 3. At least one maximal conflict-free schedule includes the class that finishes first. Proof: Let f be the class that finishes first. Suppose we have a maximal conflict-free schedule X that does not include f . Let g be the first class in X to finish. Since f finishes before g does, f cannot conflict with any class in the set S \ { g }. Thus, the schedule X = X ∪ { f } \ { g } is also conflict-free. Since X has the same size as X , it is also maximal. To finish the proof, we call on our old friend, induction. Theorem 4. The greedy schedule is an optimal schedule. Proof: Let f be the class that finishes first, and let L be the subset of classes the start after f finishes. The previous lemma implies that some optimal schedule contains f , so the best schedule that contains f is an optimal schedule. The best schedule that includes f must contain an optimal schedule for the classes that do not conflict with f , that is, an optimal schedule for L . The greedy algorithm chooses f and then, by the inductive hypothesis, computes an optimal schedule of classes from L . The proof might be easier to understand if we unroll the induction slightly. Proof: Let 〈 g1 , g2 , . . . , g k 〉 be the sequence of classes chosen by the greedy algorithm. Suppose we have a maximal conflict-free schedule of the form 〈 g1 , g2 , . . . , g j −1 , c j , c j +1 , . . . , cm 〉, where the classes ci are different from the classes chosen by the greedy algorithm. By construction, the j th greedy choice g j does not conflict with any earlier class g1 , g2 , . . . , g j −1 , and since our schedule is conflict-free, neither does c j . Moreover, g j has the earliest finish time among all classes that don’t conflict with the earlier classes; in particular, g j finishes before c j . This implies that g j does not conflict with any of the later classes c j +1 , . . . , cm . Thus, the schedule 〈 g1 , g2 , . . . , g j −1 , g j , c j +1 , . . . , cm 〉, is conflict-free. (This is just a generalization of Lemma 3, which considers the case j = 1.) By induction, it now follows that there is an optimal schedule 〈 g1 , g2 , . . . , g k , ck+1 , . . . , cm 〉 that includes every class chosen by the greedy algorithm. But this is impossible unless k = m; if there were a class ck+1 that does not conflict with g k , the greedy algorithm would choose more than k classes. 4.3 General Structure The basic structure of this correctness proof is exactly the same as for the tape-sorting problem: an inductive exchange argument. • Assume that there is an optimal solution that is different from the greedy solution. • Find the ‘first’ difference between the two solutions. 4 Algorithms Lecture 4: Greedy Algorithms • Argue that we can exchange the optimal choice for the greedy choice without degrading the solution. This argument implies by induction that there is an optimal solution that contains the entire greedy solution. Sometimes, as in the scheduling problem, an additional step is required to show no optimal solution strictly improves the greedy solution. 4.4 Huffman codes A binary code assigns a string of 0s and 1s to each character in the alphabet. A binary code is prefix-free if no code is a prefix of any other. 7-bit ASCII and Unicode’s UTF-8 are both prefix-free binary codes. Morse code is a binary code, but it is not prefix-free; for example, the code for S (· · ·) includes the code for E (·) as a prefix. Any prefix-free binary code can be visualized as a binary tree with the encoded characters stored at the leaves. The code word for any symbol is given by the path from the root to the corresponding leaf; 0 for left, 1 for right. The length of a codeword for a symbol is the depth of the corresponding leaf. (Note that the code tree is not a binary search tree. We don’t care at all about the sorted order of symbols at the leaves. (In fact. the symbols may not have a well-defined order!) Suppose we want to encode messages in an n-character alphabet so that the encoded message is as short as possible. Specifically, given an array frequency counts f [1 .. n], we want to compute a prefix-free binary code that minimizes the total encoded length of the message:2 n f [i ] · depth(i ). i =1 In 1952, David Huffman developed the following greedy algorithm to produce such an optimal code: HUFFMAN: Merge the two least frequent letters and recurse. For example, suppose we want to encode the following helpfully self-descriptive sentence, discovered by Lee Sallows:3 This sentence contains three a’s, three c’s, two d’s, twenty-six e’s, five f’s, three g’s, eight h’s, thirteen i’s, two l’s, sixteen n’s, nine o’s, six r’s, twenty-seven s’s, twenty-two t’s, two u’s, five v’s, eight w’s, four x’s, five y’s, and only one z. To keep things simple, let’s forget about the forty-four spaces, nineteen apostrophes, nineteen commas, three hyphens, and one period, and just encode the letters. Here’s the frequency table: A C D 3 3 E 2 F G H 26 5 3 I 8 L N O R S T U V W X Y Z 13 2 16 9 6 27 22 2 5 8 451 Huffman’s algorithm picks out the two least frequent letters, breaking ties arbitrarily—in this case, say, Z and D—and merges them together into a single new character D with frequency 3. This new character Z becomes an internal node in the code tree we are constructing, with Z and D as its children; it doesn’t matter which child is which. The algorithm then recursively constructs a Huffman code for the new frequency table A C F G H 3 3 26 5 E 3 8 N O R U V W X Y D Z 13 2 16 I L 9 6 27 22 2 5 8 45 3 2 S T This looks almost exactly like the cost of a binary search tree, but the optimization problem is very different: code trees are not search trees! 3 A. K. Dewdney. Computer recreations. Scientific American, October 1984. Douglas Hofstadter published a few earlier examples of Lee Sallows’ self-descriptive sentences in his Scientific American column in January 1982. 5 Algorithms Lecture 4: Greedy Algorithms After 19 merges, all 20 characters have been merged together. The record of merges gives us our code tree. The algorithm makes a number of arbitrary choices; as a result, there are actually several different Huffman codes. One such code is shown below. 170 59 111 S 27 32 60 N 16 16 H 8 51 39 W 8 21 T 22 17 O 9 8 X 4 10 F 5 11 V 5 Y 5 I 13 12 R 6 6 A 3 4 L 2 E 26 25 6 C 3 G 3 U 2 3 D 2 Z 1 A Huffman code for Lee Sallows’ self-descriptive sentence; the numbers are frequencies for merged characters For example, the code for A is 110000, and the code for S is 00. The encoded message starts like this: 1001 0100 1101 00 00 111 011 1001 111 011 110001 111 110001 10001 011 1001 110000 1101 T H I SS E N T E N C E C O N T A I ··· Here is the list of costs for encoding each character, along with that character’s contribution to the total length of the encoded message: char. freq. depth total A C D E F G H I L N O R S T U V W X Y Z 3 3 2 26 5 3 8 13 2 16 9 6 27 22 2 5 8 4 51 6 6 7 3 5 6 4 4 7 3 4 4 2 4 7 5 4 6 57 18 18 14 78 25 18 32 52 14 48 36 24 54 88 14 25 32 24 25 7 Altogether, the encoded message is 646 bits long. Different Huffman codes would assign different codes, possibly with different lengths, to various characters, but the overall length of the encoded message is the same for any Huffman code: 646 bits. Given the simple structure of Huffman’s algorithm, it’s rather surprising that it produces an optimal prefix-free binary code. Encoding Lee Sallows’ sentence using any prefix-free code requires at least 646 bits! Fortunately, the recursive structure makes this claim easy to prove using an exchange argument, similar to our earlier optimality proofs. We start by proving that the algorithm’s very first choice is correct. Lemma 5. Let x and y be the two least frequent characters (breaking ties between equally frequent characters arbitrarily). There is an optimal code tree in which x and y are siblings. Proof: I’ll actually prove a stronger statement: There is an optimal code in which x and y are siblings and have the largest depth of any leaf. Let T be an optimal code tree, and suppose this tree has depth d . Since T is a full binary tree, it has at least two leaves at depth d that are siblings. (Verify this by induction!) Suppose those two leaves are not x and y , but some other characters a and b. 6 Algorithms Lecture 4: Greedy Algorithms Let T be the code tree obtained by swapping x and a. The depth of x increases by some amount ∆, and the depth of a decreases by the same amount. Thus, cost( T ) = cost( T ) − ( f [a] − f [ x ])∆. By assumption, x is one of the two least frequent characters, but a is not, which implies that f [a] ≥ f [ x ]. Thus, swapping x and a does not increase the total cost of the code. Since T was an optimal code tree, swapping x and a does not decrease the cost, either. Thus, T is also an optimal code tree (and incidentally, f [a] actually equals f [ x ]). Similarly, swapping y and b must give yet another optimal code tree. In this final optimal code tree, x and y as maximum-depth siblings, as required. Now optimality is guaranteed by our dear friend the Recursion Fairy! Essentially we’re relying on the following recursive definition for a full binary tree: either a single node, or a full binary tree where some leaf has been replaced by an internal node with two leaf children. Theorem 6. Huffman codes are optimal prefix-free binary codes. Proof: If the message has only one or two different characters, the theorem is trivial. Otherwise, let f [1 .. n] be the original input frequencies, where without loss of generality, f [1] and f [2] are the two smallest. To keep things simple, let f [n + 1] = f [1] + f [2]. By the previous lemma, we know that some optimal code for f [1 .. n] has characters 1 and 2 as siblings. Let T be the Huffman code tree for f [3 .. n + 1]; the inductive hypothesis implies that T is an optimal code tree for the smaller set of frequencies. To obtain the final code tree T , we replace the leaf labeled n + 1 with an internal node with two children, labelled 1 and 2. I claim that T is optimal for the original frequency array f [1 .. n]. To prove this claim, we can express the cost of T in terms of the cost of T as follows. (In these equations, depth(i ) denotes the depth of the leaf labelled i in either T or T ; if the leaf appears in both T and T , it has the same depth in both trees.) n cost( T ) = f [i ] · depth(i ) i =1 n+1 = f [i ] · depth(i ) + f [1] · depth(1) + f [2] · depth(2) − f [n + 1] · depth(n + 1) i =3 = cost( T ) + f [1] · depth(1) + f [2] · depth(2) − f [n + 1] · depth(n + 1) = cost( T ) + ( f [1] + f [2]) · depth( T ) − f [n + 1] · (depth( T ) − 1) = cost( T ) + f [1] + f [2] This equation implies that minimizing the cost of T is equivalent to minimizing the cost of T ; in particular, attaching leaves labeled 1 and 2 to the leaf in T labeled n + 1 gives an optimal code tree for the original frequencies. To actually implement Huffman codes efficiently, we keep the characters in a min-heap, where the priority of each character is its frequency. We can construct the code tree by keeping three arrays of indices, listing the left and right children and the parent of each node. The root of the tree is the node with index 2n − 1. 7 Algorithms Lecture 4: Greedy Algorithms BUILDHUFFMAN( f [1 .. n]): for i ← 1 to n L [i ] ← 0; R[i ] ← 0 INSERT(i , f [i ]) for i ← n to 2n − 1 x ← EXTRACTMIN( ) y ← EXTRACTMIN( ) f [i ] ← f [ x ] + f [ y ] L [i ] ← x ; R[i ] ← y P [ x ] ← i; P [ y ] ← i INSERT(i , f [i ]) P [ 2 n − 1] ← 0 The algorithm performs O(n) min-heap operations. If we use a balanced binary tree as the heap, each operation requires O(log n) time, so the total running time of BUILDHUFFMAN is O(n log n). Finally, here are simple algorithms to encode and decode messages: HUFFMANENCODE(A[1 .. k]): m←1 for i ← 1 to k HUFFMANENCODEONE(A[i ]) HUFFMANDECODE(B [1 .. m]): k←1 v ← 2n − 1 for i ← 1 to m if B [i ] = 0 v ← L[v] else v ← R[ v ] HUFFMANENCODEONE( x ): if x < 2n − 1 HUFFMANENCODEONE( P [ x ]) if x = L [ P [ x ]] B [ m] ← 0 else B [ m] ← 1 m← m+1 if L [ v ] = 0 A[k] ← v k ← k+1 v ← 2n − 1 Exercises 1. Let X be a set of n intervals on the real line. A subset of intervals Y ⊆ X is called a tiling path if the intervals in Y cover the intervals in X , that is, any real value that is contained in some interval in X is also contained in some interval in Y . The size of a tiling cover is just the number of intervals. Describe and analyze an algorithm to compute the smallest tiling path of X as quickly as possible. Assume that your input consists of two arrays X L [1 .. n] and X R [1 .. n], representing the left and right endpoints of the intervals in X . If you use a greedy algorithm, you must prove that it is correct. A set of intervals. The seven shaded intervals form a tiling path. 2. Let X be a set of n intervals on the real line. We say that a set P of points stabs X if every interval in X contains at least one point in P . Describe and analyze an efficient algorithm to compute the smallest set of points that stabs X . Assume that your input consists of two arrays X L [1 .. n] and 8 Algorithms Lecture 4: Greedy Algorithms X R [1 .. n], representing the left and right endpoints of the intervals in X . As usual, If you use a greedy algorithm, you must prove that it is correct. A set of intervals stabbed by four points (shown here as vertical segments) 3. Let X be a set of n intervals on the real line. A proper coloring of X assigns a color to each interval, so that any two overlapping intervals are assigned different colors. Describe and analyze an efficient algorithm to compute the minimum number of colors needed to properly color X . Assume that your input consists of two arrays L [1 .. n] and R[1 .. n], where L [i ] and R[i ] are the left and right endpoints of the i th interval. As usual, if you use a greedy algorithm, you must prove that it is correct. 1 2 2 3 1 5 5 4 4 3 5 4 3 3 1 2 A proper coloring of a set of intervals using five colors. 4. Suppose you are standing in a field surrounded by several large balloons. You want to use your brand new Acme Brand Zap-O-MaticTM to pop all the balloons, without moving from your current location. The Zap-O-MaticTM shoots a high-powered laser beam, which pops all the balloons it hits. Since each shot requires enough energy to power a small country for a year, you want to fire as few shots as possible. Nine balloons popped by 4 shots of the Zap-O-MaticTM The minimum zap problem can be stated more formally as follows. Given a set C of n circles in the plane, each specified by its radius and the ( x , y ) coordinates of its center, compute the minimum number of rays from the origin that intersect every circle in C . Your goal is to find an efficient algorithm for this problem. 9 Algorithms Lecture 4: Greedy Algorithms (a) Suppose it is possible to shoot a ray that does not intersect any balloons. Describe and analyze a greedy algorithm that solves the minimum zap problem in this special case. [Hint: See Exercise 2.] (b) Describe and analyze a greedy algorithm whose output is within 1 of optimal. That is, if m is the minimum number of rays required to hit every balloon, then your greedy algorithm must output either m or m + 1. (Of course, you must prove this fact.) (c) Describe an algorithm that solves the minimum zap problem in O(n2 ) time. (d) Describe an algorithm that solves the minimum zap problem in O(n log n) time. Assume you have a subroutine INTERSECTS( r, c ) that determines whether a ray r intersects a circle!c in O(1) time. It’s not that hard to write this subroutine, but it’s not the interesting part of the problem. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 10 Algorithms Non-Lecture D: Matroids The problem is that we attempt to solve the simplest questions cleverly, thereby rendering them unusually complex. One should seek the simple solution. — Anton Pavlovich Chekhov (c. 1890) I love deadlines. I like the whooshing sound they make as they fly by. — Douglas Adams D Matroids D.1 Definitions Many problems that can be correctly solved by greedy algorithms can be described in terms of an abstract combinatorial object called a matroid. Matroids were first described in 1935 by the mathematician Hassler Whitney as a combinatorial generalization of linear independence of vectors—‘matroid’ means ‘something sort of like a matrix’. A matroid M is a finite collection of finite sets that satisfies three axioms: • Non-emptiness: The empty set is in M. (Thus, M is not itself empty.) • Heredity: If a set X is an element of M, then any subset of X is also in M. • Exchange: If X and Y are two sets in M and |X | > |Y |, then there is an element x ∈ X \ Y such that Y ∪ { x } is in M. The sets in M are typically called independent sets; for example, we would say that any subset of an independent set is independent. The union of all sets in M is called the ground set. An independent set is called a basis if it is not a proper subset of another independent set. The exchange property implies that every basis of a matroid has the same cardinality. The rank of a subset X of the ground set is the size of the largest independent subset of X . A subset of the ground set that is not in M is called dependent (surprise, surprise). Finally, a dependent set is called a circuit if every proper subset is independent. Most of this terminology is justified by Whitney’s original example: Linear matroid: Let A be any n × m matrix. A subset I ⊆ {1, 2, . . . , n} is independent if and only if the corresponding subset of columns of A is linearly independent. The heredity property follows directly from the definition of linear independence; the exchange property is implied by an easy dimensionality argument. A basis in any linear matroid is also a basis (in the linear-algebra sense) of the vector space spanned by the columns of A. Similarly, the rank of a set of indices is precisely the rank (in the linear-algebra sense) of the corresponding set of column vectors. Here are several other examples of matroids; some of these we will see again later. I will leave the proofs that these are actually matroids as exercises for the reader. • Uniform matroid Uk ,n : A subset X ⊆ {1, 2, . . . , n} is independent if and only if |X | ≤ k. Any subset of {1, 2, . . . , n} of size k is a basis; any subset of size k + 1 is a circuit. • Graphic/cycle matroid M(G ): Let G = (V, E ) be an arbitrary undirected graph. A subset of E is independent if it defines an acyclic subgraph of G . A basis in the graphic matroid is a spanning tree of G ; a circuit in this matroid is a cycle in G . 1 Algorithms Non-Lecture D: Matroids • Cographic/cocycle matroid M∗ (G ): Let G = (V, E ) be an arbitrary undirected graph. A subset I ⊆ E is independent if the complementary subgraph (V, E \ I ) of G is connected. A basis in this matroid is the complement of a spanning tree; a circuit in this matroid is a cocycle—a minimal set of edges that disconnects the graph. • Matching matroid: Let G = (V, E ) be an arbitrary undirected graph. A subset I ⊆ V is independent if there is a matching in G that covers I . • Disjoint path matroid: Let G = (V, E ) be an arbitrary directed graph, and let s be a fixed vertex of G . A subset I ⊆ V is independent if and only if there are edge-disjoint paths from s to each vertex in I . Now suppose each element of the ground set of a matroid M is given an arbitrary non-negative weight. The matroid optimization problem is to compute a basis with maximum total weight. For example, if M is the cycle matroid for a graph G , the matroid optimization problem asks us to find the maximum spanning tree of G . Similarly, if M is the cocycle matroid for G , the matroid optimization problem seeks (the complement of) the minimum spanning tree. The following natural greedy strategy computes a basis for any weighted matroid: GREEDYBASIS(M, w ): X [1 .. n] ← M (the ground set) sort X in decreasing order of weight w G←∅ for i ← 1 to n if G ∪ {X [i ]} ∈ M add X [i ] to G return G Suppose we can test in F (n) whether a given subset of the ground set is independent. Then this algorithm runs in O(n log n + n · F (n)) time. Theorem 1. For any matroid M and any weight function w , GREEDYBASIS(M, w ) returns a maximumweight basis of M. Proof: Let G = { g1 , g2 , . . . , g k } be the independent set returned by GREEDYBASIS(M, w ). If any other element could be added to G to get a larger independent set, the greedy algorithm would have added it. Thus, G is a basis. For purposes of deriving a contradiction, suppose there is an independent set H = {h1 , h2 , . . . , h } such that k w( gi ) < i =1 w (hi ). j =1 Without loss of generality, we assume that H is a basis. The exchange property now implies that k = . Now suppose the elements of G and H are indexed in order of decreasing weight. Let i be the smallest index such that w ( g i ) < w (hi ), and consider the independent sets G i −1 = { g 1 , g 2 , . . . , g i −1 } and H i = {h1 , h2 , . . . , hi −1 , hi }. By the exchange property, there is some element h j ∈ H i such that Gi −1 ∪ {h j } is an independent set. We have w (h j ) ≥ w (hi ) > w ( g i ). Thus, the greedy algorithm considers and rejects the heavier element h j before it considers the lighter element g i . But this is impossible—the greedy algorithm accepts elements in decreasing order of weight. 2 Algorithms Non-Lecture D: Matroids We now immediately have a correct greedy optimization algorithm for any matroid. Returning to our examples: • Linear matroid: Given a matrix A, compute a subset of vectors of maximum total weight that span the column space of A. • Uniform matroid: Given a set of weighted objects, compute its k largest elements. • Cycle matroid: Given a graph with weighted edges, compute its maximum spanning tree. In this setting, the greedy algorithm is better known as Kruskal’s algorithm. • Cocycle matroid: Given a graph with weighted edges, compute its minimum spanning tree. • Matching matroid: Given a graph, determine whether it has a perfect matching. • Disjoint path matroid: Given a directed graph with a special vertex s, find the largest set of edge-disjoint paths from s to other vertices. The exchange condition for matroids turns out to be crucial for the success of this algorithm. A subset system is a finite collection S of finite sets that satisfies the heredity condition—If X ∈ S and Y ⊆ X , then Y ∈ S—but not necessarily the exchange condition. Theorem 2. For any subset system S that is not a matroid, there is a weight function w such that GREEDYBASIS(S, w ) does not return a maximum-weight set in S. Proof: Let X and Y be two sets in S that violate the exchange property—|X | > |Y |, but for any element x ∈ X \ Y , the set Y ∪ { x } is not in S. Let m = |Y |. We define a weight function as follows: • Every element of Y has weight m + 2. • Every element of X \ Y has weight m + 1. • Every other element of the ground set has weight zero. With these weights, the greedy algorithm will consider and accept every element of Y , then consider and reject every element of X , and finally consider all the other elements. The algorithm returns a set with total weight m(m + 2) = m2 + 2m. But the total weight of X is at least (m + 1)2 = m2 + 2m + 1. Thus, the output of the greedy algorithm is not the maximum-weight set in S. Recall the Applied Chaos scheduling problem considered in the previous lecture note. There is a natural subset system associated with this problem: A set of classes is independent if and only if not two classes overlap. (This is just the graph-theory notion of ‘independent set’!) This subset system is not a matroid, because there can be maximal independent sets of different sizes, which violates the exchange property. If we consider a weighted version of the class scheduling problem, say where each class is worth a different number of hours, Theorem 2 implies that the greedy algorithm will not always find the optimal schedule. (In fact, there’s an easy counterexample with only two classes!) However, Theorem 2 does not contradict the correctness of the greedy algorithm for the original unweighted problem, however; that problem uses a particularly lucky choice of weights (all equal). 3 Algorithms D.2 Non-Lecture D: Matroids Scheduling with Deadlines Suppose you have n tasks to complete in n days; each task requires your attention for a full day. Each task comes with a deadline, the last day by which the job should be completed, and a penalty that you must pay if you do not complete each task by its assigned deadline. What order should you perform your tasks in to minimize the total penalty you must pay? More formally, you are given an array D[1 .. n] of deadlines an array P [1 .. n] of penalties. Each deadline D[i ] is an integer between 1 and n, and each penalty P [i ] is a non-negative real number. A schedule is a permutation of the integers {1, 2, . . . , n}. The scheduling problem asks you to find a schedule π that minimizes the following cost: n cost(π) := P [i ] · [π(i ) > D[i ]]. i =1 This doesn’t look anything like a matroid optimization problem. For one thing, matroid optimization problems ask us to find an optimal set; this problem asks us to find an optimal permutation. Surprisingly, however, this scheduling problem is actually a matroid optimization in disguise! For any schedule π, call tasks i such that π(i ) > D[i ] late, and all other tasks on time. The following trivial observation is the key to revealing the underlying matroid structure. The cost of a schedule is determined by the subset of tasks that are on time. Call a subset X of the tasks realistic if there is a schedule π in which every task in X is on time. We can precisely characterize the realistic subsets as follows. Let X ( t ) denote the subset of tasks in X whose deadline is on or before t : X ( t ) := { i ∈ X | D [ i ] ≤ t } . In particular, X (0) = ∅ and X (n) = X . Lemma 3. Let X {1, 2, . . . , n} be an arbitrary subset of the n tasks. X is realistic if and only if |X ( t )| ≤ t for every integer t . Proof: Let π be a schedule in which every task in X is on time. Let i t be the t th task in X to be completed. On the one hand, we have π(i t ) ≥ t , since otherwise, we could not have completed t − 1 other jobs in X before i t . On the other hand, π(i t ) ≤ D[i ], because i t is on time. We conclude that D[i t ] ≥ t , which immediately implies that |X ( t )| ≤ t . Now suppose |X ( t )| ≤ t for every integer t . If we perform the tasks in X in increasing order of deadline, then we complete all tasks in X with deadlines t or less by day t . In particular, for any i ∈ X , we perform task i on or before its deadline D[i ]. Thus, X is realistic. We can define a canonical schedule for any set X as follows: execute the tasks in X in increasing deadline order, and then execute the remaining tasks in any order. The previous proof implies that a set X is realistic if and only if every task in X is on time in the canonical schedule for X . Thus, our scheduling problem can be rephrased as follows: Find a realistic subset X such that i ∈X P [i ] is maximized. So we’re looking for optimal subsets after all. Lemma 4. The collection of realistic sets of jobs forms a matroid. 4 Algorithms Non-Lecture D: Matroids Proof: The empty set is vacuously realistic, and any subset of a realistic set is clearly realistic. Thus, to prove the lemma, it suffices to show that the exchange property holds. Let X and Y be realistic sets of jobs with |X | > |Y |. Let t ∗ be the largest integer such that |X ( t ∗ )| ≤ |Y ( t ∗ )|. This integer must exist, because |X (0)| = 0 ≤ 0 = |Y (0)| and |X (n)| = |X | > |Y | = |Y (n)|. By definition of t ∗ , there are more tasks with deadline t ∗ + 1 in X than in Y . Thus, we can choose a task j in X \ Y with deadline t ∗ + 1; let Z = Y ∪ { j }. Let t be an arbitrary integer. If t ≤ t ∗ , then | Z ( t )| = |Y ( t )| ≤ t , because Y is realistic. On the other hand, if t > t ∗ , then | Z ( t )| = |Y ( t )| + 1 ≤ |X ( t )| < t by definition of t ∗ and because X is realistic. The previous lemma now implies that Z is realistic. This completes the proof of the exchange property. This lemma implies that our scheduling problem is a matroid optimization problem, so the greedy algorithm finds the optimal schedule. GREEDYSCHEDULE( D[1 .. n], P [1 .. n]): Sort P in increasing order, and permute D to match j←0 for i ← 1 to n X [ j + 1] ← i if X [1 .. j + 1] is realistic j ← j+1 return the canonical schedule for X [1 .. j ] To turn this outline into a real algorithm, we need a procedure to test whether a given subset of jobs is realistic. Lemma 9 immediately suggests the following strategy to answer this question in O(n) time. REALISTIC?(X [1 .. m], D[1 .. n]): 〈〈X is sorted by increasing deadline: i ≤ j =⇒ D[X [i ]] ≤ D[X [ j ]]〉〉 N ←0 j←0 for t ← 1 to n if D[X [ j ]] = t N ← N + 1; j ← j + 1 〈〈Now N = |X ( t )|〉〉 if N > t return FALSE return TRUE If we use this subroutine, GREEDYSCHEDULE runs in O(n2 ) time. By using some appropriate data structures, the running time can be reduced to O(n log n); details are left as an exercise for the reader. Exercises 1. Prove that for any graph G , the ‘graphic matroid’ M(G ) is in fact a matroid. (This problem is really asking you to prove that Kruskal’s algorithm is correct!) 2. Prove that for any graph G , the ‘cographic matroid’ M∗ (G ) is in fact a matroid. 3. Prove that for any graph G , the ‘matching matroid’ of G is in fact a matroid. [Hint: What is the symmetric difference of two matchings?] 5 Algorithms Non-Lecture D: Matroids 4. Prove that for any directed graph G and any vertex s of G , the resulting ‘disjoint path matroid’ of G is in fact a matroid. [Hint: This question is much easier if you’re already familiar with maximum flows.] 5. Let G be an undirected graph. A set of cycles {c1 , c2 , . . . , ck } in G is called redundant if every edge in G appears in an even number of ci ’s. A set of cycles is independent if it contains no redundant subset. A maximal independent set of cycles is called a cycle basis for G . (a) Let C be any cycle basis for G . Prove that for any cycle γ in G , there is a subset A ⊆ C such that A ∩ {γ} is redundant. In other words, γ is the ‘exclusive or’ of the cycles in A. (b) Prove that the set of independent cycle sets form a matroid. (c) Now suppose each edge of G has a weight. Define the weight of a cycle to be the total weight of its edges, and the weight of a set of cycles to be the total weight of all cycles in the set. (Thus, each edge is counted once for every cycle in which it appears.) Describe and analyze an efficient algorithm to compute the minimum-weight cycle basis in G . 6. Describe a modification of GREEDYSCHEDULE that runs in O(n log n) time. [Hint: Store X in an appropriate data structure that supports the operations “Is X ∪ {i } realistic?” and “Add i to X ” in O(log n) time each.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 6 Algorithms Lecture 5: Randomized Algorithms The first nuts and bolts appeared in the middle 1400’s. The bolts were just screws with straight sides and a blunt end. The nuts were hand-made, and very crude. When a match was found between a nut and a bolt, they were kept together until they were finally assembled. In the Industrial Revolution, it soon became obvious that threaded fasteners made it easier to assemble products, and they also meant more reliable products. But the next big step came in 1801, with Eli Whitney, the inventor of the cotton gin. The lathe had been recently improved. Batches of bolts could now be cut on different lathes, and they would all fit the same nut. Whitney set up a demonstration for President Adams, and Vice-President Jefferson. He had piles of musket parts on a table. There were 10 similar parts in each pile. He went from pile to pile, picking up a part at random. Using these completely random parts, he quickly put together a working musket. — Karl S. Kruszelnicki (‘Dr. Karl’), Karl Trek, December 1997 Dr [John von] Neumann in his Theory of Games and Economic Behavior introduces the cut-up method of random action into game and military strategy: Assume that the worst has happened and act accordingly. If your strategy is at some point determined. . . by random factor your opponent will gain no advantage from knowing your strategy since he cannot predict the move. The cut-up method could be used to advantage in processing scientific data. How many discoveries have been made by accident? We cannot produce accidents to order. — William S. Burroughs, "The Cut-Up Method of Brion Gysin" in The Third Mind by William S. Burroughs and Brion Gysin (1978) 5 Randomized Algorithms 5.1 Nuts and Bolts Suppose we are given n nuts and n bolts of different sizes. Each nut matches exactly one bolt and vice versa. The nuts and bolts are all almost exactly the same size, so we can’t tell if one bolt is bigger than the other, or if one nut is bigger than the other. If we try to match a nut witch a bolt, however, the nut will be either too big, too small, or just right for the bolt. Our task is to match each nut to its corresponding bolt. But before we do this, let’s try to solve some simpler problems, just to get a feel for what we can and can’t do. Suppose we want to find the nut that matches a particular bolt. The obvious algorithm — test every nut until we find a match — requires exactly n − 1 tests in the worst case. We might have to check every bolt except one; if we get down the the last bolt without finding a match, we know that the last nut is the one we’re looking for.1 Intuitively, in the ‘average’ case, this algorithm will look at approximately n/2 nuts. But what exactly does ‘average case’ mean? 5.2 Deterministic vs. Randomized Algorithms Normally, when we talk about the running time of an algorithm, we mean the worst-case running time. This is the maximum, over all problems of a certain size, of the running time of that algorithm on that input: Tworst-case (n) = max T (X ). |X |=n On extremely rare occasions, we will also be interested in the best-case running time: Tbest-case (n) = min T (X ). |X |=n 1 “Whenever you lose something, it’s always in the last place you look. So why not just look there first?” 1 Algorithms Lecture 5: Randomized Algorithms The average-case running time is best defined by the expected value, over all inputs X of a certain size, of the algorithm’s running time for X :2 Taverage-case (n) = E [ T (X )] = |X |=n T ( x ) · Pr[X ]. |X |=n The problem with this definition is that we rarely, if ever, know what the probability of getting any particular input X is. We could compute average-case running times by assuming a particular probability distribution—for example, every possible input is equally likely—but this assumption doesn’t describe reality very well. Most real-life data is decidedly non-random (or at least random in some unpredictable way). Instead of considering this rather questionable notion of average case running time, we will make a distinction between two kinds of algorithms: deterministic and randomized. A deterministic algorithm is one that always behaves the same way given the same input; the input completely determines the sequence of computations performed by the algorithm. Randomized algorithms, on the other hand, base their behavior not only on the input but also on several random choices. The same randomized algorithm, given the same input multiple times, may perform different computations in each invocation. This means, among other things, that the running time of a randomized algorithm on a given input is no longer fixed, but is itself a random variable. When we analyze randomized algorithms, we are typically interested in the worst-case expected running time. That is, we look at the average running time for each input, and then choose the maximum over all inputs of a certain size: Tworst-case expected (n) = max E[ T (X )]. |X |=n It’s important to note here that we are making no assumptions about the probability distribution of possible inputs. All the randomness is inside the algorithm, where we can control it! 5.3 Back to Nuts and Bolts Let’s go back to the problem of finding the nut that matches a given bolt. Suppose we use the same algorithm as before, but at each step we choose a nut uniformly at random from the untested nuts. ‘Uniformly’ is a technical term meaning that each nut has exactly the same probability of being chosen.3 So if there are k nuts left to test, each one will be chosen with probability 1/k. Now what’s the expected number of comparisons we have to perform? Intuitively, it should be about n/2, but let’s formalize our intuition. Let T (n) denote the number of comparisons our algorithm uses to find a match for a single bolt out of n nuts.4 We still have some simple base cases T (1) = 0 and T (2) = 1, but when n > 2, T (n) is a random variable. T (n) is always between 1 and n − 1; it’s actual value depends on our algorithm’s random choices. We are interested in the expected value or expectation of T (n), which is defined as follows: n−1 E[ T (n)] = k · Pr[ T (n) = k] k =1 The notation E[ ] for expectation has nothing to do with the shift operator E used in the annihilator method for solving recurrences! 3 This is what most people think ‘random’ means, but they’re wrong. 4 Note that for this algorithm, the input is completely specified by the number n. Since we’re choosing the nuts to test at random, even the order in which the nuts and bolts are presented doesn’t matter. That’s why I’m using the simpler notation T (n) instead of T (X ). 2 2 Algorithms Lecture 5: Randomized Algorithms If the target nut is the kth nut tested, our algorithm performs min{k, n − 1} comparisons. In particular, if the target nut is the last nut chosen, we don’t actually test it. Because we choose the next nut to test uniformly at random, the target nut is equally likely—with probability exactly 1/n—to be the first, second, third, or kth bolt tested, for any k. Thus: Pr[ T (n) = k] = 1/ n if k < n − 1, 2/ n if k = n − 1. Plugging this into the definition of expectation gives us our answer. n−2 E[ T (n)] = k =1 n−1 = k =1 = = k n k n + 2(n − 1) + n n−1 n( n − 1) 2n n+1 2 − n +1− 1 n 1 n We can get exactly the same answer by thinking of this algorithm recursively. We always have to perform at least one test. With probability 1/n, we successfully find the matching nut and halt. With the remaining probability 1 − 1/n, we recursively solve the same problem but with one fewer nut. We get the following recurrence for the expected number of tests: T (1) = 0, E[ T (n)] = 1 + n−1 n E[ T (n − 1)] To get the solution, we define a new function t (n) = n E[ T (n)] and rewrite: t (1) = 0, t (n) = n + t (n − 1) This recurrence translates into a simple summation, which we can easily solve. n t ( n) = k= n(n + 1) 2 k =2 =⇒ E[ T (n)] = 5.4 t ( n) n = n+1 2 − −1 1 n Finding All Matches Not let’s go back to the problem introduced at the beginning of the lecture: finding the matching nut for every bolt. The simplest algorithm simply compares every nut with every bolt, for a total of n2 comparisons. The next thing we might try is repeatedly finding an arbitrary matched pair, using our very first nuts and bolts algorithm. This requires n (i − 1) = i =1 n2 − n 2 comparisons in the worst case. So we save roughly a factor of two over the really stupid algorithm. Not very exciting. 3 Algorithms Lecture 5: Randomized Algorithms Here’s another possibility. Choose a pivot bolt, and test it against every nut. Then test the matching pivot nut against every other bolt. After these 2n − 1 tests, we have one matched pair, and the remaining nuts and bolts are partitioned into two subsets: those smaller than the pivot pair and those larger than the pivot pair. Finally, recursively match up the two subsets. The worst-case number of tests made by this algorithm is given by the recurrence T (n) = 2n − 1 + max { T (k − 1) + T (n − k)} 1≤k≤n = 2 n − 1 + T ( n − 1) Along with the trivial base case T (0) = 0, this recurrence solves to n (2n − 1) = n2 . T ( n) = i =1 In the worst case, this algorithm tests every nut-bolt pair! We could have been a little more clever—for example, if the pivot bolt is the smallest bolt, we only need n − 1 tests to partition everything, not 2n − 1—but cleverness doesn’t actually help that much. We still end up with about n2 /2 tests in the worst case. However, since this recursive algorithm looks almost exactly like quicksort, and everybody ‘knows’ that the ‘average-case’ running time of quicksort is Θ(n log n), it seems reasonable to guess that the average number of nut-bolt comparisons is also Θ(n log n). As we shall see shortly, if the pivot bolt is always chosen uniformly at random, this intuition is exactly right. 5.5 Reductions to and from Sorting The second algorithm for mathing up the nuts and bolts looks exactly like quicksort. The algorithm not only matches up the nuts and bolts, but also sorts them by size. In fact, the problems of sorting and matching nuts and bolts are equivalent, in the following sense. If the bolts were sorted, we could match the nuts and bolts in O(n log n) time by performing a binary search with each nut. Thus, if we had an algorithm to sort the bolts in O(n log n) time, we would immediately have an algorithm to match the nuts and bolts, starting from scratch, in O(n log n) time. This process of assuming a solution to one problem and using it to solve another is called reduction—we can reduce the matching problem to the sorting problem in O(n log n) time. There is a reduction in the other direction, too. If the nuts and bolts were matched, we could sort them in O(n log n) time using, for example, merge sort. Thus, if we have an O(n log n) time algorithm for either sorting or matching nuts and bolts, we automatically have an O(n log n) time algorithm for the other problem. Unfortunately, since we aren’t allowed to directly compare two bolts or two nuts, we can’t use heapsort or mergesort to sort the nuts and bolts in O(n log n) worst case time. In fact, the problem of sorting nuts and bolts deterministically in O(n log n) time was only ‘solved’ in 19955 , but both the algorithms and their analysis are incredibly technical and the constant hidden in the O(·) notation is quite large. Reductions will come up again later in the course when we start talking about lower bounds and NP-completeness. 5 János Komlós, Yuan Ma, and Endre Szemerédi, Sorting nuts and bolts in O(n log n) time, SIAM J. Discrete Math 11(3):347– 372, 1998. See also Phillip G. Bradford, Matching nuts and bolts optimally, Technical Report MPI-I-95-1-025, Max-Planck-Institut für Informatik, September 1995. Bradford’s algorithm is slightly simpler. 4 Algorithms 5.6 Lecture 5: Randomized Algorithms Recursive Analysis Intuitively, we can argue that our quicksort-like algorithm will usually choose a bolt of approximately median size, and so the average numbers of tests should be O(n log n). We can now finally formalize this intuition. To simplify the notation slightly, I’ll write T (n) in place of E[ T (n)] everywhere. Our randomized matching/sorting algorithm chooses its pivot bolt uniformly at random from the set of unmatched bolts. Since the pivot bolt is equally likely to be the smallest, second smallest, or kth smallest for any k, the expected number of tests performed by our algorithm is given by the following recurrence: T ( n) = 2 n − 1 + E k T ( k − 1) + T ( n − k ) = 2n − 1 + 1 n n k =1 T ( k − 1) + T ( n − k ) The base case is T (0) = 0. (We can save a few tests by setting T (1) = 0 instead of 1, but the analysis will be easier if we’re a little stupid.) Yuck. At this point, we could simply guess the solution, based on the incessant rumors that quicksort runs in O(n log n) time in the average case, and prove our guess correct by induction. A similar inductive proof appears in [CLR, pp. 166–167], but it was removed from the new edition [CLRS]. That’s okay; nobody ever really understood that proof anyway. (See Section 5.8 below for details.) However, if we’re only interested in asymptotic bounds, we can afford to be a little conservative. What we’d really like is for the pivot bolt to be the median bolt, so that half the bolts are bigger and half the bolts are smaller. This isn’t very likely, but there is a good chance that the pivot bolt is close to the median bolt. Let’s say that a pivot bolt is good if it’s in the middle half of the final sorted set of bolts, that is, bigger than at least n/4 bolts and smaller than at least n/4 bolts. If the pivot bolt is good, then the worst split we can have is into one set of 3n/4 pairs and one set of n/4 pairs. If the pivot bolt is bad, then our algorithm is still better than starting over from scratch. Finally, a randomly chosen pivot bolt is good with probability 1/2. These simple observations give us the following simple recursive upper bound for the expected running time of our algorithm: T (n) ≤ 2n − 1 + 1 2 T 3n 4 +T n 4 + 1 2 · T ( n) A little algebra simplifies this even further: T ( n) ≤ 4 n − 2 + T 3n 4 +T n 4 We can solve this recurrence using the recursion tree method, giving us the unsurprising upper bound T (n) = O(n log n). A similar argument gives us the matching lower bound T (n) = Ω(n log n). Unfortunately, while this argument is convincing, it is not a formal proof, because it relies on the unproven assumption that T (n) is a convex function, which means that T (n + 1) + T (n − 1) ≥ 2 T (n) for all n. T (n) is actually convex, but we never proved it. Convexity follows form the closed-form solution of the recurrence, but using that fact would be circular logic. Sadly, formally proving convexity seems to be almost as hard as solving the recurrence. If we want a proof of the expected cost of our algorithm, we need another way to proceed. 5 Algorithms 5.7 Lecture 5: Randomized Algorithms Iterative Analysis By making a simple change to our algorithm, which has no effect on the number of tests, we can analyze it much more directly and exactly, without solving a recurrence or relying on hand-wavy intuition. The recursive subproblems solved by quicksort can be laid out in a binary tree, where each node corresponds to a subset of the nuts and bolts. In the usual recursive formulation, the algorithm partitions the nuts and bolts at the root, then the left child of the root, then the leftmost grandchild, and so forth, recursively sorting everything on the left before starting on the right subproblem. But we don’t have to solve the subproblems in this order. In fact, we can visit the nodes in the recursion tree in any order we like, as long as the root is visited first, and any other node is visited after its parent. Thus, we can recast quicksort in the following iterative form. Choose a pivot bolt, find its match, and partition the remaining nuts and bolts into two subsets. Then pick a second pivot bolt and partition whichever of the two subsets contains it. At this point, we have two matched pairs and three subsets of nuts and bolts. Continue choosing new pivot bolts and partitioning subsets, each time finding one match and increasing the number of subsets by one, until every bolt has been chosen as the pivot. At the end, every bolt has been matched, and the nuts and bolts are sorted. Suppose we always choose the next pivot bolt uniformly at random from the bolts that haven’t been pivots yet. Then no matter which subset contains this bolt, the pivot bolt is equally likely to be any bolt in that subset. That implies (by induction) that our randomized iterative algorithm performs exactly the same set of tests as our randomized recursive algorithm, but possibly in a different order. Now let Bi denote the i th smallest bolt, and N j denote the j th smallest nut. For each i and j , define an indicator variable X i j that equals 1 if our algorithm compares Bi with N j and zero otherwise. Then the total number of nut/bolt comparisons is exactly n n T ( n) = Xi j. i =1 j =1 We are interested in the expected value of this double summation: n E[ T (n)] = E n i =1 j =1 n Xi j = n E[X i j ]. i =1 j =1 This equation uses a crucial property of random variables called linearity of expectation: for any random variables X and Y , the sum of their expectations is equal to the expectation of their sum: E [ X + Y ] = E [ X ] + E [ Y ]. To analyze our algorithm, we only need to compute the expected value of each X i j . By definition of expectation, E[X i j ] = 0 · Pr[X i j = 0] + 1 · Pr[X i j = 1] = Pr[X i j = 1], so we just need to calculate Pr[X i j = 1] for all i and j . First let’s assume that i < j . The only comparisons our algorithm performs are between some pivot bolt (or its partner) and a nut (or bolt) in the same subset. The only thing that can prevent us from comparing Bi and N j is if some intermediate bolt Bk , with i < k < j , is chosen as a pivot before Bi or B j . In other words: Our algorithm compares Bi and N j if and only if the first pivot chosen from the set {Bi , Bi +1 , . . . , B j } is either Bi or B j . 6 Algorithms Lecture 5: Randomized Algorithms Since the set {Bi , Bi +1 , . . . , B j } contains j − i + 1 bolts, each of which is equally likely to be chosen first, we immediately have 2 E[ X i j ] = for all i < j . j−i+1 Symmetric arguments give us E[X i j ] = i −2+1 for all i > j . Since our algorithm is a little stupid, every j bolt is compared with its partner, so X ii = 1 for all i . (In fact, if a pivot bolt is the only bolt in its subset, we don’t need to compare it against its partner, but this improvement complicates the analysis.) Putting everything together, we get the following summation. n n E[ T (n)] = E[X i j ] i =1 j =1 n = n n E[X ii ] + 2 i =1 E[ X i j ] i =1 j = i +1 n n = n+4 i =1 j = i +1 1 j−i+1 This is quite a bit simpler than the recurrence we got before. With just a few more lines of algebra, we can turn it into an exact, closed-form expression for the expected number of comparisons. n n−i +1 E[ T (n)] = n + 4 i =1 n n−k+1 = n+4 k =2 [substitute k = j − i + 1] k j =2 = n+4 k =2 n 1 1 [reorder summations] k i =1 n−k+1 k n = n+4 1 ( n − 1) k k =2 n − 1 k =2 = n + 4((n + 1)(H n − 1) − (n − 1)) = 4nH n − 7n + 4H n Sure enough, it’s Θ(n log n). 5.8 Masochistic Analysis If we’re feeling particularly masochistic, we can actually solve the recurrence directly, all the way to an exact closed-form solution. I’m including this only to show you it can be done; this won’t be on the test. First we simplify the recurrence slightly by combining symmetric terms. T (n) = 2n − 1 + = 2n − 1 + 1 n T ( k − 1) + T ( n − k ) n k =1 2 n −1 n k =0 T (k) 7 Algorithms Lecture 5: Randomized Algorithms We then convert this ‘full history’ recurrence into a ‘limited history’ recurrence by shifting and subtracting away common terms. (I call this “Magic step #1”.) To make this step slightly easier, we first multiply both sides of the recurrence by n to get rid of the fractions. n −1 2 n T ( n) = 2 n − n + 2 T (k) k =0 n−2 (n − 1) T (n − 1) = 2(n − 1)2 − (n − 1) + 2 T (k) k =0 2n2 −5n+3 n T ( n) − ( n − 1) T ( n − 1) = 4 n − 3 + 2 T ( n − 1) 3 n+1 T ( n) = 4 − + T ( n − 1) n n To solve this limited-history recurrence, we define a new function t (n) = T (n)/(n + 1). (I call this “Magic step #2”.) This gives us an even simpler recurrence for t (n) in terms of t (n − 1): t ( n) = = = T (n) n+1 1 n+1 4 4− 3 n 3 + ( n + 1) T ( n − 1) n + t ( n − 1) n( n + 1) 3 = − + t ( n − 1) n+1 n n+1 7 − 1 I used the technique of partial fractions (remember calculus?) to replace n(n1 1) with 1 − n+1 in the last + n step. The base case for this recurrence is t (0) = 0. Once again, we have a recurrence that translates directly into a summation, which we can solve with just a few lines of algebra. n t (n) = i =1 n =7 i =1 7 i+1 1 i+1 − 3 i n −3 i =1 1 i = 7(H n+1 − 1) − 3H n 7 = 4H n − 7 + n+1 The last step uses the recursive definition of the harmonic numbers: H n+1 = H n + 1 . n+1 Finally, substituting T (n) = (n + 1) t (n) and simplifying gives us the exact solution to the original recurrence. T (n) = 4(n + 1)H n − 7(n + 1) + 7 = 4nH n − 7n + 4H n Surprise, surprise, we get exactly the same solution! 8 Algorithms Lecture 5: Randomized Algorithms Exercises Unless a problem specifically states otherwise, you can assume a function RANDOM(k ) that returns, given any positive integer k , an integer chosen independently and uniformly at random from the set {1, 2, . . . , k }, in O (1) time. For example, to perform a fair coin flip, one could call RANDOM(2). 1. Consider the following randomized algorithm for choosing the largest bolt. Draw a bolt uniformly at random from the set of n bolts, and draw a nut uniformly at random from the set of n nuts. If the bolt is smaller than the nut, discard the bolt, draw a new bolt uniformly at random from the unchosen bolts, and repeat. Otherwise, discard the nut, draw a new nut uniformly at random from the unchosen nuts, and repeat. Stop either when every nut has been discarded, or every bolt except the one in your hand has been discarded. What is the exact expected number of nut-bolt tests performed by this algorithm? Prove your answer is correct. [Hint: What is the expected number of unchosen nuts and bolts when the algorithm terminates?] 2. Consider the following algorithm for finding the smallest element in an unsorted array: RANDOMMIN(A[1 .. n]): min ← ∞ for i ← 1 to n in random order if A[i ] < min min ← A[i ] ( ) return min (a) In the worst case, how many times does RANDOMMIN execute line ( )? (b) What is the probability that line ( ) is executed during the nth iteration of the for loop? (c) What is the exact expected number of executions of line ( )? 3. Let S be a set of n points in the plane. A point p in S is called Pareto-optimal if no other point in S is both above and to the right of p. (a) Describe and analyze a deterministic algorithm that computes the Pareto-optimal points in S in O(n log n) time. (b) Suppose each point in S is chosen independently and uniformly at random from the unit square [0, 1] × [0, 1]. What is the exact expected number of Pareto-optimal points in S ? 4. Suppose we want to write an efficient function RANDOMPERMUTATION(n) that returns a permutation of the integers 〈1, . . . , n〉 chosen uniformly at random. (a) Prove that the following algorithm is not correct. [Hint: Consider the case n = 3.] RANDOMPERMUTATION(n): for i ← 1 to n π[i ] ← i for i ← 1 to n swap π[i ] ↔ π[RANDOM(n)] 9 Algorithms Lecture 5: Randomized Algorithms (b) Consider the following implementation of RANDOMPERMUTATION. RANDOMPERMUTATION(n): for i ← 1 to n π[i ] ← NULL for i ← 1 to n j ← RANDOM(n) while (π[ j ] != NULL) j ← RANDOM(n) π[ j ] ← i return π Prove that this algorithm is correct. Analyze its expected runtime. (c) Consider the following partial implementation of RANDOMPERMUTATION. RANDOMPERMUTATION(n): for i ← 1 to n A[i ] ← RANDOM(n) π ← SOMEFUNCTION(A) return π Prove that if the subroutine SOMEFUNCTION is deterministic, then this algorithm cannot be correct. [Hint: There is a one-line proof.] (d) Consider a correct implementation of RANDOMPERMUTATION(n) with the following property: whenever it calls RANDOM(k), the argument k is at most m. Prove that this algorithm always n log n calls RANDOM at least Ω( log m ) times. (e) Describe and analyze an implementation of RANDOMPERMUTATION that runs in expected worst-case time O(n). 5. Consider the following randomized algorithm for generating biased random bits. The subroutine FAIRCOIN returns either 0 or 1 with equal probability; the random bits returned by FAIRCOIN are mutually independent. ONEINTHREE: if FAIRCOIN = 0 return 0 else return 1 − ONEINTHREE (a) Prove that ONEINTHREE returns 1 with probability 1/3. (b) What is the exact expected number of times that this algorithm calls FAIRCOIN? (c) Now suppose you are given a subroutine ONEINTHREE that generates a random bit that is equal to 1 with probability 1/3. Describe a FAIRCOIN algorithm that returns either 0 or 1 with equal probability, using ONEINTHREE as a subroutine. For this question, your only source of randomness is ONEINTHREE; in particular, you may not use the RANDOM function. (d) What is the exact expected number of times that your FAIRCOIN algorithm calls ONEINTHREE? 10 Algorithms Lecture 5: Randomized Algorithms 6. Suppose n lights labeled 0, . . . , n − 1 are placed clockwise around a circle. Initially, every light is off. Consider the following random process. LIGHT THECIRCLE(n): k←0 turn on light 0 while at least one light is off with probability 1/2 k ← (k + 1) mod n else k ← (k − 1) mod n if light k is off, turn it on (a) Let p(i , n) be the probability that light i is the last to be turned on by LIGHT THECIRCLE(n, 0). For example, p(0, 2) = 0 and p(1, 2) = 1. Find an exact closed-form expression for p(i , n) in terms of n and i . Prove your answer is correct. (b) Give the tightest upper bound you can on the expected running time of this algorithm. 7. Consider a random walk on a path with vertices numbered 1, 2, . . . , n from left to right. At each step, we flip a coin to decide which direction to walk, moving one step left or one step right with equal probability. The random walk ends when we fall off one end of the path, either by moving left from vertex 1 or by moving right from vertex n. (a) Prove that the probability that the walk ends by falling off the right end of the path is exactly 1/(n + 1). (b) Prove that if we start at vertex k, the probability that we fall off the right end of the path is exactly k/(n + 1). (c) Prove that if we start at vertex 1, the expected number of steps before the random walk ends is exactly n. (d) Suppose we start at vertex n/2 instead. State and prove a tight Θ-bound on the expected length of the random walk in this case. 8. A data stream is an extremely long sequence of items that you can only read only once, in order. A good example of a data stream is the sequence of packets that pass through a router. Data stream algorithms must process each item in the stream quickly, using very little memory; there is simply too much data to store, and it arrives too quickly for any complex computations. Every data stream algorithm looks roughly like this: DOSOMETHINGINTERESTING(stream S ): repeat x ← next item in S 〈〈do something fast with x 〉〉 until S ends return 〈〈something〉〉 Describe and analyze an algorithm that chooses one element uniformly at random from a data stream, without knowing the length of the stream in advance. Your algorithm should spend O(1) time per stream element and use O(1) space (not counting the stream itself). 11 Algorithms Lecture 5: Randomized Algorithms 9. The following randomized algorithm, sometimes called ‘one-armed quicksort’, selects the r th smallest element in an unsorted array A[1 .. n]. For example, to find the smallest element, you would call RANDOMSELECT(A, 1); to find the median element, you would call RANDOMSELECT(A, n/2 ). Recall from lecture that PARTITION splits the array into three parts by comparing the pivot element A[ p] to every other element of the array, using n − 1 comparisons altogether, and returns the new index of the pivot element. The subroutine RANDOM(n) returns an integer chosen uniformly at random between 1 and n, in O(1) time. RANDOMSELECT(A[1 .. n], r ) : k ← PARTITION(A[1 .. n], RANDOM(n)) if r < k return RANDOMSELECT(A[1 .. k − 1], r ) else if r > k return RANDOMSELECT(A[k + 1 .. n], r − k) else return A[k] (a) State a recurrence for the expected running time of RANDOMSELECT, as a function of n and r . (b) What is the exact probability that RANDOMSELECT compares the i th smallest and j th smallest elements in the input array? The correct answer is a simple function of i , j , and r . [Hint: Check your answer by trying a few small examples.] (c) Show that for any n and r , the expected running time of RANDOMSELECT is Θ(n). You can use either the recurrence from part (a) or the probabilities from part (b). For extra credit, find the exact expected number of comparisons, as a function of n and r . (d) What is the expected number of times that RANDOMSELECT calls itself recursively? 10. Let M [1 .. n][1 .. n] be an n × n matrix in which every row and every column is sorted. Such an array is called totally monotone. No two elements of M are equal. (a) Describe and analyze an algorithm to solve the following problem in O(n) time: Given indices i , j , i , j as input, compute the number of elements of M smaller than M [i ][ j ] and larger than M [i ][ j ]. (b) Describe and analyze an algorithm to solve the following problem in O(n) time: Given indices i , j , i , j as input, return an element of M chosen uniformly at random from the elements smaller than M [i ][ j ] and larger than M [i ][ j ]. Assume the requested range is always non-empty. (c) Describe and analyze a randomized algorithm to compute the median element of M in O(n log n) expected time. 11. Clock Solitaire is played with a standard deck of 52 cards, containing 13 ranks of cards in four different suits. To set up the game, deal the cards face down into 13 piles of four cards each, one in each of the ‘hour’ positions of a clock and one in the center. Each pile corresponds to a particular rank—A through Q in clockwise order for the hour positions, and K for the center. To start the game, turn over a card in the center pile. Then repeatedly turn over a card in the pile corresponding to the value of the previous card. The game ends when you try to turn over a card from a pile whose four cards are already face up. (This is always the center pile—why?) You win if and only if every card is face up when the game ends. 12 Algorithms Lecture 5: Randomized Algorithms What is the exact probability that you win a game of Clock Solitaire, assuming that the cards are permuted uniformly at random before they are dealt into their piles? 12. Suppose we have a circular linked list of numbers, implemented as a pair of arrays, one storing the actual numbers and the other storing successor pointers. Specifically, let X [1 .. n] be an array of n distinct real numbers, and let N [1 .. n] be an array of indices with the following property: If X [i ] is the largest element of X , then X [N [i ]] is the smallest element of X ; otherwise, X [N [i ]] is the smallest element of X that is larger than X [i ]. For example: i1 2 3 4 5 6 7 8 9 X [i ] 83 54 16 31 45 99 78 62 27 N [i ] 6 8 9 5 2 3 1 7 4 Describe and analyze a randomized algorithm that determines whether a given number x appears in the array X in O( n) expected time. Your algorithm may not modify the arrays X and N. 13. Death knocks on your door one cold blustery morning and challenges you to a game. Death knows that you are an algorithms student, so instead of the traditional game of chess, Death presents you with a complete binary tree with 4n leaves, each colored either black or white. There is a token at the root of the tree. To play the game, you and Death will take turns moving the token from its current node to one of its children. The game will end after 2n moves, when the token lands on a leaf. If the final leaf is black, you die; if it’s white, you will live forever. You move first, so Death gets the last turn. ∨ ∧ ∧ ∨ ∨ ∧ ∧ ∧ ∨ ∧ ∧ ∨ ∧ ∧ ∧ You can decide whether it’s worth playing or not as follows. Imagine that the nodes at even levels (where it’s your turn) are OR gates, the nodes at odd levels (where it’s Death’s turn) are AND gates. Each gate gets its input from its children and passes its output to its parent. White and black stand for TRUE and FALSE. If the output at the top of the tree is TRUE, then you can win and live forever! If the output at the top of the tree is FALSE, you should challenge Death to a game of Twister instead. (a) Describe and analyze a deterministic algorithm to determine whether or not you can win. [Hint: This is easy!] (b) Unfortunately, Death won’t give you enough time to look at every node in the tree. Describe a randomized algorithm that determines whether you can win in O(3n ) expected time. [Hint: Consider the case n = 1.] (c) Describe and analyze a randomized algorithm that determines whether you can win in O(c n ) expected time, for some constant c < 3. [Hint: You may not need to change your algorithm from part (b) at all!] 13 Algorithms Lecture 5: Randomized Algorithms 14. A majority tree is a complete binary tree with depth n, where every leaf is labeled either 0 or 1. The value of a leaf is its label; the value of any internal node is the majority of the values of its three children. Consider the problem of computing the value of the root of a majority tree, given the sequence of 3n leaf labels as input. For example, if n = 2 and the leaves are labeled 1, 0, 0, 0, 1, 0, 1, 1, 1, the root has value 0. 0 0 0 1 100 010 111 A majority tree with depth n = 2. (a) Prove that any deterministic algorithm that computes the value of the root of a majority tree must examine every leaf. [Hint: Consider the special case n = 1. Recurse.] (b) Describe and analyze a randomized algorithm that computes the value of the root in worstcase expected time O(c n ) for some constant c < 3. [Hint: Consider the special case n = 1. Recurse.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 14 Algorithms Lecture 6: Treaps and Skip Lists I thought the following four [rules] would be enough, provided that I made a firm and constant resolution not to fail even once in the observance of them. The first was never to accept anything as true if I had not evident knowledge of its being so. . . . The second, to divide each problem I examined into as many parts as was feasible, and as was requisite for its better solution. The third, to direct my thoughts in an orderly way. . . establishing an order in thought even when the objects had no natural priority one to another. And the last, to make throughout such complete enumerations and such general surveys that I might be sure of leaving nothing out. — René Descartes, Discours de la Méthode (1637) What is luck? Luck is probability taken personally. It is the excitement of bad math. — Penn Jillette (2001), quoting Chip Denman (1998) 6 Randomized Binary Search Trees In this lecture, we consider two randomized alternatives to balanced binary search tree structures such as AVL trees, red-black trees, B-trees, or splay trees, which are arguably simpler than any of these deterministic structures. 6.1 Treaps A treap is a binary tree in which every node has both a search key and a priority, where the inorder sequence of search keys is sorted and each node’s priority is smaller than the priorities of its children.1 In other words, a treap is simultaneously a binary search tree for the search keys and a (min-)heap for the priorities. In our examples, we will use letters for the search keys and numbers for the priorities. M 1 H 2 G 7 A 9 T 3 I 4 R 5 L 8 O 6 A treap. The top half of each node shows its search key and the bottom half shows its priority. I’ll assume from now on that all the keys and priorities are distinct. Under this assumption, we can easily prove by induction that the structure of a treap is completely determined by the search keys and priorities of its nodes. Since it’s a heap, the node v with highest priority must be the root. Since it’s also a binary search tree, any node u with key(u) < key( v ) must be in the left subtree, and any node w with key(w ) > key( v ) must be in the right subtree. Finally, since the subtrees are treaps, by induction, their structures are completely determined. The base case is the trivial empty treap. Another way to describe the structure is that a treap is exactly the binary tree that results by inserting the nodes one at a time into an initially empty tree, in order of increasing priority, using the usual insertion algorithm. This is also easy to prove by induction. 1 Sometimes I hate English. Normally, ‘higher priority’ means ‘more important’, but ‘first priority’ is also more important than ‘second priority’. Maybe ‘posteriority’ would be better; one student suggested ‘unimportance’. 1 Algorithms Lecture 6: Treaps and Skip Lists A third description interprets the keys and priorities as the coordinates of a set of points in the plane. The root corresponds to a T whose joint lies on the topmost point. The T splits the plane into three parts. The top part is (by definition) empty; the left and right parts are split recursively. This interpretation has some interesting applications in computational geometry, which (unfortunately) we probably won’t have time to talk about. 1 2 3 4 5 6 7 8 9 A G H I L M O R T A geometric interpretation of the same treap. Treaps were first discovered by Jean Vuillemin in 1980, but he called them Cartesian trees.2 The word ‘treap’ was first used by Edward McCreight around 1980 to describe a slightly different data structure, but he later switched to the more prosaic name priority search trees.3 Treaps were rediscovered and used to build randomized search trees by Cecilia Aragon and Raimund Seidel in 1989.4 A different kind of randomized binary search tree, which uses random rebalancing instead of random priorities, was later discovered and analyzed by Conrado Martínez and Salvador Roura in 1996.5 6.2 Treap Operations The search algorithm is the usual one for binary search trees. The time for a successful search is proportional to the depth of the node. The time for an unsuccessful search is proportional to the depth of either its successor or its predecessor. To insert a new node z , we start by using the standard binary search tree insertion algorithm to insert it at the bottom of the tree. At the point, the search keys still form a search tree, but the priorities may no longer form a heap. To fix the heap property, as long as z has smaller priority than its parent, perform a rotation at z , a local operation that decreases the depth of z by one and increases its parent’s depth by one, while maintaining the search tree property. Rotations can be performed in constant time, since they only involve simple pointer manipulation. y right x x y left A right rotation at x and a left rotation at y are inverses. 2 J. Vuillemin, A unifying look at data structures. Commun. ACM 23:229–239, 1980. E. M. McCreight. Priority search trees. SIAM J. Comput. 14(2):257–276, 1985. 4 R. Seidel and C. R. Aragon. Randomized search trees. Algorithmica 16:464–497, 1996. 5 C. Martínez and S. Roura. Randomized binary search trees. J. ACM 45(2):288-323, 1998. The results in this paper are virtually identical (including the constant factors!) to the corresponding results for treaps, although the analysis techniques are quite different. 3 2 Algorithms Lecture 6: Treaps and Skip Lists The overall time to insert z is proportional to the depth of z before the rotations—we have to walk down the treap to insert z , and then walk back up the treap doing rotations. Another way to say this is that the time to insert z is roughly twice the time to perform an unsuccessful search for key(z ). M 1 M 1 H 2 G 7 A 9 T 3 I 4 H 2 G 7 R 5 L 8 O 6 M 1 S −1 A 9 T 3 L 8 S −1 H 2 S −1 I 4 S −1 G 7 R 5 A 9 I 4 R 5 L 8 M 1 T 3 H 2 O 6 O 6 T 3 G 7 A 9 R 5 I 4 O 6 L 8 Left to right: After inserting (S , 10), rotate it up to fix the heap property. Right to left: Before deleting (S , 10), rotate it down to make it a leaf. Deleting a node is exactly like inserting a node, but in reverse order. Suppose we want to delete node z . As long as z is not a leaf, perform a rotation at the child of z with smaller priority. This moves z down a level and its smaller-priority child up a level. The choice of which child to rotate preserves the heap property everywhere except at z . When z becomes a leaf, chop it off. We sometimes also want to split a treap T into two treaps T< and T> along some pivot key π, so that all the nodes in T< have keys less than π and all the nodes in T> have keys bigger then π. A simple way to do this is to insert a new node z with key(z ) = π and priority(z ) = −∞. After the insertion, the new node is the root of the treap. If we delete the root, the left and right sub-treaps are exactly the trees we want. The time to split at π is roughly twice the time to (unsuccessfully) search for π. Similarly, we may want to merge two treaps T< and T> , where every node in T< has a smaller search key than any node in T> , into one super-treap. Merging is just splitting in reverse—create a dummy root whose left sub-treap is T< and whose right sub-treap is T> , rotate the dummy node down to a leaf, and then cut it off. The cost of each of these operations is proportional to the depth of some node v in the treap. • Search: A successful search for key k takes O(depth( v )) time, where v is the node with key( v ) = k. For an unsuccessful search, let v − be the inorder predecessor of k (the node whose key is just barely smaller than k), and let v + be the inorder successor of k (the node whose key is just barely larger than k). Since the last node examined by the binary search is either v − or v + , the time for an unsuccessful search is either O(depth( v + )) or O(depth( v − )). • Insert/Delete: Inserting a new node with key k takes either O(depth( v + )) time or O(depth( v − )) time, where v + and v − are the predecessor and successor of the new node. Deletion is just insertion in reverse. • Split/Merge: Splitting a treap at pivot value k takes either O(depth( v + )) time or O(depth( v − )) time, since it costs the same as inserting a new dummy root with search key k and priority −∞. Merging is just splitting in reverse. Since the depth of a node in a treap is Θ(n) in the worst case, each of these operations has a worst-case running time of Θ(n). 6.3 Random Priorities A randomized treap is a treap in which the priorities are independently and uniformly distributed continuous random variables. That means that whenever we insert a new search key into the treap, we generate a 3 Algorithms Lecture 6: Treaps and Skip Lists random real number between (say) 0 and 1 and use that number as the priority of the new node. The only reason we’re using real numbers is so that the probability of two nodes having the same priority is zero, since equal priorities make the analysis slightly messier. In practice, we could just choose random integers from a large range, like 0 to 231 − 1, or random floating point numbers. Also, since the priorities are independent, each node is equally likely to have the smallest priority. The cost of all the operations we discussed—search, insert, delete, split, join—is proportional to the depth of some node in the tree. Here we’ll see that the expected depth of any node is O(log n), which implies that the expected running time for any of those operations is also O(log n). Let x k denote the node with the kth smallest search key. To analyze the expected depth, we define an indicator variable Aik = x i is a proper ancestor of x k . (The superscript doesn’t mean power in this case; it just a reminder of which node is supposed to be further up in the tree.) Since the depth of v is just the number of proper ancestors of v , we have the following identity: n depth( x k ) = i =1 Aik . Now we can express the expected depth of a node in terms of these indicator variables as follows. n E[depth( x k )] = i =1 Pr[Aik = 1] (Just as in our analysis of matching nuts and bolts, we’re using linearity of expectation and the fact that E[X ] = Pr[X = 1] for any indicator variable X .) So to compute the expected depth of a node, we just have to compute the probability that some node is a proper ancestor of some other node. Fortunately, we can do this easily once we prove a simple structural lemma. Let X (i , k) denote either the subset of treap nodes { x i , x i +1 , . . . , x k } or the subset { x k , x k+1 , . . . , x i }, depending on whether i < k or i > k. X (i , k) and X (k, i ) always denote prceisly the same subset, and this subset contains |k − i | + 1 nodes. X (1, n) = X (n, 1) contains all n nodes in the treap. Lemma 1. For all i = k, x i is a proper ancestor of x k if and only if x i has the smallest priority among all nodes in X (i , k). Proof: If x i is the root, then it is an ancestor of x k , and by definition, it has the smallest priority of any node in the treap, so it must have the smallest priority in X (i , k). On the other hand, if x k is the root, then x i is not an ancestor of x k , and indeed x i does not have the smallest priority in X (i , k) — x k does. On the gripping hand6 , suppose some other node x j is the root. If x i and x k are in different subtrees, then either i < j < k or i > j > k, so x j ∈ X (i , k). In this case, x i is not an ancestor of x k , and indeed x i does not have the smallest priority in X (i , k) — x j does. Finally, if x i and x k are in the same subtree, the lemma follows inductively (or, if you prefer, recursively), since the subtree is a smaller treap. The empty treap is the trivial base case. Since each node in X (i , k) is equally likely to have smallest priority, we immediately have the probability we wanted: 1 if i < k k − i +1 [i = k] i Pr[Ak = 1] = =0 if i = k |k − i | + 1 1 if i > k i − k +1 6 See Larry Niven and Jerry Pournelle, The Gripping Hand, Pocket Books, 1994. 4 Algorithms Lecture 6: Treaps and Skip Lists To compute the expected depth of a node x k , we just plug this probability into our formula and grind through the algebra. k −1 n E[depth( x k )] = i =1 Pr[Aik = 1] = i =1 k = j =2 n 1 k−i+1 1 j + n−k+1 + i =2 i = k +1 1 i−k+1 1 j = H k − 1 + H n−k+1 − 1 < ln k + ln(n − k + 1) − 2 < 2 ln n − 2. In conclusion, every search, insertion, deletion, split, and merge operation in an n-node randomized binary search tree takes O(log n) expected time. Since a treap is exactly the binary tree that results when you insert the keys in order of increasing priority, a randomized treap is the result of inserting the keys in random order. So our analysis also automatically gives us the expected depth of any node in a binary tree built by random insertions (without using priorities). 6.4 Randomized Quicksort (Again!) We’ve already seen two completely different ways of describing randomized quicksort. The first is the familiar recursive one: choose a random pivot, partition, and recurse. The second is a less familiar iterative version: repeatedly choose a new random pivot, partition whatever subset contains it, and continue. But there’s a third way to describe randomized quicksort, this time in terms of binary search trees. RANDOMIZEDQUICKSORT: T ← an empty binary search tree insert the keys into T in random order output the inorder sequence of keys in T Our treap analysis tells us is that this algorithm will run in O(n log n) expected time, since each key is inserted in O(log n) expected time. Why is this quicksort? Just like last time, all we’ve done is rearrange the order of the comparisons. Intuitively, the binary tree is just the recursion tree created by the normal version of quicksort. In the recursive formulation, we compare the initial pivot against everything else and then recurse. In the binary tree formulation, the first “pivot” becomes the root of the tree without any comparisons, but then later as each other key is inserted into the tree, it is compared against the root. Either way, the first pivot chosen is compared with everything else. The partition splits the remaining items into a left subarray and a right subarray; in the binary tree version, these are exactly the items that go into the left subtree and the right subtree. Since both algorithms define the same two subproblems, by induction, both algorithms perform the same comparisons. We even saw the probability |k−1|+1 before, when we were talking about sorting nuts and bolts i with a variant of randomized quicksort. In the more familiar setting of sorting an array of numbers, the probability that randomized quicksort compares the i th largest and kth largest elements is exactly 2 . The binary tree version compares x i and x k if and only if x i is an ancestor of x k or vice versa, so |k−i |+1 the probabilities are exactly the same. 5 Algorithms 6.5 Lecture 6: Treaps and Skip Lists Skip Lists Skip lists, which were first discovered by Bill Pugh in the late 1980’s,7 have many of the usual desirable properties of balanced binary search trees, but their structure is very different. At a high level, a skip list is just a sorted linked list with some random shortcuts. To do a search in a normal singly-linked list of length n, we obviously need to look at n items in the worst case. To speed up this process, we can make a second-level list that contains roughly half the items from the original list. Specifically, for each item in the original list, we duplicate it with probability 1/2. We then string together all the duplicates into a second sorted linked list, and add a pointer from each duplicate back to its original. Just to be safe, we also add sentinel nodes at the beginning and end of both lists. −∞ 0 1 −∞ 0 1 3 2 3 6 4 5 7 6 7 9 8 +∞ 9 +∞ A linked list with some randomly-chosen shortcuts. Now we can find a value x in this augmented structure using a two-stage algorithm. First, we scan for x in the shortcut list, starting at the −∞ sentinel node. If we find x , we’re done. Otherwise, we reach some value bigger than x and we know that x is not in the shortcut list. Let w be the largest item less than x in the shortcut list. In the second phase, we scan for x in the original list, starting from w . Again, if we reach a value bigger than x , we know that x is not in the data structure. −∞ 0 1 −∞ 0 1 3 2 3 6 4 5 7 6 7 9 8 +∞ 9 +∞ Searching for 5 in a list with shortcuts. Since each node appears in the shortcut list with probability 1/2, the expected number of nodes examined in the first phase is at most n/2. Only one of the nodes examined in the second phase has a duplicate. The probability that any node is followed by k nodes without duplicates is 2−k , so the expected number of nodes examined in the second phase is at most 1 + k≥0 2−k = 2. Thus, by adding these random shortcuts, we’ve reduced the cost of a search from n to n/2 + 2, roughly a factor of two in savings. 6.6 Recursive Random Shortcuts Now there’s an obvious improvement—add shortcuts to the shortcuts, and repeat recursively. That’s exactly how skip lists are constructed. For each node in the original list, we flip a coin over and over until we get tails. For each heads, we make a duplicate of the node. The duplicates are stacked up in levels, and the nodes on each level are strung together into sorted linked lists. Each node v stores a search key (key( v )), a pointer to its next lower copy (down( v )), and a pointer to the next node in its level (right( v )). 7 William Pugh. Skip lists: A probabilistic alternative to balanced trees. Commun. ACM 33(6):668–676, 1990. 6 Algorithms Lecture 6: Treaps and Skip Lists −∞ +∞ 7 +∞ 7 +∞ 6 7 +∞ 6 7 6 7 −∞ −∞ 1 −∞ 1 −∞ 0 1 −∞ 0 1 3 2 3 4 5 9 8 +∞ 9 +∞ A skip list is a linked list with recursive random shortcuts. The search algorithm for skip lists is very simple. Starting at the leftmost node L in the highest level, we scan through each level as far as we can without passing the target value x , and then proceed down to the next level. The search ends when we either reach a node with search key x or fail to find x on the lowest level. SKIPLISTFIND( x , L ): v←L while ( v = NULL and key( v ) = x ) if key(right( v )) > x v ← down( v ) else v ← right( v ) return v −∞ +∞ 7 +∞ 7 +∞ 6 7 +∞ 6 7 6 7 −∞ −∞ 1 −∞ 1 −∞ 0 1 −∞ 0 1 3 2 3 4 5 9 8 +∞ 9 +∞ Searching for 5 in a skip list. Intuitively, since each level of the skip lists has about half the number of nodes as the previous level, the total number of levels should be about O(log n). Similarly, each time we add another level of random shortcuts to the skip list, we cut the search time roughly in half, except for a constant overhead, so after O(log n) levels, we should have a search time of O(log n). Let’s formalize each of these two intuitive observations. 6.7 Number of Levels The actual values of the search keys don’t affect the skip list analysis, so let’s assume the keys are the integers 1 through n. Let L ( x ) be the number of levels of the skip list that contain some search key x , not counting the bottom level. Each new copy of x is created with probability 1/2 from the previous level, essentially by flipping a coin. We can compute the expected value of L ( x ) recursively—with probability 7 Algorithms Lecture 6: Treaps and Skip Lists 1/2, we flip tails and L ( x ) = 0; and with probability 1/2, we flip heads, increase L ( x ) by one, and recurse: 1 1 E [ L ( x )] = · 0 + 1 + E [ L ( x )] 2 2 Solving this equation gives us E [L( x )] = 1. In order to analyze the expected worst-case cost of a search, however, we need a bound on the number of levels L = max x L ( x ). Unfortunately, we can’t compute the average of a maximum the way we would compute the average of a sum. Instead, we will derive a stronger result, showing that the depth is O(log n) with high probability. ‘High probability’ is a technical term that means the probability is at least 1 − 1/nc for some constant c ≥ 1; the hidden constant in the O(log n) bound could depend on c . In order for a search key x to appear on level , it must have flipped heads in a row when it was inserted, so Pr[L( x ) ≥ ] = 2− . The skip list has at least levels if and only if L ( x ) ≥ for at least one of the n search keys. Pr[ L ≥ ] = Pr ( L (1) ≥ ) ∨ ( L (2) ≥ ) ∨ · · · ∨ ( L (n) ≥ ) Using the union bound — Pr[A ∨ B ] ≤ P r [A] + Pr[B ] for any random events A and B — we can simplify this as follows: n n Pr[ L ≥ ] ≤ Pr[ L ( x ) ≥ ] = n · Pr[ L ( x ) ≥ ] = . 2 x =1 When ≤ lg n, this bound is trivial. However, for any constant c > 1, we have a strong upper bound Pr[ L ≥ c lg n] ≤ 1 n c −1 . We conclude that with high probability, a skip list has O (log n ) levels. This high-probability bound indirectly implies a bound on the expected number of levels. Some simple algebra gives us the following alternate definition for expectation: E[ L ] = · Pr[ L = ] = ≥0 Clearly, if < , then Pr[ L ( x ) ≥ ] > Pr[ L ( x ) ≥ number of levels as follows: E[ L ( x )] = Pr[ L ≥ ] ≥1 . So we can derive an upper bound on the expected Pr[ L ≥ ] ≥1 lg n = Pr[ L ≥ ] + Pr[ L ≥ ] =1 ≥lg n+1 lg n n 1+ ≤ =1 ≥lg n+1 2 1 = lg n + i ≥1 [i = − lg n] 2i = lg n + 2 So in expectation, a skip list has at most two more levels than an ideal version where each level contains exactly half the nodes of the next level below. 8 Algorithms 6.8 Lecture 6: Treaps and Skip Lists Logarithmic Search Time It’s a little easier to analyze the cost of a search if we imagine running the algorithm backwards. D N I F T S I L P I K S takes the output from S KIP L IST F IND as input and traces back through the data structure to the upper left corner. Skip lists don’t really have up and left pointers, but we’ll pretend that they do so we don’t have to write ‘) v (nwod ← v ’ or ‘) v (thgir ← v ’.8 D N I F T S I L P I K S( v ): while ( v = L ) if up( v ) exists v ← up( v ) else v ← left( v ) Now for every node v in the skip list, up( v ) exists with probability 1/2. So for purposes of analysis, D N I F T S I L P I K S is equivalent to the following algorithm: FLIPWALK( v ): while ( v = L ) if COINFLIP = HEADS v ← up( v ) else v ← left( v ) Obviously, the expected number of heads is exactly the same as the expected number of TAILs. Thus, the expected running time of this algorithm is twice the expected number of upward jumps. Since we already know that the number of upward jumps is O(log n) with high probability, we can conclude that the worst-case search time is O(log n) with high probability (and therefore in expectation). Exercises 1. Prove that the expected number of proper descendants of any node in a treap is exactly equal to the expected depth of that node. 2. What is the exact expected number of leaves in an n-node treap? [Hint: What is the probability that in an n-node treap, the node with kth smallest search key is a leaf?] 3. Consider an n-node treap T . As in the lecture notes, we identify nodes in T by the ranks of their search keys. Thus, ‘node 5’ means the node with the 5th smallest search key. Let i , j , k be integers such that 1 ≤ i ≤ j ≤ k ≤ n. (a) What is the exact probability that node j is a common ancestor of node i and node k? (b) What is the exact expected length of the unique path from node i to node k in T ? 4. Recall that a priority search tree is a binary tree in which every node has both a search key and a priority, arranged so that the tree is simultaneously a binary search tree for the keys and a min-heap for the priorities. A heater is a priority search tree in which the priorities are given by 8 .terces seirevocsid sih peek ot detnaw eh esuaceb ton tub ,gnitirw-rorrim gnisu seton sih lla etorw icniV ad odranoeL !dnah thgir sih ni sitirhtra dab yllaer dah tsuj eH 9 Algorithms Lecture 6: Treaps and Skip Lists the user, and the search keys are distributed uniformly and independently at random in the real interval [0, 1]. Intuitively, a heater is a sort of dual treap. The following problems consider an n-node heater T whose node priorities are the integers from 1 to n. We identify nodes in T by their priorities; thus, ‘node 5’ means the node in T with priority 5. The min-heap property implies that node 1 is the root of T . Finally, let i and j be integers with 1 ≤ i < j ≤ n. (a) Prove that in a random permutation of the (i + 1)-element set {1, 2, . . . , i , j }, elements i and j are adjacent with probability 2/(i + 1). (b) Prove that node i is an ancestor of node j with probability 2/(i + 1). [Hint: Use part (a)!] (c) What is the probability that node i is a descendant of node j ? [Hint: Don’t use part (a)!] (d) What is the exact expected depth of node j ? (e) Describe and analyze an algorithm to insert a new item into a heater. Express the expected running time of the algorithm in terms of the rank of the newly inserted item. (f) Describe an algorithm to delete the minimum-priority item (the root) from an n-node heater. What is the expected running time of your algorithm? 5. A meldable priority queue stores a set of keys from some totally-ordered universe (such as the integers) and supports the following operations: • MAKEQUEUE: Return a new priority queue containing the empty set. • FINDMIN(Q): Return the smallest element of Q (if any). • DELETEMIN(Q): Remove the smallest element in Q (if any). • INSERT(Q, x ): Insert element x into Q, if it is not already there. • DECREASEKEY(Q, x , y ): Replace an element x ∈ Q with a smaller key y . (If y > x , the operation fails.) The input is a pointer directly to the node in Q containing x . • DELETE(Q, x ): Delete the element x ∈ Q. The input is a pointer directly to the node in Q containing x . • MELD(Q 1 , Q 2 ): Return a new priority queue containing all the elements of Q 1 and Q 2 ; this operation destroys Q 1 and Q 2 . A simple way to implement such a data structure is to use a heap-ordered binary tree, where each node stores a key, along with pointers to its parent and two children. MELD can be implemented using the following randomized algorithm: MELD(Q 1 , Q 2 ): if Q 1 is empty return Q 2 if Q 2 is empty return Q 1 if key(Q 1 ) > key(Q 2 ) swap Q 1 ↔ Q 2 with probability 1/2 left(Q 1 ) ← MELD(left(Q 1 ), Q 2 ) else right(Q 1 ) ← MELD(right(Q 1 ), Q 2 ) return Q 1 10 Algorithms Lecture 6: Treaps and Skip Lists (a) Prove that for any heap-ordered binary trees Q 1 and Q 2 (not just those constructed by the operations listed above), the expected running time of MELD(Q 1 , Q 2 ) is O(log n), where n = |Q 1 | + |Q 2 |. [Hint: How long is a random root-to-leaf path in an n-node binary tree if each left/right choice is made with equal probability?] (b) Prove that MELD(Q 1 , Q 2 ) runs in O(log n) time with high probability. (c) Show that each of the other meldable priority queue operations can be implemented with at most one call to MELD and O(1) additional time. (This implies that every operation takes O(log n) time with high probability.) 6. In the usual theoretical presentation of treaps, the priorities are random real numbers chosen uniformly from the interval [0, 1], but in practice, computers only have access to random bits. This problem asks you to analyze a modification of treaps that takes this limitation into account. Suppose the priority of a node v is abstractly represented as an infinite sequence π v [1 .. ∞] of random bits, which is interpreted as the rational number ∞ π v [ i ] · 2− i . priority( v ) = i =1 However, only a finite number v of these bits are actually known at any given time. When a node v is first created, none of the priority bits are known: v = 0. We generate (or ‘reveal’) new random bits only when they are necessary to compare priorities. The following algorithm compares the priorities of any two nodes in O(1) expected time: LARGERPRIORITY( v, w ): for i ← 1 to ∞ if i > v v ← i ; π v [ i ] ← R ANDOM B IT if i > w w ← i ; πw [i ] ← RANDOMBIT if π v [i ] > πw [i ] return v else if π v [i ] < πw [i ] return w Suppose we insert n items one at a time into an initially empty treap. Let L = v v denote the total number of random bits generated by calls to LARGERPRIORITY during these insertions. (a) Prove that E [ L ] = Θ(n). (b) Prove that E [ v ] = Θ(1) for any node v . [Hint: This is equivalent to part (a). Why?] (c) Prove that E [ root ] = Θ(log n). [Hint: Why doesn’t this contradict part (b)?] 7. Any skip list L can be transformed into a binary search tree T (L) as follows. The root of T (L) is the leftmost node on the highest non-empty level of L; the left and subtrees are constructed recursively from the nodes to the left and to the right of the root. Let’s call the resulting tree T (L) a skip list tree. 11 Algorithms Lecture 6: Treaps and Skip Lists (a) Show that any search in T (L) is no more expensive than the corresponding search in L. (Searching in T (L) could be considerably cheaper—why?) (b) Describe an algorithm to insert a new search key into a skip list tree in O(log n) expected time. Inserting key x into T (L) should produce exactly the same tree as inserting x into L and then transforming L into a tree. [Hint: You will need to maintain some additional information in the tree nodes.] (c) Describe an algorithm to delete a search key from a skip list tree in O(log n) expected time. Again, deleting key x from T (L) should produce exactly the same tree as deleting x from L and then transforming L into a tree. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 12 Algorithms Non-Lecture E: Tail Inequalities If you hold a cat by the tail you learn things you cannot learn any other way. — Mark Twain E Tail Inequalities The simple recursive structure of skip lists made it relatively easy to derive an upper bound on the expected worst-case search time, by way of a stronger high-probability upper bound on the worst-case search time. We can prove similar results for treaps, but because of the more complex recursive structure, we need slightly more sophisticated probabilistic tools. These tools are usually called tail inequalities; intuitively, they bound the probability that a random variable with a bell-shaped distribution takes a value in the tails of the distribution, far away from the mean. E.1 Markov’s Inequality Perhaps the simplest tail inequality was named after the Russian mathematician Andrey Markov; however, in strict accordance with Stigler’s Law of Eponymy, it first appeared in the works of Markov’s probability teacher, Pafnuty Chebyshev.1 Markov’s Inequality. Let X be a non-negative integer random variable. For any t > 0, we have Pr[X ≥ t ] ≤ E[X ]/ t . Proof: The inequality follows from the definition of expectation by simple algebraic manipulation. ∞ E[ X ] = k · Pr[X = k] [definition of E[X ]] k =0 ∞ = Pr[X ≥ k] [algebra] Pr[X ≥ k] [since t < ∞] Pr[X ≥ t ] [since k < t ] k =0 t −1 ≥ k =0 t −1 ≥ k =0 = t · Pr[X ≥ t ] [algebra] Unfortunately, the bounds that Markov’s inequality implies (at least directly) are often very weak, even useless. (For example, Markov’s inequality implies that with high probability, every node in an n-node treap has depth O(n2 log n). Well, duh!) To get stronger bounds, we need to exploit some additional structure in our random variables. 1 The closely related tail bound traditionally called Chebyshev’s inequality was actually discovered by the French statistician Irénée-Jules Bienaymé, a friend and colleague of Chebyshev’s. 1 Algorithms E.2 Non-Lecture E: Tail Inequalities Sums of Indicator Variables A set of random variables X 1 , X 2 , . . . , X n are said to be mutually independent if and only if n n (X i = x i ) = Pr Pr[X i = x i ] i =1 i =1 for all possible values x 1 , x 2 , . . . , x n . For examples, different flips of the same fair coin are mutually independent, but the number of heads and the number of tails in a sequence of n coin flips are not independent (since they must add to n). Mutual independence of the X i ’s implies that the expectation of the product of the X i ’s is equal to the product of the expectations: n n Xi = E i =1 E[ X i ] . i =1 Moreover, if X 1 , X 2 , . . . , X n are independent, then for any function f , the random variables f (X 1 ), f (X 2 ), . . . , f (X n ) are also mutually independent. n Suppose X = i =1 X i is the sum of n mutually independent random indicator variables X i . For each i , let pi = Pr[X i = 1], and let µ = E[X ] = i E[X i ] = i pi . eδ Chernoff Bound (Upper Tail). Pr[X > (1 + δ)µ] < µ for any δ > 0. (1 + δ)1+δ Proof: The proof is fairly long, but it replies on just a few basic components: a clever substitution, Markov’s inequality, the independence of the X i ’s, The World’s Most Useful Inequality e x > 1 + x , a tiny bit of calculus, and lots of high-school algebra. We start by introducing a variable t , whose role will become clear shortly. P r [X > (1 + δ)µ] = Pr[e t X > e t (1+δ)µ ] To cut down on the superscripts, I’ll usually write exp( x ) instead of e x in the rest of the proof. Now apply Markov’s inequality to the right side of this equation: P r [X > (1 + δ)µ] < E[exp( t X )] exp( t (1 + δ)µ) . We can simplify the expectation on the right using the fact that the terms X i are independent. E[exp( t X )] = E exp t Xi =E i We can bound the individual expectations E e t X i exp( t X i ) = i E[exp( t X i )] i using The World’s Most Useful Inequality: E[exp( t X i )] = pi e t + (1 − pi ) = 1 + (e t − 1) pi < exp (e t − 1) pi This inequality gives us a simple upper bound for E[e t X ]: exp((e t − 1) pi ) < exp E[exp( t X )] < i (e t − 1) pi = exp((e t − 1)µ) i 2 Algorithms Non-Lecture E: Tail Inequalities Substituting this back into our original fraction from Markov’s inequality, we obtain P r [X > (1 + δ)µ] < E[exp( t X )] exp( t (1 + δ)µ) < exp((e t − 1)µ) exp( t (1 + δ)µ) = exp(e t − 1 − t (1 + δ)) µ Notice that this last inequality holds for all possible values of t . To obtain the final tail bound, we will choose t to make this bound as tight as possible. To minimize e t − 1 − t − t δ, we take its derivative with respect to t and set it to zero: d dt (e t − 1 − t (1 + δ)) = e t − 1 − δ = 0. (And you thought calculus would never be useful!) This equation has just one solution t = ln(1 + δ). Plugging this back into our bound gives us P r [X > (1 + δ)µ] < exp(δ − (1 + δ) ln(1 + δ)) µ = µ eδ (1 + δ)1+δ And we’re done! This form of the Chernoff bound can be a bit clumsy to use. A more complicated argument gives us the bound Pr[X > (1 + δ)µ] < e−µδ 2 /3 for any 0 < δ < 1. A similar argument gives us an inequality bounding the probability that X is significantly smaller than its expected value: Chernoff Bound (Lower Tail). Pr[X < (1 − δ)µ] < E.3 eδ (1 − δ)1−δ µ < e−µδ 2 /2 for any δ > 0. Back to Treaps In our analysis of randomized treaps, we defined the indicator variable Aik to have the value 1 if and only if the node with the i th smallest key (‘node i ’) was a proper ancestor of the node with the kth smallest key (‘node k’). We argued that [i = k] Pr[Aik = 1] = , |k − i | + 1 and from this we concluded that the expected depth of node k is n E[depth(k)] = i =1 Pr[Aik = 1] = H k + H n−k − 2 < 2 ln n. To prove a worst-case expected bound on the depth of the tree, we need to argue that the maximum depth of any node is small. Chernoff bounds make this argument easy, once we establish that the relevant indicator variables are mutually independent. Lemma 1. For any index k, the k − 1 random variables Aik with i < k are mutually independent. Similarly, for any index k, the n − k random variables Aik with i > k are mutually independent. 3 Algorithms Non-Lecture E: Tail Inequalities Proof: To simplify the notation, we explicitly consider only the case k = 1, although the argument generalizes easily to other values of k. Fix n − 1 arbitrary indicator values x 2 , x 3 , . . . , x n . We prove the lemma by induction on n, with the vacuous base case n = 1. The definition of conditional probability gives us n−1 n (Ai1 Pr (Aik = x i ) ∧ An = x n 1 = x i ) = Pr i =2 i =2 n−1 (Aik = x i ) An = x n · Pr An = x n 1 1 = Pr i =2 Now recall that = 1 if and only if node n has the smallest priority, and the other n − 2 indicator variables Ai1 depend only on the order of the priorities of nodes 1 through n − 1. There are exactly (n − 1)! permutations of the n priorities in which the nth priority is smallest, and each of these permutations is equally likely. Thus, An 1 n−1 n −1 (Aik = x i ) An = x n = Pr 1 Pr i =2 (Aik = x i ) i =2 A2 , . . . , An−1 1 1 The inductive hypothesis implies that the variables n−1 n−1 (Aik = x i ) = Pr are mutually independent, so i =2 i =2 Pr Ai1 = x i . We conclude that Pr n (Ai1 i =2 n −1 n−1 = x i ) = Pr An = x n · 1 Pr i =2 Ai1 = xi = i =1 Pr Ai1 = x i , or in other words, that the indicator variables are mutually independent. Theorem 2. The depth of a randomized treap with n nodes is O(log n) with high probability. Proof: First let’s bound the probability that the depth of node k is at most 8 ln n. There’s nothing special about the constant 8 here; I’m being generous to make the analysis easier. The depth is a sum of n indicator variables Aik , as i ranges from 1 to n. Our Observation allows us to partition these variables into two mutually independent subsets. Let d< (k) = i <k Aik and d> (k) = i <k Aik , so that depth(k) = d< (k) + d> (k). If depth(k) > 8 ln n, then either d< (k) > 4 ln n or d> (k) > 4 ln n. Chernoff’s inequality, with µ = E[d< (k)] = H k − 1 < ln n and δ = 3, bounds the probability that d< (k) > 4 ln n as follows. Pr[d< (k) > 4 ln n] < Pr[d< (k) > 4µ] < e3 44 µ < e3 44 ln n = nln(e 3 /44 ) = n3−4 ln 4 < 1 n2 . (The last step uses the fact that 4 ln 4 ≈ 5.54518 > 5.) The same analysis implies that Pr[d> (k) > 4 ln n] < 1/n2 . These inequalities imply the crude bound Pr[depth(k) > 4 ln n] < 2/n2 . Now consider the probability that the treap has depth greater than 10 ln n. Even though the distributions of different nodes’ depths are not independent, we can conservatively bound the probability of failure as follows: n n 2 Pr max depth(k) > 8 ln n = Pr (depth(k) > 8 ln n) ≤ Pr[depth(k) > 8 ln n] < . k n k =1 k =1 4 Algorithms Non-Lecture E: Tail Inequalities This argument implies more generally that for any constant c , the depth of the treap is greater than c ln n with probability at most 2/nc ln c −c . We can make the failure probability an arbitrarily small polynomial by choosing c appropriately. This lemma implies that any search, insertion, deletion, or merge operation on an n-node treap requires O(log n) time with high probability. In particular, the expected worst-case time for each of these operations is O(log n). Exercises 1. Prove that for any integer k such that 1 < k < n, the n − 1 indicator variables Aik with i = k are not mutually independent. [Hint: Consider the case n = 3.] 2. Recall from Exercise 1 in the previous note that the expected number of descendants of any node in a treap is O(log n). Why doesn’t the Chernoff-bound argument for depth imply that, with high probability, every node in a treap has O(log n) descendants? The conclusion is clearly bogus—Every treap has a node with n descendants!—but what’s the hole in the argument? 3. A heater is a sort of dual treap, in which the priorities of the nodes are given, but their search keys are generate independently and uniformly from the unit interval [0, 1]. You can assume all priorities and keys are distinct. (a) Prove that for any r , the node with the r th smallest priority has expected depth O(log r ). (b) Prove that an n-node heater has depth O(log n) with high probability. (c) Describe algorithms to perform the operations INSERT and DELETEMIN in a heater. What are the expected worst-case running times of your algorithms? In particular, can you express the expected running time of INSERT in terms of the priority rank of the newly inserted item? c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 5 Algorithms Lecture 7: Hash Tables Calvin: There! I finished our secret code! Hobbes: Let’s see. Calvin: I assigned each letter a totally random number, so the code will be hard to crack. For letter “A”, you write 3,004,572,688. “B” is 28,731,569 1 2 . / Hobbes: That’s a good code all right. Calvin: Now we just commit this to memory. Calvin: Did you finish your map of our neighborhood? Hoobes: Not yet. How many bricks does the front walk have? — Bill Watterson, “Calvin and Hobbes” (August 23, 1990) 7 7.1 Hash Tables Introduction A hash table is a data structure for storing a set of items, so that we can quickly determine whether an item is or is not in the set. The basic idea is to pick a hash function h that maps every possible item x to a small integer h( x ). Then we store x in slot h( x ) in an array. The array is the hash table. Let’s be a little more specific. We want to store a set of n items. Each item is an element of some finite1 set U called the universe; we use u to denote the size of the universe, which is just the number of items in U. A hash table is an array T [1 .. m], where m is another positive integer, which we call the table size. Typically, m is much smaller than u. A hash function is any function of the form h : U → {0, 1, . . . , m − 1}, mapping each possible item in U to a slot in the hash table. We say that an item x hashes to the slot T [h( x )]. Of course, if u = m, then we can always just use the trivial hash function h( x ) = x . In other words, use the item itself as the index into the table. This is called a direct access table, or more commonly, an array. In most applications, though, the universe of possible keys is orders of magnitude too large for this approach to be practical. Even when it is possible to allocate enough memory, we usually need to store only a small fraction of the universe. Rather than wasting lots of space, we should make m roughly equal to n, the number of items in the set we want to maintain. What we’d like is for every item in our set to hash to a different position in the array. Unfortunately, unless m = u, this is too much to hope for, so we have to deal with collisions. We say that two items x and y collide if the have the same hash value: h( x ) = h( y ). Since we obviously can’t store two items in the same slot of an array, we need to describe some methods for resolving collisions. The two most common methods are called chaining and open addressing. 7.2 Chaining In a chained hash table, each entry T [i ] is not just a single item, but rather (a pointer to) a linked list of all the items that hash to T [i ]. Let ( x ) denote the length of the list T [h( x )]. To see if an item x is in the hash table, we scan the entire list T [h( x )]. The worst-case time required to search for x is O(1) to compute h( x ) plus O(1) for every element in T [h( x )], or O(1 + ( x )) overall. Inserting and deleting x also take O(1 + ( x )) time. 1 This finiteness assumption is necessary for several of the technical details to work out, but can be ignored in practice. To hash elements from an infinite universe (for example, the positive integers), pretend that the universe is actually finite but very very large. In fact, in real practice, the universe actually is finite but very very large. For example, on most modern computers, there are only 264 integers (unless you use a big integer package like GMP in which case the number of integers is , 32 closer to 22 .) 1 Algorithms Lecture 7: Hash Tables GH RO M AL I T S A chained hash table with load factor 1. In the worst case, every item would be hashed to the same value, so we’d get just one long list of n items. In principle, for any deterministic hashing scheme, a malicious adversary can always present a set of items with exactly this property. In order to defeat such malicious behavior, we’d like to use a hash function that is as random as possible. Choosing a truly random hash function is completely impractical, but there are several heuristics for producing hash functions that behave randomly, or at least close to randomly on real data. Thus, we will analyze the performance as though our hash function were truly random. More formally, we make the following assumption. Simple uniform hashing assumption: If x = y then Pr h( x ) = h( y ) = 1/m. In the next section, I’ll describe a small set of functions with the property that a random hash function in this set satisfies the simple uniform hashing assumption. Most actual implementations of has tables use deterministic hash functions. These clearly violate the uniform hashing assumption—the collision probability is either 0 or 1, depending on the pair of items! Nevertheless, it is common practice to adopt the uniform hashing assumption as a convenient fiction for purposes of analysis. Let’s compute the expected value of ( x ) under this assumption; this will immediately imply a bound on the expected time to search for an item x . To be concrete, let’s suppose that x is not already stored in the hash table. For all items x and y , we define the indicator variable C x , y = h( x ) = h( y ) . (In case you’ve forgotten the bracket notation, C x , y = 1 if h( x ) = h( y ) and C x , y = 0 if h( x ) = h( y ).) Since the length of T [h( x )] is precisely equal to the number of items that collide with x , we have (x) = Cx, y . y ∈T We can rewrite the simple uniform hashing assumption as follows: x = y =⇒ E[C x , y ] = Pr[C x , y = 1] = 1 m . Now we just have to grind through the definitions. E[ ( x )] = 1 E Cx, y = y ∈T y ∈T m = n m We call this fraction n/m the load factor of the hash table. Since the load factor shows up everywhere, we will give it its own symbol α. α= 2 n m Algorithms Lecture 7: Hash Tables Our analysis implies that the expected time for an unsuccessful search in a chained hash table is Θ(1 + α). As long as the number if items n is only a constant factor bigger than the table size m, the search time is a constant. A similar analysis gives the same expected time bound (with a slightly smaller constant) for a successful search. Obviously, linked lists are not the only data structure we could use to store the chains; any data structure that can store a set of items will work. For example, if the universe U has a total ordering, we can store each chain in a balanced binary search tree. This reduces the expected time for any search to O(1 + log ( x )), and under the simple uniform hashing assumption, the expected time for any search is O(1 + log α). Another natural possibility is to work recursively! Specifically, for each T [i ], we maintain a hash table Ti containing all the items with hash value i . Collisions in those secondary tables are resolved recursively, by storing secondary overflow lists in tertiary hash tables, and so on. The resulting data structure is a tree of hash tables, whose leaves correspond to items that (at some level of the tree) are hashed without any collisions. If every hash table in this tree has size m, then the expected time for any search is O(logm n). In particular, if we set m = n, the expected time for any search is constant. On the other hand, there is no inherent reason to use the same hash table size everywhere; after all, hash tables deeper in the tree are storing fewer items. Caveat Lector!2 The preceding analysis does not imply bounds on the expected worst-case search time is constant. The expected worst-case search time is O(1 + L ), where L = max x ( x ). Under the uniform hashing assumption, the maximum list size L is very likely to grow faster than any constant, unless the load factor α is significantly smaller than 1. For example, E[ L ] = Θ(log n/ log log n) when α = 1. We’ve stumbled on a powerful but counterintuitive fact about probability: When several individual items are distributed independently and uniformly at random, the resulting distribution is not uniform in the traditional sense! In a later section, I’ll describe how to achieve constant expected worst-case search time using secondary hash tables. 7.3 Universal Hashing Now I’ll describe a method to generate random hash functions that satisfy the simple uniform hashing assumption. We say that a set H of hash function is universal if it satisfies the following property: For any items x = y , if a hash function h is chosen uniformly at random from the set H, then Pr[h( x ) = h( y )] = 1/m. Note that this probability holds for any items x and y ; the randomness is entirely in choosing a hash function from the set H. To simplify the following discussion, I’ll assume that the universe U contains exactly m2 items, each represented as a pair ( x , x ) of integers between 0 and m − 1. (Think of the items as two-digit numbers in base m.) I will also assume that m is a prime number. For any integers 0 ≤ a, b ≤ m − 1, define the function ha, b : U → {0, 1, . . . , m − 1} as follows: ha, b ( x , x ) = (a x + b x ) mod m. Then the set H = ha, b 0 ≤ a , b ≤ m − 1 of all such functions is universal. To prove this, we need to show that for any pair of distinct items ( x , x ) = ( y, y ), exactly m of the m2 functions in H cause a collision. 2 No, this is not the name of Hannibal’s brother. It’s Latin for “Reader beware!” 3 Algorithms Lecture 7: Hash Tables Choose two items ( x , x ) = ( y, y ), and assume without loss of generality3 that x = y . A function ha, b ∈ H causes a collision between ( x , x ) and ( y, y ) if and only if ha, b ( x , x ) = ha, b ( y , y ) (a x + b x ) mod m = (a y + b y ) mod m ax + b x ≡ a y + b y (mod m) a( x − y ) ≡ b( y − x ) (mod m) a≡ b( y − x ) x−y (mod m). In the last step, we are using the fact that m is prime and x − y = 0, which implies that x − y has a unique multiplicative inverse modulo m. (For example, the multiplicative inverse of 12 modulo 17 is 10, since 12 · 10 = 120 ≡ 1 (mod 17).) For each possible value of b, the last identity defines a unique value of a such that ha, b causes a collision. Since there are m possible values for b, there are exactly m hash functions ha, b that cause a collision, which is exactly what we needed to prove. Thus, if we want to achieve the constant expected time bounds described earlier, we should choose a random element of H as our hash function, by generating two numbers a and b uniformly at random between 0 and m − 1. This is precisely the same as choosing a element of U uniformly at random. One perhaps undesirable ‘feature’ of this construction is that we have a small chance of choosing the trivial hash function h0,0 , which maps everything to 0. So in practice, if we happen to pick a = b = 0, we should reject that choice and pick new random numbers. By taking h0,0 out of consideration, we reduce the probability of a collision from 1/m to (m − 1)/(m2 − 1) = 1/(m + 1). In other words, the set H \ {h0,0 } is slightly better than universal. This construction can be easily generalized to larger universes. Suppose u = m r for some constant r , so that each element x ∈ U can be represented by a vector ( x 0 , x 1 , . . . , x r −1 ) of integers between 0 and m − 1. (Think of x as an r -digit number written in base m.) Then for each vector a = (a0 , a1 , . . . , a r −1 ), define the corresponding hash function ha as follows: ha ( x ) = (a0 x 0 + a1 x 1 + · · · + a r −1 x r −1 ) mod m. Then the set of all m r such functions is universal. 7.4 High Probability Bounds: Balls and Bins Although any particular search in a chained hash tables requires only constant expected time, but what about the worst search time? Under a stronger version4 of the uniform hashing assumption, this is equivalent to the following more abstract problem. Suppose we toss n balls independently and uniformly at random into one of n bins. Can we say anything about the number of balls in the fullest bin? Lemma 1. If n balls are thrown independently and uniformly into n bins, then with high probability, the fullest bin contains O(log n/ log log n) balls. 3 ‘Without loss of generality’ is a phrase that appears (perhaps too) often in combinatorial proofs. What it means is that we are considering one of many possible cases, but once we see the proof for one case, the proofs for all the other cases are obvious thanks to some inherent symmetry. For this proof, we are not explicitly considering what happens when x = y and x =y. 4 The simple uniform hashing assumption requires only pairwise independence, but the following analysis requires full independence. 4 Algorithms Lecture 7: Hash Tables ˆ Proof: Let X j denote the number of balls in bin j , and let X = max j X j be the maximum number of balls in any bin. Clearly, E[X j ] = 1 for all j . n Now consider the probability that bin j contains exactly k balls. There are k choices for those k balls; each chosen ball has probability 1/n of landing in bin j ; and each of the remaining balls has probability 1 − 1/n of missing bin j . Thus, n n ≤ n k 1− k 1 k! 1 < k 1 k Pr[X j = k] = 1− n n−k 1 n n−k 1 n k! This bound shrinks super-exponentially as k increases, so we can very crudely bound the probability that bin 1 contains at least k balls as follows: Pr[X j ≥ k] < n k! To prove the high-probability bound, we need to choose a value for k such that n/k! ≈ 1/nc for some constant c . Taking logs of both sides of the desired approximation and applying Stirling’s approximation, we find ln k! ≈ k ln k − k ≈ (c + 1) ln n ⇐⇒ k ≈ ≈ (c + 1) ln n ln k − 1 (c + 1) ln n ln (c +1) ln n ln k−1 −1 (c + 1) ln n = ln ln n + ln(c + 1) − ln(ln k − 1) − 1 (c + 1) ln n . ≈ ln ln n We have shown (modulo some algebraic hand-waving that is easy but tedious to clean up) that Pr X j ≥ (c + 1) ln n ln ln n < 1 nc . This probability bound holds for every bin j . Thus, by the union bound, we conclude that Pr max X j > j (c + 1) ln n ln ln n = Pr X j > (c + 1) ln n ln ln n n Pr X j > ≤ j =1 < 1 n c −1 5 . for all j (c + 1) ln n ln ln n Algorithms Lecture 7: Hash Tables A similar analysis shows that if we throw n balls randomly into n bins, then with high probability, at least one bin contains Ω(log n/ log log n) balls. However, if we make the hash table large enough, we can expect every ball to land in a different bin. Suppose there are m bins. Let Ci j be the indicator variable that equals 1 if and only if i = j and ball i and ball j land in the same bin, and let C = i < j Ci j be the total number of pairwise collisions. Since n the balls are thrown uniformly at random, the probability of a collision is exactly 1/m, so E[C ] = 2 /m. In particular, if m = n2 , the expected number of collisions is less than 1/2. To get a high probability bound, let X j denote the number of balls in bin j , as in the previous proof. We can easily bound the probability that bin j is empty, by taking the two most significant terms in a binomial expansion: Pr[X j = 0] = 1− 1 n n m −1 n = i =1 i i = 1− m n m +Θ n2 > 1− m2 n m We can similarly bound the probability that bin j contains exactly one ball: Pr[X j = 1] = n · 1 m 1− 1 m n−1 = n m 1− n−1 m +Θ n2 m2 > n m − n( n − 1) m2 ˆ It follows immediately that Pr[X j > 1] < n(n − 1)/m2 . The union bound now implies that Pr[X > 1] < 2+ n(n − 1)/m. If we set m = n for any constant > 0, then the probability that no bin contains more than one ball is at least 1 − 1/n . Lemma 2. For any > 0, if n balls are thrown independently and uniformly into n2+ bins, then with high probability, no bin contains more than one ball. 7.5 Perfect Hashing So far we are faced with two alternatives. If we use a small hash table, to keep the space usage down, the resulting worst-case expected search time is Θ(log n/ log log n) with high probability, which is not much better than a binary search tree. On the other hand, we can get constant worst-case search time, at least in expectation, by using a table of quadratic size, but that seems unduly wasteful. Fortunately, there is a fairly simple way to combine these two ideas to get a data structure of linear expected size, whose expected worst-case search time is constant. At the top level, we use a hash table of size n, but instead of linked lists, we use secondary hash tables to resolve collisions. Specifically, the j th secondary hash table has size n2 , where n j is the number of items whose primary hash value is j . j The expected worst-case search time in any secondary hash table is O(1), by our earlier analysis. Although this data structure needs significantly more memory for each secondary structure, the overall increase in space is insignificant, at least in expectation. Lemma 3. Under the simple uniform hashing assumption, E[n2 ] < 2. j Proof: Let X i j be the indicator variable that equals 1 if item i hashes to slot j in the primary hash table. 6 Algorithms Lecture 7: Hash Tables n E[n2 ] = E j n n n Xkj = E Xi j i =1 n Xi jXk j i =1 k =1 k =1 n = E[X i j X k j ] i =1 k =1 n = i =1 [linearity of expectation] n E[X i2j ] + 2 n E[X i j X k j ] i =1 k = i +1 Because X i j is an indicator variable, we have X i2j = X i j , which implies that E[X i2j ] = E[X i j ] = 1/n by the uniform hashing assumption. The uniform hashing assumption also implies that X i j and X k j are independent whenever i = k, so E[X i j X k j ] = E[X i j ] E[X k j ] = 1/n2 . Thus, n E[n2 ] = j i =1 1 n n n +2 i = 1 k = i +1 1 n2 =1+2 1 =2− . 2 n2 n n 1 This lemma implies that the expected size of our two-level hash table is O(n). By our earlier analysis, the expected worst-case search time is O(1). 7.6 Open Addressing Another method used to resolve collisions in hash tables is called open addressing. Here, rather than building secondary data structures, we resolve collisions by looking elsewhere in the table. Specifically, we have a sequence of hash functions 〈h0 , h1 , h2 , . . . , hm−1 〉, such that for any item x , the probe sequence h0 ( x ), h1 ( x ), . . . , hm−1 ( x ) is a permutation of 〈0, 1, 2, . . . , m − 1〉. In other words, different hash functions in the sequence always map x to different locations in the hash table. We search for x using the following algorithm, which returns the array index i if T [i ] = x , ‘absent’ if x is not in the table but there is an empty slot, and ‘full’ if x is not in the table and there no no empty slots. OPENADDRESSSEARCH( x ): for i ← 0 to m − 1 if T [hi ( x )] = x return hi ( x ) else if T [hi ( x )] = ∅ return ‘absent’ return ‘full’ The algorithm for inserting a new item into the table is similar; only the second-to-last line is changed to T [hi ( x )] ← x . Notice that for an open-addressed hash table, the load factor is never bigger than 1. Just as with chaining, we’d like to pretend that the sequence of hash values is random. For purposes of analysis, there is a stronger uniform hashing assumption that gives us constant expected search and insertion time. Strong uniform hashing assumption: For any item x , the probe sequence 〈h0 ( x ), h1 ( x ), . . . , hm−1 ( x )〉 is equally likely to be any permutation of the set {0, 1, 2, . . . , m − 1}. 7 Algorithms Lecture 7: Hash Tables Let’s compute the expected time for an unsuccessful search using this stronger assumption. Suppose there are currently n elements in the hash table. Our strong uniform hashing assumption has two important consequences: • The initial hash value h0 ( x ) is equally likely to be any integer in the set {0, 1, 2, . . . , m − 1}. • If we ignore the first probe, the remaining probe sequence 〈h1 ( x ), h2 ( x ), . . . , hm−1 ( x )〉 is equally likely to be any permutation of the smaller set {0, 1, 2, . . . , m − 1} \ {h0 ( x )}. The first sentence implies that the probability that T [h0 ( x )] is occupied is exactly n/m. The second sentence implies that if T [h0 ( x )] is occupied, our search algorithm recursively searches the rest of the hash table! Since the algorithm will never again probe T [h0 ( x )], for purposes of analysis, we might as well pretend that slot in the table no longer exists. Thus, we get the following recurrence for the expected number of probes, as a function of m and n: E[ T (m, n)] = 1 + n m E[ T (m − 1, n − 1)]. The trivial base case is T (m, 0) = 1; if there’s nothing in the hash table, the first probe always hits an empty slot. We can now easily prove by induction that EMPHE[ T (m, n)] ≤ m/(m − n): E[ T (m, n)] = 1 + n E[ T (m − 1, n − 1)] m n m−1 ≤1+ · [induction hypothesis] m m−n n m <1+ · [m − 1 < m] m m−n m [algebra] = m−n Rewriting this in terms of the load factor α = n/m, we get E[ T (m , n )] ≤ 1/(1 − α). In other words, the expected time for an unsuccessful search is O(1), unless the hash table is almost completely full. In practice, however, we can’t generate truly random probe sequences, so we use one of the following heuristics: • Linear probing: We use a single hash function h( x ), and define hi ( x ) = (h( x ) + i ) mod m. This is nice and simple, but collisions tend to make items in the table clump together badly, so this is not really a good idea. • Quadratic probing: We use a single hash function h( x ), and define hi ( x ) = (h( x ) + i 2 ) mod m. Unfortunately, for certain values of m, the sequence of hash values 〈hi ( x )〉 does not hit every possible slot in the table; we can avoid this problem by making m a prime number. (That’s often a good idea anyway.) Although quadratic probing does not suffer from the same clumping problems as linear probing, it does have a weaker clustering problem: If two items have the same initial hash value, their entire probe sequences will be the same. • Double hashing: We use two hash functions h( x ) and h ( x ), and define hi as follows: hi ( x ) = (h( x ) + i · h ( x )) mod m To guarantee that this can hit every slot in the table, the stride function h ( x ) and the table size m must be relatively prime. We can guarantee this by making m prime, but a simpler solution is to 8 Algorithms Lecture 7: Hash Tables make m a power of 2 and choose a stride function that is always odd. Double hashing avoids the clustering problems of linear and quadratic probing. In fact, the actual performance of double hashing is almost the same as predicted by the uniform hashing assumption, at least when m is large and the component hash functions h and h are sufficiently random. This is the method of choice!5 7.7 Deleting from an Open-Addressed Hash Table This section assumes familiarity with amortized analysis. Deleting an item x from an open-addressed hash table is a bit more difficult than in a chained hash table. We can’t simply clear out the slot in the table, because we may need to know that T [h( x )] is occupied in order to find some other item! Instead, we should delete more or less the way we did with scapegoat trees. When we delete an item, we mark the slot that used to contain it as a wasted slot. A sufficiently long sequence of insertions and deletions could eventually fill the table with marks, leaving little room for any real data and causing searches to take linear time. However, we can still get good amortized performance by using two rebuilding rules. First, if the number of items in the hash table exceeds m/4, double the size of the table (m ← 2m) and rehash everything. Second, if the number of wasted slots exceeds m/2, clear all the marks and rehash everything in the table. Rehashing everything takes m steps to create the new hash table and O(n) expected steps to hash each of the n items. By charging a $4 tax for each insertion and a $2 tax for each deletion, we expect to have enough money to pay for any rebuilding. In conclusion, the expected amortized cost of any insertion or deletion is O(1), under the uniform hashing assumption. Notice that we’re doing two very different kinds of averaging here. On the one hand, we are averaging the possible costs of each individual search over all possible probe sequences (‘expected’). On the other hand, we are also averaging the costs of the entire sequence of operations to ‘smooth out’ the cost of rebuilding (‘amortized’). Both randomization and amortization are necessary to get this constant time bound. Exercises 1. Suppose we are using an open-addressed hash table of size m to store n items, where n ≤ m/2. Assume that the strong uniform hashing assumption holds. For any i , let X i denote the number of probes required for the i th insertion into the table, and let X = maxi X i denote the length of the longest probe sequence. (a) Prove that Pr[X i > k] ≤ 1/2k for all i and k. (b) Prove that Pr[X i > 2 lg n] ≤ 1/n2 for all i . (c) Prove that Pr[X > 2 lg n] ≤ 1/n. (d) Prove that E[X ] = O(lg n). 5 ...unless your hash tables are really huge, in which case linear probing has far better cache behavior, especially when the load factor is small. 9 Algorithms Lecture 7: Hash Tables 2. Your boss wants you to find a perfect hash function for mapping a known set of n items into a table of size m. A hash function is perfect if there are no collisions; each of the n items is mapped to a different slot in the hash table. Of course, this requires that m ≥ n. (This is a different definition of perfect hashing than the one considered in the lecture notes.) After cursing your algorithms instructor for not teaching you about perfect hashing, you decide to try something simple: repeatedly pick random hash functions until you find one that happens to be perfect. A random hash function h satisfies two properties: • Pr[h( x ) = h( y )] = 1/m for any pair of items x = y . • Pr[h( x ) = i ] = 1/m for any item x and any integer 1 ≤ i ≤ m. (a) Suppose you pick a random hash function h. What is the exact expected number of collisions, as a function of n (the number of items) and m (the size of the table)? Don’t worry about how to resolve collisions; just count them. (b) What is the exact probability that a random hash function is perfect? (c) What is the exact expected number of different random hash functions you have to test before you find a perfect hash function? (d) What is the exact probability that none of the first N random hash functions you try is perfect? (e) How many random hash functions do you have to test to find a perfect hash function with high probability? 3. Recall that Fk denotes the kth Fibonacci number: F0 = 0, F1 = 1, and Fk = Fk−1 + Fk−2 for all k ≥ 2. Suppose we are building a hash table of size m = Fk using the hash function h( x ) = ( Fk−1 · x ) mod Fk Prove that if the consecutive integers 0, 1, 2, . . . , Fk − 1 are inserted in order into an initially empty table, each integer will be hashed into the largest contiguous empty interval in the table. In particular, show that there are no collisions. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 10 Algorithms Non-Lecture F: Randomized Minimum Cut Jaques: But, for the seventh cause; how did you find the quarrel on the seventh cause? Touchstone: Upon a lie seven times removed:–bear your body more seeming, Audrey:–as thus, sir. I did dislike the cut of a certain courtier’s beard: he sent me word, if I said his beard was not cut well, he was in the mind it was: this is called the Retort Courteous. If I sent him word again ‘it was not well cut,’ he would send me word, he cut it to please himself: this is called the Quip Modest. If again ‘it was not well cut,’ he disabled my judgment: this is called the Reply Churlish. If again ‘it was not well cut,’ he would answer, I spake not true: this is called the Reproof Valiant. If again ‘it was not well cut,’ he would say I lied: this is called the Counter-cheque Quarrelsome: and so to the Lie Circumstantial and the Lie Direct. Jaques: And how oft did you say his beard was not well cut? Touchstone: I durst go no further than the Lie Circumstantial, nor he durst not give me the Lie Direct; and so we measured swords and parted. — William Shakespeare, As You Like It, Act V, Scene 4 (1600) F F.1 Randomized Minimum Cut Setting Up the Problem This lecture considers a problem that arises in robust network design. Suppose we have a connected multigraph1 G representing a communications network like the UIUC telephone system, the internet, or Al-Qaeda. In order to disrupt the network, an enemy agent plans to remove some of the edges in this multigraph (by cutting wires, placing police at strategic drop-off points, or paying street urchins to ‘lose’ messages) to separate it into multiple components. Since his country is currently having an economic crisis, the agent wants to remove as few edges as possible to accomplish this task. More formally, a cut partitions the nodes of G into two nonempty subsets. The size of the cut is the number of crossing edges, which have one endpoint in each subset. Finally, a minimum cut in G is a cut with the smallest number of crossing edges. The same graph may have several minimum cuts. a b d c e g f h A multigraph whose minimum cut has three edges. This problem has a long history. The classical deterministic algorithms for this problem rely on network flow techniques, which are discussed in another lecture. The fastest such algorithms (that we will discuss) run in O(n3 ) time and are quite complex and difficult to understand (unless you’re already familiar with network flows). Here I’ll describe a relatively simple randomized algorithm discovered by David Karger when he was a Ph.D. student.2 Karger’s algorithm uses a primitive operation called collapsing an edge. Suppose u and v are vertices that are connected by an edge in some multigraph G . To collapse the edge {u, v }, we create a new node called uv , replace any edge of the form {u, w } or { v, w } with a new edge {uv, w }, and then delete the original vertices u and v . Equivalently, collapsing the edge shrinks the edge down to nothing, pulling the 1 A multigraph allows multiple edges between the same pair of nodes. Everything in this lecture could be rephrased in terms of simple graphs where every edge has a non-negative weight, but this would make the algorithms and analysis slightly more complicated. 2 David R. Karger*. Random sampling in cut, flow, and network design problems. Proc. 25th STOC, 648–657, 1994. 1 Algorithms Non-Lecture F: Randomized Minimum Cut two endpoints together. The new collapsed graph is denoted G /{u, v }. We don’t allow self-loops in our multigraphs; if there are multiple edges between u and v , collapsing any one of them deletes them all. a a c d a c b be d cd b e e A graph G and two collapsed graphs G /{ b, e} and G /{c , d }. I won’t describe how to actually implement collapsing an edge—it will be a homework exercise later in the course—but it can be done in O(n) time. Let’s just accept collapsing as a black box subroutine for now. The correctness of our algorithms will eventually boil down the following simple observation: For any cut in G /{u, v }, there is cut in G with exactly the same number of crossing edges. In fact, in some sense, the ‘same’ edges form the cut in both graphs. The converse is not necessarily true, however. For example, in the picture above, the original graph G has a cut of size 1, but the collapsed graph G /{c , d } does not. This simple observation has two immediate but important consequences. First, collapsing an edge cannot decrease the minimum cut size. More importantly, collapsing an edge increases the minimum cut size if and only if that edge is part of every minimum cut. F.2 Blindly Guessing Let’s start with an algorithm that tries to guess the minimum cut by randomly collapsing edges until the graph has only two vertices left. GUESSMINCUT(G ): for i ← n downto 2 pick a random edge e in G G ← G /e return the only cut in G Since each collapse takes O(n) time, this algorithm runs in O(n2 ) time. Our earlier observations imply that as long as we never collapse an edge that lies in every minimum cut, our algorithm will actually guess correctly. But how likely is that? Suppose G has only one minimum cut—if it actually has more than one, just pick your favorite—and this cut has size k. Every vertex of G must lie on at least k edges; otherwise, we could separate that vertex from the rest of the graph with an even smaller cut. Thus, the number of incident vertex-edge pairs is at least kn. Since every edge is incident to exactly two vertices, G must have at least kn/2 edges. That implies that if we pick an edge in G uniformly at random, the probability of picking an edge in the minimum cut is at most 2/n. In other words, the probability that we don’t screw up on the very first step is at least 1 − 2/n. Once we’ve collapsed the first random edge, the rest of the algorithm proceeds recursively (with independent random choices) on the remaining (n − 1)-node graph. So the overall probability P (n) that GUESSMINCUT returns the true minimum cut is given by the following recurrence: P ( n) ≥ n−2 n 2 · P (n − 1). Algorithms Non-Lecture F: Randomized Minimum Cut The base case for this recurrence is P (2) = 1. We can immediately expand this recurrence into a product, most of whose factors cancel out immediately. n n P ( n) ≥ i =3 i−2 i = n −2 ( i − 2) i =3 i i =1 n = n i i i =3 F.3 = 2 n(n − 1) i =3 Blindly Guessing Over and Over That’s not very good. Fortunately, there’s a simple method for increasing our chances of finding the minimum cut: run the guessing algorithm many times and return the smallest guess. Randomized algorithms folks like to call this idea amplification. KARGERMINCUT(G ): mink ← ∞ for i ← 1 to N X ← GUESSMINCUT(G ) if |X | < mink mink ← |X | minX ← X return minX Both the running time and the probability of success will depend on the number of iterations N , which we haven’t specified yet. First let’s figure out the probability that KARGERMINCUT returns the actual minimum cut. The only way for the algorithm to return the wrong answer is if GUESSMINCUT fails N times in a row. Since each guess is independent, our probability of success is at least 1− 1− N 2 . n( n − 1) We can simplify this using one of the most important (and easiest) inequalities known to mankind: 1 − x ≤ e− x So our success probability is at least 1 − e−2N /n(n−1) . By making N larger, we can make this probability arbitrarily close to 1, but never equal to 1. In particular, n if we set N = c 2 ln n for some constant c , then KARGERMINCUT is correct with probability at least 1 − e−c ln n = 1 − 1 nc . When the failure probability is a polynomial fraction, we say that the algorithm is correct with high probability. Thus, KARGERMINCUT computes the minimum cut of any n-node graph in O (n 4 log n ) time. If we make the number of iterations even larger, say N = n2 (n − 1)/2, the success probability becomes 1 − e−n . When the failure probability is exponentially small like this, we say that the algorithm is correct with very high probability. In practice, very high probability is usually overkill; high probability is enough. (Remember, there is a small but non-zero probability that your computer will transform itself into a kitten before your program is finished.) 3 Algorithms F.4 Non-Lecture F: Randomized Minimum Cut Not-So-Blindly Guessing The O(n4 log n) running time is actually comparable to some of the simpler flow-based algorithms, but it’s nothing to get excited about. But we can improve our guessing algorithm, and thus decrease the number of iterations in the outer loop, by observing that as the graph shrinks, the probability of collapsing an edge in the minimum cut increases. At first the probability is quite small, only 2/n, but near the end of execution, when the graph has only three vertices, we have a 2/3 chance of screwing up! A simple technique for working around this increasing probability of error was developed by David Karger and Cliff Stein.3 Their idea is to group the first several random collapses a ‘safe’ phase, so that the cumulative probability of screwing up is small—less than 1/2, say—and a ‘dangerous’ phase, which is much more likely to screw up. The safe phase shrinks the graph from n nodes to n/ 2 + 1 nodes, using a sequence of n − n/ 2 − 1 random collapses. Following our earlier analysis, the probability that none of these safe collapses touches the minimum cut is at least n i =n/ 2+2 i−2 i = (n/ 2)(n/ 2 + 1) n( n − 1) = n+ 2 2(n − 1) > 1 2 . Now, to get around the danger of the dangerous phase, we use amplification. However, instead of running through the dangerous phase once, we run it twice and keep the best of the two answers. Naturally, we treat the dangerous phase recursively, so we actually obtain a binary recursion tree, which expands as we get closer to the base case, instead of a single path. More formally, the algorithm looks like this: CONTRACT(G , m): for i ← n downto m pick a random edge e in G G ← G /e return G BETTERGUESS(G ): if G has more than 8 vertices X 1 ← BETTERGUESS(CONTRACT(G , n/ 2 + 1)) X 2 ← BETTERGUESS(CONTRACT(G , n/ 2 + 1)) return min{X 1 , X 2 } else use brute force This might look like we’re just doing to same thing twice, but remember that CONTRACT (and thus BETTERGUESS) is randomized. Each call to CONTRACT contracts an independent random set of edges; X 1 and X 2 are almost always different cuts. BETTERGUESS correctly returns the minimum cut unless both recursive calls return the wrong result. X 1 is the minimum cut of G if and only if (1) none of the min cut edges are CONTRACTed and (2) the recursive BETTERGUESS returns the minimum cut of the CONTRACTed graph. If P (n) denotes the probability that BETTERGUESS returns a minimum cut of an n-node graph, then X 1 is the minimum cut with probability at least 1/2 · P (n/ 2), and X 2 is the minimum cut with the same probability. Since these two events are independent, we have the following recurrence, with base case P (n) = 1 for all n ≤ 6. 2 1 n P ( n) ≥ 1 − 1 − P +1 2 2 Using a series of transformations, Karger and Stein prove that P (n) = Ω(1/ log n). I’ve included the proof at the end of this note. 3 ˜ David R. Karger∗ and Cliff Stein. An O(n2 ) algorithm for minimum cuts. Proc. 25th STOC, 757–765, 1993. 4 Algorithms Non-Lecture F: Randomized Minimum Cut For the running time, we get a simple recurrence that is easily solved using recursion trees or the Master theorem (after a domain transformation to remove the +1 from the recurrence). T ( n) = O ( n2 ) + 2 T n 2 +1 = O(n2 log n) So all this splitting and recursing has slowed down the guessing algorithm slightly, but the probability of failure is exponentially smaller! Let’s express the lower bound P (n) = Ω(1/ log n) explicitly as P (n) ≥ α/ ln n for some constant α. (Karger and Klein’s proof implies α > 2). If we call BETTERGUESS N = c ln2 n times, for some new constant c , the overall probability of success is at least 1− 1− α ln n c ln2 n ≥ 1 − e−(c /α) ln n = 1 − 1 nc /α . By setting c sufficiently large, we can bound the probability of failure by an arbitrarily small polynomial function of n. In other words, we now have an algorithm that computes the minimum cut with high probability in only O (n 2 log3 n ) time! F.5 Solving the Karger/Stein recurrence Recall the following recurrence for the probability that BETTERGUESS successfully finds a minimum cut of an n-node graph: 2 n 1 P ( n) ≥ 1 − 1 − P +1 2 2 Karger and Stein solve this rather ugly recurrence through a series of functional transformations. Let p(k) denote the probability of success at the kth level of recursion, counting upward from the base case. This function satisfies the recurrence p(k) ≥ 1 − 1 − p ( k − 1) 2 2 with base case p(0) = 1. Let ¯ (k) be the function that satisfies this recurrence with equality; clearly, p p p(k) ≥ ¯ (k). Now consider the function z (k) = 4/ ¯ (k) − 1. Substituting ¯ (k) = 4/(z (k) + 1) into our p p old recurrence implies (after a bit of algebra) that z ( k ) = z ( k − 1) + 2 + 1 z ( k − 1) . Since clearly z (k) > 1 for all k, we have a conservative upper bound z (k) < z (k − 1) + 2, which implies inductively that z (k) ≤ 2k + 3, since z (0) = 3. It follows that p(k) ≥ ¯ (k) > p 1 2k + 4 = Ω(1/k). To compute the number of levels of recursion that BETTERGUESS executes for an n-node graph, we solve the secondary recurrence n k ( n) = 1 + k +1 2 with base cases k(n) = 0 for all n ≤ 8. After a domain transformation to remove the +1 from the right side, the recursion tree method (or the Master theorem) implies that k(n) = Θ(log n). We conclude that P (n ) = p (k (n )) = Ω(1/ log n ), as promised. 5 Algorithms Non-Lecture F: Randomized Minimum Cut Exercises 1. Suppose you had an algorithm to compute the minimum spanning tree of a graph in O(m) time, where m is the number of edges in the input graph.4 Use this algorithm as a subroutine to improve the running time of GUESSMINCUT from O(n2 ) to O(m). 2. Suppose you are given a graph G with weighted edges, and your goal is to find a cut whose total weight (not just number of edges) is smallest. (a) Describe an algorithm to select a random edge of G , where the probability of choosing edge e is proportional to the weight of e. (b) Prove that if you use the algorithm from part (a), instead of choosing edges uniformly at random, the probability that GUESSMINCUT returns a minimum-weight cut is still Ω(1/n2 ). (c) What is the running time of your modified GUESSMINCUT algorithm? 3. Prove that GUESSMINCUT returns the second smallest cut in its input graph with probability Ω(1/n3 ). (The second smallest cut could be significantly larger than the minimum cut.) 4 In fact, there is a randomized MST algorithm (due to Philip Klein, David Karger, and Robert Tarjan) whose expected running time is O(m). © Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 6 Algorithms Lecture 8: Amortized Analysis The goode workes that men don whil they ben in good lif al amortised by synne folwyng. — Geoffrey Chaucer, “The Persones [Parson’s] Tale” (c.1400) I will gladly pay you Tuesday for a hamburger today. — J. Wellington Wimpy, “Thimble Theatre” (1931) I want my two dollars! — Johnny Gasparini [Demian Slade], “Better Off Dead” (1985) 8 8.1 Amortized Analysis Incrementing a Binary Counter It is a straightforward exercise in induction, which often appears on Homework 0, to prove that any non-negative integer n can be represented as the sum of distinct powers of 2. Although some students correctly use induction on the number of bits—pulling off either the least significant bit or the most significant bit in the binary representation and letting the Recursion Fairy convert the remainder—the most commonly submitted proof uses induction on the value of the integer, as follows: Proof: The base case n = 0 is trivial. For any n > 0, the inductive hypothesis implies that there is set of distinct powers of 2 whose sum is n − 1. If we add 20 to this set, we obtain a multiset of powers of two whose sum is n, which might contain two copies of 20 . Then as long as there are two copies of any 2i in the multiset, we remove them both and insert 2i +1 in their place. The sum of the elements of the multiset is unchanged by this replacement, because 2i +1 = 2i + 2i . Each iteration decreases the size of the multiset by 1, so the replacement process must eventually terminate. When it does terminate, we have a set of distinct powers of 2 whose sum is n. This proof is describing an algorithm to increment a binary counter from n − 1 to n. Here’s a more formal (and shorter!) description of the algorithm to add 1 to a binary counter. The input B is an (infinite) array of bits, where B [i ] = 1 if and only if 2i appears in the sum. INCREMENT(B [0 .. ∞]): i←0 while B [i ] = 1 B [i ] ← 0 i ← i+1 B [i ] ← 1 We’ve already argued that INCREMENT must terminate, but how quickly? Obviously, the running time depends on the array of bits passed as input. If the first k bits are all 1s, then INCREMENT takes Θ(k) time. The binary representation of any positive integer n is exactly lg n + 1 bits long. Thus, if B represents an integer between 0 and n, INCREMENT takes Θ(log n) time in the worst case. 8.2 Counting from 0 to n Now suppose we want to use INCREMENT to count from 0 to n. If we only use the worst-case running time for each INCREMENT, we get an upper bound of O(n log n) on the total running time. Although this bound is correct, we can do better. There are several general methods for proving that the total running time is actually only O(n). Many of these methods are logically equivalent, but different formulations are more natural for different problems. 1 Algorithms 8.2.1 Lecture 8: Amortized Analysis The Summation (Aggregate) Method The easiest way to get a tighter bound is to observe that we don’t need to flip Θ(log n) bits every time INCREMENT is called. The least significant bit B [0] does flip every time, but B [1] only flips every other time, B [2] flips every 4th time, and in general, B [i ] flips every 2i th time. If we start with an array full of 0’s, a sequence of n INCREMENTs flips each bit B [i ] exactly n/2i times. Thus, the total number of bit-flips for the entire sequence is lg n i =0 n 2i ∞ < i =0 n 2i = 2 n. Thus, on average, each call to INCREMENT flips only two bits, and so runs in constant time. This ‘on average’ is quite different from the averaging we consider with randomized algorithms. There is no probability involved; we are averaging over a sequence of operations, not the possible running times of a single operation. This averaging idea is called amortization—the amortized time for each INCREMENT is O(1). Amortization is a sleazy clever trick used by accountants to average large one-time costs over long periods of time; the most common example is calculating uniform payments for a loan, even though the borrower is paying interest on less and less capital over time. For this reason, ‘amortized cost’ is a common synonym for amortized running time. Most textbooks call this technique for bounding amortized costs the aggregate method, or aggregate analysis, but this is just a fancy name for adding up the costs of the individual operations and dividing by the number of operations. The Summation Method. Let T (n) be the worst-case running time for a sequence of n operations. The amortized time for each operation is T (n)/n. 8.2.2 The Taxation (Accounting) Method A second method we can use to derive amortized bounds is called either the accounting method or the taxation method. Suppose it costs us a dollar to toggle a bit, so we can measure the running time of our algorithm in dollars. Time is money! Instead of paying for each bit flip when it happens, the Increment Revenue Service charges a twodollar increment tax whenever we want to set a bit from zero to one. One of those dollars is spent changing the bit from zero to one; the other is stored away as credit until we need to reset the same bit to zero. The key point here is that we always have enough credit saved up to pay for the next INCREMENT. The amortized cost of an INCREMENT is the total tax it incurs, which is exactly 2 dollars, since each INCREMENT changes just one bit from 0 to 1. It is often useful to distribute the tax income to specific pieces of the data structure. For example, for each INCREMENT, we could store one of the two dollars on the single bit that is set for 0 to 1, so that that bit can pay to reset itself back to zero later on. Taxation Method 1. Certain steps in the algorithm charge you taxes, so that the total cost incurred by the algorithm is never more than the total tax you pay. The amortized cost of an operation is the overall tax charged to you during that operation. A different way to schedule the taxes is for every bit to charge us a tax at every operation, regardless of whether the bit changes of not. Specifically, each bit B [i ] charges a tax of 1/2i dollars for each INCREMENT. The total tax we are charged during each INCREMENT is i ≥0 2−i = 2 dollars. Every time a bit B [i ] actually needs to be flipped, it has collected exactly $1, which is just enough for us to pay for the flip. 2 Algorithms Lecture 8: Amortized Analysis Taxation Method 2. Certain portions of the data structure charge you taxes at each operation, so that the total cost of maintaining the data structure is never more than the total taxes you pay. The amortized cost of an operation is the overall tax you pay during that operation. In both of the taxation methods, our task as algorithm analysts is to come up with an appropriate ‘tax schedule’. Different ‘schedules’ can result in different amortized time bounds. The tightest bounds are obtained from tax schedules that just barely stay in the black. 8.2.3 The Charging Method Another common method of amortized analysis involves charging the cost of some steps to some other, earlier steps. The method is similar to taxation, except that we focus on where each unit of tax is (or will be) spent, rather than where is it collected. By charging the cost of some operations to earlier operations, we are overestimating the total cost of any sequence of operations, since we pay for some charges from future operations that may never actually occur. For example, in our binary counter, suppose we charge the cost of clearing a bit (changing its value from 1 to 0) to the previous operation that sets that bit (changing its value from 0 to 1). If we flip k bits during an INCREMENT, we charge k − 1 of those bit-flips to earlier bit-flips. Conversely, the single operation that sets a bit receives at most one unit of charge from the next time that bit is cleared. So instead of paying for k bit-flips, we pay for at most two: one for actually setting a bit, plus at most one charge from the future for clearing that same bit. Thus, the total amortized cost of the INCREMENT is at most two bit-flips. The Charging Method. Charge the cost of some steps of the algorithm to earlier steps, or to steps in some earlier operation. The amortized cost of the algorithm is its actual running time, minus its total charges to past operations, plus its total charge from future operations. 8.2.4 The Potential Method The most powerful method (and the hardest to use) builds on a physics metaphor of ‘potential energy’. Instead of associating costs or taxes with particular operations or pieces of the data structure, we represent prepaid work as potential that can be spent on later operations. The potential is a function of the entire data structure. Let Di denote our data structure after i operations, and let Φi denote its potential. Let ci denote the actual cost of the i th operation (which changes Di −1 into Di ). Then the amortized cost of the i th operation, denoted ai , is defined to be the actual cost plus the increase in potential: a i = c i + Φ i − Φ i −1 So the total amortized cost of n operations is the actual total cost plus the total increase in potential: n n n ai = i =1 c i + Φ i − Φ i −1 = i =1 c i + Φ n − Φ0 . i =1 A potential function is valid if Φi − Φ0 ≥ 0 for all i . If the potential function is valid, then the total actual cost of any sequence of operations is always less than the total amortized cost: n n ci = i =1 n ai − Φn ≤ i =1 ai . i =1 3 Algorithms Lecture 8: Amortized Analysis For our binary counter example, we can define the potential Φi after the i th INCREMENT to be the number of bits with value 1. Initially, all bits are equal to zero, so Φ0 = 0, and clearly Φi > 0 for all i > 0, so this is a valid potential function. We can describe both the actual cost of an INCREMENT and the change in potential in terms of the number of bits set to 1 and reset to 0. ci = #bits changed from 0 to 1 + #bits changed from 1 to 0 Φi − Φi −1 = #bits changed from 0 to 1 − #bits changed from 1 to 0 Thus, the amortized cost of the i th INCREMENT is ai = ci + Φi − Φi −1 = 2 × #bits changed from 0 to 1 Since INCREMENT changes only one bit from 0 to 1, the amortized cost INCREMENT is 2. The Potential Method. Define a potential function for the data structure that is initially equal to zero and is always nonnegative. The amortized cost of an operation is its actual cost plus the change in potential. For this particular example, the potential is precisely the total unspent taxes paid using the taxation method, so not too surprisingly, we obtain precisely the same amortized cost. In general, however, there may be no way of interpreting the change in potential as ‘taxes’. Taxation and charging are useful when there is a convenient way to distribute costs to specific steps in the algorithm or components of the data structure; potential arguments allow us to argue more globally when a local distribution is difficult or impossible. Different potential functions can lead to different amortized time bounds. The trick to using the potential method is to come up with the best possible potential function. A good potential function goes up a little during any cheap/fast operation, and goes down a lot during any expensive/slow operation. Unfortunately, there is no general technique for doing this other than playing around with the data structure and trying lots of different possibilities. 8.3 Incrementing and Decrementing Now suppose we wanted a binary counter that we can both increment and decrement efficiently. A standard binary counter won’t work, even in an amortized sense; if we alternate between 2k and 2k − 1, every operation costs Θ(k) time. A nice alternative is represent a number as a pair of bit strings ( P, N ), where for any bit position i , at most one of the bits P [i ] and N [i ] is equal to 1. The actual value of the counter is P − N . Here are algorithms to increment and decrement our double binary counter. INCREMENT( P, N ): i←0 while P [i ] = 1 P [i ] ← 0 i ← i+1 DECREMENT( P, N ): i←0 while N [i ] = 1 N [i ] ← 0 i ← i+1 if N [i ] = 1 N [i ] ← 0 else P [i ] ← 1 if P [i ] = 1 P [i ] ← 0 else N [i ] ← 1 Here’s an example of these algorithms in action. Notice that any number other than zero can be represented in multiple (in fact, infinitely many) ways. 4 Algorithms Lecture 8: Amortized Analysis P = 10001 P = 10000 P = 10000 P = 10000 P = 10011 P = 10010 P = 10001 ++ −− −− ++ ++ ++ N = 01100 −→ N = 01100 −→ N = 01100 −→ N = 01000 −→ N = 01001 −→ N = 01010 −→ N = 01010 P−N =7 P−N =6 P−N =7 P−N =8 P−N =7 P−N =6 P−N =5 Incrementing and decrementing a double-binary counter. Now suppose we start from (0, 0) and apply a sequence of n INCREMENTs and DECREMENTs. In the worst case, operation takes Θ(log n) time, but what is the amortized cost? We can’t use the aggregate method here, since we don’t know what the sequence of operations looks like. What about the taxation method? It’s not hard to prove (by induction, of course) that after either P [i ] or N [i ] is set to 1, there must be at least 2i operations, either INCREMENTs or DECREMENTs, before that bit is reset to 0. So if each bit P [i ] and N [i ] pays a tax of 2−i at each operation, we will always have enough money to pay for the next operation. Thus, the amortized cost of each operation is at most −i = 4. i ≥0 2 · 2 We can get even better bounds using the potential method. Define the potential Φi to be the number of 1-bits in both P and N after i operations. Just as before, we have ci = #bits changed from 0 to 1 + #bits changed from 1 to 0 Φi − Φi −1 = #bits changed from 0 to 1 − #bits changed from 1 to 0 =⇒ ai = 2 × #bits changed from 0 to 1 Since each operation changes at most one bit to 1, the i th operation has amortized cost ai ≤ 2. 8.4 Gray Codes An attractive alternate solution to the increment/decrement problem was independently suggested by several students. Gray codes (named after Frank Gray, who discovered them in the 1950s) are methods for representing numbers as bit strings so that successive numbers differ by only one bit. For example, here is the four-bit binary reflected Gray code for the integers 0 through 15: 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, 1000 The general rule for incrementing a binary reflected Gray code is to invert the bit that would be set from 0 to 1 by a normal binary counter. In terms of bit-flips, this is the perfect solution; each increment of decrement by definition changes only one bit. Unfortunately, the naïve algorithm to find the single bit to flip still requires Θ(log n) time in the worst case. Thus, so the total cost of maintaining a Gray code, using the obvious algorithm, is the same as that of maintaining a normal binary counter. Fortunately, this is only true of the naïve algorithm. The following algorithm, discovered by Gideon Ehrlich1 in 1973, maintains a Gray code counter in constant worst-case time per increment! The algorithm uses a separate ‘focus’ array F [0 .. n] in addition to a Gray-code bit array G [0 .. n − 1]. EHRLICHGRAYINCREMENT(n): j ← F [ 0] F [0] ← 0 if j = n G [ n − 1] ← 1 − G [ n − 1] else G[ j] = 1 − G[ j] F [ j ] ← F [ j + 1] F [ j + 1] ← j + 1 EHRLICHGRAYINIT(n): for i ← 0 to n − 1 G [i ] ← 0 for i ← 0 to n F [i ] ← i 1 Gideon Ehrlich. Loopless algorithms for generating permutations, combinations, and other combinatorial configurations. J. Assoc. Comput. Mach. 20:500–513, 1973. 5 Algorithms Lecture 8: Amortized Analysis The EHRLICHGRAYINCREMENT algorithm obviously runs in O(1) time, even in the worst case. Here’s the algorithm in action with n = 4. The first line is the Gray bit-vector G , and the second line shows the focus vector F , both in reverse order: G : 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, 1000 F : 3210, 3211, 3220, 3212, 3310, 3311, 3230, 3213, 4210, 4211, 4220, 4212, 3410, 3411, 3240, 3214 Voodoo! I won’t explain in detail how Ehrlich’s algorithm works, except to point out the following invariant. Let B [i ] denote the i th bit in the standard binary representation of the current number. If B[ j ] = 0 and B[ j − 1] = 1, then F [ j ] is the smallest integer k > j such that B[k ] = 1; otherwise, F [ j ] = j . Got that? But wait — this algorithm only handles increments; what if we also want to decrement? Sorry, I don’t have a clue. Extra credit, anyone? 8.5 Generalities and Warnings Although computer scientists usually apply amortized analysis to understand the efficiency of maintaining and querying data structures, you should remember that amortization can be applied to any sequence of numbers. Banks have been using amortization to calculate fixed payments for interest-bearing loans for centuries. The IRS allows taxpayers to amortize business expenses or gambling losses across several years for purposes of computing income taxes. Some cell phone contracts let you to apply amortization to calling time, by rolling unused minutes from one month into the next month. It’s also important to keep in mind when you’re doing amortized analysis is that amortized time bounds are not unique. For a data structure that supports multiple operations, different amortization schemes can assign different costs to exactly the same algorithms. For example, consider a generic data structure that can be modified by three algorithms: FOLD, SPINDLE, and MUTILATE. One amortization scheme might imply that FOLD and SPINDLE each run in O(log n) amortized time, while MUTILATE runs in O(n) amortized time. Another scheme might imply that FOLD runs in O( n) amortized time, while SPINDLE and MUTILATE each run in O(1) amortized time. These two results are not necessarily inconsistent! Moreover, there is no general reason to prefer one of these sets of amortized time bounds over the other; our preference may depend on the context in which the data structure is used. Exercises 1. Suppose we are maintaining a data structure under a series of operations. Let f (n) denote the actual running time of the nth operation. For each of the following functions f , determine the resulting amortized cost of a single operation. (For practice, try all of the methods described in this note.) (a) f (n) is the largest power of 2 that divides n. (b) f (n) = n if n is a power of 2, and f (n) = 1 otherwise. (c) f (n) = n2 if n is a power of 2, and f (n) = 1 otherwise. 2. A multistack consists of an infinite series of stacks S0 , S1 , S2 , . . . , where the i th stack Si can hold up to 3i elements. The user always pushes and pops elements from the smallest stack S0 . However, before any element can be pushed onto any full stack Si , we first pop all the elements off Si and push them onto stack Si +1 to make room. (Thus, if Si +1 is already full, we first recursively move 6 Algorithms Lecture 8: Amortized Analysis all its members to Si +2 .) Similarly, before any element can be popped from any empty stack Si , we first pop 3i elements from Si +1 and push them onto Si to make room. (Thus, if Si +1 is already empty, we first recursively fill it by popping elements from Si +2 .) Moving a single element from one stack to another takes O(1) time. Here is pseudocode for the multistack operations MSPUSH and MSPOP. The internal stacks are managed with the subroutines PUSH and POP. MPUSH( x ) : i←0 while Si is full i ← i+1 MPOP( x ) : i←0 while Si is empty i ← i+1 while i > 0 i ← i−1 for j ← 1 to 3i PUSH(Si +1 , POP(Si )) while i > 0 i ← i−1 for j ← 1 to 3i PUSH(Si , POP(Si +1 )) PUSH(S0 , x ) return POP(S0 ) ×9 ×3 Making room in a multistack, just before pushing on a new element. (a) In the worst case, how long does it take to push one more element onto a multistack containing n elements? (b) Prove that if the user never pops anything from the multistack, the amortized cost of a push operation is O(log n), where n is the maximum number of elements in the multistack during its lifetime. (c) Prove that in any intermixed sequence of pushes and pops, each push or pop operation takes O(log n) amortized time, where n is the maximum number of elements in the multistack during its lifetime. 3. Remember the difference between stacks and queues? Good. (a) Describe how to implement a queue using two stacks and O(1) additional memory, so that the amortized time for any enqueue or dequeue operation is O(1). The only access you have to the stacks is through the standard subroutines PUSH and POP. (b) A quack is a data structure combining properties of both stacks and queues. It can be viewed as a list of elements written left to right such that three operations are possible: • Push: add a new item to the left end of the list; 7 Algorithms Lecture 8: Amortized Analysis • Pop: remove the item on the left end of the list; • Pull: remove the item on the right end of the list. Implement a quack using three stacks and O(1) additional memory, so that the amortized time for any push, pop, or pull operation is O(1). In particular, each element pushed onto the quack should be stored in exactly one of the three stacks. Again, you are only allowed to access the stacks through the standard functions PUSH and POP. 4. Suppose we can insert or delete an element into a hash table in O(1) time. In order to ensure that our hash table is always big enough, without wasting a lot of memory, we will use the following global rebuilding rules: • After an insertion, if the table is more than 3/4 full, we allocate a new table twice as big as our current table, insert everything into the new table, and then free the old table. • After a deletion, if the table is less than 1/4 full, we allocate a new table half as big as our current table, insert everything into the new table, and then free the old table. Show that for any sequence of insertions and deletions, the amortized time per operation is still O(1). [Hint: Do not even look at the potential-function argument in CLRS; there is a much easier solution!] 5. Chicago has many tall buildings, but only some of them have a clear view of Lake Michigan. Suppose we are given an array A[1 .. n] that stores the height of n buildings on a city block, indexed from west to east. Building i has a good view of Lake Michigan if and only if every building to the east of i is shorter than i . Here is an algorithm that computes which buildings have a good view of Lake Michigan. What is the running time of this algorithm? GOODVIEW(A[1 .. n]): initialize a stack S for i ← 1 to n while (S not empty and A[i ] > A[TOP(S )]) POP(S ) PUSH(S , i ) return S 6. Design and analyze a simple data structure that maintains a list of integers and supports the following operations. • CREATE( ) creates and returns a new list • PUSH( L , x ) appends x to the end of L • POP( L ) deletes the last entry of L and returns it • LOOKUP( L , k) returns the kth entry of L Your solution may use these primitive data structures: arrays, balanced binary search trees, heaps, queues, single or doubly linked lists, and stacks. If your algorithm uses anything fancier, you must give an explicit implementation. Your data structure must support all operations in amortized constant time. In addition, your data structure must support each LOOKUP in worst-case O(1) time. At all times, the size of your data structure must be linear in the number of objects it stores. 8 Algorithms Lecture 8: Amortized Analysis 7. Suppose instead of powers of two, we represent integers as the sum of Fibonacci numbers. In other words, instead of an array of bits, we keep an array of fits, where the i th least significant fit indicates whether the sum includes the i th Fibonacci number Fi . For example, the fitstring 101110 F represents the number F6 + F4 + F3 + F2 = 8 + 3 + 2 + 1 = 14. Describe algorithms to increment and decrement a single fitstring in constant amortized time. [Hint: Most numbers can be represented by more than one fitstring!] 8. A doubly lazy binary counter represents any number as a weighted sum of powers of two, where each weight is one of four values: −1, 0, 1, or 2. (For succinctness, I’ll write 1 instead of −1.) Every integer—positive, negative, or zero—has an infinite number of doubly lazy binary representations. For example, the number 13 can be represented as 1101 (the standard binary representation), or 2101 (because 2 · 23 − 22 + 20 = 13) or 10111 (because 24 − 22 + 21 − 20 = 13) or 11200010111 (because −210 + 29 + 2 · 28 + 24 − 22 + 21 − 20 = 13). To increment a doubly lazy binary counter, we add 1 to the least significant bit, then carry the rightmost 2 (if any). To decrement, we subtract 1 from the lest significant bit, and then borrow the rightmost 1 (if any). LAZYINCREMENT(B [0 .. n]): B [ 0] ← B [ 0] + 1 for i ← 1 to n − 1 if B [i ] = 2 B [i ] ← 0 B [ i + 1] ← B [ i + 1] + 1 return LAZYDECREMENT(B [0 .. n]): B [ 0] ← B [ 0] − 1 for i ← 1 to n − 1 if B [i ] = −1 B [i ] ← 1 B [ i + 1] ← B [ i + 1] − 1 return For example, here is a doubly lazy binary count from zero up to twenty and then back down to zero. The bits are written with the least significant bit B [0] on the right, omitting all leading 0’s ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ 0 −→ 1 −→ 10 −→ 11 −→ 20 −→ 101 −→ 110 −→ 111 −→ 120 −→ 201 −→ 210 ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ −− −− −− −− −− −− −− −− −− −− −→ 1011 −→ 1020 −→ 1101 −→ 1110 −→ 1111 −→ 1120 −→ 1201 −→ 1210 −→ 2011 −→ 2020 −→ 2011 −→ 2010 −→ 2001 −→ 2000 −→ 2011 −→ 2110 −→ 2101 −→ 1100 −→ 1111 −→ 1010 −− −− −− −− −− −− −− −− −− −− −→ 1001 −→ 1000 −→ 1011 −→ 1110 −→ 1101 −→ 100 −→ 111 −→ 10 −→ 1 −→ 0 Prove that for any intermixed sequence of increments and decrements of a doubly lazy binary number, starting with 0, the amortized time for each operation is O(1). Do not assume, as in the example above, that all the increments come before all the decrements. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 9 Algorithms Lecture 9: Scapegoat and Splay Trees Everything was balanced before the computers went off line. Try and adjust something, and you unbalance something else. Try and adjust that, you unbalance two more and before you know what’s happened, the ship is out of control. — Blake, Blake’s 7, “Breakdown” (March 6, 1978) A good scapegoat is nearly as welcome as a solution to the problem. — Anonymous Let’s play. — El Mariachi [Antonio Banderas], Desperado (1992) CAPTAIN: CAPTAIN: CAPTAIN: CAPTAIN: TAKE OFF EVERY ’ZIG’!! YOU KNOW WHAT YOU DOING. MOVE ’ZIG’. FOR GREAT JUSTICE. — Zero Wing (1992) 9 Scapegoat and Splay Trees 9.1 Definitions I’ll assume that everyone is already familiar with the standard terminology for binary search trees—node, search key, edge, root, internal node, leaf, right child, left child, parent, descendant, sibling, ancestor, subtree, preorder, postorder, inorder, etc.—as well as the standard algorithms for searching for a node, inserting a node, or deleting a node. Otherwise, consult your favorite data structures textbook. For this lecture, we will consider only full binary trees—where every internal node has exactly two children—where only the internal nodes actually store search keys. In practice, we can represent the leaves with null pointers. Recall that the depth of a node is its distance from the root, and its height is the distance to the farthest leaf in its subtree. The height (or depth) of the tree is just the height of the root. The size of a node is the number of nodes in its subtree. The size n of the whole tree is just the total number of nodes. A tree with height h has at most 2h leaves, so the minimum height of an n-leaf binary tree is lg n . In the worst case, the time required for a search, insertion, or deletion to the height of the tree, so in general we would like keep the height as close to lg n as possible. The best we can possibly do is to have a perfectly balanced tree, in which each subtree has as close to half the leaves as possible, and both subtrees are perfectly balanced. The height of a perfectly balanced tree is lg n , so the worst-case search time is O(log n). However, even if we started with a perfectly balanced tree, a malicious sequence of insertions and/or deletions could make the tree arbitrarily unbalanced, driving the search time up to Θ(n). To avoid this problem, we need to periodically modify the tree to maintain ‘balance’. There are several methods for doing this, and depending on the method we use, the search tree is given a different name. Examples include AVL trees, red-black trees, height-balanced trees, weight-balanced trees, bounded-balance trees, path-balanced trees, B -trees, treaps, randomized binary search trees, skip lists,1 and jumplists. Some of these trees support searches, insertions, and deletions, in O(log n) worst-case time, others in O(log n) amortized time, still others in O(log n) expected time. In this lecture, I’ll discuss two binary search tree data structures with good amortized performance. The first is the scapegoat tree, discovered by Arne Andersson* in 1989 [1, 2] and independently2 by Igal 1 Yeah, yeah. Skip lists aren’t really binary search trees. Whatever you say, Mr. Picky. The claim of independence is Andersson’s [2]. The two papers actually describe very slightly different rebalancing algorithms. The algorithm I’m using here is closer to Andersson’s, but my analysis is closer to Galperin and Rivest’s. 2 1 Algorithms Lecture 9: Scapegoat and Splay Trees Galperin* and Ron Rivest in 1993 [10]. The second is the splay tree, discovered by Danny Sleator and Bob Tarjan in 1981 [16, 14]. 9.2 Lazy Deletions: Global Rebuilding First let’s consider the simple case where we start with a perfectly-balanced tree, and we only want to perform searches and deletions. To get good search and delete times, we will use a technique called global rebuilding. When we get a delete request, we locate and mark the node to be deleted, but we don’t actually delete it. This requires a simple modification to our search algorithm—we still use marked nodes to guide searches, but if we search for a marked node, the search routine says it isn’t there. This keeps the tree more or less balanced, but now the search time is no longer a function of the amount of data currently stored in the tree. To remedy this, we also keep track of how many nodes have been marked, and then apply the following rule: Global Rebuilding Rule. As soon as half the nodes in the tree have been marked, rebuild a new perfectly balanced tree containing only the unmarked nodes.3 With this rule in place, a search takes O(log n) time in the worst case, where n is the number of unmarked nodes. Specifically, since the tree has at most n marked nodes, or 2n nodes altogether, we need to examine at most lg n + 1 keys. There are several methods for rebuilding the tree in O(n) time, where n is the size of the new tree. (Homework!) So a single deletion can cost Θ(n) time in the worst case, but only if we have to rebuild; most deletions take only O(log n) time. We spend O(n) time rebuilding, but only after Ω(n) deletions, so the amortized cost of rebuilding the tree is O(1) per deletion. (Here I’m using a simple version of the ‘taxation method’. For each deletion, we charge a $1 tax; after n deletions, we’ve collected $n, which is just enough to pay for rebalancing the tree containing the remaining n nodes.) Since we also have to find and mark the node being ‘deleted’, the total amortized time for a deletion is O(log n) . 9.3 Insertions: Partial Rebuilding Now suppose we only want to support searches and insertions. We can’t ‘not really insert’ new nodes into the tree, since that would make them unavailable to the search algorithm.4 So instead, we’ll use another method called partial rebuilding. We will insert new nodes normally, but whenever a subtree becomes unbalanced enough, we rebuild it. The definition of ‘unbalanced enough’ depends on an arbitrary constant α > 1. Each node v will now also store height( v ) and size( v ). We now modify our insertion algorithm with the following rule: Partial Rebuilding Rule. After we insert a node, walk back up the tree updating the heights and sizes of the nodes on the search path. If we encounter a node v where height( v ) > α · lg(size( v )), rebuild its subtree into a perfectly balanced tree (in O(size( v )) time). If we always follow this rule, then after an insertion, the height of the tree is at most α · lg n. Thus, since α is a constant, the worst-case search time is O(log n). In the worst case, insertions require Θ(n) time—we might have to rebuild the entire tree. However, the amortized time for each insertion is again only O(log n). Not surprisingly, the proof is a little bit more complicated than for deletions. 3 Alternately: When the number of unmarked nodes is one less than an exact power of two, rebuild the tree. This rule ensures that the tree is always exactly balanced. 4 Well, we could use the Bentley-Saxe* logarithmic method [3], but that would raise the query time to O(log2 n). 2 Algorithms Lecture 9: Scapegoat and Splay Trees Define the imbalance I ( v ) of a node v to be one less than the absolute difference between the sizes of its two subtrees, or zero, whichever is larger: I ( v ) = max 0, size(left( v )) − size(right( v )) − 1 A simple induction proof implies that I ( v ) = 0 for every node v in a perfectly balanced tree. So immediately after we rebuild the subtree of v , we have I ( v ) = 0. On the other hand, each insertion into the subtree of v increments either size(left( v )) or size(right( v )), so I ( v ) changes by at most 1. The whole analysis boils down to the following lemma. Lemma 1. Just before we rebuild v ’s subtree, I ( v ) = Ω(size( v )). Before we prove this, let’s first look at what it implies. If I ( v ) = Ω(size( v )), then Ω(size( v )) keys have been inserted in the v ’s subtree since the last time it was rebuilt from scratch. On the other hand, rebuilding the subtree requires O(size( v )) time. Thus, if we amortize the rebuilding cost across all the insertions since the last rebuilding, v is charged constant time for each insertion into its subtree. Since each new key is inserted into at most α · lg n = O(log n) subtrees, the total amortized cost of an insertion is O (log n ). Proof: Since we’re about to rebuild the subtree at v , we must have height( v ) > α · lg size( v ). Without loss of generality, suppose that the node we just inserted went into v ’s left subtree. Either we just rebuilt this subtree or we didn’t have to, so we also have height(left( v )) ≤ α · lg size(left( v )). Combining these two inequalities with the recursive definition of height, we get α · lg size( v ) < height( v ) ≤ height(left( v )) + 1 ≤ α · lg size(left( v )) + 1. After some algebra, this simplifies to size(left( v )) > size( v )/21/α . Combining this with the identity size( v ) = size(left( v )) + size(right( v )) + 1 and doing some more algebra gives us the inequality size(right( v )) < 1 − 1/21/α size( v ) − 1. Finally, we combine these two inequalities using the recursive definition of imbalance. I ( v ) ≥ size(left( v )) − size(right( v )) − 1 > 2/21/α − 1 size( v ) Since α is a constant bigger than 1, the factor in parentheses is a positive constant. 9.4 Scapegoat Trees Finally, to handle both insertions and deletions efficiently, scapegoat trees use both of the previous techniques. We use partial rebuilding to re-balance the tree after insertions, and global rebuilding to re-balance the tree after deletions. Each search takes O(log n) time in the worst case, and the amortized time for any insertion or deletion is also O(log n). There are a few small technical details left (which I won’t describe), but no new ideas are required. Once we’ve done the analysis, we can actually simplify the data structure. It’s not hard to prove that at most one subtree (the scapegoat) is rebuilt during any insertion. Less obviously, we can even get the same amortized time bounds (except for a small constant factor) if we only maintain the three integers in addition to the actual tree: the size of the entire tree, the height of the entire tree, and the number of marked nodes. Whenever an insertion causes the tree to become unbalanced, we can compute the sizes of all the subtrees on the search path, starting at the new leaf and stopping at the scapegoat, in time proportional to the size of the scapegoat subtree. Since we need that much time to re-balance the scapegoat subtree, this computation increases the running time by only a small constant factor! Thus, unlike almost every other kind of balanced trees, scapegoat trees require only O(1) extra space. 3 Algorithms 9.5 Lecture 9: Scapegoat and Splay Trees Rotations, Double Rotations, and Splaying Another method for maintaining balance in binary search trees is by adjusting the shape of the tree locally, using an operation called a rotation. A rotation at a node x decreases its depth by one and increases its parent’s depth by one. Rotations can be performed in constant time, since they only involve simple pointer manipulation. y x right x y left Figure 1. A right rotation at x and a left rotation at y are inverses. For technical reasons, we will need to use rotations two at a time. There are two types of double rotations, which might be called zig-zag and roller-coaster. A zig-zag at x consists of two rotations at x , in opposite directions. A roller-coaster at a node x consists of a rotation at x ’s parent followed by a rotation at x , both in the same direction. Each double rotation decreases the depth of x by two, leaves the depth of its parent unchanged, and increases the depth of its grandparent by either one or two, depending on the type of double rotation. Either type of double rotation can be performed in constant time. z z w x x w w x z Figure 2. A zig-zag at x . The symmetric case is not shown. z x y y x x y z z Figure 3. A right roller-coaster at x and a left roller-coaster at z . Finally, a splay operation moves an arbitrary node in the tree up to the root through a series of double rotations, possibly with one single rotation at the end. Splaying a node v requires time proportional to depth( v ). (Obviously, this means the depth before splaying, since after splaying v is the root and thus has depth zero!) 9.6 Splay Trees A splay tree is a binary search tree that is kept more or less balanced by splaying. Intuitively, after we access any node, we move it to the root with a splay operation. In more detail: • Search: Find the node containing the key using the usual algorithm, or its predecessor or successor if the key is not present. Splay whichever node was found. 4 Algorithms Lecture 9: Scapegoat and Splay Trees b a b d c a m k e n m f e j i a k h g d c l f b c m f l n f c a m k j e k n m d c g k l j e l n f h l h g b x d j e x h g a x j x d n x b h i g i i i Figure 4. Splaying a node. Irrelevant subtrees are omitted for clarity. • Insert: Insert a new node using the usual algorithm, then splay that node. • Delete: Find the node x to be deleted, splay it, and then delete it. This splits the tree into two subtrees, one with keys less than x , the other with keys bigger than x . Find the node w in the left subtree with the largest key (the inorder predecessor of x in the original tree), splay it, and finally join it to the right subtree. x x w w w Figure 5. Deleting a node in a splay tree. Each search, insertion, or deletion consists of a constant number of operations of the form walk down to a node, and then splay it up to the root. Since the walk down is clearly cheaper than the splay up, all we need to get good amortized bounds for splay trees is to derive good amortized bounds for a single splay. Believe it or not, the easiest way to do this uses the potential method. We define the rank of a node v to be lg size( v ) , and the potential of a splay tree to be the sum of the ranks of its nodes: Φ= rank( v ) = v lg size( v ) v It’s not hard to observe that a perfectly balanced binary tree has potential Θ(n), and a linear chain of nodes (a perfectly unbalanced tree) has potential Θ(n log n). The amortized analysis of splay trees boils down to the following lemma. Here, rank( v ) denotes the rank of v before a (single or double) rotation, and rank ( v ) denotes its rank afterwards. Recall that the amortized cost is defined to be the number of rotations plus the drop in potential. The Access Lemma. The amortized cost of a single rotation at any node v is at most 1 + 3 rank ( v ) − 3 rank( v ), and the amortized cost of a double rotation at any node v is at most 3 rank ( v ) − 3 rank( v ). Proving this lemma is a straightforward but tedious case analysis of the different types of rotations. For the sake of completeness, I’ll give a proof (of a generalized version) in the next section. 5 Algorithms Lecture 9: Scapegoat and Splay Trees By adding up the amortized costs of all the rotations, we find that the total amortized cost of splaying a node v is at most 1 + 3 rank ( v ) − 3 rank( v ), where rank ( v ) is the rank of v after the entire splay. (The intermediate ranks cancel out in a nice telescoping sum.) But after the splay, v is the root! Thus, rank ( v ) = lg n , which implies that the amortized cost of a splay is at most 3 lg n − 1 = O(log n). We conclude that every insertion, deletion, or search in a splay tree takes O(log n) amortized time. 9.7 Other Optimality Properties In fact, splay trees are optimal in several other senses. Some of these optimality properties follow easily from the following generalization of the Access Lemma. Let’s arbitrarily assign each node v a non-negative real weight w ( v ). These weights are not actually stored in the splay tree, nor do they affect the splay algorithm in any way; they are only used to help with the analysis. We then redefine the size s( v ) of a node v to be the sum of the weights of the descendants of v , including v itself: s( v ) := w ( v ) + s(right( v )) + s(left( v )). If w ( v ) = 1 for every node v , then the size of a node is just the number of nodes in its subtree, as in the previous section. As before, we define the rank of any node v to be r ( v ) = lg s( v ), and the potential of a splay tree to be the sum of the ranks of all its nodes: Φ= r (v) = v lg s( v ) v In the following lemma, r ( v ) denotes the rank of v before a (single or double) rotation, and r ( v ) denotes its rank afterwards. The Generalized Access Lemma. For any assignment of non-negative weights to the nodes, the amortized cost of a single rotation at any node x is at most 1 + 3 r ( x ) − 3 r ( x ), and the amortized cost of a double rotation at any node v is at most 3 r ( x ) − 3 r ( x ). Proof: First consider a single rotation, as shown in Figure 1. 1 + Φ − Φ = 1 + r (x) + r ( y) − r(x) − r( y) [only x and y change rank] ≤ 1 + r (x) − r(x) [ r ( y ) ≤ r ( y )] ≤ 1 + 3r ( x ) − 3r ( x ) [ r ( x ) ≥ r ( x )] Now consider a zig-zag, as shown in Figure 2. Only w , x , and z change rank. 2+Φ −Φ = 2 + r (w ) + r ( x ) + r (z ) − r (w ) − r ( x ) − r (z ) ≤ 2 + r (w ) + r ( x ) + r (z ) − 2 r ( x ) [only w, x , z change rank] [ r ( x ) ≤ r (w ) and r ( x ) = r (z )] = 2 + ( r (w ) − r ( x )) + ( r (z ) − r ( x )) + 2( r ( x ) − r ( x )) = 2 + lg s (w ) + lg s (z ) + 2( r ( x ) − r ( x )) s (x) s (x) s ( x )/2 ≤ 2 + 2 lg + 2( r ( x ) − r ( x )) s (x) = 2( r ( x ) − r ( x )) [s (w ) + s (z ) ≤ s ( x ), lg is concave] ≤ 3( r ( x ) − r ( x )) [ r ( x ) ≥ r ( x )] 6 Algorithms Lecture 9: Scapegoat and Splay Trees Finally, consider a roller-coaster, as shown in Figure 3. Only x , y , and z change rank. 2+Φ −Φ = 2 + r ( x ) + r ( y ) + r (z ) − r ( x ) − r ( y ) − r (z ) [only x , y, z change rank] ≤ 2 + r ( x ) + r (z ) − 2 r ( x ) [ r ( y ) ≤ r (z ) and r ( x ) ≥ r ( y )] = 2 + ( r ( x ) − r ( x )) + ( r (z ) − r ( x )) + 3( r ( x ) − r ( x )) s( x ) + lg s (z ) + 3( r ( x ) − r ( x )) s (x) s (x) s ( x )/2 + 3( r ( x ) − r ( x )) ≤ 2 + 2 lg s (x) = 3( r ( x ) − r ( x )) = 2 + lg This completes the proof. [s( x ) + s (z ) ≤ s ( x ), lg is concave] 5 Observe that this argument works for arbitrary non-negative vertex weights. By adding up the amortized costs of all the rotations, we find that the total amortized cost of splaying a node x is at most 1 + 3 r (root) − 3 r ( x ). (The intermediate ranks cancel out in a nice telescoping sum.) This analysis has several immediate corollaries. The first corollary is that the amortized search time in a splay tree is within a constant factor of the search time in the best possible static binary search tree. Thus, if some nodes are accessed more often than others, the standard splay algorithm automatically keeps those more frequent nodes closer to the root, at least most of the time. Static Optimality Theorem. Suppose each node x is accessed at least t ( x ) times, and let T = The amortized cost of accessing x is O(log T − log t ( x )). x t ( x ). Proof: Set w ( x ) = t ( x ) for each node x . For any nodes x and z , let dist( x , z ) denote the rank distance between x and y , that is, the number of nodes y such that key( x ) ≤ key( y ) ≤ key(z ) or key( x ) ≥ key( y ) ≥ key(z ). In particular, dist( x , x ) = 1 for all x . Static Finger Theorem. For any fixed node f (‘the finger’), the amortized cost of accessing x is O(lg dist( f , x )). Proof: Set w ( x ) = 1/dist( x , f )2 for each node x . Then s(root) ≤ r ( x ) ≥ lg w ( x ) = −2 lg dist( f , x ). ∞ 2 i =1 2/ i = π2 /3 = O(1), and Here are a few more interesting properties of splay trees, which I’ll state without proof.6 The proofs of these properties (especially the dynamic finger theorem) are considerably more complicated than the amortized analysis presented above. Working Set Theorem [14]. The amortized cost of accessing node x is O(log D), where D is the number of distinct items accessed since the last time x was accessed. (For the first access to x , we set D = n.) 5 This proof is essentially taken verbatim from the original Sleator and Tarjan paper. Another proof technique, which may be more accessible, involves maintaining lg s( v ) tokens on each node v and arguing about the changes in token distribution caused by each single or double rotation. But I haven’t yet internalized this approach enough to include it here. 6 This list and the following section are taken almost directly from Erik Demaine’s lecture notes [5]. 7 Algorithms Lecture 9: Scapegoat and Splay Trees Scanning Theorem [17]. Splaying all nodes in a splay tree in order, starting from any initial tree, requires O(n) total rotations. Dynamic Finger Theorem [7, 6]. Immediately after accessing node y , the amortized cost of accessing node x is O(lg dist( x , y )). 9.8 Splay Tree Conjectures Splay trees are conjectured to have many interesting properties in addition to the optimality properties that have been proved; I’ll describe just a few of the more important ones. The Deque Conjecture [17] considers the cost of dynamically maintaining two fingers l and r , starting on the left and right ends of the tree. Suppose at each step, we can move one of these two fingers either one step left or one step right; in other words, we are using the splay tree as a doubly-ended queue. Sundar* proved that the total cost of m deque operations on an n-node splay tree is O((m + n)α(m + n)) [15]. More recently, Pettie later improved this bound to O(mα∗ (n)) [13]. The Deque Conjecture states that the total cost is actually O(m + n). The Traversal Conjecture [14] states that accessing the nodes in a splay tree, in the order specified by a preorder traversal of any other binary tree with the same keys, takes O(n) time. This is generalization of the Scanning Theorem. The Unified Conjecture [12] states that the time to access node x is O(lg min y ( D( y ) + d ( x , y ))), where D( y ) is the number of distinct nodes accessed since the last time y was accessed. This would immediately imply both the Dynamic Finger Theorem, which is about spatial locality, and the Working Set Theorem, which is about temporal locality. Two other structures are known that satisfy the unified bound [4, 12]. Finally, the most important conjecture about splay trees, and one of the most important open problems about data structures, is that they are dynamically optimal [14]. Specifically, the cost of any sequence of accesses to a splay tree is conjectured to be at most a constant factor more than the cost of the best possible dynamic binary search tree that knows the entire access sequence in advance. To make the rules concrete, we consider binary search trees that can undergo arbitrary rotations after a search; the cost of a search is the number of key comparisons plus the number of rotations. We do not require that the rotations be on or even near the search path. This is an extremely strong conjecture! No dynamically optimal binary search tree is known, even in the offline setting. However, three very similar O(log log n)-competitive binary search trees have been discovered in the last few years: Tango trees [8], multisplay trees [18], and chain-splay trees [11]. A recently-published geometric formulation of dynamic binary search trees [9] also offers significant hope for future progress. References [1] A. Andersson*. Improving partial rebuilding by using simple balance criteria. Proc. Workshop on Algorithms and Data Structures, 393–402, 1989. Lecture Notes Comput. Sci. 382, Springer-Verlag. [2] A. Andersson. General balanced trees. J. Algorithms 30:1–28, 1999. [3] J. L. Bentley and J. B. Saxe*. Decomposable searching problems I: Static-to-dynamic transformation. J. Algorithms 1(4):301–358, 1980. ˘ [4] M. Badiou* and E. D. Demaine. A simplified and dynamic unified structure. Proc. 6th Latin American Symp. Theoretical Informatics, 466–473, 2004. Lecture Notes Comput. Sci. 2976, Springer-Verlag. [5] J. Cohen* and E. Demaine. 6.897: Advanced data structures (Spring 2005), Lecture 3, February 8 2005. 〈http://theory.csail.mit.edu/classes/6.897/spring05/lec.html〉. [6] R. Cole. On the dynamic finger conjecture for splay trees. Part II: The proof. SIAM J. Comput. 30(1):44–85, 2000. 8 Algorithms Lecture 9: Scapegoat and Splay Trees [7] R. Cole, B. Mishra, J. Schmidt, and A. Siegel. On the dynamic finger conjecture for splay trees. Part I: Splay sorting log n-block sequences. SIAM J. Comput. 30(1):1–43, 2000. ˘s [8] E. D. Demaine, D. Harmon*, J. Iacono, and M. Patra¸cu*. Dynamic optimality—almost. Proc. 45th Ann. IEEE Symp. Foundations Comput. Sci., 484–490, 2004. ˘s [9] E. D. Demaine, D. Harmon*, J. Iacono, D. Kane*, and M. Patra¸cu*. The geometry of binary search trees. Proc. 20th ACM-SIAM Symp. Discrete Algorithms., 496–505, 2009. [10] I. Galperin* and R. R. Rivest. Scapegoat trees. Proc. 4th Ann. ACM-SIAM Symp. Discrete Algorithms, 165–174, 1993. [11] G. Georgakopolous. Chain-splay trees, or, how to achieve and prove log log N -competitiveness by splaying. Inform. Proc. Lett. 106:37–34, 2008. [12] J. Iacono*. Alternatives to splay trees with O(log n) worst-case access times. Proc. 12th Ann. ACM-SIAM Symp. Discrete Algorithms, 516–522, 2001. [13] S. Pettie. Splay trees, Davenport-Schinzel sequences, and the deque conjecture. Proc. 19th Ann. ACM-SIAM Symp. Discrete Algorithms, 1115–1124, 2008. [14] D. D. Sleator and R. E. Tarjan. Self-adjusting binary search trees. J. ACM 32(3):652–686, 1985. [15] R. Sundar*. On the Deque conjecture for the splay algorithm. Combinatorica 12(1):95–124, 1992. [16] R. E. Tarjan. Data Structures and Network Algorithms. CBMS-NSF Regional Conference Series in Applied Mathematics 44. SIAM, 1983. [17] R. E. Tarjan. Sequential access in splay trees takes linear time. Combinatorica 5(5):367–378, 1985. [18] C. C. Wang*, J. Derryberry*, and D. D. Sleator. O(log log n)-competitive dynamic binary search trees. Proc. 17th Ann. ACM-SIAM Symp. Discrete Algorithms, 374–383, 2006. *Starred authors were students at the time that the cited work was published. Exercises 1. (a) An n-node binary tree is perfectly balanced if either n ≤ 1, or its two subtrees are perfectly balanced binary trees, each with at most n/2 nodes. Prove that I ( v ) = 0 for every node v of any perfectly balanced tree. (b) Prove that at most one subtree is rebalanced during a scapegoat tree insertion. 2. In a dirty binary search tree, each node is labeled either clean or dirty. The lazy deletion scheme used for scapegoat trees requires us to purge the search tree, keeping all the clean nodes and deleting all the dirty nodes, as soon as half the nodes become dirty. In addition, the purged tree should be perfectly balanced. (a) Describe and analyze an algorithm to purge an arbitrary n-node dirty binary search tree in O(n) time. (Such an algorithm is necessary for scapegoat trees to achieve O(log n) amortized insertion cost.) (b) Modify your algorithm so that is uses only O(log n) space, in addition to the tree itself. Don’t forget to include the recursion stack in your space bound. (c) Modify your algorithm so that is uses only O(1) additional space. In particular, your algorithm cannot call itself recursively at all. 3. Consider the following simpler alternative to splaying: MOVETOROOT( v ): while parent( v ) = NULL rotate at v 9 Algorithms Lecture 9: Scapegoat and Splay Trees Prove that the amortized cost of MOVETOROOT in an n-node binary tree can be Ω(n). That is, prove that for any integer k, there is a sequence of k MOVETOROOT operations that require Ω(kn) time to execute. 4. Suppose we want to maintain a dynamic set of values, subject to the following operations: • INSERT( x ): Add x to the set (if it isn’t already there). • PRINT&DELETEBETWEEN(a, b): Print every element x in the range a ≤ x ≤ b, in increasing order, and delete those elements from the set. For example, if the current set is {1, 5, 3, 4, 8}, then PRINT&DELETEBETWEEN(4, 6) prints the numbers 4 and 5 and changes the set to {1, 3, 8}. For any underlying set, PRINT&DELETEBETWEEN(−∞, ∞) prints its contents in increasing order and deletes everything. (a) Describe and analyze a data structure that supports these operations, each with amortized cost O(log n), where n is the maximum number of elements in the set. (b) What is the running time of your INSERT algorithm in the worst case? (c) What is the running time of your PRINT&DELETEBETWEEN algorithm in the worst case? 5. Let P be a set of n points in the plane. The staircase of P is the set of all points in the plane that have at least one point in P both above and to the right. A set of points in the plane and its staircase (shaded). (a) Describe an algorithm to compute the staircase of a set of n points in O(n log n) time. (b) Describe and analyze a data structure that stores the staircase of a set of points, and an algorithm ABOVE?( x , y ) that returns TRUE if the point ( x , y ) is above the staircase, or FALSE otherwise. Your data structure should use O(n) space, and your ABOVE? algorithm should run in O(log n) time. TRUE FALSE Two staircase queries. (c) Describe and analyze a data structure that maintains a staircase as new points are inserted. Specifically, your data structure should support a function INSERT( x , y ) that adds the point 10 Algorithms Lecture 9: Scapegoat and Splay Trees ( x , y ) to the underlying point set and returns TRUE or FALSE to indicate whether the staircase of the set has changed. Your data structure should use O(n) space, and your INSERT algorithm should run in O(log n) amortized time. TRUE! FALSE! Two staircase insertions. 6. Say that a binary search tree is augmented if every node v also stores size( v ), the number of nodes in the subtree rooted at v . (a) Show that a rotation in an augmented binary tree can be performed in constant time. (b) Describe an algorithm SCAPEGOATSELECT(k) that selects the kth smallest item in an augmented scapegoat tree in O(log n) worst-case time. (The scapegoat trees presented in these notes are already augmented.) (c) Describe an algorithm SPLAYSELECT(k) that selects the kth smallest item in an augmented splay tree in O(log n) amortized time. (d) Describe an algorithm TREAPSELECT(k) that selects the kth smallest item in an augmented treap in O(log n) expected time. 7. Many applications of binary search trees attach a secondary data structure to each node in the tree, to allow for more complicated searches. Let T be an arbitrary binary tree. The secondary data structure at any node v stores exactly the same set of items as the subtree of T rooted at v . This secondary structure has size O(size( v )) and can be built in O(size( v )) time, where size( v ) denotes the number of descendants of v . The primary and secondary data structures are typically defined by different attributes of the data being stored. For example, to store a set of points in the plane, we could define the primary tree T in terms of the x -coordinates of the points, and define the secondary data structures in terms of their y -coordinate. Maintaining these secondary structures complicates algorithms for keeping the top-level search tree balanced. Specifically, performing a rotation at any node v in the primary tree now requires O(size( v )) time, because we have to rebuild one of the secondary structures (at the new child of v ). When we insert a new item into T , we must also insert into one or more secondary data structures. (a) Overall, how much space does this data structure use in the worst case? (b) How much space does this structure use if the primary search tree is perfectly balanced? (c) Suppose the primary tree is a splay tree. Prove that the amortized cost of a splay (and therefore of a search, insertion, or deletion) is Ω(n). [Hint: This is easy!] (d) Now suppose the primary tree T is a scapegoat tree. How long does it take to rebuild the subtree of T rooted at some node v , as a function of size( v )? 11 Algorithms Lecture 9: Scapegoat and Splay Trees (e) Suppose the primary tree and all the secondary trees are scapegoat trees. What is the amortized cost of a single insertion? (f) Finally, suppose the primary tree and every secondary tree is a treap. What is the worst-case expected time for a single insertion? 8. Let X = 〈 x 1 , x 2 , . . . , x m 〉 be a sequence of m integers, each from the set {1, 2, . . . , n}. We can visualize this sequence as a set of integer points in the plane, by interpreting each element x i as the point ( x i , i ). The resulting point set, which we can also call X , has exactly one point on each row of the n × m integer grid. (a) Let Y be an arbitrary set of integer points in the plane. Two points ( x 1 , y1 ) and ( x 2 , y2 ) in Y are isolated if (1) x 1 = x 2 and y1 = y2 , and (2) there is no other point ( x , y ) ∈ Y with x 1 ≤ x ≤ x 2 and y1 ≤ y ≤ y2 . If the set Y contains no isolated pairs of points, we call Y a commune.7 Let X be an arbitrary set of points on the n × n integer grid with exactly one point per row. Show that there is a commune Y that contains X and consists of O(n log n) points. (b) Consider the following model of self-adjusting binary search trees. We interpret X as a sequence of accesses in a binary search tree. Let T0 denote the initial tree. In the i th round, we traverse the path from the root to node x i , and then arbitrarily reconfigure some subtree Si of the current search tree Ti −1 to obtain the next search tree Ti . The only restriction is that the subtree Si must contain both x i and the root of Ti −1 . (For example, in a splay tree, Si is the search path to x i .) The cost of the i th access is the number of nodes in the subtree Si . Prove that the minimum cost of executing an access sequence X in this model is at least the size of the smallest commune containing the corresponding point set X . [Hint: Lowest common ancestor.] (c) Suppose X is a random permutation of the integers 1, 2, . . . , n. Use the lower bound in part (b) to prove that the expected minimum cost of executing X is Ω(n log n). (d) Describe a polynomial-time algorithm to compute (or even approximate up to constant factors) the smallest commune containing a given set X of integer points, with at most one point per row. Alternately, prove that the problem is NP-hard. 7 Demaine et al. [9] refer to communes as arborally satisfied sets. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 12 Algorithms Lecture 10: Disjoint Sets E pluribus unum (Out of many, one) — Ofcial motto of the United States of America John: Who’s your daddy? C’mon, you know who your daddy is! Who’s your daddy? D’Argo, tell him who his daddy is!" D’Argo: I’m your daddy. — Farscape, “Thanks for Sharing” (June 15, 2001) What rolls down stairs, alone or in pairs, rolls over your neighbor’s dog? What’s great for a snack, and fits on your back? It’s Log, Log, Log! It’s Log! It’s Log! It’s big, it’s heavy, it’s wood! It’s Log! It’s Log! It’s better than bad, it’s good! — Ren & Stimpy, “Stimpy’s Big Day/The Big Shot" (August 11, 1991) lyrics by John Kricfalusi The thing’s hollow - it goes on forever - and - oh my God! - it’s full of stars! — Capt. David Bowman’s last words(?) 2001: A Space Odyssey by Arthur C. Clarke (1968) 10 Data Structures for Disjoint Sets In this lecture, we describe some methods for maintaining a collection of disjoint sets. Each set is represented as a pointer-based data structure, with one node per element. We will refer to the elements as either ‘objects’ or ‘nodes’, depending on whether we want to emphasize the set abstraction or the actual data structure. Each set has a unique ‘leader’ element, which identifies the set. (Since the sets are always disjoint, the same object cannot be the leader of more than one set.) We want to support the following operations. • MAKESET( x ): Create a new set { x } containing the single element x . The object x must not appear in any other set in our collection. The leader of the new set is obviously x . • FIND( x ): Find (the leader of) the set containing x . • UNION(A, B ): Replace two sets A and B in our collection with their union A ∪ B . For example, UNION(A, MAKESET( x )) adds a new element x to an existing set A. The sets A and B are specified by arbitrary elements, so UNION( x , y ) has exactly the same behavior as UNION(FIND( x ), FIND( y )). Disjoint set data structures have lots of applications. For instance, Kruskal’s minimum spanning tree algorithm relies on such a data structure to maintain the components of the intermediate spanning forest. Another application is maintaining the connected components of a graph as new vertices and edges are added. In both these applications, we can use a disjoint-set data structure, where we maintain a set for each connected component, containing that component’s vertices. 10.1 Reversed Trees One of the easiest ways to store sets is using trees, in which each node represents a single element of the set. Each node points to another node, called its parent, except for the leader of each set, which points to itself and thus is the root of the tree. MAKESET is trivial. FIND traverses parent pointers up to the leader. UNION just redirects the parent pointer of one leader to the other. Unlike most tree data structures, nodes do not have pointers down to their children. 1 Algorithms Lecture 10: Disjoint Sets FIND( x ): while x = parent( x ) x ← parent( x ) return x MAKESET( x ): parent( x ) ← x a b UNION( x , y ): x ← FIND( x ) y ← FIND( y ) parent( y ) ← x p d q p r a c b q r d c Merging two sets stored as trees. Arrows point to parents. The shaded node has a new parent. MAKE-SET clearly takes Θ(1) time, and UNION requires only O(1) time in addition to the two FINDs. The running time of FIND( x ) is proportional to the depth of x in the tree. It is not hard to come up with a sequence of operations that results in a tree that is a long chain of nodes, so that FIND takes Θ(n ) time in the worst case. However, there is an easy change we can make to our UNION algorithm, called union by depth, so that the trees always have logarithmic depth. Whenever we need to merge two trees, we always make the root of the shallower tree a child of the deeper one. This requires us to also maintain the depth of each tree, but this is quite easy. MAKESET( x ): parent( x ) ← x depth( x ) ← 0 FIND( x ): while x = parent( x ) x ← parent( x ) return x UNION( x , y ) x ← FIND( x ) y ← FIND( y ) if depth( x ) > depth( y ) parent( y ) ← x else parent( x ) ← y if depth( x ) = depth( y ) depth( y ) ← depth( y ) + 1 With this new rule in place, it’s not hard to prove by induction that for any set leader x , the size of x ’s set is at least 2depth( x ) , as follows. If depth( x ) = 0, then x is the leader of a singleton set. For any d > 0, when depth( x ) becomes d for the first time, x is becoming the leader of the union of two sets, both of whose leaders had depth d − 1. By the inductive hypothesis, both component sets had at least 2d −1 elements, so the new set has at least 2d elements. Later UNION operations might add elements to x ’s set without changing its depth, but that only helps us. Since there are only n elements altogether, the maximum depth of any set is lg n. We conclude that if we use union by depth, both FIND and UNION run in Θ(log n ) time in the worst case. 10.2 Shallow Threaded Trees Alternately, we could just have every object keep a pointer to the leader of its set. Thus, each set is represented by a shallow tree, where the leader is the root and all the other elements are its children. With this representation, MAKESET and FIND are completely trivial. Both operations clearly run in constant time. UNION is a little more difficult, but not much. Our algorithm sets all the leader pointers in one set to point to the leader of the other set. To do this, we need a method to visit every element in a set; we will ‘thread’ a linked list through each set, starting at the set’s leader. The two threads are merged in the UNION algorithm in constant time. 2 Algorithms Lecture 10: Disjoint Sets b c a p a d q p r q r b c d Merging two sets stored as threaded trees. Bold arrows point to leaders; lighter arrows form the threads. Shaded nodes have a new leader. UNION( x , y ): x ← FIND( x ) y ← FIND( y ) MAKESET( x ): leader( x ) ← x next( x ) ← x FIND( x ): return leader( x ) y←y leader( y ) ← x while (next( y ) = NULL) y ← next( y ) leader( y ) ← x next( y ) ← next( x ) next( x ) ← y The worst-case running time of UNION is a constant times the size of the larger set. Thus, if we merge a one-element set with another n-element set, the running time can be Θ(n). Generalizing this idea, it is quite easy to come up with a sequence of n MAKESET and n − 1 UNION operations that requires Θ(n2 ) time to create the set {1, 2, . . . , n} from scratch. WORSTCASESEQUENCE(n): MAKESET(1) for i ← 2 to n MAKESET(i ) UNION(1, i ) We are being stupid in two different ways here. One is the order of operations in WORSTCASESEQUENCE. Obviously, it would be more efficient to merge the sets in the other order, or to use some sort of divide and conquer approach. Unfortunately, we can’t fix this; we don’t get to decide how our data structures are used! The other is that we always update the leader pointers in the larger set. To fix this, we add a comparison inside the UNION algorithm to determine which set is smaller. This requires us to maintain the size of each set, but that’s easy. MAKEWEIGHTEDSET( x ): leader( x ) ← x next( x ) ← x size( x ) ← 1 WEIGHTEDUNION( x , y ) x ← FIND( x ) y ← FIND( y ) if size( x ) > size( y ) UNION( x , y ) size( x ) ← size( x ) + size( y ) else UNION( y , x ) size( x ) ← size( x ) + size( y ) The new WEIGHTEDUNION algorithm still takes Θ(n) time to merge two n-element sets. However, in an amortized sense, this algorithm is much more efficient. Intuitively, before we can merge two large sets, we have to perform a large number of MAKEWEIGHTEDSET operations. Theorem 1. A sequence of m MAKEWEIGHTEDSET operations and n WEIGHTEDUNION operations takes O(m + n log n) time in the worst case. 3 Algorithms Lecture 10: Disjoint Sets Proof: Whenever the leader of an object x is changed by a WEIGHTEDUNION, the size of the set containing x increases by at least a factor of two. By induction, if the leader of x has changed k times, the set containing x has at least 2k members. After the sequence ends, the largest set contains at most n members. (Why?) Thus, the leader of any object x has changed at most lg n times. Since each WEIGHTEDUNION reduces the number of sets by one, there are m − n sets at the end of the sequence, and at most n objects are not in singleton sets. Since each of the non-singleton objects had O(log n) leader changes, the total amount of work done in updating the leader pointers is O(n log n). The aggregate method now implies that each WEIGHTEDUNION has amortized cost O (log n ). 10.3 Path Compression Using unthreaded tress, FIND takes logarithmic time and everything else is constant; using threaded trees, UNION takes logarithmic amortized time and everything else is constant. A third method allows us to get both of these operations to have almost constant running time. We start with the original unthreaded tree representation, where every object points to a parent. The key observation is that in any FIND operation, once we determine the leader of an object x , we can speed up future FINDs by redirecting x ’s parent pointer directly to that leader. In fact, we can change the parent pointers of all the ancestors of x all the way up to the root; this is easiest if we use recursion for the initial traversal up the tree. This modification to FIND is called path compression. p a b q p c r d b a q r d c Path compression during Find(c ). Shaded nodes have a new parent. FIND( x ) if x = parent( x ) parent( x ) ← FIND(parent( x )) return parent( x ) If we use path compression, the ‘depth’ field we used earlier to keep the trees shallow is no longer correct, and correcting it would take way too long. But this information still ensures that FIND runs in Θ(log n) time in the worst case, so we’ll just give it another name: rank. The following algorithm is usually called union by rank: MAKESET( x ): parent( x ) ← x rank( x ) ← 0 UNION( x , y ) x ← FIND( x ) y ← FIND( y ) if rank( x ) > rank( y ) parent( y ) ← x else parent( x ) ← y if rank( x ) = rank( y ) rank( y ) ← rank( y ) + 1 4 Algorithms Lecture 10: Disjoint Sets FIND still runs in O(log n) time in the worst case; path compression increases the cost by only most a constant factor. But we have good reason to suspect that this upper bound is no longer tight. Our new algorithm memoizes the results of each FIND, so if we are asked to FIND the same item twice in a row, the second call returns in constant time. Splay trees used a similar strategy to achieve their optimal amortized cost, but our up-trees have fewer constraints on their structure than binary search trees, so we should get even better performance. This intuition is exactly correct, but it takes a bit of work to define precisely how much better the performance is. As a first approximation, we will prove below that the amortized cost of a FIND operation is bounded by the iterated logarithm of n, denoted log∗ n, which is the number of times one must take the logarithm of n before the value is less than 1: lg∗ n = if n ≤ 2, 1 ∗ 1 + lg (lg n) otherwise. Our proof relies on several useful properties of ranks, which follow directly from the UNION and FIND algorithms. • If a node x is not a set leader, then the rank of x is smaller than the rank of its parent. • Whenever parent( x ) changes, the new parent has larger rank than the old parent. • Whenever the leader of x ’s set changes, the new leader has larger rank than the old leader. • The size of any set is exponential in the rank of its leader: size( x ) ≥ 2rank( x ) . (This is easy to prove by induction, hint, hint.) • In particular, since there are only n objects, the highest possible rank is lg n . • For any integer r , there are at most n/2 r objects of rank r . Only the last property requires a clever argument to prove. Fix your favorite integer r . Observe that only set leaders can change their rank. Whenever the rank of any set leader x changes from r − 1 to r , mark all the objects in x ’s set. Since leader ranks can only increase over time, each object is marked at most once. There are n objects altogether, and any object with rank r marks at least 2 r objects. It follows that there are at most n/2 r objects with rank r , as claimed. 10.4 O (log∗ n ) Amortized Time The following analysis of path compression was discovered just a few years ago by Raimund Seidel and Micha Sharir.1 Previous proofs2 relied on complicated charging schemes or potential-function arguments; Seidel and Sharir’s analysis relies on a comparatively simple recursive decomposition. Seidel and Sharir phrase their analysis in terms of two more general operations on set forests. Their more general COMPRESS operation compresses any directed path, not just paths that lead to the root. The new SHATTER operation makes every node on a root-to-leaf path into its own parent. COMPRESS( x , y ): 〈〈 y must be an ancestor of x 〉〉 if x = y COMPRESS(parent( x ), y ) parent( x ) ← parent( y ) 1 2 SHATTER( x ): if parent( x ) = x SHATTER(parent( x )) parent( x ) ← x Raimund Seidel and Micha Sharir. Top-down analysis of path compression. SIAM J. Computing 34(3):515–525, 2005. Robert E. Tarjan. Efficiency of a good but not linear set union algorithm. J. Assoc. Comput. Mach. 22:215–225, 1975. 5 Algorithms Lecture 10: Disjoint Sets Clearly, the running time of FIND( x ) operation is dominated by the running time of COMPRESS( x , y ), where y is the leader of the set containing x . This implies that we can prove the upper bound by analyzing an arbitrary sequence of UNION and COMPRESS operations. Moreover, we can assume that the arguments to each UNION operation are set leaders, so that each UNION takes only constant worst-case time. Finally, since each call to COMPRESS specifies the top node in the path to be compressed, we can reorder the sequence of operations, so that every UNION occurs before any COMPRESS, without changing the number of pointer assignments. y x y x y x x y y y x x Top row: A COMPRESS followed by a UNION. Bottom row: The same operations in the opposite order. Each UNION requires only constant time in the worst case, so we only need to analyze the amortized cost of COMPRESS. The running time of COMPRESS is proportional to the number of parent pointer assignments, plus O(1) overhead, so we will phrase our analysis in terms of pointer assignments. Let T (m , n , r ) denote the worst case number of pointer assignments in any sequence of at most m COMPRESS operations, executed on a forest of at most n nodes, with maximum rank at most r . The following trivial upper bound will be the base case for our recursive argument. Theorem 2. T (m, n, r ) ≤ nr Proof: Each node can change parents at most r times, because each new parent has higher rank than the previous parent. Fix a forest F of n nodes with maximum rank r , and a sequence C of m COMPRESS operations on F , and let T ( F, C ) denote the total number of pointer assignments executed by this sequence. Let s be an arbitrary positive rank. Partition F into two sub-forests: a ‘low’ forest F− containing all nodes with rank at most s, and a ‘high’ forest F+ containing all nodes with rank greater than s. Since ranks increase as we follow parent pointers, every ancestor of a high node is another high node. Let n− and n+ denote the number of nodes in F− and F+ , respectively. Finally, let m+ denote the number of COMPRESS operations that involve any node in F+ , and let m− = m − m+ . F F+ rank ≥ s rank < s rank ≥ s rank < s F– Splitting the forest F (in this case, a single tree) into sub-forests F+ and F− at rank s. 6 Algorithms Lecture 10: Disjoint Sets Any sequence of COMPRESS operations on F can be decomposed into a sequence of COMPRESS operations on F+ , plus a sequence of COMPRESS and SHATTER operations on F− , with the same total cost. This requires only one small modification to the code: We forbid any low node from having a high parent. Specifically, if x is a low node and y is a high node, we replace any assignment parent( x ) ← y with parent( x ) ← x . A Compress operation in F splits into a Compress operation in F+ and a Shatter operation in F− This modification is equivalent to the following reduction: COMPRESS( x , y, F ): 〈〈 y is an ancestor of x 〉〉 if rank( x ) > r COMPRESS( x , y, F+ ) 〈〈in C+ 〉〉 else if rank( y ) ≤ r COMPRESS( x , y, F− ) 〈〈in C− 〉〉 else z ← highest ancestor of x in F with rank at most r COMPRESS(parent F (z ), y, F+ ) 〈〈in C+ 〉〉 SHATTER( x , z , F− ) parent(z ) ← z (∗) The pointer assignment in the last line looks redundant, but it is actually necessary for the analysis. Each execution of line (∗) mirrors an assignment of the form parent( x ) ← y , where x is a low node, y is a high node, and the previous parent of x was a high node. Each of these ‘redundant’ assignments happens immediately after a COMPRESS in the top forest, so we perform at most m+ redundant assignments. Each node x is touched by at most one SHATTER operation, so the total number of pointer reassignments in all the SHATTER operations is at most n. Thus, by partitioning the forest F into F+ and F− , we have also partitioned the sequence C of COMPRESS operations into subsequences C+ and C− , with respective lengths m+ and m− , such that the following inequality holds: T ( F, C ) ≤ T ( F+ , C+ ) + T ( F− , C− ) + m+ + n Since there are only n/2i nodes of any rank i , we have n+ ≤ i >s n/2i = n/2s . The number of different ranks in F+ is r − s < r . Thus, Theorem 2 implies the upper bound T ( F+ , C+ ) < r n/2s . Let us fix s = lg r , so that T ( F+ , C+ ) ≤ n. We can now simplify our earlier recurrence to T ( F, C ) ≤ T ( F− , C− ) + m+ + 2n, 7 Algorithms Lecture 10: Disjoint Sets or equivalently, T ( F, C ) − m ≤ T ( F− , C− ) − m− + 2n. Since this argument applies to any forest F and any sequence C , we have just proved that T (m, n, r ) ≤ T (m, n, lg r ) + 2n , where T (m, n, r ) = T (m, n, r ) − m. The solution to this recurrence is T (n, m, r ) ≤ 2n lg∗ r . Voilá! Theorem 3. T (m, n, r ) ≤ m + 2n lg∗ r 10.5 Turning the Crank There is one place in the preceding analysis where we have significant room for improvement. Recall that we bounded the total cost of the operations on F+ using the trivial upper bound from Theorem 2. But we just proved a better upper bound in Theorem 3! We can apply precisely the same strategy, using Theorem 3 instead of Theorem 2, to improve the bound even more. ∗ Suppose we fix s = lg∗ r , so that n+ = n/2lg r . Theorem 3 implies that T ( F+ , C+ ) ≤ m+ + 2n lg∗ r 2lg ∗ ≤ m+ + 2n. r This implies the recurrence T ( F, C ) ≤ T ( F− , C− ) + 2m+ + 3n, which in turn implies that T (m, n, r ) ≤ T (m, n, lg∗ r ) + 3n, where T (m, n, r ) = T (m, n, r ) − 2m. The solution to this equation is T (m, n, r ) ≤ 2m + 3n lg∗∗ r , where lg∗∗ r is the iterated iterated logarithm of r : lg∗∗ r = if r ≤ 2, 1 ∗∗ ∗ 1 + lg (lg r ) otherwise. Naturally we can apply the same improvement strategy again, and again, as many times as we like, each time producing a tighter upper bound. Applying the reduction c times, for any positive integer c , gives us c T (m, n, r ) ≤ cm + (c + 1)n lg∗ r where ∗c lg r = lg r if c = 0, 1 if r ≤ 2, ∗c ∗ c −1 1 + lg (lg r ) otherwise. Each time we ‘turn the crank’, the dependence on m increases, while the dependence on n and r decreases. For sufficiently large values of c , the cm term dominates the time bound, and further iterations only make things worse. The point of diminishing returns can be estimated by the minimum number of stars such that lg∗∗···∗ r is smaller than a constant: c α( r ) = min c ≥ 1 lg∗ n ≤ 3 . c (The threshold value 3 is used here because lg∗ 5 ≥ 2 for all c .) By setting c = α( r ), we obtain our final upper bound. 8 Algorithms Lecture 10: Disjoint Sets Theorem 4. T (m, n, r ) ≤ mα( r ) + 3n(α( r ) + 1) We can assume without loss of generality that m ≥ n by ignoring any singleton sets, so this upper bound can be further simplified to T (m, n, r ) = O(mα( r )) = O(mα(n)). It follows that if we use union by rank, FIND with path compression runs in O(α(n)) amortized time . Even this upper bound is somewhat conservative if m is larger than n. A closer estimate is given by the function c α(m, n) = min c ≥ 1 log∗ (lg n) ≤ m/n . It’s not hard to prove that if m = Θ(n), then α(m, n) = Θ(α(n)). On the other hand, if m ≥ n lg∗∗∗∗∗ n, for any constant number of stars, then α(m, n) = O(1). So even if the number of FIND operations is only slightly larger than the number of nodes, the amortized cost of each FIND is constant. O(α(m, n)) is actually a tight upper bound for the amortized cost of path compression; there are no more tricks that will improve the analysis further. More surprisingly, this is the best amortized bound we obtain for any pointer-based data structure for maintaining disjoint sets; the amortized cost of every FIND algorithm is at least Ω(α(m, n)). The proof of the matching lower bound is, unfortunately, far beyond the scope of this class.3 10.6 The Ackermann Function and its Inverse The iterated logarithms that fell out of our analysis of path compression are the inverses of a hierarchy of recursive functions defined by Wilhelm Ackermann in 1928.4 c 2↑ n= 2 if n = 1 2n if c = 0 c −1 c 2↑ (2 ↑ (n − 1)) otherwise For each fixed c , the function 2 ↑c n is monotonically increasing in n, and these functions grow incredibly faster as the index c increases. 2 ↑ n is the familiar power function 2n . 2 ↑↑ n is the tower function 22 .2 .. n 2 ; this function is also sometimes called tetration. John Conway named 2 ↑↑↑ n the wower function: 2 ↑↑↑ n = 2 ↑↑ 2 ↑↑ · · · ↑↑ 2. And so on, et cetera, ad infinitum. n For any fixed c , the function log∗ n is the inverse of the function 2 ↑c +1 n, the (c + 1)th row in the Ackerman hierarchy. Thus, for any remotely reasonable values of n, say n ≤ 2256 , we have log∗ n ≤ 5, c log∗∗ n ≤ 4, and log∗ n ≤ 3 for any c ≥ 3. The function α(n) is usually called the inverse Ackerman function.5 Our earlier definition is equivalent to α(n) = min{c ≥ 1 | 2 ↑c +2 3 ≥ n}; in other words, α(n) + 2 is the inverse of the third column in the c Ackermann hierarchy. The function α(n) grows much more slowly than log∗ n for any fixed c ; we have c 3 Robert E. Tarjan. A class of algorithms which require non-linear time to maintain disjoint sets. J. Comput. Syst. Sci. 19:110–127, 1979. 4 Ackermann didn’t define his functions this way—I’m actually describing a slightly cleaner hierarchy defined 35 years later by R. Creighton Buck—but the exact details of the definition are surprisingly irrelevant! The mnemonic up-arrow notation for these functions was introduced by Don Knuth in the 1970s. 5 Strictly speaking, the name ‘inverse Ackerman function’ is inaccurate. One good formal definition of the true inverse c ˜ Ackerman function is α(n) = min c ≥ 1 lg∗ n ≤ c = min c ≥ 1 2 ↑c +2 c ≥ n . However, it’s not hard to prove that ˜ ˜ α(n) ≤ α(n) ≤ α(n) + 1 for all sufficiently large n, so the inaccuracy is completely forgivable. As I said in the previous footnote, the exact details of the definition are surprisingly irrelevant! 9 Algorithms Lecture 10: Disjoint Sets α(n) ≤ 3 for all even remotely imaginable values of n. Nevertheless, the function α(n) is eventually larger than any constant, so it is not O(1). 2 ↑c n n=1 2 n=3 n=4 n=5 2n 2 4 6 8 10 2↑n 2 4 8 16 32 2 ↑↑ n 2 4 16 65536 265536 2 ↑↑↑ n 2 ↑↑↑↑ n 2 ↑↑↑↑↑ n .2 .. 2 2 2 4 65536 22 4 4 22 .2 .. 2 2 2...2 2...2 . .. 2...2 2 65536 2 65536 22 2 .. 2. 2...2 2...2 2...2 . .. 65536 65536 22 2 2 .. 2. 22 .2 .. 2. 22 2 .. 65536 65536 〈〈Yeah, right.〉〉 65536 〈〈Very funny.〉〉 〈〈Argh! My eyes!〉〉 Small (!!) values of Ackermann’s functions. 10.7 To infinity. . . and beyond! Of course, one can generalize the inverse Ackermann function to functions that grow arbitrarily more slowly, starting with the iterated inverse Ackermann function α∗ (n) = if n ≤ 4 1 ∗ 1 + α (α(n)) otherwise, then the iterated iterated inverse Ackermann function α∗∗ (n) = if n ≤ 4 1 ∗∗ 1 + α (α(n)) otherwise, and then the diagonalized inverse Ackermann function c Head-asplode(n) = min{c ≥ 1 | α∗ n ≤ 4}, and so on forever. Fortunately(?), such functions appear extremely rarely in algorithm analysis. In fact, the only example (as far as Jeff knows) of a naturally-occurring super-constant sub-inverse-Ackermann function is a recent result of Seth Pettie6 , who proved that if a splay tree is used as a double-ended queue — insertions and deletions of only smallest or largest elements — then the amortized cost of any operation is O(α∗ (n)). Exercises 1. Consider the following solution for the union-find problem, called union-by-weight. Each set leader x stores the number of elements of its set in the field weight( x ). Whenever we UNION two sets, the leader of the smaller set becomes a new child of the leader of the larger set (breaking ties arbitrarily). 6 Splay trees, Davenport-Schinzel sequences, and the deque conjecture. Proceedings of the 19th Annual ACM-SIAM Symposium on Discrete Algorithms, 1115–1124, 2008. 10 Algorithms Lecture 10: Disjoint Sets MAKESET( x ): parent( x ) ← x weight( x ) ← 1 FIND( x ): while x = parent( x ) x ← parent( x ) return x UNION( x , y ) x ← FIND( x ) y ← FIND( y ) if weight( x ) > weight( y ) parent( y ) ← x weight( x ) ← weight( x ) + weight( y ) else parent( x ) ← y weight( x ) ← weight( x ) + weight( y ) Prove that if we use union-by-weight, the worst-case running time of FIND( x ) is O(log n), where n is the cardinality of the set containing x . 2. Consider a union-find data structure that uses union by depth (or equivalently union by rank) without path compression. For all integers m and n such that m ≥ 2n, prove that there is a sequence of n MakeSet operations, followed by m UNION and FIND operations, that require Ω(m log n) time to execute. 3. Consider an arbitrary sequence of m MAKESET operations, followed by u UNION operations, followed by f FIND operations, and let n = m + u + f . Prove that if we use union by rank and FIND with path compression, all n operations are executed in O(n) time. 4. Describe and analyze a data structure to support the following operations on an array X [1 .. n] as quickly as possible. Initially, X [i ] = 0 for all i . • Given an index i such that X [i ] = 0, set X [i ] to 1. • Given an index i , return X [i ]. • Given an index i , return the smallest index j ≥ i such that X [ j ] = 0, or report that no such index exists. For full credit, the first two operations should run in worst-case constant time, and the amortized cost of the third operation should be as small as possible. 5. (a) Describe and analyze an algorithm to compute the size of the largest connected component of black pixels in an n × n bitmap B [1 .. n, 1 .. n]. For example, given the bitmap below as input, your algorithm should return the number 9, because the largest conected black component (marked with white dots on the right) contains nine pixels. 9 (b) Design and analyze an algorithm BLACKEN(i , j ) that colors the pixel B [i , j ] black and returns the size of the largest black component in the bitmap. For full credit, the amortized running time of your algorithm (starting with an all-white bitmap) must be as small as possible. 11 Algorithms Lecture 10: Disjoint Sets For example, at each step in the sequence below, we blacken the pixel marked with an X. The largest black component is marked with white dots; the number underneath shows the correct output of the BLACKEN algorithm. 9 14 14 16 17 (c) What is the worst-case running time of your BLACKEN algorithm? 6. Consider the following game. I choose a positive integer n and keep it secret; your goal is to discover this integer. We play the game in rounds. In each round, you write a list of at most n integers on the blackboard. If you write more than n numbers in a single round, you lose. (Thus, in the first round, you must write only the number 1; do you see why?) If n is one of the numbers you wrote, you win the game; otherwise, I announce which of the numbers you wrote is smaller or larger than n, and we proceed to the next round. For example: You Me 1 It’s bigger than 1. 4, 42 It’s between 4 and 42. 8, 15, 16, 23, 30 It’s between 8 and 15. 9, 10, 11, 12, 13, 14 It’s 11; you win! Describe a strategy that allows you to win in O(α(n)) rounds! c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 12 Algorithms Non-Lecture G: String Matching Why are our days numbered and not, say, lettered? — Woody Allen G String Matching G.1 Brute Force The basic object that we’re going to talk about for the next two lectures is a string, which is really just an array. The elements of the array come from a set Σ called the alphabet; the elements themselves are called characters. Common examples are ASCII text, where each character is an seven-bit integer1 , strands of DNA, where the alphabet is the set of nucleotides {A, C , G , T }, or proteins, where the alphabet is the set of 22 amino acids. The problem we want to solve is the following. Given two strings, a text T [1 .. n] and a pattern P [1 .. m], find the first substring of the text that is the same as the pattern. (It would be easy to extend our algorithms to find all matching substrings, but we will resist.) A substring is just a contiguous subarray. For any shift s, let Ts denote the substring T [s .. s + m − 1]. So more formally, we want to find the smallest shift s such that Ts = P , or report that there is no match. For example, if the text is the string ‘AMANAPLANACATACANALPANAMA’2 and the pattern is ‘CAN’, then the output should be 15. If the pattern is ‘SPAM’, then the answer should be ‘none’. In most cases the pattern is much smaller than the text; to make this concrete, I’ll assume that m < n/2. Here’s the ‘obvious’ brute force algorithm, but with one immediate improvement. The inner while loop compares the substring Ts with P . If the two strings are not equal, this loop stops at the first character mismatch. ALMOSTBRUTEFORCE( T [1 .. n], P [1 .. m]): for s ← 1 to n − m + 1 equal ← true i←1 while equal and i ≤ m if T [s + i − 1] = P [i ] equal ← false else i ← i+1 if equal return s return ‘none’ 1 Yes, seven. Most computer systems use some sort of 8-bit character set, but there’s no universally accepted standard. Java supposedly uses the Unicode character set, which has variable-length characters and therefore doesn’t really fit into our framework. Just think, someday you’ll be able to write ‘¶ = ℵ[∞++]/ ;’ in your Java code! Joy! 2 Dan Hoey (or rather, his computer program) found the following 540-word palindrome in 1984: A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a pint, a catalpa, a gas, an oil, a bird, a yell, a vat, a caw, a pax, a wag, a tax, a nay, a ram, a cap, a yam, a gay, a tsar, a wall, a car, a luger, a ward, a bin, a woman, a vassal, a wolf, a tuna, a nit, a pall, a fret, a watt, a bay, a daub, a tan, a cab, a datum, a gall, a hat, a fag, a zap, a say, a jaw, a lay, a wet, a gallop, a tug, a trot, a trap, a tram, a torr, a caper, a top, a tonk, a toll, a ball, a fair, a sax, a minim, a tenor, a bass, a passer, a capital, a rut, an amen, a ted, a cabal, a tang, a sun, an ass, a maw, a sag, a jam, a dam, a sub, a salt, an axon, a sail, an ad, a wadi, a radian, a room, a rood, a rip, a tad, a pariah, a revel, a reel, a reed, a pool, a plug, a pin, a peek, a parabola, a dog, a pat, a cud, a nu, a fan, a pal, a rum, a nod, an eta, a lag, an eel, a batik, a mug, a mot, a nap, a maxim, a mood, a leek, a grub, a gob, a gel, a drab, a citadel, a total, a cedar, a tap, a gag, a rat, a manor, a bar, a gal, a cola, a pap, a yaw, a tab, a raj, a gab, a nag, a pagan, a bag, a jar, a bat, a way, a papa, a local, a gar, a baron, a mat, a rag, a gap, a tar, a decal, a tot, a led, a tic, a bard, a leg, a bog, a burg, a keel, a doom, a mix, a map, an atom, a gum, a kit, a baleen, a gala, a ten, a don, a mural, a pan, a faun, a ducat, a pagoda, a lob, a rap, a keep, a nip, a gulp, a loop, a deer, a leer, a lever, a hair, a pad, a tapir, a door, a moor, an aid, a raid, a wad, an alias, an ox, an atlas, a bus, a madam, a jag, a saw, a mass, an anus, a gnat, a lab, a cadet, an em, a natural, a tip, a caress, a pass, a baronet, a minimax, a sari, a fall, a ballot, a knot, a pot, a rep, a carrot, a mart, a part, a tort, a gut, a poll, a gateway, a law, a jay, a sap, a zag, a fat, a hall, a gamut, a dab, a can, a tabu, a day, a batt, a waterfall, a patina, a nut, a flow, a lass, a van, a mow, a nib, a draw, a regular, a call, a war, a stay, a gam, a yap, a cam, a ray, an ax, a tag, a wax, a paw, a cat, a valley, a drib, a lion, a saga, a plat, a catnip, a pooh, a rail, a calamus, a dairyman, a bater, a canal—Panama! Peter Norvig generated even longer examples in 2002; see http://norvig.com/palindrome.html. 1 Algorithms Non-Lecture G: String Matching In the worst case, the running time of this algorithm is O((n − m)m) = O(nm), and we can actually achieve this running time by searching for the pattern AAA...AAAB with m − 1 A’s, in a text consisting entirely of n A’s. In practice, though, breaking out of the inner loop at the first mismatch makes this algorithm quite practical. We can wave our hands at this by assuming that the text and pattern are both random. Then on average, we perform a constant number of comparisons at each position i , so the total expected number of comparisons is O(n). Of course, neither English nor DNA is really random, so this is only a heuristic argument. G.2 Strings as Numbers For the rest of the lecture, let’s assume that the alphabet consists of the numbers 0 through 9, so we can interpret any array of characters as either a string or a decimal number. In particular, let p be the numerical value of the pattern P , and for any shift s, let t s be the numerical value of Ts : m m 10m−i · P [i ] p= 10m−i · T [s + i − 1] ts = i =1 i =1 For example, if T = 31415926535897932384626433832795028841971 and m = 4, then t 17 = 2384. Clearly we can rephrase our problem as follows: Find the smallest s, if any, such that p = t s . We can compute p in O(m) arithmetic operations, without having to explicitly compute powers of ten, using Horner’s rule: p = P [m] + 10 P [m − 1] + 10 P [m − 2] + · · · + 10 P [2] + 10 · P [1] · · · We could also compute any t s in O(m) operations using Horner’s rule, but this leads to essentially the same brute-force algorithm as before. But once we know t s , we can actually compute t s+1 in constant time just by doing a little arithmetic — subtract off the most significant digit T [s] · 10m−1 , shift everything up by one digit, and add the new least significant digit T [ r + m]: t s+1 = 10 t s − 10m−1 · T [s] + T [s + m] To make this fast, we need to precompute the constant 10m−1 . (And we know how to do that quickly. Right?) So it seems that we can solve the string matching problem in O(n) worst-case time using the following algorithm: NUMBERSEARCH( T [1 .. n], P [1 .. m]): σ ← 10m−1 p←0 t1 ← 0 for i ← 1 to m p ← 10 · p + P [i ] t 1 ← 10 · t 1 + T [i ] for s ← 1 to n − m + 1 if p = t s return s t s+1 ← 10 · t s − σ · T [s] + T [s + m] return ‘none’ Unfortunately, the most we can say is that the number of arithmetic operations is O(n). These operations act on numbers with up to m digits. Since we want to handle arbitrarily long patterns, we can’t assume that each operation takes only constant time! 2 Algorithms G.3 Non-Lecture G: String Matching Karp-Rabin Fingerprinting To make this algorithm efficient, we will make one simple change, discovered by Richard Karp and Michael Rabin in 1981: Perform all arithmetic modulo some prime number q. We choose q so that the value 10q fits into a standard integer variable, so that we don’t need any fancy long-integer data types. The values ( p mod q) and ( t s mod q) are called the fingerprints of P and Ts , respectively. We can now compute ( p mod q) and ( t 1 mod q) in O(m) time using Horner’s rule ‘mod q’ p mod q = P [m] + · · · + 10 · P [2] + 10 · P [1] mod q mod q mod q · · · mod q and similarly, given ( t s mod q), we can compute ( t s+1 mod q) in constant time. t s+1 mod q = 10 · t s − 10m−1 mod q · T [s] mod q mod q mod q + T [s + m] mod q Again, we have to precompute the value (10m−1 mod q) to make this fast. If ( p mod q) = ( t s mod q), then certainly P = Ts . However, if ( p mod q) = ( t s mod q), we can’t tell whether P = Ts or not. All we know for sure is that p and t s differ by some integer multiple of q. If P = Ts in this case, we say there is a false match at shift s. To test for a false match, we simply do a brute-force string comparison. (In the algorithm below, ˜ = p mod q and ˜s = t s mod q.) p t KARPRABIN( T [1 .. n], P [1 .. m]: choose a small prime q σ ← 10m−1 mod q ˜←0 p ˜1 ← 0 t for i ← 1 to m ˜ ← (10 · ˜ mod q) + P [i ] mod q p p ˜1 ← (10 · ˜1 mod q) + T [i ] mod q t t for s ← 1 to n − m + 1 if ˜ = ˜s pt if P = Ts 〈〈brute-force O(m)-time comparison〉〉 return s ˜s+1 ← 10 · ˜s − σ · T [s] mod q mod q mod q + T [s + m] mod q t t return ‘none’ The running time of this algorithm is O(n + F m), where F is the number of false matches. Intuitively, we expect the fingerprints t s to jump around between 0 and q − 1 more or less at random, so the ‘probability’ of a false match ‘ought’ to be 1/q. This intuition implies that F = n/q ‘on average’, which gives us an ‘expected’ running time of O(n + nm/q). If we always choose q ≥ m, this simplifies to O(n). But of course all this intuitive talk of probabilities is just frantic meaningless handwaving, since we haven’t actually done anything random yet. G.4 Random Prime Number Facts The real power of the Karp-Rabin algorithm is that by choosing the modulus q randomly, we can actually formalize this intuition! The first line of KARPRABIN should really read as follows: Let q be a random prime number less than nm2 log(nm2 ). 3 Algorithms Non-Lecture G: String Matching For any positive integer u, let π(u) denote the number of prime numbers less than u. There are π(nm2 log nm2 ) possible values for q, each with the same probability of being chosen. Our analysis needs two results from number theory. I won’t even try to prove the first one, but the second one is quite easy. Lemma 1 (The Prime Number Theorem). π(u) = Θ(u/ log u). Lemma 2. Any integer x has at most lg x distinct prime divisors. Proof: If x has k distinct prime divisors, then x ≥ 2k , since every prime number is bigger than 1. Let’s assume that there are no true matches, so p = t s for all s. (That’s the worst case for the algorithm anyway.) Let’s define a strange variable X as follows: n−m+1 X= p − ts . s =1 Notice that by our assumption, X can’t be zero. Now suppose we have false match at shift s. Then p mod q = t s mod q, so p − t s is an integer multiple of q, and this implies that X is also an integer multiple of q. In other words, if there is a false match, then q must one of the prime divisors of X . Since p < 10m and t s < 10m , we must have X < 10nm . Thus, by the second lemma, X has O(mn) prime divisors. Since we chose q randomly from a set of π(nm2 log(nm2 )) = Ω(nm2 ) prime numbers, the probability that q divides X is at most O(nm) Ω(nm2 ) =O 1 m . We have just proven the following amazing fact. The probability of getting a false match is O(1/m). Recall that the running time of KARPRABIN is O(n + mF ), where F is the number of false matches. By using the really loose upper bound E[ F ] ≤ Pr[ F > 0] · n, we can conclude that the expected number of false matches is O(n/m). Thus, the expected running time of the KARPRABIN algorithm is O(n). G.5 Random Prime Number? Actually choosing a random prime number is not particularly easy. The best method known is to repeatedly generate a random integer and test to see if it’s prime. In practice, it’s enough to choose a random probable prime. You can read about probable primes in the lecture notes on number-theoretic algorithms, or in the textbook Randomized Algorithms by Rajeev Motwani and Prabhakar Raghavan (Cambridge, 1995). Exercises 1. Describe and analyze a two-dimensional variant of KARPRABIN that searches for a given twodimensional pattern P [1 .. p][1 .. q] within a given two-dimensional ‘text’ T [1 .. m][1 .., n]. Your algorithm should report all index pairs (i , j ) such that the subarray T [i .. i + p − 1][ j .. j + q − 1] is identical to the given pattern, in O( pq + mn) expected time. 4 Algorithms Non-Lecture G: String Matching 2. Describe and analyze a variant of KARPRABIN that looks for strings inside labeled rooted trees. The input consists of a pattern string P [1 .. m] and a rooted text tree T with n nodes, each labeled with a single character. Nodes in T can have any number of children. Your algorithm should either return a downward path in T whose labels match the string P , or report that there is no such path. The expected running time of your algorithm should be O(m + n). P H E I S A N A Z R E E L M A R C W M Q S O K H F The string SEARCH appears on a downward path in the tree. 3. Describe and analyze a variant of KARPRABIN that searches for subtrees of ordered rooted binary trees (every node has a left subtree and a right subtree, either or both of which may be empty). The input consists of a pattern tree P with m nodes and a text tree T with n nodes. Your algorithm should report all nodes v in T such that the subtree rooted at v is structurally identical to P . The expected running time of your algorithm should be O(m + n). Ignore all search keys, labels, or other data in the nodes; only the left/right pointer structure matters. The pattern tree (left) appears exactly twice in the text tree (right). 4. How important is the requirement that the fingerprint modulus q is prime? Specifically, suppose q is chosen uniformly at random in the range 1 .. N . If t s = p, what is the probability that ˜s = ˜ ? t p What does this imply about the expected number of false matches? How large should N be to guarantee expected running time O(m + n)? [Hint: This will require some additional number theory.] 5 Algorithms Non-Lecture H: More String Matching Philosophers gathered from far and near To sit at his feat and hear and hear, Though he never was heard To utter a word But “Abracadabra, abracadab, Abracada, abracad, Abraca, abrac, abra, ab!” ’Twas all he had, ’Twas all they wanted to hear, and each Made copious notes of the mystical speech, Which they published next – A trickle of text In the meadow of commentary. Mighty big books were these, In a number, as leaves of trees; In learning, remarkably – very! — Jamrach Holobom, quoted by Ambrose Bierce, The Devil’s Dictionary (1911) H H.1 More String Matching Redundant Comparisons Let’s go back to the character-by-character method for string matching. Suppose we are looking for the pattern ‘ABRACADABRA’ in some longer text using the (almost) brute force algorithm described in the previous lecture. Suppose also that when s = 11, the substring comparison fails at the fifth position; the corresponding character in the text (just after the vertical line below) is not a C. At this point, our algorithm would increment s and start the substring comparison from scratch. HOCUSPOCUSABRA BRACADABRA... ABRA /ADABRA C ABR ACADABRA If we look carefully at the text and the pattern, however, we should notice right away that there’s no point in looking at s = 12. We already know that the next character is a B — after all, it matched P [2] during the previous comparison — so why bother even looking there? Likewise, we already know that the next two shifts s = 13 and s = 14 will also fail, so why bother looking there? HOCUSPOCUSABRA BRACADABRA... ABRA /ADABRA C A /BR ACADABRA A /B RACADABRA A BRACADABRA Finally, when we get to s = 15, we can’t immediately rule out a match based on earlier comparisons. However, for precisely the same reason, we shouldn’t start the substring comparison over from scratch — we already know that T [15] = P [4] = A. Instead, we should start the substring comparison at the second character of the pattern, since we don’t yet know whether or not it matches the corresponding text character. If you play with this idea long enough, you’ll notice that the character comparisons should always advance through the text. Once we’ve found a match for a text character, we never need to do another comparison with that text character again. In other words, we should be able to optimize the brute-force algorithm so that it always advances through the text. 1 Algorithms Non-Lecture H: More String Matching You’ll also eventually notice a good rule for finding the next ‘reasonable’ shift s. A prefix of a string is a substring that includes the first character; a suffix is a substring that includes the last character. A prefix or suffix is proper if it is not the entire string. Suppose we have just discovered that T [i ] = P [ j ]. The next reasonable shift is the smallest value of s such that T [ s .. i − 1], which is a suffix of the previously-read text, is also a proper prefix of the pattern. In this lecture, we’ll describe a string matching algorithm, published by Donald Knuth, James Morris, and Vaughn Pratt in 1977, that implements both of these ideas. H.2 Finite State Machines If we have a string matching algorithm that follows our first observation (that we always advance through the text), we can interpret it as feeding the text through a special type of finite-state machine. A finite state machine is a directed graph. Each node in the graph, or state, is labeled with a character from the pattern, except for two special nodes labeled $ and ! . Each node has two outgoing edges, a success edge and a failure edge. The success edges define a path through the characters of the pattern in order, starting at $ and ending at ! . Failure edges always point to earlier characters in the pattern. ! $ A B A R R A B C A D A A finite state machine for the string ‘ABRADACABRA’. Thick arrows are the success edges; thin arrows are the failure edges. We use the finite state machine to search for the pattern as follows. At all times, we have a current text character T [i ] and a current node in the graph, which is usually labeled by some pattern character P [ j ]. We iterate the following rules: • If T [i ] = P [ j ], or if the current label is $ , follow the success edge to the next node and increment i . (So there is no failure edge from the start node $ .) • If T [i ] = P [ j ], follow the failure edge back to an earlier node, but do not change i . For the moment, let’s simply assume that the failure edges are defined correctly—we’ll come back to this later. If we ever reach the node labeled ! , then we’ve found an instance of the pattern in the text, and if we run out of text characters (i > n) before we reach ! , then there is no match. The finite state machine is really just a (very!) convenient metaphor. In a real implementation, we would not construct the entire graph. Since the success edges always go through the pattern characters in order, we only have to remember where the failure edges go. We can encode this failure function in an array fail[1 .. n], so that for each j there is a failure edge from node j to node fail[ j ]. Following a failure edge back to an earlier state exactly corresponds, in our earlier formulation, to shifting the pattern forward. The failure function fail[ j ] tells us how far to shift after a character mismatch T [i ] = P [ j ]. Here’s what the actual algorithm looks like: 2 Algorithms Non-Lecture H: More String Matching KNUTHMORRISPRATT( T [1 .. n], P [1 .. m]): j←1 for i ← 1 to n while j > 0 and T [i ] = P [ j ] j ← fail[ j ] if j = m 〈〈Found it!〉〉 return i − m + 1 j ← j+1 return ‘none’ Before we discuss computing the failure function, let’s analyze the running time of KNUTHMORRISPRATT under the assumption that a correct failure function is already known. At each character comparison, either we increase i and j by one, or we decrease j and leave i alone. We can increment i at most n − 1 times before we run out of text, so there are at most n − 1 successful comparisons. Similarly, there can be at most n − 1 failed comparisons, since the number of times we decrease j cannot exceed the number of times we increment j . In other words, we can amortize character mismatches against earlier character matches. Thus, the total number of character comparisons performed by KNUTHMORRISPRATT in the worst case is O(n). H.3 Computing the Failure Function We can now rephrase our second intuitive rule about how to choose a reasonable shift after a character mismatch T [i ] = P [ j ]: P [1 .. fail[ j ] − 1] is the longest proper prefix of P [1 .. j − 1] that is also a suffix of T [1 .. i − 1]. Notice, however, that if we are comparing T [i ] against P [ j ], then we must have already matched the first j − 1 characters of the pattern. In other words, we already know that P [1 .. j − 1] is a suffix of T [1 .. i − 1]. Thus, we can rephrase the prefix-suffix rule as follows: P [1 .. fail[ j ] − 1] is the longest proper prefix of P [1 .. j − 1] that is also a suffix of P [1 .. j − 1]. This is the definition of the Knuth-Morris-Pratt failure function fail[ j ] for all j > 1.1 By convention we set fail[1] = 0; this tells the KMP algorithm that if the first pattern character doesn’t match, it should just give up and try the next text character. P [i ] fail[i ] A B R A C A D A B R A 0 1 1 1 2 1 2 1 2 3 4 Failure function for the string ‘ABRACADABRA’ (Compare with the finite state machine on the previous page.) We could easily compute the failure function in O(m3 ) time by checking, for each j , whether every prefix of P [1 .. j − 1] is also a suffix of P [1 .. j − 1], but this is not the fastest method. The following algorithm essentially uses the KMP search algorithm to look for the pattern inside itself! 1 Many algorithms textbooks, including CLRS, define a similar prefix function, denoted π[ j ], as follows: P [1 .. π[ j ]] is the longest proper prefix of P [1 .. j ] that is also a suffix of P [1 .. j ]. These two functions are not the same, but they are related by the simple equation π[ j ] = fail[ j + 1] − 1. The off-by-one difference between the two functions adds a few extra +1s to the CLRS version of the algorithm. 3 Algorithms Non-Lecture H: More String Matching COMPUTEFAILURE( P [1 .. m]): j←0 for i ← 1 to m fail[i ] ← j (∗) while j > 0 and P [i ] = P [ j ] j ← fail[ j ] j ← j+1 Here’s an example of this algorithm in action. In each line, the current values of i and j are indicated by superscripts; $ represents the beginning of the string. (You should imagine pointing at P [ j ] with your left hand and pointing at P [i ] with your right hand, and moving your fingers according to the algorithm’s directions.) j ← 0, i ← 1 fail[i ] ← j j ← j + 1, i ← i + 1 fail[i ] ← j j ← fail[ j ] j ← j + 1, i ← i + 1 fail[i ] ← j j ← fail[ j ] j ← j + 1, i ← i + 1 fail[i ] ← j j ← j + 1, i ← i + 1 fail[i ] ← j j ← fail[ j ] j ← fail[ j ] j ← j + 1, i ← i + 1 fail[i ] ← j j ← j + 1, i ← i + 1 fail[i ] ← j j ← fail[ j ] j ← fail[ j ] j ← j + 1, i ← i + 1 fail[i ] ← j j ← j + 1, i ← i + 1 fail[i ] ← j j ← j + 1, i ← i + 1 fail[i ] ← j j ← j + 1, i ← i + 1 fail[i ] ← j j ← fail[ j ] j ← fail[ j ] $j Ai B R A C A D A B R X ... R A C A D A B R X ... R A C A D A B R X ... A C A D A B R X ... A C A D A B R X ... C A D A B R X ... Ci A D A B R X ... A D A B R X ... 0 ... $ Aj Bi 0 1 $j A Bi ... $j Aj B Ri 0 $ 1 1 A B Ri ... $ Aj B R Ai 0 $ 1 1 1 A Bj R A ... 0 1 1 1 2 $ Aj B R A Ci $j A B R A Ci A D A B R X ... $ Aj B R A C Ai D A B R X ... 0 1 1 1 2 1 A Bj R A C A Di A B R X ... A B R X ... $ ... ... 0 1 1 1 2 1 2 $ Aj B R A C A Di $j A B R A C A Di A B R X ... $ Aj B R A C A D Ai B R X ... 0 1 1 1 2 1 2 1 A Bj R A C A D A Bi R X ... 0 1 1 1 2 1 2 1 2 A B Rj A C A D A B Ri X ... 0 1 1 1 2 1 2 1 2 3 ... A B R Aj C A D A B R Xi . . . 4 ... $ $ $ ... ... ... 0 1 1 1 2 1 2 1 2 3 $ Aj B R A C A D A B R Xi . . . $j A B R A C A D A B R Xi . . . ComputeFailure in action. Do this yourself by hand! Just as we did for KNUTHMORRISPRATT, we can analyze COMPUTEFAILURE by amortizing character mismatches against earlier character matches. Since there are at most m character matches, COMPUTEFAILURE runs in O(m) time. Let’s prove (by induction, of course) that COMPUTEFAILURE correctly computes the failure function. The base case fail[1] = 0 is obvious. Assuming inductively that we correctly computed fail[1] through fail[i ] in line (∗), we need to show that fail[i + 1] is also correct. Just after the i th iteration of line (∗), we have j = fail[i ], so P [1 .. j − 1] is the longest proper prefix of P [1 .. i − 1] that is also a suffix. Let’s define the iterated failure functions failc [ j ] inductively as follows: fail0 [ j ] = j , and c c fail [ j ] = fail[ fail c −1 [ j ]] = fail[ fail[· · · [ fail[ j ]] · · · ]]. 4 Algorithms Non-Lecture H: More String Matching In particular, if failc −1 [ j ] = 0, then failc [ j ] is undefined. We can easily show by induction that every string of the form P [1 .. failc [ j ] − 1] is both a proper prefix and a proper suffix of P [1 .. i − 1], and in fact, these are the only examples. Thus, the longest proper prefix/suffix of P [1 .. i ] must be the longest string of the form P [1 .. failc [ j ]] — that is, the one with smallest c — such that P [ failc [ j ]] = P [i ]. This is exactly what the while loop in COMPUTEFAILURE computes; the (c + 1)th iteration compares P [ failc [ j ]] = P [ failc +1 [i ]] against P [i ]. COMPUTEFAILURE is actually a dynamic programming implementation of the following recursive definition of fail[i ]: fail[i ] = if i = 0, 0 c c max fail [i − 1] + 1 P [i − 1] = P [ fail [i − 1]] c ≥1 H.4 otherwise. Optimizing the Failure Function We can speed up KNUTHMORRISPRATT slightly by making one small change to the failure function. Recall that after comparing T [i ] against P [ j ] and finding a mismatch, the algorithm compares T [i ] against P [ fail[ j ]]. With the current definition, however, it is possible that P [ j ] and P [ fail[ j ]] are actually the same character, in which case the next character comparison will automatically fail. So why do the comparison at all? We can optimize the failure function by ‘short-circuiting’ these redundant comparisons with some simple post-processing: OPTIMIZEFAILURE( P [1 .. m], fail[1 .. m]): for i ← 2 to m if P [i ] = P [ fail[i ]] fail[i ] ← fail[ fail[i ]] We can also compute the optimized failure function directly by adding three new lines (in bold) to the COMPUTEFAILURE function. COMPUTEOPTFAILURE( P [1 .. m]): j←0 for i ← 1 to m if P [i ] = P [ j ] fail[i ] ← fail[ j ] else fail[i ] ← j while j > 0 and P [i ] = P [ j ] j ← fail[ j ] j ← j+1 This optimization slows down the preprocessing slightly, but it may significantly decrease the number of comparisons at each text character. The worst-case running time is still O(n); however, the constant is about half as big as for the unoptimized version, so this could be a significant improvement in practice. 5 Algorithms Non-Lecture H: More String Matching ! $ A B A R R A B C A A D Optimized finite state machine for the string ‘ABRADACABRA’ P [i ] fail[i ] A B R A C A D A B R A 0 1 1 0 2 0 2 0 1 1 0 Optimized failure function for ‘ABRACADABRA’, with changes in bold. Here are the unoptimized and optimized failure functions for a few more patterns: P [i ] A N A N A B A N A N A N A unoptimized fail[i ] 0 1 1 2 3 4 1 2 3 4 5 6 5 optimized fail[i ] 0 1 0 1 0 4 0 1 0 1 0 6 0 Failure functions for ‘ANANABANANANA’. P [i ] A B A B C A B A B C A B C unoptimized fail[i ] 0 1 1 2 3 1 2 3 4 5 6 7 8 optimized fail[i ] 0 1 0 1 3 0 1 0 1 3 0 1 8 Failure functions for ‘ABABCABABCABC’. P [i ] A B B A B B A B A B B A B unoptimized fail[i ] 0 1 1 1 2 3 4 5 6 2 3 4 5 optimized fail[i ] 0 1 1 0 1 1 0 1 6 1 1 0 1 Failure functions for ‘ABBABBABABBAB’. Exercises 1. A palindrome is any string that is the same as its reversal, such as X, ABBA, or REDIVIDER. Describe and analyze an algorithm that computes the longest palindrome that is a (not necessarily proper) prefix of a given string T [1 .. n]. Your algorithm should run in O(n) time. 2. Describe a modification of KNUTHMORRISPRATT in which the pattern can contain any number of wildcard symbols * , each of which matches an arbitrary string. For example, the pattern AIN ABR* CAD* BRA appears in the text SCHABRAIN AINCADBRANCH; in this case, the second * matches the empty string. Your algorithm should run in O(m + n) time, where m is the length of the pattern and n is the length of the text. 3. Describe a modification of KNUTHMORRISPRATT in which the pattern can contain any number of wildcard symbols ? , each of which matches an arbitrary single character. For example, the pattern ? ? U I ABR? CAD? BRA appears in the text SCHABRU CADI BRANCH. Your algorithm should run in O(m + qn) time, where m is the length of the pattern, n is the length of the text., and q is the number of ? s in the pattern. 6 Algorithms Non-Lecture H: More String Matching 4. Describe another algorithm for the previous problem that runs in time O(m + kn), where k is the number of runs of consecutive non-wildcard characters in the pattern. For example, the pattern ??? ??IS???? ? ? FISH??? ?? ???? ???B?? ????CUIT? has k = 4 runs. 5. Describe a modification of KNUTHMORRISPRATT in which the pattern can contain any number of wildcard symbols = , each of which matches the same arbitrary single character. For example, the = = U U U A A A pattern = HOC= SPOC= S appears in the texts WHU HOCU SPOCU SOT and ABRA HOCA SPOCA SCADABRA, but SHOCU SPOCE STIX. Your algorithm should run in O(m + n) time, where m is the U E not in the text FRIS length of the pattern and n is the length of the text. 6. Describe and analyze a variant of KNUTHMORRISPRATT that looks for strings inside labeled rooted trees. The input consists of a pattern string P [1 .. m] and a rooted text tree T with n nodes, each labeled with a single character. Nodes in T can have any number of children. Your algorithm should either return a downward path in T whose labels match the string P , or report that there is no such path. Your algorithm should run in O(m + n) time. P H E I S A N A Z R E E L M A R C W M Q S O K H F The string SEARCH appears on a downward path in the tree. 7. Describe and analyze a variant of KNUTHMORRISPRATT that searches for subtrees of ordered rooted binary trees (every node has a left subtree and a right subtree, either or both of which may be empty). The input consists of a pattern tree P with m nodes and a text tree T with n nodes. Your algorithm should report all nodes v in T such that the subtree rooted at v is structurally identical to P . Your algorithm should run in O(m + n) time. Ignore all search keys, labels, or other data in the nodes; only the left/right pointer structure matters. The pattern tree (left) appears exactly twice in the text tree (right). 7 Algorithms Non-Lecture H: More String Matching 8. This problem considers the maximum length of a failure chain j → fail[ j ] → fail[ fail[ j ]] → fail[ fail[ fail[ j ]]] → · · · → 0, or equivalently, the maximum number of iterations of the inner loop of KNUTHMORRISPRATT. This clearly depends on which failure function we use: unoptimized or optimized. Let m be an arbitrary positive integer. (a) Describe a pattern A[1 .. m] whose longest unoptimized failure chain has length m. (b) Describe a pattern B [1 .. m] whose longest optimized failure chain has length Θ(log m). (c) Describe a pattern C [1 .. m] containing only two different characters, whose longest optimized failure chain has length Θ(log m). (d) Prove that for any pattern of length m, the longest optimized failure chain has length at most O(log m). 8 Algorithms Lecture 11: Basic Graph Properties Obie looked at the seein’ eye dog. Then at the twenty-seven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on the back of each one. . . and then he looked at the seein’ eye dog. And then at the twenty-seven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on the back of each one and began to cry. Because Obie came to the realization that it was a typical case of American blind justice, and there wasn’t nothin’ he could do about it, and the judge wasn’t gonna look at the twentyseven 8 by 10 color glossy pictures with the circles and arrows and a paragraph on the back of each one explainin’ what each one was, to be used as evidence against us. And we was fined fifty dollars and had to pick up the garbage. In the snow. But that’s not what I’m here to tell you about. — Arlo Guthrie, “Alice’s Restaurant” (1966) I study my Bible as I gather apples. First I shake the whole tree, that the ripest might fall. Then I climb the tree and shake each limb, and then each branch and then each twig, and then I look under each leaf. — Martin Luther 11 11.1 Basic Graph Properties Definitions A graph G is a pair of sets (V, E ). V is a set of arbitrary objects that we call vertices1 or nodes. E is a set of vertex pairs, which we call edges or occasionally arcs. In an undirected graph, the edges are unordered pairs, or just sets of two vertices. In a directed graph, the edges are ordered pairs of vertices. We will only be concerned with simple graphs, where there is no edge from a vertex to itself and there is at most one edge from any vertex to any other. Following standard (but admittedly confusing) practice, I’ll also use V to denote the number of vertices in a graph, and E to denote the number of edges. Thus, in an undirected graph, we have V 0 ≤ E ≤ 2 , and in a directed graph, 0 ≤ E ≤ V (V − 1). We usually visualize graphs by looking at an embedding. An embedding of a graph maps each vertex to a point in the plane and each edge to a curve or straight line segment between the two vertices. A graph is planar if it has an embedding where no two edges cross. The same graph can have many different embeddings, so it is important not to confuse a particular embedding with the graph itself. In particular, planar graphs can have non-planar embeddings! c b d h a a f c e e d f g h g i b i A non-planar embedding of a planar graph with nine vertices, thirteen edges, and two connected components, and a planar embedding of the same graph. 1 The singular of ‘vertices’ is vertex. The singular of ‘matrices’ is matrix. Unless you’re speaking Italian, there is no such thing as a vertice, a matrice, an indice, an appendice, a helice, an apice, a vortice, a radice, a simplice, a codice, a directrice, a dominatrice, a Unice, a Kleenice, an Asterice, an Obelice, a Dogmatice, a Getafice, a Cacofonice, a Vitalstatistice, a Geriatrice, or Jimi Hendrice! You will lose points for using any of these so-called words. 1 Algorithms Lecture 11: Basic Graph Properties There are other ways of visualizing and representing graphs that are sometimes also useful. For example, the intersection graph of a collection of objects has a node for every object and an edge for every intersecting pair. Whether a particular graph can be represented as an intersection graph depends on what kind of object you want to use for the vertices. Different types of objects—line segments, rectangles, circles, etc.—define different classes of graphs. One particularly useful type of intersection graph is an interval graph, whose vertices are intervals on the real line, with an edge between any two intervals that overlap. f d e a g b h b d a c h fg i c (a) e i a (b) bc de i gh f (c) The example graph is also the intersection graph of (a) a set of line segments, (b) a set of circles, or (c) a set of intervals on the real line (stacked for visibility). If (u, v ) is an edge in an undirected graph, then u is a neighbor or v and vice versa. The degree of a node is the number of neighbors. In directed graphs, we have two kinds of neighbors. If u → v is a directed edge, then u is a predecessor of v and v is a successor of u. The in-degree of a node is the number of predecessors, which is the same as the number of edges going into the node. The out-degree is the number of successors, or the number of edges going out of the node. A graph G = (V , E ) is a subgraph of G = (V, E ) if V ⊆ V and E ⊆ E . A path is a sequence of edges, where each successive pair of edges shares a vertex, and all other edges are disjoint. A graph is connected if there is a path from any vertex to any other vertex. A disconnected graph consists of several connected components, which are maximal connected subgraphs. Two vertices are in the same connected component if and only if there is a path between them. A cycle is a path that starts and ends at the same vertex, and has at least one edge. A graph is acyclic if no subgraph is a cycle; acyclic graphs are also called forests. Trees are special graphs that can be defined in several different ways. You can easily prove by induction (hint, hint, hint) that the following definitions are equivalent. • A tree is a connected acyclic graph. • A tree is a connected component of a forest. • A tree is a connected graph with at most V − 1 edges. • A tree is a minimal connected graph; removing any edge makes the graph disconnected. • A tree is an acyclic graph with at least V − 1 edges. • A tree is a maximal acyclic graph; adding an edge between any two vertices creates a cycle. A spanning tree of a graph G is a subgraph that is a tree and contains every vertex of G . Of course, a graph can only have a spanning tree if it’s connected. A spanning forest of G is a collection of spanning trees, one for each connected component of G . 2 Algorithms 11.2 Lecture 11: Basic Graph Properties Explicit Representations of Graphs There are two common data structures used to explicitly represent graphs: adjacency matrices2 and adjacency lists. The adjacency matrix of a graph G is a V × V matrix of indicator variables. Each entry in the matrix indicates whether a particular edge is or is not in the graph: A[i , j ] = (i , j ) ∈ E . For undirected graphs, the adjacency matrix is always symmetric: A[i , j ] = A[ j , i ]. Since we don’t allow edges from a vertex to itself, the diagonal elements A[i , i ] are all zeros. Given an adjacency matrix, we can decide in Θ(1) time whether two vertices are connected by an edge just by looking in the appropriate slot in the matrix. We can also list all the neighbors of a vertex in Θ(V ) time by scanning the corresponding row (or column). This is optimal in the worst case, since a vertex can have up to V − 1 neighbors; however, if a vertex has few neighbors, we may still have to examine every entry in the row to see them all. Similarly, adjacency matrices require Θ(V 2 ) space, regardless of how many edges the graph actually has, so it is only space-efficient for very dense graphs. a b c d e f g h i abcde f ghi 01100 0 000 10111 0 000 11011 0 000 01101 1 000 01110 1 000 00011 0 000 00000 0 010 00000 0 101 00000 0 110 a b c d e f g h i b a a b b d h g g c c b c c e i i h d d e d e e f f Adjacency matrix and adjacency list representations for the example graph. For sparse graphs—graphs with relatively few edges—we’re better off using adjacency lists. An adjacency list is an array of linked lists, one list per vertex. Each linked list stores the neighbors of the corresponding vertex. For undirected graphs, each edge (u, v ) is stored twice, once in u’s neighbor list and once in v ’s neighbor list; for directed graphs, each edge is stores only once. Either way, the overall space required for an adjacency list is O(V + E ). Listing the neighbors of a node v takes O(1 + deg( v )) time; just scan the neighbor list. Similarly, we can determine whether (u, v ) is an edge in O(1 + deg(u)) time by scanning the neighbor list of u. For undirected graphs, we can speed up the search by simultaneously scanning the neighbor lists of both u and v , stopping either we locate the edge or when we fall of the end of a list. This takes O(1 + min{deg(u), deg( v )}) time. The adjacency list structure should immediately remind you of hash tables with chaining. Just as with hash tables, we can make adjacency list structure more efficient by using something besides a linked list to store the neighbors. For example, if we use a hash table with constant load factor, when we can detect edges in O(1) expected time, just as with an adjacency list. In practice, this will only be useful for vertices with large degree, since the constant overhead in both the space and search time is larger for hash tables than for simple linked lists. You might at this point ask why anyone would ever use an adjacency matrix. After all, if you use hash tables to store the neighbors of each vertex, you can do everything as fast or faster with an adjacency list as with an adjacency matrix, only using less space. The answer is that many graphs are only represented 2 See footnote 1. 3 Algorithms Lecture 11: Basic Graph Properties implicitly. For example, intersection graphs are usually represented implicitly by simply storing the list of objects. As long as we can test whether two objects overlap in constant time, we can apply any graph algorithm to an intersection graph by pretending that it is stored explicitly as an adjacency matrix. On the other hand, any data structure build from records with pointers between them can be seen as a directed graph. Algorithms for searching graphs can be applied to these data structures by pretending that the graph is represented explicitly using an adjacency list. To keep things simple, we’ll consider only undirected graphs for the rest of this lecture, although the algorithms I’ll describe also work for directed graphs. 11.3 Traversing connected graphs Suppose we want to visit every node in a connected graph (represented either explicitly or implicitly). The simplest method to do this is an algorithm called depth-first search, which can be written either recursively or iteratively. It’s exactly the same algorithm either way; the only difference is that we can actually see the ‘recursion’ stack in the non-recursive version. Both versions are initially passed a source vertex s. ITERATIVEDFS(s): PUSH(s) while stack not empty v ← POP if v is unmarked mark v for each edge ( v, w ) PUSH(w ) RECURSIVEDFS( v ): if v is unmarked mark v for each edge ( v, w ) RECURSIVEDFS(w ) Depth-first search is one (perhaps the most common) instance of a general family of graph traversal algorithms. The generic graph traversal algorithm stores a set of candidate edges in some data structure that I’ll call a ‘bag’. The only important properties of a ‘bag’ are that we can put stuff into it and then later take stuff back out. (In C++ terms, think of the ‘bag’ as a template for a real data structure.) Here’s the algorithm: TRAVERSE(s): put (∅, s) in bag while the bag is not empty take ( p, v ) from the bag () if v is unmarked mark v parent( v ) ← p for each edge ( v, w ) (†) put ( v, w ) into the bag ( ) Notice that we’re keeping edges in the bag instead of vertices. This is because we want to remember, whenever we visit a vertex v for the first time, which previously-visited vertex p put v into the bag. The vertex p is called the parent of v . Lemma 1. TRAVERSE(s) marks every vertex in any connected graph exactly once, and the set of edges ( v, parent( v )) with parent( v ) = ∅ form a spanning tree of the graph. Proof: first, it should be obvious that no node is marked more than once. Clearly, the algorithm marks s. Let v = s be a vertex, and let s → · · · → u → v be the path from s to v with the minimum number of edges. Since the graph is connected, such a path always exists. (If s and v 4 Algorithms Lecture 11: Basic Graph Properties are neighbors, then u = s, and the path has just one edge.) If the algorithm marks u, then it must put (u, v ) into the bag, so it must later take (u, v ) out of the bag, at which point v must be marked (if it isn’t already). Thus, by induction on the shortest-path distance from s, the algorithm marks every vertex in the graph. Call an edge ( v, parent( v )) with parent( v ) = ∅ a parent edge. For any node v , the path of parent edges v → parent( v ) → parent(parent( v )) → · · · eventually leads back to s, so the set of parent edges form a connected graph. Clearly, both endpoints of every parent edge are marked, and the number of parent edges is exactly one less than the number of vertices. Thus, the parent edges form a spanning tree. The exact running time of the traversal algorithm depends on how the graph is represented and what data structure is used as the ‘bag’, but we can make a few general observations. Since each vertex is visited at most once, the for loop (†) is executed at most V times. Each edge is put into the bag exactly twice; once as (u, v ) and once as ( v, u), so line ( ) is executed at most 2 E times. finally, since we can’t take more things out of the bag than we put in, line ( ) is executed at most 2 E + 1 times. 11.4 Examples Let’s first assume that the graph is represented by an adjacency list, so that the overhead of the for loop (†) is only a constant per edge. • If we implement the ‘bag’ by using a stack, we have depth-first search. Each execution of ( ) or ( ) takes constant time, so the overall running time is O(V + E ). Since the graph is connected, V ≤ E + 1, so we can simplify the running time to O( E ). The spanning tree formed by the parent edges is called a depth-first spanning tree. The exact shape of the tree depends on the order in which neighbor edges are pushed onto the stack, but the in general, depth-first spanning trees are long and skinny. • If we use a queue instead of a stack, we have breadth-first search. Again, each execution of ( ) or ( ) takes constant time, so the overall running time is still O( E ). In this case, the breadth-first spanning tree formed by the parent edges contains shortest paths from the start vertex s to every other vertex in its connected component. The exact shape of the breadth-first spanning tree depends on the order in which neighbor edges are pushed onto the queue, but the in general, shortest path trees are short and bushy. We’ll see shortest paths again in a future lecture. b d b f a c d f a e c e A depth-first spanning tree and a breadth-first spanning tree of one component of the example graph, with start vertex a. • Suppose the edges of the graph are weighted. If we implement the ‘bag’ using a priority queue, always extracting the minimum-weight edge in line ( ), then we we have what might be called shortest-first search. In this case, each execution of ( ) or ( ) takes O(log E ) time, so the overall running time is O(V + E log E ), which simplifies to O( E log E ) if the graph is connected. For this algorithm, the set of parent edges form the minimum spanning tree of the connected component of s. We’ll see minimum spanning trees again in the next lecture. 5 Algorithms Lecture 11: Basic Graph Properties If the graph is represented using an adjacency matrix instead of an adjacency list, finding all the neighbors of each vertex in line (†) takes O(V ) time. Thus, depth- and breadth-first search each take O(V 2 ) time overall, and ‘shortest-first search’ takes O(V 2 + E log E ) = O(V 2 log V ) time overall. 11.5 Searching disconnected graphs If the graph is disconnected, then TRAVERSE(s) only visits the nodes in the connected component of the start vertex s. If we want to visit all the nodes in every component, we can use the following ‘wrapper’ around our generic traversal algorithm. Since TRAVERSE computes a spanning tree of one component, TRAVERSEALL computes a spanning forest of the entire graph. TRAVERSEALL(s): for all vertices v if v is unmarked TRAVERSE( v ) Exercises 1. Prove that the following definitions are all equivalent. • A tree is a connected acyclic graph. • A tree is a connected component of a forest. • A tree is a connected graph with at most V − 1 edges. • A tree is a minimal connected graph; removing any edge makes the graph disconnected. • A tree is an acyclic graph with at least V − 1 edges. • A tree is a maximal acyclic graph; adding an edge between any two vertices creates a cycle. 2. Prove that any connected acyclic graph with n ≥ 2 vertices has at least two vertices with degree 1. Do not use the words ‘tree’ of ‘leaf’, or any well-known properties of trees; your proof should follow entirely from the definitions. 3. Let G be a connected graph, and let T be a depth-first spanning tree of G rooted at some node v . Prove that if T is also a breadth-first spanning tree of G rooted at v , then G = T . 4. Whenever groups of pigeons gather, they instinctively establish a pecking order. For any pair of pigeons, one pigeon always pecks the other, driving it away from food or potential mates. The same pair of pigeons always chooses the same pecking order, even after years of separation, no matter what other pigeons are around. Surprisingly, the overall pecking order can contain cycles—for example, pigeon A pecks pigeon B , which pecks pigeon C , which pecks pigeon A. (a) Prove that any finite set of pigeons can be arranged in a row from left to right so that every pigeon pecks the pigeon immediately to its left. Pretty please. (b) Suppose you are given a directed graph representing the pecking relationships among a set of n pigeons. The graph contains one vertex per pigeon, and it contains an edge i → j if and only if pigeon i pecks pigeon j . Describe and analyze an algorithm to compute a pecking order for the pigeons, as guaranteed by part (a). 6 Algorithms Lecture 11: Basic Graph Properties 5. You are helping a group of ethnographers analyze some oral history data they have collected by interviewing members of a village to learn about the lives of people lived there over the last two hundred years. From the interviews, you have learned about a set of people, all now deceased, whom we will denote P1 , P2 , . . . , Pn . The ethnographers have collected several facts about the lifespans of these people. Specifically, for some pairs ( Pi , P j ), the ethnographers have learned one of the following facts: (a) Pi died before P j was born. (b) Pi and P j were both alive at some moment. Naturally, the ethnographers are not sure that their facts are correct; memories are not so good, and all this information was passed down by word of mouth. So they’d like you to determine whether the data they have collected is at least internally consistent, in the sense that there could have existed a set of people for which all the facts they have learned simultaneously hold. Describe and analyze and algorithm to answer the ethnographers’ problem. Your algorithm should either output possible dates of birth and death that are consistent with all the stated facts, or it should report correctly that no such dates exist. 6. Let G = (V, E ) be a given directed graph. (a) The transitive closure G T is a directed graph with the same vertices as G , that contains any edge u→ v if and only if there is a directed path from u to v in G . Describe an efficient algorithm to compute the transitive closure of G . (b) The transitive reduction G T R is the smallest graph (meaning fewest edges) whose transitive closure is G T . Describe an efficient algorithm to compute the transitive reduction of G . 7. A graph (V, E ) is bipartite if the vertices V can be partitioned into two subsets L and R, such that every edge has one vertex in L and the other in R. (a) Prove that every tree is a bipartite graph. (b) Describe and analyze an efficient algorithm that determines whether a given undirected graph is bipartite. 8. An Euler tour of a graph G is a closed walk through G that traverses every edge of G exactly once. (a) Prove that a connected graph G has an Euler tour if and only if every vertex has even degree. (b) Describe and analyze an algorithm to compute an Euler tour in a given graph, or correctly report that no such graph exists. 9. The d -dimensional hypercube is the graph defined as follows. There are 2d vertices, each labeled with a different string of d bits. Two vertices are joined by an edge if their labels differ in exactly one bit. (a) A Hamiltonian cycle in a graph G is a cycle of edges in G that visits every vertex of G exactly once. Prove that for all d ≥ 2, the d -dimensional hypercube has a Hamiltonian cycle. (b) Which hypercubes have an Euler tour (a closed walk that traverses every edge exactly once)? [Hint: This is very easy.] 7 Algorithms Lecture 11: Basic Graph Properties 10. Racetrack (also known as Graph Racers and Vector Rally) is a two-player paper-and-pencil racing game that Jeff played on the bus in 5th grade.3 The game is played with a track drawn on a sheet of graph paper. The players alternately choose a sequence of grid points that represent the motion of a car around the track, subject to certain constraints explained below. Each car has a position and a velocity, both with integer x - and y -coordinates. The initial position is a point on the starting line, chosen by the player; the initial velocity is always (0, 0). At each step, the player optionally increments or decrements either or both coordinates of the car’s velocity; in other words, each component of the velocity can change by at most 1 in a single step. The car’s new position is then determined by adding the new velocity to the car’s previous position. The new position must be inside the track; otherwise, the car crashes and that player loses the race. The race ends when the first car reaches a position on the finish line. Suppose the racetrack is represented by an n × n array of bits, where each 0 bit represents a grid point inside the track, each 1 bit represents a grid point outside the track, the ‘starting line’ is the first column, and the ‘finish line’ is the last column. Describe and analyze an algorithm to find the minimum number of steps required to move a car from the starting line to the finish line of a given racetrack. [Hint: Build a graph. What are the vertices? What are the edges? What problem is this?] position (1, 5) (2, 5) (4, 4) (7, 4) (9, 5) (10, 7) (10, 10) (9, 14) (9, 17) (10, 19) (12, 21) (14, 22) (16, 22) (17, 21) (19, 20) (22, 20) (25, 21) START (0, 0) (1, 0) (2, −1) (3, 0) (2, 1) (1, 2) (0, 3) (−1, 4) (0, 3) (1, 2) (2, 2) (2, 1) (2, 0) (1, −1) (2, −1) (3, 0) (3, 1) FINISH velocity A 16-step Racetrack run, on a 25 × 25 track. This is not the shortest run on this track. 11. Draughts/checkers is a game played on an m × m grid of squares, alternately colored light and dark. (The game is usually played on an 8 × 8 or 10 × 10 board, but the rules easily generalize to any board size.) Each dark square is occupied by at most one game piece (usually called a checker in the U.S.), which is either black or white; light squares are always empty. One player (‘White’) moves the white pieces; the other (‘Black’) moves the black pieces. Consider the following simple version of the game, essentially American checkers or British draughts, but where every piece is a king.4 Pieces can be moved in any of the four diagonal 3 The actual game is a bit more complicated than the version described here. In particular, in the actual game, the boundaries of the track are a free-form curve, and (at least by default) the entire line segment between any two consecutive positions must lie inside the track. In the version Jeff played, if a car does run off the track, the car starts its next turn with zero velocity, at the legal grid point closest to where the car left the track. 4 Most other variants of draughts have ‘flying kings’, which behave very differently than what’s described here. 8 Algorithms Lecture 11: Basic Graph Properties directions, either one or two steps at a time. On each turn, a player either moves one of her pieces one step diagonally into an empty square, or makes a series of jumps with one of her checkers. In a single jump, a piece moves to an empty square two steps away in any diagonal direction, but only if the intermediate square is occupied by a piece of the opposite color; this enemy piece is captured and immediately removed from the board. Multiple jumps are allowed in a single turn as long as they are made by the same piece. A player wins if her opponent has no pieces left on the board. Describe an algorithm that correctly determines whether White can capture every black piece, thereby winning the game, in a single turn. The input consists of the width of the board (m), a list of positions of white pieces, and a list of positions of black pieces. For full credit, your algorithm should run in O(n) time, where n is the total number of pieces. 4 5 3 11 6 2 10 7 1 9 8 White wins in one turn. White cannot win in one turn from either of these positions. [Hint: The greedy strategy—make arbitrary jumps until you get stuck—does not always find a winning sequence of jumps even when one exists. See problem 8. Parity, parity, parity.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 9 Algorithms Lecture 12: Minimum Spanning Trees We must all hang together, gentlemen, or else we shall most assuredly hang separately. — Benjamin Franklin, at the signing of the Declaration of Independence (July 4, 1776) It is a very sad thing that nowadays there is so little useless information. — Oscar Wilde A ship in port is safe, but that is not what ships are for. — Rear Admiral Grace Murray Hopper 12 12.1 Minimum Spanning Trees Introduction Suppose we are given a connected, undirected, weighted graph. This is a graph G = (V, E ) together with a function w : E → IR that assigns a weight w (e) to each edge e. For this lecture, we’ll assume that the weights are real numbers. Our task is to find the minimum spanning tree of G , that is, the spanning tree T minimizing the function w(T ) = w (e). e∈ T To keep things simple, I’ll assume that all the edge weights are distinct: w (e) = w (e ) for any pair of edges e and e . Distinct weights guarantee that the minimum spanning tree of the graph is unique. Without this condition, there may be several different minimum spanning trees. For example, if all the edges have weight 1, then every spanning tree is a minimum spanning tree with weight V − 1. 8 5 10 2 18 3 12 30 16 14 4 26 A weighted graph and its minimum spanning tree. If we have an algorithm that assumes the edge weights are unique, we can still use it on graphs where multiple edges have the same weight, as long as we have a consistent method for breaking ties. One way to break ties consistently is to use the following algorithm in place of a simple comparison. SHORTEREDGE takes as input four integers i , j , k, l , and decides which of the two edges (i , j ) and (k, l ) has ‘smaller’ weight. SHORTEREDGE(i , j , k, l ) if w (i , j ) < w (k, l ) return (i , j ) if w (i , j ) > w (k, l ) return (k, l ) if min(i , j ) < min(k, l ) return (i , j ) if min(i , j ) > min(k, l ) return (k, l ) if max(i , j ) < max(k, l ) return (i , j ) 〈〈if max(i,j) < max(k,l) 〉〉 return (k, l ) 1 Algorithms 12.2 Lecture 12: Minimum Spanning Trees The Only Minimum Spanning Tree Algorithm There are several different methods for computing minimum spanning trees, but really they are all instances of the following generic algorithm. The situation is similar to the previous lecture, where we saw that depth-first search and breadth-first search were both instances of a single generic traversal algorithm. The generic minimum spanning tree algorithm maintains an acyclic subgraph F of the input graph G , which we will call an intermediate spanning forest. F is a subgraph of the minimum spanning tree of G , and every component of F is a minimum spanning tree of its vertices. Initially, F consists of n one-node trees. The generic algorithm merges trees together by adding certain edges between them. When the algorithm halts, F consists of a single n-node tree, which must be the minimum spanning tree. Obviously, we have to be careful about which edges we add to the evolving forest, since not every edge is in the minimum spanning tree. The intermediate spanning forest F induces two special types of edges. An edge is useless if it is not an edge of F , but both its endpoints are in the same component of F . For each component of F , we associate a safe edge—the minimum-weight edge with exactly one endpoint in that component. Different components might or might not have different safe edges. Some edges are neither safe nor useless—we call these edges undecided. All minimum spanning tree algorithms are based on two simple observations. Lemma 1. The minimum spanning tree contains every safe edge and no useless edges.1 Proof: Let T be the minimum spanning tree. Suppose F has a ‘bad’ component whose safe edge e = (u, v ) is not in T . Since T is connected, it contains a unique path from u to v , and at least one edge e on this path has exactly one endpoint in the bad component. Removing e from the minimum spanning tree and adding e gives us a new spanning tree. Since e is the bad component’s safe edge, we have w (e ) > w (e), so the the new spanning tree has smaller total weight than T . But this is impossible— T is the minimum spanning tree. So T must contain every safe edge. Adding any useless edge to F would introduce a cycle. u e v e’ Proving that every safe edge is in the minimum spanning tree. The ‘bad’ component of F is highlighted. So our generic minimum spanning tree algorithm repeatedly adds one or more safe edges to the evolving forest F . Whenever we add new edges to F , some undecided edges become safe, and others become useless. To specify a particular algorithm, we must decide which safe edges to add, and how to identify new safe and new useless edges, at each iteration of our generic template. 1 This lemma is actually a special case of two more general theorems. First, for any partition of the vertices of G into two disjoint subsets, the minimum-weight edge with one endpoint in each subset is in the minimum spanning tree. Second, the maximum-weight edge in any cycle in G is not in the minimum spanning tree. 2 Algorithms 12.3 Lecture 12: Minimum Spanning Trees Bor˙ vka’s Algorithm u ˙ The oldest and arguably simplest minimum spanning tree algorithm was discovered by Boruvka in 1926, 2 long before computers even existed, and practically before the invention of graph theory! The algorithm was rediscovered by Choquet in 1938; again by Florek, Łukaziewicz, Perkal, Stienhaus, and Zubrzycki in 1951; and again by Sollin some time in the early 1960s. Because Sollin was the only Western computer scientist in this list—Choquet was a civil engineer; Florek and his co-authors were anthropologists—this is often called ‘Sollin’s algorithm’, especially in the parallel computing literature. ˙ The Boruvka/Choquet/Florek/Łukaziewicz/Perkal/Stienhaus/Zubrzycki/Sollin algorithm can be summarized in one line: ˙ B OR U VKA: Add all the safe edges and recurse. 8 5 10 2 18 3 12 30 16 18 12 14 14 4 26 26 Bor˙ vka’s algorithm run on the example graph. Thick edges are in F . u Arrows point along each component’s safe edge. Dashed edges are useless. ˙ At the beginning of each phase of the Boruvka algorithm, each component elects an arbitrary ‘leader’ node. The simplest way to hold these elections is a depth-first search of F ; the first node we visit in any component is that component’s leader. Once the leaders are elected, we find the safe edges for each component, essentially by brute force. Finally, we add these safe edges to F . FINDSAFEEDGES(V, E ): for each leader v safe( v ) ← ∞ for each edge (u, v ) ∈ E u ← leader(u) v ← leader( v ) if u = v if w (u, v ) < w (safe(u)) safe(u) ← (u, v ) if w (u, v ) < w (safe( v )) safe( v ) ← (u, v ) ˙ B OR U VKA(V, E ): F = (V, ∅) while F has more than one component choose leaders using DFS FINDSAFEEDGES(V, E ) for each leader v add safe( v ) to F Each call to FINDSAFEEDGES takes O( E ) time, since it examines every edge. Since the graph is ˙ connected, it has at most E + 1 vertices. Thus, each iteration of the while loop in B OR U VKA takes O( E ) time, assuming the graph is represented by an adjacency list. Each iteration also reduces the number of components of F by at least a factor of two—the worst case occurs when the components coalesce in pairs. Since there are initially V components, the while loop iterates O(log V ) times. Thus, the overall running time of Bor˙ vka’s algorithm is O (E log V ). u ˙ Despite its relatively obscure origin, early algorithms researchers were aware of Boruvka’s algorithm, ˙ but dismissed it as being ‘too complicated’! As a result, despite its simplicity and efficiency, Boruvka’s algorithm is rarely mentioned in algorithms and data structures textbooks. On the other hand, more 2 Leonard Euler published the first graph theory result, his famous theorem about the bridges of Königsburg, in 1736. However, the first textbook on graph theory, written by Dénes König, was not published until 1936. 3 Algorithms Lecture 12: Minimum Spanning Trees ˙ recent algorithms to compute minimum spanning trees are all generalizations of Boruvka’s algorithm, not the other two classical algorithms described next. 12.4 Jarník’s (‘Prim’s’) Algorithm The next oldest minimum spanning tree algorithm was first described by the Polish mathematician ˙ Vojtˇch Jarník in a 1929 letter to Boruvka. The algorithm was independently rediscovered by Kruskal in e 1956, by Prim in 1957, by Loberman and Weinberger in 1957, and finally by Dijkstra in 1958. Prim, Loberman, Weinberger, and Dijkstra all (eventually) knew of and even cited Kruskal’s paper, but since Kruskal also described two other minimum-spanning-tree algorithms in the same paper, this algorithm is usually called ‘Prim’s algorithm’, or sometimes even ‘the Prim/Dijkstra algorithm’, even though by 1958 Dijkstra already had another algorithm (inappropriately) named after him. In Jarník’s algorithm, the forest F contains only one nontrivial component T ; all the other components are isolated vertices. Initially, T consists of an arbitrary vertex of the graph. The algorithm repeats the following step until T spans the whole graph: JARNÍK: Find T ’s safe edge and add it to T . 8 5 8 5 10 2 18 3 12 2 30 14 4 8 5 10 16 18 3 12 8 10 2 30 16 18 5 10 3 3 16 30 30 16 14 26 26 26 26 8 30 26 16 5 30 16 26 Jarník’s algorithm run on the example graph, starting with the bottom vertex. At each stage, thick edges are in T , an arrow points along T ’s safe edge, and dashed edges are useless. To implement Jarník’s algorithm, we keep all the edges adjacent to T in a heap. When we pull the minimum-weight edge off the heap, we first check whether both of its endpoints are in T . If not, we add the edge to T and then add the new neighboring edges to the heap. In other words, Jarník’s algorithm is just another instance of the generic graph traversal algorithm we saw last time, using a heap as the ‘bag’! If we implement the algorithm this way, its running time is O( E log E ) = O( E log V ). However, we can speed up the implementation by observing that the graph traversal algorithm visits each vertex only once. Rather than keeping edges in the heap, we can keep a heap of vertices, where the key of each vertex v is the length of the minimum-weight edge between v and T (or ∞ if there is no such edge). Each time we add a new edge to T , we may need to decrease the key of some neighboring vertices. To make the description easier, we break the algorithm into two parts. JARNÍKINIT initializes the vertex heap. JARNÍKLOOP is the main algorithm. The input consists of the vertices and edges of the graph, plus the start vertex s. 4 Algorithms Lecture 12: Minimum Spanning Trees JARNÍK(V, E , s): JARNÍKINIT(V, E , s) JARNÍKLOOP(V, E , s) JARNÍKINIT(V, E , s): for each vertex v ∈ V \ {s} if ( v, s) ∈ E edge( v ) ← ( v, s) key( v ) ← w ( v, s) else edge( v ) ← NULL key( v ) ← ∞ INSERT( v ) JARNÍKLOOP(V, E , s): T ← ({s}, ∅) for i ← 1 to |V | − 1 v ← EXTRACTMIN add v and edge( v ) to T for each edge (u, v ) ∈ E if u ∈ T and key(u) > w (u, v ) / edge(u) ← (u, v ) DECREASEKEY(u, w (u, v )) The running time of JARNÍK is dominated by the cost of the heap operations INSERT, EXTRACTMIN, and DECREASEKEY. INSERT and EXTRACTMIN are each called O(V ) times once for each vertex except s, and DECREASEKEY is called O( E ) times, at most twice for each edge. If we use a standard binary heap, each of these operations requires O(log V ) time, so the overall running time of JARNÍK is O((V + E ) log V ) = O (E log V ). The running time can be improved to O( E + V log V ) using a data structure called a Fibonacci heap, which supports INSERT and DECREASEKEY in constant amortized time; this is faster than Bor˙ vka’s u algorithm unless E = O(V ). 12.5 Kruskal’s Algorithm The last minimum spanning tree algorithm I’ll discuss was first described by Kruskal in 1956, in the same paper where he rediscovered Jarnik’s algorithm. Kruskal was motivated by ‘a typewritten translation ˙ ˙ (of obscure origin)’ of Boruvka’s original paper, claiming that Boruvka’s algorithm was ‘unnecessarily 3 elaborate’. This algorithm was also rediscovered in 1957 by Loberman and Weinberger, but somehow avoided being renamed after them. KRUSKAL: Scan all edges in increasing weight order; if an edge is safe, add it to F . 8 8 5 18 18 3 12 8 5 8 10 5 8 10 10 3 10 2 5 10 12 30 16 18 12 14 30 4 16 30 16 18 12 14 26 4 30 16 18 12 14 26 30 16 14 26 26 14 4 26 10 18 30 16 18 30 16 18 12 30 26 26 12 30 26 30 30 16 26 30 26 18 18 14 14 14 16 26 Kruskal’s algorithm run on the example graph. Thick edges are in F . Dashed edges are useless. 3 ˙ To be fair, Boruvka’s original paper was unnecessarily elaborate, but in his followup paper, also published in 1927, simplified his algorithm to its current modern form. Kruskal was apparently unaware of Bor˙ vka’s second paper. Stupid Iron Curtain. u 5 Algorithms Lecture 12: Minimum Spanning Trees Since we examine the edges in order from lightest to heaviest, any edge we examine is safe if and only if its endpoints are in different components of the forest F . To prove this, suppose the edge e joins two components A and B but is not safe. Then there would be a lighter edge e with exactly one endpoint in A. But this is impossible, because (inductively) any previously examined edge has both endpoints in the same component of F . ˙ Just as in Boruvka’s algorithm, each component of F has a ‘leader’ node. An edge joins two ˙ components of F if and only if the two endpoints have different leaders. But unlike Boruvka’s algorithm, we do not recompute leaders from scratch every time we add an edge. Instead, when two components are joined, the two leaders duke it out in a nationally-televised no-holds-barred steel-cage grudge match.4 One of the two emerges victorious as the leader of the new larger component. More formally, we will use our earlier algorithms for the UNION-FIND problem, where the vertices are the elements and the components of F are the sets. Here’s a more formal description of the algorithm: KRUSKAL(V, E ): sort E by wieght F ←∅ for each vertex v ∈ V MAKESET( v ) for i ← 1 to | E | (u, v ) ← i th lightest edge in E if FIND(u) = FIND( v ) UNION(u, v ) add (u, v ) to F return F In our case, the sets are components of F , and n = V . Kruskal’s algorithm performs O( E ) FIND operations, two for each edge in the graph, and O(V ) UNION operations, one for each edge in the minimum spanning tree. Using union-by-rank and path compression allows us to perform each UNION or FIND in O(α( E , V )) time, where α is the not-quite-constant inverse-Ackerman function. So ignoring the cost of sorting the edges, the running time of this algorithm is O( E α( E , V )). We need O( E log E ) = O( E log V ) additional time just to sort the edges. Since this is bigger than the time for the UNION-FIND data structure, the overall running time of Kruskal’s algorithm is O (E log V ), exactly the same as Bor˙ vka’s algorithm, or Jarník’s algorithm with a normal (non-Fibonacci) heap. u Exercises 1. Most classical minimum-spanning-tree algorithms use the notions of ‘safe’ and ‘useless’ edges described in the lecture notes, but there is an alternate formulation. Let G be a weighted undirected graph, where the edge weights are distinct. We say that an edge e is dangerous if it is the longest edge in some cycle in G , and useful if it does not lie in any cycle in G . (a) Prove that the minimum spanning tree of G contains every useful edge. (b) Prove that the minimum spanning tree of G does not contain any dangerous edge. (c) Describe and analyze an efficient implementation of the “anti-Kruskal” MST algorithm: Examine the edges of G in decreasing order; if an edge is dangerous, remove it from G . [Hint: It won’t be as fast as Kruskal’s algorithm.] 4 Live at the Assembly Hall! Only $49.95 on Pay-Per-View! 6 Algorithms Lecture 12: Minimum Spanning Trees 2. Let G = (V, E ) be an arbitrary connected graph with weighted edges. (a) Prove that for any partition of the vertices V into two subsets, the minimum-weight edge with one endpoint in each subset is in the minimum spanning tree of G . (b) Prove that the maximum-weight edge in any cycle of G is not in the minimum spanning tree of G . (c) Prove or disprove: The minimum spanning tree of G includes the minimum-weighted edge in every cycle in G . 3. Throughout this lecture note, we assumed that no two edges in the input graph have equal weights, which implies that the minimum spanning tree is unique. In fact, a weaker condition on the edge weights implies MST uniqueness. (a) Describe an edge-weighted graph that has a unique minimum spanning tree, even though two edges have equal weights. (b) Prove that an edge-weighted graph G has a unique minimum spanning tree if and only if the following conditions hold: • For any partition of the vertices of G into two subsets, the minimum-weight edge with one endpoint in each subset is unique. • The maximum-weight edge in any cycle of G is unique. (c) Describe and analyze an algorithm to determine whether or not a graph has a unique minimum spanning tree. 4. (a) Describe and analyze an algorithm to compute the maximum-weight spanning tree of a given edge-weighted graph. (b) A feedback edge set of a graph G is a subset F of the edges such that every cycle in G contains at least one edge in F . In other words, removing every edge in F makes the graph G acyclic. Describe and analyze a fast algorithm to compute the minimum weight feedback edge set of of a given edge-weighted graph. 5. Consider a path between two vertices s and t in an undirected weighted graph G . The bottleneck length of this path is the maximum weight of any edge in the path. The bottleneck distance between s and t is the minimum bottleneck length of any path from s to t . (If there are no paths from s to t , the bottleneck distance between s and t is ∞.) 11 1 6 t 3 4 7 5 2 10 s 9 8 12 The bottleneck distance between s and t is 5. 7 Algorithms Lecture 12: Minimum Spanning Trees Describe and analyze an algorithm to compute the bottleneck distance between every pair of vertices in an arbitrary undirected weighted graph. Assume that no two edges have the same weight. 6. Suppose you are given a graph G with weighted edges and a minimum spanning tree T of G . (a) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is decreased. (b) Describe an algorithm to update the minimum spanning tree when the weight of a single edge e is increased. In both cases, the input to your algorithm is the edge e and its new weight; your algorithms should modify T so that it is still a minimum spanning tree. [Hint: Consider the cases e ∈ T and e ∈ T separately.] 7. (a) Describe and analyze and algorithm to find the second smallest spanning tree of a given graph G , that is, the spanning tree of G with smallest total weight except for the minimum spanning tree. (b) Describe and analyze an efficient algorithm to compute, given a weighted undirected graph G and an integer k, the k spanning trees of G with smallest weight. 8. We say that a graph G = (V, E ) is dense if E = Θ(V 2 ). Describe a modification of Jarník’s minimumspanning tree algorithm that runs in O(V 2 ) time (independent of E ) when the input graph is dense, using only simple data structures (and in particular, without using a Fibonacci heap). ˙ 9. Consider an algorithm that first performs k passes of Boruvka’s algorithm, and then runs Jarník’s algorithm (with a Fibonacci heap) on the resulting contracted graph. (a) What is the running time of this hybrid algorithm, as a function of V , E , and k? (b) For which value of k is this running time minimized? What is the resulting running time? 10. Describe an algorithm to compute the minimum spanning tree of an n-vertex planar graph in O(n) time. [Hint: Contracting an edge in a planar graph yields another planar graph. Any planar graph with n vertices has at most 3n − 6 edges.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 8 Algorithms Lecture 13: Shortest Paths Well, ya turn left by the fire station in the village and take the old post road by the reservoir and. . . no, that won’t do. Best to continue straight on by the tar road until you reach the schoolhouse and then turn left on the road to Bennett’s Lake until. . . no, that won’t work either. East Millinocket, ya say? Come to think of it, you can’t get there from here. — Robert Bryan and Marshall Dodge, Bert and I and Other Stories from Down East (1961) Hey farmer! Where does this road go? Been livin’ here all my life, it ain’t gone nowhere yet. Hey farmer! How do you get to Little Rock? Listen stranger, you can’t get there from here. Hey farmer! You don’t know very much do you? No, but I ain’t lost. — Michelle Shocked, “Arkansas Traveler" (1992) 13 13.1 Shortest Paths Introduction Given a weighted directed graph G = (V, E , w ) with two special vertices, a source s and a target t , we want to find the shortest directed path from s to t . In other words, we want to find the path p starting at s and ending at t minimizing the function w ( p) = w (e). e∈ p For example, if I want to answer the question ‘What’s the fastest way to drive from my old apartment in Champaign, Illinois to my wife’s old apartment in Columbus, Ohio?’, we might use a graph whose vertices are cities, edges are roads, weights are driving times, s is Champaign, and t is Columbus.1 The graph is directed since the driving times along the same road might be different in different directions.2 Perhaps counter to intuition, we will allow the weights on the edges to be negative. Negative edges make our lives complicated, since the presence of a negative cycle might mean that there is no shortest path. In general, a shortest path from s to t exists if and only if there is at least one path from s to t , but there is no path from s to t that touches a negative cycle. If there is a negative cycle between s and t , then se can always find a shorter path by going around the cycle one more time. 2 s −8 5 3 4 t 1 There is no shortest path from s to t . Every algorithm known for solving this problem actually solves (large portions of) the following more general single source shortest path or SSSP problem: find the shortest path from the source vertex s 1 West on Church, north on Prospect, east on I-74, south on I-465, east on Airport Expressway, north on I-65, east on I-70, north on Grandview, east on 5th, north on Olentangy River, east on Dodridge, north on High, west on Kelso, south on Neil. Depending on traffic. We both live in Urbana now. 2 In 1999 and 2000, there was a speed trap on I-70 just east of the Indiana/Ohio border, but only for eastbound traffic. 1 Algorithms Lecture 13: Shortest Paths to every other vertex in the graph. In fact, the problem is usually solved by finding a shortest path tree rooted at s that contains all the desired shortest paths. It’s not hard to see that if shortest paths are unique, then they form a tree. To prove this, it’s enough to observe that any subpath of a shortest path is also a shortest path. If there are multiple shortest paths to the same vertices, we can always choose one path to each vertex so that the union of the paths is a tree. If there are shortest paths to two vertices u and v that diverge, then meet, then diverge again, we can modify one of the paths so that the two paths only diverge once. b s c u a d x y v If s→a→ b→c →d → v and s→a→ x → y →d →u are both shortest paths, then s→a→ b→c →d →u is also a shortest path. Shortest path trees and minimum spanning trees are usually very different. For one thing, there is only one minimum spanning tree, but in general, there is a different shortest path tree for every source vertex. Moreover, in general, all of these shortest path trees are different from the minimum spanning tree. 8 5 8 5 10 2 18 10 3 12 2 30 16 18 12 14 4 3 30 16 14 26 4 26 A minimum spanning tree and a shortest path tree (rooted at the topmost vertex) of the same graph. All of the algorithms described in this lecture also work for undirected graphs, with some slight modifications. Most importantly, we must specifically prohibit alternating back and forth across the same undirected negative-weight edge. (Our unmodified algorithms would interpret any negative-weight edge as a negative cycle of length 2.) To emphasize the direction, I will consistently use the nonstandard notation u→ v to denote a directed edge from u to v . 13.2 The Only SSSP Algorithm Just like graph traversal and minimum spanning trees, there are several different SSSP algorithms, but they are all special cases of the a single generic algorithm, first proposed by Ford in 1956, and independently by Dantzig in 1957.3 Each vertex v in the graph stores two values, which (inductively) describe a tentative shortest path from s to v . • dist( v ) is the length of the tentative shortest s 3 v path, or ∞ if there is no such path. Specifically, Dantzig showed that the shortest path problem can be phrased as a linear programming problem, and then described an interpretation of the simplex method (which Dantzig discovered) in terms of the original graph. His description was equivalent to Ford’s relaxation strategy. 2 Algorithms Lecture 13: Shortest Paths • pred( v ) is the predecessor of v in the tentative shortest s vertex. v path, or NULL if there is no such The predecessor pointers automatically define a tentative shortest path tree; they play the same role as the ‘parent’ pointers in our generic graph traversal algorithm. We already know that dist(s) = 0 and pred(s) = NULL. For every vertex v = s, we initially set dist( v ) = ∞ and pred( v ) = NULL to indicate that we do not know of any path from s to v . We call an edge u→ v tense if dist(u) + w (u→ v ) < dist( v ). If u→ v is tense, then the tentative shortest path s v is incorrect, since the path s u→ v is shorter. Our generic algorithm repeatedly finds a tense edge in the graph and relaxes it: RELAX(u→ v ): dist( v ) ← dist(u) + w (u→ v ) pred( v ) ← u If there are no tense edges, our algorithm is finished, and we have our desired shortest path tree. The correctness of the relaxation algorithm follows directly from three simple claims: 1. If dist( v ) = ∞, then dist( v ) is the total weight of the predecessor chain ending at v : s→ · · · →pred(pred( v ))→pred( v )→ v. This is easy to prove by induction on the number of relaxation steps. (Hint, hint.) 2. If the algorithm halts, then dist( v ) ≤ w (s v ) for any path s on the number of edges in the path s v . (Hint, hint.) v . This is easy to prove by induction 3. The algorithm halts if and only if there is no negative cycle reachable from s. The ‘only if’ direction is easy—if there is a reachable negative cycle, then after the first edge in the cycle is relaxed, the cycle always has at least one tense edge. The ‘if’ direction follows from the fact that every relaxation step reduces either the number of vertices with dist( v ) = ∞ by 1 or reduces the sum of the finite shortest path lengths by the difference between two edge weights. Actually proving the first two claims above is not as straightforward as I make it sound; there are some unfortunate subtleties. It’s much easier to prove the following weaker claims, which also imply correctness: • For every vertex v , dist( v ) is always greater than or equal to the shortest-path distance from s to v . (Induction on the number of relaxation steps.) • If no edge is tense, then for every vertex v , dist( v ) is the shortest-path distance from s to v . (Induction on the number of edges in the true shortest path; see Bellman-Ford. If the edge weights are non-negative, induction on the rank of the shortest-path distance also works; see Dijkstra!) • If no edge is tense, then for every vertex v , the path s→ · · · →pred(pred( v ))→pred( v )→ v is a shortest path from s to v . (Induction on the number of edges in the path; why does pred( v ) have its current value?) • The algorithm halts if there are no negative cycles reachable from s; see above. 3 Algorithms Lecture 13: Shortest Paths I haven’t said anything about how we detect which edges can be relaxed, or in what order we relax them. In order to make this easier, we can refine the relaxation algorithm slightly, into something closely resembling the generic graph traversal algorithm. We maintain a ‘bag’ of vertices, initially containing just the source vertex s. Whenever we take a vertex u out of the bag, we scan all of its outgoing edges, looking for something to relax. Whenever we successfully relax an edge u→ v , we put v into the bag. Unlike our generic graph traversal algorithm, the same vertex might be visited many times. GENERIC SSSP(s): INITSSSP(s) put s in the bag while the bag is not empty take u from the bag for all edges u→ v if u→ v is tense RELAX(u→ v ) put v in the bag INITSSSP(s): dist(s) ← 0 pred(s) ← NULL for all vertices v = s dist( v ) ← ∞ pred( v ) ← NULL Just as with graph traversal, using different data structures for the ‘bag’ gives us different algorithms. There are three obvious choices to try: a stack, a queue, and a heap. Unfortunately, if we use a stack, we have to perform Θ(2V ) relaxation steps in the worst case! (Proving this is a good homework problem.) The other two possibilities are much more efficient. 13.3 Dijkstra’s Algorithm If we implement the bag as a heap, where the key of a vertex v is dist( v ), we obtain an algorithm first ‘published’4 by Leyzorek, Gray, Johnson, Ladew, Meaker, Petry, and Seitz in 1957, and then later independently rediscovered by Edsger Dijkstra in 1959. A very similar algorithm was also described by Dantzig in 1958. Dijkstra’s algorithm, as it is universally known5 , is particularly well-behaved if the graph has no negative-weight edges. In this case, it’s not hard to show (by induction, of course) that the vertices are scanned in increasing order of their shortest-path distance from s. It follows that each vertex is scanned at most once, and thus that each edge is relaxed at most once. Since the key of each vertex in the heap is its tentative distance from s, the algorithm performs a DECREASEKEY operation every time an edge is relaxed. Thus, the algorithm performs at most E DECREASEKEYs. Similarly, there are at most V INSERT and EXTRACTMIN operations. Thus, if we store the vertices in a Fibonacci heap, the total running time of Dijkstra’s algorithm is O (E + V log V ); if we use a regular binary heap, the running time is O( E log V ). This analysis assumes that no edge has negative weight. Dijkstra’s algorithm (in the form I’m presenting here) is still correct if there are negative edges6 , but the worst-case running time could be exponential. (Proving this unfortunate fact is a good homework problem.) 4 in the first annual report on a research project performed for the Combat Development Department of the Army Electronic Proving Ground 5 I will follow this common convention, despite the historical inaccuracy, because I don’t think anybody wants to read about the "Leyzorek-Gray-Johnson-Ladew-Meaker-Petry-Seitz algorithm". 6 Many textbooks present a version of Dijkstra’s algorithm that gives incorrect results for graphs with negative edges. 4 Algorithms Lecture 13: Shortest Paths ∞ ∞ 3 2 1 ∞ 0 1 5 0 12 8 ∞ 2 ∞ ∞ ∞ 10 6 ∞ 12 3 6 8 ∞ 3 3 0 s ∞ 2 1 3 14 1 5 8 0 12 8 3 4 ∞ 3 12 7 3 4 6 0 s 9 5 4 10 7 6 2 4 9 4 10 3 4 6 7 3 0 12 7 0 s 4 12 5 4 10 3 4 0 s 1 0 7 ∞ 2 ∞ 5 8 ∞ 3 4 10 7 4 ∞ 3 3 0 s Four phases of Dijkstra’s algorithm run on a graph with no negative edges. At each phase, the shaded vertices are in the heap, and the bold vertex has just been scanned. The bold edges describe the evolving shortest path tree. 13.4 The A∗ Heuristic A slight generalization of Dijkstra’s algorithm, commonly known as the A∗ algorithm, is frequently used to find a shortest path from a single source node s to a single target node t . A∗ uses a black-box function GUESSDISTANCE( v, t ) that returns an estimate of the distance from v to t . The only difference between Dijkstra and A∗ is that the key of a vertex v is dist( v ) + GUESSDISTANCE( v, t ). The function GUESSDISTANCE is called admissible if GUESSDISTANCE( v, t ) never overestimates the actual shortest path distance from v to t . If GUESSDISTANCE is admissible and the actual edge weights are all non-negative, the A∗ algorithm computes the actual shortest path from s to t at least as quickly as Dijkstra’s algorithm. The closer GUESSDISTANCE( v, t ) is to the real distance from v to t , the faster the algorithm. However, in the worst case, the running time is still O( E + V log V ). The heuristic is especially useful in situations where the actual graph is not known. For example, A∗ can be used to solve many puzzles (15-puzzle, Freecell, Shanghai, Sokoban, Atomix, Rush Hour, Rubik’s Cube, . . . ) and other path planning problems where the starting and goal configurations are given, but the graph of all possible configurations and their connections is not given explicitly. 13.5 Shimbel’s Algorithm (‘Bellman-Ford’) If we replace the heap in Dijkstra’s algorithm with a queue, we get an algorithm that was first published by Shimbel in 1955, then independently rediscovered by Moore in 1957, by Woodbury and Dantzig in 1957, and by Bellman in 1958. Since Bellman used the idea of relaxing edges, which was first proposed by Ford in 1956, this is usually called the ‘Bellman-Ford’ algorithm. Shimbel’s algorithm is efficient even if there are negative edges, and it can be used to quickly detect the presence of negative cycles. If there 5 Algorithms Lecture 13: Shortest Paths are no negative edges, however, Dijkstra’s algorithm is faster. (In fact, in practice, Dijkstra’s algorithm is often faster even for graphs with negative edges.) Is describing this algorithm with a queue really helpful, or just confusing? After all, it’s just a straightforward dynamic programming algorithm. And the structure of the algorithm is very close to the proof of correctness of the generic algorithm (induction on the number of edges in the shortest path). The easiest way to analyze the algorithm is to break the execution into phases, by introducing an imaginary token. Before we even begin, we insert the token into the queue. The current phase ends when we take the token out of the queue; we begin the next phase by reinserting the token into the queue. The 0th phase consists entirely of scanning the source vertex s. The algorithm ends when the queue contains only the token. A simple inductive argument (hint, hint) implies the following invariant: At the end of the i th phase, for every vertex v , dist( v ) is less than or equal to the length of the shortest path s v consisting of i or fewer edges. Since a shortest path can only pass through each vertex once, either the algorithm halts before the V th phase, or the graph contains a negative cycle. In each phase, we scan each vertex at most once, so we relax each edge at most once, so the running time of a single phase is O( E ). Thus, the overall running time of Shimbel’s algorithm is O (V E ). Once we understand how the phases of Shimbel’s algorithm behave, we can simplify the algorithm considerably. Instead of using a queue to perform a partial breadth-first search of the graph in each phase, we can simply scan through the adjacency list directly and try to relax every edge in the graph. SHIMBELSSSP(s) INITSSSP(s) repeat V times: for every edge u→ v if u→ v is tense RELAX(u→ v ) for every edge u→ v if u→ v is tense return ‘Negative cycle!’ This is how most textbooks present the ‘Bellman-Ford’ algorithm.7 The O(V E ) running time of this version of the algorithm should be obvious, but it may not be clear that the algorithm is still correct. To prove correctness, we just have to show that our earlier invariant holds; as before, this can be proved by induction on i . 7 In fact, this is closer to the description that Shimbel and Bellman used. Bob Tarjan recognized in the early 1980s that Shimbel’s algorithm is equivalent to Dijkstra’s algorithm with a queue instead of a heap. 6 Algorithms Lecture 13: Shortest Paths e ∞ −8 d ∞ −8 2 f ∞ 1 0 6 0 4 3 6 3 c 4 6 0 s e 9 e 9 2 −8 8 −3 6 5 b 2 −3 −1 8 3 c −3 −1 4 1 a 3 f 7 1 0 4 −2 a d 2 5 b 2 −3 2 −8 f 7 1 0 3 c 3 0 s d 1 −3 −1 6 a 3 0 s 5 8 −1 6 a f 7 b 2 −3 −3 8 ∞ c 4 2 1 5 b 4 −1 d 4 f ∞ 1 −3 −3 −8 2 0 8 ∞ a d ∞ 5 b ∞ −3 e ∞ e ∞ 6 0 s 3 c 3 0 s Four phases of Shimbel’s algorithm run on a directed graph with negative edges. Nodes are taken from the queue in the order s a b c d f b a e d d a , where is the token. Shaded vertices are in the queue at the end of each phase. The bold edges describe the evolving shortest path tree. 13.6 Greedy Relaxation? Here’s another algorithm that fits our generic framework, but which I’ve never seen analyzed. Repeatedly relax the tensest edge. Specifically, let’s define the ‘tension’ of an edge u→ v as follows: tension(u→ v ) = max {0, dist( v ) − dist(u) − w (u→ v )} (This is defined even when dist( v ) = ∞ or dist(u) = ∞, as long as we treat ∞ just like some indescribably large but finite number.) If an edge has zero tension, it’s not tense. If we relax an edge u→ v , then dist( v ) decreases tension(u→ v ) and tension(u→ v ) becomes zero. Intuitively, we can keep the edges of the graph in some sort of heap, where the key of each edge is its tension. Then we repeatedly pull out the tensest edge u→ v and relax it. Then we need to recompute the tension of other edges adjacent to v . Edges leaving v possibly become more tense, and edges coming into v possibly become less tense. So we need a heap that efficiently supports the operations INSERT, EXTRACTMAX, INCREASEKEY, and DECREASEKEY. If there are no negative cycles, this algorithm eventually halts with a shortest path tree, but how quickly? Can the same edge be relaxed more than once, and if so, how many times? Is it faster if all the edge weights are positive? Hmm.... This sounds like a good extra credit problem!8 8 I first proposed this bizarre algorithm in 1998, the very first time I taught an algorithms class. As far as I know, nobody has even seriously attempted an analysis. Or maybe it has been analyzed, but it requires an exponential number of relaxation steps in the worst case, so nobody’s ever bothered to publish it. 7 Algorithms Lecture 13: Shortest Paths Exercises 1. This question asks you to fill in the remaining proof details for Ford’s generic shortest-path algorithm: while at least one edge is tense, relax an arbitrary tense edge. You may assume that the input graph does not contain a negative cycle, and that all shortest paths in the input graph are unique. (a) Prove that the generic shortest-path algorithm halts. (b) Prove that after every call to RELAX, for every vertex v , either dist( v ) = ∞ or dist( v ) is the total weight of some path from s to v . (c) Prove that when the generic shortest-path algorithm halts, then for every vertex v , either dist( v ) = ∞, or dist( v ) is the total weight of the predecessor chain ending at v : s→ · · · →pred(pred( v ))→pred( v )→ v. (d) Prove that when the generic shortest-path algorithm halts, then dist( v ) ≤ w (s path s v . v ) for every 2. Prove the following statement for every integer i and every vertex v : At the end of the i th phase of Shimbel’s algorithm, dist( v ) is less than or equal to the length of the shortest path s v consisting of i or fewer edges. 3. Prove that Ford’s generic shortest-path algorithm (while the graph contains a tense edge, relax it) can take exponential time in the worst case when implemented with a stack instead of a priority queue (like Dijkstra) or a queue (like Shimbel’s algorithm). SpeciÞcally, for every positive integer n, construct a weighted directed n-vertex graph Gn , such that the stack-based shortest-path algorithm call RELAX Ω(2n ) times when Gn is the input graph. [Hint: Towers of Hanoi.] 4. Prove that Dijkstra’s shortest-path algorithm can require exponential time in the worst case when edges are allowed to have negative weight. Specifically, for every positive integer n, construct a weighted directed n-vertex graph Gn , such that Dijkstra’s algorithm calls RELAX Ω(2n ) times when Gn is the input graph. [Hint: This should be easy if you’ve already solved the previous problem.] 5. A looped tree is a weighted, directed graph built from a binary tree by adding an edge from every leaf back to the root. Every edge has a non-negative weight. 5 4 17 0 23 8 9 16 A looped tree. 8 42 1 14 7 Algorithms Lecture 13: Shortest Paths (a) How much time would Dijkstra’s algorithm require to compute the shortest path between two vertices u and v in a looped tree with n nodes? (b) Describe and analyze a faster algorithm. 6. (a) Describe a modification of Ford’s generic shortest-path algorithm that actually returns a negative cycle if any such cycle is reachable from s, or a shortest-path tree if there is no such cycle. You may assume that the unmodified algorithm halts in O(2V ) steps if there is no negative cycle. (b) Describe a modification of Shimbel’s shortest-path algorithm that actually returns a negative cycle if any such cycle is reachable from s, or a shortest-path tree if there is no such cycle. The modified algorithm should still run in O(V E ) time. 7. For any edge e in any graph G , let G \ e denote the graph obtained by deleting e from G . (a) Suppose we are given a directed graph G in which the shortest path σ from vertex s to vertex t passes through every vertex of G . Describe an algorithm to compute the shortest-path distance from s to t in G \ e, for every edge e of G , in O( E log V ) time. Your algorithm should output a set of E shortest-path distances, one for each edge of the input graph. You may assume that all edge weights are non-negative. [Hint: If we delete an edge of the original shortest path, how do the old and new shortest paths overlap?] (b) Let s and t be arbitrary vertices in an arbitrary directed graph G . Describe an algorithm to compute the shortest-path distance from s to t in G \ e, for every edge e of G , in O( E log V ) time. Again, you may assume that all edge weights are non-negative. 8. Let G = (V, E ) be a connected directed graph with non-negative edge weights, let s and t be vertices of G , and let H be a subgraph of G obtained by deleting some edges. Suppose we want to reinsert exactly one edge from G back into H , so that the shortest path from s to t in the resulting graph is as short as possible. Describe and analyze an algorithm that chooses the best edge to reinsert, in O( E log V ) time. 9. Negative edges cause problems in shortest-path algorithms because of the possibility of negative cycles. But what if the input graph has no cycles? (a) Describe an efficient algorithm to compute the shortest path between two nodes s and t in a given directed acyclic graph with weighted edges. The edge weights could be positive, negative, or zero. (b) Describe an efficient algorithm to compute the longest path between two nodes s and t in a given directed acyclic graph with weighted edges. 10. After a grueling algorithms midterm, you decide to take the bus home. Since you planned ahead, you have a schedule that lists the times and locations of every stop of every bus in ChampaignUrbana. Unfortunately, there isn’t a single bus that visits both your exam building and your home; you must transfer between bus lines at least once. Describe and analyze an algorithm to determine the sequence of bus rides that will get you home as early as possible, assuming there are b different bus lines, and each bus stops n times per 9 Algorithms Lecture 13: Shortest Paths day. Your goal is to minimize your arrival time, not the time you actually spend traveling. Assume that the buses run exactly on schedule, that you have an accurate watch, and that you are too tired to walk between bus stops. 11. After graduating you accept a job with Aerophobes-R-Us, the leading traveling agency for people who hate to fly. Your job is to build a system to help customers plan airplane trips from one city to another. All of your customers are afraid of flying (and by extension, airports), so any trip you plan needs to be as short as possible. You know all the departure and arrival times of all the flights on the planet. Suppose one of your customers wants to fly from city X to city Y . Describe an algorithm to find a sequence of flights that minimizes the total time in transit—the length of time from the initial departure to the final arrival, including time at intermediate airports waiting for connecting flights. [Hint: Modify the input data and apply Dijkstra’s algorithm.] 12. Mulder and Scully have computed, for every road in the United States, the exact probability that someone driving on that road won’t be abducted by aliens. Agent Mulder needs to drive from Langley, Virginia to Area 51, Nevada. What route should he take so that he has the least chance of being abducted? More formally, you are given a directed graph G = (V, E ), where every edge e has an independent safety probability p(e). The safety of a path is the product of the safety probabilities of its edges. Design and analyze an algorithm to determine the safest path from a given start vertex s to a given target vertex t . Las Vegas, NV 0.1 0.5 0.2 0.7 Memphis, TN 0.9 Langley, VA 0.5 Area 51, AZ For example, with the probabilities shown above, if Mulder tries to drive directly from Langley to Area 51, he has a 50% chance of getting there without being abducted. If he stops in Memphis, he has a 0.7 × 0.9 = 63% chance of arriving safely. If he stops first in Memphis and then in Las Vegas, he has a 1 − 0.7 × 0.1 × 0.5 = 96.5% chance of being abducted! (That’s how they got Elvis, you know.) 13. On an overnight camping trip in Sunnydale National Park, you are woken from a restless sleep by a scream. As you crawl out of your tent to investigate, a terrified park ranger runs out of the woods, covered in blood and clutching a crumpled piece of paper to his chest. As he reaches your tent, he gasps, “Get out. . . while. . . you. . . ”, thrusts the paper into your hands, and falls to the ground. Checking his pulse, you discover that the ranger is stone dead. You look down at the paper and recognize a map of the park, drawn as an undirected graph, where vertices represent landmarks in the park, and edges represent trails between those 10 Algorithms Lecture 13: Shortest Paths landmarks. (Trails start and end at landmarks and do not cross.) You recognize one of the vertices as your current location; several vertices on the boundary of the map are labeled EXIT. On closer examination, you notice that someone (perhaps the poor dead park ranger) has written a real number between 0 and 1 next to each vertex and each edge. A scrawled note on the back of the map indicates that a number next to an edge is the probability of encountering a vampire along the corresponding trail, and a number next to a vertex is the probability of encountering a vampire at the corresponding landmark. (Vampires can’t stand each other’s company, so you’ll never see more than one vampire on the same trail or at the same landmark.) The note warns you that stepping off the marked trails will result in a slow and painful death. You glance down at the corpse at your feet. Yes, his death certainly looked painful. Wait, was that a twitch? Are his teeth getting longer? After driving a tent stake through the undead ranger’s heart, you wisely decide to leave the park immediately. Describe and analyze an efficient algorithm to find a path from your current location to an arbitrary EXIT node, such that the total expected number of vampires encountered along the path is as small as possible. Be sure to account for both the vertex probabilities and the edge probabilities! — Randall Munroe, xkcd (http://xkcd.com/c69.html) c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 11 Algorithms Lecture 14: All-Pairs Shortest Paths The tree which fills the arms grew from the tiniest sprout; the tower of nine storeys rose from a (small) heap of earth; the journey of a thousand li commenced with a single step. — Lao-Tzu, Tao Te Ching, chapter 64 (6th century BC), translated by J. Legge (1891) And I would walk five hundred miles, And I would walk five hundred more, Just to be the man who walks a thousand miles To fall down at your door. — The Proclaimers, “Five Hundred Miles (I’m Gonna Be)”, Sunshine on Leith (2001) Almost there. . . Almost there. . . — Red Leader [Drewe Henley], Star Wars (1977) 14 All-Pairs Shortest Paths 14.1 The Problem In the previous lecture, we saw algorithms to find the shortest path from a source vertex s to a target vertex t in a directed graph. As it turns out, the best algorithms for this problem actually find the shortest path from s to every possible target (or from every possible source to t ) by constructing a shortest path tree. The shortest path tree specifies two pieces of information for each node v in the graph: • dist( v ) is the length of the shortest path (if any) from s to v ; • pred( v ) is the second-to-last vertex (if any) the shortest path (if any) from s to v . In this lecture, we want to generalize the shortest path problem even further. In the all pairs shortest path problem, we want to find the shortest path from every possible source to every possible destination. Specifically, for every pair of vertices u and v , we need to compute the following information: • dist(u, v ) is the length of the shortest path (if any) from u to v ; • pred(u, v ) is the second-to-last vertex (if any) on the shortest path (if any) from u to v . For example, for any vertex v , we have dist( v, v ) = 0 and pred( v, v ) = NULL. If the shortest path from u to v is only one edge long, then dist(u, v ) = w (u→ v ) and pred(u, v ) = u. If there is no shortest path from u to v —either because there’s no path at all, or because there’s a negative cycle—then dist(u, v ) = ∞ and pred( v, v ) = NULL. The output of our shortest path algorithms will be a pair of V × V arrays encoding all V 2 distances and predecessors. Many maps include a distance matrix—to find the distance from (say) Champaign to (say) Columbus, you would look in the row labeled ‘Champaign’ and the column labeled ‘Columbus’. In these notes, I’ll focus almost exclusively on computing the distance array. The predecessor array, from which you would compute the actual shortest paths, can be computed with only minor additions to the algorithms I’ll describe (hint, hint). 14.2 Lots of Single Sources The obvious solution to the all-pairs shortest path problem is just to run a single-source shortest path algorithm V times, once for every possible source vertex! Specifically, to fill in the one-dimensional subarray dist[s, ·], we invoke either Dijkstra’s or Shimbel’s algorithm starting at the source vertex s. 1 Algorithms Lecture 14: All-Pairs Shortest Paths OBVIOUSAPSP(V, E , w ): for every vertex s dist[s, ·] ← SSSP(V, E , w, s) The running time of this algorithm depends on which single-source shortest path algorithm we use. If we use Shimbel’s algorithm, the overall running time is Θ(V 2 E ) = O(V 4 ). If all the edge weights are non-negative, we can use Dijkstra’s algorithm instead, which decreases the running time to Θ(V E + V 2 log V ) = O(V 3 ). For graphs with negative edge weights, Dijkstra’s algorithm can take exponential time, so we can’t get this improvement directly. 14.3 Reweighting One idea that occurs to most people is increasing the weights of all the edges by the same amount so that all the weights become positive, and then applying Dijkstra’s algorithm. Unfortunately, this simple idea doesn’t work. Different paths change by different amounts, which means the shortest paths in the reweighted graph may not be the same as in the original graph. 3 s 2 2 4 t 4 Increasing all the edge weights by 2 changes the shortest path s to t . However, there is a more complicated method for reweighting the edges in a graph. Suppose each vertex v has some associated cost c ( v ), which might be positive, negative, or zero. We can define a new weight function w as follows: w (u→ v ) = c (u) + w (u→ v ) − c ( v ) To give some intuition, imagine that when we leave vertex u, we have to pay an exit tax of c (u), and when we enter v , we get c ( v ) as an entrance gift. Now it’s not too hard to show that the shortest paths with the new weight function w are exactly the same as the shortest paths with the original weight function w . In fact, for any path u v from one vertex u to another vertex v , we have w (u v ) = c (u) + w (u v ) − c ( v ). We pay c (u) in exit fees, plus the original weight of of the path, minus the c ( v ) entrance gift. At every intermediate vertex x on the path, we get c ( x ) as an entrance gift, but then immediately pay it back as an exit tax! 14.4 Johnson’s Algorithm Johnson’s all-pairs shortest path algorithm finds a cost c ( v ) for each vertex, so that when the graph is reweighted, every edge has non-negative weight. Suppose the graph has a vertex s that has a path to every other vertex. Johnson’s algorithm computes the shortest paths from s to every other vertex, using Shimbel’s algorithm (which doesn’t care if the edge weights are negative), and then sets c ( v ) = dist(s, v ), 2 Algorithms Lecture 14: All-Pairs Shortest Paths so the new weight of every edge is w (u→ v ) = dist(s, u) + w (u→ v ) − dist(s, v ). Why are all these new weights non-negative? Because otherwise, Shimbel’s algorithm wouldn’t be finished! Recall that an edge u→ v is tense if dist(s, u) + w (u→ v ) < dist(s, v ), and that single-source shortest path algorithms eliminate all tense edges. The only exception is if the graph has a negative cycle, but then shortest paths aren’t defined, and Johnson’s algorithm simply aborts. But what if the graph doesn’t have a vertex s that can reach everything? No matter where we start Shimbel’s algorithm, some of those vertex costs will be infinite. Johnson’s algorithm avoids this problem by adding a new vertex s to the graph, with zero-weight edges going from s to every other vertex, but no edges going back into s. This addition doesn’t change the shortest paths between any other pair of vertices, because there are no paths into s. So here’s Johnson’s algorithm in all its glory. JOHNSONAPSP(V, E , w ) : create a new vertex s for every vertex v ∈ V w (s→ v ) ← 0 w ( v →s ) ← ∞ dist[s, ·] ← SHIMBEL(V, E , w, s) abort if SHIMBEL found a negative cycle for every edge (u, v ) ∈ E w (u→ v ) ← dist[s, u] + w (u→ v ) − dist[s, v ] for every vertex u ∈ V dist[u, ·] ← DIJKSTRA(V, E , w , u) for every vertex v ∈ V dist[u, v ] ← dist[u, v ] − dist[s, u] + dist[s, v ] The algorithm spends Θ(V ) time adding the artificial start vertex s, Θ(V E ) time running SHIMBEL, O( E ) time reweighting the graph, and then Θ(V E + V 2 log V ) running V passes of Dijkstra’s algorithm. Thus, the overall running time is Θ(V E + V 2 log V ). 14.5 Dynamic Programming There’s a completely different solution to the all-pairs shortest path problem that uses dynamic programming instead of a single-source algorithm. For dense graphs where E = Ω(V 2 ), the dynamic programming approach eventually leads to the same O(V 3 ) running time as Johnson’s algorithm, but with a much simpler algorithm. In particular, the new algorithm avoids Dijkstra’s algorithm, which gets its efficiency from Fibonacci heaps, which are rather easy to screw up in the implementation. In the rest of this lecture, I will assume that the input graph contains no negative cycles. As usual for dynamic programming algorithms, we first need to come up with a recursive formulation of the problem. Here is an “obvious" recursive definition for dist(u, v ): dist(u, v ) = if u = v 0 min dist(u, x ) + w ( x → v ) x otherwise In other words, to find the shortest path from u to v , try all possible predecessors x , compute the shortest path from u to x , and then add the last edge u→ v . Unfortunately, this recurrence doesn’t work! In 3 Algorithms Lecture 14: All-Pairs Shortest Paths order to compute dist(u, v ), we first have to compute dist(u, x ) for every other vertex x , but to compute any dist(u, x ), we first need to compute dist(u, v ). We’re stuck in an infinite loop! To avoid this circular dependency, we need an additional parameter that decreases at each recursion, eventually reaching zero at the base case. One possibility is to include the number of edges in the shortest path as this third magic parameter. So let’s define dist(u, v, k) to be the length of the shortest path from u to v that uses at most k edges. Since we know that the shortest path between any two vertices has at most V − 1 vertices, what we’re really trying to compute is dist(u, v, V − 1). After a little thought, we get the following recurrence. 0 if u = v if k = 0 and u = v dist(u, v, k) = ∞ min dist(u, x , k − 1) + w ( x → v ) otherwise x Just like last time, the recurrence tries all possible predecessors of v in the shortest path, but now the recursion actually bottoms out when k = 0. Now it’s not difficult to turn this recurrence into a dynamic programming algorithm. Even before we write down the algorithm, though, we can tell that its running time will be Θ(V 4 ) simply because recurrence has four variables—u, v , k, and x —each of which can take on V different values. Except for the base cases, the algorithm itself is just four nested for loops. To make the algorithm a little shorter, let’s assume that w ( v → v ) = 0 for every vertex v . DYNAMICPROGRAMMINGAPSP(V, E , w ): for all vertices u ∈ V for all vertices v ∈ V if u = v dist[u, v, 0] ← 0 else dist[u, v, 0] ← ∞ for k ← 1 to V − 1 for all vertices u ∈ V for all vertices v ∈ V dist[u, v, k] ← ∞ for all vertices x ∈ V dist[u, v, k] ← min dist[u, v, k], dist[u, x , k − 1] + w ( x → v ) The last four lines actually evaluate the recurrence. In fact, this algorithm is almost exactly the same as running Shimbel’s algorithm once for every source vertex. The only difference is the innermost loop, which in Shimbel’s algorithm would read “for all edges x → v ”. This simple change improves the running time to Θ(V 2 E ), assuming the graph is stored in an adjacency list. 14.6 Divide and Conquer But we can make a more significant improvement. The recurrence we just used broke the shortest path into a slightly shorter path and a single edge, by considering all predecessors. Instead, let’s break it into two shorter paths at the middle vertex on the path. This idea gives us a different recurrence for dist(u, v, k). Once again, to simplify things, let’s assume w ( v → v ) = 0. dist(u, v, k) = w (u→ v ) if k = 1 min dist(u, x , k/2) + dist( x , v, k/2) otherwise x 4 Algorithms Lecture 14: All-Pairs Shortest Paths This recurrence only works when k is a power of two, since otherwise we might try to find the shortest path with a fractional number of edges! But that’s not really a problem, since dist(u, v, 2 lg V ) gives us the overall shortest distance from u to v . Notice that we use the base case k = 1 instead of k = 0, since we can’t use half an edge. Once again, a dynamic programming solution is straightforward. Even before we write down the algorithm, we can tell the running time is Θ(V 3 log V )—we consider V possible values of u, v , and x , but only lg V possible values of k. FASTDYNAMICPROGRAMMINGAPSP(V, E , w ): for all vertices u ∈ V for all vertices v ∈ V dist[u, v, 0] ← w (u→ v ) for i ← 1 to lg V 〈〈k = 2i 〉〉 for all vertices u ∈ V for all vertices v ∈ V dist[u, v, i ] ← ∞ for all vertices x ∈ V if dist[u, v, i ] > dist[u, x , i − 1] + dist[ x , v, i − 1] dist[u, v, i ] ← dist[u, x , i − 1] + dist[ x , v, i − 1] 14.7 Aside: ‘Funny’ Matrix Multiplication There is a very close connection (first observed by Shimbel, and later independently by Bellman) between computing shortest paths in a directed graph and computing powers of a square matrix. Compare the following algorithm for multiplying two n × n matrices A and B with the inner loop of our first dynamic programming algorithm. (I’ve changed the variable names in the second algorithm slightly to make the similarity clearer.) MATRIXMULTIPLY(A, B ): for i ← 1 to n for j ← 1 to n C [i , j ] ← 0 for k ← 1 to n C [i , j ] ← C [i , j ] + A[i , k] · B [k, j ] APSPINNERLOOP: for all vertices u for all vertices v D [u, v ] ← ∞ for all vertices x D [u, v ] ← min D [u, v ], D[u, x ] + w [ x , v ] The only difference between these two algorithms is that we use addition instead of multiplication and minimization instead of addition. For this reason, the shortest path inner loop is often referred to as ‘funny’ matrix multiplication. DYNAMICPROGRAMMINGAPSP is the standard iterative algorithm for computing the (V − 1)th ‘funny power’ of the weight matrix w . The first set of for loops sets up the ‘funny identity matrix’, with zeros on the main diagonal and infinity everywhere else. Then each iteration of the second main for loop computes the next ‘funny power’. FASTDYNAMICPROGRAMMINGAPSP replaces this iterative method for computing powers with repeated squaring, exactly like we saw at the beginning of the semester. The fast algorithm is simplified slightly by the fact that unless there are negative cycles, every ‘funny power’ after the V th is the same. 5 Algorithms Lecture 14: All-Pairs Shortest Paths There are faster methods for multiplying matrices, similar to Karatsuba’s divide-and-conquer algorithm for multiplying integers. (Google for ‘Strassen’s algorithm’.) Unfortunately, these algorithms us subtraction, and there’s no ‘funny’ equivalent of subtraction. (What’s the inverse operation for min?) So at least for general graphs, there seems to be no way to speed up the inner loop of our dynamic programming algorithms. Fortunately, this isn’t true. There is a beautiful randomized algorithm, due to Noga Alon, Zvi Galil, Oded Margalit*, and Moni Noar,1 that computes all-pairs shortest paths in undirected graphs in O( M (V ) log2 V ) expected time, where M (V ) is the time to multiply two V × V integer matrices. A simplified version of this algorithm for unweighted graphs was discovered by Raimund Seidel.2 14.8 Floyd and Warshall’s Algorithm Our fast dynamic programming algorithm is still a factor of O(log V ) slower than Johnson’s algorithm. A different formulation due to Floyd and Warshall removes this logarithmic factor. Their insight was to use a different third parameter in the recurrence. Number the vertices arbitrarily from 1 to V . For every pair of vertices u and v and every integer r , we define a path π(u, v, r ) as follows: π(u, v, r ) := the shortest path from u to v where every intermediate vertex (that is, every vertex except u and v ) is numbered at most r . If r = 0, we aren’t allowed to use any intermediate vertices, so π(u, v, 0) is just the edge (if any) from u to v . If r > 0, then either π(u, v, r ) goes through the vertex numbered r , or it doesn’t. If π(u, v, r ) does contain vertex r , it splits into a subpath from u to r and a subpath from r to v , where every intermediate vertex in these two subpaths is numbered at most r − 1. Moreover, the subpaths are as short as possible with this restriction, so they must be π(u, r, r − 1) and π( r, v, r − 1). On the other hand, if π(u, v, r ) does not go through vertex r , then every intermediate vertex in π(u, v, r ) is numbered at most r − 1; since π(u, v, r ) must be the shortest such path, we have π(u, v, r ) = π(u, v, r − 1). u intermediate nodes ≤ r v = intermediate nodes ≤ r-1 u — int no erm de ed s ≤ iat r-1 e or — r v te dia me r-1 ter s ≤ in de no Recursive structure of the restricted shortest path π(u, v, r ). This recursive structure implies the following recurrence for the length of π(u, v, r ), which we will denote by dist(u, v, r ): dist(u, v, r ) = w (u→ v ) if r = 0 min dist(u, v, r − 1), dist(u, r, r − 1) + dist( r, v, r − 1) otherwise We need to compute the shortest path distance from u to v with no restrictions, which is just dist(u, v, V ). 1 N. Alon, Z. Galil, O. Margalit*, and M. Naor. Witnesses for Boolean matrix multiplication and for shortest paths. Proc. 33rd FOCS 417-426, 1992. See also N. Alon, Z. Galil, O. Margalit*. On the exponent of the all pairs shortest path problem. Journal of Computer and System Sciences 54(2):255–262, 1997. 2 R. Seidel. On the all-pairs-shortest-path problem in unweighted undirected graphs. Journal of Computer and System Sciences, 51(3):400-403, 1995. This is one of the few algorithms papers where (in the conference version at least) the algorithm is completely described and analyzed in the abstract of the paper. 6 Algorithms Lecture 14: All-Pairs Shortest Paths Once again, we should immediately see that a dynamic programming algorithm that implements this recurrence will run in Θ(V 3 ) time: three variables appear in the recurrence (u, v , and r ), each of which can take on V possible values. Here’s one way to do it: FLOYDWARSHALL(V, E , w ): for u ← 1 to V for v ← 1 to V dist[u, v, 0] ← w (u→ v ) for r ← 1 to V for u ← 1 to V for v ← 1 to V if dist[u, v, r − 1] < dist[u, r, r − 1] + dist[ r, v, r − 1] dist[u, v, r ] ← dist[u, v, r − 1] else dist[u, v, r ] ← dist[u, r, r − 1] + dist[ r, v, r − 1] Exercises 1. Let G = (V, E ) be a directed graph with weighted edges; edge weights could be positive, negative, or zero. (a) How could we delete some node v from this graph, without changing the shortest-path distance between any other pair of nodes? Describe an algorithm that constructs a directed graph G = (V , E ) with weighted edges, where V = V \ { v }, and the shortest-path distance between any two nodes in H is equal to the shortest-path distance between the same two nodes in G , in O(V 2 ) time. (b) Now suppose we have already computed all shortest-path distances in G . Describe an algorithm to compute the shortest-path distances from v to every other node, and from every other node to v , in the original graph G , in O(V 2 ) time. (c) Combine parts (a) and (b) into another all-pairs shortest path algorithm that runs in O(V 3 ) time. 2. All of the algorithms discussed in this lecture fail if the graph contains a negative cycle. Johnson’s algorithm detects the negative cycle in the initialization phase (via Shimbel’s algorithm) and aborts; the dynamic programming algorithms just return incorrect results. However, all of these algorithms can be modified to return correct shortest-path distances, even in the presence of negative cycles. Specifically, if there is a path from vertex u to a negative cycle and a path from that negative cycle to vertex v , the algorithm should report that dist[u, v ] = −∞. If there is no directed path from u to v , the algorithm should return dist[u, v ] = ∞. Otherwise, dist[u, v ] should equal the length of the shortest directed path from u to v . (a) Describe how to modify Johnson’s algorithm to return the correct shortest-path distances, even if the graph has negative cycles. (b) Describe how to modify the Floyd-Warshall algorithm to return the correct shortest-path distances, even if the graph has negative cycles. 3. Let G = (V, E ) be a directed graph with weighted edges; edge weights could be positive, negative, or zero. Suppose the vertices of G are partitioned into k disjoint subsets V1 , V2 , . . . , Vk ; that is, 7 Algorithms Lecture 14: All-Pairs Shortest Paths every vertex of G belongs to exactly one subset Vi . For each i and j , let δ(i , j ) denote the minimum shortest-path distance between vertices in Vi and vertices in Vj : δ(i , j ) = min{dist(u, v ) | u ∈ Vi and v ∈ Vj }. Describe an algorithm to compute δ(i , j ) for all i and j in time O(V 2 + kE log E ). 4. Recall3 that a deterministic finite automaton (DFA) is formally defined as a tuple M = (Σ, Q, q0 , F, δ), where the finite set Σ is the input alphabet, the finite set Q is the set of states, q0 ∈ Q is the start state, F ⊆ Q is the set of final (accepting) states, and δ : Q × Σ → Q is the transition function. Equivalently, a DFA is a directed (multi-)graph with labeled edges, such that each symbol in Σ is the label of exactly one edge leaving any vertex. There is a special ‘start’ vertex q0 , and a subset of the vertices are marked as ‘accepting states’. Any string in Σ∗ describes a unique walk starting at q0 ; a string in Σ∗ is accepted by M if this walk ends at a vertex in F . Stephen Kleene4 proved that the language accepted by any DFA is identical to the language described by some regular expression. This problem asks you to develop a variant of the FloydWarshall all-pairs shortest path algorithm that computes a regular expression that is equivalent to the language accepted by a given DFA. Suppose the input DFA M has n states, numbered from 1 to n, where (without loss of generality) the start state is state 1. Let L (i , j , r ) denote the set of all words that describe walks in M from state i to state j , where every intermediate state lies in the subset {1, 2, . . . , r }; thus, the language accepted by the DFA is exactly L (1, q, n). q∈ F Let R(i , j , r ) be a regular expression that describes the language L (i , j , r ). (a) What is the regular expression R(i , j , 0)? (b) Write a recurrence for the regular expression R(i , j , r ) in terms of regular expressions of the form R(i , j , r − 1). (c) Describe a polynomial-time algorithm to compute R(i , j , n) for all states i and j . (Assume that you can concatenate two regular expressions in O(1) time.) 5. Let G = (V, E ) be an undirected, unweighted, connected, n-vertex graph, represented by the adjacency matrix A[1 .. n, 1 .. n]. In this problem, we will derive Seidel’s sub-cubic algorithm to compute the n × n matrix D[1 .. n, 1 .. n] of shortest-path distances using fast matrix multiplication. Assume that we have a subroutine MATRIXMULTIPLY that multiplies two n × n matrices in Θ(nω ) time, for some unknown constant ω ≥ 2.5 (a) Let G 2 denote the graph with the same vertices as G , where two vertices are connected by a edge if and only if they are connected by a path of length at most 2 in G . Describe an algorithm to compute the adjacency matrix of G 2 using a single call to MATRIXMULTIPLY and O(n2 ) additional time. 3 Automata theory is a prerequisite for the algorithms class at UIUC. Pronounced ‘clay knee’, not ‘clean’ or ‘clean-ee’ or ‘clay-nuh’ or ‘dimaggio’. 5 The matrix multiplication algorithm you already know runs in Θ(n3 ) time, but this is not the fastest algorithm known. The current record is ω ≈ 2.376, due to Don Coppersmith and Shmuel Winograd. Determining the smallest possible value of ω is a long-standing open problem; many people believe there is an undiscovered O(n2 )-time algorithm for matrix multiplication. 4 8 Algorithms Lecture 14: All-Pairs Shortest Paths (b) Suppose we discover that G 2 is a complete graph. Describe an algorithm to compute the matrix D of shortest path distances in O(n2 ) additional time. (c) Let D2 denote the (recursively computed) matrix of shortest-path distances in G 2 . Prove that the shortest-path distance from node i to node j is either 2 · D2 [i , j ] or 2 · D2 [i , j ] − 1. (d) Suppose G 2 is not a complete graph. Let X = D2 · A, and let deg(i ) denote the degree of vertex i in the original graph G . Prove that the shortest-path distance from node i to node j is 2 · D2 [i , j ] if and only if X [i , j ] ≥ D2 [i , j ] · deg(i ). (e) Describe an algorithm to compute the matrix of shortest-path distances in G in O(nω log n) time. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 9 Algorithms Lecture 15: Maximum Flows and Minimum Cuts Col. Hogan: One of these wires disconnects the fuse, the other one fires the bomb. Which one would you cut, Shultz? Sgt. Schultz: Don’t ask me, this is a decision for an officer. Col. Hogan: All right. Which wire, Colonel Klink? Col. Klink: This one. [points to the white wire] Col. Hogan: You’re sure? Col. Klink: Yes. [Hogan cuts the black wire; the bomb stops ticking] Col. Klink: If you knew which wire it was, why did you ask me? Col. Hogan: I wasn’t sure which was the right one, but I was certain you’d pick the wrong one. — “A Klink, a Bomb, and a Short Fuse", Hogan’s Heroes (1966) 15 Maximum Flows and Minimum Cuts In the mid-1950s, Air Force researchers T. E. Harris and F. S. Ross published a classified report studying the rail network that linked the Soviet Union to its satellite countries in Eastern Europe. The network was modeled as a graph with 44 vertices, representing geographic regions, and 105 edges, representing links between those regions in the rail network. Each edge was given a weight, representing the rate at which material could be shipped from one region to the next. Essentially by trial and error, they determined both the maximum amount of stuff that could be moved from Russia into Europe, as well as the cheapest way to disrupt the network by removing links (or in less abstract terms, blowing up train tracks), which they called ‘the bottleneck’. Their results (including the figure at the top of the page) were only declassified in 1999.1 Harris and Ross’s map of the Warsaw Pact rail network This one of the firstFrom Harris and Ross [1955]: Schematicofagure 2ofmaximum oflowheanderminimum cut problems. For recorded applications Fiig the the railway netw rk of t West n Sod ram vie U o and Eastern E ropean co G s, ( h , E xi um flow o value 1 ,000 tons fr vertices s and t called the both problems, the inputtisnian directedugraphuntrie=witVa ma),malong f with63specialom Russia to Eastern Europe, and a cut of capacity 163,000 tons indicated as ‘The bottleneck’. source and target. As in the previous lectures, I will use u v to denote the directed edge from vertex u to vertex v . Intuitively, the maximum flow problem asks for the largest amount of material that can The max-flow min-cut theorem be transported from one vertex to another; the minimum cut problem asks for the minimum damage In the RAND Report of 19 November 1954, Ford and Fulkerson [1954] gave (next to defining needed to separate two maximum flow problem and suggesting the simplex method for it) the max-flow minthe vertices. 1 cut theorem for undirected graphs, saying that the maximum flow value is equal to the Both the map and the storymwere itakencfromarating source and terminal. Their proof is nsurveyructive, history of combinatorial minimu capac ty of a ut sep Alexander Schrijver’s fascinating ot const ‘On the optimization (till 1960)’. but for planar graphs, with source and sink on the outer boundary, they give a polynomialtime, constructive method. In a report of 26 May 1955, Robacker [1955a] showed that the max-flow min-cut theorem can be derived also from the vertex-disjoint version of Menger’s theorem. 1 As for the directed case, Ford and Fulkerson [1955] observed that the max-flow min-cut theorem holds also for directed graphs. Dantzig and Fulkerson [1955] showed, by extending the results of Dantzig [1951a] on integer solutions for the transportation problem to the 25 Algorithms 15.1 Lecture 15: Maximum Flows and Minimum Cuts Flows An ( s , t )-flow (or just a flow if the source and target are clear from context) is a function f : E → IR≥0 that satisfies the following conservation constraint at every vertex v except possibly s and t : f (u v ) = f ( v w ). u w In English, the total flow into v is equal to the total flow out of v . To keep the notation simple, we assume here that f (u v ) = 0 if there is no edge u v in the graph. The value of the flow f , denoted | f |, is defined as the excess flow out of the source vertex s: | f | := f (s w ) − f (u s) w u It’s not hard to show that the value | f | is also equal to the excess flow into the target vertex t . First we observe that f (v w) − v w f (u v ) = f (v w) − u v w f (u v ) = 0 v u because both summations count the total flow across all edges. On the other hand, the conservation constraint implies that f (v w) − v f (u v ) u w = f (t w) − w f (u s) f (t w) − + u = |f | + w f (t w) − w f (u t ) f (u t ) u . u It follows that |f | = f (u t ) − u f ( t w ). w Now suppose we have another function c : E → IR≥0 that assigns a non-negative capacity c (e) to each edge e. We say that a flow f is feasible (with respect to c ) if f (e) ≤ c (e) for every edge e. Most of the time we will consider only flows that are feasible with respect to some fixed capacity function c . We say that a flow f saturates edge e if f (e) = c (e), and avoids edge e if f (e) = 0. The maximum flow problem is to compute a feasible (s, t )-flow in a given directed graph, with a given capacity function, whose value is as large as possible. 0/5 5/15 10/20 0/15 s 5/10 10/10 0/10 t 5/20 10/10 An (s, t )-flow with value 10. Each edge is labeled with its flow/capacity. 2 Algorithms 15.2 Lecture 15: Maximum Flows and Minimum Cuts Cuts An ( s , t )-cut (or just cut if the source and target are clear from context) is a partition of the vertices into disjoint subsets S and T —meaning S ∪ T = V and S ∩ T = ∅—where s ∈ S and t ∈ T . If we have a capacity function c : E → IR≥0 , the capacity of a cut is the sum of the capacities of the edges that start in S and end in T : S, T = c ( v w ). v ∈S w ∈ T (Again, if v w is not an edge in the graph, we assume c ( v w ) = 0.) Notice that the definition is asymmetric; edges that start in T and end in S are unimportant. The minimum cut problem is to compute an (s, t )-cut whose capacity is as large as possible. 5 15 20 10 s 15 10 10 t 20 10 An (s, t )-cut with capacity 15. Each edge is labeled with its capacity. Intuitively, the minimum cut is the cheapest way to disrupt all flow from s to t . Indeed, it is not hard to show that the value of any feasible ( s , t )-flow is at most the capacity of any ( s , t )-cut. Choose your favorite flow f and your favorite cut (S , T ), and then follow the bouncing inequalities: |f | = f (s w ) − w f (u s) by definition u = f (v w) − v ∈S w v ∈S w∈T f (u v ) by the convervation constraint f (u v ) removing duplicate edges u = f (v w) − u∈ T f (v w) since f (u v ) ≥ 0 c(v w) ≤ since f (u v ) ≤ c ( v w ) v ∈S w ∈ T ≤ v ∈S w ∈ T = S, T by definition Our derivation actually implies the following stronger observation: | f | = S, T if and only if f saturates every edge from S to T and avoids every edge from T to S. Moreover, if we have a flow f and a cut (S , T ) that satisfies this equality condition, f must be a maximum flow, and (S , T ) must be a minimum cut. 3 Algorithms 15.3 Lecture 15: Maximum Flows and Minimum Cuts The Max-Flow Min-Cut Theorem Surprisingly, for any weighted directed graph, there is always a flow f and a cut (S , T ) that satisfy the equality condition. This is the famous max-flow min-cut theorem: The value of the maximum flow is equal to the capacity of the minimum cut. The rest of this section gives a proof of this theorem; we will eventually turn this proof into an algorithm. Fix a graph G , vertices s and t , and a capacity function c : E → IR≥0 . The proof will be easier if we assume that the capacity function is reduced: For any vertices u and v , either c (u v ) = 0 or c ( v u) = 0, or equivalently, if an edge appears in G , then its reversal does not. This assumption is easy to enforce. Whenever an edge u v and its reversal v u are both the graph, replace the edge u v with a path u x v of length two, where x is a new vertex and c (u x ) = c ( x v ) = c (u v ). The modified graph has the same maximum flow value and minimum cut capacity as the original graph. Enforcing the one-direction assumption. Let f be a feasible flow. We define a new capacity function c f : V × V → IR, called the residual capacity, as follows: c (u v ) − f (u v ) if u v ∈ E c f (u v ) = f ( v u) 0 if v u ∈ E . otherwise Since f ≥ 0 and f ≤ c , the residual capacities are always non-negative. It is possible to have c f (u v ) > 0 even if u v is not an edge in the original graph G . Thus, we define the residual graph G f = (V, E f ), where E f is the set of edges whose residual capacity is positive. Notice that the residual capacities are not necessarily reduced; it is quite possible to have both c f (u v ) > 0 and c f ( v u) > 0. 0/5 5 5/10 10/10 0/10 5 10 0/15 s 10 10 5/15 10/20 t s 10 15 5 5 5 10 5/20 10/10 t 15 10 A flow f in a weighted graph G and the corresponding residual graph G f . Suppose there is no path from the source s to the target t in the residual graph G f . Let S be the set of vertices that are reachable from s in G f , and let T = V \ S . The partition (S , T ) is clearly an (s, t )-cut. For every vertex u ∈ S and v ∈ T , we have c f (u v ) = (c (u v ) − f (u v )) + f ( v u) = 0, which implies that c (u v ) − f (u v ) = 0 and f ( v u) = 0. In other words, our flow f saturates every edge from S to T and avoids every edge from T to S . It follows that | f | = S , T . Moreover, f is a maximum flow and (S , T ) is a minimum cut. 4 Algorithms Lecture 15: Maximum Flows and Minimum Cuts 5 5/5 10 10 s 5/15 10/20 5 10 10 15 5 0/15 5 t s 0/10 5/10 t 5 10 5/10 15 10 10/20 10/10 An augmenting path in G f with value F = 5 and the augmented flow f . On the other hand, suppose there is a path s = v0 v1 · · · vr = t in G f . We refer to v0 v1 · · · vr as an augmenting path. Let F = mini c f ( vi vi +1 ) denote the maximum amount of flow that we can push through the augmenting path in G f . We define a new flow function f : E → IR as follows: f (u v ) + F f (u v ) = f (u v ) − F f (u v ) if u v is in the augmenting path if v u is in the augmenting path otherwise To prove that the flow f is feasible with respect to the original capacities c , we need to verify that f ≥ 0 and f ≤ c . Consider an edge u v in G . If u v is in the augmenting path, then f (u v ) > f (u v ) ≥ 0 and f (u v ) = f (u v ) + F by definition of f ≤ f (u v ) + c f (u v ) by definition of F = f (u v ) + c (u v ) − f (u v ) by definition of c f = c (u v ) Duh. On the other hand, if the reversal v u is in the augmenting path, then f (u v ) < f (u v ) ≤ c (u v ), which implies that f (u v ) = f (u v ) − F by definition of f ≥ f (u v ) − c f ( v u) by definition of F = f (u v ) − f (u v ) by definition of c f =0 Duh. Finally, we observe that (without loss of generality) only the first edge in the augmenting path leaves s, so | f | = | f | + F > 0. In other words, f is not a maximum flow. This completes the proof! Exercises 1. Let (S , T ) and (S , T ) be minimum (s, t )-cuts in some flow network G . Prove that (S ∩ S , T ∪ T ) and (S ∪ S , T ∩ T ) are also minimum (s, t )-cuts in G . 2. Suppose (S , T ) is a minimum (s, t )-cut in some flow network. Prove that (S , T ) is also a minimum ( x , y )-cut for all vertices x ∈ S and y ∈ T . 5 Algorithms Lecture 15: Maximum Flows and Minimum Cuts 3. Cuts are sometimes defined as subsets of the edges of the graph, instead of as partitions of its vertices. In this problem, you will prove that these two definitions are almost equivalent. We say that a subset X of (directed) edges separates s and t if every directed path from s to t contains at least one (directed) edge in X . For any subset S of vertices, let δS denote the set of directed edges leaving S ; that is, δS := {u v | u ∈ S , v ∈ S }. (a) Prove that if (S , T ) is an (s, t )-cut, then δS separates s and t . (b) Let X be an arbitrary subset of edges that separates s and t . Prove that there is an (s, t )-cut (S , T ) such that δS ⊆ X . (c) Let X be a minimal subset of edges that separates s and t . Prove that there is an (s, t )-cut (S , T ) such that δS = X . 4. A flow f is acyclic if the subgraph of directed edges with positive flow contains no directed cycles. (a) Prove that for any flow f , there is an acyclic flow with the same value as f . (In particular, this implies that some maximum flow is acyclic.) (b) A path flow assigns positive values only to the edges of one simple directed path from s to t . Prove that every acyclic flow can be written as the sum of a finite number of path flows. (c) Describe a flow in a directed graph that cannot be written as the sum of path flows. (d) A cycle flow assigns positive values only to the edges of one simple directed cycle. Prove that every flow can be written as the sum of a finite number of path flows and cycle flows. (e) Prove that every flow with value 0 can be written as the sum of a finite number of cycle flows. (Zero-value flows are also called circulations.) 5. Suppose instead of capacities, we consider networks where each edge u v has a non-negative demand d (u v ). Now an (s, t )-flow f is feasible if and only if f (u v ) ≥ d (u v ) for every edge u v . (Feasible flow values can now be arbitrarily large.) A natural problem in this setting is to find a feasible (s, t )-flow of minimum value. (a) Describe an efficient algorithm to compute a feasible (s, t )-flow, given the graph, the demand function, and the vertices s and t as input. [Hint: Find a flow that is non-zero everywhere, and then scale it up to make it feasible.] (b) Suppose you have access to a subroutine MAXFLOW that computes maximum flows in networks with edge capacities. Describe an efficient algorithm to compute a minimum flow in a given network with edge demands; your algorithm should call MAXFLOW exactly once. (c) State and prove an analogue of the max-flow min-cut theorem for this setting. (Do minimum flows correspond to maximum cuts?) c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 6 Algorithms Lecture 16: Max-Flow Algorithms A process cannot be understood by stopping it. Understanding must move with the flow of the process, must join it and flow with it. — The First Law of Mentat, in Frank Herbert’s Dune (1965) There’s a difference between knowing the path and walking the path. — Morpheus [Laurence Fishburne], The Matrix (1999) 16 16.1 Max-Flow Algorithms Recap Fix a directed graph G = (V, E ) that does not contain both an edge u v and its reversal v u, and fix a capacity function c : E → IR≥0 . For any flow function f : E → IR≥0 , the residual capacity is defined as c (u v ) − f (u v ) if u v ∈ E c f (u v ) = f ( v u) 0 if v u ∈ E . otherwise The residual graph G f = (V, E f ), where E f is the set of edges whose non-zero residual capacity is positive. 0/5 5 5 10 0/15 s 10 10 5/15 10/20 5/10 10/10 t 0/10 s 10 15 5 t 5 10 5/20 5 15 10/10 10 A flow f in a weighted graph G and its residual graph G f . In the last lecture, we proved the Max-flow Min-cut Theorem: In any weighted directed graph network, the value of the maximum (s, t )-flow is equal to the capacity of the minimum (s, t )-cut. The proof of the theorem is constructive. If the residual graph contains a path from s to t , then we can increase the flow by the minimum capacity of the edges on this path, so we must not have the maximum flow. Otherwise, we can define a cut (S , T ) whose capacity is the same as the flow f , such that every edge from S to T is saturated and every edge from T to S is empty, which implies that f is a maximum flow and (S , T ) is a minimum cut. 5 5/5 10 10 s 5/15 10/20 5 10 10 15 5 0/15 5 t s 0/10 5/10 t 5 10 5/10 15 10 10/20 10/10 An augmenting path in G f and the resulting (maximum) flow f . 1 Algorithms 16.2 Lecture 16: Max-Flow Algorithms Ford-Fulkerson It’s not hard to realize that this proof translates almost immediately to an algorithm, first developed by Ford and Fulkerson in the 1950s: Starting with the zero flow, repeatedly augment the flow along any path s t in the residual graph, until there is no such path. If every edge capacity is an integer, then every augmentation step increases the value of the flow by a positive integer. Thus, the algorithm halts after | f ∗ | iterations, where f ∗ is the actual maximum flow. Each iteration requires O( E ) time, to create the residual graph G f and perform a whatever-first-search to find an augmenting path. Thus, in the words case, the Ford-Fulkerson algorithm runs in O (E | f ∗ |) time. If we multiply all the capacities by the same (positive) constant, the maximum flow increases everywhere by the same constant factor. It follows that if all the edge capacities are rational, then the Ford-Fulkerson algorithm eventually halts. However, if we allow irrational capacities, the algorithm can loop forever, always finding smaller and smaller augmenting paths. Worse yet, this infinite sequence of augmentations may not even converge to the maximum flow! Perhaps the simplest example of this effect was discovered by Uri Zwick. Consider the graph shown below, with six vertices and nine edges. Six of the edges have some large integer capacity X , two have capacity 1, and one has capacity φ = ( 5 − 1)/2 ≈ 0.618034, chosen so that 1 − φ = φ 2 . To prove that the Ford-Fulkerson algorithm can get stuck, we can watch the residual capacities of the three horizontal edges as the algorithm progresses. (The residual capacities of the other six edges will always be at least X − 3.) s X 1 X X 1 ϕ X X X t A B C Uri Zwick’s non-terminating flow example, and three augmenting paths. The Ford-Fulkerson algorithm starts by choosing the central augmenting path, shown in the large figure above. The three horizontal edges„ in order from left to right, now have residual capacities 1, 0, φ . Suppose inductively that the horizontal residual capacities are φ k−1 , 0, φ k for some non-negative integer k. 1. Augment along B , adding φ k to the flow; the residual capacities are now φ k+1 , φ k , 0. 2. Augment along C , adding φ k to the flow; the residual capacities are now φ k+1 , 0, φ k . 3. Augment along B , adding φ k+1 to the flow; the residual capacities are now 0, φ k+1 , φ k+2 . 4. Augment along A, adding φ k+1 to the flow; the residual capacities are now φ k+1 , 0, φ k+2 . 2 Algorithms Lecture 16: Max-Flow Algorithms Thus, after 4n + 1 augmentation steps, the residual capacities are φ 2n−2 , 0, φ 2n−1 . As the number of augmentation steps grows to infinity, the value of the flow converges to ∞ φi = 1 + 1+2 i =1 2 1−φ =4+ 5 < 7, even though the maximum flow value is clearly 2X + 1. Picky students might wonder at this point why we care about irrational capacities; after all, computers can’t represent anything but (small) integers or (dyadic) rationals exactly. Good question! One reason is that the integer restriction is literally artificial; it’s an artifact of actual computational hardware1 , not an inherent feature of the abstract mathematical problem. Another reason, which is probably more convincing to most practical computer scientists, is that the behavior of the algorithm with irrational inputs tells us something about its worst-case behavior in practice given floating-point capacities— terrible! Even with very reasonable capacities, a careless implementation of Ford-Fulkerson could enter an infinite loop simply because of round-off error! 16.3 Edmonds-Karp: Fat Pipes The Ford-Fulkerson algorithm does not specify which alternating path to use if there is more than one. In 1972, Jack Edmonds and Richard Karp analyzed two natural heuristics for choosing the path. The first is essentially a greedy algorithm: Choose the augmenting path with largest bottleneck value. It’s a fairly easy to show that the maximum-bottleneck (s, t )-path in a directed graph can be computed in O( E log V ) time using a variant of Jarník’s minimum-spanning-tree algorithm, or of Dijkstra’s shortest path algorithm. Simply grow a directed spanning tree T , rooted at s. Repeatedly find the highest-capacity edge leaving T and add it to T , until T contains a path from s to t . Alternately, once could emulate Kruskal’s algorithm—insert edges one at a time in decreasing capacity order until there is a path from s to t —although this is less efficient. We can now analyze the algorithm in terms of the value of the maximum flow f ∗ . Let f be any flow in G , and let f be the maximum flow in the current residual graph G f . (At the beginning of the algorithm, G f = G and f = f ∗ .) Let e be the bottleneck edge in the next augmenting path. Let S be the set of vertices reachable from s through edges in G with capacity greater than c (e) and let T = V \ S . By construction, T is non-empty, and every edge from S to T has capacity at most c (e). Thus, the capacity of the cut (S , T ) is at most c (e) · E . On the other hand, the maxflow-mincut theorem implies that S , T ≥ | f |. We conclude that c (e) ≥ | f |/ E . The preceding argument implies that augmenting f along the maximum-bottleneck path in G f multiplies the maximum flow value in G f by a factor of at most 1 − 1/ E . In other words, the residual flow decays exponentially with the number of iterations. After E · ln| f ∗ | iterations, the maximum flow value in G f is at most ∗ ∗ | f ∗ | · (1 − 1/ E ) E ·ln| f | < | f ∗ |e− ln| f | = 1. (That’s Euler’s constant e, not the edge e. Sorry.) In particular, if all the capacities are integers, then after E · ln| f ∗ | iterations, the maximum capacity of the residual graph is zero and f is a maximum flow. We conclude that for graphs with integer capacities, the Edmonds-Karp ‘fat pipe’ algorithm runs in O (E 2 log E log| f ∗ |) time. 1 ...or perhaps the laws of physics. Yeah, right. Whatever. Like reality actually matters in this class. 3 Algorithms 16.4 Lecture 16: Max-Flow Algorithms Dinits/Edmonds-Karp: Short Pipes The second Edmonds-Karp heuristic was actually proposed by Ford and Fulkerson in their original max-flow paper, and first analyzed by the Russian mathematician Dinits (sometimes transliterated Dinic) in 1970. Edmonds and Karp published their independent and slightly weaker analysis in 1972. So naturally, almost everyone refers to this algorithm as ‘Edmonds-Karp’.2 Choose the augmenting path with fewest edges. The correct path can be found in O( E ) time by running breadth-first search in the residual graph. More surprisingly, the algorithm halts after a polynomial number of iterations, independent of the actual edge capacities! The proof of this upper bound relies on two observations about the evolution of the residual graph. Let f i be the current flow after i augmentation steps, let Gi be the corresponding residual graph. In particular, f0 is zero everywhere and G0 = G . For each vertex v , let leveli ( v ) denote the unweighted shortest path distance from s to v in Gi , or equivalently, the level of v in a breadth-first search tree of Gi rooted at s. Our first observation is that these levels can only increase over time. Lemma 1. leveli +1 ( v ) ≥ leveli ( v ) for all vertices v and integers i . Proof: The claim is trivial for v = s, since leveli (s) = 0 for all i . Choose an arbitrary vertex v = s, and let s · · · u v be a shortest path from s to v in Gi +1 . (If there is no such path, then leveli +1 ( v ) = ∞, and we’re done.) Because this is a shortest path, we have leveli +1 ( v ) = leveli +1 (u) + 1, and the inductive hypothesis implies that leveli +1 (u) ≥ leveli (u). We now have two cases to consider. If u v is an edge in Gi , then leveli ( v ) ≤ leveli (u) + 1, because the levels are defined by breadth-first traversal. On the other hand, if u v is not an edge in Gi , then v u must be an edge in the i th augmenting path. Thus, v u must lie on the shortest path from s to t in Gi , which implies that leveli ( v ) = leveli (u) − 1 ≤ leveli (u) + 1. In both cases, we have leveli +1 ( v ) = leveli +1 (u) + 1 ≥ leveli (u) + 1 ≥ leveli ( v ). Whenever we augment the flow, the bottleneck edge in the augmenting path disappears from the residual graph, and some other edge in the reversal of the augmenting path may (re-)appear. Our second observation is that an edge cannot appear or disappear too many times. Lemma 2. During the execution of the Dinits/Edmonds-Karp algorithm, any edge u v disappears from the residual graph G f at most V /2 times. Proof: Suppose u v is in two residual graphs Gi and G j +1 , but not in any of the intermediate residual graphs Gi +1 , . . . , G j , for some i < j . Then u v must be in the i th augmenting path, so leveli ( v ) = leveli (u) + 1, and v u must be on the j th augmenting path, so level j ( v ) = level j (u) − 1. By the previous lemma, we have level j (u) = level j ( v ) + 1 ≥ leveli ( v ) + 1 = leveli (u) + 2. 2 To be fair, Edmonds and Karp discovered their algorithm a few years before publication—getting ideas into print takes time, especially in the early 1970s—which is why some authors believe they deserve priority. I don’t buy it; Dinits also presumably discovered his algorithm a few years before its publication. (In Soviet Union, result publish you.) On the gripping hand, Dinits’s paper also described an improvement to the algorithm presented here that runs in O(V 2 E ) time instead of O(V E 2 ), so maybe that ought to be called Dinits’s algorithm. 4 Algorithms Lecture 16: Max-Flow Algorithms In other words, the distance from s to u increased by at least 2 between the disappearance and reappearance of u v . Since every level is either less than V or infinite, the number of disappearances is at most V /2. Now we can derive an upper bound on the number of iterations. Since each edge can disappear at most V /2 times, there are at most EV /2 edge disappearances overall. But at least one edge disappears on each iteration, so the algorithm must halt after at most EV /2 iterations. Finally, since each iteration requires O( E ) time, Dinits’ algorithm runs in O (V E 2 ) time overall. Exercises 1. A new assistant professor, teaching maximum flows for the first time, suggests the following greedy modification to the generic Ford-Fulkerson augmenting path algorithm. Instead of maintaining a residual graph, just reduce the capacity of edges along the augmenting path! In particular, whenever we saturate an edge, just remove it from the graph. GREEDYFLOW(G , c , s, t ): for every edge e in G f (e) ← 0 while there is a path from s to t π ← an arbitrary path from s to t F ← minimum capacity of any edge in π for every edge e in π f (e) ← f (e) + F if c (e) = F remove e from G else c (e) ← c (e) − F return f (a) Show that this algorithm does not always compute a maximum flow. (b) Prove that for any flow network, if the Greedy Path Fairy tells you precisely which path π to use at each iteration, then GREEDYFLOW does compute a maximum flow. (Sadly, the Greedy Path Fairy does not actually exist.) 2. Describe and analyze an algorithm to find the maximum-bottleneck path from s to t in a flow network G in O( E log V ) time. 3. Describe a directed graph with irrational edge capacities, such that the Edmonds-Karp ‘fat pipe’ heuristic does not halt. 4. Describe an efficient algorithm to check whether a given flow network contains a unique maximum flow. 5. For any flow network G and any vertices u and v , let bottleneckG (u, v ) denote the maximum, over all paths π in G from u to v , of the minimum-capacity edge along π. Describe an algorithm 5 Algorithms Lecture 16: Max-Flow Algorithms to construct a spanning tree T of G such that bottleneck T (u, v ) = bottleneckG (u, v ). (Edges in T inherit their capacities form G .) One way to think about this problem is to imagine the vertices of the graph as islands, and the edges as bridges. Each bridge has a maximum weight it can support. If a truck is carrying stuff from u to v , how much can the truck carry? We don’t care what route the truck takes; the point is that the smallest-weight edge on the route will determine the load. 6. We can speed up the Edmonds-Karp ‘fat pipe’ heuristic, at least for integer capacities, by relaxing our requirements for the next augmenting path. Instead of finding the augmenting path with maximum bottleneck capacity, we find a path whose bottleneck capacity is at least half of maximum, using the following capacity scaling algorithm. The algorithm maintains a bottleneck threshold ∆; initially, ∆ is the maximum capacity among all edges in the graph. In each phase, the algorithm augments along paths from s to t in which every edge has residual capacity at least ∆. When there is no such path, the phase ends, we set ∆ ← ∆/2, and the next phase begins. (a) How many phases will the algorithm execute in the worst case, if the edge capacities are integers? (b) Let f be the flow at the end of a phase for a particular value of ∆. Let S be the nodes that are reachable from s in the residual graph G f using only edges with residual capacity at least ∆, and let T = V \ S . Prove that the capacity (with respect to G ’s original edge capacities) of the cut (S , T ) is at most | f | + E · ∆. (c) Prove that in each phase of the scaling algorithm, there are at most 2 E augmentations. (d) What is the overall running time of the scaling algorithm, assuming all the edge capacities are integers? c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 6 Algorithms Lecture 17: Applications of Maximum Flow For a long time it puzzled me how something so expensive, so leading edge, could be so useless, and then it occurred to me that a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match. — Bill Bryson, Notes from a Big Country (1999) 17 17.1 Applications of Maximum Flow Edge-Disjoint Paths One of the easiest applications of maximum flows is computing the maximum number of edge-disjoint paths between two specified vertices s and t in a directed graph G using maximum flows. A set of paths in G is edge-disjoint if each edge in G appears in at most one of the paths; several edge-disjoint paths may pass through the same vertex, however. If we give each edge capacity 1, then the maxflow from s to t assigns a flow of either 0 or 1 to every edge. Since any vertex of G lies on at most two saturated edges (one in and one out, or none at all), the subgraph S of saturated edges is the union of several edge-disjoint paths and cycles. Moreover, the number of paths is exactly equal to the value of the flow. Extracting the actual paths from S is easy—just follow any directed path in S from s to t , remove that path from S , and recurse. Conversely, we can transform any collection of k edge-disjoint paths into a flow by pushing one unit of flow along each path from s to t ; the value of the resulting flow is exactly k. It follows that the maxflow algorithm actually computes the largest possible set of edge-disjoint paths. The overall running time is O(V E ), just like for maximum bipartite matchings. The same algorithm can also be used to find edge-disjoint paths in undirected graphs. We simply replace every undirected edge in G with a pair of directed edges, each with unit capacity, and compute a maximum flow from s to t in the resulting directed graph G using the Ford-Fulkerson algorithm. For any edge uv in G , if our max flow saturates both directed edges u v and v u in G , we can remove both edges from the flow without changing its value. Thus, without loss of generality, the maximum flow assigns a direction to every saturated edge, and we can extract the edge-disjoint paths by searching the graph of directed saturated edges. 17.2 Vertex Capacities and Vertex-Disjoint Paths Suppose we have capacities on the vertices as well as the edges. Here, in addition to our other constraints, we require that for any vertex v other than s and t , the total flow into v (and therefore the total flow out of v ) is at most some non-negative value c ( v ). How can we compute a maximum flow with these new constraints? One possibility is to modify our existing algorithms to take these vertex capacities into account. Given a flow f , we can define the residual capacity of a vertex v to be its original capacity minus the total flow into v : c f (v) = c(v) − f (u v ). u Since we cannot send any more flow into a vertex with residual capacity 0 we remove from the residual graph G f every edge u v that appears in G whose head vertex v is saturated. Otherwise, the augmenting-path algorithm is unchanged. But an even simpler method is to transform the input into a traditional flow network, with only edge capacities. Specifically, we replace every vertex v with two vertices vin and vout , connected by 1 Algorithms Lecture 17: Applications of Maximum Flow an edge vin vout with capacity c ( v ), and then replace every directed edge u v with the edge uout vin (keeping the same capacity). Finally, we compute the maximum flow from sout to t in in this modified flow network. It is now easy to compute the maximum number of vertex-disjoint paths from s to t in any directed graph. Simply give every vertex capacity 1, and compute a maximum flow! 17.3 Maximum Matchings in Bipartite Graphs Another natural application of maximum flows is finding large matchings in bipartite graphs. A matching is a subgraph in which every vertex has degree at most one, or equivalently, a collection of edges such that no two share a vertex. The problem is to find the matching with the maximum number of edges in a given bipartite graph. We can solve this problem by reducing it to a maximum flow problem as follows. Let G be the given bipartite graph with vertex set U ∪ W , such that every edge joins a vertex in U to a vertex in W . We create a new directed graph G by (1) orienting each edge from U to W , (2) adding two new vertices s and t , (3) adding edges from s to every vertex in U , and (4) adding edges from each vertex in W to t . Finally, we assign every edge in G a capacity of 1. Any matching M in G can be transformed into a flow f M in G as follows: For each edge uw in M , push one unit of flow along the path s u w t . These paths are disjoint except at s and t , so the resulting flow satisfies the capacity constraints. Moreover, the value of the resulting flow is equal to the number of edges in M . Conversely, consider any (s, t )-flow f in G computed using the Ford-Fulkerson augmenting path algorithm. Because the edge capacities are integers, the Ford-Fulkerson algorithm assigns an integer flow to every edge. (This is easy to verify by induction, hint, hint.) Moreover, since each edge has unit capacity, the computed flow either saturates ( f (e) = 1) or avoids ( f (e) = 0) every edge in G . Finally, since at most one unit of flow can enter any vertex in U or leave any vertex in W , the saturated edges from U to W form a matching in G . The size of this matching is exactly | f |. Thus, the size of the maximum matching in G is equal to the value of the maximum flow in G , and provided we compute the maxflow using augmenting paths, we can convert the actual maxflow into a maximum matching. The maximum flow has value at most min{|U |, |W |} = O(V ), so the Ford-Fulkerson algorithm runs in O (V E ) time. s t A maximum matching in a bipartite graph G , and the corresponding maximum flow in G . 17.4 Binary Assignment Problems Maximum-cardinality matchings are a special case of a general family of so-called assignment problems.1 An unweighted binary assignment problem involves two disjoint finite sets X and Y , which typically represent two different kinds of resources, such as web pages and servers, jobs and machines, rows and 1 Most authors refer to finding a maximum-weight matching in a bipartite graph as the assignment problem. 2 Algorithms Lecture 17: Applications of Maximum Flow columns of a matrix, hospitals and interns, or customers and pints of ice cream. Our task is to choose the largest possible collection of pairs ( x , y ) as possible, where x ∈ X and y ∈ Y , subject to several constraints of the following form: • Each element x ∈ X can appear in at most c ( x ) pairs. • Each element y ∈ Y can appear in at most c ( y ) pairs. • Each pair ( x , y ) ∈ X × Y can appear in the output at most c ( x , y ) times. Each upper bound c ( x ), c ( y ), and c ( x , y ) is either a (typically small) non-negative integer or ∞. Intuitively, we create each pair in our output by assigning an element of X to an element of y . The maximum-matching problem is a special case, where c (z ) = 1 for all z ∈ X ∪ Y , and each c ( x , y ) is either 0 or 1, depending on whether the pair x y defines an edge in the underlying bipartite graph. Here is a slightly more interesting example. A nearby school, famous for its onerous administrative hurdles, decides to organize a dance. Every pair of students (one boy, one girl) who wants to dance must register in advance. School regulations limit each boy-girl pair to at most three dances together, and limits each student to at most ten dances overall. How can we maximize the number of dances? This is a binary assignment problem for the set X of girls and the set Y of boys. For each girl x and boy y , we have c ( x ) = 10, c ( y ) = 10, and either c ( x , y ) = 3 (if x and y registered to dance) or c ( x , y ) = 0 (if they didn’t). This binary assignment problem can be reduces to a standard maximum flow problem as follows. We construct a flow network G = (V, E ) with vertices X ∪ Y ∪ {s, t } and the following edges: • an edge s x with capacity c ( x ) for each x ∈ X , • an edge y t with capacity c ( y ) for each y ∈ Y . • an edge x y with capacity c ( x , y ) for each x ∈ X and y ∈ Y , and Because all the edges have integer capacities, the Ford-Fulkerson algorithm constructs an integer maximum flow f ∗ . This flow can be decomposed into the sum of | f ∗ | paths of the form s x y t for some x ∈ X and y ∈ Y . For each such path, we report the pair ( x , y ). (Equivalently, the pair ( x , y ) appears in our output collection f ( x y ) times.) It is easy to verify (hint, hint) that this collection of pairs satisfies all the necessary constraints. Conversely, any legal collection of r pairs can be transformed into a feasible integer flow with value r in G . Thus, the largest legal collection of pairs corresponds to a maximum flow in G . So our algorithm is correct. 17.5 Baseball Elimination Every year millions of baseball fans eagerly watch their favorite team, hoping they will win a spot in the playoffs, and ultimately the World Series. Sadly, most teams are “mathematically eliminated" days or even weeks before the regular season ends. Often, it is easy to spot when a team is eliminated—they can’t win enough games to catch up to the current leader in their division. But sometimes the situation is more subtle. For example, here are the actual standings from the American League East on August 30, 1996. Team New York Yankees Baltimore Orioles Boston Red Sox Toronto Blue Jays Detroit Lions Won–Lost Left NYY 75–59 71–63 69–66 63–72 49–86 28 28 27 27 27 3 8 7 3 3 BAL BOS TOR DET 3 8 2 7 7 0 3 4 0 0 2 7 4 0 0 0 Algorithms Lecture 17: Applications of Maximum Flow Detroit is clearly behind, but some die-hard Lions fans may hold out hope that their team can still win. After all, if Detroit wins all 27 of their remaining games, they will end the season with 76 wins, more than any other team has now. So as long as every other team loses every game. . . but that’s not possible, because some of those other teams still have to play each other. Here is one complete argument:2 By winning all of their remaining games, Detroit can finish the season with a record of 76 and 86. If the Yankees win just 2 more games, then they will finish the season with a 77 and 85 record which would put them ahead of Detroit. So, let’s suppose the Tigers go undefeated for the rest of the season and the Yankees fail to win another game. The problem with this scenario is that New York still has 8 games left with Boston. If the Red Sox win all of these games, they will end the season with at least 77 wins putting them ahead of the Tigers. Thus, the only way for Detroit to even have a chance of finishing in first place, is for New York to win exactly one of the 8 games with Boston and lose all their other games. Meanwhile, the Sox must loss all the games they play agains teams other than New York. This puts them in a 3-way tie for first place. . . . Now let’s look at what happens to the Orioles and Blue Jays in our scenario. Baltimore has 2 games left with with Boston and 3 with New York. So, if everything happens as described above, the Orioles will finish with at least 76 wins. So, Detroit can catch Baltimore only if the Orioles lose all their games to teams other than New York and Boston. In particular, this means that Baltimore must lose all 7 of its remaining games with Toronto. The Blue Jays also have 7 games left with the Yankees and we have already seen that for Detroit to finish in first place, Toronto must will all of these games. But if that happens, the Blue Jays will win at least 14 more games giving them at final record of 77 and 85 or better which means they will finish ahead of the Tigers. So, no matter what happens from this point in the season on, Detroit can not finish in first place in the American League East. There has to be a better way to figure this out! Here is a more abstract formulation of the problem. Our input consists of two arrays W [1 .. n] and G [1 .. n, 1 .. n], where W [i ] is the number of games team i has already won, and G [i , j ] is the number of upcoming games between teams i and j . We want to determine whether team n can end the season with the most wins (possibly tied with other teams).3 We model this question as an assignment problem: We want to assign a winner to each game, so that team n comes in first place. We have an assignment problem! Let R[i ] = j G [i , j ] denote the number of remaining games for team i . We will assume that team n wins all R[n] of its remaining games. Then team n can come in first place if and only if every other team i wins at most W [n] + R[n] − W [i ] of its R[i ] remaining games. Since we want to assign winning teams to games, we start by building a bipartite graph, whose n nodes represent the games and the teams. We have 2 game nodes g i , j , one for each pair 1 ≤ i < j < n, and n − 1 team nodes t i , one for each 1 ≤ i < n. For each pair i , j , we add edges g i , j t i and g i , j t j with infinite capacity. We add a source vertex s and edges s g i , j with capacity G [i , j ] for each pair i , j . Finally, we add a target node t and edges t i t with capacity W [n] − W [i ] + R[n] for each team i . Theorem: Team n can end the season in first place if and only if there is a feasible flow in this graph that saturates every edge leaving s. Proof: Suppose it is possible for team n to end the season in first place. Then every team i < n wins at most W [n]+ R[n] − W [i ] of the remaining games. For each game between team i and team j that team i wins, add one unit of flow along the path s g i , j t i t . Because there are exactly G [i , j ] games between teams i and j , every edge leaving s is saturated. Because each team i wins at most W [n] + R[n] − W [i ] games, the resulting flow is feasible. Conversely, Let f be a feasible flow that saturates every edge out of s. Suppose team i wins exactly f ( g i , j t i ) games against team j , for all i and j . Then teams i and j play f ( g i , j t i ) + f ( g i , j t j ) = Both the example and this argument are taken from http://riot.ieor.berkeley.edu/~baseball/detroit.html. We assume here that no games end in a tie (always true for Major League Baseball), and that every game is actually played (not always true). 2 3 4 Algorithms Lecture 17: Applications of Maximum Flow f (s g i , j ) = G [i , j ] games, so every upcoming game is played. Moreover, each team i wins a total of j f ( g i , j t i ) = f ( t i t ) ≤ W [ n] + R[ n] − W [ i ] upcoming games, and therefore at most W [ n] + R[ n] games overall. Thus, if team n win all their upcoming games, they end the season in first place. So, to decide whether our favorite team can win, we construct the flow network, compute a maximum flow, and report whether than maximum flow saturates the edges leaving s. The flow network has O(n2 ) vertices and O(n2 ) edges, and it can be constructed in O(n2 ) time. Using Dinitz’s algorithm, we can compute the maximum flow in O(V E 2 ) = O(n6 ) time. The graph derived from the 1996 American League East standings is shown below. The total capacity of the edges leaving s is 27 (there are 27 remaining games), but the total capacity of the edges entering t is only 26. So the maximum flow has value at most 26, which means that Detroit is mathematically eliminated. NYY BAL NYY NYY BOS 3 8 s 1 BAL 5 NYY TOR 7 t 7 2 BOS BAL BOS 7 BAL TOR 13 TOR The flow graph for the 1996 American League East standings. Unlabeled edges have infinite capacity. Exercises 1. Given an undirected graph G = (V, E ), with three vertices u, v , and w , describe and analyze an algorithm to determine whether there is a path from u to w that passes through v . 2. Let G = (V, E ) be a directed graph where for each vertex v , the in-degree and out-degree of v are equal. Let u and v be two vertices G, and suppose G contains k edge-disjoint paths from u to v . Under these conditions, must G also contain k edge-disjoint paths from v to u? Give a proof or a counterexample with explanation. 3. A cycle cover of a given directed graph G = (V, E ) is a set of vertex-disjoint cycles that cover all the vertices. Describe and analyze an efficient algorithm to find a cycle cover for a given graph, or correctly report that no cycle cover exists. [Hint: Use bipartite matching!] 4. Consider a directed graph G = (V, E ) with multiple source vertices s1 , s2 , . . . , sσ and multiple target vertices t 1 , t 1 , . . . , t τ , where no vertex is both a source and a target. A multiterminal flow is a function f : E → IR≥0 that satisfies the flow conservation constraint at every vertex that is neither a source nor a target. The value | f | of a multiterminal flow is the total excess flow out of all the source vertices: σ | f | := f (si w ) − i =1 w f (u si ) u 5 Algorithms Lecture 17: Applications of Maximum Flow As usual, we are interested in finding flows with maximum value, subject to capacity constraints on the edges. (In particular, we don’t care how much flow moves from any particular source to any particular target.) (a) Consider the following algorithm for computing multiterminal flows. The variables f and f represent flow functions. The subroutine MAXFLOW(G , s, t ) solves the standard maximum flow problem with source s and target t . MAXMULTIFLOW(G , s[1 .. σ], t [1 .. τ]): f ←0 〈〈Initialize the flow〉〉 for i ← 1 to σ for j ← 1 to τ f ← MAXFLOW(G f , s[i ], t [ j ]) f ←f +f 〈〈Update the flow〉〉 return f Prove that this algorithm correctly computes a maximum multiterminal flow in G . (b) Describe a more efficient algorithm to compute a maximum multiterminal flow in G . 5. The Island of Sodor is home to a large number of towns and villages, connected by an extensive rail network. Recently, several cases of a deadly contagious disease (either swine flu or zombies; reports are unclear) have been reported in the village of Ffarquhar. The controller of the Sodor railway plans to close down certain railway stations to prevent the disease from spreading to Tidmouth, his home town. No trains can pass through a closed station. To minimize expense (and public notice), he wants to close down as few stations as possible. However, he cannot close the Ffarquhar station, because that would expose him to the disease, and he cannot close the Tidmouth station, because then he couldn’t visit his favorite pub. Describe and analyze an algorithm to find the minimum number of stations that must be closed to block all rail travel from Ffarquhar to Tidmouth. The Sodor rail network is represented by an undirected graph, with a vertex for each station and an edge for each rail connection between two stations. Two special vertices f and t represent the stations in Ffarquhar and Tidmouth. For example, given the following input graph, your algorithm should return the number 2. f t 6. Suppose we are given an array A[1 .. m][1 .. n] of non-negative real numbers. We want to round A to an integer matrix, by replacing each entry x in A with either x or x , without changing the sum of entries in any row or column of A. For example: 1.2 3.4 2.4 142 3.9 4.0 2.1 −→ 4 4 2 7.9 1.6 0.5 811 Describe an efficient algorithm that either rounds A in this fashion, or reports correctly that no such rounding is possible. 6 Algorithms Lecture 17: Applications of Maximum Flow 7. Ad-hoc networks are made up of low-powered wireless devices. In principle4 , these networks can be used on battlefields, in regions that have recently suffered from natural disasters, and in other hard-to-reach areas. The idea is that a large collection of cheap, simple devices could be distributed through the area of interest (for example, by dropping them from an airplane); the devices would then automatically configure themselves into a functioning wireless network. These devices can communication only within a limited range. We assume all the devices are identical; there is a distance D such that two devices can communicate if and only if the distance between them is at most D. We would like our ad-hoc network to be reliable, but because the devices are cheap and low-powered, they frequently fail. If a device detects that it is likely to fail, it should transmit its information to some other backup device within its communication range. We require each device x to have k potential backup devices, all within distance D of x ; we call these k devices the backup set of x . Also, we do not want any device to be in the backup set of too many other devices; otherwise, a single failure might affect a large fraction of the network. So suppose we are given the communication radius D, parameters b and k, and an array d [1 .. n, 1 .. n] of distances, where d [i , j ] is the distance between device i and device j . Describe an algorithm that either computes a backup set of size k for each of the n devices, such that that no device appears in more than b backup sets, or reports (correctly) that no good collection of backup sets exists. 8. A rooted tree is a directed acyclic graph, in which every vertex has exactly one incoming edge, except for the root, which has no incoming edges. Equivalently, a rooted tree consists of a root vertex, which has edges pointing to the roots of zero or more smaller rooted trees. Describe a polynomial-time algorithm to compute, given two rooted trees A and B , the largest common rooted subtree of A and B . [Hint: Let LC S (u, v ) denote the largest common subtree whose root in A is u and whose root in B is v . Your algorithm should compute LC S (u, v ) for all vertices u and v using dynamic programming. This would be easy if every vertex had O(1) children, and still straightforward if the children of each node were ordered from left to right and the common subtree had to respect that ordering. But for unordered trees with large degree, you need another trick to combine recursive subproblems efficiently. Don’t waste your time trying to reduce the polynomial running time.] 4 but not really in practice c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 7 Algorithms Lecture 18: Extensions of Maximum Flow “Who are you?" said Lunkwill, rising angrily from his seat. “What do you want?" “I am Majikthise!" announced the older one. “And I demand that I am Vroomfondel!" shouted the younger one. Majikthise turned on Vroomfondel. “It’s alright," he explained angrily, “you don’t need to demand that." “Alright!" bawled Vroomfondel banging on an nearby desk. “I am Vroomfondel, and that is not a demand, that is a solid fact! What we demand is solid facts!" “No we don’t!" exclaimed Majikthise in irritation. “That is precisely what we don’t demand!" Scarcely pausing for breath, Vroomfondel shouted, “We don’t demand solid facts! What we demand is a total absence of solid facts. I demand that I may or may not be Vroomfondel!" — Douglas Adams, The Hitchhiker’s Guide to the Galaxy (1979) 18 Extensions of Maximum Flow 18.1 Maximum Flows with Edge Demands Now suppose each directed edge e in has both a capacity c (e) and a demand d (e) ≤ c (e), and we want a flow f of maximum value that satisfies d (e) ≤ f (e) ≤ c (e) at every edge e. We call a flow that satisfies these constraints a feasible flow. In our original setting, where d (e) = 0 for every edge e, the zero flow is feasible; however, in this more general setting, even determining whether a feasible flow exists is a nontrivial task. Perhaps the easiest way to find a feasible flow (or determine that none exists) is to reduce the problem to a standard maximum flow problem, as follows. The input consists of a directed graph G = (V, E ), nodes s and t , demand function d : E → IR, and capacity function c : E → IR. Let D denote the sum of all edge demands in G : D := d (u v ). u v∈E We construct a new graph G = (V , E ) from G by adding new source and target vertices s and t , adding edges from s to each vertex in V , adding edges from each vertex in V to t , and finally adding an edge from t to s. We also define a new capacity function c : E → IR as follows: • For each vertex v ∈ V , we set c (s v) = u∈V d (u v ) and c ( v t ) = w ∈V d ( v w ). • For each edge u v ∈ E , we set c (u v ) = c (u v ) − d (u v ). • Finally, we set c (s t ) = ∞. Intuitively, we construct G by replacing any edge u v in G with three edges: an edge u v with capacity c (u v ) − d (u v ), an edge s v with capacity d (u v ), and an edge u t with capacity d (u v ). If this construction produces multiple edges from s to the same vertex v (or to t from the same vertex v ), we merge them into a single edge with the same total capacity. In G , the total capacity out of s and the total capacity into t are both equal to D. We call a flow with value exactly D a saturating flow, since it saturates all the edges leaving s or entering t . If G has a saturating flow, it must be a maximum flow, so we can find it using any max-flow algorithm. Lemma 1. G has a feasible (s, t )-flow if and only if G has a saturating (s , t )-flow. 1 Algorithms Lecture 18: Extensions of Maximum Flow 8 11 4 5 5 7 3 13 0..5 s s' 6..20 2..15 5..10 5..10 4..10 5 14 6 0..15 s 13 t 10 7..20 t 6 t' 14 7 3 10 3 3..10 ∞ A flow network G with demands and capacities (written d .. c ), and the transformed network G . Proof: Let f : E → IR be a feasible (s, t )-flow in the original graph G . Consider the following function f : E → IR: f (u v ) = f (u v ) − d (u v ) f (s v) = for all u v ∈ E d (u v ) for all v ∈ V d (u → w ) for all v ∈ V u∈V f (v t ) = w ∈V f ( t s) = | f | We easily verify that f is a saturating (s , t )-flow in G . The admissibility of f implies that f (e) ≥ d (e) for every edge e ∈ E , so f (e) ≥ 0 everywhere. Admissibility also implies f (e) ≤ c (e) for every edge e ∈ E , so f (e) ≤ c (e) everywhere. Tedious algebra implies that f (u v ) = u∈V f (v w) w ∈V for every vertex v ∈ V (including s and t ). Thus, f is a legal (s , t )-flow, and every edge out of s or into t is clearly saturated. Intuitively, f diverts d (u v ) units of flow from u directly to the new target t , and injects the same amount of flow into v directly from the new source s . The same tedious algebra implies that for any saturating (s , t )-flow f : E → IR for G , the function f = f | E + d is a feasible (s, t )-flow in G . Thus, we can compute a feasible (s, t )-flow for G , if one exists, by searching for a maximum (s , t )flow in G and checking that it is saturating. Once we’ve found a feasible (s, t )-flow in G , we can transform it into a maximum flow using an augmenting-path algorithm, but with one small change. To ensure that every flow we consider is feasible, we must redefine the residual capacity of an edge as follows: c (u v ) − f (u v ) if u v ∈ E , c f (u v ) = f ( v u) − d ( v u) if v u ∈ E , 0 otherwise. Otherwise, the algorithm is unchanged. If we use the Dinitz/Edmonds-Karp fat-pipe algorithm, we get an overall running time of O (V E 2 ). 2 Algorithms Lecture 18: Extensions of Maximum Flow 8/8 11/11 4/4 5/5 7/7 2/5 3/3 0/13 s s' 3/13 0/5 0/14 t 0/6 0/6 t' 0/14 7/7 10/10 10/10 3/3 3/3 11/∞ 3 2/0..5 7/7..20 2 5/2..15 1/1..15 s 5/5..10 4/4..10 4/4..10 3 13 s t 5 10 14 6 6/6..20 t 6 7 14 10/3..10 A saturating flow f in G , the corresponding feasible flow f in G , and the corresponding residual network G f . 18.2 Node Supplies and Demands Another useful variant to consider allows flow to be injected or extracted from the flow network at vertices other than s or t . Let x : (V \ {s, t }) → IR be an excess function describing how much flow is to be injected (or extracted if the value is negative) at each vertex. We now want a maximum ‘flow’ that satisfies the variant balance condition f (u v ) − u∈V f (v w) = x (v) w ∈V for every node v except s and t , or prove that no such flow exists. As above, call such a function f a feasible flow. As for flows with edge demands, the only real difficulty in finding a maximum flow under these modified constraints is finding a feasible flow (if one exists). We can reduce this problem to a standard max-flow problem, just as we did for edge demands. To simplify the transformation, let us assume without loss of generality that the total excess in the ˜ network is zero: v x ( v ) = 0. If the total excess is positive, we add an infinite capacity edge t t , where ˜ is a new target node, and set x ( t ) = − v x ( v ). Similarly, if the total excess is negative, we add t an infinite capacity edge ˜ s, where ˜ is a new source node, and set x (s) = − v x ( v ). In both cases, s s every feasible flow in the modified graph corresponds to a feasible flow in the original graph. As before, we modify G to obtain a new graph G by adding a new source s , a new target t , an infinite-capacity edge t s from the old target to the old source, and several edges from s and to t . Specifically, for each vertex v , if x ( v ) > 0, we add a new edge s v with capacity x ( v ), and if x ( v ) < 0, we add an edge v t with capacity − x ( v ). As before, we call an (s , t )-flow in G saturating if every edge leaving s or entering t is saturated; any saturating flow is a maximum flow. It is easy to check that saturating flows in G are in direct correspondence with feasible flows in G ; we leave details as an exercise (hint, hint). 3 Algorithms Lecture 18: Extensions of Maximum Flow Similar reductions allow us to solve several other variants of the maximum flow problem using the same path-augmentation algorithms. For example, we could associate capacities and lower bounds with the vertices instead of (or in addition to) the edges. We could associate a range of excesses with every node, instead of a single excess value. We can associate a cost c (e) with each edge, and then ask for the maximum-value flow f whose total cost e c (e) · f (e) is as small as possible. We could even apply all of these extensions at once: upper bounds, lower bounds, and cost functions for the flow through each edge, into each vertex, and out of each vertex. 18.3 Minimum-Cost Flows — To be written — 18.4 Maximum-Weight Matchings Recall from the previous lecture that we can find a maximum-cardinality matching in any bipartite graph in O(V E ) time by reduction to the standard maximum flow problem. Now suppose the input graph has weighted edges, and we want to find the matching with maximum total weight. Given a bipartite graph G = (U × W, E ) and a non-negative weight function w : E → IR, the goal is to compute a matching M whose total weight w ( M ) = uw ∈ M w (uw ) is as large as possible. Max-weight matchings can’t be found directly using standard max-flow algorithms1 , but we can modify the algorithm for maximum-cardinality matchings described above. It will be helpful to reinterpret the behavior of our earlier algorithm directly in terms of the original bipartite graph instead of the derived flow network. Our algorithm maintains a matching M , which is initially empty. We say that a vertex is matched if it is an endpoint of an edge in M . At each iteration, we find an alternating path π that starts and ends at unmatched vertices and alternates between edges in E \ M and edges in M . Equivalently, let G M be the directed graph obtained by orienting every edge in M from W to U , and every edge in E \ M from U to W . An alternating path is just a directed path in G M between two unmatched vertices. Any alternating path has odd length and has exactly one more edge in E \ M than in M . The iteration ends by setting M ← M ⊕ π, thereby increasing the number of edges in M by one. The max-flow/min-cut theorem implies that when there are no more alternating paths, M is a maximum matching. A matching M with 5 edges, an alternating path π, and the augmented matching M ⊕ π with 6 edges. If the edges of G are weighted, we only need to make two changes to the algorithm. First, instead of looking for an arbitrary alternating path at each iteration, we look for the alternating path π such that 1 However, max-flow algorithms can be modified to compute maximum weighted flows, where every edge has both a capacity and a weight, and the goal is to maximize u v w (u v ) f (u v ). 4 Algorithms Lecture 18: Extensions of Maximum Flow M ⊕ π has largest weight. Suppose we weight the edges in the residual graph G M as follows: w (u w ) = −w (uw ) for all uw ∈ M w (w u) = w (uw ) for all uw ∈ M We now have w ( M ⊕ π) = w ( M ) − w (π). Thus, the correct augmenting path π must be the directed path in G M with minimum total residual weight w (π). Second, because the matching with the maximum weight may not be the matching with the maximum cardinality, we return the heaviest matching considered in any iteration of the algorithm. 2 3 3 10 5 2 10 3 5 3 A maximum-weight matching is not necessarily a maximum-cardinality matching. Before we determine the running time of the algorithm, we need to check that it actually finds the maximum-weight matching. After all, it’s a greedy algorithm, and greedy algorithms don’t work unless you prove them into submission! Let Mi denote the maximum-weight matching in G with exactly i edges. In particular, M0 = ∅, and the global maximum-weight matching is equal to Mi for some i . (The figure above show M1 and M2 for the same graph.) Let Gi denote the directed residual graph for Mi , let w i denote the residual weight function for Mi as defined above, and let πi denote the directed path in Gi such that w i (πi ) is minimized. To simplify the proof, I will assume that there is a unique maximum-weight matching Mi of any particular size; this assumption can be enforced by applying a consistent tie-breaking rule. With this assumption in place, the correctness of our algorithm follows inductively from the following lemma. Lemma 2. If G contains a matching with i + 1 edges, then Mi +1 = Mi ⊕ πi . Proof: I will prove the equivalent statement Mi +1 ⊕ Mi = πi −1 . To simplify notation, call an edge in Mi +1 ⊕ Mi red if it is an edge in Mi +1 , and blue if it is an edge in Mi . The graph Mi +1 ⊕ Mi has maximum degree 2, and therefore consists of pairwise disjoint paths and cycles, each of which alternates between red and blue edges. Since G is bipartite, every cycle must have even length. The number of edges in Mi +1 ⊕ Mi is odd; specifically, Mi +1 ⊕ Mi has 2i + 1 − 2k edges, where k is the number of edges that are in both matchings. Thus, Mi +1 ⊕ Mi contains an odd number of paths of odd length, some number of paths of even length, and some number of cycles of even length. Let γ be a cycle in Mi +1 ⊕ Mi . Because γ has an equal number of edges from each matching, Mi ⊕ γ is another matching with i edges. The total weight of this matching is exactly w ( Mi ) − w i (γ), which must be less than w ( Mi ), so w i (γ) must be positive. On the other hand, Mi +1 ⊕ γ is a matching with i + 1 edges whose total weight is w ( Mi +1 ) + w i (γ) < w ( Mi +1 ), so w i (γ) must be negative! We conclude that no such cycle γ exists; Mi +1 ⊕ Mi consists entirely of disjoint paths. Exactly the same reasoning implies that no path in Mi +1 ⊕ Mi has an even number of edges. Finally, since the number of red edges in Mi +1 ⊕ Mi is one more than the number of blue edges, the number of paths that start with a red edge is exactly one more than the number of paths that start with a blue edge. The same reasoning as above implies that Mi +1 ⊕ Mi does not contain a blue-first path, because we can pair it up with a red-first path. We conclude that Mi +1 ⊕ Mi consists of a single alternating path π whose first edge is red. Since w ( Mi +1 ) = w ( Mi ) − w i (π), the path π must be the one with minimum weight w i (π). 5 Algorithms Lecture 18: Extensions of Maximum Flow We can find the alternating path πi using a single-source shortest path algorithm. Modify the residual graph Gi by adding zero-weight edges from a new source vertex s to every unmatched node in U , and from every unmatched node in W to a new target vertex t , exactly as in out unweighted matching algorithm. Then πi is the shortest path from s to t in this modified graph. Since Mi is the maximum-weight matching with i vertices, Gi has no negative cycles, so this shortest path is well-defined. We can compute the shortest path in Gi in O(V E ) time using Shimbel’s algorithm, so the overall running time our algorithm is O (V 2 E ). The residual graph Gi has negative-weight edges, so we can’t speed up the algorithm by replacing Shimbel’s algorithm with Dijkstra’s. However, we can use a variant of Johnson’s all-pairs shortest path algorithm to improve the running time to O (V E + V 2 log V ). Let di ( v ) denote the distance from s to v ˜ in the residual graph Gi , using the distance function w i . Let w i denote the modified distance function ˜ w i (u v ) = di −1 (u) + w i (u v ) − di −1 ( v ). As we argued in the discussion of Johnson’s algorithm, shortest ˜ ˜ paths with respect to w i are still shortest paths with respect to w i . Moreover, w i (u v ) > 0 for every edge u v in Gi : • If u v is an edge in Gi −1 , then w i (u v ) = w i −1 (u v ) and di −1 ( v ) ≤ di −1 (u) + w i −1 (u v ). • If u v is not in Gi −1 , then w i (u v ) = −w i −1 ( v u) and v u is an edge in the shortest path πi −1 , so di −1 (u) = di −1 ( v ) + w i −1 ( v u). ˜ ˜ Let di ( v ) denote the shortest path distance from s to v with respect to the distance function w i . ˜ ˜ Because w i is positive everywhere, we can quickly compute di ( v ) for all v using Dijkstra’s algorithm. ˜ This gives us both the shortest alternating path πi and the distances di ( v ) = di ( v ) + di −1 ( v ) needed for the next iteration. Exercises 1. Suppose we are given a directed graph G = (V, E ), two vertices s an t , and a capacity function c : V → IR+ . A flow f is feasible if the total flow into every vertex v is at most c ( v ): f (u v ) ≤ c ( v ) for every vertex v . u Describe and analyze an efficient algorithm to compute a feasible flow of maximum value. 2. Suppose we are given an n × n grid, some of whose cells are marked; the grid is represented by an array M [1 .. n, 1 .. n] of booleans, where M [i , j ] = TRUE if and only if cell (i , j ) is marked. A monotone path through the grid starts at the top-left cell, moves only right or down at each step, and ends at the bottom-right cell. Our goal is to cover the marked cells with as few monotone paths as possible. Greedily covering the marked cells in a grid with four monotone paths. 6 Algorithms Lecture 18: Extensions of Maximum Flow (a) Describe an algorithm to find a monotone path that covers the largest number of marked cells. (b) There is a natural greedy heuristic to find a small cover by monotone paths: If there are any marked cells, find a monotone path π that covers the largest number of marked cells, unmark any cells covered by π those marked cells, and recurse. Show that this algorithm does not always compute an optimal solution. (c) Describe and analyze an efficient algorithm to compute the smallest set of monotone paths that covers every marked cell. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 7 Algorithms Non-Lecture I: Linear Programming The greatest flood has the soonest ebb; the sorest tempest the most sudden calm; the hottest love the coldest end; and from the deepest desire oftentimes ensues the deadliest hate. — Socrates Th’ extremes of glory and of shame, Like east and west, become the same. — Samuel Butler, Hudibras Part II, Canto I (c. 1670) Extremes meet, and there is no better example than the haughtiness of humility. — Ralph Waldo Emerson, “Greatness”, in Letters and Social Aims (1876) I Linear Programming The maximum flow/minimum cut problem is a special case of a very general class of problems called linear programming. Many other optimization problems fall into this class, including minimum spanning trees and shortest paths, as well as several common problems in scheduling, logistics, and economics. Linear programming was used implicitly by Fourier in the early 1800s, but it was first formalized and applied to problems in economics in the 1930s by Leonid Kantorovich. Kantorivich’s work was hidden behind the Iron Curtain (where it was largely ignored) and therefore unknown in the West. Linear programming was rediscovered and applied to shipping problems in the early 1940s by Tjalling Koopmans. The first complete algorithm to solve linear programming problems, called the simplex method, was published by George Dantzig in 1947. Koopmans first proposed the name “linear programming" in a discussion with Dantzig in 1948. Kantorovich and Koopmans shared the 1975 Nobel Prize in Economics “for their contributions to the theory of optimum allocation of resources”. Dantzig did not; his work was apparently too pure. Koopmans wrote to Kantorovich suggesting that they refuse the prize in protest of Dantzig’s exclusion, but Kantorovich saw the prize as a vindication of his use of mathematics in economics, which his Soviet colleagues had written off as “a means for apologists of capitalism”. A linear programming problem asks for a vector x ∈ IRd that maximizes (or equivalently, minimizes) a given linear function, among all vectors x that satisfy a given set of linear inequalities. The general form of a linear programming problem is the following: d maximize cj x j j =1 d ai j x j ≤ bi for each i = 1 .. p ai j x j = bi for each i = p + 1 .. p + q ai j x j ≥ bi subject to for each i = p + q + 1 .. n j =1 d j =1 d j =1 Here, the input consists of a matrix A = (ai j ) ∈ IRn×d , a column vector b ∈ IRn , and a row vector c ∈ IRd . Each coordinate of the vector x is called a variable. Each of the linear inequalities is called a constraint. The function x → c · x is called the objective function. I will always use d to denote the number of variables, also known as the dimension of the problem. The number of constraints is usually denoted n. 1 Algorithms Non-Lecture I: Linear Programming A linear programming problem is said to be in canonical form1 if it has the following structure: d maximize cj x j j =1 d ai j x j ≤ bi for each i = 1 .. n xj ≥ 0 subject to for each j = 1 .. d j =1 We can express this canonical form more compactly as follows. For two vectors x = ( x 1 , x 2 , . . . , x d ) and y = ( y1 , y2 , . . . , yd ), the expression x ≥ y means that x i ≥ yi for every index i . max c · x s.t. Ax ≤ b x≥0 Any linear programming problem can be converted into canonical form as follows: • For each variable x j , add the equality constraint x j = x + − x − and the inequalities x + ≥ 0 and j j j x − ≥ 0. j • Replace any equality constraint j ai j x j ≤ bi . • Replace any upper bound j j ai j x j = bi with two inequality constraints ai j x j ≥ bi with the equivalent lower bound j j ai j x j ≥ bi and − ai j x j ≤ − bi . This conversion potentially double the number of variables and the number of constraints; fortunately, it is rarely necessary in practice. Another useful format for linear programming problems is slack form2 , in which every inequality is of the form x j ≥ 0: max c · x s.t. Ax = b x≥0 It’s fairly easy to convert any linear programming problem into slack form. Slack form is especially useful in executing the simplex algorithm (which we’ll see in the next lecture). I.1 The Geometry of Linear Programming A point x ∈ IRd is feasible with respect to some linear programming problem if it satisfies all the linear constraints. The set of all feasible points is called the feasible region for that linear program. The feasible region has a particularly nice geometric structure that lends some useful intuition to the linear programming algorithms we’ll see later. Any linear equation in d variables defines a hyperplane in IRd ; think of a line when d = 2, or a plane when d = 3. This hyperplane divides IRd into two halfspaces; each halfspace is the set of points that satisfy some linear inequality. Thus, the set of feasible points is the intersection of several hyperplanes 1 2 Confusingly, some authors call this standard form. Confusingly, some authors call this standard form. 2 Algorithms Non-Lecture I: Linear Programming (one for each equality constraint) and halfspaces (one for each inequality constraint). The intersection of a finite number of hyperplanes and halfspaces is called a polyhedron. It’s not hard to verify that any halfspace, and therefore any polyhedron, is convex—if a polyhedron contains two points x and y , then it contains the entire line segment x y . A two-dimensional polyhedron (white) defined by 10 linear inequalities. By rotating IRd (or choosing a coordinate frame) so that the objective function points downward, we can express any linear programming problem in the following geometric form: Find the lowest point in a given polyhedron. With this geometry in hand, we can easily picture two pathological cases where a given linear programming problem has no solution. The first possibility is that there are no feasible points; in this case the problem is called infeasible. For example, the following LP problem is infeasible: maximize x − y subject to 2 x + y ≤ 1 x+ y ≥2 x, y ≥ 0 An infeasible linear programming problem; arrows indicate the constraints. The second possibility is that there are feasible points at which the objective function is arbitrarily large; in this case, we call the problem unbounded. The same polyhedron could be unbounded for some objective functions but not others, or it could be unbounded for every objective function. A two-dimensional polyhedron (white) that is unbounded downward but bounded upward. 3 Algorithms I.2 Non-Lecture I: Linear Programming Example 1: Shortest Paths We can compute the length of the shortest path from s to t in a weighted directed graph by solving the following very simple linear programming problem. maximize dt subject to ds = 0 d v − du ≤ uv for every edge u v Here, u v is the length of the edge u v . Each variable d v represents a tentative shortest-path distance from s to v . The constraints mirror the requirement that every edge in the graph must be relaxed. These relaxation constraints imply that in any feasible solution, d v is at most the shortest path distance from s to v . Thus, somewhat counterintuitively, we are correctly maximizing the objective function to compute the shortest path! In the optimal solution, the objective function d t is the actual shortest-path distance from s to t , but for any vertex v that is not on the shortest path from s to t , d v may be an underestimate of the true distance from s to v . However, we can obtain the true distances from s to every other vertex by modifying the objective function: maximize dv v subject to ds = 0 d v − du ≤ uv for every edge u v There is another formulation of shortest paths as an LP minimization problem using an indicator variable x u v for each edge u v . v xs w =1 xt w = −1 xv uv · xu w =0 for every vertex v = s, t xu minimize v ≥0 for every edge u v uv subject to xu s − u w xu t − u w xu u v − w Intuitively, x u v = 1 means u v lies on the shortest path from s to t , and x u v = 0 means u v does not lie on this shortest path. The constraints merely state that the path should start at s, end at t , and either pass through or avoid every other vertex v . Any path from s to t —in particular, the shortest path—clearly implies a feasible point for this linear program. However, there are other feasible solutions, possibly even optimal solutions, with non-integral values that do not represent paths. Nevertheless, there is always an optimal solution in which every x e is either 0 or 1 and the edges e with x e = 1 comprise the shortest path. (This fact is by no means obvious, but a proof is beyond the scope of these notes.) Moreover, in any optimal solution, even if not every x e is an integer, the objective function gives the shortest path distance! I.3 Example 2: Maximum Flows and Minimum Cuts Recall that the input to the maximum (s, t )-flow problem consists of a weighted directed graph G = (V, E ), two special vertices s and t , and a function assigning a non-negative capacity ce to each edge e. Our task 4 Algorithms Non-Lecture I: Linear Programming is to choose the flow f e across each edge e, as follows: maximize fs − fu fu v =0 v ≤ cu fu subject to s fu w w v ≥0 u fv w − w for every vertex v = s, t u v for every edge u v for every edge u v Similarly, the minimum cut problem can be formulated using ‘indicator’ variables similarly to the shortest path problem. We have a variable S v for each vertex v , indicating whether v ∈ S or v ∈ T , and a variable X u v for each edge u v , indicating whether u ∈ S and v ∈ T , where (S , T ) is some (s, t )-cut.3 minimize cu v · Xu v uv subject to Xu v + S v − Su ≥ 0 for every edge u v ≥0 for every edge u v Xu v Ss = 1 St = 0 Like the minimization LP for shortest paths, there can be optimal solutions that assign fractional values to the variables. Nevertheless, the minimum value for the objective function is the cost of the minimum cut, and there is an optimal solution for which every variable is either 0 or 1, representing an actual minimum cut. No, this is not obvious; in particular, my claim is not a proof! I.4 Linear Programming Duality Each of these pairs of linear programming problems is related by a transformation called duality. For any linear programming problem, there is a corresponding dual linear program that can be obtained by a mechanical translation, essentially by swapping the constraints and the variables. The translation is simplest when the LP is in canonical form: Primal (Π) max c · x s.t. Ax ≤ b x≥0 Dual ( ) ⇐⇒ min y · b s.t. yA ≥ c y≥0 We can also write the dual linear program in exactly the same canonical form as the primal, by swapping the coefficient vector c and the objective vector b, negating both vectors, and replacing the constraint matrix A with its negative transpose.4 Dual ( ) Primal (Π) max c · x s.t. Ax ≤ b x≥ 0 ⇐⇒ 3 max − b · y s.t. −A y ≤ −c y ≥0 These two linear programs are not quite syntactic duals; I’ve added two redundant variables Ss and S t to the min-cut program to increase readability. 4 For the notational purists: In these formulations, x and b are column vectors, and y and c are row vectors. This is a somewhat nonstandard choice. Yes, that means the dot in c · x is redundant. Sue me. 5 Algorithms Non-Lecture I: Linear Programming Written in this form, it should be immediately clear that duality is an involution: The dual of the dual linear program is identical to the primal linear program Π. The choice of which LP to call the ‘primal’ and which to call the ‘dual’ is totally arbitrary.5 The Fundamental Theorem of Linear Programming. A linear program Π has an optimal solution x ∗ if and only if the dual linear program has an optimal solution y ∗ such that c · x ∗ = y ∗ Ax ∗ = y ∗ · b. The weak form of this theorem is trivial to prove. Weak Duality Theorem. If x is a feasible solution for a canonical linear program Π and y is a feasible solution for its dual , then c · x ≤ yAx ≤ y · b. Proof: Because x is feasible for Π, we have Ax ≤ b. Since y is positive, we can multiply both sides of the inequality to obtain yAx ≤ y · b. Conversely, y is feasible for and x is positive, so yAx ≥ c · x . It immediately follows that if c · x = y · b, then x and y are optimal solutions to their respective linear programs. This is in fact a fairly common way to prove that we have the optimal value for a linear program. I.5 Duality Example Before I prove the stronger duality theorem, let me first provide some intuition about where this duality thing comes from in the first place.6 Consider the following linear programming problem: maximize subject to 4 x1 + x2 + 3 x3 x1 + 4 x2 ≤2 3 x1 − x2 + x3 ≤ 4 x1, x2, x3 ≥ 0 Let σ∗ denote the optimum objective value for this LP The feasible solution x = (1, 0, 0) gives us a lower . bound σ∗ ≥ 4. A different feasible solution x = (0, 0, 3) gives us a better lower bound σ∗ ≥ 9. We could play this game all day, finding different feasible solutions and getting ever larger lower bounds. How do we know when we’re done? Is there a way to prove an upper bound on σ∗ ? In fact, there is. Let’s multiply each of the constraints in our LP by a new non-negative scalar value yi : maximize subject to 4 x1 + x2 + 3 x3 y1 ( x 1 + 4 x 2 ) ≤ 2 y1 y2 (3 x 1 − x 2 + x 3 ) ≤ 4 y2 x1, x2, x3 ≥ 0 Because each yi is non-negative, we do not reverse any of the inequalities. Any feasible solution ( x 1 , x 2 , x 3 ) must satisfy both of these inequalities, so it must also satisfy their sum: ( y1 + 3 y2 ) x 1 + (4 y1 − y2 ) x 2 + y2 x 3 ≤ 2 y1 + 4 y2 . 5 For historical reasons, maximization LPs tend to be called ‘primal’ and minimization LPs tend to be called ‘dual’. This is a pointless religious tradition, nothing more. Duality is a relationship between LP problems, not a type of LP problem. 6 This example is taken from Robert Vanderbei’s excellent textbook Linear Programming: Foundations and Extensions [Springer, 2001], but the idea appears earlier in Jens Clausen’s 1997 paper ‘Teaching Duality in Linear Programming: The Multiplier Approach’. 6 Algorithms Non-Lecture I: Linear Programming Now suppose that each yi is larger than the i th coefficient of the objective function: y1 + 3 y2 ≥ 4, 4 y1 − y2 ≥ 1, y2 ≥ 3. This assumption lets us derive an upper bound on the objective value of any feasible solution: 4 x 1 + x 2 + 3 x 3 ≤ ( y1 + 3 y2 ) x 1 + (4 y1 − y2 ) x 2 + y2 x 3 ≤ 2 y1 + 4 y2 . (∗) ∗ ∗ ∗ In particular, by plugging in the optimal solution ( x 1 , x 2 , x 3 ) for the original LP we obtain the following , ∗ upper bound on σ : ∗ ∗ ∗ σ∗ = 4 x 1 + x 2 + 3 x 3 ≤ 2 y1 + 4 y2 . Now it’s natural to ask how tight we can make this upper bound. How small can we make the expression 2 y1 + 4 y2 without violating any of the inequalities we used to prove the upper bound? This is just another linear programming problem. minimize subject to 2 y1 + 4 y2 y1 + 3 y2 ≥ 4 4 y1 − y2 ≥ 1 y2 ≥ 3 y1 , y2 ≥ 0 In fact, this is precisely the dual of our original linear program! Moreover, inequality (∗) is just an instantiation of the Weak Duality Theorem. I.6 Strong Duality The Fundamental Theorem can be rephrased in the following form: Strong Duality Theorem. If x ∗ is an optimal solution for a canonical linear program Π, then there is an optimal solution y ∗ for its dual , such that c · x ∗ = y ∗ Ax ∗ = y ∗ · b. Proof (Sketch): I’ll prove the theorem only for non-degenerate linear programs, in which (a) the optimal solution (if one exists) is a unique vertex of the feasible region, and (b) at most d constraint planes pass through any point. These non-degeneracy assumptions are relatively easy to enforce in practice and can be removed from the proof at the expense of some technical detail. I will also prove the theorem only for the case n ≥ d ; the argument for under-constrained LPs is similar (if not simpler). Let x ∗ be the optimal solution for the linear program Π; non-degeneracy implies that this solution is unique, and that exactly d of the n linear constraints are satisfied with equality. Without loss of generality (by permuting the rows of A), we can assume that these are the first d constraints. So let A• be the d × d matrix containing the first d rows of A, and let A◦ denote the other n − d rows. Similarly, partition b into its first d coordinates b• and everything else b◦ . Thus, we have partitioned the inequality Ax ∗ ≤ b into a system of equations A• x ∗ = b• and a system of strict inequalities A◦ x ∗ < b◦ . ∗ ∗ ∗ ∗ Now let y ∗ = ( y• , y◦ ) where y• = cA−1 and y◦ = 0. We easily verify that y ∗ · b = c · x ∗ : • ∗ ∗ ∗ y ∗ · b = y• · b• + y◦ · b◦ = y• · b• = cA−1 b• = c · x ∗ . • (The existence of the inverse matrix A−1 follows from our non-degeneracy assumption.) Similarly, it’s • easy to verify that y ∗ A ≥ c : ∗ ∗ ∗ y ∗ A = y• A∗ + y◦ A∗ = y• A∗ = c . • ◦ • 7 Algorithms Non-Lecture I: Linear Programming Once we prove that y ∗ is non-negative, and therefore feasible, the Weak Duality Theorem implies ∗ the result. We chose y◦ = 0. As we will see below, the optimality of x ∗ implies the strict inequality ∗ y• > 0—we had to use optimality somewhere! This is the hardest part of the proof. ∗ The key insight is to give a geometric interpretation to the vector y• = cA−1 . Each row of the • ∗ ∗ d linear system A• x = b• describes a hyperplane ai · x = bi in IR . The vector ai is normal to this hyperplane and points out of the feasible region. The vectors a1 , . . . , ad are linearly independent (by ∗ non-degeneracy) and thus describe a coordinate frame for the vector space IRd . The definition of y• can be rewritten as follows: d c= ∗ y• A• = i =1 yi∗ ai . ∗ words, y• ∗ In other lists the coefficients of the objective vector c in the coordinate frame a1 , . . . , ad . The point x lies on exactly d constraint hyperplanes; any d − 1 of these hyperplanes determine a line through x ∗ . For each 1 ≤ i ≤ d , let i denote the line that lies on all but the i th constraint plane, and let vi denote a vector based at x ∗ that points into the halfspace ai x ≤ bi along the line i . This vector lies along an edge of the feasible polytope. For all j = i , we have a j · vi = 0. Thus, we can write A• vi = (0, . . . , 0, ai · vi , 0, . . . , 0) where the scalar ai · vi appears in the i th coordinate. It follows that ∗ c · vi = y• A• vi = yi∗ (ai · vi ). The optimality of x ∗ implies that c · vi < 0, and because vi points into the feasible region while ai points out, we have ai · vi < 0. We conclude that yi∗ > 0. We’re done! Exercises 1. (a) Describe precisely how to dualize a linear program written in slack form: max c · x s.t. Ax = b x≥0 (b) Describe precisely how to dualize a linear program written in general form: d maximize cj x j j =1 d ai j x j ≤ bi for each i = 1 .. p ai j x j = bi for each i = p + 1 .. p + q ai j x j ≥ bi subject to for each i = p + q + 1 .. n j =1 d j =1 d j =1 In both cases, keep the number of dual variables as small as possible. 8 Algorithms Non-Lecture I: Linear Programming 2. A matrix A = (ai j ) is skew-symmetric if and only if a ji = −ai j for all indices i = j ; in particular, every skew-symmetric matrix is square. A canonical linear program max{c · x | Ax ≤ b; x ≥ 0} is self-dual if the matrix A is skew-symmetric and the objective vector c is equal to the constraint vector b. (a) Prove any self-dual linear program Π is equivalent to its dual program . (b) Show that any linear program Π with d variables and n constraints can be transformed into a self-dual linear program with n + d variables and n + d constraints. The optimal solution to the self-dual program should include both the optimal solution for Π (in d of the variables) and the optimal solution for the dual program (in the other n variables). 3. (a) Model the maximum-cardinality bipartite matching problem as a linear programming problem. The input is a bipartite graph G = (U ∪ V ; E ), where E ⊆ U × V ; the output is the largest matching in G . Your linear program should have one variable for each edge. (b) Now dualize the linear program from part (a). What do the dual variables represent? What does the objective function represent? What problem is this!? 4. An integer program is a linear program with the additional constraint that the variables must take only integer values. (a) Prove that deciding whether an integer program has a feasible solution is NP-complete. (b) Prove that finding the optimal feasible solution to an integer program is NP-hard. [Hint: Almost any NP-hard decision problem can be formulated as an integer program. Pick your favorite.] 5. Helly’s theorem states that for any collection of convex bodies in IRd , if every d + 1 of them intersect, then there is a point lying in the intersection of all of them. Prove Helly’s theorem for the special case where the convex bodies are halfspaces. Equivalently, show that if a system of linear inequalities Ax ≤ b does not have a solution, then we can select d + 1 of the inequalities such that the resulting subsystem also does not have a solution. [Hint: Construct a dual LP from the system by choosing a 0 cost vector.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 9 Algorithms Non-Lecture J: Linear Programming Algorithms Simplicibus itaque verbis gaudet Mathematica Veritas, cum etiam per se simplex sit Veritatis oratio. [And thus Mathematical Truth prefers simple words, because the language of Truth is itself simple.] — Tycho Brahe (quoting Seneca (quoting Euripides)) Epistolarum astronomicarum liber primus (1596) When a jar is broken, the space that was inside Merges into the space outside. In the same way, my mind has merged in God; To me, there appears no duality. — Sankara, Viveka-Chudamani (c. 700), translator unknown J Linear Programming Algorithms In this lecture, we’ll see a few algorithms for actually solving linear programming problems. The most famous of these, the simplex method, was proposed by George Dantzig in 1947. Although most variants of the simplex algorithm performs well in practice, no simplex variant is known to run in sub-exponential time in the worst case. However, if the dimension of the problem is considered a constant, there are several linear programming algorithms that run in linear time. I’ll describe a particularly simple randomized algorithm due to Raimund Seidel. My approach to describing these algorithms will rely much more heavily on geometric intuition than the usual linear-algebraic formalism. This works better for me, but your mileage may vary. For a more traditional description of the simplex algorithm, see Robert Vanderbei’s excellent textbook Linear Programming: Foundations and Extensions [Springer, 2001], which can be freely downloaded (but not legally printed) from the author’s website. J.1 Bases, Feasibility, and Local Optimality Consider the canonical linear program max{c · x | Ax ≤ b, x ≥ 0}, where A is an n × d constraint matrix, b is an n-dimensional coefficient vector, and c is a d -dimensional objective vector. We will interpret this linear program geometrically as looking for the lowest point in a convex polyhedron in IRd , described as the intersection of n + d halfspaces. As in the last lecture, we will consider only non-degenerate linear programs: Every subset of d constraint hyperplanes intersects in a single point; at most d constraint hyperplanes pass through any point; and objective vector is linearly independent from any d − 1 constraint vectors. A basis is a subset of d constraints, which by our non-degeneracy assumption must be linearly independent. The location of a basis is the unique point x that satisfies all d constraints with equality; geometrically, x is the unique intersection point of the d hyperplanes. The value of a basis is c · x , n+d where x is the location of the basis. There are precisely d bases. Geometrically, the set of constraint hyperplanes defines a decomposition of IRd into convex polyhedra; this cell decomposition is called the arrangement of the hyperplanes. Every subset of d hyperplanes (that is, every basis) defines a vertex of this arrangement (the location of the basis). I will use the words ‘vertex’ and ‘basis’ interchangeably. A basis is feasible if its location x satisfies all the linear constraints, or geometrically, if the point x is a vertex of the polyhedron. If there are no feasible bases, the linear program is infeasible. A basis is locally optimal if its location x is the optimal solution to the linear program with the same objective function and only the constraints in the basis. Geometrically, a basis is locally optimal if its location x is the lowest point in the intersection of those d halfspaces. A careful reading of the proof of the Strong Duality Theorem reveals that local optimality is the dual equivalent of feasibility; a basis is locally feasible for a linear program Π if and only if the same basis is feasible for the dual linear 1 Algorithms Non-Lecture J: Linear Programming Algorithms program . For this reason, locally optimal bases are sometimes also called dual feasible. If there are no locally optimal bases, the linear program is unbounded.1 Two bases are neighbors if they have d − 1 constraints in common. Equivalently, in geometric terms, two vertices are neighbors if they lie on a line determined by some d − 1 constraint hyperplanes. Every basis is a neighbor of exactly d n other bases; to change a basis into one of its neighbors, there are d choices for which constraint to remove and n choices for which constraint to add. The graph of vertices and edges on the boundary of the feasible polyhedron is a subgraph of the basis graph. The Weak Duality Theorem implies that the value of every feasible basis is less than or equal to the value of every locally optimal basis; equivalently, every feasible vertex is higher than every locally optimal vertex. The Strong Duality Theorem implies that (under our non-degeneracy assumption), if a linear program has an optimal solution, it is the unique vertex that is both feasible and locally optimal. Moreover, the optimal solution is both the lowest feasible vertex and the highest locally optimal vertex. J.2 The Primal Simplex Algorithm: Falling Marbles From a geometric standpoint, Dantzig’s simplex algorithm is very simple. The input is a set H of halfspaces; we want the lowest vertex in the intersection of these halfspaces. SIMPLEX1(H ): if ∩H = ∅ return INFEASIBLE x ← any feasible vertex while x is not locally optimal 〈pivot downward, maintaining feasibility〉〉 if every feasible neighbor of x is higher than x return UNBOUNDED else x ← any feasible neighbor of x that is lower than x return x Let’s ignore the first three lines for the moment. The algorithm maintains a feasible vertex x . At each so-called pivot operation, the algorithm moves to a lower vertex, so the algorithm never visits the same n+d vertex more than once. Thus, the algorithm must halt after at most d pivots. When the algorithm halts, either the feasible vertex x is locally optimal, and therefore the optimum vertex, or the feasible vertex x is not locally optimal but has no lower feasible neighbor, in which case the feasible region must be unbounded. Notice that we have not specified which neighbor to choose at each pivot. Several different pivoting rules have been proposed, but for almost every known pivot rule, there is an input polyhedron that requires an exponential number of pivots under that rule. No pivoting rule is known that guarantees a polynomial number of pivots in the worst case.2 J.3 The Dual Simplex Algorithm: Rising Bubbles We can also geometrically interpret the execution of the simplex algorithm on the dual linear program . Again, the input is a set H of halfspaces, and we want the lowest vertex in the intersection of these 1 For non-degenerate linear programs, the feasible region is unbounded in the objective direction if and only if no basis is locally optimal. However, there are degenerate linear programs with no locally optimal basis that are infeasible. 2 In 1957, Hirsch conjectured that for any linear programming instance with d variables and n + d constraints, starting at any feasible basis, there is a sequence of at most n pivots that leads to the optimal basis. Hirsch’s conjecture is still open 50 years later; no counterexamples have ever been found, but no proof is known except in a few special cases. Truly our ignorance is unbounded (or at least dual infeasible). 2 Algorithms Non-Lecture J: Linear Programming Algorithms halfspaces. By the Strong Duality Theorem, this is the same as the highest locally-optimal vertex in the hyperplane arrangement. SIMPLEX2(H ): if there is no locally optimal vertex return UNBOUNDED x ← any locally optimal vertex while x is not feasbile 〈〈pivot upward, maintaining local optimality〉〉 if every locally optimal neighbor of x is lower than x return INFEASIBLE else x ← any locally-optimal neighbor of x that is higher than x return x Let’s ignore the first three lines for the moment. The algorithm maintains a locally optimal vertex x . At each pivot operation, it moves to a higher vertex, so the algorithm never visits the same vertex more n+d than once. Thus, the algorithm must halt after at most d pivots. When the algorithm halts, either the locally optimal vertex x is feasible, and therefore the optimum vertex, or the locally optimal vertex x is not feasible but has no higher locally optimal neighbor, in which case the problem must be infeasible. The primal simplex (falling marble) algorithm in action. The dual simplex (rising bubble) algorithm in action. From the standpoint of linear algebra, there is absolutely no difference between running SIMPLEX1 on any linear program Π and running SIMPLEX2 on the dual linear program . The actual code is identical. The only difference between the two algorithms is how we interpret the linear algebra geometrically. J.4 Computing the Initial Basis To complete our description of the simplex algorithm, we need to describe how to find the initial vertex x . Our algorithm relies on the following simple observations. First, the feasibility of a vertex does not depend at all on the choice of objective vector; a vertex is either feasible for every objective function or for none. No matter how we rotate the polyhedron, every feasible vertex stays feasible. Conversely (or by duality, equivalently), the local optimality of a vertex does not depend on the exact location of the d hyperplanes, but only on their normal directions and the objective function. No matter how we translate the hyperplanes, every locally optimal vertex stays 3 Algorithms Non-Lecture J: Linear Programming Algorithms locally optimal. In terms of the original matrix formulation, feasibility depends on A and b but not c , and local optimality depends on A and c but not b. The second important observation is that every basis is locally optimal for some objective function. Specifically, it suffices to choose any vector that has a positive inner product with each of the normal vectors of the d chosen hyperplanes. Equivalently, we can make any basis feasible by translating the hyperplanes appropriately. Specifically, it suffices to translate the chosen d hyperplanes so that they pass through the origin, and then translate all the other halfspaces so that they strictly contain the origin. Our strategy for finding our initial feasible vertex is to choose any vertex, choose a new objective function that makes that vertex locally optimal, and then find the optimal vertex for that objective function by running the (dual) simplex algorithm. This vertex must be feasible, even after we restore the original objective function! (a) (b) (c) (a) Choose any basis. (b) Rotate objective to make it locally optimal, and pivot ’upward’ to find a feasible basis. (c) Pivot downward to the optimum basis for the original objective. Equivalently, to find an initial locally optimal vertex, we choose any vertex, translate the hyperplanes so that that vertex becomes feasible, and then find the optimal vertex for those translated constraints using the (primal) simplex algorithm. This vertex must be locally optimal, even after we restore the hyperplanes to their original locations! (a) (b) (c) (a) Choose any basis. (b) Translate constraints to make it feasible, and pivot downward to find a locally optimal basis. (c) Pivot upward to the optimum basis for the original constraints. 4 Algorithms Non-Lecture J: Linear Programming Algorithms Here are more complete descriptions of the simplex algorithm with this initialization rule, in both primal and dual forms. As usual, the input is a set H of halfspaces, and the algorithms either return the lowest vertex in the intersection of these halfspaces or report that no such vertex exists. SIMPLEX1(H ): x ← any vertex ˜ H ← any rotation of H that makes x locally optimal while x is not feasible ˜ if every locally optimal neighbor of x is lower (wrt H ) than x return INFEASIBLE else ˜ x ← any locally optimal neighbor of x that is higher (wrt H ) than x while x is not locally optimal if every feasible neighbor of x is higher than x return UNBOUNDED else x ← any feasible neighbor of x that is lower than x return x SIMPLEX2(H ): x ← any vertex ˜ H ← any translation of H that makes x feasible while x is not locally optimal ˜ if every feasible neighbor of x is higher (wrt H ) than x return UNBOUNDED else ˜ x ← any feasible neighbor of x that is lower (wrt H ) than x while x is not feasible if every locally optimal neighbor of x is lower than x return INFEASIBLE else x ← any locally-optimal neighbor of x that is higher than x return x J.5 Linear Expected Time for Fixed Dimensions In most geometric applications of linear programming, the number of variables is a small constant, but the number of constraints may still be very large. The input to the following algorithm is a set H of n halfspaces and a set B of b hyperplanes. (B stands for basis.) The algorithm returns the lowest point in the intersection of the halfspaces in H and the hyperplanes B . At the top level of recursion, B is empty. I will implicitly assume that the linear program is both feasible and bounded. (If necessary, we can guarantee boundedness by adding a single halfspace to H , and we can guarantee feasibility by adding a dimension.) A point x violates a constraint h if it is not contained in the corresponding halfspace. 5 Algorithms Non-Lecture J: Linear Programming Algorithms SEIDELLP(H , B ) : if |B | = d return B if |H ∪ B | = d return (H ∪ B ) h ← random element of H x ← SEIDELLP(H \ h, B ) (∗) if x violates h return SEIDELLP(H \ h, B ∪ ∂ h) else return x The point x recursively computed in line (∗) is the optimal solution if and only if the random halfspace h is not one of the d halfspaces that define the optimal solution. In other words, the probability of calling SEIDELLP(H , B ∪ h) is exactly (d − b)/n. Thus, we have the following recurrence for the expected number of recursive calls for this algorithm: if b = d or n + b = d 1 T ( n, b ) = d−b · T (n − 1, b + 1) otherwise T (n − 1, b) + n The recurrence is somewhat simpler if we write δ = d − b: if δ = 0 or n = δ 1 T ( n, δ ) = δ T (n − 1, δ) + · T (n − 1, δ − 1) otherwise n It’s easy to prove by induction that T (n, δ) = O(δ! n): T (n, δ) = T (n − 1, δ) + ≤ δ ! ( n − 1) + δ n δ n · T (n − 1, δ − 1) (δ − 1)! · (n − 1) = δ ! ( n − 1) + δ ! [induction hypothesis] n−1 n ≤ δ! n At the top level of recursion, we perform one violation test in O(d ) time. In each of the base cases, we spend O(d 3 ) time computing the intersection point of d hyperplanes, and in the first base case, we spend O(d n) additional time testing for violations. More careful analysis implies that the algorithm runs in O (d ! · n ) expected time. Exercises 1. Fix a non-degenerate linear program in canonical form with d variables and n + d constraints. (a) Prove that every feasible basis has exactly d feasible neighbors. (b) Prove that every locally optimal basis has exactly n locally optimal neighbors. 6 Algorithms Non-Lecture J: Linear Programming Algorithms 2. Suppose you have a subroutine that can solve linear programs in polynomial time, but only if they are both feasible and bounded. Describe an algorithm that solves arbitrary linear programs in polynomial time. Your algorithm should return an optimal solution if one exists; if no optimum exists, your algorithm should report that the input instance is UNBOUNDED or INFEASIBLE, whichever is appropriate. [Hint: Add one variable and one constraint.] 3. (a) Give an example of a non-empty polyhedron Ax ≤ b that is unbounded for every objective vector c . (b) Give an example of an infeasible linear program whose dual is also infeasible. In both cases, your linear program will be degenerate. 4. Describe and analyze an algorithm that solves the following problem in O(n) time: Given n red points and n blue points in the plane, either find a line that separates every red point from every blue point, or prove that no such line exists. 5. The single-source shortest path problem can be formulated as a linear programming problem, with one variable d v for each vertex v = s in the input graph, as follows: maximize dv v dv ≤ s→ v for every edge s → v d v − du ≤ subject to u→ v for every edge u → v with u = s for every vertex v = s dv ≥ 0 This problem asks you to describe the behavior of the simplex algorithm on this linear program in terms of distances. Assume that the edge weights u→ v are all non-negative and that there is a unique shortest path between any two vertices in the graph. (a) What is a basis for this linear program? What is a feasible basis? What is a locally optimal basis? (b) Show that in the optimal basis, every variable d v is equal to the shortest-path distance from s to v . (c) Describe the primal simplex algorithm for the shortest-path linear program directly in terms of vertex distances. In particular, what does it mean to pivot from a feasible basis to a neighboring feasible basis, and how can we execute such a pivot quickly? (d) Describe the dual simplex algorithm for the shortest-path linear program directly in terms of vertex distances. In particular, what does it mean to pivot from a locally optimal basis to a neighboring locally optimal basis, and how can we execute such a pivot quickly? (e) Is Dijkstra’s algorithm an instance of the simplex method? Justify your answer. (f) Is Shimbel’s algorithm an instance of the simplex method? Justify your answer. 6. The maximum (s, t )-flow problem can be formulated as a linear programming problem, with one 7 Algorithms Non-Lecture J: Linear Programming Algorithms variable fu→ v for each edge u → v in the input graph: f s→w − maximize w fu→s u fu→ v = 0 f v →w − subject to w for every vertex v = s, t u fu→ v ≤ cu→ v for every edge u → v fu→ v ≥ 0 for every edge u → v This problem asks you to describe the behavior of the simplex algorithm on this linear program in terms of flows. (a) What is a basis for this linear program? What is a feasible basis? What is a locally optimal basis? (b) Show that the optimal basis represents a maximum flow. (c) Describe the primal simplex algorithm for the flow linear program directly in terms of flows. In particular, what does it mean to pivot from a feasible basis to a neighboring feasible basis, and how can we execute such a pivot quickly? (d) Describe the dual simplex algorithm for the flow linear program directly in terms of flows. In particular, what does it mean to pivot from a locally optimal basis to a neighboring locally optimal basis, and how can we execute such a pivot quickly? (e) Is the Ford-Fulkerson augmenting path algorithm an instance of the simplex method? Justify your answer. [Hint: There is a one-line argument.] 7. (a) Formulate the minimum spanning tree problem as an instance of linear programming. Try to minimize the number of variables and constraints. (b) In your MST linear program, what is a basis? What is a feasible basis? What is a locally optimal basis? (c) Describe the primal simplex algorithm for your MST linear program directly in terms of the input graph. In particular, what does it mean to pivot from a feasible basis to a neighboring feasible basis, and how can we execute such a pivot quickly? (d) Describe the dual simplex algorithm for your MST linear program directly in terms of the input graph. In particular, what does it mean to pivot from a locally optimal basis to a neighboring locally optimal basis, and how can we execute such a pivot quickly? ˙ (e) Which of the classical MST algorithms (Boruvka, Jarník, Kruskal, reverse greedy), if any, are instances of the simplex method? Justify your answer. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 8 Algorithms Lecture 20: Adversary Arguments An adversary means opposition and competition, but not having an adversary means grief and loneliness. — Zhuangzi (Chuang-tsu) c. 300 BC It is possible that the operator could be hit by an asteroid and your $20 could fall off his cardboard box and land on the ground, and while you were picking it up, $5 could blow into your hand. You therefore could win $5 by a simple twist of fate. — Penn Jillette, explaining how to win at Three-Card Monte (1999) 20 20.1 Adversary Arguments Three-Card Monte Until Times Square was sanitized into TimesSquareLandTM by Mayor Rudy Guiliani, you could often find dealers stealing tourists’ money using a game called ‘Three Card Monte’ or ‘Spot the Lady’. The dealer has three cards, say the Queen of Hearts and the two and three of clubs. The dealer shuffles the cards face down on a table (usually slowly enough that you can follow the Queen), and then asks the tourist to bet on which card is the Queen. In principle, the tourist’s odds of winning are at least one in three. In practice, however, the tourist never1 wins, because the dealer cheats. There are actually four cards; before he even starts shuffling the cards, the dealer palms the queen or sticks it up his sleeve. No matter what card the tourist bets on, the dealer turns over a black card. If the tourist gives up, the dealer slides the queen under one of the cards and turns it over, showing the tourist ‘where the queen was all along’. If the dealer is really good, the tourist won’t see the dealer changing the cards and will think maybe the queen was there all along and he just wasn’t smart enough to figure that out. As long as the dealer doesn’t reveal all the black cards at once, the tourist has no way to prove that the dealer cheated!2 20.2 n -Card Monte Now let’s consider a similar game, but with an algorithm acting as the tourist and with bits instead of cards. Suppose we have an array of n bits and we want to determine if any of them is a 1. Obviously we can figure this out by just looking at every bit, but can we do better? Is there maybe some complicated tricky algorithm to answer the question “Any ones?” without looking at every bit? Well, of course not, but how do we prove it? The simplest proof technique is called an adversary argument. The idea is that an all-powerful malicious adversary (the dealer) pretends to choose an input for the algorithm (the tourist). When the algorithm wants looks at a bit (a card), the adversary sets that bit to whatever value will make the algorithm do the most work. If the algorithm does not look at enough bits before terminating, then there will be several different inputs, each consistent with the bits already seen, the should result in different outputs. Whatever the algorithm outputs, the adversary can ‘reveal’ an input that is has all the examined bits but contradicts the algorithm’s output, and then claim that that was the input that he was using all along. Since the only information the algorithm has is the set of bits it examined, the algorithm cannot distinguish between a malicious adversary and an honest user who actually chooses an input in advance and answers all queries truthfully. For the n-card monte problem, the adversary originally pretends that the input array is all zeros— whenever the algorithm looks at a bit, it sees a 0. Now suppose the algorithms stops before looking at all 1 What, never? No, NEVER. Anyone you see winning at Three Card Monte is a shill. Even if the dealer isn’t very good, he cheats anyway. The shills will protect him from any angry tourists who realize they’ve been ripped off, and shake down any tourist who refuses to pay. You cannot win this game. 2 1 Algorithms Lecture 20: Adversary Arguments three bits. If the algorithm says ‘No, there’s no 1,’ the adversary changes one of the unexamined bits to a 1 and shows the algorithm that it’s wrong. If the algorithm says ‘Yes, there’s a 1,’ the adversary reveals the array of zeros and again proves the algorithm wrong. Either way, the algorithm cannot tell that the adversary has cheated. One absolutely crucial feature of this argument is that the adversary makes absolutely no assumptions about the algorithm. The adversary strategy can’t depend on some predetermined order of examining bits, and it doesn’t care about anything the algorithm might or might not do when it’s not looking at bits. Any algorithm that doesn’t examine every bit falls victim to the adversary. 20.3 Finding Patterns in Bit Strings Let’s make the problem a little more complicated. Suppose we’re given an array of n bits and we want to know if it contains the substring 01, a zero followed immediately by a one. Can we answer this question without looking at every bit? It turns out that if n is odd, we don’t have to look at all the bits. First we look the bits in every even position: B [2], B [4], . . . , B [n − 1]. If we see B [i ] = 0 and B [ j ] = 1 for any i < j , then we know the pattern 01 is in there somewhere—starting at the last 0 before B [ j ]—so we can stop without looking at any more bits. If we see only 1s followed by 0s, we don’t have to look at the bit between the last 0 and the first 1. If every even bit is a 0, we don’t have to look at B [1], and if every even bit is a 1, we don’t have to look at B [n]. In the worst case, our algorithm looks at only n − 1 of the n bits. But what if n is even? In that case, we can use the following adversary strategy to show that any algorithm does have to look at every bit. The adversary will attempt to produce an ‘input’ string B without the substring 01; all such strings have the form 11 . . . 100 . . . 0. The adversary maintains two indices and r and pretends that the prefix B [1 .. ] contains only 1s and the suffix B [ r .. n] contains only 0s. Initially = 0 and r = n + 1. 111111 ↑ 0000 ↑ r What the adversary is thinking; represents an unknown bit. The adversary maintains the invariant that r − , the length of the undecided portion of the ‘input’ string, is even. When the algorithm looks at a bit between and r , the adversary chooses whichever value preserves the parity of the intermediate chunk of the array, and then moves either or r . Specifically, here’s what the adversary does when the algorithm examines bit B [i ]. (Note that I’m specifying the adversary strategy as an algorithm!) HIDE01(i ): if i ≤ B [i ] ← 1 else if i ≥ r B [i ] ← 0 else if i − is even B [i ] ← 0 r←i else B [i ] ← 1 ←i It’s fairly easy to prove that this strategy forces the algorithm to examine every bit. If the algorithm doesn’t look at every bit to the right of r , the adversary could replace some unexamined bit with a 1. 2 Algorithms Lecture 20: Adversary Arguments Similarly, if the algorithm doesn’t look at every bit to the left of , the adversary could replace some unexamined bit with a zero. Finally, if there are any unexamined bits between and r , there must be at least two such bits (since r − is always even) and the adversary can put a 01 in the gap. In general, we say that a bit pattern is evasive if we have to look at every bit to decide if a string of n bits contains the pattern. So the pattern 1 is evasive for all n, and the pattern 01 is evasive if and only if n is even. It turns out that the only patterns that are evasive for all values of n are the one-bit patterns 0 and 1. 20.4 Evasive Graph Properties Another class of problems for which adversary arguments give good lower bounds is graph problems where the graph is represented by an adjacency matrix, rather than an adjacency list. Recall that the adjacency matrix of an undirected n-vertex graph G = (V, E ) is an n × n matrix A, where A[i , j ] = (i , j ) ∈ E . We are interested in deciding whether an undirected graph has or does not have a certain property. For example, is the input graph connected? Acyclic? Planar? Complete? A tree? We call n a graph property evasive if we have to look look at all 2 entries in the adjacency matrix to decide whether a graph has that property. An obvious example of an evasive graph property is emptiness: Does the graph have any edges at all? We can show that emptiness is evasive using the following simple adversary strategy. The adversary maintains two graphs E and G . E is just the empty graph with n vertices. Initially G is the complete graph on n vertices. Whenever the algorithm asks about an edge, the adversary removes that edge from G (unless it’s already gone) and answers ‘no’. If the algorithm terminates without examining every edge, then G is not empty. Since both G and E are consistent with all the adversary’s answers, the algorithm must give the wrong answer for one of the two graphs. 20.5 Connectedness Is Evasive Now let me give a more complicated example, connectedness. Once again, the adversary maintains two graphs, Y and M (‘yes’ and ‘maybe’). Y contains all the edges that the algorithm knows are definitely in the input graph. M contains all the edges that the algorithm thinks might be in the input graph, or in other words, all the edges of Y plus all the unexamined edges. Initially, Y is empty and M is complete. Here’s the strategy that adversary follows when the adversary asks whether the input graph contains the edge e. I’ll assume that whenever an algorithm examines an edge, it’s in M but not in Y ; in other words, algorithms never ask about the same edge more than once. HIDECONNECTEDNESS(e): if M \ {e} is connected remove (i , j ) from M return 0 else add e to Y return 1 Notice that the graphs Y and M are both consistent with the adversary’s answers at all times. The adversary strategy maintains a few other simple invariants. • Y is a subgraph of M . This is obvious. • M is connected. This is also obvious. 3 Algorithms Lecture 20: Adversary Arguments • If M has a cycle, none of its edges are in Y . If M has a cycle, then deleting any edge in that cycle leaves M connected. • Y is acyclic. This follows directly from the previous invariant. • If Y = M , then Y is disconnected. The only connected acyclic graph is a tree. Suppose Y is a tree and some edge e is in M but not in Y . Then there is a cycle in M that contains e, all of whose other edges are in Y . This violated our third invariant. We can also think about the adversary strategy in terms of minimum spanning trees. Recall the anti-Kruskal algorithm for computing the maximum spanning tree of a graph: Consider the edges one at a time in increasing order of length. If removing an edge would disconnect the graph, declare it part of the spanning tree (by adding it to Y ); otherwise, throw it away (by removing it from M ). If the n algorithm examines all 2 possible edges, then Y and M are both equal to the maximum spanning tree of the complete n-vertex graph, where the weight of an edge is the time when the algorithm asked about it. n Now, if an algorithm terminates before examining all 2 edges, then there is at least one edge in M that is not in Y . Since the algorithm cannot distinguish between M and Y , even though M is connected and Y is not, the algorithm cannot possibly give the correct output for both graphs. Thus, in order to be correct, any algorithm must examine every edge—Connectedness is evasive! 20.6 An Evasive Conjecture A graph property is nontrivial is there is at least one graph with the property and at least one graph without the property. (The only trivial properties are ‘Yes’ and ‘No’.) A graph property is monotone if it is closed under taking subgraphs — if G has the property, then any subgraph of G has the property. For example, emptiness, planarity, acyclicity, and non-connectedness are monotone. The properties of being a tree and of having a vertex of degree 3 are not monotone. Conjecture 1 (Aanderraa, Karp, and Rosenberg). Every nontrivial monotone property of n-vertex graphs is evasive. The Aanderraa-Karp-Rosenberg conjecture has been proven when n = p e for some prime p and positive integer exponent e—the proof uses some interesting results from algebraic topology3 —but it is still open for other values of n.’ There are non-trivial non-evasive graph properties, but all known examples are non-monotone. One such property—‘scorpionhood’—is described in an exercise at the end of this lecture note. 20.7 Finding the Minimum and Maximum Last time, we saw an adversary argument that finding the largest element of an unsorted set of n numbers requires at least n − 1 comparisons. Let’s consider the complexity of finding the largest and smallest elements. More formally: Given a sequence X = 〈 x 1 , x 2 , . . . , x n 〉 of n distinct numbers, find indices i and j such that x i = min X and x j = max X . Let ∆ be a contractible simplicial complex whose automorphism group Aut(∆) is vertex-transitive, and let Γ be a vertextransitive subgroup of Aut(∆). If there are normal subgroups Γ1 Γ2 Γ such that |Γ1 | = pα for some prime p and integer α, |Γ/Γ2 | = qβ for some prime q and integer β , and Γ2 /Γ1 is cyclic, then ∆ is a simplex. 3 No, this will not be on the final exam. 4 Algorithms Lecture 20: Adversary Arguments How many comparisons do we need to solve this problem? An upper bound of 2n − 3 is easy: find the minimum in n − 1 comparisons, and then find the maximum of everything else in n − 2 comparisons. Similarly, a lower bound of n − 1 is easy, since any algorithm that finds the min and the max certainly finds the max. We can improve both the upper and the lower bound to 3n/2 − 2. The upper bound is established by the following algorithm. Compare all n/2 consecutive pairs of elements x 2i −1 and x 2i , and put the smaller element into a set S and the larger element into a set L . if n is odd, put x n into both L and S . Then find the smallest element of S and the largest element of L . The total number of comparisons is at most n n n 3n −1 + −1 = + − 2. 2 2 2 2 build S and L compute min S compute max L For the lower bound, we use an adversary argument. The adversary marks each element + if it might be the maximum element, and − if it might be the minimum element. Initially, the adversary puts both marks + and − on every element. If the algorithm compares two double-marked elements, then the adversary declares one smaller, removes the + mark from the smaller element, and removes the − mark from the larger one. In every other case, the adversary can answer so that at most one mark needs to be removed. For example, if the algorithm compares a double marked element against one labeled −, the adversary says the one labeled − is smaller and removes the − mark from the other. If the algorithm compares to +’s, the adversary must unmark one of the two. Initially, there are 2n marks. At the end, in order to be correct, exactly one item must be marked + and exactly one other must be marked −, since the adversary can make any + the maximum and any − the minimum. Thus, the algorithm must force the adversary to remove 2n − 2 marks. At most n/2 comparisons remove two marks; every other comparison removes at most one mark. Thus, the adversary strategy forces any algorithm to perform at least 2n − 2 − n/2 = 3n/2 − 2 comparisons. 20.8 Finding the Median Finally, let’s consider the median problem: Given an unsorted array X of n numbers, find its n/2th largest entry. (I’ll assume that n is even to eliminate pesky floors and ceilings.) More formally: Given a sequence 〈 x 1 , x 2 , . . . , x n 〉 of n distinct numbers, find the index m such that x m is the n/2th largest element in the sequence. To prove a lower bound for this problem, we can use a combination of information theory and two adversary arguments. We use one adversary argument to prove the following simple lemma: Lemma 1. Any comparison tree that correctly finds the median element also identifies the elements smaller than the median and larger than the median. Proof: Suppose we reach a leaf of a decision tree that chooses the median element x m , and there is still some element x i that isn’t known to be larger or smaller than x m . In other words, we cannot decide based on the comparisons that we’ve already performed whether x i < x m or x i > x m . Then in particular no element is known to lie between x i and x m . This means that there must be an input that is consistent with the comparisons we’ve performed, in which x i and x m are adjacent in sorted order. But then we can swap x i and x m , without changing the result of any comparison, and obtain a different consistent input in which x i is the median, not x m . Our decision tree gives the wrong answer for this ‘swapped’ input. 5 Algorithms Lecture 20: Adversary Arguments This lemma lets us rephrase the median-finding problem yet again. Given a sequence X = 〈 x 1 , x 2 , . . . , x n 〉 of n distinct numbers, find the indices of its n/2 − 1 largest elements L and its n/2th largest element x m . Now suppose a ‘little birdie’ tells us the set L of elements larger than the median. This information fixes the outcomes of certain comparisons—any item in L is bigger than any element not in L —so we can ‘prune’ those comparisons from the comparison tree. The pruned tree finds the largest element of X \ L (the median of X ), and thus must have depth at least n/2 − 1. In fact, the adversary argument in the last lecture implies that every leaf in the pruned tree must have depth at least n/2 − 1, so the pruned tree has at least 2n/2−1 leaves. n There are n/2−1 ≈ 2n / n/2 possible choices for the set L . Every leaf in the original comparison tree is also a leaf in exactly one of the least n/2−1 2n/2−1 ≈ 23n/2 / depth at least n n n/2−1 pruned trees, so the original comparison tree must have at n/2 leaves. Thus, any comparison tree that finds the median must have n 2 − 1 + lg n n/ 2 − 1 = 3n 2 − O(log n). A more complicated adversary argument (also involving pruning the comparison tree with little birdies) improves this lower bound to 2n − o(n). n A similar argument implies that at least n − k + lg k−1 = Ω((n − k) + k log(n/k)) comparisons are required to find the kth largest element in an n-element set. This bound is tight up to constant factors for all k ≤ n/2; there is an algorithm that uses at most O(n + k log(n/k)) comparisons. Moreover, this lower bound is exactly tight when k = 1 or k = 2. In fact, these are the only values of k ≤ n/2 for which the exact complexity of the selection problem is known. Even the case k = 3 is still open! Exercises 1. (a) Let X be a set containing an odd number of n-bit strings. Prove that any algorithm that decides whether a given n-bit string is an element of X must examine every bit of the input string in the worst case. (b) Give a one-line proof that the bit pattern 01 is evasive for all even n. (c) Prove that the bit pattern 11 is evasive if and only if n mod 3 = 1. (d) Prove that the bit pattern 111 is evasive if and only if n mod 4 = 0 or 3. 2. Suppose we are given the adjacency matrix of a directed graph G with n vertices. Describe an algorithm that determines whether G has a sink by probing only O(n) bits in the input matrix. A sink is a vertex that has an incoming edge from every other vertex, but no outgoing edges. 3. A scorpion is an undirected graph with three special vertices: the sting, the tail, and the body. The sting is connected only to the tail; the tail is connected only to the sting and the body; and the body is connected to every vertex except the sting. The rest of the vertices (the head, eyes, legs, antennae, teeth, gills, flippers, wheels, etc.) can be connected arbitrarily. Describe an algorithm that determines whether a given n-vertex graph is a scorpion by probing only O(n) entries in the adjacency matrix. 6 Algorithms Lecture 20: Adversary Arguments 4. Prove using an advesary argument that acyclicity is an evasive graph property. [Hint: Kruskal.] 5. Prove that finding the second largest element in an n-element array requires exactly n − 2 + lg n comparisons in the worst case. Prove the upper bound by describing and analyzing an algorithm; prove the lower bound using an adversary argument. 6. Let T be a perfect ternary tree where every leaf has depth . Suppose each of the 3 leaves of T is labeled with a bit, either 0 or 1, and each internal node is labeled with a bit that agrees with the majority of its children. (a) Prove that any deterministic algorithm that determines the label of the root must examine all 3 leaf bits in the worst case. (b) Describe and analyze a randomized algorithm that determines the root label, such that the expected number of leaves examined is o(3 ). (You may want to review the notes on randomized algorithms.) 7. UIUC has just finished constructing the new Reingold Building, the tallest dormitory on campus. In order to determine how much insurance to buy, the university administration needs to determine the highest safe floor in the building. A floor is consdered safe if a drunk student an egg can fall from a window on that floor and land without breaking; if the egg breaks, the floor is considered unsafe. Any floor that is higher than an unsafe floor is also considered unsafe. The only way to determine whether a floor is safe is to drop an egg from a window on that floor. You would like to find the lowest unsafe floor L by performing as few tests as possible; unfortunately, you have only a very limited supply of eggs. (a) Prove that if you have only one egg, you can find the lowest unsafe floor with L tests. [Hint: Yes, this is trivial.] (b) Prove that if you have only one egg, you must perform at least L tests in the worst case. In other words, prove that your algorithm from part (a) is optimal. [Hint: Use an adversary argument.] (c) Describe an algorithm to find the lowest unsafe floor using two eggs and only O( L ) tests. [Hint: Ideally, each egg should be dropped the same number of times. How many floors can you test with n drops?] (d) Prove that if you start with two eggs, you must perform at least Ω( L ) tests in the worst case. In other words, prove that your algorithm from part (c) is optimal. (e) Describe an algorithm to find the lowest unsafe floor using k eggs, using as few tests as possible, and prove your algorithm is optimal for all values of k. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms for the most recent revision. 7 Algorithms Lecture 21: NP-Hard Problems The wonderful thing about standards is that there are so many of them to choose from. — Real Admiral Grace Murray Hopper If a problem has no solution, it may not be a problem, but a fact — not to be solved, but to be coped with over time. — Shimon Peres 21 21.1 NP-Hard Problems ‘Efficient’ Problems A generally-accepted minimum requirement for an algorithm to be considered ‘efficient’ is that its running time is polynomial: O(nc ) for some constant c , where n is the size of the input.1 Researchers recognized early on that not all problems can be solved this quickly, but we had a hard time figuring out exactly which ones could and which ones couldn’t. there are several so-called NP-hard problems, which most people believe cannot be solved in polynomial time, even though nobody can prove a super-polynomial lower bound. Circuit satisfiability is a good example of a problem that we don’t know how to solve in polynomial time. In this problem, the input is a boolean circuit: a collection of AND, OR, and NOT gates connected by wires. We will assume that there are no loops in the circuit (so no delay lines or flip-flops). The input to the circuit is a set of m boolean (TRUE/FALSE) values x 1 , . . . , x m . The output is a single boolean value. Given specific input values, we can calculate the output of the circuit in polynomial (actually, linear) time using depth-first-search, since we can compute the output of a k-input gate in O(k) time. x y x∧y x y x∨y x x An And gate, an Or gate, and a Not gate. x1 x2 x3 x4 x5 A boolean circuit. inputs enter from the left, and the output leaves to the right. The circuit satisfiability problem asks, given a circuit, whether there is an input that makes the circuit output TRUE, or conversely, whether the circuit always outputs FALSE. Nobody knows how to solve this problem faster than just trying all 2m possible inputs to the circuit, but this requires exponential time. On the other hand, nobody has ever proved that this is the best we can do; maybe there’s a clever algorithm that nobody has discovered yet! 1 This notion of efficiency was independently formalized by Alan Cobham (The intrinsic computational difficulty of functions. Logic, Methodology, and Philosophy of Science (Proc. Int. Congress), 24–30, 1965), Jack Edmonds (Paths, trees, and flowers. Canadian Journal of Mathematics 17:449–467, 1965), and Michael Rabin (Mathematical theory of automata. Proceedings of the 19th ACM Symposium in Applied Mathematics, 153–175, 1966), although similar notions were considered more than a decade earlier by Kurt Gödel and John von Neumann. 1 Algorithms 21.2 Lecture 21: NP-Hard Problems P, NP, and co-NP A decision problem is a problem whose output is a single boolean value: YES or NO.2 Let me define three classes of decision problems: • P is the set of decision problems that can be solved in polynomial time.3 Intuitively, P is the set of problems that can be solved quickly. • NP is the set of decision problems with the following property: If the answer is YES, then there is a proof of this fact that can be checked in polynomial time. Intuitively, NP is the set of decision problems where we can verify a YES answer quickly if we have the solution in front of us. • co-NP is the opposite of NP. If the answer to a problem in co-NP is NO, then there is a proof of this fact that can be checked in polynomial time. For example, the circuit satisfiability problem is in NP. If the answer is YES, then any set of m input values that produces TRUE output is a proof of this fact; we can check the proof by evaluating the circuit in polynomial time. It is widely believed that circuit satisfiability is not in P or in co-NP, but nobody actually knows. Every decision problem in P is also in NP. If a problem is in P, we can verify YES answers in polynomial time recomputing the answer from scratch! Similarly, any problem in P is also in co-NP. One of the most important open questions in theoretical computer science is whether or not P = NP. Nobody knows. Intuitively, it should be obvious that P = NP; the homeworks and exams in this class and others have (I hope) convinced you that problems can be incredibly hard to solve, even when the solutions are obvious in retrospect. But nobody knows how to prove it. A more subtle but still open question is whether NP and co-NP are different. Even if we can verify every YES answer quickly, there’s no reason to think that we can also verify NO answers quickly. For example, as far as we know, there is no short proof that a boolean circuit is not satisfiable. It is generally believed that NP = co-NP, but nobody knows how to prove it. co−NP NP P What we think the world looks like. 21.3 NP-hard, NP-easy, and NP-complete A problem Π is NP-hard if a polynomial-time algorithm for Π would imply a polynomial-time algorithm for every problem in NP. In other words: Π is NP-hard ⇐⇒ If Π can be solved in polynomial time, then P=NP 2 Technically, I should be talking about languages, which are just sets of bit strings. The language associated with a decision problem is the set of bit strings for which the answer is YES. For example, for the problem is ‘Is the input graph connected?’, the corresponding language is the set of connected graphs, where each graph is represented as a bit string (for example, its adjacency matrix). 3 More formally, P is the set of languages that can be recognized in polynomial time by a single-tape Turing machine. If you want to learn more about Turing machines, take CS 579. 2 Algorithms Lecture 21: NP-Hard Problems Intuitively, if we could solve one particular NP-hard problem quickly, then we could quickly solve any problem whose solution is easy to understand, using the solution to that one special problem as a subroutine. NP-hard problems are at least as hard as any problem in NP.4 Saying that a problem is NP-hard is like saying ‘If I own a dog, then it can speak fluent English.’ You probably don’t know whether or not I own a dog, but you’re probably pretty sure that I don’t own a talking dog. Nobody has a mathematical proof that dogs can’t speak English—the fact that no one has ever heard a dog speak English is evidence, as are the hundreds of examinations of dogs that lacked the proper mouth shape and braiNPower, but mere evidence is not a mathematical proof. Nevertheless, no sane person would believe me if I said I owned a dog that spoke fluent English. So the statement ‘If I own a dog, then it can speak fluent English’ has a natural corollary: No one in their right mind should believe that I own a dog! Likewise, if a problem is NP-hard, no one in their right mind should believe it can be solved in polynomial time. Finally, a problem is NP-complete if it is both NP-hard and an element of NP (or ‘NP-easy’). NPcomplete problems are the hardest problems in NP. If anyone finds a polynomial-time algorithm for even one NP-complete problem, then that would imply a polynomial-time algorithm for every NP-complete problem. Literally thousands of problems have been shown to be NP-complete, so a polynomial-time algorithm for one (that is, all) of them seems incredibly unlikely. NP−hard co−NP NP NP−complete P More of what we think the world looks like. It is not immediately clear that any decision problems are NP-hard or NP-complete. NP-hardness is already a lot to demand of a problem; insisting that the problem also have a nondeterministic polynomial-time algorithm seems almost completely unreasonable. The following remarkable theorem was first published by Steve Cook in 1971 and independently by Leonid Levin in 1973.5 I won’t even sketch the proof, since I’ve been (deliberately) vague about the definitions. The Cook-Levin Theorem. Circuit satisfiability is NP-complete. More formally, a problem Π is NP-hard if and only if, for any problem Π in NP, there is a polynomial-time Turing reduction from Π to Π—a Turing reduction just means a reduction that can be executed on a Turing machine. Polynomial-time Turing reductions are also called Cook reductions. Complexity theorists prefer to define NP-hardness in terms of polynomial-time many-one reductions, which are also called Karp reductions. A many-one reduction from one language Π to another language Π is an function f : Σ∗ → Σ∗ such that x ∈ Π if and only if f ( x ) ∈ Π. Every Karp reduction is a Cook reduction, but not vice versa. Every reduction (between decision problems) in these notes is a Karp reduction. This definition is preferred primarily because NP is closed under Karp reductions, but believed not to be closed under Cook reductions. Moreover, the two definitions of NP-hardness are equivalent only if NP=co-NP, which is considered unlikely. In fact, there are natural problems that are (1) NP-hard with respect to Cook reductions, but (2) NP-hard with respect to Karp reductions only if P=NP! On the other hand, the Karp definition only applies to decision problems, or more formally, sets of bit-strings. To make things even more confusing, both Cook and Karp originally defined NP-hardness in terms of logarithmic-space reductions. Every logarithmic-space reduction is a polynomial-time reduction, but (we think) not vice versa. It is an open question whether relaxing the set of allowed (Cook or Karp) reductions from logarithmic-space to polynomial-time changes the set of NP-hard problems. 5 Levin submitted his results, and discussed them in talks, several years before they were published. So naturally, in accordance with Stigler’s Law, this result is often called ‘Cook’s Theorem’. Cook won the Turing award for his proof; Levin did not. 4 3 Algorithms 21.4 Lecture 21: NP-Hard Problems Reductions and SAT To prove that a problem is NP-hard, we use a reduction argument. Reducing problem A to another problem B means describing an algorithm to solve problem A under the assumption that an algorithm for problem B already exists. You’re already used to doing reductions, only you probably call it something else, like writing subroutines or utility functions, or modular programming. To prove something is NP-hard, we describe a similar transformation between problems, but not in the direction that most people expect. You should tattoo the following rule of onto the back of your hand. To prove that problem A is NP-hard, reduce a known NP-hard problem to A. In other words, to prove that your problem is hard, you need to describe an algorithm to solve a different problem, which you already know is hard, using a mythical algorithm for your problem as a subroutine. The essential logic is a proof by contradiction. Your reduction shows implies that if your problem were easy, then the other problem would be easy, too. Equivalently, since you know the other problem is hard, your problem must also be hard. For example, consider the formula satisfiability problem, usually just called SAT . The input to SAT is a boolean formula like ¯ ¯ (a ∨ b ∨ c ∨ d ) ⇔ (( b ∧ ¯) ∨ (a ⇒ d ) ∨ (c = a ∧ b)), c and the question is whether it is possible to assign boolean values to the variables a, b, c , . . . so that the formula evaluates to TRUE. To show that SAT is NP-hard, we need to give a reduction from a known NP-hard problem. The only problem we know is NP-hard so far is circuit satisfiability, so let’s start there. Given a boolean circuit, we can transform it into a boolean formula by creating new output variables for each gate, and then just writing down the list of gates separated by and. For example, we could transform the example circuit into a formula as follows: x1 y1 y4 y5 x2 x3 x4 x5 y7 y2 y8 y3 y6 ( y1 = x 1 ∧ x 4 ) ∧ ( y2 = x 4 ) ∧ ( y3 = x 3 ∧ y2 ) ∧ ( y4 = y1 ∨ x 2 ) ∧ ( y5 = x 2 ) ∧ ( y6 = x 5 ) ∧ ( y7 = y3 ∨ y5 ) ∧ ( y8 = y4 ∧ y7 ∧ y6 ) ∧ y8 A boolean circuit with gate variables added, and an equivalent boolean formula. Now the original circuit is satisfiable if and only if the resulting formula is satisfiable. Given a satisfying input to the circuit, we can get a satisfying assignment for the formula by computing the output of every gate. Given a satisfying assignment for the formula, we can get a satisfying input the the circuit by just ignoring the gate variables yi . We can transform any boolean circuit into a formula in linear time using depth-first search, and the size of the resulting formula is only a constant factor larger than the size of the circuit. Thus, we have a polynomial-time reduction from circuit satisfiability to SAT: 4 Algorithms Lecture 21: NP-Hard Problems O(n) −−→ boolean formula boolean circuit − SAT trivial TRUE or FALSE ←−− TRUE or FALSE − TCSAT (n) ≤ O(n) + TSAT (O(n)) =⇒ TSAT (n) ≥ TCSAT (Ω(n)) − O(n) The reduction implies that if we had a polynomial-time algorithm for SAT, then we’d have a polynomialtime algorithm for circuit satisfiability, which would imply that P=NP. So SAT is NP-hard. To prove that a boolean formula is satisfiable, we only have to specify an assignment to the variables that makes the formula TRUE. We can check the proof in linear time just by reading the formula from left to right, evaluating as we go. So SAT is also in NP, and thus is actually NP-complete. 21.5 3SAT (from SAT) A special case of SAT that is particularly useful in proving NP-hardness results is called 3SAT. A boolean formula is in conjunctive normal form (CNF) if it is a conjunction (AND) of several clauses, each of which is the disjunction (OR) of several literals, each of which is either a variable or its negation. For example: clause ¯ (a ∨ b ∨ c ∨ d ) ∧ ( b ∨ ¯ ∨ d ) ∧ (a ∨ c ∨ d ) ∧ (a ∨ ¯ ) c¯ b A 3CNF formula is a CNF formula with exactly three literals per clause; the previous example is not a 3CNF formula, since its first clause has four literals and its last clause has only two. 3SAT is just SAT restricted to 3CNF formulas: Given a 3CNF formula, is there an assignment to the variables that makes the formula evaluate to TRUE? We could prove that 3SAT is NP-hard by a reduction from the more general SAT problem, but it’s easier just to start over from scratch, with a boolean circuit. We perform the reduction in several stages. 1. Make sure every AND and OR gate has only two inputs. If any gate has k > 2 inputs, replace it with a binary tree of k − 1 two-input gates. 2. Write down the circuit as a formula, with one clause per gate. This is just the previous reduction. 3. Change every gate clause into a CNF formula. There are only three types of clauses, one for each type of gate: a= b∧c ¯ ¯ −→ (a ∨ ¯ ∨ ¯) ∧ (a ∨ b) ∧ (a ∨ c ) bc ¯ −→ (a ∨ b ∨ c ) ∧ (a ∨ ¯ ) ∧ (a ∨ ¯) b c a= b∨c ¯b a = ¯ −→ (a ∨ b) ∧ (a ∨ ¯ ) b 4. Make sure every clause has exactly three literals. Introduce new variables into each one- and two-literal clause, and expand it into two clauses as follows: ¯ ¯y a −→ (a ∨ x ∨ y ) ∧ (a ∨ x ∨ y ) ∧ (a ∨ x ∨ ¯ ) ∧ (a ∨ x ∨ ¯ ) y ¯ a ∨ b −→ (a ∨ b ∨ x ) ∧ (a ∨ b ∨ x ) 5 Algorithms Lecture 21: NP-Hard Problems For example, if we start with the same example circuit we used earlier, we obtain the following 3CNF formula. Although this may look a lot more ugly and complicated than the original circuit at first glance, it’s actually only a constant factor larger—every binary gate in the original circuit has been transformed into at most five clauses. Even if the formula size were a large polynomial function (like n573 ) of the circuit size, we would still have a valid reduction. ( y1 ∨ x 1 ∨ x 4 ) ∧ ( y1 ∨ x 1 ∨ z1 ) ∧ ( y1 ∨ x 1 ∨ z1 ) ∧ ( y1 ∨ x 4 ∨ z2 ) ∧ ( y1 ∨ x 4 ∨ z2 ) ∧ ( y2 ∨ x 4 ∨ z3 ) ∧ ( y2 ∨ x 4 ∨ z3 ) ∧ ( y2 ∨ x 4 ∨ z4 ) ∧ ( y2 ∨ x 4 ∨ z4 ) ∧ ( y3 ∨ x 3 ∨ y2 ) ∧ ( y3 ∨ x 3 ∨ z5 ) ∧ ( y3 ∨ x 3 ∨ z5 ) ∧ ( y3 ∨ y2 ∨ z6 ) ∧ ( y3 ∨ y2 ∨ z6 ) ∧ ( y4 ∨ y1 ∨ x 2 ) ∧ ( y4 ∨ x 2 ∨ z7 ) ∧ ( y4 ∨ x 2 ∨ z7 ) ∧ ( y4 ∨ y1 ∨ z8 ) ∧ ( y4 ∨ y1 ∨ z8 ) ∧ ( y5 ∨ x 2 ∨ z9 ) ∧ ( y5 ∨ x 2 ∨ z9 ) ∧ ( y5 ∨ x 2 ∨ z10 ) ∧ ( y5 ∨ x 2 ∨ z10 ) ∧ ( y6 ∨ x 5 ∨ z11 ) ∧ ( y6 ∨ x 5 ∨ z11 ) ∧ ( y6 ∨ x 5 ∨ z12 ) ∧ ( y6 ∨ x 5 ∨ z12 ) ∧ ( y7 ∨ y3 ∨ y5 ) ∧ ( y7 ∨ y3 ∨ z13 ) ∧ ( y7 ∨ y3 ∨ z13 ) ∧ ( y7 ∨ y5 ∨ z14 ) ∧ ( y7 ∨ y5 ∨ z14 ) ∧ ( y8 ∨ y4 ∨ y7 ) ∧ ( y8 ∨ y4 ∨ z15 ) ∧ ( y8 ∨ y4 ∨ z15 ) ∧ ( y8 ∨ y7 ∨ z16 ) ∧ ( y8 ∨ y7 ∨ z16 ) ∧ ( y9 ∨ y8 ∨ y6 ) ∧ ( y9 ∨ y8 ∨ z17 ) ∧ ( y9 ∨ y8 ∨ z17 ) ∧ ( y9 ∨ y6 ∨ z18 ) ∧ ( y9 ∨ y6 ∨ z18 ) ∧ ( y9 ∨ z19 ∨ z20 ) ∧ ( y9 ∨ z19 ∨ z20 ) ∧ ( y9 ∨ z19 ∨ z20 ) ∧ ( y9 ∨ z19 ∨ z20 ) This process transforms the circuit into an equivalent 3CNF formula; the output formula is satisfiable if and only if the input circuit is satisfiable. As with the more general SAT problem, the formula is only a constant factor larger than any reasonable description of the original circuit, and the reduction can be carried out in polynomial time. Thus, we have a polynomial-time reduction from circuit satisfiability to 3SAT: O(n) boolean circuit − −−→ 3CNF formula 3SAT trivial TRUE or FALSE ←−− TRUE or FALSE − TCSAT (n) ≤ O(n) + T3SAT (O(n)) =⇒ T3SAT (n) ≥ TCSAT (Ω(n)) − O(n) We conclude 3SAT is NP-hard. And because 3SAT is a special case of SAT, it is also in NP. Therefore, 3SAT is NP-complete. 21.6 Maximum Independent (from 3SAT) For the next few problems we consider, the input is a simple, unweighted graph, and the problem asks for the size of the largest or smallest subgraph satisfying some structural property. Let G be an arbitrary graph. An independent set in G is a subset of the vertices of G with no edges between them. The maximum independent set problem, or simply MAXINDSET, asks for the size of the largest independent set in a given graph. I’ll prove that MAXINDSET is NP-hard (but not NP-complete, since it isn’t a decision problem) using a reduction from 3SAT. I’ll describe a reduction from a 3CNF formula into a graph that has an independent set of a certain size if and only if the formula is satisfiable. The graph has one node for each instance of each literal in the formula. Two nodes are connected by an edge if (1) they correspond to literals in the same clause, or (2) they correspond to a variable and its inverse. For example, the formula ¯ (a ∨ b ∨ c ) ∧ ( b ∨ ¯ ∨ d ) ∧ (a ∨ c ∨ d ) ∧ (a ∨ ¯ ∨ d ) is transformed into the following graph. c¯ b¯ 6 Algorithms Lecture 21: NP-Hard Problems a c b ‾ b b a c ‾ ‾ d ‾ d d a ‾ c A graph derived from a 3CNF formula, and an independent set of size 4. Black edges join literals from the same clause; red (heavier) edges join contradictory literals. Now suppose the original formula had k clauses. Then I claim that the formula is satisfiable if and only if the graph has an independent set of size k. 1. independent set =⇒ satisfying assignment: If the graph has an independent set of k vertices, then each vertex must come from a different clause. To obtain a satisfying assignment, we assign the value TRUE to each literal in the independent set. Since contradictory literals are connected by edges, this assignment is consistent. There may be variables that have no literal in the independent set; we can set these to any value we like. The resulting assignment satisfies the original 3CNF formula. 2. satisfying assignment =⇒ independent set: If we have a satisfying assignment, then we can choose one literal in each clause that is TRUE. Those literals form an independent set in the graph. Thus, the reduction is correct. Since the reduction from 3CNF formula to graph takes polynomial time, we conclude that MAXINDSET is NP-hard. Here’s a diagram of the reduction: O(n) 3CNF formula with k clauses − −−→ graph with 3k nodes MAXINDSET O(1) TRUE or FALSE ←−− maximum independent set size − T3SAT (n) ≤ O(n) + TMAXINDSET (O(n)) 21.7 =⇒ TMAXINDSET (n) ≥ T3SAT (Ω(n)) − O(n) Clique (from Independent Set) A clique is another name for a complete graph, that is, a graph where every pair of vertices is connected by an edge. The maximum clique size problem, or simply MAXCLIQUE, is to compute, given a graph, the number of nodes in its largest complete subgraph. A graph with maximum clique size 4. 7 Algorithms Lecture 21: NP-Hard Problems There is an easy proof that MAXCLIQUE is NP-hard, using a reduction from MAXINDSET. Any graph G has an edge-complement G with the same vertices, but with exactly the opposite set of edges—(u, v ) is an edge in G if and only if it is not an edge in G . A set of vertices is independent in G if and only if the same vertices define a clique in G . Thus, we can compute the largest independent in a graph simply by computing the largest clique in the complement of the graph. O(n) −−→ complement graph G graph G − MAXCLIQUE trivial largest independent set ←−− largest clique − 21.8 Vertex Cover (from Independent Set) A vertex cover of a graph is a set of vertices that touches every edge in the graph. The MINVERTEXCOVER problem is to find the smallest vertex cover in a given graph. Again, the proof of NP-hardness is simple, and relies on just one fact: If I is an independent set in a graph G = (V, E ), then V \ I is a vertex cover. Thus, to find the largest independent set, we just need to find the vertices that aren’t in the smallest vertex cover of the same graph. trivial −−→ graph G = (V, E ) graph G = (V, E ) − MINVERTEXCOVER O(n) largest independent set V \ S ←−− smallest vertex cover S − 21.9 Graph Coloring (from 3SAT) A k-coloring of a graph is a map C : V → {1, 2, . . . , k} that assigns one of k ‘colors’ to each vertex, so that every edge has two different colors at its endpoints. The graph coloring problem is to find the smallest possible number of colors in a legal coloring. To show that this problem is NP-hard, it’s enough to consider the special case 3COLORABLE: Given a graph, does it have a 3-coloring? To prove that 3COLORABLE is NP-hard, we use a reduction from 3SAT. Given a 3CNF formula, we produce a graph as follows. The graph consists of a truth gadget, one variable gadget for each variable in the formula, and one clause gadget for each clause in the formula. The truth gadget is just a triangle with three vertices T , F , and X , which intuitively stand for TRUE, FALSE, and OTHER. Since these vertices are all connected, they must have different colors in any 3-coloring. For the sake of convenience, we will name those colors TRUE, FALSE, and OTHER. Thus, when we say that a node is colored TRUE, all we mean is that it must be colored the same as the node T . The variable gadget for a variable a is also a triangle joining two new nodes labeled a and a to node X in the truth gadget. Node a must be colored either TRUE or FALSE, and so node a must be colored either FALSE or TRUE, respectively. Finally, each clause gadget joins three literal nodes to node T in the truth gadget using five new unlabeled nodes and ten edges; see the figure below. If all three literal nodes in the clause gadget are colored FALSE, then the rightmost vertex in the gadget cannot have one of the three colors. Since the variable gadgets force each literal node to be colored either TRUE or FALSE, in any valid 3-coloring, at least one of the three literal nodes is colored TRUE. I need to emphasize here that the final graph contains ¯ only one node T , only one node F , and only two nodes a and a for each variable. 8 Algorithms Lecture 21: NP-Hard Problems a X X T F b a T c a Gadgets for the reduction from 3SAT to 3-Colorability: The truth gadget, a variable gadget for a, and a clause gadget for (a ∨ b ∨ ¯). c The proof of correctness is just brute force. If the graph is 3-colorable, then we can extract a satisfying assignment from any 3-coloring—at least one of the three literal nodes in every clause gadget is colored TRUE. Conversely, if the formula is satisfiable, then we can color the graph according to any satisfying assignment. O(n) 3CNF formula − −−→ graph 3COLORABLE trivial TRUE or FALSE ←−− TRUE or FALSE − ¯ For example, the formula (a ∨ b ∨ c ) ∧ ( b ∨ ¯ ∨ d ) ∧ (a ∨ c ∨ d ) ∧ (a ∨ ¯ ∨ d ) that I used to illustrate the c¯ b¯ MAXCLIQUE reduction would be transformed into the following graph. The 3-coloring is one of several that correspond to the satisfying assignment a = c = TRUE, b = d = FALSE. T F X a a b b c c d d A 3-colorable graph derived from a satisfiable 3CNF formula. We can easily verify that a graph has been correctly 3-colored in linear time: just compare the endpoints of every edge. Thus, 3COLORING is in NP, and therefore NP-complete. Moreover, since 3COLORING is a special case of the more general graph coloring problem—What is the minimum number of colors?—the more problem is also NP-hard, but not NP-complete, because it’s not a decision problem. 9 Algorithms 21.10 Lecture 21: NP-Hard Problems Hamiltonian Cycle (from Vertex Cover) A Hamiltonian cycle in a graph is a cycle that visits every vertex exactly once. This is very different from an Eulerian cycle, which is actually a closed walk that traverses every edge exactly once. Eulerian cycles are easy to find and construct in linear time using a variant of depth-first search. Finding Hamiltonian cycles, on the other hand, is NP-hard. To prove this, we use a reduction from the vertex cover problem. Given a graph G and an integer k, we need to transform it into another graph G , such that G has a Hamiltonian cycle if and only if G has a vertex cover of size k. As usual, our transformation uses several gadgets. • For each edge (u, v ) in G , we have an edge gadget in G consisting of twelve vertices and fourteen edges, as shown below. The four corner vertices (u, v, 1), (u, v, 6), ( v, u, 1), and ( v, u, 6) each have an edge leaving the gadget. A Hamiltonian cycle can only pass through an edge gadget in one of three ways. Eventually, these will correspond to one or both of the vertices u and v being in the vertex cover. (u,v,1) (u,v,2) (u,v,3) (u,v,4) (u,v,5) (u,v,6) (v,u,1) (v,u,2) (v,u,3) (v,u,4) (v,u,5) (v,u,6) An edge gadget for (u, v ) and the only possible Hamiltonian paths through it. • G also contains k cover vertices, simply numbered 1 through k. • Finally, for each vertex u in G , we string together all the edge gadgets for edges (u, v ) into a single vertex chain, and then connect the ends of the chain to all the cover vertices. Specifically, suppose u has d neighbors v1 , v2 , . . . , vd . Then G has d − 1 edges between (u, vi , 6) and (u, vi +1 , 1), plus k edges between the cover vertices and (u, v1 , 1), and finally k edges between the cover vertices and (u, vd , 6). 1 2 3 (v,x) (v,y) (v,z) (w,v) (x,v) (y,v) (z,v) ... (v,w) k The vertex chain for v : all edge gadgets involving v are strung together and joined to the k cover vertices. It’s not hard to prove that if { v1 , v2 , . . . , vk } is a vertex cover of G , then G has a Hamiltonian cycle— start at cover vertex 1, through traverse the vertex chain for v1 , then visit cover vertex 2, then traverse the vertex chain for v2 , and so forth, eventually returning to cover vertex 1. Conversely, any Hamiltonian cycle in G alternates between cover vertices and vertex chains, and the vertex chains correspond to the k vertices in a vertex cover of G . (This is a little harder to prove.) Thus, G has a vertex cover of size k if and only if G has a Hamiltonian cycle. 10 Algorithms Lecture 21: NP-Hard Problems (u,v) 1 (v,u) u v (u,w) (v,x) (w,u) w (v,w) (w,v) (x,v) x (w,x) 2 (x,w) The original graph G with vertex cover { v, w }, and the transformed graph G with a corresponding Hamiltonian cycle. Vertex chains are colored to match their corresponding vertices. The transformation from G to G takes at most O(n2 ) time, so the Hamiltonian cycle problem is NP-hard. Moreover, since we can easily verify a Hamiltonian cycle in linear time, the Hamiltonian cycle problem is in NP, and therefore is NP-complete. A closely related problem to Hamiltonian cycles is the famous traveling salesman problem—Given a weighted graph G , find the shortest cycle that visits every vertex. Finding the shortest cycle is obviously harder than determining if a cycle exists at all, so the traveling salesman problem is also NP-hard. 21.11 Subset Sum (from Vertex Cover) The last problem that we will prove NP-hard is the SUBSETSUM problem considered in the very first lecture on recursion: Given a set X of integers and an integer t , determine whether X has a subset whose elements sum to t . To prove this problem is NP-hard, we apply a reduction from the vertex cover problem. Given a graph G and an integer k, we need to transform it into set of integers X and an integer t , such that X has a subset that sums to t if and only if G has an vertex cover of size k. Our transformation uses just two ‘gadgets’; these are integers representing vertices and edges in G . Number the edges of G arbitrarily from 0 to m − 1. Our set X contains the integer bi := 4i for each edge i , and the integer a v := 4 m + 4i i ∈∆( v ) for each vertex v , where ∆( v ) is the set of edges that have v has an endpoint. Alternately, we can think of each integer in X as an (m + 1)-digit number written in base 4. The mth digit is 1 if the integer represents a vertex, and 0 otherwise. For each i < m, the i th digit is 1 if the integer represents edge i or one of its endpoints, and 0 otherwise. Finally, we set the target sum m−1 t := k · 4 m + 2 · 4i . i =0 11 Algorithms Lecture 21: NP-Hard Problems Now let’s prove that the reduction is correct. First, suppose there is a vertex cover of size k in the original graph G . Consider the subset X C ⊆ X that includes a v for every vertex v in the vertex cover, and bi for every edge i that has exactly one vertex in the cover. The sum of these integers, written in base 4, has a 2 in each of the first m digits; in the most significant digit, we are summing exactly k 1’s. Thus, the sum of the elements of X C is exactly t . On the other hand, suppose there is a subset X ⊆ X that sums to t . Specifically, we must have av + v ∈V bi = t i∈E for some subsets V ⊆ V and E ⊆ E . Again, if we sum these base-4 numbers, there are no carries in the first m digits, because for each i there are only three numbers in X whose i th digit is 1. Each edge number bi contributes only one 1 to the i th digit of the sum, but the i th digit of t is 2. Thus, for each edge in G , at least one of its endpoints must be in V . In other words, V is a vertex cover. On the other hand, only vertex numbers are larger than 4m , and t /4m = k, so V has at most k elements. (In fact, it’s not hard to see that V has exactly k elements.) For example, given the four-vertex graph used on the previous page to illustrate the reduction to Hamiltonian cycle, our set X might contain the following base-4 integers: buv buw b vw bv x bw x := 0100004 = 256 := 0010004 = 64 := 0001004 = 16 := 0000104 = 4 := 0000014 = 1 au := 1110004 = 1344 a v := 1101104 = 1300 aw := 1011014 = 1105 a x := 1000114 = 1029 If we are looking for a vertex cover of size 2, our target sum would be t := 2222224 = 2730. Indeed, the vertex cover { v, w } corresponds to the subset {a v , aw , buv , buw , b v x , bw x }, whose sum is 1300 + 1105 + 256 + 64 + 4 + 1 = 2730. The reduction can clearly be performed in polynomial time. Since VERTEXCOVER is NP-hard, it follows that SUBSETSUM is NP-hard. There is one subtle point that needs to be emphasized here. Way back at the beginning of the semester, we developed a dynamic programming algorithm to solve SUBSETSUM in time O(nt ). Isn’t this a polynomial-time algorithm? Nope. True, the running time is polynomial in n and t , but in order to qualify as a true polynomial-time algorithm, the running time must be a polynomial function of the size of the input. The values of the elements of X and the target sum t could be exponentially larger than the number of input bits. Indeed, the reduction we just described produces exponentially large integers, which would force our dynamic programming algorithm to run in exponential time. Algorithms like this are called pseudo-polynomial-time, and any NP-hard problem with such an algorithm is called weakly NP-hard. 21.12 Other Useful NP-hard Problems Literally thousands of different problems have been proved to be NP-hard. I want to close this note by listing a few NP-hard problems that are useful in deriving reductions. I won’t describe the NP-hardness 12 Algorithms Lecture 21: NP-Hard Problems for these problems, but you can find most of them in Garey and Johnson’s classic Scary Black Book of NP-Completeness.6 • PLANARCIRCUITSAT: Given a boolean circuit that can be embedded in the plane so that no two wires cross, is there an input that makes the circuit output TRUE? This can be proved NP-hard by reduction from the general circuit satisfiability problem, by replacing each crossing with a small series of gates. (This is an easy7 exercise.) • NOTALLEQUAL3SAT: Given a 3CNF formula, is there an assignment of values to the variables so that every clause contains at least one TRUE literal and at least one FALSE literal? This can be proved NP-hard by reduction from the usual 3SAT. • PLANAR3SAT: Given a 3CNF boolean formula, consider a bipartite graph whose vertices are the clauses and variables, where an edge indicates that a variable (or its negation) appears in a clause. If this graph is planar, the 3CNF formula is also called planar. The PLANAR3SAT problem asks, given a planar 3CNF formula, whether it has a satisfying assignment. This can be proved NP-hard by reduction from PLANARCIRCUITSAT.8 • EXACT3DIMENSIONALMATCHING or X3M: Given a set S and a collection of three-element subsets of S , called triples, is there a sub-collection of disjoint triples that exactly cover S ? This can be proved NP-hard by a reduction from 3SAT. • PARTITION: Given a set S of n integers, are there subsets A and B such that A ∪ B = S , A ∩ B = ∅, and a= b? a ∈A b∈B This can be proved NP-hard by a simple reduction from SUBSETSUM. Like SUBSETSUM, the PARTITION problem is only weakly NP-hard. • 3PARTITION: Given a set S of 3n integers, can it be partitioned into n disjoint subsets, each with 3 elements, such that every subset has exactly the same sum? Note that this is very different from the PARTITION problem; I didn’t make up the names. This can be proved NP-hard by reduction from X3M. Unlike PARTITION, the 3PARTITION problem is strongly NP-hard, that is, it remains NP-hard even if the input numbers are less than some polynomial in n. The similar problem of dividing a set of 2n integers into n equal-weight two-element sets can be solved in O(n log n) time. • SETCOVER: Given a collection of sets = {S1 , S2 , . . . , Sm }, find the smallest sub-collection of Si ’s that contains all the elements of i Si . This is a generalization of both VERTEXCOVER and X3M. • HITTINGSET: Given a collection of sets = {S1 , S2 , . . . , Sm }, find the minimum number of elements of i Si that hit every set in . This is also a generalization of VERTEXCOVER. • LONGESTPATH: Given a non-negatively weighted graph G and two vertices u and v , what is the longest simple path from u to v in the graph? A path is simple if it visits each vertex at most once. This is a generalization of the HAMILTONIANPATH problem. Of course, the corresponding shortest path problem is in P. 6 Michael Garey and David Johnson. Computers and Intractability: A Guide to the Theory of NP-Completeness. W. H. Freeman and Co., 1979. 7 or at least nondeterministically easy 8 Surprisingly, PLANARNOTALLEQUAL3SAT is solvable in polynomial time! 13 Algorithms Lecture 21: NP-Hard Problems • STEINERTREE: Given a weighted, undirected graph G with some of the vertices marked, what is the minimum-weight subtree of G that contains every marked vertex? If every vertex is marked, the minimum Steiner tree is just the minimum spanning tree; if exactly two vertices are marked, the minimum Steiner tree is just the shortest path between them. This can be proved NP-hard by reduction to HAMILTONIANPATH. Most interesting puzzles and solitaire games have been shown to be NP-hard, or to have NP-hard generalizations. (Arguably, if a game or puzzle isn’t at least NP-hard, it isn’t interesting!) Some familiar examples include Minesweeper (by reduction from CIRCUITSAT)9 , Tetris (by reduction from 3PARTITION)10 , and Sudoku (by reduction from 3SAT)11 . Most two-player games12 like tic-tac-toe, reversi, checkers, chess, go, mancala—or more accurately, appropriate generalizations of these constant-size games to arbitrary board sizes—are not just NP-hard, but PSPACE-hard or even EXP-hard.13 Exercises 1. Consider the following problem, called B OXDEPTH: Given a set of n axis-aligned rectangles in the plane, how big is the largest subset of these rectangles that contain a common point? (a) Describe a polynomial-time reduction from B OXDEPTH to MAXCLIQUE. (b) Describe and analyze a polynomial-time algorithm for B OXDEPTH. [Hint: O(n3 ) time should be easy, but O(n log n) time is possible.] (c) Why don’t these two results imply that P=NP? 2. (a) Describe a polynomial-time reduction from PARTITION to SUBSETSUM. (b) Describe a polynomial-time reduction from SUBSETSUM to PARTITION. 3. (a) Describe and analyze and algorithm to solve PARTITION in time O(nM ), where n is the size of the input set and M is the sum of the absolute values of its elements. Richard Kaye. Minesweeper is NP-complete. Mathematical Intelligencer 22(2):9–15, 2000. http://www.mat.bham.ac. uk/R.W.Kaye/minesw/minesw.pdf 10 Ron Breukelaar*, Erik D. Demaine, Susan Hohenberger*, Hendrik J. Hoogeboom, Walter A. Kosters, and David LibenNowell*. Tetris is Hard, Even to Approximate. International Journal of Computational Geometry and Applications 14:41–68, 2004. The reduction was considerably simplified between its discovery in 2002 and its publication in 2004. 11 Takayuki Yato and Takahiro Seta. Complexity and Completeness of Finding Another Solution and Its Application to Puzzles. IEICE Transactions on Fundamentals of Electronics, Communications and Computer Sciences E86-A(5):1052–1060, 2003. http://www-imai.is.s.u-tokyo.ac.jp/~yato/data2/MasterThesis.pdf. 12 For a good (but now slightly dated) overview of known results on the computational complexity of games and puzzles, see Erik D. Demaine’s survey “Playing Games with Algorithms: Algorithmic Combinatorial Game Theory” at http://arxiv.org/abs/ cs.CC/0106019. 13 PSPACE and EXP are the next two big steps above NP in the complexity hierarchy. PSPACE is the set of decision problems that can be solved using polynomial space. Every problem in NP (and therefore in P) is also in PSPACE. It is generally believed that NP = PSPACE, but nobody can even prove that P = PSPACE. A problem Π is PSPACE-hard if, for any problem Π that can be solved using polynomial space, there is a polynomial-time many-one reduction from Π to Π. If any PSPACE-hard problem is in NP, then PSPACE=NP. c EXP (also called EXPTIME) is the set of decision problems that can be solved in exponential time: at most 2n for some c > 0. Every problem in PSPACE (and therefore in NP (and therefore in P)) is also in EXP. It is generally believed that PSPACE EXP, but nobody can even prove that NP = EXP. We do know that P EXP, but we do not know of a single natural decision problem in P \ EXP. A problem Π is EXP-hard if, for any problem Π that can be solved in exponential time, there is a polynomial-time many-one reduction from Π to Π. If any EXP-hard problem is in PSPACE, then EXP=PSPACE. Then there’s NEXP then EXPSPACE, then EEXP then NEEXP then EEXPSPACE, and so on ad infinitum. Whee! , , , 9 14 Algorithms Lecture 21: NP-Hard Problems (b) Why doesn’t this algorithm imply that P=NP? 4. A boolean formula is in disjunctive normal form (or DNF ) if it consists of a disjunction (OR) or several terms, each of which is the conjunction (AND) of one or more literals. For example, the formula (a ∧ b ∧ c ) ∨ ( b ∧ c ) ∨ (a ∧ b ∧ c ) is in disjunctive normal form. DNF-SAT asks, given a boolean formula in disjunctive normal form, whether that formula is satisfiable. (a) Show that DNF-SAT is in P. (b) What is the error in the following argument that P=NP? Suppose we are given a boolean formula in conjunctive normal form with at most three literals per clause, and we want to know if it is satisfiable. We can use the distributive law to construct an equivalent formula in disjunctive normal form. For example, (a ∨ b ∨ c ) ∧ (a ∨ b) ⇐⇒ (a ∧ b) ∨ ( b ∧ a) ∨ (c ∧ a) ∨ (c ∧ b) Now we can use the algorithms from part (a) to determine, in polynomial time, whether the resulting DNF formula is satisfiable. We have just solved 3SAT in polynomial time! Since 3SAT is NP-hard, we must conclude that P=NP. 5. (a) Describe and analyze a polynomial-time algorithm for 2PARTITION. Given a set S of 2n positive integers, your algorithm will determine in polynomial time whether the elements of S can be split into n disjoint pairs whose sums are all equal. (b) Describe and analyze a polynomial-time algorithm for 2COLOR. Given an undirected graph G , your algorithm will determine in polynomial time whether G has a proper coloring that uses only two colors. (c) Describe and analyze a polynomial-time algorithm for 2SAT. Given a boolean formula Φ in conjunctive normal form, with exactly two literals per clause, your algorithm will determine in polynomial time whether Φ has a satisfying assignment. 6. (a) Prove that PLANARCIRCUITSAT is NP-complete. (b) Prove that NOTALLEQUAL3SAT is NP-complete. (c) Prove that the following variant of 3SAT is NP-complete: Given a formula φ in conjunctive normal form where each clause contains at most 3 literals and each variable appears in at most 3 clauses, is φ satisfiable? 7. The problem 12COLOR is defined as follows: Given a graph, can we color each vertex with one of twelve colors, so that every edge touches two different colors? Prove that 12COLOR is NP-hard. 8. Jeff tries to make his students happy. At the beginning of class, he passes out a questionnaire that lists a number of possible course policies in areas where he is flexible. Every student is asked to respond to each possible course policy with one of “strongly favor”, “mostly neutral”, or “strongly oppose”. Each student may respond with “strongly favor” or “strongly oppose” to at most five questions. Because Jeff’s students are very understanding, each student is happy if (but only if) he 15 Algorithms Lecture 21: NP-Hard Problems or she prevails in just one of his or her strong policy preferences. Either describe a polynomial-time algorithm for setting course policy to maximize the number of happy students, or show that the problem is NP-hard. 9. (a) Using the gadget on the right below, prove that deciding whether a given planar graph is 3-colorable is NP-complete. [Hint: Show that the gadget can be 3-colored, and then replace any crossings in a planar embedding with the gadget appropriately.] (b) Using part (a) and the middle gadget below, prove that deciding whether a planar graph with maximum degree 4 is 3-colorable is NP-complete. [Hint: Replace any vertex with degree greater than 4 with a collection of gadgets connected so that no degree is greater than four.] 3 1 3 0 0 (a) Gadget for planar 3-colorability. (b) Gadget for degree-4 planar 3-colorability. 4 2 2 4 (c) A careful 5-coloring. 10. Recall that a 5-coloring of a graph G is a function that assigns each vertex of G an ‘color’ from the set {0, 1, 2, 3, 4}, such that for any edge uv , vertices u and v are assigned different ’colors’. A 5-coloring is careful if the colors assigned to adjacent vertices are not only distinct, but differ by more than 1 (mod 5). Prove that deciding whether a given graph has a careful 5-coloring is NP-complete. [Hint: Reduce from the standard 5COLOR problem.] 11. Prove that the following problems are NP-complete. (a) Given two undirected graphs G and H , is G isomorphic to a subgraph of H ? (b) Given an undirected graph G , does G have a spanning tree in which every node has degree at most 17? (c) Given an undirected graph G , does G have a spanning tree with at most 42 leaves? 12. The RECTANGLETILING problem asks, given a ‘large’ rectangle R and several ‘small’ rectangles r1 , r2 , . . . , rn , whether the small rectangles can be placed inside the larger rectangle with no gaps or overlaps. Prove that RECTANGLETILING is NP-complete. 13. (a) A tonian path in a graph G is a path that goes through at least half of the vertices of G . Show that determining whether a graph has a tonian path is NP-complete. (b) A tonian cycle in a graph G is a cycle that goes through at least half of the vertices of G . Show that determining whether a graph has a tonian cycle is NP-complete. [Hint: Use part (a).] 14. For each problem below, either describe a polynomial-time algorithm or prove that the problem is NP-complete. 16 Algorithms Lecture 21: NP-Hard Problems (a) A double-Eulerian circuit in an undirected graph G is a closed walk that traverses every edge in G exactly twice. Given a graph G , does G have a double-Eulerian circuit? (b) A double-Hamiltonian circuit in an undirected graph G is a closed walk that visits every vertex in G exactly twice. Given a graph G , does G have a double-Hamiltonian circuit? 15. A boolean formula in exclusive-or conjunctive normal form (XCNF) is a conjunction (AND) of several clauses, each of which is the exclusive-or of several literals; that is, a clause is true if and only if it contains an odd number of true literals. The XCNF-SAT problem asks whether a given XCNF formula is satisfiable. Either describe a polynomial-time algorithm for XCNF-SAT or prove that it is NP-hard. 16. Let G be an undirected graph with weighted edges. A heavy Hamiltonian cycle is a cycle C that passes through each vertex of G exactly once, such that the total weight of the edges in C is at least half of the total weight of all edges in G . Prove that deciding whether a graph has a heavy Hamiltonian cycle is NP-complete. 5 8 1 6 2 4 5 12 7 8 3 9 5 A heavy Hamiltonian cycle. The cycle has total weight 34; the graph has total weight 67. 17. Pebbling is a solitaire game played on an undirected graph G , where each vertex has zero or more pebbles. A single pebbling move consists of removing two pebbles from a vertex v and adding one pebble to an arbitrary neighbor of v . (Obviously, the vertex v must have at least two pebbles before the move.) The PEBBLEDESTRUCTION problem asks, given a graph G = (V, E ) and a pebble count p( v ) for each vertex v , whether is there a sequence of pebbling moves that removes all but one pebble. Prove that PEBBLEDESTRUCTION is NP-complete. 18. The next time you are at a party, one of the guests will suggest everyone play a round of Three-Way Mumbletypeg, a game of skill and dexterity that requires three teams and a knife. The official Rules of Three-Way Mumbletypeg (fixed during the Holy Roman Three-Way Mumbletypeg Council in 1625) require that (1) each team must have at least one person, (2) any two people on the same team must know each other, and (3) everyone watching the game must be on one of the three teams. Of course, it will be a really fun party; nobody will want to leave. There will be several pairs of people at the party who don’t know each other. The host of the party, having heard thrilling tales of your prowess in all things algorithmic, will hand you a list of which pairs of party-goers know each other and ask you to choose the teams, while he sharpens the knife. Either describe and analyze a polynomial time algorithm to determine whether the party-goers can be split into three legal Three-Way Mumbletypeg teams, or prove that the problem is NP-hard. 17 Algorithms Lecture 21: NP-Hard Problems 19. (a) Suppose you are given a magic black box that can determine in polynomial time, given an arbitrary weighted graph G , the length of the shortest Hamiltonian cycle in G . Describe and analyze a polynomial-time algorithm that computes, given an arbitrary weighted graph G , the shortest Hamiltonian cycle in G , using this magic black box as a subroutine. (b) Suppose you are given a magic black box that can determine in polynomial time, given an arbitrary graph G , the number of vertices in the largest complete subgraph of G . Describe and analyze a polynomial-time algorithm that computes, given an arbitrary graph G , a complete subgraph of G of maximum size, using this magic black box as a subroutine. (c) Suppose you are given a magic black box that can determine in polynomial time, given an arbitrary weighted graph G , whether G is 3-colorable. Describe and analyze a polynomialtime algorithm that either computes a proper 3-coloring of a given graph or correctly reports that no such coloring exists, using the magic black box as a subroutine. [Hint: The input to the magic black box is a graph. Just a graph. Vertices and edges. Nothing else.] (d) Suppose you are given a magic black box that can determine in polynomial time, given an arbitrary boolean formula Φ, whether Φ is satisfiable. Describe and analyze a polynomialtime algorithm that either computes a satisfying assignment for a given boolean formula or correctly reports that no such assignment exists, using the magic black box as a subroutine. (e) Suppose you are given a magic black box that can determine in polynomial time, given an initial Tetris configuration and a finite sequence of Tetris pieces, whether a perfect player can play every piece. (This problem is NP-hard.) Describe and analyze a polynomialtime algorithm that computes the shortest Hamiltonian cycle in a given weighted graph in polynomial time, using this magic black box as a subroutine. [Hint: Use part (a). You do not need to know anything about Tetris to solve this problem.] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 18 Algorithms Non-Lecture K: Approximation Algorithms Le mieux est l’ennemi du bien. [The best is the enemy of the good.] — Voltaire, La Bégueule (1772) Who shall forbid a wise skepticism, seeing that there is no practical question on which any thing more than an approximate solution can be had? — Ralph Waldo Emerson, Representative Men (1850) Now, distrust of corporations threatens our still-tentative economic recovery; it turns out greed is bad, after all. — Paul Krugman, “Greed is Bad”, The New York Times, June 4, 2002. K K.1 Approximation Algorithms Load Balancing On the future smash hit reality-TV game show Grunt Work, scheduled to air Thursday nights at 3am (2am Central) on ESPNπ, the contestants are given a series of utterly pointless tasks to perform. Each task has a predetermined time limit; for example, “Sharpen this pencil for 17 seconds”, or “Pour pig’s blood on your head and sing The Star-Spangled Banner for two minutes”, or “Listen to this 75-minute algorithms lecture”. The directors of the show want you to assign each task to one of the contestants, so that the last task is completed as early as possible. When your predecessor correctly informed the directors that their problem is NP-hard, he was immediately fired. “Time is money!” they screamed at him. “We don’t need perfection. Wake up, dude, this is television!” Less facetiously, suppose we have a set of n jobs, which we want to assign to m machines. We are given an array T [1 .. n] of non-negative numbers, where T [ j ] is the running time of job j . We can describe an assignment by an array A[1 .. n], where A[ j ] = i means that job j is assigned to machine i . The makespan of an assignment is the maximum time that any machine is busy: makespan(A) = max i T [ j] A[ j ]=i The load balancing problem is to compute the assignment with the smallest possible makespan. It’s not hard to prove that the load balancing problem is NP-hard by reduction from PARTITION: The array T [1 .. n] can be evenly partitioned if and only if there is an assignment to two machines with makespan exactly i T [i ]/2. A slightly more complicated reduction from 3PARTITION implies that the load balancing problem is strongly NP-hard. If we really need the optimal solution, there is a dynamic programming algorithm that runs in time O(nM m ), where M is the minimum makespan, but that’s just horrible. There is a fairly natural and efficient greedy heuristic for load balancing: consider the jobs one at a time, and assign each job to the machine with the earliest finishing time. GREEDYLOADBALANCE( T [1 .. n], m): for i ← 1 to m Total[i ] ← 0 for j ← 1 to n min ← 1 for i ← 2 to n if Total[i ] < Total[min] min ← i A[ j ] ← min Total[min] ← Total[min] + T [ j ] return A[1 .. m] 1 Algorithms Non-Lecture K: Approximation Algorithms Theorem 1. The makespan of the assignment computed by GREEDYLOADBALANCE is at most twice the makespan of the optimal assignment. Proof: Fix an arbitrary input, and let OP T denote the makespan of its optimal assignment. The approximation bound follows from two trivial observations. First, the makespan of any assignment (and therefore of the optimal assignment) is at least the duration of the longest job. Second, the makespan of any assignment is at least the total duration of all the jobs divided by the number of machines. OP T ≥ max T [ j ] and j OP T ≥ 1 m n T [ j] j =1 Now consider the assignment computed by GREEDYLOADBALANCE. Suppose machine i has the largest total running time, and let j be the last job assigned to job i . Our first trivial observation implies that T [ j ] ≤ OP T . To finish the proof, we must show that Total[i ] − T [ j ] ≤ OP T . Job j was assigned to machine i because it had the smallest finishing time, so Total[i ] − T [ j ] ≤ Total[k] for all k. (Some values Total[k] may have increased since job j was assigned, but that only helps us.) In particular, Total[i ] − T [ j ] is less than or equal to the average finishing time over all machines. Thus, Total[i ] − T [ j ] ≤ 1 m m Total[i ] = i =1 1 m n T [ j ] ≤ OP T j =1 by our second trivial observation. We conclude that the makespan Total[i ] is at most 2 · OP T . makespan j ≤ OPT ≤ OPT i Proof that GreedyLoadBalance is a 2-approximation algorithm GREEDYLOADBALANCE is an online algorithm: It assigns jobs to machines in the order that the jobs appear in the input array. Online approximation algorithms are useful in settings where inputs arrive in a stream of unknown length—for example, real jobs arriving at a real scheduling algorithm. In this online setting, it may be impossible to compute an optimum solution, even in cases where the offline problem (where all inputs are known in advance) can be solved in polynomial time. The study of online algorithms could easily fill an entire one-semester course (alas, not this one). In our original offline setting, we can improve the approximation factor by sorting the jobs before piping them through the greedy algorithm. SORTEDGREEDYLOADBALANCE( T [1 .. n], m): sort T in decreasing order return GREEDYLOADBALANCE( T, m) Theorem 2. The makespan of the assignment computed by SORTEDGREEDYLOADBALANCE is at most 3/2 times the makespan of the optimal assignment. 2 Algorithms Non-Lecture K: Approximation Algorithms Proof: Let i be the busiest machine in the schedule computed by SORTEDGREEDYLOADBALANCE. If only one job is assigned to machine i , then the greedy schedule is actually optimal, and the theorem is trivially true. Otherwise, let j be the last job assigned to machine i . Since each of the first m jobs is assigned to a unique processor, we must have j ≥ m + 1. As in the previous proof, we know that Total[i ] − T [ j ] ≤ OP T . In the optimal assignment, at least two of the first m + 1 jobs, say jobs k and , must be scheduled on the same processor. Thus, T [k] + T [ ] ≤ OP T . Since max{k, } ≤ m + 1 ≤ j , and the jobs are sorted in decreasing order by direction, we have T [ j ] ≤ T [m + 1] ≤ T [max{k, }] = min { T [k], T [ ]} ≤ OP T /2. We conclude that the makespan Total[i ] is at most 3 · OP T /2, as claimed. K.2 Generalities Consider an arbitrary optimization problem. Let OP T (X ) denote the value of the optimal solution for a given input X , and let A(X ) denote the value of the solution computed by algorithm A given the same input X . We say that A is an α(n )-approximation algorithm if and only if OP T (X ) A(X ) ≤ α(n) and A(X ) OP T (X ) ≤ α(n) for all inputs X of size n. The function α(n) is called the approximation factor for algorithm A. For any given algorithm, only one of these two inequalities will be important. For maximization problems, where we want to compute a solution whose cost is as small as possible, the first inequality is trivial. For maximization problems, where we want a solution whose value is as large as possible, the second inequality is trivial. A 1-approximation algorithm always returns the exact optimal solution. Especially for problems where exact optimization is NP-hard, we have little hope of completely characterizing the optimal solution. The secret to proving that an algorithm satisfies some approximation ratio is to find a useful function of the input that provides both lower bounds on the cost of the optimal solution and upper bounds on the cost of the approximate solution. For example, if OP T (X ) ≥ f (X )/2 and A(X ) ≤ 5 f (X ), for any function f , then A is a 10-approximation algorithm. Finding the right intermediate function can be a delicate balancing act. K.3 Greedy Vertex Cover Recall that the vertex color problem asks, given a graph G , for the smallest set of vertices of G that cover every edge. This is one of the first NP-hard problems introduced in the first week of class. There is a natural and efficient greedy heuristic1 for computing a small vertex cover: mark the vertex with the largest degree, remove all the edges incident to that vertex, and recurse. GREEDYVERTEXCOVER(G ): C ←∅ while G has at least one edge v ← vertex in G with maximum degree G←G\v C ←C∪v return C 1 A heuristic is an algorithm that doesn’t work. 3 Algorithms Non-Lecture K: Approximation Algorithms Obviously this algorithm doesn’t compute the optimal vertex cover—that would imply P=NP!—but it does compute a reasonably close approximation. Theorem 3. GREEDYVERTEXCOVER is an O(log n)-approximation algorithm. Proof: For all i , let Gi denote the graph G after i iterations of the main loop, and let di denote the maximum degree of any node in Gi −1 . We can define these variables more directly by adding a few extra lines to our algorithm: GREEDYVERTEXCOVER(G ): C ←∅ G0 ← G i←0 while Gi has at least one edge i ← i+1 vi ← vertex in Gi −1 with maximum degree di ← degGi−1 ( vi ) Gi ← Gi −1 \ vi C ← C ∪ vi return C Let |Gi −1 | denote the number of edges in the graph Gi −1 . Let C ∗ denote the optimal vertex cover of G , which consists of OP T vertices. Since C ∗ is also a vertex cover for Gi −1 , we have v ∈C ∗ degGi−1 ( v ) ≥ |Gi −1 |. In other words, the average degree in Gi of any node in C ∗ is at least |Gi −1 |/OP T . It follows that Gi −1 has at least one node with degree at least |Gi −1 |/OP T . Since di is the maximum degree of any node in Gi −1 , we have | G i −1 | di ≥ OP T Moreover, for any j ≥ i − 1, the subgraph G j has no more edges than Gi −1 , so di ≥ |G j |/OP T . This observation implies that OP T OP T di ≥ i =1 i =1 | G i −1 | OP T OP T ≥ i =1 |GOP T | OP T OP T = |GOP T | = |G | − di . i =1 In other words, the first OP T iterations of GREEDYVERTEXCOVER remove at least half the edges of G . Thus, after at most OP T lg|G | ≤ 2 OP T lg n iterations, all the edges of G have been removed, and the algorithm terminates. We conclude that GREEDYVERTEXCOVER computes a vertex cover of size O(OP T log n). So far we’ve only proved an upper bound on the approximation factor of GREEDYVERTEXCOVER; perhaps a more careful analysis would imply that the approximation factor is only O(log log n), or even O(1). Alas, no such improvement is possible. For any integer n, a simple recursive construction gives us an n-vertex graph for which the greedy algorithm returns a vertex cover of size Ω(OP T · log n). Details are left as an exercise for the reader. 4 Algorithms K.4 Non-Lecture K: Approximation Algorithms Set Cover and Hitting Set The greedy algorithm for vertex cover can be applied almost immediately to two more general problems: set cover and hitting set. The input for both of these problems is a set system (X , F), where X is a finite ground set, and F is a family of subsets of X .2 A set cover of a set system (X , F) is a subfamily of sets in F whose union is the entire ground set X . A hitting set for (X , F) is a subset of the ground set X that intersects every set in F. An undirected graph can be cast as a set system in two different ways. In one formulation, the ground set X contains the vertices, and each edge defines a set of two vertices in F. In this formulation, a vertex cover is a hitting set. In the other formulation, the edges are the ground set, the vertices define the family of subsets, and a vertex cover is a set cover. Here are the natural greedy algorithms for finding a small set cover and finding a small hitting set. GREEDYSETCOVER finds a set cover whose size is at most O(log |F|) times the size of smallest set cover. GREEDYHITTINGSET finds a hitting set whose size is at most O(log |X |) times the size of the smallest hitting set. GREEDYSETCOVER(X , F): C←∅ while X = ∅ S ← arg max |S ∩ X | GREEDYHITTINGSET(X , F): H ←∅ while F = ∅ x ← arg max |{S ∈ F | x ∈ S }| S ∈F x ∈X F ← F \ {S ∈ F | x ∈ S } H ← H ∪ {x} return H X ← X \S C ← C ∪ {S } return C The similarity between these two algorithms is no coincidence. For any set system (X , F), there is a dual set system (F, X ∗ ) defined as follows. For any element x ∈ X in the ground set, let x ∗ denote the subfamily of sets in F that contain x : x ∗ = {S ∈ F | x ∈ S } . Finally, let X ∗ denote the collection of all subsets of the form x ∗ : X ∗ = x∗ x ∈S . As an example, suppose X is the set of letters of alphabet and F is the set of last names of student taking CS 573 this semester. Then X ∗ has 26 elements, each containing the subset of CS 573 students whose last name contains a particular letter of the alphabet. For example, m∗ is the set of students whose last names contain the letter m. There is a direct one-to-one correspondence between the ground set X and the dual set family X ∗ . It is a tedious but instructive exercise to prove that the dual of the dual of any set system is isomorphic to the original set system—(X ∗ , F∗ ) is essentially the same as (X , F). It is also easy to prove that a set cover for any set system (X , F) is also a hitting set for the dual set system (F, X ∗ ), and therefore a hitting set for any set system (X , F) is isomorphic to a set cover for the dual set system (F, X ∗ ). K.5 Vertex Cover, Again The greedy approach doesn’t always lead to the best approximation algorithms. Consider the following alternate heuristic for vertex cover: 2 A matroid (see the lecture note on greedy algorithms) is a special type of set system. 5 Algorithms Non-Lecture K: Approximation Algorithms DUMBVERTEXCOVER(G ): C ←∅ while G has at least one edge (u, v ) ← any edge in G G ← G \ {u, v } C ← C ∪ {u, v } return C The minimum vertex cover—in fact, every vertex cover—contains at least one of the two vertices u and v chosen inside the while loop. It follows immediately that DUMBVERTEXCOVER is a 2-approximation algorithm! The same idea can be extended to approximate the minimum hitting set for any set system (X , F), where the approximation factor is the size of the largest set in F. K.6 Traveling Salesman: The Bad News The traveling salesman problem3 problem asks for the shortest Hamiltonian cycle in a weighted undirected graph. To keep the problem simple, we can assume without loss of generality that the underlying graph is always the complete graph Kn for some integer n; thus, the input to the traveling salesman problem is n just a list of the 2 edge lengths. Not surprisingly, given its similarity to the Hamiltonian cycle problem, it’s quite easy to prove that the traveling salesman problem is NP-hard. Let G be an arbitrary undirected graph with n vertices. We can construct a length function for Kn as follows: (e) = 1 if e is an edge in G , 2 otherwise. Now it should be obvious that if G has a Hamiltonian cycle, then there is a Hamiltonian cycle in Kn whose length is exactly n; otherwise every Hamiltonian cycle in Kn has length at least n + 1. We can clearly compute the lengths in polynomial time, so we have a polynomial time reduction from Hamiltonian cycle to traveling salesman. Thus, the traveling salesman problem is NP-hard, even if all the edge lengths are 1 and 2. There’s nothing special about the values 1 and 2 in this reduction; we can replace them with any values we like. By choosing values that are sufficiently far apart, we can show that even approximating the shortest traveling salesman tour is NP-hard. For example, suppose we set the length of the ‘absent’ edges to n + 1 instead of 2. Then the shortest traveling salesman tour in the resulting weighted graph either has length exactly n (if G has a Hamiltonian cycle) or has length at least 2n (if G does not have a Hamiltonian cycle). Thus, if we could approximate the shortest traveling salesman tour within a factor of 2 in polynomial time, we would have a polynomial-time algorithm for the Hamiltonian cycle problem. Pushing this idea to its limits us the following negative result. Theorem 4. For any function f (n) that can be computed in time polynomial in n, there is no polynomialtime f (n)-approximation algorithm for the traveling salesman problem on general weighted graphs, unless P=NP. 3 This is sometimes bowdlerized into the traveling salesperson problem. That’s just silly. Who ever heard of a traveling salesperson sleeping with the farmer’s child? 6 Algorithms K.7 Non-Lecture K: Approximation Algorithms Traveling Salesman: The Good News Even though the general traveling salesman problem can’t be approximated, a common special case can be approximated fairly easily. The special case requires the edge lengths to satisfy the so-called triangle inequality: (u, w ) ≤ (u, v ) + ( v, w ) for any vertices u, v, w. This inequality is satisfied for geometric graphs, where the vertices are points in the plane (or some higher-dimensional space), edges are straight line segments, and lengths are measured in the usual Euclidean metric. Notice that the length functions we used above to show that the general TSP is hard to approximate do not (always) satisfy the triangle inequality. With the triangle inequality in place, we can quickly compute a 2-approximation for the traveling salesman tour as follows. First, we compute the minimum spanning tree T of the weighted input graph; this can be done in O(n2 log n) time (where n is the number of vertices of the graph) using any of several classical algorithms. Second, we perform a depth-first traversal of T , numbering the vertices in the order that we first encounter them. Because T is a spanning tree, every vertex is numbered. Finally, we return the cycle obtained by visiting the vertices according to this numbering. 6 7 6 5 7 1 2 1 4 3 5 2 3 4 A minimum spanning tree T , a depth-first traversal of T , and the resulting approximate traveling salesman tour. Let OP T denote the cost of the optimal TSP tour, let M S T denote the total length of the minimum spanning tree, and let A be the length of the tour computed by our approximation algorithm. Consider the ‘tour’ obtained by walking through the minimum spanning tree in depth-first order. Since this tour traverses every edge in the tree exactly twice, its length is 2 · M S T . The final tour can be obtained from this one by removing duplicate vertices, moving directly from each node to the next unvisited node.; the triangle inequality implies that taking these shortcuts cannot make the tour longer. Thus, A ≤ 2 · M S T . On the other hand, if we remove any edge from the optimal tour, we obtain a spanning tree (in fact a spanning path) of the graph; thus, M S T ≥ OP T . We conclude that A ≤ 2 · OP T ; our algorithm computes a 2-approximation of the optimal tour. We can improve this approximation factor using the following algorithm discovered by Nicos Christofides in 1976. As in the previous algorithm, we start by constructing the minimum spanning tree T . Then let O be the set of vertices with odd degree in T ; it is an easy exercise (hint, hint) to show that the number of vertices in O is even. In the next stage of the algorithm, we compute a minimum-cost perfect matching M of these odddegree vertices. A prefect matching is a collection of edges, where each edge has both endpoints in O and each vertex in O is adjacent to exactly one edge; we want the perfect matching of minimum total length. Later in the semester, we will see an algorithm to compute M in polynomial time. Now consider the multigraph T ∪ M ; any edge in both T and M appears twice in this multigraph. This graph is connected, and every vertex has even degree. Thus, it contains an Eulerian circuit: a closed walk that uses every edge exactly once. We can compute such a walk in O(n) time with a simple modification of depth-first search. To obtain the final approximate TSP tour, we number the vertices in 7 Algorithms Non-Lecture K: Approximation Algorithms the order they first appear in some Eulerian circuit of T ∪ M , and return the cycle obtained by visiting the vertices according to that numbering. 7 6 7 5 6 1 2 5 1 4 2 3 4 3 A minimum spanning tree T , a minimum-cost perfect matching M of the odd vertices in T , an Eulerian circuit of T ∪ M , and the resulting approximate traveling salesman tour. Theorem 5. Given a weighted graph that obeys the triangle inequality, the Christofides heuristic computes a (3/2)-approximation of the minimum traveling salesman tour. Proof: Let A denote the length of the tour computed by the Christofides heuristic; let OP T denote the length of the optimal tour; let M S T denote the total length of the minimum spanning tree; let M OM denote the total length of the minimum odd-vertex matching. The graph T ∪ M , and therefore any Euler tour of T ∪ M , has total length M S T + M OM . By the triangle inequality, taking a shortcut past a previously visited vertex can only shorten the tour. Thus, A ≤ M S T + M OM . By the triangle inequality, the optimal tour of the odd-degree vertices of T cannot be longer than OP T . Any cycle passing through of the odd vertices can be partitioned into two perfect matchings, by alternately coloring the edges of the cycle red and green. One of these two matchings has length at most OP T /2. On the other hand, both matchings have length at least M OM . Thus, M OM ≤ OP T /2. Finally, recall our earlier observation that M S T ≤ OP T . Putting these three inequalities together, we conclude that A ≤ 3 · OP T /2, as claimed. K.8 k -center Clustering The k-center clustering problem is defined as follows. We are given a set P = { p1 , p2 , . . . , pn } of n points in the plane4 and an integer k. Our goal to find a collection of k circles that collectively enclose all the input points, such that the radius of the largest circle is as large as possible. More formally, we want to compute a set C = {c1 , c2 , . . . , ck } of k center points, such that the following cost function is minimized: cost(C ) := max min | pi c j |. i j Here, | pi c j | denotes the Euclidean distance between input point pi and center point c j . Intuitively, each input point is assigned to its closest center point; the points assigned to a given center c j comprise a cluster. The distance from c j to the farthest point in its cluster is the radius of that cluster; the cluster is contained in a circle of this radius centered at c j . The k-center clustering cost cost(C ) is precisely the maximum cluster radius. This problem turns out to be NP-hard, even to approximate within a factor of roughly 1.8. However, there is a natural greedy strategy, first analyzed in 1985 by Teofilo Gonzalez5 , that is guaranteed to 4 The k-center problem can be defined over any metric space, and the approximation analysis in this section holds in any metric space as well. The analysis in the next section, however, does require that the points come from the Euclidean plane. 5 Teofilo F. Gonzalez. Clustering to minimize the maximum inter-cluster distance. Theoretical Computer Science 38:293-306, 1985. 8 Algorithms Non-Lecture K: Approximation Algorithms produce a clustering whose cost is at most twice optimal. Choose the k center points one at a time, starting with an arbitrary input point as the first center. In each iteration, choose the input point that is farthest from any earlier center point to be the next center point. The first five iterations of Gonzalez’s k-center clustering algorithm. In the pseudocode below, di denotes the current distance from point pi to its nearest center, and r j denotes the maximum of all di (or in other words, the cluster radius) after the first j centers have been chosen. The algorithm includes an extra iteration to computer the final clustering cost rk . GONZALEZKCENTER( P, k): for i ← 1 to n di ← ∞ c1 ← p1 for j ← 1 to k rj ← 0 for i ← 1 to n di ← min{di , | pi c j |} if r j < di r j ← d i ; c j +1 ← p i return {c1 , c2 , . . . , ck } GONZALEZKCENTER clearly runs in O(nk) time. Using more advanced data structures, Tomas Feder and Daniel Greene6 described an algorithm to compute exactly the same clustering in only O(n log k) time. Theorem 6. GONZALEZKCENTER computes a 2-approximation to the optimal k-center clustering. Proof: Let r ∗ be the optimal k-center clustering radius for some point set P , and let r be the clustering radius computed by GONZALEZKCENTER. Suppose for purposes of proving a contradiction that r > 2 r ∗ . Then among the first k + 1 center points selected by GONZALEZKCENTER, every pair has distance at least r ≥ 2 r ∗ . Thus, by the triangle inequality, any ball of radius r ∗ contains at most one of these k + 1 center points. But this implies that at least k + 1 balls of radius r ∗ are required to cover P , which contradicts the definition of r ∗ . K.9 Approximation Schemes With just a little more work, we can compute an arbitrarily close approximation of the optimal kclustering, using a so-called approximation scheme. An approximation scheme accepts both an instance of the problem and a value > 0 as input, and it computes a (1 + )-approximation of the optimal 6 Tomas Feder* and Daniel H. Greene. Optimal algorithms for approximate clustering. Proc. 20th STOC, 1988. Unlike Gonzalez’s algorithm, Feder and Greene’s faster algorithm does not work over arbitrary metric spaces; it requires that the input points come from some IRd and that distances are measured in some L p metric. The time analysis also assumes that the distance between any two points can be computed in O(1) time. 9 Algorithms Non-Lecture K: Approximation Algorithms output for that instance. As I mentioned earlier, computing even a 1.8-approximation is NP-hard, so we cannot expect our approximation scheme to run in polynomial time; nevertheless, at least for small values of k, the approximation scheme will be considerably more efficient than any exact algorithm. Our approximation scheme works in three phases: 1. Compute a 2-approximate clustering of the input set P using GONZALEZKCENTER. Let r be the cost of this clustering. 2. Create a regular grid of squares of width δ = r /2 2. Let Q be a subset of P containing one point from each non-empty cell of this grid. 3. Compute an optimal set of k centers for Q. Return these k centers as the approximate k-center clustering for P . The first phase requires O(nk) time. By our earlier analysis, we have r ∗ ≤ r ≤ 2 r ∗ , where r ∗ is the optimal k-center clustering cost for P . The second phase can be implemented in O(n) time using a hash table, or in O(n log n) time by standard sorting, by associating approximate coordinates ( x /δ , y /δ ) to each point ( x , y ) ∈ P and removing duplicates. The key observation is that the resulting point set Q is significantly smaller than P . We know P can be covered by k balls of radius r ∗ , each of which touches O( r ∗ /δ2 ) = O(1/ 2 ) grid cells. It follows that |Q| = O(k/ 2 ). Let T (n, k) be the running time of an exact k-center clustering algorithm, given n points as input. If this were a computational geometry class, we might see a “brute force” algorithm that runs in time T (n, k) = O(nk+2 ); the fastest algorithm currently known7 runs in time T (n, k) = nO( k) . If we use this algorithm, our third phase requires (k/ 2 )O( k) time. It remains to show that the optimal clustering for Q implies a (1 + )-approximation of the optimal clustering for P . Suppose the optimal clustering of Q consists of k balls B1 , B2 , . . . , Bk , each of radius ˜. Clearly ˜ ≤ r ∗ , since any set of k balls that cover P also cover any subset of P . Each point in P \ Q r r shares a grid cell with some point in Q, and therefore is within distance δ 2 of some point in Q. Thus, if we increase the radius of each ball Bi by δ 2, the expanded balls must contain every point in P . We conclude that the optimal centers for Q gives us a k-center clustering for P of cost at most r ∗ + δ 2 ≤ r ∗ + r /2 ≤ r ∗ + r ∗ = (1 + ) r ∗ . The total running time of the approximation scheme is O(nk + (k/ 2 )O( k) ). This is still exponential in the input size if k is large (say n or n/100), but if k and are fixed constants, the running time is linear in the number of input points. K.10 An FPTAS for Subset Sum An approximation scheme whose running time, for any fixed , is polynomial in n is called a polynomialtime approximation scheme or PTAS (usually pronounced “pee taz"). If in addition the running time depends only polynomially on , the algorithm is called a fully polynomial-time approximation scheme or FPTAS (usually pronounced “eff pee taz"). For example, an approximation scheme with running time 6 O(n2 / 2 ) is an FPTAS; an approximation scheme with running time O(n1/ ) is a PTAS but not an FPTAS; and our approximation scheme for k-center clustering is not a PTAS. The last problem we’ll consider is the SUBSETSUM problem: Given a set X containing n positive integers and a target integer t , determine whether X has a subset whose elements sum to t . The lecture notes on NP-completeness include a proof that SUBSETSUM is NP-hard. As stated, this problem doesn’t 7 R. Z. Hwang, R. C. T. Lee, and R. C. Chan. The slab dividing approach to solve the Euclidean p-center problem. Algorithmica 9(1):1–22, 1993. 10 Algorithms Non-Lecture K: Approximation Algorithms allow any sort of approximation—the answer is either TRUE or FALSE.8 So we will consider a related optimization problem instead: Given set X and integer t , find the subset of X whose sum is as large as possible but no larger than t . We have already seen a dynamic programming algorithm to solve the decision version SUBSETSUM in time O(nt ); a similar algorithm solves the optimization version in the same time bound. Here is a different algorithm, whose running time does not depend on t : SUBSETSUM(X [1 .. n], t ): S0 ← {0} for i ← 1 to n Si ← Si −1 ∪ (Si −1 + X [i ]) remove all elements of Si bigger than t return max Sn Here Si −1 + X [i ] denotes the set {s + X [i ] | s ∈ Si −1 }. If we store each Si in a sorted array, the i th iteration of the for-loop requires time O(|Si −1 |). Each set Si contains all possible subset sums for the first i elements of X ; thus, Si has at most 2i elements. On the other hand, since every element of Si is an integer between 0 and t , we also have |Si | ≤ t + 1. It follows that the total running time of this n algorithm is i =1 O(|Si −1 |) = O(min{2n , nt }). Of course, this is only an estimate of worst-case behavior. If several subsets of X have the same sum, the sets Si will have fewer elements, and the algorithm will be faster. The key idea for finding an approximate solution quickly is to ‘merge’ nearby elements of Si —if two subset sums are nearly equal, ignore one of them. On the one hand, merging similar subset sums will introduce some error into the output, but hopefully not too much. On the other hand, by reducing the size of the set of sums we need to maintain, we will make the algorithm faster, hopefully significantly so. Here is our approximation algorithm. We make only two changes to the exact algorithm: an initial sorting phase and an extra FILTERing step inside the main loop. FILTER( Z [1 .. k], δ): SORT( Z ) j←1 Y [ j ] ← Z [i ] for i ← 2 to k if Z [i ] > (1 + δ) · Y [ j ] j ← j+1 Y [ j ] ← Z [i ] return Y [1 .. j ] APPROXSUBSETSUM(X [1 .. n], k, ): SORT(X ) R0 ← {0} for i ← 1 to n R i ← R i −1 ∪ (R i −1 + X [i ]) Ri ← FILTER(Ri , /2n ) remove all elements of R i bigger than t return max R n Theorem 7. APPROXSUBSETSUM returns a (1 + )-approximation of the optimal subset sum, given any such that 0 < ≤ 1. Proof: The theorem follows from the following claim, which we prove by induction: For any element s ∈ Si , there is an element r ∈ R i such that r ≤ s ≤ r · (1 + n/2)i . The claim is trivial for i = 0. Let s be an arbitrary element of Si , for some i > 0. There are two cases to consider: either x ∈ Si −1 , or x ∈ Si −1 + x i . 8 Do, or do not. There is no ‘try’. (Are old one thousand when years you, alphabetical also in order talk will you.) 11 Algorithms Non-Lecture K: Approximation Algorithms (1) Suppose s ∈ Si −1 . By the inductive hypothesis, there is an element r ∈ R i −1 such that r ≤ s ≤ r · (1 + n/2)i −1 . If r ∈ R i , the claim obviously holds. On the other hand, if r ∈ R i , there must be an element r ∈ R i such that r < r ≤ r (1 + n/2), which implies that r < r ≤ s ≤ r · (1 + n/2)i −1 ≤ r · (1 + n/2)i , so the claim holds. (2) Suppose s ∈ Si −1 + x i . By the inductive hypothesis, there is an element r ∈ R i −1 such that r ≤ s − x i ≤ r · (1 + n/2)i −1 . If r + x i ∈ R i , the claim obviously holds. On the other hand, if r + x i ∈ R i , there must be an element r ∈ R i such that r < r + x i ≤ r (1 + n/2), which implies that r < r + x i ≤ s ≤ r · (1 + n/2)i −1 + x i ≤ ( r − x i ) · (1 + n/2)i + x i ≤ r · (1 + n/2)i − x i · ((1 + n/2)i − 1) ≤ r · (1 + n/2)i . so the claim holds. Now let s∗ = max Sn and r ∗ = max R n . Clearly r ∗ ≤ s∗ , since R n ⊆ Sn . Our claim implies that there is some r ∈ R n such that s∗ ≤ r · (1 + /2n)n . But r cannot be bigger than r ∗ , so s∗ ≤ r ∗ · (1 + /2n)n . The inequalities e x ≥ 1 + x for all x , and e x ≤ 2 x + 1 for all 0 ≤ x ≤ 1, imply that (1 + /2n)n ≤ e /2 ≤ 1+ . Theorem 8. APPROXSUBSETSUM runs in O((n3 log n)/ ) time. Proof: Assuming we keep each set R i in a sorted array, we can merge the two sorted arrays R i −1 and R i −1 + x i in O(|R i −1 |) time. FILTERin R i and removing elements larger than t also requires only O(|R i −1 |) time. Thus, the overall running time of our algorithm is O( i |R i |); to express this in terms of n and , we need to prove an upper bound on the size of each set R i . Let δ = /2n. Because we consider the elements of X in increasing order, every element of R i is between 0 and i · x i . In particular, every element of R i −1 + x i is between x i and i · x i . After FILTERing, at most one element r ∈ R i lies in the range (1 + δ)k ≤ r < (1 + δ)k+1 , for any k. Thus, at most log1+δ i elements of R i −1 + x i survive the call to FILTER. It follows that |R i | = |R i −1 | + ≤ |R i −1 | + ≤ |R i −1 | + ≤ |R i −1 | + log i log(1 + δ) log n [ i ≤ n] log(1 + δ) 2 ln n [e x ≤ 1 + 2 x for all 0 ≤ x ≤ 1] δ n ln n [δ = /2n] Unrolling this recurrence into a summation gives us the upper bound |R i | ≤ i · (n ln n)/ = O((n2 log n)/ ). We conclude that the overall running time of APPROXSUBSETSUM is O((n3 log n)/ ), as claimed. 12 Algorithms Non-Lecture K: Approximation Algorithms Exercises 1. (a) Prove that for any set of jobs, the makespan of the greedy assignment is at most (2 − 1/m) times the makespan of the optimal assignment, where m is the number of machines. (b) Describe a set of jobs such that the makespan of the greedy assignment is exactly (2 − 1/m) times the makespan of the optimal assignment, where m is the number of machines. (c) Describe an efficient algorithm to solve the minimum makespan scheduling problem exactly if every processing time T [i ] is a power of two. 2. (a) Find the smallest graph (minimum number of edges) for which GREEDYVERTEXCOVER does not return the smallest vertex cover. (b) For any integer n, describe an n-vertex graph for which GREEDYVERTEXCOVER returns a vertex cover of size OP T · Ω(log n). 3. (a) Find the smallest graph (minimum number of edges) for which DUMBVERTEXCOVER does not return the smallest vertex cover. (b) Describe an infinite family of graphs for which DUMBVERTEXCOVER returns a vertex cover of size 2 · OP T . 4. Consider the following heuristic for constructing a vertex cover of a connected graph G : return the set of non-leaf nodes in any depth-first spanning tree of G . (a) Prove that this heuristic returns a vertex cover of G . (b) Prove that this heuristic returns a 2-approximation to the minimum vertex cover of G . (c) Describe an infinite family of graphs for which this heuristic returns a vertex cover of size 2 · OP T . 5. Consider the following optimization version of the PARTITION problem. Given a set X of positive integers, our task is to partition X into disjoint subsets A and B such that max{ A, B } is as small as possible. This problem is clearly NP-hard. Determine the approximation ratio of the following polynomial-time approximation algorithm. Prove your answer is correct. PARTITION(X [1 .. n]): Sort X in increasing order a ← 0; b ← 0 for i ← 1 to n if a < b a ← a + X [i ] else b ← b + X [i ] return max{a, b} 6. The chromatic number χ (G ) of a graph G is the minimum number of colors required to color the vertices of the graph, so that every edge has endpoints with different colors. Computing the chromatic number exactly is NP-hard. 13 Algorithms Non-Lecture K: Approximation Algorithms Prove that the following problem is also NP-hard: Given an n-vertex graph G , return any integer between χ (G ) and χ (G ) + 573. [Note: This does not contradict the possibility of a constant factor approximation algorithm.] 7. Let G = (V, E ) be an undirected graph, each of whose vertices is colored either red, green, or blue. An edge in G is boring if its endpoints have the same color, and interesting if its endpoints have different colors. The most interesting 3-coloring is the 3-coloring with the maximum number of interesting edges, or equivalently, with the fewest boring edges. Computing the most interesting 3-coloring is NP-hard, because the standard 3-coloring problem is a special case. (a) Let zzz(G ) denote the number of boring edges in the most interesting 3-coloring of a graph G . 100 Prove that it is NP-hard to approximate zzz(G ) within a factor of 1010 . (b) Let wow(G ) denote the number of interesting edges in the most interesting 3-coloring of G . Suppose we assign each vertex in G a random color from the set {red, green, blue}. Prove that the expected number of interesting edges is at least 2 wow(G ). 3 8. Consider the following algorithm for coloring a graph G . TREECOLOR(G ): T ← any spanning tree of G Color the tree T with two colors c←2 for each edge (u, v ) ∈ G \ T T ← T ∪ {(u, v )} if color(u) = color( v ) 〈〈Try recoloring u with an existing color 〉〉 for i ← 1 to c if no neighbor of u in T has color i color(u) ← i if color(u) = color( v ) 〈〈Try recoloring v with an existing color 〉〉 for i ← 1 to c if no neighbor of v in T has color i color( v ) ← i if color(u) = color( v ) c ← c+1 color(u) ← c 〈〈Give up and create a new color 〉〉 (a) Prove that this algorithm colors any bipartite graph with just two colors. (b) Let ∆(G ) denote the maximum degree of any vertex in G . Prove that this algorithm colors any graph G with at most ∆(G ) colors. This trivially implies that TREECOLOR is a ∆(G )approximation algorithm. (c) Prove that TREECOLOR is not a constant-factor approximation algorithm. 9. The KNAPSACK problem can be defined as follows. We are given a finite set of elements X where each element x ∈ X has a non-negative size and a non-negative value, along with an integer capacity c . Our task is to determine the maximum total value among all subsets of X whose total size is at most c . This problem is NP-hard. Specifically, the optimization version of SUBSETSUM is a special case, where each element’s value is equal to its size. 14 Algorithms Non-Lecture K: Approximation Algorithms Determine the approximation ratio of the following polynomial-time approximation algorithm. Prove your answer is correct. APPROXKNAPSACK(X , c ): return max{GREEDYKNAPSACK(X , c ), PICKBESTONE(X , c )} GREEDYKNAPSACK(X , c ): Sort X in decreasing order by the ratio value/size S ← 0; V ← 0 for i ← 1 to n if S + size( x i ) > c return V S ← S + size( x i ) V ← V + value( x i ) return V PICKBESTONE(X , c ): Sort X in increasing order by size V ←0 for i ← 1 to n if size( x i ) > c return V if value( x i ) > V V ← value( x i ) return V 10. In the bin packing problem, we are given a set of n items, each with weight between 0 and 1, and we are asked to load the items into as few bins as possible, such that the total weight in each bin is at most 1. It’s not hard to show that this problem is NP-Hard; this question asks you to analyze a few common approximation algorithms. In each case, the input is an array W [1 .. n] of weights, and the output is the number of bins used. FIRSTFIT(W [1 .. n]): b←0 NEXTFIT(W [1 .. n]): b←0 Total[0] ← ∞ for i ← 1 to n j ← 1; f ound ← FALSE while j ≤ b and f ound = FALSE if Total[ j ] + W [i ] ≤ 1 Total[ j ] ← Total[ j ] + W [i ] f ound ← TRUE j ← j+1 for i ← 1 to n if Total[ b] + W [i ] > 1 b ← b+1 Total[ b] ← W [i ] else Total[ b] ← Total[ b] + W [i ] if f ound = FALSE b ← b+1 Total[ b] = W [i ] return b return b (a) Prove that NEXTFIT uses at most twice the optimal number of bins. (b) Prove that FIRSTFIT uses at most twice the optimal number of bins. (c) Prove that if the weight array W is initially sorted in decreasing order, then FIRSTFIT uses at most (4 · OP T + 1)/3 bins, where OP T is the optimal number of bins. The following facts may be useful (but you need to prove them if your proof uses them): • In the packing computed by FIRSTFIT, every item with weight more than 1/3 is placed in one of the first OP T bins. • FIRSTFIT places at most OP T − 1 items outside the first OP T bins. 11. Given a graph G with edge weights and an integer k, suppose we wish to partition the the vertices of G into k subsets S1 , S2 , . . . , Sk so that the sum of the weights of the edges that cross the partition (that is, have endpoints in different subsets) is as large as possible. 15 Algorithms Non-Lecture K: Approximation Algorithms (a) Describe an efficient (1 − 1/k)-approximation algorithm for this problem. (b) Now suppose we wish to minimize the sum of the weights of edges that do not cross the partition. What approximation ratio does your algorithm from part (a) achieve for the new problem? Justify your answer. 12. The lecture notes describe a (3/2)-approximation algorithm for the metric traveling salesman problem. Here, we consider computing minimum-cost Hamiltonian paths. Our input consists of a graph G whose edges have weights that satisfy the triangle inequality. Depending upon the problem, we are also given zero, one, or two endpoints. (a) If our input includes zero endpoints, describe a (3/2)-approximation to the problem of computing a minimum cost Hamiltonian path. (b) If our input includes one endpoint u, describe a (3/2)-approximation to the problem of computing a minimum cost Hamiltonian path that starts at u. (c) If our input includes two endpoints u and v , describe a (5/3)-approximation to the problem of computing a minimum cost Hamiltonian path that starts at u and ends at v . 13. Suppose we are given a collection of n jobs to execute on a machine containing a row of m processors. When the i th job is executed, it occupies a contiguous set of prox[i ] processors for time[i ] seconds. A schedule for a set of jobs assigns each job an interval of processors and a starting time, so that no processor works on more than one job at any time. The makespan of a schedule is the time from the start to the finish of all jobs. Finally, the parallel scheduling problem asks us to compute the schedule with the smallest possible makespan. (a) Prove that the parallel scheduling problem is NP-hard. (b) Give an algorithm that computes a 3-approximation of the minimum makespan of a set of jobs in O(m log m) time. That is, if the minimum makespan is M , your algorithm should compute a schedule with make-span at most 3 M . You can assume that n is a power of 2. 14. Consider the greedy algorithm for metric TSP: start at an arbitrary vertex u, and at each step, travel to the closest unvisited vertex. (a) Show that the greedy algorithm for metric TSP is an O(log n)-approximation, where n is the number of vertices. [Hint: Argue that the kth least expensive edge in the tour output by the greedy algorithm has weight at most OPT/(n − k + 1); try k = 1 and k = 2 first.] (b) Show that the greedy algorithm for metric TSP is no better than an O(log n)-approximation. That is, describe an infinite family of weighted graphs such that the greedy algorithm returns a cycle whose weight is Ω(log n) times the optimal TSP tour. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 16 Algorithms Appendix: Solving Recurrences “. . . O Zarathustra, who you are and must become” behold you are the teacher of the eternal recurrence – that is your destiny! That you as the first must teach this doctrine – how could this great destiny not be your greatest danger and sickness too? — Friedrich Nietzsche, Also sprach Zarathustra (1885) [translated by Walter Kaufmann] Solving Recurrences 1 Introduction A recurrence is a recursive description of a function, usually of the form F : IN → IR, or a description of such a function in terms of itself. Like all recursive structures, a recurrence consists of one or more base cases and one or more recursive cases. Each of these cases is an equation or inequality, with some function value f (n) on the left side. The base cases give explicit values for a (typically finite, typically small) subset of the possible values of n. The recursive cases relate the function value f (n) to function value f (k) for one or more integers k < n; typically, each recursive case applies to an infinite number of possible values of n. For example, the following recurrence (written in two different but standard ways) describes the identity function f (n) = n: f ( n) = 0 if n = 0 f (0) = 0 f (n − 1) + 1 otherwise f (n) = f (n − 1) + 1 for all n > 0 In both presentations, the first line is the only base case, and the second line is the only recursive case. The same function can satisfy many different recurrences; for example, both of the following recurrences also describe the identity function: 0 if n = 0 if n = 0 0 f ( n) = f ( n) = 1 if n = 1 f ( n/2 ) + f ( n/2 ) otherwise 2 · f ( n/ 2) f ( n − 1) + 1 if n is even and n > 0 if n is odd We say that a particular function satisfies a recurrence, or is the solution to a recurrence, if each of the statements in the recurrence is true. Most recurrences—at least, those that we will encounter in this class—have a solution; moreover, if every case of the recurrence is an equation, that solution is unique. Specifically, if we transform the recursive formula into a recursive algorithm, the solution to the recurrence is the function computed by that algorithm! Recurrences arise naturally in the analysis of algorithms, especially recursive algorithms. In many cases, we can express the running time of an algorithm as a recurrence, where the recursive cases of the recurrence correspond exactly to the recursive cases of the algorithm. Recurrences are also useful tools for solving counting problems—How many objects of a particular kind exist? By itself, a recurrence is not a satisfying description of the running time of an algorithm or a bound on the number of widgets. Instead, we need a closed-form solution to the recurrence; this is a non-recursive description of a function that satisfies the recurrence. For recurrence equations, we sometimes prefer an exact closed-form solution, but such a solution may not exist, or may be too complex to be useful. Thus, for most recurrences, especially those arising in algorithm analysis, we can be satisfied with an asymptotic solution of the form Θ( f (n)), for some explicit (non-recursive) function g (n). 1 Algorithms Appendix: Solving Recurrences For recursive inequalities, we prefer a tight solution; this is a function that would still satisfy the recurrence if all the inequalities were replaced with the corresponding equations. Again, exactly tight solutions may not exist, or may be too complex to be useful, so we may have to settle for a looser solution and/or an asymptotic solution of the form O( g (n)) or Ω( g (n)). 2 The Ultimate Method: Guess and Confirm Ultimately, there is only one fail-safe method to solve any recurrence: Guess the answer, and then prove it correct by induction. Later sections of these notes describe techniques to generate guesses that are guaranteed to be correct, provided you use them correctly. But if you’re faced with a recurrence that doesn’t seem to fit any of these methods, or if you’ve forgotten how those techniques work, don’t despair! If you guess a closed-form solution and then try to verify your guess inductively, usually either the proof will succeed, in which case you’re done, or the proof will fail, in which case the failure will help you refine your guess. Where you get your initial guess is utterly irrelevant1 —from a classmate, from a textbook, on the web, from the answer to a different problem, scrawled on a bathroom wall in Siebel, included in a care package from your mom, dictated by the machine elves, whatever. If you can prove that the answer is correct, then it’s correct! 2.1 Tower of Hanoi The classical Tower of Hanoi problem gives us the recurrence T (n ) = 2 T (n − 1) + 1 with base case T (0) = 0. Just looking at the recurrence we can guess that T (n) is something like 2n . If we write out the first few values of T (n), we discover that they are each one less than a power of two. T (0) = 0, T (1) = 1, T (2) = 3, T (3) = 7, T (4) = 15, T (5) = 31, T (6) = 63, ..., It looks like T (n ) = 2n − 1 might be the right answer. Let’s check. T (0) = 0 = 20 − 1 T ( n) = 2 T ( n − 1) + 1 = 2(2n−1 − 1) + 1 [induction hypothesis] n =2 −1 [algebra] We were right! Hooray, we’re done! Another way we can guess the solution is by unrolling the recurrence, by substituting it into itself: T ( n) = 2 T ( n − 1) + 1 = 2 (2 T (n − 2) + 1) + 1 = 4 T ( n − 2) + 3 = 4 (2 T (n − 3) + 1) + 3 = 8 T ( n − 2) + 7 = ··· 1 . . . except of course during exams, where you aren’t supposed to use any outside sources 2 Algorithms Appendix: Solving Recurrences It looks like unrolling the initial Hanoi recurrence k times, for any non-negative integer k, will give us the new recurrence T (n) = 2k T (n − k) + (2k − 1). Let’s prove this by induction: T (n) = 2 T (n − 1) + 1 [k = 0, by definition] T (n) = 2k−1 T (n − (k − 1)) + (2k−1 − 1) =2 k −1 2 T (n − k) + 1 + (2 k k −1 [inductive hypothesis] − 1) [initial recurrence for T (n − (k − 1))] k = 2 T (n − k) + (2 − 1) [algebra] Our guess was correct! In particular, unrolling the recurrence n times give us the recurrence T (n) = 2n T (0) + (2n − 1). Plugging in the base case T (0) = 0 give us the closed-form solution T (n) = 2n − 1. 2.2 Fibonacci numbers Let’s try a less trivial example: the Fibonacci numbers Fn = Fn −1 + Fn −2 with base cases F0 = 0 and F1 = 1. There is no obvious pattern in the first several values (aside from the recurrence itself), but we can reasonably guess that Fn is exponential in n. Let’s try to prove inductively that Fn ≤ α · c n for some constants a > 0 and c > 1 and see how far we get. Fn = Fn−1 + Fn−2 ≤ α · c n−1 + α · c n−2 [“induction hypothesis”] n ≤ α · c ??? The last inequality is satisfied if c n ≥ c n−1 + c n−2 , or more simply, if c 2 − c − 1 ≥ 0. The smallest value of c that works is φ = (1 + 5)/2 ≈ 1.618034; the other root of the quadratic equation has smaller absolute value, so we can ignore it. So we have most of an inductive proof that Fn ≤ α · φ n for some constant α. All that we’re missing are the base cases, which (we can easily guess) must determine the value of the coefficient a. We quickly compute F0 F1 0 1 = = 0 and = ≈ 0.618034 > 0, 0 1 1 φ φ φ so the base cases of our induction proof are correct as long as α ≥ 1/φ . It follows that Fn ≤ φ n −1 for all n ≥ 0. What about a matching lower bound? Essentially the same inductive proof implies that Fn ≥ β · φ n for some constant β , but the only value of β that works for all n is the trivial β = 0! We could try to find some lower-order term that makes the base case non-trivial, but an easier approach is to recall that asymptotic Ω( ) bounds only have to work for sufficiently large n. So let’s ignore the trivial base case F0 = 0 and assume that F2 = 1 is a base case instead. Some more easy calculation gives us F2 φ2 = 1 φ2 ≈ 0.381966 < 1 φ . Thus, the new base cases of our induction proof are correct as long as β ≤ 1/φ 2 , which implies that Fn ≥ φ n −2 for all n ≥ 1. Putting the upper and lower bounds together, we obtain the tight asymptotic bound Fn = Θ(φ n ). It is possible to get a more exact solution by speculatively refining and conforming our current bounds, but it’s not easy. Fortunately, if we really need it, we can get an exact solution using the annihilator method, which we’ll see later in these notes. 3 Algorithms 2.3 Appendix: Solving Recurrences Mergesort Mergesort is a classical recursive divide-and-conquer algorithm for sorting an array. The algorithm splits the array in half, recursively sorts the two halves, and then merges the two sorted subarrays into the final sorted array. MERGE(A[1 .. n], m): i ← 1; j ← m + 1 for k ← 1 to n if j > n B [k] ← A[i ]; else if i > m B [k] ← A[ j ]; else if A[i ] < A[ j ] B [k] ← A[i ]; else B [k] ← A[ j ]; MERGESORT(A[1 .. n]): if (n > 1) m ← n/ 2 MERGESORT(A[1 .. m]) MERGESORT(A[m + 1 .. n]) MERGE(A[1 .. n], m) i ← i+1 j ← j+1 i ← i+1 j ← j+1 for k ← 1 to n A[k] ← B [k] Let T (n) denote the worst-case running time of MERGESORT when the input array has size n. The MERGE subroutine clearly runs in Θ(n) time, so the function T (n) satisfies the following recurrence: T ( n) = Θ(1) T if n = 1, n/ 2 +T n/ 2 + Θ(n) otherwise. For now, let’s consider the special case where n is a power of 2; this assumption allows us to take the floors and ceilings out of the recurrence. (We’ll see how to deal with the floors and ceilings later; the short version is that they don’t matter.) Because the recurrence itself is given only asymptotically—in terms of Θ( ) expressions—we can’t hope for anything but an asymptotic solution. So we can safely simplify the recurrence further by removing the Θ’s; any asymptotic solution to the simplified recurrence will also satisfy the original recurrence. (This simplification is actually important for another reason; if we kept the asymptotic expressions, we might be tempted to simplify them inappropriately.) Our simplified recurrence now looks like this: T ( n) = 1 if n = 1, 2 T (n/2) + n otherwise. To guess at a solution, let’s try unrolling the recurrence. T (n) = 2 T (n/2) + n = 2 2 T (n/4) + n/2 + n = 4 T ( n/ 4) + 2 n = 8 T ( n/ 8) + 3 n = · · · It looks like T (n) satisfies the recurrence T (n) = 2k T (n/2k ) + kn for any positive integer k. Let’s verify this by induction. 4 Algorithms Appendix: Solving Recurrences T (n) = 2 T (n/2) + n = 21 T (n/21 ) + 1 · n [k = 1, given recurrence] T ( n) = 2 k − 1 T ( n/ 2 k −1 ) + ( k − 1) n =2 k −1 k 2 T ( n/ 2 ) + n/ 2 k −1 [inductive hypothesis] + ( k − 1) n [substitution] = 2k T (n/2k ) + kn [algebra] Our guess was right! The recurrence becomes trivial when n/2k = 1, or equivalently, when k = log2 n: T (n) = nT (1) + n log2 n = n log2 n + n. Finally, we have to put back the Θ’s we stripped off; our final closed-form solution is T (n ) = Θ(n log n ). 2.4 An uglier divide-and-conquer example Consider the divide-and-conquer recurrence T (n ) = n · T ( n ) + n . This doesn’t fit into the form required by the Master Theorem (which we’ll see below), but it still sort of resembles the Mergesort recurrence—the total size of the subproblems at the first level of recursion is n—so let’s guess that T (n) = O(n log n), and then try to prove that our guess is correct. (We could also attack this recurrence by unrolling, but let’s see how far just guessing will take us.) Let’s start by trying to prove an upper bound T (n) ≤ a n lg n for all sufficiently large n and some constant a to be determined later: T ( n) = ≤ n · T ( n) + n n·a n lg n+n [induction hypothesis] = (a/2)n lg n + n [algebra] ≤ an lg n [algebra] The last inequality assumes only that 1 ≤ (a/2) log n,or equivalently, that n ≥ 22/a . In other words, the induction proof is correct if n is sufficiently large. So we were right! But before you break out the champagne, what about the multiplicative constant a? The proof worked for any constant a, no matter how small. This strongly suggests that our upper bound T (n) = O(n log n) is not tight. Indeed, if we try to prove a matching lower bound T (n) ≥ b n log n for sufficiently large n, we run into trouble. T ( n) = ≥ n · T ( n) + n n· b n log n+n [induction hypothesis] = ( b/2)n log n + n ≥ bn log n The last inequality would be correct only if 1 > ( b/2) log n, but that inequality is false for large values of n, no matter which constant b we choose. Okay, so Θ(n log n) is too big. How about Θ(n)? The lower bound is easy to prove directly: T ( n) = n · T ( n) + n ≥ n But an inductive proof of the upper bound fails. T ( n) = ≤ n · T ( n) + n n·a n+n [induction hypothesis] = ( a + 1) n [algebra] ≤ an 5 Algorithms Appendix: Solving Recurrences Hmmm. So what’s bigger than n and smaller than n lg n? How about n T ( n) = n · T ( n) + n ≤ n·a lg n n+n = ( a / 2) n [induction hypothesis] lg n + n ≤ an lg n? for large enough n lg n [algebra] Okay, the upper bound checks out; how about the lower bound? T ( n) = n · T ( n) + n ≥ n· b n = ( b / 2) n ≥ bn lg n+n [induction hypothesis] lg n + n [algebra] lg n No, the last step doesn’t work. So Θ(n lg n) doesn’t work. Okay. . . what else is between n and n lg n? How about n lg lg n? T ( n) = n · T ( n) + n ≤ n·a n+n n lg lg [induction hypothesis] = a n lg lg n − a n + n ≤ a n lg lg n [algebra] if a ≥ 1 Hey look at that! For once, our upper bound proof requires a constraint on the hidden constant a. This is an good indication that we’ve found the right answer. Let’s try the lower bound: T ( n) = n · T ( n) + n ≥ n· b n+n n lg lg [induction hypothesis] = b n lg lg n − b n + n ≥ b n lg lg n [algebra] if b ≤ 1 Hey, it worked! We have most of an inductive proof that T (n) ≤ an lg lg n for any a ≥ 1 and most of an inductive proof that T (n) ≥ bn lg lg n for any b ≤ 1. Technically, we’re still missing the base cases in both proofs, but we can be fairly confident at this point that T (n ) = Θ(n log log n ). 3 Divide and Conquer Recurrences (Recursion Trees) Many divide and conquer algorithms give us running-time recurrences of the form T ( n) = a T ( n/ b ) + f ( n) (1) where a and b are constants and f (n) is some other function. There is a simple and general technique for solving many recurrences in this and similar forms, using a recursion tree. The root of the recursion tree is a box containing the value f (n); the root has a children, each of which is the root of a (recursively defined) recursion tree for the function T (n/ b). Equivalently, a recursion tree is a complete a-ary tree where each node at depth i contains the value f (n/ b i ). The recursion stops when we get to the base case(s) of the recurrence. Because we’re only looking for asymptotic bounds, the exact base case doesn’t matter; we can safely assume that T (1) = Θ(1), or even that T (n) = Θ(1) for all n ≤ 10100 . I’ll also assume for simplicity that n is an integral power of b; we’ll see how to avoid this assumption later (but to summarize: it doesn’t matter). 6 Algorithms Appendix: Solving Recurrences Now T (n) is just the sum of all values stored in the recursion tree. For each i , the i th level of the tree contains a i nodes, each with value f (n/ b i ). Thus, L a i f ( n/ b i ) T ( n) = (Σ) i =0 where L is the depth of the recursion tree. We easily see that L = log b n , because n/ b L = 1. The base case f (1) = Θ(1) implies that the last non-zero term in the summation is Θ(a L ) = Θ(alog b n ) = Θ(nlog b a ). For most divide-and-conquer recurrences, the level-by-level sum (Σ) is a geometric series—each term is a constant factor larger or smaller than the previous term. In this case, only the largest term in the geometric series matters; all of the other terms are swallowed up by the Θ(·) notation. f(n) a f(n/b) f(n) + f(n/b) a f(n/b) a f(n/b²) f(n/b²) ²) f(n/b f(n/b²) a⋅f(n/b) f(n/b) a a f(n/b²) f(n/b²) f(n/b²) ²) f(n/b²) ²) f(n/b f(n/b f(n/b²) f(n/b²) + a²⋅ f(n/b²) f(n/b²) f(n/b²) ²) f(n/b f(n/b²) + + f(n/b ) ) L f(n/b) ) L f(n/b) ) L f(n/b) ) L f(n/b) ) L f(n/b) ) L f(n/b) ) L f(n/b) ) L f(n/b L)f(n/b L)f(n/b L)f(n/b L)f(n/b L)f(n/b L)f(n/b L)f(n/b L) ) f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b L) f(n/b L L L f(n/bL L L f(n/bL L L f(n/bL L L f(n/bL L L f(n/bL L L f(n/bL L L f(n/b L L f(n/b )f(n/b )f(n/b )f(n/b )f(n/b )f(n/b )f(n/b )f(n/b ) ) f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b ) )f(n/b L) f(n/b L f(n/b L f(n/b L f(n/b L f(n/b L f(n/b L f(n/b L f(n/b f(n/b f(n/b f(n/b f(n/b f(n/b f(n/b f(n/b f(n/b LL LL LL LL LL LL LL LL aL⋅f(n/bL) A recursion tree for the recurrence T (n) = a T (n/ b) + f (n) Here are several examples of the recursion-tree technique in action: • Mergesort (simplified): T (n ) = 2 T (n /2) + n There are 2i nodes at level i , each with value n/2i , so every term in the level-by-level sum (Σ) is the same: L T (n) = n. i =0 The recursion tree has L = log2 n levels, so T (n ) = Θ(n log n ). • Randomized selection: T (n ) = T (3n /4) + n The recursion tree is a single path. The node at depth i has value (3/4)i n, so the level-by-level sum (Σ) is a decreasing geometric series: L (3/4)i n. T ( n) = i =0 This geometric series is dominated by its initial term n, so T (n ) = Θ(n ). The recursion tree has L = log4/3 n levels, but so what? 7 Algorithms Appendix: Solving Recurrences • Karatsuba’s multiplication algorithm: T (n ) = 3 T (n /2) + n There are 3i nodes at depth i , each with value n/2i , so the level-by-level sum (Σ) is an increasing geometric series: L (3/2)i n. T ( n) = i =0 This geometric series is dominated by its final term (3/2) L n. Each leaf contributes 1 to this term; thus, the final term is equal to the number of leaves in the tree! The recursion tree has L = log2 n levels, and therefore 3log2 n = nlog2 3 leaves, so T (n ) = Θ(n log2 3 ). • T (n ) = 2 T (n /2) + n / lg n The sum of all the nodes in the i th level is n/(lg n − i ). This implies that the depth of the tree is at most lg n − 1. The level sums are neither constant nor a geometric series, so we just have to evaluate the overall sum directly. Recall (or if you’re seeing this for the first time: Behold!) that the nth harmonic number H n is the sum of the reciprocals of the first n positive integers: n 1 H n := i =1 i It’s nat hard to show that H n = Θ(log n); in fact, we have the stronger inequalities ln(n + 1) ≤ H n ≤ ln n + 1. lg n−1 T ( n) = i =0 n lg n − i lg n n = j =1 j = nHlg n = Θ(n lg lg n ) • T (n ) = 4 T (n /2) + n lg n There are 4i nodes at each level i , each with value (n/2i ) lg(n/2i ) = (n/2i )(lg n − i ); again, the depth of the tree is at most lg n − 1. We have the following summation: lg n−1 n2i (lg n − i ) T ( n) = i =0 We can simplify this sum by substituting j = lg n − i : lg n lg n n2 j /2 j = Θ( j 2 ) n2lg n− j j = T ( n) = j =i j =i ∞ The last step uses the fact that i =1 j /2 j = 2. Although this is not quite a geometric series, it is still dominated by its largest term. • Ugly divide and conquer: T (n ) = n · T ( n) + n We solved this recurrence earlier by guessing the right answer and verifying, but we can use recursion trees to get the correct answer directly. The degree of the nodes in the recursion tree is 8 Algorithms Appendix: Solving Recurrences no longer constant, so we have to be a bit more careful, but the same basic technique still applies. −L It’s not hard to see that the nodes in any level sum to n. The depth L satisfies the identity n2 = 2 (we can’t get all the way down to 1 by taking square roots), so L = lg lg n and T (n ) = Θ(n lg lg n ). • Randomized quicksort: T (n ) = T (3n /4) + T (n /4) + n This recurrence isn’t in the standard form described earlier, but we can still solve it using recursion trees. Now modes in the same level of the recursion tree have different values, and different leaves are at different levels. However, the nodes in any complete level (that is, above any of the leaves) sum to n. Moreover, every leaf in the recursion tree has depth between log4 n and log4/3 n. To derive an upper bound, we overestimate T (n) by ignoring the base cases and extending the tree downward to the level of the deepest leaf. Similarly, to derive a lower bound, we overestimate T (n) by counting only nodes in the tree up to the level of the shallowest leaf. These observations give us the upper and lower bounds n log4 n ≤ T (n) ≤ n log4/3 n. Since these bounds differ by only a constant factor, we have T (n ) = Θ(n log n ). • Deterministic selection: T (n ) = T (n /5) + T (7n /10) + n Again, we have a lopsided recursion tree. If we look only at complete levels of the tree, we find that the level sums form a descending geometric series T (n) = n + 9n/10 + 81n/100 + · · · . We can get an upper bound by ignoring the base cases entirely and growing the tree out to infinity, and we can get a lower bound by only counting nodes in complete levels. Either way, the geometric series is dominated by its largest term, so T (n ) = Θ(n ). 3 1 • Randomized search trees: T (n ) = 4 T (n /4) + 4 T (3n /4) + 1 This looks like a divide-and-conquer recurrence, but what does it mean to have a quarter of a child? The right approach is to imagine that each node in the recursion tree has a weight in addition to its value. Alternately, we get a standard recursion tree again if we add a second real parameter to the recurrence, defining T (n) = T (n, 1), where T (n, α) = T (n/4, α/4) + T (3n/4, 3α/4) + α. In each complete level of the tree, the (weighted) node values sum to exactly 1. The leaves of the recursion tree are at different levels, but all between log4 n and log4/3 n. So we have upper and lower bounds log4 n ≤ T (n) ≤ log4/3 n, which differ by only a constant factor, so T (n ) = Θ(log n ). • Ham-sandwich trees: T (n ) = T (n /2) + T (n /4) + 1 Again, we have a lopsided recursion tree. If we only look at complete levels, we find that the level sums form an ascending geometric series T (n) = 1 + 2 + 4 + · · · , so the solution is dominated by the number of leaves. The recursion tree has log4 n complete levels, so there are more than 2log4 n = nlog4 2 = n; on the other hand, every leaf has depth at most log2 n, so the total number of leaves is at most 2log2 n = n. Unfortunately, the crude bounds n T (n ) n are the best we can derive using the techniques we know so far! The following theorem completely describes the solution for any divide-and-conquer recurrence in the ‘standard form’ T (n) = aT (n/ b) + f (n), where a and b are constants and f (n) is a polynomial. This theorem allows us to bypass recursion trees for ‘standard’ recurrences, but many people (including Jeff) find it harder to remember than the more general recursion-tree technique. Your mileage may vary. 9 Algorithms Appendix: Solving Recurrences The Master Theorem. The recurrence T (n) = aT (n/ b) + f (n) can be solved as follows. • If a f (n/ b) = κ f (n) for some constant κ < 1, then T (n) = Θ( f (n)). • If a f (n/ b) = K f (n) for some constant K > 1, then T (n) = Θ(nlog b a ). • If a f (n/ b) = f (n), then T (n) = Θ( f (n) log b n). • If none of these three cases apply, you’re on your own. Proof: If f (n) is a constant factor larger than a f ( b/n), then by induction, the sum is a descending geometric series. The sum of any geometric series is a constant times its largest term. In this case, the largest term is the first term f (n). If f (n) is a constant factor smaller than a f ( b/n), then by induction, the sum is an ascending geometric series. The sum of any geometric series is a constant times its largest term. In this case, this is the last term, which by our earlier argument is Θ(nlog b a ). Finally, if a f ( b/n) = f (n), then by induction, each of the L + 1 terms in the sum is equal to f (n). 4 Linear Recurrences (Annihilators) Another common class of recurrences, called linear recurrences, arises in the context of recursive backtracking algorithms and counting problems. These recurrences express each function value f (n) as a linear combination of a small number of nearby values f (n − 1), f (n − 2), f (n − 3), . . . . The Fibonacci recurrence is a typical example: if n = 0 0 F ( n) = 1 if n = 1 F (n − 1) + F (n − 2) otherwise It turns out that the solution to any linear recurrence is a simple combination of polynomial and exponential functions in n. For example, we can verify by induction that the linear recurrence if n = 0 1 T (n) = 0 if n = 1 or n = 2 3 T (n − 1) − 8 T (n − 2) + 4 T (n − 3) otherwise has the closed-form solution T (n ) = (n − 3)2n + 4. First we check the base cases: T (0) = (0 − 3)20 + 4 = 1 T (1) = (1 − 3)21 + 4 = 0 T (2) = (2 − 3)22 + 4 = 0 And now the recursive case: T ( n) = 3 T ( n − 1) − 8 T ( n − 2) + 4 T ( n − 3) = 3((n − 4)2n−1 + 4) − 8((n − 5)2n−2 + 4) + 4((n − 6)2n−3 + 4) 384 12 40 24 = −+ n · 2n − − + 2n + (2 − 8 + 4) · 4 248 2 4 8 = ( n − 3) · 2 n + 4 10 Algorithms Appendix: Solving Recurrences But how could we have possibly come up with that solution? In this section, I’ll describe a general method for solving linear recurrences that’s arguably easier than the induction proof! 4.1 Operators Our technique for solving linear recurrences relies on the theory of operators. Operators are higher-order functions, which take one or more functions as input and produce different functions as output. For example, your first two semesters of calculus focus almost exclusively on the differential and integral operators ddx and d x . All the operators we will need are combinations of three elementary building blocks: • Sum: ( f + g )(n) := f (n) + g (n) • Scale: (α · f )(n) := α · ( f (n)) • Shift: (E f )(n) := f (n + 1) The shift and scale operators are linear, which means they can be distributed over sums; for example, for any functions f , g , and h, we have E ( f − 3( g − h)) = E f + (−3)E g + 3E h. We can combine these building blocks to obtain more complex compound operators. For example, the compound operator E − 2 is defined by setting (E − 2) f := E f + (−2) f for any function f . We can also apply the shift operator twice: (E (E f ))(n) = f (n + 2); we write usually E 2 f as a synonym for E (E f ). More generally, for any positive integer k, the operator E k shifts its argument k times: E k f (n) = f (n + k). Similarly, (E − 2)2 is shorthand for the operator (E − 2)(E − 2), which applies (E − 2) twice. For example, here are the results of applying different operators to the function f (n) = 2n : 2 f (n) = 2 · 2n = 2n+1 3 f ( n) = 3 · 2 n E f ( n) = 2 n +1 E 2 f ( n) = 2 n +2 ( E − 2) f ( n) = E f ( n) − 2 f ( n) = 2 n +1 − 2 n +1 = 0 (E 2 − 1) f (n) = E 2 f (n) − f (n) = 2n+2 − 2n = 3 · 2n These compound operators can be manipulated exactly as though they were polynomials over the ‘variable’ E . In particular, we can ‘factor’ compound operators into ‘products’ of simpler operators, and the order of the factors is unimportant. For example, the compound operators E 2 − 3E + 2 and (E − 1)(E − 2) are equivalent: Let Then g (n) := (E − 2) f (n) = f (n + 1) − 2 f (n). (E − 1)(E − 2) f (n) = (E − 1) g (n) = g ( n + 1) − g ( n) = ( f (n + 2) − 2 f (n − 1)) − ( f (n + 1) − 2 f (n)) = f (n + 2) − 3 f (n + 1) + 2 f (n) = (E 2 − 3E + 2) f (n). It is an easy exercise to confirm that E 2 − 3E + 2 is also equivalent to the operator (E − 2)(E − 1). 11 Algorithms Appendix: Solving Recurrences The following table summarizes everything we need to remember about operators. Operator addition subtraction multiplication shift k-fold shift composition distribution 4.2 Definition ( f + g )(n) := f (n) + g (n) ( f − g )(n) := f (n) − g (n) (α · f )(n) := α · ( f (n)) E f ( n ) := f ( n + 1 ) E k f (n) := f (n + k) ( X + Y ) f := X f + Y f ( X − Y ) f := X f − Y f X Y f := X ( Y f ) = Y ( X f ) X ( f + g) = X f + X g Annihilators An annihilator of a function f is any nontrivial operator that transforms f into the zero function. (We can trivially annihilate any function by multiplying it by zero, so as a technical matter, we do not consider the zero operator to be an annihilator.) Every compound operator we consider annihilates a specific class of functions; conversely, every function composed of polynomial and exponential functions has a unique (minimal) annihilator. We have already seen that the operator (E − 2) annihilates the function 2n . It’s not hard to see that the operator (E − c ) annihilates the function α · c n , for any constants c and α. More generally, the operator (E − c ) annihilates the function a n if and only if c = a: ( E − c ) a n = E a n − c · a n = a n +1 − c · a n = ( a − c ) a n . Thus, (E − 2) is essentially the only annihilator of the function 2n . What about the function 2n + 3n ? The operator (E − 2) annihilates the function 2n , but leaves the function 3n unchanged. Similarly, (E − 3) annihilates 3n while negating the function 2n . But if we apply both operators, we annihilate both terms: (E − 2)(2n + 3n ) = E (2n + 3n ) − 2(2n + 3n ) = (2n+1 + 3n+1 ) − (2n+1 + 2 · 3n ) = 3n =⇒ (E − 3)(E − 2)(2n + 3n ) = (E − 3)3n = 0 In general, for any integers a = b, the operator (E − a)(E − b) = (E − b)(E − a) = (E 2 − (a + b)E + a b) annihilates any function of the form αa n + β b n , but nothing else. What about the operator (E − a)(E − a) = (E − a)2 ? It turns out that this operator annihilates all functions of the form (αn + β )a n : (E − a)((αn + β )a n ) = (α(n + 1) + β )a n+1 − a(αn + β )a n = αa n+1 =⇒ (E − a)2 ((αn + β )a n ) = (E − a)(αa n+1 ) = 0 More generally, the operator (E − a)d annihilates all functions of the form p(n) · a n , where p(n) is a polynomial of degree at most d − 1. For example, (E − 1)3 annihilates any polynomial of degree at most 2. 12 Algorithms Appendix: Solving Recurrences The following table summarizes everything we need to remember about annihilators. Operator E −1 E−a (E − a)(E − b) (E − a0 )(E − a1 ) · · · (E − ak ) (E − 1)2 (E − a)2 (E − a)2 (E − b) (E − a) Functions annihilated α αa n n αa + β b n [if a = b] k n i =0 α i a i [if ai distinct] αn + β (αn + β )a n (αn + β )a b + γ b n d −1 i i =0 α i n d a [if a = b] n If X annihilates f , then X also annihilates E f . If X annihilates both f and g , then X also annihilates f ± g . If X annihilates f , then X also annihilates α f , for any constant α. If X annihilates f and Y annihilates g , then X Y annihilates f ± g . 4.3 Annihilating Recurrences Given a linear recurrence for a function, it’s easy to extract an annihilator for that function. For many recurrences, we only need to rewrite the recurrence in operator notation. Once we have an annihilator, we can factor it into operators of the form (E − c ); the table on the previous page then gives us a generic solution with some unknown coefficients. If we are given explicit base cases, we can determine the coefficients by examining a few small cases; in general, this involves solving a small system of linear equations. If the base cases are not specified, the generic solution almost always gives us an asymptotic solution. Here is the technique step by step: 1. 2. 3. 4. 5. Write the recurrence in operator form Extract an annihilator for the recurrence Factor the annihilator (if necessary) Extract the generic solution from the annihilator Solve for coefficients using base cases (if known) Here are several examples of the technique in action: • r (n ) = 5 r (n − 1), where r (0) = 3. 1. We can write the recurrence in operator form as follows: r (n) = 5 r (n − 1) =⇒ r (n + 1) − 5 r (n) = 0 =⇒ (E − 5) r (n) = 0. 2. We immediately see that (E − 5) annihilates the function r (n). 3. The annihilator (E − 5) is already factored. 4. Consulting the annihilator table on the previous page, we find the generic solution r (n) = α5n for some constant α. 5. The base case r (0) = 3 implies that α = 3. 13 Algorithms Appendix: Solving Recurrences We conclude that r (n ) = 3 · 5n . We can easily verify this closed-form solution by induction: r (0) = 3 · 50 = 3 [definition] r (n) = 5 r (n − 1) [definition] = 5 · (3 · 5 n−1 ) [induction hypothesis] n =5 ·3 [algebra] • Fibonacci numbers: F (n ) = F (n − 1) + F (n − 2), where F (0) = 0 and F (1) = 1. 1. We can rewrite the recurrence as (E 2 − E − 1) F (n) = 0. 2. The operator E 2 − E − 1 clearly annihilates F (n). ˆ 3. The quadratic formula implies that the annihilator E 2 − E − 1 factors into (E − φ )(E − φ ), ˆ = (1 − 5)/2 = 1 − φ = −1/φ . where φ = (1 + 5)/2 ≈ 1.618034 is the golden ratio and φ ˆˆ ˆ 4. The annihilator implies that F (n ) = αφ n + αφ n for some unknown constants α and α. 5. The base cases give us two equations in two unknowns: ˆ F (0) = 0 = α + α ˆˆ F (1) = 1 = αφ + αφ ˆ Solving this system of equations gives us α = 1/(2φ − 1) = 1/ 5 and α = −1/ 5. We conclude with the following exact closed form for the nth Fibonacci number: F (n ) = ˆ φn − φn 5 = 1 1+ 5 2 5 n − 1 5 1− 5 n 2 With all the square roots in this formula, it’s quite amazing that Fibonacci numbers are integers. However, if we do all the math correctly, all the square roots cancel out when i is an integer. (In fact, this is pretty easy to prove using the binomial theorem.) • Towers of Hanoi: T (n ) = 2 T (n − 1) + 1, where T (0) = 0. This is our first example of a non-homogeneous recurrence, which means the recurrence has one or more non-recursive terms. 1. We can rewrite the recurrence as (E − 2) T (n) = 1. 2. The operator (E − 2) doesn’t quite annihilate the function; it leaves a residue of 1. But we can annihilate the residue by applying the operator (E − 1). Thus, the compound operator (E − 1)(E − 2) annihilates the function. 3. The annihilator is already factored. 4. The annihilator table gives us the generic solution T (n) = α2n + β for some unknown constants α and β . 5. The base cases give us T (0) = 0 = α20 + β and T (1) = 1 = α21 + β . Solving this system of equations, we find that α = 1 and β = −1. We conclude that T (n ) = 2n − 1. 14 Algorithms Appendix: Solving Recurrences For the remaining examples, I won’t explicitly enumerate the steps in the solution. • Height-balanced trees: H (n ) = H (n − 1) + H (n − 2) + 1, where H (−1) = 0 and H (0) = 1. (Yes, we’re starting at −1 instead of 0. So what?) We can rewrite the recurrence as (E 2 − E − 1)H = 1. The residue 1 is annihilated by (E − 1), so the compound operator (E − 1)(E 2 − E − 1) annihilates the recurrence. This operator factors ˆ ˆ into (E − 1)(E − φ )(E − φ ), where φ = (1 + 5)/2 and φ = (1 − 5)/2. Thus, we get the generic n n ˆ solution H (n) = α · φ + β + γ · φ , for some unknown constants α, β , γ that satisfy the following system of equations: ˆ ˆ H (−1) = 0 = αφ −1 + β + γφ −1 = α/φ + β − γ/φ ˆ H (0) = 1 = αφ 0 + β + γφ 0 = α + β + γ ˆ H (1) = 2 = αφ 1 + β + γφ 1 ˆ = αφ + β + γφ Solving this system (using Cramer’s rule or Gaussian elimination), we find that α = ( 5 + 2)/ 5, β = −1, and γ = ( 5 − 2)/ 5. We conclude that H (n ) = 5+2 1+ 5 5 2 n −1+ 5−2 5 1− 5 2 n . • T (n ) = 3 T (n − 1) − 8 T (n − 2) + 4 T (n − 3), where T (0) = 1, T (1) = 0, and T (2) = 0. This was our original example of a linear recurrence. We can rewrite the recurrence as ( E 3 − 3 E 2 + 8 E − 4) T = 0, so we immediately have an annihilator E 3 − 3 E 2 + 8 E − 4. Using high-school algebra, we can factor the annihilator into (E − 2)2 (E − 1), which implies the generic solution T (n) = αn2n + β 2n + γ. The constants α, β , and γ are determined by the base cases: T (0) = 1 = α · 0 · 20 + β 20 + γ = β +γ T (1) = 0 = α · 1 · 21 + β 21 + γ = 2α + 2β + γ T (2) = 0 = α · 2 · 22 + β 22 + γ = 8α + 4β + γ Solving this system of equations, we find that α = 1, β = −3, and γ = 4, so T (n ) = (n − 3)2n + 4. • T (n ) = T (n − 1) + 2 T (n − 2) + 2n − n 2 We can rewrite the recurrence as (E 2 − E − 2) T (n) = E 2 (2n − n2 ). Notice that we had to shift up the non-recursive parts of the recurrence when we expressed it in this form. The operator (E − 2)(E − 1)3 annihilates the residue 2n − n2 , and therefore also annihilates the shifted residue E 2 (2n + n2 ). Thus, the operator (E − 2)(E − 1)3 (E 2 − E − 2) annihilates the entire recurrence. We can factor the quadratic factor into (E − 2)( E + 1), so the annihilator factors into (E − 2)2 (E − 1)3 (E + 1). So the generic solution is T (n ) = αn 2n + β 2n + γn 2 + δn + + η(−1)n . The coefficients α, β , γ, δ, , η satisfy a system of six equations determined by the first six function values T (0) through T (5). For almost2 every set of base cases, we have α = 0, which implies that T (n ) = Θ(n 2n ). For a more detailed explanation of the annihilator method, see George Lueker, Some techniques for solving recurrences, ACM Computing Surveys 12(4):419-436, 1980. 2 In fact, the only possible solutions with α = 0 have the form −2n−1 − n2 /2 − 5n/2 + η(−1)n for some constant η. 15 Algorithms 5 Appendix: Solving Recurrences Transformations Sometimes we encounter recurrences that don’t fit the structures required for recursion trees or annihilators. In many of those cases, we can transform the recurrence into a more familiar form, by defining a new function in terms of the one we want to solve. There are many different kinds of transformations, but these three are probably the most useful: • Domain transformation: Define a new function S (n) = T ( f (n)) with a simpler recurrence, for some simple function f . • Range transformation: Define a new function S (n) = f ( T (n)) with a simpler recurrence, for some simple function f . • Difference transformation: Simplify the recurrence for T (n) by considering the difference T (n) − T (n − 1). Here are some examples of these transformations in action. • Unsimplified Mergesort: T (n ) = T ( n /2 ) + T ( n /2 ) + Θ(n ) When n is a power of 2, we can simplify the mergesort recurrence to T (n) = 2 T (n/2) + Θ(n), which has the solution T (n) = Θ(n log n). Unfortunately, for other values values of n, this simplified recurrence is incorrect. When n is odd, then the recurrence calls for us to sort a fractional number of elements! Worse yet, if n is not a power of 2, we will never reach the base case T (1) = 1. So we really need to solve the original recurrence. We have no hope of getting an exact solution, even if we ignore the Θ( ) in the recurrence; the floors and ceilings will eventually kill us. But we can derive a tight asymptotic solution using a domain transformation—we can rewrite the function T (n) as a nested function S ( f (n)), where f (n) is a simple function and the function S ( ) has an simpler recurrence. First let’s overestimate the time bound, once by pretending that the two subproblem sizes are equal, and again to eliminate the ceiling: T ( n) ≤ 2 T n/ 2 + n ≤ 2 T (n/2 + 1) + n. Now we define a new function S (n) = T (n + α), where α is a unknown constant, chosen so that S (n) satisfies the Master-Theorem-ready recurrence S (n) ≤ 2S (n/2) + O(n). To figure out the correct value of α, we compare two versions of the recurrence for the function T (n + α): S (n) ≤ 2S (n/2) + O(n) =⇒ T (n + α) ≤ 2 T (n/2 + α) + O(n) T ( n) ≤ 2 T ( n/ 2 + 1) + n =⇒ T (n + α) ≤ 2 T ((n + α)/2 + 1) + n + α For these two recurrences to be equal, we need n/2 + α = (n + α)/2 + 1, which implies that α = 2. The Master Theorem now tells us that S (n) = O(n log n), so T (n) = S (n − 2) = O((n − 2) log(n − 2)) = O(n log n). A similar argument implies the matching lower bound T (n) = Ω(n log n). So T (n ) = Θ(n log n ) after all, just as though we had ignored the floors and ceilings from the beginning! Domain transformations are useful for removing floors, ceilings, and lower order terms from the arguments of any recurrence that otherwise looks like it ought to fit either the Master Theorem or the recursion tree method. But now that we know this, we don’t need to bother grinding through the actual gory details! 16 Algorithms Appendix: Solving Recurrences • Ham-Sandwich Trees: T (n ) = T (n /2) + T (n /4) + 1 As we saw earlier, the recursion tree method only gives us the uselessly loose bounds n T ( n) n for this recurrence, and the recurrence is in the wrong form for annihilators. The authors who discovered ham-sandwich trees (yes, this is a real data structure) solved this recurrence by guessing the solution and giving a complicated induction proof. But a simple transformation allows us to solve the recurrence in just a few lines. We define a new function t (k) = T (2k ), which satisfies the simpler linear recurrence t (k) = t (k − 1) + t (k − 2) + 1. This recurrence should immediately remind you of Fibonacci numbers. Sure enough, the annihilator method implies the solution t (k) = Θ(φ k ), where φ = (1 + 5)/2 is the golden ratio. We conclude that T (n) = t (lg n) = Θ(φ lg n ) = Θ(n lg φ ) ≈ Θ(n0.69424 ). Many other divide-and-conquer recurrences can be similarly transformed into linear recurrences and then solved with annihilators. Consider once more the simplified mergesort recurrence T (n) = 2 T (n/2) + n. The function t (k) = T (2k ) satisfies the recurrence t (k) = 2 t (k − 1) + 2k . The annihilator method gives us the generic solution t (k) = Θ(k · 2k ), which implies that T (n) = t (lg n) = Θ(n log n), just as we expected. On the other hand, for some recurrences like T (n) = T (n/3) + T (2n/3) + n, the recursion tree method gives an easy solution, but there’s no way to transform the recurrence into a form where we can apply the annihilator method directly.3 • Random Binary Search Trees: T (n ) = 1 3 T (3n /4) + 1 4 4 This looks like a divide-and-conquer recurrence, so we might be tempted to apply recursion trees, but what does it mean to have a quarter of a child? If we’re not comfortable with weighted recursion trees, we can instead consider a new function U (n) = n · T (n), which satisfies the recurrence U (n) = U (n/4) + U (3n/4) + n. As we’ve already seen, recursion trees imply that U (n) = Θ(n log n), which immediately implies that T (n ) = Θ(log n ). • Randomized Quicksort: T (n ) = 2 n −1 n T (n /4) + T (k ) + n k =0 This is our first example of a full history recurrence; each function value T (n) is defined in terms of all previous function values T (k) with k < n. Before we can apply any of our existing techniques, we need to convert this recurrence into an equivalent limited history form by shifting and subtracting away common terms. To make this step slightly easier, we first multiply both sides of the recurrence by n to get rid of the fractions. However, we can still get a solution via functional transformations as follows. The function t (k) = T ((3/2)k ) satisfies the recurrence t (n) = t (n − 1) + t (n − λ) + (3/2)k , where λ = log3/2 3 = 2.709511 . . . . The characteristic function for this recurrence is ( r λ − r λ−1 − 1)( r − 3/2), which has a double root at r = 3/2 and nowhere else. Thus, t (k) = Θ(k(3/2)k ), which implies that T (n) = t (log3/2 n) = Θ(n log n). 3 17 Algorithms Appendix: Solving Recurrences n−1 T ( j ) + n2 n · T ( n) = 2 [multiply both sides by n] k =0 n−2 T ( j ) + (n − 1)2 ( n − 1) · T ( n − 1) = 2 [shift] k =0 nT (n) − (n − 1) T (n − 1) = 2 T (n − 1) + 2n − 1 n+1 1 T ( n) = T (n − 1) + 2 − n n [subtract] [simplify] We can solve this limited-history recurrence using another functional transformation. We define a new function t (n) = T (n)/(n + 1), which satisfies the simpler recurrence t ( n) = t ( n − 1) + 2 n+1 − 1 n( n + 1) , which we can easily unroll into a summation. If we only want an asymptotic solution, we can simplify the final recurrence to t (n) = t (n − 1) + Θ(1/n), which unrolls into a very familiar summation: n t ( n) = Θ(1/i ) = Θ(H n ) = Θ(log n). i =1 Finally, substituting T (n) = (n + 1) t (n) gives us a solution to the original recurrence: T (n ) = Θ(n log n ). 18 Algorithms Appendix: Solving Recurrences Exercises 1. For each of the following recurrences, first guess an exact closed-form solution, and then prove your guess is correct. You are free to use any method you want to make your guess—unrolling the recurrence, writing out the first several values, induction proof template, recursion trees, annihilators, transformations, ‘It looks like that other one’, whatever—but please describe your method. All functions are from the non-negative integers to the reals. If it simplifies your solutions, n express them in terms of Fibonacci numbers Fn , harmonic numbers H n , binomial coefficients k , factorials n!, and/or the floor and ceiling functions x and x . (a) A(n) = A(n − 1) + 1, where A(0) = 0. (b) B (n) = 0 if n < 5 B ( n − 5) + 2 otherwise (c) C (n) = C (n − 1) + 2n − 1, where C (0) = 0. (d) D(n) = D(n − 1) + n 2 n , where D(0) = 0. (e) E (n) = E (n − 1) + 2 , where E (0) = 0. (f) F (n) = 3 · F (n − 1), where F (0) = 1. (g) G (n) = G (n−1) , G (n−2) where G (0) = 1 and G (1) = 2. [Hint: This is easier than it looks.] (h) H (n) = H (n − 1) + 1/n, where H (0) = 0. (i) I (n) = I (n − 2) + 3/n, where I (0) = I (1) = 0. [Hint: Consider even and odd n separately.] (j) J (n) = J (n − 1)2 , where J (0) = 2. (k) K (n) = K ( n/2 ) + 1, where K (0) = 0. (l) L (n) = L (n − 1) + L (n − 2), where L (0) = 2 and L (1) = 1. [Hint: Write the solution in terms of Fibonacci numbers.] (m) M (n) = M (n − 1) · M (n − 2), where M (0) = 2 and M (1) = 1. [Hint: Write the solution in terms of Fibonacci numbers.] (n) N (n) = 1 + (p) P (n) = (q) Q(n) = n (N (k − 1) + N (n − k)), where N (0) = 1. k =1 n−1 k =0 (k · P (k − 1)), where P (0) = 1. 1 , 2−Q(n−1) where Q(0) = 0. (r) R(n) = max {R(k − 1) + R(n − k) + n} 1≤k≤n (s) S (n) = max {S (k − 1) + S (n − k) + 1} 1≤k≤n (t) T (n) = min { T (k − 1) + T (n − k) + n} 1≤k≤n (u) U (n) = min {U (k − 1) + U (n − k) + 1} 1≤k≤n (v) V (n) = max n/3≤k≤2n/3 { V ( k − 1) + V ( n − k ) + n} 19 Algorithms Appendix: Solving Recurrences 2. Use recursion trees to solve each of the following recurrences. (a) A(n) = 2A(n/4) + n (b) B (n) = 2B (n/4) + n (c) C (n) = 2C (n/4) + n2 (d) D(n) = 3 D(n/3) + n (e) E (n) = 3 E (n/3) + n (f) F (n) = 3 F (n/3) + n2 (g) G (n) = 4G (n/2) + n (h) H (n) = 4H (n/2) + n (i) I (n) = 4 I (n/2) + n2 (j) J (n) = J (n/2) + J (n/3) + J (n/6) + n (k) K (n) = K (n/2) + K (n/3) + K (n/6) + n2 (l) L (n) = L (n/15) + L (n/10) + 2 L (n/6) + (m) M (n) = 2 n M ( 2 n) + (n) N (n) = 2 n N ( 2 n) + n (p) P (n) = n 2 n P ( 2 n) + n2 n (q) Q(n) = Q(n − 3) + 8n — Don’t use annihilators! (r) R(n) = 2R(n − 2) + 4n — Don’t use annihilators! (s) S (n) = 4S (n − 1) + 2n — Don’t use annihilators! 3. Make up a bunch of linear recurrences and then solve them using annihilators. 4. Solve the following recurrences, using any tricks at your disposal. lg n T ( n/ 2 i ) + n (a) T (n) = [Hint: Assume n is a power of 2.] i =1 (b) More to come. . . 20 Algorithms Non-Lecture L: Fibonacci Heaps A little and a little, collected together, become a great deal; the heap in the barn consists of single grains, and drop and drop makes an inundation. — Saadi (1184–1291) The trees that are slow to grow bear the best fruit. — Molière (1622–1673) Promote yourself but do not demote another. — Rabbi Israel Salanter (1810–1883) Fall is my favorite season in Los Angeles, watching the birds change color and fall from the trees. — David Letterman L Fibonacci Heaps L.1 Mergeable Heaps A mergeable heap is a data structure that stores a collection of keys1 and supports the following operations. • INSERT: Insert a new key into a heap. This operation can also be used to create a new heap containing just one key. • FINDMIN: Return the smallest key in a heap. • DELETEMIN: Remove the smallest key from a heap. • MERGE: Merge two heaps into one. The new heap contains all the keys that used to be in the old heaps, and the old heaps are (possibly) destroyed. If we never had to use DELETEMIN, mergeable heaps would be completely trivial. Each “heap” just stores to maintain the single record (if any) with the smallest key. INSERTs and MERGEs require only one comparison to decide which record to keep, so they take constant time. FINDMIN obviously takes constant time as well. If we need DELETEMIN, but we don’t care how long it takes, we can still implement mergeable heaps so that INSERTs, MERGEs, and FINDMINs take constant time. We store the records in a circular doubly-linked list, and keep a pointer to the minimum key. Now deleting the minimum key takes Θ(n) time, since we have to scan the linked list to find the new smallest key. In this lecture, I’ll describe a data structure called a Fibonacci heap that supports INSERTs, MERGEs, and FINDMINs in constant time, even in the worst case, and also handles DELETEMIN in O(log n) amortized time. That means that any sequence of n INSERTs, m MERGEs, f FINDMINs, and d DELETEMINs takes O(n + m + f + d log n) time. L.2 Binomial Trees and Fibonacci Heaps A Fibonacci heap is a circular doubly linked list, with a pointer to the minimum key, but the elements of the list are not single keys. Instead, we collect keys together into structures called binomial heaps. Binomial heaps are trees that satisfy the heap property—every node has a smaller key than its children—and have the following special recursive structure. A kth order binomial tree, which I’ll abbreviate Bk , is defined recursively. B0 is a single node. For all k > 0, Bk consists of two copies of Bk−1 that have been linked together, meaning that the root of one Bk−1 has become a new child of the other root. 1 In the earlier lecture on treaps, I called these keys priorities to distinguish them from search keys. 1 Algorithms Non-Lecture L: Fibonacci Heaps B4 B4 B5 Binomial trees of order 0 through 5. Binomial trees have several useful properties, which are easy to prove by induction (hint, hint). • The root of Bk has degree k. • The children of the root of Bk are the roots of B0 , B1 , . . . , Bk−1 . • Bk has height k. • Bk has 2k nodes. • Bk can be obtained from Bk−1 by adding a new child to every node. • Bk has k d nodes at depth d , for all 0 ≤ d ≤ k. • Bk has 2k−h−1 nodes with height h, for all 0 ≤ h < k, and one node (the root) with height k. Although we normally don’t care in this class about the low-level details of data structures, we need to be specific about how Fibonacci heaps are actually implemented, so that we can be sure that certain operations can be performed quickly. Every node in a Fibonacci heap points to four other nodes: its parent, its ‘next’ sibling, its ‘previous’ sibling, and one of its children. The sibling pointers are used to join the roots together into a circular doubly-linked root list. In each binomial tree, the children of each node are also joined into a circular doubly-linked list using the sibling pointers. min min A high-level view and a detailed view of the same Fibonacci heap. Null pointers are omitted for clarity. With this representation, we can add or remove nodes from the root list, merge two root lists together, link one two binomial tree to another, or merge a node’s list of children with the root list, in constant time, and we can visit every node in the root list in constant time per node. Having established that these primitive operations can be performed quickly, we never again need to think about the low-level representation details. 2 Algorithms L.3 Non-Lecture L: Fibonacci Heaps Operations on Fibonacci Heaps The INSERT, MERGE, and FINDMIN algorithms for Fibonacci heaps are exactly like the corresponding algorithms for linked lists. Since we maintain a pointer to the minimum key, FINDMIN is trivial. To insert a new key, we add a single node (which we should think of as a B0 ) to the root list and (if necessary) update the pointer to the minimum key. To merge two Fibonacci heaps, we just merge the two root lists and keep the pointer to the smaller of the two minimum keys. Clearly, all three operations take O(1) time. Deleting the minimum key is a little more complicated. First, we remove the minimum key from the root list and splice its children into the root list. Except for updating the parent pointers, this takes O(1) time. Then we scan through the root list to find the new smallest key and update the parent pointers of the new roots. This scan could take Θ(n) time in the worst case. To bring down the amortized deletion time, we apply a CLEANUP algorithm, which links pairs of equal-size binomial heaps until there is only one binomial heap of any particular size. Let me describe the CLEANUP algorithm in more detail, so we can analyze its running time. The following algorithm maintains a global array B [1 .. lg n ], where B [i ] is a pointer to some previouslyvisited binomial heap of order i , or NULL if there is no such binomial heap. Notice that CLEANUP simultaneously resets the parent pointers of all the new roots and updates the pointer to the minimum key. I’ve split off the part of the algorithm that merges binomial heaps of the same order into a separate subroutine MERGEDUPES. MERGEDUPES( v ): w ← B [deg( v )] while w = NULL B [deg( v )] ← NULL if key( v ) ≤ key(w ) swap v w remove w from the root list link w to v w ← B [deg( v )] B [deg( v )] ← v CLEANUP: newmin ← some node in the root list for i ← 0 to lg n B [i ] ← NULL for all nodes v in the root list parent( v ) ← NULL ( ) if key(newmin) > key( v ) newmin ← v MERGEDUPES( v ) 0 B 1 2 0 3 v 1 2 3 0 B v 1 2 ( ) 3 B v MergeDupes( v ), ensuring that no earlier root has the same degree as v . Notices that MERGEDUPES is careful to merge heaps so that the heap property is maintained—the heap whose root has the larger key becomes a new child of the heap whose root has the smaller key. This is handled by swapping v and w if their keys are in the wrong order. The running time of CLEANUP is O( r ), where r is the length of the root list just before CLEANUP is called. The easiest way to see this is to count the number of times the two starred lines can be executed: line ( ) is executed once for every node v on the root list, and line ( ) is executed at most once for every node w on the root list. Since DELETEMIN does only a constant amount of work before calling CLEANUP, the running time of DELETEMIN is O( r ) = O( r + deg(min)) where r is the number of roots before DELETEMIN begins, and min is the node deleted. 3 Algorithms Non-Lecture L: Fibonacci Heaps Although deg(min) is at most lg n, we can still have r = Θ(n) (for example, if nothing has been deleted yet), so the worst-case time for a DELETEMIN is Θ(n). After a DELETEMIN, the root list has length O(log n), since all the binomial heaps have unique orders and the largest has order at most lg n . L.4 Amortized Analysis of DELETEMIN To bound the amortized cost, observe that each insertion increments r . If we charge a constant ‘cleanup tax’ for each insertion, and use the collected tax to pay for the CLEANUP algorithm, the unpaid cost of a DELETEMIN is only O(deg(min)) = O(log n). More formally, define the potential of the Fibonacci heap to be the number of roots. Recall that the amortized time of an operation can be defined as its actual running time plus the increase in potential, provided the potential is initially zero (it is) and we never have negative potential (we never do). Let r be the number of roots before a DELETEMIN, and let r denote the number of roots afterwards. The actual cost of DELETEMIN is r + deg(min), and the number of roots increases by r − r , so the amortized cost is r + deg(min). Since r = O(log n) and the degree of any node is O(log n), the amortized cost of DELETEMIN is O(log n). Each INSERT adds only one root, so its amortized cost is still constant. A MERGE actually doesn’t change the number of roots, since the new Fibonacci heap has all the roots from its constituents and no others, so its amortized cost is also constant. L.5 Decreasing Keys In some applications of heaps, we also need the ability to delete an arbitrary node. The usual way to do this is to decrease the node’s key to −∞, and then use DELETEMIN. Here I’ll describe how to decrease the key of a node in a Fibonacci heap; the algorithm will take O(log n) time in the worst case, but the amortized time will be only O(1). Our algorithm for decreasing the key at a node v follows two simple rules. 1. Promote v up to the root list. (This moves the whole subtree rooted at v .) 2. As soon as two children of any node w have been promoted, immediately promote w . In order to enforce the second rule, we now mark certain nodes in the Fibonacci heap. Specifically, a node is marked if exactly one of its children has been promoted. If some child of a marked node is promoted, we promote (and unmark) that node as well. Whenever we promote a marked node, we unmark it; this is theonly way to unmark a node. (Specifically, splicing nodes into the root list during a DELETEMIN is not considered a promotion.) Here’s a more formal description of the algorithm. The input is a pointer to a node v and the new value k for its key. PROMOTE( v ): unmark v if parent( v ) = NULL remove v from parent( v )’s list of children insert v into the root list if parent( v ) is marked PROMOTE(parent( v )) else mark parent( v ) DECREASEKEY( v, k): key( v ) ← k update the pointer to the smallest key PROMOTE( v ) 4 Algorithms Non-Lecture L: Fibonacci Heaps The PROMOTE algorithm calls itself recursively, resulting in a ‘cascading promotion’. Each consecutive marked ancestor of v is promoted to the root list and unmarked, otherwise unchanged. The lowest unmarked ancestor is then marked, since one of its children has been promoted. a b f l g m n h c i d j f e k l a b m g p o c h j i n d f e k l a b m g p o h n c i d e k j o p f l p b m g n h a e c j d k i a f l m p o b g n h c e j d k i o Decreasing the keys of four nodes: first f , then d , then j , and finally h. Dark nodes are marked. DecreaseKey(h) causes nodes b and a to be recursively promoted. The time to decrease the key of a node v is O(1 + #consecutive marked ancestors of v ). Binomial heaps have logarithmic depth, so if we still had only full binomial heaps, the running time would be O(log n). Unfortunately, promoting nodes destroys the nice binomial tree structure; our trees no longer have logarithmic depth! In fact, DECREASEKEY runs in Θ(n) time in the worst case. To compute the amortized cost of DECREASEKEY, we’ll use the potential method, just as we did for DELETEMIN. We need to find a potential function Φ that goes up a little whenever we do a little work, and goes down a lot whenever we do a lot of work. DECREASEKEY unmarks several marked ancestors and possibly also marks one node. So the number of marked nodes might be an appropriate potential function here. Whenever we do a little bit of work, the number of marks goes up by at most one; whenever we do a lot of work, the number of marks goes down a lot. More precisely, let m and m be the number of marked nodes before and after a DECREASEKEY operation. The actual time (ignoring constant factors) is t = 1 + #consecutive marked ancestors of v and if we set Φ = m, the increase in potential is m − m ≤ 1 − #consecutive marked ancestors of v . Since t + ∆Φ ≤ 2, the amortized cost of DECREASEKEY is O(1) . L.6 Bounding the Degree But now we have a problem with our earlier analysis of DELETEMIN. The amortized time for a DELETEMIN is still O( r + deg(min)). To show that this equaled O(log n), we used the fact that the maximum degree of any node is O(log n), which implies that after a CLEANUP the number of roots is O(log n). But now that we don’t have complete binomial heaps, this ‘fact’ is no longer obvious! So let’s prove it. For any node v , let | v | denote the number of nodes in the subtree of v , including v itself. Our proof uses the following lemma, which finally tells us why these things are called Fibonacci heaps. Lemma 1. For any node v in a Fibonacci heap, | v | ≥ Fdeg( v )+2 . 5 Algorithms Non-Lecture L: Fibonacci Heaps Proof: Label the children of v in the chronological order in which they were linked to v . Consider the situation just before the i th oldest child w i was linked to v . At that time, v had at least i − 1 children (possibly more). Since CLEANUP only links trees with the same degree, we had deg(w i ) = deg( v ) ≥ i − 1. Since that time, at most one child of w i has been promoted away; otherwise, w i would have been promoted to the root list by now. So currently we have deg(w i ) ≥ i − 2. We also quickly observe that deg(w i ) ≥ 0. (Duh.) Let sd be the minimum possible size of a tree with degree d in any Fibonacci heap. Clearly s0 = 1; for notational convenience, let s−1 = 1 also. By our earlier argument, the i th oldest child of the root has degree at least max{0, i − 2}, and thus has size at least max{1, si −2 } = si −2 . Thus, we have the following recurrence: d sd ≥ 1 + s i −2 i =1 If we assume inductively that si ≥ Fi +2 for all −1 ≤ i < d (with the easy base cases s−1 = F1 and s0 = F2 ), we have d sd ≥ 1 + F i = F d +2 . i =1 (The last step was a practice problem in Homework 0.) By definition, | v | ≥ sdeg( v ) . You can easily show (using either induction or the annihilator method) that Fk+2 > φ k where φ= 1+ 5 2 ≈ 1.618 is the golden ratio. Thus, Lemma 1 implies that deg( v ) ≤ logφ | v | = O(log| v |). Thus, since the size of any subtree in an n-node Fibonacci heap is obviously at most n, the degree of any node is O(log n), which is exactly what we wanted. Our earlier analysis is still good. L.7 Analyzing Everything Together Unfortunately, our analyses of DELETEMIN and DECREASEKEY used two different potential functions. Unless we can find a single potential function that works for both operations, we can’t claim both amortized time bounds simultaneously. So we need to find a potential function Φ that goes up a little during a cheap DELETEMIN or a cheap DECREASEKEY, and goes down a lot during an expensive DELETEMIN or an expensive DECREASEKEY. Let’s look a little more carefully at the cost of each Fibonacci heap operation, and its effect on both the number of roots and the number of marked nodes, the things we used as out earlier potential functions. Let r and m be the numbers of roots and marks before each operation, and let r and m be the numbers of roots and marks after the operation. operation INSERT MERGE DELETEMIN DECREASEKEY actual cost 1 1 r + deg(min) 1+m−m r −r 1 0 r −r 1+m−m m −m 0 0 0 m −m In particular, notice that promoting a node in DECREASEKEY requires constant time and increases the number of roots by one, and that we promote (at most) one unmarked node. 6 Algorithms Non-Lecture L: Fibonacci Heaps If we guess that the correct potential function is a linear combination of our old potential functions r and m and play around with various possibilities for the coefficients, we will eventually stumble across the correct answer: Φ = r + 2m To see that this potential function gives us good amortized bounds for every Fibonacci heap operation, let’s add two more columns to our table. operation INSERT MERGE DELETEMIN DECREASEKEY actual cost 1 1 r + deg(min) 1+m−m r −r 1 0 r −r 1+m−m Φ −Φ 1 0 r −r 1+m −m m −m 0 0 0 m −m amortized cost 2 1 r + deg(min) 2 Since Lemma 1 implies that r + deg(min) = O(log n), we’re finally done! (Whew!) L.8 Fibonacci Trees To give you a little more intuition about how Fibonacci heaps behave, let’s look at a worst-case construction for Lemma 1. Suppose we want to remove as many nodes as possible from a binomial heap of order k, by promoting various nodes to the root list, but without causing any cascading promotions. The most damage we can do is to promote the largest subtree of every node. Call the result a Fibonacci tree of order k + 1, and denote it f k+1 . As a base case, let f1 be the tree with one (unmarked) node, that is, f1 = B0 . The reason for shifting the index should be obvious after a few seconds. Fibonacci trees of order 1 through 6. Light nodes have been promoted away; dark nodes are marked. Recall that the root of a binomial tree Bk has k children, which are roots of B0 , B1 , . . . , Bk−1 . To convert Bk to f k+1 , we promote the root of Bk−1 , and recursively convert each of the other subtrees Bi to f i +1 . The root of the resulting tree f k+1 has degree k − 1, and the children are the roots of smaller Fibonacci trees f1 , f2 , . . . , f k−1 . We can also consider Bk as two copies of Bk−1 linked together. It’s quite easy to show that an order-k Fibonacci tree consists of an order k − 2 Fibonacci tree linked to an order k − 1 Fibonacci tree. (See the picture below.) B6 B5 B4 B3 B2 f7 B6 B 1B 0 B5 f5 B5 f4 f7 f3 f2 f1 f5 f6 Comparing the recursive structures of B6 and f7 . Since f1 and f2 both have exactly one node, the number of nodes in an order-k Fibonacci tree is exactly the kth Fibonacci number! (That’s why we changed in the index.) Like binomial trees, Fibonacci trees have lots of other nice properties that easy to prove by induction (hint, hint): 7 Algorithms Non-Lecture L: Fibonacci Heaps • The root of f k has degree k − 2. • f k can be obtained from f k−1 by adding a new unmarked child to every marked node and then marking all the old unmarked nodes. • f k has height k/2 − 1. • f k has Fk−2 unmarked nodes, Fk−1 marked nodes, and thus Fk nodes altogether. k − d −2 • f k has d −1 unmarked nodes, all 0 ≤ d ≤ k/2 − 1. k − d −2 d marked nodes, and k − d −1 d total nodes at depth d , for • f k has Fk−2h−1 nodes with height h, for all 0 ≤ h ≤ k/2 − 1, and one node (the root) with height k/2 − 1. I stopped covering Fibonacci heaps in my undergraduate algorithms class a few years ago, even though they are a great illustration of data structure design principles and amortized analysis. My main reason for skipping them is that the algorithms are relatively complicated, which both hinders understanding and limits any practical advantages over regular binary heaps. (The popular algorithms textbook CLRS dismisses Fibonacci heaps as “predominantly of theoretical interest” because of programming complexity and large constant factors in its running time.) Nevertheless, students interested in data structures are strongly advised to become familiar with binomial and Fibonacci heaps, since they share key structural properties with other data structures (such as union-find trees and Bentley-Saxe dynamization) and illustrate important data structure techniques (like lazy rebuilding). Also, Fibonacci heaps (and more recent extensions like Chazelle’s soft heaps) are key ingredients in the fastest algorithms known for some problems. 8 Algorithms Non-Lecture M: Number-Theoretic Algorithms And it’s one, two, three, What are we fighting for? Don’t tell me, I don’t give a damn, Next stop is Vietnam; [or: This time we’ll kill Saddam] And it’s five, six, seven, Open up the pearly gates, Well there ain’t no time to wonder why, Whoopee! We’re all going to die. — Country Joe and the Fish “I-Feel-Like-I’m-Fixin’-to-Die Rag" (1967) There are 0 kinds of mathematicians: Those who can count modulo 2 and those who can’t. — Anonymous God created the integers; all the rest is the work of man. — Kronecker M Number Theoretic Algorithms M.1 Greatest Common Divisors Before we get to any actual algorithms, we need some definitions and preliminary results. Unless specifically indicated otherwise, all variables in this lecture are integers. The symbol Z (from the German word “Zahlen", meaning ‘numbers’ or ‘to count’) to denote the set Z of integers. We say that one integer d divides another integer n, or that d is a divisor of n, if the quotient n/d is also an integer. Symbolically, we can write this definition as follows: d | n ⇐⇒ n d = n d In particular, zero is not a divisor of any integer—∞ is not an integer—but every other integer is a divisor of zero. If d and n are positive, then d | n immediately implies that d ≤ n. Any integer n can be written in the form n = qd + r for some non-negative integer 0 ≤ r ≤ |d − 1|. Moreover, the choices for the quotient q and remainder r are unique: q= n d and r = n mod d = n − d n d . Note that the remainder n mod d is always non-negative, even if n < 0 or d < 0 or both.1 If d divides two integers m and n, we say that d is a common divisor of m and n. It’s trivial to prove (by definition crunching) that any common divisor of m and n also divides any integer linear combination of m and n: (d | m) and (d | n) =⇒ d | (am + bn) The greatest common divisor of m and n, written gcd(m, n),2 is the largest integer that divides both m and n. Sometimes this is also called the greater common denominator. The greatest common divisor has another useful characterization as the smallest element of another set. Lemma 1. g cd (m, n) is the smallest positive integer of the form am + bn. 1 The sign rules for the C/C++/Java % operator are just plain stupid. I can’t count the number of times I’ve had to write x = (x+n)%n; instead of x %= n;. Frickin’ idiots. Gah! Do not use the notation (m, n) for greatest common divisor, unless you want Notation Kitty to visit you in the middle of the night and remove your eyes while you sleep. Nice kitty. 2 1 Algorithms Non-Lecture M: Number-Theoretic Algorithms Proof: Let s be the smallest positive integer of the form am + bn. Any common divisor of m and n is also a divisor of s = am + bn. In particular, gcd(m, n) is a divisor of s, which implies that gcd(m, n) ≤ s . To prove the other inequality, let’s show that s | m by calculating m mod s. m mod s = m − s m s m = m − (am + bn) s = m 1−a m s + n −b m s We observe that m mod s is an integer linear combination of m and n. Since m mod s < s, and s is the smallest positive integer linear combination, m mod s cannot be positive. So it must be zero, which implies that s | m, as we claimed. By a symmetric argument, s | n. Thus, s is a common divisor of m and n. A common divisor can’t be greater than the greatest common divisor, so s ≤ gcd(m, n) . These two inequalities imply that s = gcd(m, n), completing the proof. M.2 Euclid’s GCD Algorithm We can compute the greatest common divisor of two given integers by recursively applying two simple observations: gcd(m, n) = gcd(m, n − m) and gcd(n, 0) = n The following algorithm uses the first observation to reduce the input and recurse; the second observation provides the base case. SLOWGCD(m, n): m ← |m|; n ← |n| if m < n swap m ↔ n while n > 0 m← m−n if m < n swap m ↔ n return m The first few lines just ensure that m ≥ n ≥ 0. Each iteration of the main loop decreases one of the numbers by at least 1, so the running time is O(m + n). This bound is tight in the worst case; consider the case n = 1. Unfortunately, this is terrible. The input consists of just log m + log n bits; as a function of the input size, this algorithm runs in exponential time. Let’s think for a moment about what the main loop computes between swaps. We start with two numbers m and n and repeatedly subtract n from m until we can’t any more. This is just a (slow) recipe for computing m mod n! That means we can speed up the algorithm by using mod instead of subtraction. EUCLIDGCD(m, n): m ← |m|; n ← |n| if m < n swap m ↔ n while n > 0 m ← m mod n swap m ↔ n return m 2 () Algorithms Non-Lecture M: Number-Theoretic Algorithms This algorithm swaps m and n at every iteration, because m mod n is always less than n. This is almost universally called Euclid’s algorithm, because the main idea is included in Euclid’s Elements.3 The easiest way to analyze this algorithm is to work backward. First, let’s consider the number of iterations of the main loop, or equivalently, the number times line ( ) is executed. To keep things simple, let’s assume that m > n > 0, so the first three lines are redundant, and the algorithm performs at least one iteration. Recall that the Fibonacci numbers(!) are defined as F0 = 0, F1 = 1, and Fk = Fk−1 + Fk−2 for all k > 1. Lemma 2. If the algorithm performs k iterations, then m ≥ Fk+2 and n ≥ Fk+1 . Proof (by induction on k ): If k = 1, we have the trivial bounds n ≥ 1 = F2 and m ≥ 2 = F3 . Suppose k > 1. The first iteration of the loop replaces (m, n) with (n, m mod n). The algorithm performs k − 1 more iterations, so the inductive hypothesis implies that n ≥ Fk+1 and m mod n ≥ Fk . We’ve assumed that m > n, so m ≥ m + n(1 − m/n ) = n +(m mod n). We conclude that m ≥ Fk+1 + Fk = F k +1 . Theorem 1. EUCLIDGCD(m, n) runs in O(log m) iterations. Proof: Let k be the number of iterations. Lemma 2 implies that m ≥ Fk+2 ≥ φ k+2 / 5 − 1, where φ = (1 + 5)/2 (by the annihilator method). Thus, k ≤ logφ ( 5(m + 1)) − 2 = O(log m). What about the actual running time? Every number used by the algorithm has O(log m) bits. Computing the remainder of one b-bit integer by another using the grade-school long division algorithm requires O( b2 ) time. So crudely, the running time is O( b2 log m) = O(log3 m). More careful analysis reduces the time bound to O(log2 m) . We can make the algorithm even faster by using a fast integer division algorithm (based on FFTs, for example). M.3 Modular Arithmetic and Algebraic Groups Modular arithmetic is familiar to anyone who’s ever wondered how many minutes are left in an exam that ends at 9:15 when the clock says 8:54. When we do arithmetic ‘modulo n’, what we’re really doing is a funny kind of arithmetic on the elements of following set: Z n = {0, 1, 2, . . . , n − 1} Z Modular addition and subtraction satisfies all the axioms that we expect implicitly: • Z n is closed under addition mod n: For any a, b ∈ Z n , their sum a + b mod n is also in Z n Z Z Z 3 However, Euclid’s exposition was a little, erm, informal by current standards, primarily because the Greeks didn’t know about induction. He basically said “Try one iteration. If that doesn’t work, try three iterations." In modern language, Euclid’s algorithm would be written as follows, assuming m ≥ n > 0. ACTUALEUCLIDGCD(m, n): if n | m return n else return n mod (m mod n) This algorithm is obviously incorrect; consider the input m = 3, n = 2. Nevertheless, mathematics and algorithms students have applied ‘Euclidean induction’ to a vast number of problems, only to scratch their heads in dismay when they don’t get any credit. 3 Algorithms Non-Lecture M: Number-Theoretic Algorithms • Addition is associative: (a + b mod n) + c mod n = a + ( b + c mod n) mod n. • Zero is an additive identity element: 0 + a mod n = a + 0 mod n = a mod n. Z Z • Every element a ∈ Z n has an inverse b ∈ Z n such that a + b mod n = 0. Specifically, if a = 0, then b = 0; otherwise, b = n − a. Any set with a binary operator that satisfies the closure, associativity, identity, and inverse axioms is called a group. Since Z n is a group under an ‘addition’ operator, we call it an additive group. Moreover, Z because addition is commutative (a + b mod n = b + a mod n), we can call (Z n , + mod n) is an abelian Z 4 additive group. What about multiplication? Z n is closed under multiplication mod n, multiplication mod n is Z associative (and commutative), and 1 is a multiplicative identity, but some elements do not have multiplicative inverses. Formally, we say that Z n is a ring under addition and multiplication modulo n. Z If n is composite, then the following theorem shows that we can factor the ring Z n into two smaller Z rings. The Chinese Remainder Theorem is named a third-century Chinese mathematician and algorismist Sun Tzu (or Sun Zi).5 The Chinese Remainder Theorem. If p ⊥ q, then Z pq ∼ Z p × Z q . Z =Z Z Okay, okay, before we prove this, let’s define all the notation. The product Z p × Z q is the set of Z Z ordered pairs {(a, b) | a ∈ Z p , b ∈ Z q }, where addition, subtraction, and multiplication are defined as Z Z follows: (a, b) + (c , d ) = (a + c mod p, b + d mod q) (a, b) − (c , d ) = (a − c mod p, b − d mod q) (a, b) · (c , d ) = (ac mod p, bd mod q) It’s not hard to check that Z p × Z q is a ring under these operations, where (0, 0) is the additive identity Z Z and (1, 1) is the multiplicative identity. The funky equal sign ∼ means that these two rings are isomorphic: = there is a bijection between the two sets that is consistent with the arithmetic operations. As an example, the following table describes the bijection between Z 15 and Z 3 × Z 5 : Z Z Z 0 1 2 3 4 00 6 12 3 9 1 10 1 7 13 4 2 5 11 2 8 14 For instance, we have 8 = (2, 3) and 13 = (1, 3), and (2, 3) + (1, 3) = (2 + 1 mod 3, 3 + 3 mod 5) = (0, 1) = 6 = 21 mod 15 = (8 + 13) mod 15. (2, 3) · (1, 3) = (2 · 1 mod 3, 3 · 3 mod 5) = (2, 4) = 14 = 104 mod 15 = (8 · 13) mod 15. Proof: The functions n → (n mod p, n mod q) and (a, b) → aq(q mod p) + bp( p mod q) are inverses of each other, and each map preserves the ring structure. We can extend the Chinese remainder theorem inductively as follows: 4 after the Norwegian mathematical prodigy Niels Henrik Abel, who (among many other things) proved the insolubility of quintic equations at the ripe old age of 22. 5 The author of The Art of War, who had the same name, lived more than 500 years earlier. 4 Algorithms Non-Lecture M: Number-Theoretic Algorithms r i =1 The Real Chinese Remainder Theorem. Suppose n = r Z n ∼ i =1 Z pi = Z p1 × Z p2 × · · · × Z pr . Z= Z Z Z Z pi , where pi ⊥ p j for all i and j . Then Thus, if we want to perform modular arithmetic where the modulus n is very large, we can improve the performance of our algorithms by breaking n into several relatively prime factors, and performing modular arithmetic separately modulo each factor. So we can do modular addition, subtraction, and multiplication; what about division? As I said earlier, not every element of Z n has a multiplicative inverse. The most obvious example is 0, but there Z can be others. For example, 3 has no multiplicative inverse in Z 15 ; there is no integer x such that Z 3 x mod 15 = 1. On the other hand, 0 is the only element of Z 7 without a multiplicative inverse: Z 1 · 1 ≡ 2 · 4 ≡ 3 · 5 ≡ 6 · 6 ≡ 1 (mod 7) These examples suggest (I hope) that x has a multiplicative inverse in Z n if and only if a and x are Z relatively prime. This is easy to prove as follows. If x y mod n = 1, then x y + kn = 1 for some integer k. Thus, 1 is an integer linear combination of x and n, so Lemma 1 implies that gcd( x , n) = 1. On the other hand, if x ⊥ n, then a x + bn = 1 for some integers a and b, which implies that a x mod n = 1. Let’s define the set Z ∗ to be the set of elements if Z n that have multiplicative inverses. Zn Z Z ∗ = { a ∈ Z n | a ⊥ n} Zn Z It is a tedious exercise to show that Z ∗ is an abelian group under multiplication modulo n. As long as Zn we stick to elements of this group, we can reasonably talk about ‘division mod n’. We denote the number of elements in Z ∗ by φ (n); this is called Euler’s totient function. This Zn function is remarkably badly-behaved, but there is a relatively simple formula for φ (n) (not surprisingly) involving prime numbers and division: p−1 φ ( n) = n p|n p I won’t prove this formula, but the following intuition is helpful. If we start with Z n and throw out Z all n/2 multiples of 2, all n/3 multiples of 3, all n/5 multiples of 5, and so on. Whenever we throw out multiples of p, we multiply the size of the set by ( p − 1)/ p. At the end of this process, we’re left with precisely the elements of Z ∗ . This is not a proof! On the one hand, this argument throws out some Zn numbers (like 6) more than once, so our estimate seems too low. On the other hand, there are actually n/ p multiples of p in Z n , so our estimate seems too high. Surprisingly, these two errors exactly cancel Z each other out. M.4 Toward Primality Testing In this last section, we discuss algorithms for detecting whether a number is prime. Large prime numbers are used primarily (but not exclusively) in cryptography algorithms. A positive integer is prime if it has exactly two positive divisors, and composite if it has more than two positive divisors. The integer 1 is neither prime nor composite. Equivalently, an integer n ≥ 2 is prime if n is relatively prime with every positive integer smaller than n. We can rephrase this definition yet again: n is prime if and only if φ (n) = n − 1. The obvious algorithm for testing whether a number is prime is trial division: simply try every possible nontrivial divisor between 2 and n. 5 Algorithms Non-Lecture M: Number-Theoretic Algorithms TRIALDIVPRIME(n) : for d ← 1 to n if n mod d = 0 return COMPOSITE return PRIME Unfortunately, this algorithm is horribly slow. Even if we could do the remainder computation in constant time, the overall running time of this algorithm would be Ω( n), which is exponential in the number of input bits. This might seem completely hopeless, but fortunately most composite numbers are quite easy to detect as composite. Consider, for example, the related problem of deciding whether a given integer n, whether n = me for any integers m > 1 and e > 1. We can solve this problem in polynomial time with the following straightforward algorithm. The subroutine ROOT(n, i ) computes n1/i essentially by binary search. (I’ll leave the analysis as a simple exercise.) ROOT(n, i ): r ←0 for ← (lg n)/i down to 1 if ( r + 2 )i ≤ n r ← r +2 return r EXACTPOWER?(n): for i ← 2 to lg n if (ROOT(n, i ))i = n return TRUE return FALSE To distinguish between arbitrary prime and composite numbers, we need to exploit some results about Z ∗ from group theory and number theory. First, we define the order of an element x ∈ Z ∗ as the Zn Zn smallest positive integer k such that x k ≡ 1 (mod n). For example, in the group Z ∗ = {1, 2, 4, 7, 8, 11, 13, 14}, Z15 the number 2 has order 4, and the number 11 has order 2. For any x ∈ Z ∗ , we can partition the elements Zn ∗ k of Z n into equivalence classes, by declaring a ∼ x b if a ≡ b · x for some integer k. The size of every Z equivalence class is exactly the order of x . Because the equivalence classes must be disjoint, we can conclude that the order of any element divides the size of the group . We can express this observation more succinctly as follows: Euler’s Theorem. aφ (n) ≡ 1 (mod n).6 The most interesting special case of this theorem is when n is prime. Fermat’s Little Theorem. If p is prime, then a p ≡ a (mod p).7 This theorem leads to the following efficient pseudo-primality test. 6 This is not Euler’s only theorem; he had thousands. It’s not even his most famous theorem. His second most famous theorem is the formula v + e − f = 2 relating the vertices, edges and faces of any planar map. His most famous theorem is the magic formula eπi + 1 = 0. Naming something after a mathematician or physicist (as in ‘Euler tour’ or ‘Gaussian elimination’ or ‘Avogadro’s number’) is considered a high compliment. Using a lower case letter (‘abelian group’) is even better; abbreviating (‘volt’, ‘amp’) is better still. The number e was named after Euler. 7 This is not Fermat’s only theorem; he had hundreds, most of them stated without proof. Fermat’s Last Theorem wasn’t the last one he published, but the last one proved. Amazingly, despite his dislike of writing proofs, Fermat was almost always right. In that respect, he was very different from you and me. 6 Algorithms Non-Lecture M: Number-Theoretic Algorithms FERMATPSEUDOPRIME(n) : choose an integer a between 1 and n − 1 if a n mod n = a return COMPOSITE! else return PRIME? In practice, this algorithm is both fast and effective. The (empirical) probability that a random 100-digit composite number will return PRIME? is roughly 10−30 , even if we always choose a = 2. Unfortunately, there are composite numbers that always pass this test, no matter which value of a we use. A Carmichael number is a composite integer n such that a n ≡ a (mod n) for every integer a. Thus, Fermat’s Little Theorem can be used to distinguish between two types of numbers: (primes and Carmichael numbers) and everything else. Carmichael numbers are extremely rare; in fact, it was proved only in the 1990s that there are an infinite number of them. To deal with Carmichael numbers effectively, we need to look more closely at the structure of the group Z ∗ . We say that Z ∗ is cyclic if it contains an element of order φ (n); such an element is called a Zn Zn generator. Successive powers of any generator cycle through every element of the group in some order. For example, the group Z ∗ = {1, 2, 4, 5, 7, 8} is cyclic, with two generators: 2 and 5, but Z ∗ is not Z9 Z15 ∗ cyclic. The following theorem completely characterizes which groups Z n are cyclic. Z The Cycle Theorem. Z ∗ is cyclic if and only if n = 2, 4, p e , or 2 p e for some odd prime p and positive Zn integer e. This theorem has two relatively simple corollaries. The Discrete Log Theorem. Suppose Z ∗ is cyclic and g is a generator. Then g x ≡ g y (mod n) if and Zn only if x ≡ y (mod φ (n)). Proof: Suppose g x ≡ g y (mod n). By definition of ‘generator’, the sequence 〈1, g , g 2 , . . .〉 has period φ (n). Thus, x ≡ y (mod φ (n)). On the other hand, if x ≡ y (mod φ (n)), then x = y + kφ (n) for some integer k, so g x = g y +kφ (n) = g y · ( g φ (n) )k . Euler’s Theorem now implies that ( g φ (n) )k ≡ 1k ≡ 1 (mod n), so g x ≡ g y (mod n). The 1 Theorem. Suppose n = p e for some odd prime p and positive integer e. The only elements x ∈ Z ∗ that satisfy the equation x 2 ≡ 1 (mod n) are x = 1 and x = n − 1. Zn Proof: Obviously 12 ≡ 1 (mod n) and (n − 1)2 = n2 − 2n + 1 ≡ 1 (mod n). Suppose x 2 ≡ 1 (mod n) where n = p e . By the Cycle Theorem, Z ∗ is cyclic. Let g be a generator of Zn Z ∗ , and suppose x = g k . Then we immediately have x 2 = g 2k ≡ g 0 = 1 (mod p e ). The Discrete Log Zn Theorem implies that 2k ≡ 0 (mod φ ( p e )). Because p is and odd prime, we have φ ( p e ) = ( p − 1) p e−1 , which is even. Thus, the equation 2k ≡ 0 (mod φ ( p e )) has just two solutions: k = 0 and k = φ ( p e )/2. By the Cycle Theorem, either x = 1 or x = g φ (n)/2 . Because x = n − 1 is also a solution to the original equation, we must have g φ (n)/2 ≡ n − 1 (mod n). This theorem leads to a different pseudo-primality algorithm: SQRT1PSEUDOPRIME(n): choose a number a between 2 and n − 2 if a2 mod n = 1 return COMPOSITE! else return PRIME? 7 Algorithms Non-Lecture M: Number-Theoretic Algorithms As with the previous pseudo-primality test, there are composite numbers that this algorithm cannot identify as composite: powers of primes, for instance. Fortunately, however, the set of composites that always pass the 1 test is disjoint from the set of numbers that always pass the Fermat test. In particular, Carmichael numbers never have the form p e . M.5 The Miller-Rabin Primality Test The following randomized algorithm, adapted by Michael Rabin from an earlier deterministic algorithm of Gary Miller∗ , combines the Fermat test and the 1 test. The algorithm repeats the same two tests s times, where s is some user-chosen parameter, each time with a random value of a. MILLERRABIN(n): write n − 1 = 2 t u where u is odd for i ← 1 to s a ← RANDOM(2, n − 2) if EUCLIDGCD(a, n) = 1 return COMPOSITE! 〈〈obviously!〉〉 u x 0 ← a mod n for j ← 1 to t x j ← x 2−1 mod n j if x j = 1 and x j −1 = 1 and x j −1 = n − 1 return COMPOSITE! 〈〈by the 1 Theorem〉〉 if x t = 1 return COMPOSITE! 〈〈 x t = a n−1 mod n〉〉 〈〈by Fermat’s Little Theorem〉〉 return PRIME? First let’s consider the running time; for simplicity, we assume that all integer arithmetic is done using the quadratic-time grade school algorithms. We can compute u and t in O(log n) time by scanning the bits in the binary representation of n. Euclid’s algorithm takes O(log2 n) time. Computing au mod n requires O(log u) = O(log n) multiplications, each of which takes O(log2 n) time. Squaring x j takes O(log2 n) time. Overall, the running time for one iteration of the outer loop is O(log3 n + t log2 n) = O(log3 n), because t ≤ lg n. Thus, the total running time of this algorithm is O(s log3 n) . If we set s = O(log n), this running time is polynomial in the size of the input. Fine, so it’s fast, but is it correct? Like the earlier pseudoprime testing algorithms, a prime input will always cause MILLERRABIN to return PRIME?. Composite numbers, however, may not always return COMPOSITE!; because we choose the number a at random, there is a small probability of error.8 Fortunately, the error probability can be made ridiculously small—in practice, less than the probability that random quantum fluctuations will instantly transform your computer into a kitten—by setting s ≈ 1000. Theorem 2. If n is composite, MILLERRABIN(n) returns COMPOSITE! with probability at least 1 − 2−s . Proof: First, suppose n is not a Carmichael number. Let F be the set of elements of Z ∗ that pass the Zn Fermat test: F = {a ∈ Z ∗ | a n−1 ≡ 1 (mod n)}. Zn 8 If instead, we try all possible values of a, we obtain an exact primality testing algorithm, but it runs in exponential time. Miller’s original deterministic algorithm examined every value of a in a carefully-chosen subset of Z ∗ . If the Extended Riemann Zn Hypothesis holds, this subset has logarithmic size, and Miller’s algorithm runs in polynomial time. The Riemann Hypothesis is a century-old open problem about the distribution of prime numbers. A solution would be at least as significant as proving Fermat’s Last Theorem or P=NP . 8 Algorithms Non-Lecture M: Number-Theoretic Algorithms Because n is not a Carmichael number, F is a proper subset of Z ∗ . Given any two elements a, b ∈ F , Zn ∗ their product a · b mod n in Z n is also an element of F : Z (a · b)n−1 ≡ a n−1 b n−1 ≡ 1 · 1 ≡ 1 (mod n) We also easily observe that 1 is an element of F , and the multiplicative inverse (mod n) of any element of F is also in F . Thus, F is a proper subgroup of Z ∗ , that is, a proper subset that is also a group under Zn the same binary operation. A standard result in group theory states that if F is a subgroup of a finite group G , the number of elements of F divides the number of elements of G . (We used a special case of this result in our proof of Euler’s Theorem.) In our setting, this means that | F | divides φ (n). Since we already know that | F | < φ (n), we must have | F | ≤ φ (n)/2. Thus, at most half the elements of Z ∗ pass Zn the Fermat test. The case of Carmichael numbers is more complicated, but the main idea is the same: at most half the possible values of a pass the 1 test. See CLRS for further details. 9 Algorithms N Non-Lecture N: Convex Hulls Convex Hulls N.1 Definitions We are given a set P of n points in the plane. We want to compute something called the convex hull of P . Intuitively, the convex hull is what you get by driving a nail into the plane at each point and then wrapping a piece of string around the nails. More formally, the convex hull is the smallest convex polygon containing the points: • polygon: A region of the plane bounded by a cycle of line segments, called edges, joined end-to-end in a cycle. Points where two successive edges meet are called vertices. • convex: For any two points p, q inside the polygon, the line segment pq is completely inside the polygon. • smallest: Any convex proper subset of the convex hull excludes at least one point in P . This implies that every vertex of the convex hull is a point in P . We can also define the convex hull as the largest convex polygon whose vertices are all points in P , or the unique convex polygon that contains P and whose vertices are all points in P . Notice that P might have interior points that are not vertices of the convex hull. A set of points and its convex hull. Convex hull vertices are black; interior points are white. Just to make things concrete, we will represent the points in P by their Cartesian coordinates, in two arrays X [1 .. n] and Y [1 .. n]. We will represent the convex hull as a circular linked list of vertices in counterclockwise order. if the i th point is a vertex of the convex hull, ne x t [i ] is index of the next vertex counterclockwise and pr ed [i ] is the index of the next vertex clockwise; otherwise, ne x t [i ] = pr ed [i ] = 0. It doesn’t matter which vertex we choose as the ‘head’ of the list. The decision to list vertices counterclockwise instead of clockwise is arbitrary. To simplify the presentation of the convex hull algorithms, I will assume that the points are in general position, meaning (in this context) that no three points lie on a common line. This is just like assuming that no two elements are equal when we talk about sorting algorithms. If we wanted to really implement these algorithms, we would have to handle colinear triples correctly, or at least consistently. This is fairly easy, but definitely not trivial. N.2 Simple Cases Computing the convex hull of a single point is trivial; we just return that point. Computing the convex hull of two points is also trivial. For three points, we have two different possibilities — either the points are listed in the array in clockwise order or counterclockwise order. Suppose our three points are (a, b), (c , d ), and (e, f ), given in that order, and for the moment, let’s also suppose that the first point is furthest to the left, so a < c 1 Algorithms Non-Lecture N: Convex Hulls ←−−−−−→ and a < f . Then the three points are in counterclockwise order if and only if the line (a, b)(c , d ) is less ←−−−−−→ than the slope of the line (a, b)(e, f ): counterclockwise ⇐⇒ d−b c−a < f −b e−a Since both denominators are positive, we can rewrite this inequality as follows: counterclockwise ⇐⇒ ( f − b)(c − a) > (d − b)(e − a) This final inequality is correct even if (a, b) is not the leftmost point. If the inequality is reversed, then the points are in clockwise order. If the three points are colinear (remember, we’re assuming that never happens), then the two expressions are equal. (e,f) (a,b) (c,d) Three points in counterclockwise order. Another way of thinking about this counterclockwise test is that we’re computing the cross-product of the two vectors (c , d ) − (a, b) and (e, f ) − (a, b), which is defined as a 2 × 2 determinant: counterclockwise ⇐⇒ c−a e−a d−b >0 f −b We can also write it as a 3 × 3 determinant: 1a counterclockwise ⇐⇒ 1 c 1e b d >0 f All three boxed expressions are algebraically identical. This counterclockwise test plays exactly the same role in convex hull algorithms as comparisons play in sorting algorithms. Computing the convex hull of of three points is analogous to sorting two numbers: either they’re in the correct order or in the opposite order. N.3 Jarvis’s Algorithm (Wrapping) Perhaps the simplest algorithm for computing convex hulls simply simulates the process of wrapping a piece of string around the points. This algorithm is usually called Jarvis’s march, but it is also referred to as the gift-wrapping algorithm. Jarvis’s march starts by computing the leftmost point (that is, the point whose x -coordinate is smallest), because we know that the left most point must be a convex hull vertex. Finding clearly takes linear time. 2 Algorithms Non-Lecture N: Convex Hulls l p=l l p p p p l l l p The execution of Jarvis’s March. Then the algorithm does a series of pivoting steps to find each successive convex hull vertex, starting with and continuing until we reach again. The vertex immediately following a point p is the point that appears to be furthest to the right to someone standing at p and looking at the other points. In other words, if q is the vertex following p, and r is any other input point, then the triple p, q, r is in counter-clockwise order. We can find each successive vertex in linear time by performing a series of O(n) counter-clockwise tests. JARVISMARCH(X [1 .. n], Y [1 .. n]): ←1 for i ← 2 to n if X [i ] < X [ ] ←i p← repeat q ← p+1 〈〈Make sure p = q〉〉 for i ← 2 to n if CCW( p, i , q) q←i nex t [ p] ← q; pr ev [q] ← p p←q until p = Since the algorithm spends O(n) time for each convex hull vertex, the worst-case running time is O(n2 ). However, this naïve analysis hides the fact that if the convex hull has very few vertices, Jarvis’s march is extremely fast. A better way to write the running time is O(nh), where h is the number of convex hull vertices. In the worst case, h = n, and we get our old O(n2 ) time bound, but in the best case h = 3, and the algorithm only needs O(n) time. Computational geometers call this an output-sensitive algorithm; the smaller the output, the faster the algorithm. N.4 Divide and Conquer (Splitting) The behavior of Jarvis’s marsh is very much like selection sort: repeatedly find the item that goes in the next slot. In fact, most convex hull algorithms resemble some sorting algorithm. For example, the following convex hull algorithm resembles quicksort. We start by choosing a pivot point p. Partitions the input points into two sets L and R, containing the points to the left of p, including 3 Algorithms Non-Lecture N: Convex Hulls p itself, and the points to the right of p, by comparing x -coordinates. Recursively compute the convex hulls of L and R. Finally, merge the two convex hulls into the final output. The merge step requires a little explanation. We start by connecting the two hulls with a line segment between the rightmost point of the hull of L with the leftmost point of the hull of R. Call these points p and q, respectively. (Yes, it’s the same p.) Actually, let’s add two copies of the segment pq and call them bridges. Since p and q can ‘see’ each other, this creates a sort of dumbbell-shaped polygon, which is convex except possibly at the endpoints off the bridges. p p q p q p p q q p q q Merging the left and right subhulls. We now expand this dumbbell into the correct convex hull as follows. As long as there is a clockwise turn at either endpoint of either bridge, we remove that point from the circular sequence of vertices and connect its two neighbors. As soon as the turns at both endpoints of both bridges are counter-clockwise, we can stop. At that point, the bridges lie on the upper and lower common tangent lines of the two subhulls. These are the two lines that touch both subhulls, such that both subhulls lie below the upper common tangent line and above the lower common tangent line. Merging the two subhulls takes O(n) time in the worst case. Thus, the running time is given by the recurrence T (n) = O(n) + T (k) + T (n − k), just like quicksort, where k the number of points in R. Just like quicksort, if we use a naïve deterministic algorithm to choose the pivot point p, the worst-case running time of this algorithm is O(n2 ). If we choose the pivot point randomly, the expected running time is O(n log n). There are inputs where this algorithm is clearly wasteful (at least, clearly to us). If we’re really unlucky, we’ll spend a long time putting together the subhulls, only to throw almost everything away during the merge step. Thus, this divide-and-conquer algorithm is not output sensitive. A set of points that shouldn’t be divided and conquered. N.5 Graham’s Algorithm (Scanning) Our third convex hull algorithm, called Graham’s scan, first explicitly sorts the points in O(n log n) and then applies a linear-time scanning algorithm to finish building the hull. 4 Algorithms Non-Lecture N: Convex Hulls We start Graham’s scan by finding the leftmost point , just as in Jarvis’s march. Then we sort the points in counterclockwise order around . We can do this in O(n log n) time with any comparison-based sorting algorithm (quicksort, mergesort, heapsort, etc.). To compare two points p and q, we check whether the triple , p, q is oriented clockwise or counterclockwise. Once the points are sorted, we connected them in counterclockwise order, starting and ending at . The result is a simple polygon with n vertices. l A simple polygon formed in the sorting phase of Graham’s scan. To change this polygon into the convex hull, we apply the following ‘three-penny algorithm’. We have three pennies, which will sit on three consecutive vertices p, q, r of the polygon; initially, these are and the two vertices after . We now apply the following two rules over and over until a penny is moved forward onto : • If p, q, r are in counterclockwise order, move the back penny forward to the successor of r . • If p, q, r are in clockwise order, remove q from the polygon, add the edge pr , and move the middle penny backward to the predecessor of p. The ‘three-penny’ scanning phase of Graham’s scan. Whenever a penny moves forward, it moves onto a vertex that hasn’t seen a penny before (except the last time), so the first rule is applied n − 2 times. Whenever a penny moves backwards, a vertex is removed from the polygon, so the second rule is applied exactly n − h times, where h is as usual the number of convex hull vertices. Since each counterclockwise test takes constant time, the scanning phase takes O(n) time altogether. 5 Algorithms N.6 Non-Lecture N: Convex Hulls Chan’s Algorithm (Shattering) The last algorithm I’ll describe is an output-sensitive algorithm that is never slower than either Jarvis’s march or Graham’s scan. The running time of this algorithm, which was discovered by Timothy Chan in 1993, is O(n log h). Chan’s algorithm is a combination of divide-and-conquer and gift-wrapping. First suppose a ‘little birdie’ tells us the value of h; we’ll worry about how to implement the little birdie in a moment. Chan’s algorithm starts by shattering the input points into n/h arbitrary1 subsets, each of size h, and computing the convex hull of each subset using (say) Graham’s scan. This much of the algorithm requires O((n/h) · h log h) = O(n log h) time. Shattering the points and computing subhulls in O(n log h) time. Once we have the n/h subhulls, we follow the general outline of Jarvis’s march, ‘wrapping a string around’ the n/h subhulls. Starting with p = , where is the leftmost input point, we successively find the convex hull vertex the follows p and counterclockwise order until we return back to again. The vertex that follows p is the point that appears to be furthest to the right to someone standing at p. This means that the successor of p must lie on a right tangent line between p and one of the subhulls—a line from p through a vertex of the subhull, such that the subhull lies completely on the right side of the line from p’s point of view. We can find the right tangent line between p and any subhull in O(log h) time using a variant of binary search. (This is a practice problem in the homework!) Since there are n/h subhulls, finding the successor of p takes O((n/h) log h) time altogether. Since there are h convex hull edges, and we find each edge in O((n/h) log h) time, the overall running time of the algorithm is O(n log h). Wrapping the subhulls in O(n log h) time. Unfortunately, this algorithm only takes O(n log h) time if a little birdie has told us the value of h in advance. So how do we implement the ‘little birdie’? Chan’s trick is to guess the correct value of h; let’s denote the guess by h∗ . Then we shatter the points into n/h∗ subsets of size h∗ , compute their subhulls, and then find the first h∗ edges of the global hull. If h < h∗ , this algorithm computes the complete convex hull in O(n log h∗ ) time. Otherwise, the hull doesn’t wrap all the way back around to , so we know our guess h∗ is too small. Chan’s algorithm starts with the optimistic guess h∗ = 3. If we finish an iteration of the algorithm i and find that h∗ is too small, we square h∗ and try again. Thus, in the i th iteration, we have h∗ = 32 . In 1 In the figures, in order to keep things as clear as possible, I’ve chosen these subsets so that their convex hulls are disjoint. This is not true in general! 6 Algorithms Non-Lecture N: Convex Hulls the final iteration, h∗ < h2 , so the last iteration takes O(n log h∗ ) = O(n log h2 ) = O(n log h) time. The total running time of Chan’s algorithm is given by the sum k k i O(n log 32 ) = O(n) · i =1 2i i =1 for some integer k. Since any geometric series adds up to a constant times its largest term, the total running time is a constant times the time taken by the last iteration, which is O(n log h). So Chan’s algorithm runs in O(n log h) time overall, even without the little birdie. Exercises 1. The convex layers of a point set X are defined by repeatedly computing the convex hull of X and removing its vertices from X , until X is empty. (a) Describe an algorithm to compute the convex layers of a given set of n points in the plane in O(n2 ) time. (b) Describe an algorithm to compute the convex layers of a given set of n points in the plane in O(n log n) time. The convex layers of a set of points. 2. Let X be a set of points in the plane. A point p in X is Pareto-optimal if no other point in X is both above and to the right of p. The Pareto-optimal points can be connected by horizontal and vertical lines into the staircase of X , with a Pareto-optimal point at the top right corner of every step. See the figure on the next page. (a) QUICKSTEP: Describe a divide-and-conquer algorithm to compute the staircase of a given set of n points in the plane in O(n log n) time. (b) SCANSTEP: Describe an algorithm to compute the staircase of a given set of n points in the plane, sorted in left to right order, in O(n) time. (c) NEXTSTEP: Describe an algorithm to compute the staircase of a given set of n points in the plane in O(nh) time, where h is the number of Pareto-optimal points. (d) SHATTERSTEP: Describe an algorithm to compute the staircase of a given set of n points in the plane in O(n log h) time, where h is the number of Pareto-optimal points. In all these problems, you may assume that no two points have the same x - or y -coordinates. 7 Algorithms Non-Lecture N: Convex Hulls The staircase (thick line) and staircase layers (all lines) of a set of points. 3. The staircase layers of a point set are defined by repeatedly computing the staircase and removing the Pareto-optimal points from the set, until the set becomes empty. (a) Describe and analyze an algorithm to compute the staircase layers of a given set of n points in O(n log n) time. (b) An increasing subsequence of a point set X is a sequence of points in X such that each point is above and to the right of its predecessor in the sequence. Describe and analyze an algorithm to compute the longest increasing subsequence of a given set of n points in the plane in O(n log n) time. [Hint: There is a one-line solution that uses part (a). But why is is correct?] c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 8 Algorithms Non-Lecture O: Line Segment Intersection Spengler: There’s something very important I forgot to tell you. Venkman: What? Spengler: Don’t cross the streams. Venkman: Why? Spengler: It would be bad. Venkman: I’m fuzzy on the whole good/bad thing. What do you mean, “bad"? Spengler: Try to imagine all life as you know it stopping instantaneously and every molecule in your body exploding at the speed of light. Stantz: Total protonic reversal. Venkman: Right. That’s bad. Okay. All right. Important safety tip. Thanks, Egon. — Ghostbusters (1984) O Line Segment Intersection O.1 Introduction In this lecture, I’ll talk about detecting line segment intersections. A line segment is the convex hull of two points, called the endpoints (or vertices) of the segment. We are given a set of n line segments, each specified by the x - and y -coordinates of its endpoints, for a total of 4n real numbers, and we want to know whether any two segments intersect. To keep things simple, just as in the previous lecture, I’ll assume the segments are in general position. • No three endpoints lie on a common line. • No two endpoints have the same x -coordinate. In particular, no segment is vertical, no segment is just a point, and no two segments share an endpoint. This general position assumption lets us avoid several annoying degenerate cases. Of course, in any real implementation of the algorithm I’m about to describe, you’d have to handle these cases. Real-world data is full of degeneracies! Degenerate cases of intersecting segments that we’ll pretend never happen: Overlapping colinear segments, endpoints inside segments, and shared endpoints. O.2 Two segments The first case we have to consider is n = 2. (The problem is obviously trivial when n ≤ 1!) How do we tell whether two line segments intersect? One possibility, suggested by a student in class, is to construct the convex hull of the segments. Two segments intersect if and only if the convex hull is a quadrilateral whose vertices alternate between the two segments. In the figure below, the first pair of segments has a triangular convex hull. The last pair’s convex hull is a quadrilateral, but its vertices don’t alternate. Some pairs of segments. 1 Algorithms Non-Lecture O: Line Segment Intersection Fortunately, we don’t need (or want!) to use a full-fledged convex hull algorithm just to test two segments; there’s a much simpler test. Two segments ab and cd intersect if and only if ← → • the endpoints a and b are on opposite sides of the line cd , and ← → • the endpoints c and d are on opposite sides of the line ab. To test whether two points are on opposite sides of a line through two other points, we use the same counterclockwise test that we used for building convex hulls. Specifically, a and b are on opposite sides ← → of line cd if and only if exactly one of the two triples a, c , d and b, c , d is in counterclockwise order. So we have the following simple algorithm. INTERSECT(a, b, c , d ): if CCW(a, c , d ) = CCW( b, c , d ) return FALSE else if CCW(a, b, c ) = CCW(a, b, d ) return FALSE else return TRUE Or even simpler: INTERSECT(a, b, c , d ): return CCW(a, c , d ) = CCW( b, c , d ) ∧ CCW(a, b, c ) = CCW(a, b, d ) O.3 A Sweep Line Algorithm To detect whether there’s an intersection in a set of more than just two segments, we use something called a sweep line algorithm. First let’s give each segment a unique label. I’ll use letters, but in a real implementation, you’d probably use pointers/references to records storing the endpoint coordinates. Imagine sweeping a vertical line across the segments from left to right. At each position of the sweep line, look at the sequence of (labels of) segments that the line hits, sorted from top to bottom. The only times this sorted sequence can change is when the sweep line passes an endpoint or when the sweep line passes an intersection point. In the second case, the order changes because two adjacent labels swap places.1 Our algorithm will simulate this sweep, looking for potential swaps between adjacent segments. The sweep line algorithm begins by sorting the 2n segment endpoints from left to right by comparing their x -coordinates, in O(n log n) time. The algorithm then moves the sweep line from left to right, stopping at each endpoint. We store the vertical label sequence in some sort of balanced binary tree that supports the following operations in O(log n) time. Note that the tree does not store any explicit search keys, only segment labels. • Insert a segment label. • Delete a segment label. • Find the neighbors of a segment label in the sorted sequence. O(log n) amortized time is good enough, so we could use a scapegoat tree or a splay tree. If we’re willing to settle for an expected time bound, we could use a treap or a skip list instead. 1 Actually, if more than two segments intersect at the same point, there could be a larger reversal, but this won’t have any effect on our algorithm. 2 Algorithms Non-Lecture O: Line Segment Intersection B F EE BB CD D EE FF BD D C A A E D B A BB AC C B C D ! The sweep line algorithm in action. The boxes show the label sequence stored in the binary tree. The intersection between F and D is detected in the last step. Whenever the sweep line hits a left endpoint, we insert the corresponding label into the tree in O(log n) time. In order to do this, we have to answer questions of the form ‘Does the new label X go above or below the old label Y?’ To answer this question, we test whether the new left endpoint of X is above segment Y, or equivalently, if the triple of endpoints left(Y), right(Y), left(X) is in counterclockwise order. Once the new label is inserted, we test whether the new segment intersects either of its two neighbors in the label sequence. For example, in the figure above, when the sweep line hits the left endpoint of F, we test whether F intersects either B or E. These tests require O(1) time. Whenever the sweep line hits a right endpoint, we delete the corresponding label from the tree in O(log n) time, and then check whether its two neighbors intersect in O(1) time. For example, in the figure, when the sweep line hits the right endpoint of C, we test whether B and D intersect. If at any time we discover a pair of segments that intersects, we stop the algorithm and report the intersection. For example, in the figure, when the sweep line reaches the right endpoint of B, we discover that F and D intersect, and we halt. Note that we may not discover the intersection until long after the two segments are inserted, and the intersection we discover may not be the one that the sweep line would hit first. It’s not hard to show by induction (hint, hint) that the algorithm is correct. Specifically, if the algorithm reaches the nth right endpoint without detecting an intersection, none of the segments intersect. For each segment endpoint, we spend O(log n) time updating the binary tree, plus O(1) time performing pairwise intersection tests—at most two at each left endpoint and at most one at each right endpoint. Thus, the entire sweep requires O(n log n) time in the worst case. Since we also spent O(n log n) time sorting the endpoints, the overall running time is O(n log n). Here’s a slightly more formal description of the algorithm. The input S [1 .. n] is an array of line segments. The sorting phase in the first line produces two auxiliary arrays: • label[i ] is the label of the i th leftmost endpoint. I’ll use indices into the input array S as the labels, so the i th vertex is an endpoint of S [label[i ]]. • isleft[i ] is TRUE if the i th leftmost endpoint is a left endpoint and FALSE if it’s a right endpoint. The functions INSERT, DELETE, PREDECESSOR, and SUCCESSOR modify or search through the sorted label sequence. Finally, INTERSECT tests whether two line segments intersect. 3 Algorithms Non-Lecture O: Line Segment Intersection ANYINTERSECTIONS(S [1 .. n]): sort the endpoints of S from left to right create an empty label sequence for i ← 1 to 2n ← label[i ] if isleft[i ] INSERT( ) if INTERSECT(S [ ], S [SUCCESSOR( )]) return TRUE if INTERSECT(S [ ], S [PREDECESSOR( )]) return TRUE else if INTERSECT(S [SUCCESSOR( )], S [PREDECESSOR( )]) return TRUE DELETE(label[i ]) return FALSE Note that the algorithm doesn’t try to avoid redundant pairwise tests. In the figure below, the top and bottom segments would be checked n − 1 times, once at the top left endpoint, and once at the right endpoint of every short segment. But since we’ve already spent O(n log n) time just sorting the inputs, O(n) redundant segment intersection tests make no difference in the overall running time. The same pair of segments might be tested n − 1 times. Exercises 1. Let X be a set of n rectangles in the plane, each specified by a left and right x -coordinate and a top and bottom y -coordinate. Thus, the input consists of four arrays L [1 .. n], R[1 .. n], T [1 .. n], and B [1 .. n], where L [i ] < R[i ] and T [i ] > B [i ] for all i . (a) Describe and analyze and algorithm to determine whether any two rectangles in X intersect, in O(n log n) time. (b) Describe and analyze an algorithm to find a point that lies inside the largest number of rectangles in X , in O(n log n) time. (c) Describe and analyze an algorithm to compute the area of the union of the rectangles in X , in O(n log n) time. 2. Describe and analyze a sweepline algorithm to determine, given n circles in the plane, whether any two intersect, in O(n log n) time. Each circle is specified by its center and its radius, so the input consists of three arrays X [1 .. n], Y [1 .. n], and R[1 .. n]. Be careful to correctly implement the low-level primitives. 3. Describe an algorithm to determine, given n line segments in the plane, a list of all intersecting pairs of segments. Your algorithm should run in O(n log n + k log n) time, where n is the number of segments, and k is the number of intersecting pairs. 4 Algorithms Non-Lecture O: Line Segment Intersection 4. This problem asks you to compute skylines of various cities. In each city, all the buildings have a signature geometric shape. Describe an algorithm to compute a description of the union of n such shapes in O(n log n) time. (a) Manhattan: Each building is a rectangle whose bottom edge lies on the x -axis, specified by the left and right x -coordinates and the top y -coordinate. The Manhattan skyline (b) Giza: Each building is a right isosceles triangle whose base lies on the x -axis, specified by the ( x , y )-coordinates of its apex. The Egyptian skyline (c) Nome: Each building is a semi-circular disk whose center lies on the x -axis, specified by its center x -coordinate and radius. 5. [Adapted from CLRS, problem 33-3.] A group of n Ghostbusters are teaming up to fight n ghosts. Each Ghostbuster is armed with a proton pack that shoots a stream along a straight line until it encounters (and neutralizes) a ghost. The Ghostbusters decide that they will fire their proton packs simultaneously, with each Ghostbuster aiming at a different ghost. Crossing the streams would be bad—total protonic reversal, yadda yadda—so it is vital that each Ghostbuster choose his target carefully. Assume that each Ghostbuster and each ghost is a single point in the plane, and that no three of these 2n points are collinear. (a) Prove that there is a set of n disjoint line segments, each joining one Ghostbuster to one ghost. [Hint: Consider the matching of minimum total length.] (b) Prove that the non-collinearity assumption is necessary in part (a). (c) A partitioning line is a line in the plane such that the number of ghosts on one side of the line is equal to the number of Ghostbusters on the same side of that line. Describe an algorithm to compute a partitioning line in O(n log n) time. (d) Prove that there is a partitioning line with exactly n/2 ghosts and exactly n/2 Ghostbusters on either side. This is a special case of the so-called ham sandwich theorem. Describe an algorithm to compute such a line as quickly as possible. (e) Describe a randomized algorithm to find an approximate ham-sandwich line—that is, a partitioning line with at least n/4 ghosts on each side—in O(n log n) time. (f) Describe an algorithm to compute a set of n disjoint line segments, each joining one Ghostbuster to one ghost, as quickly as possible. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 5 Algorithms Non-Lecture P: Polygon Triangulation If triangles had a god, they would give him three sides. — Charles Louis de Secondat Montesquie (1721) Down with Euclid! Death to triangles! — Jean Dieudonné (1959) P Polygon Triangulation P .1 Introduction Recall from last time that a polygon is a region of the plane bounded by a cycle of straight edges joined end to end. Given a polygon, we want to decompose it into triangles by adding diagonals: new line segments between the vertices that don’t cross the boundary of the polygon. Because we want to keep the number of triangles small, we don’t allow the diagonals to cross. We call this decomposition a triangulation of the polygon. Most polygons can have more than one triangulation; we don’t care which one we compute. Two triangulations of the same polygon. Before we go any further, I encourage you to play around with some examples. Draw a few polygons (making sure that the edges are straight and don’t cross) and try to break them up into triangles. P .2 Existence and Complexity If you play around with a few examples, you quickly discover that every triangulation of an n-sided has n − 2 triangles. You might even try to prove this observation by induction. The base case n = 3 is trivial: there is only one triangulation of a triangle, and it obviously has only one triangle! To prove the general case, let P be a polygon with n edges. Draw a diagonal between two vertices. This splits P into two smaller polygons. One of these polygons has k edges of P plus the diagonal, for some integer k between 2 and n − 2, for a total of k + 1 edges. So by the induction hypothesis, this polygon can be broken into k − 1 triangles. The other polygon has n − k + 1 edges, and so by the induction hypothesis, it can be broken into n − k − 1 tirangles. Putting the two pieces back together, we have a total of (k − 1) + (n − k − 1) = n − 2 triangles. Breaking a polygon into two smaller polygons with a diagonal. 1 Algorithms Non-Lecture P: Polygon Triangulation This is a fine induction proof, which any of you could have discovered on your own (right?), except for one small problem. How do we know that every polygon has a diagonal? This seems patently obvious, but it’s surprisingly hard to prove, and in fact many incorrect proofs were actually published as late as 1975. The following proof is due to Meisters in 1975. Lemma 1. Every polygon with more than three vertices has a diagonal. Proof: Let P be a polygon with more than three vertices. Every vertex of a P is either convex or concave, depending on whether it points into or out of P , respectively. Let q be a convex vertex, and let p and r be the vertices on either side of q. For example, let q be the leftmost vertex. (If there is more than one leftmost vertex, let q be the the lowest one.) If pr is a diagonal, we’re done; in this case, we say that the triangle pqr is an ear. If pr is not a diagonal, then pqr must contain another vertex of the polygon. Out of all the vertices ← → inside pqr , let s be the vertex furthest away from the line pr . In other words, if we take a line parallel ← → ← → to pr through q, and translate it towards pr , then then s is the first vertex that the line hits. Then the line segment qs is a diagonal. r’ s’ p q’ r q p’ The leftmost vertex q is the tip of an ear, so pr is a diagonal. The rightmost vertex q is not, since p q r contains three other vertices. In this case, q s is a diagonal. P .3 Existence and Complexity Meister’s existence proof immediately gives us an algorithm to compute a diagonal in linear time. The input to our algorithm is just an array of vertices in counterclockwise order around the polygon. First, we can find the (lowest) leftmost vertex q in O(n) time by comparing the x -coordinates of the vertices (using y -coordinates to break ties). Next, we can determine in O(n) time whether the triangle pqr contains any of the other n − 3 vertices. Specifically, we can check whether one point lies inside a triangle by performing three counterclockwise tests. Finally, if the triangle is not empty, we can find the vertex s in O(n) time by comparing the areas of every triangle pqs; we can compute this area using the counterclockwise determinant. Here’s the algorithm in excruciating detail. We need three support subroutines to compute the area of a polygon, to determine if three poitns are in counterclockwise order, and to determine if a point is inside a triangle. 〈〈Return twice the signed area of P [i ] P [ j ] P [k]〉〉 AREA(i , j , k): return ( P [k]. y − P [i ]. y )( P [ j ]. x − P [i ]. x ) − ( P [k]. x − P [i ]. x )( P [ j ]. y − P [i ]. y ) 〈〈Are P [i ], P [ j ], P [k] in counterclockwise order?〉〉 CCW(i , j , k): return AREA(i , j , k) > 0 〈〈Is P [i ] inside P [ p] P [q] P [ r ]?〉〉 INSIDE(i , p, q, r ): return CCW(i , p, q) and CCW(i , q, r ) and CCW(i , r, p) 2 Algorithms Non-Lecture P: Polygon Triangulation FINDDIAGONAL( P [1 .. n]): q←1 for i ← 2 to n if P [i ]. x < P [q]. x q←i p ← q − 1 mod n r ← q + 1 mod n ear ← TRUE s←p for i ← 1 to n if i ≤ p and i = q and i = r and INSIDE(i , p, q, r ) ear ← FALSE if AREA(i , r, p) > AREA(s, r, p) s←i if ear = TRUE return ( p, r ) else return (q, s) Once we have a diagonal, we can recursively triangulate the two pieces. The worst-case running time of this algorithm satisfies almost the same recurrence as quicksort: T (n) ≤ max T (k + 1) + T (n − k + 1) + O(n). 2≤k≤n−2 So we can now triangulate any polygon in O(n2 ) time. P .4 Faster Special Cases For certain special cases of polygons, we can do much better than O(n2 ) time. For example, we can easily triangulate any convex polygon by connecting any vertex to every other vertex. Since we’re given the counterclockwise order of the vertices as input, this takes only O(n) time. Triangulating a convex polygon is easy. Another easy special case is monotone mountains. A polygon is monotone if any vertical line intersects the boundary in at most two points. A monotone polygon is a mountain if it contains an edge from the rightmost vertex to the leftmost vertex. Every monotone polygon consists of two chains of edges going left to right between the two extreme vertices; for mountains, one of these chains is a single edge. A monotone mountain and a monotone non-mountain. 3 Algorithms Non-Lecture P: Polygon Triangulation Triangulating a monotone mounting is extremely easy, since every convex vertex is the tip of an ear, except possibly for the vertices on the far left and far right. Thus, all we have to do is scan through the intermediate vertices, and when we find a convex vertex, cut off the ear. The simplest method for doing this is probably the three-penny algorithm used in the “Graham’s scan” convex hull algorithm—instead of filling in the outside of a polygon with triangles, we’re filling in the inside, both otherwise it’s the same process. This takes O(n) time. Triangulating a monotone mountain. (Some of the triangles are very thin.) We can also triangulate general monotone polygons in linear time, but the process is more complicated. A good way to visuialize the algorithm is to think of the polygon as a complicated room. Two people named Tom and Bob are walking along the top and bottom walls, both starting at the left end and going to the right. At all times, they have a rubber band stretched between them that can never leave the room. T B A rubber band stretched between a vertex on the top and a vertex on the bottom of a monotone polygon. Now we loop through all the vertices of the polygon in order from left to right. Whenever we see a new bottom vertex, Bob moves onto it, and whenever we see a new bottom vertex Tom moves onto it. After either person moves, we cut the polygon along the rubber band. (In fact, this will only cut the polygon along a single diagonal at any step.) When we’re done, the polygon is decomposed into triangles and boomerangs—nonconvex polygons consisting of two straight edges and a concave chain. A boomerang can only be triangulated in one way, by joining the apex to every vertex in the concave chain. 4 Algorithms Non-Lecture P: Polygon Triangulation Triangulating a monotone polygon by walking a rubber band from left to right. I don’t want to go into too many implementation details, but a few observations shoudl convince you that this algorithm can be implemented to run in O(n) time. Notice that at all times, the rubber band forms a concave chain. The leftmost edge in the rubber band joins a top vertex to a bottom vertex. If the rubber band has any other vertices, either they are all on top or all on bottom. If all the other vertices are on top, there are just three ways the rubber band can change: 1. The bottom vertex changes, the rubber band straighens out, and we get a new boomerang. 2. The top vertex changes and the rubber band gets a new concave vertex. 3. The top vertex changes, the rubber band loses some vertices, and we get a new boomerang. Deciding between the first case and the other two requires a simple comparison between x -coordinates. Deciding between the last two requires a counterclockwise test. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 5 Algorithms Non-Lecture Q: Reductions Reduce big troubles to small ones, and small ones to nothing. — Chinese proverb I have yet to see any problem, however complicated, which, when you looked at it in the right way, did not become still more complicated. — Poul Anderson, New Scientist (September 25, 1969) Q Reductions Q.1 Introduction An extremely common technique for deriving algorithms is reduction—instead of solving a problem directly, we use an algorithm for some other related problem as a subroutine or black box. For example, when we talked about nuts and bolts, we argued that once the nuts and bolts are sorted, we can match each nut to its bolt in linear time. Thus, since we can sort nuts and bolts in O(n log n) expected time, then we can also match them in O(n log n) expected time: Tmatch (n) ≤ Tsort (n) + O(n) = O(n log n) + O(n) = O(n log n). Let’s consider (as we did in the previous lecture) a decision tree model of computation, where every query is a comparison between a nut and a bolt—too big, too small, or just right? The output to the matching problem is a permutation π, where for all i , the i th nut matches the π(i )th bolt. Since there are n! permutations of n items, any nut/bolt comparison tree that matches n nuts and bolts has at least n! leaves, and thus has depth at least log3 (n!) = Ω(n log n). Now the same reduction from matching to sorting can be used to prove a lower bound for sorting nuts and bolts, just by reversing the inequality: Tsort (n) ≥ Tmatch (n) − O(n) = Ω(n log n) − O(n) = Ω(n log n). Thus, any nut-bolt comparison tree that sorts n nuts and bolts has depth Ω(n log n), and our randomized quicksort algorithm is optimal.1 The rest of this lecture assumes some familiarity with computational geometry. Q.2 Sorting to Convex Hulls Here’s a slightly less trivial example. Suppose we want to prove a lower bound for the problem of computing the convex hull of a set of n points in the plane. To do this, we demonstrate a reduction from sorting to convex hulls. To sort a list of n numbers {a, b, c , . . .}, we first transform it into a set of n points {(a, a2 ), ( b, b2 ), (c , c 2 ), . . .}. You can think of the original numbers as a set of points on a horizontal real number line, and the transformation as lifting those point up to the parabola y = x 2 . Then we compute the convex hull of the parabola points. Finally, to get the final sorted list of numbers, we output the first coordinate of every convex vertex, starting from the leftmost vertex and going in counterclockwise order. 1 We could have proved this lower bound directly. The output to the sorting problem is two permutations, so there are n!2 possible outputs, and we get a lower bound of log3 (n!2 ) = Ω(n log n). 1 Algorithms Non-Lecture Q: Reductions Reducing sorting to computing a convex hull. Transforming the numbers into points takes O(n) time. Since the convex hull is output as a circular doubly-linked list of vertices, reading off the final sorted list of numbers also takes O(n) time. Thus, given a black-box convex hull algorithm, we can sort in linear extra time. In this case, we say that there is a linear time reduction from sorting to convex hulls. We can visualize the reduction as follows: O(n) set of n numbers − −−→ set of n points CONVEXHULL O(n) − sorted list of numbers ←− convex polygon (I strongly encourage you to draw a picture like this whenever you use a reduction argument, at least until you get used to them.) The reduction gives us the following inequality relating the complexities of the two problems: Tsort (n) ≤ Tconvex hull (n) + O(n) Since we can compute convex hulls in O(n log n) time, our reduction implies that we can also sort in O(n log n) time. More importantly, by reversing the inequality, we get a lower bound on the complexity of computing convex hulls. Tconvex hull (n) ≥ Tsort (n) − O(n) Since any binary decision tree requires Ω(n log n) time to sort n numbers, it follows that any binary decision tree requires Ω(n log n) time to compute the convex hull of n points. Q.3 Watch the Model! This result about the complexity of computing convex hulls is often misquoted as follows: Since we need Ω(n log n) comparisons to sort, we also need Ω(n log n) comparisons (between x -coordinates) to compute convex hulls. Although this statement is true, it’s completely trivial, since it’s impossible to compute convex hulls using any number of comparisons! In order to compute hulls, we must perform counterclockwise tests on triples of points. The convex hull algorithms we’ve seen — Graham’s scan, Jarvis’s march, divide-and-conquer, Chan’s shatter — can all be modeled as binary2 decision trees, where every query is a counterclockwise test on three points. So our binary decision tree lower bound is meaningful, and several of those algorithms are optimal. This is a subtle but important point about deriving lower bounds using reduction arguments. In order for any lower bound to be meaningful, it must hold in a model in which the problem can be solved! 2 or ternary, if we allow colinear triples of points 2 Algorithms Non-Lecture Q: Reductions Often the problem we are reducing from is much simpler than the problem we are reducing to, and thus can be solved in a more restrictive model of computation. Q.4 Element Uniqueness (A Bad Example) The element uniqueness problem asks, given a list of n numbers x 1 , x 2 , . . . , x n , whether any two of them are equal. There is an obvious and simple algorithm to solve this problem: sort the numbers, and then scan for adjacent duplicates. Since we can sort in O(n log n) time, we can solve the element uniqueness problem in O(n log n) time. We also have an Ω(n log n) lower bound for sorting, but our reduction does not give us a lower bound for element uniqueness. The reduction goes the wrong way! Inscribe the following on the back of your hand3 : To prove that problem A is harder than problem B, reduce B to A. There isn’t (as far as I know) a reduction from sorting to the element uniqueness problem. However, using other techniques (which I won’t talk about), it is possible to prove an Ω(n log n) lower bound for the element uniqueness problem. The lower bound applies to so-called algebraic decision trees. An algebraic decision tree is a ternary decision tree, where each query asks for the sign of a constant-degree polynomial in the variables x 1 , x 2 , . . . , x n . A comparison tree is an example of an algebraic decision tree, using polynomials of the form x i − x j . The reduction from sorting to element uniqueness implies that any algebraic decision tree requires Ω(n log n) time to sort n numbers. But since algebraic decision trees are ternary decision trees, we already knew that. Q.5 Closest Pair The simplest version of the closest pair problem asks, given a list of n numbers x 1 , x 2 , . . . , x n , to find the closest pair of elements, that is, the elements x i and x j that minimize | x i − x j |. There is an obvious reduction from element uniqueness to closest pair, based on the observation that the elements of the input list are distinct if and only if the distance between the closest pair is bigger than zero. This reduction implies that the closest pair problem requires Ω(n log n) time in the algebraic decision tree model. trivial set of n numbers − −−→ set of n numbers CLOSESTPAIR O(1) TRUE or FALSE ←−− closest pair x i , x j − There are also higher-dimensional closest pair problems; for example, given a set of n points in the plane, find the two points that closest together. Since the one-dimensional problem is a special case of the 2d problem — just put all n point son the x -axis — the Ω(n log n) lower bound applies to the higher-dimensional problems as well. Q.6 3SUM to Colinearity. . . Unfortunately, lower bounds are relatively few and far between. There are thousands of computational problems for which we cannot prove any good lower bounds. We can still learn something useful about the complexity of such a problem by from reductions, namely, that it is harder than some other problem. 3 right under all those rules about logarithms, geometric series, and recurrences 3 Algorithms Non-Lecture Q: Reductions Here’s an example. The problem 3SUM asks, given a sequence of n numbers x 1 , . . . , x n , whether any three of them sum to zero. There is a fairly simple algorithm to solve this problem in O(n2 ) time (hint, hint). This is widely believed to be the fastest algorithm possible. There is an Ω(n2 ) lower bound for 3SUM, but only in a fairly weak model of computation.4 Now consider a second problem: given a set of n points in the plane, do any three of them lie on a common non-horizontal line? Again, there is an O(n2 )-time algorithm, and again, this is believed to be the best possible. The following reduction from 3SUM offers some support for this belief. Suppose we are given an array A of n numbers as input to 3SUM. Replace each element a ∈ A with three points (a, 0), (−a/2, 1), and (a, 2). Thus, we replace the n numbers with 3n points on three horizontal lines y = 0, y = 1, and y = 2. If any three points in this set lie on a common non-horizontal line, they consist of one point on each of those three lines, say (a, 0), (− b/2, 1), and (c , 2). The slope of the common line is equal to both − b/2 − a and c + b/2; since these two expressions are equal, we must have a + b + c = 0. Similarly, is any three elements a, b, c ∈ A sum to zero, then the resulting points (a, 0), (− b/2, 1), and (c , 2) are colinear. So we have a valid reduction from 3SUM to the colinear-points problem: O(n) −−→ set of 3n points set of n numbers − COLINEAR? trivial − TRUE or FALSE ←−− TRUE or FALSE T3SUM (n) ≤ Tcolinear (3n) + O(n) =⇒ Tcolinear (n) ≥ T3SUM (n/3) − O(n). Thus, if we could detect colinear points in o(n2 ) time, we could also solve 3SUM in o(n2 ) time, which seems unlikely. Conversely, if we could prove an Ω(n2 ) lower bound for 3SUM in a sufficiently powerful model of computation, it would imply an Ω(n2 ) lower bound for the colinear points problem as well. The existing Ω(n2 ) lower bound for 3SUM does not imply a lower bound for finding colinear points, because the model of computation is too weak. It is possible to prove an Ω(n2 ) lower bound directly using an adversary argument, but only in a fairly weak decision-tree model of computation. Note that in order to prove that the reduction is correct, we have to show that both yes answers and no answers are correct: the numbers sum to zero if and only if three points lie on a line. Even though the reduction itself only goes one way, from the ‘easier’ problem to the ‘harder’ problem, the proof of correctness must go both ways. Anka Gajentaan and Mark Overmars5 defined a whole class of computational geometry problems that are harder than 3SUM; they called these problems 3SUM-hard. A sub-quadratic algorithm for any 3SUM-hard problem would imply a subquadratic algorithm for 3SUM. I’ll finish the lecture with two more examples of 3SUM-hard problems. Q.7 . . . to Segment Splitting . . . Consider the following segment splitting problem: Given a collection of line segments in the plane, is there a line that does not hit any segment and splits the segments into two non-empty subsets? 4 The Ω(n2 ) lower bound holds in a decision tree model where every query asks for the sign of a linear combination of three of the input numbers. For example, ‘Is 5 x 1 + x 42 − 17 x 5 positive, negative, or zero?’ See my paper ‘Lower bounds for linear satisfiability problems’ (http://www.uiuc.edu/~jeffe/pubs/linsat.html) for the gory(!) details. 5 A. Gajentaan and M. Overmars, On a class of O(n2 ) problems in computational geometry, Comput. Geom. Theory Appl. 5:165–185, 1995. ftp://ftp.cs.ruu.nl/pub/RUU/CS/techreps/CS-1993/1993-15.ps.gz 4 Algorithms Non-Lecture Q: Reductions To show that this problem is 3SUM-hard, we start with the collection of points produced by our last reduction. Replace each point by a ‘hole’ between two horizontal line segments. To make sure that the only way to split the segments is by passing through three colinear holes, we build two ‘gadgets’, each consisting of five segments, to cap off the left and right ends as shown in the figure below. Top: 3n points, three on a non-horizontal line. Bottom: 3n + 13 segments separated by a line through three colinear holes. This reduction could be performed in linear time if we could make the holes infinitely small, but computers can’t really deal with infinitesimal numbers. On the other hand, if we make the holes too big, we might be able to thread a line through three holes that don’t quite line up. I won’t go into details, but it is possible to compute a working hole size in O(n log n) time by first computing the distance between the closest pair of points. Thus, we have a valid reduction from 3SUM to segment splitting (by way of colinearity): O ( n) O(n log n) trivial trivial −−→ set of 3n + 13 segments set of n numbers − −−→ set of 3n points −−− SPLITTABLE? TRUE or FALSE ←−− TRUE or FALSE ←−−−− TRUE or FALSE − − T3SUM (n) ≤ Tsplit (3n + 13) + O(n log n) Q.8 =⇒ Tsplit (n) ≥ T3SUM n − 13 3 − O(n log n). . . . to Motion Planning Finally, suppose we want to know whether a robot can move from one position and location to another. To make things simple, we’ll assume that the robot is just a line segment, and the environment in which the robot moves is also made up of non-intersecting line segments. Given an initial position and orientation and a final position and orientation, is there a sequence of translations and rotations that moves the robot from start to finish? To show that this motion planning problem is 3SUM-hard, we do one more reduction, starting from the set of segments output by the previous reduction algorithm. Specifically, we use our earlier set of line segments as a ‘screen’ between two large rooms. The rooms are constructed so that the robot can enter or leave each room only by passing through the screen. We make the robot long enough that the robot can pass from one room to the other if and only if it can pass through three colinear holes in the screen. (If the robot isn’t long enough, it could get between the ‘layers’ of the screen.) See the figure below: 5 Algorithms Non-Lecture Q: Reductions start end The robot can move from one room to the other if and only if the screen between the rooms has three colinear holes. Once we have the screen segments, we only need linear time to compute how big the rooms should be, and then O(1) time to set up the 20 segments that make up the walls. So we have a fast reduction from 3SUM to motion planning (by way of colinearity and segment splitting): O ( n) O(n log n) O(n) −−→ set of 3n points −−− −−→ set of 3n + 13 segments − −−→ set of 3n + 33 segments set of n numbers − MOVABLE? trivial trivial TRUE or FALSE ←−− TRUE or FALSE ←−−−− − − T3SUM (n) ≤ Tmotion (3n + 33) + O(n log n) TRUE or FALSE =⇒ trivial ←−− TRUE or FALSE − Tmtion (n) ≥ T3SUM n − 33 3 − O(n log n). Thus, a sufficiently powerful Ω(n2 ) lower bound for 3SUM would imply an Ω(n2 ) lower bound for motion planning as well. The existing Ω(n2 ) lower bound for 3SUM does not imply a lower bound for this problem the model of computation in which the lower bound holds is too weak to even solve the motion planning problem. In fact, the best lower bound anyone can prove for this motion planning problem is Ω(n log n), using a (different) reduction from element uniqueness. But the reduction does give us evidence that motion planning ‘should’ require quadratic time. c Copyright 2009 Jeff Erickson. Released under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/). Free distribution is strongly encouraged; commercial distribution is expressly forbidden. See http://www.cs.uiuc.edu/~jeffe/teaching/algorithms/ for the most recent revision. 6 CS 373 Homework 0 (due 1/26/99) Spring 1999 CS 373: Combinatorial Algorithms, Spring 1999 http://www-courses.cs.uiuc.edu/ cs373 Homework 0 (due January 26, 1999 by the beginning of class) Name: Net ID: Alias: Neatly print your name (first name first, with no comma), your network ID, and a short alias into the boxes above. Do not sign your name. Do not write your Social Security number. Staple this sheet of paper to the top of your homework. Grades will be listed on the course web site by alias, so your alias should not resemble your name (or your Net ID). If you do not give yourself an alias, you will be stuck with one we give you, no matter how much you hate it. Everyone must do the problems marked ◮. Problems marked £ are for 1-unit grad students and others who want extra credit. (There’s no such thing as “partial extra credit”!) Unmarked problems are extr