100% found this document useful (3 votes)
19 views

Algorithms on Trees and Graphs: With Python Code 2nd Edition Gabriel Valiente instant download

The document discusses the second edition of 'Algorithms on Trees and Graphs: With Python Code' by Gabriel Valiente, which has been extensively revised to include detailed pseudocode and Python implementations of algorithms. It covers various topics such as graph traversal techniques, isomorphism problems, and practical applications in computational biology. The book is suitable for upper undergraduate and graduate courses in algorithmic graph theory and related fields.

Uploaded by

kaletgodin93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
19 views

Algorithms on Trees and Graphs: With Python Code 2nd Edition Gabriel Valiente instant download

The document discusses the second edition of 'Algorithms on Trees and Graphs: With Python Code' by Gabriel Valiente, which has been extensively revised to include detailed pseudocode and Python implementations of algorithms. It covers various topics such as graph traversal techniques, isomorphism problems, and practical applications in computational biology. The book is suitable for upper undergraduate and graduate courses in algorithmic graph theory and related fields.

Uploaded by

kaletgodin93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Algorithms on Trees and Graphs: With Python Code

2nd Edition Gabriel Valiente install download

https://ebookmeta.com/product/algorithms-on-trees-and-graphs-
with-python-code-2nd-edition-gabriel-valiente/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Applied Cryptography Protocols Algorithms and Source


Code in C 2nd Edition Bruce Schneier

https://ebookmeta.com/product/applied-cryptography-protocols-
algorithms-and-source-code-in-c-2nd-edition-bruce-schneier/

Learn OpenCV with Python by Examples Implement Computer


Vision Algorithms Provided by OpenCV with Python for
Image Processing Object Detection and Machine Learning
2nd Edition James Chen
https://ebookmeta.com/product/learn-opencv-with-python-by-
examples-implement-computer-vision-algorithms-provided-by-opencv-
with-python-for-image-processing-object-detection-and-machine-
learning-2nd-edition-james-chen/

Practical Channel-Aware Resource Allocation: With


MATLAB and Python Code Michael Ghorbanzadeh

https://ebookmeta.com/product/practical-channel-aware-resource-
allocation-with-matlab-and-python-code-michael-ghorbanzadeh/

Dewey for Artists 1st Edition Mary Jane Jacob

https://ebookmeta.com/product/dewey-for-artists-1st-edition-mary-
jane-jacob/
Oblivious A Contemporary M M Best Friends to Lovers Gay
Romance Novel 1st Edition Leslie Mcadam

https://ebookmeta.com/product/oblivious-a-contemporary-m-m-best-
friends-to-lovers-gay-romance-novel-1st-edition-leslie-mcadam/

Treatment of Voice Disorders Second Edition Robert


Thayer Sataloff

https://ebookmeta.com/product/treatment-of-voice-disorders-
second-edition-robert-thayer-sataloff/

Potential Exports and Nontariff Barriers to Trade


Maldives National Study 1st Edition Asian Development
Bank

https://ebookmeta.com/product/potential-exports-and-nontariff-
barriers-to-trade-maldives-national-study-1st-edition-asian-
development-bank/

Death Notice Zhou Haohui

https://ebookmeta.com/product/death-notice-zhou-haohui/

Come to Papa A Man of the Month Club Novella A Small


Town Age Gap Romance 1st Edition Matilda Martel

https://ebookmeta.com/product/come-to-papa-a-man-of-the-month-
club-novella-a-small-town-age-gap-romance-1st-edition-matilda-
martel/
Valiant Indian Warriors Philosophies, Approaches, and
Victories First Edition C. B. Bhange

https://ebookmeta.com/product/valiant-indian-warriors-
philosophies-approaches-and-victories-first-edition-c-b-bhange/
Texts in Computer Science

Gabriel Valiente

Algorithms
on Trees
and Graphs
With Python Code
Second Edition
Texts in Computer Science

Series Editors
David Gries, Department of Computer Science, Cornell University, Ithaca, NY,
USA
Orit Hazzan , Faculty of Education in Technology and Science, Technion—Israel
Institute of Technology, Haifa, Israel
More information about this series at http://www.springer.com/series/3191
Gabriel Valiente

Algorithms on Trees
and Graphs
With Python Code
Second Edition

123
Gabriel Valiente
Department of Computer Science
Technical University of Catalonia
Barcelona, Spain

ISSN 1868-0941 ISSN 1868-095X (electronic)


Texts in Computer Science
ISBN 978-3-030-81884-5 ISBN 978-3-030-81885-2 (eBook)
https://doi.org/10.1007/978-3-030-81885-2

1st edition: © Springer-Verlag Berlin Heidelberg 2002


2nd edition: © The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature
Switzerland AG 2021
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether
the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and
transmission or information storage and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, 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 regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, expressed or implied, with respect to the material contained
herein or for any errors or omissions that may have been made. The publisher remains neutral with regard
to jurisdictional claims in published maps and institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
To my child Aleksandr,
six years old,
who is eager to grow
and read it all
Preface to the Second Edition

The first edition of this book has been extensively used for graduate teaching and
research all over the world in the last two decades. We have listed hundreds of
citing publications in Appendix C, including books, scientific articles in journals
and conference proceedings, M.Sc. and Ph.D. theses, and even United States
patents.
In this new edition, we have substituted detailed pseudocode for both the literate
programming description and the implementation of the algorithms using the
LEDA library of efficient data structures and algorithms. Although the pseudocode
is detailed enough to allow for a straightforward implementation of the algorithms
in any modern programming language, we have added a proof-of-concept imple-
mentation in Python of all the algorithms in Appendix A. This is, therefore, a
thoroughly revised and extended edition.
Regarding new material, we have added an adjacency map representation of
trees and graphs, and both maximum cardinality and maximum weight bipartite
matching as an additional application of graph traversal techniques. Further, we
have revised the end-of-chapter problems and exercises and have included solutions
to all the problems in Appendix B.
It has been a pleasure for the author to work out editorial matters together with
Sriram Srinivas and, especially, Wayne Wheeler of Springer Nature, whose
standing support and encouragement have made this new edition possible.
Last, but not least, any minor errors found so far have been corrected in this
second edition, the bibliographic notes and references have been updated, and the
index has been substantially enhanced. Even though the author and the publisher
have taken much care in the preparation of this book, they make no representation,
express or implied, with regard to the accuracy of the information contained herein
and cannot accept any legal responsibility or liability for incidental or consequential
damages arising out of the use of the information, algorithms, or program code
contained in this book.

Barcelona, Spain Gabriel Valiente


June 2021

vii
Preface to the First Edition

Graph algorithms, a long-established subject in mathematics and computer 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
on trees and graphs. Much of the material presented on the book is only available in
the specialized research literature.
The book is structured around the fundamental problem of isomorphism. Tree
isomorphism is covered in much detail, together with the related problems of
subtree isomorphism, maximum common subtree isomorphism, and tree compari-
son. Graph isomorphism is also covered in much detail, together with the related
problems of subgraph isomorphism, maximal common subgraph isomorphism, and
graph edit distance. A building block 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 approx-
imable 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 approximation algorithms [1, 16], but also as a complement to
previous texts on combinatorial and graph algorithms [2–15, 17].
The book is conceived on the ground of first, introducing simple algorithms for
these problems in order to develop some intuition before moving on to more
complicated algorithms from the research literature and second, stimulating grad-
uate research on tree and graph algorithms by providing together with the under-
lying theory, a solid basis for experimentation and further development.
Algorithms are presented on an intuitive basis, followed by a detailed 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 along the book. These implementations include result checking of imple-
mentation correctness using correctness certificates.
The choice of LEDA, which is becoming a de-facto standard for graduate
courses on graph algorithms throughout the world is not casual, because it allows
the student, lecturer, researcher, and practitioner to complement algorithmic graph

ix
x Preface to the First Edition

theory with actual implementation 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 algorithms using
LEDA is given in an appendix. The interactive demonstration also includes visual
checkers of implementation correctness.
The book is divided into four parts. Part I has an introductory nature and consists
of two chapters. Chapter 1 includes a review of basic graph-theoretical notions and
results used along the book, a brief primer of literate programming, and an expo-
sition of the implementation correctness approach by result checking using cor-
rectness certificates. Chapter 2 is devoted exclusively to the fundamental
algorithmic techniques 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.
Part II also consists of two chapters. Chapter 3 addresses the most common
methods for traversing general, rooted trees: depth-first prefix leftmost (preorder),
depth-first prefix rightmost, depth-first postfix leftmost (postorder), depth-first
postfix rightmost, breadth-first leftmost (top-down), breadth-first rightmost, and
bottom-up traversal. Tree drawing is also discussed as an application of tree
traversal methods. Chapter 4 addresses several isomorphism problems on ordered
and unordered trees: tree isomorphism, subtree isomorphism, and maximum
common subtree isomorphism. Computational molecular biology is also discussed
as an application of the different isomorphism problems on trees.
Part III consists of three chapters. Chapter 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 left-
most (top-down) tree traversal. Leftmost 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.
Chapter 6 addresses the related problems of finding cliques, independent sets, and
vertex covers in trees and graphs. Multiple alignment of protein sequences in
computational molecular biology is also discussed as an application of clique
algorithms. Chapter 7 addresses several isomorphism problems on graphs: graph
isomorphism, graph automorphism, subgraph isomorphism, and maximal common
subgraph isomorphism. Chemical structure search is also discussed as an applica-
tion of the different graph isomorphism problems.
Part IV consists of two appendices, followed by bibliographic references and an
index. Appendix A gives an overview of LEDA, including a simple C++ repre-
sentation of trees as LEDA graphs, and a C++ implementation of radix sort using
LEDA. The interactive demonstration of graph algorithms presented along the book
is put together in Appendix B. Finally, Appendix C contains a complete index to all
program modules described in the book.
This book is suitable for use in upper undergraduate and graduate level courses
on algorithmic graph theory. This book can also be used as a supplementary text in
basic undergraduate and graduate level courses on algorithms and data structures,
Preface to the First Edition xi

and in computational molecular biology and computational chemistry courses as


well. Some basic knowledge of discrete mathematics, data structures, algorithms,
and programming at the undergraduate level is assumed.
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 José L. Bal-
cázar, Rafel Casas, Jordi Cortadella, Josep Daz, Conrado Martnez, Xavier Mes-
seguer, 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 Rosselló,
and Steven Skiena, for their standing support and encouragement, and Hans-Jörg
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 Straßer
of Springer-Verlag. Special thanks are debt 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 Jānis Bārzdiņš and Rūsiņš Freivalds, for hosting the sabbatical visit.

Barcelona, Spain Gabriel Valiente


July 2002

References

1. G. Ausiello, P. Crescenzi, G. Gambosi, V. Kahn, A. MarchettiSpaccamela, and M. Protasi.


Complexity and Approximation: Combinatorial Optimization Problems and their Approx-
imability Properties. Springer-Verlag, Berlin Heidelberg, 1999.
2. G. Chartrand and O. R. Oellermann. Applied and Algorithmic Graph Theory. McGraw-Hill,
New York, 1993.
3. N. Christofides. Graph Theory: An Algorithmic Approach. Academic Press, New York, 1975.
4. S. Even. GraphAlgorithms. Computer Science Press, Rockville MD,1979.
5. A. Gibbons. Algorithmic Graph Theory. Cambridge University Press, Cambridge, 1985.
6. M. C. Golumbic. Algorithmic Graph Theory and Perfect Graphs. Academic Press, New York,
1980.
7. D. L. Kreher and D. R. Stinson. Combinatorial Algorithms: Generation, Enumeration, and
Search. CRC Press, Boca Raton FL, 1999.
8. J. van Leeuwen. Graph algorithms. In Handbook of Theoretical Computer Science, volume A,
chapter 10, pages 525-631. Elsevier, Amsterdam, 1990.
9. J. A. McHugh. Algorithmic Graph Theory. Prentice Hall, Englewood Cliffs NJ, 1990.
10. K. Mehlhorn. Graph Algorithms and NP-Completeness, volume 2 of Data Structures and
Algorithms. Springer-Verlag, Berlin Heidelberg, 1984.
11. K. Mehlhorn and S. Naher. The LEDA Platform of Combinatorial and Geometric Computing.
Cambridge University Press, Cambridge, 1999.
xii Preface to the First Edition

