algorithm and data structure pdf

448 725 1
algorithm and data structure pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Algorithms Jeff Erickson 0th edition (pre-publication draft) — December 29, 2018 — 27 26 25 24 23 22 21 20 19 18 © Copyright 2019 Jeff Erickson cb This work is available under a Creative Commons Attribution 4.0 International License For license details, see http://creativecommons.org/licenses/by/4.0/ Download this book at http://jeffe.cs.illinois.edu/teaching/algorithms/ or http://algorithms.wtf Please report errors at https://github.com/jeffgerickson/algorithms Portions of our programming are mechanically reproduced, and we now begin our broadcast day For Kim, Kate, and Hannah with love and admiration And for Erin with thanks for breaking her promise Incipit prologus in libro alghoarismi de practica arismetrice — Ioannis Hispalensis [John of Seville?], Liber algorismi de pratica arismetrice (c.1135) Shall I tell you, my friend, how you will come to understand it? Go and write a book upon it — Henry Home, Lord Kames (1696–1782), in a letter to to Sir Gilbert Elliot The individual is always mistaken He designed many things, and drew in other persons as coadjutors, quarrelled with some or all, blundered much, and something is done; all are a little advanced, but the individual is always mistaken It turns out somewhat new and very unlike what he promised himself — Ralph Waldo Emerson, “Experience”, Essays, Second Series (1844) What I have outlined above is the content of a book the realization of whose basic plan and the incorporation of whose details would perhaps be impossible; what I have written is a second or third draft of a preliminary version of this book — Michael Spivak, preface of the first edition of Differential Geometry, Volume I (1970) Preface About This Book This textbook grew out of a collection of lecture notes that I wrote for various algorithms classes at the University of Illinois at Urbana-Champaign, which I have been teaching about once a year since January 1999 Spurred by changes of our undergraduate theory curriculum, I undertook a major revision of my notes in 2016; this book consists of a subset of my revised notes on the most fundamental course material, mostly reflecting the algorithmic content of our new required junior-level theory course Prerequisites The algorithms classes I teach at Illinois have two significant prerequisites: a course on discrete mathematics and a course on fundamental data structures Consequently, this textbook is probably not suitable for most students as a first i PREFACE course in data structures and algorithms In particular, I assume at least passing familiarity with the following specific topics: • Discrete mathematics: High-school algebra, logarithm identities, naive set theory, Boolean algebra, first-order predicate logic, sets, functions, equivalences, partial orders, modular arithmetic, recursive definitions, trees (as abstract objects, not data structures), graphs (vertices and edges, not function plots) • Proof techniques: direct, indirect, contradiction, exhaustive case analysis, and induction (especially “strong” and “structural” induction) Chapter uses induction, and whenever Chapter n−1 uses induction, so does Chapter n • Iterative programming concepts: variables, conditionals, loops, records, indirection (addresses/pointers/references), subroutines, recursion I not assume fluency in any particular programming language, but I assume experience with at least one language that supports both indirection and recursion • Fundamental abstract data types: scalars, sequences, vectors, sets, stacks, queues, maps/dictionaries, ordered maps/dictionaries, priority queues • Fundamental data structures: arrays, linked lists (single and double, linear and circular), binary search trees, at least one form of balanced binary search tree (such as AVL trees, red-black trees, treaps, skip lists, or splay trees), hash tables, binary heaps, and most importantly, the difference between this list and the previous list • Fundamental computational problems: elementary arithmetic, sorting, searching, enumeration, tree traversal (preorder, inorder, postorder, levelorder, and so on) • Fundamental algorithms: elementary algorism, sequential search, binary search, sorting (selection, insertion, merge, heap, quick, radix, and so on), breadth- and depth-first search in (at least binary) trees, and most importantly, the difference between this list and the previous list • Elementary 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; writing and following mathematical arguments; recognizing and avoiding syntactic, semantic, and/or logical nonsense The book briefly covers some of this prerequisite material when it arises in context, but more as a reminder than a good introduction For a more thorough overview, I strongly recommend the following freely available references: ii Additional References • Margaret M Fleck Building Blocks for Theoretical Computer Science, unpublished textbook, most recently revised January 2013 Available from http://mfleck.cs.illinois.edu/building-blocks/ • Eric Lehman, F Thomson Leighton, and Albert R Meyer Mathematics for Computer Science, unpublished lecture notes, most recent (public) revision June 2018 Available from https://courses.csail.mit.edu/6.042/spring18/ (I strongly recommend searching for the most recent revision.) • Pat Morin Open Data Structures, most recently revised January 2016 (edition 0.1Gβ) A free open-content textbook, which Pat maintains and regularly updates Available from http://opendatastructures.org/ Additional References Please not restrict yourself to this or any other single reference Authors and readers bring their own perspectives to any intellectual material; no instructor “clicks” with every student, or even with every very strong student Finding the author that most effectively gets their intuition into your head takes some effort, but that effort pays off handsomely in the long run The following references have been particularly valuable sources of intuition, examples, exercises, and inspiration; this is not meant to be a complete list • 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 undergraduate at Rice and again as a masters student at UC Irvine.) • Thomas Cormen, Charles Leiserson, Ron Rivest, and Cliff Stein Introduction to Algorithms, third edition MIT Press/McGraw-Hill, 2009 (I used the first edition as a teaching assistant at Berkeley.) • Sanjoy Dasgupta, Christos H Papadimitriou, and Umesh V Vazirani Algorithms McGraw-Hill, 2006 (Probably the closest in content to this book, but considerably less verbose.) • Jeff Edmonds How to Think about Algorithms Cambridge University Press, 2008 • 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 • Jon Kleinberg and Éva Tardos Algorithm Design Addison-Wesley, 2005 Borrow it from the library if you can • Donald Knuth The Art of Computer Programming, volumes 1–4A AddisonWesley, 1997 and 2011 (My parents gave me the first three volumes for Christmas when I was 14 Alas, I didn’t actually read them until much later.) iii PREFACE • Udi Manber Introduction to Algorithms: A Creative Approach AddisonWesley, 1989 (I used this textbook as a teaching assistant at Berkeley.) • Ian Parberry Problems on Algorithms Prentice-Hall, 1995 (out of print) Downloadable from https://larc.unt.edu/ian/books/free/license.html after you agree to make a small charitable donation Please honor your agreement • Robert Sedgewick and Kevin Wayne Algorithms Addison-Wesley, 2011 • Robert Endre Tarjan Data Structures and Network Algorithms SIAM, 1983 • Class notes from my own algorithms classes at Berkeley, especially those taught by Dick Karp and Raimund Seidel • Lecture notes, slides, homeworks, exams, video lectures, research papers, blog posts, and full-fledged MOOCs made freely available on the web by innumerable colleagues around the world About the Exercises Each chapter ends with several exercises, most of which I have used at least once in a homework assignment, discussion/lab section, or exam The exercises are not ordered by increasing difficulty, but (generally) clustered by common techniques or themes Some problems are annotated with symbols as follows: • ♥Red hearts indicate particularly challenging problems; many of these have appeared on qualifying exams for PhD students at Illinois A small number of really hard problems are marked with ♥larger hearts, and a few open problems are marked with ♥enormous hearts • ♦Blue diamonds indicate problems that require familiarity with material from later chapters, but thematically belong where they are Problems that require familiarity with earlier material are not marked, however; the book, like life, is cumulative • ♣ Green clubs indicate problems that require familiarity with material outside the scope of this book, such as finite-state machines, linear algebra, probability, or planar graphs These are rare • ♠Black spades indicate problems that require a significant amount of grunt work and/or coding These are rare • Orange stars indicate that you are eating Lucky Charms that were manufactured before 1998 Ew These exercises are designed as opportunities to practice, not as targets for their own sake; the goal of these problems not to solve these particular problems, but to practice exercising a particular skill, or solving a type of problem Partly for this reason, I don’t provide solutions to the exercises; the solutions are not the point In particular, there is no “instructor’s manual”; if you can’t solve a iv Steal This Book! problem yourself, you probably shouldn’t assign it to your students That said, you can probably find solutions to whatever homework problems I’ve assigned this semester on the web page of whatever course I’m teaching And nothing is stopping you from writing an instructor’s manual! Steal This Book! This book is published under a Creative Commons Licence that allows you to use, redistribute, adapt, and remix its contents without my permission, as long as you point back to the original source A complete electronic version of this book is freely available at my web site http://jeffe.cs.illinois.edu/teaching/algorithms/ (or the mnemonic shortcut http://algorithms.wtf), at the bug-report site https:// github.com/jeffgerickson/algorithms, and on the Internet Archive (ask Google) The book web site also contains several hundred pages of additional lecture notes on related and more advanced material, as well as a near-complete archive of past homeworks, exams, discussion/lab problems, and other teaching resources Whenever I teach an algorithms class, I revise, update, and sometimes cull my teaching materials, so you may find more recent revisions on the web page of whatever course I am currently teaching Whether you are a student or an instructor, you are more than welcome to use any subset of this textbook or my other lecture notes in your own classes, without asking my permission—that’s why I put them on the web! However, please also cite this book, either by name or with a link back to http://algorithms.wtf; this is especially important if you are a student, and you use my course materials to help with your homework (Please also check with your instructor.) However, if you are an instructor, I strongly encourage you to supplement these with additional material that you write yourself Writing the material yourself will strengthen your mastery and in-class presentation of the material, which will in turn improve your students’ mastery of the material It will also get you past the frustration of dealing with the parts of this book that you don’t like All textbooks are crap imperfect, and this one is no exception Finally, please make whatever you write freely, easily, and globally available on the open web—not hidden behind the gates of a learning management system—so that students and instructors elsewhere can benefit from your unique insights In particular, if you develop useful resources that directly complement this textbook, such as slides, videos, or solution manuals, please let me know so that I can add links to your resources from the book web site v PREFACE Acknowledgments This textbook draws heavily on the contributions of countless algorithms students, teachers, and researchers In particular, I am immensely grateful to more than three thousand Illinois students who have used my lecture notes as a primary reference, offered useful (if sometimes painful) criticism, and suffered through some truly awful early drafts Thanks also to many colleagues and students around the world who have used these notes in their own classes and have sent helpful feedback and bug reports I am particularly grateful for the feedback and contributions (especially exercises) from my amazing teaching assistants: Aditya Ramani, Akash Gautam, Alex Steiger, Alina Ene, Amir Nayyeri, Asha Seetharam, Ashish Vulimiri, Ben Moseley, Brad Sturt, Brian Ensink, Chao Xu, Charlie Carlson, Chris Neihengen, Connor Clark, Dan Bullok, Dan Cranston, Daniel Khashabi, David Morrison, Ekta Manaktala, Erin Wolf Chambers, Gail Steitz, Gio Kao, Grant Czajkowski, Hsien-Chih Chang, Igor Gammer, Jacob Laurel, John Lee, Johnathon Fischer, Junqing Deng, Kent Quanrud, Kevin Milans, Kevin Small, Konstantinos Koiliaris, Kyle Fox, Kyle Jao, Lan Chen, Mark Idleman, Michael Bond, Mitch Harris, Naveen Arivazhagen, Nick Bachmair, Nick Hurlburt, Nirman Kumar, Nitish Korula, Patrick Lin, Phillip Shih, Rachit Agarwal, Reza Zamani-Nasab, Rishi Talreja, Rob McCann, Sahand Mozaffari, Shalan Naqvi, Shripad Thite, Spencer Gordon, Srihita Vatsavaya, Subhro Roy, Tana Wattanawaroon, Umang Mathur, Vipul Goyal, Yasu Furakawa, and Yipu Wang I’ve also been helped tremendously by many discussions with faculty colleagues at Illinois: Alexandra Kolla, Cinda Heeren, Edgar Ramos, Herbert Edelsbrunner, Jason Zych, Kim Whittlesey, Lenny Pitt, Madhu Parasarathy, Mahesh Viswanathan, Margaret Fleck, Shang-Hua Teng, Steve LaValle, and especially Chandra Chekuri, Ed Reingold, and Sariel Har-Peled Of course this book owes a great debt to the people who taught me this algorithms stuff in the first place: Bob Bixby and Michael Pearlman at Rice; David Eppstein, Dan Hirschberg, and George Lueker at Irvine; and Abhiram Ranade, Dick Karp, Manuel Blum, Mike Luby, and Raimund Seidel at Berkeley I stole the first iteration of the overall course structure, and the idea to write up my own lecture notes in the first place, from Herbert Edelsbrunner; the idea of turning a subset of my notes into a book from Steve LaValle; and several components of the book design from Robert Ghrist Caveat Lector! Of course, none of those people should be blamed for any flaws in the resulting book Despite many rounds of revision and editing, this book contains many vi 12 NP-HARDNESS (a) (b) Figure 12.23 (a) Gadget for planar 3-colorability (b) Gadget for degree-4 planar 3-colorability 15 Prove that the following problems are NP-hard (a) Given an undirected graph G, does G contain a simple path that visits all but 17 vertices? (b) Given an undirected graph G, does G have a spanning tree in which every node has degree at most 23? (c) Given an undirected graph G, does G have a spanning tree with at most 42 leaves? (d) Given an undirected graph G = (V, E), what is the size of the largest subset of vertices S ⊆ V such that at most 374 edges in E have both endpoints in S? (e) Given an undirected graph G = (V, E), what is the size of the largest subset of vertices S ⊆ V such that each vertex in S has at most 473 neighbors in S? (f) Given an undirected graph G, is it possible to color the vertices of G with three different colors, so that at most 31337 edges have both endpoints the same color? 16 Prove that the following variants of the minimum spanning tree problem are NP-hard (a) Given a graph G, compute the maximum-diameter spanning tree of G (The diameter of a spanning tree T is the length of the longest path in T ) (b) Given a graph G with weighted edges, compute the minimum-weight depth-first spanning tree of G (c) Given a graph G with weighted edges and a subset S of vertices of G, compute the minimum-weight spanning tree all of whose leaves are in S (d) Given a graph G with weighted edges and an integer , compute the minimum-weight spanning tree with at most leaves 416 Exercises (e) Given a graph G with weighted edges and an integer ∆, compute the minimum-weight spanning tree where every node has degree at most ∆ 17 There’s something special about the number (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 [Hint: This problem is strongly connected to topics described in an earlier chapter.] 18 There’s nothing special about the number (a) The problem 12Partition is defined as follows: Given a set S of 12n positive integers, determine whether the elements of S can be split into n subsets of 12 elements each whose sums are all equal Prove that 12Partition is NP-hard [Hint: Reduce from 3Partition It may be easier to consider multisets first.] (b) The problem 12Color is defined as follows: Given an undirected graph G, determine whether we can color each vertex with one of twelve colors, so that every edge touches two different colors Prove that 12Color is NP-hard [Hint: Reduce from 3Color.] (c) The problem 12SAT is defined as follows: Given a boolean formula Φ in conjunctive normal form, with exactly twelve literals per clause, determine whether Φ has a satisfying assignment Prove that 12Sat is NP-hard [Hint: Reduce from 3Sat.] 19 (a) Describe a polynomial-time reduction from 3Sat to 4Sat (b) Describe a polynomial-time reduction from 4Sat to 3Sat ♥ 20 Describe a direct polynomial-time reduction from 4Color to 3Color (This is a lot harder than the opposite direction.) 417 12 NP-HARDNESS 21 A domino is a × rectangle divided into two squares, each of which is labeled with an integer.25 In a legal arrangement of dominoes, the dominoes are lined up end-to-end so that the numbers on adjacent ends match For each of the following problems, either describe a polynomial-time algorithm or prove that the problem is NP-hard: (a) Given an arbitrary bag D of dominoes, is there a legal arrangement of all the dominos in D? (b) Given an arbitrary bag D of dominoes, is there a legal arrangement of a dominos from D in which every integer between and n appears exactly twice? Figure 12.24 A legal arrangement of dominos in which every integer between and appears twice ♥ (c) Given an arbitrary bag D of dominoes, what is the largest number of dominos we can take from D to make a legal arrangement? 22 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-hard 23 Recall that a 5-coloring of a graph G is a function that assigns each vertex of G a “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 (mod 5) Prove that deciding whether a given graph has a careful 5-coloring is NP-hard [Hint: Reduce from the standard 5Color problem.] 24 (a) A subset S of vertices in an undirected graph G is half-independent if each vertex in S is adjacent to at most one other vertex in S Prove that finding the size of the largest half-independent set of vertices in a given undirected graph is NP-hard 25 These integers are usually represented by pips, exactly like dice On a standard domino, the number of pips on each side is between and 6, although one can buy sets with up to or even 12 pips on each side; we will allow arbitrary integer labels A standard set of dominoes contains exactly one domino for each possible unordered pair of labels; we not assume that the inputs to our problems have this property 418 Exercises 3 0 Figure 12.25 A careful 5-coloring (b) A subset S of vertices in an undirected graph G is sort-of-independent if if each vertex in S is adjacent to at most 374 other vertices in S Prove that finding the size of the largest sort-of-independent set of vertices in a given undirected graph is NP-hard (c) A subset S of vertices in an undirected graph G is almost independent if at most 374 edges in G have both endpoints in S Prove that finding the size of the largest almost-independent set of vertices in a given undirected graph is NP-hard 25 A subset S of vertices in an undirected graph G is triangle-free if, for every triple of vertices u, v, w ∈ S, at least one of the three edges uv, uw, vw is absent from G Prove that finding the size of the largest triangle-free subset of vertices in a given undirected graph is NP-hard Figure 12.26 A triangle-free subset of vertices This is not the largest triangle-free subset in this graph 26 Let G = (V, E) be a graph A dominating set in G is a subset S of the vertices such that every vertex in G is either in S or adjacent to a vertex in S The DominatingSet problem asks, given a graph G and an integer k as input, whether G contains a dominating set of size k Prove that this problem is NP-hard 27 The RectangleTiling problem is defined as follows: Given one large rectangle and several smaller rectangles, determine whether the smaller rectangles can be placed inside the large rectangle with no gaps or overlaps (a) Prove that RectangleTiling is NP-hard (b) Prove that RectangleTiling is strongly NP-hard 419 12 NP-HARDNESS Figure 12.27 A dominating set of size in the Peterson graph 7 4 Figure 12.28 A positive instance of the RectangleTiling problem 28 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 more than half of the total weight of all edges in G Prove that deciding whether a graph has a heavy Hamiltonian cycle is NP-hard 12 Figure 12.29 A heavy Hamiltonian cycle The cycle has total weight 34; the graph has total weight 67 29 (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-hard (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-hard [Hint: Use part (a) Or not.] 30 (a) A subset B of vertices in a graph G is a Burr set if removing every vertex in B from G leaves a subgraph that does not contain a Hamiltonian path Prove that finding the smallest Burr set in a given graph is NP-hard 420 Exercises (b) A subset S of vertices in a graph G is a Schuyler set if removing every vertex in S from G leaves a subgraph that does contain a Hamiltonian path Prove that finding the smallest Schuyler set in a given graph is NP-hard 31 This exercise asks you to prove that a certain reduction from VertexCover to SteinerTree is correct Suppose we want to find the smallest vertex cover in a given undirected graph G = (V, E) We construct a new graph H = (V , E ) as follows: • V = V ∪ E ∪ {z} • E = {ve | v ∈ V is an endpoint of e ∈ W } ∪ {vz | v ∈ V } Equivalently, we construct H by subdividing each edge in G with a new vertex, and then connecting all the original vertices of G to a new apex vertex z Prove that G has a vertex cover of size k if and only if there is a subtree of H with k + |E| + vertices that contains every vertex in E ∪ {z} 32 There are two different versions of the Hamiltonian cycle problem, one for directed graphs and one for undirected graphs Earlier in this chapter you can find two proofs that the directed Hamiltonian cycle problem is NP-hard (a) Describe a polynomial-time reduction from the undirected Hamiltonian cycle problem to the directed Hamiltonian cycle problem Prove your reduction is correct (b) Describe a polynomial-time reduction from the directed Hamiltonian cycle problem to the undirected Hamiltonian cycle problem Prove your reduction is correct (c) Which of these two reductions implies that the undirected Hamiltonian cycle problem is NP-hard? 33 For each of the following problems, either describe a polynomial-time algorithm or prove that the problem is NP-hard (a) A double-Eulerian tour 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 tour? (b) A double-Hamiltonian tour 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 tour? (c) A double-Hamiltonian circuit in an undirected graph G is a closed walk that visits every vertex in G exactly twice and traverses each edge in G at most once Given a graph G, does G have a double-Hamiltonian circuit? 421 12 NP-HARDNESS (d) A triple-Eulerian tour in an undirected graph G is a closed walk that traverses every edge in G exactly three times Given a graph G, does G have a triple-Eulerian tour? (e) A triple-Hamiltonian tour in an undirected graph G is a closed walk that visits every vertex in G exactly three times Given a graph G, does G have a triple-Hamiltonian tour? 34 Consider the following solitaire game The puzzle consists of an n × m grid of squares, where each square may be empty, occupied by a red stone, or occupied by a blue stone The goal of the puzzle is to remove some of the given stones so that the remaining stones satisfy two conditions: (1) every row contains at least one stone, and (2) no column contains stones of both colors For some initial configurations of stones, reaching this goal is impossible A solvable puzzle and one of its many solutions An unsolvable puzzle Prove that it is NP-hard to determine, given an initial configuration of red and blue stones, whether the puzzle can be solved 35 Each of the following games involves an n × m grid of squares, where each square is either empty or occupied by a stone In a single move, you can remove all the stones in an arbitrary column (a) Prove that it is NP-hard to find the smallest subset of columns that can be cleared so that at most one stone remains in each row of the grid (b) Prove that it is NP-hard to find the largest subset of columns that can be cleared so that at least one stone remains in each row of the grid ♥ (c) Prove that it is NP-hard to determine whether any subset of columns can be cleared so that exactly one stone remains in each row of the grid 36 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 422 Exercises student is happy if (but only if) he 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 37 You’re in charge of choreographing a musical for your local community theater, and it’s time to figure out the final pose of the big show-stopping number at the end (“Streetcar!”) You’ve decided that each of the n cast members in the show will be positioned in a big line when the song finishes, all with their arms extended and showing off their best spirit fingers The director has declared that during the final flourish, each cast member must either point both their arms up or point both their arms down; it’s your job to figure out who points up and who points down Moreover, in a fit of unchecked power, the director has also given you a list of arrangements that will upset his delicate artistic temperament Each forbidden arrangement is a subset of the cast members paired with arm positions; for example: “Marge may not point her arms up while Ned, Apu, and Smithers point their arms down.” Prove that finding an acceptable arrangement of arm positions is NP-hard 38 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 39 The party you are attending is going great, but now it’s time to line up for The Algorithm March (アルゴリズムこうしん)! This dance was originally developed by the Japanese comedy duo Itsumo Kokokara (いつもここから) for the children’s television show PythagoraSwitch (ピタゴラスイッチ) The Algorithm March is performed by a line of people; each person in line starts a specific sequence of movements one measure later than the person 423 12 NP-HARDNESS directly in front of them Thus, the march is the dance equivalent of a musical round or canon, like “Row Row Row Your Boat” Proper etiquette dictates that each marcher must know the person directly in front of them in line, lest a minor mistake during lead to horrible embarrassment between strangers Suppose you are given a complete list of which people at your party know each other Prove that it is NP-hard to determine the largest number of party-goers that can participate in the Algorithm March You may assume without loss of generality that there are no ninjas at your party ♣ 40 Prove that the following problems about nondeterministic finite-state automata and regular expressions are NP-hard: (a) Given an NFA M over the alphabet Σ = {0, 1}, is there a string in Σ∗ that M does not accept? (b) Given an acyclic NFA M over the alphabet Σ = {0, 1}, what is the length of the shortest string in Σ∗ that M does not accept? (c) Given a regular expression R over the alphabet Σ = {0, 1}, is there a string in Σ∗ that R does not match? (d) Given a star-free regular expression R over the alphabet Σ = {0, 1}, what is the length of the shortest string in Σ∗ that R does not match? (In fact, problems (a) and (c) are PSPACE-complete; even proving that these problems are in PSPACE is nontrivial.) ♣ 41 (a) Describe a polynomial-time algorithm for the following problem: Given an NFA M over the alphabet Σ = {0, 1}, is there a string in Σ∗ that M does accept? (b) Describe a polynomial-time algorithm for the following problem: Given a regular expression R over the alphabet Σ = {0, 1}, is there a string in Σ∗ that R does match? (c) The complement of any regular language is another regular language So why don’t these two algorithms, together with the NP-hardness results in Problem 40, prove that P=NP? 42 Charon needs to ferry n recently deceased people across the river Acheron into Hades Certain pairs of these people are sworn enemies, who cannot be together on either side of the river unless Charon is also present (If two enemies are left alone, one will steal the obol from the other’s mouth, leaving them to wander the banks of the Acheron as a ghost for all eternity Let’s 424 Exercises just say this is a Very Bad Thing.) The ferry can hold at most k passengers at a time, including Charon, and only Charon can pilot the ferry.26 Prove that it is NP-hard to decide whether Charon can ferry all n people across the Acheron unharmed (aside from being, you know, dead) The input for Charon’s problem consists of the integers k and n and an n-vertex graph G describing the pairs of enemies The output is either True or False Please not write your solution in classical Latin 26 This is a generalization of the well-known wolf-goat-and-cabbage puzzle, whose first known appearance is in the remarkable medieval manuscript Propositiones ad Acuendos Juvenes [Problems to Sharpen the Young] XVIII Propositio De Homine et Capra et Lvpo Homo quidam debebat ultra fluuium transferre lupum, capram, et fasciculum cauli Et non potuit aliam nauem inuenire, nisi quae duos tantum ex ipsis ferre ualebat Praeceptum itaque ei fuerat, ut omnia haec ultra illaesa omnino transferret Dicat, qui potest, quomodo eis illaesis transire potuit? Solutio Simili namque tenore ducerem prius capram et dimitterem foris lupum et caulum Tum deinde uenirem, lupumque transferrem: lupoque foris misso capram naui receptam ultra reducerem; capramque foris missam caulum transueherem ultra; atque iterum remigassem, capramque assumptam ultra duxissem Sicque faciendo facta erit remigatio salubris, absque uoragine lacerationis For those few readers whose classical Latin is a little rusty, here is an English translation: XVIII The Problem of the Man, the Goat, and the Wolf A man needed to transfer a wolf, a goat, and a bundle of cabbage across a river However, he found that his boat could only bear the weight of two [objects at a time, including the man] And he had to get everything across unharmed Tell me if you can: How they were able to cross unharmed? Solution In a similar fashion [as an earlier problem], I would first take the goat across and leave the wolf and cabbage on the opposite bank Then I would take the wolf across; leaving the wolf on shore, I would retrieve the goat and bring it back again Then I would leave the goat and take the cabbage across And then I would row across again and get the goat In this way the crossing would go well, without any threat of slaughter The most likely author of the Propositiones is the prolific 8th-century English scholar Alcuin of York The evidence for Alcuin’s authorship of this treatise is somewhat circumstantial; however, we know from his correspondence with Charlemagne that he sent the emperor some “simple arithmetical problems for fun” Most modern scholars believe that even if Alcuin did write the Propositiones, he did not invent all of the problems himself, but rather collected them from even earlier sources Some things never change 425 A wisely chosen illustration is almost essential to fasten the truth upon the ordinary mind, and no teacher can afford to neglect this part of his preparation — Howard Crosby (c.1880) One showing is worth a hundred sayings — Alan Watts (misquoting a Chinese proverb), The Way of Zen (1957) Please not think that this is a neutral matter and that the only advantage of doing without pictures is that of saving space Pictures in textbooks actually interfere with the learning process — Neville Martin Gwynne, Gwynne’s Grammar (2013) Image Credits All figures in this book are original works of the author, except for the following All listed works are in the public domain unless otherwise indicated • Figure 0.1 (page 5) — Biblioteca nazionale Braidense (Milano) http://atena.beic.it/webclient/DeliveryManager?pid=2953344 • Figure 0.2 (page 5) — Internet Archive https://archive.org/details/archimedisopera05eutogoog/page/n377 • Figure 1.14 (page 45) — Internet Archive https://archive.org/details/p1rcrationsm00lucauoft/page/162 • Figure 1.23 (page 60) — Derived from a crayon portrait of the author by Tina Erickson (2000); included with permission of the artist • Figure 5.1 (page 186) — Wikimedia Commons https://commons.wikimedia.org/wiki/File:Tabula_Peutingeriana_-_Miller.jpg • Figure 5.2 (page 187) — Gallery of “Legal Trees” published by the Yale Law Library under a Creative Commons Licence https://www.flickr.com/photos/yalelawlibrary/albums/72157621954683764 • Figure 5.3 (page 187) — Internet Archive https://archive.org/details/A077240124/page/n261 • Exercises 5.20 (page 214) and 8.21 (page 302) — Original puzzles by the author, inspired by Jason Batterson and Shannon Rogers, Beast Academy Math: Practice 3A, 2012 https://beastacademy.com/pdf/3A/printables/AngleMazes.pdf https://www.beastacademy.com/resources/printables.php • Figure 10.1 (page 326) — T[homas] E Harris and F[rank] S Ross Fundamentals of a method for evaluating rail net capacities The RAND Corporation, Research Memorandum RM-1517, October 24, 1955 U S Government work in the public domain http://www.dtic.mil/dtic/tr/fulltext/u2/093458.pdf Have something to say Say it Stop when you have said it Give the paper a proper title — John Shaw Billings, “An Address on Our Medical Literature”, International Medical Congress, London (1881) 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) Colophon This book was edited in TeXShop and typeset using pdfLATEX (MacTeX-2018) using the memoir document class (with madsen chapter style, komalike head style, and Ruled page style); several standard packages including amsmath, babel, enumitem, mathdesign, microtype, and standalone; and an embarrassing amount of customization and TEX-hAXing The text is typeset in Bitstream Charter, Α ᾿ ρτεµισία, Roboto, and Inconsolata Except as indicated in the Image Credits, all figures were drawn by the author using OmniGraffle Pro and included as PDF files, using the graphicx LATEX package Portions of our programming have been mechanically reproduced, and we now conclude our broadcast day ... mathematics and a course on fundamental data structures Consequently, this textbook is probably not suitable for most students as a first i PREFACE course in data structures and algorithms In... agreement • Robert Sedgewick and Kevin Wayne Algorithms Addison-Wesley, 2011 • Robert Endre Tarjan Data Structures and Network Algorithms SIAM, 1983 • Class notes from my own algorithms classes at Berkeley,... way to present an algorithm is using a combination of pseudocode and structured English Pseudocode uses the structure of formal programming languages and mathematics to break algorithms into primitive

Ngày đăng: 27/12/2019, 14:36

Từ khóa liên quan

Mục lục

  • Preface

    • About This Book

    • Prerequisites

    • Additional References

    • About the Exercises

    • Steal This Book!

    • Acknowledgments

    • Caveat Lector!

    • Table of Contents

    • Introduction

      • What is an algorithm?

      • Multiplication

        • Lattice Multiplication

        • Duplation and Mediation

        • Compass and Straightedge

        • Congressional Apportionment

        • A Bad Example

        • Describing Algorithms

          • Specifying the Problem

          • Describing the Algorithm

          • Analyzing Algorithms

            • Correctness

            • Running Time

            • Exercises

            • Recursion

              • Reductions

Tài liệu cùng người dùng

Tài liệu liên quan