[ Team LiB ] • Table of C ontents A lgorithms in Java, Third Edition, Part 5: Graph A lgorithms By Robert Sedgewick Publishe r: Add ison We sle y Pub Da te: July 15, 2003 ISBN: 0-201-36121-3 Page s: 528 O nce again, Robert Sedgewick provides a current and comprehensive introduction to important algorithms. The focus this time is on graph algorithms, which are increasingly critical for a wide range of applications, such as network connectivity, circuit design, sc heduling, transaction processing, and resource allocation. In this book, Sedgewick offers the s ame successful blend of theory and practice that has made his work popular with programmers for many years. Michael Schidlowsky and Sedgewick have developed concise new Java implementations that both express the methods in a natural and direct manner and also can be used in real applications. Algorithms in Java, Third Edition, Part 5: Graph Algorithms is the second book in Sedgewick's thoroughly revis ed and rewritten series. The first book, Parts 1-4, addresses fundamental algorithms, data structures, sorting, and searching. A forthcoming third book will foc us on strings, geometry, and a range of advanced algorithms. Each book's expanded coverage features new algorithms and implementations, enhanced descriptions and diagrams, and a wealth of new exercises for polishing s kills. The natural match between Java classes and abstract data type (A DT) implementations makes the c ode more broadly useful and relevant for the modern object-oriented programming environment. The Web site for this book (www.cs.princeton.edu/~rs/) provides additional source code for programmers along with a variety of academic support materials for educators. Coverage includes: A complete overview of graph properties and types Diagraphs and DA Gs Minimum spanning trees Shortest paths Network flows Diagrams, sample Java code, and detailed algorithm desc riptions A landmark revision, Algorithms in Java, Third Edition, Part 5 provides a complete tool set for programmers to implement, debug, and use graph algorithms across a wide range of computer applications. [ Team LiB ] 1 / 264 [ Team LiB ] • Table of C ontents A lgorithms in Java, Third Edition, Part 5: Graph Algorithms By Robert Sedgewick Publishe r: Add ison We sle y Pub Da te: July 15, 2003 ISBN: 0-201-36121-3 Page s: 528 Copyright Prefa ce Algorithm s Scope Use in the C urriculum Algorithm s o f Practica l Use Progra m m ing La nguage Ack no wle dgm ents Java C o nsultant's Prefa ce Note s on Exe rcis e s Part V: Graph Algorithm s C ha pte r 17. Graph Propertie s a nd Type s Se ction 17.1. Glo ssary Se ction 17.2. Gra ph ADT Se ction 17.3. Adjacency-Ma trix R e presenta tio n Se ction 17.4. Adjacency-Lists Re prese nta tio n Se ction 17.5. Va ria tions , Ex tensio ns, a nd Co sts Se ction 17.6. Gra ph Ge ne rato rs Se ction 17.7. Sim ple, Eule r, a nd Ha milto n Paths Se ction 17.8. Gra ph-P rocessing P roble ms C ha pte r 18. Graph Se arch Se ction 18.1. Explo ring a Ma ze Se ction 18.2. Depth-First Se a rch Se ction 18.3. Gra ph-Sea rch ADT Methods Se ction 18.4. Prope rtie s o f DFS Forests Se ction 18.5. DFS Alg o rithm s Se ction 18.6. Se parability a nd Biconne ctivity Se ction 18.7. Bre a dth-First Sea rch Se ction 18.8. Ge neralize d Graph Se arch Se ction 18.9. Analysis of Graph Algorithm s C ha pte r 19. Digraphs a nd DAGs Exercise s Se ction 19.1. Glo ssary a nd R ule s of the Ga m e Se ction 19.2. Anato my o f DFS in Digraphs Se ction 19.3. Re a chability a nd Transitive C losure Se ction 19.4. Equivalence R elations and Partial O rde rs Se ction 19.5. DAGs Se ction 19.6. Topolo gical So rting Se ction 19.7. Re a chability in DAGs Se ction 19.8. Strong C o m po ne nts in Digraphs Se ction 19.9. Transitive Clo sure Re visite d Se ction 19.10. P e rspective C ha pte r 20. Minim um Spa nning Trees Exercise s Se ction 20.1. Re p re se nta tions Se ction 20.2. Unde rlying P rinciple s of MST Algorithm s Se ction 20.3. Prim 's Algo rithm and Priority-First Se arch 2 / 264 Se ction 20.4. Krus k al's Algorithm Se ction 20.5. Bo ruvk a 's Algorithm Se ction 20.6. Com pa risons a nd I m prove me nts Se ction 20.7. Euclide a n MST C ha pte r 21. Shortest P aths Exercise s Se ction 21.1. Unde rlying P rinciple s Se ction 21.2. Dijkstra's Algorithm Se ction 21.3. All-Pa irs Shorte st P aths Se ction 21.4. Shorte st Paths in Acyclic Netwo rks Se ction 21.5. Euclide a n Netwo rk s Se ction 21.6. Re d uction Se ction 21.7. Neg a tive We ights Se ction 21.8. Pe rspective C ha pte r 22. Netwo rk Flow Se ction 22.1. Flo w Netwo rks Se ction 22.2. Augm enting-Path Max flo w Algorithm s Se ction 22.3. Preflow-P ush Ma x flow Alg o rithm s Se ction 22.4. Ma x flow R eductio ns Se ction 22.5. Mincos t Flo ws Se ction 22.6. Network Sim ple x Algorithm Se ction 22.7. Mincos t-Flo w Re ductio ns Se ction 22.8. Pe rspective R e fe re nce s fo r Part Five [ Team LiB ] 3 / 264 [ Team LiB ] Copyright 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 A ddison-Wesley was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author 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 assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers disc ounts on this book when ordered in quantity for special sales. For more information, pleas e contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact: International Sales (317) 581-3793 international@pearsontechgroup.com V isit Addison-Wesley on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Sedgewick, Robert, 1946 — A lgorithms in Java / Robert Sedgewic k. 3d ed. p. cm. ISBN 0-201-36121-3 (alk. paper) Includes bibliographical references and index. Contents: v. 2, pts. 5. Graph algorithms. 1. Java (Computer program language) 2. C omputer algorithms. I. T itle. QA 76.73.C15S 2003 005.13 3 dc20 92-901 Copyright © 2004 by Pearson Education, Inc A ll rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in C anada. For information on obtaining permiss ion for use of material from this work, please submit a written request to: Pearson Education, Inc. Rights and C ontracts Department 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 Text printed on recycled paper 1 2 3 4 5 6 7 8 9 10—CRS—0706050403 First printing, July 2003 Dedication To Adam, Andrew, Brett, Robbie, and especially Linda [ Team LiB ] 4 / 264 [ Team LiB ] Preface Graphs and Graph algorithms are pervasive in modern computing applications. This book desc ribes the most important known methods for solving the graph- processing problems that arise in practice. Its primary aim is to make these methods and the basic principles behind them acc ess ible to the growing number of people in need of knowing them. The material is developed from first principles, starting with basic information and working through classical methods up through modern techniques that are s till under development. C arefully chosen examples, detailed figures, and c omplete implementations supplement thorough descriptions of algorithms and applications. [ Team LiB ] 5 / 264 [ Team LiB ] Algorithms This book is the s econd of three volumes that are intended to s urvey the mos t important computer algorithms in use today. The first volume (P arts 1–4) covers fundamental concepts (P art 1), data structures (P art 2), sorting algorithms (Part 3), and searching algorithms (Part 4); this volume (P art 5) c overs graphs and graph algorithms; and the (yet to be published) third volume (P arts 6–8) c overs strings (Part 6), computational geometry (P art 7), and advanced algorithms and applications (Part 8). The books are useful as texts early in the computer science curriculum, after students have acquired basic programming s kills and familiarity with c omputer systems, but before they have taken specialized courses in advanced areas of computer science or computer applications. The books als o are useful for self-study or as a reference for people engaged in the development of computer systems or applications programs because they contain implementations of useful algorithms and detailed information on these algorithms' performance characteristics. 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 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, and detailed commentary on all the figures and programs. This new material provides both coverage of new topics and fuller explanations of many of the c lassic algorithms. A new emphasis on abstract data types throughout the books makes the programs more broadly useful and relevant in modern object-oriented programming environments. People who have read previous editions will find a wealth of new information throughout; all readers will find a wealth of pedagogical material that provides effective access to essential concepts. These books are not just for programmers and computer science students. Everyone who uses a c omputer wants it to run faster or to s olve 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 simulation problems in physics to genetic-sequencing problems in molecular biology, the basic methods described here have become ess ential in scientific research; and from database systems to I nternet search engines, they have become essential parts of modern software systems. A s the scope of computer applications becomes more widespread, so grows the impact of basic algorithms . T he goal of this book is to serve as a resource so that students and professionals can know and make intelligent use of graph algorithms as the need aris es in whatever computer application they might undertake. [ Team LiB ] 6 / 264 [ Team LiB ] Scope This book, Algorithms in Java, Third Edition, Part 5: Graph Algorithms, contains six chapters that cover graph properties and types, graph s earch, direc ted graphs, minimal spanning trees, shortest paths, and networks . T he descriptions here are intended to give readers an understanding of the basic properties of as broad a range of fundamental graph algorithms as possible. You will most appreciate the material here if you have had a course covering basic principles of algorithm design and analys is and programming experience in a high-level language s uch as Java, C++, or C. Algorithms in Java, Third Edition, Parts 1–4, is certainly adequate preparation. This volume ass umes basic knowledge about arrays, linked lists, and abstract data types (ADTs ) and makes use of priority-queue, symbol-table, and union-find A DTs—all of which are described in detail in Parts 1–4 (and in many other introductory texts on algorithms and data structures). Bas ic properties of graphs and graph algorithms are developed from first principles, but full understanding often can lead to deep and diffic ult mathematics. A lthough the discussion of advanced mathematic al concepts is brief, general, and descriptive, you certainly need a higher level of mathematical maturity to appreciate graph algorithms than you do for the topics in Parts 1–4. Still, readers at various levels of mathematical maturity will be able to profit from this book. T he topic dictates this approach: Some elementary graph algorithms that should be understood and used by everyone differ only slightly from some advanced algorithms that are not understood by anyone. The primary intent here is to place important algorithms in context with other methods throughout the book, not to teach all of the mathematical material. But the rigorous treatment demanded by good mathematic s often leads us to good programs, so I have tried to provide a balance between the formal treatment favored by theoreticians and the coverage needed by practitioners, without sacrificing rigor. [ Team LiB ] 7 / 264 [ Team LiB ] Use in the Curriculum There is a great deal of flexibility in how the material here can be taught, depending on the taste of the instructor and the preparation of the students. T here is sufficient coverage of basic material for the book to be used to teach data structures to beginners, and there is sufficient detail and coverage of advanced material for the book to be used to teach the design and analysis of algorithms to upper-level students. Some instructors may wish to emphasize implementations and practical concerns; others may wish to emphasize analysis and theoretical concepts. For a more comprehensive course, this book is also available in a special bundle with Parts 1–4; thereby instructors can cover fundamentals, data structures, sorting, searching, and graph algorithms in one consistent style. 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. O thers involve implementing and putting together the algorithms, or running empirical studies to compare variants of the algorithms and to learn their properties. Still others are a repository for important information at a level of detail that is not appropriate for the text. Reading and thinking about the exercises will pay dividends for every reader. [ Team LiB ] 8 / 264 [ Team LiB ] Algorithms of Practical Use A nyone wanting to use a computer more effectively can use this book for reference or for self-study. P eople 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 cas es, algorithms in one chapter make use of methods from a previous chapter. The orientation of the book is to study algorithms likely to be of practical use. The book provides information about the tools of the trade to the point that readers can confidently implement, debug, and put algorithms to work to s olve a problem or to provide functionality in an application. Full implementations of the methods discussed are included, as are descriptions of the operations of these programs on a consistent set of examples. Because we work with real code, rather than write pseudo-code, you c an put the programs to practical use quic kly. 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 situations that 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, or to try your own modifications. Indeed, one practical application of the algorithms has been to produce the hundreds of figures throughout the book. Many algorithms are brought to light on an intuitive level through the visual dimension provided by these figures. Characteris tics of the algorithms and of the situations in which they might be useful are disc ussed in detail. Connections to the analysis of algorithms and theoretical computer science are developed in context. When appropriate, empirical and analytic results are presented to illustrate why c ertain algorithms are preferred. When interesting, the relationship of the practic al algorithms being discussed to purely theoretical results is desc ribed. Specific information on performance characteristics of algorithms and implementations is synthesized, encapsulated, and discuss ed throughout the book. [ Team LiB ] 9 / 264 [ Team LiB ] Programming Language The programming language used for all of the implementations is Java. The programs use a wide range of standard Java idioms, and the text includes concis e descriptions of each construct. Mike Schidlowsky and I developed a style of Java programming based on A DTs that we feel is an effective way to present the algorithms and data structures as real programs. We have striven for elegant, c ompact, efficient, and portable implementations. T he style is consistent whenever possible, so programs that are similar look similar. A goal of this book is to present the algorithms in as simple and direc t a form as possible. For many of the algorithms , the similarities remain regardless of which language is used: Dijkstra's algorithm (to pick one prominent example) is Dijkstra's algorithm, whether express ed in Algol-60, Basic, Fortran, Smalltalk, A da, Pascal, C, C ++, Modula-3, PostScript, Java, P ython, or any of the countless other programming languages and environments in whic h it has proved to be an effective graph-proces sing method. On the one hand, our code is informed by experience with implementing algorithms in these and numerous other languages (C and C ++ versions of this book are also available); on the other hand, some of the properties of some of these languages are informed by their designers' experience with s ome of the algorithms and data structures that we consider in this book. In the end, we feel that the code presented in the book both precisely defines the algorithms and is useful in practice. [ Team LiB ] 10 / 264 [...]... the matching problems described at the beginning of this chapter A ny subgraph of a bipartite graph is bipartite Figure 17 .5 A bipartite graph All edges in this graph connect odd-numbered vertices with even-numbered ones, so it is bipartite The bottom diagram makes the property obvious Graphs as defined to this point are called undirected graphs In directed graphs, also known as digraphs, edges are... justified in typical applications that involve static graphs, and 32 / 264 use Java collections, because maintaining complex structures with multiple pointers per node is not justified in typical applications that involve static graphs, and because we wish to avoid getting bogged down in layers of abstraction or in low-level details of maintaining multiple pointers when implementing graph-processing algorithms... for input/output and a similar class named GraphUtilities for utility methods such as the extract-edges method of Program 17.2 Program 17.4 Graph-processing input/output interface This A DT interface illustrates how we might package related graph-processing methods together in a single class It defines methods for inserting edges defined by pairs of integers on standard input (see Exercise 17.12), inserting... reading edges (pairs of integers between 0 and V – 1) from standard input 17.13 Write an A DT client that adds all the edges in a given array to a given graph 17.14 Write a method that invokes edges and prints out all the edges in the graph, in the format used in this text (vertex numbers separated by a hyphen) 17. 15 Develop an implementation for the connectivity A DT of Program 17 .5, using a union-find... is equivalent to processing all the edges in the graph), as in these methods A s discussed in Section 17 .5, it is convenient to package related graph-processing methods into a single class Program 17.4 is an A DT interface for such a class, which is named GraphIO It defines the show method of Program 17.3 and two methods for inserting into a graph edges taken from standard input (see Exercise 17.12... destination and the number of edges where it is the source, respectively) Sometimes, we are justified in thinking of an undirected graph as a digraph that has two directed edges (one in each direction); other times, it is useful to think of undirected graphs simply in terms of connections Normally, as discussed in detail in Section 17.4, we use the same representation for directed and undirected graphs... initialized If desired, we could allow for inserting and deleting vertices (see Exercise 17.20) A key feature of the constructor in Program 17.7 is that Java automatically initializes the matrix by setting its entries all to false We need to be mindful that this operation takes time proportional to V2, no matter how many edges are in the graph Error checks for insufficient memory are not included in. .. to traverse the linked list associated with vertex v A n invocation of beg() followed by a sequence of invocations of nxt() (checking that end() is false before each invocation) gives a sequence of the vertices adjacent to v in G AdjList getAdjList(int v) { return new AdjLinkedList(v); } private class AdjLinkedList implements AdjList { private int v; private Node t; AdjLinkedList(int v) { this.v =... use the same interface needed by complicated tasks O ne method can access a field intended for use by another method, contrary to encapsulation principles that we would like to follow Interfaces of this kind have come to be known as fat interfaces In a book filled with graph-processing algorithms, an interface of this sort would be fat indeed A nother approach is to use inheritance to define various... graph-processing A DTs described in this section, using the A DT conventions described in Section 4 .5 It constructs a graph with V vertices, inserts edges taken from standard input, prints the resulting graph if it is small, and computes (and prints) the number of connected components It uses the Graph, GraphIO, and GraphCC A DTs that are defined in Program 17.1, Program 17.4, and Program 17 .5 (respectively) . your list contains 3-4 -5- 3, it should not contain 3- 5- 4-3, 4 -5- 3-4, 4-3 -5- 4, 5- 3-4 -5, or 5- 4-3 -5. 17.4 C onsider the graph Determine the number of connected components, give a spanning forest,. mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in C anada. For information on obtaining permiss. encountered graphs, briefly, in Part 1. Indeed, the first algorithms that we considered in detail, the union-find algorithms in Chapter 1, are prime examples of graph algorithms. We also used graphs in