12. C. H. Papadimitriou and K. Steiglitz. CombinatorialOptimization: Algorithms and Com-


plexity. Dover, Mineola, New York, 1998.
13. E. M. Reingold, 1. Nievergelt, and N. J. Deo. Combinatorial Algorithms: Theory and
Practice. Prentice Hall, Englewood Cliffs NJ, 1977.
14. S. Skiena. The Algorithm Design Manual. Springer-Verlag, Berlin Heidelberg, 1998.
15. R. E. Tarjan. Space-efficient Implementations of Graph Search Methods. ACM Transactions
on Mathematical Software. 1983 9(3), 326–339
16. V. V. Vazirani. Approximation Algorithms. Springer-Verlag, Berlin Heidelberg, 2001.
17. H. S. Wilf. Combinatorial Algorithms: An Update. SIAM, Philadelphia PA, 1989.
Contents

Part I Introduction
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Trees and Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Basic Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.3 Representation of Trees and Graphs . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2 Algorithmic Techniques . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 45
2.1 The Tree Edit Distance Problem . . . . . . . . . . . . . . . . . . . . . . . . 45
2.2 Backtracking . . . . . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 55
2.3 Branch-and-Bound . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 61
2.4 Divide-and-Conquer . . ........ . . . . . . . . . . . . . . . . . . . . . . . 64
2.5 Dynamic Programming ........ . . . . . . . . . . . . . . . . . . . . . . . 70
Summary . . . . . . . . . . . . . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 77
Bibliographic Notes . . . . . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 77
Problems . . . . . . . . . . . . . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 78
Exercises . . . . . . . . . . . . . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 79
References . . . . . . . . . . . . . . ........ . . . . . . . . . . . . . . . . . . . . . . . 80

Part II Algorithms on Trees


3 Tree Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.1 Preorder Traversal of a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.2 Postorder Traversal of a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.3 Top-Down Traversal of a Tree . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.4 Bottom-Up Traversal of a Tree . . . . . . . . . . . . . . . . . . . . . . . . . 97
3.5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

xiii
xiv Contents

Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
4 Tree Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
4.1 Tree Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
4.1.1 Ordered Tree Isomorphism . . . . . . . . . . . . . . . . . . . . . . 113
4.1.2 Unordered Tree Isomorphism . . . . . . . . . . . . . . . . . . . . 116
4.2 Subtree Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
4.2.1 Top-Down Subtree Isomorphism . . . . . . . . . . . . . . . . . . 125
4.2.2 Top-Down Unordered Subtree Isomorphism . . . . . . . . . 127
4.2.3 Bottom-Up Subtree Isomorphism . . . . . . . . . . . . . . . . . 135
4.2.4 Bottom-Up Unordered Subtree Isomorphism . . . . . . . . . 138
4.3 Maximum Common Subtree Isomorphism . . . . . . . . . . . . . . . . . 144
4.3.1 Top-Down Maximum Common Subtree
Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.3.2 Top-Down Unordered Maximum Common Subtree
Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.3.3 Bottom-Up Maximum Common Subtree
Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.3.4 Bottom-Up Unordered Maximum Common Subtree
Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
4.4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

Part III Algorithms on Graphs


5 Graph Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
5.1 Depth-First Traversal of a Graph . . . . . . . . . . . . . . . . . . . . . . . . 183
5.1.1 Leftmost Depth-First Traversal of a Graph . . . . . . . . . . . 190
5.2 Breadth-First Traversal of a Graph . . . . . . . . . . . . . . . . . . . . . . . 193
5.3 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Contents xv

6 Clique, Independent Set, and Vertex Cover . . . . . . . . . . . . . . . . . . . 219


6.1 Cliques, Maximal Cliques, and Maximum Cliques . . . . . . . . . . . 219
6.2 Maximal and Maximum Independent Sets . . . . . . . . . . . . . . . . . 234
6.3 Minimal and Minimum Vertex Covers . . . . . . . . . . . . . . . . . . . . 239
6.4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
7 Graph Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
7.1 Graph Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
7.1.1 An Algorithm for Graph Isomorphism . . . . . . . . . . . . . . 257
7.2 Graph Automorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
7.3 Subgraph Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
7.3.1 An Algorithm for Subgraph Isomorphism . . . . . . . . . . . 266
7.4 Maximal Common Subgraph Isomorphism . . . . . . . . . . . . . . . . . 270
7.4.1 An Algorithm for Maximal Common Subgraph
Isomorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
7.5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Bibliographic Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

A: Implementation of the Algorithms in Python . . . . . . . . . . . . . . . . . . . . 287


B: Solutions to All Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
C: Citing Publications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Part I
Introduction
Introduction
1

1.1 Trees and Graphs

The notion of graph which is most useful in computer science is that of a directed
graph or just a graph. A graph is a combinatorial structure consisting of a finite
nonempty set of objects, called vertices, together with a finite (possibly empty) set
of ordered pairs of vertices, called directed edges or arcs.

Definition 1.1 A graph G = (V, E) consists of a finite nonempty set V of vertices


and a finite set E ⊆ V × V of edges. The order of a graph G = (V, E), denoted
by n, is the number of vertices, n = |V | and the size, denoted by m, is the number
of edges, m = |E|. An edge e = (v, w) is said to be incident with vertices v and w,
where v is the source and w the target of edge e, and vertices v and w are said to be
adjacent. Edges (u, v) and (v, w) are said to be adjacent, as are edges (u, v) and
(w, v), and also edges (v, u) and (v, w).

Graphs are often drawn as a set of points in the plane and a set of arrows, each
of which joins two (not necessarily different) points. In a drawing of a graph G =
(V, E), each vertex v ∈ V is drawn as a point or a small circle and each edge
(v, w) ∈ E is drawn as an arrow from the point or circle of vertex v to the point or
circle corresponding to vertex w.

Example 1.1 The graph G = (V, E) of Fig. 1.1 has order 7 and size 12. The
vertex set is V = {v1 , . . . , v7 } and the edge set is E = {e1 , . . . , e12 }, where e1 =
(v1 , v2 ), e2 = (v1 , v4 ), e3 = (v2 , v5 ), e4 = (v3 , v1 ), e5 = (v4 , v2 ), e6 = (v4 , v3 ),
e7 = (v4 , v6 ), e8 = (v4 , v7 ), e9 = (v5 , v4 ), e10 = (v5 , v7 ), e11 = (v6 , v3 ), and
e12 = (v7 , v6 ).

© The Author(s), under exclusive license to Springer Nature Switzerland AG 2021 3


G. Valiente, Algorithms on Trees and Graphs, Texts in Computer Science,
https://doi.org/10.1007/978-3-030-81885-2_1
4 1 Introduction

Fig. 1.1 A graph of order 7 1


1 2
and size 12
4 3
2 5
6 9
3 4 5

7 8
11 10

6 7
12

A vertex has two degrees in a graph, one given by the number of edges coming
into the vertex and the other given by the number of edges in the graph going out of
the vertex.

Definition 1.2 The indegree of a vertex v in a graph G = (V, E) is the number


of edges in G whose target is v, that is, indeg(v) = |{(u, v) | (u, v) ∈ E}|. The
outdegree of a vertex v in a graph G = (V, E) is the number of edges in G whose
source is v, that is, outdeg(v) = |{(v, w) | (v, w) ∈ E}|. The degree of a vertex v in
a graph G = (V, E) is the sum of the indegree and the outdegree of the vertex, that
is, deg(v) = indeg(v) + outdeg(v).

Example 1.2 The degree of the vertices in the graph of Fig. 1.1 is the following:

Vertex v1 v2 v3 v4 v5 v6 v7 sum
Indegree 1 2 2 2 1 2 2 12
Outdegree 2 1 1 4 2 1 1 12

As can be seen in Example 1.2, the sum of the indegrees and the sum of the
outdegrees of the vertices of the graph in Fig. 1.1 are both equal to 12, the number
of edges of the graph. As a matter of fact, there is a basic relationship between the
size of a graph and the degrees of its vertices, which will prove to be very useful in
analyzing the computational complexity of algorithms on graphs.

Theorem 1.1 Let G = (V, E) be a graph with n vertices and m edges, and let
V = {v1 , . . . , vn }. Then,

n 
n
indeg(vi ) = outdeg(vi ) = m .
i=1 i=1

Proof Let G = (V, E) be a graph. Every edge (vi , v j ) ∈ E contributes one to


indeg(v j ) and one to outdeg(vi ). 
1.1 Trees and Graphs 5

1 2 1 2 1 2

3 4 5 3 4 5 3 4 5

6 7 6 7 6 7

Fig. 1.2 A walk [v1 , v4 , v6 , v3 , v1 , v4 , v2 ], a trail [v1 , v4 , v2 , v5 , v4 , v6 , v3 ], and a path [v1 , v4 , v2 ,


v5 , v7 , v6 , v3 ] in the graph of Fig. 1.1

Walks, trails, and paths in a graph are alternating sequences of vertices and edges
in the graph such that each edge in the sequence is preceded by its source vertex and
followed by its target vertex. Trails are walks having no repeated edges, and paths
are trails having no repeated vertices.

Definition 1.3 A walk from vertex vi to vertex v j in a graph is an alternating


sequence
[vi , ei+1 , vi+1 , ei+2 , . . . , v j−1 , e j , v j ]
of vertices and edges in the graph, such that ek = (vk−1 , vk ) for k = i + 1, . . . , j. A
trail is a walk with no repeated edges, and a path is a trail with no repeated vertices
(except, possibly, the initial and final vertices). The length of a walk, trail, or path is
the number of edges in the sequence.

Since an edge in a graph is uniquely determined by its source and target vertices,
a walk, trail, or path can be abbreviated by just enumerating either the vertices
[vi , vi+1 , . . . , v j−1 , v j ] or the edges [ei+1 , ei+2 , . . . , e j ] in the alternating sequence
[vi , ei+1 , vi+1 , ei+2 , . . . , v j−1 , e j , v j ] of vertices and edges.

Example 1.3 The vertex sequence [v1 , v4 , v6 , v3 , v1 , v4 , v2 ] in Fig. 1.2 is a walk of


length 6 which is not a trail, [v1 , v4 , v2 , v5 , v4 , v6 , v3 ] is a trail of length 6 which is
not a path, and [v1 , v4 , v2 , v5 , v7 , v6 , v3 ] is a path of length 6.

Walks are closed if their initial and final vertices coincide.

Definition 1.4 A walk, trail, or path [vi , ei+1 , vi+1 , ei+2 , . . . , v j−1 , e j , v j ] is said
to be closed if vi = v j . A cycle is a closed path of length at least one.

Example 1.4 The vertex sequence [v1 , v4 , v6 , v3 , v1 ] in Fig. 1.3 is a cycle of


length 4.

The combinatorial structure of a graph encompasses two notions of the substruc-


ture. A subgraph of a graph is just a graph whose vertex and edge sets are contained
in the vertex and edge sets of the given graph, respectively. The subgraph of a graph
6 1 Introduction

1 2

3 4 5

6 7

Fig. 1.3 A cycle [v1 , v4 , v6 , v3 , v1 ] in the graph of Fig. 1.1

1 2 1 2

3 4 5 3 4 5

6 7 6 7

Fig. 1.4 A subgraph and an induced subgraph of the graph of Fig. 1.1

induced by a subset of its vertices has as edges the set of edges in the given graph
whose source and target belong to the subset of vertices.

Definition 1.5 Let G = (V, E) be a graph, and let W ⊆ V . A graph (W, S) is a


subgraph of G if S ⊆ E. The subgraph of G induced by W is the graph (W, E ∩
W × W ).

