algorithms on trees and graphs978-3-662-04921... · 2017-08-25 · algorithms by providing,...

12
Algorithms on Trees and Graphs

Upload: others

Post on 29-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

Algorithms on Trees and Graphs

Page 2: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

Springer-Verlag Berlin Heidelberg GmbH

Page 3: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

Gabriel Valiente

Algorithms on Trees and Graphs

With 157 Figures

" Springer

Page 4: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

prof Dr. Gabriel Valiente

Technical University of Catalonia Department of Software Jordi Girona, 1- 3 (Module C6) 08034 Barcelona

Spain

Library of Congress Cataloging-in-Publication Data applied for

Die Deutsche Bibliothek-CIP-Einheitsaufnahme Valiente, Gabriel: Algorithms on trees and graphs / Gabriel Valiente

ISBN 978-3-642-07809-5 ISBN 978-3-662-04921-1 (eBook) DOI 10.1007/978-3-662-04921-1

ACM Computing Classification (1998) E2, G.2.2, GA, D.I

ISBN 978-3-642-07809-5

This work is subject to copyright. Ali rights are reserved. whether the whole or part of the material is concerned, specificallythe rights oftranslation. reprinting, reuse ofillustrations, recitation. broadcasting, reproduction on microfilm or in any olher way, and storage in data banks. Duplication of this publication or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, in ils current version, and permission for use must always be obtained from Springer-Verlag Berlin Heidelberg GmbH. Violalions are liable for prosecution under the German Copyright Law.

© Springer-Verlag Berlin Heidelberg 2002

Originally published by Springer-Verlag Berlin Heidelberg New York in 2002 Softcover reprint of the hardcover 1st edition 2002

The use of designations, trademarks. etc. in this publication does not imply. even in the absence of a specific statement, that such names are exempt from the relevant protective laws and reguJations and therefore free for general use.

Cover Design: KunkelLopka. Heidelberg Typesetting: Computer ta film by author 5 data Printed an acid-free paper SPIN 10855180 45/3142PS 5432 I O

Page 5: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

To Daina, Kristians, and Davis

Page 6: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

Preface

Graph algorithms, a long-established subject in mathematics and com­puter science curricula, are also of much interest to disciplines such as computational molecular biology and computational chemistry. This book goes beyond the classical graph problems of shortest paths, spanning trees, flows in networks, and matchings in bipartite graphs, and addresses further algorithmic problems of practical application of trees and graphs. Much of the material presented in the book is only available in the specialized research literature.

The book is structured around the fundamental problem of isomor­phism. Tree isomorphism is covered in much detail, together with the related problems of subtree isomorphism, maximum common subtree isomorphism, and tree comparison. Graph isomorphism is also cov­ered in much detail, together with the related problems of sub graph isomorphism, maximal common subgraph isomorphism, and graph edit distance. Building blocks for solving some of these isomorphism problems are algorithms for finding maximal and maximum cliques.

Most intractable graph problems of practical application are not even approximable to within a constant bound, and several of the isomorphism problems addressed in this book are no exception. The book can thus be seen as a companion to recent texts on approxima­tion algorithms [15, 344], but also as a complement to previous texts on combinatorial and graph algorithms [69, 78, 107, 127, 130, 203, 232,236,229,255,271,304,321,336,367].

The book is conceived on the ground of first, introducing simple algorithms for these problems in order to develop some intuition be­fore moving on to more complicated algorithms from the research lit­erature, and second, stimulating graduate research on tree and graph

Page 7: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

VIII Preface

algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development.

Algorithms are presented on an intuitive basis, followed by a de­tailed exposition in a literate programming style. Correctness proofs are also given, together with a worst-case analysis of the algorithms. Further, full C++ implementation of all the algorithms using the LEDA library of efficient data structures and algorithms is given throughout the book. These implementations include result checking of implementation correctness using correctness certificates.

The choice of LEDA, which is becoming a defacto standard for graduate courses on graph algorithms throughout the world, is not casual, because it allows the student, lecturer, researcher, and prac­titioner to complement algorithmic graph theory with actual imple­mentation and experimentation, building upon a thorough library of efficient implementations of modern data structures and fundamental algorithms.

