Tải bản đầy đủ (.pdf) (1,249 trang)

Algorithms in java, 3rd ed, part 1 4 robert sedgewick

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.47 MB, 1,249 trang )

AlgorithmsinJava:Parts1-4,Third
Edition
ByRobertSedgewick
Publisher:AddisonWesley
PubDate:July23,2002
ISBN:0-201-36120-5,768pages

Sedgewickhasarealgiftforexplainingconceptsinawaythat
makes them easy to understand. The use of real programs in
page-size (or less) chunks that can be easily understood is a
real plus. The figures, programs, and tables are a significant
contribution to the learning experience of the reader; they
makethisbookdistinctive.-WilliamA.Ward,UniversityofSouth
Alabama
This edition of Robert Sedgewick's popular work provides
current and comprehensive coverage of important algorithms
for Java programmers. Michael Schidlowsky and Sedgewick
have developed new Java implementations that both express
the methods in a concise and direct manner and provide
programmers with the practical means to test them on real
applications.
Many new algorithms are presented, and the explanations of
each algorithm are much more detailed than in previous
editions. A new text design and detailed, innovative figures,
with accompanying commentary, greatly enhance the
presentation. The third edition retains the successful blend of
theory and practice that has made Sedgewick's work an
invaluableresourceformorethan400,000programmers!
This particular book, Parts 1-4, represents the essential first



half of Sedgewick's complete work. It provides extensive
coverage of fundamental data structures and algorithms for
sorting, searching, and related applications. Although the
substanceofthebookappliestoprogramminginanylanguage,
theimplementationsbySchidlowskyandSedgewickalsoexploit
thenaturalmatchbetweenJavaclassesandabstractdatatype
(ADT)implementations.
Highlights
Java class implementations of more than 100 important
practicalalgorithms
Emphasis on ADTs, modular programming, and objectorientedprogramming
Extensive coverage of arrays, linked lists, trees, and other
fundamentaldatastructures
Thorough treatment of algorithms for sorting, selection,
priorityqueueADTimplementations,andsymboltableADT
implementations(searchalgorithms)
Complete implementations for binomial queues, multiway
radix sorting, randomized BSTs, splay trees, skip lists,
multiwaytries,Btrees,extendiblehashing,andmanyother
advancedmethods
Quantitative information about the algorithms that gives
youabasisforcomparingthem
More than 1,000 exercises and more than 250 detailed
figurestohelpyoulearnpropertiesofthealgorithms
Whether you are learning the algorithms for the first time or
wish to have up-to-date reference material that incorporates
new programming styles with classic and new algorithms, you
willfindawealthofusefulinformationinthisbook.



AlgorithmsinJava:Parts1-4,ThirdEdition
Copyright
Preface
Scope
UseintheCurriculum
AlgorithmsofPracticalUse
ProgrammingLanguage
Acknowledgments
JavaConsultant'sPreface
NotesonExercises

PartI:Fundamentals
Chapter1.Introduction
Section1.1.Algorithms
Section1.2.ASampleProblem:Connectivity
Section1.3.Union–FindAlgorithms
Section1.4.Perspective
Section1.5.SummaryofTopics
Chapter2.PrinciplesofAlgorithmAnalysis
Section2.1.ImplementationandEmpiricalAnalysis
Section2.2.AnalysisofAlgorithms
Section2.3.GrowthofFunctions
Section2.4.Big-OhNotation
Section2.5.BasicRecurrences
Section2.6.ExamplesofAlgorithmAnalysis
Section2.7.Guarantees,Predictions,andLimitations
ReferencesforPartOne

PartII:DataStructures



Chapter3.ElementaryDataStructures
Section3.1.BuildingBlocks
Section3.2.Arrays
Section3.3.LinkedLists
Section3.4.ElementaryListProcessing
Section3.5.MemoryAllocationforLists
Section3.6.Strings
Section3.7.CompoundDataStructures
Chapter4.AbstractDataTypes
Exercises
Section4.1.CollectionsofItems
Section4.2.PushdownStackADT
Section4.3.ExamplesofStackADTClients
Section4.4.StackADTImplementations
Section4.5.GenericImplementations
Section4.6.CreationofaNewADT
Section4.7.FIFOQueuesandGeneralizedQueues
Section4.8.DuplicateandIndexItems
Section4.9.First-ClassADTs
Section4.10.Application-BasedADTExample
Section4.11.Perspective
Chapter5.RecursionandTrees
Section5.1.RecursiveAlgorithms
Section5.2.DivideandConquer
Section5.3.DynamicProgramming
Section5.4.Trees
Section5.5.MathematicalPropertiesofBinaryTrees
Section5.6.TreeTraversal
Section5.7.RecursiveBinary-TreeAlgorithms