Example 1.5 The subgraph with vertex set {v1 , v2 , v4 , v6 , v7 } and edge set {(v1 , v2 ),
(v1 , v4 ), (v4 , v6 ), (v7 , v6 )} shown in Fig. 1.4 is not an induced subgraph. The
subgraph induced by {v1 , v2 , v4 , v6 , v7 } has edge set {(v1 , v2 ), (v1 , v4 ), (v4 , v2 ),
(v4 , v6 ), (v4 , v7 ), (v7 , v6 )}.

Undirected Graphs

The notion of graph which is most often found in mathematics is that of an undirected
graph. Unlike the directed edges or edges of a graph, edges of an undirected graph
have no direction association with them and therefore, no distinction is made between
the source and target vertices of an edge. In a mathematical sense, an undirected graph
consists of a set of vertices and a finite set of undirected edges, where each edge has
a set of one or two vertices associated with it. In the computer science view of
undirected graphs, though, an undirected graph is the particular case of a directed
graph in which for every edge (v, w) of the graph, the reversed edge (w, v) also
belongs to the graph. Undirected graphs are also called bidirected.

Definition 1.6 A graph G = (V, E) is undirected if (v, w) ∈ E implies (w, v) ∈ E,


for all v, w ∈ V .
1.1 Trees and Graphs 7

1 2 1 2

3 4 5 3 4 5

6 7 6 7

Fig. 1.5 The undirected graph underlying the graph of Fig. 1.1. The standard presentation is given
in the drawing to the left, while the understanding of undirected edges as pairs of counter-parallel
edges is emphasized in the drawing to the right

Undirected graphs are often drawn as a set of points in the plane and a set of line
segments, each of which joins two (not necessarily different) points. In a drawing of
an undirected graph G = (V, E), each vertex v ∈ V is drawn as a point or a small
circle and each pair of counter-parallel edges (v, w), (w, v) ∈ E is drawn as a line
segment between the points or circles corresponding to vertices v and w.

Example 1.6 The undirected graph underlying the graph of Fig. 1.1 is shown in
Fig. 1.5.

The terminology of directed graphs carries over to undirected graphs, although a


few differences deserve mention. First of all, since an edge in an undirected graph is
understood as a pair of counter-parallel edges in the corresponding bidirected graph,
the number of edges coming into a given vertex and the number of edges going out
of the vertex coincide and therefore, no distinction is made between the indegree and
the outdegree of a vertex. That is, the degree of a vertex in an undirected graph is
equal to the indegree and the outdegree of the vertex in the corresponding bidirected
graph. For the same reason, the size of an undirected graph is half the size of the
corresponding bidirected graph.

Definition 1.7 The degree of a vertex v in an undirected graph G = (V, E) is the


number of edges in G that are incident with v. The degree sequence of G is the
sequence of n non-negative integers obtained by arranging the vertex degrees in
non-decreasing order.

Example 1.7 There are three vertices of degree 2 in the graph of Fig. 1.6, two
vertices of degree 3, two vertices of degree 4, and two vertices of degree 5. The
degree sequence of the graph is [2, 2, 2, 3, 3, 4, 4, 5, 5].

A walk, trail, or path in an undirected graph is a walk, trail, or path, respectively,


in the corresponding bidirected graph. Another important graph-theoretical notion
is that of connected and disconnected graphs. An undirected graph is connected if
every pair of its vertices is joined by some walk, and an undirected graph that is
not connected is said to be disconnected. On the other hand, a graph is connected if
for all vertices v and w in the graph, there is a walk from v to w, and it is strongly
8 1 Introduction

Fig. 1.6 Undirected graph


with degree sequence
[2, 2, 2, 3, 3, 4, 4, 5, 5]

Fig. 1.7 A graph with five


strong components whose
underlying undirected graph
is connected

connected if there are walks from v to w and from w back to v, for all vertices v and
w in the graph.

Definition 1.8 An undirected graph G = (V, E) is connected if for every pair of


vertices v, w ∈ E, there is a walk between v and w. A graph is connected if the
underlying undirected graph is connected. Graph G is strongly connected if for
every pair of vertices v, w ∈ E, there are walks from v to w and from w to v.

Connectivity is an equivalence relation on the vertex set of a graph, which induces a


partition of the graph into subgraphs induced by connected vertices, called connected
components or just components.

Definition 1.9 A component of an undirected graph G is a connected subgraph of


G which is not properly contained in any other connected subgraph of G. A strong
component of a graph G is a strongly connected subgraph of G which is not properly
contained in any other strongly connected subgraph of G.

Example 1.8 The graph of Fig. 1.7 has five strong components, induced, respec-
tively, by the vertex sets {v1 , v4 , v5 , v8 }, {v2 }, {v3 }, {v6 , v7 , v10 }, and {v9 }. The under-
lying undirected graph is, however, connected.

Some common families of undirected graphs are the trees, the complete graphs,
the path graphs, the cycle graphs, the wheel graphs, the bipartite graphs, and the
regular graphs. A tree is a connected graph having no cycles.

Definition 1.10 An undirected graph G = (V, E) is said to be an undirected tree


if it is connected and has no cycles.

Example 1.9 The undirected trees with one, two, three, four, five, and six vertices
are shown in Fig. 1.8.
1.1 Trees and Graphs 9

Fig. 1.8 The first fourteen undirected trees

Fig. 1.9 The first six complete graphs

In a complete graph, every pair of distinct vertices is joined by an edge.

Definition 1.11 An undirected graph G = (V, E) is said to be a complete graph


if for all v, w ∈ V with v = w, (v, w) ∈ E. The complete graph on n vertices is
denoted by K n .

Example 1.10 The complete graphs on one, two, three, four, five, and six vertices
are shown in Fig. 1.9.

A path graph can be drawn such that all vertices lie on a straight line.

Definition 1.12 A connected undirected graph G = (V, E) with n = m + 1 is said


to be a path graph if it can be drawn such that all vertices lie on a straight line. The
path graph on n vertices is denoted by Pn .
10 1 Introduction

Fig. 1.10 The first six path graphs

Fig. 1.11 The first six cycle graphs

Example 1.11 The path graphs on one, two, three, four, five, and six vertices are
shown in Fig. 1.10.

A cycle graph can be drawn such that all vertices lie on a circle.

Definition 1.13 A connected undirected graph G = (V, E) with n = m is said to


be a cycle graph if it can be drawn such that all vertices lie on a circle. The cycle
graph on n vertices is denoted by Cn .

Example 1.12 The cycle graphs on one, two, three, four, five, and six vertices are
shown in Fig. 1.11.

A wheel graph has a distinguished (inner) vertex that is connected to all other
(outer) vertices, and it can be drawn such that all outer vertices lie on a circle centered
at the inner vertex.

Definition 1.14 A connected undirected graph G = (V, E) is said to be a wheel


graph if it can be drawn such that all but a single vertex lie on a circle centered at
the single vertex, which is connected to all other vertices. The wheel graph with n
outer vertices is denoted by Wn+1 .

Example 1.13 The wheel graphs with one, two, three, four, five, and six outer ver-
tices are shown in Fig. 1.12.

Another common family of undirected graphs is the bipartite graphs. The vertex
set of a bipartite graph can be partitioned into two subsets in such a way that every
1.1 Trees and Graphs 11

Fig. 1.12 The first six wheel graphs

Fig. 1.13 The first twelve complete bipartite graphs

edge of the graph joins a vertex of one subset with a vertex of the other subset. In
a complete bipartite graph, every vertex of one subset is joined by some edge with
every vertex of the other subset.

Definition 1.15 An undirected graph G = (V, E) is said to be a bipartite graph if


V can be partitioned into two disjoint subsets X and Y such that for all (x, y) ∈ E,
either x ∈ X and y ∈ Y , or x ∈ Y and y ∈ X , and it is said to be a complete
bipartite graph if, furthermore, (x, y), (y, x) ∈ E for all x ∈ X and y ∈ Y . The
complete bipartite graph on p + q vertices, where subset X has p vertices and subset
Y has q vertices, is denoted by K p,q .

Example 1.14 The complete bipartite graphs with two, three, four, five, six, and
seven vertices are shown in Fig. 1.13. The bipartite sets are distinguished by drawing
vertices either in red or in black. Then, every edge joins a red vertex with a black
vertex.
12 1 Introduction

Fig. 1.14 Maximal (left) and maximum (right) matching in a bipartite graph. Matchings are
shown highlighted. The matching {(x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y5 )} is not maximal, because
it is included in matching {(x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y5 ), (x5 , y6 )}, which is maximal but not
maximum. Matching {(x1 , y1 ), (x2 , y6 ), (x3 , y3 ), (x4 , y5 ), (x5 , y4 ), (x6 , y2 )} is maximal and also
a maximum matching

Fig. 1.15 Maximum cardinality (left) and maximum weight (right) matching in a bipartite graph
with weighted edges. Matchings are shown highlighted

Definition 1.16 Let G = (X ∪ Y, E) be a bipartite graph with E ⊆ X × Y and


X ∩ Y = ∅. A matching in G is a set of edges M ⊆ E such that x1 = x2 and
y1 = y2 for all distinct edges (x1 , y1 ), (x2 , y2 ) ∈ M. A matching M in a bipartite
graph G is maximal if there is no matching M  in G such that M ⊆ M  and M = M  ,
and it is maximum if there is no matching in G with more edges than M. Further, a
matching M in a bipartite graph G is perfect if |M| = min(|X |, |Y |).

Example 1.15 There are 577 non-trivial matchings in the bipartite graph of Fig. 1.14,
69 of which are maximal. The smallest one has four edges, and there are 22
of them, such as {(x1 , y1 ), (x2 , y2 ), (x4 , y3 ), (x5 , y5 )}. There are also 44 maximal
matchings with five edges, such as {(x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y5 ), (x5 , y6 )},
and three maximal matchings with six edges, which are also maximum matchings:
{(x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y6 ), (x5 , y4 ), (x6 , y5 )}, {(x1 , y1 ), (x2 , y6 ), (x3 , y2 ),
(x4 , y3 ), (x5 , y4 ), (x6 , y5 )}, which is shown to the right of the figure, and {(x1 , y1 ),
(x2 , y6 ), (x3 , y3 ), (x4 , y5 ), (x5 , y4 ), (x6 , y2 )}.

A maximum matching in a bipartite graph is also called a maximum cardinal-


ity bipartite matching, in order to distinguish it from a maximum weight bipartite
matching in a bipartite graph with weighted edges, where the weight of a matching
is the total weight of the edges in the matching.
1.1 Trees and Graphs 13

Fig. 1.16 Regular graphs G 1 and G 2 of same order and size

Example 1.16 In the bipartite graph of Fig. 1.15, {(x1 , y2 ), (x2 , y3 ), (x3 , y4 ),
(x4 , y1 )} is a maximum cardinality matching, with total edge weight 6, and {(x2 , y1 ),
(x3 , y2 ), (x4 , y4 )} is a maximum weight matching of weight 16.

A natural generalization of bipartite graphs is the k-partite graphs. The vertex set
of a k-partite graph can be partitioned into k subsets in such a way that every edge
of the graph joins a vertex of one subset with a vertex of another subset.

Definition 1.17 An undirected graph G = (V, E) is said to be a k-partite graph if


V can be partitioned into k  2 subsets V1 , V2 , . . . , Vk such that for all (v, w) ∈ E
with v ∈ Vi and w ∈ V j , it holds that i = j.

Complete graphs and cycle graphs are trivial examples of regular graphs. In a
regular graph, all vertices have the same degree.

Definition 1.18 An undirected graph G = (V, E) is said to be a regular graph if


deg(v) = deg(w) for all v, w ∈ V .

Example 1.17 The regular graphs G 1 and G 2 shown in Fig. 1.16 share the same
degree sequence, [2, 2, 2, 2, 2, 2].

Labeled Graphs

Most applications of graphs in computer science involve labeled graphs, where


vertices and edges have additional attributes such as color or weight. For a labeled
graph G = (V, E), the label of a vertex v ∈ V is denoted by label[v], and the label
of an edge (v, w) = e ∈ E is denoted by label[e] or just by label[v, w].

Example 1.18 An undirected graph of the geographical adjacencies between some


