Algorithms Sariel Harpeled Instant Download
Algorithms Sariel Harpeled Instant Download
https://ebookbell.com/product/algorithms-sariel-harpeled-50099372
https://ebookbell.com/product/algorithms-and-methods-in-structural-
bioinformatics-nurit-haspel-45210820
https://ebookbell.com/product/algorithms-and-applications-for-
academic-search-recommendation-and-quantitative-association-rule-
mining-1st-edition-emmanouil-amolochitis-46497506
https://ebookbell.com/product/algorithms-for-decision-making-mykel-j-
kochenderfer-tim-a-wheeler-kyle-h-wray-46709392
https://ebookbell.com/product/algorithms-and-autonomy-the-ethics-of-
automated-decision-systems-alan-rubel-clinton-castro-adam-
pham-46770860
Algorithms In Structural Molecular Biology Bruce R Donald
https://ebookbell.com/product/algorithms-in-structural-molecular-
biology-bruce-r-donald-47312440
https://ebookbell.com/product/algorithms-for-the-people-democracy-in-
the-age-of-ai-josh-simons-47525932
Algorithms For Big Data Dfg Priority Program 1736 Hannah Bast
https://ebookbell.com/product/algorithms-for-big-data-dfg-priority-
program-1736-hannah-bast-47546596
https://ebookbell.com/product/algorithms-4-11th-printing-robert-
sedgewick-kevin-wayne-48124128
https://ebookbell.com/product/algorithms-humans-and-interactions-how-
do-algorithms-interact-with-people-designing-meaningful-ai-
experiences-1st-edition-don-donghee-shin-48299938
Algorithms With Julia Optimization Machine Learning And Differential
Equations Using The Julia Language Clemens Heitzinger
https://ebookbell.com/product/algorithms-with-julia-optimization-
machine-learning-and-differential-equations-using-the-julia-language-
clemens-heitzinger-48714106
CS 473: Algorithms¬
Sariel Har-Peled
March 8, 2019
¬ This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 171 Second
Street, Suite 300, San Francisco, California, 94105, USA.
Preface
This manuscript is a collection of class notes for the (no longer required graduate) course “473G/573/473
Graduate Algorithms” taught in the University of Illinois, Urbana-Champaign, in 1. Spring 2006, 2. Fall 07,
3. Fall 09, 4. Fall 10, 5. Fall 13, 6. Fall 14, and 7. fall 18.
Class notes for algorithms class are as common as mushrooms after a rain. I have no plan of publishing
these class notes in any form except on the web. In particular, Jeff Erickson has class notes for 374/473 which
are better written and cover some of the topics in this manuscript (but naturally, I prefer my exposition over
his).
My reasons in writing the class notes are to (i) avoid the use of a (prohibitly expensive) book in this class,
(ii) cover some topics in a way that deviates from the standard exposition, and (iii) have a clear description of
the material covered. In particular, as far as I know, no book covers all the topics discussed here. Also, this
manuscript is available (on the web) in more convenient lecture notes form, where every lecture has its own
chapter.
Most of the topics covered are core topics that I believe every graduate student in computer science should
know about. This includes NP-Completeness, dynamic programming, approximation algorithms, randomized
algorithms and linear programming. Other topics on the other hand are more optional and are nice to know
about. This includes topics like network flow, minimum-cost network flow, and union-find. Nevertheless, I
strongly believe that knowing all these topics is useful for carrying out any worthwhile research in any subfield
of computer science.
Teaching such a class always involve choosing what not to cover. Some other topics that might be worthy
of presentation include advanced data-structures, computational geometry, etc – the list goes on. Since this
course is for general consumption, more theoretical topics were left out (e.g., expanders, derandomization, etc).
In particular, these class notes cover way more than can be covered in one semester. For my own sanity, I
try to cover some new material every semester I teach this class. Furthermore, some of the notes contains more
detail than I cover in class.
In any case, these class notes should be taken for what they are. A short (and sometime dense) tour of some
key topics in algorithms. The interested reader should seek other sources to pursue them further.
If you find any typos, mistakes, errors, or lies, please email me.
Acknowledgments
(No preface is complete without them.) I would like to thank the students in the class for their input, which
helped in discovering numerous typos and errors in the manuscript. Furthermore, the content was greatly
effected by numerous insightful discussions with Chandra Chekuri, Jeff Erickson, and Edgar Ramos.
In addition, I would like to thank Qizheng He for pointing out many typos in the notes (which were fixed
more later than sooner).
Copyright
This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 License. To view a copy of
this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons,
171 Second Street, Suite 300, San Francisco, California, 94105, USA.
— Sariel Har-Peled
March 2019, Urbana, IL
1
Contents
Preface 1
Contents 2
I NP Completeness 14
1 NP Completeness I 14
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.2 Complexity classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2.1 Reductions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.3 More NP-Complete problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.3.1 3SAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.4 Bibliographical Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2 NP Completeness II 20
2.1 Max-Clique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.2 Independent Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3 Vertex Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4 Graph Coloring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 NP Completeness III 25
3.1 Hamiltonian Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2 Traveling Salesman Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.3 Subset Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.4 3 dimensional Matching (3DM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.5 Partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.6 Some other problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
II Dynamic programming 30
4 Dynamic programming 30
4.1 Basic Idea - Partition Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.1.1 A Short sermon on memoization . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.2 Example – Fibonacci numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.2.1 Why, where, and when? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.2.2 Computing Fibonacci numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.3 Edit Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
4.3.1 Shortest path in a DAG and dynamic programming . . . . . . . . . . . . . . . . 36
2
5.1 Optimal search trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.2 Optimal Triangulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.3 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.4 Longest Ascending Subsequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.5 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5.6 Slightly faster TSP algorithm via dynamic programming . . . . . . . . . . . . . . . . 41
6 Approximation algorithms 43
6.1 Greedy algorithms and approximation algorithms . . . . . . . . . . . . . . . . . . . . 43
6.1.1 Alternative algorithm – two for the price of one . . . . . . . . . . . . . . . . . . 45
6.2 Fixed parameter tractability, approximation, and fast exponential time algorithms
(to say nothing of the dog) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.2.1 A silly brute force algorithm for vertex cover . . . . . . . . . . . . . . . . . . . . 45
6.2.2 A fixed parameter tractable algorithm . . . . . . . . . . . . . . . . . . . . . . . 45
6.2.2.1 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.3 Approximating maximum matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.4 Graph diameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.5 Traveling Salesman Person . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.5.1 TSP with the triangle inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.5.1.1 A 2-approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.5.1.2 A 3/2-approximation to TSP 4, -Min . . . . . . . . . . . . . . . . . . . . . . 50
6.6 Biographical Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7 Approximation algorithms II 51
7.1 Max Exact 3SAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.2 Approximation Algorithms for Set Cover . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2.1 Guarding an Art Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2.2 Set Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2.3 Lower bound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.2.4 Just for fun – weighted set cover . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.2.4.1 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.3 Biographical Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
IV Randomized algorithms 65
9 Randomized Algorithms 65
3
9.1 Some Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
9.2 Sorting Nuts and Bolts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
9.2.1 Running time analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
9.2.1.1 Alternative incorrect solution . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.2.2 What are randomized algorithms? . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.3 Analyzing QuickSort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
9.4 QuickSelect – median selection in linear time . . . . . . . . . . . . . . . . . . . . . . . 69
10 Randomized Algorithms II 71
10.1 QuickSort and Treaps with High Probability . . . . . . . . . . . . . . . . . . . . . . . 71
10.1.1 Proving that an element participates in small number of rounds . . . . . . . . . 71
10.1.2 An alternative proof of the high probability of QuickSort . . . . . . . . . . . . . 73
10.2 Chernoff inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.2.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.2.2 Chernoff inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.2.2.1 The Chernoff Bound — General Case . . . . . . . . . . . . . . . . . . . . . 75
10.3 Treaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.3.1 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.3.2 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.3.2.1 Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
10.3.2.2 Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
10.3.2.3 Split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
10.3.2.4 Meld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
10.3.3 Summery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
10.4 Bibliographical Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
11 Hashing 78
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
11.2 Universal Hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
11.2.1 How to build a 2-universal family . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.2.1.1 On working modulo prime . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.2.1.2 Constructing a family of 2-universal hash functions . . . . . . . . . . . . . 82
11.2.1.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
11.2.1.4 Explanation via pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
11.3 Perfect hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
11.3.1 Some easy calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
11.3.2 Construction of perfect hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.3.2.1 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.4 Bloom filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
11.5 Bibliographical notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
12 Min Cut 87
12.1 Branching processes – Galton-Watson Process . . . . . . . . . . . . . . . . . . . . . . 87
12.1.1 The problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.1.2 On coloring trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
12.2 Min Cut . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.2.1 Problem Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.2.2 Some Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.3 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12.3.1 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4
12.3.1.1 The probability of success . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
12.3.1.2 Running time analysis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
12.4 A faster algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
12.5 Bibliographical Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
V Network flow 95
13 Network Flow 95
13.1 Network Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
13.2 Some properties of flows and residual networks . . . . . . . . . . . . . . . . . . . . . . 96
13.3 The Ford-Fulkerson method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
13.4 On maximum flows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
5
17.5.1 Reduced cost induced by a circulation . . . . . . . . . . . . . . . . . . . . . . . . 123
17.5.2 Bounding the number of iterations . . . . . . . . . . . . . . . . . . . . . . . . . 123
17.6 Bibliographical Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6
21.6.1 The problem and the basic idea . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
21.6.2 Pivoting as a Gauss elimination step . . . . . . . . . . . . . . . . . . . . . . . . 152
21.6.2.1 Back to the perturbation scheme . . . . . . . . . . . . . . . . . . . . . . . . 152
21.6.2.2 The overall algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
7
VIII Compression, entropy, and randomness 186
32 Matchings 203
32.1 Definitions and basic properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
32.1.1 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
32.1.2 Matchings and alternating paths . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
32.2 Unweighted matching in bipartite graph . . . . . . . . . . . . . . . . . . . . . . . . . 205
32.2.1 The slow algorithm; algSlowMatch . . . . . . . . . . . . . . . . . . . . . . . . . 205
32.2.2 The Hopcroft-Karp algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
32.2.2.1 Some more structural observations . . . . . . . . . . . . . . . . . . . . . . . 206
32.2.2.2 Improved algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
32.2.2.3 Extracting many augmenting paths: algExtManyPaths . . . . . . . . . . . 207
32.2.2.4 The result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
32.3 Bibliographical notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
33 Matchings II 210
33.1 Maximum weight matchings in a bipartite graph . . . . . . . . . . . . . . . . . . . . . 211
33.1.1 On the structure of the problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
33.1.2 Maximum Weight Matchings in a bipartite Graph . . . . . . . . . . . . . . . . . 212
8
33.1.2.1 Building the residual graph . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
33.1.2.2 The algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
33.1.3 Faster Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
33.2 The Bellman-Ford algorithm - a quick reminder . . . . . . . . . . . . . . . . . . . . . 213
33.3 Maximum size matching in a non-bipartite graph . . . . . . . . . . . . . . . . . . . . 213
33.3.1 Finding an augmenting path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
33.3.2 The algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
33.3.2.1 Running time analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
33.4 Maximum weight matching in a non-bipartite graph . . . . . . . . . . . . . . . . . . . 217
37 Streaming 233
37.1 How to sample a stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
37.2 Sampling and median selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
37.2.1 A median selection with few comparisons . . . . . . . . . . . . . . . . . . . . . . 235
37.3 Big data and the streaming model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
37.4 Heavy hitters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
37.5 Chernoff inequality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
9
X Exercises 237
XI Homeworks/midterm/final 289
10
47.1.1 Homework 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
47.1.2 Homework 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
47.1.3 Homework 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
47.1.4 Homework 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
47.1.5 Homework 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
47.1.6 Homework 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
47.1.7 Homework 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
47.2 Midterm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
47.3 Final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
11
60.1.6 Homework 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
60.1.7 Homework 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
60.1.8 Homework 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
60.1.9 Homework 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
60.1.10 Homework 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
60.1.11 Homework 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
60.2 Midterm 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
60.3 Midterm 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
60.4 Final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
12
63.1.11 Homework 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
63.2 Midterm I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
63.3 Midterm II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
63.4 Final . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Bibliography 491
Index 494
13
Part I
NP Completeness
Chapter 1
NP Completeness I
"Then you must begin a reading program immediately so that you man understand the crises of our age," Ignatius said
solemnly. "Begin with the late Romans, including Boethius, of course. Then you should dip rather extensively into
early Medieval. You may skip the Renaissance and the Enlightenment. That is mostly dangerous propaganda. Now,
that I think about of it, you had better skip the Romantics and the Victorians, too. For the contemporary period,
you should study some selected comic books."
"You’re fantastic."
"I recommend Batman especially, for he tends to transcend the abysmal society in which he’s found himself. His
morality is rather rigid, also. I rather respect Batman."
– A confederacy of Dunces, John Kennedy Toole.
1.1. Introduction
The question governing this course, would be the development of efficient algorithms. Hopefully, what is an
algorithm is a well understood concept. But what is an efficient algorithm? A natural answer (but not the only
one!) is an algorithm that runs quickly.
What do we mean by quickly? Well, we would like our algorithm to:
(A) Scale with input size. That is, it should be able to handle large and hopefully huge inputs.
(B) Low level implementation details should not matter, since they correspond to small improvements in
performance. Since faster CPUs keep appearing it follows that such improvements would (usually) be
taken care of by hardware.
(C) What we will really care about are asymptotic running time. Explicitly, polynomial time.
In our discussion, we will consider the input size to be n, and we would like to bound the overall running
time by a function of n which is asymptotically as small as possible. An algorithm with better asymptotic
running time would be considered to be better.
Example 1.1.1. It is illuminating to consider a concrete example. So assume we have an algorithm for a problem
that needs to perform c2n operations to handle an input of size n, where c is a small constant (say 10). Let
assume that we have a CPU that can do 109 operations a second. (A somewhat conservative assumption, as
14
Input size n2 ops n3 ops n4 ops 2n ops n! ops
5 0 secs 0 secs 0 secs 0 secs 0 secs
20 0 secs 0 secs 0 secs 0 secs 16 mins
30 0 secs 0 secs 0 secs 0 secs 3 · 109 years
50 0 secs 0 secs 0 secs 0 secs never
60 0 secs 0 secs 0 secs 7 mins never
70 0 secs 0 secs 0 secs 5 days never
80 0 secs 0 secs 0 secs 15.3 years never
90 0 secs 0 secs 0 secs 15,701 years never
100 0 secs 0 secs 0 secs 107 years never
8000 0 secs 0 secs 1 secs never never
16000 0 secs 0 secs 26 secs never never
32000 0 secs 0 secs 6 mins never never
64000 0 secs 0 secs 111 mins never never
200,000 0 secs 3 secs 7 days never never
2,000,000 0 secs 53 mins 202.943 years never never
108 4 secs 12.6839 years 109 years never never
109 6 mins 12683.9 years 1013 years never never
Figure 1.1: Running time as function of input size. Algorithms with exponential running times can handle
only relatively small inputs. We assume here that the computer can do 2.5 · 1015 operations per second, and
the functions are the exact number of operations performed. Remember – never is a long time to wait for a
computation to be completed.
currently [Jan 2006]¬ , the blue-gene supercomputer can do about 3 · 1014 floating-point operations a second.
Since this super computer has about 131, 072 CPUs, it is not something you would have on your desktop any
time soon.) Since 210 ≈ 103 , you have that our (cheap) computer can solve in (roughly) 10 seconds a problem
of size n = 27.
But what if we increase the problem size to n = 54? This would take our computer about 3 million years to
solve. (It is better to just wait for faster computers to show up, and then try to solve the problem. Although
there are good reasons to believe that the exponential growth in computer performance we saw in the last 40
years is about to end. Thus, unless a substantial breakthrough in computing happens, it might be that solving
problems of size, say, n = 100 for this problem would forever be outside our reach.)
The situation dramatically change if we consider an algorithm with running time 10n2 . Then, in one second
our computer can handle input of size n = 104 . Problem of size n = 108 can be solved in 10n2 /109 = 1017−9 = 108
which is about 3 years of computing (but blue-gene might be able to solve it in less than 20 minutes!).
Thus, algorithms that have asymptotically a polynomial running time (i.e., the algorithms running time is
bounded by O(nc ) where c is a constant) are able to solve large instances of the input and can solve the problem
even if the problem size increases dramatically.
Can we solve all problems in polynomial time? The answer to this question is unfortunately no. There
are several synthetic examples of this, but it is believed that a large class of important problems can not be
solved in polynomial time.
Circuit Satisfiability
Instance: A circuit C with m inputs
Question: Is there an input for C such that C returns true for it.
¬ But the recently announced Super Computer that would be completed in 2012 in Urbana, is naturally way faster. It supposedly
would do 1015 operations a second (i.e., petaflop). Blue-gene probably can not sustain its theoretical speed stated above, which is
only slightly slower.
15
As a concrete example, consider the circuit depicted on the right. x1
Currently, all solutions known to Circuit Satisfiability require checking all x2
possibilities, requiring (roughly) 2m time. Which is exponential time and too x3
slow to be useful in solving large instances of the problem. x4
This leads us to the most important open question in theoretical computer x5
science: x x∧y x x∨y x x
y y
Question 1.1.2. Can one solve Circuit Satisfiability in polynomial time? And Or Not
The common belief is that Circuit Satisfiability can NOT be solved in polynomial time. Circuit Satisfiability
has two interesting properties.
(A) Given a supposed positive solution, with a detailed assignment (i.e., proof): x1 ← 0, x2 ← 1, ..., xm ← 1
one can verify in polynomial time if this assignment really satisfies C. This is done by computing what
every gate in the circuit what its output is for this input. Thus, computing the output of C for its input.
This requires evaluating the gates of C in the right order, and there are some technicalities involved, which
we are ignoring. (But you should verify that you know how to write a program that does that efficiently.)
Intuitively, this is the difference in hardness between coming up with a proof (hard), and checking that a
proof is correct (easy).
(B) It is a decision problem. For a specific input an algorithm that solves this problem has to output either
TRUE or FALSE.
Definition 1.2.2 (NP: Nondeterministic Polynomial time). Let NP be the class of all decision problems that can
be verified in polynomial time. Namely, for an input of size n, if the solution to the given instance is true, one
(i.e., an oracle) can provide you with a proof (of polynomial length!) that the answer is indeed TRUE for this
instance. Furthermore, you can verify this proof in polynomial time in the length of the proof.
See Figure 1.2 for the currently believed relationship between these classes (of course, as mentioned above,
P ⊆ NP and P ⊆ co-NP is easy to verify). Note, that it is quite possible that P = NP = co-NP, although
this would be extremely surprising.
Definition 1.2.4. A problem Π is NP-Hard, if being able to solve Π in polynomial time implies that P = NP.
16
Intuitively, being NP-Hard implies that a problem is ridiculously hard. Conceptually, it would imply that
proving and verifying are equally hard - which nobody that did CS 473 believes is true.
In particular, a problem which is NP-Hard is at least as hard as ALL the problems in NP, as such it is
safe to assume, based on overwhelming evidence that it can not be solved in polynomial time.
Theorem 1.2.6 (Cook’s Theorem). Circuit Satisfiability is NP-Hard.
Definition 1.2.7. A problem Π is NP-Complete (NPC in short) if it is both NP-Hard and in NP.
Clearly, Circuit Satisfiability is NP-Complete, since we can verify a positive solution in polynomial time in
the size of the circuit,
1.2.1. Reductions
Let A and B be two decision problems.
Given an input I for problem A, a reduction is a transformation of the input I into a new input I 0, such that
Thus, one can solve A by first transforming and input I into an input I 0 of B, and solving B(I 0).
This idea of using reductions is omnipresent, and used almost in any program you write.
Let T : I → I 0 be the input transformation that maps A into B. How fast is T? Well, for our nefarious
purposes we need polynomial reductions; that is, reductions that take polynomial time.
For example, given an instance of Circuit Satisfiability, we would like to generate an equivalent formula. We
will explicitly write down what the circuit computes in a formula form. To see how to do this, consider the
following example.
x1 y1 y4
y5 y1 = x1 ∧ x4 y2 = x4 y3 = y2 ∧ x3
x2
y4 = x2 ∨ y1 y5 = x2 y6 = x5
x3 y7 y8 y7 = y3 ∨ y5 y8 = y4 ∧ y7 ∧ y6 y8
x4 y3
y2
x5 y6
We introduced a variable for each wire in the circuit, and we wrote down explicitly what each gate computes.
Namely, we wrote a formula for each gate, which holds only if the gate computes correctly the output for its
given input.
17
The circuit is satisfiable if and only if there is an Input: boolean circuit C
assignment such that all the above formulas hold. Al- ⇓ O(size o f C)
ternatively, the circuit is satisfiable if and only if the transform C into boolean formula F
following (single) formula is satisfiable ⇓
(y1 = x1 ∧ x4 ) ∧ (y2 = x4 ) ∧ (y3 = y2 ∧ x3 ) Find SAT assign’ for F using SAT solver
∧(y4 = x2 ∨ y1 ) ∧ (y5 = x2 )
⇓
∧(y6 = x5 ) ∧ (y7 = y3 ∨ y5 )
Return TRUE if F is sat’, otherwise FALSE.
∧(y8 = y4 ∧ y7 ∧ y6 ) ∧ y8 .
Figure 1.4: Algorithm for solving CSAT using
It is easy to verify that this transformation can be done an algorithm that solves the SAT problem
in polynomial time.
The resulting reduction is depicted in Figure 1.4.
Namely, given a solver for SAT that runs in TSAT (n),
we can solve the CSAT problem in time
where n is the size of the input circuit. Namely, if we have polynomial time algorithm that solves SAT then we
can solve CSAT in polynomial time.
Another way of looking at it, is that we believe that solving CSAT requires exponential time; namely,
TCSAT (n) ≥ 2n . Which implies by the above reduction that
Namely, TSAT (n) ≥ 2n/c −O(n), where c is some positive constant. Namely, if we believe that we need exponential
time to solve CSAT then we need exponential time to solve SAT.
This implies that if SAT ∈ P then CSAT ∈ P.
We just proved that SAT is as hard as CSAT. Clearly, SAT ∈ NP which implies the following theorem.
Theorem 1.2.10. SAT (formula satisfiability) is NP-Complete.
Definition 1.3.1. 3CNF formula is a CNF formula with exactly three literals in each clause.
The problem 3SAT is formula satisfiability when the formula is restricted to be a 3CNF formula.
Theorem 1.3.2. 3SAT is NP-Complete.
18
(A) Make sure every AND/OR gate has only two inputs. If (say) an AND gate have more inputs, we replace
it by cascaded tree of AND gates, each one of degree two.
(B) Write down the circuit as a formula by traversing the circuit, as was done for SAT. Let F be the resulting
formula.
A clause corresponding to a gate in F will be of the following forms: (i) a = b ∧ c if it corresponds to an
AND gate, (ii) a = b ∨ c if it corresponds to an OR gate, and (iii) a = b if it corresponds to a NOT gate.
Notice, that except for the single clause corresponding to the output of the circuit, all clauses are of this
form. The clause that corresponds to the output is a single variable.
(C) Change every gate clause into several CNF clauses.
(i) For example, an AND gate clause of the form a = b ∧ c will be translated into
a ∨ b ∨ c ∧ (a ∨ b) ∧ (a ∨ c). (1.1)
Note that Eq. (1.1) is true if and only if a = b ∧ c is true. Namely, we can replace the clause a = b ∧ c
in F by Eq. (1.1).
(ii) Similarly, an OR gate clause the form a = b ∨ c in F will be transformed into
(a ∨ b ∨ c) ∧ (a ∨ b) ∧ (a ∨ c).
(a ∨ b) ∧ (a ∨ b).
(D) Make sure every clause is exactly three literals. Thus, a single variable clause a would be replaced by
(a ∨ x ∨ y) ∧ (a ∨ x ∨ y) ∧ (a ∨ x ∨ y) ∧ (a ∨ x ∨ y),
by introducing two new dummy variables x and y. And a two variable clause a ∨ b would be replaced by
(a ∨ b ∨ y) ∧ (a ∨ b ∨ y),
This completes the reduction, and results in a new 3CNF formula G which is satisfiable if and only if the original
circuit C is satisfiable. The reduction is depicted in Figure 1.5. Namely, we generated an equivalent 3CNF to
the original circuit. We conclude that if T3SAT (n) is the time required to solve 3SAT then
which implies that if we have a polynomial time algorithm for 3SAT, we would solve CSAT is polynomial time.
Namely, 3SAT is NP-Complete.
19
1.4. Bibliographical Notes
Cook’s theorem was proved by Stephen Cook (http://en.wikipedia.org/wiki/Stephen_Cook). It was proved
independently by Leonid Levin (http://en.wikipedia.org/wiki/Leonid_Levin) more or less in the same
time. Thus, this theorem should be referred to as the Cook-Levin theorem.
The standard text on this topic is [GJ90]. Another useful book is [ACG+ 99], which is a more recent and
more updated, and contain more advanced stuff.
Chapter 2
NP Completeness II
2.1. Max-Clique
MaxClique
Instance: A graph G
Question: What is the largest number of nodes in G forming
a complete subgraph? Figure 2.1: A clique of size 4
inside a graph with 8 vertices.
20
How to prove that a problem X is NP-Hard? Proving that a given problem X is NP-Hard is usually
done in two steps. First, we pick a known NP-Complete problem A. Next, we show how to solve any instance
of A in polynomial time, assuming that we are given a polynomial time algorithm that solves X.
Proving that a problem X is NP-Complete requires the additional burden of showing that is in NP. Note,
that only decision problems can be NP-Complete, but optimization problems can be NP-Hard; namely, the
set of NP-Hard problems is much bigger than the set of NP-Complete problems.
Theorem 2.1.2. MaxClique is NP-Hard.
Proof: We show a reduction from 3SAT. So, consider an input to 3SAT, which is a formula F defined over n
variables (and with m clauses).
We build a graph from the formula F by scanning it, as follows: a b c
21
(a) (b) (c)
Figure 2.3: (a) A clique in a graph G, (b) the complement graph is formed by all the edges not appearing in G,
and (c) the complement graph and the independent set corresponding to the clique in G.
Proof: It is NP-Hard by the reduction of Theorem 2.1.2. Thus, we only need to show that it is in NP. This
is quite easy. Indeed, given a graph G having n vertices, a parameter k, and a set W of k vertices, verifying
that every pair of vertices in W form an edge in G takes O(u + k 2 ), where u is the size of the input (i.e., number
of edges + number of vertices). Namely, verifying a positive answer to an instance of Clique can be done in
polynomial time.
Thus, Clique is NP-Complete.
Independent Set
Instance: A graph G, integer k
Question: Is there an independent set in G of size k?
Proof: This readily follows by a reduction from Clique. Given G and k, compute the complement graph G where
we connected two vertices u, v in G iff they are independent (i.e., not connected) in G. See Figure 2.3. Clearly,
a clique in G corresponds to an independent set in G, and vice versa. Thus, Independent Set is NP-Hard, and
since it is in NP, it is NPC.
Vertex Cover
Instance: A graph G, integer k
Question: Is there a vertex cover in G of size k?
22
Lemma 2.3.2. A set S is a vertex cover in G iff V \ S is an independent set in G.
Proof: If S is a vertex cover, then consider two vertices u, v ∈ V \ S. If uv ∈ E(G) then the edge uv is not covered
by S. A contradiction. Thus V \ S is an independent set in G.
Similarly, if V \ S is an independent set in G, then for any edge uv ∈ E(G) it must be that either u or v are
not in V \ S. Namely, S covers all the edges of G.
Proof: Vertex Cover is in NP as can be easily verified. To show that it NP-Hard we will do a reduction from
Independent Set. So, we are given an instance of Independent Set which is a graph G and parameter k, and we
want to know whether there is an independent set in G of size k. By Lemma 2.3.2, G has an independent set
of k iff it has a vertex cover of size n − k. Thus, feeding G and n − k into (the supposedly given) black box that
can solves vertex cover in polynomial time, we can decide if G has an independent set of size k in polynomial
time. Thus Vertex Cover is NP-Complete.
Usually, we would like to color a graph with a minimum number of colors. Deciding if a graph can be colored
with two colors is equivalent to deciding if a graph is bipartite and can be done in linear time using DFS or
BFS¬ .
Coloring is useful for resource allocation (used in compilers for example) and scheduling type problems.
Surprisingly, moving from two colors to three colors make the problem much harder.
3Colorable
Instance: A graph G.
Question: Is there a coloring of G using three colors?
23
a u
a u a u
w r
w r w r
b v
T b v b v
T T
c s
c s c s
(1) a ⇐ F, b ⇐ F, c ⇐ F (2) a ⇐ F, b ⇐ F, c ⇐ T (3) a ⇐ F, b ⇐ T, c ⇐ F
a u a u a u
w r w r w r
b v b v b v
T T T
c s c s c s
(4) a ⇐ F, b ⇐ T, c ⇐ T (5) a ⇐ T, b ⇐ F, c ⇐ F (6) a ⇐ T, b ⇐ F, c ⇐ T
a u a u
w r w r
b v b v
T T
c s c s
(7) a ⇐ F, b ⇐ T, c ⇐ T (8) a ⇐ T, b ⇐ F, c ⇐ F
Figure 2.4: The clause a ∨ b ∨ c and all the three possible colorings to its literals. If all three literals are colored
by the color of the special node F, then there is no valid coloring of this component, see case (1).
For every variable y appearing in F , we will generate a variable gadget, which is (again)
X
a triangle including two new vertices, denoted by y and y, and the third vertex is the auxiliary
vertex X from the color generating gadget. Note, that in a valid 3-coloring of the resulting
graph either y would be colored by T (i.e., it would be assigned the same color as the color as y y
the vertex T) and y would be colored by F, or the other way around. Thus, a valid coloring
could be interpreted as assigning TRUE or FALSE value to each variable y, by just inspecting the color used for
coloring the vertex y.
Finally, for every clause we introduce a clause gadget. See the figure on the a u
right – for how the gadget looks like for the clause a ∨ b ∨ c. Note, that the vertices w r
marked by a, b and c are the corresponding vertices from the corresponding variable b v
T
gadget. We introduce five new variables for every such gadget. The claim is that this
s
gadget can be colored by three colors if and only if the clause is satisfied. This can c
be done by brute force checking all 8 possibilities, and we demonstrate it only for two cases. The reader should
verify that it works also for the other cases.
Indeed, if all three vertices (i.e., three variables in a clause) on the left side of a a u
variable clause are assigned the F color (in a valid coloring of the resulting graph), w r
then the vertices u and v must be either be assigned X and T or T and X, respectively, b v
T
in any valid 3-coloring of this gadget (see figure on the left). As such, the vertex w
c s
must be assigned the color F. But then, the vertex r must be assigned the X color.
But then, the vertex s has three neighbors with all three different colors, and there
is no valid coloring for s.
24
Figure 2.5: The formula (a ∨ b ∨ c) ∧ (b ∨ c ∨ d) ∧ (a ∨ c ∨ d) ∧ (a ∨ b ∨ d) reduces to the depicted graph.
As another example, consider the case when one of the variables on the left a u
is assigned the T color. Then the clause gadget can be colored in a valid way, w r
as demonstrated on the figure on the right. b v
T
This concludes the reduction. Clearly, the generated graph can be computed
in polynomial time. By the above argumentation, if there is a valid 3-coloring c s
of the resulting graph G, then there is a satisfying assignment for F . Similarly,
if there is a satisfying assignment for F then the G be colored in a valid way using three colors. For how the
resulting graph looks like, see Figure 2.5.
This implies that 3Colorable is NP-Complete.
Here is an interesting related problem. You are given a graph G as input, and you know that it is 3-colorable.
In polynomial time, what is the minimum number of colors you can use to color this graph legally? Currently,
the best polynomial time algorithm for coloring such graphs, uses O n3/14 colors.
Chapter 3
NP Completeness III
Definition 3.1.2. An Eulerian cycle is a cycle in a graph that uses every edge exactly once.
Finding Eulerian cycle can be done in linear time. Surprisingly, finding a Hamiltonian cycle is much harder.
25
Hamiltonian Cycle
Instance: A graph G.
Question: Is there a Hamiltonian cycle in G?
u
To overcome this problem, we will replace each e gate with a component that
guarantees, that if you visit all its vertices, you have to go forward and can
NOT go back (or change “lanes”). The new component is depicted on the left.
There only three possible ways to visit all the vertices of the components by
v
(v,u,1) (v,u,2) (v,u,3) (v,u,4) (v,u,5) (v,u,6) paths that do not start/end inside the component, and they are the following:
The proof that this is the only three possibilities is by brute force. De-
picted on the right is one impossible path, that tries to backtrack by entering
on the top and leaving on the bottom. Observe, that there are vertices left
unvisited. Which means that not all the vertices in the graph are going to be
visited, because we add the constraint, that the paths start/end outside the
gate-component (this condition would be enforced naturally by our final construction).
The resulting graph H1 for the example graph we started with is depicted
on the right. There exists a Vertex Cover in the original graph iff there exists k
paths that start on the left side and end on the right side, in this weird graph.
And these k paths visits all the vertices.
The final stroke is to add connection from the left side to the right side, such that once a a
you arrive to the right side, you can go back to the left side. However, we want connection b
b
that allow you to travel exactly k times. This is done by adding to the above graph a
“routing box” component H2 depicted on the right, with k new middle vertices. The ith c c
vertex on the left of the routing component is the left most vertex of the ith horizontal line d d
in the graph, and the ith vertex on the right of the component is the right most vertex of
the ith horizontal line in the graph. e e
It is now easy (but tedious) to verify that the resulting graph H1 ∪ H2 has a Hamiltonian path iff H1 has k
paths going from left to right, which happens, iff the original graph has a Vertex Cover of size k. It is easy to
verify that this reduction can be done in polynomial time.
26
3.2. Traveling Salesman Problem
A traveling salesman tour, is a Hamiltonian cycle in a graph, which its price is the price of all the edges it uses.
TSP
Instance: G = (V, E) a complete graph - n vertices, c(e): Integer cost function over the edges of G,
and k an integer.
Question: Is there a traveling-salesman tour with cost at most k?
How does one prove that a problem is NP-Complete? First, one has to choose an appropriate NPC to
reduce from. In this case, we will use 3SAT. Namely, we are given a 3CNF formula with n variables and m
clauses. The second stage, is to “play” with the problem and understand what kind of constraints can be
encoded in an instance of a given problem and understand the general structure of the problem.
The first observation is that we can use very long numbers as input to Subset Sum. The numbers can be of
polynomial length in the size of the input 3SAT formula F.
The second observation is that in fact, instead of thinking about Subset Sum as adding numbers, we can
think about it as a problem where we are given vectors with k components each, and the sum of the vectors
(coordinate by coordinate, must match. For example, the input might be the vectors (1, 2), (3, 4), (5, 6) and the
target vector might be (6, 8). Clearly, (1, 2) + (5, 6) give the required target vector. Lets refer to this new problem
as Vec Subset Sum.
Vec Subset Sum
Instance: S - set of n vectors of dimension k, each vector has non-negative numbers for its coordinates,
→
−
and a target vector t .
Question: Is there a subset X ⊆ S such that →
Í →− → −
−x ∈X x = t ?
Given an instance of Vec Subset Sum, we can covert it into an instance of Subset Sum as follows: We
compute the largest number in the given instance, multiply it by n2 · k · 100, and compute how many digits are
required to write this number down. Let U be this number of digits. Now, we take every vector in the given
instance and we write it down using U digits, padding it with zeroes as necessary. Clearly, each vector is now
converted into a huge integer number. The property is now that a sub of numbers in a specific column of the
given instance can not spill into digits allocated for a different column since there are enough zeroes separating
the digits corresponding to two different columns.
27
Next, let us observe that we can force the solution (if it exists) for Vec Target ?? ?? 01 ???
Subset Sum to include exactly one vector out of two vectors. To this end, we a1 ?? ?? 01 ??
will introduce a new coordinate (i.e., a new column in the table on the right) a2 ?? ?? 01 ??
for all the vectors. The two vectors a1 and a2 will have 1 in this coordinate,
and all other vectors will have zero in this coordinate. Finally, we set this coordinate in the target vector to
be 1. Clearly, a solution is a subset of vectors that in this coordinate add up to 1. Namely, we have to choose
either a1 or a2 into our solution.
In particular, for each variable x appearing in F, we will introduce two rows, denoted by x and x and
introduce the above mechanism to force choosing either x or x to the optimal solution. If x (resp. x) is chosen
into the solution, we will interpret it as the solution to F assigns TRUE (resp. FALSE) to x.
Next, consider a clause C ≡ a ∨ b ∨ c.appearing in F. This clause numbers ... C ≡ a ∨ b ∨ c ...
requires that we choose at least one row from the rows corresponding a ... 01 ...
to a, b to c. This can be enforced by introducing a new coordinate a ... 00 ...
for the clauses C, and setting 1 for each row that if it is picked then b ... 01 ...
the clauses is satisfied. The question now is what do we set the target
b ... 00 ...
to be? Since a valid solution might have any number between 1 to
c ... 00 ...
3 as a sum of this coordinate. To overcome this, we introduce three
c ... 01 ...
new dummy rows, that store in this coordinate, the numbers 7, 8
C fix-up 1 000 07 000
and 9, and we set this coordinate in the target to be 10. Clearly, if
C fix-up 2 000 08 000
we pick to dummy rows into the optimal solution then sum in this
C fix-up 3 000 09 000
coordinate would exceed 10. Similarly, if we do not pick one of these
three dummy rows to the optimal solution, the maximum sum in this TARGET 10
coordinate would be 1 + 1 + 1 = 3, which is smaller than 10. Thus, the only possibility is to pick one dummy row,
and some subset of the rows such that the sum is 10. Notice, this “gadget” can accommodate any (non-empty)
subset of the three rows chosen for a, b and c.
We repeat this process for each clause of F. We end up with a set U of 2n + 3m vectors with n + m coordinate,
and the question if there is a subset of these vectors that add up to the target vector. There is such a subset
if and only if the original formula F is satisfiable, as can be easily verified. Furthermore, this reduction can be
done in polynomial time.
Finally, we convert these vectors into an instance of Subset Sum. Clearly, this instance of Subset Sum has a
solution if and only if the original instance of 3SAT had a solution. Since Subset Sum is in NP as an be easily
verified, we conclude that that Subset Sum is NP-Complete.
3DM
Instance: X,Y, Z sets of n elements, and T a set of triples, such that (a, b, c) ∈ T ⊆ X × Y × Z.
Question: Is there a subset S ⊆ T of n disjoint triples, s.t. every element of X ∪ Y ∪ Z is covered
exactly once.?
28
numbers
numbers a∨a b∨b c∨c d∨d D ≡ b∨c∨d C ≡ a∨b∨c
010000000001
a 1 0 0 0 00 01
010000000000
a 1 0 0 0 00 00
000100000001
b 0 1 0 0 00 01
000100000100
b 0 1 0 0 01 00 000001000100
c 0 0 1 0 01 00 000001000001
c 0 0 1 0 00 01 000000010000
d 0 0 0 1 00 00 000000010101
d 0 0 0 1 01 01 000000000007
C fix-up 1 0 0 0 0 00 07 000000000008
C fix-up 2 0 0 0 0 00 08 000000000009
C fix-up 3 0 0 0 0 00 09 000000000700
D fix-up 1 0 0 0 0 07 00 000000000800
D fix-up 2 0 0 0 0 08 00 000000000900
D fix-up 3 0 0 0 0 09 00
010101011010
TARGET 1 1 1 1 10 10
Figure 3.1: The Vec Subset Sum instance generated for the 3SAT formula F = b ∨ c ∨ d ∧ (a ∨ b ∨ c) is shown
on the left. On the right side is the resulting instance of Subset Sum.
3.5. Partition
Partition
Instance: A set S of n numbers.
Question: Is there a subset T ⊆ S s.t. t ∈T t = s ∈S\T s.?
Í Í
Proof: Partition is in NP, as we can easily verify that such a partition is valid.
Reduction from Subset Sum. Let the given instance be n numbers a1, . . . , an and a target number t. Let
S = i= ai , and set an+1 = 3S − t and an+2 = 3S − (S − t) = 2S + t. It is easy to verify that there is a solution to
Ín
the given instance of subset sum, iff there is a solution to the following instance of partition:
a1, . . . , an, an+1, an+2 .
Clearly, Partition is in NP and thus it is NP-Complete.
SET COVER
Instance: (S, F, k):
S: A set of n elements
F: A family of subsets of S, s.t. X ∈F X = S.
Ð
k: A positive integer.
Question: Are there k sets S1, . . . , Sk ∈ F that cover S. Formally, i Si = S?
Ð
29
Part II
Dynamic programming
Chapter 4
Dynamic programming
The events of 8 September prompted Foch to draft the later legendary signal: “My centre is giving way, my right is
in retreat, situation excellent. I attack.” It was probably never sent.
– – The first world war, John Keegan..
Suggestion 4.1.2. Recursive algorithms are one of the main tools in developing algorithms (and writing pro- T IP
grams). If you do not feel comfortable with recursive algorithms you should spend time playing with recursive
algorithms till you feel comfortable using them. Without the ability to think recursively, this class would be a
long and painful torture to you. Speak with me if you need guidance on this topic.
30
The resulting algorithm is depicted on the right. We PartitionsI(num, d) //d-max digit
are interested in analyzing its running time. To this end, if (num ≤ 1) or (d = 1)
draw the recursion tree of Partitions and observe that return 1
the amount of work spend at each node, is proportional if d > num
to the number of children it has. Thus, the overall time d ← num
spend by the algorithm is proportional to the size of res ← 0
the recurrence tree, which is proportional (since every for i ← d down to 1
node is either a leaf or has at least two children) to the res = res + PartitionsI(num − i,i)
number of leafs in the tree, which is Θ(p(n)). return res
√ This is not very exciting, since it is easy verify that
3 n/4 ≤ p(n) ≤ nn . Partitions(n)
return PartitionsI(n, n)
Exercise 4.1.3. Prove the above bounds on p(n) (or better bounds).
Suggestion 4.1.4. Exercises in the class notes are a natural easy questions for inclusions in exams. You probably
T IP
want to spend time doing them.
√
e π 2n/3
Hardy and Ramanujan (in 1918) showed that p(n) ≈ √ (which I am sure was your first guess).
4n 3
It is natural to ask, if there is a faster algorithm. Or more specifically, why is the algorithm Partitions
so slowwwwwwwwwwwwwwwwww? The answer is that during the computation of Partitions(n) the function
PartitionsI(num, max_digit) is called a lot of times with the same parameters.
An easy way to overcome this problem is cache the PartitionsI_C(num, max_digit)
results of PartitionsI using a hash table.¬ Whenever if (num ≤ 1) or (max_digit = 1)
PartitionsI is being called, it checks in a cache table if return 1
it already computed the value of the function for this if max_digit > num
parameters, and if so it returns the result. Otherwise, it d ← num
computes the value of the function and before returning if hnum, max_digiti in cache
the value, it stores it in the cache. This simple (but return cache(hnum, max_digiti)
powerful) idea is known as memoization. res ← 0
What is the running time of PartitionS_C? Analyz- for i ← max_digit down to 1 do
ing recursive algorithm that have been transformed by res += PartitionsI_C(num − i,i)
memoization are usually analyzed as follows: (i) bound cache(hnum, max_digiti) ← res
the number of values stored in the hash table, and (ii) return res
bound the amount of work involved in storing one value
into the hash table (ignoring recursive calls). PartitionS_C(n)
Here is the argument in this case: return PartitionsI_C(n, n)
(A) If a call to PartitionsI_C takes (by itself) more than constant time, then this call performs a store in the
cache.
(B) Number of store operations in the cache is O(n2 ), since this is the number of different entries stored in the
cache. Indeed, for PartitionsI_C(num, max_digit), the parameters num and max_digit are both integers
in the range 1, . . . , n.
(C) We charge the work in the loop to the resulting store. The work in the loop is at most O(n) time (since
max_digit ≤ n).
(D) As such, the overall running time of PartitionS_C(n) is O n2 × O(n) = O n3 .
31
4.1.1. A Short sermon on memoization
This idea of memoization is generic and nevertheless very useful. To recap, it works by taking a recursive
function and caching the results as the computations goes on. Before trying to compute a value, check if it was
already computed and if it is already stored in the cache. If so, return result from the cache. If it is not in the
cache, compute it and store it in the cache (for the time being, you can think about the cache as being a hash
table).
• When does it work: There is a lot of inefficiency in the computation of the recursive function because
the same call is being performed repeatedly.
• When it does NOT work:
(A) The number of different recursive function calls (i.e., the different values of the parameters in the
recursive call) is “large”.
(B) When the function has side effects.
Tidbit 4.1.5. Some functional programming languages allow one to take a recursive function f (·) that you already
tidbit
implemented and give you a memorized version f 0(·) of this function without the programmer doing any extra
work. For a nice description of how to implement it in Scheme see [ASS96].
It is natural to ask if we can do better than just using caching? As usual in life – more pain, more gain.
Indeed, in a lot of cases we can analyze the recursive calls, and store them directly in an (sometime multi-
dimensional) array. This gets rid of the recursion (which used to be an important thing long time ago when
memory, used by the stack, was a truly limited resource, but it is less important nowadays) which usually yields
a slight improvement in performance in the real world.
This technique is known as dynamic programming . We can sometime save space and improve running
time in dynamic programming over memoization.
“The Fibonacci sequence appears in Indian mathematics, in connection with Sanskrit prosody.
In the Sanskrit oral tradition, there was much emphasis on how long (L) syllables mix with the
short (S), and counting the different patterns of L and S within a given fixed length results in the
Fibonacci numbers; the number of patterns that are m short syllables long is the Fibonacci number
Fm+1 .”
(To see that, imagine that a long syllable is equivalent in length to two short syllables.) Surprisingly, the credit
for this formalization goes back more than 2000 years (!)
As usual in life, it is not dynamic, it is not programming, and its hardly a technique. To overcome this, most texts find creative
ways to present this topic in the most opaque way possible.
32
FibDP(n)
if n ≤ 1
return 1
if F[n] initialized
return F[n]
F[n] ⇐=FibDP(n − 1)+FibDP(n − 2)
return F[n]
Figure 4.1
Fibonacci was a decent mathematician (1170—1250 AD), and his most significant and lasting contribution
was spreading the Hindu-Arabic numerical system (i.e., zero) in Europe. He was the son of a rich merchant that
spend much time growing up in Algiers, where he learned the decimal notation system. He traveled throughout
the Mediterranean world to study mathematics. When he came back to Italy he published a sequence of books
(the first one “Liber Abaci” contained the description of the decimal notations system). In this book, he also
posed the following problem:
Consider a rabbit population, assuming that: A newly born pair of rabbits, one male, one female,
are put in a field; rabbits are able to mate at the age of one month so that at the end of its second
month a female can produce another pair of rabbits; rabbits never die and a mating pair always
produces one new pair (one male, one female) every month from the second month on. The puzzle
that Fibonacci posed was: how many pairs will there be in one year?
(The above is largely based on Wikipedia.)
33
h a r – p e l e d
s h a r p <space> e y e d
1 0 0 0 1 0 1 0 1 0 0
– l
Insert: delete: replace:
s y
e
(still) insert: ignore:
<space> e
Figure 4.2: Interpreting edit-distance as a alignment task. Aligning identical characters to each other is free of
cost. The price in the above example is 4. There are other ways to get the same edit-distance in this case.
As such,
2 n−3
Fn−1 Fn−2 Fn−3 F2
0 1 0 1 0 1
= = = .
Fn 1 1 Fn−1 1 1 Fn−2 1 1 F1
Thus, computing the nth Fibonacci number can be done by computing
n−3
0 1
.
1 1
How to this quickly? Well, we know that a∗b∗c = (a∗b)∗c = FastExp(a, n)
a∗(b∗c)® , as such one can compute a n by repeated squaring, if n = 0 then
see pseudo-code on the right. The running time of FastExp return 1
is O(log n) as can be easily verified. Thus, we can compute in if n = 1 then
Fn in O(log n) time. return a
But, something is very strange. Observe that Fn has ≈ if n is even then
log10 1.68... n = Θ(n) digits. How can we compute a number return (FastE xp(a, n/2))2
that is that large in logarithmic time? Well, we assumed else
that the time to handle a number is O(1) independent of its 2
return a ∗ FastExp a, n−1
2
size. This is not true in practice if the numbers are large.
Naturally, one has to be very careful with such assumptions.
34
But how do we compute the edit-distance (min ed(A[1..m], B[1..n])
# of edit operations needed)? if m = 0 return n
The idea is to list the edit operations from left to if n = 0 return m
right. Then edit distance turns into a an alignment pinsert = ed(A[1..m], B[1..(n − 1)]) + 1
problem. See Figure 4.2. pdelete = ed(A[1..(m − 1)], B[1..n]) + 1
In particular, the idea of the recursive algorithm pr/i = ed(A[1..(m
is to inspect the last character and decide which of − 1)], B[1..(n
− 1)] )
+ A[m] , B[n]
the categories it falls into: insert, delete or ignore. return min pinsert , pdelete, pr eplace/ignor e
See pseudo-code on the right.
The running time of ed(...)? Clearly exponential, and roughly 2n+m , where n + m is the size of the input.
So how many different recursive calls ed performs? Only:O(m ∗ n) different calls, since the only parameters
that matter are n and m.
So the natural thing is to introduce edM(A[1..m], B[1..n])
memoization. The resulting algorithm edM if m = 0 return n
is depicted on the right. The running if n = 0 return m
time of edM(n, m) when executed on two if T[m, n] is initialized then return T[m, n]
strings of length n and m respective is pinsert = edM(A[1..m], B[1..(n − 1)]) + 1
O(nm), since there are O(nm) store opera- pdelete = edM(A[1..(m − 1)], B[1..n]) + 1
tions in the cache, and each store requires pr/i = edM(A[1..(m − 1)], B[1..(n − 1)]) + A[m] , B[n]
O(1) time (by charging one for each recur- T[m, n] ← min pinsert , pdelete, pr eplace/ignor e
sive call). Looking on the entry T[i, j] in return T[m, n]
the table, we realize that it depends only
on T[i − 1, j], T[i, j − 1] and T[i − 1, j − 1]. Thus, instead of recursive algorithm, we can fill the table T row by
row, from left to right.
edDP(A[1..m], B[1..n]) The dynamic programming version that
for i = 1 to m do T[i, 0] ← i uses a two dimensional array is pretty sim-
for j = 1 to n do T[0, j] ← j ple now to derive and is depicted on the
for i ← 1 to m do left. Clearly, it requires O(nm) time, and
for j ← 1 to n do O(nm) space. See the pseudo-code of the
pinsert = T[i, j − 1] + 1 resulting algorithm edDP on the left.
pdelete = T[i − 1, j] + 1 It is enlightening to think about the
pr/ignor e = T[i − 1. j − 1] + A[i] , B[ j]
algorithm as computing for each T[i, j] the
T[i, j] ← min pinsert , pdelete, pr/ignor e
cell it got the value from. What you get is
return T[m, n] a tree encoded in the table. See Figure 4.3.
It is now easy to extract from the table the
sequence of edit operations that realizes the minimum edit distance between A and B. Indeed, we start a walk
on this graph from the node corresponding to T[n, m]. Every time we walk left, it corresponds to a deletion,
every time we go up, it corresponds to an insertion, and going sideways corresponds to either replace/ignore.
Note, that when computing the ith row of T[i, j], we only need to know the value of the cell to the left of
the current cell, and two cells in the row above the current cell. It is thus easy to verify that the algorithm
needs only the remember the current and previous row to compute the edit distance. We conclude:
Theorem 4.3.1. Given two strings A and B of length n and m, respectively, one can compute their edit distance
in O(nm). This uses O(nm) space if we want to extract the sequence of edit operations, and O(n + m) space if we
only want to output the price of the edit distance.
Exercise 4.3.2. Show how to compute the sequence of edit-distance operations realizing the edit distance using
only O(n + m) space and O(nm) running time. (Hint: Use a recursive algorithm, and argue that the recursive
call is always on a matrix which is of size, roughly, half of the input matrix.)
35
A L G O R I T H M
0 ← 1 ← 2 ← 3 ← 4 ← 5 ← 6 ← 7 ← 8 ← 9
↑ v
A 1 0 ← 1 ← 2 ← 3 ← 4 ← 5 ← 6 ← 7 ← 8
↑ ↑ v
L 2 1 0 ← 1 ← 2 ← 3 ← 4 ← 5 ← 6 ← 7
↑ ↑ ↑ v -
T 3 2 1 1 ← 2 ← 3 ← 4 4 ← 5 ← 6
↑ ↑ ↑ ↑ v -
R 4 3 2 2 2 2 ← 3 ← 4 ← 5 ← 6
↑ ↑ ↑ - - v -
U 5 4 3 3 3 3 3 ← 4 ← 5 ← 6
↑ ↑ ↑ - - - v
I 6 5 4 4 4 4 3 ← 4 ← 5 ← 6
↑ ↑ ↑ ↑ ↑ ↑ ⇑
S 7 6 5 5 5 5 4 ← 4 ← 5 ← 6
↑ ↑ ↑ ↑ ↑ ↑ ↑ v
T 8 7 6 6 6 6 5 4 ← 5 ← 6
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ v
I 9 8 7 7 7 7 6 5 5 ← 6
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ v
C 10 9 8 8 8 8 7 6 6 6
36
Chapter 5
“No, mademoiselle, I don’t capture elephants. I content myself with living among them. I like them. I like looking at
them, listening to them, watching them on the horizon. To tell you the truth, I’d give anything to become an elephant
myself. That’ll convince you that I’ve nothing against the Germans in particular: they’re just men to me, and that’s
enough.”
– – The roots of heaven, Romain Gary.
Problem 5.1.1. Given a set of n (sorted) keys A[1 . . . n], build the 12 32
best binary search tree for the elements of A.
4 32 12 45
Note, that we store the values in the internal node of the binary
trees. The figure on the right shows two possible search trees for the
21 45 4 21
same set of numbers. Clearly, if we are accessing the number 12 all
the time, the tree on the left would be better to use than the tree on Two possible search trees for the set
the right. A = [4, 12, 21, 32, 45].
Usually, we just build a balanced binary tree, and this is good
enough. But assume that we have additional information about what is the frequency in which we access the
element A[i], for i = 1, . . . , n. Namely, we know that A[i] is going be accessed f [i] times, for i = 1, . . . , n.
Õn
In this case, we know that the total search time for a tree T is S(T) = (depth(T, i) + 1)f [i], where depth(T, i)
i=1
is the depth of the node in T storing the value A[i]. Assume that A[r] is the value stored in the root of the tree
T. Clearly, all the values smaller than A[r] are in the subtree leftT , and all values larger than r are in rightT .
Thus, the total search time, in this case, is
price of access to root
z }| {
r−1
Õ Õn n
Õ
S(T) = (depth(leftT , i) + 1)f [i] + f [i] + (depth(rightT , i) + 1)f [i].
i=1 i=1 i=r+1
Observe, that if T is the optimal search tree for the access frequencies f [1], . . . , f [n], then the subtree leftT
must be optimal for the elements accessing it (i.e., A[1 . . . r − 1] where r is the root).
Thus, the price of T is
Õn
S(T) = S(leftT ) + S(rightT ) + f [i],
i=1
where S(Q) is the price of searching in Q for the frequency of elements stored in Q.
37
Figure 5.1: A polygon and two possible triangulations of the polygon.
A further improvement raises from the fact that the root location is “monotone”. Formally, if R[i, j] denotes
the location of the element stored in the root for the elements A[i . . . j] then it holds that R[i, j − 1] ≤ R[i, j] ≤
R[i, j +1]. This limits the search space, and we can be more efficient in the search. This leads to O n2 algorithm.
Details are in Jeff Erickson class notes.
Observation 5.2.2. Any triangulation of a convex polygon with n vertices is made out of exactly n−2 triangles.
38
Our purpose is to find the triangulation of P that has the minimum
total length. Namely, the total length of diagonals used in the triangu-
lation is minimized. We would like to compute the optimal triangulation
using divide and conquer. As the figure on the right demonstrate, there
is always a triangle in the triangulation, that breaks the polygon into
two polygons. Thus, we can try and guess such a triangle in the optimal
triangulation, and recurse on the two polygons such created. The only
difficulty, is to do this in such a way that the recursive subproblems can
be described in succinct way.
4
5 To this end, we assume that the polygon is specified as list of vertices 1 . . . n
in a clockwise ordering. Namely, the input is a list of the vertices of the polygon,
3
for every vertex, the two coordinates are specified. The key observation, is that
in any triangulation of P, there exist a triangle that uses the edge between
6 vertex 1 and n (red edge in figure on the left).
2
In particular, removing the triangle using the edge 1 − n leaves us with two
7
polygons which their vertices are consecutive along the original polygon.
Problem 5.3.1. The input is n matrices M1, . . . , Mn such that Mi is of size D[i − 1] × D[i] (i.e., Mi has D[i − 1]
rows and D[i] columns), where D[0 . . . n] is array specifying the sizes. Find the ordering of multiplications to
compute M1 · M2 · · · Mn−1 · Mn most efficiently.
39
Again, let us define a recurrence for this problem, where M[i, j] is the amount of work involved in computing
the product of the matrices Mi · · · M j . We have
0 j =i
M[i, j] = D[i − 1] · D[i] · D[i + 1] j =i+1
mini ≤k< j (M[i, k] + M[k + 1, j] + D[i − 1] · D[k] · D[ j])
j > i + 1.
Again, using memoization (or dynamic programming), one can compute M[1, n], in O(n3 ) time, using O(n2 )
space.
The length of the longest increasing subsequence is maxi=1 n M[i]. Again, using dynamic programming, we
get an algorithm with running time O(n2 ) for this problem. It is also not hard to modify the algorithm so that
it outputs this sequence (you should figure out the details of this modification). A better O(n log n) solution is
possible using some data-structure magic.
Assume you have a string S = ”Magna Carta” and a pattern P = ”?ag ∗ at ∗ a ∗ ” where “?” can match a
single character, and “*” can match any substring. You would like to decide if the pattern matches the string.
We are interested in solving this problem using dynamic programming. This is not too hard since this is
similar to the edit-distance problem that was already covered.
40
Figure 5.2: A drawing of the Mona Lisa by solving a TSP instance. The figure on the right is the TSP in the
eyes region.
41
Can we compute opt(S) recursively?
(A) Say v ∈ S. What are the two neighbors of v in optimum tour in S?
(B) If u, w are neighbors of v in an optimum tour of S then removing v gives an optimum path from u to w
visiting all nodes in S − {v}.
Path from u to w is not a recursive subproblem! Need to find a more general problem to allow recursion.
TSP Path
Instance: A graph G = (V, E) with non-negative edge costs/lengths(c(e) for edge e) and two nodes
s, t.
Question: Find a path from s to t of minimum cost that visits each node exactly once.
A Recursive Solution.
(A) opt(u, v, S) = minw ∈S,w,u,v (c(u, w) + opt(w, v, S − {u}))
(B) What are the subproblems for the original problem opt(s, t, V)? For every subset S ⊆ V, we have the
subproblem opt(u, v, S) for u, v ∈ S.
As usual, we need to bound the number subproblems in the recursion:
(A) number of distinct subsets S of V is at most 2n
(B) number of pairs of nodes in a set S is at most n2
(C) hence number of subproblems is O(n2 2n )
Exercise 5.6.1. Show that one can compute TSP using above dynamic program in O(n3 2n ) time and O(n2 2n )
space.
Lemma 5.6.2. Given a graph G with n vertices, one can solve TSP in O(n3 2n ) time.
42
Part III
Approximation algorithms
Chapter 6
Approximation algorithms
VertexCoverMin
Instance: A graph G, and integer k.
Question: Return the smallest subset S ⊆ V(G), s.t. S touches all the edges of G.
For this problem, the greedy algorithm will always take the vertex with the high-
est degree (i.e., the one covering the largest number of edges), add it to the cover set,
remove it from the graph, and repeat. We will refer to this algorithm as GreedyVer-
texCover.
It is not too hard to see that this algorithm does not output the optimal vertex-
cover. Indeed, consider the graph depicted on the right. Clearly, the optimal solution
Figure 6.1: Example.
is the black vertices, but the greedy algorithm would pick the four yellow vertices.
This of course still leaves open the possibility that, while we do not get the optimal
vertex cover, what we get is a vertex cover which is “relatively good” (or “good enough”).
Definition 6.1.1. A minimization problem is an optimization problem, where we look for a valid solution that
minimizes a certain target function.
Example 6.1.2. In the VertexCoverMin problem the (minimization) target function is the size of the cover. For-
mally Opt(G) = minS ⊆V (G),S cover of G |S|.
The VertexCover(G) is just the set S realizing this minimum.
Definition 6.1.3. Let Opt(G) denote the value of the target function for the optimal solution.
43
Intuitively, a vertex-cover of size “close” to the optimal solution would be considered to be good.
Definition 6.1.4. Algorithm Alg for a minimization problem Min achieves an approximation factor α ≥ 1 if for
all inputs G, we have:
Alg(G)
≤ α.
Opt(G)
We will refer to Alg as an α-approximation algorithm for Min.
L R L R L R L R L R L R
Proof: Consider the graph G n above. The optimal solution is to pick all the vertices of L to the vertex cover,
which results in a cover of size n. On the other hand, the greedy algorithm picks the set R. We have that
n n j k n n
Õ Õ n Õ n Õ 1
|R| = |Ri | = ≥ −1 ≥ n − 2n = n(Hn − 2).
i=2 i=2
i i=2
i i=1
i
Here, Hn = = lg n + Θ(1) is the nth harmonic number. As such, the approximation ratio for GreedyVer-
Ín
i=1 1/i
|R| n(Hn − 2)
texCover is ≥ = = Ω(log n).
|L| n
44
Theorem 6.1.6. The greedy algorithm for VertexCover achieves Θ(log n) approximation, where n is the number
of vertices in the graph. Its running time is O(mn2 ).
Proof: The lower bound follows from Lemma 6.1.5. The upper bound follows from the analysis of the greedy
of Set Cover, which will be done shortly.
As for the running time, each iteration of the algorithm takes O(mn) time, and there are at most n iterations.
Theorem 6.1.7. ApproxVertexCover is a 2-approximation algorithm for VertexCoverMin that runs in O(n2 )
time.
Proof: Every edge picked by the algorithm contains at least one vertex of the optimal solution. As such, the
cover generated is at most twice larger than the optimal.
where ni is the number of subsets of the vertices of G of size exactly i. Observe that we do not require to know
K – the algorithm can just try all sizes of subsets, till it finds a solution. We thus get the following (not very
interesting result).
Lemma 6.2.1. Given a graph G = (V, E) with n vertices, one can solve VertexCoverMin in O nα+2 time, where
Definition 6.2.2. Let G = (V, E) be a graph. For a subset S ⊆ V, let GS be the induced subgraph over S.
Namely, it is a graph with the set of vertices being S. For any pair of vertices x, y ∈ V, we have that the edge
xy ∈ E(GS ) if and only if x y ∈ E(G), and x, y ∈ S.
45
fpVertexCoverInner (X, β)
// Computes minimum vertex cover for the induced graph GX
// β: size of VC computed so far.
if X = ∅ or GX has no edges then return β
e ← any edge uv of GX .
β1 = f pV ertexCover Inner(X \ {u, v} , β + 2)
// Only take u to the cover, but then we must also take
// all the vertices that are neighbors of v,
// to cover their edges with v
β2 = f pV ertexCover Inner X \ {u} ∪ NG X (v) , β + NG X (v)
Also, in the following, for a vertex v, let NG (v) denote the set of vertices of G that are adjacent to v.
Consider an edge e = uv in G. We know that either u or v (or both) must be in any vertex cover of G, so
consider the brute force algorithm for VertexCoverMin that tries all these possibilities. The resulting algorithm
algFPVertexCover is depicted in Figure 6.3.
Lemma 6.2.3. The algorithm algFPVertexCover (depicted in Figure 6.3) returns the optimal solution to the
given instance of VertexCoverMin.
Proof: It is easy to verify, that if the algorithm returns β then it found a vertex cover of size β. Since the depth
of the recursion is at most n, it follows that this algorithm always terminates.
Consider the optimal solution Y ⊆ V, and run the algorithm, where every stage of the recursion always pick
the option that complies with the optimal solution. Clearly, since in every level of the recursion at least one
vertex of Y is being found, then after O(|Y |) recursive calls, the remaining graph would have no edges, and it
would return |Y | as one of the candidate solution. Furthermore, since the algorithm always returns the minimum
solution encountered, it follows that it would return the optimal solution.
Lemma 6.2.4. The depth of the recursion of algFPVertexCover(G) is at most α, where α is the minimum size
vertex cover in G.
Proof: The idea is to consider all the vertices that can be added to the vertex cover being computed without
covering any new edge. In particular, in the case the algorithm takes both u and v to the cover, then one of
these vertices must be in the optimal solution, and this can happen at most α times.
The more interesting case, is when the algorithm picks NG X (v) (i.e., β2 ) to the vertex cover. We can add
v to the vertex cover in this case without getting any new edges being covered (again, we are doing this only
conceptually – the vertex cover computed by the algorithm would not contain v [only its neighbors]). We do
the same thing for the case of β3 .
Now, observe that in any of these cases, the hypothetical set cover being constructed (which has more vertices
than what the algorithm computes, but covers exactly the same set of edges in the original graph) contains one
vertex of the optimal solution picked into itself in each level of the recursion. Clearly, the algorithm is done
once we pick all the vertices of the optimal solution into the hypothetical vertex cover. It follows that the depth
the recursion is ≤ α.
46
Theorem 6.2.5. Let G be a graph with n vertices, and with the minimal vertex cover being of size α. Then,
the algorithm algFPVertexCover (depicted in Figure 6.3) returns the optimal vertex cover for G and the running
time of this algorithm is O 3α n2 .
Proof: By Lemma 6.2.4, the recursion tree has depth α. As such, it contains at most 2 · 3α nodes. Each node
in the recursion requires O n work (ignoring the recursive calls), if implemented naively. Thus, the bound on
2
Algorithms where the running time is of the form O(nc f (α)), where α is some parameter that depends on
the problem are fixed parameter tractable algorithms for the given problem.
6.2.2.1. Remarks
Currently, the fastest algorithm known for this problem has running time O(1.2738α + αn) [CKX10]. This
algorithm uses similar ideas, but is considerably more complicated.
is possible for VertexCoverMin, unless P = NP. The
It is known that no better approximationthan 1.3606
currently best approximation known is 2 − Θ 1/ log n . If the Unique Games Conjecture is true, then no
p
• The maximum-weight matching (or just maximum matching is the matching with maximum weight
among all matchings.
• A matching M is maximal if no edge can be added to it. That is, for every edge e ∈ E, we have that the
edges of M contains at least one endpoint of e.
Note the subtle difference between maximal and maximum – the first, is a local maximum, while the other one
is the global maximum.
Lemma 6.3.2. Given an undirected unweighted graph G with n vertices and m edges, one can compute a
matching M in G, such that |M | ≥ |opt| /2, where opt is the maximum size (i.e., cardinality) matching in G.
The running time is O(n + m).
Proof: The algorithm is shockingly simple – repeatedly pick an edge of G, remove it and the edges adjacent to
it, and repeat till there are no edges left in the graph. Let M be the resulting matching.
To see why this is a two approximation (i.e., 2|M | ≥ |opt|, observe that every edge of M is adjacent to at
most two edges of opt. As such, each edge of M pays for two edges of opt, which implies the claim.
One way to see that is to imagine that we start with the matching opt and let M = {m1, . . . , mt } – at each
iteration, we insert mi into the current matching, and remove any old edges that intersect it. As such, we moved
from the matching of M to the matching of opt. In each step, we deleted at most two edges, and inserted one
edges. As such, |opt| ≤ 2|M |.
47
Lemma 6.3.3. Given an undirected weighted graph G with n vertices and m edges, one can compute a matching
M in G, such that ω(M) ≥ ω(opt)/2, where opt is the maximum weight matching in G. The running time is
O(n log n + m).
Proof: We run the algorithm for the unweighted case, with the modification that we always pick the heaviest
edge still available. The same argument as in Lemma 6.3.2 implies that that this is a two approximation. As
for the running time – we need a min-heap for m elements, that performs at most n deletions, and as such, the
running time is O(n log n + m) by using a Fibonacci heap.
Remark 6.3.4. Note, that maximum matching (and all the variants mentioned above) are solvable in polynomial
time. The main thing is that the above algorithm is both simple and give us a decent starting point which can
be used in the exact algorithm.
TSP-Min
Instance: G = (V, E) a complete graph, and ω(e) a cost function on edges of G.
Question: The cheapest tour that visits all the vertices of G exactly once.
Theorem 6.5.1. TSP-Min can not be approximated within any factor unless NP = P.
Proof: Consider the reduction from Hamiltonian Cycle into TSP. Given a graph G, which is the input for the
Hamiltonian cycle, we transform it into an instance of TSP-Min. Specifically, we set the weight of every edge to
1 if it was present in the instance of the Hamiltonian cycle, and 2 otherwise. In the resulting complete graph,
if there is a tour price n then there is a Hamiltonian cycle in the original graph. If on the other hand, there
was no cycle in G then the cheapest TSP is of price n + 1.
Instead of 2, let us assign the missing edges in H a weight of cn, for c an arbitrary number. Let H denote
the resulting graph. Clearly, if G does not contain any Hamiltonian cycle in the original graph, then the price
of the TSP-Min in H is at least cn + 1.
Note, that the prices of tours of H are either (i) equal to n if there is a Hamiltonian cycle in G, or (ii) larger
than cn + 1 if there is no Hamiltonian cycle in G. As such, if one can do a c-approximation, in polynomial time,
to TSP-Min, then using it on H would yield a tour of price ≤ cn if a tour of price n exists. But a tour of price
≤ cn exists if and only if G has a Hamiltonian cycle.
Namely, such an approximation algorithm would solve a NP-Complete problem (i.e., Hamiltonian Cycle)
in polynomial time.
Note, that Theorem 6.5.1 implies that TSP-Min can not be approximated to within any factor. However, once
we add some assumptions to the problem, it becomes much more manageable (at least as far as approximation).
What the above reduction did, was to take a problem and reduce it into an instance where this is a huge gap,
between the optimal solution, and the second cheapest solution. Next, we argued that if had an approximation
algorithm that has ratio better than the ratio between the two endpoints of this empty interval, then the
approximation algorithm, would in polynomial time would be able to decide if there is an optimal solution.
48
6.5.1. TSP with the triangle inequality
6.5.1.1. A 2-approximation
Consider the following special case of TSP:
TSP4, -Min
Instance: G = (V, E) is a complete graph. There is also a cost function ω(·) defined over the edges of
G, that complies with the triangle inequality.
Question: The cheapest tour that visits all the vertices of G exactly once.
We remind the reader that the triangle inequality holds for ω(·) if
The triangle inequality implies that if we have a path σ in G, that starts at s and ends at t, then ω(st) ≤ ω(σ).
Namely, shortcutting, that is going directly from s to t, is always beneficial if the triangle inequality holds
(assuming that we do not have any reason to visit the other vertices of σ).
Definition 6.5.2. A cycle in a graph G is Eulerian if it visits every edge of G exactly once.
Unlike Hamiltonian cycle, which has to visit every vertex exactly once, an Eulerian cycle might visit a vertex
an arbitrary number of times. We need the following classical result:
Lemma 6.5.3. A graph G has a cycle that visits every edge of G exactly once (i.e., an Eulerian cycle) if and
only if G is connected, and all the vertices have even degree. Such a cycle can be computed in O(n + m) time,
where n and m are the number of vertices and edges of G, respectively.
Our purpose is to come up with a 2-approximation algorithm for TSP 4, -Min. To this end, let Copt denote
the optimal TSP tour in G. Observe that Copt is a spanning graph of G, and as such we have that
ω Copt ≥ weight cheapest spanning graph of G .
But the cheapest spanning graph of G, is the minimum spanning tree (MST) of G, and as such ω Copt ≥
ω(MST(G)). The MST can be computed in O(n log n + m) = O(n2 ) time, where n is the number of vertices of G,
and m = n2 is the number of edges (since G is the complete graph). Let T denote the MST of G, and covert
T into a tour by duplicating every edge twice. Let H denote the new graph. We have that H is a connected
graph, every vertex of H has even degree, and as such H has an Eulerian tour (i.e., a tour that visits every edge
of H exactly once).
As such, let C denote the Eulerian cycle in H. Observe that
Next, we traverse C starting from any vertex v ∈ V(C). As we traverse C, we skip vertices that we already
visited, and in particular, the new tour we extract from C will visit the vertices of V(G) in the order they first
appear in C. Let π denote the new tour of G. Clearly, since we are performing shortcutting, and the triangle
inequality holds, we have that ω(π) ≤ ω(C). The resulting algorithm is depicted in Figure 6.4.
It is easy to verify, that all the steps of our algorithm can be done in polynomial time. As such, we have
the following result.
Theorem 6.5.4. Given an instance of TSP with the triangle inequality (TSP 4, -Min) (namely, a graph G with
n vertices and n2 edges, and a cost function ω(·) on the edges that comply with the triangle inequality), one
can compute a tour of G of length ≤ 2ω Copt , where Copt is the minimum cost TSP tour of G. The running
time of the algorithm is O n .
2
49
Random documents with unrelated
content Scribd suggests to you:
Kathin ja Peterin tullessa oli talon isäntä poissa. Kathi pyysi silloin
heti päästä levolle, ja heti kun hän oli pannut maata oman pienen
huoneensa valkoiseen sänkyyn, vaipui hän uneen, niin syvään
uneen, ettei rajuilmakaan, joka kohta sen jälkeen puhkesi, voinut
herättää häntä.
"Voin lyödä vetoa ettei hän ole nukkunut tällä tavoin ainoanakaan
yönä siellä vuorilla", sanoi Peter. "Ei ollut mitään järkeä siinä, että
hänen piti lähteä kotoaan."
"Ei, hän on juuri herännyt", sanoi täti, jonka poskia pitkin valuivat
kyyneleet, "mutta luulen olevan parasta, ettet mene hänen luokseen.
Hän nukkui niin rauhallisesti käsi pään ympäri taivutettuna… Mutta
sitten luulen hänen heränneen kirkon kellojen soitosta. Hän makaa
silmät raollaan kuin sellainen, joka ei tahdo ajatella, vaan on vain
iloinen — niinkuin lapsi jouluaamuna. — Onko pyhä, täti? kysyi hän.
— Ei, sanon minä, etkö voi kuulla, että joku on kuollut! — Hän
kohoutuu vuoteessaan ja hänen silmänsä sumenevat. — Kuka? kysyy
hän. Ja minä sanon: eräs, josta kaikki olemme pitäneet! Silloin hän
katselee minua kuin se, joka rukoilee elämänsä puolesta. — Ei, ei
isä, sanon minä, vaikka hänkin oli lähellä kuolemaa; vaan pappi, joka
soitti vanhan Matthiaksen puolesta, ja häneen löi salama. Isä löysi
hänet sieltä. — Sitten hän laskeutuu makuulleen ja kääntyy
seinäänpäin. Luulen, että hänen tulee olla ihan yksinään. Pastori
neuvoi häntä niin kauniisti aikoinaan. Ja nyt kaikki herää hänessä
uudelleen — se voi olla kylliksi vaikeata hänelle, pikku-raukalle!"
— Kun täti saapui kotiin taas tunnin kuluttua, istuutui hän Kathin
luokse, joka yhä makasi kasvot seinäänpäin, ja alkoi ihan hiljaa
kertoa hänelle miten kaunista siellä oli ollut ja miten kaikki olivat
tuoneet, mikä mitäkin muassaan tehdäkseen kaiken hänen
ympärillään juhlalliseksi. Kirkkoherra oli itse lukenut kuolinrukoukset
— itku kurkussa… Eikö Kathi tahtonut nousta ja lähteä sinne? Hän oli
näöltään kuin pyhimys!
Silloin kysyi läti Kassianilta, eikö hän tahtonut lähteä sinne? Mutta
hänhän oli siellä eilen illalla, vastasi hän. Oli mukana kantamassa
hänet kotiin — — Ja hänen kyllä tuli pysyä levossa tänään, sillä joka
hetki hän tunsi pyörrytystä.
*****
Hän oli varma, ihan varma siitä. Nyt tulisi suuri tilinteko.
"Ja tahdot lähteä ulos, niinkö? — — sillä — olet ajatellut että voisit
mennä sinne?"
Mitä tämä olikaan? Eikö hän sanonut kaikkea mitä toinen olisi
voinut, mitä hänen olisi pitänyt keksiä sanoakseen! Mitä tämä
olikaan? Tahtoiko hän ensin rauhoittaa häntä — ja sitten vasta sanoa
sanottavansa?
Äkkiä hänen kasvonsa kalpenivat. Hänen katseensa oli kohdannut
paikan, jossa hän piteli käsiään — huivin alla.
Mutta siinä liikkeessä oli jotain, joka kääntyi häntä itseään vastaan
— tarttui hänen sydämeensä…
"Niin, sillä Peter käski minun pitää sinusta hyvää huolta. Hän sanoi
nimenomaan että pitäisin hyvää huolta sinusta… Ja hän uskoo
varmasti että sinusta tulee hänelle hyvä vaimo, reipas ja uskollinen
ja hyvä vaimo."
Miten oli? oliko hän sallinut hänen lähteä? Lähteä sinne… Oi, ei, ei!
Ei ikinä! Eikä hän näin pääsisi asiasta! Nyt oli hän hänen käsissään —
tänä iltana vihdoinkin tuli puhua! Oli tapahtuva suuri, suuri tilinteko…
Mutta Kathi jäi paikoilleen. Ja hänestä tyttö äkkiä kasvoi — tuli niin
suureksi öisessä pimeydessä.
Sitten nosti hän kätensä, jossa hänellä oli pitkät palmikot. "Vien ne
jokeen", sanoi hän. "Sitten ei ole enää mitään jäljellä Goldrainin
Kathista" — —
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
ebookbell.com