Section5.8.GraphTraversal
Section5.9.Perspective
ReferencesforPartTwo


PartIII:Sorting
Chapter6.ElementarySortingMethods
Section6.1.RulesoftheGame
Section6.2.GenericSortImplementations
Section6.3.SelectionSort
Section6.4.InsertionSort
Section6.5.BubbleSort
Section6.6.PerformanceCharacteristicsofElementary
Sorts
Section6.7.AlgorithmVisualization
Section6.8.Shellsort
Section6.9.SortingofLinkedLists
Section6.10.Key-IndexedCounting
Chapter7.Quicksort
Section7.1.TheBasicAlgorithm
Section7.2.PerformanceCharacteristicsofQuicksort
Section7.3.StackSize
Section7.4.SmallSubfiles
Section7.5.Median-of-ThreePartitioning
Section7.6.DuplicateKeys
Section7.7.StringsandVectors
Section7.8.Selection
Chapter8.MergingandMergesort
Section8.1.Two-WayMerging
Section8.2.AbstractIn-PlaceMerge

Section8.3.Top-DownMergesort
Section8.4.ImprovementstotheBasicAlgorithm
Section8.5.Bottom-UpMergesort
Section8.6.PerformanceCharacteristicsofMergesort
Section8.7.Linked-ListImplementationsofMergesort
Section8.8.RecursionRevisited
Chapter9.PriorityQueuesandHeapsort


Exercises
Section9.1.ElementaryImplementations
Section9.2.HeapDataStructure
Section9.3.AlgorithmsonHeaps
Section9.4.Heapsort
Section9.5.Priority-QueueADT
Section9.6.PriorityQueuesforClientArrays
Section9.7.BinomialQueues
Chapter10.RadixSorting
Section10.1.Bits,Bytes,andWords
Section10.2.BinaryQuicksort
Section10.3.MSDRadixSort
Section10.4.Three-WayRadixQuicksort
Section10.5.LSDRadixSort
Section 10.6. Performance Characteristics of Radix
Sorts
Section10.7.Sublinear-TimeSorts
Chapter11.Special-PurposeSortingMethods
Section11.1.Batcher'sOdd–EvenMergesort
Section11.2.SortingNetworks
Section11.3.SortingInPlace

Section11.4.ExternalSorting
Section11.5.Sort–MergeImplementations
Section11.6.ParallelSort–Merge
ReferencesforPartThree

PartIV:Searching
Chapter12.SymbolTablesandBinarySearchTrees
Section12.1.Symbol-TableAbstractDataType
Section12.2.Key-IndexedSearch
Section12.3.SequentialSearch
Section12.4.BinarySearch


Section 12.5. Index Implementations with Symbol
Tables
Section12.6.BinarySearchTrees
Section12.7.PerformanceCharacteristicsofBSTs
Section12.8.InsertionattheRootinBSTs
Section 12.9. BST Implementations of Other ADT
Operations
Chapter13.BalancedTrees
Exercises
Section13.1.RandomizedBSTs
Section13.2.SplayBSTs
Section13.3.Top-Down2-3-4Trees
Section13.4.Red–BlackTrees
Section13.5.SkipLists
Section13.6.PerformanceCharacteristics
Chapter14.Hashing
Section14.1.HashFunctions

Section14.2.SeparateChaining
Section14.3.LinearProbing
Section14.4.DoubleHashing
Section14.5.DynamicHashTables
Section14.6.Perspective
Chapter15.RadixSearch
Section15.1.DigitalSearchTrees
Section15.2.Tries
Section15.3.PatriciaTries
Section15.4.MultiwayTriesandTSTs
Section15.5.Text-String–IndexAlgorithms
Chapter16.ExternalSearching
Section16.1.RulesoftheGame
Section16.2.IndexedSequentialAccess
Section16.3.BTrees
Section16.4.ExtendibleHashing


Section16.5.Perspective
ReferencesforPartFour

Appendix
Exercises

Top







