算法设计与分析导论(英文版)

Paul927

贡献于2015-08-28

字数:0 关键词:

This page intentionally left blank Vice President and Editorial Director, ECS Marcia Horton Editor-in-Chief Michael Hirsch Acquisitions Editor Matt Goldstein Editorial Assistant Chelsea Bell Vice President, Marketing Patrice Jones Marketing Manager Yezan Alayan Senior Marketing Coordinator Kathryn Ferranti Marketing Assistant Emma Snider Vice President, Production Vince O’Brien Managing Editor Jeff Holcomb Production Project Manager Kayla Smith-Tarbox Senior Operations Supervisor Alan Fischer Manufacturing Buyer Lisa McDowell Art Director Anthony Gemmellaro Text Designer Sandra Rigney Cover Designer Anthony Gemmellaro Cover Illustration Jennifer Kohnke Media Editor Daniel Sandin Full-Service Project Management Windfall Software Composition Windfall Software, using ZzTEX Printer/Binder Courier Westford Cover Printer Courier Westford Text Font Times Ten Copyright © 2012, 2007, 2003 Pearson Education, Inc., publishing as Addison-Wesley. All rights reserved. Printed in the United States of America. This publication is protected by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to 201-236-3290. This is the eBook of the printed book and may not include any media, Website access codes or print supplements that may come packaged with the bound book. Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed in initial caps or all caps. Library of Congress Cataloging-in-Publication Data Levitin, Anany. Introduction to the design & analysis of algorithms / Anany Levitin. — 3rd ed. p. cm. Includes bibliographical references and index. ISBN-13: 978-0-13-231681-1 ISBN-10: 0-13-231681-1 1. Computer algorithms. I. Title. II. Title: Introduction to the design and analysis of algorithms. QA76.9.A43L48 2012 005.1—dc23 2011027089 15 14 13 12 11—CRW—10987654321 ISBN 10: 0-13-231681-1 ISBN 13: 978-0-13-231681-1 Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal Toronto Delhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo This page intentionally left blank Brief Contents New to the Third Edition xvii Preface xix 1 Introduction 1 2 Fundamentals of the Analysis of Algorithm Efficiency 41 3 Brute Force and Exhaustive Search 97 4 Decrease-and-Conquer 131 5 Divide-and-Conquer 169 6 Transform-and-Conquer 201 7 Space and Time Trade-Offs 253 8 Dynamic Programming 283 9 Greedy Technique 315 10 Iterative Improvement 345 11 Limitations of Algorithm Power 387 12 Coping with the Limitations of Algorithm Power 423 Epilogue 471 APPENDIX A Useful Formulas for the Analysis of Algorithms 475 APPENDIX B Short Tutorial on Recurrence Relations 479 References 493 Hints to Exercises 503 Index 547 v This page intentionally left blank Contents New to the Third Edition xvii Preface xix 1 Introduction 1 1.1 What Is an Algorithm? 3 Exercises 1.1 7 1.2 Fundamentals of Algorithmic Problem Solving 9 Understanding the Problem 9 Ascertaining the Capabilities of the Computational Device 9 Choosing between Exact and Approximate Problem Solving 11 Algorithm Design Techniques 11 Designing an Algorithm and Data Structures 12 Methods of Specifying an Algorithm 12 Proving an Algorithm’s Correctness 13 Analyzing an Algorithm 14 Coding an Algorithm 15 Exercises 1.2 17 1.3 Important Problem Types 18 Sorting 19 Searching 20 String Processing 20 Graph Problems 21 Combinatorial Problems 21 Geometric Problems 22 Numerical Problems 22 Exercises 1.3 23 vii viii Contents 1.4 Fundamental Data Structures 25 Linear Data Structures 25 Graphs 28 Trees 31 Sets and Dictionaries 35 Exercises 1.4 37 Summary 38 2 Fundamentals of the Analysis of Algorithm Efficiency 41 2.1 The Analysis Framework 42 Measuring an Input’s Size 43 Units for Measuring Running Time 44 Orders of Growth 45 Worst-Case, Best-Case, and Average-Case Efficiencies 47 Recapitulation of the Analysis Framework 50 Exercises 2.1 50 2.2 Asymptotic Notations and Basic Efficiency Classes 52 Informal Introduction 52 O-notation 53 -notation 54 -notation 55 Useful Property Involving the Asymptotic Notations 55 Using Limits for Comparing Orders of Growth 56 Basic Efficiency Classes 58 Exercises 2.2 58 2.3 Mathematical Analysis of Nonrecursive Algorithms 61 Exercises 2.3 67 2.4 Mathematical Analysis of Recursive Algorithms 70 Exercises 2.4 76 2.5 Example: Computing the nth Fibonacci Number 80 Exercises 2.5 83 2.6 Empirical Analysis of Algorithms 84 Exercises 2.6 89 2.7 Algorithm Visualization 91 Summary 94 Contents ix 3 Brute Force and Exhaustive Search 97 3.1 Selection Sort and Bubble Sort 98 Selection Sort 98 Bubble Sort 100 Exercises 3.1 102 3.2 Sequential Search and Brute-Force String Matching 104 Sequential Search 104 Brute-Force String Matching 105 Exercises 3.2 106 3.3 Closest-Pair and Convex-Hull Problems by Brute Force 108 Closest-Pair Problem 108 Convex-Hull Problem 109 Exercises 3.3 113 3.4 Exhaustive Search 115 Traveling Salesman Problem 116 Knapsack Problem 116 Assignment Problem 119 Exercises 3.4 120 3.5 Depth-First Search and Breadth-First Search 122 Depth-First Search 122 Breadth-First Search 125 Exercises 3.5 128 Summary 130 4 Decrease-and-Conquer 131 4.1 Insertion Sort 134 Exercises 4.1 136 4.2 Topological Sorting 138 Exercises 4.2 142 4.3 Algorithms for Generating Combinatorial Objects 144 Generating Permutations 144 Generating Subsets 146 Exercises 4.3 148 x Contents 4.4 Decrease-by-a-Constant-Factor Algorithms 150 Binary Search 150 Fake-Coin Problem 152 Russian Peasant Multiplication 153 Josephus Problem 154 Exercises 4.4 156 4.5 Variable-Size-Decrease Algorithms 157 Computing a Median and the Selection Problem 158 Interpolation Search 161 Searching and Insertion in a Binary Search Tree 163 The Game of Nim 164 Exercises 4.5 166 Summary 167 5 Divide-and-Conquer 169 5.1 Mergesort 172 Exercises 5.1 174 5.2 Quicksort 176 Exercises 5.2 181 5.3 Binary Tree Traversals and Related Properties 182 Exercises 5.3 185 5.4 Multiplication of Large Integers and Strassen’s Matrix Multiplication 186 Multiplication of Large Integers 187 Strassen’s Matrix Multiplication 189 Exercises 5.4 191 5.5 The Closest-Pair and Convex-Hull Problems by Divide-and-Conquer 192 The Closest-Pair Problem 192 Convex-Hull Problem 195 Exercises 5.5 197 Summary 198 Contents xi 6 Transform-and-Conquer 201 6.1 Presorting 202 Exercises 6.1 205 6.2 Gaussian Elimination 208 LU Decomposition 212 Computing a Matrix Inverse 214 Computing a Determinant 215 Exercises 6.2 216 6.3 Balanced Search Trees 218 AVL Trees 218 2-3 Trees 223 Exercises 6.3 225 6.4 Heaps and Heapsort 226 Notion of the Heap 227 Heapsort 231 Exercises 6.4 233 6.5 Horner’s Rule and Binary Exponentiation 234 Horner’s Rule 234 Binary Exponentiation 236 Exercises 6.5 239 6.6 Problem Reduction 240 Computing the Least Common Multiple 241 Counting Paths in a Graph 242 Reduction of Optimization Problems 243 Linear Programming 244 Reduction to Graph Problems 246 Exercises 6.6 248 Summary 250 7 Space and Time Trade-Offs 253 7.1 Sorting by Counting 254 Exercises 7.1 257 7.2 Input Enhancement in String Matching 258 Horspool’s Algorithm 259 xii Contents Boyer-Moore Algorithm 263 Exercises 7.2 267 7.3 Hashing 269 Open Hashing (Separate Chaining) 270 Closed Hashing (Open Addressing) 272 Exercises 7.3 274 7.4 B-Trees 276 Exercises 7.4 279 Summary 280 8 Dynamic Programming 283 8.1 Three Basic Examples 285 Exercises 8.1 290 8.2 The Knapsack Problem and Memory Functions 292 Memory Functions 294 Exercises 8.2 296 8.3 Optimal Binary Search Trees 297 Exercises 8.3 303 8.4 Warshall’s and Floyd’s Algorithms 304 Warshall’s Algorithm 304 Floyd’s Algorithm for the All-Pairs Shortest-Paths Problem 308 Exercises 8.4 311 Summary 312 9 Greedy Technique 315 9.1 Prim’s Algorithm 318 Exercises 9.1 322 9.2 Kruskal’s Algorithm 325 Disjoint Subsets and Union-Find Algorithms 327 Exercises 9.2 331 9.3 Dijkstra’s Algorithm 333 Exercises 9.3 337 Contents xiii 9.4 Huffman Trees and Codes 338 Exercises 9.4 342 Summary 344 10 Iterative Improvement 345 10.1 The Simplex Method 346 Geometric Interpretation of Linear Programming 347 An Outline of the Simplex Method 351 Further Notes on the Simplex Method 357 Exercises 10.1 359 10.2 The Maximum-Flow Problem 361 Exercises 10.2 371 10.3 Maximum Matching in Bipartite Graphs 372 Exercises 10.3 378 10.4 The Stable Marriage Problem 380 Exercises 10.4 383 Summary 384 11 Limitations of Algorithm Power 387 11.1 Lower-Bound Arguments 388 Trivial Lower Bounds 389 Information-Theoretic Arguments 390 Adversary Arguments 390 Problem Reduction 391 Exercises 11.1 393 11.2 Decision Trees 394 Decision Trees for Sorting 395 Decision Trees for Searching a Sorted Array 397 Exercises 11.2 399 11.3 P, NP, and NP-Complete Problems 401 P and NP Problems 402 NP-Complete Problems 406 Exercises 11.3 409 xiv Contents 11.4 Challenges of Numerical Algorithms 412 Exercises 11.4 419 Summary 420 12 Coping with the Limitations of Algorithm Power 423 12.1 Backtracking 424 n-Queens Problem 425 Hamiltonian Circuit Problem 426 Subset-Sum Problem 427 General Remarks 428 Exercises 12.1 430 12.2 Branch-and-Bound 432 Assignment Problem 433 Knapsack Problem 436 Traveling Salesman Problem 438 Exercises 12.2 440 12.3 Approximation Algorithms for NP-Hard Problems 441 Approximation Algorithms for the Traveling Salesman Problem 443 Approximation Algorithms for the Knapsack Problem 453 Exercises 12.3 457 12.4 Algorithms for Solving Nonlinear Equations 459 Bisection Method 460 Method of False Position 464 Newton’s Method 464 Exercises 12.4 467 Summary 468 Epilogue 471 APPENDIX A Useful Formulas for the Analysis of Algorithms 475 Properties of Logarithms 475 Combinatorics 475 Important Summation Formulas 476 Sum Manipulation Rules 476 Contents xv Approximation of a Sum by a Definite Integral 477 Floor and Ceiling Formulas 477 Miscellaneous 477 APPENDIX B Short Tutorial on Recurrence Relations 479 Sequences and Recurrence Relations 479 Methods for Solving Recurrence Relations 480 Common Recurrence Types in Algorithm Analysis 485 References 493 Hints to Exercises 503 Index 547 This page intentionally left blank New to the Third Edition Reordering of chapters to introduce decrease-and-conquer before divide- and-conquer Restructuring of chapter 8 on dynamic programming, including all new intro- ductory material and new exercises focusing on well-known applications More coverage of the applications of the algorithms discussed Reordering of select sections throughout the book to achieve a better align- ment of specific algorithms and general algorithm design techniques Addition of the Lomuto partition and Gray code algorithms Seventy new problems added to the end-of-chapter exercises, including algo- rithmic puzzles and questions asked during job interviews xvii This page intentionally left blank Preface The most valuable acquisitions in a scientific or technical education are the general-purpose mental tools which remain serviceable for a life-time. —George Forsythe, “What to do till the computer scientist comes.” (1968) Algorithms play the central role both in the science and practice of computing. Recognition of this fact has led to the appearance of a considerable number of textbooks on the subject. By and large, they follow one of two alternatives in presenting algorithms. One classifies algorithms according to a problem type. Such a book would have separate chapters on algorithms for sorting, searching, graphs, and so on. The advantage of this approach is that it allows an immediate comparison of, say, the efficiency of different algorithms for the same problem. The drawback of this approach is that it emphasizes problem types at the expense of algorithm design techniques. The second alternative organizes the presentation around algorithm design techniques. In this organization, algorithms from different areas of computing are grouped together if they have the same design approach. I share the belief of many (e.g., [BaY95]) that this organization is more appropriate for a basic course on the design and analysis of algorithms. There are three principal reasons for emphasis on algorithm design techniques. First, these techniques provide a student with tools for designing algorithms for new problems. This makes learning algorithm design techniques a very valuable endeavor from a practical standpoint. Second, they seek to classify multitudes of known algorithms according to an underlying design idea. Learning to see such commonality among algorithms from different application areas should be a major goal of computer science education. After all, every science considers classification of its principal subject as a major if not the central point of its discipline. Third, in my opinion, algorithm design techniques have utility as general problem solving strategies, applicable to problems beyond computing. xix xx Preface Unfortunately, the traditional classification of algorithm design techniques has several serious shortcomings, from both theoretical and educational points of view. The most significant of these shortcomings is the failure to classify many important algorithms. This limitation has forced the authors of other textbooks to depart from the design technique organization and to include chapters dealing with specific problem types. Such a switch leads to a loss of course coherence and almost unavoidably creates a confusion in students’ minds. New taxonomy of algorithm design techniques My frustration with the shortcomings of the traditional classification of algorithm design techniques has motivated me to develop a new taxonomy of them [Lev99], which is the basis of this book. Here are the principal advantages of the new taxonomy: The new taxonomy is more comprehensive than the traditional one. It includes several strategies—brute-force, decrease-and-conquer, transform-and-con- quer, space and time trade-offs, and iterative improvement—that are rarely if ever recognized as important design paradigms. The new taxonomy covers naturally many classic algorithms (Euclid’s algo- rithm, heapsort, search trees, hashing, topological sorting, Gaussian elimi- nation, Horner’s rule—to name a few) that the traditional taxonomy cannot classify. As a result, the new taxonomy makes it possible to present the stan- dard body of classic algorithms in a unified and coherent fashion. It naturally accommodates the existence of important varieties of several design techniques. For example, it recognizes three variations of decrease- and-conquer and three variations of transform-and-conquer. It is better aligned with analytical methods for the efficiency analysis (see Appendix B). Design techniques as general problem solving strategies Most applications of the design techniques in the book are to classic problems of computer science. (The only innovation here is an inclusion of some material on numerical algorithms, which are covered within the same general framework.) But these design techniques can be considered general problem solving tools, whose applications are not limited to traditional computing and mathematical problems. Two factors make this point particularly important. First, more and more computing applications go beyond the traditional domain, and there are reasons to believe that this trend will strengthen in the future. Second, developing students’ problem solving skills has come to be recognized as a major goal of college education. Among all the courses in a computer science curriculum, a course on the design and analysis of algorithms is uniquely suitable for this task because it can offer a student specific strategies for solving problems. I am not proposing that a course on the design and analysis of algorithms should become a course on general problem solving. But I do believe that the Preface xxi unique opportunity provided by studying the design and analysis of algorithms should not be missed. Toward this goal, the book includes applications to puzzles and puzzle-like games. Although using puzzles in teaching algorithms is certainly not a new idea, the book tries to do this systematically by going well beyond a few standard examples. Textbook pedagogy My goal was to write a text that would not trivialize the subject but would still be readable by most students on their own. Here are some of the things done toward this objective. Sharing the opinion of George Forsythe expressed in the epigraph, I have sought to stress major ideas underlying the design and analysis of algorithms. In choosing specific algorithms to illustrate these ideas, I limited the number of covered algorithms to those that demonstrate an underlying design technique or an analysis method most clearly. Fortunately, most classic algorithms satisfy this criterion. In Chapter 2, which is devoted to efficiency analysis, the methods used for analyzing nonrecursive algorithms are separated from those typically used for analyzing recursive algorithms. The chapter also includes sections devoted to empirical analysis and algorithm visualization. The narrative is systematically interrupted by questions to the reader. Some of them are asked rhetorically, in anticipation of a concern or doubt, and are answered immediately. The goal of the others is to prevent the reader from drifting through the text without a satisfactory level of comprehension. Each chapter ends with a summary recapping the most important concepts and results discussed in the chapter. The book contains over 600 exercises. Some of them are drills; others make important points about the material covered in the body of the text or intro- duce algorithms not covered there at all. A few exercises take advantage of Internet resources. More difficult problems—there are not many of them— are marked by special symbols in the Instructor’s Manual. (Because marking problems as difficult may discourage some students from trying to tackle them, problems are not marked in the book itself.) Puzzles, games, and puzzle-like questions are marked in the exercises with a special icon. The book provides hints to all the exercises. Detailed solutions, except for programming projects, are provided in the Instructor’s Manual, available to qualified adopters through Pearson’s Instructor Resource Center. (Please contact your local Pearson sales representative or go to www.pearsonhighered .com/irc to access this material.) Slides in PowerPoint are available to all readers of this book via anonymous ftp at the CS Support site: http://cssupport .pearsoncmg.com/. xxii Preface Changes for the third edition There are a few changes in the third edition. The most important is the new order of the chapters on decrease-and-conquer and divide-and-conquer. There are several advantages in introducing decrease-and-conquer before divide-and-conquer: Decrease-and-conquer is a simpler strategy than divide-and-conquer. Decrease-and-conquer is applicable to more problems than divide-and-con- quer. The new order makes it possible to discuss insertion sort before mergesort and quicksort. The idea of array partitioning is now introduced in conjunction with the selection problem. I took advantage of an opportunity to do this via the one- directional scan employed by Lomuto’s algorithm, leaving the two-directional scan used by Hoare’s partitioning to a later discussion in conjunction with quicksort. Binary search is now considered in the section devoted to decrease-by-a- constant-factor algorithms, where it belongs. The second important change is restructuring of Chapter 8 on dynamic pro- gramming. Specifically: The introductory section is completely new. It contains three basic examples that provide a much better introduction to this important technique than computing a binomial coefficient, the example used in the first two editions. All the exercises for Section 8.1 are new as well; they include well-known applications not available in the previous editions. I also changed the order of the other sections in this chapter to get a smoother progression from the simpler applications to the more advanced ones. The other changes include the following. More applications of the algorithms discussed are included. The section on the graph-traversal algorithms is moved from the decrease-and-conquer chapter to the brute-force and exhaustive-search chapter, where it fits better, in my opinion. The Gray code algorithm is added to the section dealing with algorithms for generating combinatorial objects. The divide- and-conquer algorithm for the closest-pair problem is discussed in more detail. Updates include the section on algorithm visualization, approximation algorithms for the traveling salesman problem, and, of course, the bibliography. I also added about 70 new problems to the exercises. Some of them are algo- rithmic puzzles and questions asked during job interviews. Prerequisites The book assumes that a reader has gone through an introductory programming course and a standard course on discrete structures. With such a background, he or she should be able to handle the book’s material without undue difficulty. Preface xxiii Still, fundamental data structures, necessary summation formulas, and recurrence relations are reviewed in Section 1.4, Appendix A, and Appendix B, respectively. Calculus is used in only three sections (Section 2.2, 11.4, and 12.4), and to a very limited degree; if students lack calculus as an assured part of their background, the relevant portions of these three sections can be omitted without hindering their understanding of the rest of the material. Use in the curriculum The book can serve as a textbook for a basic course on design and analysis of algorithms organized around algorithm design techniques. It might contain slightly more material than can be covered in a typical one-semester course. By and large, portions of Chapters 3 through 12 can be skipped without the danger of making later parts of the book incomprehensible to the reader. Any portion of the book can be assigned for self-study. In particular, Sections 2.6 and 2.7 on empirical analysis and algorithm visualization, respectively, can be assigned in conjunction with projects. Here is a possible plan for a one-semester course; it assumes a 40-class meeting format. Lecture Topic Sections 1 Introduction 1.1–1.3 2, 3 Analysis framework; O, , notations 2.1, 2.2 4 Mathematical analysis of nonrecursive algorithms 2.3 5, 6 Mathematical analysis of recursive algorithms 2.4, 2.5 (+ App. B) 7 Brute-force algorithms 3.1, 3.2 (+ 3.3) 8 Exhaustive search 3.4 9 Depth-first search and breadth-first search 3.5 10, 11 Decrease-by-one: insertion sort, topological sorting 4.1, 4.2 12 Binary search and other decrease-by-a-constant- factor algorithms 4.4 13 Variable-size-decrease algorithms 4.5 14, 15 Divide-and-conquer: mergesort, quicksort 5.1–5.2 16 Other divide-and-conquer examples 5.3 or 5.4 or 5.5 17–19 Instance simplification: presorting, Gaussian elimi- nation, balanced search trees 6.1–6.3 20 Representation change: heaps and heapsort or Horner’s rule and binary exponentiation 6.4 or 6.5 21 Problem reduction 6.6 22–24 Space-time trade-offs: string matching, hashing, B- trees 7.2–7.4 25–27 Dynamic programming algorithms 3 from 8.1–8.4 xxiv Preface 28–30 Greedy algorithms: Prim’s, Kruskal’s, Dijkstra’s, Huffman’s 9.1–9.4 31–33 Iterative improvement algorithms 3 from 10.1–10.4 34 Lower-bound arguments 11.1 35 Decision trees 11.2 36 P, NP , and NP-complete problems 11.3 37 Numerical algorithms 11.4 (+ 12.4) 38 Backtracking 12.1 39 Branch-and-bound 12.2 40 Approximation algorithms for NP-hard problems 12.3 Acknowledgments I would like to express my gratitude to the reviewers and many readers who have shared with me their opinions about the first two editions of the book and suggested improvements and corrections. The third edition has certainly ben- efited from the reviews by Andrew Harrington (Loyola University Chicago), David Levine (Saint Bonaventure University), Stefano Lombardi (UC Riverside), Daniel McKee (Mansfield University), Susan Brilliant (Virginia Commonwealth University), David Akers (University of Puget Sound), and two anonymous re- viewers. My thanks go to all the people at Pearson and their associates who worked on my book. I am especially grateful to my editor, Matt Goldstein; the editorial assistant, Chelsea Bell; the marketing manager, Yez Alayan; and the production supervisor, Kayla Smith-Tarbox. I am also grateful to Richard Camp for copyedit- ing the book, Paul Anagnostopoulos of Windfall Software and Jacqui Scarlott for its project management and typesetting, and MaryEllen Oliver for proofreading the book. Finally, I am indebted to two members of my family. Living with a spouse writing a book is probably more trying than doing the actual writing. My wife, Maria, lived through several years of this, helping me any way she could. And help she did: over 400 figures in the book and the Instructor’s Manual were created by her. My daughter Miriam has been my English prose guru over many years. She read large portions of the book and was instrumental in finding the chapter epigraphs. Anany Levitin anany.levitin@villanova.edu June 2011 This page intentionally left blank 1 Introduction Two ideas lie gleaming on the jeweler’s velvet. The first is the calculus, the second, the algorithm. The calculus and the rich body of mathematical analysis to which it gave rise made modern science possible; but it has been the algorithm that has made possible the modern world. —David Berlinski, The Advent of the Algorithm, 2000 Why do you need to study algorithms? If you are going to be a computer professional, there are both practical and theoretical reasons to study algo- rithms. From a practical standpoint, you have to know a standard set of important algorithms from different areas of computing; in addition, you should be able to design new algorithms and analyze their efficiency. From the theoretical stand- point, the study of algorithms, sometimes called algorithmics, has come to be recognized as the cornerstone of computer science. David Harel, in his delightful book pointedly titled Algorithmics: the Spirit of Computing, put it as follows: Algorithmics is more than a branch of computer science. It is the core of computer science, and, in all fairness, can be said to be relevant to most of science, business, and technology. [Har92, p. 6] But even if you are not a student in a computing-related program, there are compelling reasons to study algorithms. To put it bluntly, computer programs would not exist without algorithms. And with computer applications becoming indispensable in almost all aspects of our professional and personal lives, studying algorithms becomes a necessity for more and more people. Another reason for studying algorithms is their usefulness in developing an- alytical skills. After all, algorithms can be seen as special kinds of solutions to problems—not just answers but precisely defined procedures for getting answers. Consequently, specific algorithm design techniques can be interpreted as problem- solving strategies that can be useful regardless of whether a computer is involved. Of course, the precision inherently imposed by algorithmic thinking limits the kinds of problems that can be solved with an algorithm. You will not find, for example, an algorithm for living a happy life or becoming rich and famous. On 1 2 Introduction the other hand, this required precision has an important educational advantage. Donald Knuth, one of the most prominent computer scientists in the history of algorithmics, put it as follows: A person well-trained in computer science knows how to deal with algorithms: how to construct them, manipulate them, understand them, analyze them. This knowledge is preparation for much more than writing good computer programs; it is a general-purpose mental tool that will be a definite aid to the understanding of other subjects, whether they be chemistry, linguistics, or music, etc. The reason for this may be understood in the following way: It has often been said that a person does not really understand something until after teaching it to someone else. Actually, a person does not really understand something until after teaching it to a computer, i.e., expressing it as an algorithm...Anattempt to formalize things as algorithms leads to a much deeper understanding than if we simply try to comprehend things in the traditional way. [Knu96, p. 9] We take up the notion of algorithm in Section 1.1. As examples, we use three algorithms for the same problem: computing the greatest common divisor. There are several reasons for this choice. First, it deals with a problem familiar to ev- erybody from their middle-school days. Second, it makes the important point that the same problem can often be solved by several algorithms. Quite typically, these algorithms differ in their idea, level of sophistication, and efficiency. Third, one of these algorithms deserves to be introduced first, both because of its age—it ap- peared in Euclid’s famous treatise more than two thousand years ago—and its enduring power and importance. Finally, investigation of these three algorithms leads to some general observations about several important properties of algo- rithms in general. Section 1.2 deals with algorithmic problem solving. There we discuss several important issues related to the design and analysis of algorithms. The different aspects of algorithmic problem solving range from analysis of the problem and the means of expressing an algorithm to establishing its correctness and analyzing its efficiency. The section does not contain a magic recipe for designing an algorithm for an arbitrary problem. It is a well-established fact that such a recipe does not exist. Still, the material of Section 1.2 should be useful for organizing your work on designing and analyzing algorithms. Section 1.3 is devoted to a few problem types that have proven to be partic- ularly important to the study of algorithms and their application. In fact, there are textbooks (e.g., [Sed11]) organized around such problem types. I hold the view—shared by many others—that an organization based on algorithm design techniques is superior. In any case, it is very important to be aware of the princi- pal problem types. Not only are they the most commonly encountered problem types in real-life applications, they are used throughout the book to demonstrate particular algorithm design techniques. Section 1.4 contains a review of fundamental data structures. It is meant to serve as a reference rather than a deliberate discussion of this topic. If you need 1.1 What Is an Algorithm? 3 a more detailed exposition, there is a wealth of good books on the subject, most of them tailored to a particular programming language. 1.1 What Is an Algorithm? Although there is no universally agreed-on wording to describe this notion, there is general agreement about what the concept means: An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time. This definition can be illustrated by a simple diagram (Figure 1.1). The reference to “instructions” in the definition implies that there is some- thing or someone capable of understanding and following the instructions given. We call this a “computer,” keeping in mind that before the electronic computer was invented, the word “computer” meant a human being involved in perform- ing numeric calculations. Nowadays, of course, “computers” are those ubiquitous electronic devices that have become indispensable in almost everything we do. Note, however, that although the majority of algorithms are indeed intended for eventual computer implementation, the notion of algorithm does not depend on such an assumption. As examples illustrating the notion of the algorithm, we consider in this section three methods for solving the same problem: computing the greatest common divisor of two integers. These examples will help us to illustrate several important points: The nonambiguity requirement for each step of an algorithm cannot be com- promised. The range of inputs for which an algorithm works has to be specified carefully. The same algorithm can be represented in several different ways. There may exist several algorithms for solving the same problem. problem algorithm input output"computer" FIGURE 1.1 The notion of the algorithm. 4 Introduction Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds. Recall that the greatest common divisor of two nonnegative, not-both-zero integers m and n, denoted gcd(m, n), is defined as the largest integer that divides both m and n evenly, i.e., with a remainder of zero. Euclid of Alexandria (third century b.c.) outlined an algorithm for solving this problem in one of the volumes of his Elements most famous for its systematic exposition of geometry. In modern terms, Euclid’s algorithm is based on applying repeatedly the equality gcd(m, n) = gcd(n, m mod n), where m mod n is the remainder of the division of m by n, until m mod n is equal to 0. Since gcd(m, 0) = m (why?), the last value of m is also the greatest common divisor of the initial m and n. For example, gcd(60, 24) can be computed as follows: gcd(60, 24) = gcd(24, 12) = gcd(12, 0) = 12. (If you are not impressed by this algorithm, try finding the greatest common divisor of larger numbers, such as those in Problem 6 in this section’s exercises.) Here is a more structured description of this algorithm: Euclid’s algorithm for computing gcd(m, n) Step 1 If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2. Step 2 Divide m by n and assign the value of the remainder to r. Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. Alternatively, we can express the same algorithm in pseudocode: ALGORITHM Euclid(m, n) //Computes gcd(m, n) by Euclid’s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n = 0 do r ← m mod n m ← n n ← r return m How do we know that Euclid’s algorithm eventually comes to a stop? This follows from the observation that the second integer of the pair gets smaller with each iteration and it cannot become negative. Indeed, the new value of n on the next iteration is m mod n, which is always smaller than n (why?). Hence, the value of the second integer eventually becomes 0, and the algorithm stops. 1.1 What Is an Algorithm? 5 Just as with many other problems, there are several algorithms for computing the greatest common divisor. Let us look at the other two methods for this prob- lem. The first is simply based on the definition of the greatest common divisor of m and n as the largest integer that divides both numbers evenly. Obviously, such a common divisor cannot be greater than the smaller of these numbers, which we will denote by t = min{m, n}. So we can start by checking whether t divides both m and n: if it does, t is the answer; if it does not, we simply decrease t by 1 and try again. (How do we know that the process will eventually stop?) For example, for numbers 60 and 24, the algorithm will try first 24, then 23, and so on, until it reaches 12, where it stops. Consecutive integer checking algorithm for computing gcd(m, n) Step 1 Assign the value of min{m, n} to t. Step 2 Divide m by t. If the remainder of this division is 0, go to Step 3; otherwise, go to Step 4. Step 3 Divide n by t.If the remainder of this division is 0, return the value of t as the answer and stop; otherwise, proceed to Step 4. Step 4 Decrease the value of t by 1. Go to Step 2. Note that unlike Euclid’s algorithm, this algorithm, in the form presented, does not work correctly when one of its input numbers is zero. This example illustrates why it is so important to specify the set of an algorithm’s inputs explicitly and carefully. The third procedure for finding the greatest common divisor should be famil- iar to you from middle school. Middle-school procedure for computing gcd(m, n) Step 1 Find the prime factors of m. Step 2 Find the prime factors of n. Step 3 Identify all the common factors in the two prime expansions found in Step 1 and Step 2. (If p is a common factor occurring pm and pn times in m and n, respectively, it should be repeated min{pm, pn} times.) Step 4 Compute the product of all the common factors and return it as the greatest common divisor of the numbers given. Thus, for the numbers 60 and 24, we get 60 = 2 . 2 . 3 . 5 24 = 2 . 2 . 2 . 3 gcd(60, 24) = 2 . 2 . 3 = 12. Nostalgia for the days when we learned this method should not prevent us from noting that the last procedure is much more complex and slower than Euclid’s algorithm. (We will discuss methods for finding and comparing running times of algorithms in the next chapter.) In addition to inferior efficiency, the middle- school procedure does not qualify, in the form presented, as a legitimate algorithm. Why? Because the prime factorization steps are not defined unambiguously: they 6 Introduction require a list of prime numbers, and I strongly suspect that your middle-school math teacher did not explain how to obtain such a list. This is not a matter of unnecessary nitpicking. Unless this issue is resolved, we cannot, say, write a program implementing this procedure. Incidentally, Step 3 is also not defined clearly enough. Its ambiguity is much easier to rectify than that of the factorization steps, however. How would you find common elements in two sorted lists? So, let us introduce a simple algorithm for generating consecutive primes not exceeding any given integer n>1. It was probably invented in ancient Greece and is known as the sieve of Eratosthenes (ca. 200 b.c.). The algorithm starts by initializing a list of prime candidates with consecutive integers from 2 to n. Then, on its first iteration, the algorithm eliminates from the list all multiples of 2, i.e., 4, 6, and so on. Then it moves to the next item on the list, which is 3, and eliminates its multiples. (In this straightforward version, there is an overhead because some numbers, such as 6, are eliminated more than once.) No pass for number 4 is needed: since 4 itself and all its multiples are also multiples of 2, they were already eliminated on a previous pass. The next remaining number on the list, which is used on the third pass, is 5. The algorithm continues in this fashion until no more numbers can be eliminated from the list. The remaining integers of the list are the primes needed. As an example, consider the application of the algorithm to finding the list of primes not exceeding n = 25: 2345678910111213141516171819202122232425 2 3579 1113151719212325 2 3 5 7 11 13 17 19 23 25 23 5 7 1113 1719 23 For this example, no more passes are needed because they would eliminate num- bers already eliminated on previous iterations of the algorithm. The remaining numbers on the list are the consecutive primes less than or equal to 25. What is the largest number p whose multiples can still remain on the list to make further iterations of the algorithm necessary? Before we answer this question, let us first note that if p is a number whose multiples are being eliminated on the current pass, then the first multiple we should consider is p . p because all its smaller multiples 2p,...,(p− 1)p have been eliminated on earlier passes through the list. This observation helps to avoid eliminating the same number more than once. Obviously, p . p should not be greater than n, and therefore p cannot exceed√ n rounded down (denoted √ n using the so-called floor function). We assume in the following pseudocode that there is a function available for computing √ n ; alternatively, we could check the inequality p . p ≤ n as the loop continuation condition there. ALGORITHM Sieve(n) //Implements the sieve of Eratosthenes //Input: A positive integer n>1 //Output: Array L of all prime numbers less than or equal to n 1.1 What Is an Algorithm? 7 for p ← 2 to n do A[p] ← p for p ← 2 to √ n do //see note before pseudocode if A[p] = 0//p hasn’t been eliminated on previous passes j ← p ∗ p while j ≤ n do A[j] ← 0 //mark element as eliminated j ← j + p //copy the remaining elements of A to array L of the primes i ← 0 for p ← 2 to n do if A[p] = 0 L[i] ← A[p] i ← i + 1 return L So now we can incorporate the sieve of Eratosthenes into the middle-school procedure to get a legitimate algorithm for computing the greatest common divi- sor of two positive integers. Note that special care needs to be exercised if one or both input numbers are equal to 1: because mathematicians do not consider 1 to be a prime number, strictly speaking, the method does not work for such inputs. Before we leave this section, one more comment is in order. The exam- ples considered in this section notwithstanding, the majority of algorithms in use today—even those that are implemented as computer programs—do not deal with mathematical problems. Look around for algorithms helping us through our daily routines, both professional and personal. May this ubiquity of algorithms in to- day’s world strengthen your resolve to learn more about these fascinating engines of the information age. Exercises 1.1 1. Do some research on al-Khorezmi (also al-Khwarizmi), the man from whose name the word “algorithm” is derived. In particular, you should learn what the origins of the words “algorithm” and “algebra” have in common. 2. Given that the official purpose of the U.S. patent system is the promotion of the “useful arts,” do you think algorithms are patentable in this country? Should they be? 3. a. Write down driving directions for going from your school to your home with the precision required from an algorithm’s description. b. Write down a recipe for cooking your favorite dish with the precision required by an algorithm. 4. Design an algorithm for computing √ n for any positive integer n. Besides assignment and comparison, your algorithm may only use the four basic arithmetical operations. 8 Introduction 5. Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, respectively? 6. a. Find gcd(31415, 14142) by applying Euclid’s algorithm. b. Estimate how many times faster it will be to find gcd(31415, 14142) by Euclid’s algorithm compared with the algorithm based on checking con- secutive integers from min{m, n} down to gcd(m, n). 7. Prove the equality gcd(m, n) = gcd(n, m mod n) for every pair of positive integers m and n. 8. What does Euclid’s algorithm do for a pair of integers in which the first is smaller than the second? What is the maximum number of times this can happen during the algorithm’s execution on such an input? 9. a. What is the minimum number of divisions made by Euclid’s algorithm among all inputs 1 ≤ m, n ≤ 10? b. What is the maximum number of divisions made by Euclid’s algorithm among all inputs 1 ≤ m, n ≤ 10? 10. a. Euclid’s algorithm, as presented in Euclid’s treatise, uses subtractions rather than integer divisions. Write pseudocode for this version of Euclid’s algorithm. b. Euclid’s game (see [Bog]) starts with two unequal positive integers on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move first or second in this game? 11. The extended Euclid’s algorithm determines not only the greatest common divisor d of two positive integers m and n but also integers (not necessarily positive) x and y, such that mx + ny = d. a. Look up a description of the extended Euclid’s algorithm (see, e.g., [KnuI, p. 13]) and implement it in the language of your choice. b. Modify your program to find integer solutions to the Diophantine equation ax + by = c with any set of integer coefficients a, b, and c. 12. Locker doors There are n lockers in a hallway, numbered sequentially from 1ton. Initially, all the locker doors are closed. You make n passes by the lockers, each time starting with locker #1. On the ith pass, i = 1, 2,...,n, you toggle the door of every ith locker: if the door is closed, you open it; if it is open, you close it. After the last pass, which locker doors are open and which are closed? How many of them are open? 1.2 Fundamentals of Algorithmic Problem Solving 9 1.2 Fundamentals of Algorithmic Problem Solving Let us start by reiterating an important point made in the introduction to this chapter: We can consider algorithms to be procedural solutions to problems. These solutions are not answers but specific instructions for getting answers. It is this emphasis on precisely defined constructive procedures that makes computer science distinct from other disciplines. In particular, this distinguishes it from the- oretical mathematics, whose practitioners are typically satisfied with just proving the existence of a solution to a problem and, possibly, investigating the solution’s properties. We now list and briefly discuss a sequence of steps one typically goes through in designing and analyzing an algorithm (Figure 1.2). Understanding the Problem From a practical perspective, the first thing you need to do before designing an algorithm is to understand completely the problem given. Read the problem’s description carefully and ask questions if you have any doubts about the problem, do a few small examples by hand, think about special cases, and ask questions again if needed. There are a few types of problems that arise in computing applications quite often. We review them in the next section. If the problem in question is one of them, you might be able to use a known algorithm for solving it. Of course, it helps to understand how such an algorithm works and to know its strengths and weaknesses, especially if you have to choose among several available algorithms. But often you will not find a readily available algorithm and will have to design your own. The sequence of steps outlined in this section should help you in this exciting but not always easy task. An input to an algorithm specifies an instance of the problem the algorithm solves. It is very important to specify exactly the set of instances the algorithm needs to handle. (As an example, recall the variations in the set of instances for the three greatest common divisor algorithms discussed in the previous section.) If you fail to do this, your algorithm may work correctly for a majority of inputs but crash on some “boundary” value. Remember that a correct algorithm is not one that works most of the time, but one that works correctly for all legitimate inputs. Do not skimp on this first step of the algorithmic problem-solving process; otherwise, you will run the risk of unnecessary rework. Ascertaining the Capabilities of the Computational Device Once you completely understand a problem, you need to ascertain the capabilities of the computational device the algorithm is intended for. The vast majority of 10 Introduction Understand the problem Decide on: computational means, exact vs. approximate solving, algorithm design technique Design an algorithm Prove correctness Analyze the algorithm Code the algorithm FIGURE 1.2 Algorithm design and analysis process. algorithms in use today are still destined to be programmed for a computer closely resembling the von Neumann machine—a computer architecture outlined by the prominent Hungarian-American mathematician John von Neumann (1903– 1957), in collaboration with A. Burks and H. Goldstine, in 1946. The essence of this architecture is captured by the so-called random-access machine (RAM). Its central assumption is that instructions are executed one after another, one operation at a time. Accordingly, algorithms designed to be executed on such machines are called sequential algorithms. The central assumption of the RAM model does not hold for some newer computers that can execute operations concurrently, i.e., in parallel. Algorithms that take advantage of this capability are called parallel algorithms. Still, studying the classic techniques for design and analysis of algorithms under the RAM model remains the cornerstone of algorithmics for the foreseeable future. 1.2 Fundamentals of Algorithmic Problem Solving 11 Should you worry about the speed and amount of memory of a computer at your disposal? If you are designing an algorithm as a scientific exercise, the answer is a qualified no. As you will see in Section 2.1, most computer scientists prefer to study algorithms in terms independent of specification parameters for a particular computer. If you are designing an algorithm as a practical tool, the answer may depend on a problem you need to solve. Even the “slow” computers of today are almost unimaginably fast. Consequently, in many situations you need not worry about a computer being too slow for the task. There are important problems, however, that are very complex by their nature, or have to process huge volumes of data, or deal with applications where the time is critical. In such situations, it is imperative to be aware of the speed and memory available on a particular computer system. Choosing between Exact and Approximate Problem Solving The next principal decision is to choose between solving the problem exactly or solving it approximately. In the former case, an algorithm is called an exact algo- rithm; in the latter case, an algorithm is called an approximation algorithm.Why would one opt for an approximation algorithm? First, there are important prob- lems that simply cannot be solved exactly for most of their instances; examples include extracting square roots, solving nonlinear equations, and evaluating def- inite integrals. Second, available algorithms for solving a problem exactly can be unacceptably slow because of the problem’s intrinsic complexity. This happens, in particular, for many problems involving a very large number of choices; you will see examples of such difficult problems in Chapters 3, 11, and 12. Third, an ap- proximation algorithm can be a part of a more sophisticated algorithm that solves a problem exactly. Algorithm Design Techniques Now, with all the components of the algorithmic problem solving in place, how do you design an algorithm to solve a given problem? This is the main question this book seeks to answer by teaching you several general design techniques. What is an algorithm design technique? An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing. Check this book’s table of contents and you will see that a majority of its chapters are devoted to individual design techniques. They distill a few key ideas that have proven to be useful in designing algorithms. Learning these techniques is of utmost importance for the following reasons. First, they provide guidance for designing algorithms for new problems, i.e., problems for which there is no known satisfactory algorithm. Therefore—to use the language of a famous proverb—learning such techniques is akin to learning 12 Introduction to fish as opposed to being given a fish caught by somebody else. It is not true, of course, that each of these general techniques will be necessarily applicable to every problem you may encounter. But taken together, they do constitute a powerful collection of tools that you will find quite handy in your studies and work. Second, algorithms are the cornerstone of computer science. Every science is interested in classifying its principal subject, and computer science is no exception. Algorithm design techniques make it possible to classify algorithms according to an underlying design idea; therefore, they can serve as a natural way to both categorize and study algorithms. Designing an Algorithm and Data Structures While the algorithm design techniques do provide a powerful set of general ap- proaches to algorithmic problem solving, designing an algorithm for a particular problem may still be a challenging task. Some design techniques can be simply inapplicable to the problem in question. Sometimes, several techniques need to be combined, and there are algorithms that are hard to pinpoint as applications of the known design techniques. Even when a particular design technique is ap- plicable, getting an algorithm often requires a nontrivial ingenuity on the part of the algorithm designer. With practice, both tasks—choosing among the general techniques and applying them—get easier, but they are rarely easy. Of course, one should pay close attention to choosing data structures appro- priate for the operations performed by the algorithm. For example, the sieve of Eratosthenes introduced in Section 1.1 would run longer if we used a linked list instead of an array in its implementation (why?). Also note that some of the al- gorithm design techniques discussed in Chapters 6 and 7 depend intimately on structuring or restructuring data specifying a problem’s instance. Many years ago, an influential textbook proclaimed the fundamental importance of both algo- rithms and data structures for computer programming by its very title: Algorithms + Data Structures = Programs [Wir76]. In the new world of object-oriented pro- gramming, data structures remain crucially important for both design and analysis of algorithms. We review basic data structures in Section 1.4. Methods of Specifying an Algorithm Once you have designed an algorithm, you need to specify it in some fashion. In Section 1.1, to give you an example, Euclid’s algorithm is described in words (in a free and also a step-by-step form) and in pseudocode. These are the two options that are most widely used nowadays for specifying algorithms. Using a natural language has an obvious appeal; however, the inherent ambi- guity of any natural language makes a succinct and clear description of algorithms surprisingly difficult. Nevertheless, being able to do this is an important skill that you should strive to develop in the process of learning algorithms. Pseudocode is a mixture of a natural language and programming language- like constructs. Pseudocode is usually more precise than natural language, and its 1.2 Fundamentals of Algorithmic Problem Solving 13 usage often yields more succinct algorithm descriptions. Surprisingly, computer scientists have never agreed on a single form of pseudocode, leaving textbook authors with a need to design their own “dialects.” Fortunately, these dialects are so close to each other that anyone familiar with a modern programming language should be able to understand them all. This book’s dialect was selected to cause minimal difficulty for a reader. For the sake of simplicity, we omit declarations of variables and use indentation to show the scope of such statements as for, if, and while. As you saw in the previous section, we use an arrow “←” for the assignment operation and two slashes “//” for comments. In the earlier days of computing, the dominant vehicle for specifying algo- rithms was a flowchart, a method of expressing an algorithm by a collection of connected geometric shapes containing descriptions of the algorithm’s steps. This representation technique has proved to be inconvenient for all but very simple algorithms; nowadays, it can be found only in old algorithm books. The state of the art of computing has not yet reached a point where an algorithm’s description—be it in a natural language or pseudocode—can be fed into an electronic computer directly. Instead, it needs to be converted into a computer program written in a particular computer language. We can look at such a program as yet another way of specifying the algorithm, although it is preferable to consider it as the algorithm’s implementation. Proving an Algorithm’s Correctness Once an algorithm has been specified, you have to prove its correctness. That is, you have to prove that the algorithm yields a required result for every legitimate input in a finite amount of time. For example, the correctness of Euclid’s algorithm for computing the greatest common divisor stems from the correctness of the equality gcd(m, n) = gcd(n, m mod n) (which, in turn, needs a proof; see Problem 7 in Exercises 1.1), the simple observation that the second integer gets smaller on every iteration of the algorithm, and the fact that the algorithm stops when the second integer becomes 0. For some algorithms, a proof of correctness is quite easy; for others, it can be quite complex. A common technique for proving correctness is to use mathemati- cal induction because an algorithm’s iterations provide a natural sequence of steps needed for such proofs. It might be worth mentioning that although tracing the algorithm’s performance for a few specific inputs can be a very worthwhile activ- ity, it cannot prove the algorithm’s correctness conclusively. But in order to show that an algorithm is incorrect, you need just one instance of its input for which the algorithm fails. The notion of correctness for approximation algorithms is less straightforward than it is for exact algorithms. For an approximation algorithm, we usually would like to be able to show that the error produced by the algorithm does not exceed a predefined limit. You can find examples of such investigations in Chapter 12. 14 Introduction Analyzing an Algorithm We usually want our algorithms to possess several qualities. After correctness, by far the most important is efficiency. In fact, there are two kinds of algorithm efficiency: time efficiency, indicating how fast the algorithm runs, and space ef- ficiency, indicating how much extra memory it uses. A general framework and specific techniques for analyzing an algorithm’s efficiency appear in Chapter 2. Another desirable characteristic of an algorithm is simplicity. Unlike effi- ciency, which can be precisely defined and investigated with mathematical rigor, simplicity, like beauty, is to a considerable degree in the eye of the beholder. For example, most people would agree that Euclid’s algorithm is simpler than the middle-school procedure for computing gcd(m, n), but it is not clear whether Eu- clid’s algorithm is simpler than the consecutive integer checking algorithm. Still, simplicity is an important algorithm characteristic to strive for. Why? Because sim- pler algorithms are easier to understand and easier to program; consequently, the resulting programs usually contain fewer bugs. There is also the undeniable aes- thetic appeal of simplicity. Sometimes simpler algorithms are also more efficient than more complicated alternatives. Unfortunately, it is not always true, in which case a judicious compromise needs to be made. Yet another desirable characteristic of an algorithm is generality. There are, in fact, two issues here: generality of the problem the algorithm solves and the set of inputs it accepts. On the first issue, note that it is sometimes easier to design an algorithm for a problem posed in more general terms. Consider, for example, the problem of determining whether two integers are relatively prime, i.e., whether their only common divisor is equal to 1. It is easier to design an algorithm for a more general problem of computing the greatest common divisor of two integers and, to solve the former problem, check whether the gcd is 1 or not. There are situations, however, where designing a more general algorithm is unnecessary or difficult or even impossible. For example, it is unnecessary to sort a list of n numbers to find its median, which is its n/2th smallest element. To give another example, the standard formula for roots of a quadratic equation cannot be generalized to handle polynomials of arbitrary degrees. As to the set of inputs, your main concern should be designing an algorithm that can handle a set of inputs that is natural for the problem at hand. For example, excluding integers equal to 1 as possible inputs for a greatest common divisor algorithm would be quite unnatural. On the other hand, although the standard formula for the roots of a quadratic equation holds for complex coefficients, we would normally not implement it on this level of generality unless this capability is explicitly required. If you are not satisfied with the algorithm’s efficiency, simplicity, or generality, you must return to the drawing board and redesign the algorithm. In fact, even if your evaluation is positive, it is still worth searching for other algorithmic solutions. Recall the three different algorithms in the previous section for computing the greatest common divisor: generally, you should not expect to get the best algorithm on the first try. At the very least, you should try to fine-tune the algorithm you 1.2 Fundamentals of Algorithmic Problem Solving 15 already have. For example, we made several improvements in our implementation of the sieve of Eratosthenes compared with its initial outline in Section 1.1. (Can you identify them?) You will do well if you keep in mind the following observation of Antoine de Saint-Exup´ery, the French writer, pilot, and aircraft designer: “A designer knows he has arrived at perfection not when there is no longer anything to add, but when there is no longer anything to take away.”1 Coding an Algorithm Most algorithms are destined to be ultimately implemented as computer pro- grams. Programming an algorithm presents both a peril and an opportunity. The peril lies in the possibility of making the transition from an algorithm to a pro- gram either incorrectly or very inefficiently. Some influential computer scientists strongly believe that unless the correctness of a computer program is proven with full mathematical rigor, the program cannot be considered correct. They have developed special techniques for doing such proofs (see [Gri81]), but the power of these techniques of formal verification is limited so far to very small programs. As a practical matter, the validity of programs is still established by testing. Testing of computer programs is an art rather than a science, but that does not mean that there is nothing in it to learn. Look up books devoted to testing and debugging; even more important, test and debug your program thoroughly whenever you implement an algorithm. Also note that throughout the book, we assume that inputs to algorithms belong to the specified sets and hence require no verification. When implementing algorithms as programs to be used in actual applications, you should provide such verifications. Of course, implementing an algorithm correctly is necessary but not sufficient: you would not like to diminish your algorithm’s power by an inefficient implemen- tation. Modern compilers do provide a certain safety net in this regard, especially when they are used in their code optimization mode. Still, you need to be aware of such standard tricks as computing a loop’s invariant (an expression that does not change its value) outside the loop, collecting common subexpressions, replac- ing expensive operations by cheap ones, and so on. (See [Ker99] and [Ben00] for a good discussion of code tuning and other issues related to algorithm program- ming.) Typically, such improvements can speed up a program only by a constant factor, whereas a better algorithm can make a difference in running time by orders of magnitude. But once an algorithm is selected, a 10–50% speedup may be worth an effort. 1. I found this call for design simplicity in an essay collection by Jon Bentley [Ben00]; the essays deal with a variety of issues in algorithm design and implementation and are justifiably titled Programming Pearls. I wholeheartedly recommend the writings of both Jon Bentley and Antoine de Saint-Exup´ery. 16 Introduction A working program provides an additional opportunity in allowing an em- pirical analysis of the underlying algorithm. Such an analysis is based on timing the program on several inputs and then analyzing the results obtained. We dis- cuss the advantages and disadvantages of this approach to analyzing algorithms in Section 2.6. In conclusion, let us emphasize again the main lesson of the process depicted in Figure 1.2: As a rule, a good algorithm is a result of repeated effort and rework. Even if you have been fortunate enough to get an algorithmic idea that seems perfect, you should still try to see whether it can be improved. Actually, this is good news since it makes the ultimate result so much more enjoyable. (Yes, I did think of naming this book The Joy of Algorithms.) On the other hand, how does one know when to stop? In the real world, more often than not a project’s schedule or the impatience of your boss will stop you. And so it should be: perfection is expensive and in fact not always called for. Designing an algorithm is an engineering-like activity that calls for compromises among competing goals under the constraints of available resources, with the designer’s time being one of the resources. In the academic world, the question leads to an interesting but usually difficult investigation of an algorithm’s optimality. Actually, this question is not about the efficiency of an algorithm but about the complexity of the problem it solves: What is the minimum amount of effort any algorithm will need to exert to solve the problem? For some problems, the answer to this question is known. For example, any algorithm that sorts an array by comparing values of its elements needs about n log2 n comparisons for some arrays of size n (see Section 11.2). But for many seemingly easy problems such as integer multiplication, computer scientists do not yet have a final answer. Another important issue of algorithmic problem solving is the question of whether or not every problem can be solved by an algorithm. We are not talking here about problems that do not have a solution, such as finding real roots of a quadratic equation with a negative discriminant. For such cases, an output indicating that the problem does not have a solution is all we can and should expect from an algorithm. Nor are we talking about ambiguously stated problems. Even some unambiguous problems that must have a simple yes or no answer are “undecidable,” i.e., unsolvable by any algorithm. An important example of such a problem appears in Section 11.3. Fortunately, a vast majority of problems in practical computing can be solved by an algorithm. Before leaving this section, let us be sure that you do not have the misconception—possibly caused by the somewhat mechanical nature of the diagram of Figure 1.2—that designing an algorithm is a dull activity. There is nothing further from the truth: inventing (or discovering?) algorithms is a very creative and rewarding process. This book is designed to convince you that this is the case. 1.2 Fundamentals of Algorithmic Problem Solving 17 Exercises 1.2 1. Old World puzzle A peasant finds himself on a riverbank with a wolf, a goat, and a head of cabbage. He needs to transport all three to the other side of the river in his boat. However, the boat has room for only the peasant himself and one other item (either the wolf, the goat, or the cabbage). In his absence, the wolf would eat the goat, and the goat would eat the cabbage. Solve this problem for the peasant or prove it has no solution. (Note: The peasant is a vegetarian but does not like cabbage and hence can eat neither the goat nor the cabbage to help him solve the problem. And it goes without saying that the wolf is a protected species.) 2. New World puzzle There are four people who want to cross a rickety bridge; they all begin on the same side. You have 17 minutes to get them all across to the other side. It is night, and they have one flashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown, for example. Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes, person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must walk together at the rate of the slower person’s pace. (Note: According to a rumor on the Internet, interviewers at a well-known software company located near Seattle have given this problem to interviewees.) 3. Which of the following formulas can be considered an algorithm for comput- ing the area of a triangle whose side lengths are given positive numbers a, b, and c? a. S = p(p − a)(p − b)(p − c), where p = (a + b + c)/2 b. S = 1 2 bc sin A, where A is the angle between sides b and c c. S = 1 2 aha, where ha is the height to base a 4. Write pseudocode for an algorithm for finding real roots of equation ax2 + bx + c = 0 for arbitrary real coefficients a, b, and c. (You may assume the availability of the square root function sqrt(x).) 5. Describe the standard algorithm for finding the binary representation of a positive decimal integer a. in English. b. in pseudocode. 6. Describe the algorithm used by your favorite ATM machine in dispensing cash. (You may give your description in either English or pseudocode, which- ever you find more convenient.) 7. a. Can the problem of computing the number π be solved exactly? b. How many instances does this problem have? c. Look up an algorithm for this problem on the Internet. 18 Introduction 8. Give an example of a problem other than computing the greatest common divisor for which you know more than one algorithm. Which of them is simpler? Which is more efficient? 9. Consider the following algorithm for finding the distance between the two closest elements in an array of numbers. ALGORITHM MinDistance(A[0..n − 1]) //Input: Array A[0..n − 1] of numbers //Output: Minimum distance between two of its elements dmin ←∞ for i ← 0 to n − 1 do for j ← 0 to n − 1 do if i = j and |A[i] − A[j]| < dmin dmin ←|A[i] − A[j]| return dmin Make as many improvements as you can in this algorithmic solution to the problem. If you need to, you may change the algorithm altogether; if not, improve the implementation given. 10. One of the most influential books on problem solving, titled How To Solve It [Pol57], was written by the Hungarian-American mathematician George P´olya (1887–1985). P´olya summarized his ideas in a four-point summary. Find this summary on the Internet or, better yet, in his book, and compare it with the plan outlined in Section 1.2. What do they have in common? How are they different? 1.3 Important Problem Types In the limitless sea of problems one encounters in computing, there are a few areas that have attracted particular attention from researchers. By and large, their interest has been driven either by the problem’s practical importance or by some specific characteristics making the problem an interesting research subject; fortunately, these two motivating forces reinforce each other in most cases. In this section, we are going to introduce the most important problem types: Sorting Searching String processing Graph problems Combinatorial problems Geometric problems Numerical problems 1.3 Important Problem Types 19 These problems are used in subsequent chapters of the book to illustrate different algorithm design techniques and methods of algorithm analysis. Sorting The sorting problem is to rearrange the items of a given list in nondecreasing order. Of course, for this problem to be meaningful, the nature of the list items must allow such an ordering. (Mathematicians would say that there must exist a relation of total ordering.) As a practical matter, we usually need to sort lists of numbers, characters from an alphabet, character strings, and, most important, records similar to those maintained by schools about their students, libraries about their holdings, and companies about their employees. In the case of records, we need to choose a piece of information to guide sorting. For example, we can choose to sort student records in alphabetical order of names or by student number or by student grade-point average. Such a specially chosen piece of information is called a key. Computer scientists often talk about sorting a list of keys even when the list’s items are not records but, say, just integers. Why would we want a sorted list? To begin with, a sorted list can be a required output of a task such as ranking Internet search results or ranking students by their GPAscores. Further, sorting makes many questions about the list easier to answer. The most important of them is searching: it is why dictionaries, telephone books, class lists, and so on are sorted. You will see other examples of the usefulness of list presorting in Section 6.1. In a similar vein, sorting is used as an auxiliary step in several important algorithms in other areas, e.g., geometric algorithms and data compression. The greedy approach—an important algorithm design technique discussed later in the book—requires a sorted input. By now, computer scientists have discovered dozens of different sorting algo- rithms. In fact, inventing a new sorting algorithm has been likened to designing the proverbial mousetrap. And I am happy to report that the hunt for a better sorting mousetrap continues. This perseverance is admirable in view of the fol- lowing facts. On the one hand, there are a few good sorting algorithms that sort an arbitrary array of size n using about n log2 n comparisons. On the other hand, no algorithm that sorts by key comparisons (as opposed to, say, comparing small pieces of keys) can do substantially better than that. There is a reason for this embarrassment of algorithmic riches in the land of sorting. Although some algorithms are indeed better than others, there is no algorithm that would be the best solution in all situations. Some of the algorithms are simple but relatively slow, while others are faster but more complex; some work better on randomly ordered inputs, while others do better on almost-sorted lists; some are suitable only for lists residing in the fast memory, while others can be adapted for sorting large files stored on a disk; and so on. Two properties of sorting algorithms deserve special mention. A sorting algo- rithm is called stable if it preserves the relative order of any two equal elements in its input. In other words, if an input list contains two equal elements in positions i and j where i102 in Table 2.1.) For example, it would take about 4 . 1010 years for a computer making a trillion (1012) operations per second to execute 2100 operations. Though this is incomparably faster than it would have taken to execute 100!operations, it is still longer than 4.5 billion (4.5 . 109) years— the estimated age of the planet Earth. There is a tremendous difference between the orders of growth of the functions 2n and n!, yet both are often referred to as “exponential-growth functions” (or simply “exponential”) despite the fact that, strictly speaking, only the former should be referred to as such. The bottom line, which is important to remember, is this: Algorithms that require an exponential number of operations are practical for solving only problems of very small sizes. Another way to appreciate the qualitative difference among the orders of growth of the functions in Table 2.1 is to consider how they react to, say, a twofold increase in the value of their argument n. The function log2 n increases in value by just 1 (because log2 2n = log2 2 + log2 n = 1 + log2 n); the linear function increases twofold, the linearithmic function n log2 n increases slightly more than twofold; the quadratic function n2 and cubic function n3 increase fourfold and 2.1 The Analysis Framework 47 eightfold, respectively (because (2n)2 = 4n2 and (2n)3 = 8n3); the value of 2n gets squared (because 22n = (2n)2); and n! increases much more than that (yes, even mathematics refuses to cooperate to give a neat answer for n!). Worst-Case, Best-Case, and Average-Case Efficiencies In the beginning of this section, we established that it is reasonable to measure an algorithm’s efficiency as a function of a parameter indicating the size of the algorithm’s input. But there are many algorithms for which running time depends not only on an input size but also on the specifics of a particular input. Consider, as an example, sequential search. This is a straightforward algorithm that searches for a given item (some search key K) in a list of n elements by checking successive elements of the list until either a match with the search key is found or the list is exhausted. Here is the algorithm’s pseudocode, in which, for simplicity, a list is implemented as an array. It also assumes that the second condition A[i] = K will not be checked if the first one, which checks that the array’s index does not exceed its upper bound, fails. ALGORITHM SequentialSearch(A[0..n − 1],K) //Searches for a given value in a given array by sequential search //Input: An array A[0..n − 1] and a search key K //Output: The index of the first element in A that matches K // or −1 if there are no matching elements i ← 0 while i0isin(n2), but so are, among infinitely many others, n2 + sin n and n2 + log n. (Can you explain why?) Hopefully, this informal introduction has made you comfortable with the idea behind the three asymptotic notations. So now come the formal definitions. O-notation DEFINITION A function t(n) is said to be in O(g(n)), denoted t(n) ∈ O(g(n)), if t(n) is bounded above by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that t(n) ≤ cg(n) for all n ≥ n0. The definition is illustrated in Figure 2.1 where, for the sake of visual clarity, n is extended to be a real number. As an example, let us formally prove one of the assertions made in the introduction: 100n + 5 ∈ O(n2). Indeed, 100n + 5 ≤ 100n + n (for all n ≥ 5) = 101n ≤ 101n2. Thus, as values of the constants c and n0 required by the definition, we can take 101 and 5, respectively. Note that the definition gives us a lot of freedom in choosing specific values for constants c and n0. For example, we could also reason that 100n + 5 ≤ 100n + 5n (for all n ≥ 1) = 105n to complete the proof with c = 105 and n0 = 1. 54 Fundamentals of the Analysis of Algorithm Efficiency doesn't matter nn0 cg(n) t(n) FIGURE 2.1 Big-oh notation: t(n) ∈ O(g(n)). doesn't matter nn0 cg(n) t(n) FIGURE 2.2 Big-omega notation: t(n) ∈ (g(n)). -notation DEFINITION A function t(n) is said to be in (g(n)), denoted t(n) ∈ (g(n)), if t(n) is bounded below by some positive constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that t(n) ≥ cg(n) for all n ≥ n0. The definition is illustrated in Figure 2.2. Here is an example of the formal proof that n3 ∈ (n2): n3 ≥ n2 for all n ≥ 0, i.e., we can select c = 1 and n0 = 0. 2.2 Asymptotic Notations and Basic Efficiency Classes 55 doesn't matter nn0 c2g(n) c1g(n) t(n) FIGURE 2.3 Big-theta notation: t(n) ∈ (g(n)). -notation DEFINITION A function t(n) is said to be in (g(n)), denoted t(n) ∈ (g(n)), if t(n) is bounded both above and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constants c1 and c2 and some nonnegative integer n0 such that c2g(n) ≤ t(n) ≤ c1g(n) for all n ≥ n0. The definition is illustrated in Figure 2.3. For example, let us prove that 1 2 n(n − 1) ∈ (n2). First, we prove the right inequality (the upper bound): 1 2 n(n − 1) = 1 2 n2 − 1 2 n ≤ 1 2 n2 for all n ≥ 0. Second, we prove the left inequality (the lower bound): 1 2 n(n − 1) = 1 2 n2 − 1 2 n ≥ 1 2 n2 − 1 2 n1 2 n (for all n ≥ 2) = 1 4 n2. Hence, we can select c2 = 1 4 ,c1 = 1 2 , and n0 = 2. Useful Property Involving the Asymptotic Notations Using the formal definitions of the asymptotic notations, we can prove their general properties (see Problem 7 in this section’s exercises for a few simple examples). The following property, in particular, is useful in analyzing algorithms that comprise two consecutively executed parts. 56 Fundamentals of the Analysis of Algorithm Efficiency THEOREM If t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}). (The analogous assertions are true for the and notations as well.) PROOF The proof extends to orders of growth the following simple fact about four arbitrary real numbers a1,b1,a2,b2:ifa1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1,b2}. Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 and some non- negative integer n1 such that t1(n) ≤ c1g1(n) for all n ≥ n1. Similarly, since t2(n) ∈ O(g2(n)), t2(n) ≤ c2g2(n) for all n ≥ n2. Let us denote c3 = max{c1,c2} and consider n ≥ max{n1,n2} so that we can use both inequalities. Adding them yields the following: t1(n) + t2(n) ≤ c1g1(n) + c2g2(n) ≤ c3g1(n) + c3g2(n) = c3[g1(n) + g2(n)] ≤ c32 max{g1(n), g2(n)}. Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants c and n0 required by the O definition being 2c3 = 2 max{c1,c2} and max{n1,n2}, respectively. So what does this property imply for an algorithm that comprises two consec- utively executed parts? It implies that the algorithm’s overall efficiency is deter- mined by the part with a higher order of growth, i.e., its least efficient part: t1(n) ∈ O(g1(n)) t2(n) ∈ O(g2(n)) t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}). For example, we can check whether an array has equal elements by the following two-part algorithm: first, sort the array by applying some known sorting algorithm; second, scan the sorted array to check its consecutive elements for equality. If, for example, a sorting algorithm used in the first part makes no more than 1 2 n(n − 1) comparisons (and hence is in O(n2)) while the second part makes no more than n − 1 comparisons (and hence is in O(n)), the efficiency of the entire algorithm will be in O(max{n2,n}) = O(n2). Using Limits for Comparing Orders of Growth Though the formal definitions of O, , and are indispensable for proving their abstract properties, they are rarely used for comparing the orders of growth of two specific functions. A much more convenient method for doing so is based on 2.2 Asymptotic Notations and Basic Efficiency Classes 57 computing the limit of the ratio of two functions in question. Three principal cases may arise: limn→∞ t(n) g(n) = ⎧ ⎨ ⎩ 0 implies that t(n) has a smaller order of growth than g(n), c implies that t(n) has the same order of growth as g(n), ∞ implies that t(n) has a larger order of growth than g(n).3 Note that the first two cases mean that t(n) ∈ O(g(n)), the last two mean that t(n) ∈ (g(n)), and the second case means that t(n) ∈ (g(n)). The limit-based approach is often more convenient than the one based on the definitions because it can take advantage of the powerful calculus techniques developed for computing limits, such as L’Hˆopital’s rule limn→∞ t(n) g(n) = limn→∞ t (n) g (n) and Stirling’s formula n!≈ √ 2πn n e n for large values of n. Here are three examples of using the limit-based approach to comparing orders of growth of two functions. EXAMPLE 1 Compare the orders of growth of 1 2 n(n − 1) and n2. (This is one of the examples we used at the beginning of this section to illustrate the definitions.) limn→∞ 1 2 n(n − 1) n2 = 1 2 limn→∞ n2 − n n2 = 1 2 limn→∞(1 − 1 n) = 1 2 . Since the limit is equal to a positive constant, the functions have the same order of growth or, symbolically, 1 2 n(n − 1) ∈ (n2). EXAMPLE 2 Compare the orders of growth of log2 n and √ n. (Unlike Exam- ple 1, the answer here is not immediately obvious.) limn→∞ log2 n√ n = limn→∞ log2 n √ n = limn→∞ log2 e 1 n 1 2 √ n = 2 log2 e limn→∞ 1√ n = 0. Since the limit is equal to zero, log2 n has a smaller order of growth than √ n. (Since limn→∞ log2 n√ n = 0, we can use the so-called little-oh notation: log2 n ∈ o( √ n). Unlike the big-Oh, the little-oh notation is rarely used in analysis of algorithms.) 3. The fourth case, in which such a limit does not exist, rarely happens in the actual practice of analyzing algorithms. Still, this possibility makes the limit-based approach to comparing orders of growth less general than the one based on the definitions of O, , and . 58 Fundamentals of the Analysis of Algorithm Efficiency EXAMPLE 3 Compare the orders of growth of n! and 2n. (We discussed this informally in Section 2.1.) Taking advantage of Stirling’s formula, we get limn→∞ n! 2n = limn→∞ √ 2πn n e n 2n = limn→∞ √ 2πn nn 2nen = limn→∞ √ 2πn n 2e n =∞. Thus, though 2n grows very fast, n!grows still faster. We can write symbolically that n!∈ (2n); note, however, that while the big-Omega notation does not preclude the possibility that n! and 2n have the same order of growth, the limit computed here certainly does. Basic Efficiency Classes Even though the efficiency analysis framework puts together all the functions whose orders of growth differ by a constant multiple, there are still infinitely many such classes. (For example, the exponential functions an have different orders of growth for different values of base a.) Therefore, it may come as a surprise that the time efficiencies of a large number of algorithms fall into only a few classes. These classes are listed in Table 2.2 in increasing order of their orders of growth, along with their names and a few comments. You could raise a concern that classifying algorithms by their asymptotic effi- ciency would be of little practical use since the values of multiplicative constants are usually left unspecified. This leaves open the possibility of an algorithm in a worse efficiency class running faster than an algorithm in a better efficiency class for inputs of realistic sizes. For example, if the running time of one algorithm is n3 while the running time of the other is 106n2, the cubic algorithm will outperform the quadratic algorithm unless n exceeds 106. A few such anomalies are indeed known. Fortunately, multiplicative constants usually do not differ that drastically. As a rule, you should expect an algorithm from a better asymptotic efficiency class to outperform an algorithm from a worse class even for moderately sized inputs. This observation is especially true for an algorithm with a better than exponential running time versus an exponential (or worse) algorithm. Exercises 2.2 1. Use the most appropriate notation among O, , and to indicate the time efficiency class of sequential search (see Section 2.1) a. in the worst case. b. in the best case. c. in the average case. 2. Use the informal definitions of O, ,and to determine whether the follow- ing assertions are true or false. 2.2 Asymptotic Notations and Basic Efficiency Classes 59 TABLE 2.2 Basic asymptotic efficiency classes Class Name Comments 1 constant Short of best-case efficiencies, very few reasonable examples can be given since an algorithm’s running time typically goes to infinity when its input size grows infinitely large. log n logarithmic Typically, a result of cutting a problem’s size by a constant factor on each iteration of the algorithm (see Section 4.4). Note that a logarithmic algorithm cannot take into account all its input or even a fixed fraction of it: any algorithm that does so will have at least linear running time. n linear Algorithms that scan a list of size n (e.g., sequential search) belong to this class. n log n linearithmic Many divide-and-conquer algorithms (see Chapter 5), including mergesort and quicksort in the average case, fall into this category. n2 quadratic Typically, characterizes efficiency of algorithms with two embedded loops (see the next section). Elemen- tary sorting algorithms and certain operations on n × n matrices are standard examples. n3 cubic Typically, characterizes efficiency of algorithms with three embedded loops (see the next section). Several nontrivial algorithms from linear algebra fall into this class. 2n exponential Typical for algorithms that generate all subsets of an n-element set. Often, the term “exponential” is used in a broader sense to include this and larger orders of growth as well. n! factorial Typical for algorithms that generate all permutations of an n-element set. a. n(n + 1)/2 ∈ O(n3) b. n(n + 1)/2 ∈ O(n2) c. n(n + 1)/2 ∈ (n3) d. n(n + 1)/2 ∈ (n) 3. For each of the following functions, indicate the class (g(n)) the function belongs to. (Use the simplest g(n) possible in your answers.) Prove your assertions. a. (n2 + 1)10 b. 10n2 + 7n + 3 c. 2n lg(n + 2)2 + (n + 2)2 lg n 2 d. 2n+1 + 3n−1 e. log2 n 60 Fundamentals of the Analysis of Algorithm Efficiency 4. a. Table 2.1 contains values of several functions that often arise in the analysis of algorithms. These values certainly suggest that the functions log n, n, n log2 n, n2,n3, 2n,n! are listed in increasing order of their order of growth. Do these values prove this fact with mathematical certainty? b. Prove that the functions are indeed listed in increasing order of their order of growth. 5. List the following functions according to their order of growth from the lowest to the highest: (n − 2)!, 5lg(n + 100)10, 22n, 0.001n4 + 3n3 + 1, ln2 n, 3 √ n, 3n. 6. a. Prove that every polynomial of degree k, p(n) = aknk + ak−1nk−1 + ...+ a0 with ak > 0, belongs to (nk). b. Prove that exponential functions an have different orders of growth for different values of base a>0. 7. Prove the following assertions by using the definitions of the notations in- volved, or disprove them by giving a specific counterexample. a. If t(n) ∈ O(g(n)), then g(n) ∈ (t(n)). b. (αg(n)) = (g(n)), where α>0. c. (g(n)) = O(g(n)) ∩ (g(n)). d. For any two nonnegative functions t(n) and g(n) defined on the set of nonnegative integers, either t(n) ∈ O(g(n)), or t(n) ∈ (g(n)), or both. 8. Prove the section’s theorem for a. notation. b. notation. 9. We mentioned in this section that one can check whether all elements of an array are distinct by a two-part algorithm based on the array’s presorting. a. If the presorting is done by an algorithm with a time efficiency in(n log n), what will be a time-efficiency class of the entire algorithm? b. If the sorting algorithm used for presorting needs an extra array of size n, what will be the space-efficiency class of the entire algorithm? 10. The range of a finite nonempty set of n real numbers S is defined as the differ- ence between the largest and smallest elements of S. For each representation of S given below, describe in English an algorithm to compute the range. Indi- cate the time efficiency classes of these algorithms using the most appropriate notation (O, , or ). a. An unsorted array b. A sorted array c. A sorted singly linked list d. A binary search tree 2.3 Mathematical Analysis of Nonrecursive Algorithms 61 11. Lighter or heavier? You have n>2 identical-looking coins and a two-pan balance scale with no weights. One of the coins is a fake, but you do not know whether it is lighter or heavier than the genuine coins, which all weigh the same. Design a (1) algorithm to determine whether the fake coin is lighter or heavier than the others. 12. Door in a wall You are facing a wall that stretches infinitely in both direc- tions. There is a door in the wall, but you know neither how far away nor in which direction. You can see the door only when you are right next to it. De- sign an algorithm that enables you to reach the door by walking at most O(n) steps where n is the (unknown to you) number of steps between your initial position and the door. [Par95] 2.3 Mathematical Analysis of Nonrecursive Algorithms In this section, we systematically apply the general framework outlined in Section 2.1 to analyzing the time efficiency of nonrecursive algorithms. Let us start with a very simple example that demonstrates all the principal steps typically taken in analyzing such algorithms. EXAMPLE 1 Consider the problem of finding the value of the largest element in a list of n numbers. For simplicity, we assume that the list is implemented as an array. The following is pseudocode of a standard algorithm for solving the problem. ALGORITHM MaxElement(A[0..n − 1]) //Determines the value of the largest element in a given array //Input: An array A[0..n − 1] of real numbers //Output: The value of the largest element in A maxval ← A[0] for i ← 1 to n − 1 do if A[i] >maxval maxval ← A[i] return maxval The obvious measure of an input’s size here is the number of elements in the array, i.e., n. The operations that are going to be executed most often are in the algorithm’s for loop. There are two operations in the loop’s body: the comparison A[i] > maxval and the assignment maxval ← A[i]. Which of these two operations should we consider basic? Since the comparison is executed on each repetition of the loop and the assignment is not, we should consider the comparison to be the algorithm’s basic operation. Note that the number of comparisons will be the same for all arrays of size n; therefore, in terms of this metric, there is no need to distinguish among the worst, average, and best cases here. 62 Fundamentals of the Analysis of Algorithm Efficiency Let us denote C(n) the number of times this comparison is executed and try to find a formula expressing it as a function of size n. The algorithm makes one comparison on each execution of the loop, which is repeated for each value of the loop’s variable i within the bounds 1 and n − 1, inclusive. Therefore, we get the following sum for C(n): C(n) = n−1 i=1 1. This is an easy sum to compute because it is nothing other than 1 repeated n − 1 times. Thus, C(n) = n−1 i=1 1 = n − 1 ∈ (n). Here is a general plan to follow in analyzing nonrecursive algorithms. General Plan for Analyzing the Time Efficiency of Nonrecursive Algorithms 1. Decide on a parameter (or parameters) indicating an input’s size. 2. Identify the algorithm’s basic operation. (As a rule, it is located in the inner- most loop.) 3. Check whether the number of times the basic operation is executed depends only on the size of an input. If it also depends on some additional property, the worst-case, average-case, and, if necessary, best-case efficiencies have to be investigated separately. 4. Set up a sum expressing the number of times the algorithm’s basic operation is executed.4 5. Using standard formulas and rules of sum manipulation, either find a closed- form formula for the count or, at the very least, establish its order of growth. Before proceeding with further examples, you may want to review Appen- dix A, which contains a list of summation formulas and rules that are often useful in analysis of algorithms. In particular, we use especially frequently two basic rules of sum manipulation u i=l cai = c u i=l ai, (R1) u i=l (ai ± bi) = u i=l ai ± u i=l bi, (R2) 4. Sometimes, an analysis of a nonrecursive algorithm requires setting up not a sum but a recurrence relation for the number of times its basic operation is executed. Using recurrence relations is much more typical for analyzing recursive algorithms (see Section 2.4). 2.3 Mathematical Analysis of Nonrecursive Algorithms 63 and two summation formulas u i=l 1 = u − l + 1 where l ≤ u are some lower and upper integer limits, (S1) n i=0 i = n i=1 i = 1 + 2 + ...+ n = n(n + 1) 2 ≈ 1 2 n2 ∈ (n2). (S2) Note that the formula n−1 i=1 1 = n − 1, which we used in Example 1, is a special case of formula (S1) for l = 1 and u = n − 1. EXAMPLE 2 Consider the element uniqueness problem: check whether all the elements in a given array of n elements are distinct. This problem can be solved by the following straightforward algorithm. ALGORITHM UniqueElements(A[0..n − 1]) //Determines whether all the elements in a given array are distinct //Input: An array A[0..n − 1] //Output: Returns “true” if all the elements in A are distinct // and “false” otherwise for i ← 0 to n − 2 do for j ← i + 1 to n − 1 do if A[i] = A[j] return false return true The natural measure of the input’s size here is again n, the number of elements in the array. Since the innermost loop contains a single operation (the comparison of two elements), we should consider it as the algorithm’s basic operation. Note, however, that the number of element comparisons depends not only on n but also on whether there are equal elements in the array and, if there are, which array positions they occupy. We will limit our investigation to the worst case only. By definition, the worst case input is an array for which the number of element comparisons Cworst(n) is the largest among all arrays of size n. An inspection of the innermost loop reveals that there are two kinds of worst-case inputs—inputs for which the algorithm does not exit the loop prematurely: arrays with no equal elements and arrays in which the last two elements are the only pair of equal elements. For such inputs, one comparison is made for each repetition of the innermost loop, i.e., for each value of the loop variable j between its limits i + 1 and n − 1; this is repeated for each value of the outer loop, i.e., for each value of the loop variable i between its limits 0 and n − 2. Accordingly, we get 64 Fundamentals of the Analysis of Algorithm Efficiency Cworst(n) = n−2 i=0 n−1 j=i+1 1 = n−2 i=0 [(n − 1) − (i + 1) + 1] = n−2 i=0 (n − 1 − i) = n−2 i=0 (n − 1) − n−2 i=0 i = (n − 1) n−2 i=0 1 − (n − 2)(n − 1) 2 = (n − 1)2 − (n − 2)(n − 1) 2 = (n − 1)n 2 ≈ 1 2 n2 ∈ (n2). We also could have computed the sum n−2 i=0 (n − 1 − i) faster as follows: n−2 i=0 (n − 1 − i) = (n − 1) + (n − 2) + ...+ 1 = (n − 1)n 2 , where the last equality is obtained by applying summation formula (S2). Note that this result was perfectly predictable: in the worst case, the algorithm needs to compare all n(n − 1)/2 distinct pairs of its n elements. EXAMPLE 3 Given two n × n matrices A and B, find the time efficiency of the definition-based algorithm for computing their product C = AB. By definition, C is an n × n matrix whose elements are computed as the scalar (dot) products of the rows of matrix A and the columns of matrix B: ABC col. j Ci,j[]row i *= where C[i, j] = A[i, 0]B[0,j] + ...+ A[i, k]B[k, j] + ...+ A[i, n − 1]B[n − 1,j] for every pair of indices 0 ≤ i, j ≤ n − 1. ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − 1],B[0..n − 1, 0..n − 1]) //Multiplies two square matrices of order n by the definition-based algorithm //Input: Two n × n matrices A and B //Output: Matrix C = AB for i ← 0 to n − 1 do for j ← 0 to n − 1 do C[i, j] ← 0.0 for k ← 0 to n − 1 do C[i, j] ← C[i, j] + A[i, k] ∗ B[k, j] return C 2.3 Mathematical Analysis of Nonrecursive Algorithms 65 We measure an input’s size by matrix order n. There are two arithmetical operations in the innermost loop here—multiplication and addition—that, in principle, can compete for designation as the algorithm’s basic operation. Actually, we do not have to choose between them, because on each repetition of the innermost loop each of the two is executed exactly once. So by counting one we automatically count the other. Still, following a well-established tradition, we consider multiplication as the basic operation (see Section 2.1). Let us set up a sum for the total number of multiplications M(n) executed by the algorithm. (Since this count depends only on the size of the input matrices, we do not have to investigate the worst-case, average-case, and best-case efficiencies separately.) Obviously, there is just one multiplication executed on each repetition of the algorithm’s innermost loop, which is governed by the variable k ranging from the lower bound 0 to the upper bound n − 1. Therefore, the number of multiplications made for every pair of specific values of variables i and j is n−1 k=0 1, and the total number of multiplications M(n) is expressed by the following triple sum: M(n) = n−1 i=0 n−1 j=0 n−1 k=0 1. Now, we can compute this sum by using formula (S1) and rule (R1) given above. Starting with the innermost sum n−1 k=0 1, which is equal to n (why?), we get M(n) = n−1 i=0 n−1 j=0 n−1 k=0 1 = n−1 i=0 n−1 j=0 n = n−1 i=0 n2 = n3. This example is simple enough so that we could get this result without all the summation machinations. How? The algorithm computes n2 elements of the product matrix. Each of the product’s elements is computed as the scalar (dot) product of an n-element row of the first matrix and an n-element column of the second matrix, which takes n multiplications. So the total number of multiplica- tions is n . n2 = n3. (It is this kind of reasoning that we expected you to employ when answering this question in Problem 2 of Exercises 2.1.) If we now want to estimate the running time of the algorithm on a particular machine, we can do it by the product T (n) ≈ cmM(n) = cmn3, where cm is the time of one multiplication on the machine in question. We would get a more accurate estimate if we took into account the time spent on the additions, too: T (n) ≈ cmM(n) + caA(n) = cmn3 + can3 = (cm + ca)n3, 66 Fundamentals of the Analysis of Algorithm Efficiency where ca is the time of one addition. Note that the estimates differ only by their multiplicative constants and not by their order of growth. You should not have the erroneous impression that the plan outlined above always succeeds in analyzing a nonrecursive algorithm. An irregular change in a loop variable, a sum too complicated to analyze, and the difficulties intrinsic to the average case analysis are just some of the obstacles that can prove to be insur- mountable. These caveats notwithstanding, the plan does work for many simple nonrecursive algorithms, as you will see throughout the subsequent chapters of the book. As a last example, let us consider an algorithm in which the loop’s variable changes in a different manner from that of the previous examples. EXAMPLE 4 The following algorithm finds the number of binary digits in the binary representation of a positive decimal integer. ALGORITHM Binary(n) //Input: A positive decimal integer n //Output: The number of binary digits in n’s binary representation count ← 1 while n>1 do count ← count + 1 n ←n/2 return count First, notice that the most frequently executed operation here is not inside the while loop but rather the comparison n>1 that determines whether the loop’s body will be executed. Since the number of times the comparison will be executed is larger than the number of repetitions of the loop’s body by exactly 1, the choice is not that important. A more significant feature of this example is the fact that the loop variable takes on only a few values between its lower and upper limits; therefore, we have to use an alternative way of computing the number of times the loop is executed. Since the value of n is about halved on each repetition of the loop, the answer should be about log2 n. The exact formula for the number of times the comparison n>1 will be executed is actually log2 n+1—the number of bits in the binary representation of n according to formula (2.1). We could also get this answer by applying the analysis technique based on recurrence relations; we discuss this technique in the next section because it is more pertinent to the analysis of recursive algorithms. 2.3 Mathematical Analysis of Nonrecursive Algorithms 67 Exercises 2.3 1. Compute the following sums. a. 1 + 3 + 5 + 7 + ...+ 999 b. 2 + 4 + 8 + 16 + ...+ 1024 c. n+1 i=3 1 d. n+1 i=3 i e. n−1 i=0 i(i + 1) f. n j=1 3j+1 g. n i=1 n j=1 ij h. n i=1 1/i(i + 1) 2. Find the order of growth of the following sums. Use the (g(n)) notation with the simplest function g(n) possible. a. n−1 i=0 (i2+1)2 b. n−1 i=2 lg i2 c. n i=1(i + 1)2i−1 d. n−1 i=0 i−1 j=0(i + j) 3. The sample variance of n measurements x1,...,xn can be computed as either n i=1(xi −¯x)2 n − 1 where ¯x = n i=1 xi n or n i=1 x2 i − ( n i=1 xi)2/n n − 1 . Find and compare the number of divisions, multiplications, and additions/ subtractions (additions and subtractions are usually bunched together) that are required for computing the variance according to each of these formulas. 4. Consider the following algorithm. ALGORITHM Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i ∗ i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try to prove that, in fact, it cannot be done. 68 Fundamentals of the Analysis of Algorithm Efficiency 5. Consider the following algorithm. ALGORITHM Secret(A[0..n − 1]) //Input: An array A[0..n − 1] of n real numbers minval ← A[0]; maxval ← A[0] for i ← 1 to n − 1 do if A[i] < minval minval ← A[i] if A[i] > maxval maxval ← A[i] return maxval − minval Answer questions (a)–(e) of Problem 4 about this algorithm. 6. Consider the following algorithm. ALGORITHM Enigma(A[0..n − 1, 0..n − 1]) //Input: A matrix A[0..n − 1, 0..n − 1] of real numbers for i ← 0 to n − 2 do for j ← i + 1 to n − 1 do if A[i, j] = A[j, i] return false return true Answer questions (a)–(e) of Problem 4 about this algorithm. 7. Improve the implementation of the matrix multiplication algorithm (see Ex- ample 3) by reducing the number of additions made by the algorithm. What effect will this change have on the algorithm’s efficiency? 8. Determine the asymptotic order of growth for the total number of times all the doors are toggled in the locker doors puzzle (Problem 12 in Exercises 1.1). 9. Prove the formula n i=1 i = 1 + 2 + ...+ n = n(n + 1) 2 either by mathematical induction or by following the insight of a 10-year-old school boy named Carl Friedrich Gauss (1777–1855) who grew up to become one of the greatest mathematicians of all times. 2.3 Mathematical Analysis of Nonrecursive Algorithms 69 10. Mental arithmetic A10×10 table is filled with repeating numbers on its diagonals as shown below. Calculate the total sum of the table’s numbers in your head (after [Cra07, Question 1.33]). 123 23 3 … … … … 910 91011 91011 17 17 18 17 18 19 91011 91011 91011 91011 91011 91011 10 11 11. Consider the following version of an important algorithm that we will study later in the book. ALGORITHM GE(A[0..n − 1, 0..n]) //Input: An n × (n + 1) matrix A[0..n − 1, 0..n] of real numbers for i ← 0 to n − 2 do for j ← i + 1 to n − 1 do for k ← i to n do A[j, k] ← A[j, k] − A[i, k] ∗ A[j, i] /A[i, i] a. Find the time efficiency class of this algorithm. b. What glaring inefficiency does this pseudocode contain and how can it be eliminated to speed the algorithm up? 12. von Neumann’s neighborhood Consider the algorithm that starts with a single square and on each of its n iterations adds new squares all around the outside. How many one-by-one squares are there after n iterations? [Gar99] (In the parlance of cellular automata theory, the answer is the number of cells in the von Neumann neighborhood of range n.) The results for n = 0, 1, and 2 are illustrated below. 70 Fundamentals of the Analysis of Algorithm Efficiency n = 0 n = 1 n = 2 13. Page numbering Find the total number of decimal digits needed for num- bering pages in a book of 1000 pages. Assume that the pages are numbered consecutively starting with 1. 2.4 Mathematical Analysis of Recursive Algorithms In this section, we will see how to apply the general framework for analysis of algorithms to recursive algorithms. We start with an example often used to introduce novices to the idea of a recursive algorithm. EXAMPLE 1 Compute the factorial function F (n) = n! for an arbitrary nonneg- ative integer n. Since n!= 1 . .... (n − 1) . n = (n − 1)!. n for n ≥ 1 and 0! = 1 by definition, we can compute F (n) = F(n− 1) . n with the following recursive algorithm. ALGORITHM F(n) //Computes n! recursively //Input: A nonnegative integer n //Output: The value of n! if n = 0 return 1 else return F(n− 1) ∗ n For simplicity, we consider n itself as an indicator of this algorithm’s input size (rather than the number of bits in its binary expansion). The basic operation of the algorithm is multiplication,5 whose number of executions we denote M(n). Since the function F (n) is computed according to the formula F (n) = F(n− 1) . n for n>0, 5. Alternatively, we could count the number of times the comparison n = 0 is executed, which is the same as counting the total number of calls made by the algorithm (see Problem 2 in this section’s exercises). 2.4 Mathematical Analysis of Recursive Algorithms 71 the number of multiplications M(n) needed to compute it must satisfy the equality M(n) = M(n− 1) to compute F(n−1) + 1 to multiply F(n−1) by n for n>0. Indeed, M(n− 1) multiplications are spent to compute F(n− 1), and one more multiplication is needed to multiply the result by n. The last equation defines the sequence M(n) that we need to find. This equa- tion defines M(n) not explicitly, i.e., as a function of n, but implicitly as a function of its value at another point, namely n − 1. Such equations are called recurrence relations or, for brevity, recurrences. Recurrence relations play an important role not only in analysis of algorithms but also in some areas of applied mathematics. They are usually studied in detail in courses on discrete mathematics or discrete structures; a very brief tutorial on them is provided in Appendix B. Our goal now is to solve the recurrence relation M(n) = M(n − 1) + 1, i.e., to find an explicit formula for M(n) in terms of n only. Note, however, that there is not one but infinitely many sequences that satisfy this recurrence. (Can you give examples of, say, two of them?) To determine a solution uniquely, we need an initial condition that tells us the value with which the sequence starts. We can obtain this value by inspecting the condition that makes the algorithm stop its recursive calls: if n = 0 return 1. This tells us two things. First, since the calls stop when n = 0, the smallest value of n for which this algorithm is executed and hence M(n) defined is 0. Second, by inspecting the pseudocode’s exiting line, we can see that when n = 0, the algorithm performs no multiplications. Therefore, the initial condition we are after is M(0) = 0. the calls stop when n = 0 no multiplications when n = 0 Thus, we succeeded in setting up the recurrence relation and initial condition for the algorithm’s number of multiplications M(n): M(n) = M(n− 1) + 1 for n>0, (2.2) M(0) = 0. Before we embark on a discussion of how to solve this recurrence, let us pause to reiterate an important point. We are dealing here with two recursively defined functions. The first is the factorial function F (n) itself; it is defined by the recurrence F (n) = F(n− 1) . n for every n>0, F(0) = 1. The second is the number of multiplications M(n) needed to compute F (n) by the recursive algorithm whose pseudocode was given at the beginning of the section. 72 Fundamentals of the Analysis of Algorithm Efficiency As we just showed, M(n) is defined by recurrence (2.2). And it is recurrence (2.2) that we need to solve now. Though it is not difficult to “guess” the solution here (what sequence starts with 0 when n = 0 and increases by 1 on each step?), it will be more useful to arrive at it in a systematic fashion. From the several techniques available for solving recurrence relations, we use what can be called the method of backward substitutions. The method’s idea (and the reason for the name) is immediately clear from the way it applies to solving our particular recurrence: M(n) = M(n− 1) + 1 substitute M(n− 1) = M(n− 2) + 1 = [M(n− 2) + 1] + 1 = M(n− 2) + 2 substitute M(n− 2) = M(n− 3) + 1 = [M(n− 3) + 1] + 2 = M(n− 3) + 3. After inspecting the first three lines, we see an emerging pattern, which makes it possible to predict not only the next line (what would it be?) but also a general formula for the pattern: M(n) = M(n− i)+ i. Strictly speaking, the correctness of this formula should be proved by mathematical induction, but it is easier to get to the solution as follows and then verify its correctness. What remains to be done is to take advantage of the initial condition given. Since it is specified for n = 0, we have to substitute i = n in the pattern’s formula to get the ultimate result of our backward substitutions: M(n) = M(n− 1) + 1 = ...= M(n− i) + i = ...= M(n− n) + n = n. You should not be disappointed after exerting so much effort to get this “obvious” answer. The benefits of the method illustrated in this simple example will become clear very soon, when we have to solve more difficult recurrences. Also, note that the simple iterative algorithm that accumulates the product of n consecutive integers requires the same number of multiplications, and it does so without the overhead of time and space used for maintaining the recursion’s stack. The issue of time efficiency is actually not that important for the problem of computing n!, however. As we saw in Section 2.1, the function’s values get so large so fast that we can realistically compute exact values of n! only for very small n’s. Again, we use this example just as a simple and convenient vehicle to introduce the standard approach to analyzing recursive algorithms. Generalizing our experience with investigating the recursive algorithm for computing n!, we can now outline a general plan for investigating recursive algo- rithms. General Plan for Analyzing the Time Efficiency of Recursive Algorithms 1. Decide on a parameter (or parameters) indicating an input’s size. 2. Identify the algorithm’s basic operation. 2.4 Mathematical Analysis of Recursive Algorithms 73 3. Check whether the number of times the basic operation is executed can vary on different inputs of the same size; if it can, the worst-case, average-case, and best-case efficiencies must be investigated separately. 4. Set up a recurrence relation, with an appropriate initial condition, for the number of times the basic operation is executed. 5. Solve the recurrence or, at least, ascertain the order of growth of its solution. EXAMPLE 2 As our next example, we consider another educational workhorse of recursive algorithms: the Tower of Hanoi puzzle. In this puzzle, we (or mythical monks, if you do not like to move disks) have n disks of different sizes that can slide onto any of three pegs. Initially, all the disks are on the first peg in order of size, the largest on the bottom and the smallest on top. The goal is to move all the disks to the third peg, using the second one as an auxiliary, if necessary. We can move only one disk at a time, and it is forbidden to place a larger disk on top of a smaller one. The problem has an elegant recursive solution, which is illustrated in Fig- ure 2.4. To move n>1 disks from peg 1 to peg 3 (with peg 2 as auxiliary), we first move recursively n − 1 disks from peg 1 to peg 2 (with peg 3 as auxiliary), then move the largest disk directly from peg 1 to peg 3, and, finally, move recursively n − 1 disks from peg 2 to peg 3 (using peg 1 as auxiliary). Of course, if n = 1, we simply move the single disk directly from the source peg to the destination peg. 13 2 FIGURE 2.4 Recursive solution to the Tower of Hanoi puzzle. 74 Fundamentals of the Analysis of Algorithm Efficiency Let us apply the general plan outlined above to the Tower of Hanoi problem. The number of disks n is the obvious choice for the input’s size indicator, and so is moving one disk as the algorithm’s basic operation. Clearly, the number of moves M(n) depends on n only, and we get the following recurrence equation for it: M(n) = M(n− 1) + 1 + M(n− 1) for n>1. With the obvious initial condition M(1) = 1, we have the following recurrence relation for the number of moves M(n): M(n) = 2M(n− 1) + 1 for n>1, (2.3) M(1) = 1. We solve this recurrence by the same method of backward substitutions: M(n) = 2M(n− 1) + 1 sub. M(n− 1) = 2M(n− 2) + 1 = 2[2M(n− 2) + 1] + 1 = 22M(n− 2) + 2 + 1 sub. M(n− 2) = 2M(n− 3) + 1 = 22[2M(n− 3) + 1] + 2 + 1 = 23M(n− 3) + 22 + 2 + 1. The pattern of the first three sums on the left suggests that the next one will be 24M(n− 4) + 23 + 22 + 2 + 1, and generally, after i substitutions, we get M(n) = 2iM(n− i) + 2i−1 + 2i−2 + ...+ 2 + 1 = 2iM(n− i) + 2i − 1. Since the initial condition is specified for n = 1, which is achieved for i = n − 1, we get the following formula for the solution to recurrence (2.3): M(n) = 2n−1M(n− (n − 1)) + 2n−1 − 1 = 2n−1M(1) + 2n−1 − 1 = 2n−1 + 2n−1 − 1 = 2n − 1. Thus, we have an exponential algorithm, which will run for an unimaginably long time even for moderate values of n (see Problem 5 in this section’s exercises). This is not due to the fact that this particular algorithm is poor; in fact, it is not difficult to prove that this is the most efficient algorithm possible for this problem. It is the problem’s intrinsic difficulty that makes it so computationally hard. Still, this example makes an important general point: One should be careful with recursive algorithms because their succinctness may mask their inefficiency. When a recursive algorithm makes more than a single call to itself, it can be useful for analysis purposes to construct a tree of its recursive calls. In this tree, nodes correspond to recursive calls, and we can label them with the value of the parameter (or, more generally, parameters) of the calls. For the Tower of Hanoi example, the tree is given in Figure 2.5. By counting the number of nodes in the tree, we can get the total number of calls made by the Tower of Hanoi algorithm: C(n) = n−1 l=0 2l (where l is the level in the tree in Figure 2.5) = 2n − 1. 2.4 Mathematical Analysis of Recursive Algorithms 75 n n – 1 n – 1 n – 2 n – 2 n – 2 n – 2 22 11 2 11 2 11 11 FIGURE 2.5 Tree of recursive calls made by the recursive algorithm for the Tower of Hanoi puzzle. The number agrees, as it should, with the move count obtained earlier. EXAMPLE 3 As our next example, we investigate a recursive version of the algorithm discussed at the end of Section 2.3. ALGORITHM BinRec(n) //Input: A positive decimal integer n //Output: The number of binary digits in n’s binary representation if n = 1 return 1 else return BinRec(n/2) + 1 Let us set up a recurrence and an initial condition for the number of addi- tions A(n) made by the algorithm. The number of additions made in computing BinRec(n/2) is A(n/2), plus one more addition is made by the algorithm to increase the returned value by 1. This leads to the recurrence A(n) = A(n/2) + 1 for n>1. (2.4) Since the recursive calls end when n is equal to 1 and there are no additions made then, the initial condition is A(1) = 0. The presence of n/2 in the function’s argument makes the method of back- ward substitutions stumble on values of n that are not powers of 2. Therefore, the standard approach to solving such a recurrence is to solve it only for n = 2k and then take advantage of the theorem called the smoothness rule (see Appendix B), which claims that under very broad assumptions the order of growth observed for n = 2k gives a correct answer about the order of growth for all values of n. (Alter- natively, after getting a solution for powers of 2, we can sometimes fine-tune this solution to get a formula valid for an arbitrary n.) So let us apply this recipe to our recurrence, which for n = 2k takes the form 76 Fundamentals of the Analysis of Algorithm Efficiency A(2k) = A(2k−1) + 1 for k>0, A(20) = 0. Now backward substitutions encounter no problems: A(2k) = A(2k−1) + 1 substitute A(2k−1) = A(2k−2) + 1 = [A(2k−2) + 1] + 1 = A(2k−2) + 2 substitute A(2k−2) = A(2k−3) + 1 = [A(2k−3) + 1] + 2 = A(2k−3) + 3 ... ... = A(2k−i) + i ... = A(2k−k) + k. Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original variable n = 2k and hence k = log2 n, A(n) = log2 n ∈ (log n). In fact, one can prove (Problem 7 in this section’s exercises) that the exact solution for an arbitrary value of n is given by just a slightly more refined formula A(n) = log2 n. This section provides an introduction to the analysis of recursive algorithms. These techniques will be used throughout the book and expanded further as necessary. In the next section, we discuss the Fibonacci numbers; their analysis involves more difficult recurrence relations to be solved by a method different from backward substitutions. Exercises 2.4 1. Solve the following recurrence relations. a. x(n) = x(n − 1) + 5 for n>1,x(1) = 0 b. x(n) = 3x(n − 1) for n>1,x(1) = 4 c. x(n) = x(n − 1) + n for n>0,x(0) = 0 d. x(n) = x(n/2) + n for n>1,x(1) = 1 (solve for n = 2k) e. x(n) = x(n/3) + 1 for n>1,x(1) = 1 (solve for n = 3k) 2. Set up and solve a recurrence relation for the number of calls made by F (n), the recursive algorithm for computing n!. 3. Consider the following recursive algorithm for computing the sum of the first n cubes: S(n) = 13 + 23 + ...+ n3. 2.4 Mathematical Analysis of Recursive Algorithms 77 ALGORITHM S(n) //Input: A positive integer n //Output: The sum of the first n cubes if n = 1 return 1 else return S(n − 1) + n ∗ n ∗ n a. Set up and solve a recurrence relation for the number of times the algo- rithm’s basic operation is executed. b. How does this algorithm compare with the straightforward nonrecursive algorithm for computing this sum? 4. Consider the following recursive algorithm. ALGORITHM Q(n) //Input: A positive integer n if n = 1 return 1 else return Q(n − 1) + 2 ∗ n − 1 a. Set up a recurrence relation for this function’s values and solve it to deter- mine what this algorithm computes. b. Set up a recurrence relation for the number of multiplications made by this algorithm and solve it. c. Set up a recurrence relation for the number of additions/subtractions made by this algorithm and solve it. 5. Tower of Hanoi a. In the original version of the Tower of Hanoi puzzle, as it was published in the 1890s by ´Edouard Lucas, a French mathematician, the world will end after 64 disks have been moved from a mystical Towerof Brahma. Estimate the number of years it will take if monks could move one disk per minute. (Assume that monks do not eat, sleep, or die.) b. How many moves are made by the ith largest disk (1 ≤ i ≤ n) in this algorithm? c. Find a nonrecursive algorithm for the Tower of Hanoi puzzle and imple- ment it in the language of your choice. 6. Restricted Tower of Hanoi Consider the version of the Tower of Hanoi puzzle in which n disks have to be moved from peg A to peg C using peg B so that any move should either place a disk on peg B or move a disk from that peg. (Of course, the prohibition of placing a larger disk on top of a smaller one remains in place, too.) Design a recursive algorithm for this problem and find the number of moves made by it. 78 Fundamentals of the Analysis of Algorithm Efficiency 7. a. Prove that the exact number of additions made by the recursive algorithm BinRec(n) for an arbitrary positive decimal integer n is log2 n. b. Set up a recurrence relation for the number of additions made by the nonrecursive version of this algorithm (see Section 2.3, Example 4) and solve it. 8. a. Design a recursive algorithm for computing 2n for any nonnegative integer n that is based on the formula 2n = 2n−1 + 2n−1. b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem? 9. Consider the following recursive algorithm. ALGORITHM Riddle(A[0..n − 1]) //Input: An array A[0..n − 1] of real numbers if n = 1 return A[0] else temp ← Riddle(A[0..n − 2]) if temp ≤ A[n − 1] return temp else return A[n − 1] a. What does this algorithm compute? b. Set up a recurrence relation for the algorithm’s basic operation count and solve it. 10. Consider the following algorithm to check whether a graph defined by its adjacency matrix is complete. ALGORITHM GraphComplete(A[0..n − 1, 0..n − 1]) //Input: Adjacency matrix A[0..n − 1, 0..n − 1]) of an undirected graph G //Output: 1 (true) if G is complete and 0 (false) otherwise if n = 1 return 1 //one-vertex graph is complete by definition else if not GraphComplete(A[0..n − 2, 0..n − 2]) return 0 else for j ← 0 to n − 2 do if A[n − 1,j] = 0 return 0 return 1 What is the algorithm’s efficiency class in the worst case? 11. The determinant of an n × n matrix 2.4 Mathematical Analysis of Recursive Algorithms 79 A = ⎡ ⎢⎢⎣ a00 ... a0 n−1 a10 ... a1 n−1... ... an−10 ... an−1 n−1 ⎤ ⎥⎥⎦ , denoted det A, can be defined as a00 for n = 1 and, for n>1, by the recursive formula det A = n−1 j=0 sja0 j det Aj, where sj is +1 if j is even and −1ifj is odd, a0 j is the element in row 0 and column j, and Aj is the (n − 1) × (n − 1) matrix obtained from matrix A by deleting its row 0 and column j. a. Set up a recurrence relation for the number of multiplications made by the algorithm implementing this recursive definition. b. Without solving the recurrence, what can you say about the solution’s order of growth as compared to n!? 12. von Neumann’s neighborhood revisited Find the number of cells in the von Neumann neighborhood of range n (Problem 12 in Exercises 2.3) by setting up and solving a recurrence relation. 13. Frying hamburgers There are n hamburgers to be fried on a small grill that can hold only two hamburgers at a time. Each hamburger has to be fried on both sides; frying one side of a hamburger takes 1 minute, regardless of whether one or two hamburgers are fried at the same time. Consider the following recursive algorithm for executing this task in the minimum amount of time. If n ≤ 2, fry the hamburger or the two hamburgers together on each side. If n>2, fry any two hamburgers together on each side and then apply the same procedure recursively to the remaining n − 2 hamburgers. a. Set up and solve the recurrence for the amount of time this algorithm needs to fry n hamburgers. b. Explain why this algorithm does not fry the hamburgers in the minimum amount of time for all n>0. c. Give a correct recursive algorithm that executes the task in the minimum amount of time. 14. Celebrity problem A celebrity among a group of n people is a person who knows nobody but is known by everybody else. The task is to identify a celebrity by only asking questions to people of the form “Do you know him/her?” Design an efficient algorithm to identify a celebrity or determine that the group has no such person. How many questions does your algorithm need in the worst case? 80 Fundamentals of the Analysis of Algorithm Efficiency 2.5 Example: Computing the nth Fibonacci Number In this section, we consider the Fibonacci numbers, a famous sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... (2.5) that can be defined by the simple recurrence F (n) = F(n− 1) + F(n− 2) for n>1 (2.6) and two initial conditions F(0) = 0,F(1) = 1. (2.7) The Fibonacci numbers were introduced by Leonardo Fibonacci in 1202 as a solution to a problem about the size of a rabbit population (Problem 2 in this section’s exercises). Many more examples of Fibonacci-like numbers have since been discovered in the natural world, and they have even been used in predicting the prices of stocks and commodities. There are some interesting applications of the Fibonacci numbers in computer science as well. For example, worst-case inputs for Euclid’s algorithm discussed in Section 1.1 happen to be consecutive elements of the Fibonacci sequence. In this section, we briefly consider algorithms for computing the nth element of this sequence. Among other benefits, the discussion will provide us with an opportunity to introduce another method for solving recurrence relations useful for analysis of recursive algorithms. To start, let us get an explicit formula for F (n). If we try to apply the method of backward substitutions to solve recurrence (2.6), we will fail to get an easily discernible pattern. Instead, we can take advantage of a theorem that describes solutions to a homogeneous second-order linear recurrence with constant co- efficients ax(n) + bx(n − 1) + cx(n − 2) = 0, (2.8) where a, b, and c are some fixed real numbers (a = 0) called the coefficients of the recurrence and x(n) is the generic term of an unknown sequence to be found. Applying this theorem to our recurrence with the initial conditions given—see Appendix B—we obtain the formula F (n) = 1√ 5 (φn − ˆφn), (2.9) where φ = (1 + √ 5)/2 ≈ 1.61803 and ˆφ =−1/φ ≈−0.61803.6 It is hard to believe that formula (2.9), which includes arbitrary integer powers of irrational numbers, yields nothing else but all the elements of Fibonacci sequence (2.5), but it does! One of the benefits of formula (2.9) is that it immediately implies that F (n) grows exponentially (remember Fibonacci’s rabbits?), i.e., F (n) ∈ (φn). This 6. Constant φ is known as the golden ratio. Since antiquity, it has been considered the most pleasing ratio of a rectangle’s two sides to the human eye and might have been consciously used by ancient architects and sculptors. 2.5 Example: Computing the nth Fibonacci Number 81 follows from the observation that ˆφ is a fraction between −1 and 0, and hence ˆφn gets infinitely small as n goes to infinity. In fact, one can prove that the impact of the second term 1√ 5 ˆφn on the value of F (n) can be obtained by rounding off the value of the first term to the nearest integer. In other words, for every nonnegative integer n, F (n) = 1√ 5 φn rounded to the nearest integer. (2.10) In the algorithms that follow, we consider, for the sake of simplicity, such oper- ations as additions and multiplications at unit cost. Since the Fibonacci numbers grow infinitely large (and grow very rapidly), a more detailed analysis than the one offered here is warranted. In fact, it is the size of the numbers rather than a time-efficient method for computing them that should be of primary concern here. Still, these caveats notwithstanding, the algorithms we outline and their analysis provide useful examples for a student of the design and analysis of algorithms. To begin with, we can use recurrence (2.6) and initial conditions (2.7) for the obvious recursive algorithm for computing F (n). ALGORITHM F (n) //Computes the nth Fibonacci number recursively by using its definition //Input: A nonnegative integer n //Output: The nth Fibonacci number if n ≤ 1 return n else return F(n− 1) + F(n− 2) Before embarking on its formal analysis, can you tell whether this is an effi- cient algorithm? Well, we need to do a formal analysis anyway. The algorithm’s ba- sic operation is clearly addition, so let A(n) be the number of additions performed by the algorithm in computing F (n). Then the numbers of additions needed for computing F(n− 1) and F(n− 2) are A(n − 1) and A(n − 2), respectively, and the algorithm needs one more addition to compute their sum. Thus, we get the following recurrence for A(n): A(n) = A(n − 1) + A(n − 2) + 1 for n>1, (2.11) A(0) = 0,A(1) = 0. The recurrence A(n) − A(n − 1) − A(n − 2) = 1 is quite similar to recurrence F (n) − F(n− 1) − F(n− 2) = 0, but its right-hand side is not equal to zero. Such recurrences are called inhomogeneous. There are general techniques for solving inhomogeneous recurrences (see Appendix B or any textbook on discrete mathe- matics), but for this particular recurrence, a special trick leads to a faster solution. We can reduce our inhomogeneous recurrence to a homogeneous one by rewriting it as [A(n) + 1] − [A(n − 1) + 1] − [A(n − 2) + 1] = 0 and substituting B(n) = A(n) + 1: 82 Fundamentals of the Analysis of Algorithm Efficiency B(n) − B(n − 1) − B(n − 2) = 0, B(0) = 1,B(1) = 1. This homogeneous recurrence can be solved exactly in the same manner as recur- rence (2.6) was solved to find an explicit formula for F (n). But it can actually be avoided by noting that B(n) is, in fact, the same recurrence as F (n) except that it starts with two 1’s and thus runs one step ahead of F (n). So B(n) = F(n+ 1), and A(n) = B(n) − 1 = F(n+ 1) − 1 = 1√ 5 (φn+1 − ˆφn+1) − 1. Hence, A(n) ∈ (φn), and if we measure the size of n by the number of bits b =log2 n+1in its binary representation, the efficiency class will be even worse, namely, doubly exponential: A(b) ∈ (φ2b). The poor efficiency class of the algorithm could be anticipated by the nature of recurrence (2.11). Indeed, it contains two recursive calls with the sizes of smaller instances only slightly smaller than size n. (Have you encountered such a situation before?) We can also see the reason behind the algorithm’s inefficiency by looking at a recursive tree of calls tracing the algorithm’s execution. An example of such a tree for n = 5 is given in Figure 2.6. Note that the same values of the function are being evaluated here again and again, which is clearly extremely inefficient. We can obtain a much faster algorithm by simply computing the successive elements of the Fibonacci sequence iteratively, as is done in the following algo- rithm. ALGORITHM Fib(n) //Computes the nth Fibonacci number iteratively by using its definition //Input: A nonnegative integer n //Output: The nth Fibonacci number F[0] ← 0; F[1] ← 1 for i ← 2 to n do F[i] ← F[i − 1] + F[i − 2] return F[n] F(3) F(4) F(5) F(3) F(1)F(2)F(2) F(2) F(1) F(1) F(1)F(0) F(0) F(1) F(0) FIGURE 2.6 Tree of recursive calls for computing the 5th Fibonacci number by the definition-based algorithm. 2.5 Example: Computing the nth Fibonacci Number 83 This algorithm clearly makes n − 1 additions. Hence, it is linear as a function of n and “only” exponential as a function of the number of bits b in n’s binary representation. Note that using an extra array for storing all the preceding ele- ments of the Fibonacci sequence can be avoided: storing just two values is neces- sary to accomplish the task (see Problem 8 in this section’s exercises). The third alternative for computing the nth Fibonacci number lies in using formula (2.10). The efficiency of the algorithm will obviously be determined by the efficiency of an exponentiation algorithm used for computing φn. If it is done by simply multiplying φ by itself n − 1 times, the algorithm will be in (n) = (2b). There are faster algorithms for the exponentiation problem. For example, we will discuss (log n) = (b) algorithms for this problem in Chapters 4 and 6. Note also that special care should be exercised in implementing this approach to computing the nth Fibonacci number. Since all its intermediate results are irrational numbers, we would have to make sure that their approximations in the computer are accurate enough so that the final round-off yields a correct result. Finally, there exists a (log n) algorithm for computing the nth Fibonacci number that manipulates only integers. It is based on the equality  F(n− 1) F (n) F (n) F (n + 1)  =  01 11 n for n ≥ 1 and an efficient way of computing matrix powers. Exercises 2.5 1. Find a Web site dedicated to applications of the Fibonacci numbers and study it. 2. Fibonacci’s rabbits problem A man put a pair of rabbits in a place sur- rounded by a wall. How many pairs of rabbits will be there in a year if the initial pair of rabbits (male and female) are newborn and all rabbit pairs are not fertile during their first month of life but thereafter give birth to one new male/female pair at the end of every month? 3. Climbing stairs Find the number of different ways to climb an n-stair stair- case if each step is either one or two stairs. For example, a 3-stair staircase can be climbed three ways: 1-1-1, 1-2, and 2-1. 4. How many even numbers are there among the first n Fibonacci numbers, i.e., among the numbers F(0), F (1), . . . , F (n − 1)? Give a closed-form formula valid for every n>0. 5. Check by direct substitutions that the function 1√ 5(φn − ˆφn) indeed satisfies recurrence (2.6) and initial conditions (2.7). 6. The maximum values of the Java primitive types int and long are 231 − 1 and 263 − 1, respectively. Find the smallest n for which the nth Fibonacci number is not going to fit in a memory allocated for 84 Fundamentals of the Analysis of Algorithm Efficiency a. the type int. b. the type long. 7. Consider the recursive definition-based algorithm for computing the nth Fi- bonacci number F (n). Let C(n) and Z(n) be the number of times F(1) and F(0) are computed, respectively. Prove that a. C(n) = F (n). b. Z(n) = F(n− 1). 8. Improve algorithm Fibof the text so that it requires only (1) space. 9. Prove the equality  F(n− 1) F (n) F (n) F (n + 1)  =  01 11 n for n ≥ 1. 10. How many modulo divisions are made by Euclid’s algorithm on two consec- utive Fibonacci numbers F (n) and F(n− 1) as the algorithm’s input? 11. Dissecting a Fibonacci rectangle Given a rectangle whose sides are two con- secutive Fibonacci numbers, design an algorithm to dissect it into squares with no more than two squares being the same size. What is the time efficiency class of your algorithm? 12. In the language of your choice, implement two algorithms for computing the last five digits of the nth Fibonacci number that are based on (a) the recursive definition-based algorithm F(n); (b) the iterative definition-based algorithm Fib(n). Perform an experiment to find the largest value of n for which your programs run under 1 minute on your computer. 2.6 Empirical Analysis of Algorithms In Sections 2.3 and 2.4, we saw how algorithms, both nonrecursive and recursive, can be analyzed mathematically. Though these techniques can be applied success- fully to many simple algorithms, the power of mathematics, even when enhanced with more advanced techniques (see [Sed96], [Pur04], [Gra94], and [Gre07]), is far from limitless. In fact, even some seemingly simple algorithms have proved to be very difficult to analyze with mathematical precision and certainty. As we pointed out in Section 2.1, this is especially true for the average-case analysis. The principal alternative to the mathematical analysis of an algorithm’s ef- ficiency is its empirical analysis. This approach implies steps spelled out in the following plan. General Plan for the Empirical Analysis of Algorithm Time Efficiency 1. Understand the experiment’s purpose. 2. Decide on the efficiency metric M to be measured and the measurement unit (an operation count vs. a time unit). 3. Decide on characteristics of the input sample (its range, size, and so on). 4. Prepare a program implementing the algorithm (or algorithms) for the exper- imentation. 2.6 Empirical Analysis of Algorithms 85 5. Generate a sample of inputs. 6. Run the algorithm (or algorithms) on the sample’s inputs and record the data observed. 7. Analyze the data obtained. Let us discuss these steps one at a time. There are several different goals one can pursue in analyzing algorithms empirically. They include checking the accuracy of a theoretical assertion about the algorithm’s efficiency, comparing the efficiency of several algorithms for solving the same problem or different imple- mentations of the same algorithm, developing a hypothesis about the algorithm’s efficiency class, and ascertaining the efficiency of the program implementing the algorithm on a particular machine. Obviously, an experiment’s design should de- pend on the question the experimenter seeks to answer. In particular, the goal of the experiment should influence, if not dictate, how the algorithm’s efficiency is to be measured. The first alternative is to insert a counter (or counters) into a program implementing the algorithm to count the number of times the algorithm’s basic operation is executed. This is usually a straightforward operation; you should only be mindful of the possibility that the basic operation is executed in several places in the program and that all its executions need to be accounted for. As straightforward as this task usually is, you should always test the modified program to ensure that it works correctly, in terms of both the problem it solves and the counts it yields. The second alternative is to time the program implementing the algorithm in question. The easiest way to do this is to use a system’s command, such as the time command in UNIX. Alternatively, one can measure the running time of a code fragment by asking for the system time right before the fragment’s start (tstart) and just after its completion (tfinish), and then computing the difference between the two (tfinish− tstart).7 In C and C++, you can use the function clock for this purpose; in Java, the method currentTimeMillis() in the System class is available. It is important to keep several facts in mind, however. First, a system’s time is typically not very accurate, and you might get somewhat different results on repeated runs of the same program on the same inputs. An obvious remedy is to make several such measurements and then take their average (or the median) as the sample’s observation point. Second, given the high speed of modern com- puters, the running time may fail to register at all and be reported as zero. The standard trick to overcome this obstacle is to run the program in an extra loop many times, measure the total running time, and then divide it by the number of the loop’s repetitions. Third, on a computer running under a time-sharing system such as UNIX, the reported time may include the time spent by the CPU on other programs, which obviously defeats the purpose of the experiment. Therefore, you should take care to ask the system for the time devoted specifically to execution of 7. If the system time is given in units called “ticks,” the difference should be divided by a constant indicating the number of ticks per time unit. 86 Fundamentals of the Analysis of Algorithm Efficiency your program. (In UNIX, this time is called the “user time,” and it is automatically provided by the time command.) Thus, measuring the physical running time has several disadvantages, both principal (dependence on a particular machine being the most important of them) and technical, not shared by counting the executions of a basic operation. On the other hand, the physical running time provides very specific information about an algorithm’s performance in a particular computing environment, which can be of more importance to the experimenter than, say, the algorithm’s asymptotic efficiency class. In addition, measuring time spent on different segments of a program can pinpoint a bottleneck in the program’s performance that can be missed by an abstract deliberation about the algorithm’s basic operation. Getting such data—called profiling—is an important resource in the empirical analysis of an algorithm’s running time; the data in question can usually be obtained from the system tools available in most computing environments. Whether you decide to measure the efficiency by basic operation counting or by time clocking, you will need to decide on a sample of inputs for the experiment. Often, the goal is to use a sample representing a “typical” input; so the challenge is to understand what a “typical” input is. For some classes of algorithms—e.g., for algorithms for the traveling salesman problem that we are going to discuss later in the book—researchers have developed a set of instances they use for benchmark- ing. But much more often than not, an input sample has to be developed by the experimenter. Typically, you will have to make decisions about the sample size (it is sensible to start with a relatively small sample and increase it later if necessary), the range of instance sizes (typically neither trivially small nor excessively large), and a procedure for generating instances in the range chosen. The instance sizes can either adhere to some pattern (e.g., 1000, 2000, 3000,...,10,000 or 500, 1000, 2000, 4000,...,128,000) or be generated randomly within the range chosen. The principal advantage of size changing according to a pattern is that its impact is easier to analyze. For example, if a sample’s sizes are generated by doubling, you can compute the ratios M(2n)/M(n) of the observed metric M (the count or the time) to see whether the ratios exhibit a behavior typical of algorithms in one of the basic efficiency classes discussed in Section 2.2. The major disadvantage of nonrandom sizes is the possibility that the algorithm under investigation exhibits atypical behavior on the sample chosen. For example, if all the sizes in a sample are even and your algorithm runs much more slowly on odd- size inputs, the empirical results will be quite misleading. Another important issue concerning sizes in an experiment’s sample is whether several instances of the same size should be included. If you expect the observed metric to vary considerably on instances of the same size, it would be probably wise to include several instances for every size in the sample. (There are well-developed methods in statistics to help the experimenter make such de- cisions; you will find no shortage of books on this subject.) Of course, if several instances of the same size are included in the sample, the averages or medians of the observed values for each size should be computed and investigated instead of or in addition to individual sample points. 2.6 Empirical Analysis of Algorithms 87 Much more often than not, an empirical analysis requires generating random numbers. Even if you decide to use a pattern for input sizes, you will typically want instances themselves generated randomly. Generating random numbers on a digital computer is known to present a difficult problem because, in principle, the problem can be solved only approximately. This is the reason computer scien- tists prefer to call such numbers pseudorandom. As a practical matter, the easiest and most natural way of getting such numbers is to take advantage of a random number generator available in computer language libraries. Typically, its output will be a value of a (pseudo)random variable uniformly distributed in the interval between 0 and 1. If a different (pseudo)random variable is desired, an appro- priate transformation needs to be made. For example, if x is a continuous ran- dom variable uniformly distributed on the interval 0 ≤ x<1, the variable y = l+ x(r − l) will be uniformly distributed among the integer values between integers l and r − 1 (l < r). Alternatively, you can implement one of several known algorithms for gener- ating (pseudo)random numbers. The most widely used and thoroughly studied of such algorithms is the linear congruential method. ALGORITHM Random(n, m, seed,a,b) //Generates a sequence of n pseudorandom numbers according to the linear // congruential method //Input: A positive integer n and positive integer parameters m, seed,a,b //Output: A sequence r1,...,rn of n pseudorandom integers uniformly // distributed among integer values between 0 and m − 1 //Note: Pseudorandom numbers between 0 and 1 can be obtained // by treating the integers generated as digits after the decimal point r0 ← seed for i ← 1 to n do ri ← (a ∗ ri−1 + b) mod m The simplicity of this pseudocode is misleading because the devil lies in the details of choosing the algorithm’s parameters. Here is a partial list of recommen- dations based on the results of a sophisticated mathematical analysis (see [KnuII, pp. 184–185] for details): seed may be chosen arbitrarily and is often set to the current date and time; m should be large and may be conveniently taken as 2w, where w is the computer’s word size; a should be selected as an integer between 0.01m and 0.99m with no particular pattern in its digits but such that a mod 8 = 5; and the value of b can be chosen as 1. The empirical data obtained as the result of an experiment need to be recorded and then presented for an analysis. Data can be presented numerically in a table or graphically in a scatterplot, i.e., by points in a Cartesian coordinate system. It is a good idea to use both these options whenever it is feasible because both methods have their unique strengths and weaknesses. 88 Fundamentals of the Analysis of Algorithm Efficiency The principal advantage of tabulated data lies in the opportunity to manip- ulate it easily. For example, one can compute the ratios M(n)/g(n) where g(n) is a candidate to represent the efficiency class of the algorithm in question. If the algorithm is indeed in (g(n)), most likely these ratios will converge to some pos- itive constant as n gets large. (Note that careless novices sometimes assume that this constant must be 1, which is, of course, incorrect according to the definition of (g(n)).) Or one can compute the ratios M(2n)/M(n) and see how the running time reacts to doubling of its input size. As we discussed in Section 2.2, such ratios should change only slightly for logarithmic algorithms and most likely converge to 2, 4, and 8 for linear, quadratic, and cubic algorithms, respectively—to name the most obvious and convenient cases. On the other hand, the form of a scatterplot may also help in ascertaining the algorithm’s probable efficiency class. For a logarithmic algorithm, the scat- terplot will have a concave shape (Figure 2.7a); this fact distinguishes it from all the other basic efficiency classes. For a linear algorithm, the points will tend to aggregate around a straight line or, more generally, to be contained between two straight lines (Figure 2.7b). Scatterplots of functions in (n lg n) and (n2) will have a convex shape (Figure 2.7c), making them difficult to differentiate. A scatterplot of a cubic algorithm will also have a convex shape, but it will show a much more rapid increase in the metric’s values. An exponential algorithm will most probably require a logarithmic scale for the vertical axis, in which the val- ues of loga M(n) rather than those of M(n) are plotted. (The commonly used logarithm base is 2 or 10.) In such a coordinate system, a scatterplot of a truly exponential algorithm should resemble a linear function because M(n) ≈ can im- plies logb M(n) ≈ logb c + n logb a, and vice versa. One of the possible applications of the empirical analysis is to predict the al- gorithm’s performance on an instance not included in the experiment sample. For example, if you observe that the ratios M(n)/g(n) are close to some constant c for the sample instances, it could be sensible to approximate M(n) by the prod- uct cg(n) for other instances, too. This approach should be used with caution, especially for values of n outside the sample range. (Mathematicians call such predictions extrapolation, as opposed to interpolation, which deals with values within the sample range.) Of course, you can try unleashing the standard tech- niques of statistical data analysis and prediction. Note, however, that the majority of such techniques are based on specific probabilistic assumptions that may or may not be valid for the experimental data in question. It seems appropriate to end this section by pointing out the basic differ- ences between mathematical and empirical analyses of algorithms. The princi- pal strength of the mathematical analysis is its independence of specific inputs; its principal weakness is its limited applicability, especially for investigating the average-case efficiency. The principal strength of the empirical analysis lies in its applicability to any algorithm, but its results can depend on the particular sample of instances and the computer used in the experiment. 2.6 Empirical Analysis of Algorithms 89 count or time n (b)(a) count or time n (c) count or time n FIGURE 2.7 Typical scatter plots. (a) Logarithmic. (b) Linear. (c) One of the convex functions. Exercises 2.6 1. Consider the following well-known sorting algorithm, which is studied later in the book, with a counter inserted to count the number of key comparisons. ALGORITHM SortAnalysis(A[0..n − 1]) //Input: An array A[0..n − 1] of n orderable elements //Output: The total number of key comparisons made count ← 0 for i ← 1 to n − 1 do 90 Fundamentals of the Analysis of Algorithm Efficiency v ← A[i] j ← i − 1 while j ≥ 0 and A[j] >vdo count ← count + 1 A[j + 1] ← A[j] j ← j − 1 A[j + 1] ← v return count Is the comparison counter inserted in the right place? If you believe it is, prove it; if you believe it is not, make an appropriate correction. 2. a. Run the program of Problem 1, with a properly inserted counter (or coun- ters) for the number of key comparisons, on 20 random arrays of sizes 1000, 2000, 3000,...,20,000. b. Analyze the data obtained to form a hypothesis about the algorithm’s average-case efficiency. c. Estimate the number of key comparisons we should expect for a randomly generated array of size 25,000 sorted by the same algorithm. 3. Repeat Problem 2 by measuring the program’s running time in milliseconds. 4. Hypothesize a likely efficiency class of an algorithm based on the following empirical observations of its basic operation’s count: size 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 count 11,966 24,303 39,992 53,010 67,272 78,692 91,274 113,063 129,799 140,538 5. What scale transformation will make a logarithmic scatterplot look like a linear one? 6. How can one distinguish a scatterplot for an algorithm in (lg lg n) from a scatterplot for an algorithm in (lg n)? 7. a. Find empirically the largest number of divisions made by Euclid’s algo- rithm for computing gcd(m, n) for 1≤ n ≤ m ≤ 100. b. For each positive integer k, find empirically the smallest pair of integers 1≤ n ≤ m ≤ 100 for which Euclid’s algorithm needs to make k divisions in order to find gcd(m, n). 8. The average-case efficiency of Euclid’s algorithm on inputs of size n can be measured by the average number of divisions Davg(n) made by the algorithm in computing gcd(n, 1), gcd(n, 2),...,gcd(n, n). For example, Davg(5) = 1 5 (1 + 2 + 3 + 2 + 1) = 1.8. 2.7 Algorithm Visualization 91 Produce a scatterplot of Davg(n) and indicate the algorithm’s likely average- case efficiency class. 9. Run an experiment to ascertain the efficiency class of the sieve of Eratos- thenes (see Section 1.1). 10. Run a timing experiment for the three algorithms for computing gcd(m, n) presented in Section 1.1. 2.7 Algorithm Visualization In addition to the mathematical and empirical analyses of algorithms, there is yet a third way to study algorithms. It is called algorithm visualization and can be defined as the use of images to convey some useful information about algorithms. That information can be a visual illustration of an algorithm’s operation, of its per- formance on different kinds of inputs, or of its execution speed versus that of other algorithms for the same problem. To accomplish this goal, an algorithm visualiza- tion uses graphic elements—points, line segments, two- or three-dimensional bars, and so on—to represent some “interesting events” in the algorithm’s operation. There are two principal variations of algorithm visualization: Static algorithm visualization Dynamic algorithm visualization, also called algorithm animation Static algorithm visualization shows an algorithm’s progress through a series of still images. Algorithm animation, on the other hand, shows a continuous, movie-like presentation of an algorithm’s operations. Animation is an arguably more sophisticated option, which, of course, is much more difficult to implement. Early efforts in the area of algorithm visualization go back to the 1970s. The watershed event happened in 1981 with the appearance of a 30-minute color sound film titled Sorting Out Sorting. This algorithm visualization classic was produced at the University of Toronto by Ronald Baecker with the assistance of D. Sherman [Bae81, Bae98]. It contained visualizations of nine well-known sorting algorithms (more than half of them are discussed later in the book) and provided quite a convincing demonstration of their relative speeds. The success of Sorting Out Sorting made sorting algorithms a perennial fa- vorite for algorithm animation. Indeed, the sorting problem lends itself quite naturally to visual presentation via vertical or horizontal bars or sticks of different heights or lengths, which need to be rearranged according to their sizes (Figure 2.8). This presentation is convenient, however, only for illustrating actions of a typical sorting algorithm on small inputs. For larger files, Sorting Out Sorting used the ingenious idea of presenting data by a scatterplot of points on a coordinate plane, with the first coordinate representing an item’s position in the file and the second one representing the item’s value; with such a representation, the process of sorting looks like a transformation of a “random” scatterplot of points into the points along a frame’s diagonal (Figure 2.9). In addition, most sorting algorithms 92 Fundamentals of the Analysis of Algorithm Efficiency FIGURE 2.8 Initial and final screens of a typical visualization of a sorting algorithm using the bar representation. work by comparing and exchanging two given items at a time—an event that can be animated relatively easily. Since the appearance of Sorting Out Sorting, a great number of algorithm animations have been created, especially after the appearance of Java and the 2.7 Algorithm Visualization 93 FIGURE 2.9 Initial and final screens of a typical visualization of a sorting algorithm using the scatterplot representation. World Wide Web in the 1990s. They range in scope from one particular algorithm to a group of algorithms for the same problem (e.g., sorting) or the same applica- tion area (e.g., geometric algorithms) to general-purpose animation systems. At the end of 2010, a catalog of links to existing visualizations, maintained under the 94 Fundamentals of the Analysis of Algorithm Efficiency NSF-supported AlgoVizProject, contained over 500 links. Unfortunately, a survey of existing visualizations found most of them to be of low quality, with the content heavily skewed toward easier topics such as sorting [Sha07]. There are two principal applications of algorithm visualization: research and education. Potential benefits for researchers are based on expectations that algo- rithm visualization may help uncover some unknown features of algorithms. For example, one researcher used a visualization of the recursive Tower of Hanoi algo- rithm in which odd- and even-numbered disks were colored in two different colors. He noticed that two disks of the same color never came in direct contact during the algorithm’s execution. This observation helped him in developing a better non- recursive version of the classic algorithm. To give another example, Bentley and McIlroy [Ben93] mentioned using an algorithm animation system in their work on improving a library implementation of a leading sorting algorithm. The application of algorithm visualization to education seeks to help students learning algorithms. The available evidence of its effectiveness is decisively mixed. Although some experiments did register positive learning outcomes, others failed to do so. The increasing body of evidence indicates that creating sophisticated software systems is not going to be enough. In fact, it appears that the level of student involvement with visualization might be more important than specific features of visualization software. In some experiments, low-tech visualizations prepared by students were more effective than passive exposure to sophisticated software systems. To summarize, although some successes in both research and education have been reported in the literature, they are not as impressive as one might expect. A deeper understanding of human perception of images will be required before the true potential of algorithm visualization is fulfilled. SUMMARY There are two kinds of algorithm efficiency: time efficiency and space efficiency. Time efficiency indicates how fast the algorithm runs; space efficiency deals with the extra space it requires. An algorithm’s time efficiency is principally measured as a function of its input size by counting the number of times its basic operation is executed. A basic operation is the operation that contributes the most to running time. Typically, it is the most time-consuming operation in the algorithm’s innermost loop. For some algorithms, the running time can differ considerably for inputs of the same size, leading to worst-case efficiency, average-case efficiency, and best-case efficiency. The established framework for analyzing time efficiency is primarily grounded in the order of growth of the algorithm’s running time as its input size goes to infinity. Summary 95 The notations O, , and are used to indicate and compare the asymptotic orders of growth of functions expressing algorithm efficiencies. The efficiencies of a large number of algorithms fall into the following few classes: constant, logarithmic, linear, linearithmic, quadratic, cubic, and exponential. The main tool for analyzing the time efficiency of a nonrecursive algorithm is to set up a sum expressing the number of executions of its basic operation and ascertain the sum’s order of growth. The main tool for analyzing the time efficiency of a recursive algorithm is to set up a recurrence relation expressing the number of executions of its basic operation and ascertain the solution’s order of growth. Succinctness of a recursive algorithm may mask its inefficiency. The Fibonacci numbers are an important sequence of integers in which every element is equal to the sum of its two immediate predecessors. There are several algorithms for computing the Fibonacci numbers, with drastically different efficiencies. Empirical analysis of an algorithm is performed by running a program implementing the algorithm on a sample of inputs and analyzing the data observed (the basic operation’s count or physical running time). This often involves generating pseudorandom numbers. The applicability to any algorithm is the principal strength of this approach; the dependence of results on the particular computer and instance sample is its main weakness. Algorithm visualization is the use of images to convey useful information about algorithms. The two principal variations of algorithm visualization are static algorithm visualization and dynamic algorithm visualization (also called algorithm animation). This page intentionally left blank 3 Brute Force and Exhaustive Search Science is as far removed from brute force as this sword from a crowbar. —Edward Lytton (1803–1873), Leila, Book II, Chapter I Doing a thing well is often a waste of time. —Robert Byrne, a master pool and billiards player and a writer After introducing the framework and methods for algorithm analysis in the preceding chapter, we are ready to embark on a discussion of algorithm design strategies. Each of the next eight chapters is devoted to a particular design strategy. The subject of this chapter is brute force and its important special case, exhaustive search. Brute force can be described as follows: Brute force is a straightforward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts involved. The “force” implied by the strategy’s definition is that of a computer and not that of one’s intellect. “Just do it!” would be another way to describe the prescription of the brute-force approach. And often, the brute-force strategy is indeed the one that is easiest to apply. As an example, consider the exponentiation problem: compute an for a nonzero number a and a nonnegative integer n. Although this problem might seem trivial, it provides a useful vehicle for illustrating several algorithm design strategies, including the brute force. (Also note that computing an mod m for some large integers is a principal component of a leading encryption algorithm.) By the definition of exponentiation, an = a ∗ ...∗ a   n times . 97 98 Brute Force and Exhaustive Search This suggests simply computing an by multiplying 1 by antimes. We have already encountered at least two brute-force algorithms in the book: the consecutive integer checking algorithm for computing gcd(m, n) in Section 1.1 and the definition-based algorithm for matrix multiplication in Section 2.3. Many other examples are given later in this chapter. (Can you identify a few algorithms you already know as being based on the brute-force approach?) Though rarely a source of clever or efficient algorithms, the brute-force ap- proach should not be overlooked as an important algorithm design strategy. First, unlike some of the other strategies, brute force is applicable to a very wide va- riety of problems. In fact, it seems to be the only general approach for which it is more difficult to point out problems it cannot tackle. Second, for some impor- tant problems—e.g., sorting, searching, matrix multiplication, string matching— the brute-force approach yields reasonable algorithms of at least some practi- cal value with no limitation on instance size. Third, the expense of designing a more efficient algorithm may be unjustifiable if only a few instances of a prob- lem need to be solved and a brute-force algorithm can solve those instances with acceptable speed. Fourth, even if too inefficient in general, a brute-force algo- rithm can still be useful for solving small-size instances of a problem. Finally, a brute-force algorithm can serve an important theoretical or educational pur- pose as a yardstick with which to judge more efficient alternatives for solving a problem. 3.1 Selection Sort and Bubble Sort In this section, we consider the application of the brute-force approach to the problem of sorting: given a list of n orderable items (e.g., numbers, characters from some alphabet, character strings), rearrange them in nondecreasing order. As we mentioned in Section 1.3, dozens of algorithms have been developed for solving this very important problem. You might have learned several of them in the past. If you have, try to forget them for the time being and look at the problem afresh. Now, after your mind is unburdened of previous knowledge of sorting algo- rithms, ask yourself a question: “What would be the most straightforward method for solving the sorting problem?” Reasonable people may disagree on the answer to this question. The two algorithms discussed here—selection sort and bubble sort—seem to be the two prime candidates. Selection Sort We start selection sort by scanning the entire given list to find its smallest element and exchange it with the first element, putting the smallest element in its final position in the sorted list. Then we scan the list, starting with the second element, to find the smallest among the last n − 1 elements and exchange it with the second element, putting the second smallest element in its final position. Generally, on the 3.1 Selection Sort and Bubble Sort 99 ith pass through the list, which we number from 0 to n − 2, the algorithm searches for the smallest item among the last n − i elements and swaps it with Ai: in their final positions A0 ≤ A1 ≤ . . . ≤ Ai–1 ⏐ Ai, . . . , Amin, . . . , An–1 the last n – i elements After n − 1 passes, the list is sorted. Here is pseudocode of this algorithm, which, for simplicity, assumes that the list is implemented as an array: ALGORITHM SelectionSort(A[0..n − 1]) //Sorts a given array by selection sort //Input: An array A[0..n − 1] of orderable elements //Output: Array A[0..n − 1] sorted in nondecreasing order for i ← 0 to n − 2 do min ← i for j ← i + 1 to n − 1 do if A[j] 1and n is a large positive integer, how would you circumvent the problem of a very large magnitude of an? 3. For each of the algorithms in Problems 4, 5, and 6 of Exercises 2.3, tell whether or not the algorithm is based on the brute-force approach. 4. a. Design a brute-force algorithm for computing the value of a polynomial p(x) = anxn + an−1xn−1 + ...+ a1x + a0 at a given point x0 and determine its worst-case efficiency class. b. If the algorithm you designed is in (n2), design a linear algorithm for this problem. c. Is it possible to design an algorithm with a better-than-linear efficiency for this problem? 5. A network topology specifies how computers, printers, and other devices are connected over a network. The figure below illustrates three common topologies of networks: the ring, the star, and the fully connected mesh. ring star fully connected mesh You are given a boolean matrix A[0..n − 1, 0..n − 1], where n>3, which is supposed to be the adjacency matrix of a graph modeling a network with one of these topologies. Your task is to determine which of these three topologies, if any, the matrix represents. Design a brute-force algorithm for this task and indicate its time efficiency class. 6. Tetromino tilings Tetrominoes are tiles made of four 1 × 1 squares. There are five types of tetrominoes shown below: 3.1 Selection Sort and Bubble Sort 103 straight tetromino square tetromino L-tetromino T-tetromino Z-tetromino Is it possible to tile—i.e., cover exactly without overlaps—an 8 × 8 chessboard with a. straight tetrominoes? b. square tetrominoes? c. L-tetrominoes? d. T-tetrominoes? e. Z-tetrominoes? 7. A stack of fake coins There are n stacks of n identical-looking coins. All of the coins in one of these stacks are counterfeit, while all the coins in the other stacks are genuine. Every genuine coin weighs 10 grams; every fake weighs 11 grams. You have an analytical scale that can determine the exact weight of any number of coins. a. Devise a brute-force algorithm to identify the stack with the fake coins and determine its worst-case efficiency class. b. What is the minimum number of weighings needed to identify the stack with the fake coins? 8. Sort the list E, X, A, M, P, L, E in alphabetical order by selection sort. 9. Is selection sort stable? (The definition of a stable sorting algorithm was given in Section 1.3.) 10. Is it possible to implement selection sort for linked lists with the same (n2) efficiency as the array version? 11. Sort the list E, X, A, M, P, L, E in alphabetical order by bubble sort. 12. a. Prove that if bubble sort makes no exchanges on its pass through a list, the list is sorted and the algorithm can be stopped. b. Write pseudocode of the method that incorporates this improvement. c. Prove that the worst-case efficiency of the improved version is quadratic. 13. Is bubble sort stable? 14. Alternating disks You have a row of 2n disks of two colors, n dark and n light. They alternate: dark, light, dark, light, and so on. You want to get all the dark disks to the right-hand end, and all the light disks to the left-hand end. The only moves you are allowed to make are those that interchange the positions of two neighboring disks. Design an algorithm for solving this puzzle and determine the number of moves it takes. [Gar99] 104 Brute Force and Exhaustive Search 3.2 Sequential Search and Brute-Force String Matching We saw in the previous section two applications of the brute-force approach to the sorting porblem. Here we discuss two applications of this strategy to the problem of searching. The first deals with the canonical problem of searching for an item of a given value in a given list. The second is different in that it deals with the string-matching problem. Sequential Search We have already encountered a brute-force algorithm for the general searching problem: it is called sequential search (see Section 2.1). To repeat, the algorithm simply compares successive elements of a given list with a given search key until either a match is encountered (successful search) or the list is exhausted without finding a match (unsuccessful search). A simple extra trick is often employed in implementing sequential search: if we append the search key to the end of the list, the search for the key will have to be successful, and therefore we can eliminate the end of list check altogether. Here is pseudocode of this enhanced version. ALGORITHM SequentialSearch2(A[0..n],K) //Implements sequential search with a search key as a sentinel //Input: An array A of n elements and a search key K //Output: The index of the first element in A[0..n − 1] whose value is // equal to K or −1 if no such element is found A[n] ← K i ← 0 while A[i] = K do i ← i + 1 if i2 points not all on the same line is a convex polygon with the vertices at some of the points of S. (If all the points do lie on the same line, the polygon degenerates to a line segment but still with the endpoints at two points of S.) 112 Brute Force and Exhaustive Search p7 p6 p2p8 p3 p4 p1 p5 FIGURE 3.6 The convex hull for this set of eight points is the convex polygon with vertices at p1,p5,p6,p7, and p3. The convex-hull problem is the problem of constructing the convex hull for a given set S of n points. To solve it, we need to find the points that will serve as the vertices of the polygon in question. Mathematicians call the vertices of such a polygon “extreme points.” By definition, an extreme point of a convex set is a point of this set that is not a middle point of any line segment with endpoints in the set. For example, the extreme points of a triangle are its three vertices, the extreme points of a circle are all the points of its circumference, and the extreme points of the convex hull of the set of eight points in Figure 3.6 are p1,p5,p6,p7, and p3. Extreme points have several special properties other points of a convex set do not have. One of them is exploited by the simplex method, a very important algorithm discussed in Section 10.1. This algorithm solves linear programming problems, which are problems of finding a minimum or a maximum of a linear function of n variables subject to linear constraints (see Problem 12 in this section’s exercises for an example and Sections 6.6 and 10.1 for a general discussion). Here, however, we are interested in extreme points because their identification solves the convex-hull problem. Actually, to solve this problem completely, we need to know a bit more than just which of n points of a given set are extreme points of the set’s convex hull: we need to know which pairs of points need to be connected to form the boundary of the convex hull. Note that this issue can also be addressed by listing the extreme points in a clockwise or a counterclockwise order. So how can we solve the convex-hull problem in a brute-force manner? If you do not see an immediate plan for a frontal attack, do not be dismayed: the convex- hull problem is one with no obvious algorithmic solution. Nevertheless, there is a simple but inefficient algorithm that is based on the following observation about line segments making up the boundary of a convex hull: a line segment connecting two points pi and pj of a set of n points is a part of the convex hull’s boundary if and 3.3 Closest-Pair and Convex-Hull Problems by Brute Force 113 only if all the other points of the set lie on the same side of the straight line through these two points.2 (Verify this property for the set in Figure 3.6.) Repeating this test for every pair of points yields a list of line segments that make up the convex hull’s boundary. A few elementary facts from analytical geometry are needed to implement this algorithm. First, the straight line through two points (x1,y1), (x2,y2) in the coordinate plane can be defined by the equation ax + by = c, where a = y2 − y1, b = x1 − x2, c = x1y2 − y1x2. Second, such a line divides the plane into two half-planes: for all the points in one of them, ax + by > c, while for all the points in the other, ax + by < c. (For the points on the line itself, of course, ax + by = c.) Thus, to check whether certain points lie on the same side of the line, we can simply check whether the expression ax + by − c has the same sign for each of these points. We leave the implementation details as an exercise. What is the time efficiency of this algorithm? It is in O(n3): for each of n(n − 1)/2 pairs of distinct points, we may need to find the sign of ax + by − c for each of the other n − 2 points. There are much more efficient algorithms for this important problem, and we discuss one of them later in the book. Exercises 3.3 1. Assuming that sqrt takes about 10 times longer than each of the other oper- ations in the innermost loop of BruteForceClosestPoints, which are assumed to take the same amount of time, estimate how much faster the algorithm will run after the improvement discussed in Section 3.3. 2. Can you design a more efficient algorithm than the one based on the brute- force strategy to solve the closest-pair problem for n points x1,x2,...,xn on the real line? 3. Let x1 1 points in the plane. 10. What modification needs to be made in the brute-force algorithm for the convex-hull problem to handle more than two points on the same straight line? 11. Write a program implementing the brute-force algorithm for the convex-hull problem. 12. Consider the following small instance of the linear programming problem: maximize 3x + 5y subject to x + y ≤ 4 x + 3y ≤ 6 x ≥ 0,y≥ 0. a. Sketch, in the Cartesian plane, the problem’s feasible region, defined as the set of points satisfying all the problem’s constraints. b. Identify the region’s extreme points. c. Solve this optimization problem by using the following theorem: A linear programming problem with a nonempty bounded feasible region always has a solution, which can be found at one of the extreme points of its feasible region. 3.4 Exhaustive Search Many important problems require finding an element with a special property in a domain that grows exponentially (or faster) with an instance size. Typically, such problems arise in situations that involve—explicitly or implicitly—combinatorial objects such as permutations, combinations, and subsets of a given set. Many such problems are optimization problems: they ask to find an element that maximizes or minimizes some desired characteristic such as a path length or an assignment cost. Exhaustive search is simply a brute-force approach to combinatorial prob- lems. It suggests generating each and every element of the problem domain, se- lecting those of them that satisfy all the constraints, and then finding a desired element (e.g., the one that optimizes some objective function). Note that although the idea of exhaustive search is quite straightforward, its implementation typically requires an algorithm for generating certain combinatorial objects. We delay a dis- cussion of such algorithms until the next chapter and assume here that they exist. 116 Brute Force and Exhaustive Search We illustrate exhaustive search by applying it to three important problems: the traveling salesman problem, the knapsack problem, and the assignment problem. Traveling Salesman Problem The traveling salesman problem (TSP) has been intriguing researchers for the last 150 years by its seemingly simple formulation, important applications, and interesting connections to other combinatorial problems. In layman’s terms, the problem asks to find the shortest tour through a given set of n cities that visits each city exactly once before returning to the city where it started. The problem can be conveniently modeled by a weighted graph, with the graph’s vertices representing the cities and the edge weights specifying the distances. Then the problem can be stated as the problem of finding the shortest Hamiltonian circuit of the graph. (A Hamiltonian circuit is defined as a cycle that passes through all the vertices of the graph exactly once. It is named after the Irish mathematician Sir William Rowan Hamilton (1805–1865), who became interested in such cycles as an application of his algebraic discoveries.) It is easy to see that a Hamiltonian circuit can also be defined as a sequence of n + 1 adjacent vertices vi0,vi1,...,vin−1,vi0, where the first vertex of the sequence is the same as the last one and all the other n − 1 vertices are distinct. Further, we can assume, with no loss of generality, that all circuits start and end at one particular vertex (they are cycles after all, are they not?). Thus, we can get all the tours by generating all the permutations of n − 1 intermediate cities, compute the tour lengths, and find the shortest among them. Figure 3.7 presents a small instance of the problem and its solution by this method. An inspection of Figure 3.7 reveals three pairs of tours that differ only by their direction. Hence, we could cut the number of vertex permutations by half. We could, for example, choose any two intermediate vertices, say, b and c, and then consider only permutations in which b precedes c. (This trick implicitly defines a tour’s direction.) This improvement cannot brighten the efficiency picture much, however. The total number of permutations needed is still 1 2 (n − 1)!, which makes the exhaustive-search approach impractical for all but very small values of n.Onthe other hand, if you always see your glass as half-full, you can claim that cutting the work by half is nothing to sneeze at, even if you solve a small instance of the problem, especially by hand. Also note that had we not limited our investigation to the circuits starting at the same vertex, the number of permutations would have been even larger, by a factor of n. Knapsack Problem Here is another well-known problem in algorithmics. Given n items of known weights w1,w2,...,wn and values v1,v2,...,vn and a knapsack of capacity W, find the most valuable subset of the items that fit into the knapsack. If you do not like the idea of putting yourself in the shoes of a thief who wants to steal the most 3.4 Exhaustive Search 117 2 5 8 7 3 1 a c b d a ---> b ---> c ---> d ---> a a ---> b ---> d ---> c ---> a a ---> c ---> b ---> d ---> a a ---> c ---> d ---> b ---> a a ---> d ---> b ---> c ---> a a ---> d ---> c ---> b ---> a I = 2 + 8 + 1 + 7 = 18 I = 2 + 3 + 1 + 5 = 11 optimal Tour Length optimal I = 5 + 8 + 3 + 7 = 23 I = 5 + 1 + 3 + 2 = 11 I = 7 + 3 + 8 + 5 = 23 I = 7 + 1 + 8 + 2 = 18 —— ——— FIGURE 3.7 Solution to a small instance of the traveling salesman problem by exhaustive search. valuable loot that fits into his knapsack, think about a transport plane that has to deliver the most valuable set of items to a remote location without exceeding the plane’s capacity. Figure 3.8a presents a small instance of the knapsack problem. The exhaustive-search approach to this problem leads to generating all the subsets of the set of n items given, computing the total weight of each subset in order to identify feasible subsets (i.e., the ones with the total weight not exceeding the knapsack capacity), and finding a subset of the largest value among them. As an example, the solution to the instance of Figure 3.8a is given in Figure 3.8b. Since the number of subsets of an n-element set is 2n, the exhaustive search leads to a (2n) algorithm, no matter how efficiently individual subsets are generated. Thus, for both the traveling salesman and knapsack problems considered above, exhaustive search leads to algorithms that are extremely inefficient on every input. In fact, these two problems are the best-known examples of so- called NP-hard problems. No polynomial-time algorithm is known for any NP- hard problem. Moreover, most computer scientists believe that such algorithms do not exist, although this very important conjecture has never been proven. More-sophisticated approaches—backtracking and branch-and-bound (see Sec- tions 12.1 and 12.2)—enable us to solve some but not all instances of these and 118 Brute Force and Exhaustive Search item 4item 3item 2item 1knapsack 10 w1 = 7 v1 = $42 w2 = 3 v2 = $12 w3 = 4 v3 = $40 w4 = 5 v4 = $25 (a) Subset Total weight Total value ∅ 0$0 {1} 7 $42 {2} 3 $12 {3} 4 $40 {4} 5 $25 {1, 2} 10 $54 {1, 3} 11 not feasible {1, 4} 12 not feasible {2, 3} 7 $52 {2, 4} 8 $37 3, 4  9 $65 {1, 2, 3} 14 not feasible {1, 2, 4} 15 not feasible {1, 3, 4} 16 not feasible {2, 3, 4} 12 not feasible {1, 2, 3, 4} 19 not feasible (b) FIGURE 3.8 (a) Instance of the knapsack problem. (b) Its solution by exhaustive search. The information about the optimal selection is in bold. 3.4 Exhaustive Search 119 similar problems in less than exponential time. Alternatively, we can use one of many approximation algorithms, such as those described in Section 12.3. Assignment Problem In our third example of a problem that can be solved by exhaustive search, there are n people who need to be assigned to execute n jobs, one person per job. (That is, each person is assigned to exactly one job and each job is assigned to exactly one person.) The cost that would accrue if the ith person is assigned to the jth job is a known quantity C[i, j] for each pair i, j = 1, 2,...,n. The problem is to find an assignment with the minimum total cost. A small instance of this problem follows, with the table entries representing the assignment costs C[i, j]: Job 1 Job 2 Job 3 Job 4 Person 1 9278 Person 2 6437 Person 3 5818 Person 4 7694 It is easy to see that an instance of the assignment problem is completely specified by its cost matrix C. In terms of this matrix, the problem is to select one element in each row of the matrix so that all selected elements are in different columns and the total sum of the selected elements is the smallest possible. Note that no obvious strategy for finding a solution works here. For example, we cannot select the smallest element in each row, because the smallest elements may happen to be in the same column. In fact, the smallest element in the entire matrix need not be a component of an optimal solution. Thus, opting for the exhaustive search may appear as an unavoidable evil. We can describe feasible solutions to the assignment problem as n-tuples j1,...,jn in which the ith component, i = 1,...,n, indicates the column of the element selected in the ith row (i.e., the job number assigned to the ith person). For example, for the cost matrix above, 2, 3, 4, 1 indicates the assignment of Person 1 to Job 2, Person 2 to Job 3, Person 3 to Job 4, and Person 4 to Job 1. The requirements of the assignment problem imply that there is a one-to-one correspondence between feasible assignments and permutations of the first n integers. Therefore, the exhaustive-search approach to the assignment problem would require generating all the permutations of integers 1, 2,...,n,computing the total cost of each assignment by summing up the corresponding elements of the cost matrix, and finally selecting the one with the smallest sum. A few first iterations of applying this algorithm to the instance given above are shown in Figure 3.9; you are asked to complete it in the exercises. 120 Brute Force and Exhaustive Search 9 6 5 7 2 4 8 6 7 3 1 9 8 7 8 4 C = <1, 2, 3, 4> <1, 2, 4, 3> <1, 3, 2, 4> <1, 3, 4, 2> <1, 4, 2, 3> <1, 4, 3, 2> cost = 9 + 4 + 1 + 4 = 18 cost = 9 + 4 + 8 + 9 = 30 cost = 9 + 3 + 8 + 4 = 24 cost = 9 + 3 + 8 + 6 = 26 cost = 9 + 7 + 8 + 9 = 33 cost = 9 + 7 + 1 + 6 = 23 etc. FIGURE 3.9 First few iterations of solving a small instance of the assignment problem by exhaustive search. Since the number of permutations to be considered for the general case of the assignment problem is n!, exhaustive search is impractical for all but very small instances of the problem. Fortunately, there is a much more efficient algorithm for this problem called the Hungarian method after the Hungarian mathematicians K¨onig and Egerv´ary, whose work underlies the method (see, e.g., [Kol95]). This is good news: the fact that a problem domain grows exponentially or faster does not necessarily imply that there can be no efficient algorithm for solving it. In fact, we present several other examples of such problems later in the book. However, such examples are more of an exception to the rule. More often than not, there are no known polynomial-time algorithms for problems whose domain grows exponentially with instance size, provided we want to solve them exactly. And, as we mentioned above, such algorithms quite possibly do not exist. Exercises 3.4 1. a. Assuming that each tour can be generated in constant time, what will be the efficiency class of the exhaustive-search algorithm outlined in the text for the traveling salesman problem? b. If this algorithm is programmed on a computer that makes ten billion additions per second, estimate the maximum number of cities for which the problem can be solved in i. 1 hour. ii. 24 hours. iii. 1 year. iv. 1 century. 2. Outline an exhaustive-search algorithm for the Hamiltonian circuit problem. 3. Outline an algorithm to determine whether a connected graph represented by its adjacency matrix has an Eulerian circuit. What is the efficiency class of your algorithm? 4. Complete the application of exhaustive search to the instance of the assign- ment problem started in the text. 5. Give an example of the assignment problem whose optimal solution does not include the smallest element of its cost matrix. 3.4 Exhaustive Search 121 6. Consider the partition problem: given n positive integers, partition them into two disjoint subsets with the same sum of their elements. (Of course, the prob- lem does not always have a solution.) Design an exhaustive-search algorithm for this problem. Try to minimize the number of subsets the algorithm needs to generate. 7. Consider the clique problem: given a graph G and a positive integer k, deter- mine whether the graph contains a clique of size k, i.e., a complete subgraph of k vertices. Design an exhaustive-search algorithm for this problem. 8. Explain how exhaustive search can be applied to the sorting problem and determine the efficiency class of such an algorithm. 9. Eight-queens problem Consider the classic puzzle of placing eight queens on an 8 × 8 chessboard so that no two queens are in the same row or in the same column or on the same diagonal. How many different positions are there so that a. no two queens are on the same square? b. no two queens are in the same row? c. no two queens are in the same row or in the same column? Also estimate how long it would take to find all the solutions to the problem by exhaustive search based on each of these approaches on a computer capable of checking 10 billion positions per second. 10. Magic squares A magic square of order n is an arrangement of the integers from 1 to n2 in an n × n matrix, with each number occurring exactly once, so that each row, each column, and each main diagonal has the same sum. a. Prove that if a magic square of order n exists, the sum in question must be equal to n(n2 + 1)/2. b. Design an exhaustive-search algorithm for generating all magic squares of order n. c. Go to the Internet or your library and find a better algorithm for generating magic squares. d. Implement the two algorithms—the exhaustive search and the one you have found—and run an experiment to determine the largest value of n for which each of the algorithms is able to find a magic square of order n in less than 1 minute on your computer. 11. Famous alphametic A puzzle in which the digits in a correct mathematical expression, such as a sum, are replaced by letters is called cryptarithm;if,in addition, the puzzle’s words make sense, it is said to be an alphametic.The most well-known alphametic was published by the renowned British puzzlist Henry E. Dudeney (1857–1930): 122 Brute Force and Exhaustive Search SEND + MORE MONEY Two conditions are assumed: first, the correspondence between letters and decimal digits is one-to-one, i.e., each letter represents one digit only and dif- ferent letters represent different digits. Second, the digit zero does not appear as the left-most digit in any of the numbers. To solve an alphametic means to find which digit each letter represents. Note that a solution’s uniqueness cannot be assumed and has to be verified by the solver. a. Write a program for solving cryptarithms by exhaustive search. Assume that a given cryptarithm is a sum of two words. b. Solve Dudeney’s puzzle the way it was expected to be solved when it was first published in 1924. 3.5 Depth-First Search and Breadth-First Search The term “exhaustive search” can also be applied to two very important algorithms that systematically process all vertices and edges of a graph. These two traversal algorithms are depth-first search (DFS) and breadth-first search (BFS). These algorithms have proved to be very useful for many applications involving graphs in artificial intelligence and operations research. In addition, they are indispensable for efficient investigation of fundamental properties of graphs such as connectivity and cycle presence. Depth-First Search Depth-first search starts a graph’s traversal at an arbitrary vertex by marking it as visited. On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent to the one it is currently in. (If there are several such vertices, a tie can be resolved arbitrarily. As a practical matter, which of the adjacent unvisited candidates is chosen is dictated by the data structure representing the graph. In our examples, we always break ties by the alphabetical order of the vertices.) This process continues until a dead end—a vertex with no adjacent unvisited vertices— is encountered. At a dead end, the algorithm backs up one edge to the vertex it came from and tries to continue visiting unvisited vertices from there. The algorithm eventually halts after backing up to the starting vertex, with the latter being a dead end. By then, all the vertices in the same connected component as the starting vertex have been visited. If unvisited vertices still remain, the depth-first search must be restarted at any one of them. It is convenient to use a stack to trace the operation of depth-first search. We push a vertex onto the stack when the vertex is reached for the first time (i.e., the 3.5 Depth-First Search and Breadth-First Search 123 g a d e b c f j h h i j ga c d f b e i (a) (b) (c) d3, 1 c2, 5 a1, 6 e6, 2 b5, 3 f4, 4 j10,7 i9, 8 h8, 9 g7,10 FIGURE 3.10 Example of a DFS traversal. (a) Graph. (b) Traversal’s stack (the first subscript number indicates the order in which a vertex is visited, i.e., pushed onto the stack; the second one indicates the order in which it becomes a dead-end, i.e., popped off the stack). (c) DFS forest with the tree and back edges shown with solid and dashed lines, respectively. visit of the vertex starts), and we pop a vertex off the stack when it becomes a dead end (i.e., the visit of the vertex ends). It is also very useful to accompany a depth-first search traversal by construct- ing the so-called depth-first search forest. The starting vertex of the traversal serves as the root of the first tree in such a forest. Whenever a new unvisited vertex is reached for the first time, it is attached as a child to the vertex from which it is being reached. Such an edge is called a tree edge because the set of all such edges forms a forest. The algorithm may also encounter an edge leading to a previously visited vertex other than its immediate predecessor (i.e., its parent in the tree). Such an edge is called a back edge because it connects a vertex to its ancestor, other than the parent, in the depth-first search forest. Figure 3.10 provides an ex- ample of a depth-first search traversal, with the traversal stack and corresponding depth-first search forest shown as well. Here is pseudocode of the depth-first search. ALGORITHM DFS(G) //Implements a depth-first search traversal of a given graph //Input: Graph G = V,E //Output: Graph G with its vertices marked with consecutive integers // in the order they are first encountered by the DFS traversal mark each vertex in V with 0 as a mark of being “unvisited” count ← 0 for each vertex v in V do if v is marked with 0 dfs(v) 124 Brute Force and Exhaustive Search dfs(v) //visits recursively all the unvisited vertices connected to vertex v //by a path and numbers them in the order they are encountered //via global variable count count ← count + 1; mark v with count for each vertex w in V adjacent to v do if w is marked with 0 dfs(w) The brevity of the DFS pseudocode and the ease with which it can be per- formed by hand may create a wrong impression about the level of sophistication of this algorithm. To appreciate its true power and depth, you should trace the algorithm’s action by looking not at a graph’s diagram but at its adjacency matrix or adjacency lists. (Try it for the graph in Figure 3.10 or a smaller example.) How efficient is depth-first search? It is not difficult to see that this algorithm is, in fact, quite efficient since it takes just the time proportional to the size of the data structure used for representing the graph in question. Thus, for the adjacency matrix representation, the traversal time is in (|V |2), and for the adjacency list representation, it is in (|V |+|E|) where |V | and |E| are the number of the graph’s vertices and edges, respectively. A DFS forest, which is obtained as a by-product of a DFS traversal, deserves a few comments, too. To begin with, it is not actually a forest. Rather, we can look at it as the given graph with its edges classified by the DFS traversal into two disjoint classes: tree edges and back edges. (No other types are possible for a DFS forest of an undirected graph.) Again, tree edges are edges used by the DFS traversal to reach previously unvisited vertices. If we consider only the edges in this class, we will indeed get a forest. Back edges connect vertices to previously visited vertices other than their immediate predecessors in the traversal. They connect vertices to their ancestors in the forest other than their parents. A DFS traversal itself and the forest-like representation of the graph it pro- vides have proved to be extremely helpful for the development of efficient al- gorithms for checking many important properties of graphs.3 Note that the DFS yields two orderings of vertices: the order in which the vertices are reached for the first time (pushed onto the stack) and the order in which the vertices become dead ends (popped off the stack). These orders are qualitatively different, and various applications can take advantage of either of them. Important elementary applications of DFS include checking connectivity and checking acyclicity of a graph. Since dfs halts after visiting all the vertices con- 3. The discovery of several such applications was an important breakthrough achieved by the two American computer scientists John Hopcroft and Robert Tarjan in the 1970s. For this and other contributions, they were given the Turing Award—the most prestigious prize in the computing field [Hop87, Tar87]. 3.5 Depth-First Search and Breadth-First Search 125 nected by a path to the starting vertex, checking a graph’s connectivity can be done as follows. Start a DFS traversal at an arbitrary vertex and check, after the algorithm halts, whether all the vertices of the graph will have been vis- ited. If they have, the graph is connected; otherwise, it is not connected. More generally, we can use DFS for identifying connected components of a graph (how?). As for checking for a cycle presence in a graph, we can take advantage of the graph’s representation in the form of a DFS forest. If the latter does not have back edges, the graph is clearly acyclic. If there is a back edge from some vertex u to its ancestor v (e.g., the back edge from d to a in Figure 3.10c), the graph has a cycle that comprises the path from v to u via a sequence of tree edges in the DFS forest followed by the back edge from u to v. You will find a few other applications of DFS later in the book, although more sophisticated applications, such as finding articulation points of a graph, are not included. (A vertex of a connected graph is said to be its articulation point if its removal with all edges incident to it breaks the graph into disjoint pieces.) Breadth-First Search If depth-first search is a traversal for the brave (the algorithm goes as far from “home” as it can), breadth-first search is a traversal for the cautious. It proceeds in a concentric manner by visiting first all the vertices that are adjacent to a starting vertex, then all unvisited vertices two edges apart from it, and so on, until all the vertices in the same connected component as the starting vertex are visited. If there still remain unvisited vertices, the algorithm has to be restarted at an arbitrary vertex of another connected component of the graph. It is convenient to use a queue (note the difference from depth-first search!) to trace the operation of breadth-first search. The queue is initialized with the traversal’s starting vertex, which is marked as visited. On each iteration, the algorithm identifies all unvisited vertices that are adjacent to the front vertex, marks them as visited, and adds them to the queue; after that, the front vertex is removed from the queue. Similarly to a DFS traversal, it is useful to accompany a BFS traversal by con- structing the so-called breadth-first search forest. The traversal’s starting vertex serves as the root of the first tree in such a forest. Whenever a new unvisited vertex is reached for the first time, the vertex is attached as a child to the vertex it is being reached from with an edge called a tree edge. If an edge leading to a previously visited vertex other than its immediate predecessor (i.e., its parent in the tree) is encountered, the edge is noted as a cross edge. Figure 3.11 provides an exam- ple of a breadth-first search traversal, with the traversal queue and corresponding breadth-first search forest shown. 126 Brute Force and Exhaustive Search g a d e b c f j h h i j ga c d f b e i (a) (b) (c) a1 c2 d3 e4 f5 b6 g7 h8 j9 i10 FIGURE 3.11 Example of a BFS traversal. (a) Graph. (b) Traversal queue, with the numbers indicating the order in which the vertices are visited, i.e., added to (and removed from) the queue. (c) BFS forest with the tree and cross edges shown with solid and dotted lines, respectively. Here is pseudocode of the breadth-first search. ALGORITHM BFS(G) //Implements a breadth-first search traversal of a given graph //Input: Graph G = V,E //Output: Graph G with its vertices marked with consecutive integers // in the order they are visited by the BFS traversal mark each vertex in V with 0 as a mark of being “unvisited” count ← 0 for each vertex v in V do if v is marked with 0 bfs(v) bfs(v) //visits all the unvisited vertices connected to vertex v //by a path and numbers them in the order they are visited //via global variable count count ← count + 1; mark v with count and initialize a queue with v while the queue is not empty do for each vertex w in V adjacent to the front vertex do if w is marked with 0 count ← count + 1; mark w with count add w to the queue remove the front vertex from the queue 3.5 Depth-First Search and Breadth-First Search 127 a a e e b b f f c cg g d d h (a) (b) FIGURE 3.12 Illustration of the BFS-based algorithm for finding a minimum-edge path. (a) Graph. (b) Part of its BFS tree that identifies the minimum-edge path from a to g. Breadth-first search has the same efficiency as depth-first search: it is in (|V |2) for the adjacency matrix representation and in (|V |+|E|) for the adja- cency list representation. Unlike depth-first search, it yields a single ordering of vertices because the queue is a FIFO (first-in first-out) structure and hence the order in which vertices are added to the queue is the same order in which they are removed from it. As to the structure of a BFS forest of an undirected graph, it can also have two kinds of edges: tree edges and cross edges. Tree edges are the ones used to reach previously unvisited vertices. Cross edges connect vertices to those visited before, but, unlike back edges in a DFS tree, they connect vertices either on the same or adjacent levels of a BFS tree. BFS can be used to check connectivity and acyclicity of a graph, essentially in the same manner as DFS can. It is not applicable, however, for several less straightforward applications such as finding articulation points. On the other hand, it can be helpful in some situations where DFS cannot. For example, BFS can be used for finding a path with the fewest number of edges between two given vertices. To do this, we start a BFS traversal at one of the two vertices and stop it as soon as the other vertex is reached. The simple path from the root of the BFS tree to the second vertex is the path sought. For example, path a − b − c − g in the graph in Figure 3.12 has the fewest number of edges among all the paths between vertices a and g. Although the correctness of this application appears to stem immediately from the way BFS operates, a mathematical proof of its validity is not quite elementary (see, e.g., [Cor09, Section 22.2]). Table 3.1 summarizes the main facts about depth-first search and breadth-first search. 128 Brute Force and Exhaustive Search TABLE 3.1 Main facts about depth-first search (DFS) and breadth-first search (BFS) DFS BFS Data structure a stack a queue Number of vertex orderings two orderings one ordering Edge types (undirected graphs) tree and back edges tree and cross edges Applications connectivity, connectivity, acyclicity, acyclicity, articulation points minimum-edge paths Efficiency for adjacency matrix (|V 2|)(|V 2|) Efficiency for adjacency lists (|V |+|E|)(|V |+|E|) Exercises 3.5 1. Consider the following graph. bf c g ead a. Write down the adjacency matrix and adjacency lists specifying this graph. (Assume that the matrix rows and columns and vertices in the adjacency lists follow in the alphabetical order of the vertex labels.) b. Starting at vertex a and resolving ties by the vertex alphabetical order, traverse the graph by depth-first search and construct the corresponding depth-first search tree. Give the order in which the vertices were reached for the first time (pushed onto the traversal stack) and the order in which the vertices became dead ends (popped off the stack). 2. If we define sparse graphs as graphs for which |E|∈O(|V |), which implemen- tation of DFS will have a better time efficiency for such graphs, the one that uses the adjacency matrix or the one that uses the adjacency lists? 3. Let G be a graph with n vertices and m edges. a. True or false: All its DFS forests (for traversals starting at different ver- tices) will have the same number of trees? b. True or false: All its DFS forests will have the same number of tree edges and the same number of back edges? 4. Traverse the graph of Problem 1 by breadth-first search and construct the corresponding breadth-first search tree. Start the traversal at vertex a and resolve ties by the vertex alphabetical order. 3.5 Depth-First Search and Breadth-First Search 129 5. Prove that a cross edge in a BFS tree of an undirected graph can connect vertices only on either the same level or on two adjacent levels of a BFS tree. 6. a. Explain how one can check a graph’s acyclicity by using breadth-first search. b. Does either of the two traversals—DFS or BFS—always find a cycle faster than the other? If you answer yes, indicate which of them is better and explain why it is the case; if you answer no, give two examples supporting your answer. 7. Explain how one can identify connected components of a graph by using a. a depth-first search. b. a breadth-first search. 8. A graph is said to be bipartite if all its vertices can be partitioned into two disjoint subsets X and Y so that every edge connects a vertex in X with a vertex in Y. (One can also say that a graph is bipartite if its vertices can be colored in two colors so that every edge has its vertices colored in different colors; such graphs are also called 2-colorable.) For example, graph (i) is bipartite while graph (ii) is not. x1 y1 x3 y2 x2 y3 (i) (ii) a c b d a. Design a DFS-based algorithm for checking whether a graph is bipartite. b. Design a BFS-based algorithm for checking whether a graph is bipartite. 9. Write a program that, for a given graph, outputs: a. vertices of each connected component b. its cycle or a message that the graph is acyclic 10. One can model a maze by having a vertex for a starting point, a finishing point, dead ends, and all the points in the maze where more than one path can be taken, and then connecting the vertices according to the paths in the maze. a. Construct such a graph for the following maze. 130 Brute Force and Exhaustive Search b. Which traversal—DFS or BFS—would you use if you found yourself in a maze and why? 11. Three Jugs Sim´eon Denis Poisson (1781–1840), a famous French mathemati- cian and physicist, is said to have become interested in mathematics after encountering some version of the following old puzzle. Given an 8-pint jug full of water and two empty jugs of 5- and 3-pint capacity, get exactly 4 pints of water in one of the jugs by completely filling up and/or emptying jugs into others. Solve this puzzle by using breadth-first search. SUMMARY Brute force is a straightforward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts involved. The principal strengths of the brute-force approach are wide applicability and simplicity; its principal weakness is the subpar efficiency of most brute-force algorithms. A first application of the brute-force approach often results in an algorithm that can be improved with a modest amount of effort. The following noted algorithms can be considered as examples of the brute- force approach: . definition-based algorithm for matrix multiplication . selection sort . sequential search . straightforward string-matching algorithm Exhaustive search is a brute-force approach to combinatorial problems. It suggests generating each and every combinatorial object of the problem, selecting those of them that satisfy all the constraints, and then finding a desired object. The traveling salesman problem, the knapsack problem, and the assignment problem are typical examples of problems that can be solved, at least theoretically, by exhaustive-search algorithms. Exhaustive search is impractical for all but very small instances of problems it can be applied to. Depth-first search (DFS) and breadth-first search (BFS) are two principal graph-traversal algorithms. By representing a graph in a form of a depth-first or breadth-first search forest, they help in the investigation of many important properties of the graph. Both algorithms have the same time efficiency: (|V |2) for the adjacency matrix representation and (|V |+|E|) for the adjacency list representation. 4 Decrease-and-Conquer Plutarch says that Sertorius, in order to teach his soldiers that perseverance and wit are better than brute force, had two horses brought before them, and set two men to pull out their tails. One of the men was a burly Hercules, who tugged and tugged, but all to no purpose; the other was a sharp, weasel- faced tailor, who plucked one hair at a time, amidst roars of laughter, and soon left the tail quite bare. —E. Cobham Brewer, Dictionary of Phrase and Fable, 1898 The decrease-and-conquer technique is based on exploiting the relationship between a solution to a given instance of a problem and a solution to its smaller instance. Once such a relationship is established, it can be exploited either top down or bottom up. The former leads naturally to a recursive implementa- tion, although, as one can see from several examples in this chapter, an ultimate implementation may well be nonrecursive. The bottom-up variation is usually implemented iteratively, starting with a solution to the smallest instance of the problem; it is called sometimes the incremental approach. There are three major variations of decrease-and-conquer: decrease by a constant decrease by a constant factor variable size decrease In the decrease-by-a-constant variation, the size of an instance is reduced by the same constant on each iteration of the algorithm. Typically, this constant is equal to one (Figure 4.1), although other constant size reductions do happen occasionally. Consider, as an example, the exponentiation problem of computing an where a = 0 and n is a nonnegative integer. The relationship between a solution to an instance of size n and an instance of size n − 1 is obtained by the obvious formula an = an−1 . a. So the function f (n) = an can be computed either “top down” by using its recursive definition 131 132 Decrease-and-Conquer problem of size n subproblem of size n –1 solution to the subproblem solution to the original problem FIGURE 4.1 Decrease-(by one)-and-conquer technique. f (n) =  f(n− 1) . a if n>0, 1ifn = 0, (4.1) or “bottom up” by multiplying 1 by antimes. (Yes, it is the same as the brute-force algorithm, but we have come to it by a different thought process.) More interesting examples of decrease-by-one algorithms appear in Sections 4.1–4.3. The decrease-by-a-constant-factor technique suggests reducing a problem instance by the same constant factor on each iteration of the algorithm. In most applications, this constant factor is equal to two. (Can you give an example of such an algorithm?) The decrease-by-half idea is illustrated in Figure 4.2. For an example, let us revisit the exponentiation problem. If the instance of size n is to compute an, the instance of half its size is to compute an/2, with the obvious relationship between the two: an = (an/2)2. But since we consider here instances with integer exponents only, the former does not work for odd n.Ifn is odd, we have to compute an−1 by using the rule for even-valued exponents and then multiply the result by a. To summarize, we have the following formula: Decrease-and-Conquer 133 problem of size n subproblem of size n/2 solution to the subproblem solution to the original problem FIGURE 4.2 Decrease-(by half)-and-conquer technique. an = ⎧ ⎨ ⎩ (an/2)2 if n is even and positive, (a(n−1)/2)2 . a if n is odd, 1ifn = 0. (4.2) If we compute an recursively according to formula (4.2) and measure the algo- rithm’s efficiency by the number of multiplications, we should expect the algorithm to be in (log n) because, on each iteration, the size is reduced by about a half at the expense of one or two multiplications. A few other examples of decrease-by-a-constant-factor algorithms are given in Section 4.4 and its exercises. Such algorithms are so efficient, however, that there are few examples of this kind. Finally, in the variable-size-decrease variety of decrease-and-conquer, the size-reduction pattern varies from one iteration of an algorithm to another. Eu- clid’s algorithm for computing the greatest common divisor provides a good ex- ample of such a situation. Recall that this algorithm is based on the formula gcd(m, n) = gcd(n, m mod n). 134 Decrease-and-Conquer Though the value of the second argument is always smaller on the right-hand side than on the left-hand side, it decreases neither by a constant nor by a constant factor. A few other examples of such algorithms appear in Section 4.5. 4.1 Insertion Sort In this section, we consider an application of the decrease-by-one technique to sorting an array A[0..n − 1]. Following the technique’s idea, we assume that the smaller problem of sorting the array A[0..n − 2] has already been solved to give us a sorted array of size n − 1: A[0] ≤ ...≤ A[n − 2]. How can we take advantage of this solution to the smaller problem to get a solution to the original problem by taking into account the element A[n − 1]? Obviously, all we need is to find an appropriate position for A[n − 1] among the sorted elements and insert it there. This is usually done by scanning the sorted subarray from right to left until the first element smaller than or equal to A[n − 1] is encountered to insert A[n − 1] right after that element. The resulting algorithm is called straight insertion sort or simply insertion sort. Though insertion sort is clearly based on a recursive idea, it is more efficient to implement this algorithm bottom up, i.e., iteratively. As shown in Figure 4.3, starting with A[1]and ending with A[n − 1],A[i]is inserted in its appropriate place among the first i elements of the array that have been already sorted (but, unlike selection sort, are generally not in their final positions). Here is pseudocode of this algorithm. ALGORITHM InsertionSort(A[0..n − 1]) //Sorts a given array by insertion sort //Input: An array A[0..n − 1] of n orderable elements //Output: Array A[0..n − 1] sorted in nondecreasing order for i ← 1 to n − 1 do v ← A[i] j ← i − 1 while j ≥ 0 and A[j] >vdo A[j + 1] ← A[j] j ← j − 1 A[j + 1] ← v A[0] ≤ . . . ≤ A[ j] < A[ j + 1] ≤ . . . ≤ A[i – 1] ⏐ A[i] . . . A[n – 1] smaller than or equal to A[i] greater than A[i] FIGURE 4.3 Iteration of insertion sort: A[i] is inserted in its proper position among the preceding elements previously sorted. 4.1 Insertion Sort 135 89 | 45 45 45 29 29 17 45 89 | 68 68 45 34 29 68 68 89 | 89 68 45 34 90 90 90 90 | 89 68 45 29 29 29 29 90 | 89 68 34 34 34 34 34 90 | 89 17 17 17 17 17 17 90 FIGURE 4.4 Example of sorting with insertion sort. A vertical bar separates the sorted part of the array from the remaining elements; the element being inserted is in bold. The operation of the algorithm is illustrated in Figure 4.4. The basic operation of the algorithm is the key comparison A[j]>v.(Whynot j ≥ 0? Because it is almost certainly faster than the former in an actual computer implementation. Moreover, it is not germane to the algorithm: a better imple- mentation with a sentinel—see Problem 8 in this section’s exercises—eliminates it altogether.) The number of key comparisons in this algorithm obviously depends on the nature of the input. In the worst case, A[j] >v is executed the largest number of times, i.e., for every j = i − 1,...,0. Since v = A[i], it happens if and only if A[j] >A[i] for j = i − 1,...,0. (Note that we are using the fact that on the ith iteration of insertion sort all the elements preceding A[i] are the first i elements in the input, albeit in the sorted order.) Thus, for the worst-case input, we get A[0] > A[1] (for i = 1), A[1] >A[2] (for i = 2),...,A[n − 2] >A[n − 1] (for i = n − 1). In other words, the worst-case input is an array of strictly decreasing values. The number of key comparisons for such an input is Cworst(n) = n−1 i=1 i−1 j=0 1 = n−1 i=1 i = (n − 1)n 2 ∈ (n2). Thus, in the worst case, insertion sort makes exactly the same number of compar- isons as selection sort (see Section 3.1). In the best case, the comparison A[j] >v is executed only once on every iteration of the outer loop. It happens if and only if A[i − 1] ≤ A[i] for every i = 1,...,n− 1, i.e., if the input array is already sorted in nondecreasing order. (Though it “makes sense” that the best case of an algorithm happens when the problem is already solved, it is not always the case, as you are going to see in our discussion of quicksort in Chapter 5.) Thus, for sorted arrays, the number of key comparisons is Cbest(n) = n−1 i=1 1 = n − 1 ∈ (n). 136 Decrease-and-Conquer This very good performance in the best case of sorted arrays is not very useful by itself, because we cannot expect such convenient inputs. However, almost-sorted files do arise in a variety of applications, and insertion sort preserves its excellent performance on such inputs. A rigorous analysis of the algorithm’s average-case efficiency is based on investigating the number of element pairs that are out of order (see Problem 11 in this section’s exercises). It shows that on randomly ordered arrays, insertion sort makes on average half as many comparisons as on decreasing arrays, i.e., Cavg(n) ≈ n2 4 ∈ (n2). This twice-as-fast average-case performance coupled with an excellent efficiency on almost-sorted arrays makes insertion sort stand out among its principal com- petitors among elementary sorting algorithms, selection sort and bubble sort. In addition, its extension named shellsort, after its inventor D. L. Shell [She59], gives us an even better algorithm for sorting moderately large files (see Problem 12 in this section’s exercises). Exercises 4.1 1. Ferrying soldiers A detachment of n soldiers must cross a wide and deep river with no bridge in sight. They notice two 12-year-old boys playing in a rowboat by the shore. The boat is so tiny, however, that it can only hold two boys or one soldier. How can the soldiers get across the river and leave the boys in joint possession of the boat? How many times need the boat pass from shore to shore? 2. Alternating glasses a. There are 2n glasses standing next to each other in a row, the first n of them filled with a soda drink and the remaining n glasses empty. Make the glasses alternate in a filled-empty-filled-empty pattern in the minimum number of glass moves. [Gar78] b. Solve the same problem if 2n glasses—n with a drink and n empty—are initially in a random order. 3. Marking cells Design an algorithm for the following task. For any even n, mark n cells on an infinite sheet of graph paper so that each marked cell has an odd number of marked neighbors. Two cells are considered neighbors if they are next to each other either horizontally or vertically but not diagonally. The marked cells must form a contiguous region, i.e., a region in which there is a path between any pair of marked cells that goes through a sequence of marked neighbors. [Kor05] 4.1 Insertion Sort 137 4. Design a decrease-by-one algorithm for generating the power set of a set of n elements. (The power set of a set S is the set of all the subsets of S, including the empty set and S itself.) 5. Consider the following algorithm to check connectivity of a graph defined by its adjacency matrix. ALGORITHM Connected(A[0..n − 1, 0..n − 1]) //Input: Adjacency matrix A[0..n − 1, 0..n − 1]) of an undirected graph G //Output: 1 (true) if G is connected and 0 (false) if it is not if n = 1 return 1 //one-vertex graph is connected by definition else if not Connected(A[0..n − 2, 0..n − 2]) return 0 else for j ← 0 to n − 2 do if A[n − 1,j] return 1 return 0 Does this algorithm work correctly for every undirected graph with n>0 vertices? If you answer yes, indicate the algorithm’s efficiency class in the worst case; if you answer no, explain why. 6. Team ordering You have the results of a completed round-robin tournament in which n teams played each other once. Each game ended either with a victory for one of the teams or with a tie. Design an algorithm that lists the teams in a sequence so that every team did not lose the game with the team listed immediately after it. What is the time efficiency class of your algorithm? 7. Apply insertion sort to sort the list E, X, A, M, P , L, E in alphabetical order. 8. a. What sentinel should be put before the first element of an array being sorted in order to avoid checking the in-bound condition j ≥ 0 on each iteration of the inner loop of insertion sort? b. Is the sentinel version in the same efficiency class as the original version? 9. Is it possible to implement insertion sort for sorting linked lists? Will it have the same O(n2) time efficiency as the array version? 10. Compare the text’s implementation of insertion sort with the following ver- sion. ALGORITHM InsertSort2(A[0..n − 1]) for i ← 1 to n − 1 do j ← i − 1 while j ≥ 0 and A[j] >A[j + 1] do swap(A[j], A[j + 1]) j ← j − 1 138 Decrease-and-Conquer What is the time efficiency of this algorithm? How is it compared to that of the version given in Section 4.1? 11. Let A[0..n − 1] be an array of n sortable elements. (For simplicity, you may assume that all the elements are distinct.) A pair (A[i],A[j]) is called an inversion if iA[j]. a. What arrays of size n have the largest number of inversions and what is this number? Answer the same questions for the smallest number of inversions. b. Show that the average-case number of key comparisons in insertion sort is given by the formula Cavg(n) ≈ n2 4 . 12. Shellsort (more accurately Shell’s sort) is an important sorting algorithm that works by applying insertion sort to each of several interleaving sublists of a given list. On each pass through the list, the sublists in question are formed by stepping through the list with an increment hi taken from some predefined decreasing sequence of step sizes, h1 > ...>hi > ...> 1, which must end with 1. (The algorithm works for any such sequence, though some sequences are known to yield a better efficiency than others. For example, the sequence 1, 4,13,40,121,...,used, of course, in reverse, is known to be among the best for this purpose.) a. Apply shellsort to the list S, H, E, L, L, S, O, R, T, I, S, U, S, E, F, U, L b. Is shellsort a stable sorting algorithm? c. Implement shellsort, straight insertion sort, selection sort, and bubble sort in the language of your choice and compare their performance on random arrays of sizes 10n for n = 2, 3, 4, 5, and 6 as well as on increasing and decreasing arrays of these sizes. 4.2 Topological Sorting In this section, we discuss an important problem for directed graphs, with a variety of applications involving prerequisite-restricted tasks. Before we pose this problem, though, let us review a few basic facts about directed graphs themselves. A directed graph,ordigraph for short, is a graph with directions specified for all its edges (Figure 4.5a is an example). The adjacency matrix and adjacency lists are still two principal means of representing a digraph. There are only two notable differences between undirected and directed graphs in representing them: (1) the adjacency matrix of a directed graph does not have to be symmetric; (2) an edge in a directed graph has just one (not two) corresponding nodes in the digraph’s adjacency lists. 4.2 Topological Sorting 139 b bc cd d e e (a) (b) a a FIGURE 4.5 (a) Digraph. (b) DFS forest of the digraph for the DFS traversal started at a. Depth-first search and breadth-first search are principal traversal algorithms for traversing digraphs as well, but the structure of corresponding forests can be more complex than for undirected graphs. Thus, even for the simple example of Figure 4.5a, the depth-first search forest (Figure 4.5b) exhibits all four types of edges possible in a DFS forest of a directed graph: tree edges (ab, bc, de), back edges (ba) from vertices to their ancestors, forward edges (ac) from vertices to their descendants in the tree other than their children, and cross edges (dc), which are none of the aforementioned types. Note that a back edge in a DFS forest of a directed graph can connect a vertex to its parent. Whether or not it is the case, the presence of a back edge indicates that the digraph has a directed cycle. A directed cycle in a digraph is a sequence of three or more of its vertices that starts and ends with the same vertex and in which every vertex is connected to its immediate predecessor by an edge directed from the predecessor to the successor. For example, a, b, a is a directed cycle in the digraph in Figure 4.5a. Conversely, if a DFS forest of a digraph has no back edges, the digraph is a dag, an acronym for directed acyclic graph. Edge directions lead to new questions about digraphs that are either meaning- less or trivial for undirected graphs. In this section, we discuss one such question. As a motivating example, consider a set of five required courses {C1, C2, C3, C4, C5} a part-time student has to take in some degree program. The courses can be taken in any order as long as the following course prerequisites are met: C1 and C2 have no prerequisites, C3 requires C1 and C2, C4 requires C3, and C5 requires C3 and C4. The student can take only one course per term. In which order should the student take the courses? The situation can be modeled by a digraph in which vertices represent courses and directed edges indicate prerequisite requirements (Figure 4.6). In terms of this digraph, the question is whether we can list its vertices in such an order that for every edge in the graph, the vertex where the edge starts is listed before the vertex where the edge ends. (Can you find such an ordering of this digraph’s vertices?) This problem is called topological sorting. It can be posed for an 140 Decrease-and-Conquer C1 C4 C2 C5 C3 FIGURE 4.6 Digraph representing the prerequisite structure of five courses. C1 C51 C42 C33 C14 C1 C3 C4 C5C25 (a) (b) (c) The popping-off order: C5, C4, C3, C1, C2 The topologically sorted list: C2 C3 C4 C5C2 FIGURE 4.7 (a) Digraph for which the topological sorting problem needs to be solved. (b) DFS traversal stack with the subscript numbers indicating the popping- off order. (c) Solution to the problem. arbitrary digraph, but it is easy to see that the problem cannot have a solution if a digraph has a directed cycle. Thus, for topological sorting to be possible, a digraph in question must be a dag. It turns out that being a dag is not only necessary but also sufficient for topological sorting to be possible; i.e., if a digraph has no directed cycles, the topological sorting problem for it has a solution. Moreover, there are two efficient algorithms that both verify whether a digraph is a dag and, if it is, produce an ordering of vertices that solves the topological sorting problem. The first algorithm is a simple application of depth-first search: perform a DFS traversal and note the order in which vertices become dead-ends (i.e., popped off the traversal stack). Reversing this order yields a solution to the topological sorting problem, provided, of course, no back edge has been encountered during the traversal. If a back edge has been encountered, the digraph is not a dag, and topological sorting of its vertices is impossible. Why does the algorithm work? When a vertex v is popped off a DFS stack, no vertex u with an edge from u to v can be among the vertices popped off before v. (Otherwise, (u, v) would have been a back edge.) Hence, any such vertex u will be listed after v in the popped-off order list, and before v in the reversed list. Figure 4.7 illustrates an application of this algorithm to the digraph in Fig- ure 4.6. Note that in Figure 4.7c, we have drawn the edges of the digraph, and they all point from left to right as the problem’s statement requires. It is a con- venient way to check visually the correctness of a solution to an instance of the topological sorting problem. 4.2 Topological Sorting 141 C1 delete C1 delete C2 delete C3 The solution obtained is C1, C2, C3, C4, C5 delete C4 delete C5 C4 C4 C4 C5 C5 C5 C5 C3 C4 C5 C3 C2 C3 C2 FIGURE 4.8 Illustration of the source-removal algorithm for the topological sorting problem. On each iteration, a vertex with no incoming edges is deleted from the digraph. The second algorithm is based on a direct implementation of the decrease-(by one)-and-conquer technique: repeatedly, identify in a remaining digraph a source, which is a vertex with no incoming edges, and delete it along with all the edges outgoing from it. (If there are several sources, break the tie arbitrarily. If there are none, stop because the problem cannot be solved—see Problem 6a in this section’s exercises.) The order in which the vertices are deleted yields a solution to the topological sorting problem. The application of this algorithm to the same digraph representing the five courses is given in Figure 4.8. Note that the solution obtained by the source-removal algorithm is different from the one obtained by the DFS-based algorithm. Both of them are correct, of course; the topological sorting problem may have several alternative solutions. The tiny size of the example we used might create a wrong impression about the topological sorting problem. But imagine a large project—e.g., in construction, research, or software development—that involves a multitude of interrelated tasks with known prerequisites. The first thing to do in such a situation is to make sure that the set of given prerequisites is not contradictory. The convenient way of doing this is to solve the topological sorting problem for the project’s digraph. Only then can one start thinking about scheduling tasks to, say, minimize the total completion time of the project. This would require, of course, other algorithms that you can find in general books on operations research or in special ones on CPM (Critical Path Method) and PERT (Program Evaluation and Review Technique) methodologies. As to applications of topological sorting in computer science, they include instruction scheduling in program compilation, cell evaluation ordering in spread- sheet formulas, and resolving symbol dependencies in linkers. 142 Decrease-and-Conquer Exercises 4.2 1. Apply the DFS-based algorithm to solve the topological sorting problem for the following digraphs: a a b cb c e g gfe f d d (b)(a) 2. a. Prove that the topological sorting problem has a solution if and only if it is a dag. b. For a digraph with n vertices, what is the largest number of distinct solutions the topological sorting problem can have? 3. a. What is the time efficiency of the DFS-based algorithm for topological sorting? b. How can one modify the DFS-based algorithm to avoid reversing the vertex ordering generated by DFS? 4. Can one use the order in which vertices are pushed onto the DFS stack (instead of the order they are popped off it) to solve the topological sorting problem? 5. Apply the source-removal algorithm to the digraphs of Problem 1 above. 6. a. Prove that a nonempty dag must have at least one source. b. How would you find a source (or determine that such a vertex does not exist) in a digraph represented by its adjacency matrix? What is the time efficiency of this operation? c. How would you find a source (or determine that such a vertex does not exist) in a digraph represented by its adjacency lists? What is the time efficiency of this operation? 7. Can you implement the source-removal algorithm for a digraph represented by its adjacency lists so that its running time is in O(|V |+|E|)? 8. Implement the two topological sorting algorithms in the language of your choice. Run an experiment to compare their running times. 9. A digraph is called strongly connected if for any pair of two distinct vertices u and v there exists a directed path from u to v and a directed path from v to u. In general, a digraph’s vertices can be partitioned into disjoint maximal subsets of vertices that are mutually accessible via directed paths; these subsets are called strongly connected components of the digraph. There are two DFS- 4.2 Topological Sorting 143 based algorithms for identifying strongly connected components. Here is the simpler (but somewhat less efficient) one of the two: Step 1 Perform a DFS traversal of the digraph given and number its vertices in the order they become dead ends. Step 2 Reverse the directions of all the edges of the digraph. Step 3 Perform a DFS traversal of the new digraph by starting (and, if necessary, restarting) the traversal at the highest numbered vertex among still unvisited vertices. The strongly connected components are exactly the vertices of the DFS trees obtained during the last traversal. a. Apply this algorithm to the following digraph to determine its strongly connected components: a b c g h d e f b. What is the time efficiency class of this algorithm? Give separate answers for the adjacency matrix representation and adjacency list representation of an input digraph. c. How many strongly connected components does a dag have? 10. Spider’s web A spider sits at the bottom (point S) of its web, and a fly sits at the top (F). How many different ways can the spider reach the fly by moving along the web’s lines in the directions indicated by the arrows? [Kor05] F S 144 Decrease-and-Conquer 4.3 Algorithms for Generating Combinatorial Objects In this section, we keep our promise to discuss algorithms for generating combi- natorial objects. The most important types of combinatorial objects are permuta- tions, combinations, and subsets of a given set. They typically arise in problems that require a consideration of different choices. We already encountered them in Chapter 3 when we discussed exhaustive search. Combinatorial objects are stud- ied in a branch of discrete mathematics called combinatorics. Mathematicians, of course, are primarily interested in different counting formulas; we should be grate- ful for such formulas because they tell us how many items need to be generated. In particular, they warn us that the number of combinatorial objects typically grows exponentially or even faster as a function of the problem size. But our primary interest here lies in algorithms for generating combinatorial objects, not just in counting them. Generating Permutations We start with permutations. For simplicity, we assume that the underlying set whose elements need to be permuted is simply the set of integers from 1 to n; more generally, they can be interpreted as indices of elements in an n-element set {a1,...,an}. What would the decrease-by-one technique suggest for the problem of generating all n! permutations of {1,...,n}? The smaller-by-one problem is to generate all (n − 1)! permutations. Assuming that the smaller problem is solved, we can get a solution to the larger one by inserting n in each of the n possible positions among elements of every permutation of n − 1 elements. All the permu- tations obtained in this fashion will be distinct (why?), and their total number will be n(n − 1)!= n!. Hence, we will obtain all the permutations of {1,...,n}. We can insert n in the previously generated permutations either left to right or right to left. It turns out that it is beneficial to start with inserting n into 12 ...(n− 1) by moving right to left and then switch direction every time a new permutation of {1,...,n− 1} needs to be processed. An example of applying this approach bottom up for n = 3 is given in Figure 4.9. The advantage of this order of generating permutations stems from the fact that it satisfies the minimal-change requirement: each permutation can be ob- tained from its immediate predecessor by exchanging just two elements in it. (For the method being discussed, these two elements are always adjacent to each other. start 1 insert 2 into 1 right to left 12 21 insert 3 into 12 right to left 123 132 312 insert 3 into 21 left to right 321 231 213 FIGURE 4.9 Generating permutations bottom up. 4.3 Algorithms for Generating Combinatorial Objects 145 Check this for the permutations generated in Figure 4.9.) The minimal-change re- quirement is beneficial both for the algorithm’s speed and for applications using the permutations. For example, in Section 3.4, we needed permutations of cities to solve the traveling salesman problem by exhaustive search. If such permuta- tions are generated by a minimal-change algorithm, we can compute the length of a new tour from the length of its predecessor in constant rather than linear time (how?). It is possible to get the same ordering of permutations of n elements without explicitly generating permutations for smaller values of n. It can be done by associating a direction with each element k in a permutation. We indicate such a direction by a small arrow written above the element in question, e.g., 3 → 2 ← 4 → 1 ← . The element k is said to be mobile in such an arrow-marked permutation if its arrow points to a smaller number adjacent to it. For example, for the permutation 3 → 2 ← 4 → 1 ← , 3 and 4 are mobile while 2 and 1 are not. Using the notion of a mobile element, we can give the following description of the Johnson-Trotter algorithm for generating permutations. ALGORITHM JohnsonTrotter(n) //Implements Johnson-Trotter algorithm for generating permutations //Input: A positive integer n //Output: A list of all permutations of {1,...,n} initialize the first permutation with 1 ← 2 ← ... n ← while the last permutation has a mobile element do find its largest mobile element k swap k with the adjacent element k’s arrow points to reverse the direction of all the elements that are larger than k add the new permutation to the list Here is an application of this algorithm for n = 3, with the largest mobile element shown in bold: 1 ← 2 ← 3 ← 1 ← 3 ← 2 ← 3 ← 1 ← 2 ← 3 → 2 ← 1 ← 2 ← 3 → 1 ← 2 ← 1 ← 3 → . This algorithm is one of the most efficient for generating permutations; it can be implemented to run in time proportional to the number of permutations, i.e., in (n!). Of course, it is horribly slow for all but very small values of n; however, this is not the algorithm’s “fault” but rather the fault of the problem: it simply asks to generate too many items. One can argue that the permutation ordering generated by the Johnson- Trotter algorithm is not quite natural; for example, the natural place for permu- tation n(n − 1)...1 seems to be the last one on the list. This would be the case if permutations were listed in increasing order—also called the lexicographic or- 146 Decrease-and-Conquer der—which is the order in which they would be listed in a dictionary if the numbers were interpreted as letters of an alphabet. For example, for n = 3, 123 132 213 231 312 321. So how can we generate the permutation following a1a2 ...an−1an in lexi- cographic order? If an−1 an, we find the permutation’s longest decreasing suffix ai+1 >ai+2 > ...>an (but ai ai+2 > ...>an find the largest index j such that ai A[m]: K  A[0] ...A[m − 1]   search here if KA[m] . As an example, let us apply binary search to searching for K = 70 in the array 3 142731394255707481859398 The iterations of the algorithm are given in the following table: 3 142731394255707481859398 0123456789101112index value iteration 1 iteration 2 iteration 3 l m r l m r l,m r Though binary search is clearly based on a recursive idea, it can be easily implemented as a nonrecursive algorithm, too. Here is pseudocode of this nonre- cursive version. 4.4 Decrease-by-a-Constant-Factor Algorithms 151 ALGORITHM BinarySearch(A[0..n − 1],K) //Implements nonrecursive binary search //Input: An array A[0..n − 1] sorted in ascending order and // a search key K //Output: An index of the array’s element that is equal to K // or −1 if there is no such element l ← 0; r ← n − 1 while l ≤ r do m ←(l + r)/2 if K = A[m] return m else if K1,Cworst(1) = 1. (4.3) (Stop and convince yourself that n/2 must be, indeed, rounded down and that the initial condition must be written as specified.) We already encountered recurrence (4.3), with a different initial condition, in Section 2.4 (see recurrence (2.4) and its solution there for n = 2k). For the initial condition Cworst(1) = 1, we obtain Cworst(2k) = k + 1 = log2 n + 1. (4.4) Further, similarly to the case of recurrence (2.4) (Problem 7 in Exercises 2.4), the solution given by formula (4.4) for n = 2k can be tweaked to get a solution valid for an arbitrary positive integer n: Cworst(n) =log2 n+1 =log2(n + 1). (4.5) Formula (4.5) deserves attention. First, it implies that the worst-case time efficiency of binary search is in (log n). Second, it is the answer we should have 152 Decrease-and-Conquer fully expected: since the algorithm simply reduces the size of the remaining array by about half on each iteration, the number of such iterations needed to reduce the initial size n to the final size 1 has to be about log2 n. Third, to reiterate the point made in Section 2.1, the logarithmic function grows so slowly that its values remain small even for very large values of n. In particular, according to formula (4.5), it will take no more than log2(103 + 1)=10 three-way comparisons to find an element of a given value (or establish that there is no such element) in any sorted array of one thousand elements, and it will take no more than log2(106 + 1)=20 comparisons to do it for any sorted array of size one million! What can we say about the average-case efficiency of binary search? A so- phisticated analysis shows that the average number of key comparisons made by binary search is only slightly smaller than that in the worst case: Cavg(n) ≈ log2 n. (More accurate formulas for the average number of comparisons in a successful and an unsuccessful search are Cyes avg(n) ≈ log2 n − 1 and Cno avg(n) ≈ log2(n + 1), respectively.) Though binary search is an optimal searching algorithm if we restrict our op- erations only to comparisons between keys (see Section 11.2), there are searching algorithms (see interpolation search in Section 4.5 and hashing in Section 7.3) with a better average-case time efficiency, and one of them (hashing) does not even re- quire the array to be sorted! These algorithms do require some special calculations in addition to key comparisons, however. Finally, the idea behind binary search has several applications beyond searching (see, e.g., [Ben00]). In addition, it can be applied to solving nonlinear equations in one unknown; we discuss this continuous analogue of binary search, called the method of bisection, in Section 12.4. Fake-Coin Problem Of several versions of the fake-coin identification problem, we consider here the one that best illustrates the decrease-by-a-constant-factor strategy. Among n identical-looking coins, one is fake. With a balance scale, we can compare any two sets of coins. That is, by tipping to the left, to the right, or staying even, the balance scale will tell whether the sets weigh the same or which of the sets is heavier than the other but not by how much. The problem is to design an efficient algorithm for detecting the fake coin. An easier version of the problem—the one we discuss here—assumes that the fake coin is known to be, say, lighter than the genuine one.1 The most natural idea for solving this problem is to divide n coins into two piles of n/2 coins each, leaving one extra coin aside if n is odd, and put the two 1. A much more challenging version assumes no additional information about the relative weights of the fake and genuine coins or even the presence of the fake coin among n given coins. We pursue this more difficult version in the exercises for Section 11.2. 4.4 Decrease-by-a-Constant-Factor Algorithms 153 piles on the scale. If the piles weigh the same, the coin put aside must be fake; otherwise, we can proceed in the same manner with the lighter pile, which must be the one with the fake coin. We can easily set up a recurrence relation for the number of weighings W(n) needed by this algorithm in the worst case: W(n) = W(n/2) + 1 for n>1,W(1) = 0. This recurrence should look familiar to you. Indeed, it is almost identical to the one for the worst-case number of comparisons in binary search. (The difference is in the initial condition.) This similarity is not really surprising, since both algorithms are based on the same technique of halving an instance size. The solution to the recurrence for the number of weighings is also very similar to the one we had for binary search: W(n) =log2 n. This stuff should look elementary by now, if not outright boring. But wait: the interesting point here is the fact that the above algorithm is not the most efficient solution. It would be more efficient to divide the coins not into two but into three piles of about n/3 coins each. (Details of a precise formulation are developed in this section’s exercises. Do not miss it! If your instructor forgets, demand the instructor to assign Problem 10.) After weighing two of the piles, we can reduce the instance size by a factor of three. Accordingly, we should expect the number of weighings to be about log3 n, which is smaller than log2 n. Russian Peasant Multiplication Now we consider a nonorthodox algorithm for multiplying two positive integers called multiplication `a la russe or the Russian peasant method. Let n and m be positive integers whose product we want to compute, and let us measure the instance size by the value of n. Now, if n is even, an instance of half the size has to deal with n/2, and we have an obvious formula relating the solution to the problem’s larger instance to the solution to the smaller one: n . m = n 2 . 2m. If n is odd, we need only a slight adjustment of this formula: n . m = n − 1 2 . 2m + m. Using these formulas and the trivial case of 1 . m = m to stop, we can compute product n . m either recursively or iteratively. An example of computing 50 . 65 with this algorithm is given in Figure 4.11. Note that all the extra addends shown in parentheses in Figure 4.11a are in the rows that have odd values in the first column. Therefore, we can find the product by simply adding all the elements in the m column that have an odd number in the n column (Figure 4.11b). Also note that the algorithm involves just the simple operations of halving, doubling, and adding—a feature that might be attractive, for example, to those 154 Decrease-and-Conquer nm nm 50 65 50 65 25 130 25 130 130 12 260 (+130) 12 260 6 520 6 520 3 1040 3 1040 1040 1 2080 (+1040) 1 2080 2080 2080 +(130 + 1040) = 3250 3250 (a) (b) FIGURE 4.11 Computing 50 . 65 by the Russian peasant method. who do not want to memorize the table of multiplications. It is this feature of the algorithm that most probably made it attractive to Russian peasants who, accord- ing to Western visitors, used it widely in the nineteenth century and for whom the method is named. (In fact, the method was known to Egyptian mathematicians as early as 1650 b.c. [Cha98, p. 16].) It also leads to very fast hardware implementa- tion since doubling and halving of binary numbers can be performed using shifts, which are among the most basic operations at the machine level. Josephus Problem Our last example is the Josephus problem, named for Flavius Josephus, a famous Jewish historian who participated in and chronicled the Jewish revolt of 66–70 c.e. against the Romans. Josephus, as a general, managed to hold the fortress of Jotapata for 47 days, but after the fall of the city he took refuge with 40 diehards in a nearby cave. There, the rebels voted to perish rather than surrender. Josephus proposed that each man in turn should dispatch his neighbor, the order to be determined by casting lots. Josephus contrived to draw the last lot, and, as one of the two surviving men in the cave, he prevailed upon his intended victim to surrender to the Romans. So let n people numbered 1 to n stand in a circle. Starting the grim count with person number 1, we eliminate every second person until only one survivor is left. The problem is to determine the survivor’s number J (n). For example (Figure 4.12), if n is 6, people in positions 2, 4, and 6 will be eliminated on the first pass through the circle, and people in initial positions 3 and 1 will be eliminated on the second pass, leaving a sole survivor in initial position 5—thus, J(6) = 5. To give another example, if n is 7, people in positions 2, 4, 6, and 1 will be eliminated on the first pass (it is more convenient to include 1 in the first pass) and people in positions 5 and, for convenience, 3 on the second—thus, J(7) = 7. 4.4 Decrease-by-a-Constant-Factor Algorithms 155 12 (a) (b) 41 325 2161 11 3261 217 4152 FIGURE 4.12 Instances of the Josephus problem for (a) n = 6 and (b) n = 7. Subscript numbers indicate the pass on which the person in that position is eliminated. The solutions are J(6) = 5 and J(7) = 7, respectively. It is convenient to consider the cases of even and odd n’s separately. If n is even, i.e., n = 2k, the first pass through the circle yields an instance of exactly the same problem but half its initial size. The only difference is in position numbering; for example, a person in initial position 3 will be in position 2 for the second pass, a person in initial position 5 will be in position 3, and so on (check Figure 4.12a). It is easy to see that to get the initial position of a person, we simply need to multiply his new position by 2 and subtract 1. This relationship will hold, in particular, for the survivor, i.e., J(2k) = 2J(k)− 1. Let us now consider the case of an odd n (n>1), i.e., n = 2k + 1. The first pass eliminates people in all even positions. If we add to this the elimination of the person in position 1 right after that, we are left with an instance of size k. Here, to get the initial position that corresponds to the new position numbering, we have to multiply the new position number by 2 and add 1 (check Figure 4.12b). Thus, for odd values of n,weget J(2k + 1) = 2J(k)+ 1. Can we get a closed-form solution to the two-case recurrence subject to the initial condition J(1) = 1? The answer is yes, though getting it requires more ingenuity than just applying backward substitutions. In fact, one way to find a solution is to apply forward substitutions to get, say, the first 15 values of J (n), discern a pattern, and then prove its general validity by mathematical induction. We leave the execution of this plan to the exercises; alternatively, you can look it up in [GKP94], whose exposition of the Josephus problem we have been following. Interestingly, the most elegant form of the closed-form answer involves the binary representation of size n: J (n) can be obtained by a 1-bit cyclic shift left of n itself! For example, J(6) = J(1102) = 1012 = 5 and J(7) = J(1112) = 1112 = 7. 156 Decrease-and-Conquer Exercises 4.4 1. Cutting a stick A stick n inches long needs to be cut into n 1-inch pieces. Outline an algorithm that performs this task with the minimum number of cuts if several pieces of the stick can be cut at the same time. Also give a formula for the minimum number of cuts. 2. Design a decrease-by-half algorithm for computing log2 n and determine its time efficiency. 3. a. What is the largest number of key comparisons made by binary search in searching for a key in the following array? 3 14 27 31 39 42 55 70 74 81 85 93 98 b. List all the keys of this array that will require the largest number of key comparisons when searched for by binary search. c. Find the average number of key comparisons made by binary search in a successful search in this array. Assume that each key is searched for with the same probability. d. Find the average number of key comparisons made by binary search in an unsuccessful search in this array. Assume that searches for keys in each of the 14 intervals formed by the array’s elements are equally likely. 4. Estimate how many times faster an average successful search will be in a sorted array of one million elements if it is done by binary search versus sequential search. 5. The time efficiency of sequential search does not depend on whether a list is implemented as an array or as a linked list. Is it also true for searching a sorted list by binary search? 6. a. Design a version of binary search that uses only two-way comparisons such as ≤ and =. Implement your algorithm in the language of your choice and carefully debug it: such programs are notorious for being prone to bugs. b. Analyze the time efficiency of the two-way comparison version designed in part a. 7. Picture guessing A version of the popular problem-solving task involves pre- senting people with an array of 42 pictures—seven rows of six pictures each— and asking them to identify the target picture by asking questions that can be answered yes or no. Further, people are then required to identify the picture with as few questions as possible. Suggest the most efficient algorithm for this problem and indicate the largest number of questions that may be necessary. 8. Consider ternary search—the following algorithm for searching in a sorted array A[0..n − 1]. If n = 1, simply compare the search key K with the single 4.5 Variable-Size-Decrease Algorithms 157 element of the array; otherwise, search recursively by comparing K with A[n/3], and if K is larger, compare it with A[2n/3] to determine in which third of the array to continue the search. a. What design technique is this algorithm based on? b. Set up a recurrence for the number of key comparisons in the worst case. You may assume that n = 3k. c. Solve the recurrence for n = 3k. d. Compare this algorithm’s efficiency with that of binary search. 9. An array A[0..n − 2] contains n − 1 integers from 1 to n in increasing order. (Thus one integer in this range is missing.) Design the most efficient algorithm you can to find the missing integer and indicate its time efficiency. 10. a. Write pseudocode for the divide-into-three algorithm for the fake-coin problem. Make sure that your algorithm handles properly all values of n, not only those that are multiples of 3. b. Set up a recurrence relation for the number of weighings in the divide-into- three algorithm for the fake-coin problem and solve it for n = 3k. c. For large values of n, about how many times faster is this algorithm than the one based on dividing coins into two piles? Your answer should not depend on n. 11. a. Apply the Russian peasant algorithm to compute 26 . 47. b. From the standpoint of time efficiency, does it matter whether we multiply n by m or m by n by the Russian peasant algorithm? 12. a. Write pseudocode for the Russian peasant multiplication algorithm. b. What is the time efficiency class of Russian peasant multiplication? 13. Find J(40)—the solution to the Josephus problem for n = 40. 14. Prove that the solution to the Josephus problem is 1 for every n that is a power of 2. 15. For the Josephus problem, a. compute J (n) for n = 1, 2,...,15. b. discern a pattern in the solutions for the first fifteen values of n and prove its general validity. c. prove the validity of getting J (n) by a 1-bit cyclic shift left of the binary representation of n. 4.5 Variable-Size-Decrease Algorithms In the third principal variety of decrease-and-conquer, the size reduction pattern varies from one iteration of the algorithm to another. Euclid’s algorithm for computing the greatest common divisor (Section 1.1) provides a good example 158 Decrease-and-Conquer of this kind of algorithm. In this section, we encounter a few more examples of this variety. Computing a Median and the Selection Problem The selection problem is the problem of finding the kth smallest element in a list of n numbers. This number is called the kth order statistic. Of course, for k = 1or k = n, we can simply scan the list in question to find the smallest or largest element, respectively. A more interesting case of this problem is for k =n/2, which asks to find an element that is not larger than one half of the list’s elements and not smaller than the other half. This middle value is called the median, and it is one of the most important notions in mathematical statistics. Obviously, we can find the kth smallest element in a list by sorting the list first and then selecting the kth element in the output of a sorting algorithm. The time of such an algorithm is determined by the efficiency of the sorting algorithm used. Thus, with a fast sorting algorithm such as mergesort (discussed in the next chapter), the algorithm’s efficiency is in O(n log n). You should immediately suspect, however, that sorting the entire list is most likely overkill since the problem asks not to order the entire list but just to find its kth smallest element. Indeed, we can take advantage of the idea of partitioning a given list around some value p of, say, its first element. In general, this is a rearrangement of the list’s elements so that the left part contains all the elements smaller than or equal to p, followed by the pivot p itself, followed by all the elements greater than or equal to p. all are ≤ p all are ≥ ppp Of the two principal algorithmic alternatives to partition an array, here we discuss the Lomuto partitioning [Ben00, p. 117]; we introduce the better known Hoare’s algorithm in the next chapter. To get the idea behind the Lomuto parti- tioning, it is helpful to think of an array—or, more generally, a subarray A[l..r] (0 ≤ l ≤ r ≤ n − 1)—under consideration as composed of three contiguous seg- ments. Listed in the order they follow pivot p, they are as follows: a segment with elements known to be smaller than p,the segment of elements known to be greater than or equal to p, and the segment of elements yet to be compared to p (see Fig- ure 4.13a). Note that the segments can be empty; for example, it is always the case for the first two segments before the algorithm starts. Starting with i = l + 1, the algorithm scans the subarray A[l..r] left to right, maintaining this structure until a partition is achieved. On each iteration, it com- pares the first element in the unknown segment (pointed to by the scanning index i in Figure 4.13a) with the pivot p. If A[i] ≥ p, i is simply incremented to expand the segment of the elements greater than or equal to p while shrinking the un- processed segment. If A[i] k− 1, the kth smallest element in the entire array can be found as the kth smallest element in the left part of the partitioned array. And if sl+ k − 1 Quickselect(A[l..s − 1],k) else Quickselect(A[s + 1..r],k− 1 − s) In fact, the same idea can be implemented without recursion as well. For the nonrecursive version, we need not even adjust the value of k but just continue until s = k − 1. EXAMPLE Apply the partition-based algorithm to find the median of the fol- lowing list of nine numbers: 4, 1, 10, 8, 7, 12, 9, 2, 15. Here, k =9/2=5 and our task is to find the 5th smallest element in the array. We use the above version of array partitioning, showing the pivots in bold. 012345678 si 4 11087129215 si 4 11087129215 si 4 11087129215 si 4 1 2 871291015 si 4 1 2 871291015 214 871291015 Since s = 2 is smaller than k − 1 = 4, we proceed with the right part of the array: 4.5 Variable-Size-Decrease Algorithms 161 012345678 si 8 71291015 si 8 71291015 si 8 71291015 7 8 1291015 Now s = k − 1 = 4, and hence we can stop: the found median is 8, which is greater than 2, 1, 4, and 7 but smaller than 12, 9, 10, and 15. How efficient is quickselect? Partitioning an n-element array always requires n − 1 key comparisons. If it produces the split that solves the selection problem without requiring more iterations, then for this best case we obtain Cbest(n) = n − 1 ∈ (n). Unfortunately, the algorithm can produce an extremely unbalanced partition of a given array, with one part being empty and the other containing n − 1 elements. In the worst case, this can happen on each of the n − 1 iterations. (For a specific example of the worst-case input, consider, say, the case of k = n and a strictly increasing array.) This implies that Cworst(n) = (n − 1) + (n − 2) + ...+ 1 = (n − 1)n/2 ∈ (n2), which compares poorly with the straightforward sorting-based approach men- tioned in the beginning of our selection problem discussion. Thus, the usefulness of the partition-based algorithm depends on the algorithm’s efficiency in the average case. Fortunately, a careful mathematical analysis has shown that the average-case efficiency is linear. In fact, computer scientists have discovered a more sophisti- cated way of choosing a pivot in quickselect that guarantees linear time even in the worst case [Blo73], but it is too complicated to be recommended for practical applications. It is also worth noting that the partition-based algorithm solves a somewhat more general problem of identifying the k smallest and n − k largest elements of a given list, not just the value of its kth smallest element. Interpolation Search As the next example of a variable-size-decrease algorithm, we consider an algo- rithm for searching in a sorted array called interpolation search. Unlike binary search, which always compares a search key with the middle value of a given sorted array (and hence reduces the problem’s instance size by half), interpolation search takes into account the value of the search key in order to find the array’s element to be compared with the search key. In a sense, the algorithm mimics the way we 162 Decrease-and-Conquer value v A[r ] A[l ] l xr index FIGURE 4.14 Index computation in interpolation search. search for a name in a telephone book: if we are searching for someone named Brown, we open the book not in the middle but very close to the beginning, unlike our action when searching for someone named, say, Smith. More precisely, on the iteration dealing with the array’s portion between the leftmost element A[l] and the rightmost element A[r], the algorithm assumes that the array values increase linearly, i.e., along the straight line through the points (l, A[l]) and (r, A[r]). (The accuracy of this assumption can influence the algorithm’s efficiency but not its correctness.) Accordingly, the search key’s value v is compared with the element whose index is computed as (the round-off of) the x coordinate of the point on the straight line through the points (l, A[l]) and (r, A[r]) whose y coordinate is equal to the search value v (Figure 4.14). Writing down a standard equation for the straight line passing through the points (l, A[l]) and (r, A[r]), substituting v for y, and solving it for x leads to the following formula: x = l + (v − A[l])(r − l) A[r] − A[l] ! . (4.6) The logic behind this approach is quite straightforward. We know that the array values are increasing (more accurately, not decreasing) from A[l]toA[r], but we do not know how they do it. Had these values increased linearly, which is the simplest manner possible, the index computed by formula (4.4) would be the expected location of the array’s element with the value equal to v. Of course, if v is not between A[l] and A[r], formula (4.4) need not be applied (why?). After comparing v with A[x], the algorithm either stops (if they are equal) or proceeds by searching in the same manner among the elements indexed either 4.5 Variable-Size-Decrease Algorithms 163 between l and x − 1 or between x + 1 and r, depending on whether A[x] is smaller or larger than v. Thus, the size of the problem’s instance is reduced, but we cannot tell a priori by how much. The analysis of the algorithm’s efficiency shows that interpolation search uses fewer than log2 log2 n + 1 key comparisons on the average when searching in a list of n random keys. This function grows so slowly that the number of comparisons is a very small constant for all practically feasible inputs (see Problem 6 in this section’s exercises). But in the worst case, interpolation search is only linear, which must be considered a bad performance (why?). Assessing the worthiness of interpolation search versus that of binary search, Robert Sedgewick wrote in the second edition of his Algorithms that binary search is probably better for smaller files but interpolation search is worth considering for large files and for applications where comparisons are particularly expensive or access costs are very high. Note that in Section 12.4 we discuss a continuous counterpart of interpolation search, which can be seen as one more example of a variable-size-decrease algorithm. Searching and Insertion in a Binary Search Tree Let us revisit the binary search tree. Recall that this is a binary tree whose nodes contain elements of a set of orderable items, one element per node, so that for every node all elements in the left subtree are smaller and all the elements in the right subtree are greater than the element in the subtree’s root. When we need to search for an element of a given value v in such a tree, we do it recursively in the following manner. If the tree is empty, the search ends in failure. If the tree is not empty, we compare v with the tree’s root K(r). If they match, a desired element is found and the search can be stopped; if they do not match, we continue with the search in the left subtree of the root if v < K(r) and in the right subtree if v > K(r). Thus, on each iteration of the algorithm, the problem of searching in a binary search tree is reduced to searching in a smaller binary search tree. The most sensible measure of the size of a search tree is its height; obviously, the decrease in a tree’s height normally changes from one iteration to another of the binary tree search—thus giving us an excellent example of a variable-size-decrease algorithm. In the worst case of the binary tree search, the tree is severely skewed. This happens, in particular, if a tree is constructed by successive insertions of an increasing or decreasing sequence of keys (Figure 4.15). Obviously, the search for an−1 in such a tree requires n comparisons, making the worst-case efficiency of the search operation fall into (n). Fortunately, the average-case efficiency turns out to be in (log n). More precisely, the number of key comparisons needed for a search in a binary search tree built from n random keys is about 2ln n ≈ 1.39 log2 n. Since insertion of a new key into a binary search tree is almost identical to that of searching there, it also exemplifies the variable- size-decrease technique and has the same efficiency characteristics as the search operation. 164 Decrease-and-Conquer a0 a0 a1 a1 an–2 an–2 an–1 an–1 (a) (b) . . . . . . FIGURE 4.15 Binary search trees for (a) an increasing sequence of keys and (b) a decreasing sequence of keys. The Game of Nim There are several well-known games that share the following features. There are two players, who move in turn. No randomness or hidden information is permitted: all players know all information about gameplay. A game is impartial: each player has the same moves available from the same game position. Each of a finite number of available moves leads to a smaller instance of the same game. The game ends with a win by one of the players (there are no ties). The winner is the last player who is able to move. A prototypical example of such games is Nim. Generally, the game is played with several piles of chips, but we consider the one-pile version first. Thus, there is a single pile of n chips. Two players take turns by removing from the pile at least one and at most m chips; the number of chips taken may vary from one move to another, but both the lower and upper limits stay the same. Who wins the game by taking the last chip, the player moving first or second, if both players make the best moves possible? Let us call an instance of the game a winning position for the player to move next if that player has a winning strategy, i.e., a sequence of moves that results in a victory no matter what moves the opponent makes. Let us call an instance of the game a losing position for the player to move next if every move available for that player leads to a winning position for the opponent. The standard approach to determining which positions are winning and which are losing is to investigate small values of n first. It is logical to consider the instance of n = 0as a losing one for the player to move next because this player is the first one who cannot make a move. Any instance with 1 ≤ n ≤ m chips is obviously a winning position for the player to move next (why?). The instance with n = m + 1 chips is a losing one because taking any allowed number of chips puts the opponent in a winning position. (See an illustration for m = 4 in Figure 4.16.) Any instance with m + 2 ≤ n ≤ 2m + 1 chips is a winning position for the player to move next because there is a move that leaves the opponent with m + 1chips, which is a losing 4.5 Variable-Size-Decrease Algorithms 165 0 4 3 2 1 5 9 8 7 6 10 FIGURE 4.16 Illustration of one-pile Nim with the maximum number of chips that may be taken on each move m = 4. The numbers indicate n, the number of chips in the pile. The losing positions for the player to move are circled. Only winning moves from the winning positions are shown (in bold). position. 2m + 2 = 2(m + 1) chips is the next losing position, and so on. It is not difficult to see the pattern that can be formally proved by mathematical induction: an instance with n chips is a winning position for the player to move next if and only if n is not a multiple of m + 1. The winning strategy is to take n mod(m + 1) chips on every move; any deviation from this strategy puts the opponent in a winning position. One-pile Nim has been known for a very long time. It appeared, in particular, as the summation game in the first published book on recreational mathematics, authored by Claude-Gaspar Bachet, a French aristocrat and mathematician, in 1612: a player picks a positive integer less than, say, 10, and then his opponent and he take turns adding any integer less than 10; the first player to reach 100 exactly is the winner [Dud70]. In general, Nim is played with I>1 piles of chips of sizes n1,n2,...,nI . On each move, a player can take any available number of chips, including all of them, from any single pile. The goal is the same—to be the last player able to make a move. Note that for I = 2, it is easy to figure out who wins this game and how. Here is a hint: the answer for the game’s instances with n1 = n2 differs from the answer for those with n1 = n2. A solution to the general case of Nim is quite unexpected because it is based on the binary representation of the pile sizes. Let b1,b2,...,bI be the pile sizes in binary. Compute their binary digital sum, also known as the nim sum, defined as the sum of binary digits discarding any carry. (In other words, a binary digit si in the sum is 0 if the number of 1’s in the ith position in the addends is even, and it is 1 if the number of 1’s is odd.) It turns out that an instance of Nim is a winning one for the player to move next if and only if its nim sum contains at least one 1; consequently, Nim’s instance is a losing instance if and only if its nim sum contains only zeros. For example, for the commonly played instance with n1 = 3, n2 = 4,n3 = 5, the nim sum is 166 Decrease-and-Conquer 011 100 101 010 Since this sum contains a 1, the instance is a winning one for the player moving first. To find a winning move from this position, the player needs to change one of the three bit strings so that the new nim sum contains only 0’s. It is not difficult to see that the only way to accomplish this is to remove two chips from the first pile. This ingenious solution to the game of Nim was discovered by Harvard math- ematics professor C. L. Bouton more than 100 years ago. Since then, mathemati- cians have developed a much more general theory of such games. An excellent account of this theory, with applications to many specific games, is given in the monograph by E. R. Berlekamp, J. H. Conway, and R. K. Guy [Ber03]. Exercises 4.5 1. a. If we measure an instance size of computing the greatest common divisor of m and n by the size of the second number n, by how much can the size decrease after one iteration of Euclid’s algorithm? b. Prove that an instance size will always decrease at least by a factor of two after two successive iterations of Euclid’s algorithm. 2. Apply quickselect to find the median of the list of numbers 9, 12, 5, 17, 20, 30, 8. 3. Write pseudocode for a nonrecursive implementation of quickselect. 4. Derive the formula underlying interpolation search. 5. Give an example of the worst-case input for interpolation search and show that the algorithm is linear in the worst case. 6. a. Find the smallest value of n for which log2 log2 n + 1 is greater than 6. b. Determine which, if any, of the following assertions are true: i. log log n ∈ o(log n) ii. log log n ∈ (log n) iii. log log n ∈ (log n) 7. a. Outline an algorithm for finding the largest key in a binary search tree. Would you classify your algorithm as a variable-size-decrease algorithm? b. What is the time efficiency class of your algorithm in the worst case? 8. a. Outline an algorithm for deleting a key from a binary search tree. Would you classify this algorithm as a variable-size-decrease algorithm? b. What is the time efficiency class of your algorithm in the worst case? 9. Outline a variable-size-decrease algorithm for constructing an Eulerian circuit in a connected graph with all vertices of even degrees. Summary 167 10. Mis`ere one-pile Nim Consider the so-called mis`ere version of the one-pile Nim, in which the player taking the last chip loses the game. All the other conditions of the game remain the same, i.e., the pile contains n chips and on each move a player takes at least one but no more than m chips. Identify the winning and losing positions (for the player to move next) in this game. 11. a. Moldy chocolate Two players take turns by breaking an m × n chocolate bar, which has one spoiled 1 × 1 square. Each break must be a single straight line cutting all the way across the bar along the boundaries between the squares. After each break, the player who broke the bar last eats the piece that does not contain the spoiled square. The player left with the spoiled square loses the game. Is it better to go first or second in this game? b. Write an interactive program to play this game with the computer. Your program should make a winning move in a winning position and a random legitimate move in a losing position. 12. Flipping pancakes There are n pancakes all of different sizes that are stacked on top of each other. You are allowed to slip a flipper under one of the pancakes and flip over the whole stack above the flipper. The purpose is to arrange pancakes according to their size with the biggest at the bottom. (You can see a visualization of this puzzle on the Interactive Mathematics Miscellany and Puzzles site [Bog].) Design an algorithm for solving this puzzle. 13. You need to search for a given number in an n × n matrix in which every row and every column is sorted in increasing order. Can you design a O(n) algorithm for this problem? [Laa10] SUMMARY Decrease-and-conquer is a general algorithm design technique, based on exploiting a relationship between a solution to a given instance of a problem and a solution to a smaller instance of the same problem. Once such a relationship is established, it can be exploited either top down (usually recursively) or bottom up. There are three major variations of decrease-and-conquer: . decrease-by-a-constant, most often by one (e.g., insertion sort) . decrease-by-a-constant-factor, most often by the factor of two (e.g., binary search) . variable-size-decrease (e.g., Euclid’s algorithm) Insertion sort is a direct application of the decrease-(by one)-and-conquer technique to the sorting problem. It is a (n2) algorithm both in the worst and average cases, but it is about twice as fast on average than in the worst case. The algorithm’s notable advantage is a good performance on almost-sorted arrays. 168 Decrease-and-Conquer A digraph is a graph with directions on its edges. The topological sorting problem asks to list vertices of a digraph in an order such that for every edge of the digraph, the vertex it starts at is listed before the vertex it points to. This problem has a solution if and only if a digraph is a dag (directed acyclic graph), i.e., it has no directed cycles. There are two algorithms for solving the topological sorting problem. The first one is based on depth-first search; the second is based on a direct application of the decrease-by-one technique. The decrease-by-one technique is a natural approach to developing algo- rithms for generating elementary combinatorial objects. The most efficient class of such algorithms are minimal-change algorithms. However, the num- ber of combinatorial objects grows so fast that even the best algorithms are of practical interest only for very small instances of such problems. Binary search is a very efficient algorithm for searching in a sorted array. It is a principal example of a decrease-by-a-constant-factor algorithm. Other examples include exponentiation by squaring, identifying a fake coin with a balance scale, Russian peasant multiplication, and the Josephus problem. For some decrease-and-conquer algorithms, the size reduction varies from one iteration of the algorithm to another. Examples of such variable-size- decrease algorithms include Euclid’s algorithm, the partition-based algorithm for the selection problem, interpolation search, and searching and insertion in a binary search tree. Nim exemplifies games that proceed through a series of diminishing instances of the same game. 5 Divide-and-Conquer Whatever man prays for, he prays for a miracle. Every prayer reduces itself to this—Great God, grant that twice two be not four. —Ivan Turgenev (1818–1883), Russian novelist and short-story writer Divide-and-conquer is probably the best-known general algorithm design technique. Though its fame may have something to do with its catchy name, it is well deserved: quite a few very efficient algorithms are specific implementations of this general strategy. Divide-and-conquer algorithms work according to the following general plan: 1. A problem is divided into several subproblems of the same type, ideally of about equal size. 2. The subproblems are solved (typically recursively, though sometimes a dif- ferent algorithm is employed, especially when subproblems become small enough). 3. If necessary, the solutions to the subproblems are combined to get a solution to the original problem. The divide-and-conquer technique is diagrammed in Figure 5.1, which depicts the case of dividing a problem into two smaller subproblems, by far the most widely occurring case (at least for divide-and-conquer algorithms designed to be executed on a single-processor computer). As an example, let us consider the problem of computing the sum of n numbers a0,...,an−1.Ifn>1, we can divide the problem into two instances of the same problem: to compute the sum of the first n/2 numbers and to compute the sum of the remaining n/2 numbers. (Of course, if n = 1, we simply return a0 as the answer.) Once each of these two sums is computed by applying the same method recursively, we can add their values to get the sum in question: a0 + ...+ an−1 = (a0 + ...+ an/2−1) + (an/2 + ...+ an−1). Is this an efficient way to compute the sum of n numbers? A moment of reflection (why could it be more efficient than the brute-force summation?), a 169 170 Divide-and-Conquer subproblem 1 of size n/2 subproblem 2 of size n/2 solution to subproblem 1 solution to subproblem 2 solution to the original problem problem of size n FIGURE 5.1 Divide-and-conquer technique (typical case). small example of summing, say, four numbers by this algorithm, a formal analysis (which follows), and common sense (we do not normally compute sums this way, do we?) all lead to a negative answer to this question.1 Thus, not every divide-and-conquer algorithm is necessarily more efficient than even a brute-force solution. But often our prayers to the Goddess of Algorithmics—see the chapter’s epigraph—are answered, and the time spent on executing the divide-and-conquer plan turns out to be significantly smaller than solving a problem by a different method. In fact, the divide-and-conquer approach yields some of the most important and efficient algorithms in computer science. We discuss a few classic examples of such algorithms in this chapter. Though we consider only sequential algorithms here, it is worth keeping in mind that the divide-and-conquer technique is ideally suited for parallel computations, in which each subproblem can be solved simultaneously by its own processor. 1. Actually, the divide-and-conquer algorithm, called the pairwise summation, may substantially reduce the accumulated round-off error of the sum of numbers that can be represented only approximately in a digital computer [Hig93]. Divide-and-Conquer 171 As mentioned above, in the most typical case of divide-and-conquer a prob- lem’s instance of size n is divided into two instances of size n/2. More generally, an instance of size n can be divided into b instances of size n/b, with a of them needing to be solved. (Here, a and b are constants; a ≥ 1 and b>1.) Assuming that size n is a power of b to simplify our analysis, we get the following recurrence for the running time T (n): T (n) = aT (n/b) + f (n), (5.1) where f (n) is a function that accounts for the time spent on dividing an instance of size n into instances of size n/b and combining their solutions. (For the sum example above, a = b = 2 and f (n) = 1.) Recurrence (5.1) is called the general divide-and-conquer recurrence. Obviously, the order of growth of its solution T (n) depends on the values of the constants a and b and the order of growth of the function f (n). The efficiency analysis of many divide-and-conquer algorithms is greatly simplified by the following theorem (see Appendix B). Master Theorem If f (n) ∈ (nd) where d ≥ 0 in recurrence (5.1), then T (n) ∈ ⎧ ⎨ ⎩ (nd) if abd. Analogous results hold for the O and notations, too. For example, the recurrence for the number of additions A(n) made by the divide-and-conquer sum-computation algorithm (see above) on inputs of size n = 2k is A(n) = 2A(n/2) + 1. Thus, for this example, a = 2,b= 2, and d = 0; hence, since a>bd, A(n) ∈ (nlogb a) = (nlog2 2) = (n). Note that we were able to find the solution’s efficiency class without going through the drudgery of solving the recurrence. But, of course, this approach can only es- tablish a solution’s order of growth to within an unknown multiplicative constant, whereas solving a recurrence equation with a specific initial condition yields an exact answer (at least for n’s that are powers of b). It is also worth pointing out that if a = 1, recurrence (5.1) covers decrease- by-a-constant-factor algorithms discussed in the previous chapter. In fact, some people consider such algorithms as binary search degenerate cases of divide-and- conquer, where just one of two subproblems of half the size needs to be solved. It is better not to do this and consider decrease-by-a-constant-factor and divide- and-conquer as different design paradigms. 172 Divide-and-Conquer 5.1 Mergesort Mergesort is a perfect example of a successful application of the divide-and- conquer technique. It sorts a given array A[0..n − 1] by dividing it into two halves A[0..n/2−1] and A[n/2..n − 1], sorting each of them recursively, and then merging the two smaller sorted arrays into a single sorted one. ALGORITHM Mergesort(A[0..n − 1]) //Sorts array A[0..n − 1] by recursive mergesort //Input: An array A[0..n − 1] of orderable elements //Output: Array A[0..n − 1] sorted in nondecreasing order if n>1 copy A[0..n/2−1] to B[0..n/2−1] copy A[n/2..n − 1] to C[0..n/2−1] Mergesort(B[0..n/2−1]) Mergesort(C[0..n/2−1]) Merge(B, C, A) //see below The merging of two sorted arrays can be done as follows. Two pointers (array indices) are initialized to point to the first elements of the arrays being merged. The elements pointed to are compared, and the smaller of them is added to a new array being constructed; after that, the index of the smaller element is incremented to point to its immediate successor in the array it was copied from. This operation is repeated until one of the two given arrays is exhausted, and then the remaining elements of the other array are copied to the end of the new array. ALGORITHM Merge(B[0..p − 1],C[0..q − 1],A[0..p + q − 1]) //Merges two sorted arrays into one sorted array //Input: Arrays B[0..p − 1] and C[0..q − 1] both sorted //Output: Sorted array A[0..p + q − 1] of the elements of B and C i ← 0; j ← 0; k ← 0 while i1,C(1) = 0. Let us analyze Cmerge(n), the number of key comparisons performed during the merging stage. At each step, exactly one comparison is made, after which the total number of elements in the two arrays still needing to be processed is reduced by 1. In the worst case, neither of the two arrays becomes empty before the other one contains just one element (e.g., smaller elements may come from the alternating arrays). Therefore, for the worst case, Cmerge(n) = n − 1, and we have the recurrence Cworst(n) = 2Cworst(n/2) + n − 1 for n>1,Cworst(1) = 0. Hence, according to the Master Theorem, Cworst(n) ∈ (n log n) (why?). In fact, it is easy to find the exact solution to the worst-case recurrence for n = 2k: Cworst(n) = n log2 n − n + 1. 174 Divide-and-Conquer The number of key comparisons made by mergesort in the worst case comes very close to the theoretical minimum2 that any general comparison-based sorting algorithm can have. For large n, the number of comparisons made by this algo- rithm in the average case turns out to be about 0.25n less (see [Gon91, p. 173]) and hence is also in (n log n). A noteworthy advantage of mergesort over quick- sort and heapsort—the two important advanced sorting algorithms to be discussed later—is its stability (see Problem 7 in this section’s exercises). The principal short- coming of mergesort is the linear amount of extra storage the algorithm requires. Though merging can be done in-place, the resulting algorithm is quite complicated and of theoretical interest only. There are two main ideas leading to several variations of mergesort. First, the algorithm can be implemented bottom up by merging pairs of the array’s elements, then merging the sorted pairs, and so on. (If n is not a power of 2, only slight bookkeeping complications arise.) This avoids the time and space overhead of using a stack to handle recursive calls. Second, we can divide a list to be sorted in more than two parts, sort each recursively, and then merge them together. This scheme, which is particularly useful for sorting files residing on secondary memory devices, is called multiway mergesort. Exercises 5.1 1. a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. b. What will be your algorithm’s output for arrays with several elements of the largest value? c. Set up and solve a recurrence relation for the number of key comparisons made by your algorithm. d. How does this algorithm compare with the brute-force algorithm for this problem? 2. a. Write pseudocode for a divide-and-conquer algorithm for finding values of both the largest and smallest elements in an array of n numbers. b. Set up and solve (for n = 2k) a recurrence relation for the number of key comparisons made by your algorithm. c. How does this algorithm compare with the brute-force algorithm for this problem? 3. a. Write pseudocode for a divide-and-conquer algorithm for the exponenti- ation problem of computing an where n is a positive integer. b. Set up and solve a recurrence relation for the number of multiplications made by this algorithm. 2. As we shall see in Section 11.2, this theoretical minimum is log2 n!≈n log2 n − 1.44n. 5.1 Mergesort 175 c. How does this algorithm compare with the brute-force algorithm for this problem? 4. As mentioned in Chapter 2, logarithm bases are irrelevant in most contexts arising in analyzing an algorithm’s efficiency class. Is this true for both asser- tions of the Master Theorem that include logarithms? 5. Find the order of growth for solutions of the following recurrences. a. T (n) = 4T (n/2) + n, T (1) = 1 b. T (n) = 4T (n/2) + n2,T(1) = 1 c. T (n) = 4T (n/2) + n3,T(1) = 1 6. Apply mergesort to sort the list E, X, A, M, P, L, E in alphabetical order. 7. Is mergesort a stable sorting algorithm? 8. a. Solve the recurrence relation for the number of key comparisons made by mergesort in the worst case. You may assume that n = 2k. b. Set up a recurrence relation for the number of key comparisons made by mergesort on best-case inputs and solve it for n = 2k. c. Set up a recurrence relation for the number of key moves made by the version of mergesort given in Section 5.1. Does taking the number of key moves into account change the algorithm’s efficiency class? 9. Let A[0..n − 1] be an array of n real numbers. A pair (A[i],A[j]) is said to be an inversion if these numbers are out of order, i.e., iA[j]. Design an O(n log n) algorithm for counting the number of inversions. 10. Implement the bottom-up version of mergesort in the language of your choice. 11. Tromino puzzle A tromino (more accurately, a right tromino) is an L-shaped tile formed by three 1 × 1 squares. The problem is to cover any 2n × 2n chess- board with a missing square with trominoes. Trominoes can be oriented in an arbitrary way, but they should cover all the squares of the board except the missing one exactly and with no overlaps. [Gol94] Design a divide-and-conquer algorithm for this problem. 176 Divide-and-Conquer 5.2 Quicksort Quicksort is the other important sorting algorithm that is based on the divide-and- conquer approach. Unlike mergesort, which divides its input elements according to their position in the array, quicksort divides them according to their value. We already encountered this idea of an array partition in Section 4.5, where we discussed the selection problem. A partition is an arrangement of the array’s elements so that all the elements to the left of some element A[s] are less than or equal to A[s], and all the elements to the right of A[s] are greater than or equal to it: A[0] ...A[s − 1]   all are ≤A[s] A[s] A[s + 1] ...A[n − 1]   all are ≥A[s] Obviously, after a partition is achieved, A[s] will be in its final position in the sorted array, and we can continue sorting the two subarrays to the left and to the right of A[s] independently (e.g., by the same method). Note the difference with mergesort: there, the division of the problem into two subproblems is immediate and the entire work happens in combining their solutions; here, the entire work happens in the division stage, with no work required to combine the solutions to the subproblems. Here is pseudocode of quicksort: call Quicksort(A[0..n − 1]) where ALGORITHM Quicksort(A[l..r]) //Sorts a subarray by quicksort //Input: Subarray of array A[0..n − 1], defined by its left and right // indices l and r //Output: Subarray A[l..r] sorted in nondecreasing order if lj,we will have partitioned the subarray after exchanging the pivot with A[j]: ji→← p all are ≤ p all are ≥ p≥ p≤ p Finally, if the scanning indices stop while pointing to the same element, i.e., i = j, the value they are pointing to must be equal to p (why?). Thus, we have the subarray partitioned, with the split position s = i = j: j = i →← p all are ≤ p all are ≥ p= p We can combine the last case with the case of crossed-over indices (i > j) by exchanging the pivot with A[j] whenever i ≥ j. Here is pseudocode implementing this partitioning procedure. 178 Divide-and-Conquer ALGORITHM HoarePartition(A[l..r]) //Partitions a subarray by Hoare’s algorithm, using the first element // as a pivot //Input: Subarray of array A[0..n − 1], defined by its left and right // indices l and r(l1,Cbest(1) = 0. According to the Master Theorem, Cbest(n) ∈ (n log2 n); solving it exactly for n = 2k yields Cbest(n) = n log2 n. In the worst case, all the splits will be skewed to the extreme: one of the two subarrays will be empty, and the size of the other will be just 1 less than the size of the subarray being partitioned. This unfortunate situation will happen, in particular, for increasing arrays, i.e., for inputs for which the problem is already solved! Indeed, if A[0..n − 1] is a strictly increasing array and we use A[0] as the pivot, the left-to-right scan will stop on A[1] while the right-to-left scan will go all the way to reach A[0], indicating the split at position 0: 5.2 Quicksort 179 0 1 2 3 4 5 6 7 5 5 5 5 5 5 3 3 3 3 3 3 1 1 1 1 1 1 4 4 4 4 2 2 2 2 5 2 3 3 8 23 1 4 4 4 4 4 4 4 4 4 7 7 7 79 9 9 9 9 7 1 1 3 3 3 3 3 1 1 1 1 i i i i i i i i i i j j j j j j i i i i ij j j j j j j j j 8 8 8 9 9 8 8 8 7 7 7 7 7 2 2 2 4 4 9 9 9 8 2 28 2 8 8 9 9 7 7 (b) (a) I=0, r=3 s=1 I=2, r=3 s=2 I=5, r=7 s=6 I=0, r=7 s=4 I=3, r=3 I=5, r=5I=0, r=0 I=2, r=1 I=7, r=7 FIGURE 5.3 Example of quicksort operation. (a) Array’s transformations with pivots shown in bold. (b) Tree of recursive calls to Quicksort with input values l and r of subarray bounds and split position s of a partition obtained. A[0] A[1] A[n–1]. . . ji→← So, after making n + 1 comparisons to get to this partition and exchanging the pivot A[0] with itself, the algorithm will be left with the strictly increasing array A[1..n − 1]to sort. This sorting of strictly increasing arrays of diminishing sizes will 180 Divide-and-Conquer continue until the last one A[n − 2..n − 1] has been processed. The total number of key comparisons made will be equal to Cworst(n) = (n + 1) + n + ...+ 3 = (n + 1)(n + 2) 2 − 3 ∈ (n2). Thus, the question about the utility of quicksort comes down to its average- case behavior. Let Cavg(n) be the average number of key comparisons made by quicksort on a randomly ordered array of size n. A partition can happen in any position s(0 ≤ s ≤ n − 1) after n + 1comparisons are made to achieve the partition. After the partition, the left and right subarrays will have s and n − 1 − s elements, respectively. Assuming that the partition split can happen in each position s with the same probability 1/n, we get the following recurrence relation: Cavg(n) = 1 n n−1 s=0 [(n + 1) + Cavg(s) + Cavg(n − 1 − s)] for n>1, Cavg(0) = 0,Cavg(1) = 0. Its solution, which is much trickier than the worst- and best-case analyses, turns out to be Cavg(n) ≈ 2n ln n ≈ 1.39n log2 n. Thus, on the average, quicksort makes only 39% more comparisons than in the best case. Moreover, its innermost loop is so efficient that it usually runs faster than mergesort (and heapsort, another n log n algorithm that we discuss in Chapter 6) on randomly ordered arrays of nontrivial sizes. This certainly justifies the name given to the algorithm by its inventor. Because of quicksort’s importance, there have been persistent efforts over the years to refine the basic algorithm. Among several improvements discovered by researchers are: better pivot selection methods such as randomized quicksort that uses a random element or the median-of-three method that uses the median of the leftmost, rightmost, and the middle element of the array switching to insertion sort on very small subarrays (between 5 and 15 elements for most computer systems) or not sorting small subarrays at all and finishing the algorithm with insertion sort applied to the entire nearly sorted array modifications of the partitioning algorithm such as the three-way partition into segments smaller than, equal to, and larger than the pivot (see Problem 9 in this section’s exercises) According to Robert Sedgewick [Sed11, p. 296], the world’s leading expert on quicksort, such improvements in combination can cut the running time of the algorithm by 20%–30%. Like any sorting algorithm, quicksort has weaknesses. It is not stable. It requires a stack to store parameters of subarrays that are yet to be sorted. While 5.2 Quicksort 181 the size of this stack can be made to be in O(log n) by always sorting first the smaller of two subarrays obtained by partitioning, it is worse than the O(1) space efficiency of heapsort. Although more sophisticated ways of choosing a pivot make the quadratic running time of the worst case very unlikely, they do not eliminate it completely. And even the performance on randomly ordered arrays is known to be sensitive not only to implementation details of the algorithm but also to both computer architecture and data type. Still, the January/February 2000 issue of Computing in Science & Engineering,a joint publication of the American Institute of Physics and the IEEE Computer Society, selected quicksort as one of the 10 algorithms “with the greatest influence on the development and practice of science and engineering in the 20th century.” Exercises 5.2 1. Apply quicksort to sort the list E, X, A, M, P, L, E in alphabetical order. Draw the tree of the recursive calls made. 2. For the partitioning procedure outlined in this section: a. Prove that if the scanning indices stop while pointing to the same element, i.e., i = j, the value they are pointing to must be equal to p. b. Prove that when the scanning indices stop, j cannot point to an element more than one position to the left of the one pointed to by i. 3. Give an example showing that quicksort is not a stable sorting algorithm. 4. Give an example of an array of n elements for which the sentinel mentioned in the text is actually needed. What should be its value? Also explain why a single sentinel suffices for any input. 5. For the version of quicksort given in this section: a. Are arrays made up of all equal elements the worst-case input, the best- case input, or neither? b. Are strictly decreasing arrays the worst-case input, the best-case input, or neither? 6. a. For quicksort with the median-of-three pivot selection, are strictly increas- ing arrays the worst-case input, the best-case input, or neither? b. Answer the same question for strictly decreasing arrays. 7. a. Estimate how many times faster quicksort will sort an array of one million random numbers than insertion sort. b. True or false: For every n>1, there are n-element arrays that are sorted faster by insertion sort than by quicksort? 8. Design an algorithm to rearrange elements of a given array of n real num- bers so that all its negative elements precede all its positive elements. Your algorithm should be both time efficient and space efficient. 182 Divide-and-Conquer 9. a. The Dutch national flag problem is to rearrange an array of characters R, W, and B (red, white, and blue are the colors of the Dutch national flag) so that all the R’s come first, the W’s come next, and the B’s come last. [Dij76] Design a linear in-place algorithm for this problem. b. Explain how a solution to the Dutch national flag problem can be used in quicksort. 10. Implement quicksort in the language of your choice. Run your program on a sample of inputs to verify the theoretical assertions about the algorithm’s efficiency. 11. Nuts and bolts You are given a collection of n bolts of different widths and n corresponding nuts. You are allowed to try a nut and bolt together, from which you can determine whether the nut is larger than the bolt, smaller than the bolt, or matches the bolt exactly. However, there is no way to compare two nuts together or two bolts together. The problem is to match each bolt to its nut. Design an algorithm for this problem with average-case efficiency in (n log n). [Raw91] 5.3 Binary Tree Traversals and Related Properties In this section, we see how the divide-and-conquer technique can be applied to binary trees. A binary tree T is defined as a finite set of nodes that is either empty or consists of a root and two disjoint binary trees TL and TR called, respectively, the left and right subtree of the root. We usually think of a binary tree as a special case of an ordered tree (Figure 5.4). (This standard interpretation was an alternative definition of a binary tree in Section 1.4.) Since the definition itself divides a binary tree into two smaller structures of the same type, the left subtree and the right subtree, many problems about binary trees can be solved by applying the divide-and-conquer technique. As an example, let us consider a recursive algorithm for computing the height of a binary tree. Recall that the height is defined as the length of the longest path from the root to a leaf. Hence, it can be computed as the maximum of the heights of the root’s left Tleft Tright FIGURE 5.4 Standard representation of a binary tree. 5.3 Binary Tree Traversals and Related Properties 183 and right subtrees plus 1. (We have to add 1 to account for the extra level of the root.) Also note that it is convenient to define the height of the empty tree as −1. Thus, we have the following recursive algorithm. ALGORITHM Height(T ) //Computes recursively the height of a binary tree //Input: A binary tree T //Output: The height of T if T = ∅ return −1 else return max{Height(Tleft), Height(Tright)}+1 We measure the problem’s instance size by the number of nodes n(T ) in a given binary tree T . Obviously, the number of comparisons made to compute the maximum of two numbers and the number of additions A(n(T )) made by the algorithm are the same. We have the following recurrence relation for A(n(T )): A(n(T )) = A(n(Tleft)) + A(n(Tright)) + 1 for n(T ) > 0, A(0) = 0. Before we solve this recurrence (can you tell what its solution is?), let us note that addition is not the most frequently executed operation of this algorithm. What is? Checking—and this is very typical for binary tree algorithms—that the tree is not empty. For example, for the empty tree, the comparison T = ∅ is executed once but there are no additions, and for a single-node tree, the comparison and addition numbers are 3 and 1, respectively. It helps in the analysis of tree algorithms to draw the tree’s extension by replacing the empty subtrees by special nodes. The extra nodes (shown by little squares in Figure 5.5) are called external; the original nodes (shown by little circles) are called internal. By definition, the extension of the empty binary tree is a single external node. It is easy to see that the Height algorithm makes exactly one addition for every internal node of the extended tree, and it makes one comparison to check whether (b)(a) FIGURE 5.5 Binary tree (on the left) and its extension (on the right). Internal nodes are shown as circles; external nodes are shown as squares. 184 Divide-and-Conquer the tree is empty for every internal and external node. Therefore, to ascertain the algorithm’s efficiency, we need to know how many external nodes an extended binary tree with n internal nodes can have. After checking Figure 5.5 and a few similar examples, it is easy to hypothesize that the number of external nodes x is always 1 more than the number of internal nodes n: x = n + 1. (5.2) To prove this equality, consider the total number of nodes, both internal and external. Since every node, except the root, is one of the two children of an internal node, we have the equation 2n + 1 = x + n, which immediately implies equality (5.2). Note that equality (5.2) also applies to any nonempty full binary tree,in which, by definition, every node has either zero or two children: for a full binary tree, n and x denote the numbers of parental nodes and leaves, respectively. Returning to algorithm Height, the number of comparisons to check whether the tree is empty is C(n) = n + x = 2n + 1, and the number of additions is A(n) = n. The most important divide-and-conquer algorithms for binary trees are the three classic traversals: preorder, inorder, and postorder. All three traversals visit nodes of a binary tree recursively, i.e., by visiting the tree’s root and its left and right subtrees. They differ only by the timing of the root’s visit: In the preorder traversal, the root is visited before the left and right subtrees are visited (in that order). In the inorder traversal, the root is visited after visiting its left subtree but before visiting the right subtree. In the postorder traversal, the root is visited after visiting the left and right subtrees (in that order). These traversals are illustrated in Figure 5.6. Their pseudocodes are quite straightforward, repeating the descriptions given above. (These traversals are also a standard feature of data structures textbooks.) As to their efficiency analysis, it is identical to the above analysis of the Height algorithm because a recursive call is made for each node of an extended binary tree. Finally, we should note that, obviously, not all questions about binary trees require traversals of both left and right subtrees. For example, the search and insert operations for a binary search tree require processing only one of the two subtrees. Accordingly, we considered them in Section 4.5 not as applications of divide-and- conquer but rather as examples of the variable-size-decrease technique. 5.3 Binary Tree Traversals and Related Properties 185 b a de c preorder: inorder: postorder: a, b, d, g, e, c, f d, g, b, e, a, f, c g, d, e, b, f, c, a g f FIGURE 5.6 Binary tree and its traversals. Exercises 5.3 1. Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. (In particular, the algorithm must return 0 and 1 for the empty and single-node trees, respectively.) What is the time efficiency class of your algorithm? 2. The following algorithm seeks to compute the number of leaves in a binary tree. ALGORITHM LeafCounter(T ) //Computes recursively the number of leaves in a binary tree //Input: A binary tree T //Output: The number of leaves in T if T = ∅ return 0 else return LeafCounter(Tleft)+ LeafCounter(Tright) Is this algorithm correct? If it is, prove it; if it is not, make an appropriate correction. 3. Can you compute the height of a binary tree with the same asymptotic ef- ficiency as the section’s divide-and-conquer algorithm but without using a stack explicitly or implicitly? Of course, you may use a different algorithm altogether. 4. Prove equality (5.2) by mathematical induction. 5. Traverse the following binary tree a. in preorder. b. in inorder. c. in postorder. 186 Divide-and-Conquer b a de c f 6. Write pseudocode for one of the classic traversal algorithms (preorder, in- order, and postorder) for binary trees. Assuming that your algorithm is recur- sive, find the number of recursive calls made. 7. Which of the three classic traversal algorithms yields a sorted list if applied to a binary search tree? Prove this property. 8. a. Draw a binary tree with 10 nodes labeled 0, 1,...,9 in such a way that the inorder and postorder traversals of the tree yield the following lists: 9, 3, 1, 0, 4, 2, 7, 6, 8, 5 (inorder) and 9, 1, 4, 0, 3, 6, 7, 5, 8, 2 (postorder). b. Give an example of two permutations of the same n labels 0, 1,...,n− 1 that cannot be inorder and postorder traversal lists of the same binary tree. c. Design an algorithm that constructs a binary tree for which two given lists of n labels 0, 1,...,n− 1 are generated by the inorder and postorder traversals of the tree. Your algorithm should also identify inputs for which the problem has no solution. 9. The internal path length I of an extended binary tree is defined as the sum of the lengths of the paths—taken over all internal nodes—from the root to each internal node. Similarly, the external path length E of an extended binary tree is defined as the sum of the lengths of the paths—taken over all external nodes—from the root to each external node. Prove that E = I + 2n where n is the number of internal nodes in the tree. 10. Write a program for computing the internal path length of an extended binary tree. Use it to investigate empirically the average number of key comparisons for searching in a randomly generated binary search tree. 11. Chocolate bar puzzle Given an n × m chocolate bar, you need to break it into nm 1 × 1 pieces. You can break a bar only in a straight line, and only one bar can be broken at a time. Design an algorithm that solves the problem with the minimum number of bar breaks. What is this minimum number? Justify your answer by using properties of a binary tree. 5.4 Multiplication of Large Integers and Strassen’s Matrix Multiplication In this section, we examine two surprising algorithms for seemingly straightfor- ward tasks: multiplying two integers and multiplying two square matrices. Both 5.4 Multiplication of Large Integers and Strassen’s Matrix Multiplication 187 achieve a better asymptotic efficiency by ingenious application of the divide-and- conquer technique. Multiplication of Large Integers Some applications, notably modern cryptography, require manipulation of inte- gers that are over 100 decimal digits long. Since such integers are too long to fit in a single word of a modern computer, they require special treatment. This practi- cal need supports investigations of algorithms for efficient manipulation of large integers. In this section, we outline an interesting algorithm for multiplying such numbers. Obviously, if we use the conventional pen-and-pencil algorithm for mul- tiplying two n-digit integers, each of the n digits of the first number is multiplied by each of the n digits of the second number for the total of n2 digit multiplications. (If one of the numbers has fewer digits than the other, we can pad the shorter number with leading zeros to equalize their lengths.) Though it might appear that it would be impossible to design an algorithm with fewer than n2 digit multiplica- tions, this turns out not to be the case. The miracle of divide-and-conquer comes to the rescue to accomplish this feat. To demonstrate the basic idea of the algorithm, let us start with a case of two-digit integers, say, 23 and 14. These numbers can be represented as follows: 23 = 2 . 101 + 3 . 100 and 14 = 1 . 101 + 4 . 100. Now let us multiply them: 23 ∗ 14 = (2 . 101 + 3 . 100) ∗ (1 . 101 + 4 . 100) = (2 ∗ 1)102 + (2 ∗ 4 + 3 ∗ 1)101 + (3 ∗ 4)100. The last formula yields the correct answer of 322, of course, but it uses the same four digit multiplications as the pen-and-pencil algorithm. Fortunately, we can compute the middle term with just one digit multiplication by taking advantage of the products 2 ∗ 1 and 3 ∗ 4 that need to be computed anyway: 2 ∗ 4 + 3 ∗ 1 = (2 + 3) ∗ (1 + 4) − 2 ∗ 1 − 3 ∗ 4. Of course, there is nothing special about the numbers we just multiplied. For any pair of two-digit numbers a = a1a0 and b = b1b0, their product c can be computed by the formula c = a ∗ b = c2102 + c1101 + c0, where c2 = a1 ∗ b1 is the product of their first digits, c0 = a0 ∗ b0 is the product of their second digits, c1 = (a1 + a0) ∗ (b1 + b0) − (c2 + c0) is the product of the sum of the a’s digits and the sum of the b’s digits minus the sum of c2 and c0. 188 Divide-and-Conquer Now we apply this trick to multiplying two n-digit integers a and b where n is a positive even number. Let us divide both numbers in the middle—after all, we promised to take advantage of the divide-and-conquer technique. We denote the first half of the a’s digits by a1 and the second half by a0; for b, the notations are b1 and b0, respectively. In these notations, a = a1a0 implies that a = a110n/2 + a0 and b = b1b0 implies that b = b110n/2 + b0. Therefore, taking advantage of the same trick we used for two-digit numbers, we get c = a ∗ b = (a110n/2 + a0) ∗ (b110n/2 + b0) = (a1 ∗ b1)10n + (a1 ∗ b0 + a0 ∗ b1)10n/2 + (a0 ∗ b0) = c210n + c110n/2 + c0, where c2 = a1 ∗ b1 is the product of their first halves, c0 = a0 ∗ b0 is the product of their second halves, c1 = (a1 + a0) ∗ (b1 + b0) − (c2 + c0) is the product of the sum of the a’s halves and the sum of the b’s halves minus the sum of c2 and c0. If n/2 is even, we can apply the same method for computing the products c2,c0, and c1. Thus, if n is a power of 2, we have a recursive algorithm for computing the product of two n-digit integers. In its pure form, the recursion is stopped when n becomes 1. It can also be stopped when we deem n small enough to multiply the numbers of that size directly. How many digit multiplications does this algorithm make? Since multiplica- tion of n-digit numbers requires three multiplications of n/2-digit numbers, the recurrence for the number of multiplications M(n) is M(n) = 3M(n/2) for n>1,M(1) = 1. Solving it by backward substitutions for n = 2k yields M(2k) = 3M(2k−1) = 3[3M(2k−2)] = 32M(2k−2) = ...= 3iM(2k−i) = ...= 3kM(2k−k) = 3k. Since k = log2 n, M(n) = 3log2 n = nlog2 3 ≈ n1.585. (On the last step, we took advantage of the following property of logarithms: alogb c = clogb a.) But what about additions and subtractions? Have we not decreased the num- ber of multiplications by requiring more of those operations? Let A(n) be the number of digit additions and subtractions executed by the above algorithm in multiplying two n-digit decimal integers. Besides 3A(n/2) of these operations needed to compute the three products of n/2-digit numbers, the above formulas 5.4 Multiplication of Large Integers and Strassen’s Matrix Multiplication 189 require five additions and one subtraction. Hence, we have the recurrence A(n) = 3A(n/2) + cn for n>1,A(1) = 1. Applying the Master Theorem, which was stated in the beginning of the chapter, we obtain A(n) ∈ (nlog2 3), which means that the total number of additions and subtractions have the same asymptotic order of growth as the number of multipli- cations. The asymptotic advantage of this algorithm notwithstanding, how practical is it? The answer depends, of course, on the computer system and program quality implementing the algorithm, which might explain the rather wide disparity of reported results. On some machines, the divide-and-conquer algorithm has been reported to outperform the conventional method on numbers only 8 decimal digits long and to run more than twice faster with numbers over 300 decimal digits long—the area of particular importance for modern cryptography. Whatever this outperformance “crossover point” happens to be on a particular machine, it is worth switching to the conventional algorithm after the multiplicands become smaller than the crossover point. Finally, if you program in an object-oriented language such as Java, C++, or Smalltalk, you should also be aware that these languages have special classes for dealing with large integers. Discovered by 23-year-old Russian mathematician Anatoly Karatsuba in 1960, the divide-and-conquer algorithm proved wrong the then-prevailing opinion that the time efficiency of any integer multiplication algorithm must be in (n2). The discovery encouraged researchers to look for even (asymptotically) faster algorithms for this and other algebraic problems. We will see such an algorithm in the next section. Strassen’s Matrix Multiplication Now that we have seen that the divide-and-conquer approach can reduce the number of one-digit multiplications in multiplying two integers, we should not be surprised that a similar feat can be accomplished for multiplying matrices. Such an algorithm was published by V. Strassen in 1969 [Str69]. The principal insight of the algorithm lies in the discovery that we can find the product C of two 2 × 2 matrices A and B with just seven multiplications as opposed to the eight required by the brute-force algorithm (see Example 3 in Section 2.3). This is accomplished by using the following formulas:  c00 c01 c10 c11  =  a00 a01 a10 a11  ∗  b00 b01 b10 b11  =  m1 + m4 − m5 + m7 m3 + m5 m2 + m4 m1 + m3 − m2 + m6  , where 190 Divide-and-Conquer m1 = (a00 + a11) ∗ (b00 + b11), m2 = (a10 + a11) ∗ b00, m3 = a00 ∗ (b01 − b11), m4 = a11 ∗ (b10 − b00), m5 = (a00 + a01) ∗ b11, m6 = (a10 − a00) ∗ (b00 + b01), m7 = (a01 − a11) ∗ (b10 + b11). Thus, to multiply two 2 × 2 matrices, Strassen’s algorithm makes seven multipli- cations and 18 additions/subtractions, whereas the brute-force algorithm requires eight multiplications and four additions. These numbers should not lead us to multiplying 2 × 2 matrices by Strassen’s algorithm. Its importance stems from its asymptotic superiority as matrix order n goes to infinity. Let A and B be two n × n matrices where n is a power of 2. (If n is not a power of 2, matrices can be padded with rows and columns of zeros.) We can divide A, B, and their product C into four n/2 × n/2 submatrices each as follows: " C00 C01 C10 C11 # = " A00 A01 A10 A11 # ∗ " B00 B01 B10 B11 # . It is not difficult to verify that one can treat these submatrices as numbers to get the correct product. For example, C00 can be computed either as A00 ∗ B00 + A01 ∗ B10 or as M1 + M4 − M5 + M7 where M1,M4,M5, and M7 are found by Strassen’s formulas, with the numbers replaced by the corresponding submatrices. If the seven products of n/2 × n/2 matrices are computed recursively by the same method, we have Strassen’s algorithm for matrix multiplication. Let us evaluate the asymptotic efficiency of this algorithm. If M(n) is the number of multiplications made by Strassen’s algorithm in multiplying two n × n matrices (where n is a power of 2), we get the following recurrence relation for it: M(n) = 7M(n/2) for n>1,M(1) = 1. Since n = 2k, M(2k) = 7M(2k−1) = 7[7M(2k−2)] = 72M(2k−2) = ... = 7iM(2k−i) ...= 7kM(2k−k) = 7k. Since k = log2 n, M(n) = 7log2 n = nlog2 7 ≈ n2.807, which is smaller than n3 required by the brute-force algorithm. Since this savings in the number of multiplications was achieved at the expense of making extra additions, we must check the number of additions A(n) made by Strassen’s algorithm. To multiply two matrices of order n>1, the algorithm needs to multiply seven matrices of order n/2 and make 18 additions/subtractions of matrices of size n/2; when n = 1, no additions are made since two numbers are 5.4 Multiplication of Large Integers and Strassen’s Matrix Multiplication 191 simply multiplied. These observations yield the following recurrence relation: A(n) = 7A(n/2) + 18(n/2)2 for n>1,A(1) = 0. Though one can obtain a closed-form solution to this recurrence (see Problem 8 in this section’s exercises), here we simply establish the solution’s order of growth. According to the Master Theorem, A(n) ∈ (nlog2 7). In other words, the number of additions has the same order of growth as the number of multiplications. This puts Strassen’s algorithm in (nlog2 7), which is a better efficiency class than (n3) of the brute-force method. Since the time of Strassen’s discovery, several other algorithms for multiplying two n × n matrices of real numbers in O(nα) time with progressively smaller constants α have been invented. The fastest algorithm so far is that of Coopersmith and Winograd [Coo87] with its efficiency in O(n2.376). The decreasing values of the exponents have been obtained at the expense of the increasing complexity of these algorithms. Because of large multiplicative constants, none of them is of practical value. However, they are interesting from a theoretical point of view. On one hand, they get closer and closer to the best theoretical lower bound known for matrix multiplication, which is n2 multiplications, though the gap between this bound and the best available algorithm remains unresolved. On the other hand, matrix multiplication is known to be computationally equivalent to some other important problems, such as solving systems of linear equations (discussed in the next chapter). Exercises 5.4 1. What are the smallest and largest numbers of digits the product of two decimal n-digit integers can have? 2. Compute 2101 ∗ 1130 by applying the divide-and-conquer algorithm outlined in the text. 3. a. Prove the equality alogb c = clogb a, which was used in Section 5.4. b. Why is nlog2 3 better than 3log2 n as a closed-form formula for M(n)? 4. a. Why did we not include multiplications by 10n in the multiplication count M(n) of the large-integer multiplication algorithm? b. In addition to assuming that n is a power of 2, we made, for the sake of simplicity, another, more subtle, assumption in setting up the recurrences for M(n) and A(n), which is not always true (it does not change the final answers, however). What is this assumption? 5. How many one-digit additions are made by the pen-and-pencil algorithm in multiplying two n-digit integers? You may disregard potential carries. 6. Verify the formulas underlying Strassen’s algorithm for multiplying 2 × 2 matrices. 192 Divide-and-Conquer 7. Apply Strassen’s algorithm to compute ⎡ ⎢⎢⎣ 1021 4110 0130 5021 ⎤ ⎥⎥⎦ ∗ ⎡ ⎢⎢⎣ 0101 2104 2011 1350 ⎤ ⎥⎥⎦ exiting the recursion when n = 2, i.e., computing the products of 2 × 2 matrices by the brute-force algorithm. 8. Solve the recurrence for the number of additions required by Strassen’s algo- rithm. Assume that n is a power of 2. 9. V. Pan [Pan78] has discovered a divide-and-conquer matrix multiplication algorithm that is based on multiplying two 70 × 70 matrices using 143,640 multiplications. Find the asymptotic efficiency of Pan’s algorithm (you may ignore additions) and compare it with that of Strassen’s algorithm. 10. Practical implementations of Strassen’s algorithm usually switch to the brute- force method after matrix sizes become smaller than some crossover point. Run an experiment to determine such a crossover point on your computer system. 5.5 The Closest-Pair and Convex-Hull Problems by Divide-and-Conquer In Section 3.3, we discussed the brute-force approach to solving two classic prob- lems of computational geometry: the closest-pair problem and the convex-hull problem. We saw that the two-dimensional versions of these problems can be solved by brute-force algorithms in (n2) and O(n3) time, respectively. In this sec- tion, we discuss more sophisticated and asymptotically more efficient algorithms for these problems, which are based on the divide-and-conquer technique. The Closest-Pair Problem Let P be a set of n>1 points in the Cartesian plane. For the sake of simplicity, we assume that the points are distinct. We can also assume that the points are ordered in nondecreasing order of their x coordinate. (If they were not, we could sort them first by an efficeint sorting algorithm such as mergesort.) It will also be convenient to have the points sorted in a separate list in nondecreasing order of the y coordinate; we will denote such a list Q. If 2 ≤ n ≤ 3, the problem can be solved by the obvious brute-force algorithm. If n>3, we can divide the points into two subsets Pl and Pr of n/2 and n/2 points, respectively, by drawing a vertical line through the median m of their x coordinates so that n/2 points lie to the left of or on the line itself, and n/2 points lie to the right of or on the line. Then we can solve the closest-pair problem 5.5 The Closest-Pair and Convex-Hull Problems by Divide-and-Conquer 193 (a) (b) dr dl dmin dd dd p x = m x = m FIGURE 5.7 (a) Idea of the divide-and-conquer algorithm for the closest-pair problem. (b) Rectangle that may contain points closer than dmin to point p. recursively for subsets Pl and Pr. Let dl and dr be the smallest distances between pairs of points in Pl and Pr, respectively, and let d = min{dl,dr}. Note that d is not necessarily the smallest distance between all the point pairs because points of a closer pair can lie on the opposite sides of the separating line. Therefore, as a step combining the solutions to the smaller subproblems, we need to examine such points. Obviously, we can limit our attention to the points inside the symmetric vertical strip of width 2d around the separating line, since the distance between any other pair of points is at least d (Figure 5.7a). 194 Divide-and-Conquer Let S be the list of points inside the strip of width 2d around the separating line, obtained from Q and hence ordered in nondecreasing order of their y coor- dinate. We will scan this list, updating the information about dmin, the minimum distance seen so far, if we encounter a closer pair of points. Initially, dmin = d,and subsequently dmin ≤ d. Let p(x, y) be a point on this list. For a point p (x ,y ) to have a chance to be closer to p than dmin, the point must follow p on list S and the difference between their y coordinates must be less than dmin (why?). Geometri- cally, this means that p must belong to the rectangle shown in Figure 5.7b. The principal insight exploited by the algorithm is the observation that the rectangle can contain just a few such points, because the points in each half (left and right) of the rectangle must be at least distance d apart. It is easy to prove that the total number of such points in the rectangle, including p, does not exceed eight (Prob- lem 2 in this section’s exercises); a more careful analysis reduces this number to six (see [Joh04, p. 695]). Thus, the algorithm can consider no more than five next points following p on the list S, before moving up to the next point. Here is pseudocode of the algorithm. We follow the advice given in Section 3.3 to avoid computing square roots inside the innermost loop of the algorithm. ALGORITHM EfficientClosestPair(P, Q) //Solves the closest-pair problem by divide-and-conquer //Input: An array P of n ≥ 2 points in the Cartesian plane sorted in // nondecreasing order of their x coordinates and an array Q of the // same points sorted in nondecreasing order of the y coordinates //Output: Euclidean distance between the closest pair of points if n ≤ 3 return the minimal distance found by the brute-force algorithm else copy the first n/2 points of P to array Pl copy the same n/2 points from Q to array Ql copy the remaining n/2 points of P to array Pr copy the same n/2 points from Q to array Qr dl ← EfficientClosestPair(Pl,Ql) dr ← EfficientClosestPair(Pr,Qr) d ←min{dl,dr} m ← P[n/2−1].x copy all the points of Q for which |x − m| 1 points p1(x1,y1),...,pn(xn,yn) in the Cartesian plane. We assume that the points are sorted in nondecreasing order of their x coordinates, with ties resolved by increasing order of the y coordinates of the points involved. It is not difficult to prove the geometrically obvious fact that the leftmost point p1 and the rightmost point pn are two distinct extreme points of the set’s convex hull (Figure 5.8). Let −−−→p1pn be the straight line through points p1 and pn directed from p1 to pn. This line separates the points of S into two sets: S1 is the set of points to the left of this line, and S2 is the set of points to the right of this line. (We say that point q3 is to the left of the line −−−−→q1q2 directed from point q1 to point q2 if q1q2q3 forms a counterclockwise cycle. Later, we cite an analytical way to check this condition, based on checking the sign of a determinant formed by the coordinates of the three points.) The points of S on the line −−−→p1pn, other than p1 and pn, cannot be extreme points of the convex hull and hence are excluded from further consideration. The boundary of the convex hull of S is made up of two polygonal chains: an “upper” boundary and a “lower” boundary. The “upper” boundary, called the upper hull, is a sequence of line segments with vertices at p1, some of the points in S1 (if S1 is not empty) and pn. The “lower” boundary, called the lower hull,is a sequence of line segments with vertices at p1, some of the points in S2 (if S2 is not empty) and pn. The fact that the convex hull of the entire set S is composed of the upper and lower hulls, which can be constructed independently and in a similar fashion, is a very useful observation exploited by several algorithms for this problem. For concreteness, let us discuss how quickhull proceeds to construct the upper hull; the lower hull can be constructed in the same manner. If S1 is empty, the 196 Divide-and-Conquer p1 pn FIGURE 5.8 Upper and lower hulls of a set of points. p1 pmax pn FIGURE 5.9 The idea of quickhull. upper hull is simply the line segment with the endpoints at p1 and pn. If S1 is not empty, the algorithm identifies point pmax in S1, which is the farthest from the line−−−→p1pn (Figure 5.9). If there is a tie, the point that maximizes the angle pmaxppn can be selected. (Note that point pmax maximizes the area of the triangle with two vertices at p1 and pn and the third one at some other point of S1.) Then the algorithm identifies all the points of set S1 that are to the left of the line −−−→p1pmax; these are the points that will make up the set S1,1. The points of S1 to the left of the line −−−−−−→pmaxpn will make up the set S1,2. It is not difficult to prove the following: pmax is a vertex of the upper hull. The points inside p1pmaxpn cannot be vertices of the upper hull (and hence can be eliminated from further consideration). There are no points to the left of both lines −−−→p1pmax and −−−−−−→pmaxpn. Therefore, the algorithm can continue constructing the upper hulls of p1 ∪ S1,1 ∪ pmax and pmax ∪ S1,2 ∪ pn recursively and then simply concatenate them to get the upper hull of the entire set p1 ∪ S1 ∪ pn. 5.5 The Closest-Pair and Convex-Hull Problems by Divide-and-Conquer 197 Now we have to figure out how the algorithm’s geometric operations can be actually implemented. Fortunately, we can take advantage of the following very useful fact from analytical geometry: if q1(x1,y1), q2(x2,y2), and q3(x3,y3) are three arbitrary points in the Cartesian plane, then the area of the triangle q1q2q3 is equal to one-half of the magnitude of the determinant $$$$$$ x1 y1 1 x2 y2 1 x3 y3 1 $$$$$$ = x1y2 + x3y1 + x2y3 − x3y2 − x2y1 − x1y3, while the sign of this expression is positive if and only if the point q3 = (x3,y3) is to the left of the line −−−→q1q2. Using this formula, we can check in constant time whether a point lies to the left of the line determined by two other points as well as find the distance from the point to the line. Quickhull has the same (n2) worst-case efficiency as quicksort (Problem 9 in this section’s exercises). In the average case, however, we should expect a much better performance. First, the algorithm should benefit from the quicksort- like savings from the on-average balanced split of the problem into two smaller subproblems. Second, a significant fraction of the points—namely, those inside p1pmaxpn (see Figure 5.9)—are eliminated from further processing. Under a natural assumption that points given are chosen randomly from a uniform dis- tribution over some convex region (e.g., a circle or a rectangle), the average-case efficiency of quickhull turns out to be linear [Ove80]. Exercises 5.5 1. a. For the one-dimensional version of the closest-pair problem, i.e., for the problem of finding two closest numbers among a given set of n real num- bers, design an algorithm that is directly based on the divide-and-conquer technique and determine its efficiency class. b. Is it a good algorithm for this problem? 2. Prove that the divide-and-conquer algorithm for the closest-pair problem examines, for every point p in the vertical strip (see Figures 5.7a and 5.7b), no more than seven other points that can be closer to p than dmin, the minimum distance between two points encountered by the algorithm up to that point. 3. Consider the version of the divide-and-conquer two-dimensional closest-pair algorithm in which, instead of presorting input set P, we simply sort each of the two sets Pl and Pr in nondecreasing order of their y coordinates on each recursive call. Assuming that sorting is done by mergesort, set up a recurrence relation for the running time in the worst case and solve it for n = 2k. 4. Implement the divide-and-conquer closest-pair algorithm, outlined in this section, in the language of your choice. 198 Divide-and-Conquer 5. Find on the Web a visualization of an algorithm for the closest-pair problem. What algorithm does this visualization represent? 6. The Voronoi polygon for a point p of a set S of points in the plane is defined to be the perimeter of the set of all points in the plane closer to p than to any other point in S. The union of all the Voronoi polygons of the points in S is called the Voronoi diagram of S. a. What is the Voronoi diagram for a set of three points? b. Find a visualization of an algorithm for generating the Voronoi diagram on the Web and study a few examples of such diagrams. Based on your observations, can you tell how the solution to the previous question is generalized to the general case? 7. Explain how one can find point pmax in the quickhull algorithm analytically. 8. What is the best-case efficiency of quickhull? 9. Give a specific example of inputs that make quickhull run in quadratic time. 10. Implement quickhull in the language of your choice. 11. Creating decagons There are 1000 points in the plane, no three of them on the same line. Devise an algorithm to construct 100 decagons with their vertices at these points. The decagons need not be convex, but each of them has to be simple, i.e., its boundary should not cross itself, and no two decagons may have a common point. 12. Shortest path around There is a fenced area in the two-dimensional Eu- clidean plane in the shape of a convex polygon with vertices at points p1(x1,y1), p2(x2,y2),...,pn(xn,yn) (not necessarily in this order). There are two more points, a(xa,ya) and b(xb,yb) such that xa < min{x1,x2,...,xn} and xb > max{x1,x2,...,xn}. Design a reasonably efficient algorithm for comput- ing the length of the shortest path between a and b. [ORo98] SUMMARY Divide-and-conquer is a general algorithm design technique that solves a problem by dividing it into several smaller subproblems of the same type (ideally, of about equal size), solving each of them recursively, and then combining their solutions to get a solution to the original problem. Many efficient algorithms are based on this technique, although it can be both inapplicable and inferior to simpler algorithmic solutions. Running time T (n) of many divide-and-conquer algorithms satisfies the recurrence T (n) = aT (n/b) + f (n). The Master Theorem establishes the order of growth of its solutions. Mergesort is a divide-and-conquer sorting algorithm. It works by dividing an input array into two halves, sorting them recursively, and then merging the two Summary 199 sorted halves to get the original array sorted. The algorithm’s time efficiency is in (n log n) in all cases, with the number of key comparisons being very close to the theoretical minimum. Its principal drawback is a significant extra storage requirement. Quicksort is a divide-and-conquer sorting algorithm that works by partition- ing its input elements according to their value relative to some preselected element. Quicksort is noted for its superior efficiency among n log n al- gorithms for sorting randomly ordered arrays but also for the quadratic worst-case efficiency. The classic traversals of a binary tree—preorder, inorder, and postorder— and similar algorithms that require recursive processing of both left and right subtrees can be considered examples of the divide-and-conquer technique. Their analysis is helped by replacing all the empty subtrees of a given tree by special external nodes. There is a divide-and-conquer algorithm for multiplying two n-digit integers that requires about n1.585 one-digit multiplications. Strassen’s algorithm needs only seven multiplications to multiply two 2 × 2 matrices. By exploiting the divide-and-conquer technique, this algorithm can multiply two n × n matrices with about n2.807 multiplications. The divide-and-conquer technique can be successfully applied to two impor- tant problems of computational geometry: the closest-pair problem and the convex-hull problem. This page intentionally left blank 6 Transform-and-Conquer That’s the secret to life...replace one worry with another. —Charles M. Schulz (1922–2000), American cartoonist, the creator of Peanuts This chapter deals with a group of design methods that are based on the idea of transformation. We call this general technique transform-and-conquer because these methods work as two-stage procedures. First, in the transformation stage, the problem’s instance is modified to be, for one reason or another, more amenable to solution. Then, in the second or conquering stage, it is solved. There are three major variations of this idea that differ by what we transform a given instance to (Figure 6.1): Transformation to a simpler or more convenient instance of the same problem—we call it instance simplification. Transformation to a different representation of the same instance—we call it representation change. Transformation to an instance of a different problem for which an algorithm is already available—we call it problem reduction. In the first three sections of this chapter, we encounter examples of the instance-simplification variety. Section 6.1 deals with the simple but fruitful idea of presorting. Many algorithmic problems are easier to solve if their input is sorted. Of course, the benefits of sorting should more than compensate for the problem's instance solution simpler instance or another representation or another problem's instance FIGURE 6.1 Transform-and-conquer strategy. 201 202 Transform-and-Conquer time spent on it; otherwise, we would be better off dealing with an unsorted input directly. Section 6.2 introduces one of the most important algorithms in applied mathematics: Gaussian elimination. This algorithm solves a system of linear equations by first transforming it to another system with a special property that makes finding a solution quite easy. In Section 6.3, the ideas of instance simplification and representation change are applied to search trees. The results are AVL trees and multiway balanced search trees; of the latter we consider the simplest case, 2-3 trees. Section 6.4 presents heaps and heapsort. Even if you are already familiar with this important data structure and its application to sorting, you can still benefit from looking at them in this new light of transform-and-conquer design. In Section 6.5, we discuss Horner’s rule, a remarkable algorithm for evaluating polynomials. If there were an Algorithm Hall of Fame, Horner’s rule would be a serious candidate for induction based on the algorithm’s elegance and efficiency. We also consider there two interesting algorithms for the exponentiation problem, both based on the representation-change idea. The chapter concludes with a review of several applications of the third variety of transform-and-conquer: problem reduction. This variety should be considered the most radical of the three: one problem is reduced to another, i.e., transformed into an entirely different problem. This is a very powerful idea, and it is extensively used in the complexity theory (Chapter 11). Its application to designing practical algorithms is not trivial, however. First, we need to identify a new problem into which the given problem should be transformed. Then we must make sure that the transformation algorithm followed by the algorithm for solving the new prob- lem is time efficient compared to other algorithmic alternatives. Among several examples, we discuss an important special case of mathematical modeling,or expressing a problem in terms of purely mathematical objects such as variables, functions, and equations. 6.1 Presorting Presorting is an old idea in computer science. In fact, interest in sorting algorithms is due, to a significant degree, to the fact that many questions about a list are easier to answer if the list is sorted. Obviously, the time efficiency of algorithms that involve sorting may depend on the efficiency of the sorting algorithm being used. For the sake of simplicity, we assume throughout this section that lists are implemented as arrays, because some sorting algorithms are easier to implement for the array representation. So far, we have discussed three elementary sorting algorithms—selection sort, bubble sort, and insertion sort—that are quadratic in the worst and average cases, and two advanced algorithms—mergesort, which is always in (n log n), and quicksort, whose efficiency is also (n log n) in the average case but is quadratic in the worst case. Are there faster sorting algorithms? As we have already stated in Section 1.3 (see also Section 11.2), no general comparison-based sorting algorithm 6.1 Presorting 203 can have a better efficiency than n log n in the worst case, and the same result holds for the average-case efficiency.1 Following are three examples that illustrate the idea of presorting. More examples can be found in this section’s exercises. EXAMPLE 1 Checking element uniqueness in an array If this element unique- ness problem looks familiar to you, it should; we considered a brute-force algo- rithm for the problem in Section 2.3 (see Example 2). The brute-force algorithm compared pairs of the array’s elements until either two equal elements were found or no more pairs were left. Its worst-case efficiency was in (n2). Alternatively, we can sort the array first and then check only its consecutive elements: if the array has equal elements, a pair of them must be next to each other, and vice versa. ALGORITHM PresortElementUniqueness(A[0..n − 1]) //Solves the element uniqueness problem by sorting the array first //Input: An array A[0..n − 1] of orderable elements //Output: Returns “true” if A has no equal elements, “false” otherwise sort the array A for i ← 0 to n − 2 do if A[i] = A[i + 1] return false return true The running time of this algorithm is the sum of the time spent on sorting and the time spent on checking consecutive elements. Since the former requires at least n log n comparisons and the latter needs no more than n − 1 comparisons, it is the sorting part that will determine the overall efficiency of the algorithm. So, if we use a quadratic sorting algorithm here, the entire algorithm will not be more efficient than the brute-force one. But if we use a good sorting algorithm, such as mergesort, with worst-case efficiency in (n log n), the worst-case efficiency of the entire presorting-based algorithm will be also in (n log n): T (n) = Tsort(n) + Tscan(n) ∈ (n log n) + (n) = (n log n). EXAMPLE 2 Computing a mode A mode is a value that occurs most often in a given list of numbers. For example, for 5, 1, 5, 7, 6, 5, 7, the mode is 5. (If several different values occur most often, any of them can be considered a mode.) The brute-force approach to computing a mode would scan the list and compute the frequencies of all its distinct values, then find the value with the largest frequency. 1. Sorting algorithms called radix sorts are linear but in terms of the total number of input bits. These algorithms work by comparing individual bits or pieces of keys rather than keys in their entirety. Although the running time of these algorithms is proportional to the number of input bits, they are still essentially n log n algorithms because the number of bits per key must be at least log2 n in order to accommodate n distinct keys of input. 204 Transform-and-Conquer In order to implement this idea, we can store the values already encountered, along with their frequencies, in a separate list. On each iteration, the ith element of the original list is compared with the values already encountered by traversing this auxiliary list. If a matching value is found, its frequency is incremented; otherwise, the current element is added to the list of distinct values seen so far with a frequency of 1. It is not difficult to see that the worst-case input for this algorithm is a list with no equal elements. For such a list, its ith element is compared with i − 1 elements of the auxiliary list of distinct values seen so far before being added to the list with a frequency of 1. As a result, the worst-case number of comparisons made by this algorithm in creating the frequency list is C(n) = n i=1 (i − 1) = 0 + 1 + ...+ (n − 1) = (n − 1)n 2 ∈ (n2). The additional n − 1 comparisons needed to find the largest frequency in the aux- iliary list do not change the quadratic worst-case efficiency class of the algorithm. As an alternative, let us first sort the input. Then all equal values will be adjacent to each other. To compute the mode, all we need to do is to find the longest run of adjacent equal values in the sorted array. ALGORITHM PresortMode(A[0..n − 1]) //Computes the mode of an array by sorting it first //Input: An array A[0..n − 1] of orderable elements //Output: The array’s mode sort the array A i ← 0 //current run begins at position i modef requency ← 0 //highest frequency seen so far while i ≤ n − 1 do runlength ← 1; runvalue ← A[i] while i + runlength ≤ n − 1 and A[i + runlength] = runvalue runlength ← runlength + 1 if runlength > modefrequency modef requency ← runlength; modevalue ← runvalue i ← i + runlength return modevalue The analysis here is similar to the analysis of Example 1: the running time of the algorithm will be dominated by the time spent on sorting since the remainder of the algorithm takes linear time (why?). Consequently, with an n log n sort, this method’s worst-case efficiency will be in a better asymptotic class than the worst- case efficiency of the brute-force algorithm. 6.1 Presorting 205 EXAMPLE 3 Searching problem Consider the problem of searching for a given value v in a given array of n sortable items. The brute-force solution here is sequential search (Section 3.1), which needs n comparisons in the worst case. If the array is sorted first, we can then apply binary search, which requires only log2 n+1 comparisons in the worst case. Assuming the most efficient n log n sort, the total running time of such a searching algorithm in the worst case will be T (n) = Tsort(n) + Tsearch(n) = (n log n) + (log n) = (n log n), which is inferior to sequential search. The same will also be true for the average- case efficiency. Of course, if we are to search in the same list more than once, the time spent on sorting might well be justified. (Problem 4 in this section’s exercises asks to estimate the minimum number of searches needed to justify presorting.) Before we finish our discussion of presorting, we should mention that many, if not most, geometric algorithms dealing with sets of points use presorting in one way or another. Points can be sorted by one of their coordinates, or by their distance from a particular line, or by some angle, and so on. For example, presorting was used in the divide-and-conquer algorithms for the closest-pair problem and for the convex-hull problem, which were discussed in Section 5.5. Further, some problems for directed acyclic graphs can be solved more easily after topologically sorting the digraph in question. The problems of finding the longest and shortest paths in such digraphs (see the exercises for Sections 8.1 and 9.3) illustrate this point. Finally, most algorithms based on the greedy technique, which is the subject of Chapter 9, require presorting of their inputs as an intrinsic part of their operations. Exercises 6.1 1. Consider the problem of finding the distance between the two closest numbers in an array of n numbers. (The distance between two numbers x and y is computed as |x − y|.) a. Design a presorting-based algorithm for solving this problem and deter- mine its efficiency class. b. Compare the efficiency of this algorithm with that of the brute-force algo- rithm (see Problem 9 in Exercises 1.2). 2. Let A ={a1,...,an} and B ={b1,...,bm} be two sets of numbers. Consider the problem of finding their intersection, i.e., the set C of all the numbers that are in both A and B. a. Design a brute-force algorithm for solving this problem and determine its efficiency class. b. Design a presorting-based algorithm for solving this problem and deter- mine its efficiency class. 206 Transform-and-Conquer 3. Consider the problem of finding the smallest and largest elements in an array of n numbers. a. Design a presorting-based algorithm for solving this problem and deter- mine its efficiency class. b. Compare the efficiency of the three algorithms: (i) the brute-force algo- rithm, (ii) this presorting-based algorithm, and (iii) the divide-and-conquer algorithm (see Problem 2 in Exercises 5.1). 4. Estimate how many searches will be needed to justify time spent on presorting an array of 103 elements if sorting is done by mergesort and searching is done by binary search. (You may assume that all searches are for elements known to be in the array.) What about an array of 106 elements? 5. To sort or not to sort? Design a reasonably efficient algorithm for solving each of the following problems and determine its efficiency class. a. You are given n telephone bills and m checks sent to pay the bills (n ≥ m). Assuming that telephone numbers are written on the checks, find out who failed to pay. (For simplicity, you may also assume that only one check is written for a particular bill and that it covers the bill in full.) b. You have a file of n student records indicating each student’s number, name, home address, and date of birth. Find out the number of students from each of the 50 U.S. states. 6. Given a set of n ≥ 3 points in the Cartesian plane, connect them in a simple polygon, i.e., a closed path through all the points so that its line segments (the polygon’s edges) do not intersect (except for neighboring edges at their common vertex). For example, P3 P3 P2 P2 P6 P1 P4 P5 P4 P6 P1 P5 a. Does the problem always have a solution? Does it always have a unique solution? b. Design a reasonably efficient algorithm for solving this problem and indi- cate its efficiency class. 7. You have an array of n real numbers and another integer s. Find out whether the array contains two elements whose sum is s. (For example, for the array 5, 9, 1, 3 and s = 6, the answer is yes, but for the same array and s = 7, the answer 6.1 Presorting 207 is no.) Design an algorithm for this problem with a better than quadratic time efficiency. 8. Youhave a list of n open intervals (a1,b1), (a2,b2),...,(an,bn) on the real line. (An open interval (a, b) comprises all the points strictly between its endpoints a and b, i.e., (a, b) ={x| a 3 < 4 < 6 10. Maxima search a. A point (xi,yi) in the Cartesian plane is said to be dominated by point (xj,yj) if xi ≤ xj and yi ≤ yj with at least one of the two inequalities being strict. Given a set of n points, one of them is said to be a maximum of the set if it is not dominated by any other point in the set. For example, in the figure below, all the maximum points of the set of 10 points are circled. y x Design an efficient algorithm for finding all the maximum points of a given set of n points in the Cartesian plane. What is the time efficiency class of your algorithm? b. Give a few real-world applications of this algorithm. 208 Transform-and-Conquer 11. Anagram detection a. Design an efficient algorithm for finding all sets of anagrams in a large file such as a dictionary of English words [Ben00]. For example, eat, ate, and tea belong to one such set. b. Write a program implementing the algorithm. 6.2 Gaussian Elimination You are certainly familiar with systems of two linear equations in two unknowns: a11x + a12y = b1 a21x + a22y = b2. Recall that unless the coefficients of one equation are proportional to the coef- ficients of the other, the system has a unique solution. The standard method for finding this solution is to use either equation to express one of the variables as a function of the other and then substitute the result into the other equation, yield- ing a linear equation whose solution is then used to find the value of the second variable. In many applications, we need to solve a system of n equations in n unknowns: a11x1 + a12x2 + ...+ a1nxn = b1 a21x1 + a22x2 + ...+ a2nxn = b2... an1x1 + an2x2 + ...+ annxn = bn where n is a large number. Theoretically, we can solve such a system by general- izing the substitution method for solving systems of two linear equations (what general design technique would such a method be based upon?); however, the resulting algorithm would be extremely cumbersome. Fortunately, there is a much more elegant algorithm for solving systems of linear equations called Gaussian elimination.2 The idea of Gaussian elimination is to transform a system of n linear equations in n unknowns to an equivalent system (i.e., a system with the same solution as the original one) with an upper- triangular coefficient matrix, a matrix with all zeros below its main diagonal: 2. The method is named after Carl Friedrich Gauss (1777–1855), who—like other giants in the history of mathematics such as Isaac Newton and Leonhard Euler—made numerous fundamental contributions to both theoretical and computational mathematics. The method was known to the Chinese 1800 years before the Europeans rediscovered it. 6.2 Gaussian Elimination 209 a11x1 + a12x2 + ...+ a1nxn = b1 a 11x1 + a 12x2 + ...+ a 1nxn = b 1 a21x1 + a22x2 + ...+ a2nxn = b2 a 22x2 + ...+ a 2nxn = b 2... ⇒ ... an1x1 + an2x2 + ...+ annxn = bn a nnxn = b n. In matrix notations, we can write this as Ax = b ⇒ A x = b , where A = ⎡ ⎢⎢⎣ a11 a12 ... a1n a21 a22 ... a2n... an1 an2 ... ann ⎤ ⎥⎥⎦,b= ⎡ ⎢⎢⎣ b1 b2... bn ⎤ ⎥⎥⎦,A = ⎡ ⎢⎢⎣ a 11 a 12 ... a 1n 0 a 22 ... a 2n... 00... a nn ⎤ ⎥⎥⎦,b= ⎡ ⎢⎢⎣ b 1 b 2... b n ⎤ ⎥⎥⎦ . (We added primes to the matrix elements and right-hand sides of the new system to stress the point that their values differ from their counterparts in the original system.) Why is the system with the upper-triangular coefficient matrix better than a system with an arbitrary coefficient matrix? Because we can easily solve the system with an upper-triangular coefficient matrix by back substitutions as follows. First, we can immediately find the value of xn from the last equation; then we can substitute this value into the next to last equation to get xn−1, and so on, until we substitute the known values of the last n − 1 variables into the first equation, from which we find the value of x1. So how can we get from a system with an arbitrary coefficient matrix A to an equivalent system with an upper-triangular coefficient matrix A ? We can do that through a series of the so-called elementary operations: exchanging two equations of the system replacing an equation with its nonzero multiple replacing an equation with a sum or difference of this equation and some multiple of another equation Since no elementary operation can change a solution to a system, any system that is obtained through a series of such operations will have the same solution as the original one. Let us see how we can get to a system with an upper-triangular matrix. First, we use a11 as a pivot to make all x1 coefficients zeros in the equations below the first one. Specifically, we replace the second equation with the difference between it and the first equation multiplied by a21/a11 to get an equation with a zero coefficient for x1. Doing the same for the third, fourth, and finally nth equation—with the multiples a31/a11,a41/a11,...,an1/a11 of the first equation, respectively—makes all the coefficients of x1 below the first equation zero. Then we get rid of all the coefficients of x2 by subtracting an appropriate multiple of the second equation from each of the equations below the second one. Repeating this 210 Transform-and-Conquer elimination for each of the first n − 1 variables ultimately yields a system with an upper-triangular coefficient matrix. Before we look at an example of Gaussian elimination, let us note that we can operate with just a system’s coefficient matrix augmented, as its (n + 1)st column, with the equations’ right-hand side values. In other words, we need to write explicitly neither the variable names nor the plus and equality signs. EXAMPLE 1 Solve the system by Gaussian elimination. 2x1 − x2 + x3 = 1 4x1 + x2 − x3 = 5 x1 + x2 + x3 = 0. ⎡ ⎣ 2 −11 1 41−15 111 0 ⎤ ⎦ row 2 − 4 2 row 1 row 3 − 1 2 row 1 ⎡ ⎣ 2 −11 1 03−33 0 3 2 1 2 − 1 2 ⎤ ⎦ row 3 − 1 2 row 2 ⎡ ⎣ 2 −111 03−33 002−2 ⎤ ⎦ Now we can obtain the solution by back substitutions: x3 = (−2)/2 =−1,x2 = (3 − (−3)x3)/3 = 0, and x1 = (1 − x3 − (−1)x2)/2 = 1. Here is pseudocode of the first stage, called forward elimination, of the algorithm. ALGORITHM ForwardElimination(A[1..n, 1..n],b[1..n]) //Applies Gaussian elimination to matrix A of a system’s coefficients, //augmented with vector b of the system’s right-hand side values //Input: Matrix A[1..n, 1..n] and column-vector b[1..n] //Output: An equivalent upper-triangular matrix in place of A with the //corresponding right-hand side values in the (n + 1)st column for i ← 1 to n do A[i, n + 1] ← b[i] //augments the matrix for i ← 1 to n − 1 do for j ← i + 1 to n do for k ← i to n + 1 do A[j, k] ← A[j, k] − A[i, k] ∗ A[j, i] /A[i, i] 6.2 Gaussian Elimination 211 There are two important observations to make about this pseudocode. First, it is not always correct: if A[i, i]= 0, we cannot divide by it and hence cannot use the ith row as a pivot for the ith iteration of the algorithm. In such a case, we should take advantage of the first elementary operation and exchange the ith row with some row below it that has a nonzero coefficient in the ith column. (If the system has a unique solution, which is the normal case for systems under consideration, such a row must exist.) Since we have to be prepared for the possibility of row exchanges anyway, we can take care of another potential difficulty: the possibility that A[i, i] is so small and consequently the scaling factor A[j, i]/A[i, i] so large that the new value of A[j, k]might become distorted by a round-off error caused by a subtraction of two numbers of greatly different magnitudes.3 To avoid this problem, we can always look for a row with the largest absolute value of the coefficient in the ith column, exchange it with the ith row, and then use the new A[i, i] as the ith iteration’s pivot. This modification, called partial pivoting, guarantees that the magnitude of the scaling factor will never exceed 1. The second observation is the fact that the innermost loop is written with a glaring inefficiency. Can you find it before checking the following pseudocode, which both incorporates partial pivoting and eliminates this inefficiency? ALGORITHM BetterForwardElimination(A[1..n, 1..n],b[1..n]) //Implements Gaussian elimination with partial pivoting //Input: Matrix A[1..n, 1..n] and column-vector b[1..n] //Output: An equivalent upper-triangular matrix in place of A and the //corresponding right-hand side values in place of the (n + 1)st column for i ← 1 to n do A[i, n + 1] ← b[i] //appends b to A as the last column for i ← 1 to n − 1 do pivotrow ← i for j ← i + 1 to n do if |A[j, i]| > |A[pivotrow, i]| pivotrow ← j for k ← i to n + 1 do swap(A[i, k],A[pivotrow, k]) for j ← i + 1 to n do temp ← A[j, i] /A[i, i] for k ← i to n + 1 do A[j, k] ← A[j, k] − A[i, k] ∗ temp Let us find the time efficiency of this algorithm. Its innermost loop consists of a single line, A[j, k] ← A[j, k] − A[i, k] ∗ temp, 3. We discuss round-off errors in more detail in Section 11.4. 212 Transform-and-Conquer which contains one multiplication and one subtraction. On most computers, multi- plication is unquestionably more expensive than addition/subtraction, and hence it is multiplication that is usually quoted as the algorithm’s basic operation.4 The standard summation formulas and rules reviewed in Section 2.3 (see also Appen- dix A) are very helpful in the following derivation: C(n) = n−1 i=1 n j=i+1 n+1 k=i 1 = n−1 i=1 n j=i+1 (n + 1 − i + 1) = n−1 i=1 n j=i+1 (n + 2 − i) = n−1 i=1 (n + 2 − i)(n − (i + 1) + 1) = n−1 i=1 (n + 2 − i)(n − i) = (n + 1)(n − 1) + n(n − 2) + ...+ 3 . 1 = n−1 j=1 (j + 2)j = n−1 j=1 j2 + n−1 j=1 2j = (n − 1)n(2n − 1) 6 + 2(n − 1)n 2 = n(n − 1)(2n + 5) 6 ≈ 1 3 n3 ∈ (n3). Since the second (back substitution) stage of Gaussian elimination is in (n2), as you are asked to show in the exercises, the running time is dominated by the cubic elimination stage, making the entire algorithm cubic as well. Theoretically, Gaussian elimination always either yields an exact solution to a system of linear equations when the system has a unique solution or discovers that no such solution exists. In the latter case, the system will have either no solutions or infinitely many of them. In practice, solving systems of significant size on a computer by this method is not nearly so straightforward as the method would lead us to believe. The principal difficulty lies in preventing an accumulation of round-off errors (see Section 11.4). Consult textbooks on numerical analysis that analyze this and other implementation issues in great detail. LU Decomposition Gaussian elimination has an interesting and very useful byproduct called LU de- composition of the coefficient matrix. In fact, modern commercial implementa- tions of Gaussian elimination are based on such a decomposition rather than on the basic algorithm outlined above. EXAMPLE Let us return to the example in the beginning of this section, where we applied Gaussian elimination to the matrix 4. As we mentioned in Section 2.1, on some computers multiplication is not necessarily more expensive than addition/subtraction. For this algorithm, this point is moot since we can simply count the number of times the innermost loop is executed, which is, of course, exactly the same number as the number of multiplications and the number of subtractions there. 6.2 Gaussian Elimination 213 A = ⎡ ⎣ 2 −11 41−1 111 ⎤ ⎦ . Consider the lower-triangular matrix L made up of 1’s on its main diagonal and the row multiples used in the forward elimination process L = ⎡ ⎣ 100 210 1 2 1 2 1 ⎤ ⎦ and the upper-triangular matrix U that was the result of this elimination U = ⎡ ⎣ 2 −11 03−3 002 ⎤ ⎦ . It turns out that the product LU of these matrices is equal to matrix A. (For this particular pair of L and U, you can verify this fact by direct multiplication, but as a general proposition, it needs, of course, a proof, which we omit here.) Therefore, solving the system Ax = b is equivalent to solving the system LUx = b. The latter system can be solved as follows. Denote y = Ux, then Ly = b. Solve the system Ly = b first, which is easy to do because L is a lower-triangular matrix; then solve the system Ux = y, with the upper-triangular matrix U, to find x. Thus, for the system at the beginning of this section, we first solve Ly = b: ⎡ ⎣ 100 210 1 2 1 2 1 ⎤ ⎦ ⎡ ⎣ y1 y2 y3 ⎤ ⎦ = ⎡ ⎣ 1 5 0 ⎤ ⎦ . Its solution is y1 = 1,y2 = 5 − 2y1 = 3,y3 = 0 − 1 2 y1 − 1 2 y2 =−2. Solving Ux = y means solving ⎡ ⎣ 2 −11 03−3 002 ⎤ ⎦ ⎡ ⎣ x1 x2 x3 ⎤ ⎦ = ⎡ ⎣ 1 3 −2 ⎤ ⎦ , and the solution is x3 = (−2)/2 =−1,x2 = (3 − (−3)x3)/3 = 0,x1 = (1 − x3 − (−1)x2)/2 = 1. Note that once we have the LU decomposition of matrix A, we can solve systems Ax = b with as many right-hand side vectors b as we want to, one at a time. This is a distinct advantage over the classic Gaussian elimination discussed earlier. Also note that the LU decomposition does not actually require extra memory, because we can store the nonzero part of U in the upper-triangular part of A 214 Transform-and-Conquer (including the main diagonal) and store the nontrivial part of L below the main diagonal of A. Computing a Matrix Inverse Gaussian elimination is a very useful algorithm that tackles one of the most important problems of applied mathematics: solving systems of linear equations. In fact, Gaussian elimination can also be applied to several other problems of linear algebra, such as computing a matrix inverse. The inverse of an n × n matrix A is an n × n matrix, denoted A−1, such that AA−1 = I, where I is the n × n identity matrix (the matrix with all zero elements except the main diagonal elements, which are all ones). Not every square matrix has an inverse, but when it exists, the inverse is unique. If a matrix A does not have an inverse, it is called singular. One can prove that a matrix is singular if and only if one of its rows is a linear combination (a sum of some multiples) of the other rows. A convenient way to check whether a matrix is nonsingular is to apply Gaussian elimination: if it yields an upper-triangular matrix with no zeros on the main diagonal, the matrix is nonsingular; otherwise, it is singular. So being singular is a very special situation, and most square matrices do have their inverses. Theoretically, inverse matrices are very important because they play the role of reciprocals in matrix algebra, overcoming the absence of the explicit division operation for matrices. For example, in a complete analogy with a linear equation in one unknown ax = b whose solution can be written as x = a−1b (if a is not zero), we can express a solution to a system of n equations in n unknowns Ax = b as x = A−1b (if A is nonsingular) where b is, of course, a vector, not a number. According to the definition of the inverse matrix for a nonsingular n × n matrix A, to compute it we need to find n2 numbers xij ,1≤ i, j ≤ n, such that ⎡ ⎢⎢⎣ a11 a12 ... a1n a21 a22 ... a2n... an1 an2 ... ann ⎤ ⎥⎥⎦ ⎡ ⎢⎢⎣ x11 x12 ... x1n x21 x22 ... x2n... xn1 xn2 ... xnn ⎤ ⎥⎥⎦ = ⎡ ⎢⎢⎣ 10... 0 01... 0 ... 00... 1 ⎤ ⎥⎥⎦ . We can find the unknowns by solving n systems of linear equations that have the same coefficient matrix A, the vector of unknowns xj is the jth column of the inverse, and the right-hand side vector ej is the jth column of the identity matrix (1 ≤ j ≤ n): Axj = ej. We can solve these systems by applying Gaussian elimination to matrix A aug- mented by the n × n identity matrix. Better yet, we can use forward elimina- tion to find the LU decomposition of A and then solve the systems LUxj = ej, j = 1,...,n, as explained earlier. 6.2 Gaussian Elimination 215 Computing a Determinant Another problem that can be solved by Gaussian elimination is computing a determinant. The determinant of an n × n matrix A, denoted det A or |A|, is a number whose value can be defined recursively as follows. If n = 1, i.e., if A consists of a single element a11, det A is equal to a11; for n>1, det A is computed by the recursive formula det A = n j=1 sja1j det Aj, where sj is +1ifj is odd and −1ifj is even, a1j is the element in row 1 and column j, and Aj is the (n − 1) × (n − 1) matrix obtained from matrix A by deleting its row 1 and column j. In particular, for a 2 × 2 matrix, the definition implies a formula that is easy to remember: det  a11 a12 a21 a22  = a11 det [a22] − a12 det [a21] = a11a22 − a12a21. In other words, the determinant of a 2 × 2 matrix is simply equal to the difference between the products of its diagonal elements. Fora3× 3 matrix, we get det ⎡ ⎣ a11 a12 a13 a21 a22 a23 a31 a32 a33 ⎤ ⎦ = a11 det  a22 a23 a32 a33  − a12 det  a21 a23 a31 a33  + a13 det  a21 a22 a31 a32  = a11a22a33 + a12a23a31 + a13a21a32 − a11a23a32 − a12a21a33 − a13a22a31. Incidentally, this formula is very handy in a variety of applications. In particular, we used it twice already in Section 5.5 as a part of the quickhull algorithm. But what if we need to compute a determinant of a large matrix? Although this is a task that is rarely needed in practice, it is worth discussing nevertheless. Using the recursive definition can be of little help because it implies computing the sum of n!terms. Here, Gaussian elimination comes to the rescue again. The central point is the fact that the determinant of an upper-triangular matrix is equal to the product of elements on its main diagonal, and it is easy to see how elementary operations employed by the elimination algorithm influence the determinant’s value. (Basically, it either remains unchanged or changes a sign or is multiplied by the constant used by the elimination algorithm.) As a result, we can compute the determinant of an n × n matrix in cubic time. Determinants play an important role in the theory of systems of linear equa- tions. Specifically, a system of n linear equations in n unknowns Ax = b has a unique solution if and only if the determinant of its coefficient matrix det A is 216 Transform-and-Conquer not equal to zero. Moreover, this solution can be found by the formulas called Cramer’s rule, x1 = det A1 det A ,...,xj = det Aj det A ,...,xn = det An det A , where det Aj is the determinant of the matrix obtained by replacing the jth column of A by the column b. You are asked to investigate in the exercises whether using Cramer’s rule is a good algorithm for solving systems of linear equations. Exercises 6.2 1. Solve the following system by Gaussian elimination: x1 + x2 + x3 = 2 2x1 + x2 + x3 = 3 x1 − x2 + 3x3 = 8. 2. a. Solve the system of the previous question by the LU decomposition method. b. From the standpoint of general algorithm design techniques, how would you classify the LU decomposition method? 3. Solve the system of Problem 1 by computing the inverse of its coefficient matrix and then multiplying it by the vector on the right-hand side. 4. Would it be correct to get the efficiency class of the forward elimination stage of Gaussian elimination as follows? C(n) = n−1 i=1 n j=i+1 n+1 k=i 1 = n−1 i=1 (n + 2 − i)(n − i) = n−1 i=1 [(n + 2)n − i(2n + 2) + i2] = n−1 i=1 (n + 2)n − n−1 i=1 (2n + 2)i + n−1 i=1 i2. Since s1(n) = n−1 i=1 (n + 2)n ∈ (n3), s2(n) = n−1 i=1 (2n + 2)i ∈ (n3), and s3(n) = n−1 i=1 i2 ∈ (n3), s1(n) − s2(n) + s3(n) ∈ (n3). 5. Write pseudocode for the back-substitution stage of Gaussian elimination and show that its running time is in (n2). 6. Assuming that division of two numbers takes three times longer than their multiplication, estimate how much faster BetterForwardElimination is than ForwardElimination. (Of course, you should also assume that a compiler is not going to eliminate the inefficiency in ForwardElimination.) 6.2 Gaussian Elimination 217 7. a. Give an example of a system of two linear equations in two unknowns that has a unique solution and solve it by Gaussian elimination. b. Give an example of a system of two linear equations in two unknowns that has no solution and apply Gaussian elimination to it. c. Give an example of a system of two linear equations in two unknowns that has infinitely many solutions and apply Gaussian elimination to it. 8. The Gauss-Jordan elimination method differs from Gaussian elimination in that the elements above the main diagonal of the coefficient matrix are made zero at the same time and by the same use of a pivot row as the elements below the main diagonal. a. Apply the Gauss-Jordan method to the system of Problem 1 of these exercises. b. What general design strategy is this algorithm based on? c. In general, how many multiplications are made by this method in solving a system of n equations in n unknowns? How does this compare with the number of multiplications made by the Gaussian elimination method in both its elimination and back-substitution stages? 9. A system Ax = b of n linear equations in n unknowns has a unique solution if and only if det A = 0. Is it a good idea to check this condition before applying Gaussian elimination to the system? 10. a. Apply Cramer’s rule to solve the system of Problem 1 of these exercises. b. Estimate how many times longer it will take to solve a system of n linear equations in n unknowns by Cramer’s rule than by Gaussian elimination. Assume that all the determinants in Cramer’s rule formulas are computed independently by Gaussian elimination. 11. Lights out This one-person game is played on an n × n board composed of 1 × 1 light panels. Each panel has a switch that can be turned on and off, thereby toggling the on/off state of this and four vertically and horizontally adjacent panels. (Of course, toggling a corner square affects a total of three panels, and toggling a noncorner panel on the board’s border affects a total of four squares.) Given an initial subset of lighted squares, the goal is to turn all the lights off. a. Show that an answer can be found by solving a system of linear equations with 0/1 coefficients and right-hand sides using the modulo 2 arithmetic. b. Use Gaussian elimination to solve the 2 × 2 “all-ones” instance of this problem, where all the panels of the 2 × 2 board are initially lit. c. Use Gaussian elimination to solve the 3 × 3 “all-ones” instance of this problem, where all the panels of the 3 × 3 board are initially lit. 218 Transform-and-Conquer 6.3 Balanced Search Trees In Sections 1.4, 4.5, and 5.3, we discussed the binary search tree—one of the prin- cipal data structures for implementing dictionaries. It is a binary tree whose nodes contain elements of a set of orderable items, one element per node, so that all ele- ments in the left subtree are smaller than the element in the subtree’s root, and all the elements in the right subtree are greater than it. Note that this transformation from a set to a binary search tree is an example of the representation-change tech- nique. What do we gain by such transformation compared to the straightforward implementation of a dictionary by, say, an array? We gain in the time efficiency of searching, insertion, and deletion, which are all in (log n), but only in the av- erage case. In the worst case, these operations are in (n) because the tree can degenerate into a severely unbalanced one with its height equal to n − 1. Computer scientists have expended a lot of effort in trying to find a structure that preserves the good properties of the classical binary search tree—principally, the logarithmic efficiency of the dictionary operations and having the set’s ele- ments sorted—but avoids its worst-case degeneracy. They have come up with two approaches. The first approach is of the instance-simplification variety: an unbalanced binary search tree is transformed into a balanced one. Because of this, such trees are called self-balancing. Specific implementations of this idea differ by their definition of balance. An AVL tree requires the difference between the heights of the left and right subtrees of every node never exceed 1. A red-black tree tolerates the height of one subtree being twice as large as the other subtree of the same node. If an insertion or deletion of a new node creates a tree with a violated balance requirement, the tree is restructured by one of a family of special transformations called rotations that restore the balance required. In this section, we will discuss only AVL trees. Information about other types of binary search trees that utilize the idea of rebalancing via rotations, including red-black trees and splay trees, can be found in the references [Cor09], [Sed02], and [Tar83]. The second approach is of the representation-change variety: allow more than one element in a node of a search tree. Specific cases of such trees are 2-3 trees, 2-3-4 trees, and more general and important B-trees. They differ in the number of elements admissible in a single node of a search tree, but all are perfectly balanced. We discuss the simplest case of such trees, the 2-3 tree, in this section, leaving the discussion of B-trees for Chapter 7. AVL Trees AVL trees were invented in 1962 by two Russian scientists, G. M. Adelson-Velsky and E. M. Landis [Ade62], after whom this data structure is named. 6.3 Balanced Search Trees 219 10 10 5 20 5 20 4 7 12 4 7 2 8 2 8 1 1 1–1 –11 0 0 0 0 00 0 0 2 (a) (b) FIGURE 6.2 (a) AVL tree. (b) Binary search tree that is not an AVL tree. The numbers above the nodes indicate the nodes’ balance factors. DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of the node’s left and right subtrees, is either 0 or +1 or −1. (The height of the empty tree is defined as −1. Of course, the balance factor can also be computed as the difference between the numbers of levels rather than the height difference of the node’s left and right subtrees.) For example, the binary search tree in Figure 6.2a is an AVL tree but the one in Figure 6.2b is not. If an insertion of a new node makes an AVL tree unbalanced, we transform the tree by a rotation. A rotation in an AVL tree is a local transformation of its subtree rooted at a node whose balance has become either +2or−2. If there are several such nodes, we rotate the tree rooted at the unbalanced node that is the closest to the newly inserted leaf. There are only four types of rotations; in fact, two of them are mirror images of the other two. In their simplest form, the four rotations are shown in Figure 6.3. The first rotation type is called the single right rotation,orR-rotation. (Imag- ine rotating the edge connecting the root and its left child in the binary tree in Figure 6.3a to the right.) Figure 6.4 presents the single R-rotation in its most gen- eral form. Note that this rotation is performed after a new key is inserted into the left subtree of the left child of a tree whose root had the balance of +1 before the insertion. The symmetric single left rotation,orL-rotation, is the mirror image of the single R-rotation. It is performed after a new key is inserted into the right subtree of the right child of a tree whose root had the balance of −1 before the insertion. (You are asked to draw a diagram of the general case of the single L-rotation in the exercises.) 220 Transform-and-Conquer 3 3 3 3 2 2 2 2 2 1 1 1 1 1 3 1 2 –1 –1 1 –2 –2 2 0 0 00 2 1 3 0 0 0 0 0 0 2 1 3 0 00 2 1 3 0 00 (a) (b) (c) (d) R L LR RL FIGURE 6.3 Four rotation types for AVL trees with three nodes. (a) Single R-rotation. (b) Single L-rotation. (c) Double LR-rotation. (d) Double RL-rotation. The second rotation type is called the double left-right rotation (LR- rotation). It is, in fact, a combination of two rotations: we perform the L-rotation of the left subtree of root r followed by the R-rotation of the new tree rooted at r (Figure 6.5). It is performed after a new key is inserted into the right subtree of the left child of a tree whose root had the balance of +1 before the insertion. 6.3 Balanced Search Trees 221 T3 T2T1 c r T3T2 T1 c r single R-rotation FIGURE 6.4 General form of the R-rotation in the AVL tree. A shaded node is the last one inserted. T4 T3T2 T1 c g or r T4 T3T2 T1 c or g r double LR-rotation FIGURE 6.5 General form of the double LR-rotation in the AVL tree. A shaded node is the last one inserted. It can be either in the left subtree or in the right subtree of the root’s grandchild. The double right-left rotation (RL-rotation) is the mirror image of the double LR-rotation and is left for the exercises. Note that the rotations are not trivial transformations, though fortunately they can be done in constant time. Not only should they guarantee that a resulting tree is balanced, but they should also preserve the basic requirements of a binary search tree. For example, in the initial tree of Figure 6.4, all the keys of subtree T1 are smaller than c, which is smaller than all the keys of subtree T2, which are smaller than r, which is smaller than all the keys of subtree T3. And the same relationships among the key values hold, as they must, for the balanced tree after the rotation. 222 Transform-and-Conquer 5 5 6 6 6 6 6 5 5 3 3 2 2 5 8 8 8 8 83 5 5 8 6 –1 –1 –2 0 0 0 0 00 00 0 0 1 1 1 1 56 22 5 4 4 68 33 0 0 0 0 00 1 0 –1 –1 –2 –1 0 86 55 22 4 8 7 4 76 33 000 0 1 0 00 00 0 0 2 2 2 0 0 00 L(5) R(5) LR(6) RL(6) FIGURE 6.6 Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive insertions. The parenthesized number of a rotation’s abbreviation indicates the root of the tree being reorganized. An example of constructing an AVL tree for a given list of numbers is shown in Figure 6.6. As you trace the algorithm’s operations, keep in mind that if there are several nodes with the ±2 balance, the rotation is done for the tree rooted at the unbalanced node that is the closest to the newly inserted leaf. How efficient are AVL trees? As with any search tree, the critical charac- teristic is the tree’s height. It turns out that it is bounded both above and below 6.3 Balanced Search Trees 223 by logarithmic functions. Specifically, the height h of any AVL tree with n nodes satisfies the inequalities log2 n≤h<1.4405 log2(n + 2) − 1.3277. (These weird-looking constants are round-offs of some irrational numbers related to Fibonacci numbers and the golden ratio—see Section 2.5.) The inequalities immediately imply that the operations of searching and in- sertion are (log n) in the worst case. Getting an exact formula for the average height of an AVL tree constructed for random lists of keys has proved to be dif- ficult, but it is known from extensive experiments that it is about 1.01log2 n + 0.1 except when n is small [KnuIII, p. 468]. Thus, searching in an AVL tree requires, on average, almost the same number of comparisons as searching in a sorted array by binary search. The operation of key deletion in an AVL tree is considerably more difficult than insertion, but fortunately it turns out to be in the same efficiency class as insertion, i.e., logarithmic. These impressive efficiency characteristics come at a price, however. The drawbacks of AVL trees are frequent rotations and the need to maintain bal- ances for its nodes. These drawbacks have prevented AVL trees from becoming the standard structure for implementing dictionaries. At the same time, their un- derlying idea—that of rebalancing a binary search tree via rotations—has proved to be very fruitful and has led to discoveries of other interesting variations of the classical binary search tree. 2-3 Trees As mentioned at the beginning of this section, the second idea of balancing a search tree is to allow more than one key in the same node of such a tree. The simplest implementation of this idea is 2-3 trees, introduced by the U.S. computer scientist John Hopcroft in 1970. A 2-3 tree is a tree that can have nodes of two kinds: 2-nodes and 3-nodes. A 2-node contains a single key K and has two children: the left child serves as the root of a subtree whose keys are less than K, and the right child serves as the root of a subtree whose keys are greater than K. (In other words, a 2-node is the same kind of node we have in the classical binary search tree.) A 3-node contains two ordered keys K1 and K2 (K1 K2(K1, K2) K1, K2 > K K < K 2-node 3-node FIGURE 6.7 Two kinds of nodes of a 2-3 tree. subtree if K is, respectively, smaller or larger than the root’s key. If the root is a 3- node, we know after no more than two key comparisons whether the search can be stopped (if K is equal to one of the root’s keys) or in which of the root’s three subtrees it needs to be continued. Inserting a new key in a 2-3 tree is done as follows. First of all, we always insert a new key K in a leaf, except for the empty tree. The appropriate leaf is found by performing a search for K. If the leaf in question is a 2-node, we insert K there as either the first or the second key, depending on whether K is smaller or larger than the node’s old key. If the leaf is a 3-node, we split the leaf in two: the smallest of the three keys (two old ones and the new key) is put in the first leaf, the largest key is put in the second leaf, and the middle key is promoted to the old leaf’s parent. (If the leaf happens to be the tree’s root, a new root is created to accept the middle key.) Note that promotion of a middle key to its parent can cause the parent’s overflow (if it was a 3-node) and hence can lead to several node splits along the chain of the leaf’s ancestors. An example of a 2-3 tree construction is given in Figure 6.8. As for any search tree, the efficiency of the dictionary operations depends on the tree’s height. So let us first find an upper bound for it. A 2-3 tree of height h with the smallest number of keys is a full tree of 2-nodes (such as the final tree in Figure 6.8 for h = 2). Therefore, for any 2-3 tree of height h with n nodes, we get the inequality n ≥ 1 + 2 + ...+ 2h = 2h+1 − 1, and hence h ≤ log2(n + 1) − 1. On the other hand, a 2-3 tree of height h with the largest number of keys is a full tree of 3-nodes, each with two keys and three children. Therefore, for any 2-3 tree with n nodes, n ≤ 2 . 1 + 2 . 3 + ...+ 2 . 3h = 2(1 + 3 + ...+ 3h) = 3h+1 − 1 6.3 Balanced Search Trees 225 8 8 8 5 2 2 2 2 3 8 9 9 94 47 7 25 5 9 99 9 9 93, 5 3, 8 3, 8 3, 8 4, 5 5, 9 2, 3, 5 3, 5, 8 4, 5, 7 5, 8, 9 FIGURE 6.8 Construction of a 2-3 tree for the list 9, 5, 8, 3, 2, 4, 7. and hence h ≥ log3(n + 1) − 1. These lower and upper bounds on height h, log3(n + 1) − 1 ≤ h ≤ log2(n + 1) − 1, imply that the time efficiencies of searching, insertion, and deletion are all in (log n) in both the worst and average case. We consider a very important gener- alization of 2-3 trees, called B-trees, in Section 7.4. Exercises 6.3 1. Which of the following binary trees are AVL trees? 3 6 4 6 3 6 5 5 5 2 8 2 8 1 1 3 7 9 2 7 9 (a)(b)(c) 2. a. For n = 1, 2, 3, 4, and 5, draw all the binary trees with n nodes that satisfy the balance requirement of AVL trees. 226 Transform-and-Conquer b. Draw a binary tree of height 4 that can be an AVLtree and has the smallest number of nodes among all such trees. 3. Draw diagrams of the single L-rotation and of the double RL-rotation in their general form. 4. For each of the following lists, construct an AVL tree by inserting their ele- ments successively, starting with the empty tree. a. 1, 2, 3, 4, 5, 6 b. 6, 5, 4, 3, 2, 1 c. 3, 6, 5, 1, 2, 4 5. a. For an AVLtree containing real numbers, design an algorithm for comput- ing the range (i.e., the difference between the largest and smallest numbers in the tree) and determine its worst-case efficiency. b. True or false: The smallest and the largest keys in an AVL tree can always be found on either the last level or the next-to-last level? 6. Write a program for constructing an AVL tree for a given list of n distinct integers. 7. a. Construct a 2-3 tree for the list C, O, M, P,U, T, I, N, G. Use the alphabetical order of the letters and insert them successively starting with the empty tree. b. Assuming that the probabilities of searching for each of the keys (i.e., the letters) are the same, find the largest number and the average number of key comparisons for successful searches in this tree. 8. Let TB and T2-3 be, respectively, a classical binary search tree and a 2-3 tree constructed for the same list of keys inserted in the corresponding trees in the same order. True or false: Searching for the same key in T2-3 always takes fewer or the same number of key comparisons as searching in TB? 9. For a 2-3 tree containing real numbers, design an algorithm for computing the range (i.e., the difference between the largest and smallest numbers in the tree) and determine its worst-case efficiency. 10. Write a program for constructing a 2-3 tree for a given list of n integers. 6.4 Heaps and Heapsort The data structure called the “heap” is definitely not a disordered pile of items as the word’s definition in a standard dictionary might suggest. Rather, it is a clever, partially ordered data structure that is especially suitable for implementing priority queues. Recall that a priority queue is a multiset of items with an orderable characteristic called an item’s priority, with the following operations: 6.4 Heaps and Heapsort 227 10 10 10 5 7 4 2 1 2 1 6 2 1 5 57 7 FIGURE 6.9 Illustration of the definition of heap: only the leftmost tree is a heap. finding an item with the highest (i.e., largest) priority deleting an item with the highest priority adding a new item to the multiset It is primarily an efficient implementation of these operations that makes the heap both interesting and useful. Priority queues arise naturally in such ap- plications as scheduling job executions by computer operating systems and traf- fic management by communication networks. They also arise in several impor- tant algorithms, e.g., Prim’s algorithm (Section 9.1), Dijkstra’s algorithm (Sec- tion 9.3), Huffman encoding (Section 9.4), and branch-and-bound applications (Section 12.2). The heap is also the data structure that serves as a cornerstone of a theoretically important sorting algorithm called heapsort. We discuss this algo- rithm after we define the heap and investigate its basic properties. Notion of the Heap DEFINITION A heap can be defined as a binary tree with keys assigned to its nodes, one key per node, provided the following two conditions are met: 1. The shape property—the binary tree is essentially complete (or simply com- plete), i.e., all its levels are full except possibly the last level, where only some rightmost leaves may be missing. 2. The parental dominance or heap property—the key in each node is greater than or equal to the keys in its children. (This condition is considered auto- matically satisfied for all leaves.)5 For example, consider the trees of Figure 6.9. The first tree is a heap. The second one is not a heap, because the tree’s shape property is violated. And the third one is not a heap, because the parental dominance fails for the node with key 5. Note that key values in a heap are ordered top down; i.e., a sequence of values on any path from the root to a leaf is decreasing (nonincreasing, if equal keys are allowed). However, there is no left-to-right order in key values; i.e., there is no 5. Some authors require the key at each node to be less than or equal to the keys at its children. We call this variation a min-heap. 228 Transform-and-Conquer 10 8 7 5 2 53 1 6 10875216351 012345678910 1 index value the array representation parents leaves FIGURE 6.10 Heap and its array representation. relationship among key values for nodes either on the same level of the tree or, more generally, in the left and right subtrees of the same node. Here is a list of important properties of heaps, which are not difficult to prove (check these properties for the heap of Figure 6.10, as an example). 1. There exists exactly one essentially complete binary tree with n nodes. Its height is equal to log2 n. 2. The root of a heap always contains its largest element. 3. A node of a heap considered with all its descendants is also a heap. 4. A heap can be implemented as an array by recording its elements in the top- down, left-to-right fashion. It is convenient to store the heap’s elements in positions 1 through n of such an array, leaving H[0] either unused or putting there a sentinel whose value is greater than every element in the heap. In such a representation, a. the parental node keys will be in the first n/2 positions of the array, while the leaf keys will occupy the last n/2 positions; b. the children of a key in the array’s parental position i(1 ≤ i ≤n/2) will be in positions 2i and 2i + 1, and, correspondingly, the parent of a key in position i(2 ≤ i ≤ n) will be in position i/2. Thus, we could also define a heap as an array H[1..n] in which every element in position i in the first half of the array is greater than or equal to the elements in positions 2i and 2i + 1, i.e., H[i] ≥ max{H[2i],H[2i + 1]} for i = 1,...,n/2. (Of course, if 2i + 1 >n, just H[i] ≥ H[2i] needs to be satisfied.) While the ideas behind the majority of algorithms dealing with heaps are easier to understand if we think of heaps as binary trees, their actual implementations are usually much simpler and more efficient with arrays. How can we construct a heap for a given list of keys? There are two principal alternatives for doing this. The first is the bottom-up heap construction algorithm illustrated in Figure 6.11. It initializes the essentially complete binary tree with n nodes by placing keys in the order given and then “heapifies” the tree as follows. Starting with the last parental node, the algorithm checks whether the parental 6.4 Heaps and Heapsort 229 2 9 7 6 5 8 2 9 8 6 5 7 2 9 8 6 5 7 9 2 8 6 5 7 9 6 8 2 5 7 2 9 8 6 5 7 FIGURE 6.11 Bottom-up construction of a heap for the list 2, 9, 7, 6, 5, 8. The double- headed arrows show key comparisons verifying the parental dominance. dominance holds for the key in this node. If it does not, the algorithm exchanges the node’s key K with the larger key of its children and checks whether the parental dominance holds for K in its new position. This process continues until the parental dominance for K is satisfied. (Eventually, it has to because it holds automatically for any key in a leaf.) After completing the “heapification” of the subtree rooted at the current parental node, the algorithm proceeds to do the same for the node’s immediate predecessor. The algorithm stops after this is done for the root of the tree. ALGORITHM HeapBottomUp(H[1..n]) //Constructs a heap from elements of a given array // by the bottom-up algorithm //Input: An array H[1..n] of orderable items //Output: A heap H[1..n] for i ←n/2 downto 1 do k ← i; v ← H[k] heap ← false while not heap and 2 ∗ k ≤ n do j ← 2 ∗ k if j0 from the ith vertex to the jth vertex of a graph (undirected or directed) equals the (i, j)th element of Ak where A is the adjacency matrix of the graph. Therefore, the problem of counting a graph’s paths can be solved with an algorithm for computing an appropriate power of its adjacency matrix. Note that the exponentiation algorithms we discussed before for computing powers of numbers are applicable to matrices as well. As a specific example, consider the graph of Figure 6.16. Its adjacency matrix A and its square A2 indicate the numbers of paths of length 1 and 2, respectively, between the corresponding vertices of the graph. In particular, there are three ba a b c ddc abcd A = 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 a b c d abcd A2 = 3 0 1 1 0 1 1 1 1 1 2 1 1 1 1 2 FIGURE 6.16 A graph, its adjacency matrix A, and its square A2. The elements of A and A2 indicate the numbers of paths of lengths 1 and 2, respectively. 6.6 Problem Reduction 243 paths of length 2 that start and end at vertex a (a − b − a,a − c − a, and a − d − a); but there is only one path of length 2 from a to c(a− d − c). Reduction of Optimization Problems Our next example deals with solving optimization problems. If a problem asks to find a maximum of some function, it is said to be a maximization problem;ifit asks to find a function’s minimum, it is called a minimization problem. Suppose now that you need to find a minimum of some function f(x) and you have an algorithm for function maximization. How can you take advantage of the latter? The answer lies in the simple formula min f(x)=−max[−f(x)]. In other words, to minimize a function, we can maximize its negative instead and, to get a correct minimal value of the function itself, change the sign of the answer. This property is illustrated for a function of one real variable in Figure 6.17. Of course, the formula max f(x)=−min[−f(x)] is valid as well; it shows how a maximization problem can be reduced to an equivalent minimization problem. This relationship between minimization and maximization problems is very general: it holds for functions defined on any domain D. In particular, we can y f(x*) f(x) –f(x) –f(x*) x* x FIGURE 6.17 Relationship between minimization and maximization problems: min f(x)=−max[−f(x)]. 244 Transform-and-Conquer apply it to functions of several variables subject to additional constraints. A very important class of such problems is introduced below in this section. Now that we are on the topic of function optimization, it is worth pointing out that the standard calculus procedure for finding extremum points of a function is, in fact, also based on problem reduction. Indeed, it suggests finding the function’s derivative f (x) and then solving the equation f (x) = 0 to find the function’s critical points. In other words, the optimization problem is reduced to the problem of solving an equation as the principal part of finding extremum points. Note that we are not calling the calculus procedure an algorithm, since it is not clearly defined. In fact, there is no general method for solving equations. A little secret of calculus textbooks is that problems are carefully selected so that critical points can always be found without difficulty. This makes the lives of both students and instructors easier but, in the process, may unintentionally create a wrong impression in students’ minds. Linear Programming Many problems of optimal decision making can be reduced to an instance of the linear programming problem—a problem of optimizing a linear function of several variables subject to constraints in the form of linear equations and linear inequalities. EXAMPLE 1 Consider a university endowment that needs to invest $100 million. This sum has to be split between three types of investments: stocks, bonds, and cash. The endowment managers expect an annual return of 10%, 7%, and 3% for their stock, bond, and cash investments, respectively. Since stocks are more risky than bonds, the endowment rules require the amount invested in stocks to be no more than one-third of the moneys invested in bonds. In addition, at least 25% of the total amount invested in stocks and bonds must be invested in cash. How should the managers invest the money to maximize the return? Let us create a mathematical model of this problem. Let x, y, and z be the amounts (in millions of dollars) invested in stocks, bonds, and cash, respectively. By using these variables, we can pose the following optimization problem: maximize 0.10x + 0.07y + 0.03z subject to x + y + z = 100 x ≤ 1 3y z ≥ 0.25(x + y) x ≥ 0,y≥ 0,z≥ 0. Although this example is both small and simple, it does show how a problem of optimal decision making can be reduced to an instance of the general linear programming problem 6.6 Problem Reduction 245 maximize (or minimize) c1x1 + ...+ cnxn subject to ai1x1 + ...+ ainxn ≤ (or ≥ or =) bi for i = 1,...,m x1 ≥ 0,...,xn ≥ 0. (The last group of constraints—called the nonnegativity constraints—are, strictly speaking, unnecessary because they are special cases of more general constraints ai1x1 + ...+ ainxn ≥ bi, but it is convenient to treat them separately.) Linear programming has proved to be flexible enough to model a wide variety of important applications, such as airline crew scheduling, transportation and communication network planning, oil exploration and refining, and industrial production optimization. In fact, linear programming is considered by many as one of the most important achievements in the history of applied mathematics. The classic algorithm for this problem is called the simplex method (Sec- tion 10.1). It was discovered by the U.S. mathematician George Dantzig in the 1940s [Dan63]. Although the worst-case efficiency of this algorithm is known to be exponential, it performs very well on typical inputs. Moreover, a more recent al- gorithm by Narendra Karmarkar [Kar84] not only has a proven polynomial worst- case efficiency but has also performed competitively with the simplex method in empirical tests. It is important to stress, however, that the simplex method and Karmarkar’s algorithm can successfully handle only linear programming problems that do not limit its variables to integer values. When variables of a linear programming problem are required to be integers, the linear programming problem is said to be an integer linear programming problem. Except for some special cases (e.g., the assignment problem and the problems discussed in Sections 10.2–10.4), integer linear programming problems are much more difficult. There is no known polynomial-time algorithm for solving an arbitrary instance of the general integer linear programming problem and, as we see in Chapter 11, such an algorithm quite possibly does not exist. Other approaches such as the branch-and-bound technique discussed in Section 12.2 are typically used for solving integer linear programming problems. EXAMPLE 2 Let us see how the knapsack problem can be reduced to a linear programming problem. Recall from Section 3.4 that the knapsack problem can be posed as follows. Given a knapsack of capacity W and n items of weights w1,...,wn and values v1,...,vn, find the most valuable subset of the items that fits into the knapsack. We consider first the continuous (or fractional) version of the problem, in which any fraction of any item given can be taken into the knapsack. Let xj, j = 1,...,n, be a variable representing a fraction of item j taken into the knapsack. Obviously, xj must satisfy the inequality 0 ≤ xj ≤ 1. Then the total weight of the selected items can be expressed by the sum n j=1 wjxj, and their total value by the sum n j=1 vjxj. Thus, the continuous version of the knapsack problem can be posed as the following linear programming problem: 246 Transform-and-Conquer maximize n j=1 vjxj subject to n j=1 wjxj ≤ W 0 ≤ xj ≤ 1 for j = 1,...,n. There is no need to apply a general method for solving linear programming problems here: this particular problem can be solved by a simple special algorithm that is introduced in Section 12.3. (But why wait? Try to discover it on your own now.) This reduction of the knapsack problem to an instance of the linear programming problem is still useful, though, to prove the correctness of the algorithm in question. In the discrete (or 0-1) version of the knapsack problem, we are only allowed either to take a whole item or not to take it at all. Hence, we have the following integer linear programming problem for this version: maximize n j=1 vjxj subject to n j=1 wjxj ≤ W xj ∈{0, 1} for j = 1,...,n. This seemingly minor modification makes a drastic difference for the com- plexity of this and similar problems constrained to take only discrete values in their potential ranges. Despite the fact that the 0-1 version might seem to be eas- ier because it can ignore any subset of the continuous version that has a fractional value of an item, the 0-1 version is, in fact, much more complicated than its con- tinuous counterpart. The reader interested in specific algorithms for solving this problem will find a wealth of literature on the subject, including the monographs [Mar90] and [Kel04]. Reduction to Graph Problems As we pointed out in Section 1.3, many problems can be solved by a reduction to one of the standard graph problems. This is true, in particular, for a variety of puzzles and games. In these applications, vertices of a graph typically represent possible states of the problem in question, and edges indicate permitted transi- tions among such states. One of the graph’s vertices represents an initial state and another represents a goal state of the problem. (There might be several vertices of the latter kind.) Such a graph is called a state-space graph. Thus, the transfor- mation just described reduces the problem to the question about a path from the initial-state vertex to a goal-state vertex. 6.6 Problem Reduction 247 Pg P Pw Pw P Pc Pg Pc Pg Pg wc | | Pg c | | Pwg w | | Pgc g | | Pwc | | Pwgc Pwgc | | Pwc | | g Pwg | | c Pg | | wc Pgc | | w FIGURE 6.18 State-space graph for the peasant, wolf, goat, and cabbage puzzle. EXAMPLE Let us revisit the classic river-crossing puzzle that was included in the exercises for Section 1.2. A peasant finds himself on a river bank with a wolf, a goat, and a head of cabbage. He needs to transport all three to the other side of the river in his boat. However, the boat has room only for the peasant himself and one other item (either the wolf, the goat, or the cabbage). In his absence, the wolf would eat the goat, and the goat would eat the cabbage. Find a way for the peasant to solve his problem or prove that it has no solution. The state-space graph for this problem is given in Figure 6.18. Its vertices are labeled to indicate the states they represent: P, w, g, c stand for the peasant, the wolf, the goat, and the cabbage, respectively; the two bars || denote the river; for convenience, we also label the edges by indicating the boat’s occupants for each crossing. In terms of this graph, we are interested in finding a path from the initial-state vertex labeled Pwgc||to the final-state vertex labeled ||Pwgc. It is easy to see that there exist two distinct simple paths from the initial- state vertex to the final state vertex (what are they?). If we find them by applying breadth-first search, we get a formal proof that these paths have the smallest number of edges possible. Hence, this puzzle has two solutions requiring seven river crossings, which is the minimum number of crossings needed. Our success in solving this simple puzzle should not lead you to believe that generating and investigating state-space graphs is always a straightforward task. To get a better appreciation of them, consult books on artificial intelligence (AI), the branch of computer science in which state-space graphs are a principal subject. 248 Transform-and-Conquer In this book, we deal with an important special case of state-space graphs in Sections 12.1 and 12.2. Exercises 6.6 1. a. Prove the equality lcm(m, n) = m . n gcd(m, n) that underlies the algorithm for computing lcm(m, n). b. Euclid’s algorithm is known to be in O(log n). If it is the algorithm that is used for computing gcd(m, n), what is the efficiency of the algorithm for computing lcm(m, n)? 2. You are given a list of numbers for which you need to construct a min-heap. (A min-heap is a complete binary tree in which every key is less than or equal to the keys in its children.) How would you use an algorithm for constructing a max-heap (a heap as defined in Section 6.4) to construct a min-heap? 3. Prove that the number of different paths of length k>0 from the ith vertex to the jth vertex in a graph (undirected or directed) equals the (i, j)th element of Ak where A is the adjacency matrix of the graph. 4. a. Design an algorithm with a time efficiency better than cubic for checking whether a graph with n vertices contains a cycle of length 3 [Man89]. b. Consider the following algorithm for the same problem. Starting at an arbi- trary vertex, traverse the graph by depth-first search and check whether its depth-first search forest has a vertex with a back edge leading to its grand- parent. If it does, the graph contains a triangle; if it does not, the graph does not contain a triangle as its subgraph. Is this algorithm correct? 5. Given n>3 points P1 = (x1,y1),...,Pn = (xn,yn) in the coordinate plane, design an algorithm to check whether all the points lie within a triangle with its vertices at three of the points given. (You can either design an algorithm from scratch or reduce the problem to another one with a known algorithm.) 6. Consider the problem of finding, for a given positive integer n, the pair of integers whose sum is n and whose product is as large as possible. Design an efficient algorithm for this problem and indicate its efficiency class. 7. The assignment problem introduced in Section 3.4 can be stated as follows: There are n people who need to be assigned to execute n jobs, one person per job. (That is, each person is assigned to exactly one job and each job is assigned to exactly one person.) The cost that would accrue if the ith person is assigned to the jth job is a known quantity C[i, j]for each pair i, j = 1,...,n. The problem is to assign the people to the jobs to minimize the total cost of 6.6 Problem Reduction 249 the assignment. Express the assignment problem as a 0-1 linear programming problem. 8. Solve the instance of the linear programming problem given in Section 6.6: maximize 0.10x + 0.07y + 0.03z subject to x + y + z = 100 x ≤ 1 3y z ≥ 0.25(x + y) x ≥ 0,y≥ 0,z≥ 0. 9. The graph-coloring problem is usually stated as the vertex-coloring prob- lem: Assign the smallest number of colors to vertices of a given graph so that no two adjacent vertices are the same color. Consider the edge-coloring problem: Assign the smallest number of colors possible to edges of a given graph so that no two edges with the same endpoint are the same color. Ex- plain how the edge-coloring problem can be reduced to a vertex-coloring problem. 10. Consider the two-dimensional post office location problem: given n points (x1,y1),...,(xn,yn) in the Cartesian plane, find a location (x, y) for a post office that minimizes 1 n n i=1(|xi − x|+|yi − y|), the average Manhattan dis- tance from the post office to these points. Explain how this problem can be efficiently solved by the problem reduction technique, provided the post office does not have to be located at one of the input points. 11. Jealous husbands There are n ≥ 2 married couples who need to cross a river. They have a boat that can hold no more than two people at a time. To complicate matters, all the husbands are jealous and will not agree on any crossing procedure that would put a wife on the same bank of the river with another woman’s husband without the wife’s husband being there too, even if there are other people on the same bank. Can they cross the river under such constraints? a. Solve the problem for n = 2. b. Solve the problem for n = 3, which is the classical version of this problem. c. Does the problem have a solution for n ≥ 4? If it does, indicate how many river crossings it will take; if it does not, explain why. 12. Double-n dominoes Dominoes are small rectangular tiles with dots called spots or pips embossed at both halves of the tiles. A standard “double-six” domino set has 28 tiles: one for each unordered pair of integers from (0, 0) to (6, 6). In general, a “double-n” domino set would consist of domino tiles for each unordered pair of integers from (0, 0) to (n, n). Determine all values of n for which one constructs a ring made up of all the tiles in a double-n domino set. 250 Transform-and-Conquer SUMMARY Transform-and-conquer is the fourth general algorithm design (and problem- solving) strategy discussed in the book. It is, in fact, a group of techniques based on the idea of transformation to a problem that is easier to solve. There are three principal varieties of the transform-and-conquer strategy: instance simplification, representation change, and problem reduction. Instance simplification is transforming an instance of a problem to an instance of the same problem with some special property that makes the problem easier to solve. List presorting, Gaussian elimination, and rotations in AVL trees are good examples of this strategy. Representation change implies changing one representation of a problem’s instance to another representation of the same instance. Examples discussed in this chapter include representation of a set by a 2-3 tree, heaps and heapsort, Horner’s rule for polynomial evaluation, and two binary exponentiation algorithms. Problem reduction calls for transforming a given problem to another problem that can be solved by a known algorithm. Among examples of applying this idea to algorithmic problem solving (see Section 6.6), reductions to linear programming and reductions to graph problems are especially important. Some examples used to illustrate transform-and-conquer happen to be very important data structures and algorithms. They are: heaps and heapsort, AVL and 2-3 trees, Gaussian elimination, and Horner’s rule. A heap is an essentially complete binary tree with keys (one per node) satisfying the parental dominance requirement. Though defined as binary trees, heaps are normally implemented as arrays. Heaps are most important for the efficient implementation of priority queues; they also underlie heapsort. Heapsort is a theoretically important sorting algorithm based on arranging elements of an array in a heap and then successively removing the largest element from a remaining heap. The algorithm’s running time is in (n log n) both in the worst case and in the average case; in addition, it is in-place. AVL trees are binary search trees that are always balanced to the extent possible for a binary tree. The balance is maintained by transformations of four types called rotations. All basic operations on AVL trees are in O(log n); it eliminates the bad worst-case efficiency of classic binary search trees. 2-3 trees achieve a perfect balance in a search tree by allowing a node to contain up to two ordered keys and have up to three children. This idea can be generalized to yield very important B-trees, discussed later in the book. Summary 251 Gaussian elimination—an algorithm for solving systems of linear equations— is a principal algorithm in linear algebra. It solves a system by transforming it to an equivalent system with an upper-triangular coefficient matrix, which is easy to solve by back substitutions. Gaussian elimination requires about 1 3n3 multiplications. Horner’s rule is an optimal algorithm for polynomial evaluation without coefficient preprocessing. It requires only n multiplications and n additions to evaluate an n-degree polynomial at a given point. Horner’s rule also has a few useful byproducts, such as the synthetic division algorithm. Two binary exponentiation algorithms for computing an are introduced in Section 6.5. Both of them exploit the binary representation of the exponent n, but they process it in the opposite directions: left to right and right to left. Linear programming concerns optimizing a linear function of several vari- ables subject to constraints in the form of linear equations and linear inequal- ities. There are efficient algorithms capable of solving very large instances of this problem with many thousands of variables and constraints, provided the variables are not required to be integers. The latter, called integer linear programming, constitute a much more difficult class of problems. This page intentionally left blank 7 Space and Time Trade-Offs Things which matter most must never be at the mercy of things which matter less. —Johann Wolfgang von G¨oethe (1749–1832) Space and time trade-offs in algorithm design are a well-known issue for both theoreticians and practitioners of computing. Consider, as an example, the problem of computing values of a function at many points in its domain. If it is time that is at a premium, we can precompute the function’s values and store them in a table. This is exactly what human computers had to do before the advent of electronic computers, in the process burdening libraries with thick volumes of mathematical tables. Though such tables have lost much of their appeal with the widespread use of electronic computers, the underlying idea has proven to be quite useful in the development of several important algorithms for other problems. In somewhat more general terms, the idea is to preprocess the problem’s input, in whole or in part, and store the additional information obtained to accelerate solving the problem afterward. We call this approach input enhancement1 and discuss the following algorithms based on it: counting methods for sorting (Section 7.1) Boyer-Moore algorithm for string matching and its simplified version sug- gested by Horspool (Section 7.2) The other type of technique that exploits space-for-time trade-offs simply uses extra space to facilitate faster and/or more flexible access to the data. We call this approach prestructuring. This name highlights two facets of this variation of the space-for-time trade-off: some processing is done before a problem in question 1. The standard terms used synonymously for this technique are preprocessing and preconditioning. Confusingly, these terms can also be applied to methods that use the idea of preprocessing but do not use extra space (see Chapter 6). Thus, in order to avoid confusion, we use “input enhancement” as a special name for the space-for-time trade-off technique being discussed here. 253 254 Space and Time Trade-Offs is actually solved but, unlike the input-enhancement variety, it deals with access structuring. We illustrate this approach by: hashing (Section 7.3) indexing with B-trees (Section 7.4) There is one more algorithm design technique related to the space-for-time trade-off idea: dynamic programming. This strategy is based on recording solu- tions to overlapping subproblems of a given problem in a table from which a solu- tion to the problem in question is then obtained. We discuss this well-developed technique separately, in the next chapter of the book. Two final comments about the interplay between time and space in algo- rithm design need to be made. First, the two resources—time and space—do not have to compete with each other in all design situations. In fact, they can align to bring an algorithmic solution that minimizes both the running time and the space consumed. Such a situation arises, in particular, when an algorithm uses a space- efficient data structure to represent a problem’s input, which leads, in turn, to a faster algorithm. Consider, as an example, the problem of traversing graphs. Re- call that the time efficiency of the two principal traversal algorithms—depth-first search and breadth-first search—depends on the data structure used for repre- senting graphs: it is (n2) for the adjacency matrix representation and (n + m) for the adjacency list representation, where n and m are the numbers of vertices and edges, respectively. If input graphs are sparse, i.e., have few edges relative to the number of vertices (say, m ∈ O(n)), the adjacency list representation may well be more efficient from both the space and the running-time points of view. The same situation arises in the manipulation of sparse matrices and sparse polynomi- als: if the percentage of zeros in such objects is sufficiently high, we can save both space and time by ignoring zeros in the objects’ representation and processing. Second, one cannot discuss space-time trade-offs without mentioning the hugely important area of data compression. Note, however, that in data compres- sion, size reduction is the goal rather than a technique for solving another problem. We discuss just one data compression algorithm, in the next chapter. The reader interested in this topic will find a wealth of algorithms in such books as [Say05]. 7.1 Sorting by Counting As a first example of applying the input-enhancement technique, we discuss its application to the sorting problem. One rather obvious idea is to count, for each element of a list to be sorted, the total number of elements smaller than this element and record the results in a table. These numbers will indicate the positions of the elements in the sorted list: e.g., if the count is 10 for some element, it should be in the 11th position (with index 10, if we start counting with 0) in the sorted array. Thus, we will be able to sort the list by simply copying its elements to their appropriate positions in a new, sorted list. This algorithm is called comparison- counting sort (Figure 7.1). 7.1 Sorting by Counting 255 Array A[0..5] Array S[0..5] Count [] Count [] Count [] Count [] Count [] Count [] Count [] Initially After pass i = 0 After pass i = 1 After pass i = 2 After pass i = 3 After pass i = 4 Final state 62 31 84 96 19 47 0 3 3 0 0 1 1 0 1 2 4 4 0 1 2 3 5 5 0 0 0 0 0 0 0 0 0 1 1 1 2 2 19 31 47 62 84 96 FIGURE 7.1 Example of sorting by comparison counting. ALGORITHM ComparisonCountingSort(A[0..n − 1]) //Sorts an array by comparison counting //Input: An array A[0..n − 1] of orderable elements //Output: Array S[0..n − 1] of A’s elements sorted in nondecreasing order for i ← 0 to n − 1 do Count[i] ← 0 for i ← 0 to n − 2 do for j ← i + 1 to n − 1 do if A[i] 0. For example, if we search for the pattern BARBER in some text and match the last two characters before failing on letter A, we can shift the pattern by t1(A) − 2 = 4 − 2 = 2 positions: s0 ... AER ... sn−1  BARBER B A RBER 264 Space and Time Trade-Offs If t1(c) − k ≤ 0, we obviously do not want to shift the pattern by 0 or a negative number of positions. Rather, we can fall back on the brute-force thinking and simply shift the pattern by one position to the right. To summarize, the bad-symbol shift d1 is computed by the Boyer-Moore algorithm either as t1(c) − k if this quantity is positive and as 1 if it is negative or zero. This can be expressed by the following compact formula: d1 = max{t1(c) − k, 1}. (7.2) The second type of shift is guided by a successful match of the last k>0 characters of the pattern. We refer to the ending portion of the pattern as its suffix of size k and denote it suff (k). Accordingly, we call this type of shift the good-suffix shift. We now apply the reasoning that guided us in filling the bad-symbol shift table, which was based on a single alphabet character c, to the pattern’s suffixes of sizes 1,...,m− 1 to fill in the good-suffix shift table. Let us first consider the case when there is another occurrence of suff (k) in the pattern or, to be more accurate, there is another occurrence of suff (k) not preceded by the same character as in its rightmost occurrence. (It would be useless to shift the pattern to match another occurrence of suff (k) preceded by the same character because this would simply repeat a failed trial.) In this case, we can shift the pattern by the distance d2 between such a second rightmost occurrence (not preceded by the same character as in the rightmost occurrence) of suff (k) and its rightmost occurrence. For example, for the pattern ABCBAB, these distances for k = 1 and 2 will be 2 and 4, respectively: k pattern d2 1 ABCBAB 2 2 ABCBAB 4 What is to be done if there is no other occurrence of suff (k) not preceded by the same character as in its rightmost occurrence? In most cases, we can shift the pattern by its entire length m. For example, for the pattern DBCBAB and k = 3, we can shift the pattern by its entire length of 6 characters: s0 ... c BAB ... sn−1   DB C BAB DBCBAB Unfortunately, shifting the pattern by its entire length when there is no other occurrence of suff (k) not preceded by the same character as in its rightmost occurrence is not always correct. For example, for the pattern ABCBAB and k = 3, shifting by 6 could miss a matching substring that starts with the text’s AB aligned with the last two characters of the pattern: 7.2 Input Enhancement in String Matching 265 s0 ... c BABCBAB ... sn−1  ABCBAB ABCBAB Note that the shift by 6 is correct for the pattern DBCBAB but not for ABCBAB, because the latter pattern has the same substring AB as its prefix (beginning part of the pattern) and as its suffix (ending part of the pattern). To avoid such an erroneous shift based on a suffix of size k, for which there is no other occurrence in the pattern not preceded by the same character as in its rightmost occurrence, we need to find the longest prefix of size l0, also retrieve the corresponding d2 entry from the good-suffix table. Shift the pattern to the right by the 266 Space and Time Trade-Offs number of positions computed by the formula d =  d1 if k = 0, max{d1,d2} if k>0, (7.3) where d1 = max{t1(c) − k, 1}. Shifting by the maximum of the two available shifts when k>0 is quite log- ical. The two shifts are based on the observations—the first one about a text’s mismatched character, and the second one about a matched group of the pattern’s rightmost characters—that imply that shifting by less than d1 and d2 characters, re- spectively, cannot lead to aligning the pattern with a matching substring in the text. Since we are interested in shifting the pattern as far as possible without missing a possible matching substring, we take the maximum of these two numbers. EXAMPLE As a complete example, let us consider searching for the pattern BAOBAB in a text made of English letters and spaces. The bad-symbol table looks as follows: c A B C D ... O ... Z _ t1(c) 1 2 6 6 6 3 6 6 6 The good-suffix table is filled as follows: k pattern d2 1 BAOBAB 2 2 BAOBAB 5 3 BAOBAB 5 4 BAOBAB 5 5 BAOBAB 5 The actual search for this pattern in the text given in Figure 7.3 proceeds as follows. After the last B of the pattern fails to match its counterpart K in the text, the algorithm retrieves t1(K) = 6 from the bad-symbol table and shifts the pat- tern by d1 = max{t1(K) − 0, 1}=6 positions to the right. The new try successfully matches two pairs of characters. After the failure of the third comparison on the space character in the text, the algorithm retrieves t1( ) = 6 from the bad-symbol table and d2 = 5 from the good-suffix table to shift the pattern by max{d1,d2}= max{6 − 2, 5}=5. Note that on this iteration it is the good-suffix rule that leads to a farther shift of the pattern. The next try successfully matches just one pair of B’s. After the failure of the next comparison on the space character in the text, the algorithm retrieves t1( ) = 6 from the bad-symbol table and d2 = 2 from the good-suffix table to shift 7.2 Input Enhancement in String Matching 267 BESS _ KN E W_AB O UT_BAOBABS BAOB A B d1 = t1(K) − 0 = 6 B A OBAB d1 = t1( ) − 2 = 4 B A OBAB d2 = 5 d1 = t1( ) − 1 = 5 d = max{4, 5}=5 d2 = 2 d = max{5, 2}=5 BAOBAB FIGURE 7.3 Example of string matching with the Boyer-Moore algorithm. the pattern by max{d1,d2}=max{6 − 1, 2}=5. Note that on this iteration it is the bad-symbol rule that leads to a farther shift of the pattern. The next try finds a matching substring in the text after successfully matching all six characters of the pattern with their counterparts in the text. When searching for the first occurrence of the pattern, the worst-case effi- ciency of the Boyer-Moore algorithm is known to be linear. Though this algorithm runs very fast, especially on large alphabets (relative to the length of the pattern), many people prefer its simplified versions, such as Horspool’s algorithm, when dealing with natural-language–like strings. Exercises 7.2 1. Apply Horspool’s algorithm to search for the pattern BAOBAB in the text BESS KNEW ABOUT BAOBABS 2. Consider the problem of searching for genes in DNA sequences using Hor- spool’s algorithm. A DNA sequence is represented by a text on the alphabet {A, C, G, T}, and the gene or gene segment is the pattern. a. Construct the shift table for the following gene segment of your chromo- some 10: TCCTATTCTT b. Apply Horspool’s algorithm to locate the above pattern in the following DNA sequence: TTATAGATCTCGTATTCTTTTATAGATCTCCTATTCTT 268 Space and Time Trade-Offs 3. How many character comparisons will be made by Horspool’s algorithm in searching for each of the following patterns in the binary text of 1000 zeros? a. 00001 b. 10000 c. 01010 4. For searching in a text of length n for a pattern of length m(n≥ m) with Horspool’s algorithm, give an example of a. worst-case input. b. best-case input. 5. Is it possible for Horspool’s algorithm to make more character comparisons than the brute-force algorithm would make in searching for the same pattern in the same text? 6. If Horspool’s algorithm discovers a matching substring, how large a shift should it make to search for a next possible match? 7. How many character comparisons will the Boyer-Moore algorithm make in searching for each of the following patterns in the binary text of 1000 zeros? a. 00001 b. 10000 c. 01010 8. a. Would the Boyer-Moore algorithm work correctly with just the bad-symbol table to guide pattern shifts? b. Would the Boyer-Moore algorithm work correctly with just the good-suffix table to guide pattern shifts? 9. a. If the last characters of a pattern and its counterpart in the text do match, does Horspool’s algorithm have to check other characters right to left, or can it check them left to right too? b. Answer the same question for the Boyer-Moore algorithm. 10. Implement Horspool’s algorithm, the Boyer-Moore algorithm, and the brute- force algorithm of Section 3.2 in the language of your choice and run an experiment to compare their efficiencies for matching a. random binary patterns in random binary texts. b. random natural-language patterns in natural-language texts. 11. You are given two strings S and T , each n characters long. You have to establish whether one of them is a right cyclic shift of the other. For example, PLEA is a right cyclic shift of LEAP, and vice versa. (Formally, T is a right cyclic shift of S if T can be obtained by concatenating the (n − i)-character suffix of S and the i-character prefix of S for some 1 ≤ i ≤ n.) a. Design a space-efficient algorithm for the task. Indicate the space and time efficiencies of your algorithm. b. Design a time-efficient algorithm for the task. Indicate the time and space efficiencies of your algorithm. 7.3 Hashing 269 7.3 Hashing In this section, we consider a very efficient way to implement dictionaries. Recall that a dictionary is an abstract data type, namely, a set with the operations of searching (lookup), insertion, and deletion defined on its elements. The elements of this set can be of an arbitrary nature: numbers, characters of some alphabet, character strings, and so on. In practice, the most important case is that of records (student records in a school, citizen records in a governmental office, book records in a library). Typically, records comprise several fields, each responsible for keeping a particular type of information about an entity the record represents. For example, a student record may contain fields for the student’s ID, name, date of birth, sex, home address, major, and so on. Among record fields there is usually at least one called a key that is used for identifying entities represented by the records (e.g., the student’s ID). In the discussion below, we assume that we have to implement a dictionary of n records with keys K1,K2,...,Kn. Hashing is based on the idea of distributing keys among a one-dimensional array H[0..m − 1] called a hash table. The distribution is done by computing, for each of the keys, the value of some predefined function h called the hash function. This function assigns an integer between 0 and m − 1, called the hash address,to a key. For example, if keys are nonnegative integers, a hash function can be of the form h(K) = K mod m; obviously, the remainder of division by m is always between 0 and m − 1. If keys are letters of some alphabet, we can first assign a letter its position in the alphabet, denoted here ord(K), and then apply the same kind of a function used for integers. Finally, if K is a character string c0c1 ...cs−1,we can use, as a very unsophisticated option, ( s−1 i=0 ord(ci)) mod m. A better option is to compute h(K) as follows:2 h ← 0; for i ← 0 to s − 1 do h ← (h ∗ C + ord(ci)) mod m, where C is a constant larger than every ord(ci). In general, a hash function needs to satisfy somewhat conflicting require- ments: A hash table’s size should not be excessively large compared to the number of keys, but it should be sufficient to not jeopardize the implementation’s time efficiency (see below). A hash function needs to distribute keys among the cells of the hash table as evenly as possible. (This requirement makes it desirable, for most applications, to have a hash function dependent on all bits of a key, not just some of them.) A hash function has to be easy to compute. 2. This can be obtained by treating ord(ci) as digits of a number in the C-based system, computing its decimal value by Horner’s rule, and finding the remainder of the number after dividing it by m. 270 Space and Time Trade-Offs Ki Kj m –10 b . . .. . . FIGURE 7.4 Collision of two keys in hashing: h(Ki) = h(Kj). Obviously, if we choose a hash table’s size m to be smaller than the number of keys n, we will get collisions—a phenomenon of two (or more) keys being hashed into the same cell of the hash table (Figure 7.4). But collisions should be expected even if m is considerably larger than n (see Problem 5 in this section’s exercises). In fact, in the worst case, all the keys could be hashed to the same cell of the hash table. Fortunately, with an appropriately chosen hash table size and a good hash function, this situation happens very rarely. Still, every hashing scheme must have a collision resolution mechanism. This mechanism is different in the two principal versions of hashing: open hashing (also called separate chaining) and closed hashing (also called open addressing). Open Hashing (Separate Chaining) In open hashing, keys are stored in linked lists attached to cells of a hash table. Each list contains all the keys hashed to its cell. Consider, as an example, the following list of words: A, FOOL, AND, HIS, MONEY, ARE, SOON, PARTED. As a hash function, we will use the simple function for strings mentioned above, i.e., we will add the positions of a word’s letters in the alphabet and compute the sum’s remainder after division by 13. We start with the empty table. The first key is the word A; its hash value is h(A) = 1 mod 13 = 1. The second key—the word FOOL—is installed in the ninth cell since (6 + 15 + 15 + 12) mod 13 = 9, and so on. The final result of this process is given in Figure 7.5; note a collision of the keys ARE and SOON because h(ARE)= (1 + 18 + 5) mod 13 = 11 and h(SOON)=(19 + 15 + 15 + 14) mod 13 = 11. How do we search in a dictionary implemented as such a table of linked lists? We do this by simply applying to a search key the same procedure that was used for creating the table. To illustrate, if we want to search for the key KID in the hash table of Figure 7.5, we first compute the value of the same hash function for the key: h(KID) = 11. Since the list attached to cell 11 is not empty, its linked list may contain the search key. But because of possible collisions, we cannot tell whether this is the case until we traverse this linked list. After comparing the string KID first with the string ARE and then with the string SOON, we end up with an unsuccessful search. In general, the efficiency of searching depends on the lengths of the linked lists, which, in turn, depend on the dictionary and table sizes, as well as the quality 7.3 Hashing 271 keys hash addresses 1 9 6 10 7 11 11 12 11 12109876543210 AFOOL FOOLMONEYANDA HIS ARE SOON PARTED AND HIS MONEY ARESOON PARTED ↓↓↓↓↓↓ ↓ ↓ FIGURE 7.5 Example of a hash table construction with separate chaining. of the hash function. If the hash function distributes n keys among m cells of the hash table about evenly, each list will be about n/m keys long. The ratio α = n/m, called the load factor of the hash table, plays a crucial role in the efficiency of hashing. In particular, the average number of pointers (chain links) inspected in successful searches, S, and unsuccessful searches, U, turns out to be S ≈ 1 + α 2 and U = α, (7.4) respectively, under the standard assumptions of searching for a randomly selected element and a hash function distributing keys uniformly among the table’s cells. These results are quite natural. Indeed, they are almost identical to searching sequentially in a linked list; what we have gained by hashing is a reduction in average list size by a factor of m, the size of the hash table. Normally, we want the load factor to be not far from 1. Having it too small would imply a lot of empty lists and hence inefficient use of space; having it too large would mean longer linked lists and hence longer search times. But if we do have the load factor around 1, we have an amazingly efficient scheme that makes it possible to search for a given key for, on average, the price of one or two comparisons! True, in addition to comparisons, we need to spend time on computing the value of the hash function for a search key, but it is a constant-time operation, independent from n and m. Note that we are getting this remarkable efficiency not only as a result of the method’s ingenuity but also at the expense of extra space. The two other dictionary operations—insertion and deletion—are almost identical to searching. Insertions are normally done at the end of a list (but see Problem 6 in this section’s exercises for a possible modification of this rule). Deletion is performed by searching for a key to be deleted and then removing it from its list. Hence, the efficiency of these operations is identical to that of searching, and they are all (1) in the average case if the number of keys n is about equal to the hash table’s size m. 272 Space and Time Trade-Offs Closed Hashing (Open Addressing) In closed hashing, all keys are stored in the hash table itself without the use of linked lists. (Of course, this implies that the table size m must be at least as large as the number of keys n.) Different strategies can be employed for collision resolution. The simplest one—called linear probing—checks the cell following the one where the collision occurs. If that cell is empty, the new key is installed there; if the next cell is already occupied, the availability of that cell’s immediate successor is checked, and so on. Note that if the end of the hash table is reached, the search is wrapped to the beginning of the table; i.e., it is treated as a circular array. This method is illustrated in Figure 7.6 with the same word list and hash function used above to illustrate separate chaining. To search for a given key K, we start by computing h(K) where h is the hash function used in the table construction. If the cell h(K) is empty, the search is unsuccessful. If the cell is not empty, we must compare K with the cell’s occupant: if they are equal, we have found a matching key; if they are not, we compare K with a key in the next cell and continue in this manner until we encounter either a matching key (a successful search) or an empty cell (unsuccessful search). For example, if we search for the word LIT in the table of Figure 7.6, we will get h(LIT) = (12 + 9 + 20) mod 13 = 2 and, since cell 2 is empty, we can stop immediately. However, if we search for KID with h(KID) = (11 + 9 + 4) mod 13 = 11, we will have to compare KID with ARE, SOON, PARTED, and A before we can declare the search unsuccessful. Although the search and insertion operations are straightforward for this version of hashing, deletion is not. For example, if we simply delete the key ARE from the last state of the hash table in Figure 7.6, we will be unable to find the key SOON afterward. Indeed, after computing h(SOON) = 11, the algorithm would find this location empty and report the unsuccessful search result. A simple solution FOOL FOOL FOOL FOOL FOOL FOOL FOOL MONEY MONEY MONEY MONEY AND AND AND AND AND AND A A A A A A A A HIS HIS HIS HIS HIS ARE ARE ARE SOON SOONPARTED keys hash addresses 1 9 6 10 7 11 11 12 11 12109876543210 AFOOL AND HIS MONEY ARESOON PARTED FIGURE 7.6 Example of a hash table construction with linear probing. 7.3 Hashing 273 is to use “lazy deletion,” i.e., to mark previously occupied locations by a special symbol to distinguish them from locations that have not been occupied. The mathematical analysis of linear probing is a much more difficult problem than that of separate chaining.3 The simplified versions of these results state that the average number of times the algorithm must access the hash table with the load factor α in successful and unsuccessful searches is, respectively, S ≈ 1 2 (1 + 1 1 − α ) and U ≈ 1 2 (1 + 1 (1 − α)2 ) (7.5) (and the accuracy of these approximations increases with larger sizes of the hash table). These numbers are surprisingly small even for densely populated tables, i.e., for large percentage values of α: α 1 2 (1 + 1 1−α ) 1 2 (1 + 1 (1−α)2 ) 50% 1.5 2.5 75% 2.5 8.5 90% 5.5 50.5 Still, as the hash table gets closer to being full, the performance of linear prob- ing deteriorates because of a phenomenon called clustering. A cluster in linear probing is a sequence of contiguously occupied cells (with a possible wrapping). For example, the final state of the hash table of Figure 7.6 has two clusters. Clus- ters are bad news in hashing because they make the dictionary operations less efficient. As clusters become larger, the probability that a new element will be attached to a cluster increases; in addition, large clusters increase the probabil- ity that two clusters will coalesce after a new key’s insertion, causing even more clustering. Several other collision resolution strategies have been suggested to alleviate this problem. One of the most important is double hashing. Under this scheme, we use another hash function, s(K), to determine a fixed increment for the probing sequence to be used after a collision at location l = h(K): (l + s(K)) mod m, (l + 2s(K)) mod m, ... . (7.6) To guarantee that every location in the table is probed by sequence (7.6), the incre- ment s(k) and the table size m must be relatively prime, i.e., their only common divisor must be 1. (This condition is satisfied automatically if m itself is prime.) Some functions recommended in the literature are s(k) = m − 2 − k mod (m − 2) and s(k) = 8 − (k mod 8) for small tables and s(k) = k mod 97 + 1 for larger ones. 3. This problem was solved in 1962 by a young graduate student in mathematics named Donald E. Knuth. Knuth went on to become one of the most important computer scientists of our time. His multivolume treatise The Art of Computer Programming [KnuI, KnuII, KnuIII, KnuIV] remains the most comprehensive and influential book on algorithmics ever published. 274 Space and Time Trade-Offs Mathematical analysis of double hashing has proved to be quite difficult. Some partial results and considerable practical experience with the method suggest that with good hashing functions—both primary and secondary—double hashing is su- perior to linear probing. But its performance also deteriorates when the table gets close to being full. A natural solution in such a situation is rehashing: the current table is scanned, and all its keys are relocated into a larger table. It is worthwhile to compare the main properties of hashing with balanced search trees—its principal competitor for implementing dictionaries. Asymptotic time efficiency With hashing, searching, insertion, and deletion can be implemented to take (1) time on the average but (n) time in the very unlikely worst case. For balanced search trees, the average time efficiencies are (log n) for both the average and worst cases. Ordering preservation Unlike balanced search trees, hashing does not assume existence of key ordering and usually does not preserve it. This makes hashing less suitable for applications that need to iterate over the keys in or- der or require range queries such as counting the number of keys between some lower and upper bounds. Since its discovery in the 1950s by IBM researchers, hashing has found many important applications. In particular, it has become a standard technique for stor- ing a symbol table—a table of a computer program’s symbols generated during compilation. Hashing is quite handy for such AI applications as checking whether positions generated by a chess-playing computer program have already been con- sidered. With some modifications, it has also proved to be useful for storing very large dictionaries on disks; this variation of hashing is called extendible hashing. Since disk access is expensive compared with probes performed in the main mem- ory, it is preferable to make many more probes than disk accesses. Accordingly, a location computed by a hash function in extendible hashing indicates a disk ad- dress of a bucket that can hold up to b keys. When a key’s bucket is identified, all its keys are read into main memory and then searched for the key in question. In the next section, we discuss B-trees, a principal alternative for storing large dictionaries. Exercises 7.3 1. For the input 30, 20, 56, 75, 31, 19 and hash function h(K) = K mod 11 a. construct the open hash table. b. find the largest number of key comparisons in a successful search in this table. c. find the average number of key comparisons in a successful search in this table. 2. For the input 30, 20, 56, 75, 31, 19 and hash function h(K) = K mod 11 a. construct the closed hash table. 7.3 Hashing 275 b. find the largest number of key comparisons in a successful search in this table. c. find the average number of key comparisons in a successful search in this table. 3. Why is it not a good idea for a hash function to depend on just one letter (say, the first one) of a natural-language word? 4. Find the probability of all n keys being hashed to the same cell of a hash table of size m if the hash function distributes keys evenly among all the cells of the table. 5. Birthday paradox The birthday paradox asks how many people should be in a room so that the chances are better than even that two of them will have the same birthday (month and day). Find the quite unexpected answer to this problem. What implication for hashing does this result have? 6. Answer the following questions for the separate-chaining version of hashing. a. Where would you insert keys if you knew that all the keys in the dictionary are distinct? Which dictionary operations, if any, would benefit from this modification? b. We could keep keys of the same linked list sorted. Which of the dictio- nary operations would benefit from this modification? How could we take advantage of this if all the keys stored in the entire table need to be sorted? 7. Explain how to use hashing to check whether all elements of a list are distinct. What is the time efficiency of this application? Compare its efficiency with that of the brute-force algorithm (Section 2.3) and of the presorting-based algorithm (Section 6.1). 8. Fill in the following table with the average-case (as the first entry) and worst- case (as the second entry) efficiency classes for the five implementations of the ADT dictionary: unordered ordered binary balanced array array search tree search tree hashing search insertion deletion 9. We have discussed hashing in the context of techniques based on space–time trade-offs. But it also takes advantage of another general strategy. Which one? 10. Write a computer program that uses hashing for the following problem. Given a natural-language text, generate a list of distinct words with the number of occurrences of each word in the text. Insert appropriate counters in the pro- gram to compare the empirical efficiency of hashing with the corresponding theoretical results. 276 Space and Time Trade-Offs T0 T1 p0 Ti –1 Tn –2 pn –2 Kn –1 pn –1pi –1p1K1 piKi Tn –1Ti . . . . . . FIGURE 7.7 Parental node of a B-tree. 7.4 B-Trees The idea of using extra space to facilitate faster access to a given data set is partic- ularly important if the data set in question contains a very large number of records that need to be stored on a disk. A principal device in organizing such data sets is an index, which provides some information about the location of records with indicated key values. For data sets of structured records (as opposed to “unstruc- tured” data such as text, images, sound, and video), the most important index organization is the B-tree, introduced by R. Bayer and E. McGreight [Bay72]. It extends the idea of the 2-3 tree (see Section 6.3) by permitting more than a single key in the same node of a search tree. In the B-tree version we consider here, all data records (or record keys) are stored at the leaves, in increasing order of the keys. The parental nodes are used for indexing. Specifically, each parental node contains n − 1 ordered keys K1 < ...0, we have the following inequality: n ≥ 1 + h−1 i=1 2m/2i−1(m/2−1) + 2m/2h−1. After a series of standard simplifications (see Problem 2 in this section’s exercises), this inequality reduces to 278 Space and Time Trade-Offs n ≥ 4m/2h−1 − 1, which, in turn, yields the following upper bound on the height h of the B-tree of order m with n nodes: h ≤logm/2 n + 1 4 +1. (7.7) Inequality (7.7) immediately implies that searching in a B-tree is a O(log n) operation. But it is important to ascertain here not just the efficiency class but the actual number of disk accesses implied by this formula. The following table contains the values of the right-hand-side estimates for a file of 100 million records and a few typical values of the tree’s order m: order m 50 100 250 h’s upper bound 6 5 4 Keep in mind that the table’s entries are upper estimates for the number of disk accesses. In actual applications, this number rarely exceeds 3, with the B-tree’s root and sometimes first-level nodes stored in the fast memory to minimize the number of disk accesses. The operations of insertion and deletion are less straightforward than search- ing, but both can also be done in O(log n) time. Here we outline an insertion algorithm only; a deletion algorithm can be found in the references (e.g., [Aho83], [Cor09]). The most straightforward algorithm for inserting a new record into a B- tree is quite similar to the algorithm for insertion into a 2-3 tree outlined in Section 6.3. First, we apply the search procedure to the new record’s key K to find the appropriate leaf for the new record. If there is room for the record in that leaf, we place it there (in an appropriate position so that the keys remain sorted) and we are done. If there is no room for the record, the leaf is split in half by sending the second half of the records to a new node. After that, the smallest key K in the new node and the pointer to it are inserted into the old leaf’s parent (immediately after the key and pointer to the old leaf). This recursive procedure may percolate up to the tree’s root. If the root is already full too, a new root is created with the two halves of the old root’s keys split between two children of the new root. As an example, Figure 7.9 shows the result of inserting 65 into the B-tree in Figure 7.8 under the restriction that the leaves cannot contain more than three items. You should be aware that there are other algorithms for implementing inser- tions into a B-tree. For example, to avoid the possibility of recursive node splits, we can split full nodes encountered in searching for an appropriate leaf for the new record. Another possibility is to avoid some node splits by moving a key to the node’s sibling. For example, inserting 65 into the B-tree in Figure 7.8 can be done by moving 60, the smallest key of the full leaf, to its sibling with keys 51 and 55, and replacing the key value of their parent by 65, the new smallest value in 7.4 B-Trees 279 20 51 25 34 40 60 6811 15 40, 43, 4615, 16, 19 51, 55 60, 65 68, 8034, 3825, 2820, 2411, 144, 7, 10 FIGURE 7.9 B-tree obtained after inserting 65 into the B-tree in Figure 7.8. the second child. This modification tends to save some space at the expense of a slightly more complicated algorithm. A B-tree does not have to be always associated with the indexing of a large file, and it can be considered as one of several search tree varieties. As with other types of search trees—such as binary search trees, AVL trees, and 2-3 trees—a B- tree can be constructed by successive insertions of data records into the initially empty tree. (The empty tree is considered to be a B-tree, too.) When all keys reside in the leaves and the upper levels are organized as a B-tree comprising an index, the entire structure is usually called, in fact, a B+++-tree. Exercises 7.4 1. Give examples of using an index in real-life applications that do not involve computers. 2. a. Prove the equality 1 + h−1 i=1 2m/2i−1(m/2−1) + 2m/2h−1 = 4m/2h−1 − 1, which was used in the derivation of upper bound (7.7) for the height of a B-tree. b. Complete the derivation of inequality (7.7). 3. Find the minimum order of the B-tree that guarantees that the number of disk accesses in searching in a file of 100 million records does not exceed 3. Assume that the root’s page is stored in main memory. 4. Draw the B-tree obtained after inserting 30 and then 31 in the B-tree in Figure 7.8. Assume that a leaf cannot contain more than three items. 5. Outline an algorithm for finding the largest key in a B-tree. 6. a. A top-down 2-3-4 tree is a B-tree of order 4 with the following modifica- tion of the insert operation: Whenever a search for a leaf for a new key 280 Space and Time Trade-Offs encounters a full node (i.e., a node with three keys), the node is split into two nodes by sending its middle key to the node’s parent, or, if the full node happens to be the root, the new root for the middle key is created. Construct a top-down 2-3-4 tree by inserting the following list of keys in the initially empty tree: 10, 6, 15, 31, 20, 27, 50, 44, 18. b. What is the principal advantage of this insertion procedure compared with the one used for 2-3 trees in Section 6.3? What is its disadvantage? 7. a. Write a program implementing a key insertion algorithm in a B-tree. b. Write a program for visualization of a key insertion algorithm in a B-tree. SUMMARY Space and time trade-offs in algorithm design are a well-known issue for both theoreticians and practitioners of computing. As an algorithm design technique, trading space for time is much more prevalent than trading time for space. Input enhancement is one of the two principal varieties of trading space for time in algorithm design. Its idea is to preprocess the problem’s input, in whole or in part, and store the additional information obtained in order to accelerate solving the problem afterward. Sorting by distribution counting and several important algorithms for string matching are examples of algorithms based on this technique. Distribution counting is a special method for sorting lists of elements from a small set of possible values. Horspool’s algorithm for string matching can be considered a simplified version of the Boyer-Moore algorithm.Both algorithms are based on the ideas of input enhancement and right-to-left comparisons of a pattern’s characters. Both algorithms use the same bad-symbol shift table; the Boyer-Moore also uses a second table, called the good-suffix shift table. Prestructuring—the second type of technique that exploits space-for-time trade-offs—uses extra space to facilitate a faster and/or more flexible access to the data. Hashing and B+-trees are important examples of prestructuring. Hashing is a very efficient approach to implementing dictionaries. It is based on the idea of mapping keys into a one-dimensional table. The size limitations of such a table make it necessary to employ a collision resolution mechanism. The two principal varieties of hashing are open hashing or separate chaining (with keys stored in linked lists outside of the hash table) and closed hashing Summary 281 or open addressing (with keys stored inside the table). Both enable searching, insertion, and deletion in (1) time, on average. The B-tree is a balanced search tree that generalizes the idea of the 2-3 tree by allowing multiple keys at the same node. Its principal application, called the B+-tree, is for keeping index-like information about data stored on a disk. By choosing the order of the tree appropriately, one can implement the operations of searching, insertion, and deletion with just a few disk accesses even for extremely large files. This page intentionally left blank 8 Dynamic Programming An idea, like a ghost...must be spoken to a little before it will explain itself. —Charles Dickens (1812–1870) Dynamic programming is an algorithm design technique with a rather inter- esting history. It was invented by a prominent U.S. mathematician, Richard Bellman, in the 1950s as a general method for optimizing multistage decision pro- cesses. Thus, the word “programming” in the name of this technique stands for “planning” and does not refer to computer programming. After proving its worth as an important tool of applied mathematics, dynamic programming has even- tually come to be considered, at least in computer science circles, as a general algorithm design technique that does not have to be limited to special types of optimization problems. It is from this point of view that we will consider this tech- nique here. Dynamic programming is a technique for solving problems with overlapping subproblems. Typically, these subproblems arise from a recurrence relating a given problem’s solution to solutions of its smaller subproblems. Rather than solving overlapping subproblems again and again, dynamic programming suggests solving each of the smaller subproblems only once and recording the results in a table from which a solution to the original problem can then be obtained. This technique can be illustrated by revisiting the Fibonacci numbers dis- cussed in Section 2.5. (If you have not read that section, you will be able to follow the discussion anyway. But it is a beautiful topic, so if you feel a temptation to read it, do succumb to it.) The Fibonacci numbers are the elements of the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,..., which can be defined by the simple recurrence F (n) = F(n− 1) + F(n− 2) for n>1 (8.1) 283 284 Dynamic Programming and two initial conditions F(0) = 0,F(1) = 1. (8.2) If we try to use recurrence (8.1) directly to compute the nth Fibonacci number F (n), we would have to recompute the same values of this function many times (see Figure 2.6 for an example). Note that the problem of computing F (n) is expressed in terms of its smaller and overlapping subproblems of computing F(n− 1) and F(n− 2). So we can simply fill elements of a one-dimensional array with the n + 1 consecutive values of F (n) by starting, in view of initial conditions (8.2), with 0 and 1 and using equation (8.1) as the rule for producing all the other elements. Obviously, the last element of this array will contain F (n). Single-loop pseudocode of this very simple algorithm can be found in Section 2.5. Note that we can, in fact, avoid using an extra array to accomplish this task by recording the values of just the last two elements of the Fibonacci sequence (Problem 8 in Exercises 2.5). This phenomenon is not unusual, and we shall en- counter it in a few more examples in this chapter. Thus, although a straightforward application of dynamic programming can be interpreted as a special variety of space-for-time trade-off, a dynamic programming algorithm can sometimes be re- fined to avoid using extra space. Certain algorithms compute the nth Fibonacci number without computing all the preceding elements of this sequence (see Section 2.5). It is typical of an algorithm based on the classic bottom-up dynamic programming approach, however, to solve all smaller subproblems of a given problem. One variation of the dynamic programming approach seeks to avoid solving unnecessary subproblems. This technique, illustrated in Section 8.2, exploits so-called memory functions and can be considered a top-down variation of dynamic programming. Whether one uses the classical bottom-up version of dynamic programming or its top-down variation, the crucial step in designing such an algorithm remains the same: deriving a recurrence relating a solution to the problem to solutions to its smaller subproblems. The immediate availability of equation (8.1) for computing the nth Fibonacci number is one of the few exceptions to this rule. Since a majority of dynamic programming applications deal with optimiza- tion problems, we also need to mention a general principle that underlines such applications. Richard Bellman called it the principle of optimality. In terms some- what different from its original formulation, it says that an optimal solution to any instance of an optimization problem is composed of optimal solutions to its subin- stances. The principle of optimality holds much more often than not. (To give a rather rare example, it fails for finding the longest simple path in a graph.) Al- though its applicability to a particular problem needs to be checked, of course, such a check is usually not a principal difficulty in developing a dynamic program- ming algorithm. In the sections and exercises of this chapter are a few standard examples of dynamic programming algorithms. (The algorithms in Section 8.4 were, in fact, 8.1 Three Basic Examples 285 invented independently of the discovery of dynamic programming and only later came to be viewed as examples of this technique’s applications.) Numerous other applications range from the optimal way of breaking text into lines (e.g., [Baa00]) to image resizing [Avi07] to a variety of applications to sophisticated engineering problems (e.g., [Ber01]). 8.1 Three Basic Examples The goal of this section is to introduce dynamic programming via three typical examples. EXAMPLE 1 Coin-row problem There is a row of n coins whose values are some positive integers c1,c2,...,cn, not necessarily distinct. The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent in the initial row can be picked up. Let F (n) be the maximum amount that can be picked up from the row of n coins. To derive a recurrence for F (n), we partition all the allowed coin selections into two groups: those that include the last coin and those without it. The largest amount we can get from the first group is equal to cn + F(n− 2)—the value of the nth coin plus the maximum amount we can pick up from the first n − 2 coins. The maximum amount we can get from the second group is equal to F(n− 1) by the definition of F (n). Thus, we have the following recurrence subject to the obvious initial conditions: F (n) = max{cn + F(n− 2), F (n − 1)} for n>1, F(0) = 0,F(1) = c1. (8.3) We can compute F (n) by filling the one-row table left to right in the manner similar to the way it was done for the nth Fibonacci number by Algorithm Fib(n) in Section 2.5. ALGORITHM CoinRow(C[1..n]) //Applies formula (8.3) bottom up to find the maximum amount of money //that can be picked up from a coin row without picking two adjacent coins //Input: Array C[1..n] of positive integers indicating the coin values //Output: The maximum amount of money that can be picked up F[0] ← 0; F[1] ← C[1] for i ← 2 to n do F[i] ← max(C[i] + F[i − 2],F[i − 1]) return F[n] The application of the algorithm to the coin row of denominations 5, 1, 2, 10, 6, 2 is shown in Figure 8.1. It yields the maximum amount of 17. It is worth pointing 286 Dynamic Programming 0 0 index C FF[0] = 0, F[1] = c1 = 5 1 5 5 2 1 3 2 4 10 5 6 6 2 F[2] = max{1 + 0, 5} = 5 F[3] = max{2 + 5, 5} = 7 F[4] = max{10 + 5, 7} = 15 F[5] = max{6 + 7, 15} = 15 F[6] = max{2 + 15, 15} = 17 0 0 index C F 1 5 5 2 1 5 3 2 7 4 10 15 5 6 15 6 2 17 0 0 index C F 1 5 5 2 1 5 3 2 7 4 10 15 5 6 15 6 2 0 0 index C F 1 5 5 2 1 5 3 2 7 4 10 15 5 6 6 2 0 0 index C F 1 5 5 2 1 5 3 2 7 4 10 5 6 6 2 0 0 index C F 1 5 5 2 1 5 3 2 4 10 5 6 6 2 FIGURE 8.1 Solving the coin-row problem by dynamic programming for the coin row 5, 1, 2, 10, 6, 2. out that, in fact, we also solved the problem for the first i coins in the row given for every 1 ≤ i ≤ 6. For example, for i = 3, the maximum amount is F(3) = 7. To find the coins with the maximum total value found, we need to back- trace the computations to see which of the two possibilities—cn + F(n− 2) or F(n− 1)—produced the maxima in formula (8.3). In the last application of the formula, it was the sum c6 + F(4), which means that the coin c6 = 2 is a part of an optimal solution. Moving to computing F(4), the maximum was produced by the sum c4 + F(2), which means that the coin c4 = 10 is a part of an optimal solution as well. Finally, the maximum in computing F(2) was produced by F(1), implying that the coin c2 is not the part of an optimal solution and the coin c1 = 5 is. Thus, the optimal solution is {c1,c4,c6}. To avoid repeating the same computations during the backtracing, the information about which of the two terms in (8.3) was larger can be recorded in an extra array when the values of F are computed. Using the CoinRow to find F (n), the largest amount of money that can be picked up, as well as the coins composing an optimal set, clearly takes (n) time and (n) space. This is by far superior to the alternatives: the straightforward top- 8.1 Three Basic Examples 287 down application of recurrence (8.3) and solving the problem by exhaustive search (Problem 3 in this section’s exercises). EXAMPLE 2 Change-making problem Consider the general instance of the following well-known problem. Give change for amount n using the minimum number of coins of denominations d1 0, F(0) = 0. (8.4) We can compute F (n) by filling a one-row table left to right in the manner similar to the way it was done above for the coin-row problem, but computing a table entry here requires finding the minimum of up to m numbers. ALGORITHM ChangeMaking(D[1..m],n) //Applies dynamic programming to find the minimum number of coins //of denominations d1 F(i,j− 1), an optimal path to cell (i, j) must come down from the adjacent cell above it; if F(i− 1,j) 0, to compute the entry in the ith row and the jth column, F(i,j), we compute the maximum of the entry in the previous row and the same column and the sum of vi and the entry in the previous row and wi columns to the left. The table can be filled either row by row or column by column. 0 00 0 0 0 0 0 0 goal j –wi wi, vi Wj i i –1 n F(i –1, j –wi) F(i –1, j) F(i, j) FIGURE 8.4 Table for solving the knapsack problem by dynamic programming. 294 Dynamic Programming capacity j i 012345 0 000000 w1 = 2,v1 = 12 1 0 0 12 12 12 12 w2 = 1,v2 = 10 2 01012222222 w3 = 3,v3 = 20 3 01012223032 w4 = 2,v4 = 15 4 01015253037 FIGURE 8.5 Example of solving an instance of the knapsack problem by the dynamic programming algorithm. EXAMPLE 1 Let us consider the instance given by the following data: item weight value 1 2 $12 2 1 $10 capacity W = 5. 3 3 $20 4 2 $15 The dynamic programming table, filled by applying formulas (8.6) and (8.7), is shown in Figure 8.5. Thus, the maximal value is F(4, 5) = $37. We can find the composition of an optimal subset by backtracing the computations of this entry in the table. Since F(4, 5)>F(3, 5), item 4 has to be included in an optimal solution along with an optimal subset for filling 5 − 2 = 3 remaining units of the knapsack capacity. The value of the latter is F(3, 3). Since F(3, 3) = F(2, 3), item 3 need not be in an optimal subset. Since F(2, 3)>F(1, 3), item 2 is a part of an optimal selection, which leaves element F(1, 3 − 1) to specify its remaining composition. Similarly, since F(1, 2)>F(0, 2), item 1 is the final part of the optimal solution {item 1, item 2, item 4}. The time efficiency and space efficiency of this algorithm are both in (nW). The time needed to find the composition of an optimal solution is in O(n). You are asked to prove these assertions in the exercises. Memory Functions As we discussed at the beginning of this chapter and illustrated in subsequent sections, dynamic programming deals with problems whose solutions satisfy a recurrence relation with overlapping subproblems. The direct top-down approach to finding a solution to such a recurrence leads to an algorithm that solves common subproblems more than once and hence is very inefficient (typically, exponential 8.2 The Knapsack Problem and Memory Functions 295 or worse). The classic dynamic programming approach, on the other hand, works bottom up: it fills a table with solutions to all smaller subproblems, but each of them is solved only once. An unsatisfying aspect of this approach is that solutions to some of these smaller subproblems are often not necessary for getting a solution to the problem given. Since this drawback is not present in the top-down approach, it is natural to try to combine the strengths of the top-down and bottom-up approaches. The goal is to get a method that solves only subproblems that are necessary and does so only once. Such a method exists; it is based on using memory functions. This method solves a given problem in the top-down manner but, in addition, maintains a table of the kind that would have been used by a bottom-up dynamic programming algorithm. Initially, all the table’s entries are initialized with a spe- cial “null” symbol to indicate that they have not yet been calculated. Thereafter, whenever a new value needs to be calculated, the method checks the correspond- ing entry in the table first: if this entry is not “null,” it is simply retrieved from the table; otherwise, it is computed by the recursive call whose result is then recorded in the table. The following algorithm implements this idea for the knapsack problem. After initializing the table, the recursive function needs to be called with i = n (the number of items) and j = W (the knapsack capacity). ALGORITHM MFKnapsack(i, j) //Implements the memory function method for the knapsack problem //Input: A nonnegative integer i indicating the number of the first // items being considered and a nonnegative integer j indicating // the knapsack capacity //Output: The value of an optimal feasible subset of the first i items //Note: Uses as global variables input arrays Weights[1..n], V alues[1..n], //and table F[0..n, 0..W] whose entries are initialized with −1’s except for //row 0 and column 0 initialized with 0’s if F[i, j] < 0 if j0,c(0) = 1, which grows to infinity as fast as 4n/n1.5 (see Problem 7 in this section’s exercises). So let a1,...,an be distinct keys ordered from the smallest to the largest and let p1,...,pn be the probabilities of searching for them. Let C(i, j)be the smallest average number of comparisons made in a successful search in a binary search tree T j i made up of keys ai,...,aj, where i, j are some integer indices, 1 ≤ i ≤ j ≤ n. Following the classic dynamic programming approach, we will find values of C(i, j) for all smaller instances of the problem, although we are interested just in C(1,n). To derive a recurrence underlying a dynamic programming algorithm, we will consider all possible ways to choose a root ak among the keys ai,...,aj. For such a binary search tree (Figure 8.8), the root contains key ak, the left subtree T k−1 i contains keys ai,...,ak−1 optimally arranged, and the right subtree T j k+1 8.3 Optimal Binary Search Trees 299 ak Optimal BST for ai,...,ak–1 Optimal BST for ak +1,...,aj FIGURE 8.8 Binary search tree (BST) with root ak and two optimal binary search subtrees T k−1 i and T j k+1. contains keys ak+1,...,aj also optimally arranged. (Note how we are taking advantage of the principle of optimality here.) If we count tree levels starting with 1 to make the comparison numbers equal the keys’ levels, the following recurrence relation is obtained: C(i, j) = mini≤k≤j {pk . 1 + k−1 s=i ps . (level of as in T k−1 i + 1) + j s=k+1 ps . (level of as in T j k+1 + 1)} = mini≤k≤j { k−1 s=i ps . level of as in T k−1 i + j s=k+1 ps . level of as in T j k+1 + j s=i ps} = mini≤k≤j {C(i, k − 1) + C(k + 1,j)}+ j s=i ps. Thus, we have the recurrence C(i, j) = mini≤k≤j {C(i, k − 1) + C(k + 1,j)}+ j s=i ps for 1 ≤ i ≤ j ≤ n. (8.8) We assume in formula (8.8) that C(i, i − 1) = 0 for 1 ≤ i ≤ n + 1, which can be interpreted as the number of comparisons in the empty tree. Note that this formula implies that C(i, i) = pi for 1 ≤ i ≤ n, as it should be for a one-node binary search tree containing ai. 300 Dynamic Programming p1 pn p2 i jn n +1 01 goal0 0 0 1 C [i,j ] FIGURE 8.9 Table of the dynamic programming algorithm for constructing an optimal binary search tree. The two-dimensional table in Figure 8.9 shows the values needed for comput- ing C(i, j)by formula (8.8): they are in row i and the columns to the left of column j and in column j and the rows below row i. The arrows point to the pairs of en- tries whose sums are computed in order to find the smallest one to be recorded as the value of C(i, j). This suggests filling the table along its diagonals, starting with all zeros on the main diagonal and given probabilities pi, 1 ≤ i ≤ n, right above it and moving toward the upper right corner. The algorithm we just sketched computes C(1,n)—the average number of comparisons for successful searches in the optimal binary tree. If we also want to get the optimal tree itself, we need to maintain another two-dimensional table to record the value of k for which the minimum in (8.8) is achieved. The table has the same shape as the table in Figure 8.9 and is filled in the same manner, starting with entries R(i, i) = i for 1 ≤ i ≤ n. When the table is filled, its entries indicate indices of the roots of the optimal subtrees, which makes it possible to reconstruct an optimal tree for the entire set given. EXAMPLE Let us illustrate the algorithm by applying it to the four-key set we used at the beginning of this section: 8.3 Optimal Binary Search Trees 301 key ABCD probability 0.1 0.2 0.4 0.3 The initial tables look like this: 0.1 0 0.2 0 0.4 0 0.3 0 01 2 3 4 5 01 2 3 4 1 2 3 4 5 01 1 2 2 3 3 4 4 main table root table Let us compute C(1, 2): C(1, 2) = min k = 1: C(1, 0) + C(2, 2) + 2 s=1 ps = 0 + 0.2 + 0.3 = 0.5 k = 2: C(1, 1) + C(3, 2) + 2 s=1 ps = 0.1 + 0 + 0.3 = 0.4 = 0.4. Thus, out of two possible binary trees containing the first two keys, A and B, the root of the optimal tree has index 2 (i.e., it contains B), and the average number of comparisons in a successful search in this tree is 0.4. We will ask you to finish the computations in the exercises. You should arrive at the following final tables: 0.1 0 0.4 0.2 0 1.1 0.8 0.4 0 1.7 1.4 1.0 0.3 0 01 2 3 4 5 01 2 3 4 1 2 3 4 5 01 12 2 3 3 3 3 3 3 4 234 main table root table Thus, the average number of key comparisons in the optimal tree is equal to 1.7. Since R(1, 4) = 3, the root of the optimal tree contains the third key, i.e., C. Its left subtree is made up of keys A and B, and its right subtree contains just key D (why?). To find the specific structure of these subtrees, we find first their roots by consulting the root table again as follows. Since R(1, 2) = 2, the root of the optimal tree containing A and B is B, with A being its left child (and the root of the one- node tree: R(1, 1) = 1). Since R(4, 4) = 4, the root of this one-node optimal tree is its only key D. Figure 8.10 presents the optimal tree in its entirety. 302 Dynamic Programming B D A C FIGURE 8.10 Optimal binary search tree for the example. Here is pseudocode of the dynamic programming algorithm. ALGORITHM OptimalBST(P[1..n]) //Finds an optimal binary search tree by dynamic programming //Input: An array P[1..n] of search probabilities for a sorted list of n keys //Output: Average number of comparisons in successful searches in the // optimal BST and table R of subtrees’ roots in the optimal BST for i ← 1 to n do C[i, i − 1] ← 0 C[i, i] ← P[i] R[i, i] ← i C[n + 1,n] ← 0 for d ← 1 to n − 1 do //diagonal count for i ← 1 to n − d do j ← i + d minval ←∞ for k ← i to j do if C[i, k − 1] + C[k + 1,j] < minval minval ← C[i, k − 1] + C[k + 1,j]; kmin ← k R[i, j] ← kmin sum ← P[i]; for s ← i + 1 to j do sum ← sum + P [s] C[i, j] ← minval + sum return C[1,n],R The algorithm’s space efficiency is clearly quadratic; the time efficiency of this version of the algorithm is cubic (why?). A more careful analysis shows that entries in the root table are always nondecreasing along each row and column. This limits values for R(i, j) to the range R(i, j − 1), . . . , R(i + 1,j)and makes it possible to reduce the running time of the algorithm to (n2). 8.3 Optimal Binary Search Trees 303 Exercises 8.3 1. Finish the computations started in the section’s example of constructing an optimal binary search tree. 2. a. Why is the time efficiency of algorithm OptimalBST cubic? b. Why is the space efficiency of algorithm OptimalBST quadratic? 3. Write pseudocode for a linear-time algorithm that generates the optimal binary search tree from the root table. 4. Devise a way to compute the sums j s=i ps, which are used in the dynamic programming algorithm for constructing an optimal binary search tree, in constant time (per sum). 5. True or false: The root of an optimal binary search tree always contains the key with the highest search probability? 6. How would you construct an optimal binary search tree for a set of n keys if all the keys are equally likely to be searched for? What will be the average number of comparisons in a successful search in such a tree if n = 2k? 7. a. Show that the number of distinct binary search trees b(n) that can be constructed for a set of n orderable keys satisfies the recurrence relation b(n) = n−1 k=0 b(k)b(n − 1 − k) for n>0,b(0) = 1. b. It is known that the solution to this recurrence is given by the Catalan numbers. Verify this assertion for n = 1, 2,...,5. c. Find the order of growth of b(n). What implication does the answer to this question have for the exhaustive-search algorithm for constructing an optimal binary search tree? 8. Design a (n2) algorithm for finding an optimal binary search tree. 9. Generalize the optimal binary search algorithm by taking into account unsuc- cessful searches. 10. Write pseudocode of a memory function for the optimal binary search tree problem. You may limit your function to finding the smallest number of key comparisons in a successful search. 11. Matrix chain multiplication Consider the problem of minimizing the total number of multiplications made in computing the product of n matrices A1 . A2 . .... An whose dimensions are d0 × d1,d1 × d2,...,dn−1 × dn, respectively. Assume that all intermediate products of two matrices are computed by the brute- force (definition-based) algorithm. 304 Dynamic Programming a. Give an example of three matrices for which the number of multiplications in (A1 . A2) . A3 and A1 . (A2 . A3) differ at least by a factor of 1000. b. How many different ways are there to compute the product of n matrices? c. Design a dynamic programming algorithm for finding an optimal order of multiplying n matrices. 8.4 Warshall’s and Floyd’s Algorithms In this section, we look at two well-known algorithms: Warshall’s algorithm for computing the transitive closure of a directed graph and Floyd’s algorithm for the all-pairs shortest-paths problem. These algorithms are based on essentially the same idea: exploit a relationship between a problem and its simpler rather than smaller version. Warshall and Floyd published their algorithms without mention- ing dynamic programming. Nevertheless, the algorithms certainly have a dynamic programming flavor and have come to be considered applications of this tech- nique. Warshall’s Algorithm Recall that the adjacency matrix A ={aij } of a directed graph is the boolean matrix that has 1 in its ith row and jth column if and only if there is a directed edge from the ith vertex to the jth vertex. We may also be interested in a matrix containing the information about the existence of directed paths of arbitrary lengths between vertices of a given graph. Such a matrix, called the transitive closure of the digraph, would allow us to determine in constant time whether the jth vertex is reachable from the ith vertex. Here are a few application examples. When a value in a spreadsheet cell is changed, the spreadsheet software must know all the other cells affected by the change. If the spreadsheet is modeled by a digraph whose vertices represent the spreadsheet cells and edges indicate cell dependencies, the transitive closure will provide such information. In software engineering, transitive closure can be used for investigating data flow and control flow dependencies as well as for inheritance testing of object-oriented software. In electronic engineering, it is used for redundancy identification and test generation for digital circuits. DEFINITION The transitive closure of a directed graph with n vertices can be defined as the n × n boolean matrix T ={tij }, in which the element in the ith row and the jth column is 1 if there exists a nontrivial path (i.e., directed path of a positive length) from the ith vertex to the jth vertex; otherwise, tij is 0. An example of a digraph, its adjacency matrix, and its transitive closure is given in Figure 8.11. We can generate the transitive closure of a digraph with the help of depth- first search or breadth-first search. Performing either traversal starting at the ith 8.4 Warshall’s and Floyd’s Algorithms 305 ba a b c ddc abcd A = 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 a b c d abcd T = 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 (a) (b) (c) FIGURE 8.11 (a) Digraph. (b) Its adjacency matrix. (c) Its transitive closure. vertex gives the information about the vertices reachable from it and hence the columns that contain 1’s in the ith row of the transitive closure. Thus, doing such a traversal for every vertex as a starting point yields the transitive closure in its entirety. Since this method traverses the same digraph several times, we should hope that a better algorithm can be found. Indeed, such an algorithm exists. It is called Warshall’s algorithm after Stephen Warshall, who discovered it [War62]. It is convenient to assume that the digraph’s vertices and hence the rows and columns of the adjacency matrix are numbered from 1 to n. Warshall’s algorithm constructs the transitive closure through a series of n × n boolean matrices: R(0),...,R(k−1),R(k),...R(n). (8.9) Each of these matrices provides certain information about directed paths in the digraph. Specifically, the element r(k) ij in the ith row and jth column of matrix R(k) (i, j = 1, 2,...,n,k= 0, 1,...,n) is equal to 1 if and only if there exists a directed path of a positive length from the ith vertex to the jth vertex with each intermediate vertex, if any, numbered not higher than k. Thus, the series starts with R(0), which does not allow any intermediate vertices in its paths; hence, R(0) is nothing other than the adjacency matrix of the digraph. (Recall that the adjacency matrix contains the information about one-edge paths, i.e., paths with no intermediate vertices.) R(1) contains the information about paths that can use the first vertex as intermediate; thus, with more freedom, so to speak, it may contain more 1’s than R(0). In general, each subsequent matrix in series (8.9) has one more vertex to use as intermediate for its paths than its predecessor and hence may, but does not have to, contain more 1’s. The last matrix in the series, R(n), reflects paths that can use all n vertices of the digraph as intermediate and hence is nothing other than the digraph’s transitive closure. The central point of the algorithm is that we can compute all the elements of each matrix R(k) from its immediate predecessor R(k−1) in series (8.9). Let r(k) ij , the element in the ith row and jth column of matrix R(k), be equal to 1. This means that there exists a path from the ith vertex vi to the jth vertex vj with each intermediate vertex numbered not higher than k: vi, a list of intermediate vertices each numbered not higher than k, vj. (8.10) 306 Dynamic Programming k R(k – 1) jkj 1 11 k i 1= R(k) k i = 01 ↑ → FIGURE 8.12 Rule for changing zeros in Warshall’s algorithm. Two situations regarding this path are possible. In the first, the list of its inter- mediate vertices does not contain the kth vertex. Then this path from vi to vj has intermediate vertices numbered not higher than k − 1, and therefore r(k−1) ij is equal to 1 as well. The second possibility is that path (8.10) does contain the kth vertex vk among the intermediate vertices. Without loss of generality, we may assume that vk occurs only once in that list. (If it is not the case, we can create a new path from vi to vj with this property by simply eliminating all the vertices between the first and last occurrences of vk in it.) With this caveat, path (8.10) can be rewritten as follows: vi, vertices numbered ≤ k − 1,vk, vertices numbered ≤ k − 1,vj. The first part of this representation means that there exists a path from vi to vk with each intermediate vertex numbered not higher than k − 1 (hence, r(k−1) ik = 1), and the second part means that there exists a path from vk to vj with each intermediate vertex numbered not higher than k − 1 (hence, r(k−1) kj = 1). What we have just proved is that if r(k) ij = 1, then either r(k−1) ij = 1 or both r(k−1) ik = 1 and r(k−1) kj = 1. It is easy to see that the converse of this assertion is also true. Thus, we have the following formula for generating the elements of matrix R(k) from the elements of matrix R(k−1): r(k) ij = r(k−1) ij or r(k−1) ik and r(k−1) kj . (8.11) Formula (8.11) is at the heart of Warshall’s algorithm. This formula implies the following rule for generating elements of matrix R(k) from elements of matrix R(k−1), which is particularly convenient for applying Warshall’s algorithm by hand: If an element rij is1inR(k−1), it remains 1 in R(k). If an element rij is0inR(k−1), it has to be changed to 1 in R(k) if and only if the element in its row i and column k and the element in its column j and row k are both 1’s in R(k−1). This rule is illustrated in Figure 8.12. As an example, the application of Warshall’s algorithm to the digraph in Figure 8.11 is shown in Figure 8.13. 8.4 Warshall’s and Floyd’s Algorithms 307 R(0) = a b c d abcd 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 R(1) = a b c d abcd 0 0 0 1 1 0 0 1 0 0 0 1 0 1 0 0 R(2) = a b c d abcd 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 1 R(3) = a b c d abcd 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 1 R(4) = a b c d abcd 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1’s reflect the existence of paths with no intermediate vertices (R(0) is just the adjacency matrix); boxed row and column are used for getting R(1). 1’s reflect the existence of paths with intermediate vertices numbered not higher than 1, i.e., just vertex a (note a new path from d to b); boxed row and column are used for getting R(2). 1’s reflect the existence of paths with intermediate vertices numbered not higher than 2, i.e., a and b (note two new paths); boxed row and column are used for getting R(3). 1’s reflect the existence of paths with intermediate vertices numbered not higher than 3, i.e., a, b, and c (no new paths); boxed row and column are used for getting R(4). 1’s reflect the existence of paths with intermediate vertices numbered not higher than 4, i.e., a, b, c, and d (note five new paths). ba dc FIGURE 8.13 Application of Warshall’s algorithm to the digraph shown. New 1’s are in bold. Here is pseudocode of Warshall’s algorithm. ALGORITHM Warshall(A[1..n, 1..n]) //Implements Warshall’s algorithm for computing the transitive closure //Input: The adjacency matrix A of a digraph with n vertices //Output: The transitive closure of the digraph R(0) ← A for k ← 1 to n do for i ← 1 to n do for j ← 1 to n do R(k)[i, j] ← R(k−1)[i, j] or (R(k−1)[i, k] and R(k−1)[k, j]) return R(n) Several observations need to be made about Warshall’s algorithm. First, it is remarkably succinct, is it not? Still, its time efficiency is only (n3). In fact, for sparse graphs represented by their adjacency lists, the traversal-based algorithm 308 Dynamic Programming ba a b c ddc abcd W = 0 2 ∞ 6 ∞ 0 7 ∞ 3 ∞ 0 ∞ ∞ ∞ 1 0 a b c d abcd D = 0 2 7 6 10 0 7 16 3 5 0 9 4 6 1 0 (a) (b) (c) 3 2 6 7 1 FIGURE 8.14 (a) Digraph. (b) Its weight matrix. (c) Its distance matrix. mentioned at the beginning of this section has a better asymptotic efficiency than Warshall’s algorithm (why?). We can speed up the above implementation of Warshall’s algorithm for some inputs by restructuring its innermost loop (see Problem 4 in this section’s exercises). Another way to make the algorithm run faster is to treat matrix rows as bit strings and employ the bitwise or operation available in most modern computer languages. As to the space efficiency of Warshall’s algorithm, the situation is similar to that of computing a Fibonacci number and some other dynamic programming algorithms. Although we used separate matrices for recording intermediate results of the algorithm, this is, in fact, unnecessary. Problem 3 in this section’s exercises asks you to find a way of avoiding this wasteful use of the computer memory. Finally, we shall see below how the underlying idea of Warshall’s algorithm can be applied to the more general problem of finding lengths of shortest paths in weighted graphs. Floyd’s Algorithm for the All-Pairs Shortest-Paths Problem Given a weighted connected graph (undirected or directed), the all-pairs shortest- paths problem asks to find the distances—i.e., the lengths of the shortest paths— from each vertex to all other vertices. This is one of several variations of the problem involving shortest paths in graphs. Because of its important applications to communications, transportation networks, and operations research, it has been thoroughly studied over the years. Among recent applications of the all-pairs shortest-path problem is precomputing distances for motion planning in computer games. It is convenient to record the lengths of shortest paths in an n × n matrix D called the distance matrix: the element dij in the ith row and the jth column of this matrix indicates the length of the shortest path from the ith vertex to the jth vertex. For an example, see Figure 8.14. We can generate the distance matrix with an algorithm that is very similar to Warshall’s algorithm. It is called Floyd’s algorithm after its co-inventor Robert W. Floyd.1 It is applicable to both undirected and directed weighted graphs provided 1. Floyd explicitly referenced Warshall’s paper in presenting his algorithm [Flo62]. Three years earlier, Bernard Roy published essentially the same algorithm in the proceedings of the French Academy of Sciences [Roy59]. 8.4 Warshall’s and Floyd’s Algorithms 309 that they do not contain a cycle of a negative length. (The distance between any two vertices in such a cycle can be made arbitrarily small by repeating the cycle enough times.) The algorithm can be enhanced to find not only the lengths of the shortest paths for all vertex pairs but also the shortest paths themselves (Problem 10 in this section’s exercises). Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices through a series of n × n matrices: D(0),...,D(k−1),D(k),...,D(n). (8.12) Each of these matrices contains the lengths of shortest paths with certain con- straints on the paths considered for the matrix in question. Specifically, the el- ement d(k) ij in the ith row and the jth column of matrix D(k) (i, j = 1, 2,...,n, k = 0, 1,...,n)is equal to the length of the shortest path among all paths from the ith vertex to the jth vertex with each intermediate vertex, if any, numbered not higher than k. In particular, the series starts with D(0), which does not allow any intermediate vertices in its paths; hence, D(0) is simply the weight matrix of the graph. The last matrix in the series, D(n), contains the lengths of the shortest paths among all paths that can use all n vertices as intermediate and hence is nothing other than the distance matrix being sought. As in Warshall’s algorithm, we can compute all the elements of each matrix D(k) from its immediate predecessor D(k−1) in series (8.12). Let d(k) ij be the element in the ith row and the jth column of matrix D(k). This means that d(k) ij is equal to the length of the shortest path among all paths from the ith vertex vi to the jth vertex vj with their intermediate vertices numbered not higher than k: vi, a list of intermediate vertices each numbered not higher than k, vj. (8.13) We can partition all such paths into two disjoint subsets: those that do not use the kth vertex vk as intermediate and those that do. Since the paths of the first subset have their intermediate vertices numbered not higher than k − 1, the shortest of them is, by definition of our matrices, of length d(k−1) ij . What is the length of the shortest path in the second subset? If the graph does not contain a cycle of a negative length, we can limit our attention only to the paths in the second subset that use vertex vk as their intermediate vertex exactly once (because visiting vk more than once can only increase the path’s length). All such paths have the following form: vi, vertices numbered ≤ k − 1,vk, vertices numbered ≤ k − 1,vj. In other words, each of the paths is made up of a path from vi to vk with each intermediate vertex numbered not higher than k − 1 and a path from vk to vj with each intermediate vertex numbered not higher than k − 1. The situation is depicted symbolically in Figure 8.15. Since the length of the shortest path from vi to vk among the paths that use intermediate vertices numbered not higher than k − 1 is equal to d(k−1) ik and the length of the shortest path from vk to vj among the paths that use intermediate 310 Dynamic Programming vi vj vk dkj (k –1) dij (k –1) dik (k –1) FIGURE 8.15 Underlying idea of Floyd’s algorithm. vertices numbered not higher than k − 1is equal to d(k−1) kj , the length of the shortest path among the paths that use the kth vertex is equal to d(k−1) ik + d(k−1) kj . Taking into account the lengths of the shortest paths in both subsets leads to the following recurrence: d(k) ij = min{d(k−1) ij ,d(k−1) ik + d(k−1) kj } for k ≥ 1,d(0) ij = wij . (8.14) To put it another way, the element in row i and column j of the current distance matrix D(k−1) is replaced by the sum of the elements in the same row i and the column k and in the same column j and the row k if and only if the latter sum is smaller than its current value. The application of Floyd’s algorithm to the graph in Figure 8.14 is illustrated in Figure 8.16. Here is pseudocode of Floyd’s algorithm. It takes advantage of the fact that the next matrix in sequence (8.12) can be written over its predecessor. ALGORITHM Floyd(W[1..n, 1..n]) //Implements Floyd’s algorithm for the all-pairs shortest-paths problem //Input: The weight matrix W of a graph with no negative-length cycle //Output: The distance matrix of the shortest paths’ lengths D ← W //is not necessary if W can be overwritten for k ← 1 to n do for i ← 1 to n do for j ← 1 to n do D[i, j] ← min{D[i, j], D[i, k] + D[k, j]} return D Obviously, the time efficiency of Floyd’s algorithm is cubic—as is the time efficiency of Warshall’s algorithm. In the next chapter, we examine Dijkstra’s algorithm—another method for finding shortest paths. 8.4 Warshall’s and Floyd’s Algorithms 311 D(0) = a b c d abcd 0 2 ∞ 6 ∞ 0 7 ∞ 3 ∞ 0 ∞ ∞ ∞ 1 0 D(1) = a b c d abcd 0 2 ∞ 6 ∞ 0 7 ∞ 3 5 0 9 ∞ ∞ 1 0 D(2) = a b c d abcd 0 2 9 6 ∞ 0 7 ∞ 3 5 0 9 ∞ ∞ 1 0 D(3) = a b c d abcd 0 2 9 6 10 0 7 16 3 5 0 9 4 6 1 0 D(4) = a b c d abcd 0 2 7 6 10 0 7 16 3 5 0 9 4 6 1 0 Lengths of the shortest paths with no intermediate vertices (D(0) is simply the weight matrix). Lengths of the shortest paths with intermediate vertices numbered not higher than 1, i.e., just a (note two new shortest paths from b to c and from d to c ). Lengths of the shortest paths with intermediate vertices numbered not higher than 2, i.e., a and b (note a new shortest path from c to a). Lengths of the shortest paths with intermediate vertices numbered not higher than 3, i.e., a, b, and c (note four new shortest paths from a to b, from a to d, from b to d, and from d to b). Lengths of the shortest paths with intermediate vertices numbered not higher than 4, i.e., a, b, c, and d (note a new shortest path from c to a). ba dc 3 2 67 1 FIGURE 8.16 Application of Floyd’s algorithm to the digraph shown. Updated elements are shown in bold. Exercises 8.4 1. Apply Warshall’s algorithm to find the transitive closure of the digraph de- fined by the following adjacency matrix: ⎡ ⎢⎢⎣ 0100 0010 0001 0000 ⎤ ⎥⎥⎦ 2. a. Prove that the time efficiency of Warshall’s algorithm is cubic. b. Explain why the time efficiency class of Warshall’s algorithm is inferior to that of the traversal-based algorithm for sparse graphs represented by their adjacency lists. 312 Dynamic Programming 3. Explain how to implement Warshall’s algorithm without using extra memory for storing elements of the algorithm’s intermediate matrices. 4. Explain how to restructure the innermost loop of the algorithm Warshall to make it run faster at least on some inputs. 5. Rewrite pseudocode of Warshall’s algorithm assuming that the matrix rows are represented by bit strings on which the bitwise or operation can be per- formed. 6. a. Explain how Warshall’s algorithm can be used to determine whether a given digraph is a dag (directed acyclic graph). Is it a good algorithm for this problem? b. Is it a good idea to apply Warshall’s algorithm to find the transitive closure of an undirected graph? 7. Solve the all-pairs shortest-path problem for the digraph with the following weight matrix: ⎡ ⎢⎢⎢⎢⎣ 02∞ 18 6032∞ ∞∞ 04∞ ∞∞ 203 3 ∞∞∞ 0 ⎤ ⎥⎥⎥⎥⎦ 8. Prove that the next matrix in sequence (8.12) of Floyd’s algorithm can be written over its predecessor. 9. Give an example of a graph or a digraph with negative weights for which Floyd’s algorithm does not yield the correct result. 10. Enhance Floyd’s algorithm so that shortest paths themselves, not just their lengths, can be found. 11. Jack Straws In the game of Jack Straws, a number of plastic or wooden “straws” are dumped on the table and players try to remove them one by one without disturbing the other straws. Here, we are only concerned with whether various pairs of straws are connected by a path of touching straws. Given a list of the endpoints for n>1straws (as if they were dumped on a large piece of graph paper), determine all the pairs of straws that are connected. Note that touching is connecting, but also that two straws can be connected indirectly via other connected straws. [1994 East-Central Regionals of the ACM International Collegiate Programming Contest] SUMMARY Dynamic programming is a technique for solving problems with overlapping subproblems. Typically, these subproblems arise from a recurrence relating a solution to a given problem with solutions to its smaller subproblems of the Summary 313 same type. Dynamic programming suggests solving each smaller subproblem once and recording the results in a table from which a solution to the original problem can be then obtained. Applicability of dynamic programming to an optimization problem requires the problem to satisfy the principle of optimality: an optimal solution to any of its instances must be made up of optimal solutions to its subinstances. Among many other problems, the change-making problem with arbitrary coin denominations can be solved by dynamic programming. Solving a knapsack problem by a dynamic programming algorithm exempli- fies an application of this technique to difficult problems of combinatorial optimization. The memory function technique seeks to combine the strengths of the top- down and bottom-up approaches to solving problems with overlapping subproblems. It does this by solving, in the top-down fashion but only once, just the necessary subproblems of a given problem and recording their solutions in a table. Dynamic programming can be used for constructing an optimal binary search tree for a given set of keys and known probabilities of searching for them. Warshall’s algorithm for finding the transitive closure and Floyd’s algorithm for the all-pairs shortest-paths problem are based on the idea that can be interpreted as an application of the dynamic programming technique. This page intentionally left blank 9 Greedy Technique Greed, for lack of a better word, is good! Greed is right! Greed works! —Michael Douglas, US actor in the role of Gordon Gecko, in the film Wall Street, 1987 Let us revisit the change-making problem faced, at least subconsciously, by millions of cashiers all over the world: give change for a specific amount n with the least number of coins of the denominations d1 >d2 > ...>dm used in that locale. (Here, unlike Section 8.1, we assume that the denominations are ordered in decreasing order.) For example, the widely used coin denominations in the United States are d1 = 25 (quarter), d2 = 10 (dime),d3 = 5 (nickel), and d4 = 1 (penny). How would you give change with coins of these denominations of, say, 48 cents? If you came up with the answer 1 quarter, 2 dimes, and 3 pennies, you followed— consciously or not—a logical strategy of making a sequence of best choices among the currently available alternatives. Indeed, in the first step, you could have given one coin of any of the four denominations. “Greedy” thinking leads to giving one quarter because it reduces the remaining amount the most, namely, to 23 cents. In the second step, you had the same coins at your disposal, but you could not give a quarter, because it would have violated the problem’s constraints. So your best selection in this step was one dime, reducing the remaining amount to 13 cents. Giving one more dime left you with 3 cents to be given with three pennies. Is this solution to the instance of the change-making problem optimal? Yes, it is. In fact, one can prove that the greedy algorithm yields an optimal solution for every positive integer amount with these coin denominations. At the same time, it is easy to give an example of coin denominations that do not yield an optimal solution for some amounts—e.g., d1 = 25,d2 = 10,d3 = 1 and n = 30. The approach applied in the opening paragraph to the change-making prob- lem is called greedy. Computer scientists consider it a general design technique despite the fact that it is applicable to optimization problems only. The greedy approach suggests constructing a solution through a sequence of steps, each ex- panding a partially constructed solution obtained so far, until a complete solution 315 316 Greedy Technique to the problem is reached. On each step—and this is the central point of this technique—the choice made must be: feasible, i.e., it has to satisfy the problem’s constraints locally optimal, i.e., it has to be the best local choice among all feasible choices available on that step irrevocable, i.e., once made, it cannot be changed on subsequent steps of the algorithm These requirements explain the technique’s name: on each step, it suggests a “greedy” grab of the best alternative available in the hope that a sequence of locally optimal choices will yield a (globally) optimal solution to the entire problem. We refrain from a philosophical discussion of whether greed is good or bad. (If you have not seen the movie from which the chapter’s epigraph is taken, its hero did not end up well.) From our algorithmic perspective, the question is whether such a greedy strategy works or not. As we shall see, there are problems for which a sequence of locally optimal choices does yield an optimal solution for every instance of the problem in question. However, there are others for which this is not the case; for such problems, a greedy algorithm can still be of value if we are interested in or have to be satisfied with an approximate solution. In the first two sections of the chapter, we discuss two classic algorithms for the minimum spanning tree problem: Prim’s algorithm and Kruskal’s algorithm. What is remarkable about these algorithms is the fact that they solve the same problem by applying the greedy approach in two different ways, and both of them always yield an optimal solution. In Section 9.3, we introduce another classic algorithm— Dijkstra’s algorithm for the shortest-path problem in a weighted graph. Section 9.4 is devoted to Huffman trees and their principal application, Huffman codes—an important data compression method that can be interpreted as an application of the greedy technique. Finally, a few examples of approximation algorithms based on the greedy approach are discussed in Section 12.3. As a rule, greedy algorithms are both intuitively appealing and simple. Given an optimization problem, it is usually easy to figure out how to proceed in a greedy manner, possibly after considering a few small instances of the problem. What is usually more difficult is to prove that a greedy algorithm yields an optimal solution (when it does). One of the common ways to do this is illustrated by the proof given in Section 9.1: using mathematical induction, we show that a partially constructed solution obtained by the greedy algorithm on each iteration can be extended to an optimal solution to the problem. The second way to prove optimality of a greedy algorithm is to show that on each step it does at least as well as any other algorithm could in advancing toward the problem’s goal. Consider, as an example, the following problem: find the minimum number of moves needed for a chess knight to go from one corner of a 100 × 100 board to the diagonally opposite corner. (The knight’s moves are L-shaped jumps: two squares horizontally or vertically followed by one square in Greedy Technique 317 the perpendicular direction.) A greedy solution is clear here: jump as close to the goal as possible on each move. Thus, if its start and finish squares are (1,1) and (100, 100), respectively, a sequence of 66 moves such as (1, 1) − (3, 2) − (4, 4) − ...− (97, 97) − (99, 98) − (100, 100) solves the problem. (The number k of two-move advances can be obtained from the equation 1 + 3k = 100.) Why is this a minimum-move solution? Because if we measure the distance to the goal by the Manhattan distance, which is the sum of the difference between the row numbers and the difference between the column numbers of two squares in question, the greedy algorithm decreases it by 3 on each move—the best the knight can do. The third way is simply to show that the final result obtained by a greedy algorithm is optimal based on the algorithm’s output rather than the way it op- erates. As an example, consider the problem of placing the maximum number of chips on an 8 × 8 board so that no two chips are placed on the same or adjacent— vertically, horizontally, or diagonally—squares. To follow the prescription of the greedy strategy, we should place each new chip so as to leave as many available squares as possible for next chips. For example, starting with the upper left corner of the board, we will be able to place 16 chips as shown in Figure 9.1a. Why is this solution optimal? To see why, partition the board into sixteen 4 × 4 squares as shown in Figure 9.1b. Obviously, it is impossible to place more than one chip in each of these squares, which implies that the total number of nonadjacent chips on the board cannot exceed 16. As a final comment, we should mention that a rather sophisticated theory has been developed behind the greedy technique, which is based on the abstract combinatorial structure called “matroid.” An interested reader can check such books as [Cor09] as well as a variety of Internet resources on the subject. FIGURE 9.1 (a) Placement of 16 chips on non-adjacent squares. (b) Partition of the board proving impossibility of placing more than 16 chips. 318 Greedy Technique ba dc ba dc ba dc ba dc 1 2 1 225 3 1 5 1 5 33 graph w(T1) = 6 w(T2) = 9 w(T3) = 8 FIGURE 9.2 Graph and its spanning trees, with T1 being the minimum spanning tree. 9.1 Prim’s Algorithm The following problem arises naturally in many practical situations: given n points, connect them in the cheapest possible way so that there will be a path between ev- ery pair of points. It has direct applications to the design of all kinds of networks— including communication, computer, transportation, and electrical—by providing the cheapest way to achieve connectivity. It identifies clusters of points in data sets. It has been used for classification purposes in archeology, biology, sociology, and other sciences. It is also helpful for constructing approximate solutions to more difficult problems such the traveling salesman problem (see Section 12.3). We can represent the points given by vertices of a graph, possible connections by the graph’s edges, and the connection costs by the edge weights. Then the question can be posed as the minimum spanning tree problem, defined formally as follows. DEFINITION A spanning tree of an undirected connected graph is its connected acyclic subgraph (i.e., a tree) that contains all the vertices of the graph. If such a graph has weights assigned to its edges, a minimum spanning tree is its spanning tree of the smallest weight, where the weight of a tree is defined as the sum of the weights on all its edges. The minimum spanning tree problem is the problem of finding a minimum spanning tree for a given weighted connected graph. Figure 9.2 presents a simple example illustrating these notions. If we were to try constructing a minimum spanning tree by exhaustive search, we would face two serious obstacles. First, the number of spanning trees grows exponentially with the graph size (at least for dense graphs). Second, generating all spanning trees for a given graph is not easy; in fact, it is more difficult than finding a minimum spanning tree for a weighted graph by using one of several efficient algorithms available for this problem. In this section, we outline Prim’s algorithm, which goes back to at least 19571 [Pri57]. 1. Robert Prim rediscovered the algorithm published 27 years earlier by the Czech mathematician Vojtˇech Jarn´ık in a Czech journal. 9.1 Prim’s Algorithm 319 Prim’s algorithm constructs a minimum spanning tree through a sequence of expanding subtrees. The initial subtree in such a sequence consists of a single vertex selected arbitrarily from the set V of the graph’s vertices. On each iteration, the algorithm expands the current tree in the greedy manner by simply attaching to it the nearest vertex not in that tree. (By the nearest vertex, we mean a vertex not in the tree connected to a vertex in the tree by an edge of the smallest weight. Ties can be broken arbitrarily.) The algorithm stops after all the graph’s vertices have been included in the tree being constructed. Since the algorithm expands a tree by exactly one vertex on each of its iterations, the total number of such iterations is n − 1, where n is the number of vertices in the graph. The tree generated by the algorithm is obtained as the set of edges used for the tree expansions. Here is pseudocode of this algorithm. ALGORITHM Prim(G) //Prim’s algorithm for constructing a minimum spanning tree //Input: A weighted connected graph G = V,E //Output: ET , the set of edges composing a minimum spanning tree of G VT ←{v0} //the set of tree vertices can be initialized with any vertex ET ← ∅ for i ← 1 to |V |−1 do find a minimum-weight edge e∗ = (v∗,u∗) among all the edges (v, u) such that v is in VT and u is in V − VT VT ← VT ∪{u∗} ET ← ET ∪{e∗} return ET The nature of Prim’s algorithm makes it necessary to provide each vertex not in the current tree with the information about the shortest edge connecting the vertex to a tree vertex. We can provide such information by attaching two labels to a vertex: the name of the nearest tree vertex and the length (the weight) of the corresponding edge. Vertices that are not adjacent to any of the tree vertices can be given the ∞ label indicating their “infinite” distance to the tree vertices and a null label for the name of the nearest tree vertex. (Alternatively, we can split the vertices that are not in the tree into two sets, the “fringe” and the “unseen.” The fringe contains only the vertices that are not in the tree but are adjacent to at least one tree vertex. These are the candidates from which the next tree vertex is selected. The unseen vertices are all the other vertices of the graph, called “unseen” because they are yet to be affected by the algorithm.) With such labels, finding the next vertex to be added to the current tree T = VT ,ET becomes a simple task of finding a vertex with the smallest distance label in the set V − VT . Ties can be broken arbitrarily. After we have identified a vertex u∗ to be added to the tree, we need to perform two operations: 320 Greedy Technique Move u∗ from the set V − VT to the set of tree vertices VT . For each remaining vertex u in V − VT that is connected to u∗ by a shorter edge than the u’s current distance label, update its labels by u∗ and the weight of the edge between u∗ and u, respectively.2 Figure 9.3 demonstrates the application of Prim’s algorithm to a specific graph. Does Prim’s algorithm always yield a minimum spanning tree? The answer to this question is yes. Let us prove by induction that each of the subtrees Ti, i = 0,...,n− 1, generated by Prim’s algorithm is a part (i.e., a subgraph) of some minimum spanning tree. (This immediately implies, of course, that the last tree in the sequence, Tn−1, is a minimum spanning tree itself because it contains all n vertices of the graph.) The basis of the induction is trivial, since T0 consists of a single vertex and hence must be a part of any minimum spanning tree. For the inductive step, let us assume that Ti−1 is part of some minimum spanning tree T . We need to prove that Ti, generated from Ti−1 by Prim’s algorithm, is also a part of a minimum spanning tree. We prove this by contradiction by assuming that no minimum spanning tree of the graph can contain Ti. Let ei = (v, u) be the minimum weight edge from a vertex in Ti−1 to a vertex not in Ti−1 used by Prim’s algorithm to expand Ti−1 to Ti. By our assumption, ei cannot belong to any minimum spanning tree, including T . Therefore, if we add ei to T , a cycle must be formed (Figure 9.4). In addition to edge ei = (v, u), this cycle must contain another edge (v ,u ) connecting a vertex v ∈ Ti−1 to a vertex u that is not in Ti−1. (It is possible that v coincides with v or u coincides with u but not both.) If we now delete the edge (v ,u ) from this cycle, we will obtain another spanning tree of the entire graph whose weight is less than or equal to the weight of T since the weight of ei is less than or equal to the weight of (v ,u ). Hence, this spanning tree is a minimum spanning tree, which contradicts the assumption that no minimum spanning tree contains Ti. This completes the correctness proof of Prim’s algorithm. How efficient is Prim’s algorithm? The answer depends on the data structures chosen for the graph itself and for the priority queue of the set V − VT whose vertex priorities are the distances to the nearest tree vertices. (You may want to take another look at the example in Figure 9.3 to see that the set V − VT indeed operates as a priority queue.) In particular, if a graph is represented by its weight matrix and the priority queue is implemented as an unordered array, the algorithm’s running time will be in (|V |2). Indeed, on each of the |V |−1 iterations, the array implementing the priority queue is traversed to find and delete the minimum and then to update, if necessary, the priorities of the remaining vertices. We can also implement the priority queue as a min-heap. A min-heap is a mirror image of the heap structure discussed in Section 6.4. (In fact, it can be im- plemented by constructing a heap after negating all the key values given.) Namely, a min-heap is a complete binary tree in which every element is less than or equal 2. If the implementation with the fringe/unseen split is pursued, all the unseen vertices adjacent to u∗ must also be moved to the fringe. b c a df e 1 2 55 4436 86 Tree vertices Remaining vertices Illustration a(−, −) b(a, 3) c(−, ∞) d(−, ∞) e(a, 6) f(a, 5) b c a df e 1 2 55 4436 86 b(a, 3) c(b, 1) d(−, ∞) e(a, 6) f(b, 4) b c a df e 1 2 55 4436 86 c(b, 1) d(c, 6) e(a, 6) f(b, 4) b c a df e 1 2 55 4436 86 f(b, 4) d(f, 5) e(f, 2) b c a df e 1 2 55 4436 86 e(f, 2) d(f, 5) b c a df e 1 2 55 4436 86 d(f, 5) FIGURE 9.3 Application of Prim’s algorithm. The parenthesized labels of a vertex in the middle column indicate the nearest tree vertex and edge weight; selected vertices and edges are shown in bold. 322 Greedy Technique v ′ u ′ uv Ti –1 ei FIGURE 9.4 Correctness proof of Prim’s algorithm. to its children. All the principal properties of heaps remain valid for min-heaps, with some obvious modifications. For example, the root of a min-heap contains the smallest rather than the largest element. Deletion of the smallest element from and insertion of a new element into a min-heap of size n are O(log n) operations, and so is the operation of changing an element’s priority (see Problem 15 in this section’s exercises). If a graph is represented by its adjacency lists and the priority queue is im- plemented as a min-heap, the running time of the algorithm is in O(|E| log |V |). This is because the algorithm performs |V |−1 deletions of the smallest element and makes |E| verifications and, possibly, changes of an element’s priority in a min-heap of size not exceeding |V |. Each of these operations, as noted earlier, is a O(log |V |) operation. Hence, the running time of this implementation of Prim’s algorithm is in (|V |−1 +|E|)O(log |V |) = O(|E| log |V |) because, in a connected graph, |V |−1 ≤|E|. In the next section, you will find another greedy algorithm for the minimum spanning tree problem, which is “greedy” in a manner different from that of Prim’s algorithm. Exercises 9.1 1. Write pseudocode of the greedy algorithm for the change-making problem, with an amount n and coin denominations d1 >d2 > ...>dm as its input. What is the time efficiency class of your algorithm? 2. Design a greedy algorithm for the assignment problem (see Section 3.4). Does your greedy algorithm always yield an optimal solution? 3. Job scheduling Consider the problem of scheduling n jobs of known dura- tions t1,t2,...,tn for execution by a single processor. The jobs can be executed in any order, one job at a time. You want to find a schedule that minimizes 9.1 Prim’s Algorithm 323 the total time spent by all the jobs in the system. (The time spent by one job in the system is the sum of the time spent by this job in waiting plus the time spent on its execution.) Design a greedy algorithm for this problem. Does the greedy algorithm always yield an optimal solution? 4. Compatible intervals Given n open intervals (a1,b1), (a2,b2),...,(an,bn) on the real line, each representing start and end times of some activity requiring the same resource, the task is to find the largest number of these intervals so that no two of them overlap. Investigate the three greedy algorithms based on a. earliest start first. b. shortest duration first. c. earliest finish first. For each of the three algorithms, either prove that the algorithm always yields an optimal solution or give a counterexample showing this not to be the case. 5. Bridge crossing revisited Consider the generalization of the bridge crossing puzzle (Problem 2 in Exercises 1.2) in which we have n>1 people whose bridge crossing times are t1,t2,...,tn. All the other conditions of the problem remain the same: at most two people at a time can cross the bridge (and they move with the speed of the slower of the two) and they must carry with them the only flashlight the group has. Design a greedy algorithm for this problem and find how long it will take to cross the bridge by using this algorithm. Does your algorithm yield a minimum crossing time for every instance of the problem? If it does—prove it; if it does not—find an instance with the smallest number of people for which this happens. 6. Averaging down There are n>1 identical vessels, one of them with W pints of water and the others empty. You are allowed to perform the following operation: take two of the vessels and split the total amount of water in them equally between them. The object is to achieve a minimum amount of water in the vessel containing all the water in the initial set up by a sequence of such operations. What is the best way to do this? 7. Rumor spreading There are n people, each in possession of a different rumor. They want to share all the rumors with each other by sending electronic messages. Assume that a sender includes all the rumors he or she knows at the time the message is sent and that a message may only have one addressee. Design a greedy algorithm that always yields the minimum number of messages they need to send to guarantee that every one of them gets all the rumors. 8. Bachet’s problem of weights Find an optimal set of n weights {w1,w2,..., wn} so that it would be possible to weigh on a balance scale any integer load in the largest possible range from 1 to W, provided a. weights can be put only on the free cup of the scale. 324 Greedy Technique b. weights can be put on both cups of the scale. 9. a. Apply Prim’s algorithm to the following graph. Include in the priority queue all the vertices not already in the tree. ba dc 5 5 23 76e 4 4 b. Apply Prim’s algorithm to the following graph. Include in the priority queue only the fringe vertices (the vertices not in the current tree which are adjacent to at least one tree vertex). a d h c g k 4 4 4 5 5 5 b e i l 3 33 3 221 f j6 6 9 6 75 8 10. The notion of a minimum spanning tree is applicable to a connected weighted graph. Do we have to check a graph’s connectivity before applying Prim’s algorithm, or can the algorithm do it by itself? 11. Does Prim’s algorithm always work correctly on graphs with negative edge weights? 12. Let T be a minimum spanning tree of graph G obtained by Prim’s algorithm. Let Gnew be a graph obtained by adding to G a new vertex and some edges, with weights, connecting the new vertex to some vertices in G. Can we con- struct a minimum spanning tree of Gnew by adding one of the new edges to T ? If you answer yes, explain how; if you answer no, explain why not. 13. How can one use Prim’s algorithm to find a spanning tree of a connected graph with no weights on its edges? Is it a good algorithm for this problem? 14. Prove that any weighted connected graph with distinct weights has exactly one minimum spanning tree. 15. Outline an efficient algorithm for changing an element’s value in a min-heap. What is the time efficiency of your algorithm? 9.2 Kruskal’s Algorithm 325 9.2 Kruskal’s Algorithm In the previous section, we considered the greedy algorithm that “grows” a mini- mum spanning tree through a greedy inclusion of the nearest vertex to the vertices already in the tree. Remarkably, there is another greedy algorithm for the mini- mum spanning tree problem that also always yields an optimal solution. It is named Kruskal’s algorithm after Joseph Kruskal, who discovered this algorithm when he was a second-year graduate student [Kru56]. Kruskal’s algorithm looks at a minimum spanning tree of a weighted connected graph G = V,E as an acyclic subgraph with |V |−1 edges for which the sum of the edge weights is the smallest. (It is not difficult to prove that such a subgraph must be a tree.) Consequently, the algorithm constructs a minimum spanning tree as an expanding sequence of subgraphs that are always acyclic but are not necessarily connected on the inter- mediate stages of the algorithm. The algorithm begins by sorting the graph’s edges in nondecreasing order of their weights. Then, starting with the empty subgraph, it scans this sorted list, adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise. ALGORITHM Kruskal(G) //Kruskal’s algorithm for constructing a minimum spanning tree //Input: A weighted connected graph G = V,E //Output: ET , the set of edges composing a minimum spanning tree of G sort E in nondecreasing order of the edge weights w(ei1) ≤ ...≤ w(ei|E|) ET ← ∅; ecounter ← 0 //initialize the set of tree edges and its size k ← 0 //initialize the number of processed edges while ecounter < |V |−1 do k ← k + 1 if ET ∪{eik } is acyclic ET ← ET ∪{eik }; ecounter ← ecounter + 1 return ET The correctness of Kruskal’s algorithm can be proved by repeating the essen- tial steps of the proof of Prim’s algorithm given in the previous section. The fact that ET is actually a tree in Prim’s algorithm but generally just an acyclic subgraph in Kruskal’s algorithm turns out to be an obstacle that can be overcome. Figure 9.5 demonstrates the application of Kruskal’s algorithm to the same graph we used for illustrating Prim’s algorithm in Section 9.1. As you trace the algorithm’s operations, note the disconnectedness of some of the intermediate subgraphs. Applying Prim’s and Kruskal’s algorithms to the same small graph by hand may create the impression that the latter is simpler than the former. This impres- sion is wrong because, on each of its iterations, Kruskal’s algorithm has to check whether the addition of the next edge to the edges already selected would create a b c a df e 1 2 55 4436 86 Tree edges Sorted list of edges Illustration bc 1 ef 2 ab 3 bf 4 cf 4 af 5 df 5 ae 6 cd 6 de 8 b c a df e 1 2 55 4436 86 bc 1 bc 1 ef 2 ab 3 bf 4 cf 4 af 5 df 5 ae 6 cd 6 de 8 b c a df e 1 2 55 4436 86 ef 2 bc 1 ef 2 ab 3 bf 4 cf 4 af 5 df 5 ae 6 cd 6 de 8 b c a df e 1 2 55 4436 86 ab 3 bc 1 ef 2 ab 3 bf 4 cf 4 af 5 df 5 ae 6 cd 6 de 8 b c a df e 1 2 55 4436 86 bf 4 bc 1 ef 2 ab 3 bf 4 cf 4 af 5 df 5 ae 6 cd 6 de 8 b c a df e 1 2 55 4436 86 df 5 FIGURE 9.5 Application of Kruskal’s algorithm. Selected edges are shown in bold. 9.2 Kruskal’s Algorithm 327 u v (a) u v (b) FIGURE 9.6 New edge connecting two vertices may (a) or may not (b) create a cycle. cycle. It is not difficult to see that a new cycle is created if and only if the new edge connects two vertices already connected by a path, i.e., if and only if the two ver- tices belong to the same connected component (Figure 9.6). Note also that each connected component of a subgraph generated by Kruskal’s algorithm is a tree because it has no cycles. In view of these observations, it is convenient to use a slightly different interpretation of Kruskal’s algorithm. We can consider the algorithm’s operations as a progression through a series of forests containing all the vertices of a given graph and some of its edges. The initial forest consists of |V | trivial trees, each comprising a single vertex of the graph. The final forest consists of a single tree, which is a minimum spanning tree of the graph. On each iteration, the algorithm takes the next edge (u, v) from the sorted list of the graph’s edges, finds the trees containing the vertices u and v, and, if these trees are not the same, unites them in a larger tree by adding the edge (u, v). Fortunately, there are efficient algorithms for doing so, including the crucial check for whether two vertices belong to the same tree. They are called union- find algorithms. We discuss them in the following subsection. With an efficient union-find algorithm, the running time of Kruskal’s algorithm will be dominated by the time needed for sorting the edge weights of a given graph. Hence, with an efficient sorting algorithm, the time efficiency of Kruskal’s algorithm will be in O(|E| log |E|). Disjoint Subsets and Union-Find Algorithms Kruskal’s algorithm is one of a number of applications that require a dynamic partition of some n element set S into a collection of disjoint subsets S1,S2,...,Sk. After being initialized as a collection of n one-element subsets, each containing a different element of S, the collection is subjected to a sequence of intermixed union and find operations. (Note that the number of union operations in any such sequence must be bounded above by n − 1 because each union increases a subset’s size at least by 1 and there are only n elements in the entire set S.) Thus, we are 328 Greedy Technique dealing here with an abstract data type of a collection of disjoint subsets of a finite set with the following operations: makeset(x) creates a one-element set {x}. It is assumed that this operation can be applied to each of the elements of set S only once. find(x) returns a subset containing x. union(x, y) constructs the union of the disjoint subsets Sx and Sy containing x and y, respectively, and adds it to the collection to replace Sx and Sy, which are deleted from it. For example, let S ={1, 2, 3, 4, 5, 6}. Then makeset(i) creates the set {i} and applying this operation six times initializes the structure to the collection of six singleton sets: {1}, {2}, {3}, {4}, {5}, {6}. Performing union(1, 4) and union(5, 2) yields {1, 4}, {5, 2}, {3}, {6}, and, if followed by union(4, 5) and then by union(3, 6), we end up with the disjoint subsets {1, 4, 5, 2}, {3, 6}. Most implementations of this abstract data type use one element from each of the disjoint subsets in a collection as that subset’s representative. Some implemen- tations do not impose any specific constraints on such a representative; others do so by requiring, say, the smallest element of each subset to be used as the subset’s representative. Also, it is usually assumed that set elements are (or can be mapped into) integers. There are two principal alternatives for implementing this data structure. The first one, called the quick find, optimizes the time efficiency of the find operation; the second one, called the quick union, optimizes the union operation. The quick find uses an array indexed by the elements of the underlying set S; the array’s values indicate the representatives of the subsets containing those elements. Each subset is implemented as a linked list whose header contains the pointers to the first and last elements of the list along with the number of elements in the list (see Figure 9.7 for an example). Under this scheme, the implementation of makeset(x) requires assigning the corresponding element in the representative array to x and initializing the corre- sponding linked list to a single node with the x value. The time efficiency of this operation is obviously in (1), and hence the initialization of n singleton subsets is in (n). The efficiency of find(x) is also in (1): all we need to do is to retrieve the x’s representative in the representative array. Executing union(x, y) takes longer. A straightforward solution would simply append the y’s list to the end of the x’s list, update the information about their representative for all the elements in the 9.2 Kruskal’s Algorithm 329 subset representatives element index representative 1 1 2 1 3 3 4 1 5 1 6 3 list 1 list 2 list 3 list 4 list 5 list 6 size last first 414 5 2 null 0 null null null null null 23 0 null null0 null null0 6 FIGURE 9.7 Linked-list representation of subsets {1, 4, 5, 2} and {3, 6} obtained by quick find after performing union(1, 4), union(5, 2), union(4, 5), and union(3, 6). The lists of size 0 are considered deleted from the collection. y list, and then delete the y’s list from the collection. It is easy to verify, however, that with this algorithm the sequence of union operations union(2, 1), union(3, 2),...,union(i + 1,i),...,union(n, n − 1) runs in (n2) time, which is slow compared with several known alternatives. A simple way to improve the overall efficiency of a sequence of union oper- ations is to always append the shorter of the two lists to the longer one, with ties broken arbitrarily. Of course, the size of each list is assumed to be available by, say, storing the number of elements in the list’s header. This modification is called the 330 Greedy Technique 1 54 2 3 6 1 4 5 62 (a) (b) 3 FIGURE 9.8 (a) Forest representation of subsets {1, 4, 5, 2} and {3, 6} used by quick union. (b) Result of union(5, 6). union by size. Though it does not improve the worst-case efficiency of a single ap- plication of the union operation (it is still in (n)), the worst-case running time of any legitimate sequence of union-by-size operations turns out to be in O(nlog n).3 Here is a proof of this assertion. Let ai be an element of set S whose disjoint subsets we manipulate, and let Ai be the number of times ai’s representative is updated in a sequence of union-by-size operations. How large can Ai get if set S has n elements? Each time ai’s representative is updated, ai must be in a smaller subset involved in computing the union whose size will be at least twice as large as the size of the subset containing ai. Hence, when ai’s representative is updated for the first time, the resulting set will have at least two elements; when it is updated for the second time, the resulting set will have at least four elements; and, in general, if it is updated Ai times, the resulting set will have at least 2Ai elements. Since the entire set S has n elements, 2Ai ≤ n and hence Ai ≤ log2 n. Therefore, the total number of possible updates of the representatives for all n elements in S will not exceed n log2 n. Thus, for union by size, the time efficiency of a sequence of at most n − 1 unions and m finds is in O(n log n + m). The quick union—the second principal alternative for implementing disjoint subsets—represents each subset by a rooted tree. The nodes of the tree contain the subset’s elements (one per node), with the root’s element considered the subset’s representative; the tree’s edges are directed from children to their parents (Figure 9.8). In addition, a mapping of the set elements to their tree nodes— implemented, say, as an array of pointers—is maintained. This mapping is not shown in Figure 9.8 for the sake of simplicity. For this implementation, makeset(x) requires the creation of a single-node tree, which is a (1) operation; hence, the initialization of n singleton subsets is in (n). A union(x, y) is implemented by attaching the root of the y’s tree to the root of the x’s tree (and deleting the y’s tree from the collection by making the pointer to its root null). The time efficiency of this operation is clearly (1).Afind(x) is 3. This is a specific example of the usefulness of the amortized efficiency we mentioned back in Chapter 2. 9.2 Kruskal’s Algorithm 331 T4 x T4 x T3T2 T1 T3 T2 T1 FIGURE 9.9 Path compression. performed by following the pointer chain from the node containing x to the tree’s root whose element is returned as the subset’s representative. Accordingly, the time efficiency of a single find operation is in O(n) because a tree representing a subset can degenerate into a linked list with n nodes. This time bound can be improved. The straightforward way for doing so is to always perform a union operation by attaching a smaller tree to the root of a larger one, with ties broken arbitrarily. The size of a tree can be measured either by the number of nodes (this version is called union by size) or by its height (this version is called union by rank). Of course, these options require storing, for each node of the tree, either the number of node descendants or the height of the subtree rooted at that node, respectively. One can easily prove that in either case the height of the tree will be logarithmic, making it possible to execute each find in O(log n) time. Thus, for quick union, the time efficiency of a sequence of at most n − 1 unions and m finds is in O(n + m log n). In fact, an even better efficiency can be obtained by combining either vari- ety of quick union with path compression. This modification makes every node encountered during the execution of a find operation point to the tree’s root (Fig- ure 9.9). According to a quite sophisticated analysis that goes beyond the level of this book (see [Tar84]), this and similar techniques improve the efficiency of a sequence of at most n − 1 unions and m finds to only slightly worse than linear. Exercises 9.2 1. Apply Kruskal’s algorithm to find a minimum spanning tree of the following graphs. a. b c a ed 1 34 62 56 332 Greedy Technique b. a d h c g k 4 4 4 5 5 5 b e i l 3 33 3 221 f j6 6 9 6 75 8 2. Indicate whether the following statements are true or false: a. If e is a minimum-weight edge in a connected weighted graph, it must be among edges of at least one minimum spanning tree of the graph. b. If e is a minimum-weight edge in a connected weighted graph, it must be among edges of each minimum spanning tree of the graph. c. If edge weights of a connected weighted graph are all distinct, the graph must have exactly one minimum spanning tree. d. If edge weights of a connected weighted graph are not all distinct, the graph must have more than one minimum spanning tree. 3. What changes, if any, need to be made in algorithm Kruskal to make it find a minimum spanning forest for an arbitrary graph? (A minimum spanning forest is a forest whose trees are minimum spanning trees of the graph’s connected components.) 4. Does Kruskal’s algorithm work correctly on graphs that have negative edge weights? 5. Design an algorithm for finding a maximum spanning tree—a spanning tree with the largest possible edge weight—of a weighted connected graph. 6. Rewrite pseudocode of Kruskal’s algorithm in terms of the operations of the disjoint subsets’ ADT. 7. Prove the correctness of Kruskal’s algorithm. 8. Prove that the time efficiency of find(x) is in O(log n) for the union-by-size version of quick union. 9. Find at least two Web sites with animations of Kruskal’s and Prim’s algorithms. Discuss their merits and demerits. 10. Design and conduct an experiment to empirically compare the efficiencies of Prim’s and Kruskal’s algorithms on random graphs of different sizes and densities. 9.3 Dijkstra’s Algorithm 333 11. Steiner tree Four villages are located at the vertices of a unit square in the Euclidean plane. You are asked to connect them by the shortest network of roads so that there is a path between every pair of the villages along those roads. Find such a network. 12. Write a program generating a random maze based on a. Prim’s algorithm. b. Kruskal’s algorithm. 9.3 Dijkstra’s Algorithm In this section, we consider the single-source shortest-paths problem: for a given vertex called the source in a weighted connected graph, find shortest paths to all its other vertices. It is important to stress that we are not interested here in a single shortest path that starts at the source and visits all the other vertices. This would have been a much more difficult problem (actually, a version of the traveling salesman problem introduced in Section 3.4 and discussed again later in the book). The single-source shortest-paths problem asks for a family of paths, each leading from the source to a different vertex in the graph, though some paths may, of course, have edges in common. A variety of practical applications of the shortest-paths problem have made the problem a very popular object of study. The obvious but probably most widely used applications are transportation planning and packet routing in communi- cation networks, including the Internet. Multitudes of less obvious applications include finding shortest paths in social networks, speech recognition, document formatting, robotics, compilers, and airline crew scheduling. In the world of enter- tainment, one can mention pathfinding in video games and finding best solutions to puzzles using their state-space graphs (see Section 6.6 for a very simple example of the latter). There are several well-known algorithms for finding shortest paths, including Floyd’s algorithm for the more general all-pairs shortest-paths problem discussed in Chapter 8. Here, we consider the best-known algorithm for the single-source shortest-paths problem, called Dijkstra’s algorithm.4 This algorithm is applicable to undirected and directed graphs with nonnegative weights only. Since in most ap- plications this condition is satisfied, the limitation has not impaired the popularity of Dijkstra’s algorithm. Dijkstra’s algorithm finds the shortest paths to a graph’s vertices in order of their distance from a given source. First, it finds the shortest path from the source 4. Edsger W. Dijkstra (1930–2002), a noted Dutch pioneer of the science and industry of computing, discovered this algorithm in the mid-1950s. Dijkstra said about his algorithm: “This was the first graph problem I ever posed myself and solved. The amazing thing was that I didn’t publish it. It was not amazing at the time. At the time, algorithms were hardly considered a scientific topic.” 334 Greedy Technique u* v* v0 FIGURE 9.10 Idea of Dijkstra’s algorithm. The subtree of the shortest paths already found is shown in bold. The next nearest to the source v0 vertex, u∗, is selected by comparing the lengths of the subtree’s paths increased by the distances to vertices adjacent to the subtree’s vertices. to a vertex nearest to it, then to a second nearest, and so on. In general, before its ith iteration commences, the algorithm has already identified the shortest paths to i − 1 other vertices nearest to the source. These vertices, the source, and the edges of the shortest paths leading to them from the source form a subtree Ti of the given graph (Figure 9.10). Since all the edge weights are nonnegative, the next vertex nearest to the source can be found among the vertices adjacent to the vertices of Ti. The set of vertices adjacent to the vertices in Ti can be referred to as “fringe vertices”; they are the candidates from which Dijkstra’s algorithm selects the next vertex nearest to the source. (Actually, all the other vertices can be treated as fringe vertices connected to tree vertices by edges of infinitely large weights.) To identify the ith nearest vertex, the algorithm computes, for every fringe vertex u, the sum of the distance to the nearest tree vertex v (given by the weight of the edge (v, u)) and the length dv of the shortest path from the source to v (previously determined by the algorithm) and then selects the vertex with the smallest such sum. The fact that it suffices to compare the lengths of such special paths is the central insight of Dijkstra’s algorithm. To facilitate the algorithm’s operations, we label each vertex with two labels. The numeric label d indicates the length of the shortest path from the source to this vertex found by the algorithm so far; when a vertex is added to the tree, d indicates the length of the shortest path from the source to that vertex. The other label indicates the name of the next-to-last vertex on such a path, i.e., the parent of the vertex in the tree being constructed. (It can be left unspecified for the source s and vertices that are adjacent to none of the current tree vertices.) With such labeling, finding the next nearest vertex u∗ becomes a simple task of finding a fringe vertex with the smallest d value. Ties can be broken arbitrarily. After we have identified a vertex u∗ to be added to the tree, we need to perform two operations: 9.3 Dijkstra’s Algorithm 335 Move u∗ from the fringe to the set of tree vertices. For each remaining fringe vertex u that is connected to u∗ by an edge of weight w(u∗,u)such that du∗ + w(u∗,u) 1 n > 3 n >2 no no yes no yes no yes yes n = 3 n = 3 n = 4 n = 4 n = 1 n = 2 n = 2 no no yes yes FIGURE 9.13 Two decision trees for guessing an integer between 1 and 4. n i=1 lipi, where li is the length of the path from the root to the ith leaf, indicates the average number of questions needed to “guess” the chosen number with a game strategy represented by its decision tree. If each of the numbers is chosen with the same probability of 1/n, the best strategy is to successively eliminate half (or almost half) the candidates as binary search does. This may not be the case for arbitrary pi’s, however. For example, if n = 4 and p1 = 0.1,p2 = 0.2,p3 = 0.3, and p4 = 0.4, the minimum weighted path tree is the rightmost one in Figure 9.13. Thus, we need Huffman’s algorithm to solve this problem in its general case. Note that this is the second time we are encountering the problem of con- structing an optimal binary tree. In Section 8.3, we discussed the problem of constructing an optimal binary search tree with positive numbers (the search prob- abilities) assigned to every node of the tree. In this section, given numbers are assigned just to leaves. The latter problem turns out to be easier: it can be solved by the greedy algorithm, whereas the former is solved by the more complicated dynamic programming algorithm. Exercises 9.4 1. a. Construct a Huffman code for the following data: symbol ABC D _ frequency 0.4 0.1 0.2 0.15 0.15 b. Encode ABACABAD using the code of question (a). c. Decode 100010111001010 using the code of question (a). 2. For data transmission purposes, it is often desirable to have a code with a minimum variance of the codeword lengths (among codes of the same average length). Compute the average and variance of the codeword length in two 9.4 Huffman Trees and Codes 343 Huffman codes that result from a different tie breaking during a Huffman code construction for the following data: symbol ABCDE probability 0.1 0.1 0.2 0.2 0.4 3. Indicate whether each of the following properties is true for every Huffman code. a. The codewords of the two least frequent symbols have the same length. b. The codeword’s length of a more frequent symbol is always smaller than or equal to the codeword’s length of a less frequent one. 4. What is the maximal length of a codeword possible in a Huffman encoding of an alphabet of n symbols? 5. a. Write pseudocode of the Huffman-tree construction algorithm. b. What is the time efficiency class of the algorithm for constructing a Huff- man tree as a function of the alphabet size? 6. Show that a Huffman tree can be constructed in linear time if the alphabet symbols are given in a sorted order of their frequencies. 7. Given a Huffman coding tree, which algorithm would you use to get the codewords for all the symbols? What is its time-efficiency class as a function of the alphabet size? 8. Explain how one can generate a Huffman code without an explicit generation of a Huffman coding tree. 9. a. Write a program that constructs a Huffman code for a given English text and encode it. b. Write a program for decoding of an English text which has been encoded with a Huffman code. c. Experiment with your encoding program to find a range of typical compres- sion ratios for Huffman’s encoding of English texts of, say, 1000 words. d. Experiment with your encoding program to find out how sensitive the compression ratios are to using standard estimates of frequencies instead of actual frequencies of symbol occurrences in English texts. 10. Card guessing Design a strategy that minimizes the expected number of questions asked in the following game [Gar94]. You have a deck of cards that consists of one ace of spades, two deuces of spades, three threes, and on up to nine nines, making 45 cards in all. Someone draws a card from the shuffled deck, which you have to identify by asking questions answerable with yes or no. 344 Greedy Technique SUMMARY The greedy technique suggests constructing a solution to an optimization problem through a sequence of steps, each expanding a partially constructed solution obtained so far, until a complete solution to the problem is reached. On each step, the choice made must be feasible, locally optimal, and irrevocable. Prim’s algorithm is a greedy algorithm for constructing a minimum spanning tree of a weighted connected graph. It works by attaching to a previously constructed subtree a vertex closest to the vertices already in the tree. Kruskal’s algorithm is another greedy algorithm for the minimum spanning tree problem. It constructs a minimum spanning tree by selecting edges in nondecreasing order of their weights provided that the inclusion does not create a cycle. Checking the latter condition efficiently requires an application of one of the so-called union-find algorithms. Dijkstra’s algorithm solves the single-source shortest-path problem of finding shortest paths from a given vertex (the source) to all the other vertices of a weighted graph or digraph. It works as Prim’s algorithm but compares path lengths rather than edge lengths. Dijkstra’s algorithm always yields a correct solution for a graph with nonnegative weights. A Huffman tree is a binary tree that minimizes the weighted path length from the root to the leaves of predefined weights. The most important application of Huffman trees is Huffman codes. A Huffman code is an optimal prefix-free variable-length encoding scheme that assigns bit strings to symbols based on their frequencies in a given text. This is accomplished by a greedy construction of a binary tree whose leaves represent the alphabet symbols and whose edges are labeled with 0’s and 1’s. 10 Iterative Improvement The most successful men in the end are those whose success is the result of steady accretion. —Alexander Graham Bell (1835–1910) The greedy strategy, considered in the preceding chapter, constructs a solution to an optimization problem piece by piece, always adding a locally optimal piece to a partially constructed solution. In this chapter, we discuss a different approach to designing algorithms for optimization problems. It starts with some feasible solution (a solution that satisfies all the constraints of the problem) and proceeds to improve it by repeated applications of some simple step. This step typically involves a small, localized change yielding a feasible solution with an improved value of the objective function. When no such change improves the value of the objective function, the algorithm returns the last feasible solution as optimal and stops. There can be several obstacles to the successful implementation of this idea. First, we need an initial feasible solution. For some problems, we can always start with a trivial solution or use an approximate solution obtained by some other (e.g., greedy) algorithm. But for others, finding an initial solution may require as much effort as solving the problem after a feasible solution has been identified. Second, it is not always clear what changes should be allowed in a feasible solution so that we can check efficiently whether the current solution is locally optimal and, if not, replace it with a better one. Third—and this is the most fundamental difficulty— is an issue of local versus global extremum (maximum or minimum). Think about the problem of finding the highest point in a hilly area with no map on a foggy day. A logical thing to do would be to start walking “up the hill” from the point you are at until it becomes impossible to do so because no direction would lead up. You will have reached a local highest point, but because of a limited feasibility, there will be no simple way to tell whether the point is the highest (global maximum you are after) in the entire area. Fortunately, there are important problems that can be solved by iterative- improvement algorithms. The most important of them is linear programming. 345 346 Iterative Improvement We have already encountered this topic in Section 6.6. Here, in Section 10.1, we introduce the simplex method, the classic algorithm for linear programming. Discovered by the U.S. mathematician George B. Dantzig in 1947, this algorithm has proved to be one of the most consequential achievements in the history of algorithmics. In Section 10.2, we consider the important problem of maximizing the amount of flow that can be sent through a network with links of limited capacities. This problem is a special case of linear programming. However, its special structure makes it possible to solve the problem by algorithms that are more efficient than the simplex method. We outline the classic iterative-improvement algorithm for this problem, discovered by the American mathematicians L. R. Ford, Jr., and D. R. Fulkerson in the 1950s. The last two sections of the chapter deal with bipartite matching. This is the problem of finding an optimal pairing of elements taken from two disjoint sets. Examples include matching workers and jobs, high school graduates and colleges, and men and women for marriage. Section 10.3 deals with the problem of maximizing the number of matched pairs; Section 10.4 is concerned with the matching stability. We also discuss several iterative-improvement algorithms in Section 12.3, where we consider approximation algorithms for the traveling salesman and knap- sack problems. Other examples of iterative-improvement algorithms can be found in the algorithms textbook by Moret and Shapiro [Mor91], books on continuous and discrete optimization (e.g., [Nem89]), and the literature on heuristic search (e.g., [Mic10]). 10.1 The Simplex Method We have already encountered linear programming (see Section 6.6)—the general problem of optimizing a linear function of several variables subject to a set of linear constraints: maximize (or minimize) c1x1 + ...+ cnxn subject to ai1x1 + ...+ ainxn ≤ (or ≥ or =) bi for i = 1,...,m x1 ≥ 0,...,xn ≥ 0. (10.1) We mentioned there that many important practical problems can be modeled as instances of linear programming. Tworesearchers, L. V.Kantorovich of the former Soviet Union and the Dutch-American T. C. Koopmans, were even awarded the Nobel Prize in 1975 for their contributions to linear programming theory and its applications to economics. Apparently because there is no Nobel Prize in mathematics, the Royal Swedish Academy of Sciences failed to honor the U.S. mathematician G. B. Dantzig, who is universally recognized as the father of linear 10.1 The Simplex Method 347 programming in its modern form and the inventor of the simplex method, the classic algorithm for solving such problems.1 Geometric Interpretation of Linear Programming Before we introduce a general method for solving linear programming problems, let us consider a small example, which will help us to see the fundamental prop- erties of such problems. EXAMPLE 1 Consider the following linear programming problem in two vari- ables: maximize 3x + 5y subject to x + y ≤ 4 x + 3y ≤ 6 x ≥ 0,y≥ 0. (10.2) By definition, a feasible solution to this problem is any point (x, y) that satisfies all the constraints of the problem; the problem’s feasible region is the set of all its feasible points. It is instructive to sketch the feasible region in the Cartesian plane. Recall that any equation ax + by = c, where coefficients a and b are not both equal to zero, defines a straight line. Such a line divides the plane into two half-planes: for all the points in one of them, ax + by < c, while for all the points in the other, ax + by > c. (It is easy to determine which of the two half-planes is which: take any point (x0,y0) not on the line ax + by = c and check which of the two inequalities hold, ax0 + by0 >cor ax0 + by0 0 for j = 1, 2,...,n. 8. Can we apply the simplex method to solve the knapsack problem (see Exam- ple 2 in Section 6.6)? If you answer yes, indicate whether it is a good algorithm for the problem in question; if you answer no, explain why not. 9. Prove that no linear programming problem can have exactly k ≥ 1 optimal solutions unless k = 1. 10. If a linear programming problem maximize n j=1 cjxj subject to n j=1 aij xj ≤ bi for i = 1, 2,...,m x1,x2,...,xn ≥ 0 is considered as primal, then its dual is defined as the linear programming problem minimize m i=1 biyi subject to m i=1 aij yi ≥ cj for j = 1, 2,...,n y1,y2,...,ym ≥ 0. 10.2 The Maximum-Flow Problem 361 a. Express the primal and dual problems in matrix notations. b. Find the dual of the linear programming problem maximize x1 + 4x2 − x3 subject to x1 + x2 + x3 ≤ 6 x1 − x2 − 2x3 ≤ 2 x1,x2,x3 ≥ 0. c. Solve the primal and dual problems and compare the optimal values of their objective functions. 10.2 The Maximum-Flow Problem In this section, we consider the important problem of maximizing the flow of a ma- terial through a transportation network (pipeline system, communication system, electrical distribution system, and so on). We will assume that the transportation network in question can be represented by a connected weighted digraph with n vertices numbered from 1 to n and a set of edges E, with the following properties: It contains exactly one vertex with no entering edges; this vertex is called the source and assumed to be numbered 1. It contains exactly one vertex with no leaving edges; this vertex is called the sink and assumed to be numbered n. The weight uij of each directed edge (i, j) is a positive integer, called the edge capacity. (This number represents the upper bound on the amount of the material that can be sent from i to j through a link represented by this edge.) A digraph satisfying these properties is called a flow network or simply a network.3 A small instance of a network is given in Figure 10.4. It is assumed that the source and the sink are the only source and destination of the material, respectively; all the other vertices can serve only as points where a flow can be redirected without consuming or adding any amount of the material. In other words, the total amount of the material entering an intermediate vertex must be equal to the total amount of the material leaving the vertex. This con- dition is called the flow-conservation requirement. If we denote the amount sent through edge (i, j) by xij , then for any intermediate vertex i,the flow-conservation requirement can be expressed by the following equality constraint: j: (j,i)∈E xji = j: (i,j)∈E xij for i = 2, 3,...,n− 1, (10.8) 3. In a slightly more general model, one can consider a network with several sources and sinks and allow capacities uij to be infinitely large. 362 Iterative Improvement 12 4 2 31 3 5 5 34 62 FIGURE 10.4 Example of a network graph. The vertex numbers are vertex “names”; the edge numbers are edge capacities. where the sums in the left- and right-hand sides express the total inflow and outflow entering and leaving vertex i, respectively. Since no amount of the material can change by going through intermediate vertices of the network, it stands to reason that the total amount of the material leaving the source must end up at the sink. (This observation can also be derived formally from equalities (10.8), a task you will be asked to do in the exercises.) Thus, we have the following equality: j: (1,j)∈E x1j = j: (j,n)∈E xjn. (10.9) This quantity, the total outflow from the source—or, equivalently, the total inflow into the sink—is called the value of the flow. We denote it by v. It is this quantity that we will want to maximize over all possible flows in a network. Thus, a (feasible) flow is an assignment of real numbers xij to edges (i, j) of a given network that satisfy flow-conservation constraints (10.8) and the capacity constraints 0 ≤ xij ≤ uij for every edge (i, j) ∈ E. (10.10) The maximum-flow problem can be stated formally as the following optimization problem: maximize v = j: (1,j)∈E x1j subject to j: (j,i)∈E xji − j: (i,j)∈E xij = 0 for i = 2, 3,...,n− 1 0 ≤ xij ≤ uij for every edge (i, j) ∈ E. (10.11) We can solve linear programming problem (10.11) by the simplex method or by another algorithm for general linear programming problems (see Section 10.1). However, the special structure of problem (10.11) can be exploited to design faster algorithms. In particular, it is quite natural to employ the iterative-improvement 10.2 The Maximum-Flow Problem 363 idea as follows. We can always start with the zero flow (i.e., set xij = 0 for every edge (i, j) in the network). Then, on each iteration, we can try to find a path from source to sink along which some additional flow can be sent. Such a path is called flow augmenting. If a flow-augmenting path is found, we adjust the flow along the edges of this path to get a flow of an increased value and try to find an augmenting path for the new flow. If no flow-augmenting path can be found, we conclude that the current flow is optimal. This general template for solving the maximum-flow problem is called the augmenting-path method, also known as the Ford-Fulkerson method after L. R. Ford, Jr., and D. R. Fulkerson, who discovered it (see [For57]). An actual implementation of the augmenting path idea is, however, not quite straightforward. To see this, let us consider the network in Figure 10.4. We start with the zero flow shown in Figure 10.5a. (In that figure, the zero amounts sent through each edge are separated from the edge capacities by the slashes; we will use this notation in the other examples as well.) It is natural to search for a flow- augmenting path from source to sink by following directed edges (i, j) for which the current flow xij is less than the edge capacity uij . Among several possibilities, let us assume that we identify the augmenting path 1→2→3→6 first. We can increase the flow along this path by a maximum of 2 units, which is the smallest unused capacity of its edges. The new flow is shown in Figure 10.5b. This is as far as our simpleminded idea about flow-augmenting paths will be able to take us. Unfortunately, the flow shown in Figure 10.5b is not optimal: its value can still be increased along the path 1→4→3←2→5→6 by increasing the flow by 1 on edges (1, 4), (4, 3), (2, 5), and (5, 6) and decreasing it by 1 on edge (2, 3). The flow obtained as the result of this augmentation is shown in Figure 10.5c. It is indeed maximal. (Can you tell why?) Thus, to find a flow-augmenting path for a flow x, we need to consider paths from source to sink in the underlying undirected graph in which any two consec- utive vertices i, j are either i. connected by a directed edge from i to j with some positive unused capacity rij = uij − xij (so that we can increase the flow through that edge by up to rij units), or ii. connected by a directed edge from j to i with some positive flow xji (so that we can decrease the flow through that edge by up to xji units). Edges of the first kind are called forward edges because their tail is listed before their head in the vertex list 1 → ...i → j ...→ n defining the path; edges of the second kind are called backward edges because their tail is listed after their head in the path list 1 → ...i ← j ...→ n. To illustrate, for the path 1→4→3←2→5→6 of the last example, (1, 4), (4, 3), (2, 5), and (5, 6) are the forward edges, and (3, 2) is the backward edge. For a given flow-augmenting path, let r be the minimum of all the unused capacities rij of its forward edges and all the flows xji of its backward edges. It is easy to see that if we increase the current flow by r on each forward edge and decrease it by this amount on each backward edge, we will obtain a feasible 364 Iterative Improvement 1 2 4 0/2 0/3 0/1 3 5 0/5 (a) 0/3 0/4 60/2 1 2 4 2/2 0/3 0/1 3 5 2/5 0/3 0/4 62/2 (b) 1 2 4 2/2 1/3 1/1 3 5 1/5 1/3 1/4 62/2 (c) FIGURE 10.5 Illustration of the augmenting-path method. Flow-augmenting paths are shown in bold. The flow amounts and edge capacities are indicated by the numbers before and after the slash, respectively. flow whose value is r units greater than the value of its predecessor. Indeed, let i be an intermediate vertex on a flow-augmenting path. There are four possible combinations of forward and backward edges incident to vertex i: +r−→ i +r−→ , +r−→ i −r←− , −r←− i +r−→ , −r←− i −r←− . 10.2 The Maximum-Flow Problem 365 For each of them, the flow-conservation requirement for vertex i will still hold after the flow adjustments indicated above the edge arrows. Further, since r is the minimum among all the positive unused capacities on the forward edges and all the positive flows on the backward edges of the flow-augmenting path, the new flow will satisfy the capacity constraints as well. Finally, adding r to the flow on the first edge of the augmenting path will increase the value of the flow by r. Under the assumption that all the edge capacities are integers, r will be a positive integer too. Hence, the flow value increases at least by 1 on each iteration of the augmenting-path method. Since the value of a maximum flow is bounded above (e.g., by the sum of the capacities of the source edges), the augmenting-path method has to stop after a finite number of iterations.4 Surprisingly, the final flow always turns out to be maximal, irrespective of a sequence of augmenting paths. This remarkable result stems from the proof of the Max-Flow Min-Cut Theorem (see, e.g., [For62]), which we replicate later in this section. The augmenting-path method—as described above in its general form—does not indicate a specific way for generating flow-augmenting paths. A bad sequence of such paths may, however, have a dramatic impact on the method’s efficiency. Consider, for example, the network in Figure 10.6a, in which U stands for some large positive integer. If we augment the zero flow along the path 1→2→3→4, we shall obtain the flow of value 1 shown in Figure 10.6b. Augmenting that flow along the path 1→3←2→4 will increase the flow value to 2 (Figure 10.6c). If we continue selecting this pair of flow-augmenting paths, we will need a total of 2U iterations to reach the maximum flow of value 2U (Figure 10.6d). Of course, we can obtain the maximum flow in just two iterations by augmenting the initial zero flow along the path 1→2→4 followed by augmenting the new flow along the path 1→3→4. The dramatic difference between 2U and 2 iterations makes the point. Fortunately, there are several ways to generate flow-augmenting paths ef- ficiently and avoid the degradation in performance illustrated by the previous example. The simplest of them uses breadth-first search to generate augment- ing paths with the least number of edges (see Section 3.5). This version of the augmenting-path method, called shortest-augmenting-path or first-labeled-first- scanned algorithm, was suggested by J. Edmonds and R. M. Karp [Edm72]. The labeling refers to marking a new (unlabeled) vertex with two labels. The first label indicates the amount of additional flow that can be brought from the source to the vertex being labeled. The second label is the name of the vertex from which the vertex being labeled was reached. (It can be left undefined for the source.) It is also convenient to add the + or − sign to the second label to indicate whether the vertex was reached via a forward or backward edge, respectively. The source can be always labeled with ∞, −. For the other vertices, the labels are computed as follows. 4. If capacity upper bounds are irrational numbers, the augmenting-path method may not terminate (see, e.g., [Chv83, pp. 387–388], for a cleverly devised example demonstrating such a situation). This limitation is only of theoretical interest because we cannot store irrational numbers in a computer, and rational numbers can be transformed into integers by changing the capacity measurement unit. 366 Iterative Improvement 1 3 (a) 2 0/U 0/U 0/1 0/U 0/U 4 1 3 2 1/U 0/U 1/1 0/U 1/U 4 (b) 1 3 2 1/U 1/U 0/1 1/U 1/U 4 (c) 1 3 2 U/UU/U 0/1 U/UU/U 4 (d) FIGURE 10.6 Efficiency degradation of the augmenting-path method. If unlabeled vertex j is connected to the front vertex i of the traversal queue by a directed edge from i to j with positive unused capacity rij = uij − xij , then vertex j is labeled with lj,i+, where lj = min{li,rij }. If unlabeled vertex j is connected to the front vertex i of the traversal queue by a directed edge from j to i with positive flow xji, then vertex j is labeled with lj,i−, where lj = min{li,xji}. If this labeling-enhanced traversal ends up labeling the sink, the current flow can be augmented by the amount indicated by the sink’s first label. The augmentation is performed along the augmenting path traced by following the vertex second labels from sink to source: the current flow quantities are increased on the forward edges and decreased on the backward edges of this path. If, on the other hand, the sink remains unlabeled after the traversal queue becomes empty, the algorithm returns the current flow as maximum and stops. ALGORITHM ShortestAugmentingPath(G) //Implements the shortest-augmenting-path algorithm //Input: A network with single source 1, single sink n, and // positive integer capacities uij on its edges (i, j) //Output: A maximum flow x assign xij = 0 to every edge (i, j) in the network label the source with ∞, − and add the source to the empty queue Q 10.2 The Maximum-Flow Problem 367 while not Empty(Q) do i ← Front(Q); Dequeue(Q) for every edge from i to j do //forward edges if j is unlabeled rij ← uij − xij if rij > 0 lj ← min{li,rij }; label j with lj,i+ Enqueue(Q, j) for every edge from j to i do //backward edges if j is unlabeled if xji > 0 lj ← min{li,xji}; label j with lj,i− Enqueue(Q, j) if the sink has been labeled //augment along the augmenting path found j ← n //start at the sink and move backwards using second labels while j = 1 //the source hasn’t been reached if the second label of vertex j is i+ xij ← xij + ln else //the second label of vertex j is i− xji ← xji − ln j ← i; i ← the vertex indicated by i’s second label erase all vertex labels except the ones of the source reinitialize Q with the source return x //the current flow is maximum An application of this algorithm to the network in Figure 10.4 is illustrated in Figure 10.7. The optimality of a final flow obtained by the augmenting-path method stems from a theorem that relates network flows to network cuts. A cut induced by partitioning vertices of a network into some subset X containing the source and¯X, the complement of X, containing the sink is the set of all the edges with a tail in X and a head in ¯X. We denote a cut C(X, ¯X) or simply C. For example, for the network in Figure 10.4: if X ={1} and hence ¯X ={2, 3, 4, 5, 6}, C(X, ¯X) ={(1, 2), (1, 4)}; if X ={1, 2, 3, 4, 5} and hence ¯X ={6}, C(X, ¯X) ={(3, 6), (5, 6)}; if X ={1, 2, 4} and hence ¯X ={3, 5, 6}, C(X, ¯X) ={(2, 3), (2, 5), (4, 3)}. The name “cut” stems from the following property: if all the edges of a cut were deleted from the network, there would be no directed path from source to sink. Indeed, let C(X, ¯X) be a cut. Consider a directed path from source to sink. If vi is the first vertex of that path which belongs to ¯X (the set of such vertices is not 368 Iterative Improvement 1 2 4 3 5 6 ↑↑ ↑↑ 0/3 0/2 0/3 0/1 0/5 0/2 0/4 Queue: 1 2 4 3 5 6 1 2 4 3, 1+ 3 2, 2+ 5 2, 1+ 2, 2+ 0/5 6 2, 3+∞, – 0/20/2 0/3 0/1 0/40/3 Augment the flow by 2 (the sink’s first label) along the path 1 → 2 → 3 → 6. 1 2 4 3 5 6 ↑↑↑↑↑ 0/3 2/2 0/3 0/1 2/5 2/2 0/4 Queue: 1 4 3 2 5 6 1 2 4 3, 1+ 3 1, 4+ 5 1, 2+ 6 1, 5+∞, – Augment the flow by 1 (the sink’s first label) along the path 1 → 4 → 3 ← 2 → 5 → 6. 1, 3–2/2 2/22/5 0/3 0/4 0/3 0/1 1 2 4 3 5 6 ↑↑ 1/3 1/5 1/4 1/11/3 2/22/2 Queue: 1 4 1 2 4 2, 1+ 3 5 6∞, – No augmenting path (the sink is unlabeled); the current flow is maximal. 1/11/3 1/3 1/4 1/52/2 2/2 FIGURE 10.7 Illustration of the shortest-augmenting-path algorithm. The diagrams on the left show the current flow before the next iteration begins; the diagrams on the right show the results of the vertex labeling on that iteration, the augmenting path found (in bold), and the flow before its augmentation. Vertices deleted from the queue are indicated by the ↑ symbol. 10.2 The Maximum-Flow Problem 369 empty, because it contains the sink), then vi is not the source and its immediate predecessor vi−1 on that path belongs to X. Hence, the edge from vi−1 to vi must be an element of the cut C(X, ¯X). This proves the property in question. The capacity of a cut C(X, ¯X), denoted c(X, ¯X), is defined as the sum of capacities of the edges that compose the cut. For the three examples of cuts given above, the capacities are equal to 5, 6, and 9, respectively. Since the number of different cuts in a network is nonempty and finite (why?), there always exists a minimum cut, i.e., a cut with the smallest capacity. (What is a minimum cut in the network of Figure 10.4?) The following theorem establishes an important relationship between the notions of maximum flow and minimum cut. THEOREM (Max-Flow Min-Cut Theorem) The value of a maximum flow in a network is equal to the capacity of its minimum cut. PROOF First, let x be a feasible flow of value v and let C(X, ¯X) be a cut of capacity c in the same network. Consider the flow across this cut defined as the difference between the sum of the flows on the edges from X to ¯X and the sum of the flows on the edges from ¯X to X. It is intuitively clear and can be formally derived from the equations expressing the flow-conservation requirement and the definition of the flow value (Problem 6b in this section’s exercises) that the flow across the cut C(X, ¯X) is equal to v, the value of the flow: v = i∈X, j∈ ¯X xij − j∈ ¯X, i∈X xji. (10.12) Since the second sum is nonnegative and the flow xij on any edge (i, j) cannot exceed the edge capacity uij , equality (10.12) implies that v ≤ i∈X, j∈ ¯X xij ≤ i∈X, j∈ ¯X uij , i.e., v ≤ c. (10.13) Thus, the value of any feasible flow in a network cannot exceed the capacity of any cut in that network. Let v∗ be the value of a final flow x∗ obtained by the augmenting-path method. If we now find a cut whose capacity is equal to v∗, we will have to conclude, in view of inequality (10.13), that (i) the value v∗ of the final flow is maximal among all feasible flows, (ii) the cut’s capacity is minimal among all cuts in the network, and (iii) the maximum-flow value is equal to the minimum-cut capacity. To find such a cut, consider the set of vertices X∗ that can be reached from the source by following an undirected path composed of forward edges with positive unused capacities (with respect to the final flow x∗) and backward edges with positive flows on them. This set contains the source but does not contain the sink: if it did, we would have an augmenting path for the flow x∗, which would 370 Iterative Improvement contradict the assumption that the flow x∗ is final. Consider the cut C(X∗, X∗). By the definition of set X∗, each edge (i, j) from X∗ to X∗ has zero unused capacity, i.e., x∗ ij = uij , and each edge (j, i) from X∗ to X∗ has the zero flow on it (otherwise, j would be in X∗). Applying equality (10.12) to the final flow x∗ and the set X∗ defined above, we obtain v∗ = i∈X∗,j∈X∗ x∗ ij − j∈X∗,i∈X∗ x∗ ji = i∈X∗,j∈X∗ uij − 0 = c(X∗, X∗), which proves the theorem. The proof outlined above accomplishes more than proving the equality of the maximum-flow value and the minimum-cut capacity. It also implies that when the augmenting-path method terminates, it yields both a maximum flow and a mini- mum cut. If labeling of the kind utilized in the shortest-augmenting-path algorithm is used, a minimum cut is formed by the edges from the labeled to unlabeled ver- tices on the last iteration of the method. Finally, the proof implies that all such edges must be full (i.e., the flows must be equal to the edge capacities), and all the edges from unlabeled vertices to labeled, if any, must be empty (i.e., have zero flows on them). In particular, for the network in Figure 10.7, the algorithm finds the cut {(1, 2), (4, 3)} of minimum capacity 3, both edges of which are full as required. Edmonds and Karp proved in their paper [Edm72] that the number of aug- menting paths needed by the shortest-augmenting-path algorithm never exceeds nm/2, where n and m are the number of vertices and edges, respectively. Since the time required to find a shortest augmenting path by breadth-first search is in O(n + m) = O(m) for networks represented by their adjacency lists, the time efficiency of the shortest-augmenting-path algorithm is in O(nm2). More efficient algorithms for the maximum-flow problem are known (see the monograph [Ahu93], as well as appropriate chapters in such books as [Cor09] and [Kle06]). Some of them implement the augmenting-path idea in a more efficient manner. Others are based on the concept of preflows. A preflow is a flow that satisfies the capacity constraints but not the flow-conservation requirement. Any vertex is allowed to have more flow entering the vertex than leaving it. A preflow- push algorithm moves the excess flow toward the sink until the flow-conservation requirement is reestablished for all intermediate vertices of the network. Faster al- gorithms of this kind have worst-case efficiency close to O(nm). Note that preflow- push algorithms fall outside the iterative-improvement paradigm because they do not generate a sequence of improving solutions that satisfy all the constraints of the problem. To conclude this section, it is worth pointing out that although the initial interest in studying network flows was caused by transportation applications, this model has also proved to be useful for many other areas. We discuss one of them in the next section. 10.2 The Maximum-Flow Problem 371 Exercises 10.2 1. Since maximum-flow algorithms require processing edges in both directions, it is convenient to modify the adjacency matrix representation of a network as follows. If there is a directed edge from vertex i to vertex j of capacity uij , then the element in the ith row and the jth column is set to uij , and the element in the jth row and the ith column is set to −uij ; if there is no edge between vertices i and j, both these elements are set to zero. Outline a simple algorithm for identifying a source and a sink in a network presented by such a matrix and indicate its time efficiency. 2. Apply the shortest-augmenting path algorithm to find a maximum flow and a minimum cut in the following networks. a. 1255 7 2 8 3 644 46 b. 2 16 43 2 7 1 5 44 2 35 3. a. Does the maximum-flow problem always have a unique solution? Would your answer be different for networks with different capacities on all their edges? b. Answer the same questions for the minimum-cut problem of finding a cut of the smallest capacity in a given network. 4. a. Explain how the maximum-flow problem for a network with several sources and sinks can be transformed into the same problem for a network with a single source and a single sink. b. Some networks have capacity constraints on the flow amounts that can flow through their intermediate vertices. Explain how the maximum-flow problem for such a network can be transformed to the maximum-flow problem for a network with edge capacity constraints only. 5. Consider a network that is a rooted tree, with the root as its source, the leaves as its sinks, and all the edges directed along the paths from the root to the leaves. Design an efficient algorithm for finding a maximum flow in such a network. What is the time efficiency of your algorithm? 6. a. Prove equality (10.9). 372 Iterative Improvement b. Prove that for any flow in a network and any cut in it, the value of the flow is equal to the flow across the cut (see equality (10.12)). Explain the relationship between this property and equality (10.9). 7. a. Express the maximum-flow problem for the network in Figure 10.4 as a linear programming problem. b. Solve this linear programming problem by the simplex method. 8. As an alternative to the shortest-augmenting-path algorithm, Edmonds and Karp [Edm72] suggested the maximum-capacity-augmenting-path algorithm, in which a flow is augmented along the path that increases the flow by the largest amount. Implement both these algorithms in the language of your choice and perform an empirical investigation of their relative efficiency. 9. Write a report on a more advanced maximum-flow algorithm such as (i) Dinitz’s algorithm, (ii) Karzanov’s algorithm, (iii) Malhotra-Kamar- Maheshwari algorithm, or (iv) Goldberg-Tarjan algorithm. 10. Dining problem Several families go out to dinner together. To increase their social interaction, they would like to sit at tables so that no two members of the same family are at the same table. Show how to find a seating arrangement that meets this objective (or prove that no such arrangement exists) by using a maximum-flow problem. Assume that the dinner contingent has p families and that the ith family has ai members. Also assume that q tables are available and the jth table has a seating capacity of bj. [Ahu93] 10.3 Maximum Matching in Bipartite Graphs In many situations we are faced with a problem of pairing elements of two sets. The traditional example is boys and girls for a dance, but you can easily think of more serious applications. It is convenient to represent elements of two given sets by vertices of a graph, with edges between vertices that can be paired. A matching in a graph is a subset of its edges with the property that no two edges share a vertex. A maximum matching—more precisely, a maximum cardinality matching—is a matching with the largest number of edges. (What is it for the graph in Figure 10.8? Is it unique?) The maximum-matching problem is the problem of finding a maximum matching in a given graph. For an arbitrary graph, this is a rather difficult problem. It was solved in 1965 by Jack Edmonds [Edm65]. (See [Gal86] for a good survey and more recent references.) We limit our discussion in this section to the simpler case of bipartite graphs. In a bipartite graph, all the vertices can be partitioned into two disjoint sets V and U, not necessarily of the same size, so that every edge connects a vertex in one of these sets to a vertex in the other set. In other words, a graph is bipartite if its vertices can be colored in two colors so that every edge has its vertices colored in different colors; such graphs are also said to be 2-colorable. The graph in Figure 10.8 is bipartite. It is not difficult to prove that a graph is bipartite if and only if it does not have a cycle of an odd length. We will assume for the rest of this section that 10.3 Maximum Matching in Bipartite Graphs 373 1234 5 V U 678 FIGURE 10.8 Example of a bipartite graph. the vertex set of a given bipartite graph has been already partitioned into sets V and U as required by the definition (see Problem 8 in Exercises 3.5). Let us apply the iterative-improvement technique to the maximum- cardinality-matching problem. Let M be a matching in a bipartite graph G = V,U,E . How can we improve it, i.e., find a new matching with more edges? Obviously, if every vertex in either V or U is matched (has a mate), i.e., serves as an endpoint of an edge in M, this cannot be done and M is a maximum matching. Therefore, to have a chance at improving the current matching, both V and U must contain unmatched (also called free) vertices, i.e., vertices that are not inci- dent to any edge in M. For example, for the matching Ma ={(4, 8), (5, 9)} in the graph in Figure 10.9a, vertices 1, 2, 3, 6, 7, and 10 are free, and vertices 4, 5, 8, and 9 are matched. Another obvious observation is that we can immediately increase a current matching by adding an edge between two free vertices. For example, adding (1, 6) to the matching Ma ={(4, 8), (5, 9)} in the graph in Figure 10.9a yields a larger matching Mb ={(1, 6), (4, 8), (5, 9)} (Figure 10.9b). Let us now try to find a matching larger than Mb by matching vertex 2. The only way to do this would be to include the edge (2, 6) in a new matching. This inclusion requires removal of (1, 6), which can be compensated by inclusion of (1, 7) in the new matching. This new matching Mc ={(1, 7), (2, 6), (4, 8), (5, 9)} is shown in Figure 10.9c. In general, we increase the size of a current matching M by constructing a simple path from a free vertex in V to a free vertex in U whose edges are alternately in E − M and in M. That is, the first edge of the path does not belong to M, the second one does, and so on, until the last edge that does not belong to M. Such a path is called augmenting with respect to the matching M. For example, the path 2, 6, 1, 7 is an augmenting path with respect to the matching Mb in Figure 10.9b. Since the length of an augmenting path is always odd, adding to the matching M the path’s edges in the odd-numbered positions and deleting from it the path’s edges in the even-numbered positions yields a matching with one more edge than in M. Such a matching adjustment is called augmentation. Thus, in Figure 10.9, the matching Mb was obtained by augmentation of the matching Ma along the augmenting path 1, 6, and the matching Mc was obtained by augmentation of the matching Mb along the augmenting path 2, 6, 1, 7. Moving further, 3, 8, 4, 9, 5, 10 is an augmenting path for the matching Mc (Figure 10.9c). After adding to Mc the edges (3, 8), (4, 9), and (5, 10) and deleting (4, 8) and (5, 9), we obtain the matching Md ={(1, 7), (2, 6), (3, 8), (4, 9), (5, 10)} shown in Figure 10.9d. The 374 Iterative Improvement 1 2 3 4 6 V U 7 8 9 5 10 (a) Augmenting path: 1, 6 1 2 3 4 6 7 8 9 5 10 (b) Augmenting path: 2, 6, 1, 7 1 2 3 4 6 7 8 9 5 10 (c) Augmenting path: 3, 8, 4, 9, 5, 10 1 2 3 4 6 7 8 9 5 10 (d) Maximum matching FIGURE 10.9 Augmenting paths and matching augmentations. 10.3 Maximum Matching in Bipartite Graphs 375 matching Md is not only a maximum matching but also perfect, i.e., a matching that matches all the vertices of the graph. Before we discuss an algorithm for finding an augmenting path, let us settle the issue of what nonexistence of such a path means. According to the theorem discovered by the French mathematician Claude Berge, it means the current matching is maximal. THEOREM A matching M is a maximum matching if and only if there exists no augmenting path with respect to M. PROOF If an augmenting path with respect to a matching M exists, then the size of the matching can be increased by augmentation. Let us prove the more difficult part: if no augmenting path with respect to a matching M exists, then the matching is a maximum matching. Assume that, on the contrary, this is not the case for a certain matching M in a graph G. Let M∗ be a maximum matching in G; by our assumption, the number of edges in M∗ is at least one more than the number of edges in M, i.e., |M∗| > |M|. Consider the edges in the symmetric difference M ⊕ M∗ = (M − M∗) ∪ (M∗ − M), the set of all the edges that are either in M or in M∗ but not in both. Note that |M∗ − M| > |M − M∗| because |M∗| > |M| by assumption. Let G be the subgraph of G made up of all the edges in M ⊕ M∗ and their endpoints. By definition of a matching, any vertex in G ⊆ G can be incident to no more than one edge in M and no more than one edge in M∗. Hence, each of the vertices in G has degree 2 or less, and therefore every connected component of G is either a path or an even-length cycle of alternating edges from M − M∗ and M∗ − M. Since |M∗ − M| > |M − M∗| and the number of edges from M − M∗ and M∗ − M is the same for any even-length cycle of alternating edges in G , there must exist at least one path of alternating edges that starts and ends with an edge from M∗ − M.Hence, this is an augmenting path for the matching M, which contradicts the assumption that no such path exists. Our discussion of augmenting paths leads to the following general method for constructing a maximum matching in a bipartite graph. Start with some initial matching (e.g., the empty set). Find an augmenting path and augment the current matching along this path. When no augmenting path can be found, terminate the algorithm and return the last matching, which is maximum. We now give a specific algorithm implementing this general template. We will search for an augmenting path for a matching M by a BFS-like traversal of the graph that starts simultaneously at all the free vertices in one of the sets V and U, say, V.(It would be logical to select the smaller of the two vertex sets, but we will ignore this observation in the pseudocode below.) Recall that an augmenting path, if it exists, is an odd-length path that connects a free vertex in V with a free vertex in U and which, unless it consists of a single edge, “zigs” from a vertex in V to another vertex’ mate in U, then “zags” back to V along the uniquely defined edge from M, and so on until a free vertex in U is reached. (Draw augmenting paths for the matchings in Figure 10.9, for example.) Hence, any candidate to be such a 376 Iterative Improvement path must have its edges alternate in the pattern just described. This motivates the following rules for labeling vertices during the BFS-like traversal of the graph. Case 1 (the queue’s front vertex w is in V) If u is a free vertex adjacent to w, it is used as the other endpoint of an augmenting path; so the labeling stops and augmentation of the matching commences. The augmenting path in question is obtained by moving backward along the vertex labels (see below) to alternately add and delete its edges to and from the current matching. If u is not free and connected to w by an edge not in M, label u with w unless it has been already labeled. Case 2 (the front vertex w is in U) In this case, w must be matched and we label its mate in V with w. Here is pseudocode of the algorithm in its entirety. ALGORITHM MaximumBipartiteMatching(G) //Finds a maximum matching in a bipartite graph by a BFS-like traversal //Input: A bipartite graph G = V,U,E //Output: A maximum-cardinality matching M in the input graph initialize set M of edges with some valid matching (e.g., the empty set) initialize queue Q with all the free vertices in V (in any order) while not Empty(Q) do w ← Front(Q); Dequeue(Q) if w ∈ V for every vertex u adjacent to w do if u is free //augment M ← M ∪ (w, u) v ← w while v is labeled do u ← vertex indicated by v’s label; M ← M − (v, u) v ← vertex indicated by u’s label; M ← M ∪ (v, u) remove all vertex labels reinitialize Q with all free vertices in V break //exit the for loop else //u is matched if (w, u) ∈ M and u is unlabeled label u with w Enqueue(Q, u) else //w ∈ U (and matched) label the mate v of w with w Enqueue(Q, v) return M //current matching is maximum 10.3 Maximum Matching in Bipartite Graphs 377 An application of this algorithm to the matching in Figure 10.9a is shown in Figure 10.10. Note that the algorithm finds a maximum matching that differs from the one in Figure 10.9d. 1 2 3 4 6 V U 7 8 9 5 10 Queue: 1 2 3 1 1 2 3 4 7 8 9 5 106 Augment from 6 ↑ Queue: 1 2 3 1 2 3 4 7 8 9 5 106 Queue: 2 3 1 6 213 8 2 3 4 8 9 5 1067 Augment from 7 ↑↑↑↑↑ Queue: 2 3 6 8 1 4 1 2 3 4 8 9 5 1067 Queue: 3 1 3 6 3 8 44 2 3 4 8 9 5 1067 Augment from 10 ↑↑↑↑↑ Queue: 3 6 8 2 4 9 1 2 3 4 8 9 5 1067 Queue: empty ⇒ maximum matching FIGURE 10.10 Application of the maximum-cardinality-matching algorithm. The left column shows a current matching and initialized queue at the next iteration’s start; the right column shows the vertex labeling generated by the algorithm before augmentation is performed. Matching edges are shown in bold. Vertex labels indicate the vertices from which the labeling is done. The discovered endpoint of an augmenting path is shaded and labeled for clarity. Vertices deleted from the queue are indicated by ↑. 378 Iterative Improvement How efficient is the maximum-matching algorithm? Each iteration except the last one matches two previously free vertices—one from each of the sets V and U. Therefore, the total number of iterations cannot exceed n/2+1, where n =|V |+|U| is the number of vertices in the graph. The time spent on each iteration is in O(n+ m), where m =|E| is the number of edges in the graph. (This assumes that the information about the status of each vertex—free or matched and the vertex’ mate if the latter—can be retrieved in constant time, e.g., by storing it in an array.) Hence, the time efficiency of the algorithm is in O(n(n + m)). Hopcroft and Karp [Hop73] showed how the efficiency can be improved to O( √ n(n + m)) by combining several iterations into a single stage to maximize the number of edges added to the matching with one search. We were concerned in this section with matching the largest possible number of vertex pairs in a bipartite graph. Some applications may require taking into ac- count the quality or cost of matching different pairs. For example, workers may execute jobs with different efficiencies, or girls may have different preferences for their potential dance partners. It is natural to model such situations by bipartite graphs with weights assigned to their edges. This leads to the problem of maxi- mizing the sum of the weights on edges connecting matched pairs of vertices. This problem is called maximum-weight matching. We encountered it under a differ- ent name—the assignment problem—in Section 3.4. There are several sophisti- cated algorithms for this problem, which are much more efficient than exhaustive search (see, e.g., [Pap82], [Gal86], [Ahu93]). We have to leave them outside of our discussion, however, because of their complexity, especially for general graphs. Exercises 10.3 1. For each matching shown below in bold, find an augmentation or explain why no augmentation exists. a. b. 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 2. Apply the maximum-matching algorithm to the following bipartite graph: 123 456 10.3 Maximum Matching in Bipartite Graphs 379 3. a. What is the largest and what is the smallest possible cardinality of a match- ing in a bipartite graph G = V,U,E with n vertices in each vertex set V and U and at least n edges? b. What is the largest and what is the smallest number of distinct solutions the maximum-cardinality-matching problem can have for a bipartite graph G = V,U,E with n vertices in each vertex set V and U and at least n edges? 4. a. Hall’s Marriage Theorem asserts that a bipartite graph G = V,U,E has a matching that matches all vertices of the set V if and only if for each subset S ⊆ V,|R(S)|≥|S| where R(S) is the set of all vertices adjacent to a vertex in S. Check this property for the following graph with (i) V ={1, 2, 3, 4} and (ii) V ={5, 6, 7}. 1234 567 b. You have to devise an algorithm that returns yes if there is a matching in a bipartite graph G = V,U,E that matches all vertices in V and returns no otherwise. Would you base your algorithm on checking the condition of Hall’s Marriage Theorem? 5. Suppose there are five committees A, B, C, D, and E composed of six persons a, b, c, d, e, and f as follows: committee A’s members are b and e; committee B’s members are b, d, and e; committee C’s members are a, c, d, e, and f ; committee D’s members are b, d, and e; committee E’s members are b and e. Is there a system of distinct representatives, i.e., is it possible to select a representative from each committee so that all the selected persons are distinct? 6. Show how the maximum-cardinality-matching problem for a bipartite graph can be reduced to the maximum-flow problem discussed in Section 10.2. 7. Consider the following greedy algorithm for finding a maximum matching in a bipartite graph G = V,U,E . Sort all the vertices in nondecreasing order of their degrees. Scan this sorted list to add to the current matching (initially empty) the edge from the list’s free vertex to an adjacent free vertex of the lowest degree. If the list’s vertex is matched or if there are no adjacent free vertices for it, the vertex is simply skipped. Does this algorithm always produce a maximum matching in a bipartite graph? 8. Design a linear-time algorithm for finding a maximum matching in a tree. 9. Implement the maximum-matching algorithm of this section in the language of your choice. Experiment with its performance on bipartite graphs with n vertices in each of the vertex sets and randomly generated edges (in both 380 Iterative Improvement dense and sparse modes) to compare the observed running time with the algorithm’s theoretical efficiency. 10. Domino puzzle A domino is a 2 × 1 tile that can be oriented either hori- zontally or vertically. A tiling of a given board composed of 1 × 1 squares is covering it with dominoes exactly and without overlap. Is it possible to tile with dominoes an 8 × 8 board without two unit squares at its diagonally opposite corners? 10.4 The Stable Marriage Problem In this section, we consider an interesting version of bipartite matching called the stable marriage problem. Consider a set Y ={m1,m2,...,mn} of n men and a set X ={w1,w2,...,wn} of n women. Each man has a preference list ordering the women as potential marriage partners with no ties allowed. Similarly, each woman has a preference list of the men, also with no ties. Examples of these two sets of lists are given in Figures 10.11a and 10.11b. The same information can also be presented by an n × n ranking matrix (see Figure 10.11c). The rows and columns of the matrix represent the men and women of the two sets, respectively. A cell in row m and column w contains two rankings: the first is the position (ranking) of w in the m’s preference list; the second is the position (ranking) of m in the w’s preference list. For example, the pair 3, 1 in Jim’s row and Ann’s column in the matrix in Figure 10.11c indicates that Ann is Jim’s third choice while Jim is Ann’s first. Which of these two ways to represent such information is better depends on the task at hand. For example, it is easier to specify a match of the sets’ elements by using the ranking matrix, whereas the preference lists might be a more efficient data structure for implementing a matching algorithm. A marriage matching M is a set of n (m, w) pairs whose members are selected from disjoint n-element sets Y and X in a one-one fashion, i.e., each man m from Y is paired with exactly one woman w from X and vice versa. (If we represent Y and X as vertices of a complete bipartite graph with edges connecting possible marriage partners, then a marriage matching is a perfect matching in such a graph.) men’s preferences women’s preferences ranking matrix 1st 2nd 3rd 1st 2nd 3rd Ann Lea Sue Bob: Lea Ann Sue Ann: Jim Tom Bob Bob 2,3 1,2 3,3 Jim: Lea Sue Ann Lea: Tom Bob Jim Jim 3,1 1,3 2,1 Tom: Sue Lea Ann Sue: Jim Tom Bob Tom 3,2 2,1 1,2 (a) (b) (c) FIGURE 10.11 Data for an instance of the stable marriage problem. (a) Men’s preference lists; (b) women’s preference lists. (c) Ranking matrix (with the boxed cells composing an unstable matching). 10.4 The Stable Marriage Problem 381 A pair (m, w), where m ∈ Y, w ∈ X, is said to be a blocking pair for a marriage matching M if man m and woman w are not matched in M but they prefer each other to their mates in M. For example, (Bob, Lea) is a blocking pair for the marriage matching M ={(Bob, Ann), (Jim, Lea), (Tom, Sue)} (Figure 10.11c) because they are not matched in M while Bob prefers Lea to Ann and Lea prefers Bob to Jim. A marriage matching M is called stable if there is no blocking pair for it; otherwise, M is called unstable. According to this definition, the marriage matching in Figure 10.11c is unstable because Bob and Lea can drop their designated mates to join in a union they both prefer. The stable marriage problem is to find a stable marriage matching for men’s and women’s given preferences. Surprisingly, this problem always has a solution. (Can you find it for the instance in Figure 10.11?) It can be found by the following algorithm. Stable marriage algorithm Input: A set of n men and a set of n women along with rankings of the women by each man and rankings of the men by each woman with no ties allowed in the rankings Output: A stable marriage matching Step 0 Start with all the men and women being free. Step 1 While there are free men, arbitrarily select one of them and do the following: Proposal The selected free man m proposes to w, the next woman on his preference list (who is the highest-ranked woman who has not rejected him before). Response If w is free, she accepts the proposal to be matched with m. If she is not free, she compares m with her current mate. If she prefers m to him, she accepts m’s proposal, making her former mate free; otherwise, she simply rejects m’s proposal, leaving m free. Step 2 Return the set of n matched pairs. Before we analyze this algorithm, it is useful to trace it on some input. Such an example is presented in Figure 10.12. Let us discuss properties of the stable marriage algorithm. THEOREM The stable marriage algorithm terminates after no more than n2 iterations with a stable marriage output. PROOF The algorithm starts with n men having the total of n2 women on their ranking lists. On each iteration, one man makes a proposal to a woman. This reduces the total number of women to whom the men can still propose in the future because no man proposes to the same woman more than once. Hence, the algorithm must stop after no more than n2 iterations. 382 Iterative Improvement Free men: Bob, Jim, Tom Ann Lea Sue Bob 2, 3 1,2 3, 3 Jim 3, 11, 32, 1 Tom 3, 22, 11, 2 Bob proposed to Lea Lea accepted Free men: Jim, Tom Ann Lea Sue Bob 2, 3 1,2 3, 3 Jim 3, 11, 3 2, 1 Tom 3, 22, 11, 2 Jim proposed to Lea Lea rejected Free men: Jim, Tom Ann Lea Sue Bob 2, 3 1,2 3, 3 Jim 3, 11, 3 2,1 Tom 3, 22, 11, 2 Jim proposed to Sue Sue accepted Free men: Tom Ann Lea Sue Bob 2, 3 1,2 3, 3 Jim 3, 11, 3 2,1 Tom 3, 22, 11, 2 Tom proposed to Sue Sue rejected Free men: Tom Ann Lea Sue Bob 2, 31, 23, 3 Jim 3, 11, 3 2,1 Tom 3, 2 2,1 1, 2 Tom proposed to Lea Lea replaced Bob with Tom Free men: Bob Ann Lea Sue Bob 2,3 1, 23, 3 Jim 3, 11, 3 2,1 Tom 3, 2 2,1 1, 2 Bob proposed to Ann Ann accepted FIGURE 10.12 Application of the stable marriage algorithm. An accepted proposal is indicated by a boxed cell; a rejected proposal is shown by an underlined cell. Let us now prove that the final matching M is a stable marriage matching. Since the algorithm stops after all the n men are one-one matched to the n women, the only thing that needs to be proved is the stability of M. Suppose, on the contrary, that M is unstable. Then there exists a blocking pair of a man m and a woman w who are unmatched in M and such that both m and w prefer each other to the persons they are matched with in M. Since m proposes to every woman on his ranking list in decreasing order of preference and w precedes m’s match in M, m must have proposed to w on some iteration. Whether w refused m’s proposal or accepted it but replaced him on a subsequent iteration with a higher-ranked match, w’s mate in M must be higher on w’s preference list than m because the rankings of the men matched to a given woman may only improve on each iteration of the algorithm. This contradicts the assumption that w prefers m to her final match in M. The stable marriage algorithm has a notable shortcoming. It is not “gender neutral.” In the form presented above, it favors men’s preferences over women’s 10.4 The Stable Marriage Problem 383 preferences. We can easily see this by tracing the algorithm on the following instance of the problem: woman 1 woman 2 man 1 1, 2 2, 1 man 2 2, 1 1, 2 The algorithm obviously yields the stable matching M = {(man 1, woman 1), (man 2, woman 2)}. In this matching, both men are matched to their first choices, which is not the case for the women. One can prove that the algorithm always yields a stable matching that is man-optimal: it assigns to each man the highest-ranked woman possible under any stable marriage. Of course, this gender bias can be reversed, but not eliminated, by reversing the roles played by men and women in the algorithm, i.e., by making women propose and men accept or reject their proposals. There is another important corollary to the fact that the stable marriage algorithm always yields a gender-optimal stable matching. It is easy to prove that a man (woman)-optimal matching is unique for a given set of participant preferences. Therefore the algorithm’s output does not depend on the order in which the free men (women) make their proposals. Consequently, we can use any data structure we might prefer—e.g., a queue or a stack—for representing this set with no impact on the algorithm’s outcome. The notion of the stable matching as well as the algorithm discussed above was introduced by D. Gale and L. S. Shapley in the paper titled “College Admissions and the Stability of Marriage” [Gal62]. I do not know which of the two applications mentioned in the title you would consider more important. The point is that stability is a matching property that can be desirable in a variety of applications. For example, it has been used for many years in the United States for matching medical-school graduates with hospitals for residency training. For a brief history of this application and an in-depth discussion of the stable marriage problem and its extensions, see the monograph by Gusfield and Irwing [Gus89]. Exercises 10.4 1. Consider an instance of the stable marriage problem given by the following ranking matrix: ABC α 1, 32, 23, 1 β 3, 11, 32, 2 γ 2, 23, 11, 3 For each of its marriage matchings, indicate whether it is stable or not. For the unstable matchings, specify a blocking pair. For the stable matchings, indicate whether they are man-optimal, woman-optimal, or neither. (Assume that the Greek and Roman letters denote the men and women, respectively.) 384 Iterative Improvement 2. Design a simple algorithm for checking whether a given marriage matching is stable and determine its time efficiency class. 3. Find a stable marriage matching for the instance given in Problem 1 by apply- ing the stable marriage algorithm a. in its men-proposing version. b. in its women-proposing version. 4. Find a stable marriage matching for the instance defined by the following ranking matrix: ABCD α 1, 32, 33, 24, 3 β 1, 44, 13, 42, 2 γ 2, 21, 43, 34, 1 δ 4, 12, 23, 11, 4 5. Determine the time-efficiency class of the stable marriage algorithm a. in the worst case. b. in the best case. 6. Prove that a man-optimal stable marriage set is always unique. Is it also true for a woman-optimal stable marriage matching? 7. Prove that in the man-optimal stable matching, each woman has the worst partner that she can have in any stable marriage matching. 8. Implement the stable-marriage algorithm given in Section 10.4 so that its running time is in O(n2). Run an experiment to ascertain its average-case efficiency. 9. Write a report on the college admission problem (residents-hospitals assign- ment) that generalizes the stable marriage problem in that a college can accept “proposals” from more than one applicant. 10. Consider the problem of the roommates, which is related to but more difficult than the stable marriage problem: “An even number of boys wish to divide up into pairs of roommates. A set of pairings is called stable if under it there are no two boys who are not roommates and who prefer each other to their actual roommates.” [Gal62] Give an instance of this problem that does not have a stable pairing. SUMMARY The iterative-improvement technique involves finding a solution to an op- timization problem by generating a sequence of feasible solutions with improving values of the problem’s objective function. Each subsequent so- lution in such a sequence typically involves a small, localized change in the previous feasible solution. When no such change improves the value of the Summary 385 objective function, the algorithm returns the last feasible solution as optimal and stops. Important problems that can be solved exactly by iterative-improvement algorithms include linear programming, maximizing the flow in a network, and matching the maximum possible number of vertices in a graph. The simplex method is the classic method for solving the general linear programming problem. It works by generating a sequence of adjacent extreme points of the problem’s feasible region with improving values of the objective function. The maximum-flow problem asks to find the maximum flow possible in a network, a weighted directed graph with a source and a sink. The Ford-Fulkerson method is a classic template for solving the maximum- flow problem by the iterative-improvement approach. The shortest- augmenting-path method implements this idea by labeling network vertices in the breadth-first search manner. The Ford-Fulkerson method also finds a minimum cut in a given network. A maximum cardinality matching is the largest subset of edges in a graph such that no two edges share the same vertex. For a bipartite graph, it can be found by a sequence of augmentations of previously obtained matchings. The stable marriage problem is to find a stable matching for elements of two n- element sets based on given matching preferences. This problem always has a solution that can be found by the Gale-Shapley algorithm. This page intentionally left blank 11 Limitations of Algorithm Power Intellect distinguishes between the possible and the impossible; reason distinguishes between the sensible and the senseless. Even the possible can be senseless. —Max Born (1882–1970), My Life and My Views, 1968 In the preceding chapters of this book, we encountered dozens of algorithms for solving a variety of different problems. A fair assessment of algorithms as problem-solving tools is inescapable: they are very powerful instruments, espe- cially when they are executed by modern computers. But the power of algorithms is not unlimited, and its limits are the subject of this chapter. As we shall see, some problems cannot be solved by any algorithm. Other problems can be solved algo- rithmically but not in polynomial time. And even when a problem can be solved in polynomial time by some algorithms, there are usually lower bounds on their efficiency. We start, in Section 11.1, with methods for obtaining lower bounds, which are estimates on a minimum amount of work needed to solve a problem. In general, obtaining a nontrivial lower bound even for a simple-sounding problem is a very difficult task. As opposed to ascertaining the efficiency of a particular algorithm, the task here is to establish a limit on the efficiency of any algorithm, known or unknown. This also necessitates a careful description of the operations such algo- rithms are allowed to perform. If we fail to define carefully the “rules of the game,” so to speak, our claims may end up in the large dustbin of impossibility-related statements as, for example, the one made by the celebrated British physicist Lord Kelvin in 1895: “Heavier-than-air flying machines are impossible.” Section 11.2 discusses decision trees. This technique allows us, among other applications, to establish lower bounds on the efficiency of comparison-based algorithms for sorting and for searching in sorted arrays. As a result, we will be able to answer such questions as whether it is possible to invent a faster sorting algorithm than mergesort and whether binary search is the fastest algorithm for searching in a sorted array. (What does your intuition tell you the answers to these questions will turn out to be?) Incidentally, decision trees are also a great vehicle 387 388 Limitations of Algorithm Power for directing us to a solution of some puzzles, such as the coin-weighing problem discussed in Section 4.4. Section 11.3 deals with the question of intractability: which problems can and cannot be solved in polynomial time. This well-developed area of theoretical computer science is called computational complexity theory. We present the basic elements of this theory and discuss informally such fundamental notions as P,NP, and NP-complete problems, including the most important unresolved question of theoretical computer science about the relationship between P and NP problems. The last section of this chapter deals with numerical analysis. This branch of computer science concerns algorithms for solving problems of “continuous” mathematics—solving equations and systems of equations, evaluating such func- tions as sin x and ln x,computing integrals, and so on. The nature of such problems imposes two types of limitations. First, most cannot be solved exactly. Second, solving them even approximately requires dealing with numbers that can be rep- resented in a digital computer with only a limited level of precision. Manipulating approximate numbers without proper care can lead to very inaccurate results. We will see that even solving a basic quadratic equation on a computer poses sig- nificant difficulties that require a modification of the canonical formula for the equation’s roots. 11.1 Lower-Bound Arguments We can look at the efficiency of an algorithm two ways. We can establish its asymp- totic efficiency class (say, for the worst case) and see where this class stands with respect to the hierarchy of efficiency classes outlined in Section 2.2. For exam- ple, selection sort, whose efficiency is quadratic, is a reasonably fast algorithm, whereas the algorithm for the Tower of Hanoi problem is very slow because its ef- ficiency is exponential. We can argue, however, that this comparison is akin to the proverbial comparison of apples to oranges because these two algorithms solve different problems. The alternative and possibly “fairer” approach is to ask how efficient a particular algorithm is with respect to other algorithms for the same problem. Seen in this light, selection sort has to be considered slow because there are O(n log n) sorting algorithms; the Tower of Hanoi algorithm, on the other hand, turns out to be the fastest possible for the problem it solves. When we want to ascertain the efficiency of an algorithm with respect to other algorithms for the same problem, it is desirable to know the best possible efficiency any algorithm solving the problem may have. Knowing such a lower bound can tell us how much improvement we can hope to achieve in our quest for a better algorithm for the problem in question. If such a bound is tight, i.e., we already know an algorithm in the same efficiency class as the lower bound, we can hope for a constant-factor improvement at best. If there is a gap between the efficiency of the fastest algorithm and the best lower bound known, the door for possible improvement remains open: either a faster algorithm matching the lower bound could exist or a better lower bound could be proved. 11.1 Lower-Bound Arguments 389 In this section, we present several methods for establishing lower bounds and illustrate them with specific examples. As we did in analyzing the efficiency of specific algorithms in the preceding chapters, we should distinguish between a lower-bound class and a minimum number of times a particular operation needs to be executed. As a rule, the second problem is more difficult than the first. For example, we can immediately conclude that any algorithm for finding the median of n numbers must be in (n) (why?), but it is not simple at all to prove that any comparison-based algorithm for this problem must do at least 3(n − 1)/2 comparisons in the worst case (for odd n). Trivial Lower Bounds The simplest method of obtaining a lower-bound class is based on counting the number of items in the problem’s input that must be processed and the number of output items that need to be produced. Since any algorithm must at least “read” all the items it needs to process and “write” all its outputs, such a count yields a trivial lower bound. For example, any algorithm for generating all permutations of n distinct items must be in (n!) because the size of the output is n!. And this bound is tight because good algorithms for generating permutations spend a constant time on each of them except the initial one (see Section 4.3). As another example, consider the problem of evaluating a polynomial of degree n p(x) = anxn + an−1xn−1 + ...+ a0 at a given point x, given its coefficients an,an−1,...,a0. It is easy to see that all the coefficients have to be processed by any polynomial-evaluation algorithm. Indeed, if it were not the case, we could change the value of an unprocessed coefficient, which would change the value of the polynomial at a nonzero point x. This means that any such algorithm must be in (n). This lower bound is tight because both the right-to-left evaluation algorithm (Problem 2 in Exercises 6.5) and Horner’s rule (Section 6.5) are both linear. In a similar vein, a trivial lower bound for computing the product of two n × n matrices is (n2) because any such algorithm has to process 2n2 elements in the input matrices and generate n2 elements of the product. It is still unknown, however, whether this bound is tight. Trivial lower bounds are often too low to be useful. For example, the trivial bound for the traveling salesman problem is (n2), because its input is n(n − 1)/2 intercity distances and its output is a list of n + 1 cities making up an optimal tour. But this bound is all but useless because there is no known algorithm with the running time being a polynomial function of any degree. There is another obstacle to deriving a meaningful lower bound by this method. It lies in determining which part of an input must be processed by any algorithm solving the problem in question. For example, searching for an ele- ment of a given value in a sorted array does not require processing all its elements (why?). As another example, consider the problem of determining connectivity of 390 Limitations of Algorithm Power an undirected graph defined by its adjacency matrix. It is plausible to expect that any such algorithm would have to check the existence of each of the n(n − 1)/2 potential edges, but the proof of this fact is not trivial. Information-Theoretic Arguments While the approach outlined above takes into account the size of a problem’s output, the information-theoretical approach seeks to establish a lower bound based on the amount of information it has to produce. Consider, as an example, the well-known game of deducing a positive integer between 1 and n selected by somebody by asking that person questions with yes/no answers. The amount of uncertainty that any algorithm solving this problem has to resolve can be measured by log2 n, the number of bits needed to specify a particular number among the n possibilities. We can think of each question (or, to be more accurate, an answer to each question) as yielding at most 1 bit of information about the algorithm’s output, i.e., the selected number. Consequently, any such algorithm will need at least log2 n such steps before it can determine its output in the worst case. The approach we just exploited is called the information-theoretic argument because of its connection to information theory. It has proved to be quite useful for finding the so-called information-theoretic lower bounds for many problems involving comparisons, including sorting and searching. Its underlying idea can be realized much more precisely through the mechanism of decision trees. Because of the importance of this technique, we discuss it separately and in more detail in Section 11.2. Adversary Arguments Let us revisit the same game of “guessing” a number used to introduce the idea of an information-theoretic argument. We can prove that any algorithm that solves this problem must ask at least log2 n questions in its worst case by playing the role of a hostile adversary who wants to make an algorithm ask as many questions as possible. The adversary starts by considering each of the numbers between 1 and n as being potentially selected. (This is cheating, of course, as far as the game is concerned, but not as a way to prove our assertion.) After each question, the adversary gives an answer that leaves him with the largest set of numbers consistent with this and all the previously given answers. This strategy leaves him with at least one-half of the numbers he had before his last answer. If an algorithm stops before the size of the set is reduced to 1, the adversary can exhibit a number that could be a legitimate input the algorithm failed to identify. It is a simple technical matter now to show that one needs log2 n iterations to shrink an n-element set to a one-element set by halving and rounding up the size of the remaining set. Hence, at least log2 n questions need to be asked by any algorithm in the worst case. This example illustrates the adversary method for establishing lower bounds. It is based on following the logic of a malevolent but honest adversary: the malev- 11.1 Lower-Bound Arguments 391 olence makes him push the algorithm down the most time-consuming path, and his honesty forces him to stay consistent with the choices already made. A lower bound is then obtained by measuring the amount of work needed to shrink a set of potential inputs to a single input along the most time-consuming path. As another example, consider the problem of merging two sorted lists of size n a1 k . (For the sake of simplicity, we assume throughout this section that all input items are distinct.) Each leaf represents a possible outcome of the algorithm’s run on some input of size n. Note that the number of leaves can be greater than the number of outcomes because, for some algorithms, the same outcome can be arrived at through a different chain of comparisons. (This happens to be the case for the decision tree in Figure 11.1.) An important point is that the number of leaves must be at least as large as the number of possible outcomes. The algorithm’s work on a particular input of size n can be traced by a path from the root to a leaf in its decision tree, and the number of comparisons made by the algorithm on such 11.2 Decision Trees 395 yes yes no no no yes acbc a < cb < c a < b FIGURE 11.1 Decision tree for finding a minimum of three numbers. a run is equal to the length of this path. Hence, the number of comparisons in the worst case is equal to the height of the algorithm’s decision tree. The central idea behind this model lies in the observation that a tree with a given number of leaves, which is dictated by the number of possible outcomes, has to be tall enough to have that many leaves. Specifically, it is not difficult to prove that for any binary tree with l leaves and height h, h ≥log2 l. (11.1) Indeed, a binary tree of height h with the largest number of leaves has all its leaves on the last level (why?). Hence, the largest number of leaves in such a tree is 2h. In other words, 2h ≥ l, which immediately implies (11.1). Inequality (11.1) puts a lower bound on the heights of binary decision trees and hence the worst-case number of comparisons made by any comparison-based algorithm for the problem in question. Such a bound is called the information- theoretic lower bound (see Section 11.1). We illustrate this technique below on two important problems: sorting and searching in a sorted array. Decision Trees for Sorting Most sorting algorithms are comparison based, i.e., they work by comparing elements in a list to be sorted. By studying properties of decision trees for such algorithms, we can derive important lower bounds on their time efficiencies. We can interpret an outcome of a sorting algorithm as finding a permutation of the element indices of an input list that puts the list’s elements in ascending order. Consider, as an example, a three-element list a, b, c of orderable items such as real numbers or strings. For the outcome a A[3] A[0] A[2](A[0], A[1]) (A[1], A[2]) (A[2], A[3]) <>= <<>>== <>= FIGURE 11.4 Ternary decision tree for binary search in a four-element array. We will use decision trees to determine whether this is the smallest possible number of comparisons. Since we are dealing here with three-way comparisons in which search key K is compared with some element A[i]to see whether KA[i], it is natural to try using ternary decision trees. Figure 11.4 presents such a tree for the case of n = 4. The internal nodes of that tree indicate the array’s elements being compared with the search key. The leaves indicate either a matching element in the case of a successful search or a found interval that the search key belongs to in the case of an unsuccessful search. We can represent any algorithm for searching a sorted array by three-way comparisons with a ternary decision tree similar to that in Figure 11.4. For an array of n elements, all such decision trees will have 2n + 1 leaves (n for successful searches and n + 1for unsuccessful ones). Since the minimum height h of a ternary tree with l leaves is log3 l, we get the following lower bound on the number of worst-case comparisons: Cworst(n) ≥log3(2n + 1). This lower bound is smaller than log2(n + 1), the number of worst-case comparisons for binary search, at least for large values of n (and smaller than or equal to log2(n + 1) for every positive integer n—see Problem 7 in this section’s exercises). Can we prove a better lower bound, or is binary search far from being optimal? The answer turns out to be the former. To obtain a better lower bound, we should consider binary rather than ternary decision trees, such as the one in Figure 11.5. Internal nodes in such a tree correspond to the same three- way comparisons as before, but they also serve as terminal nodes for successful searches. Leaves therefore represent only unsuccessful searches, and there are n + 1 of them for searching an n-element array. 11.2 Decision Trees 399 A[1] < A[0] > A[3] (A[0], A[1]) (A[1], A[2]) (A[2], A[3]) < << < > >> > A[0] A[2] A[3] FIGURE 11.5 Binary decision tree for binary search in a four-element array. As comparison of the decision trees in Figures 11.4 and 11.5 illustrates, the binary decision tree is simply the ternary decision tree with all the middle subtrees eliminated. Applying inequality (11.1) to such binary decision trees immediately yields Cworst(n) ≥log2(n + 1). (11.5) This inequality closes the gap between the lower bound and the number of worst- case comparisons made by binary search, which is also log2(n + 1). A much more sophisticated analysis (see, e.g., [KnuIII, Section 6.2.1]) shows that under the standard assumptions about searches, binary search makes the smallest number of comparisons on the average, as well. The average number of comparisons made by this algorithm turns out to be about log2 n − 1 and log2(n + 1) for successful and unsuccessful searches, respectively. Exercises 11.2 1. Prove by mathematical induction that a. h ≥log2 l for any binary tree with height h and the number of leaves l. b. h ≥log3 l for any ternary tree with height h and the number of leaves l. 2. Consider the problem of finding the median of a three-element set {a, b, c} of orderable items. a. What is the information-theoretic lower bound for comparison-based al- gorithms solving this problem? b. Draw a decision tree for an algorithm solving this problem. c. If the worst-case number of comparisons in your algorithm is greater than the information-theoretic lower bound, do you think an algorithm 400 Limitations of Algorithm Power matching the lower bound exists? (Either find such an algorithm or prove its impossibility.) 3. Draw a decision tree and find the number of key comparisons in the worst and average cases for a. the three-element basic bubble sort. b. the three-element enhanced bubble sort (which stops if no swaps have been made on its last pass). 4. Design a comparison-based algorithm for sorting a four-element array with the smallest number of element comparisons possible. 5. Design a comparison-based algorithm for sorting a five-element array with seven comparisons in the worst case. 6. Draw a binary decision tree for searching a four-element sorted list by sequen- tial search. 7. Compare the two lower bounds for searching a sorted array—log3(2n + 1) and log2(n + 1)—to show that a. log3(2n + 1)≤log2(n + 1) for every positive integer n. b. log3(2n + 1) < log2(n + 1) for every positive integer n ≥ n0. 8. What is the information-theoretic lower bound for finding the maximum of n numbers by comparison-based algorithms? Is this bound tight? 9. A tournament tree is a complete binary tree reflecting results of a “knockout tournament”: its leaves represent n players entering the tournament, and each internal node represents a winner of a match played by the players represented by the node’s children. Hence, the winner of the tournament is represented by the root of the tree. a. What is the total number of games played in such a tournament? b. How many rounds are there in such a tournament? c. Design an efficient algorithm to determine the second-best player using the information produced by the tournament. How many extra games does your algorithm require? 10. Advanced fake-coin problem There are n ≥ 3 coins identical in appearance; either all are genuine or exactly one of them is fake. It is unknown whether the fake coin is lighter or heavier than the genuine one. You have a balance scale with which you can compare any two sets of coins. That is, by tipping to the left, to the right, or staying even, the balance scale will tell whether the sets weigh the same or which of the sets is heavier than the other, but not by how much. The problem is to find whether all the coins are genuine and, if not, to find the fake coin and establish whether it is lighter or heavier than the genuine ones. 11.3 P, NP, and NP-Complete Problems 401 a. Prove that any algorithm for this problem must make at least log3(2n + 1) weighings in the worst case. b. Draw a decision tree for an algorithm that solves the problem for n = 3 coins in two weighings. c. Prove that there exists no algorithm that solves the problem for n = 4 coins in two weighings. d. Draw a decision tree for an algorithm that solves the problem for n = 4 coins in two weighings by using an extra coin known to be genuine. e. Draw a decision tree for an algorithm that solves the classic version of the problem—that for n = 12 coins in three weighings (with no extra coins being used). 11. Jigsaw puzzle A jigsaw puzzle contains n pieces. A “section” of the puzzle is a set of one or more pieces that have been connected to each other. A “move” consists of connecting two sections. What algorithm will minimize the number of moves required to complete the puzzle? 11.3 P, NP, and NP-Complete Problems In the study of the computational complexity of problems, the first concern of both computer scientists and computing professionals is whether a given problem can be solved in polynomial time by some algorithm. DEFINITION 1 We say that an algorithm solves a problem in polynomial time if its worst-case time efficiency belongs to O(p(n)) where p(n) is a polynomial of the problem’s input size n. (Note that since we are using big-oh notation here, problems solvable in, say, logarithmic time are solvable in polynomial time as well.) Problems that can be solved in polynomial time are called tractable, and problems that cannot be solved in polynomial time are called intractable. There are several reasons for drawing the intractability line in this way. First, the entries of Table 2.1 and their discussion in Section 2.1 imply that we cannot solve arbitrary instances of intractable problems in a reasonable amount of time unless such instances are very small. Second, although there might be a huge difference between the running times in O(p(n)) for polynomials of drastically different degrees, there are very few useful polynomial-time algorithms with the degree of a polynomial higher than three. In addition, polynomials that bound running times of algorithms do not usually have extremely large coefficients. Third, polynomial functions possess many convenient properties; in particular, both the sum and composition of two polynomials are always polynomials too. Fourth, the choice of this class has led to a development of an extensive theory called computational complexity, which seeks to classify problems according to their inherent difficulty. And according to this theory, a problem’s intractability 402 Limitations of Algorithm Power remains the same for all principal models of computations and all reasonable input-encoding schemes for the problem under consideration. We just touch on some basic notions and ideas of complexity theory in this section. If you are interested in a more formal treatment of this theory, you will have no trouble finding a wealth of textbooks devoted to the subject (e.g., [Sip05], [Aro09]). P and NP Problems Most problems discussed in this book can be solved in polynomial time by some algorithm. They include computing the product and the greatest common divisor of two integers, sorting a list, searching for a key in a list or for a pattern in a text string, checking connectivity and acyclicity of a graph, and finding a minimum spanning tree and shortest paths in a weighted graph. (You are invited to add more examples to this list.) Informally, we can think about problems that can be solved in polynomial time as the set that computer science theoreticians call P.A more formal definition includes in P only decision problems, which are problems with yes/no answers. DEFINITION 2 Class P is a class of decision problems that can be solved in polynomial time by (deterministic) algorithms. This class of problems is called polynomial. The restriction of P to decision problems can be justified by the following reasons. First, it is sensible to exclude problems not solvable in polynomial time because of their exponentially large output. Such problems do arise naturally— e.g., generating subsets of a given set or all the permutations of n distinct items— but it is apparent from the outset that they cannot be solved in polynomial time. Second, many important problems that are not decision problems in their most natural formulation can be reduced to a series of decision problems that are easier to study. For example, instead of asking about the minimum number of colors needed to color the vertices of a graph so that no two adjacent vertices are colored the same color, we can ask whether there exists such a coloring of the graph’s vertices with no more than m colors for m = 1, 2,....(The latter is called the m- coloring problem.) The first value of m in this series for which the decision problem of m-coloring has a solution solves the optimization version of the graph-coloring problem as well. It is natural to wonder whether every decision problem can be solved in polynomial time. The answer to this question turns out to be no. In fact, some decision problems cannot be solved at all by any algorithm. Such problems are called undecidable, as opposed to decidable problems that can be solved by an algorithm. A famous example of an undecidable problem was given by Alan 11.3 P, NP, and NP-Complete Problems 403 Turing in 1936.1 The problem in question is called the halting problem: given a computer program and an input to it, determine whether the program will halt on that input or continue working indefinitely on it. Here is a surprisingly short proof of this remarkable fact. By way of contra- diction, assume that A is an algorithm that solves the halting problem. That is, for any program P and input I, A(P, I) =  1, if program P halts on input I; 0, if program P does not halt on input I. We can consider program P as an input to itself and use the output of algorithm A for pair (P, P ) to construct a program Q as follows: Q(P ) =  halts, if A(P, P ) = 0, i.e., if program P does not halt on input P ; does not halt, if A(P, P ) = 1, i.e., if program P halts on input P . Then on substituting Q for P,we obtain Q(Q) =  halts, if A(Q, Q) = 0, i.e., if program Q does not halt on input Q; does not halt, if A(Q, Q) = 1, i.e., if program Q halts on input Q. This is a contradiction because neither of the two outcomes for program Q is possible, which completes the proof. Are there decidable but intractable problems? Yes, there are, but the number of known examples is surprisingly small, especially of those that arise naturally rather than being constructed for the sake of a theoretical argument. There are many important problems, however, for which no polynomial-time algorithm has been found, nor has the impossibility of such an algorithm been proved. The classic monograph by M. Garey and D. Johnson [Gar79] contains a list of several hundred such problems from different areas of computer science, mathematics, and operations research. Here is just a small sample of some of the best-known problems that fall into this category: Hamiltonian circuit problem Determine whether a given graph has a Hamiltonian circuit—a path that starts and ends at the same vertex and passes through all the other vertices exactly once. Traveling salesman problem Find the shortest tour through n cities with known positive integer distances between them (find the shortest Hamiltonian circuit in a complete graph with positive integer weights). 1. This was just one of many breakthrough contributions to theoretical computer science made by the English mathematician and computer science pioneer Alan Turing (1912–1954). In recognition of this, the ACM—the principal society of computing professionals and researchers—has named after him an award given for outstanding contributions to theoretical computer science. A lecture given on such an occasion by Richard Karp [Kar86] provides an interesting historical account of the development of complexity theory. 404 Limitations of Algorithm Power Knapsack problem Find the most valuable subset of n items of given positive integer weights and values that fit into a knapsack of a given positive integer capacity. Partition problem Given n positive integers, determine whether it is possi- ble to partition them into two disjoint subsets with the same sum. Bin-packing problem Given n items whose sizes are positive rational num- bers not larger than 1, put them into the smallest number of bins of size 1. Graph-coloring problem For a given graph, find its chromatic number, which is the smallest number of colors that need to be assigned to the graph’s vertices so that no two adjacent vertices are assigned the same color. Integer linear programming problem Find the maximum (or minimum) value of a linear function of several integer-valued variables subject to a finite set of constraints in the form of linear equalities and inequalities. Some of these problems are decision problems. Those that are not have decision-version counterparts (e.g., the m-coloring problem for the graph-coloring problem). What all these problems have in common is an exponential (or worse) growth of choices, as a function of input size, from which a solution needs to be found. Note, however, that some problems that also fall under this umbrella can be solved in polynomial time. For example, the Eulerian circuit problem—the problem of the existence of a cycle that traverses all the edges of a given graph exactly once—can be solved in O(n2) time by checking, in addition to the graph’s connectivity, whether all the graph’s vertices have even degrees. This example is particularly striking: it is quite counterintuitive to expect that the problem about cycles traversing all the edges exactly once (Eulerian circuits) can be so much easier than the seemingly similar problem about cycles visiting all the vertices exactly once (Hamiltonian circuits). Another common feature of a vast majority of decision problems is the fact that although solving such problems can be computationally difficult, checking whether a proposed solution actually solves the problem is computationally easy, i.e., it can be done in polynomial time. (We can think of such a proposed solution as being randomly generated by somebody leaving us with the task of verifying its validity.) For example, it is easy to check whether a proposed list of vertices is a Hamiltonian circuit for a given graph with n vertices. All we need to check is that the list contains n + 1 vertices of the graph in question, that the first n vertices are distinct whereas the last one is the same as the first, and that every consecutive pair of the list’s vertices is connected by an edge. This general observation about decision problems has led computer scientists to the notion of a nondeterministic algorithm. DEFINITION 3 A nondeterministic algorithm is a two-stage procedure that takes as its input an instance I of a decision problem and does the following. Nondeterministic (“guessing”) stage: An arbitrary string S is generated that can be thought of as a candidate solution to the given instance I (but may be complete gibberish as well). 11.3 P, NP, and NP-Complete Problems 405 Deterministic (“verification”) stage: A deterministic algorithm takes both I and S as its input and outputs yes if S represents a solution to instance I. (If S is not a solution to instance I, the algorithm either returns no or is allowed not to halt at all.) We say that a nondeterministic algorithm solves a decision problem if and only if for every yes instance of the problem it returns yes on some execu- tion. (In other words, we require a nondeterministic algorithm to be capable of “guessing” a solution at least once and to be able to verify its validity. And, of course, we do not want it to ever output a yes answer on an instance for which the answer should be no.) Finally, a nondeterministic algorithm is said to be nondeterministic polynomial if the time efficiency of its verification stage is polynomial. Now we can define the class of NP problems. DEFINITION 4 Class NP is the class of decision problems that can be solved by nondeterministic polynomial algorithms. This class of problems is called nonde- terministic polynomial. Most decision problems are in NP. First of all, this class includes all the problems in P: P ⊆ NP. This is true because, if a problem is in P, we can use the deterministic polynomial- time algorithm that solves it in the verification-stage of a nondeterministic algo- rithm that simply ignores string S generated in its nondeterministic (“guessing”) stage. But NP also contains the Hamiltonian circuit problem, the partition prob- lem, decision versions of the traveling salesman, the knapsack, graph coloring, and many hundreds of other difficult combinatorial optimization problems cataloged in [Gar79]. The halting problem, on the other hand, is among the rare examples of decision problems that are known not to be in NP. This leads to the most important open question of theoretical computer sci- ence: Is P a proper subset of NP, or are these two classes, in fact, the same? We can put this symbolically as P ?= NP. Note that P = NP would imply that each of many hundreds of difficult combinatorial decision problems can be solved by a polynomial-time algorithm, although computer scientists have failed to find such algorithms despite their per- sistent efforts over many years. Moreover, many well-known decision problems are known to be “NP-complete” (see below), which seems to cast more doubts on the possibility that P = NP. 406 Limitations of Algorithm Power NP-Complete Problems Informally, an NP-complete problem is a problem in NP that is as difficult as any other problem in this class because, by definition, any other problem in NP can be reduced to it in polynomial time (shown symbolically in Figure 11.6). Here are more formal definitions of these concepts. DEFINITION 5 A decision problem D1 is said to be polynomially reducible to a decision problem D2, if there exists a function t that transforms instances of D1 to instances of D2 such that: 1. t maps all yes instances of D1 to yes instances of D2 and all no instances of D1 to no instances of D2 2. t is computable by a polynomial time algorithm This definition immediately implies that if a problem D1 is polynomially reducible to some problem D2 that can be solved in polynomial time, then problem D1 can also be solved in polynomial time (why?). DEFINITION 6 A decision problem D is said to be NP-complete if: 1. it belongs to class NP 2. every problem in NP is polynomially reducible to D The fact that closely related decision problems are polynomially reducible to each other is not very surprising. For example, let us prove that the Hamiltonian circuit problem is polynomially reducible to the decision version of the traveling NP -complete problem NP problems FIGURE 11.6 Notion of an NP-complete problem. Polynomial-time reductions of NP problems to an NP-complete problem are shown by arrows. 11.3 P, NP, and NP-Complete Problems 407 salesman problem. The latter can be stated as the existence problem of a Hamil- tonian circuit not longer than a given positive integer m in a given complete graph with positive integer weights. We can map a graph G of a given instance of the Hamiltonian circuit problem to a complete weighted graph G representing an in- stance of the traveling salesman problem by assigning 1 as the weight to each edge in G and adding an edge of weight 2 between any pair of nonadjacent vertices in G. As the upper bound m on the Hamiltonian circuit length, we take m = n, where n is the number of vertices in G (and G ). Obviously, this transformation can be done in polynomial time. Let G be a yes instance of the Hamiltonian circuit problem. Then G has a Hamiltonian circuit, and its image in G will have length n, making the image a yes instance of the decision traveling salesman problem. Conversely, if we have a Hamiltonian circuit of the length not larger than n in G , then its length must be exactly n (why?) and hence the circuit must be made up of edges present in G, making the inverse image of the yes instance of the decision traveling salesman problem be a yes instance of the Hamiltonian circuit problem. This completes the proof. The notion of NP-completeness requires, however, polynomial reducibility of all problems in NP, both known and unknown, to the problem in question. Given the bewildering variety of decision problems, it is nothing short of amazing that specific examples of NP-complete problems have been actually found. Neverthe- less, this mathematical feat was accomplished independently by Stephen Cook in the United States and Leonid Levin in the former Soviet Union.2 In his 1971 paper, Cook [Coo71] showed that the so-called CNF-satisfiability problem is NP- complete. The CNF-satisfiability problem deals with boolean expressions. Each boolean expression can be represented in conjunctive normal form, such as the following expression involving three boolean variables x1,x2, and x3 and their negations denoted ¯x1, ¯x2, and ¯x3, respectively: (x1 ∨¯x2 ∨¯x3)&(¯x1 ∨ x2)&(¯x1 ∨¯x2 ∨¯x3). The CNF-satisfiability problem asks whether or not one can assign values true and false to variables of a given boolean expression in its CNF form to make the entire expression true. (It is easy to see that this can be done for the above formula: if x1 = true, x2 = true, and x3 = false, the entire expression is true.) Since the Cook-Levin discovery of the first known NP-complete problems, computer scientists have found many hundreds, if not thousands, of other exam- ples. In particular, the well-known problems (or their decision versions) men- tioned above—Hamiltonian circuit, traveling salesman, partition, bin packing, and graph coloring—are all NP-complete. It is known, however, that if P = NP there must exist NP problems that neither are in P nor are NP-complete. 2. As it often happens in the history of science, breakthrough discoveries are made independently and almost simultaneously by several scientists. In fact, Levin introduced a more general notion than NP- completeness, which was not limited to decision problems, but his paper [Lev73] was published two years after Cook’s. 408 Limitations of Algorithm Power For a while, the leading candidate to be such an example was the problem of determining whether a given integer is prime or composite. But in an im- portant theoretical breakthrough, Professor Manindra Agrawal and his students Neeraj Kayal and Nitin Saxena of the Indian Institute of Technology in Kanpur announced in 2002 a discovery of a deterministic polynomial-time algorithm for primality testing [Agr04]. Their algorithm does not solve, however, the related problem of factoring large composite integers, which lies at the heart of the widely used encryption method called the RSA algorithm [Riv78]. Showing that a decision problem is NP-complete can be done in two steps. First, one needs to show that the problem in question is in NP; i.e., a randomly generated string can be checked in polynomial time to determine whether or not it represents a solution to the problem. Typically, this step is easy. The second step is to show that every problem in NP is reducible to the problem in question in polynomial time. Because of the transitivity of polynomial reduction, this step can be done by showing that a known NP-complete problem can be transformed to the problem in question in polynomial time (see Figure 11.7). Although such a transformation may need to be quite ingenious, it is incomparably simpler than proving the existence of a transformation for every problem in NP. For example, if we already know that the Hamiltonian circuit problem is NP-complete, its polynomial reducibility to the decision traveling salesman problem implies that the latter is also NP-complete (after an easy check that the decision traveling salesman problem is in class NP). The definition of NP-completeness immediately implies that if there exists a deterministic polynomial-time algorithm for just one NP-complete problem, then every problem in NP can be solved in polynomial time by a deterministic algo- rithm, and hence P = NP. In other words, finding a polynomial-time algorithm known NP -complete problem candidate for NP -completeness NP problems FIGURE 11.7 Proving NP-completeness by reduction. 11.3 P, NP, and NP-Complete Problems 409 for one NP-complete problem would mean that there is no qualitative difference between the complexity of checking a proposed solution and finding it in polyno- mial time for the vast majority of decision problems of all kinds. Such implications make most computer scientists believe that P = NP, although nobody has been successful so far in finding a mathematical proof of this intriguing conjecture. Sur- prisingly, in interviews with the authors of a book about the lives and discoveries of 15 prominent computer scientists [Sha98], Cook seemed to be uncertain about the eventual resolution of this dilemma whereas Levin contended that we should expect the P = NP outcome. Whatever the eventual answer to the P ?= NP question proves to be, knowing that a problem is NP-complete has important practical implications for today. It means that faced with a problem known to be NP-complete, we should probably not aim at gaining fame and fortune3 by designing a polynomial-time algorithm for solving all its instances. Rather, we should concentrate on several approaches that seek to alleviate the intractability of such problems. These approaches are outlined in the next chapter of the book. Exercises 11.3 1. A game of chess can be posed as the following decision problem: given a legal positioning of chess pieces and information about which side is to move, determine whether that side can win. Is this decision problem decidable? 2. A certain problem can be solved by an algorithm whose running time is in O(nlog2 n). Which of the following assertions is true? a. The problem is tractable. b. The problem is intractable. c. Impossible to tell. 3. Give examples of the following graphs or explain why such examples cannot exist. a. graph with a Hamiltonian circuit but without an Eulerian circuit b. graph with an Eulerian circuit but without a Hamiltonian circuit c. graph with both a Hamiltonian circuit and an Eulerian circuit d. graph with a cycle that includes all the vertices but with neither a Hamil- tonian circuit nor an Eulerian circuit 3. In 2000, The Clay Mathematics Institute (CMI) of Cambridge, Massachusetts, designated a $1 million prize for the solution to this problem. 410 Limitations of Algorithm Power 4. For each of the following graphs, find its chromatic number. e b d c a a b c f g d e h ae b f c g d h a. b. c. 5. Design a polynomial-time algorithm for the graph 2-coloring problem: deter- mine whether vertices of a given graph can be colored in no more than two colors so that no two adjacent vertices are colored the same color. 6. Consider the following brute-force algorithm for solving the composite num- ber problem: Check successive integers from 2 to n/2 as possible divisors of n. If one of them divides n evenly, return yes (i.e., the number is composite); if none of them does, return no. Why does this algorithm not put the problem in class P? 7. State the decision version for each of the following problems and outline a polynomial-time algorithm that verifies whether or not a proposed solution solves the problem. (You may assume that a proposed solution represents a legitimate input to your verification algorithm.) a. knapsack problem b. bin packing problem 8. Show that the partition problem is polynomially reducible to the decision version of the knapsack problem. 9. Show that the following three problems are polynomially reducible to each other. (i) Determine, for a given graph G = V,E and a positive integer m ≤|V |, whether G contains a clique of size m or more. (A clique of size k in a graph is its complete subgraph of k vertices.) (ii) Determine, for a given graph G = V,E and a positive integer m ≤|V |, whether there is a vertex cover of size m or less for G. (A vertex cover of size k for a graph G = V,E is a subset V ⊆ V such that |V |=k and, for each edge (u, v) ∈ E, at least one of u and v belongs to V .) (iii) Determine, for a given graph G = V,E and a positive integer m ≤|V |, whether G contains an independent set of size m or more. (An independent 11.3 P, NP, and NP-Complete Problems 411 set of size k for a graph G = V,E is a subset V ⊆ V such that |V |=k and for all u, v ∈ V , vertices u and v are not adjacent in G.) 10. Determine whether the following problem is NP-complete. Given several sequences of uppercase and lowercase letters, is it possible to select a letter from each sequence without selecting both the upper- and lowercase versions of any letter? For example, if the sequences are Abc, BC, aB, and ac, it is possible to choose A from the first sequence, B from the second and third, and c from the fourth. An example where there is no way to make the required selections is given by the four sequences AB, Ab, aB, and ab. [Kar86] 11. Which of the following diagrams do not contradict the current state of our knowledge about the complexity classes P, NP, and NPC (NP-complete problems)? a. P = NP = NPC b. P = NP NPC c. NPC NP P d. NPC NP P e. NPC NP P 12. King Arthur expects 150 knights for an annual dinner at Camelot. Unfortu- nately, some of the knights quarrel with each other, and Arthur knows who quarrels with whom. Arthur wants to seat his guests around a table so that no two quarreling knights sit next to each other. a. Which standard problem can be used to model King Arthur’s task? b. As a research project, find a proof that Arthur’s problem has a solution if each knight does not quarrel with at least 75 other knights. 412 Limitations of Algorithm Power 11.4 Challenges of Numerical Algorithms Numerical analysis is usually described as the branch of computer science con- cerned with algorithms for solving mathematical problems. This description needs an important clarification: the problems in question are problems of “continuous” mathematics—solving equations and systems of equations, evaluating such func- tions as sin x and ln x,computing integrals, and so on—as opposed to problems of discrete mathematics dealing with such structures as graphs, trees, permutations, and combinations. Our interest in efficient algorithms for mathematical problems stems from the fact that these problems arise as models of many real-life phe- nomena both in the natural world and in the social sciences. In fact, numerical analysis used to be the main area of research, study, and application of computer science. With the rapid proliferation of computers in business and everyday-life applications, which deal primarily with storage and retrieval of information, the relative importance of numerical analysis has shrunk in the last 30 years. However, its applications, enhanced by the power of modern computers, continue to expand in all areas of fundamental research and technology. Thus, wherever one’s inter- ests lie in the wide world of modern computing, it is important to have at least some understanding of the special challenges posed by continuous mathematical problems. We are not going to discuss the variety of difficulties posed by modeling, the task of describing a real-life phenomenon in mathematical terms. Assuming that this has already been done, what principal obstacles to solving a mathematical problem do we face? The first major obstacle is the fact that most numerical analy- sis problems cannot be solved exactly.4 They have to be solved approximately, and this is usually done by replacing an infinite object by a finite approximation. For example, the value of ex at a given point x can be computed by approximating its infinite Taylor’s series about x = 0 by a finite sum of its first terms, called the nth-degree Taylor polynomial: ex ≈ 1 + x + x2 2! + ...+ xn n! . (11.6) To give another example, the definite integral of a function can be approximated by a finite weighted sum of its values, as in the composite trapezoidal rule that you might remember from your calculus class: b a f(x)dx≈ h 2 [f(a)+ 2 n−1 i=1 f(xi) + f(b)], (11.7) where h = (b − a)/n, xi = a + ih for i = 0, 1,...,n(Figure 11.8). The errors of such approximations are called truncation errors. One of the major tasks in numerical analysis is to estimate the magnitudes of truncation 4. Solving a system of linear equations and polynomial evaluation, discussed in Sections 6.2 and 6.5, respectively, are rare exceptions to this rule. 11.4 Challenges of Numerical Algorithms 413 x b hhhh a x1 xi –1 xn –1xi +1xi FIGURE 11.8 Composite trapezoidal rule. errors. This is typically done by using calculus tools, from elementary to quite advanced. For example, for approximation (11.6) we have |ex − [1 + x + x2 2! + ...+ xn n!]|≤ M (n + 1)! |x|n+1, (11.8) where M = max eξ on the segment with the endpoints at 0 and x. This formula makes it possible to determine the degree of Taylor’s polynomial needed to guar- antee a predefined accuracy level of approximation (11.6). For example, if we want to compute e0.5 by formula (11.6) and guarantee the truncation error to be smaller than 10−4, we can proceed as follows. First, we estimate M of formula (11.8): M = max 0≤ξ≤0.5 eξ ≤ e0.5 < 2. Using this bound and the desired accuracy level of 10−4, we obtain from (11.8) M (n + 1)! |0.5|n+1 < 2 (n + 1)!0.5n+1 < 10−4. To solve the last inequality, we can compute the first few values of 2 (n + 1)!0.5n+1 = 2−n (n + 1)! to see that the smallest value of n for which this inequality holds is 5. Similarly, for approximation (11.7), the standard bound of the truncation error is given by the inequality | b a f(x)dx− h 2 [f(a)+ 2 n−1 i=1 f(xi) + f(b)]|≤(b − a)h2 12 M2, (11.9) 414 Limitations of Algorithm Power where M2 = max |f (x)| on the interval a ≤ x ≤ b. You are asked to use this inequality in the exercises for this section (Problems 5 and 6). The other type of errors, called round-off errors, are caused by the limited accuracy with which we can represent real numbers in a digital computer. These errors arise not only for all irrational numbers (which, by definition, require an infinite number of digits for their exact representation) but for many rational numbers as well. In the overwhelming majority of situations, real numbers are represented as floating-point numbers, ±.d1d2 ...dp . BE, (11.10) where B is the number base, usually 2 or 16 (or, for unsophisticated calculators, 10); d1,d2, ...,dp are digits (0 ≤ di 0 unless the number is 0) representing together the fractional part of the number and called its mantissa; and E is an integer exponent with the range of values approximately symmetric about 0. The accuracy of the floating-point representation depends on the number of significant digits p in representation (11.10). Most computers permit two or even three levels of precision: single precision (typically equivalent to between 6 and 7 significant decimal digits), double precision (13 to 14 significant decimal digits), and extended precision (19 to 20 significant decimal digits). Using higher- precision arithmetic slows computations but may help to overcome some of the problems caused by round-off errors. Higher precision may need to be used only for a particular step of the algorithm in question. As with an approximation of any kind, it is important to distinguish between the absolute error and the relative error of representing a number α∗ by its approximation α: absolute error =|α − α∗|, (11.11) relative error = |α − α∗| |α∗| . (11.12) (The relative error is undefined if α∗ = 0.) Very large and very small numbers cannot be represented in floating-point arithmetic because of the phenomena called overflow and underflow, respec- tively. An overflow happens when an arithmetic operation yields a result out- side the range of the computer’s floating-point numbers. Typical examples of overflow arise from the multiplication of large numbers or division by a very small number. Sometimes we can eliminate this problem by making a simple change in the order in which an expression is evaluated (e.g., (1029 . 1130)/1230 = 1029 . (11/12)30), by replacing an expression with an equal one (e.g., computing 100 2 not as 100!/(2!(100 − 2)!) but as (100 . 99)/2), or by computing a logarithm of an expression instead of the expression itself. Underflow occurs when the result of an operation is a nonzero fraction of such a small magnitude that it cannot be represented as a nonzero floating-point 11.4 Challenges of Numerical Algorithms 415 number. Usually, underflow numbers are replaced by zero, but a special signal is generated by hardware to indicate such an event has occurred. It is important to remember that, in addition to inaccurate representation of numbers, the arithmetic operations performed in a computer are not always exact, either. In particular, subtracting two nearly equal floating-point numbers may cause a large increase in relative error. This phenomenon is called subtractive cancellation. EXAMPLE 1 Consider two irrational numbers α∗ = π = 3.14159265 ... and β∗ = π − 6 . 10−7 = 3.14159205 ... represented by floating-point numbers α = 0.3141593 . 101 and β = 0.3141592 . 101, respectively. The relative errors of these approximations are small: |α − α∗| α∗ = 0.0000003 ... π < 4 310−7 and |β − β∗| β∗ = 0.00000005 ... π − 6 . 10−7 < 1 310−7, respectively. The relative error of representing the difference γ ∗ = α∗ − β∗ by the difference of the floating-point representations γ = α − β is |γ − γ ∗| γ ∗ = 10−6 − 6 . 10−7 6 . 10−7 = 2 3 , which is very large for a relative error despite quite accurate approximations for both α and β. Note that we may get a significant magnification of round-off error if a low- accuracy difference is used as a divisor. (We already encountered this problem in discussing Gaussian elimination in Section 6.2. Our solution there was to use partial pivoting.) Many numerical algorithms involve thousands or even millions of arithmetic operations for typical inputs. For such algorithms, the propagation of round-off errors becomes a major concern from both the practical and theoretical standpoints. For some algorithms, round-off errors can propagate through the algorithm’s operations with increasing effect. This highly undesirable property of a numerical algorithm is called instability. Some problems exhibit such a high level of sensitivity to changes in their input that it is all but impossible to design a stable algorithm to solve them. Such problems are called ill-conditioned. EXAMPLE 2 Consider the following system of two linear equations in two unknowns: 1.001x + 0.999y = 2 0.999x + 1.001y = 2. 416 Limitations of Algorithm Power Its only solution is x = 1,y= 1. To see how sensitive this system is to small changes to its right-hand side, consider the system with the same coefficient matrix but slightly different right-hand side values: 1.001x + 0.999y = 2.002 0.999x + 1.001y = 1.998. The only solution to this system is x = 2,y= 0, which is quite far from the solution to the previous system. Note that the coefficient matrix of this system is close to being singular (why?). Hence, a minor change in its coefficients may yield a system with either no solutions or infinitely many solutions, depending on its right-hand- side values. You can find a more formal and detailed discussion of how we can measure the degree of ill-condition of the coefficient matrix in numerical analysis textbooks (e.g., [Ger03]). We conclude with a well-known problem of finding real roots of the quadratic equation ax2 + bx + c = 0 (11.13) for any real coefficients a,b, and c(a= 0). According to secondary-school algebra, equation (11.13) has real roots if and only if its discriminant D = b2 − 4ac is nonnegative, and these roots can be found by the following formula x1,2 = −b ± b2 − 4ac 2a . (11.14) Although formula (11.14) provides a complete solution to the posed problem as far as a mathematician is concerned, it is far from being a complete solution for an algorithm designer. The first major obstacle is evaluating the square root. Even for most positive integers D, √ D is an irrational number that can be computed only approximately. There is a method of computing square roots that is much better than the one commonly taught in secondary school. (It follows from New- ton’s method, a very important algorithm for solving equations, which we discuss in Section 12.4.) This method generates the sequence {xn} of approximations to√ D, where D is a given nonnegative number, according to the formula xn+1 = 1 2 (xn + D xn ) for n = 0, 1,..., (11.15) where the initial approximation x0 can be chosen, among other possibilities, as x0 = (1 + D)/2. It is not difficult to prove that sequence (11.15) is decreasing (if D = 1) and always converges to √ D. We can stop generating its elements either when the difference between its two consecutive elements is less than a predefined error tolerance >0 xn − xn+1 < 11.4 Challenges of Numerical Algorithms 417 or when x2 n+1 is sufficiently close to D.Approximation sequence (11.15) converges very fast to √ D for most values of D. In particular, one can prove that if 0.25 ≤ D<1, then no more than four iterations are needed to guarantee that |xn − √ D| < 4 . 10−15, and we can always scale a given value of d to one in the interval [0.25, 1) by the formula d = D2p, where p is an even integer. EXAMPLE 3 Let us apply Newton’s algorithm to compute √ 2. (For simplicity, we ignore scaling.) We will round off the numbers to six decimal places and use the standard numerical analysis notation .= to indicate the round-offs. x0 = 1 2 (1 + 2) = 1.500000, x1 = 1 2 (x0 + 2 x0 ) .= 1.416667, x2 = 1 2 (x1 + 2 x1 ) .= 1.414216, x3 = 1 2 (x2 + 2 x2 ) .= 1.414214, x4 = 1 2 (x3 + 2 x3 ) .= 1.414214. At this point we have to stop because x4 = x3 .= 1.414214 and hence all other approximations will be the same. The exact value of √ 2is1.41421356 .... With the issue of computing square roots squared away (I do not know whether or not the pun was intended), are we home free to write a program based on formula (11.14)? The answer is no because of the possible impact of round-off errors. Among other obstacles, we are faced here with the menace of subtractive cancellation. If b2 is much larger than 4ac, b2 − 4ac will be very close to |b|, and a root computed by formula (11.14) might have a large relative error. EXAMPLE 4 Let us follow a paper by George Forsythe5 [For69] and consider the equation x2 − 105x + 1 = 0. Its true roots to 11 significant digits are x∗ 1 .= 99999.999990 5. George E. Forsythe (1917–1972), a noted numerical analyst, played a leading role in establishing computer science as a separate academic discipline in the United States. It is his words that are used as the epigraph to this book’s preface. 418 Limitations of Algorithm Power and x∗ 2 .= 0.000010000000001. If we use formula (11.14) and perform all the computations in decimal floating- point arithmetic with, say, seven significant digits, we obtain (−b)2 = 0.1000000 . 1011, 4ac = 0.4000000 . 101, D .= 0.1000000 . 1011,√ D .= 0.1000000 . 106, x1 .= −b + √ D 2a .= 0.1000000 . 106, x2 .= −b − √ D 2a .= 0. And although the relative error of approximating x∗ 1 by x1 is very small, for the second root it is very large: |x2 − x∗ 2| x∗ 2 = 1 (i.e., 100%) To avoid the possibility of subtractive cancellation in formula (11.14), we can use instead another formula, obtained as follows: x1 = −b + b2 − 4ac 2a = (−b + b2 − 4ac)(−b − b2 − 4ac) 2a(−b − b2 − 4ac) = 2c −b − b2 − 4ac , with no danger of subtractive cancellation in the denominator if b>0. As to x2, it can be computed by the standard formula x2 = −b − b2 − 4ac 2a , with no danger of cancellation either for a positive value of b. The case of b<0 is symmetric: we can use the formulas x1 = −b + b2 − 4ac 2a 11.4 Challenges of Numerical Algorithms 419 and x2 = 2c −b + b2 − 4ac . (The case of b = 0 can be considered with either of the other two cases.) There are several other obstacles to applying formula (11.14), which are re- lated to limitations of floating-point arithmetic: if a is very small, division by a can cause an overflow; there seems to be no way to fight the danger of subtractive cancellation in computing b2 − 4ac other than calculating it with double precision; and so on. These problems have been overcome by William Kahan of the Univer- sity of Toronto (see [For69]), and his algorithm is considered to be a significant achievement in the history of numerical analysis. Hopefully, this brief overview has piqued your interest enough for you to seek more information in the many books devoted exclusively to numerical algorithms. In this book, we discuss one more topic in the next chapter: three classic methods for solving equations in one unknown. Exercises 11.4 1. Some textbooks define the number of significant digits in the approximation of number α∗ by number α as the largest nonnegative integer k for which |α − α∗| |α∗| < 5 . 10−k. According to this definition, how many significant digits are there in the approximation of π by a. 3.1415? b. 3.1417? 2. If α = 1.5 is known to approximate some number α∗ with the absolute error not exceeding 10−2, find a. the range of possible values of α∗. b. the range of the relative errors of these approximations. 3. Find the approximate value of √ e = 1.648721 ...obtained by the fifth-degree Taylor’s polynomial about 0 and compute the truncation error of this approx- imation. Does the result agree with the theoretical prediction made in the section? 4. Derive formula (11.7) of the composite trapezoidal rule. 5. Use the composite trapezoidal rule with n = 4 to approximate the following definite integrals. Find the truncation error of each approximation and com- pare it with the one given by formula (11.9). a. 1 0 x2dx b. 3 1 x−1dx 420 Limitations of Algorithm Power 6. If 1 0 esin xdx is to be computed by the composite trapezoidal rule, how large should the number of subintervals be to guarantee a truncation error smaller than 10−4? Smaller than 10−6? 7. Solve the two systems of linear equations and indicate whether they are ill- conditioned. a. 2x + 5y = 7 2x + 5.000001y = 7.000001 b. 2x + 5y = 7 2x + 4.999999y = 7.000002 8. Write a computer program to solve the equation ax2 + bx + c = 0. 9. a. Prove that for any nonnegative number D, the sequence of Newton’s method for computing √ D is strictly decreasing and converges to √ D for any value of the initial approximation x0 > √ D. b. Prove that if 0.25 ≤ D<1 and x0 = (1 + D)/2, no more than four iterations of Newton’s method are needed to guarantee that |xn − √ D| < 4 . 10−15. 10. Apply four iterations of Newton’s method to compute √ 3 and estimate the absolute and relative errors of this approximation. SUMMARY Given a class of algorithms for solving a particular problem, a lower bound indicates the best possible efficiency any algorithm from this class can have. A trivial lower bound is based on counting the number of items in the problem’s input that must be processed and the number of output items that need to be produced. An information-theoretic lower bound is usually obtained through a mecha- nism of decision trees. This technique is particularly useful for comparison- based algorithms for sorting and searching. Specifically: Any general comparison-based sorting algorithm must perform at least log2 n!≈n log2 n key comparisons in the worst case. Any general comparison-based algorithm for searching a sorted array must perform at least log2(n + 1) key comparisons in the worst case. The adversary method for establishing lower bounds is based on following the logic of a malevolent adversary who forces the algorithm into the most time-consuming path. A lower bound can also be established by reduction, i.e., by reducing a problem with a known lower bound to the problem in question. Complexity theory seeks to classify problems according to their computational complexity. The principal split is between tractable and intractable problems— Summary 421 problems that can and cannot be solved in polynomial time, respectively. For purely technical reasons, complexity theory concentrates on decision problems, which are problems with yes/no answers. The halting problem is an example of an undecidable decision problem; i.e., it cannot be solved by any algorithm. P is the class of all decision problems that can be solved in polynomial time. NP is the class of all decision problems whose randomly guessed solutions can be verified in polynomial time. Many important problems in NP (such as the Hamiltonian circuit problem) are known to be NP-complete: all other problems in NP are reducible to such a problem in polynomial time. The first proof of a problem’s NP-completeness was published by S. Cook for the CNF-satisfiability problem. It is not known whether P = NP or P is just a proper subset of NP. This question is the most important unresolved issue in theoretical computer science. A discovery of a polynomial-time algorithm for any of the thousands of known NP-complete problems would imply that P = NP. Numerical analysis is a branch of computer science dealing with solving continuous mathematical problems. Two types of errors occur in solving a majority of such problems: truncation error and round-off error. Truncation errors stem from replacing infinite objects by their finite approximations. Round-off errors are due to inaccuracies of representing numbers in a digital computer. Subtractive cancellation happens as a result of subtracting two near-equal floating-point numbers. It may lead to a sharp increase in the relative round- off error and therefore should be avoided (by either changing the expression’s form or by using a higher precision in computing such a difference). Writing a general computer program for solving quadratic equations ax2 + bx + c = 0 is a difficult task. The problem of computing square roots can be solved by utilizing Newton’s method; the problem of subtractive cancellation can be dealt with by using different formulas depending on whether coefficient b is positive or negative and by computing the discriminant b2 − 4ac with double precision. This page intentionally left blank 12 Coping with the Limitations of Algorithm Power Keep on the lookout for novel ideas that others have used successfully. Your idea has to be original only in its adaptation to the problem you’re working on. —Thomas Edison (1847–1931) As we saw in the previous chapter, there are problems that are difficult to solve algorithmically. At the same time, some of them are so important that we cannot just sigh in resignation and do nothing. This chapter outlines several ways of dealing with such difficult problems. Sections 12.1 and 12.2 introduce two algorithm design techniques—back- tracking and branch-and-bound—that often make it possible to solve at least some large instances of difficult combinatorial problems. Both strategies can be considered an improvement over exhaustive search, discussed in Section 3.4. Unlike exhaustive search, they construct candidate solutions one component at a time and evaluate the partially constructed solutions: if no potential values of the remaining components can lead to a solution, the remaining components are not generated at all. This approach makes it possible to solve some large instances of difficult combinatorial problems, though, in the worst case, we still face the same curse of exponential explosion encountered in exhaustive search. Both backtracking and branch-and-bound are based on the construction of a state-space tree whose nodes reflect specific choices made for a solution’s compo- nents. Both techniques terminate a node as soon as it can be guaranteed that no solution to the problem can be obtained by considering choices that correspond to the node’s descendants. The techniques differ in the nature of problems they can be applied to. Branch-and-bound is applicable only to optimization problems because it is based on computing a bound on possible values of the problem’s objective function. Backtracking is not constrained by this demand, but more often than not, it applies to nonoptimization problems. The other distinction be- tween backtracking and branch-and-bound lies in the order in which nodes of the 423 424 Coping with the Limitations of Algorithm Power state-space tree are generated. For backtracking, this tree is usually developed depth-first (i.e., similar to DFS). Branch-and-bound can generate nodes accord- ing to several rules: the most natural one is the so-called best-first rule explained in Section 12.2. Section 12.3 takes a break from the idea of solving a problem exactly. The algorithms presented there solve problems approximately but fast. Specifically, we consider a few approximation algorithms for the traveling salesman and knap- sack problems. For the traveling salesman problem, we discuss basic theoretical results and pertinent empirical data for several well-known approximation algo- rithms. For the knapsack problem, we first introduce a greedy algorithm and then a parametric family of polynomial-time algorithms that yield arbitrarily good ap- proximations. Section 12.4 is devoted to algorithms for solving nonlinear equations. After a brief discussion of this very important problem, we examine three classic methods for approximate root finding: the bisection method, the method of false position, and Newton’s method. 12.1 Backtracking Throughout the book (see in particular Sections 3.4 and 11.3), we have encoun- tered problems that require finding an element with a special property in a domain that grows exponentially fast (or faster) with the size of the problem’s input: a Hamiltonian circuit among all permutations of a graph’s vertices, the most valu- able subset of items for an instance of the knapsack problem, and the like. We addressed in Section 11.3 the reasons for believing that many such problems might not be solvable in polynomial time. Also recall that we discussed in Section 3.4 how such problems can be solved, at least in principle, by exhaustive search. The exhaustive-search technique suggests generating all candidate solutions and then identifying the one (or the ones) with a desired property. Backtracking is a more intelligent variation of this approach. The principal idea is to construct solutions one component at a time and evaluate such partially constructed candidates as follows. If a partially constructed solution can be de- veloped further without violating the problem’s constraints, it is done by taking the first remaining legitimate option for the next component. If there is no legiti- mate option for the next component, no alternatives for any remaining component need to be considered. In this case, the algorithm backtracks to replace the last component of the partially constructed solution with its next option. It is convenient to implement this kind of processing by constructing a tree of choices being made, called the state-space tree. Its root represents an initial state before the search for a solution begins. The nodes of the first level in the tree represent the choices made for the first component of a solution, the nodes of the second level represent the choices for the second component, and so on. A node in a state-space tree is said to be promising if it corresponds to a partially constructed solution that may still lead to a complete solution; otherwise, 12.1 Backtracking 425 it is called nonpromising. Leaves represent either nonpromising dead ends or complete solutions found by the algorithm. In the majority of cases, a state- space tree for a backtracking algorithm is constructed in the manner of depth- first search. If the current node is promising, its child is generated by adding the first remaining legitimate option for the next component of a solution, and the processing moves to this child. If the current node turns out to be nonpromising, the algorithm backtracks to the node’s parent to consider the next possible option for its last component; if there is no such option, it backtracks one more level up the tree, and so on. Finally, if the algorithm reaches a complete solution to the problem, it either stops (if just one solution is required) or continues searching for other possible solutions. n-Queens Problem As our first example, we use a perennial favorite of textbook writers: the n-queens problem. The problem is to place n queens on an n × n chessboard so that no two queens attack each other by being in the same row or in the same column or on the same diagonal. For n = 1, the problem has a trivial solution, and it is easy to see that there is no solution for n = 2 and n = 3. So let us consider the four-queens problem and solve it by the backtracking technique. Since each of the four queens has to be placed in its own row, all we need to do is to assign a column for each queen on the board presented in Figure 12.1. We start with the empty board and then place queen 1 in the first possible position of its row, which is in column 1 of row 1. Then we place queen 2, after trying unsuccessfully columns 1 and 2, in the first acceptable position for it, which is square (2, 3), the square in row 2 and column 3. This proves to be a dead end because there is no acceptable position for queen 3. So, the algorithm backtracks and puts queen 2 in the next possible position at (2, 4). Then queen 3 is placed at (3, 2), which proves to be another dead end. The algorithm then backtracks all the way to queen 1 and moves it to (1, 2). Queen 2 then goes to (2, 4), queen 3 to (3, 1), and queen 4 to (4, 3), which is a solution to the problem. The state-space tree of this search is shown in Figure 12.2. If other solutions need to be found (how many of them are there for the four- queens problem?), the algorithm can simply resume its operations at the leaf at which it stopped. Alternatively, we can use the board’s symmetry for this purpose. queen 1 queen 2 queen 3 queen 4 1 1 2 2 3 3 4 4 FIGURE 12.1 Board for the four-queens problem. 426 Coping with the Limitations of Algorithm Power 0 1 1 2 11234 12 1234 344 23 Q QQ QQ Q Q Q Q Q Q Q Q Q 321 7 Q Q Q Q 6 5 8 solution FIGURE 12.2 State-space tree of solving the four-queens problem by backtracking. × denotes an unsuccessful attempt to place a queen in the indicated column. The numbers above the nodes indicate the order in which the nodes are generated. Finally, it should be pointed out that a single solution to the n-queens problem for any n ≥ 4 can be found in linear time. In fact, over the last 150 years mathe- maticians have discovered several alternative formulas for nonattacking positions of n queens [Bel09]. Such positions can also be found by applying some general algorithm design strategies (Problem 4 in this section’s exercises). Hamiltonian Circuit Problem As our next example, let us consider the problem of finding a Hamiltonian circuit in the graph in Figure 12.3a. Without loss of generality, we can assume that if a Hamiltonian circuit exists, it starts at vertex a. Accordingly, we make vertex a the root of the state-space 12.1 Backtracking 427 a d b e a b c d de e f f d c e a f fc (a) (b) dead end dead end solution dead end5 4 3 78 6 2 12 11 10 9 1 0 FIGURE 12.3 (a) Graph. (b) State-space tree for finding a Hamiltonian circuit. The numbers above the nodes of the tree indicate the order in which the nodes are generated. tree (Figure 12.3b). The first component of our future solution, if it exists, is a first intermediate vertex of a Hamiltonian circuit to be constructed. Using the alphabet order to break the three-way tie among the vertices adjacent to a, we select vertex b. From b, the algorithm proceeds to c, then to d, then to e, and finally to f, which proves to be a dead end. So the algorithm backtracks from f to e, then to d, and then to c, which provides the first alternative for the algorithm to pursue. Going from c to e eventually proves useless, and the algorithm has to backtrack from e to c and then to b. From there, it goes to the vertices f , e, c, and d, from which it can legitimately return to a, yielding the Hamiltonian circuit a, b, f , e, c, d, a. If we wanted to find another Hamiltonian circuit, we could continue this process by backtracking from the leaf of the solution found. Subset-Sum Problem As our last example, we consider the subset-sum problem: find a subset of a given set A ={a1,...,an} of n positive integers whose sum is equal to a given positive integer d. For example, for A ={1, 2, 5, 6, 8} and d = 9, there are two solutions: {1, 2, 6} and {1, 8}. Of course, some instances of this problem may have no solutions. It is convenient to sort the set’s elements in increasing order. So, we will assume that a1 15 5+7<153+7<159+7>1514+7>15 8<15 0+13<15 solution FIGURE 12.4 Complete state-space tree of the backtracking algorithm applied to the instance A ={3, 5, 6, 7} and d = 15 of the subset-sum problem. The number inside a node is the sum of the elements already included in the subsets represented by the node. The inequality below a leaf indicates the reason for its termination. The state-space tree can be constructed as a binary tree like that in Figure 12.4 for the instance A ={3, 5, 6, 7} and d = 15. The root of the tree represents the starting point, with no decisions about the given elements made as yet. Its left and right children represent, respectively, inclusion and exclusion of a1 in a set being sought. Similarly, going to the left from a node of the first level corresponds to inclusion of a2 while going to the right corresponds to its exclusion, and so on. Thus, a path from the root to a node on the ith level of the tree indicates which of the first i numbers have been included in the subsets represented by that node. We record the value of s, the sum of these numbers, in the node. If s is equal to d, we have a solution to the problem. We can either report this result and stop or, if all the solutions need to be found, continue by backtracking to the node’s parent. If s is not equal to d, we can terminate the node as nonpromising if either of the following two inequalities holds: s + ai+1 >d (the sum s is too large), s + n j=i+1 aj = l of node 11 a 4 lb = 19 a, e 3 lb = 16 a, d 2 a, c 1 lb = 14 a, b 6 lb = 16 a, b, d 5 891011 lb = 16 a, b, c l = 24 a, b, c, d, (e, a) l = 19 a, b, c, e, (d, a) l = 24 a, b, d, c, (e, a) l = 16 a, b, d, e, (c, a) 7 lb = 19 a, b, e XX lb > l of node 11 X lb > l of node 11 X first tour better tour inferior tour optimal tour FIGURE 12.9 (a) Weighted graph. (b) State-space tree of the branch-and-bound algorithm to find a shortest Hamiltonian circuit in this graph. The list of vertices in a node specifies a beginning part of the Hamiltonian circuits represented by the node. Figure 12.9a. To reduce the amount of potential work, we take advantage of two observations made in Section 3.4. First, without loss of generality, we can consider only tours that start at a. Second, because our graph is undirected, we can generate only tours in which b is visited before c. In addition, after visiting n − 1 = 4 cities, a tour has no choice but to visit the remaining unvisited city and return to the starting one. The state-space tree tracing the algorithm’s application is given in Figure 12.9b. The comments we made at the end of the preceding section about the strengths and weaknesses of backtracking are applicable to branch-and-bound as well. To reiterate the main point: these state-space tree techniques enable us to solve many large instances of difficult combinatorial problems. As a rule, however, it is virtually impossible to predict which instances will be solvable in a realistic amount of time and which will not. Incorporation of additional information, such as a symmetry of a game’s board, can widen the range of solvable instances. Along this line, a branch-and- bound algorithm can be sometimes accelerated by a knowledge of the objective 440 Coping with the Limitations of Algorithm Power function’s value of some nontrivial feasible solution. The information might be obtainable—say, by exploiting specifics of the data or even, for some problems, generated randomly—before we start developing a state-space tree. Then we can use such a solution immediately as the best one seen so far rather than waiting for the branch-and-bound processing to lead us to the first feasible solution. In contrast to backtracking, solving a problem by branch-and-bound has both the challenge and opportunity of choosing the order of node generation and find- ing a good bounding function. Though the best-first rule we used above is a sensible approach, it may or may not lead to a solution faster than other strategies. (Arti- ficial intelligence researchers are particularly interested in different strategies for developing state-space trees.) Finding a good bounding function is usually not a simple task. On the one hand, we want this function to be easy to compute. On the other hand, it cannot be too simplistic—otherwise, it would fail in its principal task to prune as many branches of a state-space tree as soon as possible. Striking a proper balance be- tween these two competing requirements may require intensive experimentation with a wide variety of instances of the problem in question. Exercises 12.2 1. What data structure would you use to keep track of live nodes in a best-first branch-and-bound algorithm? 2. Solve the same instance of the assignment problem as the one solved in the section by the best-first branch-and-bound algorithm with the bounding function based on matrix columns rather than rows. 3. a. Give an example of the best-case input for the branch-and-bound algo- rithm for the assignment problem. b. In the best case, how many nodes will be in the state-space tree of the branch-and-bound algorithm for the assignment problem? 4. Write a program for solving the assignment problem by the branch-and-bound algorithm. Experiment with your program to determine the average size of the cost matrices for which the problem is solved in a given amount of time, say, 1 minute on your computer. 5. Solve the following instance of the knapsack problem by the branch-and- bound algorithm: item weight value 1 10 $100 2 7 $63 W = 16 3 8 $56 4 4 $12 12.3 Approximation Algorithms for NP-Hard Problems 441 6. a. Suggest a more sophisticated bounding function for solving the knapsack problem than the one used in the section. b. Use your bounding function in the branch-and-bound algorithm applied to the instance of Problem 5. 7. Write a program to solve the knapsack problem with the branch-and-bound algorithm. 8. a. Prove the validity of the lower bound given by formula (12.2) for instances of the traveling salesman problem with symmetric matrices of integer intercity distances. b. How would you modify lower bound (12.2) for nonsymmetric distance matrices? 9. Apply the branch-and-bound algorithm to solve the traveling salesman prob- lem for the following graph: 2 5 8 7 3 a b 1 c d (We solved this problem by exhaustive search in Section 3.4.) 10. As a research project, write a report on how state-space trees are used for programming such games as chess, checkers, and tic-tac-toe. The two principal algorithms you should read about are the minimax algorithm and alpha-beta pruning. 12.3 Approximation Algorithms for NP-Hard Problems In this section, we discuss a different approach to handling difficult problems of combinatorial optimization, such as the traveling salesman problem and the knapsack problem. As we pointed out in Section 11.3, the decision versions of these problems are NP-complete. Their optimization versions fall in the class of NP-hard problems—problems that are at least as hard as NP-complete problems.2 Hence, there are no known polynomial-time algorithms for these problems, and there are serious theoretical reasons to believe that such algorithms do not exist. What then are our options for handling such problems, many of which are of significant practical importance? 2. The notion of an NP-hard problem can be defined more formally by extending the notion of polynomial reducibility to problems that are not necessarily in class NP, including optimization problems of the type discussed in this section (see [Gar79, Chapter 5]). 442 Coping with the Limitations of Algorithm Power If an instance of the problem in question is very small, we might be able to solve it by an exhaustive-search algorithm (Section 3.4). Some such problems can be solved by the dynamic programming technique we demonstrated in Section 8.2. But even when this approach works in principle, its practicality is limited by dependence on the instance parameters being relatively small. The discovery of the branch-and-bound technique has proved to be an important breakthrough, because this technique makes it possible to solve many large instances of difficult optimization problems in an acceptable amount of time. However, such good performance cannot usually be guaranteed. There is a radically different way of dealing with difficult optimization prob- lems: solve them approximately by a fast algorithm. This approach is particularly appealing for applications where a good but not necessarily optimal solution will suffice. Besides, in real-life applications, we often have to operate with inaccurate data to begin with. Under such circumstances, going for an approximate solution can be a particularly sensible choice. Although approximation algorithms run a gamut in level of sophistication, most of them are based on some problem-specific heuristic. A heuristic is a common-sense rule drawn from experience rather than from a mathematically proved assertion. For example, going to the nearest unvisited city in the traveling salesman problem is a good illustration of this notion. We discuss an algorithm based on this heuristic later in this section. Of course, if we use an algorithm whose output is just an approximation of the actual optimal solution, we would like to know how accurate this approximation is. We can quantify the accuracy of an approximate solution sa to a problem of minimizing some function f by the size of the relative error of this approximation, re(sa) = f(sa) − f(s∗) f(s∗) , where s∗ is an exact solution to the problem. Alternatively, since re(sa) = f(sa)/ f(s∗) − 1, we can simply use the accuracy ratio r(sa) = f(sa) f(s∗) as a measure of accuracy of sa. Note that for the sake of scale uniformity, the accuracy ratio of approximate solutions to maximization problems is usually com- puted as r(sa) = f(s∗) f(sa) to make this ratio greater than or equal to 1, as it is for minimization problems. Obviously, the closer r(sa) is to 1, the better the approximate solution is. For most instances, however, we cannot compute the accuracy ratio, because we typically do not know f(s∗), the true optimal value of the objective function. Therefore, our hope should lie in obtaining a good upper bound on the values of r(sa). This leads to the following definitions. 12.3 Approximation Algorithms for NP-Hard Problems 443 DEFINITION A polynomial-time approximation algorithm is said to be a c- approximation algorithm, where c ≥ 1, if the accuracy ratio of the approximation it produces does not exceed c for any instance of the problem in question: r(sa) ≤ c. (12.3) The best (i.e., the smallest) value of c for which inequality (12.3) holds for all instances of the problem is called the performance ratio of the algorithm and denoted RA. The performance ratio serves as the principal metric indicating the quality of the approximation algorithm. We would like to have approximation algorithms with RA as close to 1 as possible. Unfortunately, as we shall see, some approxima- tion algorithms have infinitely large performance ratios (RA =∞). This does not necessarily rule out using such algorithms, but it does call for a cautious treatment of their outputs. There are two important facts about difficult combinatorial optimization problems worth keeping in mind. First, although the difficulty level of solving most such problems exactly is the same to within a polynomial-time transforma- tion of one problem to another, this equivalence does not translate into the realm of approximation algorithms. Finding good approximate solutions is much easier for some of these problems than for others. Second, some of the problems have special classes of instances that are both particularly important for real-life appli- cations and easier to solve than their general counterparts. The traveling salesman problem is a prime example of this situation. Approximation Algorithms for the Traveling Salesman Problem We solved the traveling salesman problem by exhaustive search in Section 3.4, mentioned its decision version as one of the most well-known NP-complete problems in Section 11.3, and saw how its instances can be solved by a branch- and-bound algorithm in Section 12.2. Here, we consider several approximation algorithms, a small sample of dozens of such algorithms suggested over the years for this famous problem. (For a much more detailed discussion of the topic, see [Law85], [Hoc97], [App07], and [Gut07].) But first let us answer the question of whether we should hope to find a polynomial-time approximation algorithm with a finite performance ratio on all instances of the traveling salesman problem. As the following theorem [Sah76] shows, the answer turns out to be no, unless P = NP. THEOREM 1 If P = NP, there exists no c-approximation algorithm for the traveling salesman problem, i.e., there exists no polynomial-time approximation algorithm for this problem so that for all instances f(sa) ≤ cf (s ∗) for some constant c. 444 Coping with the Limitations of Algorithm Power PROOF By way of contradiction, suppose that such an approximation algorithm A and a constant c exist. (Without loss of generality, we can assume that c is a positive integer.) We will show that this algorithm could then be used for solving the Hamiltonian circuit problem in polynomial time. We will take advantage of a variation of the transformation used in Section 11.3 to reduce the Hamiltonian circuit problem to the traveling salesman problem. Let G be an arbitrary graph with n vertices. We map G to a complete weighted graph G by assigning weight 1 to each edge in G and adding an edge of weight cn + 1 between each pair of vertices not adjacent in G.IfG has a Hamiltonian circuit, its length in G is n; hence, it is the exact solution s∗ to the traveling salesman problem for G . Note that if sa is an approximate solution obtained for G by algorithm A, then f(sa) ≤ cn by the assumption. If G does not have a Hamiltonian circuit in G, the shortest tour in G will contain at least one edge of weight cn + 1, and hence f(sa) ≥ f(s∗)>cn. Taking into account the two derived inequalities, we could solve the Hamiltonian circuit problem for graph G in polynomial time by mapping G to G , applying algorithm A to get tour sa in G , and comparing its length with cn. Since the Hamiltonian circuit problem is NP-complete, we have a contradiction unless P = NP. Greedy Algorithms for the TSP The simplest approximation algorithms for the traveling salesman problem are based on the greedy technique. We will discuss here two such algorithms. Nearest-neighbor algorithm The following well-known greedy algorithm is based on the nearest-neighbor heuristic: always go next to the nearest unvisited city. Step 1 Choose an arbitrary city as the start. Step 2 Repeat the following operation until all the cities have been visited: go to the unvisited city nearest the one visited last (ties can be broken arbitrarily). Step 3 Return to the starting city. EXAMPLE 1 For the instance represented by the graph in Figure 12.10, with a as the starting vertex, the nearest-neighbor algorithm yields the tour (Hamiltonian circuit) sa: a − b − c − d − a of length 10. 1 6 3 3 2 a b 1 d c FIGURE 12.10 Instance of the traveling salesman problem. 12.3 Approximation Algorithms for NP-Hard Problems 445 The optimal solution, as can be easily checked by exhaustive search, is the tour s∗: a − b − d − c − a of length 8. Thus, the accuracy ratio of this approximation is r(sa) = f(sa) f(s∗) = 10 8 = 1.25 (i.e., tour sa is 25% longer than the optimal tour s∗). Unfortunately, except for its simplicity, not many good things can be said about the nearest-neighbor algorithm. In particular, nothing can be said in general about the accuracy of solutions obtained by this algorithm because it can force us to traverse a very long edge on the last leg of the tour. Indeed, if we change the weight of edge (a, d) from 6 to an arbitrary large number w ≥ 6 in Example 1, the algorithm will still yield the tour a − b − c − d − a of length 4 + w, and the optimal solution will still be a − b − d − c − a of length 8. Hence, r(sa) = f(sa) f(s∗) = 4 + w 8 , which can be made as large as we wish by choosing an appropriately large value of w. Hence, RA =∞for this algorithm (as it should be according to Theorem 1). Multifragment-heuristic algorithm Another natural greedy algorithm for the traveling salesman problem considers it as the problem of finding a minimum-weight collection of edges in a given complete weighted graph so that all the vertices have degree 2. (With this emphasis on edges rather than vertices, what other greedy algorithm does it remind you of?) An application of the greedy technique to this problem leads to the following algorithm [Ben90]. Step 1 Sort the edges in increasing order of their weights. (Ties can be broken arbitrarily.) Initialize the set of tour edges to be constructed to the empty set. Step 2 Repeat this step n times, where n is the number of cities in the instance being solved: add the next edge on the sorted edge list to the set of tour edges, provided this addition does not create a vertex of degree 3 or a cycle of length less than n; otherwise, skip the edge. Step 3 Return the set of tour edges. As an example, applying the algorithm to the graph in Figure 12.10 yields {(a, b), (c, d), (b, c), (a, d)}. This set of edges forms the same tour as the one pro- duced by the nearest-neighbor algorithm. In general, the multifragment-heuristic algorithm tends to produce significantly better tours than the nearest-neighbor algorithm, as we are going to see from the experimental data quoted at the end of this section. But the performance ratio of the multifragment-heuristic algorithm is also unbounded, of course. 446 Coping with the Limitations of Algorithm Power There is, however, a very important subset of instances, called Euclidean, for which we can make a nontrivial assertion about the accuracy of both the nearest- neighbor and multifragment-heuristic algorithms. These are the instances in which intercity distances satisfy the following natural conditions: triangle inequality d[i, j] ≤ d[i, k] + d[k, j] for any triple of cities i, j, and k (the distance between cities i and j cannot exceed the length of a two-leg path from i to some intermediate city k to j) symmetry d[i, j] = d[j, i] for any pair of cities i and j (the distance from i to j is the same as the distance from j to i) A substantial majority of practical applications of the traveling salesman prob- lem are its Euclidean instances. They include, in particular, geometric ones, where cities correspond to points in the plane and distances are computed by the standard Euclidean formula. Although the performance ratios of the nearest-neighbor and multifragment-heuristic algorithms remain unbounded for Euclidean instances, their accuracy ratios satisfy the following inequality for any such instance with n ≥ 2 cities: f(sa) f(s∗) ≤ 1 2 (log2 n+1), where f(sa) and f(s∗) are the lengths of the heuristic tour and shortest tour, respectively (see [Ros77] and [Ong84]). Minimum-Spanning-Tree–Based Algorithms There are approximation algori- thms for the traveling salesman problem that exploit a connection between Hamil- tonian circuits and spanning trees of the same graph. Since removing an edge from a Hamiltonian circuit yields a spanning tree, we can expect that the structure of a minimum spanning tree provides a good basis for constructing a shortest tour approximation. Here is an algorithm that implements this idea in a rather straight- forward fashion. Twice-around-the-tree algorithm Step 1 Construct a minimum spanning tree of the graph corresponding to a given instance of the traveling salesman problem. Step 2 Starting at an arbitrary vertex, perform a walk around the minimum spanning tree recording all the vertices passed by. (This can be done by a DFS traversal.) Step 3 Scan the vertex list obtained in Step 2 and eliminate from it all repeated occurrences of the same vertex except the starting one at the end of the list. (This step is equivalent to making shortcuts in the walk.) The vertices remaining on the list will form a Hamiltonian circuit, which is the output of the algorithm. EXAMPLE 2 Let us apply this algorithm to the graph in Figure 12.11a. The minimum spanning tree of this graph is made up of edges (a, b), (b, c), (b, d), and (d, e) (Figure 12.11b). A twice-around-the-tree walk that starts and ends at a is 12.3 Approximation Algorithms for NP-Hard Problems 447 12 9 48 9 711 a e 8 610 b d c (a) a e b d c (b) FIGURE 12.11 Illustration of the twice-around-the-tree algorithm. (a) Graph. (b) Walk around the minimum spanning tree with the shortcuts. a, b, c, b, d, e, d, b, a. Eliminating the second b (a shortcut from c to d), the second d, and the third b (a shortcut from e to a) yields the Hamiltonian circuit a, b, c, d, e, a of length 39. The tour obtained in Example 2 is not optimal. Although that instance is small enough to find an optimal solution by either exhaustive search or branch-and- bound, we refrained from doing so to reiterate a general point. As a rule, we do not know what the length of an optimal tour actually is, and therefore we cannot compute the accuracy ratio f(sa)/f (s∗). For the twice-around-the-tree algorithm, we can at least estimate it above, provided the graph is Euclidean. THEOREM 2 The twice-around-the-tree algorithm is a 2-approximation algo- rithm for the traveling salesman problem with Euclidean distances. PROOF Obviously, the twice-around-the-tree algorithm is polynomial time if we use a reasonable algorithm such as Prim’s or Kruskal’s in Step 1. We need to show that for any Euclidean instance of the traveling salesman problem, the length of a tour sa obtained by the twice-around-the-tree algorithm is at most twice the length of the optimal tour s∗, i.e., f(sa) ≤ 2f(s∗). Since removing any edge from s∗ yields a spanning tree T of weight w(T ), which must be greater than or equal to the weight of the graph’s minimum spanning tree w(T ∗), we get the inequality f(s∗)>w(T)≥ w(T ∗). 448 Coping with the Limitations of Algorithm Power This inequality implies that 2f(s∗)>2w(T ∗) = the length of the walk obtained in Step 2 of the algorithm. The possible shortcuts outlined in Step 3 of the algorithm to obtain sa cannot increase the total length of the walk in a Euclidean graph, i.e., the length of the walk obtained in Step 2 ≥ the length of the tour sa. Combining the last two inequalities, we get the inequality 2f(s∗)>f(sa), which is, in fact, a slightly stronger assertion than the one we needed to prove. Christofides Algorithm There is an approximation algorithm with a better per- formance ratio for the Euclidean traveling salesman problem—the well-known Christofides algorithm [Chr76]. It also uses a minimum spanning tree but does this in a more sophisticated way than the twice-around-the-tree algorithm. Note that a twice-around-the-tree walk generated by the latter algorithm is an Eule- rian circuit in the multigraph obtained by doubling every edge in the graph given. Recall that an Eulerian circuit exists in a connected multigraph if and only if all its vertices have even degrees. The Christofides algorithm obtains such a multi- graph by adding to the graph the edges of a minimum-weight matching of all the odd-degree vertices in its minimum spanning tree. (The number of such vertices is always even and hence this can always be done.) Then the algorithm finds an Eulerian circuit in the multigraph and transforms it into a Hamiltonian circuit by shortcuts, exactly the same way it is done in the last step of the twice-around-the- tree algorithm. EXAMPLE 3 Let us trace the Christofides algorithm in Figure 12.12 on the same instance (Figure 12.12a) used for tracing the twice-around-the-tree algorithm in Figure 12.11. The graph’s minimum spanning tree is shown in Figure 12.12b. It has four odd-degree vertices: a, b, c, and e. The minimum-weight matching of these four vertices consists of edges (a, b) and (c, e). (For this tiny instance, it can be found easily by comparing the total weights of just three alternatives: (a, b) and (c, e), (a, c) and (b, e), (a, e) and (b, c).) The traversal of the multigraph, starting at vertex a, produces the Eulerian circuit a − b − c − e − d − b − a, which, after one shortcut, yields the tour a − b − c − e − d − a of length 37. The performance ratio of the Christofides algorithm on Euclidean instances is 1.5 (see, e.g., [Pap82]). It tends to produce significantly better approximations to optimal tours than the twice-around-the-tree algorithm does in empirical tests. (We quote some results of such tests at the end of this subsection.) The quality of a tour obtained by this heuristic can be further improved by optimizing shortcuts made on the last step of the algorithm as follows: examine the multiply-visited cities in some arbitrary order and for each make the best possible shortcut. This 12.3 Approximation Algorithms for NP-Hard Problems 449 12 9 48 9 711 a e 8 610 b d c (a) a b e d c 8 6 44711 (b) a b e d c 6 47911 (c) FIGURE 12.12 Application of the Christofides algorithm. (a) Graph. (b) Minimum spanning tree with added edges (in dash) of a minimum-weight matching of all odd-degree vertices. (c) Hamiltonian circuit obtained. enhancement would have not improved the tour a − b − c − e − d − a obtained in Example 3 from a − b − c − e − d − b − a because shortcutting the second occur- rence of b happens to be better than shortcutting its first occurrence. In general, however, this enhancement tends to decrease the gap between the heuristic and optimal tour lengths from about 15% to about 10%, at least for randomly gener- ated Euclidean instances [Joh07a]. Local Search Heuristics For Euclidean instances, surprisingly good approxima- tions to optimal tours can be obtained by iterative-improvement algorithms, which are also called local search heuristics. The best-known of these are the 2-opt, 3- opt, and Lin-Kernighan algorithms. These algorithms start with some initial tour, e.g., constructed randomly or by some simpler approximation algorithm such as the nearest-neighbor. On each iteration, the algorithm explores a neighborhood around the current tour by replacing a few edges in the current tour by other edges. If the changes produce a shorter tour, the algorithm makes it the current 450 Coping with the Limitations of Algorithm Power C4 C1 C2 C3 (a) C1 C2 C4 C3 (b) FIGURE 12.13 2-change: (a) Original tour. (b) New tour. tour and continues by exploring its neighborhood in the same manner; otherwise, the current tour is returned as the algorithm’s output and the algorithm stops. The 2-opt algorithm works by deleting a pair of nonadjacent edges in a tour and reconnecting their endpoints by the different pair of edges to obtain another tour (see Figure 12.13). This operation is called the 2-change. Note that there is only one way to reconnect the endpoints because the alternative produces two disjoint fragments. EXAMPLE 4 If we start with the nearest-neighbor tour a − b − c − d − e − a in the graph of Figure 12.11, whose length lnn is equal to 39, the 2-opt algorithm will move to the next tour as shown in Figure 12.14. To generalize the notion of the 2-change, one can consider the k-change for any k ≥ 2. This operation replaces up to k edges in a current tour. In addition to 2-changes, only the 3-changes have proved to be of practical interest. The two principal possibilities of 3-changes are shown in Figure 12.15. There are several other local search algorithms for the traveling salesman problem. The most prominent of them is the Lin-Kernighan algorithm [Lin73], which for two decades after its publication in 1973 was considered the best algo- rithm to obtain high-quality approximations of optimal tours. The Lin-Kernighan algorithm is a variable-opt algorithm: its move can be viewed as a 3-opt move followed by a sequence of 2-opt moves. Because of its complexity, we have to re- frain from discussing this algorithm here. The excellent survey by Johnson and McGeoch [Joh07a] contains an outline of the algorithm and its modern exten- sions as well as methods for its efficient implementation. This survey also contain results from the important empirical studies about performance of many heuris- tics for the traveling salesman problem, including of course, the Lin-Kernighan algorithm. We conclude our discussion by quoting some of these data. Empirical Results The traveling salesman problem has been the subject of in- tense study for the last 50 years. This interest was driven by a combination of pure 12.3 Approximation Algorithms for NP-Hard Problems 451 a b e d c 610 47 12 a b e d c 8 6 97 12 l = 42 > lnn = 39 a b e d c 610 47 12 10 a b e d c 6 99 12 l = 46 > lnn = 39 a b e d c 610 4 47 12 10 a b e d c 8 11 12 l = 45 > lnn = 39 a b e d c 610 4 47 12 10 a b e d c 89 7l = 38 < lnn = 39 (new tour) FIGURE 12.14 2-changes from the nearest-neighbor tour of the graph in Figure 12.11. 452 Coping with the Limitations of Algorithm Power C1 C4C5 C3 C6 C2 (a) C1 C2 C3 C4C5 C6 (b) C1 C2 C3 C4C5 C6 (c) FIGURE 12.15 3-change: (a) Original tour. (b), (c) New tours. theoretical interest and serious practical needs stemming from such newer ap- plications as circuit-board and VLSI-chip fabrication, X-ray crystallography, and genetic engineering. Progress in developing effective heuristics, their efficient im- plementation by using sophisticated data structures, and the ever-increasing power of computers have led to a situation that differs drastically from a pessimistic pic- ture painted by the worst-case theoretical results. This is especially true for the most important applications class of instances of the traveling salesman problem: points in the two-dimensional plane with the standard Euclidean distances be- tween them. Nowadays, Euclidean instances with up to 1000 cities can be solved exactly in quite a reasonable amount of time—typically, in minutes or faster on a good workstation—by such optimization packages as Concord [App]. In fact, according to the information on the Web site maintained by the authors of that package, the largest instance of the traveling salesman problem solved exactly as of January 2010 was a tour through 85,900 points in a VLSI application. It significantly ex- ceeded the previous record of the shortest tour through all 24,978 cities in Sweden. There should be little doubt that the latest record will also be eventually super- seded and our ability to solve ever larger instances exactly will continue to expand. This remarkable progress does not eliminate the usefulness of approximation al- gorithms for such problems, however. First, some applications lead to instances that are still too large to be solved exactly in a reasonable amount of time. Second, one may well prefer spending seconds to find a tour that is within a few percent of optimum than to spend many hours or even days of computing time to find the shortest tour exactly. But how can one tell how good or bad the approximate solution is if we do not know the length of an optimal tour? A convenient way to overcome this difficulty is to solve the linear programming problem describing the instance in question by ignoring the integrality constraints. This provides a lower bound—called the Held- Karp bound—on the length of the shortest tour. The Held-Karp bound is typically very close (less than 1%) to the length of an optimal tour, and this bound can be computed in seconds or minutes unless the instance is truly huge. Thus, for a tour 12.3 Approximation Algorithms for NP-Hard Problems 453 TABLE 12.1 Average tour quality and running times for various heuristics on the 10,000-city random uniform Euclidean instances [Joh07a] % excess over the Running time Heuristic Held-Karp bound (seconds) nearest neighbor 24.79 0.28 multifragment 16.42 0.20 Christofides 9.81 1.04 2-opt 4.70 1.41 3-opt 2.88 1.50 Lin-Kernighan 2.00 2.06 sa obtained by some heuristic, we estimate the accuracy ratio r(sa) = f(sa)/f (s∗) from above by the ratio f(sa)/H K(s∗), where f(sa) is the length of the heuristic tour sa and HK(s∗) is the Held-Karp lower bound on the shortest-tour length. The results (see Table 12.1) from a large empirical study [Joh07a] indicate the average tour quality and running times for the discussed heuristics.3 The instances in the reported sample have 10,000 cities generated randomly and uniformly as integral-coordinate points in the plane, with the Euclidean distances rounded to the nearest integer. The quality of tours generated by the heuristics remain about the same for much larger instances (up to a million cities) as long as they belong to the same type of instances. The running times quoted are for expert implementations run on a Compaq ES40 with 500 Mhz Alpha processors and 2 gigabytes of main memory or its equivalents. Asymmetric instances of the traveling salesman problem—i.e., those with a nonsymmetic matrix of intercity distances—have proved to be significantly harder to solve, both exactly and approximately, than Euclidean instances. In partic- ular, exact optimal solutions for many 316-city asymmetric instances remained unknown at the time of the state-of-the-art survey by Johnson et al. [Joh07b]. Approximation Algorithms for the Knapsack Problem The knapsack problem, another well-known NP-hard problem, was also intro- duced in Section 3.4: given n items of known weights w1,...,wn and values v1,...,vn and a knapsack of weight capacity W, find the most valuable sub- set of the items that fits into the knapsack. We saw how this problem can be solved by exhaustive search (Section 3.4), dynamic programming (Section 8.2), 3. We did not include the results for the twice-around-the-tree heuristic because of the inferior quality of its approximations with the average excess of about 40%. Nor did we quote the results for the most sophisticated local search heuristics with the average excess over optimum of less than a fraction of 1%. 454 Coping with the Limitations of Algorithm Power and branch-and-bound (Section 12.2). Now we will solve this problem by approx- imation algorithms. Greedy Algorithms for the Knapsack Problem We can think of several greedy approaches to this problem. One is to select the items in decreasing order of their weights; however, heavier items may not be the most valuable in the set. Alternatively, if we pick up the items in decreasing order of their value, there is no guarantee that the knapsack’s capacity will be used efficiently. Can we find a greedy strategy that takes into account both the weights and values? Yes, we can, by computing the value-to-weight ratios vi/wi,i= 1, 2,...,n,and selecting the items in decreasing order of these ratios. (In fact, we already used this approach in designing the branch-and-bound algorithm for the problem in Section 12.2.) Here is the algorithm based on this greedy heuristic. Greedy algorithm for the discrete knapsack problem Step 1 Compute the value-to-weight ratios ri = vi/wi,i= 1,...,n,for the items given. Step 2 Sort the items in nonincreasing order of the ratios computed in Step 1. (Ties can be broken arbitrarily.) Step 3 Repeat the following operation until no item is left in the sorted list: if the current item on the list fits into the knapsack, place it in the knapsack and proceed to the next item; otherwise, just proceed to the next item. EXAMPLE 5 Let us consider the instance of the knapsack problem with the knapsack capacity 10 and the item information as follows: item weight value 1 7 $42 2 3 $12 3 4 $40 4 5 $25 Computing the value-to-weight ratios and sorting the items in nonincreasing order of these efficiency ratios yields item weight value value/weight 1 4 $40 10 2 7 $42 6 3 5 $25 5 4 3 $12 4 12.3 Approximation Algorithms for NP-Hard Problems 455 The greedy algorithm will select the first item of weight 4, skip the next item of weight 7, select the next item of weight 5, and skip the last item of weight 3. The solution obtained happens to be optimal for this instance (see Section 12.2, where we solved the same instance by the branch-and-bound algorithm). Does this greedy algorithm always yield an optimal solution? The answer, of course, is no: if it did, we would have a polynomial-time algorithm for the NP- hard problem. In fact, the following example shows that no finite upper bound on the accuracy of its approximate solutions can be given either. EXAMPLE 6 item weight value value/weight 1 1 2 2 The knapsack capacity is W>2. 2 WW 1 Since the items are already ordered as required, the algorithm takes the first item and skips the second one; the value of this subset is 2. The optimal selection con- sists of item 2 whose value is W.Hence, the accuracy ratio r(sa) of this approximate solution is W/2, which is unbounded above. It is surprisingly easy to tweak this greedy algorithm to get an approximation algorithm with a finite performance ratio. All it takes is to choose the better of two alternatives: the one obtained by the greedy algorithm or the one consisting of a single item of the largest value that fits into the knapsack. (Note that for the instance of the preceding example, the second alternative is better than the first one.) It is not difficult to prove that the performance ratio of this enhanced greedy algorithm is 2. That is, the value of an optimal subset s∗ will never be more than twice as large as the value of the subset sa obtained by this enhanced greedy algorithm, and 2 is the smallest multiple for which such an assertion can be made. It is instructive to consider the continuous version of the knapsack problem as well. In this version, we are permitted to take arbitrary fractions of the items given. For this version of the problem, it is natural to modify the greedy algorithm as follows. Greedy algorithm for the continuous knapsack problem Step 1 Compute the value-to-weight ratios vi/wi,i= 1,...,n,for the items given. Step 2 Sort the items in nonincreasing order of the ratios computed in Step 1. (Ties can be broken arbitrarily.) Step 3 Repeat the following operation until the knapsack is filled to its full capacity or no item is left in the sorted list: if the current item on the list fits into the knapsack in its entirety, take it and proceed to the next item; otherwise, take its largest fraction to fill the knapsack to its full capacity and stop. 456 Coping with the Limitations of Algorithm Power For example, for the four-item instance used in Example 5 to illustrate the greedy algorithm for the discrete version, the algorithm will take the first item of weight 4 and then 6/7 of the next item on the sorted list to fill the knapsack to its full capacity. It should come as no surprise that this algorithm always yields an optimal solution to the continuous knapsack problem. Indeed, the items are ordered according to their efficiency in using the knapsack’s capacity. If the first item on the sorted list has weight w1 and value v1, no solution can use w1 units of capacity with a higher payoff than v1. If we cannot fill the knapsack with the first item or its fraction, we should continue by taking as much as we can of the second- most efficient item, and so on. A formal rendering of this proof idea is somewhat involved, and we will leave it for the exercises. Note also that the optimal value of the solution to an instance of the contin- uous knapsack problem can serve as an upper bound on the optimal value of the discrete version of the same instance. This observation provides a more sophisti- cated way of computing upper bounds for solving the discrete knapsack problem by the branch-and-bound method than the one used in Section 12.2. Approximation Schemes We now return to the discrete version of the knap- sack problem. For this problem, unlike the traveling salesman problem, there exist polynomial-time approximation schemes, which are parametric families of algo- rithms that allow us to get approximations s(k) a with any predefined accuracy level: f(s∗) f(s(k) a ) ≤ 1 + 1/k for any instance of size n, where k is an integer parameter in the range 0 ≤ k0. Since xn is the middle point of [an,bn] and x∗ lies within this interval as well, we have |xn − x∗|≤bn − an 2 . (12.5) Hence, we can stop the algorithm as soon as (bn − an)/2 <εor, equivalently, xn − an <ε. (12.6) It is not difficult to prove that |xn − x∗|≤b1 − a1 2n for n = 1, 2,.... (12.7) This inequality implies that the sequence of approximations {xn} can be made as close to root x∗ as we wish by choosing n large enough. In other words, we can say that {xn} converges to root x∗. Note, however, that because any digital computer represents extremely small values by zero (Section 11.4), the convergence asser- tion is true in theory but not necessarily in practice. In fact, if we choose ε below a certain machine-dependent threshold, the algorithm may never stop! Another source of potential complications is round-off errors in computing values of the function in question. Therefore, it is a good practice to include in a program im- plementing the bisection method a limit on the number of iterations the algorithm is allowed to run. Here is pseudocode of the bisection method. ALGORITHM Bisection(f (x), a, b, eps, N) //Implements the bisection method for finding a root of f(x)= 0 //Input: Two real numbers a and b, a < b, // a continuous function f(x)on [a, b], f (a)f (b) < 0, // an upper bound on the absolute error eps > 0, // an upper bound on the number of iterations N 462 Coping with the Limitations of Algorithm Power //Output: An approximate (or exact) value x of a root in (a, b) //or an interval bracketing the root if the iteration number limit is reached n ← 1 //iteration count while n ≤ N do x ← (a + b)/2 if x − alog2 b1 − a1 ε , (12.8) does the trick. EXAMPLE 1 Let us consider equation x3 − x − 1 = 0. (12.9) It has one real root. (See Figure 12.18 for the graph of f(x)= x3 − x − 1.) Since f(0)<0 and f(2)>0, the root must lie within interval [0, 2]. If we choose the error tolerance level as ε = 10−2, inequality (12.8) would require n>log2(2/10−2) or n ≥ 8 iterations. Figure 12.19 contains a trace of the first eight iterations of the bisection method applied to equation (12.9). Thus, we obtained x8 = 1.3203125 as an approximate value for the root x∗ of equation (12.9), and we can guarantee that |1.3203125 − x∗| < 10−2. Moreover, if we take into account the signs of the function f(x)at a8,b8, and x8, we can assert that the root lies between 1.3203125 and 1.328125. The principal weakness of the bisec