of the first European states to adopt the European currency is shown in Fig. 1.17.
Vertices represent European states and are labeled with a standard abbreviation for
the name of the state. There is an edge between two vertices if the corresponding
states share a border. Edges are labeled with the distance in kilometers between the
capital cities of the states they join.
14 1 Introduction

Fig. 1.17 A labeled undirected graph of geographical adjacencies between some European states

Fig. 1.18 An ordered undirected graph. The relative order of the vertices adjacent with a given
vertex reflects the counter-clockwise ordering of the outgoing edges of the vertex in the drawing

Ordered Graphs

An ordered graph is a graph in which the relative order of the adjacent vertices is
fixed for each vertex. Ordered graphs arise when a graph is drawn or embedded on
a certain surface, for instance, in the Euclidean plane.

Example 1.19 The relative order of the vertices adjacent with each of the vertices
of the ordered undirected graph shown in Fig. 1.18 reflects the counter-clockwise
ordering of the outgoing edges of the vertex in the drawing. For instance, the the
ordered sequence of vertices [v1 , v8 , v7 , v3 ] adjacent with vertex v2 reflects the
counter-clockwise ordering {v2 , v1 }, {v2 , v8 }, {v2 , v7 }, {v2 , v3 } of the edges incident
with vertex v2 in the drawing of the graph.

Trees

The families of undirected graphs introduced above have a directed graph counter-
part. In particular, while the notion of tree most often found in discrete mathematics
is that of an undirected tree, the notion of the tree which is most useful in computer
science is that of the rooted directed tree or just tree. A tree is the particular case of
a graph in which there is a distinguished vertex, called the root of the tree, such that
there is a unique walk from the root to any vertex of the tree. The vertices of a tree
are called nodes.
1.1 Trees and Graphs 15

Fig. 1.19 Graph T1 is not a rooted tree, although it is connected and has no cycles. Graph T2 is a
tree rooted at node v1

Definition 1.19 A connected graph G = (V, E) is said to be a tree if the underlying


undirected graph has no cycles and there is a distinguished node r ∈ V , called the
root of the tree and denoted by root[T ] such that for all nodes v ∈ V , there is a path
in G from the root r to node v.

Example 1.20 Two connected graphs are shown in Fig. 1.19. Graph T1 is not a tree
rooted at node u 1 , although the underlying undirected graph has no cycles. As a
matter of fact, there is no path in T1 from node u 1 to any of nodes u 7 , u 8 , and u 9 .
Graph T2 , on the other hand, is indeed a tree rooted at node v1 .

The existence of a unique path in a tree from the root to any other node imposes a
hierarchical structure in the tree. The root lies at the top, and nodes can be partitioned
in hierarchical levels according to their distance from the root of the tree.

Definition 1.20 Let T = (V, E) be a tree. The depth of node v ∈ V , denoted by


depth[v], is the length of the unique path from the root node root[T ] to node v, for all
nodes v ∈ V . The depth of T is the maximum among the depth of all nodes v ∈ V .

The hierarchical structure embodied in a tree leads to further distinguishing among


the nodes of the tree: parent nodes are joined by edges to their children nodes, nodes
sharing the same parent are called sibling, and nodes having no children are called
leaves.

Definition 1.21 Let T = (V, E) be a tree. Node v ∈ V is said to be the parent of


node w ∈ V , denoted by parent[w], if (v, w) ∈ E and, in such a case, node w is said
to be a child of node v. The children of node v ∈ V are the set of nodes W ⊆ V such
that (v, w) ∈ E for all w ∈ W . A node having no children is said to be a leaf node.
Non-root nodes v, w ∈ V are said to be sibling nodes if parent[v] = parent[w]. The
number of children of node v ∈ V is denoted by children[v]. The tree is said to be a
binary tree if |children[v]|  2 for every node v ∈ V , and a complete binary tree
if, furthermore, it has n/2 non-leaf nodes, where n = |V |, and at most one node
v ∈ V with |children[v]| = 1, whose only child is a leaf node.
16 1 Introduction

Example 1.21 In tree T2 of Fig. 1.19, node v2 is the parent of node v5 , and v8 , v9
are sibling nodes, because they are both children of node v7 . The root of T2 is node
v1 , and the leaves are v3 , v4 , v5 , v8 , v9 , v10 , v12 , v13 . Nodes v2 , v6 , v7 , v11 are neither
root nor leaf nodes.

The reader may prefer to consider the hierarchical structure of a tree the other way
around, though. The leaves constitute the first level of height zero, their parents form
the second level of height one, and so on. In other words, nodes can be partitioned
in levels according to their height.

Definition 1.22 Let T = (V, E) be a tree. The height of node v ∈ V , denoted by


height[v], is the length of a longest path from node v to any node in the subtree of T
rooted at node v, for all nodes v ∈ V . The height of T is the maximum among the
height of all nodes v ∈ V .

It follows from Definition 1.20 that in a tree T = (V, E), depth[v]  0 for all
nodes v ∈ V , and that depth[v] = 0 if and only if v = root[T ]. Further, it follows
from Definition 1.22 that height[v]  0 for all nodes v ∈ V , and that height[v] = 0
if and only if v is a leaf node.

Example 1.22 In the tree shown twice in Fig. 1.20, node v1 is the root and has
depth zero and height three, which is also the height of the tree. Node v2 has depth
and height one; nodes v3 , v4 , v5 , v10 have depth two and height zero; node v6 has
depth one and height two; nodes v7 , v11 have depth two and height one; and nodes
v8 , v9 , v12 , v13 have depth three, which is also the depth of the tree, and height zero,
which is the height of all the leaves in the tree.

Trees are, indeed, the least connected graphs. The number of edges in a tree is
one less than the number of nodes in the tree.

Theorem 1.2 Let T = (V, E) be a tree. Then m = n − 1.

Fig. 1.20 The tree of Fig. 1.19 is shown twice, emphasizing the depth (left) and the height (right)
of all nodes in the tree
1.1 Trees and Graphs 17

Proof By induction on the number of nodes n in the tree. For n = 1, a tree with one
node has no edges, that is, m = 0. Assume, then, that every tree with n  1 nodes
has n − 1 edges.
Let T = (V, E) be a tree with n + 1 nodes, and let v ∈ V be any leaf node of T .
The subgraph of T induced by V \ {v} is a tree with n nodes and has, by induction
hypothesis, n − 1 edges. Now, since there is only one edge (w, v) ∈ E (namely, with
w the parent in T of node v), it follows that the subgraph of T induced by V \ {v}
has one edge less than T . Therefore, T has n + 1 nodes and n edges. 

As with graphs, there is also a basic relationship between the number of nodes in
a tree and the number of children of the nodes of the tree, which will prove to be
very useful in analyzing the computational complexity of algorithms on trees.

Lemma 1.1 Let T = (V, E) be a tree on n nodes, and let V = {v1 , . . . , vn }. Then,
n
children[vi ] = n − 1 .
i=1

Proof Let T = (V, E) be a tree. Since every non-root node w ∈ V is the target
n
of
n a different edge (v, w) ∈ E, it holds by Theorem
n 1.1 that i=1 children[vi ] =
i=1 outdeg(vi ) = m and then, by Theorem 1.2, i=1 children[vi ] = n − 1. 

Now, given that trees are a particular case of graphs, it is natural to consider trees
as subgraphs of a given graph and, as a matter of fact, those trees that span all the
vertices of a graph arise in several graph algorithms. A spanning tree of a graph is a
subgraph that contains all the vertices of the graph and is a tree.

Definition 1.23 Let G = (V, E) be a graph. A subgraph (W, S) of G is a spanning


tree of graph G if W = V and (W, S) is a tree.

Example
6 1.23 The graph of Fig. 1.21 has n = 4 vertices and m = 6 edges, and thus
3 = 20 subgraphs with 4 vertices and 3 edges. However, only 6 of these subgraphs
are trees. These spanning trees of the graph are shown highlighted.

Fig. 1.21 Spanning trees of a graph


18 1 Introduction

Fig. 1.22 Spanning forests of a graph

Fig. 1.23 An ordered tree.


The relative order of the
children of a given node
reflects the
counter-clockwise ordering
of the outgoing edges of the
node in the drawing

Not every graph has a spanning tree, though, but every graph has a spanning
forest, that is, an ordered set of pairwise-disjoint subgraphs that are trees and which,
together, span all the vertices of the graph.

Definition 1.24 Let G = (V, E) be a graph. A sequence [(W1 , S1 ), . . . , (Wk , Sk )]


of k  1 subgraphs of G is a spanning forest of graph G if W1 ∪ · · · ∪ Wk = V ;
Wi ∩ W j = ∅ for all 1  i, j  k with i = j; and (Wi , Si ) is a tree, for all 1  i  k.

Example 1.24 The graph of Fig. 1.22 has no spanning tree, but there are 12 forests
that span all the vertices of the graph. These spanning forests of the graph are shown
highlighted, up to a permutation of the sequence of trees in a forest. Consider, for
instance, trees T1 = ({v1 }, ∅) and T2 = ({v2 , v3 , v4 }, {(v2 , v3 ), (v2 , v4 )}). Spanning
forests [T1 , T2 ] and [T2 , T1 ] are both highlighted together, at the left of the top row.

Ordered Trees

An ordered tree is a tree in which the relative order of the children is fixed for each
node. As a particular case of ordered graphs, ordered trees arise when a tree is drawn
or embedded in the Euclidean plane.

Example 1.25 In the ordered tree shown in Fig. 1.23, the children of node v1
are [v2 , v6 ]; the children of node v2 are [v3 , v4 , v5 ]; the children of node v6 are
1.1 Trees and Graphs 19

[v7 , v10 , v11 ]; the children of node v7 are [v8 , v9 ]; and the children of node v11 are
[v12 , v13 ]. The remaining nodes have no children. The relative order of the children
of each of the nodes reflects the counter-clockwise ordering of the outgoing edges of
the node in the drawing. For instance, the ordered sequence [v7 , v10 , v11 ] of the chil-
dren of node v6 reflects the counter-clockwise ordering (v6 , v7 ), (v6 , v10 ), (v6 , v11 )
of the edges going out of node v6 in the drawing of the ordered tree.

The relative order of children nodes leads to further distinguishing among the
nodes of an ordered tree: children nodes are ordered from the first up to the last,
non-first children nodes have a previous sibling, and non-last children nodes have a
next sibling. For sibling nodes v and w, let v < w denote that v precedes w in the
ordered tree.

Definition 1.25 Let T = (V, E) be an ordered tree, and let (v, w) ∈ E. Node w ∈ V
is said to be the first child of node v ∈ V , denoted by first[v], if there is no node
z ∈ V such that (v, z) ∈ E and z < w. Node w ∈ V is said to be the last child
of node v ∈ V , denoted by last[v], if there is no node z ∈ V such that (v, z) ∈ E
and w < z. Node z ∈ V is said to be the next sibling of node w ∈ V , denoted by
next[w], if (v, z) ∈ E, w < z, and there is no node x ∈ V such that (v, x) ∈ E and
w < x < z. Node w ∈ V is said to be the previous sibling of node z ∈ V , denoted
by previous[z], if next[w] = z.

Example 1.26 In the ordered tree shown in Fig. 1.23, first[v6 ] = v7 , last[v6 ] = v11 ,
and next[v7 ] = v10 = previous[v11 ].

1.2 Basic Data Structures

Some of the basic data structures needed for the description of algorithms on trees and
graphs are illustrated below using a fragment of pseudocode. Pseudocode conven-
tions follow modern programming guidelines, such as avoiding global side effects
(with the only exception of object attributes, which are hidden behind dot-notation)
and the unconditional transfer of control by way of goto or gosub statements.

• Assignment of value a to variable x is denoted by x ← a.


• Comparison of equality between the values of two variables x and y is denoted
by x = y; comparison of strict inequality is denoted by either x = y, x < y,
or x > y; and comparison of non-strict inequality is denoted by either x  y or
x  y.
• Logical true and false are denoted by true and false, respectively.
• Logical negation, conjunction, and disjunction are denoted by not, and, and or,
respectively.
• Non-existence is denoted by nil.
20 1 Introduction

