This page intentionally left blank

AlgorithmsFOURTH EDITIONRobert SedgewickandKevin WaynePrinceton UniversityUpper Saddle River, NJ Boston Indianapolis San FranciscoNew York Toronto Montreal London Munich Paris MadridCapetown Sydney Tokyo Singapore Mexico City

Many of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and the publisher was aware of a trademarkclaim, the designations have been printed with initial capital letters or in all capitals.The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumedfor incidental or consequential damages in connection with or arising out of the use of the information orprograms contained herein.The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases orspecial sales, which may include electronic versions and/or custom covers and content particular to yourbusiness, training goals, marketing focus, and branding interests. This publication is protected by copyright,and permission must be obtained from the publisher prior to any prohibited reproduction, storage ina retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying,recording, or likewise. For information regarding permissions, write to:Pearson Education, Inc.Rights and Contracts Department501 Boylston Street, Suite 900Boston, MA 02116 Text printed in the United States on recycled paper at Courier in Westford, Massachusetts.First printing, March

To Adam, Andrew, Brett, Robbieand especially LindaTo Jackie and Alex

CONTENTSPreface . . . . . . . . . . . . . . . . . . . . . . . . .viii1 Fundamentals . . . . . . . . . . . . . . . . . . . . . .31.1Basic Programming Model1.2Data Abstraction1.3Bags, Queues, and Stacks8641201.4 Analysis of Algorithms1721.5216Case Study: Union-Find2 Sorting . . . . . . . . . . . . . . . . . . . . . . . 2432.1Elementary Sorts2442.2Mergesort2702.3 Quicksort2882.4308Priority Queues2.5 Applications3363 Searching . . . . . . . . . . . . . . . . . . . . . . 361vi3.1Symbol Tables3623.2Binary Search Trees3963.3Balanced Search Trees4243.4Hash Tables4583.5 Applications486

4 Graphs . . . . . . . . . . . . . . . . . . . . . . . 5154.1Undirected Graphs5184.2Directed Graphs5664.3Minimum Spanning Trees6044.4Shortest Paths6385 Strings . . . . . . . . . . . . . . . . . . . . . . . 6955.1String Sorts7025.2Tries7305.3Substring Search7585.4Regular Expressions7885.5Data Compression8106 Context . . . . . . . . . . . . . . . . . . . . . . . 853Index . . . . . . . . . . . . . . . . . . . . . . . . . 933Algorithms . . . . . . . . . . . . . . . . . . . . . . 954Clients . . . . . . . . . . . . . . . . . . . . . . . . 955vii

PREFACEThis book is intended to survey the most important computer algorithms in use today,and to teach fundamental techniques to the growing number of people in need ofknowing them. It is intended for use as a textbook for a second course in computerscience, after students have acquired basic programming skills and familiarity with computersystems. The book also may be useful for self-study or as a reference for people engaged inthe development of computer systems or applications programs, since it contains implementations of useful algorithms and detailed information on performance characteristics andclients. The broad perspective taken makes the book an appropriate introduction to the field.the study of algorithms and data structures is fundamental to any computerscience curriculum, but it is not just for programmers and computer-science students. Everyone who uses a computer wants it to run faster or to solve larger problems. The algorithmsin this book represent a body of knowledge developed over the last 50 years that has becomeindispensable. From N-body simulation problems in physics to genetic-sequencing problemsin molecular biology, the basic methods described here have become essential in scientificresearch; from architectural modeling systems to aircraft simulation, they have become essential tools in engineering; and from database systems to internet search engines, they havebecome essential parts of modern software systems. And these are but a few examples—as thescope of computer applications continues to grow, so grows the impact of the basic methodscovered here.Before developing our fundamental approach to studying algorithms, we develop datatypes for stacks, queues, and other low-level abstractions that we use throughout the book.Then we survey fundamental algorithms for sorting, searching, graphs, and strings. The lastchapter is an overview placing the rest of the material in the book in a larger context.viii

Distinctive features The orientation of the book is to study algorithms likely to be ofpractical use. The book teaches a broad variety of algorithms and data structures and provides sufficient information about them that readers can confidently implement, debug, andput them to work in any computational environment. The approach involves:Algorithms. Our descriptions of algorithms are based on complete implementations and ona discussion of the operations of these programs on a consistent set of examples. Instead ofpresenting pseudo-code, we work with real code, so that the programs can quickly be put topractical use. Our programs are written in Java, but in a style such that most of our code canbe reused to develop implementations in other modern programming languages.Data types. We use a modern programming style based on data abstraction, so that algorithms and their data structures are encapsulated together.Applications. Each chapter has a detailed description of applications where the algorithmsdescribed play a critical role. These range from applications in physics and molecular biology,to engineering computers and systems, to familiar tasks such as data compression and searching on the web.A scientific approach. We emphasize developing mathematical models for describing theperformance of algorithms, using the models to develop hypotheses about performance, andthen testing the hypotheses by running the algorithms in realistic contexts.Breadth of coverage. We cover basic abstract data types, sorting algorithms, searching algorithms, graph processing, and string processing. We keep the material in algorithmic context, describing data structures, algorithm design paradigms, reduction, and problem-solvingmodels. We cover classic methods that have been taught since the 1960s and new methodsthat have been invented in recent years.Our primary goal is to introduce the most important algorithms in use today to as wide anaudience as possible. These algorithms are generally ingenious creations that, remarkably, caneach be expressed in just a dozen or two lines of code. As a group, they represent problemsolving power of amazing scope. They have enabled the construction of computational artifacts, the solution of scientific problems, and the development of commercial applicationsthat would not have been feasible without them.ix

