It provides extensivecoverage of fundamental data structures and algorithms forsorting, searching, and related applications.. Although thesubstance of the book applies to programming in
Trang 1Algorithms in Java: Parts 1-4, Third Edition
By Robert Sedgewick
Publisher: Addison Wesley Pub Date: July 23, 2002 ISBN: 0-201-36120-5, 768 pages
Sedgewick has a real gift for explaining concepts in a way thatmakes them easy to understand The use of real programs inpage-size (or less) chunks that can be easily understood is areal plus The figures, programs, and tables are a significantcontribution to the learning experience of the reader; theymake this book distinctive.-William A Ward, University of SouthAlabama
This edition of Robert Sedgewick's popular work providescurrent and comprehensive coverage of important algorithmsfor Java programmers Michael Schidlowsky and Sedgewickhave developed new Java implementations that both expressthe methods in a concise and direct manner and provideprogrammers with the practical means to test them on realapplications
Many new algorithms are presented, and the explanations ofeach algorithm are much more detailed than in previouseditions A new text design and detailed, innovative figures,with accompanying commentary, greatly enhance thepresentation The third edition retains the successful blend oftheory and practice that has made Sedgewick's work aninvaluable resource for more than 400,000 programmers!
This particular book, Parts 1-4, represents the essential first
Trang 2half of Sedgewick's complete work It provides extensivecoverage of fundamental data structures and algorithms forsorting, searching, and related applications Although thesubstance of the book applies to programming in any language,the implementations by Schidlowsky and Sedgewick also exploitthe natural match between Java classes and abstract data type(ADT) implementations.
Complete implementations for binomial queues, multiwayradix sorting, randomized BSTs, splay trees, skip lists,multiway tries, B trees, extendible hashing, and many otheradvanced methods
Quantitative information about the algorithms that givesyou a basis for comparing them
More than 1,000 exercises and more than 250 detailedfigures to help you learn properties of the algorithms
Whether you are learning the algorithms for the first time orwish to have up-to-date reference material that incorporatesnew programming styles with classic and new algorithms, youwill find a wealth of useful information in this book
Trang 3Part II: Data Structures
Trang 4Section 5.8 Graph Traversal
Section 5.9 Perspective
References for Part Two
Trang 5Chapter 9 Priority Queues and Heapsort
Trang 6Section 12.3 Sequential Search
Section 12.4 Binary Search
Trang 7Section 12.5 Index Implementations with SymbolTables
Trang 8Appendix
Exercises
Top
Trang 11Many of the designations used by manufacturers and sellers todistinguish their products are claimed as trademarks Wherethose designations appear in this book and Addison-Wesley wasaware of a trademark claim, the designations have been printed
in initial capital letters or all capitals
The author and publisher have taken care in the preparation ofthis book, but make no expressed or implied warranty of anykind and assume no responsibility for errors or omissions Noliability is assumed for incidental or consequential damages inconnection with or arising out of the use of the information orprograms contained herein
The publisher offers discounts on this book when ordered inquantity for special sales For more information, please contact:U.S Corporate and Government Sales
Trang 12For information on obtaining permission for use of material fromthis work, please submit a written request to:
Trang 15This book is the first of three volumes that are intended tosurvey the most important computer algorithms in use today.This first volume (Parts I–IV) covers fundamental concepts (PartI), data structures (Part II), sorting algorithms (Part III), andsearching algorithms (Part IV); the second volume (Part 5)covers graphs and graph algorithms; and the (yet to bepublished) third volume (Parts 6–8) covers strings (Part 6),computational geometry (Part 7), and advanced algorithms andapplications (Part 8)
The books are useful as texts early in the computer sciencecurriculum, after students have acquired basic programmingskills and familiarity with computer systems, but before theyhave taken specialized courses in advanced areas of computerscience or computer applications The books also are useful forself-study or as a reference for people engaged in thedevelopment of computer systems or applications programsbecause they contain implementations of useful algorithms anddetailed information on these algorithms' performancecharacteristics The broad perspective taken makes the series
an appropriate introduction to the field
Together the three volumes comprise the Third Edition of a book
that has been widely used by students and programmersaround the world for many years I have completely rewrittenthe text for this edition, and I have added thousands of newexercises, hundreds of new figures, dozens of new programs,and detailed commentary on all the figures and programs Thisnew material provides both coverage of new topics and fullerexplanations of many of the classic algorithms A new emphasis
on abstract data types throughout the books makes theprograms more broadly useful and relevant in modern object-oriented programming environments People who have read
Trang 16previous editions will find a wealth of new informationthroughout; all readers will find a wealth of pedagogicalmaterial that provides effective access to essential concepts.
These books are not just for programmers and computerscience students Everyone who uses a computer wants it torun faster or to solve larger problems The algorithms that weconsider represent a body of knowledge developed during thelast 50 years that is the basis for the efficient use of the
computer for a broad variety of applications From N-body
simulation problems in physics to genetic-sequencing problems
in molecular biology, the basic methods described here havebecome essential in scientific research; and from databasesystems to Internet search engines, they have become essentialparts of modern software systems As the scope of computerapplications becomes more widespread, so grows the impact ofbasic algorithms The goal of this book is to serve as a resource
so that students and professionals can know and makeintelligent use of these fundamental algorithms as the needarises in whatever computer application they might undertake
Top
Trang 17
Scope
This book, Algorithms in Java, Third Edition, Parts 1-4, contains
16 chapters grouped into four major parts: fundamentals, datastructures, sorting, and searching The descriptions here areintended to give readers an understanding of the basicproperties of as broad a range of fundamental algorithms aspossible The algorithms described here have found widespreaduse for years, and represent an essential body of knowledge forboth the practicing programmer and the computer-sciencestudent The second volume is devoted to graph algorithms,and the third consists of four additional parts that cover strings,geometry, and advanced topics My primary goal in developingthese books has been to bring together fundamental methodsfrom these areas, to provide access to the best methods knownfor solving problems by computer
You will most appreciate the material here if you have had one
or two previous courses in computer science or have hadequivalent programming experience: one course inprogramming in a high-level language such as Java, C, or C++,and perhaps another course that teaches fundamental concepts
of programming systems This book is thus intended for anyoneconversant with a modern programming language and with thebasic features of modern computer systems References thatmight help to fill in gaps in your background are suggested inthe text
Most of the mathematical material supporting the analyticresults is self-contained (or is labeled as beyond the scope ofthis book), so little specific preparation in mathematics isrequired for the bulk of the book, although mathematicalmaturity is definitely helpful
Trang 18
Top
Trang 19An elementary course on data structures and algorithms mightemphasize the basic data structures in Part II and their use inthe implementations in Parts III and IV A course on design andanalysis of algorithms might emphasize the fundamentalmaterial in Part I and Chapter 5, then study the ways in whichthe algorithms in Parts III and IV achieve good asymptoticperformance A course on software engineering might omit themathematical and advanced algorithmic material, andemphasize how to integrate the implementations given hereinto large programs or systems A course on algorithms mighttake a survey approach and introduce concepts from all theseareas.
Earlier editions of this book that are based on otherprogramming languages have been used at scores of collegesand universities as a text for the second or third course incomputer science and as supplemental reading for othercourses At Princeton, our experience has been that the breadth
of coverage of material in this book provides our majors with anintroduction to computer science that can be expanded on inlater courses on analysis of algorithms, systems programming,
Trang 20and theoretical computer science, while providing the growinggroup of students from other disciplines with a large set oftechniques that these people can put to good use immediately.
The exercises—nearly all of which are new to this third edition—fall into several types Some are intended to test understanding
of material in the text, and simply ask readers to work through
an example or to apply concepts described in the text Othersinvolve implementing and putting together the algorithms, orrunning empirical studies to compare variants of the algorithmsand to learn their properties Still others are a repository forimportant information at a level of detail that is not appropriatefor the text Reading and thinking about the exercises will paydividends for every reader
Top
Trang 21
Algorithms of Practical Use
Anyone wanting to use a computer more effectively can use thisbook for reference or for self-study People with programmingexperience can find information on specific topics throughoutthe book To a large extent, you can read the individualchapters in the book independently of the others, although, insome cases, algorithms in one chapter make use of methodsfrom a previous chapter
The orientation of the book is to study algorithms likely to be ofpractical use The book provides information about the tools ofthe trade to the point that readers can confidently implement,debug, and put algorithms to work to solve a problem or toprovide functionality in an application Full implementations ofthe methods discussed are included, as are descriptions of theoperations of these programs on a consistent set of examples
Because we work with real code, rather than write pseudo-code,you can put the programs to practical use quickly Programlistings are available from the book's home page You can usethese working programs in many ways to help you studyalgorithms Read them to check your understanding of thedetails of an algorithm, or to see one way to handleinitializations, boundary conditions, and other awkwardsituations that often pose programming challenges Run them
to see the algorithms in action, to study performanceempirically and check your results against the tables in thebook, or to try your own modifications
Characteristics of the algorithms and of the situations in whichthey might be useful are discussed in detail Connections to theanalysis of algorithms and theoretical computer science aredeveloped in con-text When appropriate, empirical and analytic
Trang 22results are presented to illustrate why certain algorithms arepreferred When interesting, the relationship of the practicalalgorithms being discussed to purely theoretical results isdescribed Specific information on performance characteristics
of algorithms and implementations is synthesized,encapsulated, and discussed throughout the book
Top
Trang 23Mike Schidlowsky and I developed a style of Java programmingbased on abstract data types that we feel is an effective way topresent the algorithms and data structures as real programs.
We have striven for elegant, compact, efficient, and portableimplementations The style is consistent whenever possible, soprograms that are similar look similar
For many of the algorithms in this book, the similarities holdregardless of the language: Quicksort is quicksort (to pick oneprominent example), whether expressed in Ada, Algol-60,Basic, C, C++, Fortran, Java, Mesa, Modula-3, Pascal,PostScript, Smalltalk, or countless other programminglanguages and environments where it has proved to be aneffective sorting method On the one hand, our code is informed
by experience with implementing algorithms in these andnumerous other languages (C and C++ versions of this bookare also available); on the other hand, some of the properties ofsome of these languages are informed by their designers'experience with some of the algorithms and data structures that
we consider in this book
Chapter 1 constitutes a detailed example of this approach todeveloping efficient Java implementations of our algorithms,and Chapter 2 describes our approach to analyzing them.Chapters 3 and 4 are devoted to describing and justifying thebasic mechanisms that we use for data type and ADTimplementations These four chapters set the stage for the rest
Trang 24Top
Trang 25
Acknowledgments
Many people gave me helpful feedback on earlier versions ofthis book In particular, hundreds of students at Princeton andBrown have suffered through preliminary drafts over the years.Special thanks are due to Trina Avery and Tom Freeman fortheir help in producing the first edition; to Janet Incerpi for hercreativity and ingenuity in persuading our early and primitivedigital computerized typesetting hardware and software toproduce the first edition; to Marc Brown for his part in thealgorithm visualization research that was the genesis of somany of the figures in the book; and to Dave Hanson andAndrew Appel for their willingness to answer all of my questionsabout programming languages I would also like to thank themany readers who have provided me with comments aboutvarious editions, including Guy Almes, Jon Bentley, Marc Brown,Jay Gischer, Allan Heydon, Kennedy Lemke, Udi Manber, DanaRichards, John Reif, M Rosenfeld, Stephen Seidman, MichaelQuinn, and William Ward
To produce this new edition, I have had the pleasure of workingwith Peter Gordon and Helen Goldstein at Addison-Wesley, whohave patiently shepherded this project as it has evolved It hasalso been my pleasure to work with several other members ofthe professional staff at Addison-Wesley The nature of thisproject made the book a somewhat unusual challenge for many
of them, and I much appreciate their forbearance In particular,Marilyn Rash did an outstanding job managing the book'sproduction within a tightly compressed schedule
I have gained three new mentors in writing this book, andparticularly want to express my appreciation to them First,Steve Summit carefully checked early versions of themanuscript on a technical level and provided me with literally
Trang 26thousands of detailed comments, particularly on the programs.Steve clearly understood my goal of providing elegant, efficient,and effective implementations, and his comments not onlyhelped me to provide a measure of consistency across theimplementations, but also helped me to improve many of themsubstantially Second, Lyn Dupré e also provided me withthousands of detailed comments on the manuscript, which wereinvaluable in helping me not only to correct and avoidgrammatical errors, but also—more important—to find aconsistent and coherent writing style that helps bind togetherthe daunting mass of technical material here Third, Chris VanWyk, in a long series of spirited electronic mail exchanges,patiently defended the basic precepts of object-orientedprogramming and helped me develop a style of coding thatexhibits the algorithms with clarity and precision while stilltaking advantage of what object-oriented programming has tooffer The basic approach that we developed for the C++version of this book has substantially influenced the Java codehere and will certainly influence future volumes in bothlanguages (and C as well) I am extremely grateful for theopportunity to learn from Steve, Lyn, and Chris—their input wasvital in the development of this book.
Much of what I have written here I have learned from theteaching and writings of Don Knuth, my advisor at Stanford.Although Don had no direct influence on this work, his presencemay be felt in the book, for it was he who put the study ofalgorithms on the scientific footing that makes a work such asthis possible My friend and colleague Philippe Flajolet, who hasbeen a major force in the development of the analysis ofalgorithms as a mature research area, has had a similarinfluence on this work
I am deeply thankful for the support of Princeton University,Brown University, and the Institut National de Recherche enInformatique et Automatique (INRIA), where I did most of thework on the book; and of the Institute for Defense Analyses and
Trang 27the Xerox Palo Alto Research Center, where I did some work onthe book while visiting Many parts of the book are dependent
on research that has been generously supported by the NationalScience Foundation and the Office of Naval Research Finally, Ithank Bill Bowen, Aaron Lemonick, and Neil Rudenstine for theirsupport in building an academic environment at Princeton inwhich I was able to prepare this book, despite my numerousother responsibilities
Trang 29In the past decade, Java has become the language of choice for
a variety of applications But Java developers have foundthemselves repeatedly referring to references such as
Sedgewick's Algorithms in C for solutions to common
programming problems There has long been an empty space
on the bookshelf for a comparable reference work for Java; thisbook is here to fill that space
We wrote the sample programs as utility methods to be used in
a variety of contexts To that end, we did not use the Javapackage mechanism To focus on the algorithms at hand (and toexpose the algorithmic basis of many fundamental libraryclasses), we avoided the standard Java library in favor of morefundamental types Proper error checking and other defensivepractices would both substantially increase the amount of codeand distract the reader from the core algorithms Developersshould introduce such code when using the programs in largerapplications
Although the algorithms we present are language independent,
we have paid close attention to Java-specific performanceissues The timings throughout the book are provided as onecontext for comparing algorithms, and will vary depending onthe virtual machine As Java environments evolve, programswill perform as fast as natively compiled code, but suchoptimizations will not change the performance of algorithmsrelative to one another We provide the timings as a usefulreference for such comparisons
I would like to thank Mike Zamansky, for his mentorship anddevotion to the teaching of computer science, and DanielChaskes, Jason Sanders, and James Percy, for their unwaveringsupport I would also like to thank my family for their supportand for the computer that bore my first programs Bringing
Trang 30together Java with the classic algorithms of computer sciencewas an exciting endeavor for which I am very grateful Thankyou, Bob, for the opportunity to do so.
Michael Schidlowsky Oakland Gardens, New York, 2002
Top
Trang 32Classifying exercises is an activity fraught with peril becausereaders of a book such as this come to the material with variouslevels of knowledge and experience Nonetheless, guidance isappropriate, so many of the exercises carry one of fourannotations to help you decide how to approach them
Exercises that test your understanding of the material are
marked with an open triangle, as follows:
9.57 Give the binomial queue that results when the keys
E A S Y Q U E S T I O N are inserted into an initially emptybinomial queue
Most often, such exercises relate directly to examples in thetext They should present no special difficulty, but working themmight teach you a fact or concept that may have eluded youwhen you read the text
Exercises that add new and thought-provoking information to
the material are marked with an open circle, as follows:
14.20 Write a program that inserts N random integers
into a table of size N/100 using separate chaining, then finds the length of the shortest and longest lists, for N =
Exercises that are intended to challenge you are marked with a
black dot, as follows:
Trang 33The annotations are intended to be neutral with respect to yourprogramming and mathematical ability Those exercises thatrequire expertise in programming or in mathematical analysisare self-evident All readers are encouraged to test theirunderstanding of the algorithms by implementing them Still, anexercise such as this one is straightforward for a practicingprogrammer or a student in a programming course, but mayrequire substantial work for someone who has not recentlyprogrammed:
1.23 Modify Program 1.4 to generate random pairs of
integers between 0 and N - 1 instead of reading them from standard input, and to loop until N - 1 union operations have been performed Run your program for N = 103, 104,
105, and 106 and print out the total number of edges
generated for each value of N.
Trang 34In a similar vein, all readers are encouraged to strive toappreciate the analytic underpinnings of our knowledge aboutproperties of algorithms Still, an exercise such as this one isstraightforward for a scientist or a student in a discretemathematics course, but may require substantial work forsomeone who has not recently done mathematical analysis:
1.13 Compute the average distance from a node to the root
in a worst-case tree of 2n nodes built by the weighted
quick-union algorithm
There are far too many exercises for you to read and assimilatethem all; my hope is that there are enough exercises here tostimulate you to strive to come to a broader understanding onthe topics that interest you than you can glean by simplyreading the text
Top
Trang 38The objective of this book is to study a broad variety of
important and useful algorithms—methods for solving problems
that are suited for computer implementation We shall deal withmany different areas of application, always concentrating onfundamental algorithms that are important to know andinteresting to study We shall spend enough time on eachalgorithm to understand its essential characteristics and torespect its subtleties Our goal is to learn well enough to beable to use and appreciate a large number of the mostimportant algorithms used on computers today
The strategy that we use for understanding the programspresented in this book is to implement and test them, toexperiment with their variants, to discuss their operation onsmall examples, and to try them out on larger examples similar
to what we might encounter in practice We shall use the Javaprogramming language to describe the algorithms, thusproviding useful implementations at the same time Ourprograms have a uniform style that is amenable to translationinto other modern programming languages as well
We also pay careful attention to performance characteristics ofour algorithms in order to help us develop improved versions,compare different algorithms for the same task, and predict orguarantee performance for large problems Understanding howthe algorithms perform might require experimentation ormathematical analysis or both We consider detailed informationfor many of the most important algorithms, developing analyticresults directly when feasible, or calling on results from theresearch literature when necessary
To illustrate our general approach to developing algorithmicsolutions, we consider in this chapter a detailed examplecomprising a number of algorithms that solve a particular
Trang 39is a fundamental computational task, and the solution that wedevelop is of use in a variety of applications We start with asimple solution, then seek to understand that solution'sperformance characteristics, which help us to see how toimprove the algorithm After a few iterations of this process, wecome to an efficient and useful algorithm for solving theproblem This prototypical example sets the stage for our use ofthe same general methodology throughout the book
Trang 40
1.1 Algorithms
When we write a computer program, we are generallyimplementing a method that has been devised previously tosolve some problem This method is often independent of theparticular computer to be used—it is likely to be equallyappropriate for many computers and many computerlanguages It is the method, rather than the computer programitself, that we must study to learn how the problem is being
attacked The term algorithm is used in computer science to
describe a problem-solving method suitable for implementation
as a computer program Algorithms are the stuff of computerscience: They are central objects of study in many, if not most,areas of the field
Most algorithms of interest involve methods of organizing thedata involved in the computation Objects created in this way
are called data structures, and they also are central objects of
study in computer science Thus, algorithms and data structures
go hand in hand In this book we take the view that datastructures exist as the byproducts or end products of algorithmsand that we must therefore study them in order to understandthe algorithms Simple algorithms can give rise to complicateddata structures and, conversely, complicated algorithms can usesimple data structures We shall study the properties of manydata structures in this book; indeed, the book might well have
been called Algorithms and Data Structures in Java.
When we use a computer to help us solve a problem, wetypically are faced with a number of possible differentapproaches For small problems, it hardly matters whichapproach we use, as long as we have one that solves theproblem correctly For huge problems (or applications where weneed to solve huge numbers of small problems), however, we