• Mathematical notation is preferred over programming notation. For example, the


cardinality of a set S is denoted by |S|, membership of an element x in a set S is
denoted by x ∈ S, insertion of an element x into a set S is denoted by S ← S ∪{x},
and deletion of an element x from a set S is denoted by S ← S \ {x}.
• Control structures use the following reserved words: all, break, do, else, for, if,
repeat, return, then, to, until, and while.
• Blocks of statements are shown by means of indention.

if … then for all … do while … do repeat


… … … …
else … if … then …
… return … … until …

The collection of abstract operations on arrays, matrices, lists, stacks, queues,


priority queues, sets, and dictionaries is presented next by way of examples.

Arrays

An array is a one-dimensional array indexed by non-negative integers. The [i]


operation returns the i-th element of the array, assuming there is such an element.

let A[1..n] be a new array


for i = 1 to n do
A[i] ← false

Matrices

A matrix is a two-dimensional array indexed by non-negative integers. The [i, j]


operation returns the element in the i-th row and the j-th column of the matrix,
assuming there is such an element.

let M[1..m][1..n] be a new matrix


for i = 1 to m do
for j = 1 to n do
A[i, j] ← 0
1.2 Basic Data Structures 21

Lists

A list is just a sequence of elements. The front operation returns the first element
and the back operation returns the last element in the list, assuming the list is not
empty. The prev operation returns the element before a given element in the list,
assuming the given element is not at the front of the list. The next operation returns
the element after a given element in the list, assuming the given element is not at
the back of the list. The append operation inserts an element at the rear of the list.
The concatenate operation deletes the elements of another list and inserts them at
the rear of the list.

let L be an empty list


append x to L L .append(x)
let L  be an empty list
append x to L 
concatenate L  to L L .concatenate(L  )
let x be the element at the front of L x ← L .front()
while x = nil do
output x
x ← L .next(x)
let x be the element at the back of L x ← L .back()
while x = nil do
output x
x ← L .prev(x)

Stacks

A stack is a sequence of elements that are inserted and deleted at the same end (the
top) of the sequence. The top operation returns the top element in the stack, assuming
the stack is not empty. The pop operation deletes and returns the top element in the
stack, also assuming the stack is not empty. The push operation inserts an element
at the top of the stack.

let S be an empty stack


push x onto S S.push(x)
let x be the element at the top of S x ← S.top()
output x
while S is not empty do ¬S.empty()
pop from S the top element x x ← S.pop()
output x
22 1 Introduction

Queues

A queue is a sequence of elements that are inserted at one end (the rear) and deleted
at the other end (the front) of the sequence. The front operation returns the front
element in the queue, assuming the queue is not empty. The dequeue operation
deletes and returns the front element in the queue, assuming the queue is not empty.
The enqueue operation inserts an element at the rear end of the queue.

let Q be an empty queue


enqueue x into Q Q.enqueue(x)
let x be the element at the front of Q x ← Q.front()
output x
repeat
dequeue from Q the front element x x ← Q.dequeue()
output x
until Q is empty Q.empty()

Priority Queues

A priority queue is a queue of elements with both an information and a priority


associated with each element, where there is a linear order defined on the priorities.
The front operation returns an element with the minimum priority, assuming the
priority queue is not empty. The dequeue operation deletes and returns an element
with the minimum priority in the queue, assuming the priority queue is not empty.
The enqueue operation inserts an element with a given priority in the priority queue.

let Q be an empty priority queue


enqueue (x, y) into Q Q.enqueue(x, y)
let (x, y) be an element x
with the minimum priority y in Q (x, y) ← Q.front()
output (x, y)
repeat
dequeue from Q an element x
with the minimum priority y (x, y) ← Q.dequeue()
output (x, y)
until Q is empty Q.empty()
1.2 Basic Data Structures 23

Sets

A set is just a set of elements. The insert operation inserts an element in the set. The
delete operation deletes an element from the set. The member operation returns true
if an element belongs to the set and false otherwise.

let S be an empty set


S = S ∪ {x} S.insert(x)
for all x ∈ S do S.member(x)
output x
delete x from S S.delete(x)

Dictionaries

A dictionary is an associative container, consisting of a set of elements with both an


information and a unique key associated with each element, where there is a linear
order defined on the keys and the information associated with an element is retrieved
on the basis of its key. The member operation returns true if there is an element
with a given key in the dictionary, and false otherwise. The lookup operation returns
the element with a given key in the dictionary, or nil if there is no such element.
The insert operation inserts and returns an element with a given key and given
information in the dictionary, replacing the element (if any) with the given key. The
delete operation deletes the element with a given key from the dictionary if there is
such an element.

let D be an empty dictionary


D[x] ← y D.insert(x, y)
for all x ∈ D do D.member(x)
y ← D[x] y ← D.lookup(x)
output (x, y)
delete x from D D.delete(x)

1.3 Representation of Trees and Graphs

There are several different ways in which graphs and, in particular, trees can be
represented in a computer, and the choice of a data structure often depends on the
efficiency with which the operations that access and update the data need to be
supported. As a matter of fact, there is often a tradeoff between the space complexity
of a data structure and the time complexity of the operations.
24 1 Introduction

Representation of Graphs

A graph representation consists of a collection of abstract operations, a concrete


representation of graphs by appropriate data structures, and an implementation of
the abstract operations using the concrete data structures.
The choice of abstract operations to be included in a graph representation depends
on the particular application area. For instance, the representation of graphs in the
LEDA library of efficient data structures and algorithms [43] supports about 120 dif-
ferent operations, roughly half of which address the needs of computational geom-
etry algorithms, and the representation of graphs in the BGL library of graph algo-
rithms [55] supports about 50 different operations. The following collection of 32
abstract operations, though, covers the needs of most of the algorithms on graphs
presented in this book.

• G.vertices() and G.edges() give, respectively, a list of the vertices and a list of the
edges of graph G in the order fixed by the representation of G.
• G.incoming(v) and G.outgoing(v) give a list of the edges of graph G coming into
and going out of vertex v, respectively, in the order fixed by the representation
of G.
• G.number_of_vertices() and G.number_of_edges() give, respectively, the order n
and the size m of graph G.
• G.indeg(v) and G.outdeg(v) give, respectively, the number of edges coming into
and going out of vertex v in graph G.
• G.adjacent(v, w) is true if there is an edge in graph G going out of vertex v and
coming into vertex w, and false otherwise.
• G.source(e) and G.target(e) give, respectively, the source and the target vertex of
edge e in graph G.
• G.opposite(v, e) gives G.target(e) if vertex v is the source of edge e in graph G,
and G.source(e) otherwise.
• G.first_vertex() and G.last_vertex() give, respectively, the first and the last vertex
in the representation of graph G. Further, G.pred_vertex(v) and
G.succ_vertex(v) give, respectively, the predecessor and the successor of vertex
v in the representation of graph G. These operations support iteration over the
vertices of the graph.
• G.first_edge() and G.last_edge() give, respectively, the first and the last edge in
the representation of graph G. Further, G.pred_edge(e) and G.succ_edge(e) give,
respectively, the predecessor and the successor of edge e in the representation of
graph G. These operations support iteration over the edges of the graph.
• G.first_in_edge(v) and G.last_in_edge(v) give, respectively, the first and the last
edge in the representation of graph G coming into vertex v. Further, G.in_pred(e)
and G.in_succ(e) give, respectively, the previous and the next edge after e in
the representation of graph G coming into vertex G.target(e). These operations
support iteration over the vertices of the graph adjacent with a given vertex.
1.3 Representation of Trees and Graphs 25

• G.first_adj_edge(v) and G.last_adj_edge(v) give, respectively, the first and the last
edge in the representation of graph G going out of vertex v. Further, G.adj_pred(e)
and G.adj_succ(e) give, respectively, the previous and the next edge after e in the
representation of graph G going out of vertex G.source(e). These operations also
support iteration over the vertices of the graph adjacent with a given vertex.
• G.new_vertex() inserts a new vertex in graph G, and G.new_edge(v, w) inserts
a new edge in graph G going out of vertex v and coming into vertex w. Further,
G.del_vertex(v) deletes vertex v from graph G, together with all those edges going
out of or coming into vertex v, and G.del_edge(e) deletes edge e from graph G.
These operations support dynamic graphs.

Notice that some of these operations are actually defined in terms of a smaller set
of 10 abstract operations on graphs,

• G.vertices() • G.target(e)
• G.edges() • G.new_vertex()
• G.incoming(v) • G.new_edge(v, w)
• G.outgoing(v) • G.del_vertex(v)
• G.source(e) • G.del_edge(e)

using the abstract operations on basic data structures presented in Sect. 1.2, as fol-
lows:
• G.number_of_vertices() is given by G.vertices().size().
• G.number_of_edges() is given by G.edges().size().
• G.indeg(v) is given by G.incoming(v).size().
• G.outdeg(v) is given by G.outgoing(v).size().
• G.adjacent(v, w) is true if there is an edge e ∈ G.outgoing(v) such that G.target(e) =
w, and it is false otherwise.
• G.opposite(v, e) is given by G.target(e) if G.source(e) = v, and it is given by
G.source(e) otherwise, that is, if G.target(e) = v.
• G.first_vertex() is given by G.vertices().front().
• G.last_vertex() is given by G.vertices().back().
• G.pred_vertex(v) is given by G.vertices().prev(v).
• G.succ_vertex(v) is given by G.vertices().next(v).
• G.first_edge() is given by G.edges().front().
• G.last_edge() is given by G.edges().back().
• G.pred_edge(e) is given by G.edges().prev(e).
• G.succ_edge(e) is given by G.edges().next(e).
• G.first_in_edge(v) is given by G.incoming(v).front().
• G.last_in_edge(v) is given by G.incoming(v).back().
• G.in_pred(e) is given by G.incoming(v).prev(e).
• G.in_succ(e) is given by G.incoming(v).next(e).
• G.first_adj_edge(v) is given by G.outgoing(v).front().
26 1 Introduction

• G.last_adj_edge(v) is given by G.outgoing(v).back().


• G.adj_pred(e) is given by G.outgoing(v).prev(e).
• G.adj_succ(e) is given by G.outgoing(v).next(e).

Together with the abstract operations on the underlying basic data structures, these
operations support iteration over the vertices and edges of a graph. For instance, in
the following procedure, variable v is assigned each of the vertices of graph G in
turn,

for all vertices v of G do v ∈ G.vertices()


and in the following procedure, all those vertices of graph G which are adjacent with
vertex v are assigned in turn to variable w.

for all vertices w adjacent with vertex v in G do e ∈ G.outgoing(v)


… w = G.target(e)

Together, they provide for a simple traversal of a graph, in which vertices and
edges are visited in the order fixed by the representation of the graph.

for all vertices v of G do v ∈ G.vertices()


for all vertices w adjacent with vertex v in G do e ∈ G.outgoing(v)
… w = G.target(e)

Adjacency Matrix

The data structures most often used for representing graphs are adjacency matrices
and adjacency lists. The adjacency matrix representation of a graph is a Boolean
matrix, with an entry for each ordered pair of vertices in the graph, where the entry
corresponding to vertices v and w has the value true if there is an edge in the graph
going out of vertex v and coming into vertex w, and it has the value false otherwise.

Definition 1.26 Let G = (V, E) be a graph with n vertices. The adjacency matrix
representation of G is an n × n Boolean matrix with an entry for each ordered pair
of vertices of G, where the entry corresponding to vertices v and w is equal to true
if (v, w) ∈ E and is equal to false otherwise, for all vertices v, w ∈ V .

Example 1.27 The adjacency matrix representation of the graph of Fig. 1.1 is shown
in Fig. 1.24. Entries equal to true are denoted by 1, and false entries are denoted by 0.
1.3 Representation of Trees and Graphs 27

Fig. 1.24 Adjacency matrix representation of the graph of Fig. 1.1