Copyright
Manyofthedesignationsusedbymanufacturersandsellersto
distinguish their products are claimed as trademarks. Where
thosedesignationsappearinthisbookandAddison-Wesleywas
awareofatrademarkclaim,thedesignationshavebeenprinted
ininitialcapitallettersorallcapitals.
Theauthorandpublisherhavetakencareinthepreparationof
this book, but make no expressed or implied warranty of any
kind and assume no responsibility for errors or omissions. No
liability is assumed for incidental or consequential damages in
connection with or arising out of the use of the information or
programscontainedherein.
The publisher offers discounts on this book when ordered in
quantityforspecialsales.Formoreinformation,pleasecontact:
U.S.CorporateandGovernmentSales
(800)382-3410

ForsalesoutsideoftheUnitedStates,pleasecontact:
InternationalSales
(317)581-3793

VisitAddison-WesleyontheWeb:www.awprofessional.com
LibraryofCongressCataloging-in-PublicationData
Sedgewick,Robert,1946–


AlgorithmsinJava/RobertSedgewick.—3ded.
p.cm.
Includesbibliographicalreferencesandindex.

Contents: v. 1, pts. 1–4. Fundamentals, data structures,
sorting,searching.
1.Java(Computerprogramlanguage)2.Computeralgorithms.
I.Title.
QA76.73.C15S2003
005.13'3—dc2092-901
CIP
Copyright©2003byPearsonEducation,Inc.
All rights reserved. No part of this publication may be
reproduced,storedinaretrievalsystem,ortransmitted,inany
form or by any means, electronic, mechanical, photocopying,
recording,orotherwise,withoutthepriorwrittenpermissionof
thepublisher.PrintedintheUnitedStatesofAmerica.Published
simultaneouslyinCanada.
Forinformationonobtainingpermissionforuseofmaterialfrom
thiswork,pleasesubmitawrittenrequestto:
PearsonEducation,Inc.
75ArlingtonStreet,Suite300
Boston,MA02116
Fax:(617)848-7047



Textprintedonrecycledpaper
12345678910–CRS–0605040302
Firstprinting,July2002

Dedication
ToAdam,Andrew,Brett,Robbie,andespeciallyLinda



Top







Preface
This book is the first of three volumes that are intended to
survey the most important computer algorithms in use today.
Thisfirstvolume(PartsI–IV)coversfundamentalconcepts(Part
I), data structures (Part II), sorting algorithms (Part III), and
searching algorithms (Part IV); the second volume (Part 5)
covers graphs and graph algorithms; and the (yet to be
published) third volume (Parts 6–8) covers strings (Part 6),
computationalgeometry(Part7),andadvancedalgorithmsand
applications(Part8).
The books are useful as texts early in the computer science
curriculum, after students have acquired basic programming
skills and familiarity with computer systems, but before they
have taken specialized courses in advanced areas of computer
scienceorcomputerapplications.Thebooksalsoareusefulfor
self-study or as a reference for people engaged in the
development of computer systems or applications programs
becausetheycontainimplementationsofusefulalgorithmsand
detailed information on these algorithms' performance
characteristics. The broad perspective taken makes the series
anappropriateintroductiontothefield.

TogetherthethreevolumescomprisetheThirdEditionofabook
that has been widely used by students and programmers
around the world for many years. I have completely rewritten
the text for this edition, and I have added thousands of new
exercises, hundreds of new figures, dozens of new programs,
anddetailedcommentaryonallthefiguresandprograms.This
new material provides both coverage of new topics and fuller
explanationsofmanyoftheclassicalgorithms.Anewemphasis
on abstract data types throughout the books makes the
programs more broadly useful and relevant in modern objectoriented programming environments. People who have read


previous editions will find a wealth of new information
throughout; all readers will find a wealth of pedagogical
materialthatprovideseffectiveaccesstoessentialconcepts.
These books are not just for programmers and computer
science students. Everyone who uses a computer wants it to
run faster or to solve larger problems. The algorithms that we
consider represent a body of knowledge developed during the
last 50 years that is the basis for the efficient use of the
computer for a broad variety of applications. From N-body
simulationproblemsinphysicstogenetic-sequencingproblems
in molecular biology, the basic methods described here have
become essential in scientific research; and from database
systemstoInternetsearchengines,theyhavebecomeessential
parts of modern software systems. As the scope of computer
applicationsbecomesmorewidespread,sogrowstheimpactof
basicalgorithms.Thegoalofthisbookistoserveasaresource
so that students and professionals can know and make
intelligent use of these fundamental algorithms as the need

