Algorithms Part II, 4th Edition
Algorithms F O U R T H E D I T I O N PA RT I I
This page intentionally left blank
Algorithms
Robert Sedgewick
and
Kevin Wayne
Princeton University
F O U R T H E D I T I O N
PA RT I I
Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
Many of the designations used 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 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 im-
plied 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
programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may
include electronic versions; custom cover designs; and content particular to your business, training goals,
marketing focus, or branding interests), please contact our corporate sales department at (800) 382-3419
or corpsales@pearsoned.com .For government sales inquiries, please contact governmentsales@pearsoned.com .
For questions about sales outside the United States, please contact international@pearsoned.com. Visit us on the Web: informit.com/aw Copyright © 2014 Pearson Education, Inc.
All rights reserved. Printed in the United States of America. This publication is protected by copyright, and
permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retriev-
al system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording,
or likewise. To obtain permission 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.ISBN-13: 978-0-13-379911-8
ISBN-10: 0-13-379911-5 First digital release, February 2014
______________________________
To Adam, Andrew, Brett, Robbie
and especially Linda
______________________________
___________________
To Jackie and Alex
___________________
CONTENTS
Note: This is an online edition of Chapters 4 through 6 of Algorithms, Fourth Edition, which con-
tains the content covered in our online course Algorithms, Part II.
Chapters 1 through 3, which correspond to our online course Algorithms, Part I, are available as
Algorithms, Fourth Edition, Part I.4. Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Glossary • Undirected graph type • Adjacency-lists representation • Depth-first search • Breadth-first search • Connected components • Degrees of separation
Glossary • Digraph data type • Depth-first search • Directed cycle detection • Precedence-constrained scheduling • Topological sort • Strong connectivity • Kosaraju-Sharir algorithm • Transitive closure
Cut property • Greedy algorithm • Edge-weighted graph data type • Prim’s algorithm • Kruskal’s algorithm
Properties of shortest paths • Edge-weighted digraph data types • Generic shortest paths algorithm • Dijkstra’s algorithm • Shortest paths in edge- weighted DAGs • Critical-path method • Bellman-Ford algorithm • Negative cycle detection • Arbitrage
5. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
Key-indexed counting • LSD string sort • MSD string sort • 3-way string quicksort
String symbol table API • R-way tries • Ternary search tries • Character- based operations
Brute-force algorithm • Knuth-Morris-Pratt algorithm • Boyer-Moore algorithm • Rabin-Karp fingerprint algorithm
Describing patterns with REs • Applications • Nondeterministic finite-state automata • Simulating an NFA • Building an NFA corresponding to an RE
Rules of the game • Reading and writing binary data • Limitations • Run-length coding • Huffman compression • LZW compression
6. Context . . . . . . . . . . . . . . . . . . . . . . . . . . . 853
Hard-disc model • Collision prediction • Collision resolution
Cost model • Search and insert
Suffix sorting • Longest repeated substring • Keyword in context
Maximum flow • Minimum cut • Ford-Fulkerson algorithm
Sorting • Shortest path • Bipartite matching • Linear programming
Longest-paths problem • P vs. NP • Boolean satisfiability • NP-completeness
This page intentionally left blank
ix T
his 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 of knowing them. It is intended for use as a textbook for a second course in computer science, after students have acquired basic programming skills and familiarity with computer systems. The book also may be useful for self-study or as a reference for people engaged in the development of computer systems or applications programs, since it contains implemen- tations of useful algorithms and detailed information on performance characteristics and clients. The broad perspective taken makes the book an appropriate introduction to the field. the study of algorithms and data structures is fundamental to any computer- science curriculum, but it is not just for programmers and computer-science students. Every- one who uses a computer wants it to run faster or to solve larger problems. The algorithms in this book represent a body of knowledge developed over the last 50 years that has become indispensable. From N-body simulation problems in physics to genetic-sequencing problems in molecular biology, the basic methods described here have become essential in scientific research; from architectural modeling systems to aircraft simulation, they have become es- sential tools in engineering; and from database systems to internet search engines, they have become essential parts of modern software systems. And these are but a few examples—as the scope of computer applications continues to grow, so grows the impact of the basic methods covered here.
In Chapter 1, we develop our fundamental approach to studying algorithms, includ- ing coverage of data types for stacks, queues, and other low-level abstractions that we use throughout the book. In Chapters 2 and 3, we survey fundamental algorithms for sorting and searching; and in Chapters 4 and 5, we cover algorithms for processing graphs and strings.
Chapter 6 is an overview placing the rest of the material in the book in a larger context. PREFACE
Distinctive features The orientation of the book is to study algorithms likely to be of
practical use. The book teaches a broad variety of algorithms and data structures and pro- vides sufficient information about them that readers can confidently implement, debug, and put them to work in any computational environment. The approach involves:
Algorithms. Our descriptions of algorithms are based on complete implementations and on
a discussion of the operations of these programs on a consistent set of examples. Instead of presenting pseudo-code, we work with real code, so that the programs can quickly be put to practical use. Our programs are written in Java, but in a style such that most of our code can be reused to develop implementations in other modern programming languages.
Data types. We use a modern programming style based on data abstraction, so that algo-
rithms and their data structures are encapsulated together.Applications. Each chapter has a detailed description of applications where the algorithms
described 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 search- ing on the web.
A scientific approach. We emphasize developing mathematical models for describing the
performance of algorithms, using the models to develop hypotheses about performance, and then testing the hypotheses by running the algorithms in realistic contexts.
Breadth of coverage. We cover basic abstract data types, sorting algorithms, searching al-
gorithms, graph processing, and string processing. We keep the material in algorithmic con- text, describing data structures, algorithm design paradigms, reduction, and problem-solving models. We cover classic methods that have been taught since the 1960s and new methods that have been invented in recent years.
Our primary goal is to introduce the most important algorithms in use today to as wide an audience as possible. These algorithms are generally ingenious creations that, remarkably, can each be expressed in just a dozen or two lines of code. As a group, they represent problem- solving power of amazing scope. They have enabled the construction of computational ar- tifacts, the solution of scientific problems, and the development of commercial applications that would not have been feasible without them.
x
Booksite An important feature of the book is its relationship to the booksite algs4.cs.princeton.edu
. This site is freely available and contains an extensive amount of material about algorithms and data structures, for teachers, students, and practitioners, in- cluding:
An online synopsis. The text is summarized in the booksite to give it the same overall struc- ture 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 for
program development. Many other implementations are also available, including advanced implementations and improvements described in the book, answers to selected exercises, and client code for various applications. The emphasis is on testing algorithms in the context of meaningful applications.
Exercises and answers. The booksite expands on the exercises in the book by adding drill
exercises (with answers available with a click), a wide variety of examples illustrating the reach of the material, programming exercises with code solutions, and challenging problems.
Dynamic visualizations. Dynamic simulations are impossible in a printed book, but the
website is replete with implementations that use a graphics class to present compelling visual demonstrations of algorithm applications.
Course materials.
A complete set of lecture slides is tied directly to the material in the book and on the booksite. A full selection of programming assignments, with check lists, test data, and preparatory material, is also included.
Online course. A full set of lecture videos and self-assessment materials provide opportuni-
ties for students to learn or review the material on their own and for instructors to replace or supplement their lectures.
Links to related material. Hundreds of links lead students to background information about applications 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 or when trying to get a global picture, and you should use the booksite as a reference when pro- gramming or as a starting point when searching for more detail while online.
xi
Use in the curriculum The book is intended as a textbook in a second course in com-
puter science. It provides full coverage of core material and is an excellent vehicle for stu- dents to gain experience and maturity in programming, quantitative reasoning, and problem- solving. Typically, one course in computer science will suffice as a prerequisite—the book is intended for anyone conversant with a modern programming language and with the basic features of modern computer systems.
The algorithms and data structures are expressed in Java, but in a style accessible to people fluent in other modern languages. We embrace modern Java abstractions (including generics) but resist dependence upon esoteric features of the language.
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 maturity is definitely helpful. Ap- plications are drawn from introductory material in the sciences, again self-contained.
The material covered is a fundamental background for any student intending to major in computer science, electrical engineering, or operations research, and is valuable for any student with interests in science, mathematics, or engineering.
Context The book is intended to follow our introductory text, An Introduction to Pro-
gramming 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 sci- ence that will give any student the requisite background to successfully address computation in 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 Al-
gorithms books. In spirit, this book is closest to the first and second editions of that book, but
this text benefits from decades of experience teaching and learning that material. Sedgewick’s current Algorithms in C/C++/Java, Third Edition is more appropriate as a reference or a text for an advanced course; this book is specifically designed to be a textbook for a one-semester course for first- or second-year college students and as a modern introduction to the basics and a reference for use by working programmers.
xii
Acknowledgments This book has been nearly 40 years in the making, so full recogni-
tion of all the people who have made it possible is simply not feasible. Earlier editions of this book list dozens of names, including (in alphabetical order) Andrew Appel, Trina Avery, Marc Brown, Lyn Dupré, Philippe Flajolet, Tom Freeman, Dave Hanson, Janet Incerpi, Mike Schid- lowsky, 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 fourth edition, we are grateful to the hundreds of students at Princeton and several other institutions who have suffered through preliminary versions of the work, and to readers around the world for sending in comments and corrections through the booksite.
We are grateful for the support of Princeton University in its unwavering commitment to excellence in teaching and learning, which has provided the basis for the development of this work.
Peter Gordon has provided wise counsel throughout the evolution of this work almost from the beginning, including a gentle introduction of the “back to the basics” idea that is the foundation of this edition. For this fourth edition, we are grateful to Barbara Wood for her careful and professional copyediting, to Julie Nahil for managing the production, and to many others at Pearson for their roles in producing and marketing the book. All were ex- tremely responsive to the demands of a rather tight schedule without the slightest sacrifice to the quality of the result.
Robert Sedgewick Kevin Wayne Princeton, New Jersey January 2014 xiii
airwiseconnectionsbetweenitemsplayacriticalroleinavastarrayofcompu- tationalapplications.Therelationshipsimpliedbytheseconnectionsleadim- mediatelytoahostofnaturalquestions:Isthereawaytoconnectoneitemto
P
anotherbyfollowingtheconnections?Howmanyotheritemsareconnectedtoagiven item?Whatistheshortestchainofconnectionsbetweenthisitemandthisotheritem? Tomodelsuchsituations,weuseabstractmathematicalobjectscalledgraphs.Inthis chapter,weexaminebasicpropertiesofgraphsindetail,settingthestageforustostudy avarietyofalgorithmsthatareusefulforansweringquestionsofthetypejustposed. Thesealgorithmsserveasthebasisforattackingproblemsinimportantapplications whosesolutionwecouldnotevencontemplatewithoutgoodalgorithmictechnology.
Graphtheory,amajorbranchofmathematics,hasbeenstudiedintensivelyforhun- dredsofyears.Manyimportantandusefulpropertiesofgraphshavebeendiscovered, manyimportantalgorithmshavebeendeveloped,andmanydifficultproblemsarestill actively being studied. In this chapter, we introduce a variety of fundamental graph algorithmsthatareimportantindiverseapplications.
Likesomanyoftheotherproblemdomainsthatwehavestudied,thealgorithmicin- vestigationofgraphsisrelativelyrecent.Althoughafewofthefundamentalalgorithms arecenturiesold,themajorityoftheinterestingoneshavebeendiscoveredwithinthe lastseveraldecadesandhavebenefitedfromtheemergenceofthealgorithmictechnol- ogythatwehavebeenstudying.Eventhesimplestgraphalgorithmsleadtousefulcom- puterprograms,andthenontrivialalgorithmsthatweexamineareamongthemost elegantandinterestingalgorithmsknown.
Toillustratethediversityofapplicationsthatinvolvegraphprocessing,webeginour explorationofalgorithmsinthisfertileareabyintroducingseveralexamples.
515 n 516 Graphs
Chapter 4 Maps. Apersonwhoisplanningatripmayneedtoanswerquestionssuchas“Whatis
theshortestroutefromProvidencetoPrinceton?”Aseasonedtravelerwhohasexperi- encedtrafficdelaysontheshortestroutemayaskthequestion“Whatisthefastestway togetfromProvidencetoPrinceton?”Toanswersuchquestions,weprocessinforma- tionaboutconnections(roads)betweenitems(intersections).
Web content.
Whenwebrowsetheweb,weencounterpagesthatcontainreferences (links)tootherpagesandwemovefrompagetopagebyclickingonthelinks.The entirewebisagraph,wheretheitemsarepagesandtheconnectionsarelinks.Graph- processingalgorithmsareessentialcomponentsofthesearchenginesthathelpuslo- cateinformationontheweb.
Circuits. An electric circuit comprises devices such as transistors, resistors, and ca-
pacitorsthatareintricatelywiredtogether.Weusecomputerstocontrolmachinesthat makecircuitsandtocheckthatthecircuitsperformdesiredfunctions.Weneedtoan- swersimplequestionssuchas“Isashort-circuitpresent?”aswellascomplicatedques- tionssuchas“Canwelayoutthiscircuitonachipwithoutmakinganywirescross?” The answer to the first question depends on only the properties of the connections (wires),whereastheanswertothesecondquestionrequiresdetailedinformationabout thewires,thedevicesthatthosewiresconnect,andthephysicalconstraintsofthechip.
Schedules. Amanufacturingprocessrequiresavarietyofjobstobeperformed,under
asetofconstraintsthatspecifythatcertainjobscannotbestarteduntilcertainother jobshavebeencompleted.Howdoweschedulethejobssuchthatwebothrespectthe givenconstraintsandcompletethewholeprocessintheleastamountoftime?
Commerce. Retailers and financial institutions track buy/sell orders in a market. A
connectioninthissituationrepresentsthetransferofcashandgoodsbetweenanin- stitutionandacustomer.Knowledgeofthenatureoftheconnectionstructureinthis instancemayenhanceourunderstandingofthenatureofthemarket.
Matching. Studentsapplyforpositionsinselectiveinstitutionssuchassocialclubs,
universities,ormedicalschools.Itemscorrespondtothestudentsandtheinstitutions; connectionscorrespondtotheapplications.Wewanttodiscovermethodsformatching interestedstudentswithavailablepositions.
Computer networks. Acomputernetworkconsistsofinterconnectedsitesthatsend,
forward,andreceivemessagesofvarioustypes.Weareinterestedinknowingaboutthe natureoftheinterconnectionstructurebecausewewanttolaywiresandbuildswitches thatcanhandlethetrafficefficiently. n Graphs 517
Chapter 4 Software. A compiler builds graphs to represent relationships among modules in a
largesoftwaresystem.Theitemsarethevariousclassesormodulesthatcomprisethe system;connectionsareassociatedeitherwiththepossibilitythatamethodinoneclass mightcallanother(staticanalysis)orwithactualcallswhilethesystemisinoperation (dynamicanalysis).Weneedtoanalyzethegraphtodeterminehowbesttoallocate resourcestotheprogrammostefficiently.
Social networks. Whenyouuseasocialnetwork,youbuildexplicitconnectionswith
yourfriends.Itemscorrespondtopeople;connectionsaretofriendsorfollowers.Un- derstandingthepropertiesofthesenetworksisamoderngraph-processingapplication ofintenseinterestnotjusttocompaniesthatsupportsuchnetworks,butalsoinpoli- tics,diplomacy,entertainment,education,marketing,andmanyotherdomains.
These examples indicate the range of applicationsforwhichgraphsaretheap- propriate abstraction and also the range of computational problems that we might encounterwhenweworkwithgraphs.Thousandsofsuchproblemshavebeenstudied, butmanyproblemscanbeaddressedinthecontextofoneofseveralbasicgraphmod- els—we will study the most important onesinthischapter.Inpracticalappli-
application item connection
cations,itiscommonforthevolumeof
map intersection road
data involved to be truly huge, so that efficientalgorithmsmakethedifference
web content page link
betweenwhetherornotasolutionisat
circuit device wire allfeasible.
To organize the presentation, we
schedule job constraint
progressthroughthefourmostimpor-
commerce customer transaction
tant types of graph models: undirected
graphs (with simple connections), di- matching student application graphs(wherethedirectionofeachcon- computer network site connection
nection is significant), edge-weighted
software method call graphs (where each connection has an
associated weight), and edge-weighted
social network person friendship digraphs (where each connection has typical graph applications bothadirectionandaweight).
Our stARting pointisthestudyofgraphmodelswhereedgesarenothingmorethan connections between vertices .Weusethetermundirectedgraphincontextswherewe needtodistinguishthismodelfromothermodels(suchasthetitleofthissection),but, sincethisisthesimplestmodel,westartwiththefollowingdefinition:
Definition.
A graphisasetofverticesandacollectionofedgesthateachconnecta pairofvertices.
Vertexnamesarenotimportanttothedefinition,butweneedaway torefertovertices.Byconvention,weusethenames0throughV1 fortheverticesinaV-vertexgraph.Themainreasonthatwechoose thissystemistomakeiteasytowritecodethatefficientlyaccessesin- formationcorrespondingtoeachvertex,usingarrayindexing.Itisnot difficulttouseasymboltabletoestablisha1-1mappingtoassociate
V arbitraryvertexnameswiththeV integers between 0 and V1 (see
),sotheconvenienceofusingindicesasvertexnamescomes withoutlossofgenerality(andwithoutmuchlossofefficiency).We
v-w v w
usethenotation torefertoanedgethatconnects and ;thenota-
w-v
tion isanalternatewaytorefertothesameedge. Wedrawagraphwithcirclesfortheverticesandlinesconnecting themfortheedges.Adrawinggivesusintuitionaboutthestructureof
Two drawings of the same graph
thegraph;butthisintuitioncanbemisleading,becausethegraphis definedindependentlyofthedrawing.Forexample,thetwodrawings at left represent the same graph, because the graph is nothing more than its (unor- dered)setofverticesandits(unordered)collectionofedges(vertexpairs).
Anomalies. Ourdefinitionallowstwosimpleanomalies: n parallel self-loop n A self-loop isanedgethatconnectsavertextoitself. edges Twoedgesthatconnectthesamepairofverticesareparallel.
Mathematicians sometimes refer to graphs with parallel edges
Anomalies
as multigraphsandgraphswithnoparalleledgesorself-loopsas
simple graphs.Typically,ourimplementationsallowself-loopsand
paralleledges(becausetheyariseinapplications),butwedonotincludetheminex- amples.Thus,wecanrefertoeveryedgejustbynamingthetwoverticesitconnects.
518 n Undirected Graphs 519
4.1 Glossary Asubstantialamountofnomenclatureisassociatedwithgraphs.Mostof
thetermshavestraightforwarddefinitions,and,forreference,weconsidertheminone place:here.
Whenthereisanedgeconnectingtwovertices,wesaythattheverticesareadjacent
to oneanotherandthattheedgeisincident tobothvertices.Thedegreeofavertexisthe
numberofedgesincidenttoit.Asubgraphisasubsetofagraph’sedges(andassociated vertices)thatconstitutesagraph.Manycomputationaltasks involveidentifyingsubgraphsofvarioustypes.Ofparticular vertex
edge
interestareedgesthattakeusthroughasequenceofvertices cycle of
length 5 inagraph. path of length 4
Definition.
A path in a graph is a sequence of vertices connectedbyedges.Asimplepathisonewithnorepeated
vertex of degree 3
vertices.Acycleisapathwithatleastoneedgewhosefirst andlastverticesarethesame.Asimple cycleisacyclewith norepeatededgesorvertices(excepttherequisiterepeti- tionofthefirstandlastvertices).Thelengthofapathor connected
components acycleisitsnumberofedges.
Anatomy of a graph
Mostoften,weworkwithsimplecyclesandsimplepathsand dropthesimplemodifer;whenwewanttoallowrepeatedver- tices,werefertogeneralpathsandcycles.Wesaythatonevertexisconnected toanother
u-v-w-x
ifthereexistsapaththatcontainsbothofthem.Weusenotationlike to repre-
u x u-v-w-x-u u v w x
sentapathfrom to and torepresentacyclefrom to to to and back
u
to again.Severalofthealgorithmsthatweconsiderfindpathsandcycles.Moreover, pathsandcyclesleadustoconsiderthestructuralpropertiesofagraphasawhole:
Definition.
Agraphisconnectedifthereisapathfromeveryvertextoeveryother vertexinthegraph.Agraphthatisnot connectedconsistsofasetofconnected com-
ponents ,whicharemaximalconnectedsubgraphs.
Intuitively,iftheverticeswerephysicalobjects,suchasknotsorbeads,andtheedges werephysicalconnections,suchasstringsorwires,aconnectedgraphwouldstayin onepieceifpickedupbyanyvertex,andagraphthatisnotconnectedcomprisestwoor moresuchpieces.Generally,processingagraphnecessitatesprocessingtheconnected componentsoneatatime.
An acyclic graph is a graph with no cycles. Several of thealgorithmsthatweconsiderareconcernedwithfind- ingacyclicsubgraphsofagivengraphthatsatisfycertain properties. We need additional terminology to refer to thesestructures: Definition.
A tree isanacyclicconnectedgraph.Adis- jointsetoftreesiscalledaforest.A spanning treeofa connectedgraphisasubgraphthatcontainsallofthat graph’sverticesandisasingletree.Aspanning forestof agraphistheunionofspanningtreesofitsconnected components.
This definition of tree is quite general: with suitable refine- mentsitembracesthetreesthatwetypicallyusetomodelpro- grambehavior(function-callhierarchies)anddatastructures (BSTs, 2-3 trees, and so forth). Mathematical properties of treesarewell-studiedandintuitive,sowestatethemwithout proof.Forexample,agraphGwithVverticesisatreeifand onlyifitsatisfiesanyofthefollowingfiveconditions: n
GhasV1edgesandnocycles. n GhasV1edgesandisconnected. n Gisconnected,butremovinganyedgedisconnectsit. n Gisacyclic,butaddinganyedgecreatesacycle. n
ExactlyonesimplepathconnectseachpairofverticesinG. Severalofthealgorithmsthatweconsiderfindspanningtreesandforests,andthese propertiesplayanimportantroleintheiranalysisandimplementation.
The density of a graph is the propor- tionofpossiblepairsofverticesthatare connected by edges. A sparse graph has relativelyfewofthepossibleedgespres- ent; a dense graph has relatively few of the possible edges missing. Generally, we think of a graph as being sparse if its number of different edges is within a small constant factor of V and as be- ingdenseotherwise.Thisruleofthumb
A tree acyclic 18 vertices
17 edges connected A spanning forest sparse (E = 200) dense (E = 1000) Two graphs (V = 50) 520
n Graphs n Undirected Graphs 521
4.1 3/2
leavesagrayarea(whenthenumberofedgesis,say,~c V )butthedistinctionbe- tweensparseanddenseistypicallyveryclearinapplications.Theapplicationsthatwe considernearlyalwaysinvolvesparsegraphs.
A bipartitegraphisagraphwhoseverticeswecandivideintotwosets suchthatalledgesconnectavertexinonesetwithavertexintheother set.Thefigureatrightgivesanexampleofabipartitegraph,whereone setofverticesiscoloredredandtheothersetofverticesiscoloredblack. Bipartitegraphsariseinanaturalwayinmanysituations,oneofwhich
A bipartite graph
wewillconsiderindetailattheendofthissection. With these preparations, we are ready to move on to consider graph-processing algorithms.WebeginbyconsideringanAPIandimplementationforagraphdatatype, thenweconsiderclassicalgorithmsforsearchinggraphsandforidentifyingconnected components.Toconcludethesection,weconsiderreal-worldapplicationswherevertex namesneednotbeintegersandgraphsmayhavehugenumbersofverticesandedges.
- vertex graph with no edges
Thesecondconstructorassumesaninputformatconsistingof2E +2integervalues:
V E 522
V E
13 0 5 4 3 0 1 9 12 6 4 5 4 0 2 11 12 9 10 0 6 7 8 9 11 5 3 tinyG.txt Input format for Graph constructor (two examples) ... 250 1273 244 246 239 240 238 245 235 238 233 240 232 248 231 248 229 249 228 241 226 231 (1263 additional lines) mediumG.txt
clientcodeareshowninthetableonthefacingpage. 13
Graph
thataredepictedbelow. Severalexamplesof
mediumG.txt
and
tinyG.txt
,thenE,thenEpairsofvaluesbetween0andV1,eachpairdenotinganedge.As examples,weusethetwographs
V
adj() .
Undirected graph data type Ourstartingpointfordevelopinggraph-process-
thatal- lowsclientcodetoiteratethroughtheverticesadjacenttoagivenvertex(theorderof iterationisnotspecified).Remarkably,wecanbuildallofthealgorithmsthatwecon- siderinthissectiononthebasicabstractionembodiedin
adj()
method,andamethod
toString()
This API contains two constructors, methods to return the number of vertices and edges,amethodtoaddanedge,a
String toString() string representation apI for an undirected graph
Iterable<Integer> adj(int v) vertices adjacent to v
Graph(In in) read a graph from input stream in int V() number of vertices int E() number of edges void addEdge(int v, int w) add edge v-w to this graph
V
public class Graph Graph(int V) create a
ingalgorithmsisanAPIthatdefinesthefundamentalgraphoperations.Thisscheme allowsustoaddressgraph-processingtasksrangingfromelementarymaintenanceop- erationstosophisticatedsolutionsofdifficultproblems.
n Graphs n Undirected Graphs 523
4.1 task implementation public static int degree(Graph G, int v) { int degree = 0; v computethedegreeof
for (int w : G.adj(v)) degree++;
return degree; }public static int maxDegree(Graph G)
{ int max = 0; for (int v = 0; v < G.V(); v++) compute maximum degree if (degree(G, v) > max) max = degree(G, v); return max; } public static double averageDegree(Graph G) compute average degree{ return 2.0 * G.E() / G.V(); } public static int numberOfSelfLoops(Graph G) { int count = 0; for (int v = 0; v < G.V(); v++) count self-loops for (int w : G.adj(v)) if (v == w) count++; return count/2; // each edge counted twice } public String toString() { String s = V + " vertices, " + E + " edges\n"; for (int v = 0; v < V; v++) { stringrepresentationofthe s += v + ": "; graph’sadjacencylists for (int w : this.adj(v))
Graph ( instancemethodin ) s += w + " "; s += "\n"; } return s; } typical graph-processing code n 524 Graphs
Chapter 4 Representation alternatives. The next decision that we face in graph processing is
whichgraphrepresentation(datastructure)tousetoimplementthisAPI.Wehavetwo basicrequirements: n Wemusthavethespacetoaccommodatethetypesofgraphsthatwearelikelyto n encounterinapplications.
Graph
We want to develop time -efficientimplementationsof instancemeth- ods—thebasicmethodsthatweneedtodevelopgraph-processingclients. Theserequirementsareabitvague,butthey arestillhelpfulinchoosingamongthethree
6
2
1
5
data structures that immediately suggest themselvesforrepresentinggraphs: n Bag
objects
An adjacency matrix ,wherewemain- tain a V-by-V booleanarray,withthe
v w
entry in row andcolumn definedto adj[]
5
4 true
be ifthereisanedgeinthegraph 1
v w
thatconnectsvertex andvertex , and 2
5
6
3 false
to be otherwise.Thisrepresenta- 3 tionfailsonthefirstcount—graphs 4
3
4
withmillionsofverticesarecommon 2 5 andthespacecostfortheV boolean 6
4 n valuesneededisprohibitive. 7 Edge
8 An array of edges ,usingan class 8 representations int 9
withtwoinstancevariablesoftype .
of the same edge 10
7 Thisdirectrepresentationissimple, 11 butitfailsonthesecondcount— 12
11
10
12 adj()
implementing wouldinvolve examiningalltheedgesinthegraph. n
9 An array of adjacency lists ,wherewe
maintainavertex-indexedarrayoflists
9
12
oftheverticesadjacenttoeachvertex.
11
9 Thisdatastructuresatisfiesbothre-
quirementsfortypicalapplicationsand
Adjacency-lists representation (undirected graph)