Notice that with most data structures used for representing graphs, there is an
order on the vertices fixed by the representation. In the case of the adjacency
matrix representation A of a graph G = (V, E), vertex v precedes vertex w
if and only if the row of A corresponding to v lies above the row of A cor-
responding to w or, in an equivalent formulation, the column of A correspond-
ing to v lies to the left of the column of A corresponding to w, for all vertices
v, w ∈ V .
Now, since the edges are implicit in the adjacency matrix representation of a
graph, those operations having an edge as argument or giving an edge as result
cannot be implemented using an adjacency matrix representation. These opera-
tions are: G.edges(), G.incoming(v), G.outgoing(v), G.source(e), G.target(e), and
G.del_edge(e). Furthermore, G.new_vertex() and G.del_vertex(v) cannot be imple-
mented unless the adjacency matrix can be dynamically resized, and G.new_edge(v, w)
can be implemented by setting to true the entry of A at the row corresponding to
vertex v and the column corresponding to vertex w, and takes O(1) time, but the
operation cannot give the new edge as result.
Let G = (V, E) be a graph with n vertices, let us assume the vertices are numbered
1, 2, . . . , n in some arbitrary manner and their number is stored in the attribute index,
and let A be the adjacency matrix representation of G. The only remaining operation,
G.vertices(), is just the sequence of all vertices v ∈ V ordered by the index attribute.
The adjacency matrix representation of a graph G = (V, E) with n vertices takes
(n 2 ) space. Therefore, no graph algorithm can be implemented using an adjacency
matrix representation to run in O(n) time. The main advantage of the adjacency
matrix representation is the support of the adjacency test in O(1) time.

Adjacency List

The adjacency list representation of a graph, on the other hand, is an array of lists,
one for each vertex in the graph, where the list corresponding to vertex v contains
the target vertices of the edges coming out of vertex v.
28 1 Introduction

Fig. 1.25 Adjacency list


representation of the graph
of Fig. 1.1. Lists of adjacent
vertices are shown as
sequences

Definition 1.27 Let G = (V, E) be a graph with n vertices and m edges. The
adjacency list representation of G consists of a list of n elements (the vertices of
the graph) and a list of n lists with a total of m elements (the target vertices for the
edges of the graph). The list corresponding to vertex v contains all vertices w ∈ V
with (v, w) ∈ E, for all vertices v ∈ V .

Notice that adjacency lists are not necessarily arranged in any particular order
although there is, as a matter of fact, an order on the vertices and edges fixed by the
adjacency list representation of the graph. Given the adjacency list representation of
a graph G = (V, E), vertex precedence is given by the order of the corresponding
entries in the list of lists, and edge (v, w) precedes edge (v, z) if and only if vertex w
precedes vertex z in the list corresponding to vertex v, for all edges (v, w), (v, z) ∈ E.

Example 1.28 The adjacency list representation of the graph of Fig. 1.1 is shown in
simplified form in Fig. 1.25, where lists of adjacent vertices are shown as sequences.

As in the case of adjacency matrices, edges are implicit in the adjacency list rep-
resentation of a graph, and those operations having an edge as argument or giving an
edge as result cannot be implemented using an adjacency list representation. These
operations are: G.edges(), G.incoming(v), G.outgoing(v), G.source(e), G.target(e),
and
G.del_edge(e). Furthermore, G.new_vertex() and G.del_vertex(v) cannot be imple-
mented unless the list of lists can be dynamically resized, and G.new_edge(v, w) can
be implemented by appending vertex w to the list corresponding to vertex v, and takes
O(1) time, but the operation cannot give the new edge as result. The only remaining
operation, G.adjacent(v, w), can be implemented by scanning the list corresponding
to vertex v and takes O(outdeg(v)) time.
The adjacency list representation of a graph G = (V, E) with n vertices and m
edges takes (n + m) space. Beside the low space requirement, the main advan-
tage of the adjacency list representation is the support of iteration operations in
O(1) time. Their main disadvantage, though, lies in the fact that an adjacency test
G.adjacent(v, w) is not supported in O(1) time but in O(outdeg(v)) time.
Notice that the adjacency matrix representation of a graph can be easily obtained
from the adjacency list representation, assuming the vertices are numbered 1, 2, . . . , n
in some arbitrary manner and their number is stored in attribute index. The following
1.3 Representation of Trees and Graphs 29

procedure makes A the adjacency matrix of graph G and runs in O(n 2 +m) = O(n 2 )
time.

let A[1..n][1..n] be a new matrix


for i = 1 to n do
for j = 1 to n do
A[i, j] ← false
for all edges e of G do
A[G.source(e).index, G.target(e).index] ← true

Then, implementing the adjacency test using the adjacency matrix representation
of a graph is straightforward. The following function adjacent(A, v, w) returns true
if (v, w) ∈ E and false otherwise and, given the adjacency matrix representation A
of the graph, runs in O(outdeg(v)) time.

function adjacent(A, v, w)
return A[v.index, w.index]

An alternative implementation of the adjacency test consists of scanning adjacent


vertices in the adjacency list representation. Let G = (V, E) be a graph, and let
v, w ∈ V . The following function adjacent(v, w) returns true if (v, w) ∈ E, and
false otherwise, and runs in O(outdeg(v)) time.

function adjacent(v, w)
for all vertices x adjacent with vertex v in G do
if x = w then
return true
return false

Extended Adjacency List

The adjacency list representation can be extended by making edges explicit. The
extended adjacency list representation of a graph consists of a list of vertices and
a list of edges. Associated with each vertex are two lists of incoming and outgoing
edges. Associated with each edge are its source and target vertices.

Definition 1.28 Let G = (V, E) be a graph with n vertices and m edges. The
extended adjacency list representation of G consists of a list of n elements (the
vertices of the graph), a list of m elements (the edges of the graph), and two lists of n
lists of a total of m elements (the edges of the graph). The incoming list corresponding
30 1 Introduction

Fig. 1.26 Extended


adjacency list representation
for the graph of Fig. 1.1.
Lists of incoming and
outgoing edges are shown as
sequences

to vertex v contains all edges (u, v) ∈ E coming into vertex v, for all vertices v ∈ V .
The outgoing list corresponding to vertex v contains all edges (v, w) ∈ E going out
of vertex v, for all vertices v ∈ V . The source vertex v and the target vertex w are
associated with each edge (v, w) ∈ E.

Example 1.29 The extended adjacency list representation of the graph of Fig. 1.1 is
shown in a simplified form in Fig. 1.26, where lists of incoming and outgoing edges
are shown as sequences.

Notice that edges are explicit in the extended adjacency list graph representation.
Therefore, all of the previous operations can be implemented using the extended adja-
cency list representation and take O(1) time, with the exception of G.del_node(v),
which takes O(deg(v)) time. The operations can be implemented as follows:

• G.vertices() and G.edges() are, respectively, the list of vertices and the list of
edges of graph G.
• G.incoming(v) and G.outgoing(v) are, respectively, the list of edges coming into
vertex v and the list of edges going out of vertex v.
• G.source(e) and G.target(e) are, respectively, the source and the target vertex
associated with edge e.
• G.new_vertex() is implemented by appending a new vertex v to the list of vertices
of graph G and returning vertex v.
• G.new_edge(v, w) is implemented by appending a new edge e to the list of edges
of graph G, setting to v the source vertex associated with edge e, setting to w the
target vertex associated with edge e, appending e to the list of edges going out of
vertex v and to the list of edges coming into vertex w, and returning edge e.
• G.del_vertex(v) is implemented by performing G.del_edge(e) for each edge e in
the list of edges coming into vertex v and for each edge e in the list of edges going
out of vertex v and then deleting vertex v from the list of vertices of graph G.
1.3 Representation of Trees and Graphs 31

• G.del_edge(e) is implemented by deleting edge e from the list of edges of graph G,


from the list of edges coming into vertex G.target(e), and from the list of edges
going out of vertex G.source(e).

The extended adjacency list representation of a graph G = (V, E) with n vertices


and m edges takes (n + m) space.

Adjacency Map

The adjacency list representation can also be extended by making edges explicit in
a different way. The lists of incoming and outgoing edges can be replaced with dic-
tionaries of source vertices to incoming edges and target vertices to outgoing edges.
This allows for a more efficient adjacency test, although adding a logarithmic factor
to the cost to all of the operations (when dictionaries are implemented using balanced
trees) or turning the worst-case cost for all of the operations into the expected cost
(when dictionaries are implemented using hashing).
The adjacency map representation of a graph consists of a dictionary D of vertices
to a pair of dictionaries of vertices to edges: a first dictionary I of source vertices to
incoming edges, and a second dictionary O of target vertices to outgoing edges.

Definition 1.29 Let G = (V, E) be a graph with n vertices and m edges. The adja-
cency map representation of G consists of a dictionary of n elements (the vertices
of the graph) to a pair of dictionaries of m elements (the source and target vertices
for the edges of the graph, respectively). The incoming dictionary corresponding
to vertex v contains the mappings (u, (u, v)) for all edges (u, v) ∈ E coming into
vertex v, for all vertices v ∈ V . The outgoing dictionary corresponding to vertex v
contains the mappings (v, (v, w)) for all edges (v, w) ∈ E going out of vertex v, for
all vertices v ∈ V .

Example 1.30 The adjacency map representation of the graph of Fig. 1.1 is shown
in simplified form in Fig. 1.27, where dictionaries are shown as sequences of vertex-
edge mappings.

Fig. 1.27 Adjacency map representation for the graph of Fig. 1.1. Dictionaries are shown as
sequences of vertex-edge mappings
32 1 Introduction

Notice that both vertices and edges are explicit in the adjacency map representation
of a graph. The vertices are the keys of the dictionary. The edges are the values in
the dictionary of outgoing edges, and also the values in the dictionary of incoming
edges, for all vertices of the graph. Therefore, all of the previous abstract operations
can be implemented using the adjacency map representation and take expected O(1)
time, with the exception of G.del_vertex(v), which takes O(deg(v)) expected time.
The operations can be implemented as follows:

• G.vertices() are the keys in dictionary D.


• G.edges() are the values D[v].O[w] for all keys v in dictionary D and for all keys
w in dictionary D[v].O.
• G.incoming(v) are the values D[v].I [u] for all keys u in dictionary D[v].I .
• G.outgoing(v) are the values D[v].O[w] for all keys w in dictionary D[v].O.
• G.source(e) and G.target(e) are, respectively, the source and the target vertex
associated with edge e.
• G.new_vertex() is implemented by inserting an entry in dictionary D, with key
a new vertex v and value a pair of empty dictionaries D[v].I and D[v].O, and
returning vertex v.
• G.new_edge(v, w) is implemented by setting to v the source vertex associated with
a new edge e, setting to w the target vertex associated with edge e, inserting an
entry in dictionary D[v].O with key w and value e, inserting an entry in dictionary
D[w].I with key v and value e, and returning edge e.
• G.del_vertex(v) is implemented by performing G.del_edge(e) for each entry with
key u and value e in dictionary D[v].I and for each entry with key w and value e
in dictionary D[v].O and then deleting the entry with key v from dictionary D.
• G.del_edge(e) is implemented by deleting the entry with key w from dictionary
D[v].O and deleting the entry with key v from dictionary D[w].I , where v =
G.source(e) and w = G.target(e).

Recall that the adjacency test G.adjacent(v, w) can be defined in terms of the
small set of abstract operations on graphs, using the abstract operations on basic data
structures, by scanning the list of outgoing edges G.outgoing(v) for an edge e such
that G.target(e) = w. Now, besides the low space requirement, the main advantage
of the adjacency map representation is the support of adjacency test in expected O(1)
time, when dictionaries are implemented using hashing, as follows:

• G.adjacent(v, w) is true if (w, e) ∈ D[v].O (where e = D[v].O[w]), and false


otherwise.

The adjacency map representation of a graph G = (V, E) with n vertices and m