arisesinwhatevercomputerapplicationtheymightundertake.




Top






Scope
Thisbook,AlgorithmsinJava,ThirdEdition,Parts1-4,contains
16chaptersgroupedintofourmajorparts:fundamentals,data
structures, sorting, and searching. The descriptions here are
intended to give readers an understanding of the basic
properties of as broad a range of fundamental algorithms as
possible.Thealgorithmsdescribedherehavefoundwidespread
useforyears,andrepresentanessentialbodyofknowledgefor
both the practicing programmer and the computer-science
student. The second volume is devoted to graph algorithms,
andthethirdconsistsoffouradditionalpartsthatcoverstrings,
geometry, and advanced topics. My primary goal in developing
these books has been to bring together fundamental methods
fromtheseareas,toprovideaccesstothebestmethodsknown
forsolvingproblemsbycomputer.
Youwillmostappreciatethematerialhereifyouhavehadone
or two previous courses in computer science or have had
equivalent programming experience: one course in

programminginahigh-levellanguagesuchasJava,C,orC++,
andperhapsanothercoursethatteachesfundamentalconcepts
ofprogrammingsystems.Thisbookisthusintendedforanyone
conversantwithamodernprogramminglanguageandwiththe
basic features of modern computer systems. References that
might help to fill in gaps in your background are suggested in
thetext.
Most of the mathematical material supporting the analytic
results is self-contained (or is labeled as beyond the scope of
this book), so little specific preparation in mathematics is
required for the bulk of the book, although mathematical
maturityisdefinitelyhelpful.






Top






UseintheCurriculum
Thereisagreatdealofflexibilityinhowthematerialherecan
be taught, depending on the taste of the instructor and the
preparationofthestudents.Thereissufficientcoverageofbasic
material for the book to be used to teach data structures to

beginners, and there is sufficient detail and coverage of
advancedmaterialforthebooktobeusedtoteachthedesign
and analysis of algorithms to upper-level students. Some
instructors may wish to emphasize implementations and
practicalconcerns;othersmaywishtoemphasizeanalysisand
theoreticalconcepts.
Anelementarycourseondatastructuresandalgorithmsmight
emphasize the basic data structures in PartII and their use in
theimplementationsinPartsIIIandIV.Acourseondesignand
analysis of algorithms might emphasize the fundamental
materialinPartIandChapter5,thenstudythewaysinwhich
the algorithms in Parts III and IV achieve good asymptotic
performance.Acourseonsoftwareengineeringmightomitthe
mathematical and advanced algorithmic material, and
emphasize how to integrate the implementations given here
into large programs or systems. A course on algorithms might
take a survey approach and introduce concepts from all these
areas.
Earlier editions of this book that are based on other
programming languages have been used at scores of colleges
and universities as a text for the second or third course in
computer science and as supplemental reading for other
courses.AtPrinceton,ourexperiencehasbeenthatthebreadth
ofcoverageofmaterialinthisbookprovidesourmajorswithan
introduction to computer science that can be expanded on in
later courses on analysis of algorithms, systems programming,


and theoretical computer science, while providing the growing
group of students from other disciplines with a large set of

techniquesthatthesepeoplecanputtogooduseimmediately.
Theexercises—nearlyallofwhicharenewtothisthirdedition—
fallintoseveraltypes.Someareintendedtotestunderstanding
ofmaterialinthetext,andsimplyaskreaderstoworkthrough
an example or to apply concepts described in the text. Others
involve implementing and putting together the algorithms, or
runningempiricalstudiestocomparevariantsofthealgorithms
and to learn their properties. Still others are a repository for
importantinformationatalevelofdetailthatisnotappropriate
for the text. Reading and thinking about the exercises will pay
dividendsforeveryreader.




Top






AlgorithmsofPracticalUse
Anyonewantingtouseacomputermoreeffectivelycanusethis
book for reference or for self-study. People with programming
experience can find information on specific topics throughout
the book. To a large extent, you can read the individual
chapters in the book independently of the others, although, in
some cases, algorithms in one chapter make use of methods
fromapreviouschapter.