Booksite An important feature of the book is its relationship. Thissite is freely available and contains an extensive amount ofmaterial about algorithms and data structures, for teachers, students, and practitioners, including:An online synopsis. The text is summarized in the booksite to give it the same overall structure as the book, but linked so as to provide easy navigation through the material.Full implementations. All code in the book is available on the booksite, in a form suitable forprogram development. Many other implementations are also available, including advancedimplementations and improvements described in the book, answers to selected exercises, andclient code for various applications. The emphasis is on testing algorithms in the context ofmeaningful applications.Exercises and answers. The booksite expands on the exercises in the book by adding drillexercises (with answers available with a click), a wide variety of examples illustrating thereach of the material, programming exercises with code solutions, and challenging problems.Dynamic visualizations. Dynamic simulations are impossible in a printed book, but thewebsite is replete with implementations that use a graphics class to present compelling visualdemonstrations of algorithm applications.Course materials. A complete set of lecture slides is tied directly to the material in the bookand on the booksite. A full selection of programming assignments, with check lists, test data,and preparatory material, is also included.Links to related material. Hundreds of links lead students to background information aboutapplications and to resources for studying algorithms.Our goal in creating this material was to provide a complementary approach to the ideas.Generally, you should read the book when learning specific algorithms for the first time orwhen trying to get a global picture, and you should use the booksite as a reference when programming or as a starting point when searching for more detail while online.x

Use in the curriculum The book is intended as a textbook in a second course in computer science. It provides full coverage of core material and is an excellent vehicle for students to gain experience and maturity in programming, quantitative reasoning, and problemsolving. Typically, one course in computer science will suffice as a prerequisite—the book isintended for anyone conversant with a modern programming language and with the basicfeatures of modern computer systems.The algorithms and data structures are expressed in Java, but in a style accessible topeople fluent in other modern languages. We embrace modern Java abstractions (includinggenerics) but resist dependence upon esoteric features of the language.Most of the mathematical material supporting the analytic results is self-contained (oris labeled as beyond the scope of this book), so little specific preparation in mathematics isrequired for the bulk of the book, although mathematical maturity is definitely helpful. Applications are drawn from introductory material in the sciences, again self-contained.The material covered is a fundamental background for any student intending to majorin computer science, electrical engineering, or operations research, and is valuable for anystudent with interests in science, mathematics, or engineering.Context The book is intended to follow our introductory text, An Introduction to Programming in Java: An Interdisciplinary Approach, which is a broad introduction to the field.Together, these two books can support a two- or three-semester introduction to computer science that will give any student the requisite background to successfully address computationin any chosen field of study in science, engineering, or the social sciences.The starting point for much of the material in the book was the Sedgewick series of Algorithms books. In spirit, this book is closest to the first and second editions of that book, butthis text benefits from decades of experience teaching and learning that material. Sedgewick’scurrent Algorithms in C/C /Java, Third Edition is more appropriate as a reference or a textfor an advanced course; this book is specifically designed to be a textbook for a one-semestercourse for first- or second-year college students and as a modern introduction to the basicsand a reference for use by working programmers.xi

Acknowledgments This book has been nearly 40 years in the making, so full recognition of all the people who have made it possible is simply not feasible. Earlier editions of thisbook list dozens of names, including (in alphabetical order) Andrew Appel, Trina Avery, MarcBrown, Lyn Dupré, Philippe Flajolet, Tom Freeman, Dave Hanson, Janet Incerpi, Mike Schidlowsky, Steve Summit, and Chris Van Wyk. All of these people deserve acknowledgement,even though some of their contributions may have happened decades ago. For this fourthedition, we are grateful to the hundreds of students at Princeton and several other institutionswho have suffered through preliminary versions of the work, and to readers around the worldfor sending in comments and corrections through the booksite.We are grateful for the support of Princeton University in its unwavering commitmentto excellence in teaching and learning, which has provided the basis for the development ofthis work.Peter Gordon has provided wise counsel throughout the evolution of this work almostfrom the beginning, including a gentle introduction of the “back to the basics” idea that isthe foundation of this edition. For this fourth edition, we are grateful to Barbara Wood forher careful and professional copyediting, to Julie Nahil for managing the production, andto many others at Pearson for their roles in producing and marketing the book. All were extremely responsive to the demands of a rather tight schedule without the slightest sacrifice tothe quality of the result.Robert SedgewickKevin WaynePrinceton, NJJanuary, xii

This page intentionally left blank

ONEFundamentals1.1Basic Programming Model. . . . . . . . . 81.2Data Abstraction . . . . . . . . . . . . . . 641.3Bags, Queues, and Stacks . . . . . . . 120