edges takes (n + m) space.
Exploring the Variety of Random
Documents with Different Content
"Old man, I did not mean to offend by my speech just now; but I,
too, knew something of Daisy Forrest's history, and I was indignant
at the deed Mrs. Fielding would have done. I hope you will accept
this peace-offering from one who wishes you nothing but kindness."
The kind, gray eyes looking at him enforced the speech so
emphatically that the sexton melted at once, and replied in kindly
terms, while gratefully accepting the offered gold-piece which, like
the donor, had an English appearance.
Then the stranger moved away and sought Flower, who was sobbing
violently now in her seat under the willows. At the sound of his step
she raised to his face the beautiful eyes, all drowned in tears, like
purple-blue pansies wet with dew.
He stopped beside her, and said, gently:
"Miss Fielding, this is an opportune meeting for you and me."
"I do not understand you, sir," said Flower, in a sweet, timid voice,
and he answered, quietly:
"Perhaps not, but I will soon explain to you. Still, this may not be a
proper place to begin my story. There is my card. Will you permit
me, an old man, and the friend of yourself and your kindred, to call
upon you at your home?"
She looked at the bit of pasteboard, and read the name, scrawled in
a bold hand:
"William R. Kelso,
"London, England."
Lifting her sad eyes to his face, she said:
"Mr. Kelso, I am staying at the Springville Hotel. I have no home. I
was driven from Mrs. Fielding's house, after she was sent to the
asylum, by the cruelty of my half-sister. I am indebted to the
kindness of a poor colored woman for the means that enabled me to
reach this place. I must now seek work that I may have the means
of prolonging my miserable existence."
Something like a smile crossed the man's lips at her concluding
words, and a grieved look came into her eyes.
Why should he smile at her sorrows, she wondered.
"I beg your pardon for smiling. I know you think me unfeeling," he
said. "But you will understand me better when you have heard the
good news I have to tell you."
She looked at him with a startled face, and murmured piteously, as
she clasped her little hands together:
"Good news, you say! Ah, if you have anything like that to tell me,
do not wait! Let me hear it now! But, alas! what good fortune could
come to me?" despondently, for the quick thought of Laurie Meredith
was turned aside by the remembrance that he was dead.
Mr. Kelso seated himself on the rustic bench beside her, and said,
earnestly:
"What if I should tell you that I came recently from England to seek
Daisy Forrest and her descendants?"
The quivering red lips parted in wonder, but Flower did not speak,
and he continued:
"I suppose you have never heard that your maternal grandfather
was English?"
Her lips quivered painfully as she answered:
"No, I know nothing, except that my birth was my mother's shame,
and the cause of her death."
"Poor soul!" sighed William Kelso, compassionately, then he added:
"Yes, he was the younger son of a noble English family. His eldest
brother was heir to the title and estates, the second brother was in
the army, and John Forrest, the third and last, was designed for the
church. He was young and wild, and revolted against the restraints
of a clerical life, and ran away to America."
Flower sat up, listening eagerly. This began to sound like one of her
favorite novels.
Smiling sympathetically at the lovely, startled face, Mr. Kelso
continued:
"Lord Ivon was both stern and proud. He vowed he would never
forgive his disobedient, runaway son. When letters came from him
they were laid aside unread, and poor John's fate remained a
mystery to his kindred. His mother pined, but her stern husband
forbid her ever to think of the truant again."
"He was cruel!" Flower murmured, indignantly.
"Yes, he was very hard; but Heaven punished him!" said William
Kelso. "The heir died in a few years, and the second son came home
from the army to take his place. He married late in life, and his
beautiful, delicate wife bore him two sons, and then died. Her
husband was drowned a year later on Lake Como. His two boys
inherited their mother's consumptive tendency, and one died in early
boyhood, and the other just before he attained his majority. Lord
Ivon's house was left unto him desolate."
Flower sighed, and he continued:
"There was no one to inherit the title and estates unless John
Forrest had survived his brothers, or had married and left
descendants. So the letters that had been flung aside at last were
opened eagerly to discover John Forrest's whereabouts. There were
scores of them, for he had never ceased to implore his parents for
their forgiveness. He wrote that he was here in the South, that he
had married a lovely girl, then that he had a lovely child called
Daisy."
"My mother!" Flower exclaimed, sadly.
"Yes, your mother!" said Mr. Kelso.
He paused a moment, watching the long shadows of sunset as they
began to creep across the grave-stones in the old cemetery; then he
resumed:
"After the letter that told of Daisy Forrest's birth, no more came to
Lord Ivon, and he supposed that his son had grown tired of writing,
and had reconciled himself to the alienation. Alas! poor John was
dead."
"Dead!" exclaimed Flower.
"Yes, although his father knows it not yet," said Mr. Kelso. "You
remember all this was thirty-seven years ago, Miss Fielding. Well, to
resume my story, Lord Ivon's heart turned to his younger son when
all his other descendants were gone, and he came to me, his lawyer,
and begged me to cross the ocean and seek an heir to Ivon."
"Alas!" sighed Flower, thinking of the little dead baby she had kissed
and left in Poky's arms. Had it lived—her lovely little child—it would
have been heir to one of the finest titles and estates in old England.
"So I came to this place," continued Mr. Kelso. "I have been here
little more than a week, but I have had no trouble in tracing John
Forrest, for many of the old people in the country about here
remember him well. It seems he had poor luck, or perhaps his
training as a rich man's son had not fitted him to encounter the
hardships of life. He drifted down here from New York, and obtained
employment as an overseer on a farm. Soon after he married the
farmer's only child, a sweet girl named Mary. A year after Daisy was
born, and her father died soon afterward with malarial fever. His wife
survived until her daughter was ten years old, and, dying, left her to
take care of her farmer grandparents. They died when Daisy was
seventeen, and the farm was sold to satisfy a mortgage, and the
beautiful granddaughter was thrown upon the world, helpless and
penniless. She went into a grand family as a nursery governess, met
Charley Fielding, and—the rest you know."
Her low moan of pain attested that she did, and for a moment there
was a deep silence.
Then Mr. Kelso resumed:
"They told me that Daisy Forrest was dead, and her child, too, and I
came here this afternoon to look at her grave before I went back to
England to tell Lord Ivon with him the proud title and name must
die. I am happy that I am spared this sorrowful task, for I think that
after you have examined my credentials you will not hesitate to
secure a maid and return with me to England that I may place you
in the care of your great-grandparents."
He saw the old sexton, who had now replaced the turf and flowers
on Daisy Forrest's grave, looking at them curiously as he leaned on
his spade, and he beckoned him to approach.
Then he gave the old man a brief account of John Forrest's story,
telling him that his father had been a rich man, and that poor Daisy's
child was going home with him to live with her grandparents. He did
not tell him that this great-grandfather was a nobleman, thinking
that Flower would not wish to create too much sensation.
CHAPTER XXIX.
It was of all these stirring events that Azalia Brooks was thinking as
she sat in Raynold Clinton's library, crushing in her jeweled hands
the paper that held those two names with their magic power to
evoke the past, her sad eyes full of retrospection, her heart heavy
with pain.
Since that May day, more than two years ago, when William Kelso
had so opportunely found her beside her mother's grave, she had
been a most fortunate girl, for Lord Ivon and his wife, in their
loneliness and their desire for an heir to reign after them, welcomed
her with open arms, overlooked the dark stain upon her birth, and
only stipulated that it should be kept from the knowledge of the
world. In order to further this end, and to destroy her identity with
Daisy Forrest's illegitimate daughter, they changed her name to
Azalia Brooke, and as no one in England knew any better, except
William Kelso, who kept the secret inviolate, her right to the name
remained undisputed. She remained for a year secluded at Lord
Ivon's magnificent country house in Cornwall, under the care of
accomplished governesses and masters, and when she was
presented in society created the greatest furore by her grace and
beauty. The lovely American, as she was called, was all the rage,
and scores of suitors bowed before her, but all in vain, for no one
ever awakened her heart, they said, and Lord and Lady Ivon began
to feel sorely disappointed. They had hoped she would fall in love
with some of her noble suitors and marry.
"Perhaps you have left a lover in America, dear?" Lady Ivon said,
anxiously, one day; and she never forgot the look of pain that
shadowed the beautiful face as Azalia replied:
"No, grandmamma, I did not. I never had but one lover, and he died
in a few months after we became acquainted."
"But you must have been so very young at the time that you could
not have cared for him so very much," said Lady Ivon, anxiously.
"Yes, I was very young," Azalia answered, dreamily; but she added
to herself that she cared so much that she should never forget her
dead husband, and sighed:

"Forget thee? Yes, when life shall cease


To thrill this heart of mine;
But not till then can I forget
One look or tone of thine!"

There was a burden on her heart—the burden of the secret she had
not dared to confess either to Mr. Kelso or to her great-
grandparents.
She feared that they would not receive her if she confessed that she
had been married in secret to a man who had deserted her so
strangely, and that she had borne a little child that was dead and
had been buried in a secret grave.
"If I told them they might say, like Jewel, that it was all a sham, that
the man had deceived me," she thought, with burning cheeks. "They
might drive me out into the cold, hard world, of which I am so
terribly afraid. No, no, I dare not speak!"
So she kept her sorrowful secret hidden in her own heart; and when
Lady Ivon sometimes caught that look of sad retrospection on the
fair face, she thought that she was thinking of a dead lover—not a
dead husband and child.
"I fear that she must have cared more than I suspected," the old
lady would say to herself, uneasily; and, could she have gazed upon
Azalia now, she would have felt more anxious than ever.
She said to herself that she must find out the truth as to this Laurie
Meredith. But how to accomplish it was the question that occurred to
her, since she dare not ask any questions.
No answer presented itself to her mind, and she could only hope
that she might meet this Laurie Meredith in society.
"But what if I should meet Jewel, too? Would she recognize me?
Would she tax me with my identity? If she did, I should not
acknowledge the truth."
CHAPTER XXX.
It was perhaps a week after that snowy day when Azalia Brooke sat,
looking back with dim, wet eyes into her shadowy past, that Jewel
Fielding reclined at ease in a beautiful boudoir hung in white and
gold, and listened to the roar of the winter wind as it whistled in the
eaves of the handsome but ancient old mansion that she called
home.
The house had been built by an Englishman almost a century ago,
and outside it looked like a small-sized castle, while within it was of
peculiar construction, having some very large and beautiful rooms,
with others so small and ill-ventilated that Jewel turned up her pretty
nose at them, declaring that they were stuffy holes, fit for nothing
that she could see but lumber-closets. There was a great, big,
noisome cellar under the house, too, that in winter often stood feet
deep in water, and was therefore never used for any purpose, but
given over to the use and occupancy of immense rats.
But there were plenty of elegant, comfortable rooms in the grand
house, and the beautiful boudoir where Jewel lay was fine enough
for a queen, and Jewel herself was not unlike a queen in her purple
velvet robe, with its border of silvery fur that was so becoming to
the dusky beauty of her dark, sparkling face, with its crown of jetty
braided hair.
It was a gloomy, overcast afternoon, with a keen, north-east wind
blowing, and heavy patches of last week's deep snow still cumbering
the ground. But the curtains were drawn and the gas ablaze in
Jewel's room, while the leaping flames inside the grate added
tropical warmth to the large room with its beautiful furniture and tall
stands of blooming flowers.
Jewel's eyes were shining with pleasure, for her maid had just
brought in for her inspection a new dress that she was to wear that
night—a marvel of richness, a stately purple brocade and plush, in
which, with her costly diamonds, Jewel knew that she would look
imperially lovely.
"Leave it there, Marie," she said to the pert French maid with her
dainty, beribboned cap; "I wish to study the fall of the drapery at my
leisure. I will ring when I desire you."
Marie bowed and withdrew, and the vain beauty lay idly at full
length, her arms thrown over her head, her dainty slipper tapping
the carpet, and feasted her dark eyes on the shining robe.
"I shall look like a queen—there will be no one to rival me!" she
declared, triumphantly. "Let me see, what flowers shall I wear?—
crimson roses, or creamy-white ones? Or the delicate gold of the
Maréchal Niel? I declare, I can not make up my mind. I shall have to
let Marie decide. She has exquisite taste."
Suddenly a slight frown wrinkled the beautiful forehead, and the
dark eyes flashed.
"Ah, I forgot," she muttered. "They say that that English beauty will
be there! Pshaw! What does it matter? I shall eclipse my Lord Ivon's
great-granddaughter, in spite of the prestige of her position, for they
say she is a blonde, and her pink-and-white charms will stand no
chance against my brunette beauty. All blondes look insipid. I never
saw but one that could hold her own against me, and that was my
twin sister—ah, I forgot—I mean Flower."
She shivered a little, and the slow opening of the door gave her a
violent start.
It was Marie, who had been flirting with the postman at the door.
She carried a letter on a salver.
Jewel snatched it up eagerly, and dismissed her maid.
In a moment she had drawn the letter from the envelope and was
quickly perusing it.
Her face darkened with anger, and she gnawed her crimson lower lip
sharply with her pearly teeth, muttering vindictively:
"I will not do it—never, never! She shall stay there till she dies!"
CHAPTER XXXI.
Again the door opened, and Jewel thrust the letter into the envelope
and slipped her hand down among the folds of her rich gown.
"Marie, what do you mean by interrupting me like this?" she broke
out, petulantly.
Marie courtesied, apologized, and explained that a lady, a woman,
had called to see Miss Fielding, and would not be denied.
"What do you mean by a lady, a woman?" Jewel mimicked,
impatiently; and the maid explained, in broken French, that the
caller had a high-bred voice and air, but was dressed very shabbily,
and had come on foot.
"Her name?" Jewel demanded.
But the shabby caller had given the maid no card.
"Why did you not send her to Mrs. Wellings since she would not go
away?"
Mrs. Wellings had gone to her room with a headache, and desired no
one to disturb her in the little nap with which she proposed to while
away the dull afternoon.
"Headache! too much wine at luncheon!" Jewel muttered, scornfully;
and then, having nothing else to do, and being of a curious
disposition, she said, lightly: "Go, and show your impertinent shabby
lady up here, Marie, and I will find out what she wishes. A beggar,
perhaps—insolent creature!"
Marie withdrew, and Jewel threw herself into an attitude of studied
grace, the better to impress the caller, whom she opined was some
poor creature, a needle-woman desiring work, most probably.
The door opened, and a slight, dark figure, very poorly dressed,
indeed, followed Marie over the threshold and stood there hesitating.
Jewel looked at her curiously, but a dark veil was drawn over the
features of the unknown.
"Well?" she interrogated, curtly and haughtily.
"Send your maid away, please, Miss Fielding," said a low, imploring
voice that made Jewel start in spite of her haughty self-command.
She immediately motioned Marie away, and, rising quickly, turned
the key in the lock after her exit.
Then, with a swift tremor shaking her whole frame, she confronted
the veiled figure.
"Now," she said, sharply, and the veil was flung aside by an agitated
hand, and Jewel and Flower, the long-parted half-sisters, the
beautiful rivals, stood face to face!