Theorientationofthebookistostudyalgorithmslikelytobeof
practicaluse.Thebookprovidesinformationaboutthetoolsof
the trade to the point that readers can confidently implement,
debug, and put algorithms to work to solve a problem or to
provide functionality in an application. Full implementations of
the methods discussed are included, as are descriptions of the
operationsoftheseprogramsonaconsistentsetofexamples.
Becauseweworkwithrealcode,ratherthanwritepseudo-code,
you can put the programs to practical use quickly. Program
listings are available from the book's home page. You can use
these working programs in many ways to help you study
algorithms. Read them to check your understanding of the
details of an algorithm, or to see one way to handle
initializations, boundary conditions, and other awkward
situations that often pose programming challenges. Run them
to see the algorithms in action, to study performance
empirically and check your results against the tables in the
book,ortotryyourownmodifications.
Characteristics of the algorithms and of the situations in which
theymightbeusefularediscussedindetail.Connectionstothe
analysis of algorithms and theoretical computer science are
developedincon-text.Whenappropriate,empiricalandanalytic


results are presented to illustrate why certain algorithms are
preferred. When interesting, the relationship of the practical
algorithms being discussed to purely theoretical results is
described. Specific information on performance characteristics
of algorithms and implementations is synthesized,
encapsulated,anddiscussedthroughoutthebook.





Top






ProgrammingLanguage
Theprogramminglanguageusedforalloftheimplementations
is Java. The programs use a wide range of standard Java
idioms, and the text includes concise descriptions of each
construct.
MikeSchidlowskyandIdevelopedastyleofJavaprogramming
basedonabstractdatatypesthatwefeelisaneffectivewayto
present the algorithms and data structures as real programs.
We have striven for elegant, compact, efficient, and portable
implementations. The style is consistent whenever possible, so
programsthataresimilarlooksimilar.
For many of the algorithms in this book, the similarities hold
regardless of the language: Quicksort is quicksort (to pick one
prominent example), whether expressed in Ada, Algol-60,
Basic, C, C++, Fortran, Java, Mesa, Modula-3, Pascal,
PostScript, Smalltalk, or countless other programming
languages and environments where it has proved to be an
effectivesortingmethod.Ontheonehand,ourcodeisinformed
by experience with implementing algorithms in these and

numerous other languages (C and C++ versions of this book
arealsoavailable);ontheotherhand,someofthepropertiesof
some of these languages are informed by their designers'
experiencewithsomeofthealgorithmsanddatastructuresthat
weconsiderinthisbook.
Chapter 1 constitutes a detailed example of this approach to
developing 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 the
basic mechanisms that we use for data type and ADT
implementations.Thesefourchapterssetthestagefortherest


ofthebook.




Top






Acknowledgments
Many people gave me helpful feedback on earlier versions of
this book. In particular, hundreds of students at Princeton and
Brownhavesufferedthroughpreliminarydraftsovertheyears.
Special thanks are due to Trina Avery and Tom Freeman for

theirhelpinproducingthefirstedition;toJanetIncerpiforher
creativity and ingenuity in persuading our early and primitive
digital computerized typesetting hardware and software to
produce the first edition; to Marc Brown for his part in the
algorithm visualization research that was the genesis of so
many of the figures in the book; and to Dave Hanson and
AndrewAppelfortheirwillingnesstoanswerallofmyquestions
about programming languages. I would also like to thank the
many readers who have provided me with comments about
variouseditions,includingGuyAlmes,JonBentley,MarcBrown,
Jay Gischer, Allan Heydon, Kennedy Lemke, Udi Manber, Dana
Richards, John Reif, M. Rosenfeld, Stephen Seidman, Michael
Quinn,andWilliamWard.
Toproducethisnewedition,Ihavehadthepleasureofworking
withPeterGordonandHelenGoldsteinatAddison-Wesley,who
havepatientlyshepherdedthisprojectasithasevolved.Ithas
also been my pleasure to work with several other members of
the professional staff at Addison-Wesley. The nature of this
projectmadethebookasomewhatunusualchallengeformany
ofthem,andImuchappreciatetheirforbearance.Inparticular,
Marilyn Rash did an outstanding job managing the book's
productionwithinatightlycompressedschedule.
I have gained three new mentors in writing this book, and
particularly want to express my appreciation to them. First,
Steve Summit carefully checked early versions of the
manuscript on a technical level and provided me with literally


×