An interactive demonstration including animations of all the al­gorithms using LEDA is given in an appendix and is available on a web site, at http://www.lsi. upc. esrvaliente/algorithm/, together with the source code of all the algorithms. The interactive demonstration also includes visual checkers of implementation cor­rectness.

Structure

The book is divided into four parts. Part I is of an introductory na­ture and consists of two chapters. Chap. 1 includes a review of ba­sic graph-theoretical notions and results used throughout the book, a brief primer of literate programming, and an exposition of the imple­mentation correctness approach by result checking using correctness certificates.

Chap. 2 is devoted exclusively to the fundamental algorithmic tech­niques used in the book: backtracking, branch-and-bound, divide-and­conquer, and dynamic programming. These techniques are illustrated by means of a running example: algorithms for the tree edit distance problem.

Page 8: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

Preface IX

Part II also consists of two chapters. Chap. 3 addresses the most common methods for traversing general, rooted trees: depth-first pre­fix leftmost (preorder), depth-first prefix rightmost, depth-first postfix leftmost (postorder), depth-first postfix rightmost, breadth-first left­most (top-down), breadth-first rightmost, and bottom-up traversal. Tree drawing is also discussed as an application of tree traversal meth­ods.

Chap. 4 addresses several isomorphism problems on ordered and unordered trees: tree isomorphism, subtree isomorphism, and maxi­mum common subtree isomorphism. Computational molecular biol­ogy is also discussed as an application of the different isomorphism problems on trees.

Part III consists of three chapters. Chap. 5 addresses the most common methods for traversing graphs: depth-first and breadth-first traversal, which respectively generalize depth-first prefix leftmost (preorder) and breadth-first leftmost (top-down) tree traversal. Left­most depth-first traversal of an undirected graph, a particular case of depth-first traversal, is also discussed. Isomorphism of ordered graphs is also discussed as an application of graph traversal methods.

Chap. 6 addresses the related problems of finding cliques, inde­pendent sets, and vertex covers in trees and graphs. Multiple align­ment of protein sequences in computational molecular biology is also discussed as an application of clique algorithms.

Chap. 7 addresses several isomorphism problems on graphs: graph isomorphism, graph automorphism, subgraph isomorphism, and max­imal common subgraph isomorphism. Chemical structure search is also discussed as an application of the different graph isomorphism problems.

Part IV consists of three appendices, followed by bibliographic ref­erences and an index. Appendix A gives an overview of LEDA, in­cluding a simple C++ representation of trees as LEDA graphs, and a C++ implementation of radix sort using LEDA. The interactive demonstration of graph algorithms presented throughout the book is put together in Appendix B. Finally, Appendix C contains a complete index to all program modules described in the book.

Page 9: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

X Preface

Audience

This book is suitable for use in upper undergraduate and graduate level courses on algorithmic graph theory. It can also be used as a sup­plementary text in basic undergraduate and graduate-level courses on algorithms and data structures, and in computational molecular biol­ogy and computational chemistry courses as well. Some basic knowl­edge of discrete mathematics, data structures, algorithms, and pro­gramming at the undergraduate level is assumed.

Acknowledgments

This book is based on lectures taught at the Technical University of Catalonia, Barcelona between 1996 and 2002, and the University of Latvia, Riga between 2000 and 2002.

Numerous colleagues at the Technical University of Catalonia have influenced the approach to data structures and algorithms on trees and graphs expressed in this book. In particular, the author would like to thank Jose L. Balcazar, Rafel Casas, Jordi Cortadella, Josep Diaz, Conrado Martinez, Xavier Messeguer, Roberto Nieuwenhuis, Fernando Orejas, Jordi Petit, Salvador Roura, and Maria Serna, to name just a few. It has been a pleasure to share teaching and research experiences with them over the last several years.

The author would also like to thank Ricardo Baeza-Yates, Francesc Rosse116, and Steven Skiena, for their standing support and encour­agement, and Hans-Jorg Kreowski, for supporting basic and applied research on graph algorithms within the field of graph transformation.