Something like a groan of despair came from Jewel's blanched lips,


and Flower said, bitterly:
"You know me!"
Jewel was not taken wholly by surprise. She had been looking for
something like this for two years, never having quite believed her
own story of Flower's suicide. She remained silent a moment,
collecting her thoughts, then said, coldly:
"I have believed you dead for two years, but the moment you spoke
I knew your voice. I never heard a voice quite like yours. But where
have you been so long, and what has brought you here to-night?"
Flower, whose beautiful face was wan and ghastly white, answered,
with sudden passion:
"It matters not where I have been, since it is evident you were glad
to believe me dead. But I will tell you why I am here, Jewel!" and
she drew from beneath her long, black water-proof a worn
newspaper, and held it out to Jewel. "You have read this paragraph,
of course?" she said. "Tell me what it means, or I shall go mad!"
The dark eyes glanced at the short paragraph, the red lips parted in
a malicious smile, and Jewel said, airily:
"It means what it says, of course."
She saw the slight, graceful form shiver with emotion, the blue eyes
dilate widely.
"Oh, Jewel!" gasped the girl, pleadingly. "This Laurie Meredith—who
is he?"
Jewel gave utterance to a low, mocking laugh, and answered:
"Not the dead alive, certainly; for although you have come back
from your supposed grave, your old lover has not. I could keep you
in suspense awhile, but I see you are not able to bear it, so I will tell
you at once that this man whom I am so soon to marry is a cousin
of your Laurie Meredith."
"A cousin!" Flower faltered, disappointedly, plainly betraying the wild
hope that had lurked in her heart, and causing Jewel to exclaim
sharply:
"Why, of course! You could not suppose it was the same man after
you read his death in the paper."
"I—I—thought—hoped, it might be a mistake—that it was some one
else who was dead—not my husband! Oh, I can not tell what I
hoped when I saw that dear name in the paper again!" wailed
Flower; and unable to stand longer, she sunk upon the velvet couch,
and sobbed heart-brokenly.
Jewel watched the bowed, golden head with a terrible hatred, a
panther-like fury in her large, black eyes, and clinching her white
teeth fiercely, she said to herself:
"Ah, I did not know what a hell of hate was in my heart until this
weak girl came between me and my heart's beloved! I can
understand now how my mother hated her mother! I can feel the
same murderous jealousy that made her life wretched! Ah, what am
I to do? She is alive, she is in the same city with Laurie Meredith,
and they will surely find each other out despite all my lies and all my
schemes."
Dark, terrible thoughts came into her mind. She wished that she
could see her sister fall down dead at her feet, so bitter was her
hate.
Suddenly Flower lifted her beautiful, pathetic face, and a gleam of
her old spirit shone in her eyes. She exclaimed, warningly:
"Jewel, I warn you not to deceive me! If it be really Laurie Meredith,
if it was not he who died, tell me the truth! What could it profit you
to keep us apart now? I remember that you used to love him, that
you were angry because he preferred me, but even if he had learned
to love you, believing me dead, you could not be his wife now—now,
while I am alive!"
A cruel, mocking laugh came from Jewel's writhing lips. She bent
forward, and hissed, vindictively:
"You were always a fool, Flower! You never would listen to me when
I told you that Laurie Meredith fooled you into an illegal marriage.
Now, as you demand the truth, you shall have it. Laurie Meredith
was a married man when he first came to our sea-side home, had a
young wife in Boston when he betrayed you. She found out his
treachery somehow, and that was why he left Virginia so suddenly.
She was so imbittered by his wickedness that they say she did not
shed a tear when he died, and in a short time she sold all her
property here and went abroad, never to return."
"No, no; I will not believe he could be so wicked," came in a whisper
of agony from Flower's white lips. "Oh, Jewel, how did you learn all
this?"
"From my betrothed, the cousin of your heartless betrayer," Jewel
replied, coolly; and a short silence fell between the two.
Then Flower exclaimed:
"Jewel, I should like to see this man! I should like to hear from his
own lips—"
Jewel recoiled in horror.
"You are mad!" she cried. "Do you think I would permit it, that I
would own you, the half-sister whose kinship to me is her disgrace
and a brand on the memory of my dead father?"
She turned her back on the poor girl with a disdainful gesture, and
swept toward the fire, and stood there with her pretty pointed
slipper on the fender, murderous thoughts rising in her heart.
"I could kill her, I hate and fear her so much!" she thought, hotly.
Flower's tear-wet eyes had fallen to the floor. They fastened on an
envelope lying close to her feet half under the folds of her dress.
She saw the name of her sister on the upper side.
She did not feel much interest in the letter. She could not
understand afterward, when she came to think of it soberly, why she
had picked it up and hid it in her breast.
CHAPTER XXXII.
Jewel moved from her position in front of the fire, and trailed the
beautiful folds of her purple velvet dress across the floor to the
window.
She drew back a fold of the lace curtain and peered through the
window-pane and the closed shutters into the street.
The short, wintery afternoon was darkening into twilight, and the
sky was obscured by dark, heavy clouds. The proud, imperious
beauty leaned her brow against the cold pane, trying to solve the
problem that tormented her mind.
"I must get rid of her somehow. She dimly suspects treachery on my
part. If she goes out of the house again she will prosecute her
search and learn all," she thought. "Ah, I have a plan! If I could only
detain her here long enough to have that doctor come and take her
to the maison de santé where mamma is, she would never get out
again!"
She turned swiftly, crossed the room to Flower, and sat down by her
side on the sofa, placing her white arm around her sister's neck.
"My poor little sister, forgive me my harshness," she whispered,
penitently.
The drooping, despondent girl started and looked up. That Judas
face was wreathed in a loving smile that bewildered her. Never had
she caught such a look on her half-sister's face since their early
happy days ere Laurie Meredith's love had come between their
hearts.
"Jewel!" she exclaimed, incredulously.
"Darling Flower!" answered the other, and clasped her victim in a
loving embrace, whispering, fondly:
"You see how the old love comes back, dear, in spite of all my efforts
to be your enemy. After all, we are half-sisters. Nothing can alter
that, just as nothing can wholly change our love that was so sweet
and strong when we believed ourselves twins. I forgive you all, for—
listen"—and she pressed her lips to Flower's cold cheek—"I loved
him, too, you know, and if he had fancied me I might have been his
victim instead of you."
Flower clung to her, weeping, all her resentment and suspicion
melted before this specious show of solicitude and affection.
"And," continued Jewel, "I want you to stay with me always, Flower,
and share my home and my wealth. You must take off these shabby
clothes that," playfully, "looks as if they belonged to somebody's
servant. Marie shall bring you one of my prettiest tea-gowns, and
when we have had some tea you shall tell me where you have been
all this while, the reason you ran away that night, and what became
of your little child."
As if those words touched a subtle cord of memory, Flower flung off
the arm that clasped her with sinuous softness, like a serpent's fold,
and cried out, in a terrified voice:
"Not now, Jewel, for I have stayed too long already. I shall be
missed; my—they will be alarmed at my long absence. I must go
now, dear sister, but I—I will come again, or—I will write."
She rushed toward the door, but Jewel clung to her tightly,
entreating her to stay.
"This is your home, your rightful home," she cried, desperately. "It is
too dark and cold for you to go out now! At least stay to-night, and
in the morning—"
She never finished the sentence, for Flower interrupted her,
protesting that she could not, would not, must not stay. She would
come again, but her mistress expected her now.
Jewel's arms began to tighten obstinately about Flower, and then,
frightened and panting, the girl began to struggle frantically to get
away.
It all passed in a minute. Jewel saw that her victim would escape
her, for her frantic struggles began to tell, and she was dragging her
foe with her toward the door. There was a marble-topped stand in
their way, littered with costly trifles of bric-à-brac. Jewel flung out
one hand, caught up something, she knew not what, and brought it
down heavily on the golden head from which the close bonnet had
fallen in the struggle.
There was one low, stifled moan, one only, then the struggling form
relaxed its rigidity, the outstretched arms fell heavily, and in a minute
more Jewel was standing still, looking at something lying very white
and still upon the floor.
CHAPTER XXXIII.
Jewel stood like one rooted to the floor gazing at her terrible work,
for to all appearance life had fled from her hapless victim.
Flower lay like one dead upon the velvet carpet, her eyes half
closed, her face ashen, and the missile with which Jewel had struck
her, a small bronze toy, had glanced aside after doing its deadly
work and fallen several feet away.
Jewel's dark face grew pale, too, and she shuddered with horror of
the deed she had done.
In a minute she flung herself down upon her knees and felt for her
sister's heart, but no pulse stirred the white breast of the prostrate
girl.
"Before Heaven, I did not mean to do this!" Jewel muttered. "I only
meant to stun her that she might not get away. I did not mean to kill
her, but she is dead, and I am a terrible sinner in the sight of God!"
For a minute she felt shocked and remorseful, and longed to bring
Flower back to life; but then that momentary mood was succeeded
by the bitter jealous one of a little while ago, and a half-ashamed
exultation crept into her heart.
"After all," whispered her evil genius, "it is better that it happened
thus. She is out of your way now, and you can marry Laurie
Meredith without fearing that she may turn up at any minute to take
him from you. Rejoice, heart, that your rival is no more!"
After that she thought of nothing but the relief she would feel
hereafter in knowing that Flower was really dead, and of hiding her
dead body where no one could ever find it.
After a few minutes' reflection she thought of the old cellar under
the house. Doubtless it was several feet under water now, owing to

You might also like