Graph Algorithms
Graph Algorithms
Shimon Evens Graph Algorithms, published in 1979, was a seminal introductory book
on algorithms read by everyone engaged in the eld. This thoroughly revised second
edition, witha forewordbyRichardM. Karpandnotes byAndrewV. Goldberg, continues
the exceptional presentation from the rst edition and explains algorithms in formal but
simple language with a direct and intuitive presentation.
The material covered by the book begins with basic material, including graphs and
shortest paths, trees, depth-rst search, and breadth-rst search. The main part of the
book is devoted to network ows and applications of network ows. The book ends with
two chapters on planar graphs and on testing graph planarity.
S HI MON E VE N (19352004) was a pioneering researcher on graph algorithms and
cryptography. He was a highly inuential educator who played a major role in establish-
ing computer science education in Israel at the Weizmann Institute and the Technion.
He served as a source of professional inspiration and as a role model for generations
of students and researchers. He is the author of Algorithmic Combinatorics (1973) and
Graph Algorithms (1979).
Graph Algorithms
2nd Edition
SHIMON EVEN
Edited by
GUY EVEN
Tel-Aviv University
CAMBRI DGE UNI VE RS I T Y P RE S S
Cambridge, New York, Melbourne, Madrid, Cape Town,
Singapore, So Paulo, Delhi, Tokyo, Mexico City
Cambridge University Press
32 Avenue of the Americas, New York, NY 10013-2473, USA
www.cambridge.org
Information on this title: www.cambridge.org/9780521736534
Shimon Even 1979
Shimon Even and Guy Even 2012
This publication is in copyright. Subject to statutory exception
and to the provisions of relevant collective licensing agreements,
no reproduction of any part may take place without the written
permission of Cambridge University Press.
First edition published 1979 by Computer Science Press
Second edition published 2012
Printed in the United States of America
A catalog record for this publication is available from the British Library.
Library of Congress Cataloging in Publication Data
ISBN 978-0-521-51718-8 Hardback
ISBN 978-0-521-73653-4 Paperback
Cambridge University Press has no responsibility for the persistence or accuracy of URLs
for external or third-party Internet Web sites referred to in this publication and does not
guarantee that any content on such Web sites is, or will remain, accurate or appropriate.
Contents
Foreword by Richard M. Karp page vii
Preface to the Second Edition ix
Preface to the First Edition xi
1 Paths in Graphs 1
1.1 Introduction to Graph Theory 1
1.2 Computer Representation of Graphs 3
1.3 Euler Graphs 6
1.4 De Bruijn Sequences 9
1.5 Shortest-Path Algorithms 11
1.6 Problems 22
2 Trees 29
2.1 Tree Denitions 29
2.2 Minimum Spanning Tree 31
2.3 Cayleys Theorem 34
2.4 Directed Tree Denitions 37
2.5 The Innity Lemma 39
2.6 Problems 42
3 Depth-First Search 46
3.1 DFS of Undirected Graphs 46
3.2 Algorithm for Nonseparable Components 52
3.3 DFS on Directed Graphs 57
3.4 Strongly Connected Components of a Digraph 58
3.5 Problems 62
4 Ordered Trees 65
4.1 Uniquely Decipherable Codes 65
v
vi Contents
4.2 Positional Trees and Huffmans Optimization Problem 69
4.3 Application of the Huffman Tree to Sort-by-Merge
Techniques 75
4.4 Catalan Numbers 77
4.5 Problems 82
5 Flow in Networks 85
5.1 Introduction 85
5.2 The Algorithm of Ford and Fulkerson 87
5.3 The Dinitz Algorithm 94
5.4 Networks with Upper and Lower Bounds 102
5.5 Problems 109
5.6 Notes by Andrew Goldberg 115
6 Applications of Network Flow Techniques 117
6.1 Zero-One Network Flow 117
6.2 Vertex Connectivity of Graphs 121
6.3 Connectivity of Digraphs and Edge Connectivity 129
6.4 Maximum Matching in Bipartite Graphs 135
6.5 Two Problems on PERT Digraphs 137
6.6 Problems 141
7 Planar Graphs 146
7.1 Bridges and Kuratowskis Theorem 146
7.2 Equivalence 157
7.3 Eulers Theorem 158
7.4 Duality 159
7.5 Problems 164
8 Testing Graph Planarity 168
8.1 Introduction 168
8.2 The Path Addition Algorithm of Hopcroft and Tarjan 169
8.3 Computing an st-Numbering 177
8.4 The Vertex Addition Algorithm of Lempel, Even, and
Cederbaum 179
8.5 Problems 185
Index 187
Foreword
In Appreciation of Shimon Even
Shimon was a great computer scientist who inspired generations of Israeli
stutents and young researchers, including many future leaders of theoretical
computer science.
He was a master at creating combinatorial algorithms, constructions, and
proofs. He always sought the simplest and most lucid solutions. Because he
never allowedhimself touse a knowntheoremunless he understoodits proof, his
discoveries were often based on original methods. His lectures were legendary
for their clarity.
Shimon was devoted to his family, generous to his colleagues, and freely
available to the students in his classes.
He expressed his views forcefully and with complete honesty. He expected
honesty in return, and reserved his disapproval for those who tried to obfuscate
or mislead.
Shimon had an unending supply of interesting anecdotes, and would laugh
uproariously at good jokes, including his own.
In sum, he was a great and unforgettable man and a great scientist, and his
name has a permanent place in the annals of theoretical computer science.
Richard M. Karp
Berkeley, April 2011
vii
Preface to the Second Edition
My father, Shimon Even, died on May 1, 2004. In the year prior to his illness,
he began revising this book. He used to tell me with great satisfaction whenever
he completed the revision of a chapter. To his surprise, he often discovered
that, after twenty-ve years, he preferred to present the material differently (the
rst edition was published in 1979). Unfortunately, he only managed to revise
Chapters 1, 2, 3, and 5. These revised chapters appear in this edition. However,
since the material in Chapters 9 and 10 on NP-completeness is well covered in
a few other books, we decided to omit these chapters from the second edition.
Therefore, the second edition contains only the rst eight chapters.
As I was reading the manuscript for the second edition, my fathers deep voice
resonated clearly in my mind. Not only his voice, but also his passion for teach-
ing, for elegant explanations, and, most importantly, for distilling the essence.
As an exceptional teacher, he used his voice and his physique to reinforce his
arguments. His smile revealed how happy he was to have the opportunity to
tell newcomers about this wonderful topic. One cannot overvalue the power of
such enthusiasm. Luckily, this enthusiasm is conveyed in this book.
Many people tell me (with a smile) about being introduced to the topic of
algorithms through this book. I believe the source of their smiles is its outstand-
ing balance between clarity and preciseness. When one writes mathematical
text, it is very easy to get carried away with the desire to be precise. The written
letter is long lasting, and being aware that ones text leaves certain gaps requires
boldness. For my father this task was trivial. The audience he had in mind con-
sisted simply of himself. He wrote as he would have wanted the material to be
presented to him. This meant that he elaborated where he needed to, and he did
not hesitate to skim over details he felt comfortable with. As a child, I recall
seeing him prepare for class by reading a chapter from his book. I asked him:
ix
x Preface to the Second Edition
Why are you reading your own book? Presumably, you know what is there.
True, he replied, but I dont remember!
This second edition would have never been completed without Oded
Goldreich. Oded Goldreich began by convincing me to prepare the second edi-
tion. Then he put me in touch with Lauren Cowles from Cambridge University
Press. Finally, he continuously encouraged me to complete this project. It took
almost seven years! There is no good excuse for it. We all knowhowsuch a task
can be pushed aside by more urgent and demanding tasks. Apparently, it
took me some time to realize how important this task was, and that it could not
be completed without a coordinated effort. Only after I recruited LotemKaplan
to do the typesetting of the unrevised chapters and complete the missing gure
and index terms did this project begin to progress seriously. I am truly grateful
to Oded for his insistence, to Lotem for her assistance, and to Lauren for her
kind patience.
Finally, I wish to thank Richard M. Karp, an old friend of my fathers, for his
foreword. I also wish to thank Andrew Goldberg, the expert in network ow
algorithms, for the notes he contributed in Chapter 5. These notes outline the
major developments in the algorithms for maximumow that have taken place
since the rst edition of this book was published.
Guy Even
Tel-Aviv, March 2011
Preface to the First Edition
Graph theory has long been recognized as one of the more useful mathematical
subjects for the computer science student to master. The approachthat is natural
to computer science is the algorithmic one; our interest is not so much in the
existence proofs or enumerationtechniques as it is in nding efcient algorithms
for solving relevant problems or, alternatively, in showing evidence that no such
algorithmexists. Although algorithmic graph theory was started by Euler, if not
earlier, its development in the last ten years has been dramatic and revolutionary.
Much of the material in Chapters 3, 5, 6, 8, 9, and 10 is less than ten years old.
This book is meant to be a textbook for an upper-level undergraduate, or a
graduate course. It is the result of my experience in teaching such a course
numerous times, since 1967, at Harvard, the Weizmann Institute of Science,
Tel-Aviv University, the University of California at Berkeley, and the Tech-
nion. There is more than enough material for a one-semester course; I am sure
that most teachers will have to omit parts of the book. If the course is for
undergraduates, Chapters 1 to 5 provide enough material, and even then, the
teacher may choose to omit a few sections, such as 2.6, 2.7, 3.3, and 3.4.
1
Chapter 7 consists of classical nonalgorithmic studies of planar graphs, which
are necessary in order to understand the tests of planarity, described in Chapter
8; it may be assigned as a preparatory reading assignment. The mathematical
background needed for understanding Chapters 1 to 8 includes some knowl-
edge of set theory, combinatorics, and algebra, which the computer science
student usually masters during his freshman year through courses on discrete
mathematics and on linear algebra. However, the student also needs to know
something about data structures and programming techniques, or he may not
The rst edition was published in 1979 (G.E.).
1
Sections 2.6 and 2.7 were removed from the second edition by Shimon Even.
xi
xii Preface to the First Edition
appreciate the algorithmic side or may miss the complexity considerations. It
is my experience that after two courses in programming, students have the nec-
essary knowledge. However, in order to follow Chapters 9 and 10,
2
additional
background is necessary, namely, in theory of computation. Specically, the
student should know about Turing machines and Churchs thesis.
The book is self-contained. No previous knowledge is needed beyond the
general background just described. No comments such as the rest of the proof
is left to the reader or this is beyond the scope of this book are ever made.
Some unproved results are mentioned, with a reference, but are not used later
in the book.
At the end of each chapter, there are a few problems teachers can use for
homework assignments. The teacher is advised to use them discriminately,
since some of them may be too hard for his students.
I would like to thank some of my past colleagues for our joint work and for the
inuence they have had on my work, and therefore on this book: I. Cederbaum,
M. R. Garey, J. E. Hopcroft, R. M. Karp, A. Lempel, A. Pnuely, A. Shamir,
and R. E. Tarjan. Also, I would like to thank some of my former Ph.D. students
for all that I have learned from them: O. Kariv, A. Itai, Y. Perl, M. Rodeh,
and Y. Shiloach. Finally, I would like to thank E. Horowitz for his continuing
encouragement.
S.E., Techinion, Haifa, Israel
2
Chapters 9 and 10 are not included in the second edition.
1
Paths in Graphs
1.1 Introduction to Graph Theory
A graph G(V, E) is a structure consisting of a set of vertices V = {v
1
, v
2
, . . .}
and a set of edges E = {e
1
, e
2
, . . .}; each edge e has two endpoints, which are
vertices, and they are not necessarily distinct.
Unless otherwise stated, both V and E are assumed to be nite. In this case
we say that G is nite.
For example, consider the graph in Figure 1.1. Here, V = {v
1
, v
2
, v
3
, v
4
, v
5
},
E = {e
1
, e
2
, e
3
, e
4
, e
5
}. The endpoints of e
2
are v
1
and v
2
. Alternatively, we say
that e
2
is incident on v
1
and v
2
. The edges e
4
and e
5
have the same endpoints
and are therefore called parallel. Both endpoints of e
1
are the same v
1
; such
an edge is called a self-loop.
The degree of a vertex v, d(v), is the number of times v is used as an endpoint.
Clearly, a self-loop uses its endpoint twice. Thus, in our example, d(v
1
) = 4,
d(v
2
) =3. Also, a vertexwhose degree is zerois called isolated. In our example,
v
3
is isolated since d(v
3
) = 0.
Lemma 1.1 In a nite graph the number of vertices of odd degree is even.
Proof: Let |V| and |E| be the number of vertices and edges, respectively. It is
easy to see that
|V|
i=1
d(v
i
) = 2 |E|,
since each edge contributes two to the left-hand side: one to the degree of each
of its two endpoints if they are distinct; and two to the degree of its endpoint if
the edge is a self-loop. For the left-hand side to sum up to an even number, the
number of odd terms must be even.
1
2 1 Paths in Graphs
e
3
v
1
e
1
e
2
v
2
v
3
v
4
e
5
e
4
v
5
Figure 1.1: Example of a graph.
The notation u
e
v means that the edge e is incident on vertices u and v. In
this case we also say that e connects vertices u and v, or that vertices u and v
are adjacent.
A path, P, is a sequence of vertices and edges, interweaved in the following
way: P starts with a vertex, say v
0
, followed by an edge e
1
incident to v
0
,
followed by the other endpoint v
1
of e
1
, and so on. We write
P : v
0
e
1
v
1
e
2
v
2
If P is nite, it ends with a vertex, say v
l
. We call v
0
the start-vertex of P and
v
l
the end-vertex of P. The number of edge appearances in P, l, is called the
length of P. If l = 0, then P is said to be empty, but it has a start-vertex and
an end-vertex, which are identical. (We shall not use the term path unless a
start-vertex exists.)
In a path, edges and vertices may appear more than once, unless otherwise
stated. If no vertex appears more than once, and therefore no edge can appear
more than once, the path is called simple.
A circuit, C, is a nite path in which the start and end vertices are identical.
However, an empty path is not considered a circuit. By denition, the start and
end vertices of a circuit are the same, and if there is no other repetitionof a vertex,
the circuit is called simple. However, a circuit of length two, a
e
b
e
a, where
the same edge, e, appears twice, is not considered simple. (For a longer circuit,
it is superuous to state that if it is simple, then no edge appears more than
once.) A self-loop is a simple circuit of length one.
If for every two vertices u and v of a graph G, there is a (nite) path that starts
in u and ends in v, then G is said to be connected.
A digraph or directed graph G(V, E) is dened similarly to a graph, except
that the pair of endpoints of every edge is now ordered. If the ordered pair of
1.2 Computer Representation of Graphs 3
endpoints of a (directed) edge e is (u, v), we write
u
e
v.
The vertex u is called the start-vertex of e; and the vertex, v, the end-vertex of
e. The edge e is said to be directed fromuto v. Edges with the same start-vertex
and the same end-vertex are called parallel. If u,=v, u
e
1
v and v
e
2
u, then
e
1
and e
2
are antiparallel. An edge u u is called a self-loop.
The out-degree d
out
(v) of vertex v is the number of (directed) edges having
v as their start-vertex; in-degree d
in
(v) is similarly dened. Clearly, for every
nite digraph G(V, E),
vV
d
in
(v) =
vV
d
out
(v).
A directed path is similar to a path in an undirected graph; if the sequence of
edges is e
1
, e
2
, then for every i 1, the end-vertex of e
i
is the start-vertex of
e
i+1
. The directed path is simple if no vertex appears on it more than once. A
nite directed path Cis a directed circuit if the start-vertex and end-vertex of C
are the same. If C consists of one edge, it is a self-loop. As stated, the start and
end vertices of C are identical, but if there is no other repetition of a vertex, C
is simple. A digraph is said to be strongly connected if, for every ordered pair
of vertices (u, v) there is a directed path which starts at u and ends in v.
1.2 Computer Representation of Graphs
To understand the time and space complexities of graph algorithms one needs
to know how graphs are represented in the computers memory. In this section
two of the most common methods of graph representation are briey described.
Let us consider graphs and digraphs that have no parallel edges. For such
graphs, the specication of the two endpoints is sufcient to specify the edge;
for digraphs, the specication of the start-vertex and the end-vertexis sufcient.
Thus, we can represent such a graph or digraph of nvertices by an nnmatrix
M, where M
ij
=1 if there is an edge connecting vertex v
i
to v
j
, and M
ij
=0, if
not. Clearly, in the case of (undirected) graphs, M
ij
= 1 implies that M
ji
=1;
or in other words, Mis symmetric. But in the case of digraphs, any nnmatrix
of zeros and ones is possible. This matrix is called the adjacency matrix.
Given the adjacency matrix Mof a graph, one can compute d(v
i
) by counting
the number of ones in the i-th row, except that a one on the main diagonal
represents a self-loop and contributes two to the count. For a digraph, the number
4 1 Paths in Graphs
of ones in the i-th row is equal to d
out
(v
i
), and the number of ones in the j-th
column is equal to d
in
(v
j
).
The adjacency matrix is not an efcient representation of the graphif the graph
is sparse; namely, the number of edges is signicantly smaller than n
2
. In these
cases, it is more efcient to use the incidence lists representation, described
later. We use this representation, which also allows parallel edges, in this book
unless stated otherwise.
A vertex array is used. For each vertex v, it lists vs incident edges and a
pointer indicating the current edge. The incidence list may simply be an array
or may be a linked list. Initially, the pointer points to the rst edge on the list.
Also, we use an edge array, which tells us for each edge its two endpoints (or
start-vertex and end-vertex, in the case of a digraph).
Assume we want an algorithm TRACE(s, P), such that given a nite graph
G(V, E) and a start-vertex s V traces a maximal path P that starts at s and does
not use any edge more than once. Note that by maximal we do not mean that
the resulting path, P, will be the longest possible; we only mean that P cannot
be extended, that is, there are no unused incident edges at the end-vertex.
We can trace a path starting at s by taking the rst edge e
1
on the incidence list
of s, marking e
1
as used in the edge array, and looking up its other endpoint
v
1
(which is s if e
1
is a self-loop). Next, use the vertex array to nd the pointer
to the current edge on the list of v
1
. Scan the incidence list of v
1
for the rst
unused edge, take it, and so on. If the scanning hits the last edge and it is used,
TRACE(s, P) halts. A PASCAL-like description of TRACE(s, P) is presented
in Algorithm 1.1. Here is a list of the data structures it uses:
(i) A vertex table such that, for every v V, it includes the following:
A list of the edges incident on v, which ends with NIL
A pointer N(v) to the current item in this list. Initially, N(v) points to
the rst edge on the list (or to NIL, if the list is empty).
(ii) An edge table such that every e E consists of the following:
The two endpoints of e
A ag that indicates whether e is used or unused. Initially, all edges are
unused.
(iii) An array (or linked list) P of edges that is initially empty, and when
TRACE(s, P) halts, will contain the resulting path.
Notice that in each application of the while loop of TRACE (lines 210 in
Algorithm 1.1), either N(v) is moved to the next item on the incidence list of
v (line 4), or lines 610 are applied, but not both. The number of times line
1.2 Computer Representation of Graphs 5
Procedure TRACE(s, P)
1 v s
2 while N(v) points to an edge (and not to NIL) do
3 if N(v) points to a used edge do
4 change N(v) to point to the next item on the list
5 else do
6 e N(v)
7 change the ag of e to used
8 add e to the end of P
9 use the edge table to nd the second endpoint of e, say u
10 v u
Algorithm 1.1: The TRACE algorithm.
4 is applied is clearly O(|E|). The number of times lines 610 are applied is
also O(|E|), since the ag of an unused edge changes to used, and each of these
lines takes time bounded by a constant to run. Thus, the time complexity of
TRACE is O(|E| ).
1
(In fact, if the length of the resulting P is L then the time
complexity is O(l); this follows from the fact that each edge that joins P can
cause a waste of computing time only twice: once when it joins P and, at
most, once again by its appearance on the incidence list of the adjacent vertex.)
If one uses the adjacency matrix representation, in the worst case, the tracing
algorithmtakes time (and space) complexity (|V |
2
).
2
And if |E| << |V |
2
, as is
the case for sparse graphs, the complexity is reduced by using the incidence-list
representation. Since in most applications, the graphs are sparse, we prefer to
use the incidence-list representation.
Note that in our discussions of complexity, we assume that the word length of
our computer is sufcient to store the names of our atomic components: vertices
and edges. If one does not make this assumption, then one may have to allow
(log(|E| +|V|)) bits to represent the atomic components, and to multiply the
complexities by this factor.
1
f(x) is O(g(x)) if there are two constants k
1
and k
2
, such that for every x, f(x)k
1
g(x)+k
2
.
2
f(x) is (g(x)) if there are two constants k
3
and k
4
, such that for every x, f(x)k
3
g(x)+k
4
.
6 1 Paths in Graphs
1.3 Euler Graphs
An Euler path of a nite undirected graph G(V, E) is a path such that every
edge of G appears on it once. Therefore, the length of an Euler path is |E|. If G
has an Euler path, then it is called an Euler graph.
Theorem 1.1 A nite (undirected) connected graph is an Euler graph if and
only if exactly two vertices are of odd degree or all vertices are of even degree.
In the latter case, every Euler path of the graph is a circuit, and in the former
case, none is.
As an immediate conclusion of Theorem 1.1 we observe that none of the
graphs in Figure 1.2 is an Euler graph because both have four vertices of odd
degree. The graph shown in Figure 1.2(a) is the famous Knigsberg bridge
problemsolved by Euler in 1736. The graph shown in Figure 1.2(b) is a common
misleading puzzle of the type draw without lifting your pen from the paper.
Proof: It is clear that if a graph has an Euler path that is not a circuit, then
the start-vertex and the end-vertex of the path are of odd degree, while all the
other vertices are of even degree. Also, if a graph has an Euler circuit, then all
vertices are of even degree.
Assume now that G is a nite graph with exactly two vertices of odd degree,
a and b. We nowdescribe an algorithm(A), which will nd an Euler path from
a to b.
First, trace a maximal path P, as in the previous section, starting at vertex a.
Since G is nite, the algorithm halts, producing a path. But as soon as the path
emanates froma, one of the edges incident to a is used, and as residual degree
becomes even. Thus, every time a is reentered, there is an unused edge to leave
(a) (b)
Figure 1.2: Non-Eulerian graphs.
1.3 Euler Graphs 7
a by. This proves that P cannot end in a. Similarly, if vertex v V \ {a, b}, then
P cannot end in v. It follows that P ends in b.
If P contains all the edges of G, we are done. If not, we make the following
observations:
at v. Since all
vertices of the residual graph are of even degree, P
) to produce P
.
Now, let the last edge of P
point to e.
Note that when TRACE(v, P
Since every subpath of a shortest path is shortest from its start-vertex to its
end-vertex, the subpath of T from s to w is shortest, and its length consists
of the length of a shortest path froms to u plus l(e), that is,
(w) =(u) +l(e).
Since uhas joined P before time , by the inductive hypothesis, (u) (u).
Thus,
(u) +l(e) (u) +l(e).
1.5 Shortest-Path Algorithms 17
Since u has joined P before time , all its outgoing edges, including e, have
been examined, as per Lines 813. Thus, at time , (w) has been assigned,
and
(u) +l(e) (w).
k
(i, j) stands for the length of a shortest path from vertex i to
vertex j, among all paths which do not go through vertices k+1, k+2, . . . , n
as intermediate vertices.
The Floyd algorithm is described in Algorithm 1.6.
It is easy to see that the time complexity of the Floyd algorithmis O(n
3
); there
are napplications of the outer loop (Lines 911), and in each of its applications,
there are n
2
applications of Line 11.
The proof of validity is also easy, by induction on k. A shortest path from i
to j, among paths that do not go through vertices higher than k, either does not
7
Rdenotes the set of real numbers.
8
We will show that only one matrix is necessary, but for didactic reasons, let us start with n+1
matrices.
22 1 Paths in Graphs
Procedure FLOYD(G(V, E), l;
n
)
1 for every 1 i n do
2 if there is a self-loop i
e
i and l(e) <0 then do
3
0
(i, i) l(e)
4 else
0
(i, i) 0
5 for every 1 i, j n such that i ,=j do
6 if there is an edge i
e
j then do
7
0
(i, j) l(e)
8 else
0
(i, j)
9 for every k, starting with k = 1 and ending with k =n do
10 for every 1 i, j n do
11
k
(i, j) min{
k1
(i, j),
k1
(i, k) +
k1
(k, j)}
Algorithm 1.6: The Floyd algorithm.
go through vertex k, and is therefore equal to
k1
(i, j), or does go through k
and is therefore equal to
k1
(i, k) +
k1
(k, j).
However, the space complexity of the algorithm, as stated in Algorithm 1.6,
is (n
3
), since there are n matrices of size nn each. It is easy to see that
there is no need to keep previous matrices. Two matrices sufce: The previous
one and the one being computed. In fact, one matrix will do, where some
of the entries are as in
k1
; and some, as in
k
. (Observe that a shortest path
from i to k, or from k to j, never needs to go through k, since there are no
negative circuits.) Thus, in fact, the space complexity can be reduced to O(n
2
),
by dropping the superscript indexes of .
Finally, one can use Floyds algorithmto check whether there are negative cir-
cuits in the digraph. Simply apply the algorithm, and in the end, check whether
there is an i for which (i, i) <0. If so, there is a negative circuit.
1.6 Problems
Problem 1.1 Prove that if a connected (undirected) nite graph has exactly 2k
vertices of odd degree, then the set of edges can be partitioned into k paths such
that every edge is used exactly once. Is the condition of connectivity necessary
or can it be replaced by a weaker condition?
1.6 Problems 23
Figure 1.6: A graph for Problem 1.4.
Problem 1.2 Let G(V, E) be an undirected nite circular Euler graph; that is,
Gis connected and for every v V, d(v) is even. A vertex s is called universal
if every application of TRACE(s, G), no matter how the edges are ordered in
the incidence lists, produces an Euler circuit.
Prove that s is universal if and only if s appears in every simple circuit
of G.
9
Problem1.3 Let G(V, E) be a nite digraph such that for every v V, d
in
(v) =
d
out
(v). Also, assume that the exits from v are labeled 1, 2, . . . , d
out
(v).
Consider a tour in G, which starts at a given vertex s. Every time a vertex
v is visited, the next exit is chosen to leave, starting with exit number 1 and
continuing cyclically. However, the tour stops if s is reached and all its exits
have been taken.
Prove that the tour stops and that every edge has been used at most once.
10
Problem1.4 AHamilton path (circuit) is a simple path (circuit) in which every
vertex of the graph appears exactly once.
Prove that the graph shown in Figure 1.6 has no Hamilton path or circuit.
Problem 1.5 Prove that in every completely connected digraph (a digraph in
which every two vertices are connected by exactly one directed edge in one of
the two possible directions), there is always a directed Hamilton path. (Hint:
Prove by induction on the number of vertices.)
Problem 1.6 Prove that a directed Hamilton circuit of the de Bruijn digraph,
G
,n
, corresponds to a directed Euler circuit of G
,n1
. Is it true that G
,n
always has a directed Hamilton circuit?
9 See[10].
10
More about such tours and nding Euler circuits can be found in [11].
24 1 Paths in Graphs
... ...
a
c
b
Figure 1.7: A switch for Problem 1.7.
Problem1.7 In the following, assume that G(V, E) is a nite undirected graph,
with no parallel edges and no self-loops.
(i) Describe an algorithm which attempts to nd a Hamilton circuit in G by
working with a partial simple path. If the path cannot be extended in either
direction, then try to close it into a simple circuit by the edge between its
endpoints, if it exists, or by a switch, as suggested by Figure 1.7, where
edges a and b are added and c is deleted. Once a circuit is formed, look
for an edge from one of its vertices to a new vertex, and open the circuit
to a now longer simple path, and so on.
(ii) Prove that if for every two vertices u and v, d(u) +d(v) n, where
n = |V|, then the algorithm never fails to produce a Hamilton circuit.
(iii) Deduce Diracs Theorem [12]: If for every vertex v, d(v)
n
2
, then G
has a Hamilton circuit.
Problem 1.8 Describe an algorithm for nding the number of shortest paths
from s to t, after the BFS algorithm has been performed.
Problem 1.9 A digraph is called acyclic if there are no directed circuits.
11
Let
G(V, E) be a nite acyclic digraph. A bijection f : V {1, 2, . . . , n}, where
n = |V|, is called a topological sorting if for every edge u v, f(u) <f(v).
Consider the procedure described in Algorithm 1.7. A queue Qof vertices is
used, which is initially empty.
Prove that this is an algorithm, that it computes a topological sorting and that
its time complexity is O(|V| + |E|).
Problem 1.10 Show that the Dijkstra algorithm is not applicable if there are
negative edges, even if the digraph is acyclic.
11
Sometimes called DAG, for directed acyclic graph.
1.6 Problems 25
Procedure TOPO.SORT(G;f)
1 for every v V compute d
in
(v)
2 for every v V do
3 if d
in
(v) = 0 then put v in Q
4 i 1
5 while Q,= do
6 remove the rst vertex u from Q
7 f(u) i
8 i i +1
9 for every edge u v do
10 d
in
(v) d
in
(v) 1
11 if d
in
(v) = 0 then put v in Q
Algorithm 1.7: Topological sorting.
Problem1.11 In the Dijkstra algorithm, assume the sequence of vertices which
join P, in this order, is s =v
1
, v
2
, . . .. Prove that the sequence (v
1
), (v
2
), . . .
is nondecreasing.
Problem1.12 Assume G(V, E) is a nite digraph, l : E!a length function,
and assume the length of every directed circuit is positive. Also, assume s V
is the source, V
!is the
distance function.
We want to compute the function : V
, E
) be a subgraph of G, where E
k
(i, j) for all i, j and k.
Set l(i, j) as follows:
l(i, j) =
: a =v
0
e
1
v
1
e
2
v
2
v
1
e
=b.
Since both paths are simple, one cannot be the beginning of the other. Let i be
the rst index for which e
i
,=e
i
. That is, the two paths split at v
i1
=v
i1
. Let
v be the rst vertex on P, after the split, which is also on P
k
. The subpath of P, between v
i1
and v
j
, and the subpath
of P
, between v
i1
and v
k
, form a simple circuit.
(c) (d): We assume that Ghas no self-loops and that for every two vertices,
there is a unique simple path connecting them. Thus, G is connected.
Assume now that we delete an edge a
e
b from G. Since G has no self-
loops, a ,=b. If there is still a (simple) path in (V, E\ {e}) connecting a and b,
then in (V, E), there are two simple paths connecting a and b. A contradiction.
(d) (a): We assume that G is connected and that no edge can be deleted
without interrupting the connectivity.
If G has a simple circuit, any edge on this circuit can be deleted without
interrupting the connectivity. Thus, G is circuit-free.
There are two, more common ways to dene a nite tree. These are presented
in the following theorem:
Theorem 2.2 Let G(V, E) be a nite graph and n = |V|. The following three
conditions are equivalent:
(a) G is a tree.
(b) G is circuit-free and |E| =n1.
(c) G is connected and |E| =n1.
Proof: For n =1 the theorem is trivial. Assume n2. We shall prove that (a)
(b) (c) (a).
(a) (b): Let us prove, by induction on n, that if G(V, E) is a tree, then
|E| = n1. This statement clearly holds for n = 1. Assume that it is true for
all n <m, and let G be a tree with m vertices.
Delete from G any edge e. By condition (d) of Theorem 2.1, the resulting
graph is not connected any more, and has two disjoint connected components.
Each of these components is circuit-free and is therefore a tree. By the inductive
hypothesis, each component has one edge less than its number of vertices. Thus,
together they have m2 edges. Add e back, and the number of edges is m1.
(b) (c): We assume that G is circuit-free and has n1 edges. Let us rst
show that G has at least two vertices of degree 1.
2.2 Minimum Spanning Tree 31
Choose any edge u
e
v; there is at least one edge, since the number of
edges is n1 and n2. Also, u ,=v since Gis circuit-free. As in the TRACE
procedure, walk on new edges, starting from u. Since the number of edges is
nite, this walk must end, say, at vertex t
1
. Also, the traced path is simple,
or a simple circuit would have been formed. Thus, d(t
1
) = 1. A similar walk,
starting from v, yields another vertex, t
2
, and d(t
2
) = 1, as well. Thus, there
are two vertices of degree 1.
Now, the proof that G is connected proceeds by induction on the number of
vertices, n. Obviously, the statement holds for n = 1. Assume that it holds for
n=m1, and let G be a circuit-free graph with mvertices and m1 edges.
Eliminate from G a vertex v of degree 1 and its incident edge. The resulting
graph is still circuit-free and has m1 vertices and m2 edges. Thus, by the
inductive hypothesis it is connected. Therefore, G is connected, as well.
(c) (a): Assume that G is connected and has n1 edges. As long as G
has simple circuits, we can eliminate edges (without eliminating vertices) and
maintain the connectivity. When this process terminates, the resulting graph is
a tree, and, by (a) (b), it has n1 edges. This, however, is the number of
edges we started with. Thus, no edge has been eliminated, and therefore G is
circuit-free.
(V
, E
V and
E
E.
1
Assume G(V, E) is a nite, connected (undirected) graph and each edge e E
has a known length l(e) >0. Assume that we want to nd a connected subgraph
G
(V, E
eE
l(e), is minimum; or, in other words, we
want to remove fromG a subset of edges whose total length is maximum, and
yet, the resulting subgraph remains connected. Such a subgraph is a tree. For
G
V and E
may not be in V
.
32 2 Trees
Procedure PRIM(G, l;T)
1 for every v V do
2 (v)
3 choose a vertex s V
4 (s) 0
5 (s)
6 TEMP V
7 T
8 while TEMP ,= do
9 choose a vertex v TEMP for which (v) is minimum
10 TEMP TEMP \ {v}
11 T T (v)
12 for every v
e
u do
13 if u TEMP and (u) >l(e) then do
14 (u) l(e)
15 (u) {e}
Algorithm 2.1: The Prim algorithm.
is a tree is called a spanning tree of G. Thus, our problem is that of nding a
minimum (length) spanning tree (MST) of G.
There are several known algorithms for constructing an MST. We describe
here the Primalgorithm[1]. Additional algorithms are discussed inthe problems
section in the end of the chapter.
The Prim algorithm is described in Algorithm 2.1.
2
In the following discussion, we shall use T to denote a subgraph of G, which
is a tree, and the set of edges in this tree. TEMP denotes the set of vertices, not
yet in T.
The idea of the algorithm is to grow T, by starting with a tree with one
vertex, s, and in each step adding a new leaf, v, to T, until all vertices have
joined it. Thus, v is chosen in such a way that the edge connecting it to T (the
single edge in the set (v)) is of minimum length among edges which connect
a vertex of T to a vertex of TEMP.
2
The structure of the algorithm is similar to that of Dijkstra, but historically, Prims algorithm
precedes it.
2.2 Minimum Spanning Tree 33
When a new vertex, v, joins T, all its incident edges, v
e
u are checked. If
u is not a vertex of T yet (this is equivalent to the fact that u TEMP), and if
l(e) is shorter than the presently known shortest edge (if any) that connects u
to a vertex of T, then the value of (u) is updated to be l(e), and the edge e is
recorded in (the set) (u).
Since G is connected, when PRIM halts, the set of edges in T constitutes a
spanning tree. The complexity of the algorithm is similar to that of Dijkstras
algorithm; see Section 1.5.2.
Before we discuss the validity of the algorithm, let us dene the concept of a
cut.
Let S V and
S =V \S. The cut (S;
(V, (T
opt
{e}) \ {e
}).
Since an edge of a simple circuit has been removed, H
is a spanning tree.
By the choice of v, (v) is minimum(among values of for vertices in TEMP),
and the edge e (v) satises l(e) =(v) l(e
The analogous problem for digraphs, namely, that of nding a subset of the
edges E
) is
a strongly connected subgraph, is much harder. In fact, even the case where
l(e) = 1 for all edges is hard. This is discussed in Chapter 10.
34 2 Trees
2 3
1
2 3
1
2 3
1
Figure 2.1: The spanning trees on three named vertices.
2.3 Cayleys Theorem
In a later section we shall consider the question of the number of spanning trees
of a given graph. Here we consider the more restricted, and yet interesting,
problem of the number of trees one can construct on a given set of vertices,
V = {1, 2, . . . , n}.
For n=2, there is only one tree that one can construct, consisting of an edge
between the two vertices. For n = 3, there are three possible trees, as shown
in Figure 2.1. The reader can verify, by exhausting all cases, that for n = 4 the
number of trees is 16. The following theorem is due to Cayley [3]. Warning:
We shall use the integers 1, 2, . . . , n in three different meanings. As names of
vertices, as integers, and as letters of an alphabet.
Theorem 2.4 The number of spanning trees for n distinct vertices is n
n2
.
The remainder of this section describes a proof due to Prfer [4]. (For a survey
of various proofs see Moon [5].)
Assume V = {1, 2, . . . , n}. Let us display a bijection between the set of
the spanning trees and the n
n2
words of length n 2 over the alphabet
{1, 2, . . . , n}. The mapping fromthe set of spanning trees to the corresponding
set of words is dened by an algorithm which is described in Algorithm 2.2.
For example, assume that n = 6 and T is as shown in Figure 2.2. We now
apply TREEtoWORD. We start with the given T and an empty template for
a word w of 4 letters. This is depicted in the rst line of Figure 2.3. T has 3
leaves, and vertex 2 has the least name. Therefore, vertex 2 and its incident
edge, 2 4, are removed. The rst letter in the word w is 4, as shown in the
second line of Figure 2.3. The next leaf to be removed is 3, and a
2
= 1, and
so on. After four steps, the tree consists of two vertices (4 and 6) and an edge
between them, while w= 4164.
By Corollary 2.1, Line (2) of TREEtoWORD algorithm can always be per-
formed. Note that when a leaf is removed, the remaining graph is still a tree. It
follows that for every tree of n vertices, a word wof length n2 is produced.
Since algorithm TREEtoWORD is deterministic, it denes a mapping f from
2.3 Cayleys Theorem 35
Procedure TREEtoWORD(T(V, E);w=a
1
a
2
a
n2
)
1 for every i starting with 1 and ending with n2 do
2 choose a leaf v whose name is minimum among the leaves of T
3 let v
e
u be its incident edge
4 a
i
u
5 remove e and v from T
Algorithm 2.2: The TREEtoWORD algorithm. Mapping a spanning
tree T to a word w.
2
5
4 6 1 3
Figure 2.2: T: An example of a spanning tree with six vertices.
trees to words. It remains to be shown that no word is produced by two different
trees, and that for every word w there is a tree T such that f(T) =w.
Notice that TREEtoWORD is insensitive to the nature of the set of vertices,
V, of T, as long as the names of the vertices are distinct and an order is dened
on these names. We shall assume that V is a set of integers, not necessarily
{1, 2, . . . , n}.
Lemma 2.1 If in T a vertex v has a degree d(v), then in w=f(T) the letter v
appears d(v) 1 times.
Proof: When each of the edges incident on v are removed, except the last one,
one writes the letter v in w. Thus, v appears d(v) 1 times in w. The last edge
may not be removed at all, if v is one of the two vertices which remain when
TREEtoWORD halts. And if vs last edge is removed, then v is the removed
leaf, and its neighbor, not v, is written in w.
Lemma 2.2 For every word w = a
1
a
2
a
n2
over an alphabet V, where
|V| =n, there is a unique tree T whose vertices are V and f(T) =w.
36 2 Trees
2
4 6 1 3
5
4 6 1 3 4 5
4 6 1 4 5 1
6 4 6 4 5 1
6 4 6 4 1 4
Figure 2.3: Applying TREEtoWORD to the the example tree T.
Proof: By induction on n2. For n=2, wis the empty word and if V = {u, v},
there is only one tree whose set of vertices is V. TREEtoWORD does nothing
(leaving T intact, and w empty).
Now assume the claim holds for n 1 and let us prove it for n. Let w =
a
1
a
2
a
n2
be a word over an alphabet V, where |V| =n.
Since the alphabet has n letters, while there are n2 appearances of letters
in w, there are at least two letters missing fromw. Let v be the least letter that
does not appear in w. Now consider the word w
= a
2
a
3
a
n2
, with the
alphabet V
) =w
. Notice that a
1
appears in w,
and therefore a
1
,=v, and thus a
1
V
.
By Lemma 2.1, the set of leaves of T
, with the
possible exception of a
1
.
Now dene T to be the tree of n vertices that results from T
by adding to it
the vertex v and an edge connecting v to a
1
. Clearly T is a tree, and a
1
is not a
leaf of T, even if it has been a leaf of T
is a tree. Thus, by Theorem 2.1 part (c), there is a unique simple path
fromr to every vertex in G
. Also, G
, is
connected, d
in
(r) = 0, and for v ,=r, d
in
(v) = 1. First let us prove that r is a
root of G.
Let P
. This must
correspond to a directed path, P, fromr to v in G, for if any of the edges points
in the wrong direction, it would imply either that d
in
(r) >0 or that for some
u, d
in
(u) >1.
Now, assume G
,= e,
then remove e
: If e / T, do nothing.
Otherwise, let S and
S be the two sets of vertices in the two connected compo-
nents of T \ {e}. If in G
S) = then G
}.
Prove the validity of this algorithm and compare its time complexity with
computing an MST anew.
Problem2.7 Compute the number of trees that can be built on n, given labeled
vertices with unlabeled edges, in such a way that one specied vertex is of
degree k.
Problem 2.8 Let V = {1, 2, . . . , n} and for each 1 i n d(i) is a positive
integer. Prove that if
n
i=1
d(i) = 2n2,
then there exists a tree T(V, E) in which for every i, the degree of i is d(i). How
many such trees are there if the edges have no names?
Problem 2.9 What is the number of trees that one can build with n labeled
vertices and m=n1 labeled edges? Prove that the number of trees that can
be built with m2 labeled edges (and no labels on the vertices) is (m+1)
m2
.
Explain why the condition that m2 is necessary.
4
Problem 2.10 A digraph which has no directed circuits is called a DAG
(directed acyclic graph). One wants an algorithm that checks whether a given
nite DAG, G(V, E), has a root.
One way to do this is rst to check that there is only one vertex r, such that
d
in
(r) = 0, and then check if all vertices are reachable from r. Explain why
this is valid, and prove that the time complexity of such an algorithmis O(|E|).
4
This problem was inspired by an unpublished report of S. Golomb and A. Lempel, and a
comment made by A. Pnueli.
44 2 Trees
Problem 2.11 Given a nite digraph G(V, E). Describe an algorithm which
runs in time O(|V|) and checks whether G is a directed tree.
Problem2.12 Prove that, if Gis an innite undirected connected graph whose
vertices are of nite degrees, then every vertex of G is the start vertex of some
simple innite path.
Problem 2.13 Show that, if rotation or ipping of tiles is allowed, then the
question of tiling the plane becomes trivial.
Problem 2.14 Consider the following (innite) set of tile families: For every
ordered pair of positive integers (i, j), there is a tile family with a label i 1 in
the West, i in the East, j 1 in the South, and j in the North. Prove that one can
tile the upper-right quadrant with this set of families, but not the whole plane.
Problem 2.15 Let T be an undirected tree with n vertices. We want to invest
O(n) time, labeling the graph in a way that will allow one to nd a (minimum)
path between any two vertices that are of distance apart, in time O(). Describe
both a preparatory algorithmand an algorithmfor nding the path once the two
vertices are specied.
Problem 2.16 A clique is a simple undirected graph such that for every two
vertices there is an edge connecting them. Let G(V, E) be a clique and T(V, E
)
a spanning tree of G. Prove that the complement of T (= (V, E
)) is either
connected or consists of one isolated vertex, while the remaining vertices form
a clique.
Problem2.17 Let G(V, E) be an undirectednite connectedgraph, where each
edge e has a given length l(e) >0 and s is a designated vertex. Also, let (v)
denote the distance froms to v.
(a) Explain why every vertex v ,= s, has an incident edge u
e
v, such that
(v) =(u) +l(e).
(b) Show that if one such edge is chosen for each vertex v ,=s, then the set of
these edges forms a spanning tree of G.
(c) Show that such a tree is not necessarily an MST.
Bibliography
[1] Prim, R.C., Shortest Connection Networks and Some Generalizations, Bell
System Tech. J., Vol. 36, 1957, pp. 13891401.
[2] Kruskal, J.B., On the Shortest Spanning Subtree of a Graph and the Traveling
Salesman Problem, Proc. of the Amer. Math. Society, Vol. 7, 1956, pp. 4850.
2.6 Problems 45
[3] Cayley, A., A Theorem on Trees, Quart. J. Math., Vol. 23, pp. 376378. Also in
Collected Papers, Vol. 13, Cambridge, 1897, pp. 2628.
[4] Prfer, H., Neuer Beweise eines Satzes ber Permutationen, Arch. Math. Phys.,
Vol. 27, 1918, pp. 742744.
[5] Moon, J.W., Various Proofs of Cayleys Formula for Counting Trees, A Seminar
on Graph Theory, F. Harary (ed.), Holt, Rinehart and Winston, 1967, pp. 7078.
[6] Berge, C., and A. Ghouila-Houri, Programming, Games and Transportation
Networks, Wiley, 1965, Sec. 7.4.
[7] Knig, D., Theorie der endlichen und unendlichen Graphen, Liepzig, 1936.
Reprinted by Chelsea, 1950.
[8] Wang, H., Proving Theorems by Pattern Recognition, Bell System Tech. J.,
Vol. 40, 1961, pp. 141.
3
Depth-First Search
3.1 DFS of Undirected Graphs
The depth-rst search (DFS) technique is a method of scanning a nite,
undirected graph. Since the publication of the papers of Hopcroft and Tar-
jan [4, 6], DFS has been widely recognized as a powerful technique for solving
various graph problems. However, the algorithmhas been known since the nine-
teenth century as a technique for threading mazes. See, for example, Lucas
report of Trmauxs work [5]. Another algorithm, which was suggested later
by Tarry [7], is just as good for threading mazes, and in fact, DFS is a special
case of it. But the additional structure of DFS is what makes the technique so
useful.
3.1.1 Trmauxs Algorithm
Assume one is given a nite, connected graph G(V, E), which we will also refer
to as the maze. Starting in one of the vertices, one wants to walk along the
edges, from vertex to vertex, visit all vertices, and halt. We seek an algorithm
that will guarantee that the whole graph will be scanned without wandering
too long in the maze, and that the procedure will allow one to recognize when
the task is done. However, before one starts walking in the maze, one does not
knowanything about its structure, and therefore, no preplanning is possible. So,
decisions about where to go next must be made one by one as one goes along.
We will use markers, which will be placed in the maze to help one to
recognize that one has returned to a place visited earlier and to make later
decisions on where to go next. Let us mark the passages, namely the connections
of the edges to vertices. If the graph is presented by incidence lists, then we
can think of each of an edges two appearances in the incidence lists of its two
endpoints as its two passages. It sufces to use two types of markers: F for the
rst passage used to enter the vertex, and E for any other passage used to leave
46
3.1 DFS of Undirected Graphs 47
Procedure TRMAUX(G, s)
1 v s
2 while there is an unmarked passage in v or v has a passage marked F do
3 if there is an unmarked passage to edge v
e
u, then do
4 mark the passage of e at v by E
5 if u has no marked passages then do
6 mark the passage of e at u by F
7 v u
8 else mark the passage of e at u by E
9 else (there is a passage in v marked F) do
10 use the passage marked F to move to the neighboring vertex u
11 v u
Algorithm 3.1: The Trmaux algorithm.
s
a
b
c
E
E
E
F
E
E
E
F E
E
Figure 3.1: An example of running Trmauxs algorithm.
the vertex. No marker is ever erased or changed. There is no use of memory
other than the markers on the passages and the stage of the algorithmone is has
reached; in other words, the moves are controlled by a nite state automaton.
As we shall prove later, the algorithmdescribed in Algorithm3.1 will terminate
in the original starting vertex s, after scanning each edge once in each direction.
Let us demonstrate the algorithmon the graph shown in Figure 3.1. The initial
value of v, the place where we are situated, or the center of activity, is s. All
passages are unlabeled. We choose one, mark it Eand traverse the edge. Its other
endpoint is a (u =a). None of its passages are marked, therefore we mark the
48 3 Depth-First Search
passage through which a has been entered by F, the new center of activity is
a (v = a), and we are back in Line 2. Since a has two unmarked passages,
assume that we choose the one leading to b. The passage at a is marked E and
the one at b is marked F since b is new, etc. The complete excursion is shown
in Figure 3.1 by the dashed line.
Lemma 3.1 Trmauxs algorithm never allows an edge to be traversed twice
in the same direction.
Proof:
1
If a passage is used as to exit a vertex and enter an edge), then either it
is being marked E in the process, and thus the edge is never traversed again in
this direction, or the passage is already marked F. It remains to be shown that
no passage marked F is ever reused for entering the edge.
Let u
e
v be the rst edge to be traversed twice in the same direction, from
u to v. The passage of e, at u, must be labeled F. Since s has no passages
marked F, u ,=s. Vertex u has been left d(u) +1 times; once through each of
the passages marked E and twice through e. Thus, u must have been entered
d(u) +1 times and some edge been used twice to enter u, before e is used for
the second time. A contradiction.
An immediate corollary of Lemma 3.1 is that the process described by Tr-
mauxs algorithm will always terminate. Clearly, it can only terminate in s,
since every other visited vertex has an F passage. Therefore, all we need to
prove is that, upon termination, the whole graph has been scanned.
Lemma 3.2 Upon termination of Trmauxs algorithm, every edge of the graph
has been traversed once in each direction.
Proof: Let us state the proposition differently: For every vertex, all its incident
edges have been traversed in both directions.
First, consider the start vertex s. Since the algorithm has terminated, all the
incident edges of s have been traversed from s outward. Thus, s has been left
d(s) times, and since we end up in s, it has also been entered d(s) times.
However, by Lemma 3.1 no edge is traversed more than once in the same
direction. Therefore, every edge incident to s has been traversed once in each
direction. Let S be the set of vertices for which the statement that each of their
incident edges has been traversed once in each direction holds. Since s S,
S ,= . Assume V ,= S. By the connectivity of the graph there must be edges
1
The following terminology is used in the proof. A passage from a node uto an edge v
e
u is
used as an exit if the algorithm exits v via the passage. In such a case, the passage is used to
enter the edge. (G.E.)
3.1 DFS of Undirected Graphs 49
connecting vertices of S with V \ S. All these edges have been traversed once
in each direction. Let e be the rst edge to be traversed from a vertex v S to
u V \ S. Clearly, the passage of e, at u, is marked F. Since this passage has
been entered, all other passages of u must have been marked E. Thus, each of
us incident edges has been traversed outward. The search has not started in u
and has not ended in u. Therefore, u has been entered d(u) times, and each of
its incident edges has been traversed inward. A contradiction, since u belongs
in S.
Observe that the loop (Lines 210) is applied at most once for every passage,
and the number of computational steps in each application of the loop is bounded
by a constant. Thus, the time complexity is O(|E|).
3.1.2 The Hopcroft-Tarjan Version of DFS
The Hopcroft and Tarjan version of DFS is essentially the same as Trmauxs,
except that they number the vertices from 1 to n(= |V|) in the order in which
they are discovered. This is not necessary, as we have seen, for scanning the
graph, but the numbering is useful in applying the algorithmfor more advanced
tasks. Let us denote the number assigned to vertex v by k(v). Also, instead of
marking passages, they mark edges as used, and instead of using the F mark to
indicate the edge through which the vertex was discovered and through which
it is left for the last time, let us record for each vertex v other than s the vertex
f(v) from which v has been discovered. Then f(v) is called the father of v; this
name will be justied later. DFS is described in Algorithm 3.2.
Since this algorithm is just a simple variation of the previous one, our proof
that the whole (connected) graph will be scanned, each edge once in each
direction, still applies. Here, in Line 11, if k(u) ,=0, then u is not a new vertex,
and v, the center of activity, does not change. This is equivalent to the scanning
of e fromv to u and back to v, as was done in Trmauxs version. Also, moving
our center of activity fromv to f(v) (Line 16) corresponds to traversing the edge
v f(v), in this direction. Thus, the whole algorithm is of time complexity
O(|E|), namely, linear in the size of the graph.
Now that we have applied DFS to a nite and connected G(V, E), let us
consider the set of edges E
) is a directed tree.
Clearly, if one ignores the edge directions, (V, E
) is a spanning tree of G.
In a directed tree, vertex u is called an ancestor of v, and v is called a
descendant of u if there is a directed path from u to v.
The following very useful lemma is due to Hopcroft and Tarjan [4, 6]:
3.1 DFS of Undirected Graphs 51
Lemma 3.4 Let (V, E
(V
, E
V the induced
subgraph G
(V
, E
V = {s
1
, s
2
, . . . , s
p
} {C
1
, C
2
, . . . , C
m
},
E = {s
i
C
j
| s
i
is a vertex of C
j
in G}.
By the observations we made in the beginning of the section,
G is a tree. By
Corollary 2.1, if m>1, then there must be at least two leaves in
G. However,
the degree of a separating vertex s
i
in
G is greater than 1. Thus, there are at
least two leaf components in G, each containing only one separating vertex.
By Lemma 3.2, the whole graph will be explored by the DFS. Now, assume
the search starts in a vertex r that is not a separating vertex. Even if it is in one of
the leaf components, eventually, we will enter another leaf component C, say,
via its separating vertex s and an edge s v. By Lemma 3.6, L(v) k(s),
and if L(v) is known when we backtrack fromv to s, then by using Lemma 3.5,
we can detect that s is a separating vertex. Also, as far as the component C is
concerned, from the time C is entered via s v until it is entirely explored,
we can think of the algorithm as running on C alone, with s as the starting
vertex. Thus, by Lemma 3.7, there is only one tree edge froms to other vertices
of C, and all other vertices of Care descendants of v and are therefore explored
after v is discovered and before the backtrack from v to s. This suggests the
use of a stack (pushdown store) for producing the vertices of the component.
We store the vertices in the stack in the order that they are discovered. If on
backtracking from v to f(v), we discover that f(v) is a separating vertex, we
3.2 Algorithm for Nonseparable Components 55
read off all vertices fromthe top of the stack down to and including v. All these
vertices, plus f(v), (which is not removed at this point from the stack even if it
is the next on top) constitute a component. This, in effect, removes the leaf C
fromthe tree
G, and if its adjacent separating vertex s has nowd(s) = 1, then we
may assume that it is removed too. The new superstructure is again a tree, and
the same process will repeat itself, detecting and trimming one leaf at a time
until only one component is left when the DFS terminates.
If the search starts in a separating vertex r, then all but the components con-
taining r are detected and produced as before. All components that contain r,
except the last one, are detected by Lemma 3.7: Each time we backtrack into r,
on r v, if r still has additional unexplored incident edges, then we conclude
that r is a separating vertex, and the vertices on the stack above, including v,
plus r, constitute a component.
Finally, when the search is about to end, since we are going to backtrack
to r, and r has no new incident edges, all vertices on the stack form the last
component, although no separating vertex is discovered at this point.
The remaining problem is that of computing L(v) in time; that is, its value
should be known by the time we backtrack from v. If v is a leaf of the DFS
tree, then L(v) is the least element in the following set: {k(u) | u =v or v
uis a back edge}. Let us assign L(v) =k(v) immediately when v is discovered,
and as each back edge v uis explored, let us assign L(v) =min{L(v), k(u)}.
Clearly, by the time we backtrack fromv, all the back edges have been explored,
and L(v) has the right value. If v is not a leaf of the DFStree, then L(v) is the least
element in the following set: {k(u) | u =v or v u is a back edge} {L(u) |
v uis a tree edge}. When we backtrack fromv, we have already backtracked
from all its sons earlier, and therefore already know their lowpoint. Thus, in
addition to what we do for a tree leaf, it sufces to do the following: When we
backtrack from u to v =f(u), we assign L(v) = min{L(v), L(u)}.
Putting together the ideas described above leads to the algorithmrepresented
in Algorithm 3.3. In this representation, L() is the lowpoint function, and
S is a stack of vertices. The remaining variables are as in DFS. For the given
undirected, connected, and nite graphG(V, E), |V| >1, the algorithmproduces
the set of separating vertices and a list of its nonseparable components, both of
which are assumed to be initially empty. Note that just before the last backtrack
into s, Lines 2627 produce the last nonseparable component, which may be
the entire V if G has no separating vertices.
Although this algorithm is more complicated then the original DFS, its time
complexity is still O(|E|). This follows easily from the fact that each edge is
still scanned exactly once in each direction. The number of operations per edge
56 3 Depth-First Search
Procedure NONSEPARABLE(G(V, E), s;set of separating vertices,
list of nonseparable components)
1 for every e E mark e new
2 for every u V do
3 k(u) 0
4 f(u) NIL
5 v s
6 k(s) 1
7 i 2
8 vacate S
9 push s into S
10 while v has a new incident edge or f(v) ,= NIL do
11 if v has a new incident edge v
e
u then do
12 mark e old
13 if k(u) = 0 (u is a new vertex) then do
14 push u into S
15 f(u) v
16 k(u) i
17 L(u) i
18 i i +1
19 v u
20 else (u is old) do
21 L(v) min{L(v), k(u)}
22 else (f(v) is dened)
23 if L(v) k(f(v)) then do
24 if f(v) ,=s or s has a new incident edge, then do
25 add f(v) to the set of separating vertices
26 pop vertices from S down to and including v
27 the set of popped vertices, with f(v), is an element
of the set of nonseparable components
28 else (L(v) <k(f(v))) then do
29 L(f(v)) min{L(f(v)), L(v)}
30 v f(v)
Algorithm 3.3: Using DFS to nd the separating vertices and
nonseparable components.
3.3 DFS on Directed Graphs 57
is bounded by a constant, except when a nonseparable component is produced.
Each vertex is pushed into S once, and popped once. Thus, the total time to
produce the components is O|V|.
3.3 DFS on Directed Graphs
Running DFS on digraphs is similar to runningit on undirected graphs. We start
scanning from a new vertex and will scan all vertices (and edges) that can be
reached from it via directed paths. A new scan is started from a new vertex, as
long as the previous search has left unscanned vertices. Thus, the fact that the
whole graph is scanned is trivial.
Assuming the given nite digraph is G(V, E), where |V| =n. Again, we assign
a number k(u) to every vertex u, where k : V {1, 2, . . . , n} is a bijection. If
a vertex u is rst discovered by scanning an edge v u, then we assign
f(u) =v. Here, too, v denotes the center of activity.
The algorithm is described in Algorithm 3.4. It is easy to see that the time
complexity is O(|V| + |E|).
Let us call a vertex v ripe if all its outgoing edges are old and the center of
activity is at v. When v is ripe, and if f(v) ,= NIL, then we backtrack to f(v).
Otherwise, we return to Line 6 in Algorithm 3.4.
Let us denote by T
u
the directed subtree whose root is u, and all vertices that
are discovered from the time u is discovered to the time u is ripe are in it.
2
The
edges of T
u
are of the formf(w)
e
w, where both f(w) and ware in T
u
, and
w has been discovered via e.
Lemma 3.8 If vertex wis newwhen u is discovered, and if at that time there is
a directed path from u to w such that all its intermediate vertices (and edges)
are new, then w is in T
u
.
Proof: By contradiction. Assume that wis as in the premise of the Lemma, but
wis not in T
u
. Let P be a directed path fromuto wsuch that all its intermediate
vertices are new when u is discovered, and let b be the rst vertex on P that
does not belong to T
u
. Let a
e
b be the edge on P that enters b.
Since a T
u
, and it eventually becomes ripe, e must have been investigated,
as in Line 11 of Algorithm 3.4, and b has been discovered and belongs to T
u
.
A contradiction.
2
Note that u is not necessarily a root of a search in the sense that it may not have been picked in
Line 7, and may belong to some T
v
for v =u.
58 3 Depth-First Search
Procedure Directed-DFS(G(V, E), s;k(), f())
1 for every e E mark e new
2 for every u V do
3 k(u) 0
4 f(u) NIL
5 i 1
6 while there is a vertex u for which k(u) = 0 do
7 let v be such a vertex
8 k(v) i
9 i i +1
10 while v has a new outgoing edge or f(v) ,= NIL do
11 if v has a new outgoing edge v
e
w, then do
12 mark e old
13 if k(w) = 0 (u is a new vertex) then do
14 f(w) v
15 k(w) i
16 i i +1
17 v w
18 else v f(v)
Algorithm 3.4: DFS on a directed graph.
3.4 Strongly Connected Components of a Digraph
Let G(V, E) be a nite digraph. Let us dene the relation , a subset of V V,
in the following way: For x, y V, x y if there is a directed path from x to y
and also a directed path from y to x.
The relation is easily seen to be reexive, symmetric, and transitive. Thus,
it is an equivalence relation. An equivalence class of this relation is called a
strongly connected component or, in short, a strong component, and if there is
only one equivalence class, then G is said to be strongly connected.
The super-structure of G,
G(
V,
E) is a digraph constructed as follows:
V is the set of strong components of V.
E = {X
e
Y | there exists an edge x
e
y in E such that x X and y Y}.
3.4 Strongly Connected Components of a Digraph 59
Observe that
G is a DAG (directed acyclic graph). A strong component C is
called a source if there are no edges that enter C in
G. A sink component is
similarly dened.
Given a digraph G(V, E), our purpose is to describe an efcient algorithmfor
nding its strong components. The rst linear-time algorithmthat achieved this
goal was presented by Tarjan [6]. However, we present an algorithm attributed
to Kosaraju and Sharir [1], which is simpler to explain.
3
The algorithm consists of three phases:
Phase 1: Run a DFS-A on G, in which vertices are numbered h : V
{1, 2, . . . , n} in the order in which they become ripe. This is described
in Algorithm 3.5.
Phase 2: Reverse the direction of all edges of G(V, E) to obtain G
R
(V, E
R
).
Phase 3: Run a DFS-B on G
R
, where each time a new search begins, it is
started in the new vertex v for which h(v) is maximum; the set of
vertices found in a search is declared to be a strong component of G.
This is described in Algorithm 3.6.
Observe that the strong components of G
R
are identical to those of G. Also,
the algorithm consists of three phases, each of time complexity O(|V| + |E|).
Thus, the whole algorithm is of time complexity O(|V| + |E|).
Lemma 3.9 The function h(), produced by DFS-A, satises the following
condition: For every u V,
h(u) = max
wT
u
{h(w)} .
Proof: Follows from the fact that u is the last vertex in T
u
to become ripe.
Lemma 3.10 Let C be a strong component of G, and let u be the vertex of
C for which h(u) is maximum. It follows that u is the rst vertex of C to be
discovered in DFS-A.
Proof: If a is the rst vertex of C to be discovered in DFS-A, then by
Lemma 3.8, u T
a
. By Lemma 3.9, h(a) h(u). Since h is a bijection,
and h(u) is maximum in C, it follows that a =u.
Corollary 3.1 If the premise of Lemma 3.10 holds, then all vertices of C are
in T
u
.
3
The algorithm of Tarjan uses DFS once, while that of Kosaraju and Sharir uses two runs of DFS.
60 3 Depth-First Search
Procedure DFS-A(G(V, E);h())
1 for every e E mark e new
2 for every u V do
3 f(u) NIL
4 mark u new
5 i 1
6 while there are new vertices do
7 let v be such a vertex
8 mark v old
9 while v has new outgoing edges or f(v) ,=NIL do
10 if v has new outgoing edges, then do
11 let v
e
w be a new edge
12 mark e old
13 if w is new then do
14 mark w old
15 f(w) v
16 v w
17 else (f(v) ,=NIL) then do
18 h(v) i
19 i i +1
20 v f(v)
21 h(v) i
22 i i +1
Algorithm 3.5: DFS-A.
Lemma 3.11 Assume DFS-A was applied to G and vertex v was assigned the
highest value of h(v). The strong component C to which v belongs is a source
component.
Proof: By contradiction. Assume there is an edge a
e
b, such that a / C
and b C.
Let r be the root, chosen in Line 7, of the search in which a is discovered.
Then r / C, for otherwise, there would be a directed path fromC, which starts
in r, to a, to b, and thus, a belongs to C, contradicting the assumption that
a / C. We conclude that r ,=v.
3.4 Strongly Connected Components of a Digraph 61
Procedure DFS-B(G
R
(V, E
R
), h();strong components of G)
1 for every e E
R
mark e new
2 for every u V do
3 f(u) NIL
4 mark u new
5 while there are new vertices do
6 let v be the new vertex for which h(v) is maximum
7 S {v}
8 mark v old
9 while v has new outgoing edges or f(v) ,=NIL do
10 if v has new outgoing edges, then do
11 let v
e
w be a new edge
12 mark e old
13 if w is new then do
14 mark w old
15 f(w) v
16 S S{w}
17 v w
18 else (f(v) ,=NIL) then do
19 v f(v)
20 print: The set S is a strong component
Algorithm 3.6: DFS-B.
Observe that the last vertexto serve as a root of a search is the vertex for which
h() is maximum. Thus, v is a root of a search. Also, T
r
is constructed before v
is discovered. But when r is chosen to be a root of a search, all vertices on the
path fromr to a, to b, to v, are new. Thus, by Lemma 3.8, v T
r
, contradicting
the fact that v is a root of a new search.
Since the component C, containing the vertex v for which h(v) is maximum,
is a source component of G, it is a sink component of G
R
. It follows that in
DFS-B, since the root of the rst search is v, all vertices of C, and none else,
are discovered in this search, and indeed, this set S is declared to be a strong
component.
62 3 Depth-First Search
Procedure TARRY(G, s)
1 v s
2 while there is an unmarked passage in v or v has a passage marked F do
3 if there is an unmarked passage to edge v
e
u then do
4 mark the passage of e at v by E
5 if u has no marked passages then do
6 mark the passage of e at u by F
7 v u
8 else (there is a passage in v marked F) do
9 use the passage marked F to move to the neighboring vertex u
10 v u
Algorithm 3.7: The Tarry algorithm.
Now, assume we remove from G all vertices of the rst declared component
and their incident (outgoing) edges to forma directed subgraphG
. The remain-
ing directed subforest, of the original DFS-A forest of G, and the values of h()
for the remaining vertices are legitimate in the sense that one can run DFS-A
on G
to yield exactly this forest and assignments of h(). It follows that the
next declared strong component is valid too. By induction, all declared strong
components are valid as well.
3.5 Problems
Problem 3.1 Tarrys algorithm [7] is like Trmauxs, with the following
change. Upon reaching an old vertex u, one moves the center of activity to
that vertex anyway, instead of insisting on marking the passage through which
one has just reached u by E and not moving the center of activity to u. Tarrys
algorithmis described in Algorithm3.7. Prove that Tarrys algorithmterminates
after all edges of G have been traversed, once in each direction.
Problem 3.2 Consider the set of edges which upon termination of Tarrys
algorithm(see Problem 3.1) have one endpoint marked E and the other marked
F. Also, assume these edges are now directed from E to F.
(i) Prove that this set of edges form a directed spanning tree of G, with root s.
(ii) Does a statement like that of Lemma 3.4hold in this case? Prove or disprove.
3.5 Problems 63
Problem 3.3 Fraenkel [2, 3] showed that the number of edge traversals can
sometimes be reduced, in comparison to the Tarry algorithm(see Problem3.1),
if the use of a two-way counter is allowed. Each time a new vertex is entered,
the counter is incremented. When it is realized that all incident edges of a vertex
have been traversed at least in one direction, the counter is decremented. If the
counter reaches the start value, the search is terminated. (One can return to s
via the passages marked F.)
Write an algorithm that realizes this idea. (Hint: An additional mark that
temporarily marks the passages used to reenter a vertex is used.) Prove the
validity of your algorithm. Showthat for some graphs the algorithmwill traverse
each edge exactly once for others; the savings depends on the choice of passages.
Yet there are graphs for which the algorithm can save only one traversal, even
if we do not insist on returning to s.
Problem 3.4 Assume that G is drawn in the plane in such a way that no two
edges cross. Show how Trmauxs algorithm can be modied in such a way
that the whole scanning path never crosses itself.
Problem 3.5 In an undirected graph G, a set of vertices K is called a clique if
every two vertices of K are connected by an edge. Prove that, in the spanning
(directed) tree resulting from running DFS on a nite and connected G, all
vertices of a clique appear on one directed path. Do they necessarily appear
consecutively on the path? Justify your answer.
Problem 3.6 Prove or disprove the following claim: If C is a simple circuit in
an undirected, nite, and connected graph G to which DFS is applied, then all
vertices of C appear on one directed path of the (directed) DFS tree. Does you
answer change if C is an induced circuit? (A circuit C is an induced circuit in
G if G does not contain an edge between two nonadjacent vertices in C.)
Problem 3.7 Prove that if C is a directed circuit of a nite digraph to which
DFS is applied and v is a vertex on C for which k(v) is minimum, then v is a
root of a subtree of the resulting directed forest, and all vertices of C are in this
subtree.
Problem3.8 An edge e of a connected undirected graph Gis called a bridge if
the deletion of e destroys Gs connectivity. Describe an algorithm to compute
all bridges of a given nite G. (Hint: There are two ways to solve the problem.
In the rst solution, one modies the graph and uses the algorithm for nding
the separating vertices. In the second, one modies the algorithm and applies
the modied algorithm to G.)
64 3 Depth-First Search
Problem 3.9 (This problem was suggested by Silvio Micali.) Let G be a
connected graph.
1
c
2
c
n
with the following properties:
65
66 4 Ordered Trees
(1) c
i
, 1 i m, and c
j
, 1 j n are code-words, and c
1
,=c
1
;
(2) t is a sufx of c
n
;
(3) c
1
c
2
c
m
t =c
1
c
2
c
n
.
Lemma 4.1 A code C is UD if and only if no tail is a code-word.
Proof: If a code-word c is a tail then, by denition, there exist two messages
c
1
c
2
c
m
and c
1
c
2
c
n
that satisfy c
1
c
2
c
m
c =c
1
c
2
c
n
, while c
1
,=c
1
.
Thus, there are two different ways to parse this message, and C is not UD.
If C is not UD, then there exist messages that can be parsed in more than
one way. Let be such an ambiguous message, whose length is minimum: =
c
1
c
2
c
k
=c
1
c
2
c
n
; i.e. all the c
i
-s and c
j
-s are code-words and c
1
,= c
1
.
Now, without loss of generality we can assume that c
k
is a sufx of c
n
(or
change sides). Thus, c
k
is a tail.
The following algorithmgenerates all the tails. If a code is a tail, the algorithm
terminates with a negative answer.
Algorithm for UD:
(1) For every two code-words, c
i
and c
j
(i ,=j), do the following:
a. If c
i
=c
j
, halt; C is not UD.
b. If for some word s, either c
i
s =c
j
or c
i
=c
j
s, put s in the set of tails.
(2) For every tail t and every code-word c do the following:
a. If t =c, halt; C is not UD.
b. If some word s, either ts =c or cs =t, put s in the set of tails.
(3) Halt; C is UD.
Clearly, in Step (1), the words declared to be tails are indeed tails. In step
(2), since t is already known to be a tail, there exist code-words c
1
, c
2
, . . . , c
m
and c
1
, c
2
, . . . , c
n
such that c
1
c
2
c
m
t = c
1
c
2
c
n
. Now, if ts = c, then
c
1
c
2
c
m
c = c
1
c
2
c
n
s, and therefore s is a tail; and if cs = t, then
c
1
c
2
c
m
cs =c
1
c
2
c
n
and s is a tail.
Next, if the algorithmhalts in (3), we want to showthat all the tails have been
produced. Once this is established, it is easy to see that the conclusion that C
is UD follows; each tail has been checked, in Line (2a.), whether it is equal to
a code-word, and no such equality has been found. By lemma 4.1, the code C
is UD.
For every t let m(t) = c
1
c
2
c
m
be a shortest message such that
c
1
c
2
c
m
t = c
1
c
2
c
n
and t is a sufx of c
n
. We prove by induction on
4.1 Uniquely Decipherable Codes 67
the length of m(t) that t is produced. If m(t) =1, then t is produced by ((1)b.),
since m=n = 1.
Now assume that all tails p for which m(p) < m(t) have been produced.
Since t is a sufx of c
n
, let p denote the word such that pt = c
n
. Therefore,
c
1
c
2
c
m
=c
1
c
2
c
n1
p, and p is a tail.
If p =c
m
, then c
m
t =c
n
, and t is produced in Step (1).
If p is a sufx of c
m
, then p is a tail. Also, m(p) is shorter then m(t). By
the inductive hypothesis, p has been produced. When Step (2b) is applied to
pt =c
n
(with p as a tail and c
n
as a code-word), the tail t is produced.
If c
m
is a sufx of p, then c
m
t is a sufx of c
n
, and therefore, c
m
t is a
tail. Also, m(c
m
t) =c
1
c
2
c
m1
and is shorter than m(t). By the inductive
hypothesis, c
m
t has been produced. When Step (2b) is applied to the tail c
m
t
and code-word c
m
, the tail t is produced.
This proves that the algorithm halts with the right answer.
Let the code consist of nwords, and l be the maximumlength of a code-word.
Step (1) takes at most O(n
2
l) elementary operations. The number of tails is
at most O(n l). Thus, Step (2) takes at most O(n
2
l
2
) elementary operations.
Therefore, the whole algorithmis of time complexity O(n
2
l
2
). Other algorithms
of the same complexity can be found in [3] and [4]; these tests are extendible
to test for additional properties [5, 6, 7].
Theorem 4.1 Let C = {c
1
, c
2
, . . . , c
n
} be a UD code over an alphabet of
letters. If l
i
=l(c
i
), i = 1, 2, . . . , n, then
n
i=1
l
i
1. (4.1)
The left-hand side of 4.1 is called the characteristic sum of C; clearly, it
characterizes the vector (l
1
, l
2
, . . . , l
n
), rather than C. The inequality 4.1is called
the characteristic sumcondition. The theoremwas rst proved by McMillan [8].
The following proof is due to Karush [9].
Proof: Let e be a positive integer
_
n
i=1
l
i
_
e
=
n
i
1
=1
n
i
2
=1
n
i
e
=1
(l
i
1
+l
i
2
++l
i
e
)
.
There is a unique term, on the right-hand side, for each of the n
e
messages of
e code-words. Let us denote by N(e, j) the number of messages of e code-words
68 4 Ordered Trees
whose length is j. It follows that
n
i
1
=1
n
i
2
=1
n
i
e
=1
(l
i
1
+l
i
2
++l
i
e
)
=
e
j=e
N(e, j)
j
,
where
l is the maximum length of a code-word. Since C is UD, no two
messages can be equal. Thus, N(e, j)
j
. We now have
e
j=e
N(e, j)
j
j=e
j
j
e
l.
We conclude that for all e 1,
_
n
i=1
l
i
_
e
e
l.
This implies 4.1.
A code C is said to be prex if no code-word is a prex of another. For
example, the code {00, 10, 11, 100, 110} is not prex, since 10 is a prex of
100; the code {00, 10, 11, 010, 011} is a prex. A prex code has no tails and
is therefore UD. In fact, it is very easy to parse the messages: Reading the
messages from left to right, as soon as we read a code-word, we know that
it is the rst code-word of the message, since it cannot be the beginning of
another code-word. Therefore, in most applications, prex codes are used. The
following theorem, due to Kraft [10], in a sense shows us that we do not need
nonprex codes.
Theorem 4.2 If the vector of integers, (l
1
, l
2
, . . . , l
n
), satises
n
i=1
l
i
1, (4.2)
then there exists a prex code C= {c
1
, c
2
, . . . , c
n
} over the alphabet of letters
such that l
i
=l(c
i
).
Proof: Let
1
<
2
< <
m
be integers such that each l
i
is equal to one of
the
j
-s and each
j
is equal to at least one of the l
i
-s. Let k
j
be the number of
4.2 Positional Trees and Huffmans Optimization Problem 69
l
j
-s that are equal to
j
. We have to show that there exists a prex code C such
that the number of code-words of length
j
is k
j
.
Clearly, 4.2 implies that
m
j=1
k
j
j
1 (4.3)
We prove by induction on r that for every 1 r m there exists a prex
code C
r
such that, for every 1 j r, the number of its code-words of
length
j
is k
j
.
First assume that r = 1. Inequality 4.3 implies that k
1
1
1, or k
1
1
.
Since there are
1
distinct words of length
1
, we can assign any k
1
of them
to constitute C
1
.
Now, assume C
r
exists. If r <m then 4.3 implies that
r+1
j=1
k
j
j
1.
Multiplying both sides by
r+1
yields
r+1
j=1
k
j
r
+1
j
r
+1
,
which is equivalent to
k
r+1
r+1
j=1
k
j
r
+1
j
. (4.4)
The number of distinct words of length
r+1
, a prex of which of length
j
is a code-word in C
r
, equals k
j
r
+1
j
. Thus, 4.4 implies that among the
r+1
words of length
r+1
, there are at least k
r+1
words, none of which has a
prex in C
r
. The enlarged set of code-words is C
r+1
.
This proof suggests an algorithm for the construction of a code with a given
vector of code-word length. We return later to the question of prex code
construction, but rst we introduce positional trees.
4.2 Positional Trees and Huffmans Optimization Problem
A positional -tree (or when is known, a positional tree) is a directed tree
with the following property: Each edge out of a vertex v is associated with one
70 4 Ordered Trees
00
0
01
011 100 101
10
1
0
0 0 1
1 1 0
1
Figure 4.1: A positional 2-tree.
of the letters of the alphabet = {0, 1, . . . , 1}; different edges, out of v, are
associated with different letters. It follows that the number of edges out of a
vertex is at most , but may be less; in fact, a leaf has none.
We associate with each vertex v the word consisting of the sequence of letters
associated with the edges on the path fromthe root r to v. For example, consider
the binary tree (positional 2-tree) of Figure 4.1, where the associated word is
written in each vertex. ( denotes the empty word.)
Clearly, the set of words associated with the leaves of a positional tree is a pre-
x code. Also, every prex code canbe described by a positional tree inthis way.
The level of a vertex v of a tree is the length of the directed path fromthe root
to v; it is equal to the length of the word associated with v.
Our next goal is to describe a construction of an optimumcode in a sense that
we discuss shortly. It is described here as a communication problem, as it was
viewed by Huffman [11], who solved it. In the next section, we shall describe
one more application of this optimization technique.
Assume that words over a source alphabet of n letters have to be transmitted
over a channel that can transfer one letter of the alphabet = {0, 1, . . . , 1} at
a time, and <n. We want to construct a code over with n code-words and
associate a code-word with each source letter. A word over the source alphabet
is translated into a message over the code, by concatenating the code-words that
correspond to the source letters in the same order as they appear in the source
word. This message can now be transmitted through the channel. Clearly, the
code must be UD.
4.2 Positional Trees and Huffmans Optimization Problem 71
Assume further that the source letters have given probabilities p
1
, p
2
, . . . p
n
of
appearance and that the choice of the next letter in the source word is indepen-
dent of its previous letters. If the vector of code-word lengths is (l
1
, l
2
, . . . , l
n
),
then the average code-word length, l, is given by
l =
n
i=1
p
i
l
i
. (4.5)
We want to nd a code for which
, satises n
1 mod (1),
and will be equal to one, mod ( 1), from there on. The reason for this
rule is that we should end up with exactly probabilities, each to be assigned
length 1. Now, 1 mod (1), and since in each ordinary step the number
of probabilities is reduced by 1, we want n 1 mod (1). In case this
condition is not satised by the given n, we correct it in the rst step as is
done by our rule. Our next goal is to prove that this indeed leads to an optimum
assignment of a vector of code-word lengths.
Lemma 4.2 If C= {c
1
, c
2
, . . . , c
n
} is an optimum prex code for the probabili-
ties p
1
, p
2
, . . . , p
n
, then p
i
>p
j
implies that l(c
i
) l(c
j
).
Proof: Assume l(c
i
) >l(c
j
). Make the following switch: Assign c
i
to proba-
bility p
j
, and c
j
to p
i
; all other assignments remain unchanged. Let
l denote the
average code-word length of the new assignment, while
l denotes the previous
4.2 Positional Trees and Huffmans Optimization Problem 73
one. By (4.5), we have
l l = [p
i
l(c
i
) +p
j
l(c
j
)] [p
i
l(c
j
) +p
j
l(c
i
)]
= (p
i
p
j
)(l(c
i
) l(c
j
)) >0
contradicting the assumption that
l is minimum.
Lemma 4.3 There exists an optimum prex code for the probabilities p
1
p
2
p
n
such that the positional tree that represents it has the following
properties:
(1) All the internal vertices of the tree, except possibly one internal vertex v,
have exactly sons.
(2) Vertex v has 1 < sons, where n mod (1).
(3) Vertex v of ((1)) is on the lowest level that contains internal vertices, and
its sons are assigned to p
n+1
, p
n+2
, . . . , p
n
.
Proof: Let T be a positional tree representing an optimumprex code. If there
exists an internal vertex u that is not on the lowest level of T containing internal
vertices and it has less than sons, then we can perform the following change
in T: Remove one of the leaves of T from its lowest level and assign to the
probability a new son of u. The resulting tree, and therefore its corresponding
prex code, has a smaller average code-word length. A contradiction. Thus, we
conclude that no such internal vertex u exists.
If there are internal vertices on the lowest level of internal vertices that have
less than sons, choose one of them, say v. Now eliminate sons from v and
attach their probabilities to new sons of the others, so that their number of sons
is . Clearly, such a change does not change the average length, and the tree
remains optimum. If, before lling in all the missing sons, v has no more sons,
we can use v as a leaf and assign to it one of the probabilities from the lowest
level, thus creating a new tree that is better than T. A contradiction. Thus, we
never run out of sons of v to be transferred to other lacking internal vertices
on the same level. Also, when this process ends, v is the only lacking internal
vertex (proving (1)), and its number of remaining sons must be greater than
one, or its son can be removed and its probability attached to v. This proves
that the number of sons of v, , satises 1 < .
If vs sons are removed, the new tree has n
, satises n
(p
1
, p
2
, . . . , p
n
) be the set of all -ary positional trees with nleaves, assigned
with the probabilities p
1
, p
2
, . . . , p
n
in such a way that p
nd+1
, p
nd+2
, . . . , p
n
(see Equation 4.6) are assigned, in this order, to the rst d sons of a vertex v,
which has no other sons. By Lemma 4.3,
(p
1
, p
2
, . . . , p
n
) contains at least
one optimum tree. Thus, we may restrict our search for an optimum tree to
(p
1
, p
2
, . . . , p
n
).
Lemma 4.4 There is a one to one correspondence between
(p
1
, p
2
, . . . , p
n
)
and the set of -ary positional trees, with n d + 1 leaves assigned with
p
1
, p
2
, . . . , p
nd
, p
, where p
=
n
i=nd+1
p
i
. The average word-length
l of the
prex code, represented by a tree T of
(p
1
, p
2
, . . . , p
n
), and the average code-
word-length
l
, which corresponds
to T, satisfy
l =
l
+p
. (4.7)
Proof: The tree T
to it.
It is easy to see that two different trees T
1
and T
2
in
(p
1
, p
2
, . . . , p
n
) will
yield two different trees T
1
and T
2
, and that every -ary tree T
with nd+1
leaves assigned p
1
, p
2
, . . . , p
nd
, p
l =
nd
i=1
p
i
l
i
+l
n
i=nd+1
p
i
=
nd
i=1
p
i
l
i
+l
n
p
=
nd
i=1
p
i
l
i
+(l
n
1) p
+p
=
l
+p
.
4.3 Application of the Huffman Tree to Sort-by-Merge Techniques 75
Lemma 4.4 suggests a recursive approach to nd an optimum T. For
l to be
minimum,
l
, and then
nd T by attaching d sons to the vertex of T
assigned p
i=1
a
i
l
i
. (4.8)
Burge [15] observed that the attempt to nd a positional m-ary that mini-
mizes (4.8) is similar to that of the minimum average word-length problem
solved by Huffman. The fact that the Huffman construction is in terms of prob-
abilities does not matter, since the fact that p
1
+p
2
+ +p
L
= 1 is never
used in the construction or its validity proof. Let us demonstrate the implied
procedure by the following example:
4.4 Catalan Numbers 77
9 8 8 7 6 5 5 4 6 6 3 3
1 2 2 2 2 2 2 2 2 2 2 2
10 9 8 8 7 6 6 6 5 5
1 1 2 2 2 2 2 2 2 2
22 10 9 8 8 7 6
1 1 1 2 2 2 2
29 22 10 9
1 1 1 1
Figure 4.4: An example of sort-by-merge for L = 12 and m= 4.
29 22 10
70
9
8 8 7 6 6 6 5 5 4 3 3
Figure 4.5: The positional tree describing the example in Figure 4.4.
Assume L = 12 and m = 4; the b
i
s are given in nonincreasing order:
9, 8, 8, 7, 6, 6, 6, 5, 5, 4, 3, 3. Since L 0(mod 3), according to (4.6), d =3.
Thus, in the rst step we merge the last three lists to form a list of length 10,
which is now put in the rst place (see Figure 4.4). From there on, we merge
each time the four lists of least length. The whole merge procedure is described
in the tree shown in Figure 4.5.
4.4 Catalan Numbers
The set of well-formed sequences of parentheses is dened by the following
recursive denition:
(1) The empty sequence is well formed.
(2) If A and B are well-formed sequences, so is AB (the concatenation of A
and B).
(3) If A is well formed, so is (A).
78 4 Ordered Trees
(4) There are no other well-formed sequences.
For example, (()(())) is well formed; (()))(() is not.
Lemma 4.5 A sequence of (left and right) parentheses is well formed if and
only if it contains an even number of parentheses, half of which are left, and the
other half, right. Also as we read the sequence from left to right, the number of
right parentheses never exceeds the number of left parentheses.
Proof: First let us prove the only if part. Since the construction of every well-
formed sequence starts with no parentheses (the empty sequence), and each time
we add on parentheses (Step 3) there is one left and one right, it is clear that
there are nleft parentheses and nright parentheses. Now, assume that for every
well-formed sequence of m left and m right parentheses where m < n, it is
true that as we read it from left to right the number of right parentheses never
exceeds the number of left parentheses. If the last step in the construction of
our sequence was (2), then since Ais a well-formed sequence, as we read from
left to right, as long as we still read A, the condition is satised. When we are
between Aand B, the count of left and right parentheses equalizes. From there
on, the balance of left and right is safe, since B is well formed and contains less
than n parentheses. If the last step in the construction of our sequence was (3),
since A satises the condition, so does (A).
Now, we shall prove the if part, again by induction on the number of paren-
theses. (Here, as before, the basis of the induction is trivial.) Assume that the
statement holds for all sequences of mleft and mright parentheses, if m<n,
and we are given a sequence of n left and n right parentheses that satises
the condition. Clearly, if after reading 2m symbols of it from left to right, the
number of left and right parentheses is equal, and if m< n, then this subse-
quence, A, by the inductive hypothesis, is well formed. Now, the remainder
of our sequence, B, must satisfy the condition, too, and again by the inductive
hypothesis is well formed. Thus, by Step (2), AB is well formed. If there is no
such nonempty subsequence A, which leaves a nonempty B, then as we read
from left to right, the number of right parentheses, after reading one symbol
and before reading the whole sequence, is strictly less then the number of left
parentheses. Thus, if we delete the rst symbol, which is a (, and the last,
which is a ), the remainder sequence, A, still satises the condition, and
by the inductive hypothesis, is well formed. By Step (3) our sequence is well
formed too.
4.4 Catalan Numbers 79
We shall now show a one-to-one correspondence between the non-well-
formed sequences of n left and n right parentheses, and all sequences of n1
left parentheses and n+1 right parentheses.
Let p
1
p
2
p
2n
be a sequence of n left and n right parentheses that is not
well formed. By Lemma 4.5, there is a prex of it that contains more right
parentheses than left parenthesis. Let j be the least integer such that the number
of right parentheses exceeds the number of left parentheses in the subsequence
p
1
p
2
p
j
. Clearly, the number of right parentheses is then one larger than the
number of left parentheses, or j is not the least index to satisfy the condition.
Now, invert all p
i
s for i >j fromleft parentheses to right parentheses, and from
right parentheses to left parentheses. Clearly, the number of left parentheses is
now n1, and the number of right parentheses is now n+1.
Conversely, given any sequence p
1
p
2
p
2n
of n 1 left parentheses and
n+1 right parentheses, let j be the rst index such that p
1
p
2
p
j
contains one
right parenthesis more than left parentheses. If we nowinvert all the parentheses
in the section p
j+1
p
j+2
p
2n
from left to right and from right to left, we get
a sequence of n left and n right parentheses which is not well formed. This
transformation is the inverse of the one in the previous paragraph. Thus, the
one-to-one correspondence is established.
The number of sequences of n1 left and n+1 right parentheses is
_
2n
n1
_
,
for we can choose the places for the left parentheses, and the remaining places
will have right parentheses. Thus, the number of well-formed sequences of
length n is
_
2n
n
_
_
2n
n1
_
=
1
1 +n
_
2n
n
_
. (4.9)
These numbers are called Catalan numbers.
An ordered tree is a directed tree such that for each internal vertex there is
a dened order of its sons. Clearly, every positional tree is ordered, but the
converse does not hold: In the case of ordered trees there are no predetermined
potential sons; only the order of the sons counts, not their position, and there
is no limit on the number of sons.
An ordered forest is a sequence of ordered trees. We usually draw a forest
with all the roots on one horizontal line. The sons of a vertex are drawn from
left to right in their given order. For example, the forest shown in Figure 4.6
consists of three ordered trees whose roots are A, B, and C.
80 4 Ordered Trees
B A C ((( )( ))) (( )( )( )) ((( )( ))( ))
(( )( )) (( )( ))
( ) ( ) ( ) ( )
( ) ( ) ( ) ( )
Figure 4.6: An example of three ordered trees.
There is a natural correspondence between well-formed sequences of n pairs
of parentheses and ordered forests of n vertices. Let us label each leaf with
the sequence (). Every vertex whose sons are labeled w
1
, w
2
, . . . , w
s
is labeled
with the concatenation (w
1
w
2
w
s
); clearly, the order of the labels is in the
order of the sons. Finally, once the roots are labeled x
1
, x
2
, . . . , x
r
, the sequence
corresponding to the forest is the concatenation x
1
x
2
x
n
. For example, the
sequence correspondingto the forest of Figure 4.6 is ((()())())(()()())((()())). The
inverse transformation clearly exists, and thus the one-to-one correspondence
is established. Therefore, the number of ordered forests of n vertices is given
by (4.9).
We now describe a one-to-one correspondence between ordered forests and
positional binary trees. The leftmost root of the forest is the root of the binary
tree. The leftmost son of the vertex in the forest is the left son of the vertex in
the binary tree. The next brother on the right, or, in the case of a root, the next
root on the right is the right son in the binary tree. For example, see Figure 4.7,
where an ordered forest and its corresponding binary tree are drawn. Again, it
is clear that this is a one-to-one correspondence, and therefore the number of
positional binary trees with n vertices is given by (4.9).
There is yet another combinatorial enumeration that is directly related to
these.
A stack is a storage device that can be described as follows. Suppose that n
cars travel on a narrow one-way street where no passing is possible. This leads
into a narrow two-way street on which the cars can park or back up to enter
another narrowone-way street (see Figure 4.8). Our problemis to nd howmay
4.4 Catalan Numbers 81
A
D E
J K
J
K
E
D
A
F
B
G
H L
M
I
C
F G H
B C
I
L M
Figure 4.7: An ordered forest and its corresponding binary tree.
Figure 4.8: An example of a stack of cars.
permutations of the cars can be realized from input to output if we assume that
the cars enter in the natural order.
The order of operations in the stack is fully described by the sequence of
drive-in and drive-out operations. There is no need to specify which car drives
in, for it must be the rst one on the leading-in present queue; also, the only one
that can drive out is the top one in the stack. If we denote a drive-in operation
by (, and a drive-out operation by ), the whole procedure is described by a
well-formed sequence of n pairs of parentheses.
The sequence must be well formed, by Lemma 4.5, since the number of drive-
out operations can never exceed the number of drive-in operations. Also, every
well-formed sequence of n pairs of parentheses denes a realizable sequence
of operations, since, again by Lemma 4.5, a drive-out is never instructed when
82 4 Ordered Trees
the stack is empty. Also, different sequences yield different permutations. Thus,
the number of permutations on n cars realizable by a stack is given by (4.9).
Let us now consider the problem of nding the number of full binary trees.
Denote the number of leaves of a binary tree T by L(T), and the number of
internal vertices by I(T). It is easy to prove, by induction on the number of
leaves, that L(T) =I(T) +1. Also, if all leaves of T are removed, the resulting
tree of I(T) vertices is a positional binary tree T
Two vertices s and t are specied; s is called the source; and t, the sink.
2
e(v)
f(e) =
e(v)
f(e).
The total ow F, of f in N, is dened by
F =
e(t)
f(e)
e(t)
f(e) . (5.1)
Namely, F is the net sum of ow into the sink.
1
Self-loops are useless in this context, and parallel edges can be replaced with one edge whose
capacity is the sum of the capacities of the parallel edges.
2
The source is not necessarily a graphical source; i.e., it may have incoming edges. Similarly,
the sink is not necessarily a graphical sink.
85
86 5 Flow in Networks
In the next two sections, we shall discuss methods for computing a ow
function f for which F is maximum.
Given a set S V, let
S =V \S. In the following, we shall discuss sets S, such
that s S and t
S. Also, (S;
S;S) is similarly dened, and is called a backward cut. The union of (S;
S)
and (
e(S;
S)
f(e)
e(
S;S)
f(e) . (5.2)
Proof: By the vertex rule, for every v (V \ {s, t}),
0 =
e(v)
f(e)
e(v)
f(e) . (5.3)
Also, consider again Equation 5.1:
F =
e(t)
f(e)
e(t)
f(e) .
Now, add the equations, as in Equation 5.3, for every v (
S \ {t}), as well as
Equation 5.1. The aggregate equation has F on the l.h.s.
3
In order to see what happens on the r.h.s., consider an edge x
e
y. If both
x and y belong to S then f(e) does not appear on the r.h.s. of the aggregate
equation at all, in agreement with Equation 5.2. If both x and y belong to
S
then f(e) appears twice on the r.h.s. of the aggregate equation; once positively,
in the equation for y, and once negatively, in the equation for x. Thus, it is
canceled out in the summation, again in agreement with Equation 5.2. If x S
and y
S then f(e) appears on the r.h.s. of the aggregate equation, as part of
Equation 5.3 for y, positively, and in no equation for other vertices included
in the summation. In this case e (S;
S;S).
Let us denote by c(S) the capacity of the cut determinedby S, which is dened
as follows:
c(S) =
e(S;
S)
c(e) . (5.4)
Lemma 5.2 For every ow function f, with total ow F, and every {s} S
(V \ {t}), the following inequality holds:
F c(S) . (5.5)
Proof: By Lemma 5.1,
F =
e(S;
S)
f(e)
e(
S;S)
f(e) .
By the edge rule, for every edge e, 0 f(e) c(e). Thus,
F
e(S;
S)
c(e) 0 .
Finally, by Equation 5.4, F c(S).
The following is a very important corollary of Lemma 5.2. It allows us to
detect that a given total ow F is maximum, and the capacity of a given cut,
dened by S, is minimum.
Corollary 5.1 If F and S satisfy Equation 5.5 by equality, then F is maximum
and the cut dened by S is of minimum capacity.
5.2 The Algorithm of Ford and Fulkerson
Ford and Fulkerson [7] suggested the use of augmentingpaths to change a given
ow function f in order to increase the total ow. A procedure for nding an
augmenting path, if one exists, is used. If an augmenting path is found, then it is
used to increase the total ow. If no augmenting path is found, then the present
ow is declared maximum and the process terminates.
If the direction of the edges is ignored, an augmenting path P is a simple path
froms to t. It is used to add a quantity >0 to the total ow by pushing along
88 5 Flow in Networks
P additional units of ow. To do this, if an edge e of P is directed in the
direction froms to t, c(e) f(e) + must hold. But if e of P is directed in the
opposite direction, we must be able to reduce its ow from f(e) to f(e) .
Thus, f(e) must hold.
In attempt to nd an augmenting path for a given ow, a labeling procedure is
used. We rst label s. Then, as long as there is an unlabeled vertex v for which
an augmenting path from s to v is found, v is labeled. If t is labeled, then an
augmenting path has been found and the labeling process is terminated.
Every vertex v is assigned a label (v), which is one of the following:
(s) =; only vertex s gets this label, and it is the only label s gets.
(v) = (e, ), where e is the edge through which v has been assigned its label,
= + if e has been used forwardly and = if e has been used backwardly.
An edge u
e
v is said to be useful from u to v if (u) ,=NIL, (v) =NIL
and one of the following conditions holds:
u
e
v and f(e) <c(e). In this case, we say that e is forwardly useful.
v
e
u and f(e) >0. In this case, we say that e is backwardly useful.
Algorithm 5.1 describes the labeling procedure, named LABEL. The input
of LABEL consists of the network N and the present ow function f. () is
dened for all vertices. The procedure may modify these labels. Thus, () is
both an input and output. The output of LABEL includes a function (), which
is dened on a subset of E, and its value is a positive real number.
Procedure LABEL(N, f, , )
1 while there is an edge u
e
v which is useful from u to v and
(t) =NIL do
2 if e is forwardly useful then do
3 (v) (e, +)
4 (e) c(e) f(e)
5 if e is backwardly useful then do
6 (v) (e, )
7 (e) f(e)
Algorithm 5.1: The labeling procedure.
5.2 The Algorithm of Ford and Fulkerson 89
Procedure AUGMENT(G, , , f)
1 empty Q
2
3 v t (v is called the center of activity)
4 while v ,=s do
5 put (v) into Q
6 let (v) = (e, )
7 min{, (e)}
8 let e be u
e
v
9 v u
10 while Q is not empty do
11 remove the rst label, (e, ), from Q
12 if = + then do
13 f(e) f(e) +
14 else ( = ) do
15 f(e) f(e)
Algorithm 5.2: The augmenting procedure.
If (t) ,= NIL when LABEL is terminated, then an augmenting path exists.
The augmenting path is found in the AUGMENT procedure and used to change
f and thus, increment F. This is presented in Algorithm 5.2.
The input to AUGMENTconsists of G(V, E), () and (). The owfunction
f is both an input and an output. A queue Q of vertex labels is used, as well as
a real these variables are internal.
The Ford and Fulkerson procedure is described in Algorithm 5.3 and uses
LABEL and AUGMENT as subroutines. Initially, f is any legitimate ow in
the network N, and in the absence of better ideas, one can use f(e) = 0 for
every edge e. Thus, the input is N and f is both an input and an output.
Note that, if for an edge e (s), initially f(e) =0, then f(e) is never changed.
The same is true for an edge e (t).
As an example, consider the networks shown in Figure 5.1. Next to each edge
e we write c(e), f(e), in this order. We assume a zero initial ow everywhere.
A rst wave of label propagation might be as follows: s is labeled, e
2
is used to
label c, e
6
is used to label d, e
4
is used to label a, e
3
used to label b and nally,
90 5 Flow in Networks
Procedure FORD-FULKERSON(N, f)
1 for every v V do
2 (v) NIL
3 (s)
4 call LABEL(N, f, , )
5 while (t) ,=NIL do
6 call AUGMENT(G, , , f)
7 for every v V \ {s} do
8 (v) NIL
9 call LABEL(N, f, , )
10 print The present ow f is maximum
Algorithm 5.3: The Ford-Fulkerson procedure.
s
a b
c d
t
15,0
12,0
7,0
4,0
10,0
5,0
10,0
3,0
e
1
e
3
e
6
e
4
e
7
e
8
e
2
e
5
Figure 5.1: An example of a network.
e
7
is used to label t. The path is
s
e
2
c
e
6
d
e
4
a
e
3
b
e
7
t ,
= 4, and the new ow is shown in Figure 5.2. The second augmenting path
may be
s
e
1
a
e
3
b
e
5
c
e
6
d
e
8
t ,
= 3 and the new ow is shown in Figure 5.3. The third augmenting path
may be
s
e
1
a
e
3
b
e
7
t ,
=3 and the newowis shown in Figure 5.4. Up to nowonly forward labeling
5.2 The Algorithm of Ford and Fulkerson 91
s
a b
c d
t
15,0
12,4
7,4
4,4
10,4
5,4
10,0
3,0
e
1
e
3
e
6
e
4
e
5
e
7
e
8
e
2
Figure 5.2: The example after the rst augmenting path.
s
a b
c d
t
15,3
12,7
7,4
4,4
10,7
5,4
10,3
3,3
e
1
e
3
e
6
e
4
e
5
e
7
e
8
e
2
Figure 5.3: The example after the second augmenting path.
s
a b
c d
t
15,6
12,10
7,7
4,4
10,7
10,3
3,3
5,4
e
1
e
3
e
6
e
4
e
5
e
7
e
8
e
2
Figure 5.4: The example after the third augmenting path.
has been used. In the next application of LABEL, we can still label vertex a
via e
1
; and vertex b, via e
3
, both of which are forward labeling, but no further
forward labeling exists. However, e
4
is useful backwardly, and through it one
92 5 Flow in Networks
s
a b
c d
t
12,10
7,7
10,7
10,7
5,0
3,3
15,10
4,4
e
1
e
3
e
6
e
4
e
5
e
7
e
8
e
2
Figure 5.5: The example after the fourth augmenting path.
can label d. Now one can use e
8
to label t. The augmenting path is
s
e
1
a
e
4
d
e
8
t ,
= 4, and the new ow is shown in Figure 5.5. Now, the total ow, F, is equal
14. The next application of LABEL does not reach t. The set of labeled vertices
S is {s, a, b} and the forward cut, (S;
S;S),
consists of one edge, e
4
, and its ow is 0. Thus, F =c(S), and by Corollary 5.1,
F is maximum and c(S) is minimum.
It is easy to see that the ow produced by the algorithm remains legitimate
throughout. The denitions of () and guaranty that forwardly used edges
will not be overowed, that is, f(e) c(e), and that backward edges will not
be underowed, that is, f(e) 0. Also, since units of ow are are pushed
froms to t on a path, the incoming ow will remain equal to the outgoing ow
in every vertex v V \ {s, t}.
Assuming the Ford and Fulkerson procedure halts, the last labeling process
has not reached t. As above, let S be the set of vertices labeled in the last
application of the labeling process. If e (S;
e(S;
S)
f(e)
e(
S;S)
f(e) =
e(S;
S)
c(e)
e(
S;S)
0 =c(S) .
By Corollary 5.1, F is maximum and c(S) is minimum.
The question of whether the Ford and Fulkerson procedure will always halt
remains to be discussed. Note rst a very important property of the procedure:
5.2 The Algorithm of Ford and Fulkerson 93
If the initial ow is integral, for example, zero everywhere, and if all capacities
are integers, then the algorithm never introduces fractions. The algorithm adds
and subtracts, but it never divides. Also, if t is labeled, the resulting augmenting
path is used to increase the total owby at least one unit. Since there is an upper
bound on the total ow (any cut), the process must terminate.
Ford and Fulkerson showed that their procedure may fail if the capacities are
allowed to be irrational numbers. Their counterexample ([7, p. 21]) displays an
innite sequence of owaugmentations. The owconverges (in innitely many
steps) to a value which is one-fourth of the maximum total ow. We shall not
present their example here; it is fairly complex, and as the reader will shortly
discover, it is not as important any more.
One could have arguedthat, for all practical purposes, we may assume that the
algorithm is sure to halt. This follows from the fact that our computations are
usually through a xed radix (decimal, binary, etc.) number representation with
a bound on the number of digits used; in other words, all gures are multiples
of a xed quantumand the termination proof works here as it does for integers.
However, a simple example shows the weakness of this argument. Consider the
network shown in Figure 5.6.
Assume that M is a very large integer. If the algorithm starts with f(e) = 0
for every e, and alternately uses
s a b t
and
s b a t
a
b
s t
M M
M M
1
Figure 5.6: An example which demonstrating the possibility that the
Ford and Fulkerson procedure may be inefcient.
94 5 Flow in Networks
as augmenting paths, it will use 2Maugmentations before F =2Mis achieved.
This is exponential time in terms of the length of the input data, since it takes
only ,log
2
(M+1)| bits to represent M.
Edmonds and Karp [4] were rst to overcome this problem. They showed
that if Breadth-First Search (BFS) is used in the labeling algorithm and thus,
every augmentingpath is a shortest one, the algorithmterminates in O(|V| |E|
2
)
time, regardless of the capacities. (Here, of course, we assume that our com-
puter can handle, in one step, any real number.) In the next section we shall
present the more advanced work of Dinitz (see [2]); his algorithm has time
complexity O(|V|
2
|E|). A signicantly more efcient algorithm was pre-
sented by Goldberg and Tarjan [9], but this algorithm is not described in this
book.
The existence of the algorithmof Edmonds and Karp, or that of Dinitz, assures
that if one proceeds according to a proper strategy in the labeling procedure,
the algorithmis guaranteed to halt (in polynomial time). When it does, the total
ow is maximum, and the cut indicated is minimum, thus providing the max
ow min-cut theorem:
Theorem 5.1 Every network has a maximum total ow which is equal to the
capacity of a cut for which the capacity is minimum.
5.3 The Dinitz Algorithm
As in the Ford and Fulkerson algorithm, the Dinitz algorithm
4
starts with some
legitimate ow function f and improves it. When no improvement is possible
the algorithm halts, and the total ow, F, is maximum.
Given a network N(G(V, E), s, t, c) and a ow function f, let us dene the
secondary network N
(G
(V, E
), s, t, c
) as follows:
. Also, c
For every e E, a
e
b such that f(e) >0, there is an edge b
e
a in E
.
Also, c
(e
, since it is useful both in the forward and the backward directions. Thus,
|E| |E
| 2|E|.
4
In [3], Yem Dinitz tells the story of his algorithm, and the differences between his version and
the one presented here (G.E.).
5.3 The Dinitz Algorithm 95
Procedure LAYER(N
, V
i
, vertex labels, T)
1 T
2 while there is an edge u v in N
, u V
i
and v is not labeled, do
3 T T {v}
4 label v
Algorithm 5.4: Finding the next layer in the BFS of N
.
Before we go into a detailed description of the Dinitz algorithm, here is an
abstract of its structure:
The Dinitz algorithm proceeds in phases. In each phase the current f is used
to produce the corresponding secondary network N
. A layered network, N
,
is then produced by a BFS on N
, is found in N
. The ow f is then
augmented, by using f
, fromN
(G
(V
, E
), s, t, c
) is
constructed as follows:
=
l
i=0
V
i
,
i
= {u
e
v | u V
i
, v V
i+1
, e
},
=
l1
i=0
E
i
,
for every e
(e
) =c
(e
).
96 5 Flow in Networks
Procedure LAYERS(N
;V
0
, V
1
, . . . V
n1
, l)
Local variables: T a set of vertices, vertex labels.
1 label every v V unlabeled
2 label s
3 V
0
{s}
4 i 0
5 repeat
6 call LAYER(N
, V
i
, vertex labels
, T)
7 i i +1
8 V
i
T
9 until t T or T =
10 if t T then do
11 l i
12 V
l
{t}
13 else (T =) declare: f is a maximum ow.
Algorithm 5.5: Finding the BFS Layers of N
.
s t
a b
c d
1,1
1,1
1,1
1,0
1,0
1,0
1,0
V
0
V
1
V
2
V
3
Figure 5.7: An example of a maximal ow in a layered network that
is not maximum.
Next, we construct a maximal, or blocking ow in N
. A maximal ow f
is a legitimate ow in N
, blocking-labels, S)
1 empty S
2 push (NIL, s) into S
3 while S is not empty and t is not the right element in the top pair of S do
4 let (x, v) be the top pair on S
5 if there is an unblocked edge v
e
, then do
6 push (e
, v
) into S
7 else do
8 if x ,=NIL then label x blocked
9 pop (x, v) from S
Algorithm 5.6: Finding an augmenting path of length l in N
.
for which the total is 2. Yet it is maximal, since on every directed path of length
3 from s to t, there is at least one saturated edge.
It is easier to nd maximal ow than maximum ow, since the ow in edges
never has to decrease. In other words, no backward labeling of vertices is
necessary.
The procedure MAXIMALto nd a maximal owin a given layered network
N
starts with f
(e
) = 0 for every e
to blocked.
FIND-PATH is described in Algorithm 5.6. Its input is N
. The blocking-
labels are both an input and an output, and the stackSis anoutput. The procedure
uses a DFS strategy. As long as there is an unblocked edge to continue on, we
keep going, and store the sequence of the edges and their end-points in a stack
S. Thus, S is a stack of pairs, such as (e, v), where v is the vertex that e enters.
If t is reached, the sequence of edges stored in S is a directed augmenting path
from s to t of length l. If there are no unblocked edges out of the vertex we
have reached, we pop the top edge (and its endpoint) from S, change its label
to blocked and backtrack to the previous vertex on the path. If we are stuck
at s, the search terminates with no edges in S.
The procedure INCREASE-FLOW is described in Algorithm 5.7. The pro-
cedure uses two local variables: S
, S, f
, blocking-labels)
1 empty S
2
3 while the pair on top of S is not (NIL, s) do
4 pop the top pair (e, v) from S
5 min{, c
(e) f
(e)}
6 push e into S
7 while S
is not empty do
8 pop the top edge e from S
9 f
(e) f
(e) +
10 if f
(e) =c
.
N
. In the second scan (Lines 710), the ow in every edge of the augmenting
path is increased by , and if an edge becomes saturated, its label is changed
from unblocked to blocked. Clearly, at least one edge on the path becomes
saturated.
The procedure MAXIMAL described in Algorithm 5.8 nds a maximal ow,
f
, N
, are
internal variables.
Lemma 5.3 If procedure DINITZ halts, the resulting f is a legitimate ow
in N.
Proof: First, observe that in each phase the ow f
is legitimate in N
. The
edge rule is observed since is chosen in INCREASE-FLOW in a way that
5.3 The Dinitz Algorithm 99
Procedure MAXIMAL(N
, f
)
1 for every e E
do
2 label e unblocked
3 f
(e) 0
4 run FIND-PATH(N
, blocking-labels, S)
5 while S is not empty then do
6 run INCREASE-FLOW(N
, S, f
, blocking-labels)
7 run FIND-PATH(N
, blocking-labels, S)
Algorithm 5.8: Constructing a maximal ow in a layered network N
.
Procedure DINITZ(N;f)
1 for every e E do
2 f(e) = 0
3 N
N
4 run LAYERS(N
;V
0
, V
1
, . . . , V
n1
, l)
5 while f has not been declared to be maximum do
6 construct N
7 run MAXIMAL(N
;f
)
8 for every e
do
9 if e
(e
)
11 else (u
e
v N
, but v
e
u N) do
12 f(e) f(e) f
(e
)
13 construct N
from (N, f)
14 run LAYERS(N
;V
0
, V
1
, . . . , V
n1
, l)
Algorithm 5.9: The Dinitz algorithm.
ensures that no edge is overowed. The vertex rule is observed, since the ow
is built up by augmenting paths.
By the denition of c
and thus, c
to (from) the previous f, the edge rule is maintained in the edges of G. Also,
100 5 Flow in Networks
since f is the superposition of two ows, each of which observes the vertex rule,
so does their sum.
Lemma 5.4 If procedure DINITZ halts, the resulting f is a maximum ow
in N.
Proof: The proof here is very similar to the one in the Ford and Fulkerson
algorithm. Consider the last phase, in which vertex t is not reached in procedure
LAYERS (see Line 13). Let S be the union of V
0
, V
1
, . . . , V
i
, where V
i
is the
last (nonempty) layer before T = is encountered. Nowconsider the cuts (S;
S)
and (
is maximal in N
.
Proof: Initially, all edges of N
.
Note that in Algorithm5.6 we traverse edges (see Lines 56). If we backtrack
to a vertex other than s (Lines 79), an edge is marked blocked.
The number of consecutive edge traversals between two blockings of edges
is bounded by l for the following reasons:
It is possible that we have backtracked into a vertex other than s. In that case,
in less than l consecutive traversals, either another backtrack occurs or t is
reached, and as in the previous case, at least one edge is marked blocked.
Since the number of edges in N
of phase k was not maximal. This proves the claim of the lemma in the case
that all vertices of P are in the k-th layered network.
If not all the vertices of P appear in the kth layered network, then let v
a
e
a+1
v
a+1
be the rst edge of P such that for some b, v
a
V
b
, but v
a+1
is not in the
102 5 Flow in Networks
k-th layered network. Thus, e
a+1
was not used in phase k. Since e
a+1
is useful
in the beginning of phase k+1, it was also useful in the beginning of phase k.
The only possible reason for v
a+1
not to belong to V
b+1
is that b+1 =l
k
and
v
a+1
,=t. Since t =v
l
k+1
, it follows that a+1 <l
k+1
. By the argument of the
previous paragraph, a b. Thus, l
k
=b+1 a+1 <l
k+1
.
Corollary 5.2 The number of phases is bounded by |V|.
Proof: Clearly, l
1
1. By Lemma 5.7, for the k-th phase, if it is not the last,
l
k
k. Since k l
k
|V| 1, the number of phases, including the last, is
bounded by |V|.
Theorem 5.2 The Dinitz algorithm terminates in time O(|V|
2
|E|) and yields a
maximum ow.
Proof: By Corollary 5.2, the number of phases is bounded by |V|. By
Lemma 5.6, each phase requires O(|V| |E|) time. Thus, the whole algo-
rithm takes O(|V|
2
|E|) time to terminate. By Lemma 5.4, the resulting ow
is maximum in N.
Theorem 5.3 (The max-ow min-cut theorem) Every nite network has a
maximum ow and a minimum cut, and their values are equal.
Proof: By Theorem 5.2 there is a max-ow, and by the proof of Lemma 5.4,
there is a cut of the same value. By Corollary 5.1, this cut is of minimum
value.
5.4 Networks with Upper and Lower Bounds
In the previous sections, we have assumed that the owin each edge is bounded
from above by the capacity of the edge, but that the lower bound on the ow in
every edge is zero. The signicance of this assumption is that the assignment
of f(e) = 0, for every edge e, denes a legitimate ow, and the algorithm for
improving the ow can be started with this zero ow.
In this section, in addition to the upper bound c(e) on the owin e, we assume
that the owis also boundedfrombelowby b(e). Thus, the edge rule is changed
as follows: The ow f(e), in every edge e, must satisfy
b(e) f(e) c(e). (5.6)
The vertex rule remains unchanged.
Thus, the problem of nding a maximum ow in a given network
N(G(V, E), s, t, b, c) is divided into two subproblems. First, check whether N
5.4 Networks with Upper and Lower Bounds 103
s t a
0,1 2,3
Figure 5.8: An example of a network which has no legitimate ow.
has legitimate ows, and if the answer is positive, nd one. Second, increase
this initial ow and nd a maximum ow.
A simple example of a network that has no legitimate ow is shown in
Figure 5.8. Here, next to each edge e we write b(e), c(e).
The following method for testing whether a given network Nhas a legitimate
ow function is due to Ford and Fulkerson [7]. It reduces the problem to one
of determining a maximum ow in an auxiliary network
N(
G(
V,
E), s,
t, c), in
which all lower bounds are zero. Here, s is called the auxiliary source, and
t
is called the auxiliary sink. In the auxiliary ow problem, s and t are not the
source and the sink anymore and must satisfy the vertex rule. We shall show
that the original N has legitimate ows if and only if the maximum ow in
N
satises a condition to be specied shortly.
N is dened as follows:
(i)
V =V { s,
, e
}, where o, T and {e
, e
t | v V},
and s
e
t and t
e
s.
(iii) c :
ER
0
{} is dened separately for each of the four sets of edges:
For e E,
c(e) =c(e) b(e). (5.7)
For s
v, c() =
e(v)
b(e).
For v
t, c() =
e(v)
b(e).
c(e
) =and c(e
) =.
Let us demonstrate this construction on the network shown in Figure 5.9. The
auxiliary network is shown in Figure 5.10. The upper bounds are shown next
to the edges to which they apply.
Now we can use the Ford and Fulkerson or the Dinitz algorithm to nd a
maximumow in the auxiliary network. It is left to the reader to verify that the
104 5 Flow in Networks
s t
w x
y z
5,7
1,3 3,5
0,10 2,6
2,4
1,3
2,8
Figure 5.9: An example of a network for which we want to determine
whether it has a legitimate ow.
s t
w x
y z
1 2 4 4 0
1 0 3 5 5 2
2
2
2 2
2
6
10 4
5
s
e(v)
f(e) +
f() =
e(v)
f(e) +
f() . (5.11)
By the assumption, and are saturated. Thus,
e(v)
f(e) + c() =
e(v)
f(e) + c() .
By the denitions of c() and c(), it follows that
e(v)
f(e) +
e(v)
b(e) =
e(v)
f(e) +
e(v)
b(e) .
By Equation 5.8 and merging the sums on each side of the equation, one gets
e(v)
f(e) =
e(v)
f(e) , (5.12)
106 5 Flow in Networks
and the vertex rule is proved. Thus, f is legitimate in N.
Next, we prove the necessity of the condition. Assume there is a legitimate
ow f in N. Let us show that there is a maximum ow
f in
N for which all
edges of o are saturated.
The steps of the previous proof are reversible, with minor modications. For
an edge e E, use Equation 5.8 to dene
f(e). For an edge o, dene
) =F and
f(e
) = 0
and
f(e
) = F.
To show that
f observes the edge rule, all we need to do is show it for e E;
obviously, in the remaining edges,
f is dened in a way which satises the edge
rule. Since f is legitimate in N, it satises Equation 5.10. By Equations 5.8 and
5.7, one concludes that Equation 5.9 holds.
To show that
f observes the vertex rule, rst consider a vertex v V \ {s, t}.
Since f observes the vertex rule in N, Equation 5.12 holds. By reversing the
steps of the proof above, one gets Equation 5.11, which implies that v satises
the vertex rule.
Finally, for vertex s,
f satises the vertex rule for the following reasons. In N,
s may not be balanced. If F >0, then the total outgoing ow is F units greater
than the total incoming ow. The total outgoing ow in
Nin edges of (s) and
in is as it is in N, and the total incoming ow in
N in edges of (s) and in
is also as in N. However,
f(e
t
, 5
, 0
Figure 5.11: Amaximumowin the auxiliary network of the example
network.
s t
y z
w x
1,3,3
5,7,5
3,5,3
2,4,2
1,3,2
0,10,2
2,6,2
2,8,4
Figure 5.12: A legitimate ow in the original example network.
(i) u is labeled and v is not,
(ii) f(e) >b(e).
The label that v gets is (e, ). In this case, Line 7 of the procedure LABEL (see
Algorithm 5.1) is changed to
(e) f(e) b(e).
108 5 Flow in Networks
With this exception, the algorithm is exactly as described in Section 5.2. The
proof that the ow is maximum when the algorithm terminates is similar, but
we need to redene the capacity of a cut determined by S (see Equation 5.4) as
follows:
c(S) =
e(S;
S)
c(e)
e(
S;S)
b(e) .
It is easy to prove that a statement just like Lemma 5.2 still holds; namely,
F c(S) .
Now, the set of labeled vertices S, when the algorithm terminates, satises this
inequality by equality. Thus, the ow is maximum, and the indicated cut is
minimum.
Clearly, the Dinitz algorithm can also be used. It is left as an exercise for the
reader to make the necessary changes.
In certain applications, what we want is a minimum ow, that is, a legitimate
ow function f for which the total ow F is minimum. Consider a network
N(G(V, E), b, c), where function b : E R species the lower bounds on the
ow in the edges, and c : E R species the upper bounds on the ow in the
edges. Let s, t V be any two vertices. Denote by f : E R a legitimate ow
function of the network (G(V, E), s, t, b, c), where s plays the role of the source
and t plays the role of the sink. Also, let F
s,t
be the corresponding total ow.
Clearly, f is legitimate in (G(V, E), t, s, b, c) as well, where the roles of the
source and the sink are reversed. Also,
F
s,t
= F
t,s
.
It follows that f is a minimum ow in (G(V, E), s, t, b, c) if and only if f is a
maximum ow in (G(V, E), t, s, b, c). Thus, our techniques solve the problem
of minimizing the total ow by simply exchanging the roles of s and t.
For a set {s} S V \ {t}, let us dene c(S), (the value of the cut (S;
S) for
minimum ow purposes) as follows:
c(S) =
e(S;
S)
b(e)
e(
S;S)
c(e) .
Clearly, c(S) = c(
S) .
Thus,
F
s,t
= c(S) ,
and
F
s,t
=c(S) .
One can prove and use a lemma similar to Lemma 5.2, with the inequality
reversed. Thus, c(S) is maximum. It follows that for networks with lower and
upper bounds, which have legitimate ows, a min-owmax-cut theoremholds.
5.5 Problems
Problem 5.1 Find a maximum ow in the network shown below.
s
a b
c d
e f
t
3
2
9
6
2
7
5
7
1
2
4
6
The number next to each edge is its capacity. Show a minimum cut. How
many minimum cuts can you nd?
Problem 5.2 In the following network x
1
, x
2
, x
3
, are all sources (of the same
commodity). The supply available at x
1
is 5, at x
2
is 10, and at x
3
is 5. The
110 5 Flow in Networks
a
b c
d
7
9
4
3
3
7
3
2 5
8
8
1 6
8
1
2
4
2
7
x
1
x
2
x
3
y
1
y
2
y
3
vertices y
1
, y
2
, y
3
, are all sinks. The demand required at y
1
is 5, at y
2
is 10,
and at y
3
is 5. Find out whether all requirements can be met simultaneously.
(Hint: One way of solving this type of problem is to reduce it to the familiar
one-source one-sink format. Introduce auxiliary source s and sink t. Connect s
to x
i
through a directed edge of capacity equal to x
i
s supply. Connect each y
i
to t through a directed edge of capacity equal to y
i
s demand. Find a maximum
ow in the resulting network, and observe whether all demands are met.)
Problem 5.3 In the following network, in addition to the capacities of the
edges, each vertex other than s and t has an upper bound on the ow that may
s t
5
4
5
a
5
b
c
5
d
e
4
f
3
3
2
9
6
7
2
7
5
3
4
2
6
1
5.5 Problems 111
ow through it. These vertex capacities are written below the vertex labels.
Find a maximum ow for this network. (Hint: One way of solving this type
of problem is to replace each vertex v by two vertices v
and v
with an edge
v
e
v
, where c(e) is the upper bound on the ow through v. All edges that
previously entered v now enter v
.)
Problem 5.4
(1) Describe an alternative labeling procedure, like that of Ford and Fulkerson,
for maximizing the ow, except that the labeling starts at t, and if it reaches
s, an augmenting path is found.
(2) Demonstrate your algorithm on the following network:
s t
a
c
b
d
4
5
6
4
4
4
2
7
6
(3) Describe a method of locating an edge with the property that increasing
its capacity increases the maximum ow in the network. (Hint: One way
of doing this is to use both source-to-sink and sink-to-source labelings.)
Demonstrate your method on the network above.
(4) Does an edge like this always exist? Prove your claim.
Problem 5.5 In a network N(G(V, E), s, t, c), there are two sets, {s} S
1
V \ {t} and {s} S
2
V \ {t}, and each of them denes a minimum cut. Prove
that each of S
1
S
2
and S
1
S
2
denes a minimum cut as well.
Problem 5.6 Let f be a maximum ow in N(G(V, E), s, t, c), where all edge
capacities are positive integers, andfor everye E, f(e) is a nonnegative integer.
112 5 Flow in Networks
The capacity of u
e
v is reduced by one unit. It is necessary to nd a
maximum ow in the new network.
Describe an algorithm to achieve this goal whose time complexity is O(|E|).
(Hint: Without loss of generality, assume f(e) = c(e). If there is a directed
circuit via e that carries ow, reduce the ow on every edge of the circuit by
one unit. If not, rst locate a directed path from s to t via e that carries ow,
and reduce the ow in every edge of the path by one unit, and then look for an
augmenting path from s to t in the new network.)
Problem 5.7 Let G(V, E) be a nite directed graph without parallel edges.
Describe an algorithmthat is a modication of the algorithmof Ford and Fulk-
erson for nding a maximum ow in a given network N(G, s, t, c), except that,
in each stage, one looks for an augmenting path for which , the value by
which the ow is increased, is maximum. The time complexity of nding each
such path should be O(|V|
2
). (Comment: This algorithm can be shown to be
polynomial; see [4].)
Problem 5.8 In the following network, the capacities of the edges are written
next to them. Use the Dinitz algorithmto nd its maximumowwhen the initial
ow is zero everywhere. How many phases are there in which t is reached?
s t
2
2 2 2 2
2 2 2 2
2
2
2
1 1
Problem 5.9 A ow in a network is said to have circuits if there is at least one
directed circuit such that on all its edges the ow is positive. Such a circular
ow is superuous since it contributes nothing to the total ow.
5.5 Problems 113
Show that if we start with zero ow everywhere and use the Dinitz algorithm
to nd a maximum ow, we may end up with a ow that has circuits. (Hint:
Consider the graph depicted in Figure 5.13.)
s t
Figure 5.13: Hint for Problem 5.9.
Describe an O(|E|
2
) algorithm to remove all circular ow from a given ow
function.
Problem 5.10 Let N(G(V, E), c) be a network where G is a nite directed
graph, and c is a capacity function on the edges. For every x, y V, let F
xy
denote the maximum ow in case x is the source and y is the sink.
Prove that for every three vertices u, v, w V, F
uw
min{F
uv
, F
vw
}.
Problem 5.11 Prove that in a network with a lower bound b(e) 0 for every
edge e, but no upper bound (c(e) = ), there is a legitimate ow if and only
if for every edge e, for which b(e) >0, either e is in a directed circuit or e is
in a directed path from s to t or from t to s. Show that in such a network, a
legitimate ow can be found in time O(|V| |E|).
Problem5.12 Find a minimumowfroms to t for the network of Problem5.1,
where the numbers next to the edges are now assumed to be lower bounds, and
there are no upper bounds.
Problem 5.13 The two networks shown below have both lower and upper
bounds on the ow through the edges. Which of the two networks has no
legitimate ow? Find both a maximum ow and a minimum ow if a legit-
imate ow exists. If no legitimate ow exists, display a set of vertices that
114 5 Flow in Networks
includes neither the source nor the sink and is required to produce ow or to
absorb it.
5
s t
3,5
2,6
9,12
3,4
1,2
4,6
2,4
3,6
s t
3,5
2,6
5,10
3,4
1,2
4,6
2,4
3,6
(a)
(b)
Problem 5.14 Prove that a network with lower and upper bounds on the ow
in the edges has no legitimate ow if and only if there exists a set of vertices
which includes the neither source nor the sink and is required to produce ow
or to absorb it.
5
A set of vertices AV \{s, t} is required to absorb ow if
e(
A;A)
b(e) >
e(A;
A)
c(e) .
5.6 Notes by Andrew Goldberg 115
5.6 Notes by Andrew Goldberg
The augmenting path algorithm is due to Ford and Fulkerson [7, 6]. Dinitz [2]
developed the blocking ow algorithm, which runs in polynomial time. The
observation that augmenting along a shortest path leads to a polynomial-time
algorithmhas been made independentlyby Edmonds and Karp [4]. Amore ef-
cient, O(|V|
3
), variant of the blocking ow algorithm based on preows is due
to Karzanov [12]. Sleator and Tarjan [14] used the dynamic tree data structure
to improve this bound to O(|V||E| log|V|). This has been further improved to
O(|V||E| log(|V|
2
/|E|)) by Goldberg and Tarjan [10].
Goldberg and Tarjan [9] developed the push-relabel method that uses pre-
ows and the push operation similar to Karzanovs algorithm. However, instead
of building a layered network, the algorithm uses the relabel operation for
ne-grains updates of vertex distances. The push-relabel method leads to the
best currently known, strongly polynomial bound of King et al. [13]. This
bounds comes very close, but does not quite achieve, O(|V||E|). The push-
relabel algorithm is also highly practical when used in combination with
efciency-enhancing heuristics [1].
Karzanov [11] and, independently, Even and Tarjan [5] have shown that
on unit capacity networks, the blocking ow algorithm algorithm runs in
O(min((|E|
1/2
, |V|
2/3
)|E|) time. This leaves a polynomial gap between the
general and the unit-capacity cases.
When talking about shortest augmenting paths, one has to assign lengths
for residual edges. All polynomial-time algorithms mentioned above use the
unit length function. Edmonds and Karp [4] observed that one can use
other length functions. However, for a long time nobody was able to use
this observation to improve the time bounds. Goldberg and Rao [8] use the
binary length function (zero for high- and one for low-capacity edges) to get
an O(min((|E|
1/2
, |V|
2/3
)|E| log
|V|
2
|E|
logU) bound for networks with integral
capacities in the range [1, U]. This closes the gap between the general and the
unit-capacity case.
Bibliography
[1] B. V. Cherkassky and A. V. Goldberg. On Implementing Push-Relabel Method
for the Maximum Flow Problem. Algorithmica, 19:390410, 1997.
[2] E. A. Dinic. Algorithmfor Solution of a Problemof Maximum Flowin Networks
with Power Estimation. Soviet Math. Dokl., 11:12771280, 1970.
116 5 Flow in Networks
[3] YemDinitz. Dinitz Algorithm: The original version and Evens version. In Oded
Goldreich, Arnold L. Rosenberg, and Alan L. Selman, editors, Essays in Memory
of Shimon Even, Vol. 3895 of Lecture Notes in Computer Science, pp. 218240.
Springer, 2006.
[4] J. Edmonds and R. M. Karp. Theoretical Improvements in Algorithmic Efciency
for Network Flow Problems. J. Assoc. Comput. Mach., 19:248264, 1972.
[5] S. Even and R. E. Tarjan. Network Flow and Testing Graph Connectivity. SIAM
J. Comput., 4:507518, 1975.
[6] L. R. Ford, Jr. and D. R. Fulkerson. Maximal Flow Through a Network.
Canadian Journal of Math., 8:399404, 1956.
[7] L. R. Ford, Jr. and D. R. Fulkerson. Flows in Networks. Princeton University Press,
1962.
[8] A. V. Goldberg and S. Rao. Beyond the Flow Decomposition Barrier. J. Assoc.
Comput. Mach., 45:753782, 1998.
[9] A. V. Goldberg and R. E. Tarjan. A New Approach to the Maximum Flow
Problem. J. Assoc. Comput. Mach., 35:921940, 1988.
[10] A. V. Goldberg and R. E. Tarjan. Finding Minimum-Cost Circulations by
Successive Approximation. Math. of OR, 15:430466, 1990.
[11] A. V. Karzanov. O nakhozhdenii maksimalnogo potoka v setyakh spetsialnogo
vida i nekotorykh prilozheniyakh. In Matematicheskie Voprosy Upravleniya
Proizvodstvom, volume 5. Moscow State University Press, 1973. In Russian; title
translation: On Finding Maximum Flows in Networks with Special Structure and
Some Applications.
[12] A. V. Karzanov. Determining the Maximal Flow in a Network by the Method of
Preows. Soviet Math. Dok., 15:434437, 1974.
[13] V. King, S. Rao, and R. Tarjan. A Faster Deterministic Maximum Flow
Algorithm. J. Algorithms, 17:447474, 1994.
[14] D. D. Sleator and R. E. Tarjan. A Data Structure for Dynamic Trees. J. Comput.
System Sci., 26:362391, 1983.
6
Applications of Network Flow Techniques
6.1 Zero-One Network Flow
Several combinatorial problems can be solved through network owtechniques.
In the networks we get, the capacity of all the edges is one. To get better
algorithms with lower time complexities, we need to study these network ow
problems. We follow here the work of Even and Tarjan [1].
Consider a maximum ow problem where for every edge e of G(V, E),
c(e) = 1.
The rst observation is that in the Dinitz algorithm for maximal ow in a
layered network, each time we nd a path, all the edges on it become blocked;
in case the last edge leads to a dead end, we backtrack on this edge, and it
becomes blocked. Thus, the total number of edge traversals is bounded by |E|,
and the whole phase is of time complexity O(|E|). Since the number of phases
is bounded by |V|, the Dinitz algorithm for maximum ow is of complexity
O(|V| |E|).
Our rst goal is to prove a better bound yet: O(|E|
3/2
). However, we need to
prepare a few results beforehand.
Let G(V, E) be a 0-1 network in which c(e) = 1 for all e E with some
integral legal ow function f. Dene
G(V,
E) as follows:
(i) If u
e
v in G, and f(e) = 0, then e
E.
(ii) If u
e
v in G, and f(e) = 1, then u
e
v is in
G. Clearly, e
is a new
edge that corresponds to e.
Thus, |E| = |
E|. Clearly, the useful edges of the layered network that is con-
structed for G with present ow f, with their direction of usefulness, are all
edges of
G.
117
118 6 Applications of Network Flow Techniques
Let us denote by (SI
S)
G
, where s S, t / S, and
S =V S, the set of edges
that emanate from a vertex of S and enter a vertex of
S, and let c(S, G) be the
capacity of the corresponding cut in G. Also, let Mbe the total maximum ow
in G, while F is the total present ow.
Lemma 6.1
M=MF.
Proof: Let S be a subset of V such that s S and t / S. The denition of
G
implies that
c(S,
G) = |(S;
S)
G
| =
e(S;
S)
G
(1 f(e)) +
e(
S;S)
G
f(e).
However,
F =
e(S;
S)
G
f(e)
e(
S;S)
G
f(e).
Thus,
c(S,
G) = |(S;
S)
G
| F =c(S, G) F.
This implies that the minimum cut of G corresponds to the minimum cut
of
G; that is, is dened by the same S. By the max-ow min-cut theorem
(Theorem 5.1), the capacity of a minimum cut of
G is
M (the maximum total
ow in
G). Thus, the lemma follows.
Lemma 6.2 The length of the layered network for the 0-1 network dened by
G(V, E) (with a given s and t) and zero ow everywhere is at most |E|/M.
Proof: We remind the reader that V
i
is the set of vertices of the i-th layer of
the layered network, and E
i
is the set of edges fromV
i1
to V
i
. Since f(e) =0
for every e E, the useful directions are all forward. Thus, every E
i
is equal to
(S;
S)
G
, where S =V
0
V
1
. . . V
i1
. Thus, by Lemma 5.1,
M|E
i
|. (6.1)
Summing up (6.1), for every i = 1, 2, . . . , l where l is the length of the layered
network, we get l M|E|, or
l |E|/M.
M=MF >|E|
1/2
.
By Lemma 6.2, the length l of this layered network satises
l |E|/
M<|E|/|E|
1/2
= |E|
1/2
.
Therefore, the number of phases up to this point is at most |E|
1/2
1, and since
the number of additional phases to completionis at most |E|
1/2
, the total number
of phases is at most 2|E|
1/2
.
A 0-1 network is of type 1 if it has no parallel edges. For such a network we
can prove another upper bound on the time complexity. First, we prove a lemma
similar to Lemma 6.2.
Lemma 6.3 Let G(V, E) dene a 0-1 network of type 1, with maximum total
ow M from s to t. The length l of the rst layered network, when the ow is
zero everywhere, is at most 2|V|/M
1/2
.
Proof: Let V
i
be the set of vertices of the i-th layer. Since there are no parallel
edges, the set of edges, E
i+1
, from V
i
to V
i+1
in the layered network satises
|E
i+1
| |V
i
| |V
i+1
| for every i = 0, 1, . . . , l 1. Since each |E
i
| is the capacity
of a cut, we get that
M|V
i
| |V
i+1
|.
Thus, either V
i
M
1/2
or |V
i+1
| M
1/2
. Clearly,
|V|
i=0
|V
i
|
_
l +1
2
_
M
1/2
.
Thus,
|V|
M
1/2
_
l +1
2
_
2
,
120 6 Applications of Network Flow Techniques
and
2|V|
M
1/2
,
and the lemma follows.
Theorem6.2 For 0-1 networks of type 1, Dinitzs algorithmhas time complexity
O(|V|
2/3
|E|).
Proof: If M |V|
2/3
, the result follows immediately. Let F be the total ow
when the layered network, for the phase during which the total ow reaches the
value M|V|
2/3
, is constructed. This layered network is identical with the rst
layered network for
Gwith zero oweverywhere.
Gmay not be of type 1 since
it may have parallel edges, but it can have at most two parallel edges from one
vertex to another; if e
1
and e
2
are antiparallel in G, f(e
1
) = 0 and f(e
2
) = 1,
then in
Gthere are two parallel edges: e
1
and e
2
. A result similar to Lemma 6.3
yields that
l <2
3/2
|V|/
M
1/2
.
Since
M=MF >M (M |V|
2/3
) = |V|
2/3
, we get
l <
2
3/2
|V|
|V|
1/3
= 2
3/2
|V|
2/3
.
Thus, the number of phases up to this point is O(|V|
2/3
). Since the number of
phases from here to completion is at most |V|
2/3
, the total number of phases is
O(|V|
2/3
).
In certain applications, the networks that arise satisfy the condition that for
each vertex other than s or t, there is either only one edge emanating from it or
only one edge entering it. Such 0-1 networks are called type 2.
Lemma 6.4 Let the 0-1 network dened by G(V, E) be of type 2, with maximum
total owMfrom s to t. The length l of the rst layered network, when the ow
is zero everywhere, is at most (|V| 2)/M+1.
Proof: The structure of G implies that a max-ow in G can be decomposed
into vertex-disjoint directed paths from s to t; that is, no two of these paths
share any vertices, except their common start-vertex s and end-vertex t. (The
ow may imply some directed circuits that are vertex-disjoint from each other
and from the paths above, except possibly at s or t. These circuits are of no
interest to us). The number of these paths is equal to M. Let be the length of a
6.2 Vertex Connectivity of Graphs 121
shortest of these paths. Thus, each of the paths uses at least 1 intermediate
vertices. We have
M (1) |V| 2,
which implies (|V| 2)/M + 1. However, l . Thus, the lemma
follows.
Lemma 6.5 If the 0-1 network dened by Gis of type 2, and if the present ow
function is f, then the corresponding
G also denes a type 2, 0-1 network.
Proof: Clearly
G denes a 0-1 network. What remains to be shown is that in
G, for every vertex v, there is either one emanating edge or only one entering
edge. If there is no ow through v (per f), then, in
G, v has exactly the same
incident edges, and the condition continues to hold. If the ow going through v
is 1, (clearly, it cannot be more) assume that it enters via e
1
and leaves via e
2
.
In
G, neither of these two edges appears, but two edges e
1
and e
2
are added,
which have directions opposite to e
1
and e
2
, respectively. The other edges of G
that are incident to v remain intact in
G. Thus, the numbers of incoming edges
and outgoing edges of v remain the same. Since G is of type 2, so is
G.
Theorem 6.3 For a 0-1 network of type 2, Dinitzs algorithm is of time
complexity O(|V|
1/2
|E|).
Proof: If M |V|
1/2
, then the number of phases is bounded by |V|
1/2
, and
the result follows. Otherwise, consider the phase during which the total ow
reaches the value M |V|
1/2
. Therefore, the layered network for this phase is
constructed when F < M |V|
1/2
. This layered network is identical with the
rst for
G, with zero oweverywhere. Also, by Lemma 6.5,
Gis of type 2. Thus,
by Lemma 6.4, the length l of the layered network is at most (|V| 2)/
M+1.
Now,
M=MF >M (M |V|
1/2
) = |V|
1/2
. Thus,
l
|V| 2
|V|
1/2
+1 =O(|V|
1/2
).
Therefore, the number of phases up to this one is at most O(|V|
1/2
). Since the
number of phases to completion is at most |V|
1/2
more, the total number of
phases is at most O(|V|
1/2
).
6.2 Vertex Connectivity of Graphs
Intuitively, the connectivity of a graph is the minimum number of elements
whose removal from the graph disconnect it. There are four cases. We may
122 6 Applications of Network Flow Techniques
discuss undirected graphs or digraphs; we may discuss the elimination of edges
or vertices. We start with the problem of determining the vertex-connectivity
of an undirected graph. The other cases, which are simpler, are discussed in the
next section.
Let G(V, E) be a nite undirected graph, with no self-loops and no parallel
edges. A set of vertices, S, is called an (a, b) vertex separator if {a, b} V \S,
and every path connecting a and b passes through at least one vertex of S.
Clearly, if a and b are connected by an edge, no (a, b) vertex separator exists.
Let a, b mean that there is no such edge. In this case, let N(a, b) be the
least cardinality of an (a, b) vertex separator. Also, let p(a, b) be the maximum
number of pairwise vertex-disjoint paths connecting a and b in G; clearly, all
these paths share the two end-vertices, but no other vertex appears on more than
one of them.
Theorem 6.4 If a , b then N(a, b) =p(a, b).
This is one of the variations of Mengers theorem [2]. It is not only rem-
iniscent of the max-ow min-cut theorem, but can be proved by it. Dantzig
and Fulkerson [3] pointed out how this can be done, and we shall follow their
approach.
Proof: Construct a digraph
G(
V,
E) as follows. For every v V put two vertices
v
and v
in
V with an edge v
e
v
v
and v
in
G. Dene now a network, with digraph
G,
source a
, sink b
and e
type (called external edges). For example, in Figure 6.1(b) the network for G,
as shown in Figure 6.1(a), is demonstrated.
We now claim that p(a, b) is equal to the total maximum ow F (froma
to
b
1
v
1
e
v
1
v
1
e
2
v
2
e
v
2
v
2
e
3
. . .
e
l1
v
l1
e
v
l1
v
l1
e
l
b
These directed paths are vertex-disjoint, and each can be used to ow one
unit froma
to b
. Thus,
F p(a, b).
6.2 Vertex Connectivity of Graphs 123
a
(a) (b)
b
b b
1
1
1
1
a a
Figure 6.1: Construction in the proof of Theorem 6.4
Next, assume f is a ow function which achieves a maximum total ow F in
the network. We may assume that f(e) is either zero or one, for every e
E.
This follows from the fact that one can use the Ford and Fulkerson algorithm,
or the Dinitz algorithm, in which the ow is always integral. Also, the edges
with innite capacity enter a vertex with a single outgoing edge whose capacity
is one and which must satisfy the conversation rule (C2), or they emanate from
a vertex with only one incoming edge of unit capacity which is subject to C2;
thus, the ow through them is actually bounded from above by one. (We have
assigned them innite capacity for convenience reasons, which will become
clear shortly.) Therefore, the total ow F can be decomposed to paths, each
describing the way that one unit reaches b
from a
or v
S and b
/ S. Since
c(S) =
e(S;
S)
c(e),
the set (S;
to b
in
G uses at least one edge of (S;
v
(S;
S). R is a
minimum(a, b) vertex separator in G. This addition work is of time complexity
O(|E|).
The vertex connectivity, c, of an undirected graph G(V, E) is dened as
follows:
(i) If G is completely connected, (i.e., every two vertices are connected by an
edge), then c = |V| 1.
(ii) If G is not completely connected, then
c = min
a, b
N(a, b).
Lemma 6.6 If G is not completely connected, then
min
a, b
p(a, b) = min
a,b
p(a, b);
namely, the smallest value of p(a, b) occurs also for some two vertices a and
b that are not connected by an edge.
Proof: Let a, b be a pair of vertices such that a
e
b and p(a, b) is minimum
over all pairs of vertices of the graph. Let G
, p
(a, b) =p(a, b) 1.
Also, since a, b in G
such that p
(a, b) = |R|.
If |R| = |V| 2, then p(a, b) = |V| 1, and p(a, b) cannot be the least of all
{p(u, v) | u, v V}, since for any u, v, p(u, v) |V| 2. Hence, |R| <|V| 2.
Therefore, there must be some vertex v V (R{a, b}). Now, without loss of
generality, we may assume that R is also an (a, v) vertex separator (or exchange
a and b). Thus, a, v in G and R{b} is an (a, v) vertex separator in G. We
now have
p(a, v) |R| +1 =p(a, b),
and the lemma follows.
Theorem 6.5 c = min
a,b
p(a, b).
Proof: If G is completely connected, then for every two vertices a and b,
p(a, b) = |V| 1, and the theorem holds. If G is not completely connected,
then, by denition,
c = min
a, b
N(a, b).
By Theorem 6.4, min
a, b
N(a, b) = min
a, b
p(a, b). Now by Lemma 6.6,
min
a, b
p(a, b) = min
a,b
p(a, b).
We can use the intermediate result,
c = min
a, b
p(a, b),
to compute the vertex connectivity of G with time complexity O(|V|
5/2
|E|).
However, a slightly better bound can be obtained.
Lemma 6.7 c 2|E|/|V|.
Proof: The vertex (or edge) connectivity of a graph cannot exceed the degree
of any vertex. Thus,
c min
v
d(v).
Also,
v
d(v) = 2 |E|.
Thus, min
v
d(v) 2 |E|/|V|, and the lemma follows.
126 6 Applications of Network Flow Techniques
Procedure VERTEX-CONNECTIVITY(V, E)
1 Order the vertices v
1
, v
2
, . . . , v
|V|
in such a way that v
1
, v for some v.
2
3 i 1
4 while i do
5 for every v such that v
i
, v do
6 min{, N(v
i
, v)}
7 return .
Algorithm 6.1: Constructing the vertex connectivity of a graph G=
(V, E) that is not completely connected.
A procedure to nd the vertex connectivity c of a graph G that is not
completely connected is listed in Algorithm 6.1.
Theorem6.6 The procedure VERTEX-CONNECTIVITYterminates with =c.
Proof: Clearly, after the rst computation of N(v
1
, v) for some v
1
, v,
satises
c |V| 2. (6.2)
From there on, can only decrease, but (6.2) still holds. Thus, for some
k |V| 1, the procedure will terminate. When it does, k +1 c +1.
By denition, c equal to the cardinality of a minimum vertex separator R of
G. Thus, at least one of the vertices v
1
, v
2
, . . . , v
k
is not in R, say v
i
. R separates
the remaining vertices into at least two sets, such that each path from a vertex
of one set to a vertex of another passes through at least on vertex of R. Thus,
there exists a vertex v such that N(v
i
, v) |R| =c, and therefore c.
Clearly, the time complexity of this procedure is O(c |V|
3/2
|E|). By
Lemma 6.7, this is bounded by O(|V
1/2
|E|
2
).
If c =0, then Gis not connected. We can use DFS (or BFS) to test whether this
is the case in O(|E|) time. If c =1, then Gis separable, and as we sawin Section
3.2, this can be tested also by DFS in O(|E|) time. This algorithm determines
also whether c 2, that is, whether it is nonseparable. Before we discuss testing
6.2 Vertex Connectivity of Graphs 127
for a given k, whether c k, let us consider the following interesting theorem
about equivalent conditions for G to be nonseparable.
1
Theorem6.7 Let G(V, E) be an undirected graph with |V| >2 and no isolated
vertices.
2
The following six statements are equivalent.
1. G is nonseparable.
2. For every two vertices x and y there exists a simple circuit which goes
through both.
3. For every two edges e
1
and e
2
there exists a simple circuit which goes through
both.
4. For every two vertices x and y and an edge e there exists a simple path from
x to y which goes through e.
5. For every three vertices x, y and z there exists a simple path from x to z
which goes through y.
6. For every three vertices x, y and z there exists a simple path from x to z
which avoids
Proof: First we prove that (1) is equivalent to (2).
(1) (2): Since G is nonseparable, c 2. By Theorem 6.5, for every two
vertices x and y p(x, y) 2; thus, there is a simple circuit that goes through x
and y.
(2) (1): There cannot exist a separation vertex in G, since every two vertices
lie on some common simple circuit.
Next, let us show that (1) and (3) are equivalent.
(1) (3): From G construct G
, is still
nonseparable. By the equivalence of (1) and (3), G
in G
. Therefore, there is
a simple path in G from x to y through e.
(4) (5): Let e be an edge incident to vertex y; such an edge exists, since
there are no isolated vertices in G. By (4), there is a simple path from x to z
through e. Thus, this path goes through y.
(5) (6): Let p be a simple path which goes from x to y through z; such a
path exists, since (5) holds for every three vertices. The rst part of p, from x
to z does not pass through y.
(6) (1): If (6) holds, then there cannot be any separation vertex in G.
Let us now return to the problem of testing the vertex connectivity of a given
graph G; that is, testing whether c is greater than or equal to a given positive
integer k. We have already seen that for k=1 and 2, there is an O(|E|) algorithm.
Hopcroft and Tarjan [4] showed that k =3 can also be tested in linear time, but
their algorithmis quite complicated and does not seem to generalize for higher
values of k. Let us present a method suggested by Kleitman [5] and improved
by Even [6].
Let L = {v
1
, v
2
, . . . , v
l
} be a subset of V, where l k. Dene
G as follows:
G
includes all the vertices and edges of G. In addition, it includes a new vertex
s connected by an edge to each of the vertices of L;
G is called the auxiliary
graph.
Lemma 6.8 Let u V L. If p(v
i
, u) k in G, for every v
i
L, then, in
G,
p(s, u) k.
Proof: Assume not. Then p(s, u) < k. By Theorem 6.4, there exists a (s, u)
vertex separator S in
Gsuch that |S| <k. Let R be the set of vertices such that all
paths in
Gfroms to v R pass through at least one vertex of S. Clearly, v
i
/ R,
since v
i
is connected by an edge to s. However, since l k >|S|, there exists
some 1 i l such that v
i
/ S. All paths from v
i
to u go through vertices of
S. Thus, p(v
i
, u) |S| <k, contradicting the assumption.
Let V = {v
1
, v
2
, . . . , v
n
}. Let j be the least integer such that for some i < j,
p(v
i
, v
j
) <k in G.
6.3 Connectivity of Digraphs and Edge Connectivity 129
Lemma 6.9 Let j be as dened above and
G be the auxiliary graph for L =
{v
1
, v
2
, . . . , v
j1
}. In
G, p(s, v
j
) <k
Proof: Consider a minimum (v
i
, v
j
) vertex separator S. By Theorem 6.4,
|S| <k. Let R be the set of all vertices v V such that all the paths from v
i
to v in Gpass through vertices of S. Clearly, v
j
R. If for some j
<j, v
j
R,
then p(i, j
in
G.
The vertex connectivity, c, of a digraph G(V, E) is dened as follows:
(i) If G is completely connected, (i.e., for every two vertices a and b, there
are edges a b and b a), then c = |V| 1.
(ii) If G is not completely connected, then
c = min
a, b
N(a, b).
The lemma analogous to Lemma 6.6 still holds, and the proof goes along
the same lines. Also, the theorem analogous to Theorem 6.5 holds, and the
complexity it yields is the same. If G has no parallel edges, a statement like
Lemma 6.7 holds, and the procedure and the proof of its validity (Theorem6.6)
extend to the directed case, except that for each v
i
, we compute both N(v
i
, v)
and N(v, v
i
).
The algorithmfor testing k connectivity extends also to the directed case, and
again all we need to change is that whenever p(a, b) was computed, we now
have to compute both p(a, b) and p(b, a).
Let us nowconsider the case of edge connectivity both in graphs and digraphs.
Let G(V, E) be an undirected graph. Aset of edges, T, is called an (a, b) edge
separator if every path from a to b passes through at least one edge of T. Let
M(a, b) be the least cardinality of an (a, b) edge separator. Let p(a, b) be now
the maximum number of edge disjoint paths which connect a with b.
Theorem 6.8 M(a, b) =p(a, b).
The proof is similar to that of Theorem 6.4, only simpler. There is no need
to split vertices. Thus, in
G,
V = V. We still represent each edge u v of
G by two edges u
e
v and v
e
u in
G. There is no loss of generality in
assuming that the ow function in
Gsatises the condition that either f(e
) =0
or f(e
) =f(e
V{v}
M(v, v
).
We need to solve at most |V| 1 network owproblems. Thus, the complexity
of the algorithm is O(|V| |E| min{|E|
1/2
, |V|
2/3
}).
In the case of edge connectivity of digraphs, we need to consider directed
paths. The denition of an (a, b) edge separator is accordingly a set of edges,
T, such that every directed path from a to b uses at least one edge of T. The
denition of p(a, b) again uses directed paths, and the proof of the statement
analogous to Theorem6.8 is the easiest of all, since
Gis nowGwith no changes.
In the denition of c, the edge connectivity, we need the following change:
c = min{M(a, b) | (a, b) V V},
namely, we need to consider all ordered pairs of vertices.
The networks we get are still of type 1 andthe complexity of each is still O(|E|
min{|E|
1/2
, |V|
2/3
}). The approach of testing for one vertex v, both M(v, v
) and
M(v
, v) for all v
S) in G. If H is a
subgraph of G then GH is the digraph resulting from the deletion of all the
edges of H from G.
Clearly, the condition that min
vV{a}
M(v, a) k is equivalent to the
statement that, for every S V, S ,=V and a S,
G
(S) k.
Let F(V
, E
;
V
,=V,
(3)
GF
(S) =k1.
Let us show that if no such S exists, then one can add any edge e (V
;
V
)
to F. Clearly, F+e satises (i). Now, if (ii) does not hold, then there exists an S
such that S ,=V, a S, and
G(F+e)
(S) <k1. It follows that
GF
(S)<k.
Now, by (ii),
GF
(S) k1. Thus,
GF
(S) =k1, and S satises condi-
tion ((3)). Let uand v be vertices such that u
e
v. Since
G(F+e)
(S) <k1
and
GF
(S) = k 1, v / S. Also, v / V
. Thus, S V
,= V, satisfying
condition ((2)). Therefore, S satises all three conditions; A contradiction.
Now, let A be a maximal
3
set of vertices that satises ((1)), ((2)), and ((3)).
Since the edges of F all enter vertices of V
GF
(AV
) =
G
(AV
) k.
By condition ((3)),
GF
(AV
) >
GF
(A).
The inequality implies that there exists an edge x
e
y that belongs to (A
V
;AV
and
y
A
V
S), then
G(F+e)
(S) =
GF
(S) k1.
Assume e (S;
GF
(SA) +
GF
(SA)
GF
(S) +
GF
(A),
by considering the sets of edges connecting S A, S
A,
S A, and
S
A.
Now,
GF
(A) =k1 and
GF
(SA) k1. Therefore,
GF
(SA)
GF
(S).
3
Namely, no larger set that contains Ahas all three properties.
134 6 Applications of Network Flow Techniques
Since x Sand x / A, SA; namely, SAis larger than A. Also, y
S, y
A,
and y
V
. Thus, (SA)V
S)| <k in
G. Clearly, S ,= {a} for |({a};V)| =k. Let x S {a}. Thus,
M(x, y) < k for every y
S, and the same also holds in G, since G is a sub-
graph of
G. This contradicts the assumption that in G, the edge connectivity is
at least k.
Now, by Theorem 6.9, there are k edge-disjoint directed spanning trees, in
G, rooted at a. Exactly one edge out of a appears in each tree. Thus, each
of the trees that uses an edge a u contains a directed path from u to v,
and each of the trees that uses an edge a v contains a directed path from
v to u. All these paths are, clearly, edge disjoint.
Corollary 6.1 If the edge connectivity of a digraph is at least 2, then for every
two vertices u and v there exists a directed circuit that goes through u and v
in which no edge appears more than once.
Proof: Use k = 2, l = 1 in Theorem 6.10.
It is interesting to note that no such easy result exists in the case of vertex
connectivity and a simple directed circuit through given two vertices. In [11],
6.4 Maximum Matching in Bipartite Graphs 135
a digraph with vertex connectivity 5 is shown such that for every two of its
vertices there is no simple directed circuit that passes through both. The author
does not know whether any vertex connectivity will guarantee the existence of
a simple directed circuit through any two vertices.
6.4 Maximum Matching in Bipartite Graphs
A set of edges, M, of a graph G(V, E) with no self-loops, is called a matching
if every vertex is incident to at most one edge of M. The problem of nding a
maximummatching was rst solved in polynomial time by Edmonds [12]. The
best known result of Even and Kariv [13] is O(|V|
2.5
). These algorithms are too
complicated to be included here, and they do not use network ow techniques.
An easier problemis to nd a maximummatching in a bipartite graph, that is,
a graph in which V =XY, XY =, and each edge has one end-vertex in X
and one in Y. This problemis also known as the marriage problem. We present
here its solution via network owand showthat its complexity is O(|V|
1/2
|E|).
This result was rst achieved by Hopcroft and Karp [14].
Let us construct a network N(G). Its digraph
G(
V,
E) is dened as follows:
V = {s, t} V,
1
1
1
1
1
1
1
1
1
s t
x
1
x
3
x
4
x
5
x
2
y
1
y
2
y
3
y
4
Figure 6.2: Construction of N(G).
,1
,1
1,1
1,1
1,1
1,1
1,1
,1
,0
,0
,0
,1
,0
1,1
1,1
1,1
1,1
s t
x
1
x
3
x
4
x
5
x
2
y
1
y
2
y
3
y
4
x
1
x
3
x
4
x
5
x
2
y
1
y
2
y
3
y
4
(a) (b)
Figure 6.3: Maximum ow found by Dinitzs algorithm and its
corresponding matching.
to t, then no other edge x y
or x
S) = ({s};XA) ((A);{t}).
Since (S;
T), for there is no upper bound on the ow in the edges, and any such
edge would enable to continue the labeling of vertices. Thus, the set (
T;T) is
concurrent.
If S is a set of concurrent edges, then the number of processors required is,
at least |S|. This can be seen by assigning the edges of S a very large length;
and all the others, a short length. Since no directed path leads from one edge
of S to another, they all will be operative simultaneously. This implies that the
number of processors required is at least |(
T;T)|.
140 6 Applications of Network Flow Techniques
s t
s t
3
3
3
3
2
1
2
(a)
2
1
1
1
1
2
2
2
2
2
1
1
1
1
1
1
(b)
Figure 6.5: (a) The minimum ow in the PERT; (b) decompositions
of the ow into the PERT into F directed paths.
However, the owcan be decomposed into F directed paths froms to t, where
F is the minimum total ow, such that every edge is on at least one such (since
f(e) 1 for every e E). This is demonstratedfor our example in Figure 6.5(b).
We can now assign to each processor all the edges of one such path. Each such
processor executes the processes represented by the edges of the path in the
order in which they appear on the path. If one process is assigned to more than
one processor, then one of them executes while the others are idle. It follows
that whenever a process that corresponds to u v is executable (because all
the processes which correspond to (u) have been executed), the processor to
which this process is assigned is available for its execution. Thus, F processors
are sufcient for our purpose.
Since F = |(
= {s, t} {x
1
, x
2
, . . . , x
|V|
} {y
1
, y
2
, . . . , y
|V|
}.
E
= {s x
i
| 1 i |V|} {y
i
t | 1 i |V|}
{x
i
y
j
| v
i
v
j
in G}.
The capacity of all edges is 1.
Showthat the minimumnumber of paths that cover V in Gis equal to |V| F,
where F is the maximum total ow of the network.)
2. Is the condition that Gis acyclic essential for the validity of your algorithm?
Explain.
3. Give the best upper bound you can on the time complexity of your algorithm.
Problem 6.2 This problem is similar to Problem 6.1, except that the paths are
not required to be vertex- (or edge-) disjoint.
1. Describe an algorithm for nding a minimum number of covering paths.
(Hint. Form a network as follows:
V
= {s, t} {x
1
, x
2
, . . . , x
|V|
} {y
1
, y
2
, . . . , y
|V|
}.
E
= {s x
i
| 1 i |V|} {y
i
t | 1 i |V|}
{x
i
y
i
| 1 i |V|} {y
i
x
j
| v
i
v
j
in G}.
The lower bound of each x
i
y
i
edge is 1.
The lower bound of all other edges is 0.
The upper bound of all the edges is . Find a minimum ow from s to t.)
5
A digraph is called acyclic if it has no directed circuits.
142 6 Applications of Network Flow Techniques
2. Is the condition that Gis acyclic essential for the validity of your algorithm?
Explain.
3. Give the best upper bound you can on the time complexity of your algorithm.
(Hint. O(|V| |E|) is achievable.)
4. Two vertices u and v are called concurrent if no directed path exists from
u to v or fromv to u. A set of concurrent vertices is such that every two in
the set are concurrent. Prove that the minimum number of paths that cover
the vertices of G is equal to the maximum number of concurrent vertices.
(This is Dilworths Theorem [16].)
Problem 6.3 1. Let G(X, Y, E) be a nite bipartite graph. Describe an efcient
algorithm for nding a minimum set of edges such that each vertex is an
end-vertex of at least one of the edges in the set.
2. Discuss the time complexity of your algorithm.
3. Prove that the size of a minimum set of edges which cover the ver-
tices (as in (1)) is equal to the maximum size of an independent set of
vertices of G.
6
Problem 6.4 1. Prove that if G(X, Y, E) is a complete bipartite graph (i.e., for
every two vertices x X and y Y, there is an edge x y) then the vertex
connectivity of G is
c(G) = min{|X|, |Y|}.
2. Prove that for every k, there exists a graph G such that c(G) k and G has
no Hamilton path. (See Problem 1.4.)
Problem 6.5 Let M be a matching of a bipartite graph. Prove that there exists
a maximummatching M
.
Problem 6.6 Let G(V, E) be a nite acyclic digraph with exactly one vertex
s for which d
in
(s) = 0 and exactly one vertex t for which d
out
= 0. We say
that the edge a b is greater than the edge c d if and only if there is a
directed path in G fromb to c. A set of edges is called a slice if no edge in it is
greater than another and it is maximal; no other set of edges with this property
contains it. Prove that the following three conditions on a set of edges, P, are
equivalent:
6
A set of vertices of a graph is called independent if there is no edge between two vertices of the
set.
6.6 Problems 143
1. P is a slice.
2. P is an (s, t) edge separator in which no edge is greater than any other.
3. P = (S;
S;S) =.
Problem 6.7 (The problem of a system of distinct representatives [SDR]). Let
S
1
, S
2
, . . . , S
m
be nite sets. A set {e
1
, e
2
, . . . , e
m
} is called an SDR if for every
1 i m, e
i
S
i
.
1. Describe an efcient algorithm for nding an SDR, if one exists. (Hint.
Dene a bipartite graph and solve a matching problem.)
2. Prove that an SDR exists if and only if the union of any 1 k m of the
sets contains at least k elements.
Problem 6.8 Let
1
and
2
be two partitions of a set of m elements, each
containing exactly r disjoint subsets. We want to nd a set of r elements such
that each of the subsets of
1
and
2
is represented.
1. Describe an efcient algorithm to determine whether there is such a set of r
representatives.
2. State a necessary and sufcient condition for the existence of such a set,
similar to Theorem 6.12.
Problem6.9 Let G(V, E) be a completely connecteddigraph (see Problem1.5);
it is called classiable if V can be partitioned into two nonempty classes, Aand
B, such that all the edges connecting between them are directed from A to B.
Let V = {v
1
, v
2
, . . . , v
n
} where the vertices satisfy
d
out
(v
1
) d
out
(v
2
) . . . d
out
(v
n
).
Prove that G is classiable if and only if there exists a k <n such that
k
i=1
d
out
(v
i
) =
_
k
2
_
.
Problem 6.10 Let S be a set of people such that |S| 4. We assume that
acquaintance is a mutual relationship. Prove that if, in every subset of four
people, there is one who knows all the others, then there is someone in S who
knows everybody.
Problem 6.11 In the acyclic digraph shown in Figure 6.6, there are both AND
vertices (designated by
_
) and OR vertices (designated by
_
). As in a PERT
network, the edges represent processes, and the edge length is the time the
144 6 Applications of Network Flow Techniques
t s
4
5
6
3
3
5
4
6
2
6
2
5
4
3
3
5
6
1
Figure 6.6: Graph for Problem 6.11.
process requires. The processes represented by the edges that emanate from
an AND (OR) vertex can be started when all (at least one of) the incoming
processes are (is) completed. Describe an algorithm for nding the minimum
time fromstart (s) to reach termination (which depends on the type of t). Apply
your algorithmon the given network. What is the complexityof your algorithm?
Problem 6.12 Consider Problem 6.11 on digraphs that are not necessarily
acyclic. Show how to modify the algorithm to solve the problem or conclude
that there is no solution.
Problem 6.13 In a school are n boys and n girls. Each boy knows exactly k
girls (1 k n), and each girl knows exactly k boys. Prove that if knowing"
is mutual, then all the boys and girls can participate in one dance, where every
pair of dancers (a boy and a girl) know each other. Also show that it is always
true that k consecutive dances can be organized so that everyone will dance
once with everyone he or she knows.
Problem6.14 Prove or disprove the following claim: If the vertex connectivity
of a digraph is at least 2, then for every three vertices x, y, and z, there exists a
simple directed path from x to z via y.
Problem 6.15 Let G be a nite undirected graph whose edge connectivity is
at least 2. For each one of the following claims, determine whether it is true or
false. Justify your answer.
1. For every three vertices x, y, and z there exists a path in which no edge
appears more than once, from x to z via y.
2. For every three vertices x, y, and z there exists a path in which no edge
appears more than once, from x to z, which avoids y.
6.6 Problems 145
Bibliography
[1] Even, S., Tarjan, R. E., Network Flow and Testing Connectivity, SIAM J. on
comput., Vol. 4, 1975, pp. 507518.
[2] Menger, K., Zur Aligemeinen Kurventheorie, Fund. Math., Vol. 10, 1927,
pp. 96115.
[3] Dantzig, G. B., Fulkerson, D. R., On the Max-Flow Min-Cut Theorem of
Networks, Linear Inequalities and Related Systems, Annals of Math. Study 38,
Princeton University Press, 1956, pp. 215221.
[4] Hopcroft, J., and Tarjan, R. E., Dividing a Graph into Triconnected Components,
SIAM J. on Comput., Vol. 2, 1973, pp. 135158.
[5] Kleitman, D. J., Methods for Investigating Connectivity of Large Graphs, IEEE
Trans. on Circuit Theory, CT-16, 1969, pp. 232233.
[6] Even, S., Algorithm for Determining whether the Connectivity of a Graph is at
Least k, Siam J. on Comput., Vol. 4, 1977, pp. 393396.
[7] Gomory, R. E., and Hu, T. C., Multi-Terminal Network Flows, J. of SIAM,
Vol. 9, 1961, pp. 551570.
[8] Schnorr, C. P., Multiterminal Network Flow and Connectivity in Unsymmetrical
Networks, Department of Applied Math, University of Frankfurt, October 1977.
[9] Edmonds, J., Edge-Disjoint Branchings, in Combinatorial Algorithms, Courant
Inst. Sci. Symp. 9, R. Rustin, Ed., Algorithmics Press Inc., 1973, pp. 9196.
[10] Lovsz, L., On Two Minimax Theorems in Graph Theory, Journal of Combina-
torial Theory, Series B, Vol. 21:2, 1976, pp. 96103.
[11] Even, S., Garey, M. R., and Tarjan, R. E., A Note on Connectivity and Circuits
in Directed Graphs, unpublished manuscript (1977).
[12] Edmonds, J., Paths, Trees, and Flowers, Canadian J. of Math., Vol. 17, 1965,
pp. 449467.
[13] Even, S., and Kariv, O., An O(n
2.5
) Algorithmfor MaximumMatching in General
Graphs, 16th Annual Symposium on Foundations of Computer Science, IEEE,
1975, pp. 100112.
[14] Hopcroft, J., and Karp, R. M., An O(n
5/2
) Algorithm for Maximum Matching
in Bipartite Graphs, SIAM J. on Comput., 1975, pp. 225231.
[15] Hall, P., On Representation of Subsets, J. London Math. Soc. Vol. 10, 1935,
pp. 2630.
[16] Dilworth, R. P., A Decomposition Theorem for Partially Ordered Sets, Ann.
Math., Vol. 51, 1950, pp. 161166.
7
Planar Graphs
7.1 Bridges and Kuratowskis Theorem
Consider a graph drawn in the plane in such a way that each vertex is repre-
sented by a point; each edge is represented by a continuous line connecting
the two points that represent its end vertices, and no two lines, which represent
edges, share any points, except in their ends. Such a drawing is called a plane
graph. If a graph G has a representation in the plane that is a plane graph then
it is said to be planar.
In this chapter, we shall discuss some of the classical work concerning planar
graphs. The question of efciently testing whether a given nite graph is planar
is discussed in the next chapter.
Let S be a set of vertices of a nonseparable graph G(V, E). Consider the
partition of the set V S into classes, such that two vertices are in the same
class if and only if there is a path connecting them that does not use any vertex
of S. Each such class K denes a component as follows: The component is a
subgraph H(V
, E
), where V
K. In addition, V
(v
1
, v
2
) that uses vertices of Konly; if v
1
=v
2
,
this path is empty. Also, there is a simple path P
(v
3
, v
1
) that uses vertices of K
only. Let v be the rst vertexof P
(v
3
, v
1
) that is also on P
. Now, let P
1
(v, a
1
) be
the part of P
.
7.1 Bridges and Kuratowskis Theorem 149
Corollary 7.1 Let Gbe a nonseparable graph; and C, a simple circuit in G. In
this case G is planar if and only if the bridges B
1
, B
2
, . . . , B
k
of G, with respect
to C, satisfy the following conditions:
1. For every 1 i k, C+B
i
is planar.
2. The set of bridges can be partitioned into two subsets, such that no two
bridges in the same subset interlace.
Let us introduce the coloring of graphs. Here, we consider only 2-coloring
of vertices. A graph G(V, E) is said to be 2-colorable if V can be partitioned
into V
1
and V
2
in such a way that there is no edge in G with two end vertices
in V
1
(V
2
). (Obviously, a 2-colorable graph is bipartite, and vice versa. It is
customary to use the termbipartite" if the partition is given, and 2-colorable"
if one exists.) The following theorem is due to Knig [1].
Theorem 7.1 A graph G is 2-colorable if and only if it has no odd length
circuits.
Proof: It is easy to see that if a graph has an odd length circuit, then it is not
2-colorable. In order to prove the converse, we may assume that Gis connected,
for if each component is 2-colorable, then the whole graph is 2-colorable.
Let v be any vertex. Let us perform BFS (see Section 1.5.1) starting from v.
There cannot be an edge u win Gif u and wbelong to the same layer, that
is, are the same distance fromv. For if such an edge exists then we can display
an odd length circuit as follows: Let P
1
(v, u) be a shortest path from v to u.
P
2
(v, w) is dened similarly and is of the same length. Let x be the last vertex
that is common to P
1
and P
2
. The part of P
1
fromx to u, and the part of P
2
from
x to ware of equal length, and together with u w, they form an odd length
simple circuit.
Now, we can color all the vertices of even distance fromv with one color and
all the vertices of odd distance fromv with a second color.
We can use the concept of 2-colorability to decide whether the bridges
B
1
, B
2
, . . . , B
k
, with respect to a simple circuit C, can be partitioned into two
pairwise noninterlacing subsets, as follows: Construct a graph G
whose ver-
tices are the bridges B
1
, B
2
, . . . , B
k
, and two vertices are connected by an edge
if and only if the corresponding bridges in G interlace. Now test whether the
graph G
.
Before we take on Kuratowskis theorem, we need a fewmore denitions and
a lemma.
Let G(V, E) be a nite nonseparable plane graph with |V| >2. A face of G is
a maximal part of the plane such that, for every two points x and y in it, there is
a continuous line fromx and y that does not share any point with the realization
of G. The contour of each face is a simple circuit of G; if any of these circuits
is not simple, then G is separable. Each of these circuits is called a window.
7.1 Bridges and Kuratowskis Theorem 151
e
1
e
5
e
2
e
4
e
3
Figure 7.3: G
goes through a
0
and b
0
, and the interior of C is either completely included
in the interior of C
has
a larger interior than C, in
G
0
. In the later case, we have to nd another plane
realization that is similar to G
0
, but the exterior of C
.
154 7 Planar Graphs
external bridge has at most one attachment in C[a
0
, b
0
] and C[b
0
, a
0
]. Since an
external bridge must have at least two attachments, it follows that neither a
0
nor b
0
can be an attachment of an external bridge.
It follows that each external bridge has at most two attachments. Since the
vertex connectivity is at least 3, we conclude that all external bridges are singular
(i.e., consist of a single edge).
Finally, there is at least one such external singular bridge; otherwise, one
could draw the edge e
0
outside C, to yield a planar realization of G. Every
external singular bridge interlaces with a
0
e
0
b
0
since the attachments are on
C(a
0
, b
0
) and C(b
0
, a
0
).
Similarly, there must be an internal bridge, B
interlaces with an
external singular bridge, say, a
1
e
1
b
1
. The situation is schematically shown
in Figure 7.6. We divide the argument to two cases according to whether B
has an attachment a
2
other than a
0
, b
0
, a
1
, b
1
. Without loss of
generality we may assume that a
2
is on C(a
1
, a
0
). Since B
interlaces e
1
, it must
have an attachment on C(b
1
, a
1
).
Case 1.1: B
has an attachment b
2
on C(b
1
, b
0
). In B
, there is a path P
connecting a
2
with b
2
. The situation is shown in Figure 7.7. The subgraph of
a
1
b
1
b
0
a
0
e
1
B
Figure 7.6: The bridge B
2
on C(a
0
, b
1
); that is, it may be b
1
but not a
0
. Also, B
has an attachment b
2
on
C[b
0
, a
1
). By Lemma 7.1, there exists a vertex v and three vertex-disjoint paths
in B
: P
1
(v, a
2
), P
2
(v, b
2
), and P
3
(v, b
2
). The situation is shown in Figure 7.8.
If we erase from the subgraph of G, shown in Figure 7.8 the edges in the path
C[b
1
, b
0
] and all its intermediate vertices, the resulting subgraph is homeomor-
phic to K
3,3
: Vertices a
2
, b
2
, and b
2
play the role of the upper vertices; and
a
0
, a
1
, and v, the lower vertices.
Case 2: B
and e
1
do not interlace; if
a
1
or b
1
are not, then B
: P
1
(v, a
0
), P
2
(v, b
0
), P
3
(v, a
1
), and P
4
(v, b
1
). This case is shown
in Figure 7.9, and the shown subgraph is clearly homeomorphic to K
5
.
Case 2.2: No vertex as in Case 2.1 exists. Let P
0
(a
0
, b
0
) and P
1
(a
1
, b
1
) be two
simple paths in B
. Let c
1
be the rst vertex on P
1
that is common with P
0
, and
let c
2
be the last on P
1
that is common with P
0
. We use only the rst part, A,
156 7 Planar Graphs
e
1
a
0
b
2
b
2
b
1
a
2
a
1
b
0
v
e
0
P
2
P
1
P
3
Figure 7.8: Case 1.2 in the proof of Theorem 7.2.
e
1
a
0
b
1
a
1
b
0
e
0
v
Figure 7.9: Case 2.1 in the proof of Theorem 7.2.
of P
1
, connecting a
1
and c
1
; and the last part, B, connecting c
2
with b
1
. The
pertaining subgraph of G is now shown in Figure 7.10, and is homeomorphic
to K
3,3
after the edges in C[a
0
, b
1
] and C[b
0
, a
1
] and all their intermediate
vertices are erased: Vertices a
0
, b
1
, and c
1
play the role of the upper vertices;
and b
0
, a
1
, and c
2
, the lower. (If c
1
is closer to a
0
than c
2
, then we erase C[a
1
, a
0
]
and C[b
1
, b
0
], instead, and the upper vertices are a
0
, a
1
and c
2
.)
Kuratowskis theorem provides a necessary and sufcient condition for a
graph to be planar. However, it does not yield an efcient algorithmfor planarity
7.2 Equivalence 157
e
1
a
0
b
1
a
1
b
0
e
0
c
1
c
2
A
B
P
0
Figure 7.10: Case 2.2. in the proof of Theorem 7.2.
testing. The obvious procedure, that of trying for all subsets of ve vertices to
see whether there are ten vertex disjoint paths connecting all pairs, or for every
pairs of three and three vertices whether there are nine paths, suffers from two
shortcomings. First there are
_
|V|
_
choices of ve-sets and 1/2
_
|V|
3
_
_
|V|3
3
_
choices of three, and three vertices; this alone is O(|V|
6
). But what is worse,
we have no efcient way to look for the disjoint paths; this problem may, in
fact, be exponential.
Fortunately, there are O(|E|) tests, as we shall see in the next chapter, for
testing whether a given graph is planar.
7.2 Equivalence
Let
G
1
and
G
2
be two plane realizations of the graph G. We say that
G
1
and
G
2
are equivalent if every window of one of them is also a window in the other.
G may be 2-connected and have nonequivalent plane realization; for example,
see Figure 7.11
Let us restrict our discussion to planar nite graphs with no parallel edges
and no self-loops. Our aim is to show that if the vertex connectivity of G, c(G),
is at least three then the plane realization of G is unique up to equivalence.
Lemma 7.5 A planar nonseparable graph G is 2-connected if there is a plane
realization of it,
G, and one of its windows has more than one bridge.
Proof: If C is a window of
G with more than one bridge, then all Cs bridges
are external. Therefore, no two bridges interlace. As in the rst paragraph of the
158 7 Planar Graphs
1
2
4
6 7
5
3
8
1
2 3
4
5
6
8
7
Figure 7.11: Two nonequivalent plane realizations of the same graph.
proof of Lemma 7.2, there exists a bridge B whose attachments can be ordered
a
1
, a
2
, . . . a
t
, and no attachments of any other bridge appear on C(a
1
, a
t
). It
is easy to see that {a
1
, a
t
} is a separating pair; it separates the vertices of B
and C(a
1
, a
t
) fromthe set of vertices of all other bridges and C(a
t
, a
1
), where
neither set can be empty since G has no parallel edges.
Theorem7.3 If Gis a plane graph with no parallel edges and no self-loops and
if its vertex connectivity, c(G), is at least 3, then every two plane realizations
of G are equivalent.
Proof: Assume that Ghas two plane realizations
G
1
and
G
2
that are not equiv-
alent. Without loss of generality we may assume that there is a windowCin
G
1
that is not a windowin
G
2
. Therefore, Chas at least two bridges; one interior and
one exterior. By Lemma 7.5, and since C is a window in
G
1
, G is 2-connected.
A contradiction, since c(G) 3.
7.3 Eulers Theorem
The following theorem is due to Euler.
Theorem 7.4 Let G(V, E) be a nonempty connected plane graph. The number
of faces, f, satises
|V| +f |E| = 2. (7.1)
7.4 Duality 159
Proof: By induction on |E|. If |E| =0, then Gconsists of one vertex and there is
one face, and Equation 7.1 holds. Assume that the theorem holds for all graphs
with m = |E|. Let G(V, E) be a connected plane graph with m+1 edges. If
G contains a circuit, then we can remove one of its edges. The resulting plane
graph is connected and has medges and, therefore, by the inductive hypothesis,
satises Equation 7.1. Adding back the edge increases the number of faces by
one and the number of edges by one, and thus Equation 7.1 is maintained. If G
contains no circuits, then it is a tree. By Corollary 2.1, it has at least two leaves.
Removing a leaf and its incident edge yields a connected graph with one less
edge and one less vertex, which satises Equation 7.1. Therefore, G satises
Equation 7.1 too.
The theorem implies that all connected plane graphs with |V| vertices and |E|
edges have the same number of faces. One can draw many conclusions from
the theorem. Some of them are the following:
Corollary 7.2 If G(V, E) is a connected plane graph with no parallel edges,
no self-loops and |V| >2, then
|E| 3|V| 6. (7.2)
Proof: Since there are no parallel edges, every window consists of at least
three edges. Each edge appears on the windows of two faces, or twice on the
window of one face. Thus, 3 f 2 |E|. By Equation 7.1, |E| = |V| +f 2.
Thus, |E| |V| +2/3|E| 2, and (7.2) follows.
Corollary 7.3 Every connected plane graph with no parallel edges and no
self-loops has at least one vertex whose degree is 5 or less.
Proof: Assume the contrary; that is, the degree of everyvertexis at least 6. Thus,
2 |E| 6 |V|; note that each edge is counted in each of its two end-vertices.
This contradicts (7.2).
7.4 Duality
Let G(V, E) be a nite undirected and connected graph. A set KE is called a
cutset if it is a minimal separating set of edges; that is, the removal of K from
G interrupts its connectivity, but no proper subset of K does it. It is easy to
see that a cutset separates Ginto two connected components: Consider rst the
removal of K{e}, where e K. Gremains connected. Nowremove e. Clearly,
G breaks into two components.
160 7 Planar Graphs
x
6
x
1
x
5
x
3
x
4 x
2
y
3
y
2
y
4
y
5
y
1
y
6
z
5
z
2
z
6
z
1
z
4
z
3
Figure 7.12: (a) A connected graph G
1
; (b) G
2
, a dual of G
1
; (c) G
3
,
also a dual of G
1
.
The graph G
2
(V
2
, E
2
) is said to be the dual of a connected graph G
1
(V
1
, E
1
)
if there is a 1 1 correspondence f : E
1
E
2
, such that a set of edges S forms
a simple circuit in G
1
if and only if f(S) (the corresponding set of edges in
G
2
) forms a cutset in G
2
. Consider the graph G
1
shown in Figure 7.12 (a). G
2
shown in Figure 7.12(b) is a dual of G
1
, but so is G
3
, shown in Figure 7.12(c),
as the reader can verify by considering all (six) simple circuits of G
1
and all
cutsets of G
2
, or G
3
.
Acontraction of an edge x
e
yof a graph G(V, E) is the following operation:
Delete the edge e, and merge x with y. The new contracted graph, G
, has one
less edge and one less vertex, if x ,=y. Clearly, if Gis connected, so is G
. Also,
graph G
from G.
Lemma 7.6 If a connected graph G
1
has a dual and G
1
is a connected subgraph
of G
1
, then G
1
has a dual.
Proof: We can get G
1
from G
1
by a sequence of two kinds of deletions:
7.4 Duality 161
1. A deletion of an edge e of the present graph, which is not in G
1
, and whose
deletion does not interrupt the connectivity of the present graph.
2. Adeletion of a leaf of the present graph, which is not a vertex of G
1
, together
with its incident edge.
We want to showthat each of the resulting graphs, starting with G
1
and ending
with G
1
, has a dual.
Let Gbe one of these graphs, except the last, and its dual be G
d
. First consider
a deletion of type (7.4), of an edge e. Construct f(e) in G
d
, to get G
dc
. If C
is a simple circuit in Ge, then clearly it cannot use e, and therefore it is a
circuit in G too. The set of edges of C is denoted by S. Thus, f(S) is a cutset of
G
d
, and it does not include f(e). Thus, the end vertices of f(e) are in the same
component of G
d
with respect to f(S). It follows that f(S) is a cutset of G
dc
too. If K is a cutset of G
dc
, then it is a cutset of G
d
too. Thus, f
1
(K) form a
simple circuit C
.
Hence, C
G
2
(V
2
, E
2
) as follows: The set of lines connecting the p
i
s is a representation
of E
2
. The 1 1 correspondence f : E
1
E
2
is dened as follows: f(e) is the
edge of
G
2
which crosses e. Clearly,
G
2
is a plane graph that is a realization
of a graph G
2
(V
2
, E
2
). It remains to show that there is a 1 1 correspondence
of the simple circuits of G
1
to the cutsets of G
2
. The construction described
3
An edge whose removal from G
1
interrupts its connectivity.
7.4 Duality 163
Figure 7.13: For the proof of Theorem 7.5.
above is demonstrated in Figure 7.13, where
G
1
is shown in solid lines, and
G
2
is shown in dashed lines.
Let C be a simple circuit of G
1
. Clearly, in
G
1
, C describes a simple closed
curve in the plane. There must be at least one vertex of
G
2
inside this circuit,
since at least one edge of
G
2
crosses the circuit, and it crosses exactly once.
The same argument also applies to the outside. This implies that f(S), where S
is the set of edges of C, forms a separating set of G
2
. Let us postpone the proof
of the minimality of f(S) for a little while.
Now let K be a cutset of G
2
. Let T and
T be the sets of vertices of the two
components of G
2
formed by the deletion of K. The set of faces of
G
1
that
correspond to the vertices of T, forms a continuous region, but not the whole
plane. The minimality of K implies that the boundary of this region is a circuit
in G
1
, whose edges correspond to K. Thus, we have shown that every simple
circuit of G
1
corresponds to a separating set of G
2
, and every cutset of G
2
corresponds to a circuit of G
1
.
Now let us handle the minimality. If S is the set of edges of a simple circuit C
of G
1
and f(S) is not a cutset of G
2
, then there is a proper subset of f(S) which
is a cutset, say K. Therefore, f
1
(K) is a circuit of G
1
. However, f
1
(K) S. A
contradiction to the assumption that C is simple. The proof that if K is a cutset
then f
1
(K) is a simple circuit is similar.
This completes the proof that the connected planar graph has a dual. We turn
to the proof that a nonplanar graph has no dual. Here, we follow the proof of
Parson [3].
First, let us show that neither K
3,3
nor K
5
have a dual. In K
3,3
, the shortest
circuit is of length four, and for every two edges there exists a simple circuit
that one but does not use the other. Thus, in its dual no cutset consists of less
164 7 Planar Graphs
than four edges and there are no parallel edges. Therefore, the degree of each
vertex is at least 4 and there must be at least ve vertices. The number of edges
is therefore at least (5 4/2) = 10, while K
3,3
has 9 edges. Thus, K
3,3
has no
dual. In the case of K
5
, it has ten edges, ten simple circuits of length three and no
shorter circuits, and for every two edges there is a simple circuit that uses one
but not the other. Thus, the dual must have ten edges, ten cutsets of three edges,
no cutset of lesser size and therefore the degree of every vertex is at least three.
Also, there are no parallel edges in the dual. If the dual has ve vertices, then it is
K
5
itself (ten edges and no parallel edges), but K
5
has no cutsets of three edges. If
the dual has seven vertices or more, then it has at least eleven edges (,7 3/2|).
Thus, the dual must have six vertices. Since it has ten clusters of three edges,
there is one that separates S from
S, where neither consists of a single vertex.
If |S| = 2, then it contains a vertex whose degree is 2. If |S| = |
S| = 3, then the
maximum number of edges in the dual is nine. Thus, K
5
has no dual.
Now, if G is a nonplanar graph with a dual, then by Kuratowskis theorem
(Theorem7.2) it contains a subgraph G
, which is constructed
by the procedure in the rst part of the proof of Theorem 7.5, is called its
geometric dual.
Prove that if G
.
7.5 Problems 167
Bibliography
[1] Knig, D., Theorie der endlichen und unendlichen Graphen. Leipzig, 1936.
Reprinted Chelsea, 1950.
[2] Kuratowski, K., Sur le problme des Courbes Gauches en Topologie, Fund. Math.,
Vol. 15, 1930, pp. 217283.
[3] Parson, T. D., On Planar Graphs, Am. Math. Monthly, Vol. 78, No. 2, 1971,
pp. 176178.
[4] Harary, F., Graph Theory, Addison Wesley, 1969.
[5] Ore, O., The Four-Color Problem, Academic Press, 1976.
[6] Wilson, R. J., Introduction to Graph Theory, Longman, 1972.
8
Testing Graph Planarity
8.1 Introduction
There are two known planarity testing algorithms that have been shown to be
realizable in a way that achieves linear time (O(|V|)). The idea in both is to
follow the decisions to be made during the planar construction of the graph,
piece by piece, as to the relative location of the various pieces. The construction
is not carried out explicitly because there are difculties, such as the crowding
of elements into a relatively small portion of the area allocated, which, as yet,
we do not knowto avoid. Also, an explicit drawing of the graph is not necessary,
as we shall see, to decide whether such a drawing is possible. We shall imagine
that such a realization is being carried out, but will only decide where the various
pieces are laid relative to each other, and not their exact shape. Such decisions
may change later to make a place for later additions of pieces. In both cases, it
was shown that the algorithmterminates within (O(|V|)) steps, and if it fails to
nd a realization, then none exists.
The rst algorithmstarts by nding a simple circuit and adding toit one simple
path at a time. Each such new path connects two old vertices via new edges
and vertices. (Whole pieces are sometimes ipped over, around some line).
Thus, we call it the path addition algorithm. The basic ideas were suggested
by various authors, such as Auslander and Parter [1] and Goldstein [2], but the
algorithm in its present form, both from the graph-theoretic point of view, and
complexity point of view, is the contribution of Hopcroft and Tarjan [3]. They
were rst to show that planarity testing can be done in linear time.
The second algorithm adds one vertex in each step. Previously drawn edges
incident to this vertex are connected to it, and new edges incident to it are
drawn and their other endpoints are left unconnected. (Here, too, sometimes
whole pieces have to be ipped around or permuted). The algorithm is due to
Lempel, Even, and Cederbaum [4]. It consists of two parts. The rst part was
168
8.2 The Path Addition Algorithm of Hopcroft and Tarjan 169
shown to be linearly realizable by Even and Tarjan [5]; the second part was
shown to be linearly realizable by Booth and Leuker [6]. We call this algorithm
the vertex addition algorithm.
Each of these algorithms can be divided into its graph-theoretic part and
its data structures and their manipulation. The algorithms are fairly complex
and a complete description and proof would require a much more elaborate
exposition. Thus, since this is a book on graphs, and not on programming, I
have chosen to describe in full the graph-theoretic aspects of both algorithms
and to only briey describe the details of the data manipulation techniques.
An attempt is made to convince the reader that the algorithms work, but in
order to see the details which make it linear one will have to refer to the papers
mentioned above.
Throughout this chapter, for reasons explained in Chapter 7, we shall assume
that G(V, E) is a nite undirected graph with no parallel edges and no self-
loops. We shall also assume that G is nonseparable. The rst thing that we can
do is check whether |E| 3 |V| 6. By Corollary 7.1, if this condition does
not hold, then G is nonplanar. Thus, we can restrict our algorithm to the cases
where |E| =O(|V|).
8.2 The Path Addition Algorithm of Hopcroft and Tarjan
The algorithmstarts with a DFS of G. We assume that Gis nonseparable. Thus,
we drop from the DFS the steps for testing nonseparability. However, we still
need the lowpoint function, to be denoted now L1(v). In addition, we need the
second lowpoint function, L2(v), to be dened as follows: Let S(v) be the set
of values k(u) of vertices u reachable from descendants of v by a single back
edge. Clearly, L1(v) = min{{k(v)} S(v)}.
Dene
L2(v) = min{{k(v)} [S(v) {L1(v)}]}.
Let us now rewrite the DFS in order to compute these functions (we denote
an assignment statement, x gets the value of y by x :=y):
1. Mark all the edges unused. For every v V, let k(v) := 0. Let i := 0 and
v :=s (the vertex s is where we choose to start the DFS).
2. i :=i +1, k(v) :=i, L1(v) :=i, L2(v) :=i.
3. If v has no unused incident edges, go to Step (8.2).
4. Choose an unused incident edge v
e
u. Mark e used. If k(u) ,= 0 then
do the following:
If k(u) <L1(v), then L2(v) :=L1(v), L1(v) :=k(u).
170 8 Testing Graph Planarity
If k(u) >L1(v), then L2(v) := min{L2(v), k(u)}.
Go to Step (8.2).
Otherwise (k(u) = 0), let f(u) :=v, v :=u and go to Step (8.2).
5. If k(v) =1, halt.
6. (k(v) >1; we backtrack).
If L1(v) < L1(f(v)) then L2(f(v)) := min{L2(v), L1(f(v))}, L1(f(v))
:=L1(v).
If L1(v) =L1(f(v)), then L2(f(v)) := min{L2(v), L2(f(v))}.
Otherwise (L1(v) >L1(f(v))), L2(f(v)) := min{L1(v), L2(f(v))}.
v :=f(v).
Go to Step (8.2).
Fromnowon we refer to the vertices by their k(v) number; that is, we change
the name of v to k(v).
Let A(v) be the adjacency list of v; that is, the list of edges incident fromv.
We remind the reader that after the DFS each of the edges is directed; the tree
edges are directed from low to high, and the back edges are directed from high
to low. Thus, each edge appears once in the adjacency lists. Now, we want to
reorder the adjacency lists, but rst, we must dene an order on the edges. Let
the value (e) of an edge u
e
v be dened as follows:
(e) =
2 v if u
e
v is a back edge.
2 L1(v) if u
e
v is a tree edge and L2(v) u.
2 L1(v) +1 if u
e
v is a tree edge and L2(v) <u.
Next, we order the edges in each adjacency list to be in nondecreasing order
with respect to . [This can be done on O(|V|) time as follows. First, compute for
each edge its value. Prepare 2 |V| +1 buckets, numbered 1, 2, . . . , 2 |V| +1.
Put each e into the (e) bucket. Now, empty the buckets in order, rst bucket
number 1, then 2 and so on, putting the edges taken out into the proper new
adjacency lists, in the order that they are taken out of the buckets.]
The new adjacency lists are now used, in a second run of a DFS algorithm, to
generate the paths, one by one. In this second DFS, vertices are not renumbered,
and there is no need to recompute f(v), L1(v) or L2(v). The tree remains the
same, although the vertices may not be visited in the same order. The paths
nding algorithm is as follows:
1. Mark all edges unused and let v := 1.
2. Start the circuit C; its rst vertex is 1.
8.2 The Path Addition Algorithm of Hopcroft and Tarjan 171
3. Let the rst unused edge in A(v) be v
e
u.
If u ,= 1, then mark e as used, add u to C, update v := u, and repeat
Step (3). Otherwise, (u = 1), C is closed. Output C.
4. If v =1, halt.
5. If every edge in A(v) is used, then update v :=f(v) and go to Step (4).
6. Start a path P with v as its rst vertex.
7. Let v
e
u be the rst unused edge in A(v).
If e is a back edge (u <v), terminate P with u, output P, and go to (5).
8. Otherwise, (e is a tree edge). Add u to P, update v :=u, and go to Step (7).
Lemma 8.1 The paths nding algorithm nds rst a circuit C that consists of
a path from 1 (the root) to some vertex v, via tree edges, and a back edge from
v to 1.
Proof: Let 1 u be the rst edge of the tree to be traced (in the rst appli-
cation of Step (3)). We assume that G is nonseparable, and |V| > 2. Thus, by
Lemma 3.7, this edge is the only tree edge out of 1, and u=2. Also, 2 has some
descendants, other than itself. Clearly, 2 3 is a tree edge. By Lemma 3.5,
L1(3) <2, that is, L1(3) =1. Thus, L1(2) =1. The reordering of the adjacency
lists assures that the rst path to be chosen out of 1 will lead back to 1 as
claimed.
Lemma 8.2 Each generated path P is simple, and it contains exactly two ver-
tices in common with previously generated paths; they are the rst vertex, f,
and the last, l.
Proof: The edge scanning during the paths nding algorithm is in a DFS
manner, in accord with the structure of the tree (but not necessarily in the same
scanning order of vertices). Thus, a path starts from some (old) vertex f, goes
along tree edges, via intermediate vertices which are all new, and ends with
a back edge which leads to l. Since back edges always lead to ancestors, l is
old. Also, by the reordering of the adjacency lists and the assumption that G is
nonseparable, l must be lower than f. Thus, the path is simple.
Let S
v
denote the set of descendants of v, including v itself.
Lemma 8.3 Let f and l be the rst and last vertices of a generated path P and
f v be its rst edge.
1. If v ,=l then L1(v) =l.
2. l is the lowest vertex reachable from S
f
via a back edge that has not been
used in any path yet.
172 8 Testing Graph Planarity
Proof: Let us partition S
f
{f} into two parts, and , as follows. Adescendant
ubelongs to if and only if when the construction of P begins, we have already
backtracked from u. Clearly, f and all the back edges out of have been
scanned already. Let u be the lowest vertex reachable via an unused back edge
from. Clearly, the rst remaining (unused) edge of A(f) is the beginning of a
directed path to u, which is either an unused back edge fromf to u or a path of
tree edges, via vertices of , followed by a single back edge to u. Thus, u =l,
and the lemma follows.
Lemma 8.4 Let P
1
and P
2
be two generated paths whose rst and last ver-
tices are f
1
, l
1
and f
2
, l
2
, respectively. If P
1
is generated before P
2
and f
2
is a
descendant of f
1
then l
1
l
2
.
Proof: The lemma follows immediately from Lemma 8.3.
So far, we have not made any use of L2(v). However, Lemma 8.5 relies on it.
Lemma 8.5 Let P
1
: f
e
1
v
1
. . . l and P
2
: f
e
2
v
2
. . . l be
two generated paths, where P
1
is generated before P
2
. If v
1
,=l and L2(v
1
) <f,
then v
2
,=l and L2(v2) <f.
Proof: By the denition of (e), (e
1
) = 2 l +1. If v
2
= l or L2(v2) f,
then (e
2
) =2 l, and e
2
should appear in A(f) before e
1
. A contradiction.
Let Cbe 1 v
1
v
2
. . . v
n
l. Clearly 1 <v
1
<v
2
<. . . <v
n
.
Consider now the bridges of G with respect to C.
Lemma 8.6 Let B be a nonsingular bridge of G with respect to C, whose
highest attachment is v
i
. There exists a tree edge v
i
e
u that belongs to B,
and all other edges of B with endpoints on C are back edges.
Proof: The lemma follows from the fact that the paths nding algorithm is
a DFS. First C is found. We then backtrack from a vertex v
j
only if all its
descendants have been scanned. No internal part of B can be scanned before
we backtrack into v
i
. There must be a tree edge v
i
u, where u belongs to
B, for the following reasons. If all the edges of B, incident to v
i
are back edges,
they all must come fromdescendants or go to ancestors of v
i
(see Lemma 3.4).
An edge from v
i
to one of its ancestors (which must be on C) is a singular
bridge and is not part of B. An edge from a descendant w of v
i
implies that w
cannot be in B, for it has been scanned already, and we have observed that no
internal part of B can be scanned before we backtrack into v
i
. If any other edge
v
k
x of B is also a tree edge, then, by denition of a bridge, there is a path
8.2 The Path Addition Algorithm of Hopcroft and Tarjan 173
connecting u and x that is vertex-disjoint from C. Along this path there is at
least one edge that contradicts Lemma 3.4.
Corollary 8.1 Once a bridge B is entered, it is completely traced before
it is left.
Proof: By Lemma 8.6, there is only one edge through which B is entered.
Since eventually the whole graph is scanned, and no edge is scanned twice in
the same direction, the corollary follows.
Assuming that C and the bridges explored from vertices higher than v
i
have
already been explored and drawn in the plane. Lemma 8.7 provides a test for
whether the next generated path could be drawn inside; the answer is negative,
even if the path itself can be placed inside, but it is already clear that the whole
bridge to which it belongs cannot be placed there.
Lemma 8.7 Let v
i
u
1
u
2
. . . u
l
(=v
j
) be the rst path, P, of
the bridge B to be generated by the paths nding algorithm. P can be drawn
inside (outside) C if there is no back edge w v
k
drawn inside (outside)
for which j < k < i. If there is such an edge, then B cannot be drawn inside
(outside).
Proof: The sufciency is immediate. If there is no back edge drawn inside C,
that enters the path, v
j
v
j+1
v
i
in one of its internal vertices,
then there cannot be any inside edge incident to these vertices, since bridges to
be explored fromvertices lower than v
i
have not been scanned yet. Thus, P can
be drawn inside if it is placed sufciently close to C.
Now, assume there is a back edge wv
k
, drawn inside, for which j <k <i.
Let P
to which it belongs.
Case 1: p > i. The bridges B and B
to be generated, P
: v
i
x
1
x
2
. . . v
q
. By Lemma 8.4, q j, since B
is explored before B.
Case 2.1: q<j. Since v
i
and v
j
are attachments of B, v
k
and v
q
are attachments
of B
and q < j < k < i, the two bridges interlace. Thus, B cannot be drawn
inside.
174 8 Testing Graph Planarity
Case 2.2: q = j. P
interlace by either part (i) or part (ii) of the denition of interlacement, and
B cannot be drawn inside.
The algorithm assumes that the rst path of the new bridge B is drawn inside
C. Now, we use the results of Corollary 7.1, Theorem 7.1 and the discussion
that follows it, to decide whether the part of the graph explored so far is planar,
assuming that C+B is planar. By Lemma 8.7, we nd which previous bridges
interlace with B. The part of the graph explored so far is planar if and only if
the set of its bridges can be partitioned into two sets such that no two bridges in
the same set interlace. If the answer is negative, the algorithm halts, declaring
that graph nonplanar. If the answer is positive, we still have to check whether
C+B is planar.
Let the rst path of B be P: v
i
u
1
u
2
. . . v
j
. We now have a
circuit C
consisting of C[v
j
, v
i
] and P. The rest of Cis an outside path P
, with
respect to C
2
such that v
j
<v
k
<v
i
then clearly the attachments of B (in C(1, v
i
)) form
a mew block (assuming C+B is planar), and the lemma holds. However, if
there are vertices of
1
or
2
in between v
j
and v
i
, then by Lemma 8.7, the
bridges they are attachments of all interlace with B. Thus, the old blocks which
these attachments belong to, must now be merged into one new block with
the attachments of B (in C(1, v
i
)). Now, let v
l
be the lowest vertex of the new
block and v
h
be the highest. Clearly, v
l
was the lowest vertex of some old block
whose highest vertexwas v
h
, and v
h
>v
j
. Thus, by the inductive hypothesis, no
attachment of another block could be between v
l
and v
h
and therefore cannot
be in this region after the merger. Also, all the attachments in between v
j
and
v
h
are in the new block since they are attachments of bridges which interlace
with B. Thus, all the entries of
1
or
2
which are in between v
l
and v
h
belong
to the new block.
Corollary 8.2 The entries of one block appear consecutively on
1
(
2
).
Thus, when we consider the rst path P : v
i
. . . v
j
of the new bridge
B in order to decide whether it can be drawn inside, we check the top entries
t
1
and t
2
of
1
and
2
, respectively.
If v
j
t
1
and v
j
t
2
, then no merger is necessary; the attachments of B (in
C(1, v
i
)) are entered as a block (if C+B is found to be planar) on top of
1
.
If v
j
<t
1
and v
j
t
2
, then we rst join all the blocks for which their highest
entry is higher than v
j
. To this end there is no need to check
2
, since v
j
t
2
;
176 8 Testing Graph Planarity
however, several blocks still may merge. Next, switch the sections of the new
block, that is, the section on
1
exchanges places with the section on
2
. Finally,
place the attachments of B in nondecreasing order on top of
1
; these entries
join the new block.
If v
j
t
1
and v
j
<t
2
, then again we join all blocks whose highest element is
greater than v
j
; only the sections on
2
need to be checked. The attachments
of B join the same block are placed on top of
1
.
If v
j
<t
1
and v
j
<t
2
, then all blocks whose highest element is greater than v
j
are joined into one new block. As we join the blocks, we examine them one by
one. If the highest entry in the section on
1
is higher than v
j
, then we switch the
sections. If it is still higher, then we halt and declare the graph nonplanar. If all
these switches succeed, then the merger is completed by adding the attachments
of B on top of
1
.
In order to handle the sections switching, the examination of their tops and
mergers efciently, we use a third stack,
3
. It consists of pairs of pointers, one
pair (x, y) per block; x points to the lowest entry of the section in
1
; and y, to
the lowest entry of the section in
2
. (If the section is empty, then the pointers
value is 0). Two adjacent blocks are joined by simply discarding the pair of the
top one. When several blocks have to be joined together upon the drawing of
the rst path of a new bridge, only the pair of the lowest of these blocks need
remain, except when one of its entries is 0. In this case, the lowest nonzero entry
of the pairs above it on the same side, if any such entry exists, takes its place.
When we enter a recursive step, a special end of stack marker E is placed on
top of
2
, and the three stacks are used as in the main algorithm. If the recursive
step ends successfully, we rst attempt to switch sections for each of the blocks
with a nonempty section on
2
, above the top most E. If we fail to expose E,
then C+B is nonplanar and we halt. Otherwise, all the blocks created during
the recursion are joined to the one which includes v
j
(the end vertex of the rst
path of B). The exposed E, on top of
2
, is removed, and we continue with the
previous level of recursion. When we backtrack into a vertex v
i
, all occurrences
of v
i
are removed from the top of
1
and
2
, together with pairs of pointers of
3
which point to removed entries on both
1
and
2
. (Technically, instead of
pointing to an occurrence of v
i
, we point to 0 and pairs (0, 0) are removed).
Theorem 8.1 The complexity of the path addition algorithm is O(|V|).
Proof: As in the closing remarks of Section 8.1, we can assume |E| =O(|V|).
The DFSand the reordering of the adjacency lists have been shown to be O(|V|).
Each edge in the paths nding algorithm is used again, once in each direction.
The total number of entries in the stacks
1
and
2
is bounded by the number of
8.3 Computing an st-Numbering 177
back edges (|E| |V| +1), and is therefore O(|V|). After each section switching
the number of blocks is reduced by one; thus the total work invested in section
switchings is O(|V|).
8.3 Computing an st-Numbering
In this section, we dene an st-numbering and describe a linear time algorithm
to compute it. Thus numbering is necessary for the vertex addition algorithm,
for testing planarity, of Lempel, Even, and Cederbaum.
Given any edge s t of a nonseparable graph G(V, E), a 1-1 function
g : V {1, 2, . . . , |V|} is called an st-numbering if the following conditions are
satised:
1. g(s) = 1.
2. g(t) = |V| (=n).
3. For every v V {s, t} there are adjacent vertices u and wsuch that g(u) <
g(v) <g(w).
Lempel, Even, and Cederbaum showed that, for every nonseparable graph
and every edge s t, there exists an st-numbering. The algorithm described
here, following the work of Even and Tarjan [5], achieves this goal in linear
time.
The algorithm starts with a DFS whose rst vertex is t and rst edge is
t s. (i.e., k(t) = 1 and k(s) = 2). This DFS computes for each vertex v, its
DFS number, k(v), its father, f(v), and its lowpoint L(v) and distinguishes tree
edges fromback edges. This information is used in the paths-nding algorithm
to be described next, which is different from the one used in the path addition
algorithm.
Initially, s, t, and the edge connecting them are marked old, and all the
other edges and vertices are marked new. The path-nding algorithm starts
from a given vertex v and nds a path from it. This path may be directed from
v or into v.
1. If there is a new back edge v
e
w (in this case k(w) < k(v)), then do
the following:
Mark e old.
The path is v
e
w.
Halt.
2. If there is a new tree edge v
e
w(in this case k(w) >k(v)), then do the
following:
178 8 Testing Graph Planarity
Trace a path whose rst edge is e, and fromthere it follows a path that dened
L(w), that is, it goes up the tree and ends with a back edge into a vertex u
such that k(u) =L(w). All vertices and edges on the path are marked old.
Halt.
3. If there is a new back edge w
e
v (in this case k(w) > k(v)), then do
the following:
Start the path with e (going backwards on it) and continue backwards via
tree edges until you encounter an old vertex. All vertices and edges on the
path are marked old. Halt.
4. (All edges incident to v are old). The path produced is empty. Halt.
Lemma 8.9 If the path nding algorithm is always applied from an old
vertex v ,=t, then all the ancestors of an old vertex are old too.
Proof: By induction on the number of applications of the path nding algo-
rithm. Clearly, before the rst application, the only ancestor of s is t, and it is
old. Assuming the statement is true up to the present application, it is easy to
see that if any of the four steps is applicable, the statement continues to hold
after its application.
Corollary 8.3 If G is nonseparable and under the condition of Lemma 8.9,
each application of the path-ndingalgorithmfroman old vertex v produces
a path, through new vertices and edges, to another old vertex, or in case
all edges incident to v are old, it returns the empty path.
Proof: The only case which requires a discussion is when case (2) of the path
nding algorithm is applied. Since G is nonseparable, by Lemma 3.5, L(w) <
k(v). Thus, the path ends bellow v, in one of its ancestors. By Lemma 8.9,
this ancestor is old.
We are now ready to present the algorithmwhich produces an st-numbering.
It uses a stack S that initially contains only t and s, s on top of t.
1. i 1.
2. Let v be the top vertex on S. Remove v from S. If v =t then g(t) i and
halt.
3. (v ,= t) Apply the path nding algorithm to v. If the path is empty, then
g(v) i, i i +1 and go to Step (2).
4. (The path is not empty) Let the path be v u
1
u
2
. . . u
l
w.
Put u
l
, u
l1
, . . . , u
2
, u
1
, v on S in this order (v comes out on top), and go to
Step (2).
8.4 The Vertex Addition Algorithm of Lempel, Even, and Cederbaum 179
Theorem 8.2 The algorithm above computes an st-numbering for every
nonseparable graph G(V, E).
Proof: First, we make a few observations about the algorithm:
1. No vertex ever appears in two or more places on S at the same time.
2. Once a vertex v is placed on S, nothing under v receives a number until v
does.
3. A vertex is permanently removed from S only after all its incident edges
become old.
Next, we want to show that each vertex v is placed on S before t is removed.
Since t and s are placed on S initially, the statement needs to be proved for
v ,= s, t only. Since G is nonseparable, there exists a simple path from s to v
which does not pass through t (see Theorem 6.7 part (??)). Let this path be
s =u
1
u
2
u
l
= v. Let m be the rst index such that u
m
is not
placed on S. Since u
m1
is placed on S, t can be removed only after u
m1
(fact (ii)), and u
m1
is removed only after all its incident edges are old (fact
(ii)). Thus, u
m
must be placed on S before t is removed.
It remains to be shown that the algorithm computes an st-numbering.
Since each vertex is placed on S and eventually is removed, each vertex v
gets a number g(v). Clearly, g(s) = 1, for it is the rst to be removed. After
each assignment i is incremented. Thus, g(t) = |V|. Every other vertex v is
placed on S, for the rst time, as an intermediate vertex on a path. Thus, there is
an adjacent vertex stored below it, and an adjacent vertex stored above it. The
one above it (by fact (ii)) gets a lower number and the one below it, a higher
number.
It is easy to see that the whole algorithm is of time complexity O(|E|): First,
the DFS is O(|E|). The total time spent on path nding is also O(|E|) since
no edge is used more than once. The total number of operations in the main
algorithm is bounded also by O(|E|) because the number of stack insertions is
exactly |E| +1.
8.4 The Vertex Addition Algorithm of Lempel, Even, and Cederbaum
In this section, we assume that G(V, E) is a nonseparable graph whose vertices
are st-numbered. Fromnowon, we shall refer to the vertices by their st-number.
Thus, V = {1, 2, . . . , n}. Also, the edges are now directed from low to high.
A (graphical) source of a digraph is a vertex v such that d
in
(v) =0; a (graph-
ical) sink is a vertex v such that d
out
(v) = 0. (Do not confuse with the source
180 8 Testing Graph Planarity
2
(b) (a)
3
2
1
3
4
5 6
1 4
6 5
6 5
4
Figure 8.1: (a) An example of a digraph; (b) B
3
of the digraph from (a).
and sink of a network. The source of a network is not necessarily a (graphical)
source, etc.) Clearly, vertex 1 is a source of G and vertex n is a sink. Further-
more, due to the st-numbering, no other vertex is either a source or a sink. Let
V
k
= {1, 2, . . . , k}. Then, G
k
(V
k
, E
k
) is the digraph induced by V
k
, that is, E
k
consists of all the edges of G whose endpoints are both in V
k
.
If Gis planar, let
Gbe a plane realization of G. It contains a plane realization
of G
k
. The following simple lemma reveals the reason for the st-numbering.
Lemma 8.10 If
G
k
is a plane realization of G
k
contained in a plane digraph
H
2
, respectively. If the ys do not appear in the same order on the outside
windows of
H
1
and
H
2
, then there are two ys, y
i
and y
j
which are next to each
other in
H
1
but not in
H
2
(see Fig. 8.4). Therefore, in
H
2
, there are two other
ys, y
k
and y
l
which interpose between y
i
and y
j
on the two paths between
them on the outside window of
H
2
. However, from
H
1
we see that there are
two paths, P
1
[y
i
, y
j
] and P
2
[y
k
, y
l
], which are completely disjoint. These two
paths cannot exist simultaneously in
H
2
. A contradiction.
8.4 The Vertex Addition Algorithm of Lempel, Even, and Cederbaum 183
7
2
3
1
4
5
6
9 8 9 8 9
2
3
1
5
7
6
4
9
5
8
4
9
3
2
1
9 8
6
7
9
(a) (b)
(c) (d)
8 9 9 8 8 9 8 9
9 8
9 8 9
5
8
4
9
7
6
3
2
1
8 9
Figure 8.3: (a) Abush form; (b) a bush formobtained by permuting (a)
about vertex 1; (c) a bush formobtained by ipping (a) about vertex 1;
(d) a bush form obtained by ipping (c) about vertex 2.
y
i
y
i
y
k
y
j
y
l
y
j
H
1
H
2
Figure 8.4: Proof of Lemma 8.12.
Theorem 8.3 If
B
1
k
and
B
2
k
are bush forms of the same B
k
, then there exists a
sequence of permutations and ippings that transforms
B
1
k
into
B
3
k
, such that
in
B
2
k
and
B
3
k
, the virtual vertices appear in the same order.
Proof: By induction on the size of bush or subbush forms.
1
Clearly, if each
of the two (sub-)bushes consists of only one vertex and one virtual vertex,
then the statement is trivial. Let v be the lowest vertex in the (sub-)bushes
1
Size the number of vertices.
184 8 Testing Graph Planarity
B
1
and
B
2
of the same B. If v is a separation vertex, then the components of
B appear as subbushes in
B
1
and
B
2
. If they are not in the same order, it is
possible, by permuting them in
B
1
, to put them in the same order as in
B
2
. By
the inductive hypothesis, there is a sequence of permutations and ippings that
will change each subbush of
B
1
to have the same order of its virtual vertice as
in its counterpart in
B
2
, and therefore the theorem follows.
If v is not a separating vertex then let Hbe the maximal nonseparable compo-
nent of B which contains v. In
B
1
(
B
2
) there is a planar realization
H
1
(
H
2
) of H.
The vertices y
1
, y
2
, . . . , y
m
of H, which are also endpoints of edges of BH,
by Lemma 8.12, must appear on the outside windowof Hin the same order, up
to orientation. If the orientation of the ys in
H
1
is opposite to that of
H
2
, ip
the (sub-)bush
B
1
about v. Now, each of the ys is the lowest vertex of some
subbush of B, and these subbushes appear in (the new)
B
1
and
B
2
in the same
order. By the inductive hypothesis, each of these subbushes can be transformed
by a sequence of permutations and ippings to have its virtual vertices in the
same order as its counterpart in
B
2
.
Corollary 8.4 If G is planar and
B
k
is a bush form of B
k
, then there exists a
sequence of permutations and ippings which transforms
B
k
into a
B
k
in which
all the virtual vertices labeled k+1 appear together on the horizontal line.
It remains to be shown how one decides which permutation or ipping to
apply, how to represent the necessary information, without actually drawing
bush forms, and how to do it all efciently. Lempel, Even, and Cederbaum
described a method that uses a representation of the pertinent information by
proper expressions. However, a better representation was suggested by Booth
and Leuker. They invented a data structure, called PQ-trees, through which the
algorithmcan be run in linear time. PQ-trees were used to solve other problems
of interest; see [6].
I shall not describe PQ-trees in detail. The description in [6] is long (30 pages)
although not hard to follow. It involves ideas of data structure manipulation, but
almost no graph theory. The following is a brief and incomplete description.
A PQ-tree is a directed ordered tree, with three types of vertices: P-vertices,
Q-vertices and leaves. Each P-vertex or Q-vertex, has at least one son. The
sons of a P-vertex, which in our application represents a separating vertex v of
B
k
, may be permuted into any new order. Each sons and its subtree, represents
a subbush. A Q-vertex represents a maximal nonseparable component, and its
sons, which represent the ys, may not be permuted, but their order can be
reversed. The leaves represent the virtual vertices.
8.5 Problems 185
The attempt to gather all the leaves labeled k +1 into an unbroken run, is
done from sons to fathers, starting with the leaves labeled k +1. Through a
technique of template matching, vertices are modied while the k +1 labeled
leaves are bunched together. Only the smallest subtree containing all the k+1
labeled leaves is scanned. All these leaves, if successfully gathered, are merged
into one P-vertex, and its sons represent the virtual edges out of k +1. This
procedure is repeated until k+1 =n.
8.5 Problems
Problem 8.1 Demonstrate the path-addition algorithm on the Peterson graph
(see Problem 7.3). Show the data for all the steps: the DFS for numbering
the vertices, dening the tree and computing L1 and L2. The function on
the edges. The sorting of the adjacency lists. Use the path-nding algorithm
in the new DFS to decompose the graph into C and a sequence of paths. Use
1
,
2
,
3
and end-of-stack markers to carry out all the recursive steps up to
planarity decision.
Problem8.2 Repeat the path-addition planarity test, as in Problem8.1, for the
graph depicted in Figure 8.5.
Figure 8.5: A graph for Problem 8.2.
Problem 8.3 Demonstrate the vertex-addition planarity test on the Peterson
graph. Show the steps for the DFS, the st-numbering, and the sequence of bush
forms.
Problem 8.4 Repeat the vertex-addition planarity test for the graph of
Problem 8.2.
Problem8.5 Showthat if a graph is nonplanar, then a subgraph homeomorphic
to one of the Kuratowskis graphs can be found in O(|V|
2
). (Hints: Only O(|V|)
edges need to be considered. Delete edges if their deletion does not make the
graph planar. What is left?)
186 8 Testing Graph Planarity
Bibliography
[1] Auslander, L., and Parter, S. V., On Embedding Graphs in the Plane, J. Math. and
Mech., Vol. 10, No. 3, May 1961, pp. 517523.
[2] Goldstein, A. J., An Efcient and Constructive Algorithm for Testing Whether a
Graph Can Be Embedded in a Plane, Graph and Combinatorics Conf., Contract
No. NONR 1858-(21), Ofce of Naval Research Logistics Proj., Dept. of Math.,
Princeton Univ., May 1618, 1963, 2 pp.
[3] Hopcroft, J., and Tarjan, R., Efcient Planarity Testing, JACM, Vol. 21, No. 4,
Oct. 1974, pp. 549568.
[4] Lempel, A., Even, S., and Cederbaum, I., An Algorithm for Planarity Test-
ing of Graphs, Theory of Graphs, International Symposium, Rome, July, 1966.
P. Rosenstiehl, Ed., Gordon and Breach, N.Y. 1967, pp. 215232.
[5] Even, S., and Tarjan, R. E., Computing and st-numbering, Th. Comp. Sci., Vol. 2,
1976, pp. 339344.
[6] Booth, K. S., and Lueker, G. S., Testing for the Consecutive Ones Property, Interval
Graphs, and Graph Planarity Using PQ-tree Algorithms, J. of Comp. and Sys.
Sciences, Vol. 13, 1976, pp. 335379.
Index
acyclic digraph, 141
adjacency list, 170
adjacency matrix, 3
attachments, 146
augment procedure, 89
augmenting path, 87
backward cut, 86
BFS, see breadth-rst search
biconnected, 127
bipartite graph, 135
Breadth-rst search, 11
bridge, 63
bridges, 146
interlace, 148
capacity, 85
capacity function, 85
capacity of a cut, 87
residual capacity, 94
circuit, 2
simple, 2
classiable, 143
clique, 44, 63
code, 65
word length, 65
alphabet, 65
characteristic sum, 67
characteristic sum condition, 67
exhaustive, 83
letters, 65
message, 65
prex, 65, 68
sufx, 65
tail, 65
uniquely decipherable, 65
word, 65
coloring, 149
component, 146
nonseparable, 52
singular component, 146
strong, 58
superstructure, 54
concurrent set of edges, 139
connectivity
vertex, 121, 124
contraction, 160
critical path, 138
cut, 33, 86
cutset, 159
Dantzig algorithm, 27
De Bruijn sequence, 9
depth-rst search, 46
directed, 57
lowpoint, 53
number, 49
tree, 49
back edges, 51
tree edges, 51
DFS, see depth-rst search
digraph, see graph
Dijkstra algorithm, 14
Dinitz algorithm, 94
187
188 Index
edge, 1
antiparallel, 3
edge array, 4
parallel, 3
self-loop, 3
edge rule, 102
edge separator, 130
Euler, 6
ow
absorb, 114
blocking, 95
maximal, 95
ow function, 85
Floyd algorithm, 21
Ford algorithm, 17
Ford-Fulkerson algorithm, 87
forward cut, 86
geometric dual, 166
graph, 1
2-colorable, 149
acyclic, 24
bipartite, 149
circuit-free, 29
connected, 2
directed (digraph), 2
arbitrated, 38
dual, 160
Euler graph, 6
planar, 146
sparse, 4
strongly connected, 3, 58
triangular, 166
triangulation, 166
underlying graph, 7
homeomorphic, 152
incidence list, 4
independent set, 142
isomorphic, 152
label
labeling procedure, 88
leaf, 31
marriage problem, 135
matching, 135
complete, 137
max-ow min-cut theorem, 94
Mengers Theorem, 122
network, 85
0-1, 117
0-1 type 1, 119
0-1 type 2, 120
auxiliary network, 103
layered network, 95
secondary network, 94
nonseparable, 126
path, 2
directed, 3
directed Euler path, 7
Euler path, 6
Hamilton, 23
shortest, 11
simple, 2
PERT digraph, 137
Petersen graph, 165
planar
PQ-trees, 184
block, 175
bush form, 181
equivalent, 157
face, 150
external, 151
path addition, 168
second lowpoint function, 169
sink, 179
source, 179
st-numbering, 177
vetex addition, 169
virtual edges, 180
virtual vertices, 180
window, 150
plane graph, 146
Prim algorithm, 32
root, 37
separating edge, 162
separation vertex, 52
sink, 85
auxiliary sink, 103
slice, 142
source, 85
auxiliary source, 103
spanning tree, 32
subgraph, 31
Index 189
topological sorting, 24
total ow, 85
Trmauxs algorithm, 46
tree, 29
directed, 37
useful
backwardly useful, 88
forwardly useful, 88
useful edge, 88
vertex, 1
degree, 1
in-degree, 3
out-degree, 3
sink, 58
source, 58
vertex array, 4
vertex separator, 122, 130
Warshalls Algorithm, 26
Warshalls algorithm, 26