It has been a pleasure for the author to work out editorial matters together with Alfred Hofmann, Ingeborg Mayer, and Peter StraBer of Springer-Verlag.

Special thanks go to the Technical University of Catalonia for funding the sabbatical year during which this book was written, and to the Institute of Mathematics and Computer Science, University of Latvia, in particular to Janis Barzdins and Rusins Freivalds, for host-, , ing the sabbatical visit.

July 2002 Gabriel Valiente

Page 10: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

Contents

Part I. Introduction

1. Introduction..................................... 3 1.1 Trees and Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Literate Programming ...... . . . . . . . . . . . . . . . . . . . . .. 23 1.3 Implementation Correctness . . . . . . . . . . . . . . . . . . . . . .. 28 1.4 Representation of Trees and Graphs. . . . . . . . . . . . . . . .. 34 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 49 Bibliographic Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 49 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 50 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 51

2. Algorithmic Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . .. 55 2.1 The Tree Edit Distance Problem. . . . . . . . . . . . . . . . . . .. 55 2.2 Backtracking.................................... 70 2.3 Branch-and-Bound............................... 80 2.4 Divide-and-Conquer.............................. 85 2.5 Dynamic Programming . . . . . . . . . . . . . . . . . . . . . . . . . .. 94 Summary ........................................... 107 Bibliographic Notes .................................. 107 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 108 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 110

Part II. Algorithms on Trees

3. Tree Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 113 3.1 Preorder Traversal of a Tree ....................... 113 3.2 Postorder Traversal of a Tree ...................... 119

Page 11: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

XII Contents

3.3 Top-Down Traversal of a Tree ..................... 124 3.4 Bottom-Up Traversal of a Tree ..................... 129 3.5 Applications .................................... 135 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 145 Bibliographic Notes .................................. 145 Review Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 146 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 147

4. Tree Isomorphism . ................................ 151 4.1 Tree Isomorphism ............................... 151 4.2 Subtree Isomorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . .. 170 4.3 Maximum Common Subtree Isomorphism ........... 206 4.4 Applications .................................... 239 Summary ........................................... 242 Bibliographic Notes .................................. 244 Review Problems .................................... 246 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

Part ID. Algorithms on Graphs

5. Graph Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 5.1 Depth-First Traversal of a Graph ................... 255 5.2 Breadth-First Traversal of a Graph .................. 276 5.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 Summary ........................................... 294 Bibliographic Notes .................................. 294 Review Problems .................................... 295 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297

6. Clique, Independent Set, and Vertex Cover. . . . . . . . . . . . 299 6.1 Cliques, Maximal Cliques, and Maximum Cliques .... 299 6.2 Maximal and Maximum Independent Sets ........... 322 6.3 Minimal and Minimum Vertex Covers ............... 332 6.4 Applications .................................... 341 Summary ........................................... 346 Bibliographic Notes .................................. 346

Page 12: Algorithms on Trees and Graphs978-3-662-04921... · 2017-08-25 · algorithms by providing, together with the underlying theory, a solid basis for experimentation and further development

Contents XIII

Review Problems .................................... 347 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

7. Graph Isomorphism . .............................. 351 7.1 Graph Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 7.2 Graph Automorphism ............................ 363 7.3 Subgraph Isomorphism ........................... 367 7.4 Maximal Common Subgraph Isomorphism ........... 377 7.5 Applications .................................... 389 Summary ........................................... 392 Bibliographic Notes .................................. 392 Review Problems .................................... 394 Exercises ........................................... 395

Part IV. Appendices

A. An Overview of LEDA ............................. 401 A.I Introduction....... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 A.2 Data Structures .................................. 402 A.3 Fundamental Graph Algorithms .................... 424 A.4 A Simple Representation of Trees .................. 426 A.5 A Simple Implementation of Radix Sort ............. 434 Bibliographic Notes .................................. 445

B. Interactive Demonstration of Graph Algorithms ....... 447

C. Program Modules . ................................ 453

Bibliography ........................................ 459

Index . .............................................. 489