Professional Documents
Culture Documents
M. Ashraf Iqbal
ii
Copyright
2010
c by M Ashraf Iqbal
ISBN . . .
. . . Publications
To my grand daughter Nariman
iv
Contents
1 Introduction 1
1.1 Why a new book? . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 What do we emphasize? . . . . . . . . . . . . . . . . . . . . . 3
1.3 How the book is organized? . . . . . . . . . . . . . . . . . . . 6
1.4 How is the book designed? . . . . . . . . . . . . . . . . . . . . 9
1.5 Some salient features of the book . . . . . . . . . . . . . . . . 9
1.6 What tools do we use? . . . . . . . . . . . . . . . . . . . . . . 10
1.7 A Possible Sequence . . . . . . . . . . . . . . . . . . . . . . . 10
1. Definitions (concepts)
2. Implications & theorems (connections between concepts)
3. Algorithms (actions which transform an input into an output)
Let us start with a few concepts used in our subject. For example graph, con-
nected graph, tree, vertex, edge, bridge edge, edge weight, path, edge-disjoint
paths, & edge cut are all concepts, and a learner needs to become familiar
with these concepts. He or she should be able to feel, visualize, and connect
these concepts. An implication connects two or more concepts. For exam-
ple if every edge is a bridge edge in a connected graph G then the graph G
is a tree. Menger’s theorem connects maximum edge-disjoint paths to the
minimum edge cut in a graph. We need to prove a theorem like this one
in a formal but convincing manner. An algorithm converts an input into a
useful output. For example an algorithm is to be designed to find maximum
edge-disjoint paths as well as the minimum edge cut in a given input graph.
Many books tilting towards graph theory do not emphasize graph algorithms.
Similarly a number of graph algorithm books ignore the theory part of this
course namely graph theorems and their proofs. Unlike these books we op-
erate on all the three layers, thus reinforcing understanding at various levels.
In fact we try our best to design constructive proofs which not only prove a
connection but also provide an algorithm to find something useful. For exam-
ple we use constructive techniques to find as well as prove that the maximum
number of edge-disjoint paths are equal to the size of the minimum cut in a
graph.
What do we emphasize? 3
Most of the other textbooks use a historical perspective of how and when
a graph algorithm was discovered. For example Dijkstra designed a greedy
algorithm to find shortest path between two vertices in a weighted graph.
Similarly Prim designed a greedy algorithm to find a minimum spanning tree
in a weighted graph. The historical perspective encourages one to teach these
two algorithms in isolation without making any connections between the two
algorithms. We think it is important from a learning perspective to integrate
as well as differentiate concepts and techniques especially the ones which
solve nearly identical problems. We thus encourage our readers to transform
one algorithm (Prim’s algorithm) into another (Dijksta’s algorithm).
Similarly, it is possible and desirable to transform one proof into another. For
example we encourage our readers to transform the techniques used to prove
that Dijkstra’s algorithm really finds shortest paths into techniques to prove
that Prim’s algorithm really finds a minimum spanning tree in a weighted
graph. We think that not only the similarities but also the differences should
be highlighted between two almost similar concepts or algorithms. For ex-
ample Prim’s algorithm can find a minimum spanning tree or a maximum
spanning tree (after a minor modification) and can handle positive or nega-
tive edge weights while Dijkstra’s algorithm is unable to do so.
The single most prominent feature which distinguishes this book from other
books in this field is an emphasis on transformations (or reductions). We
expect our readers to first think of a non graph problem in terms of a graph.
(see Fig. 1.2.1). We then encourage our readers to transform that non-graph
problem into a graph problem. We also encourage them to transform a
graph problem into another graph problem. Similarly we encourage them to
transform one theorem into another theorem. We provide platforms where a
learner is provoked to transform one proof technique (or an algorithm) into
another proof technique (or an algorithm). We think of this course on graph
theory & algorithms as a course on (intelligent) transformations. Transform-
ing one concept into another and transforming one graph into another is the
driving force behind this exercise (see Fig. 1.2.2).
4 Introduction
j j j
3 3 3
g 4 i g 4 i 4
g i
2 3 2 3 2 3
h 6 h 6 6
h
3 k 3 k 6 k
9 9 3
9
a 3 4 3 3
3 3 a 3 a 4
f c f c f
2 5 2 c
5 2
2 2 2 2
d 2 2
b d b d
1 3 1 3 3 b
1
e 1 e 1 1
e
t
bu
es
ta nc aths
is p
t d test
es r
h o r t sh o
s e
the of th w(a,k) = min{w(a,k), w(a, j)+w(j,k)}
find ack
r
We se t
lo
j j j
3 3 3
g 2 4 i g 4 i g 4 i
2 2
3 3 3
h 6 h 6 h 6
3 6 k 3 k 3 k
7 6 6
7 7
a 3 3 3
a 3 a
f 3 4 f 3 4 f 4
c c c
5 2 5 2 5 2
2 2 2
3 2 2 2
d b d 3 d
1 1 b 1 3 b
5 5 5
e 1 e 1 e 1
10. We use a number of tools from the science of learning, e.g., concept
maps.
Figure 1.6.2: Visualizing how an algorithm changes a graph and what price
is paid in terms of number steps performed. This helps a learner to visualize
the time complexity of an algorithm. Each edge in the pink graph exactly
corresponds to one step in the algorithm.
A Possible Sequence 13
Figure 1.6.3: A 3-person drama format for teaching Graph Theory & Algo-
rithms at the Virtual University of Pakistan.
14 Introduction
Figure 1.6.4: Virtual University video lectures along with Power Point slides
are available on www.synote.com. A learner has the facility of expressing
and sharing his or her feedback regarding the understanding of a concept in
this software platform.
A Possible Sequence 15
Chapter 3 (1 Lectures)
Problems, Models & Graphs: Why Study Graph Theory
Reducing one Problem into another, the Satisfiability Problem (in Logic Cir-
cuits), An Activity Scheduling Problem (in Civil Works), a Dual Machine
Serial Processing Environment (from Distributed Computing), Sequencing
by Hybridization (in Computational Biology), discussion & problems (from
Operations Research)
Chapter 4 (3 Lectures)
Basics of Graph Theory
A Mutual Friendship Graph, Representation of a Graph, Complement of
a Graph, Equal Graphs & Isomorphic Graphs, the Degree Sequence, Walks,
Trails & Paths, Multi-graphs and Pseudo-graphs, Broad Categories of Graphs,
Integration of Concepts, Properties, and Action Items
Chapter 5 (4 Lectures)
Basics of Graph Algorithms
Design of Algorithms, the Bucket Algorithm, Finding if a Graph is Con-
nected, Finding if a Graph is a Tree, Finding a Spanning Tree of a Graph,
Finding a minimum (maximum) spanning (MST) tree, Finding a Path in a
Graph, the Shortest Path Problem, Single source & all pair shortest paths,
Graph Traversal Techniques, some Graph Theoretic Claims.
Chapter 6 (9 Lectures)
Connectivity, Matching Problems & Network Flows
Definitions, Prior Knowledge, & Warm-up Exercises, Konig’s Theorem, Menger’s
Theorem, & Hall’s (Marriage) Theorem, Menger’s Theorem, Finding Max-
imum Edge-Disjoint Paths, The Concept of a Minimum Cut, a Proof of
Menger’s Theorem & Finding the Min-Cut, Finding Maximum Vertex-Disjoint
Paths, Menger’s Theorem for Un-directed Graphs, Edge Connectivity & Ver-
tex Connectivity, Konig’s Theorem, Menger’s Theorem, & Hall’s (Marriage)
Theorem Re-visited. (3 Lectures)
Network Flows: Finding Maximum Edge-Disjoint Paths in Multi-Graphs,
the Maximum Flow & the Minimum Cut, Algorithmic Issues & Complex-
ity Calculations, Lower Bounds on Edge Flows: Min-Flow & the Max-Cut,
Lower as well as Upper Bounds on Edge Flows, the Matching Problem in Bi-
16 Introduction
Acknowledgements
Graph
Multi Graphs
We assume that multiple edges (known as parallel edges) between the same
two vertices are not allowed in a graph. Similarly an edge coming out of
a vertex and terminating at the same vertex (known as a self loop) is not
allowed. In M ultiGraphs we allow parallel edges as well as self loops. (Please
see Chapter 4 for more details)
Adjacent Vertices
Degree of a vertex
Regular graph
Star graph
A graph of p vertices in which one vertex has degree equal to p−1 while every
other vertex has a degree equal to 1. It is known as a star graph because
it looks like a star with rays of lighy coming out? (Please see Chapter 4 for
more details)
Complete Graph
An un-directed graph G is complete if there is an edge between every pair of
vertices of graph G.(Please see Chapter 4 for more details)
Bipartite Graph
A graph G is bipartite if the vertex set V (G) can be divided into two subsets
(or partites) A and B such that every edge in G connects a vertex in partite
A and a vertex in partite B. (Please see Chapter 4 for more details)
Equal Graphs
If for every edge {u, v} in a graph G there is an edge {u, v} in graph H and
vice versa then the two graphs G and H are equal. (Please see Chapter 4 for
more details)
Isomorphic Graphs
Two graphs G and H are isomorphic if graph G becomes equal to graph H
by some relabeling of vertices of graph H. Please note that two equal graphs
are always isomorphic but two isomorphic graphs may not be equal. (Please
see Chapter 4 for more details)
22 Basic Definitions in Graph Theory and Algorithms
Automorphism
If a permutation p of a graph G creates a graph H which is equal to graph
G then that permutation is known as an automorphism of graph G. The
identity permutation is always an automorphism - it is known as a trivial
permutation. We may be more interested in the non trivial permutations
of graph G? It may be possible for a certain category of graphs that the
only automorphism is the trivial (identity) permutation? Please note that
if p is an automorphism of graph G then the permutation p2 is always an
automorphism of graph G. (Please see Chapter 4 for more details)
Complementing Permutation
If a permutation p of vertices of graph G creates a graph H which is the
complement of graph G and at the same time H is isomorhic to G then the
permutation p is known as the complementing permutation of graph G and
graph G will be a self complementary graph. (Please see Chapter 4 for more
details)
Walk
You can walk on the edges of graph G edges starting from vertex u and
ending at vertex v traversing different edges and vertices. In a walk you
23
can traverse an edge more than once. A walk is open if vertex u and v are
different. It is a closed walk if vertex u and v are the same. Please note that
in an un-directed graph you can traverse an edge in both directions but in
a directed graph you can traverse an edge in only one direction - that is the
direction of that edge. (Please see Chapter 4 and 7 for more details)
Path
If neither an edge nor a vertex is repeated in a walk starting from a vertex
u and ending at vertex v then the walk is known as a path. It is known as a
u − v path. A path is always a trail (or a walk) but it is not the other way
round. (Please see Chapter 4 and 5 for more details)
Shortest path
Among all paths between vertex u and vertex v, the one with minimum
length is known as the shortest path between vertex u and vertex v. In an
unweighted graph G the minimum length is measured in terms of number of
edges encountered in the u − v path. In a weighted graph G the minimum
length is measured in terms of sum of weights of all edges in the u − v path.
(Please see Chapter 4 for more details)
Cycle
If a path is closed that means you come back to the vertex from where you
have started then that path is known as a cycle. A cycle is a circuit but a
circuit may not be a cycle as no vertex should be repeated in a cycle. (Please
see Chapter 4 and 5 for more details)
24 Basic Definitions in Graph Theory and Algorithms
A Connected Graph
An un-directed graph G is connected if there is a path between every pair of
vertices of that graph. (Please see Chapter 4 and 5 for more details)
directed acyclic graph. (Please see Chapter 5 and 9 for more details)
A Tournament Graph
A directed graph D is a tournament if there is a directed edge from vertex
u to vertex v exclusive OR a directed edge from vertex v to u for every pair
(u, v) in directed graph D. In other words if we put directions on edges in
a completely connected un-directed graph G then the un-directed graph G
transforms into a tournament (directed) graph D. It is known as a tournament
graph as some (actual) tournaments (like f ootball league are played in the
form of a tournament graph? (Please see Chapter 9 for more details)
Disconnected graph
An un-directed graph is disconnected if for any pair of vertices u and v there
is no u − v path. (Please see Chapter 4 and 5 for more details)
Cyclic graph G
A graph G is cyclic if it contains one or more cycles. (Please see Chapter 4
and 5 for more details)
A Forest
A (disconnected)graph G with no cycles. Please note that a tree is a con-
nected graph with no cycles while a forest may be a disconnected graph.
(Please see Chapter 4 and 5 for more details)
26 Basic Definitions in Graph Theory and Algorithms
A Binary Tree
A binary tree is a tree such that the degree of each vertex is not more than
three. (Please see Chapter 4 for more details)
A Path graph
A path graph is a tree provided it has two vertices with degree one while
all other vertices has degree exactly equal to two. (Please see Chapter 4 for
more details)
Hamiltonian Cycle
It is a cycle in a graph G which traverses each vertex of G exactly once. A
graph which contains a Hamiltonian cycle is known as a Hamiltonian graph.
(Please see Chapter 8 for more details)
Eulerian Circuit
A circuit in a graph G such that every edge of graph G is traversed exactly
once. A graph which contains a Eulerian circuit is known as a Eulerian
graph. (Please see Chapter 7 for more details)
Set Cover
Given a set of subsets S of the U niversal Set U , what is the smallest subset
T of S such that the union of all these sub sets in T covers all elements of U .
We have already witnessed the subset sum problem (in previous courses) in
which we have to select integers (out of a set of integers) such that the sum
of the selected integers is equal to a given constant. (Please see Chapter 6
for more details)
27
Vertex Cover
The Universal set U is the set of all edges in a graph. What is the smallest
subset of vertices of the graph that covers all edges? (Please see Chapter 6
for more details)
Maximal Matching
This is a matching in which more edges cannot be added in the existing
matching to increase the size of this matching. It may be possible to increase
the size of the matching by first discarding the initial matching edges. (Please
see Chapter 6 for more details)
Maximum Matching
This is a matching in a graph with as many edges as possible? Please note
that maximum matching is always maximal.Thus it is a matching of maxi-
mum size. (Please see Chapter 4, 5 and 6 for more details)
Edge Cover
The Universal set is the set of all vertices in a graph. Then the edge cover
is the smallest subset of edges, which covers all vertices? How small (or big)
can the size of the edge cover become, as compared to the number of vertices
in a graph? (Please see Chapter 6 for more details)
28 Basic Definitions in Graph Theory and Algorithms
Perfect Matching
A P erf ect Matching is a matching which covers all vertices of the graph.
That is, every vertex of the graph is incident to exactly one edge of the
matching. Every perfect matching is both maximum and hence maximal.
In some literature, the term complete matching is used for it. (Please see
Chapter 6 for more details)
Cut vertex
A vertex u is a cut vertex if its removal disconnects an un-directed graph G.
(Please see Chapter 6 for more details)
Minimum Vertex-Cut
A set of (minimum number) of vertices which if removed will disconnect a
special vertex s from another special vertex t in a graph G. (Please see
Chapter 6 for more details)
Edge Connectivity
It is the minimum number of edges which if removed will disconnect an un-
directed connected graph G. (Please see Chapter 6 for more details)
Vertex Connectivity
It is the minimum number of vertices which if removed will disconnect an
un-directed connected graph G. (Please see Chapter 6 for more details)
29
3.1 Introduction
3.2 Reducing One Problem into Another
3.3 The Satisfiability Problem in Logic Circuits
3.4 An Activity Scheduling Problem
3.5 A Dual Machine Serial Processing Environment
3.6 Sequencing by Hybridization in Computational Biology
3.7 Discussion & Problems
32 Problems, Models & Graphs: Why Study Graph Theory?
3.1 Introduction
When we face a real life problem then one possibility is to solve it right
from scratch. A more desirable option is to better understand the problem
in terms of reducing it into one of the known problems in computer science.
There may be good chances that you are able to transform (or reduce) your
unknown problem into one of the solvable problems in graph theory. Even
if you end up reducing your problem into one of the hard (or unsolvable)
problems – you certainly get a better insight. We shall discuss a number
of diverse problems in this chapter; the problems are taken from digital
logic, civil works, distributed computing, molecular biology, and the field
of operations research. Almost all these problems do not seem to have a
any relationship with graphs, yet each of them is transformed into a graph
problem. As demonstrated in this chapter, it is very much possible (and in
fact desirable) to reduce an unknown problem into multiple known problems
instead of just one known graph problem. It may happen that out of these
multiple known problems, one of the problems may be relatively simple to
solve while the other known problem may be a hard one. Once your problem
is transformed into a known problem, the complexity of solving your unknown
problem will depend upon the complexity of your transformation as well as
that of solving the known problem (see Fig. 3.1.1). The real challenge is thus
to find an intelligent transformation into a simpler problem.
Inputs
Outputs
A Known Algorithm
for an Old Problem
Algorithm B
Algorithm A
Inputs
Outputs
A New Problem
Figure 3.1.1: Reducing a new problem into an old problem. The inputs for
the new problem should be transformed into the inputs of the old problem.
Similarly the outputs should also be transformed.
We shall provide (or discuss) at least one example from each category in this
chapter.
a b c a b c a b c a b c
Figure 3.3.1: A logic circuit consisting of OR gates (each with 3 inputs) and
one AND gate. The output is 1 for the selected inputs shown in orange color.
A lot many of you might have played with logic circuits; have explored as-
signments of inputs for which the output of the logic circuit is 1 – but many
The Satisfiability Problem in Logic Circuits 35
of you may not have realized (or truly appreciated) that the 3-CNF problem
is one of the NP-complete problems while the 3-DNF is a solvable problem
(in polynomial time). Remember in the 3-CNF problem one has to select in-
puts such that the output of each OR gate is high so as to satisfy the output.
In case of 3-DNF problem the output of the circuit will be high provided the
output of any AND gate is high. Surprisingly applying DeMorgans laws it
is possible to convert a 3-CNF Boolean expression in terms of a 3-DNF –
giving a false impression that we can reduce an NP-complete problem into a
P problem. We shall discuss this issue (in detail) in the coming paragraphs.
OutPut
a b c a b c a b c a b c
Figure 3.3.2: A logic circuit consisting of AND gates (each with 3 inputs)
and one OR gate. The output is 1 for the selected inputs shown in orange
color.
Problem Set 3.1.
Problem 3.1.1. Design an efficient algorithm to solve the 3-DNF Satisfia-
bility problem. Show that it is indeed possible to solve this problem in poly-
nomial time (in fact in linear time). A 3-DNF circuit is shown in Fig. 3.3.2.
Problem 3.1.2. Try to use a similar technique to solve the 3-CNF Satisfia-
bility problem. It will not work. The purpose of this problem is to appreciate
the inherent hardness of this problem. A 3-CNF circuit is shown in Fig. 3.3.1.
Inputs
Outputs
Old Problem
The Independent Set
Problem in a
Triangular Graph
Algorithm B
Algorithm A
Inputs
Outputs
New Problem
The 3-Satisfiability
Problem
The old problem in Fig. 3.3.3 is the Independent Set problem in a special
graph consisting of k triangles with edges connecting certain vertices within
different triangles as shown in Fig. 3.3.4. The problem is to find if it is
possible to select one vertex from each triangle such that no two selected
vertices have an edge in common (they should not be adjacent). In other
words we need to find if the size of the Independent Set (a set of vertices
with no common edges) in this graph is equal to the number of triangles in
the graph. It is obvious that for the size of the Independent Set to be equal
to k we have to select exactly one vertex from each triangle such that no two
selected vertices are adjacent.
The Satisfiability Problem in Logic Circuits 37
Algorithm 1: The 3-SAT Problem: Find for what inputs, the output
is high for the given logic circuit?
Input : A Boolean formula (or circuit) in 3-CNF.
Output: Yes or No; If Yes then a combination of inputs for which the
output is high.
1. We consider this as a new problem: You need to transform it into a
known problem in graph theory?
OutPut
AND
a ¬b c a b ¬c ¬a ¬b c ¬a ¬b ¬c
c ¬c c ¬c
a ¬b a b ¬a ¬b ¬a ¬b
Figure 3.3.5: The 3-Satisfiability problem (top) is reduced into the Inde-
pendent Set problem in the graph shown in the bottom. Orange vertices
in the bottom graph provides a solution to the Independent Set problem in
the bottom graph while their orange counterparts (input variables to the
logic circuit) provides a satisfiable solution to the 3-SAT problem in the top
circuit.
The Satisfiability Problem in Logic Circuits 39
Problem 3.2.1. We have already talked about the 3-Sat problem and its
reduction to independent set problem as modeled in Fig. 3.3.3, and depicted
in Fig. 3.3.5. Please design Algorithm A and Algorithm B as shown in
Fig. 3.3.3.
Problem 3.2.2. Try to use a similar reduction to reduce the 2-SAT problem
into the independent set problem in a graph. Discuss why this may or may
not be possible.
Problem 3.2.3. Try to use a similar reduction to reduce the 3-DNF problem
into the independent set problem in a graph. Discuss why this may or may
not be possible.
OutPut
AND
Dr
aw
K
Ma
p
OR OR OR OR
c 0 1
ab
00 1 0
a ¬b c a b ¬c ¬a ¬b c ¬a ¬b ¬c
01 0 1
OutPut
11 0 0
OR
10 1 1
it
rc u
Ci
aw
Dr
AND AND AND AND
a ¬b ¬c a ¬b c ¬a ¬b ¬c ¬a b c
Figure 3.3.6: The 3-CNF Satisfiability problem (top) is reduced into the 3-
DNF-Satisfiability Problem as shown in the bottom. The truth table, also
shown on the right, helps us in this reduction.
The Satisfiability Problem in Logic Circuits 41
¬a
OR
b ¬c
¬a a b
OR
¬b
AND
c ¬b ¬a
OR
a
Convert
¬c c
OR
a
Figure 3.3.7: The 2-CNF Satisfiability problem is reduced into a graph prob-
lem. If any vertex x and its complement in the graph (shown in the right di-
agram) belongs to the same strongly connected component then the Boolean
expression corresponding to the circuit (shown in the left diagram) is not
satisfiable.
We apply Algorithm 3 for every vertex x (and its complement ¬x) in graph
D, and if the answer comes out to be NO in each case then the given Boolean
expression is satisfiable otherwise not. If the Boolean expression is satisfiable
then we have to find a combination of input variables for which the output of
the logic circuit is 1. This requires a deeper understanding and appreciation
of different concepts involved in this reduction. We shall provide some hints
in this regard and leave the rest as a problem for imagination of the reader.
a→
a b ¬a OR b ¬b → b¬a
0 0 1 1
0 1 1 1 Im Trut
ui f
irc o
pli h T
C ble
ca a
t
tio ble
gi Ta
1 1 1 1
n
Gr of
Lo uth
c
Tr ap
1 0 0 0 h
Figure 3.3.8: A 2-CNF expression with only one OR/AND gate is reduced
into a directed graph. The directed graph is in fact an implication graph
with a truth table shown at the top. Note that the implication graph tells us
that if a = 1 then b should also be 1 otherwise the output of the logic circuit
will be zero.
If a = 0 then Output =1
¬a
OR a b
b
For what values of
AND
input the output is 1
¬a
OR ¬b ¬a
¬b
If a = 1 then b=1
then ¬a =1
Figure 3.3.9: The 2-CNF Satisfiability problem is reduced into a graph prob-
lem. Note that if a = 1 then we face a contradiction in the implication
graph. On the other hand if a = 0 then there is no such contradiction and
the output of the logic circuit will be 1 for any value of b.
44 Problems, Models & Graphs: Why Study Graph Theory?
in the right diagram of this figure. As you should appreciate this directed
graph is not just a directed graph – this is in fact an implication graph.
A directed edge in this graph from vertex x to vertex y implies that if the
vertex x (or meaningfully) variable x is 1 then y is 1. The truth table of this
implication is exactly the same as that of the Boolean expression as shown
in this figure. The implication tells us that in order to make the output 1
we shall make b = 1 if a = 1. This makes sense because if both inputs of the
OR gate are 0 then the expression will not be satisfiable.
The implication graph for a Boolean expression consisting of two OR gates
are shown in Fig. 3.3.9. Again the implication graph is telling us how it is
possible to make the Boolean expression satisfiable. If a = 1 then b should
be 1 so that the output of the top OR gate becomes 1. But if b = 1 then
according to the implication ¬a should be 1 so as to make the output of
the second OR gate 1 – but that is a contradiction. But if a = 0 then the
implication does not dictate any thing – it means that the Boolean expression
is satisfiable for any value of b.
We show various 2-CNF logic circuits for varying number of OR gates and
the corresponding implication graphs in Fig. 3.3.10. It will be interesting to
find if the circuit is satisfiable (as we add more OR gates); and in case it
is then what inputs should be applied so that the output is 1 for each logic
circuit shown in this figure. It should now be possible to design an efficient
graph algorithm which operates on the implication graph but which finds the
input combination for which the logic circuit output is 1. Please note that
in the bottom diagram of Fig. 3.3.10, it is possible to reach from vertex a
to vertex ¬a, it is also possible to reach from vertex ¬a to a as shown by
the closed path shown in red color. This implies that the logic circuit is not
satisfiable.
We show the same 2-CNF logic circuit in Fig. 3.3.11. The Satisfiability
problem in this logic circuit is transformed into a graph problem in the Im-
plication graph shown in the bottom diagram. The Satisfiability problem is
also transformed into an independent set problem where each OR gate in
the logic diagram now corresponds to two adjacent vertices (known as an
adjacent pair) instead of a triangular graph. If it is possible to select one
vertex from each adjacent pair such that no two selected vertices are adjacent
then we claim that the logic circuit is satisfiable. This transformation works
both ways: if the circuit is satisfiable then we can select an independent set
The Satisfiability Problem in Logic Circuits 45
a b
AND Convert
¬a
OR
b ¬b ¬a
¬a
OR a b
b
AND Convert
¬a
OR ¬b ¬a
¬b
¬c
¬a
OR
b a b
¬a AND Convert
OR ¬b ¬a
¬b
c
OR c
a
¬a
OR
b ¬c
¬a a b
OR
¬b
AND
c
OR ¬b ¬a
a
¬c
OR Convert c
a
with size equal to the number of OR gates in the logic circuit. It is now
obvious that the independent set problem in the graph (which was derived
from a 2-CNF logic circuit) can be transformed into a path finding problem
in an Implication graph. Describe an efficient algorithm which finds an in-
dependent set in such a graph using an Implication graph. Given a general
graph how can you determine that this graph in fact represents a 2-CNF
logic circuit? If it does represent such a logic circuit then we can solve this
problem after reducing it into an implication graph?
¬a
OR ¬a b
b
Transform
¬a
OR ¬a ¬b
¬b
AND
c
OR
a a c
¬c
OR
a a ¬c
Tr ¬c
an
sf
rm
or
o
m
sf
a b
an
Tr
¬b ¬a
Table 3.1: We have tabulated a number of tasks, their IDs, their duration
(in days) and what tasks must be completed before they can begin (prereq-
uisites).
6 7
20 10
1 2 3 4 5 8 10
14 28 42 21 21 35 00
50
9
10
6 7
20 00
21 21 35 00
1 2 3 4 5 8 10
14 28 42
50 21 35
9
10
6 7
20 00
21 21 35 00
1 2 3 4 5 8 10
14 28 42
50 21 35
Figure 3.4.3: Finding the longest path corresponds to the optimal scheduling
of activities
Communication Costs
10 50 20 0
Execution Costs
Processor B 50 90 30 20
Processor A 70 20 40 80
Processor Processor
There are basically four possibilities for the first pair of adjacent modules to
be assigned onto the dual processor system:
The last option provides the optimal solution in terms of minimum comple-
tion time for the sub-problem. The challenge is to find the optimal solution
for the entire problem efficiently (without enumerating all possibilities as we
did for the sub-problem). Please note that the cost consists of two parts,
cost of execution of a module on a processor, and the cost of communica-
tion between two adjacent modules provided the two modules are assigned
to different machines. There is no communication cost if the two (adjacent)
modules are assigned to the same machine. The total time of computation
is the sum of total execution times plus the total communication times.
Problem 3.3.1. Find the total cost if all modules are assigned to machine A.
This is standard sequential processing on machine A; it is not a distributed
assignment.
Problem 3.3.2. Find the total cost if all modules are assigned to machine
B. This is also sequential processing but this time on machine B.
Problem 3.3.4. Find the Optimal Assignment by hit and trial (the size of
this problem is small enough)? It should be less than (or equal to) the ones
found in 1, 2, and 3.
90 30 20
u04
50 00
Path B: All Modules on Machine B
90 30 20
u04
50 00
10+90 50+30
20+20
20+80
70 10+20 50+40
00
0
20 40 80
Figure 3.5.2: (top) We show the possibility of all modules assigned to either
processor A or to processor B. This is equivalent to traversing a path which
passes though the top vertices or the bottom vertices in a graph with two
dummy vertices. (Bottom) The possibility of some modules assigned to pro-
cessor A while other modules to processor B is shown with a zigzag path
between the two dummy vertices. A shortest path corresponds to an optimal
assignment.
54 Problems, Models & Graphs: Why Study Graph Theory?
90 30 20
u04
50 00
10+90 50+30
20+20
20+80
70 10+20 50+40
00
0
20 40 80
50 90 30 20
10 50
20
0 0
70 20 40 80
50 90 30 20
10 50
B 20
0 0
70 A 20 40 B 80
B B
50 90 30 20
10 50 20
0 0
70 20 40 80
CUT A
70 20 40 80
50 90 30 20
10 50 20 0
70 20 40 80
Cut X
50 90 30 20
CUT B
Figure 3.5.4: (top) A cut in this graph (which disconnects vertex A from
vertex B) corresponds to an assignment of modules. Cut A corresponds
to all modules assigned to processor A. Cut B corresponds to all modules
assigned to processor B. Cut X corresponds to the module assignment as
shown in the bottom diagram.
56 Problems, Models & Graphs: Why Study Graph Theory?
The challenge is to design Algorithm 6 and then use the text book Algorithm
7 in order to solve the Minimum Cost Assignment Problem.
Another challenge is to design Algorithm 8 and then use the text book Al-
gorithm 9 in order to solve the Minimum Cost Assugnment Problem.
Transform the sequencing problem into Hamiltonian path and then into an
Eulerian path problems (see the figures below)
Sequencing by Hybridization in Computational Biology 59
Spectrum(s,4) = {ATAC, CATA, ACAT, ACAG, TACA, ACAC, CACA, ACAA, CAAC, AACA}
ACAC
ATAC CACA
ATAC CATA ACAT ACAG TACA
ACAT CAAC
AACA
Start
ACAC ACAC
ACAC ACACA ACATAC
ACATA
ACACAA
ACATACA
ACACAACA
Spectrum(s,4) = {ATAC, CATA, ACAT, ACAG, TACA, ACAC, CACA, ACAA, CAAC, AACA}
ATAC
? ?
ACAG ACAC
CATA CAG ACA CAC
? ? CACA
ACAT ACAT
? ? TACA
AACA ACAA
ATAC
ATA TAC
ACAT
ACA CAT ATAC CATA
CATA
CAT ATA CATA
ATA
ACAT
CAAC
Figure 3.6.4: (Top) A Euler path in the graph of Fig. 3.6.1; this Euler path
corresponds to the sequence s1 = ACACAACAT ACAG. (Bottom) Another
Euler path is shown in the same graph; this Euler path corresponds to the
sequence s2 = ACAT ACAACACAG. Please note that in both the Euler
paths shown in this diagram there is a vertex which is repeated several times
while every edge is traversed exactly once.
1. Does your problem have an inherent graphical structure? If yes then the
transformation may be a lot simple. For example in the first example
Discussion & Problems 63
a1 b1 a1 b1
a2 b2 a2 b2
A B A B
a3 b3 a3 b3
a4 b4 a4 b4
A Matched Edge
Bipartite Graph C Maximum Matching in Graph C
Corresponds
Transforms
Transforms
a1 b1 a1 b1
a2 b2 a2 b2
A
s A B t s t
a3 b3 a3 b3
Find Max Edge-
Disjoint Paths
a4 b4 a4 b4
Figure 3.7.1: We add vertex s and vertex t to a bipartite graph and then
try to reduce a maximum bipartite matching problem into another problem
where we maximize edge-disjoint paths between vertex s and vertex t.
66 Problems, Models & Graphs: Why Study Graph Theory?
a1 b1 a1 b1
a2 b2 a2 b2
A B A B
a3 b3 a3 b3
a4 b4 a4 b4
A Matched Edge
Bipartite Graph C Maximum Matching in Graph C
Transforms
Corresponds
Transforms
a1 b1 a1 b1
a2 b2 a2 b2
A
s A B t s t
a3 b3 a3 b3
Find Max Node-
Disjoint Paths
a4 b4 a4 b4
Figure 3.7.2: We add vertex s and vertex t to a bipartite graph and then
try to reduce a maximum bipartite matching problem into another problem
where we maximize vertex-disjoint paths between vertex s and vertex t.
Discussion & Problems 67
P er(C) = a11 a22 a33 + a11 a23 a32 + a12 a21 a33 + a12 a23 a31 + a13 a22 a31 + a13 a21 a32
P er(C) = 0 + 1 + 0 + 1 + 1 + 0 = 3
As you can see each individual term in the expression for Permanent is a
permutation – there is a one to one correspondence between each non zero
term in this expression and a perfect matching in the bipartite graph. Thus if
P er(C) is zero then no perfect matching exists. On the other hand if it is non
zero then there will be as many perfect matchings as the value of P er(C).
The problem of finding a perfect matching is thus reduced to finding the
permanent of a matrix.
1 1
2 2
3 3
Pe
r fe
ct
M
at
ch
in
g
if
Pe
r(C
)n
ot
ze
ro
Figure 3.7.3: We show a bipartite graph and its adjacency matrix shown in
the top diagrams. We take the permanent of the adjacency matrix and claim
that a perfect matching in the bipartite graph exists if and only if the value
of the permanent is non zero.
Discussion & Problems 69
1 1 0
Ed
a1 b1
Adjacency Matrix of C
ge
1 1 1
Pa
Bipartite Graph C
rt
of
a2 b2 0 1
Pe
0 1 1
rfe
A B
ct
1 1
M
1 1 0
atc
a3 b3
hin
g
Find
Find
Pe
rf e
ct
M
at
c hi
ng
Ex
is
ts If Permanent is
Permanent of C
not zero
not zero
Raising all weights to a power of two and then finding the permanent of the
weighted matrix gives us a powerful reduction in which each individual term
in the permanent is in fact the weight of a perfect matching raised to power
of 2. The value of the permanent will be divisible (with remainder equal to
zero) by the weight of minimum weight perfect matching raised to the power
of 2 provided we have a unique minimum weight perfect matching in the
bipartite graph.
3. Assume that now there is a possibility that the minimum weight perfect
matching is not unique. You need not find the value of the minimum
weight perfect matching – just make an efficient check if it is unique or
not?
72 Problems, Models & Graphs: Why Study Graph Theory?
a1 b1
8 8 8 8 3
2 2 2
3 2
a2 b2 8 8 8 1 8
1 Transforms 2 2 2
A B
8 8 2 8 8
a3 b3 2 2 2
Find
a1 b1
3 2 The weight of
minimum The highest power of 2
a2 b2 weight perfect which divides the
1
A B
matching permanent will be 217 + 224+ 224+
1+2+3=6
6 218+ 26 + 219
a3 b3
2
a b c d
a b
a 0 2 5 8
4
b 2 0 3 4
8 3
5
c 5 3 0 6
d c d 8 4 6 0
6
a 2 Transforms
b 0
b b c d
3 a 2 5 8
3
b 0 3 4
c 0
c
c 3 0 6
4 8
6
d Transform
Figure 3.7.6: A weighted graph G is shown in the top left diagram. We need
to find a shortest path between vertex a and vertex d. The adjacency matrix
of a weighted graph G is shown in the top right diagram. The shortest path
problem is converted into a minimum weight perfect matching problem as
shown in the bottom diagrams.
74 Problems, Models & Graphs: Why Study Graph Theory?
2
a b 0 2 5 8 2 5 8
4
2 0 3 4 Transforms 0 3 4
8 3
5 3 0 6 3 0 6
5
d c 8 4 6 0
6
Transform
Given a weighted graph G
Adjacency Matrix of Graph G
find a shortest path from a to d
a 2 a 2
2 5 5
a b
0
4 b b b 0
b
3 3
8 3 Transform
3 Transform 3
5 c 0
c c c
0
d c
6
4 8 8
6 4
A Shortest Path from 6
vertex a to d d d
Find a Minimum Weight A Complete Weighted
Perfect Matching Bipartite graph
Figure 3.7.7: The shortest path problem is converted into a minimum weight
perfect matching problem as shown in the top diagrams. We find a minimum
weight perfect matching in the complete bipartite graph shown in the bottom
right diagram. The minimum weight perfect matching (shown in the bottom
middle diagram) provides a shortest path between vertex a and vertex d as
shown in the bottom left diagram.
Consider Fig. 3.1.1, here the New Problem is finding shortest path between
two vertices in a graph G while the Old Problem is the minimum weight
Discussion & Problems 75
assignment problem.
4.1 Introduction
4.2 A Mutual Friendship Graph
4.3 Representation of a Graph
4.4 Complement of a Graph
4.5 Equal Graphs & Isomorphic Graphs
4.6 The Degree Sequence
4.7 Walks, Trails, & Paths
4.8 Multi-graphs and Pseudo-graphs
4.9 Broad Categories of Graphs & some Special Graphs
4.10 Integration of Concepts, Properties, and Action Items
4.11 Self Complementing Graphs
78 Basics of Graph Theory
4.1 Introduction
We shall discuss a number of basic definitions in this chapter. We will be
solving a couple of puzzles; hopefully the students will realize that in such
problems a stage comes when common sense alone is not sufficient to solve
the puzzle; we need tools and techniques of graph theory. We shall discuss
some necessary conditions for a degree sequence to be graphical. We shall
then talk about graph isomorphism and then come back to a discussion of
necessary and sufficient conditions for a degree sequence to be graphical.
The concept of graph connectedness will also be provided; we shall define a
walk, a trail and a path in a connected graph. We shall also talk about some
special graphs at the end of this chapter.
exactly the same as the friendship sequence and is shown in the top right
corner as well.
1. The sum of all numbers in the sequence will always be an even number.
Why?
2. The odd numbers in the sequence will appear even number of times.
Why?
3. At least one number will be repeated in the sequence. Why?
Problem 4.1.1. Salma invites three of her office colleagues for dinner. Salma’s
husband Aslam (known as A) received the guest when they have arrived while
Salma was in the kitchen preparing food. Aslam shook hands with some of
the guests. Obviously Salma was curious about whom Aslam shook hands
and with whom he did not. She can not ask this question directly so she
simple asks every colleague about the number of hands she had shaken; she
gets the following answer: B says 3, C says 2 and D says 1. Now Salma
asks the same question to her husband; he answers zero (he does not shake
hands with females). Is the husband telling the truth assuming the guests
told the truth? How about if he answers 3 (he showed no discrimination).
Find out the number of times the husband actually shook hands and more
importantly with whom he shook hands and whom he has ignored.
Representation of a Graph 81
Problem 4.1.2. Salma & Aslam has invited three married couples. Several
handshakes took place when the guests have arrived. No one shook hands
with one self or with his or her spouse. This time Aslam asked each person
including his wife to write on a slip of paper how many hands he or she has
shaken. Surprisingly each person gave a different answer but Aslam does
not know which answer belongs to whom. Now Aslam is curious about the
following: (a) Did a female shake hands with a male? (b) Did Salma shake
hands with a male friend? (c) How many times Salma shake hands? (d) How
many times Aslam shake hands? (e) Assume that Salma has not shaken
hands with any male friend then did Aslam shake hands with a female?
Under such conditions was there a husband who refused to shake hands with
a female?
A B A B
C C
H
H
G
G
D
D
F E F E
Figure 4.3.1: We show some intermediate stages (and hints) in the solution
of Problem 4.1.2. The diagram on the left shows the graph in the making.
The diagram on the right shows the next stage. It is not yet obvious where
Salma is and where Aslam is? Each bigger circle in these diagrams contains
a husband and wife pair.
E F
G
D
C
H
B A
A B A B A B
H
C C
H
D
G
D
F E F E F E
Figure 4.4.1: A Graph is shown in the left diagram and its complement is
shown in the middle. The degree sequences of both these graphs are also
indicated. A completely connected graph is shown in the right diagram.
Figure 4.5.1: We show four graphs each having the same degree sequence.
Out of these graphs at least two are equal graphs; at least two are unequal
graphs (why?); at least two are non isomorphic graphs (why?); and at least
two are isomorphic but not equal. Please note that vertices with the same
degree are drawn in the same color.
Figure 4.5.2: Graph G shown in the top left corner is not equal to the graph
H shown in the top right corner. This is evident from the adjacency matrices
of the two graphs shown in the bottom diagram. There is still a possibility
that the two graphs are isomorphic to each other as the two graphs have a
number of similar qualities and no obvious differences.
Equal Graphs & Isomorphic Graphs 85
difference like the one that we have found for the two middle graphs of Fig.
4.5.1?
These two graphs may still be isomorphic? What does that mean? Two
graphs G and H are isomorphic provided they can be drawn with identical
graph drawings. If, for example, we rotate graph H of Fig. 4.5.2 by an angle
of 180 degrees then we get a drawing which is exactly the same as the graph
G; this is shown in Fig. 4.5.3.
Figure 4.5.3: Graph H shown in the top right diagram of Fig. 4.5.2 is redrawn
here in the top right corner after rotating it by an angle of 180 degrees. It is
quite obvious now that the drawing in the top left diagram is the same as the
drawing shown in the top right corner. Let f be an isomorphism from the
left graph (G) to the right graph (H). Then the two graphs are isomorphic
provided there is an edge between f (u1 ) and f (u2 ) in the right graph if and
only if there was an edge between u1 and u2 in the left graph for every two
vertices in the two graphs as shown in the bottom diagram of this figure.
86 Basics of Graph Theory
Figure 4.5.4: Eight graphs with same number of vertices, and the same
degree sequence. Find which two graphs are equal (and isomorphic), which
are isomorphic but not equal and which are not isomorphic (and also not
equal).
we claim that it is not possible to draw a graph for that sequence (why it is
not possible?). So we need to study necessary and sufficient conditions for a
sequence to be graphical; we have already studied some necessary condition
at the start of this chapter but those conditions were not sufficient (for a
sequence to be graphical). Before moving forward find a sequence which sat-
isfies all necessary conditions that we have discussed (earlier in this chapter)
but it is impossible to draw a graph corresponding to this sequence. How
about the degree sequence 543211?
Let us assume we are given a degree sequence SG equal to 4443322. The
maximum degree here is 4 and the total number of integers in the degree
sequence is 7. We apply the following procedure on this degree sequence SG
and convert it into a new degree sequence SH which is equal to 332222.
edges emanating from G will also be removed and that amounts to operation
number 2. This is equivalent to reducing the degree by one of all adjacent
vertices of v in G. You may have realized that the new graph will have
a degree sequence equal to SH . This meaningful interpretation is possible
provided we have the crucial assumption: the highest degree vertex v in G
is connected to the first u vertices (after v) in the degree sequence SG of G.
Claim 4.6.1. If the new sequence SH is graphical then the original sequence
SG is graphical.
Claim 4.6.2. If the original sequence SG is graphical then the new sequence
SH is graphical.
It is interesting to note that the above claims not only provides us a necessary
and sufficient condition for a sequence to be graphical; they also provide us
means to draw a graph corresponding to a graphical sequence. So before
proving the above claims let first do the more interesting exercise of finding a
The Degree Sequence 89
graph. Let us start with the same degree sequence, verify that it is graphical,
and then find the actual graph corresponding to this sequence. We start with
a p length degree sequence, and apply the following steps which outputs
whether the sequence SG is graphical or not.
2. If you get a number less than zero in the new sequence then the original
sequence was not graphical (output “No”, and terminate) otherwise
there are two possibilities. Either by carefully looking at the latest
new sequence, you can draw the corresponding graph (output “Yes”,
and terminate) or if it is not possible then repeat step (1) on the latest
new degree sequence (but sort it if it is not already sorted).
Each iteration in the above procedure makes the degree sequence smaller in
size; a stage comes when it is very much possible to draw the corresponding
graph as shown in Fig. 4.6.2. In this figure the seven digit degree sequence
is converted into a six digit degree sequence and then ultimately into a four
digit degree sequence as shown in the top diagrams. The four digit degree
sequence can be recognized to be a graphical sequence. The corresponding 4
vertex graph is shown in the middle right diagram. It is then converted into a
5 vertex graph, and eventually into a 7 vertex graph as shown in the middle
left diagram of Fig. 4.6.2. It is very much possible to have two different
graphs corresponding to the same degree sequence; this has been illustrated
by the bottom diagram of the same figure. The resulting graph shown in
the bottom left diagram is different from the middle left graph shown in the
same figure. The two graphs are neither equal nor isomorphic (why?).
90 Basics of Graph Theory
Figure 4.6.2: A seven vertex degree sequence is shown in the top left corner.
This seven vertex sequence is converted into a six vertex sequence, and then
ultimately into a four vertex sequence as shown in the top right corner.
This four vertex sequence is graphical as shown by the middle and bottom
right diagrams. We get a four vertex graph which is then converted into
a five vertex graph and ultimately into a seven vertex graph as shown in
the middle and bottom left diagrams. It is very much possible to get two
different graphs from the same degree sequence.
Let us now take up Claim 4.6.1; it says that if a new sequence SH is graphical
then the original sequence SG is also graphical. For example if 332222 is
graphical then 4443322 is graphical. Can you design a formal proof for this
claim? Is your proof based on induction or is it proof by contradiction?
Discuss briefly.
Let us design an algorithm to construct a graph G corresponding to an
original sequence SG provided the new sequence SH and its corresponding
graph H is given.
The Degree Sequence 91
Let us now take up Claim 4.6.2; it says that if the original sequence SG is
graphical then the new sequence SH is graphical. It essentially means that
now we are given a graph G corresponding to a degree sequence SG . We are
also given the new sequence SH and we need to show that it is graphical;
this means that we need to draw the corresponding graph H. If you can
prove Claim 4.6.1 then you should be able to prove this claim also; just move
backwards in Fig. 4.6.2? Can you design an algorithm similar to (or almost
a mirror image of) Algorithm 14.
Problem Set 4.3.
Problem 4.3.1. Does Algorithm 14 perform the intended function correctly?
Problem 4.3.2. Does Algorithm 15 perform the intended function correctly?
This is an important question because the answer may be no. The said algo-
rithm provides a correct solution provided we have the crucial assumption:
the highest degree vertex v in G is connected to the first u vertices (after v)
in the degree sequence SG of G. If the assumption is not true then this algo-
rithm will not provide correct results; this is certainly a serious deficiency of
this algorithm? Before correcting this deficiency let us look at it a bit more
closely in the next part.
Problem 4.3.3. Assume that we are given a degree sequence 4443322 and
a graph G as shown in the left diagram of Fig. 4.6.3. Is it possible to apply
92 Basics of Graph Theory
Figure 4.6.3: Two graphs with the same degree sequence 4443322. Please
note that the two graphs are not isomorphic.
3. From graph H, remove edges (v1 , vk ) & (vj , vn ), and insert edges
(v1 , vj ) & (vk , vn ); graph H is now transformed into graph G?
94 Basics of Graph Theory
We show our strategy in handling the necessary & sufficient conditions for
a degree sequence to be graphical in Fig. 4.6.5. We show the position of
our claims and the respective proofs. Please note that the three algorithms
perform a dual purpose; they help us in transforming one graph into another
but more importantly they provide crucial insight in designing constructive
proofs for the two claims.
Figure 4.6.4: A seven vertex graph is shown in the left diagram with a degree
sequence shown in the top of the graph. There are three vertices in this graph
with a highest degree equal to four. All these three vertices (with a degree of
4) are connected to a vertex of minimum degree; the minimum degree in this
graph is 2. The graph in the left diagram is converted into a graph shown in
the right diagram by deleting two edges and by inserting two edges (shown
in bold). The graph in the right diagram has the same degree sequence but
there is an important difference. Here there is at least one vertex (v1 ) with
a (highest degree equal to four) which is connected to vertices with higher
degrees only; v1 is connected to two vertices with degree 4 and two vertices
with degree three; it is not connected to a vertex of degree 2.
96 Basics of Graph Theory
Figure 4.6.5: A seven vertex graph is shown in the left diagram with a degree
sequence shown in the top of the graph. There are three vertices in this graph
with a highest degree equal to four. All these three vertices (with a degree
of 4) are connected to a vertex of minimum degree; the minimum degree in
this graph is 2.
The Degree Sequence 97
Figure 4.6.6: A graph with a given degree sequence shown in the top left
diagram is converted into another graph with the same degree sequence shown
in the bottom diagram.
Problem 4.4.3. Let a degree sequence consisting of 11 numbers be SG =
a, b, c, d, e, f, g, h, i, j, k and assume that a = 5. Let another sequence be
SH = b − 1, c − 1, d − 1, e − 1, f − 1, g, h, i, j, k. Do we know how to draw the
graph for SH provided we have a graph for SG ? Discuss briefly. The figure
down below may be helpful for your imagination.
98 Basics of Graph Theory
Figure 4.6.7: Two graphs are shown in this figure. The graph shown in the
left diagram is a connected graph while the graph shown in the right diagram
is a disconnected graph. Both these graphs have the same degree sequence
which is 54444411111.
Problem 4.4.6. If the answer to the above problem is no then how can we
do some thing to make sure that in the final graph, the highest degree vertex
v (with a degree equal to u) will always be connected to the first u vertices
in the degree sequence SG ?
Problem 4.4.10. What are necessary & sufficient conditions for a degree
sequence SG to be graphical such that in the resulting graph G the highest
degree vertex v (with a degree equal to u) in the graph should be connected
to the last u vertices in the degree sequence SG .
100 Basics of Graph Theory
Figure 4.6.8: Two graphs are shown in this figure. The graph shown in the
left diagram is a connected graph while the graph shown in the right diagram
is a disconnected graph. Both these graphs have the same degree sequence
which is 54444411111.
Figure 4.8.1: A simple graph containing no self loops and no parallel edges.
It can be represented by an adjacency matrix. How about graphs which are
not simple?
Figure 4.9.1: A cyclic graph which is neither bipartite nor acyclic is shown
in the right diagrams. A bipartite graph which is not acyclic is shown in the
middle diagrams; an acyclic graph (or a tree), shown in the left diagrams, is
also a bipartite graph.
104 Basics of Graph Theory
Figure 4.9.2: We show a number of non isomorphic trees with p larger than
1 and smaller than 6.
Figure 4.9.3: A graph G is shown in the left diagram. This graph is in fact
a bipartite graph as shown in the middle diagram consisting of an A partite
and a B partite. The minimum (sized) vertex cover and the maximum (sized)
independent set in graph G are shown in the right diagram.
106 Basics of Graph Theory
A graph G is a star graph if the degree of one vertex is p − 1 while the degree
of every other vertex is 1. A start graph consisting of two vertices is a special
case.
A graph G is referred to as a forest if it contains a collection or set of trees.
Also a star graph, being acyclic, is always bipartite where one partite consists
of size 1 while the other of size p − 1.
We show a number of k-regular graphs in Fig. 4.9.5. Please note that all
these graphs are bipartite as shown in the bottom diagrams of this figure.
appreciate the fact that a cycle graph is bipartite if it consists of even number
of vertices, while it is not bipartite if the cycle graph consists of odd number
of vertices. What may not be obvious is that an odd cycle graph will always
be a 3-partite graph as shown in this figure.
Figure 4.9.8: Graphs B and C are derived from graph A by deleting certain
edges.
Figure 4.9.9: Graphs E and F are derived from graph A by deleting certain
vertices.
Broad Categories of Graphs & some Special Graphs 111
bipartite graph? (b) Draw all non-isomorphic bipartite graphs which are
connected, and are regular consisting of eight vertices. (c) Draw all non-
isomorphic graphs consisting of 12 vertices and are 6-regular. (d) Draw all
non-isomorphic graphs consisting of eight vertices which are 3-regular. Out
of these graphs indicate which ones are bipartite graphs.
Problem 4.5.5. The graph G shown in the figure below is derived from
graph A by subtracting certain edges from graph A. Note that graph G is
5-regular. Draw all non-isomorphic graphs consisting of 8 vertices which are
5-regular.
graph, a cyclic graph and a completely connected graph. We have also talked
about certain properties of graphs, like a Hamiltonian path or an Eulerian
path in a graph. We have also talked about a walk, a trail, or a path be-
tween two vertices in a graph. It will be interesting if we integrate a couple
of concepts with a number of properties.
114 Basics of Graph Theory
6. Eccentricity of a vertex
Figure 4.11.1: A graph and its complement which is isomorphic to the original
graph.
Does this mean that all regular graphs are self complementing? But a cycle
graph of three or six vertices is not self complementing? There must be a
class of regular graphs which will be self complementing? The following fig-
ure shows the same graph along with the corresponding completely connected
graph of five vertices. The complement c(G) of graph G is obtained by delet-
ing all edges of graph G from the corresponding completely connected graph.
If c(G) is isomorphic to G then it should have the same number of edges as
G - that means the degree of every vertex in a regular self complementing
graph should be p−1 2
. Then the number of edges in the graph will be exactly
p(p−1)
4
. Does that mean that p or p − 1 in an SC graph should be divisible by
4? Are these necessary or sufficient conditions for such a regular graph to be
self complementing?
It will be rewarding if you draw a couple of regular graphs with the property
that the degree of each vertex is p−1
4
. Can you draw such a graph if p is even?
Can you draw such a graph if p is any odd number? It will be essential to
answer the above questions before moving forward?
If you have tried to draw such graphs then you will soon realize that a n-
regular graph (where n = p−1 4
) is possible if and only if p = 4k + 1 where
k is an integer equal to or larger than one. We have already drawn such a
graph with p = 5 when k = 1. When k = 2, p will become 9. Let us try
to explore graphs with 9 vertices with the degree of each vertex equal to 4.
Will all such graphs be isomorphic to each other? Will such graphs (or at
116 Basics of Graph Theory
least some of them) self complementing? If you take the complement of any
such graph it will certainly have the same degree sequence and same number
of edges? Again it will be rewarding if you draw some of these graphs before
arriving at a conclusion?
We show three such graphs in the figure below. Are any two of them isomor-
phic to each other? Is one complement of the other? Are they self comple-
menting? Please try to answer these questions before moving forward.
It will be exciting if you draw the complement of the left graph? You will
soon realize that the top right graph is the complement of the top left graph
while the bottom graph is isomorphic to the top right graph. What does this
example tell us?
Self Complementing Graphs 117
1 1
9 2 9 2
8 3 8 3
7 4 7 4
6 5 6 5
6 5
2 9
7 4
3 8
Figure 4.11.3: Top left diagram shows a graph G. Top right diagram shows
the complement c(G) of graph G. The bottom graph is equal to c(G).
118 Basics of Graph Theory
If you actually draw all non isomorphic graphs with p = 9 and degree of each
vertex equal to 4 you will realize that there are 16 such graphs possible. Most
of them transform into one another if you take the complement of one such
graph as shown above. A very few of them indeed transform into themselves
when you take the complement. One such graph is shown in the figure below.
1 1
7 4 9 2
5 6 8 3
2 9 7 4
8 3 6 5
1 1
13 2 12 3
12 3 10 5
4 7
11 8
5 9
10 6
6 11
9 4
8 7 2 13
1 2 3 4
1 2 3 4
Necessary Conditions
Is it possible to have a non regular graph of five vertices which is self comple-
mentary? Let us try out various degree sequences with the above necessary
condition? There are only three choices possible under the conditions laid
out before: 22222, 33211, & 32221. The first degree sequence corresponds
to a cycle graph which was regular and we have already seen it. The other
two belongs to non regular graphs and are of interest to us. Now we have to
verify if these degree sequences are really graphical, and if they are then do
they really belong to graphs which are self complementary? Let us start with
the sequence 32221. Can you draw its graph and check out if the graph is
self complementary? The sequence is graphical but is itself complementing?
120 Basics of Graph Theory
Now let us check the other sequence which is 33221; this indeed comes out
to be graphical as well as self complementary as shown below:
1 2 3 4
1 2 3 4
Please note that the condition (the degree sequence of c(G) should be such
that if we add it (after sorting it in increasing order) with the degree se-
quence of G (having p vertices) then we should get the degree sequence of a
completely connected graph of p vertices) is a necessary condition; it is not
sufficient. Thus this condition along with others (for example the number
of odd degrees in a degree sequence should be even for a degree sequence to
graphical) may narrow down our search for SC graphs but we always have
to verify if a given graph G is a SC graph? These simple conditions tells us
that an SC graph (which may or may not be regular) can only have p equal
to 4k or 4k + 1 vertices where k can be equal to or larger than 1. We have
already seen a regular SC graph of 4 + 1 = 5 vertices; we have also seen a
non regular SC graph of 5 vertices; we have seen a non regular SC graph of
4 vertices in the last diagram. In fact we have earlier claimed that a regular
SC graph can have only 4k + 1 vertices; we can safely say that a SC graph
of 4k vertices will always be non regular? Why?
Self Complementing Graphs 121
Let us actually construct such graph and see how it looks? Assume that G
122 Basics of Graph Theory
is a line graph of two vertices - then c(G) will simply consist of two isolated
vertices. How the above configuration would look like when it is actually
drawn? If you look at the diagram below you will realize that the graph is
indeed self complementary. The degree sequence of this graph is 44443333 -
it is one of the sequences that we have predicted earlier in our discussion?
We can also start with G equal to two isolated vertices - then c(G) will be a
line graph of two vertices. Using these building blocks you can also construct
another SC graph? If you have the patience of constructing such a graph you
will realize that its degree sequence will be 55552222. The number of vertices
in the SC graph will still be 4p where G has p vertices.
7 5
5 6 1 2
8 6
1 3
3 4 7 7
2 4
Figure 4.11.9: Here graph G is a line graph of two vertices 1 & 2. Then
c(G) will be its complement - and that will be two isolated vertices 3 & 4.
Another copy of G will consist of a line graph of two vertices - consisting of
vertex 7 and vertex 8.
c(G)
G
c(G)
G
c(G
) G
c(G) G
x x
Figure 4.11.10: Here vertex G is a graph while vertex c(G) is the complement
of graph G. An edge between G and c(G) means that every vertex G is
connected to every vertex of c(G). Vertex x may be a single vertex graph.
The resulting super graph is a self complementing graph.
The following diagrams will help you answer these interesting questions.
Please find the degree sequence of each of the following graph? Can you
figure out how these SC graphs are constructed?
124 Basics of Graph Theory
Here we show another interesting graph which is not regular but is self com-
plementing. Its degree sequence is 774444411. This graph may also provide
insights needed to design an intelligent algorithm to solve the above problem.
Should we always remove the vertex with degree equal to 4? Why? Should
that vertex be connected with lower degrees or higher degrees?
Now start with the same original graph (with a degree sequence equal to
44443333) while the new graph should have a degree sequence equal to
444444444. The resulting graph is SC and is also regular - although it has not
been drawn in a suitable manner in the top diagram. The graph is redrawn
in the bottom diagram to emphasize that it is indeed a beautiful regular
graph?
130 Basics of Graph Theory
So the problem is where to connect the inserted vertex and what should be
its degree? Perhaps another example may provide you a solution? Please
check the degree sequence of the graph before after inserting a new vertex?
Again can we prove that for any graph G the following configuration will
give a SC graph with diameter not more than 2?
We shall now attack the more general problem? But before that let us refresh
our knowledge regarding some old concepts: the eccentricity of a vertex, and
the radius and diameter of a graph.
Let us see how the eccentricities of various vertices look like in a line graph
of eight and five vertices as shown below.
The diameter in the top graph will be 7 while the radius will be 4 in the
top diagram. The diameter and radius for the bottom line graph will be
respectively 4 and 2. Similarly we show another five vertex graph where the
134 Basics of Graph Theory
Another five vertex graph G and its complement is shown below. Here the
eccentricity of each vertex in G is 2 except for vertex 1. Thus its diameter is
2 while its radius is 1. The diameter in the complement of graph G is infinite
while its radius is also infinite.
The five vertex line graph and its complement are shown in the bottom
diagram. The eccentricity of each vertex is indicated in the line graph as
well as in its complement. Please note that the diameter in the line graph
was 4 while it has reduced to 2 in the complement of the line graph. The
diameter as well as the radius in the complement graph is 2.
You can well imagine that if the diameter in a graph G is more than 3 then
Self Complementing Graphs 135
Figure 4.11.27: A bipartite graph BP shown in the top left diagram. The
corresponding completely connected graph is shown in the top right diagram.
The complement of graph BP is shown in the bottom diagram.
Figure 4.11.29: Top left diagram shows a bipartite graph BP. Top middle
diagram shows the corresponding completely connected bipartite graph. Top
right diagram shows the bipartite complement of the bipartite graph. The
bottom diagrams once again shows the two bipartite graphs shown in the
standard form.
The degree sequence of the above graph is 22222222. While that of the
completely connected bipartite graph is 44444444. As you can see this is a
regular SC bipartite graph.
The following bipartite graph is a non regular self complementing graph
with a degree sequence 33222211. The degree sequence of a corresponding
completely connected bipartite graph is also 44444444.
Here we show another Category B SC graph with red vertices in one partite
Self Complementing Graphs 139
vertices in graph A.
It can be observed that the following properties should hold for the original
graph G to be SC.
3. The graph G(A) and graph G(B) - there is some relationship between
these two graphs?
142 Basics of Graph Theory
(or a permutation) which maps the vertices of the new graph back onto the
vertices of graph G such that adjacency as well as non adjacency is preserved
in the two graphs. In this case this permutation p2 will be (4321)(8765)(9).
Figure 4.11.35: Graph G (top left diagram) and another graph (top right
diagram) where the vertices of G are permuted according to the given per-
mutation p1 . The two graphs are not equal but they are isomorphic thus the
permutation p1 is not an automorphism of graph G. Please note that graph
G is equal to the bottom graph
Figure 4.11.36: Graph G (left diagram) and another graph (right diagram)
where the vertices of G are permuted according to the given permutation p.
The two graphs are not only isomorphic but also equal thus the permutation
p is an automorphism of graph G but it is not a complementing permutation
of graph G.
Figure 4.11.37: Graph G (top left diagram) and another graph (top right
diagram) where the vertices of G are permuted according to the permutation
p. This new graph is also the complement of graph G - that means the
permutation p is a complementing permutation of graph G but it is not
an automorphism of graph G. The bottom diagram shows another graph
where the vertices of p(G) are permuted once again according to the same
permutation p. This new graph is equal to graph G - thus p2 (G) is an
automorphism of graph G.
146 Basics of Graph Theory
Let us now consider certain self complementary bipartite graphs and the
related complementary permutations - two of them are shown in the figures
below.
Introduction
We have studied a number of concepts related to graph theory in the last
chapter. For example, we talked about a bridge edge and a cycle in a graph.
We have also studied a number of properties linking different concepts. For
example an edge is a bridge edge provided its removal disconnects a graph.
We have also studied that a graph is cyclic if it contains a cycle. In this
chapter we shall study a number of graph algorithms. For example, how can
we check if an edge is a bridge edge; how can we check if a graph is cyclic;
how can we find an actual cycle in a cyclic graph (see Concept Map 5.1).
Almost all of these algorithms are based on the following easy to understand
and friendly to use buliding blocks:
The initial part of this chapter is based on one of our papers published in
IJECE, Vol. 2, No.2, Summer-Fall 2003. The paper was co-authored by Sara
Tahir, and its title was Should We Teach Algorithms. The later part of this
chapter is based on one of our CS department research reports which was co-
authored by Komal Syed, and Yasser Hashmi and its title was Shortest Path
Algorithms - Making and Breaking Connections. We also provide a number
of powerful learning tools to understand and design various algorithms. The
recursiuon tree and the colored puzzle are some of these visual aids which
facilitate a learner or a designer in his or her path towards discovery. In
this chapter, we provide a detailed study of a number of graph algorithms
that have applications in diverse fields like chemistry, biology, mathematics,
engineering, social sciences, and ofcourse computer science. The original
research papers were about encouraging students to discover and learn (graph
algorithms) by themselves with minimal help provided by an instructor in
the form of provocative questions.
155
j j
g i g i
h h
k k
f a f a c
c
d d b
b
e e
Figure 5.2.1: Two pictures of what the Bucket B will look like in the initial
stages of the Bucket Algorithm.
The Bucket Algorithm 159
of existing powerful algorithms in graph theory ([16], [17], [2], [11], [15],
[14], [4]). With some encouragement from the instructor, the students should
develop a keen desire and ability to understand the motives behind, and the
procedures followed in order to arrive at innovative solutions. They would
learn the ways and means of devising their own algorithms. Specifically the
Bucket Algorithm would be used to solve the following problems:
j j j
g i g i g i
h h h
k k k
f a c f a f a c
c
d d d b
b b
e e e
Figure 5.2.2: Two pictures of the Bucket B after different iterations through
the Bucket-Algorithm.
example if a teacher is talking about Quick Sort, he/she cannot expect his/her
students to discover the said algorithm just after understanding the sorting
problem. The teacher should first make the students appreciate the need for
partitioning the array into halves such that all numbers in the first half are
smaller than each number in the second half. Why we should do this and
how should we do this are both equally important for designing, discovering
(and even understanding) the said sorting algorithm. The understanding of
the previous state of an abstract system and the (usefulness of the) final
system state after the application of a so called fundamental operation [7]
(for example the partitioning procedure in Quick Sort) is crucial in problem
solving in computer science as in elsewhere. Please see Concept Map 5.2.
Assuming that the students know what a connected graph is, the instructor
should ask the students: “Can you modify the Bucket Algorithm such that
you may be able to determine whether a given graph G is connected?” The
emphasis should be on using the existing techniques with minimum modifica-
tion. The answer is simple: if, after the Bucket Algorithm has been applied
to a graph G, there are still any nodes left outside the bucket then the graph
is not connected. If, however, all nodes come inside the bucket, then graph G
is connected. Notice while students were becoming familiar with the Bucket
Algorithm, the instructor asked when there would be nodes left outside the
bucket. Brighter students would have been able to identify at that stage that
some nodes will be left outside the Bucket B when a graph is not connected
since cross edges do not exist connecting them to nodes inside the bucket
(Figure 5.3.1). Please see Concept Map 5.2.
Not all students may be able to identify this property of the Bucket Al-
gorithm. The instructor in this case will have to make an extra effort to
guide such students. Once all students have understood the solution (hav-
ing arrived at it on their own with well-timed prodding from the instructor)
the instructor should start the discussion regarding cost calculation, i.e., the
complexity of the modified algorithm.
Finding if a Graph is Connected 163
j j
g g
i i
h h
k k
f a c f a c
d b d b
e e
Figure 5.3.1: A graph G that is not connected; once the Bucket Algorithm
terminates, nodes i and j will be left outside the Bucket B.
j j
g g
i i
h h
k k
f a c f a c
d b d b
e e
Figure 5.3.2: A cut edge or bridge is one whose removal produces a graph
with more connected components than the original graph.
We know that a tree has the minimum number of edges required to connect a
given number of vertices. A spanning tree of a given graph also satisfies this
property, as it is a tree. Thus the spanning tree of a tree would be exactly
the same tree. This definition or property can be used to design an algorithm
to check if a given graph is a tree.
5.4.4 A Comparison
graph. How efficient would this be if compared with the algorithms described
earlier?
Concept Map 5.2. The Concept Map and an iterative sequence (ascending
order) of asking questions help student discover or understand a number of
useful graph algorithms.
j j j
3 3 3
g 4 i g 4 i g 4 i
2 3 2 6 3 2 6 3
h 6 h h
4 k 4 k 4 k
9 5 9 5 9 5
3 a a a
f c f 3 c f 3 c
2 2 2
2 2 2 2 2 2
d b d b d b
3 1 3 1 3
e 1 e e
j j j
3 3 3
g 4 i g 4 i g 4 i
3 3 3
2 h 6 2 6 2 6
h h
4 k 4 k 4 k
9 5 9 5 9 5
3 a a a
f c f 3 c f 3 c
2 2 2
2 2 2 2
d d 2 d 2
b b b
3 1 3 1 3
1
e e e
j j
3 j 3
3
g 4 i g 4 i
g 4 i
3 2 3
2 6 3 h
h 2 h 6 k
4 k k 4
4
X
9 5 3 a 5
3 a a 5
f 3 f c
c f c
2 2 2
2 2 2 2 2
d 2 d
b d b
3 b 3
3 1
1 1 e
e e
j j
3 3
g 4 i g 4 i
2 3 2 3
h h
4 k 4 k
3 a 3 a
f c f c
2 2 2 2
2 2
d d
X3 b b
1 1
e e
Figure 5.6.3: Pictures of the graph after cutting edges of maximum weight
in a Krushkal’s like algorithm. The minimum spanning tree is also shown in
the bottom right corner.
170 Basics of Graph Algorithms
j j j
3 3 3
4
g 4 i g 4 i g i
3 3 2 3
2 h 6 2 h 6 h 6
4 k 4 k 4 k
9 5 9 5 9 5
3 a 3 a 3 a
f c f c f c
2 2
2 2 2 2
2 2 2
d d d
b b b
3 3 3
1 1 1
e e e
j
3 j j
3 3
4
g i 4 4
g i g i
2 3
h 6 2 3 3
h 6 2 h 6
4 k
4 k 4 k
9 5
3 a 9 5 9 5
3 a 3 a
f c
2 f c f c
2 2 2
2 2 2
d 2 2
b d d
3 b b
1 3 3
e 1 1
e e
Figure 5.6.4: Pictures of the graph after growing edges of minimum weight
in a Krushkal’s like algorithm.
Finding a Minimum Spanning Tree of a Weighted Graph 171
of each tree would be the same (why?). Please note that here we are not
using any fancy data structure since the objective is not to have a complicated
design, unlike some textbooks.
It would be useful if the students were asked to prove that this greedy ap-
proach would actually find a minimum spanning tree. Without reading proofs
given in the textbook they should come up with something of their own mak-
ing. A lively discussion can be initiated to find the merits and demerits of
individual work. They should also be asked to derive the time complexity
172 Basics of Graph Algorithms
j j
3 3
g 4 i g 4 i
3 2 3
2 h 6 h 6
4 k 4 k
9 5 9 5
3 a 3 a
f c f c
2
2 2 2
2 2
d b d b
3 3
1 1
e e
j j
3 3
g 4 i g 4 i
2 6 3 2 6 3
h h
4 k 4 k
9 5 9 5
a a
f 3 c f 3 c
2 2
2 2
2 2
d d b
3 b 3
1 1
e e
12 12 12 12
c b c b c b c b
2 6 1 2 6 1 2 6 1 2 6 1
7 7 7 7
5 8 5
g a f g a 8 f g 5
a 8 f g 5
a 8 f
14 4 3 11 14 4 3 11 4 3 4 3
14 11 14 11
d 9
e d 9
e d e d e
9 9
12 12 12
c b c 12
b c b c b
2 6 1 2 6 1 2 6 1 2 6 1
7 7 7 7
5 5 5 8
g a 8 f g 5
a 8 f g a 8 f g a f
4 3 4 3 4 3 14 4 3 11
14 11 14 11 14 11
d e d e d e d 9
e
9 9 9
12 12 12 12
c b c b c b c b
2 6 1 2 6 1 2 6 1 2 6 1
7 7 7 7
5 8 5
g a f g a 8 f g a 8 f g a 8 f
5 5
14 4 3 11 14 3 11 4 3
4 14 4 3+43 11 14 11
9
d 9
e d e d e d e
9 9
Boruvka’s Algorithm
on the Boruvka’s algorithm) is shown in action (on the same weighted graph)
in the bottom diagrams of Fig. 5.6.6.
It is interesting to note the following about the three minimum spanning tree
finding algorithms:
4. If all edge weights in a graph are unique then we do not need a cycle test
in Boruvka’s algorithm. But we do need a cycle test in case the edge
weights are not unique. Why? Do we need a cycle test in Kruskal’s
algorithm in case all edge weights are unique? We do not need a cycle
test in Prim’s algorithm in spite of the fact that edge weights are not
unique? Why?
Problem 5.1.2. Apply the above algorithm to the following graph G con-
sisting of positive as well as negative edge weights. Do you think there will
be any complication in finding a maximum spanning tree if the edge weights
are negative (or if they are positive). We shall repeat this problem while find-
ing the shortest path (or the longest path) in a graph having negative edge
weights. We shall discuss that the shortest path problem becomes compli-
cated if all edge weights are not positive. Do we face a similar problem while
finding a minimum spanning tree of a graph having negative edge weights?
Finding a Path in a Graph 177
Figure 5.6.7: A Graph G having negative edge weights for problem set.
j j j
g i g i g i
h h h
k k k
f a c f a f a
c c
d b d d
b b
e e e
j j
g i g i g i
h h h
k k k
f a c f a f a
c c
d b d d
b b
e
Figure 5.7.1: Pictures of the graph after cutting edges in order to find a path
between vertex a and vertex c.
The Shortest Path Problem 179
j
j
g i
g i
h h
k k
f a c f a c
d b d b
e e
j j
g i g i
h h
k k
f a c f a c
d b d b
e e
a a
f f
g d g d
h h
e b e b
i i
j k j k
c c
Figure 5.7.2: Various stages in finding a path from every vertex to vertex a
in a graph.
The Shortest Path Problem 181
j 17 j 15 j
3 3 20 3
11 12
g 4 i g 4 i 4 i
7 g
3 2 6 3 3
2 h 6 h 23 2 h 6 9
k 4 13 k 4 k
4 9
9 5 9 5
a 9 5 a 3
3 7 f a
f c 3 0 c 4 f 3 c
2 0 2
2 2 2 2 4
2 d 2 2
d b b d
3 3 3 b
6 1 5 6
e 1 e
2 1 5 2
e
Figure 5.8.1: The middle diagram shows a minimum spanning tree (MST) of
a weighted graph G shown in the left diagram. The right diagram shows a
shortest path spanning tree (SST) of the same graph. The shortest distances
(as shown in red color) from vertex a provided by the two spanning trees
are different as indicated in these diagrams. The weight of the minimum
spanning tree is also different from that of the shortest path spanning tree.
In order to assert that Dijkstra’s like algorithm is able to find correct results
let us first present a scenario where the above mentioned algorithm fails
to find shortest paths. Remember that this algorithm works on a greedy
strategy – it makes decisions on the basis of local conditions – but produces
optimal results on a global basis provided all edge weights in a graph are
positive. We show a directed graph in the Figure 5.8.3 where some edge
weights are negative. It is obvious from this figure that Dijsktra’s algorithm
fails to produce correct results.
The same weighted graph and its (correct) shortest path spanning tree is
shown in Fig. 5.8.4. We show another weighted graph in the right diagram
of this figure. This graph is different from the one shown in the left (and
middle) diagram in the following ways:
184 Basics of Graph Algorithms
j j j
3 3 3
g -4 i g -4 i g -4 i
3 2 6 3 2 6 3
2 h 6 h h
4 k 4 k 4 k
0 0 9
9 3 9 3 3
3 a 3 a 3 a
f c f c 3 f c
2 2
2 2
2 2 2 2
d d 2 d
-4 b -4 b -4 b
-5 -5 2 -5 2
e e e
5
j j j
3 3 3
7 8
g -4 i g -4 i g -4 i
2 6 3 6 3 2 6 3
h 2 h h
4 k 4 k 4 9 k 7
0 0 0
9 3 9 3 9 3
3 a a a
3 f c 3 f 3 c 3 f 3 c
2 4 2 4 2 4
2 2 2 2 2
d d 2 d
-4 b b 5 -4 b
-5 5 -4 0 -5
2 -5 2 2
e e e
Vertex c at a shortest distance Vertex d at a shortest distance The (incorrect) Shortest Path
of (at most) three edges of (at most) four edges Spanning Tree
Figure 5.8.3: Pictures of the Bucket B while finding a shortest path spanning
tree using a Dijsktra’s like algorithm. The final answer as shown in the
bottom right diagram is incorrect.
186 Basics of Graph Algorithms
1. The shortest distance path from vertex a to any other vertex is a simple
path – no edge or vertex is repeated in this path. The magnitude of this
shortest path is finite. In other words the shortest distance between
any two vertices does not reduce if we move in a cyclic path. The above
observation is true for the left or the middle graph in the figure below
(Fig. 5.8.4). Please note that there are negative weight edges in this
graph but no negative weight cycles: a negative weight cycle is a cycle
in a graph where the net sum of the weight of the edges in the cycle is
less than zero.
2. In the graph shown in the right diagram (Fig. 5.8.4) the above obser-
vations are not true. The shortest distances of the red vertices keep on
decreasing if we move in a cyclic path as shown in red color with the
result that the shortest distance of such vertices ultimately approaches
minus infinity. This happens in any graph where there are negative
weight cycles.
5 5
j j j
3 3 3
7 8 8
g -4 i g -4 i g -12 i
6 3 7 3
2 h 2 6 2 6 3
h 6 h
4 9 k 7 4 9 k 4 k
0 9 0 0
3 9 3 9 3
a a a
3 f 3
2 c 3 f 3 c 3 f 3 c
4 2 2
2 2 2 2
d d 2 d 2
5 -4 b b b
0 -5 0 -4 -4
2 -5 5 1 -5
e e e
Figure 5.8.4: The left diagram shows an (incorrect) shortest path spanning
tree produced by a Dijkstra’s like algorithm. The middle diagram shows the
correct shortest path spanning tree for the same graph. The right diagram
shows a graph where shortest distances of red vertices from vertex a keep on
decreasing if we move in a cyclic path as shown in red.
The Shortest Path Problem 187
j j j
3 3 3
12
g -4 i g -4 i 12 g -4 i
7 7
2 3 2 6 3 2 6 3
h 6 h 9 h 7
9 4 k 4 9 k
4 k
0 0 0
9 3 9 3 9 3
3 3 a 3 a a
f 3 f c 4 3 f 3 2 c 4
c 2
2 2
2 2 2 2
d 2 d d
b 2 -4 b -5 -4 b
-4 -5 5 2 0 5 1
-5 e e
e
Vertices at (at most) two Vertices at (at most) three
Vertices at one edge
edge shortest distances: edge shortest distances:
shortest distances
Not Stable Not Stable
5 5 5
j j j
3 3 3
10 8 8
g -4 i g -4 i g -4 i
7 3 7 7 3
2 h 6 2 6 3 2 h 6
7 h 6 6
4 9 k 9 4 9 k
4 k
0 0 0 9
9 3 9 3
a a 3 a
3 f 3 c 3 3 f 3 c 3 3 f 3 c 3
2 2 2
2 2 2 2
d 2 2 d
b d b -5 b
-5 5 -4 -5 -4 0 5 -4 1
0 1 0 5 1
e e e
Vertices at (at most) four Vertices at (at most) five Vertices at (at most) six
edge shortest distances: edge shortest distances: edge shortest distances:
Not Stable Not Stable STABLE
Figure 5.8.5: We show k-edge shortest paths from vertex a to every vertex
in graph G. When k changes from 1 to 5, shortest path distances of some
vertices change. But when k increases beyond 5 then there is no change in
shortest paths.
The Shortest Path Problem 189
j j j
3 3 3
-12 12
g -12 i g -12 i 12 g i
7 7
2 3 2 6 3 2 6 3
h 6 h 9 h 7
9 4 k 4 9 k
4 k
0 0 0
9 3 9 3 9 3
3 3 a 3 a a
f 3 f c 4 3 f 3 2 c 4
c 2
2 2
2 2 2 2
d 2 d d
b 2 -4 b -5 -4 b
-4 -5 5 2 0 5 1
-5 e e
e
Vertices at one edge Vertices at (at most) two Vertices at (at most) three
shortest distances edge shortest distances edge shortest distances
-3 -3
j j
3 3 j -3
10 0 3
g -12 i g -12 i 0
7 3 7 g -12 i
2 h 6 2 6 3 7
7 h 6 2 6 3
4 9 k 9 h 6
4 k
0 4 6 k
9 3 0 9
a 3 0 9
a 3
3 f 3 c 3 3 f 3 c 3
a
2 2 3 f 3 c 3
2 2 2 2
d d 2 2
-5 -4 b b 2
5 1 -5 -4 d b
0 0 5 1
e e 0 -5 5 -4
1
e
Vertices at (at most) four Vertices at (at most) five Vertices at (at most) six
edge shortest distances edge shortest distances edge shortest distances:
Still Not Stable
-∞
j -6 j -6 j
3 3 3
-12 0 -12 -3
i -∞
g i g i -12
g
7 3 7 3 7
2 6 2 6 2 6 3
h 6 h 6 h 6
4 6 k 4 6 k 4 -∞ k
0 0 9 0
9 3 3 9 3
a a a
3 f 3 c 3 3 f 3 c 3 3 f 3 c 3
2 2 2
2 2 2 2
d 2 d 2
b b d b
0 -5 5 -4
1 0 -5 5 -4
1 0 -5 5 -4 1
e e e
Vertices at (at most) seven Vertices at (at most) eight Vertices at (at most)
edge shortest distances: edge shortest distances: infinite edge shortest
Still Not Stable Still Not Stable distances
Figure 5.8.6: We show k-edge shortest paths from vertex a to every other
vertex in graph G. When k changes from 1 to 6, the shortest paths of some
vertices change. But when k goes beyond six then we are caught in a negative
cycle – the shortest path of three vertices (belonging to a negative weight
cycle and shown in red color) keeps going down, and this situation never
becomes stable.
190 Basics of Graph Algorithms
An Important Conclusion
If there are no negative weight cycles in a directed graph consisting of some
negative weight edges then we can use our k-edge shortest path technique to
find shortest paths. As soon as all the k-edge shortest paths become stable
with increase in k, we claim that we have found the shortest paths. Please
note that the resulting shortest paths will be simple paths; no edge or vertex
is repeated in these paths. It means that k would be less than or equal to
p − 1 in case there are no negative weight cycles in the graph.
Problem 5.2.1. Please recall the weighted graph of Fig. 5.8.5, the same
weighted graph is reproduced below in the left diagram of Fig. 5.8.8. Re-
member if we increase k beyond 5 while finding k-edge shortest paths then
no (shortest) distance (with respect to vertex a) changes with k. We now
change the magnitude of the weight associated with the edge (f, g) from 4
to 40 as shown in the middle diagram of this figure. Find k-edge shortest
paths with respect to vertex a while k changes from 1 to 10. Find how and
at what value of k, the shortest paths becomes stable.
The Shortest Path Problem 191
j j j
3 3 3
-12 12
g -12 i g -12 i 12 g i
7 7
2 3 2 6 3 2 6 3
h 6 h 9 h 9
9 4 k 4 18 k
4 k
0 0 0
9 3 9 3 9 3
3 3 a 3 a a
f 3 f c 4 3 f 3 c 4
c 2
2 2
2 2 2 2 2
d 2 d d
b 2 -4 b -5 -4 b
-4 -5 5 2 0 5 2
-5 e e
e
6 j
3 6 j
12 3
g -12 i 12
7 g -12 i
2 6 3 7
h 9 2 6 3
k h 9
4 18 k
0 4 18
9 3 0
a 9 3
a
3 f 3 c 4
3 f 3 c 4
2 2 2
d 2 2 2
-5 -4 b d
5 2 -5 -4 b
0 5 2
e 0
e
Figure 5.8.7: We show k-edge longest paths from vertex a to every other
vertex in graph G. When k changes from 1 to 5 the longest path of some
vertices improves. But when k goes beyond four then the longest distance of
any vertex does not change, the situation becomes stable and we claim that
we have found longest paths in this graph.
j j j
3 3 3
g -4 i g -4 i g -4 i
2 6 3 2 h 6 3 2 6 3
h h
4 k 40 k 4 k
0 0 9 0
9 3 3 9 3
3 a 3 a 3 a
f c f c f c
2 2 2
2 2 2 2
d 2 d d 2
-4 b 2 -4 b -40 b
-5 -5 -5
e e e
Problem 5.2.6. Find k-edge longest paths for each vertex with respect to
vertex a in the graph shown in the left diagram of Fig. 5.8.8. Find if the
longest paths become stable with increasing value of k.
Problem 5.2.7. Repeat the above problem for the graph shown in the right
diagram of Fig. 5.8.8.
Problem 5.2.8. What do you think are necessary and sufficient conditions
for the longest distances to become stable for a finite value of k in a weighted
graph?
Problem 5.2.9. Comment on the claim that the longest path problem is a
hard problem while the shortest path problem is a solvable problem.
are no negative weights then a simple greedy strategy (like that of Dijskrta’s
Algorithm) will solve the shortest path problem in undirected graphs. With
negative edge weights (but no negative weight cycles) we expect that our k-
edge shortest path technique will work with the hope that the k-edge shortest
paths will stabilize for a finite value of k and we shall get the optimal answer.
Remember that the so called technique was designed for directed graphs
and in order to apply it to undirected graphs we have to first convert the
undirected graph into a directed one. We show an undirected graph in left
diagram of Fig. 5.8.9. The correct shortest paths & distances with respect
to vertex a are indicated in the middle diagram. The undirected graph is
converted into a directed graph as shown in the right diagram of this figure.
0 0 5
5
5 5 a b
a b a b
5
6 -3
6 -3 6 -3
6 -3
d c d c 2
2 2 d c
4 2 2
Figure 5.8.9: We show an undirected graph with negative edge weights but
no negative weight cycles in the left diagram. The shortest distances with
respect to vertex a are indicated in the middle diagram. The undirected
graph is converted into a directed graph as shown in the right diagram.
When we apply our k-edge shortest path algorithm to the directed graph
(shown in the right diagram of Fig. 5.8.9) we run into a complication; we have
created a two-edge negative weight cycle in the directed graph. The working
of the k-edge shortest path technique on this graph is shown in Fig. 5.8.10.
It is quite evident that k-edge shortest distances do not stabilize in this graph
with increase in k, and we get incorrect answers for shortest distances. The
challenge is how to handle this complication? Please understand that we are
limiting our focus on a directed graph which is derived from an undirected
graph as shown in Fig. 5.8.9.
Let us represent a k-edge shortest path of any vertex j from vertex x by
SP athk (j). Each such shortest path starts from vertex x and terminates
194 Basics of Graph Algorithms
0 5 0 5 0 -1
5 5 5
a b a b a b
5 5 5
6 -3 6 -3 6
-3
6 3 6 -3 6 -3
2 2 2
d c d c d c
2 2 2
6 ∞ 6 2 4 2
0 -1 -7
5 5
a b a b
5 5
-3
-3
6 6
6 6
-3 -3
2 2
d c d c
2 2
4 -4 -2 -4
Figure 5.8.10: Shows k-edge shortest paths & distances with respect to vertex
a with k changing from one to five. The shortest distances do not become
stable for any finite value of k.
Graph Traversal Techniques 195
at vertex j; assume that the last vertex in this shortest path is yj before
terminating at j. Now we are in a position to find (k + 1)-edge shortest path
of a vertex i from vertex x in a graph using the following algorithm.
a b a b
d c d c
move in a systematic manner to ensure that we do not miss out any vertex
belonging to the same connected component [3]. You might have noticed that
the Bucket-Algorithm is essentially a graph traversal algorithm. However,
in most of the current textbooks, you would only find some very specific
techniques like the Breadth and Depth First Search traversal algorithms –
more specialized and less flexible than our Bucket Algorithm.
j j j
g i g i g i
h h h
k k k
f a c f a c f a c
d b d b d b
e e e
BFS BFS
DF
S
j DFS j j
g i g i g i
h h h
k k k
f a c f a f a
c c
d b d d
b b
e e e
Figure 5.9.1: Pictures of the Bucket B while performing a BFS (DFS) traver-
sal in a graph starting from a vertex a shown in the top (bottom) diagram.
198 Basics of Graph Algorithms
tered the bucket earlier, others from vertices that are new comers in the
bucket. The way we decide which vertex to choose would convert the Bucket-
Algorithm into a Breadth First Search, Depth First Search, or a combination
of the two. Using a Last in First Out (LIFO or a stack) or a First In First
Out (FIFO or a queue) data structure to store the already discovered ver-
tices would make all the difference: a stack implementation would convert the
Bucket-Algorithm into a Depth First Search while a queue would transform
it into a Breadth First Search.
j j j
g i g i g i
h h
k h
k k
f a c f a c f a c
d b d b d b
e e e
Figure 5.9.2: A Graph G shown in the left diagram; its BFS spanning tree
is shown in the middle while a DFS spanning tree of this graph is shown in
the right diagram. The non spanning tree edges are shown in pink color by
thin lines.
Claim 5.10.1. In the first iteration of the BFS traversal, all vertices at a
distance of one edge from the starting vertex are selected (or goes in the
Bucket); in the k th iteration of the BFS traversal, all vertices at a distance
of exactly k edges from the starting vertex will be discovered. Please see the
Figure 5.10.1 below.
j j j
g i g i g i
h h h
k k k
f a c f a c f a c
d b d d b
b
e e e
a 0
a 0
a 0 f 1 k 1
1
b
f 1 1 k 1
f 1 1 k 1 b
b g 2 c
2 i 2
2
d
g 2 c 2 i 2
2
d e 3
3 3 j
h
Claim 5.10.3. If there is no edge in G other than the ones in the BFS
spanning tree of G then G is a tree provided G is a connected graph.
Claim 5.10.4. If there is an edge in G other than the ones in the BFS
spanning tree of G then this edge will connect two vertices x and y in the
BF S spanning tree such that Dist(x) − Dist(y) is either zero or one whereas
Dist(x) is the distance of vertex x from the starting vertex in the BFS span-
ning tree. Please see Fig. 5.10.2
Claim 5.10.5. If there is an edge in G other than the ones in the BFS
spanning tree of G connecting vertex x and vertex y in the BFS spanning
tree such that Dist(x) − Dist(y) is zero then there will be an odd cycle in
graph G. (Remember Dist(x) is the distance of vertex x from the starting
vertex in the BFS spanning tree). Please see Fig. 5.10.2.
j
a 0
g i
h f 1 1 k 1
k b
f a c g 2 c 2 i 2
2
d
d b 3
h e 3 3 j
e
Figure 5.10.2: BFS spanning tree of a graph G is shown. The edge between
vertex h and vertex j creates an odd cycle in the graph as both these vertices
are at the same distance from vertex a. As we know the distance of vertex h
from vertex a, we also know a path between vertex h and vertex a as shown
in green color in the top right diagram. We also know a path between vertex
j and vertex a; these two paths along with edge j, h makes a cycle. It will
be an odd cycle as vertices h and j are at the same distance from vertex a.
Claim 5.10.6. If there is an edge in G other than the ones in the BFS
spanning tree of G connecting vertex x and vertex y in the BFS spanning
Some Graph Theoretic Claims 201
tree such that Dist(x) − Dist(y) is one then there will be an even cycle in
graph G. (Remember Dist(x) is the distance of vertex x from the starting
vertex in the BFS spanning tree). Please see Fig. 5.10.3 below.
0
j a
g i
h f 1 1 k 1
k b
f a c g 2 c 2 i 2
2
d
d b e 3
3 3 j
e
h
Figure 5.10.3: BFS spanning tree of a graph G. The edge between vertex h
and vertex i creates an even cycle in the graph G as vertex h is at a distance
one larger than the distance of i from a.
Claim 5.10.8. A graph G is bipartite if and only if it does not contain any
odd cycles.
Claim 5.10.9. If a graph G is a tree then there will be a unique path between
every pair of vertices of G. Similarly if there is a unique path between every
pair if vertices in a connected graph G then G is a tree.
f
j a 0
g i a b
h f 1 1 k 1
k b
d k
f a c g 2 c 2 i 2 g
2
d e
d b c
e 3
h
3 3 j
e
h i
j
f
j
a 0
g i a b
h f 1 1 k 1
k b d k
f a c g 2 g
d c 2 i 2 e
2
d b 3 c
h e 3 3 j h
e
i
j
j j j
g i g i g i
h h h
k k k
f a f a f a
c c c
d b d b d b
e e e
No spanning edge & One One spanning edge & Two Two spanning edges & 3
vertex in the Bucket vertices in the Bucket vertices in the Bucket
j j j
g i g i g i
h h h
k k k
f a f a f a
c c c
d b d b d b
e e e
3 spanning edges & four 4 spanning edges & five 10 spanning edges & 11
vertices in the Bucket vertices in the Bucket vertices in the Bucket
Figure 5.10.5: We show the contents of the Bucket at different times while
we make a BFS traversal of a graph. Initially there will be only one vertex
and no spanning edge as shown in the top left corner. Finally there will be
10 spanning edges and 11 vertices in the Bucket.
Claim 5.10.12. If G is acyclic & number of edges in G is one less than the
number of vertices then G is a tree.
On the basis of that precious prior knowledge we can design interesting short-
est path algorithms. The interesting thing about this style of design is that
we shall be using a single building block (sometimes the Bucket Algorithm
and some times a 2-edge Shortest path Algorithm) to design or describe an
algorithm. We intend to do the following in this section:
1. Analyze the existing minimum spanning tree & shortest path finding
algorithms and improve their efficiency as far as possible. This is about
single source shortest path algorithms assuming that all edge weights
are positive in the given weighted graph. We shall further modify the
Bucket algorithm to achieve our objectives.
2. Design of a shortest path algorithm for directed acyclic graphs with neg-
ative as well positive edge weights. We shall again modify the Bucket
algorithm to achieve our objective.
3. Design of single source shortest path algorithms based on our prior
knowledge of finding k-edge shortest paths from a given vertex. Here we
assume that there may be negative edge weights in the given weighted
directed graph. We also assume that the directed graph may have
cycles.
4. Design of all pair shortest path algorithms including the slow all pair,
faster all pair, Floyd-Warshall and Johnsons shortest path algorithms.
Shortest Path Algorithms 205
Figure 5.11.1: We use a new tool ”Opening up the graph” as shown here.
It shows at what stage (edge distance) a vertex enters the Bucket while
executing crude shortest path algorithm. It also shows how and when the
distance of a vertex changes from the start vertex. As soon as a vertex enters
the bucket its color changes from blue to orange and then its distance from
the start vertex cannot change - it is fixed and finalized.
Shortest Path Algorithms 207
Figure 5.11.2: The number of edges coming out of the Bucket B at any time
will be proportional to p2 under worst case conditions.
1. It does not provide correct results for negative edge weights. Although
it provides correct results for positive edge weights.
2. Its time complexity is too high.
We shall reduce the time complexity in the following refined version of short-
est path algorithm. The issue of negative edge weights will be handled in the
next sub-section.
The number of edges coming out of the Bucket B at any time is proportional
to p2 as illustrated in Fig. 5.11.2. This requires as many comparison steps
to move forward in the while loop. As this loop runs as many times as p so
the overall time complexity will be p3 of this algorithm. Somehow we should
reduce the number of relevant edges coming out of the Bucket to as small as
p - this will automatically cut down the time complexity of this single source
shortest path algorithm to p2 . Surprisingly this can be done with a slight
modification in the crude algorithm as shown below. What we essentially do
is to change the graph itself at each step as shown in Fig. 5.11.3. Now we
consider the edges coming out of the Bucket from (only) vertex a instead of
edges coming out of the Bucket from all vertices in the Bucket. The outcome
of this step is that the number of edges (which really matters) coming out of
the bucket B from vertex a is limited by p and not p2 .
It is interesting to note how the shortest distances are provided in the output
in this new algorithm. The shortest paths, however, could not be found
208 Basics of Graph Algorithms
j j j
3 3 3
g 4 i g 4 i g 4 i
2 3 2 3 2 3
h 6 h 6 6
h
3 k 3 k 6 k
9 9 3
9
a 3 4 3 3
3 3 a 3 a 4
f c f c
5 f c
2 2 5
2 2 2
2 2 2
d b d 2
1 3 b d b
3 1 1 3
e 1 e 1
e 1
t
bu
es
ta nc aths
dis est p
st
rte ort
s ho e sh
he of th w(a,k) = min{w(a,k), w(a, j)+w(j,k)}
dt
fin rack
We se t
lo
j j j
3 3 3
g 2 4 i g 4 i g 4 i
2 2
3 3 3
h 6 h 6 h 6
3 6 k 3 k 3 k
7 6 6
7 7
a 3 3 3
a 3 a
f 3 4 f 3 4 f 4
c c c
5 2 5 2 5 2
2 2 2
3 2 2 2
d b d 3 d
1 1 b 1 3 b
5 5 5
e 1 e 1 e 1
Figure 5.11.3: We modify the graph as we move forward in the shortest path
algorithm.
210 Basics of Graph Algorithms
5.11.6 & 5.11.7. You can yourself make suitable modifications in the algo-
rithm according to these modifications.
Food for thought:
Given a parent table and shortest distances as shown in Fig. 5.11.7 determin-
ing the actual shortest paths in a directed graph is an interesting problem.
We have provided a hint in the same figure. See how by increasing memory
or space requirements we can reduce the time complexity of an algorithm.
If we can recover shortest paths then we should also be able to recover a
minimum spanning tree using the refined minimum spanning tree algorithm.
Figure 5.11.5: The original graph is shown in the top diagram. Shortest
distances with respect to vertex a are indicated in the bottom left graph.
Weight of the minimum spanning tree is indicated in the bottom right graph.
Please note that the shortest paths and minimum spanning tree are not easily
available here.
212 Basics of Graph Algorithms
Figure 5.11.6: We need to add a parent table array in order to find shortest
paths in addition to shortest distances.
Figure 5.11.7: How to recover shortest paths with the help of a parent table?
Shortest Path Algorithms 213
Figure 5.11.8: A directed acyclic graph D shown in the top diagram. In the
bottom diagram we arrange its vertices such that all edges move from left
to right. The numbers inside each vertex is the start time and finish times
obtained during a depth first search of the directed graph.
So coming back to the important question: which vertex (and on what basis)
214 Basics of Graph Algorithms
Figure 5.11.9: A concept map depicting which vertex should next enter the
bucket in different algorithms.
should enter the bucket after putting the start vertex in the bucket? The
interesting observation is that after arranging vertices of a DAG (such that
all edges move from left to right) the next vertex to enter the bucket has
already been decided. It is the next left vertex in the new arrangement of
the vertices of the graph. We need no comparisons or extra steps to make
this decision. The corresponding algorithm is described below. Its working
is shown in Fig. 5.11.10. It will be an interesting challenge to derive the time
complexity of this elegant shortest path algorithm.
Shortest Path Algorithms 215
Figure 5.11.10: We need to find shortest distances from vertex a in this graph.
The vertices of the DAG have been arranged such that all edges move from
left to right. Now the next vertex to move in the bucket will always be the
next left vertex. Weight of every edge not indicated in the diagram is equal
to 1.
Shortest Path Algorithms 217
Figure 5.11.11: We need to find longest distances from vertex a. The vertices
of the DAG have been arranged such that all edges move from left to right.
Now the next vertex to move in the bucket will always be the next left vertex.
Weight of every edge not indicated in the diagram is equal to 1.
218 Basics of Graph Algorithms
vertex a. The 2-edge shortest distance w(a, k) from vertex a to vertex k will
then be given by the following recursive equation:
0 ∞ ∞ ∞ ∞ ∞ ∞
0-edge
apart 1 2 3 4 5 6 7
w(a,k)=min{w(a,k),w(a,j)+w(j,k)}
1-edge
apart
1 2 1 3 4 5 6 7 1
1
2-edge 1 2 3 7
apart
4 5 6
1 2 ∞ 1 3 1
1 0 0
1
1
7 2 1
7 2
2 2
6 3 6 3
3 2
4 4
5 4 5 4
1 ∞
Input Graph 2-edge Shortest paths?
Figure 5.11.12: Execution of the 2-edge shortest path algorithm from start
vertex a. Edge weights not shown are equal to 1.
Figure 5.11.13: We execute the k-loop first and then the j-loop in the 2-edge
shortest path algorithm. Here intermediate vertex j is fixed at 4 and the
destination vertex k is varied first.
Please note that it is immaterial whether you execute the j-loop first and the
k-loop later or vice versa. The end result remains the same. This switching of
two loops is an exciting idea in the development of shortest path algorithms
- it also becomes a powerful tool for the algorithm designer (Fig. 5.11.13).
222 Basics of Graph Algorithms
Dist(k)
Dist(j)
j k
w(j,k)
a a
Dist(k) = 6 Dist(k) = 6
Dist(j) = 4
Dist(j) = 4
w(j,k) = 3 w(j,k) = 1
j k j k
New Dist(k) = 6 New Dist(k) = 5
Figure 5.11.17: How and when the (shortest) distance Dist(k), with respect
to vertex a changes when we consider the directed edge (j, k).
Line 3 is the basic building block of this algorithm. Its working is explained
in the figure below. In this version of Bellman-Ford the time complexity
will always be pq with an adjacency list data structure and without an extra
intelligence in its implementation.
Shortest Path Algorithms 227
Algorithm 32: Slow All Pair: Find shortest distance of every vertex
from every vertex in G
input : Adjacency matrix of weighted Graph G
output: Shortest distance of every vertex from every vertex in G
1 for each (start) vertex a of graph G do
2 for i=1 to p-1 do
3 for each (intermediate) vertex j of graph G do
4 for each (terminating) vertex k of graph G do
5 Find a two edge path from vertex a to vertex k in G
passing through an intermediate vertex j, and assign
min{w(a, k), w(a, j) + w(j, k)} to w(a, k)
Figure 5.11.18: The start vertex loop is represented by green color. The
intermediate vertex j loop is represented by orange color and the terminating
vertex k loop is represented by brown color. There is another p-loop - it is
represented by blue color. The diagram shows the effect of switching between
the blue and the green loops. The left diagram depicts the slow all pair
shortest path algorithm while the right diagram represents the faster all pair
shortest path algorithm.
Algorithm 33: Faster All Pair: Find shortest distance of every vertex
from every vertex in G
input : Adjacency matrix of weighted Graph G
output: Shortest distance of every vertex from every vertex in G
1 for i=1 to p-1 do
2 for each (start) vertex a of graph G do
3 for each (intermediate) vertex j of graph G do
4 for each (terminating) vertex k of graph G do
5 Find a two edge path from vertex a to vertex k in G
passing through an intermediate vertex j, and assign
min{w(a, k), w(a, j) + w(j, k)} to w(a, k)
Shortest Path Algorithms 229
Figure 5.11.19: Working of the faster all pair shortest path algorithm is
shown. Please note that from two edge all pair shortest distances we jump
to 4-edge and then to 8-edge shortest distances.
230 Basics of Graph Algorithms
Figure 5.11.20: The recursion tree and the colored puzzle corresponding to
faster all pair shortest path algorithm. Here it is also obvious that from
2-edge shortest distances we directly jump to 4-edge shortest distances.
Shortest Path Algorithms 231
1. Is the time complexity of this algorithm any better than that of slow
all pair shortest path algorithm? If yes then why?
2. You may have noticed that in this algorithm (FAster All Pair) the
number of edges in shortest paths jumps in the powers of 2 as shown in
Figure 5.11.20. What does that mean? The outer most loop (the blue
colored loop in the colored puzzle) should run from 1 to p or from 1 to
log2 p? How will it affect the time complexity of this so called faster all
pair shortest path algorithm.
3. In the slow all pair shortest path algorithm the blue loop should run
from 1 to p and not from 1 to log2 p? Why?
Figure 5.11.21: The colored puzzles corresponding to faster all pair shortest
path algorithm.
for vertex
for each i =1 toj p-1 for each vertex j
for each vertex k for each vertex a
Figure 5.11.24: We show a concept map of various single source and all pair
shortest path algorithms.
236 Basics of Graph Algorithms
Figure 5.11.25: We show the galaxy of single pair and all pair shortest path
algorithms. Each algorithm can be represented by a different color arrange-
ment in the rectangular puzzle. It is interesting to note that each shortest
path algorithm has at least one dual with the same performance and output.
the improved Dijkstra’s algorithm to find all pair shortest paths in time
O(p2 logp + pq). Consider the Bellman-Ford algorithm described earlier and
reproduced here. It was already discussed that its worst case time complexity
will be O(pq). All distances are measured with respect to vertex a. We
initialize Dist(a) = 0 and set Dist(x) of every other vertex x from vertex a
equal to infinity.
Figure 5.11.27: If we find i-edge distances in this graph from vertex a then
we observe that some distances will change when i goes from 4 to 5. This
does not confirm that there is a negative weight cycle. But when a distance
of a vertex from vertex a changes when i goes from 5 to 6 then that is a
confirmation that there is indeed a negative weight cycle in this graph.
Figure 5.11.28: After finding 3-edge shortest paths in this graph from vertex
a there will be no change in distance calculations. Does that mean that we
should stop here and declare that there are no negative weight cycles in this
graph?
1. Whether there are any negative weight cycles in the graph. If there are
any negative weight cycle then the algorithm should not move forward
and should terminate.
2. If there are no negative weight cycles then what are the shortest dis-
tances of each vertex from the newly added vertex x. This information
will further be used to convert negative edge weights into positive edge
weights.
If the graph does not contain any negative weight cycles but contains negative
weight edges then we should somehow try to make the negative weight edges
positive and then apply Dijsksta’s algorithm from each vertex as already
planned. How about adding a big positive number in each edge weight such
that every (edge) weight becomes positive? See what complication would
arise if we do so as shown in Fig. 5.11.31. Thus we cannot add an arbitrary
positive number in each edge weight as it will disturb the relative path lengths
in the new graph as shown in the following figure.
Converting negative weight edges into positive weight edges
Please note that we need to simultaneously fulfill the following two objectives:
Shortest Path Algorithms 241
First we shall devise a simple scheme to alter edge weights such that relative
distances between any two vertices do not change in the given graph. Then
we shall modify this scheme so as to convert all negative edge weights into
positive edge weights thus fulfilling both the above objectives.
Consider a directed edge (j, k) with a weight equal to w. How about associ-
ating any arbitrary number Label(j) with a vertex j and another arbitrary
number Label(k) with vertex k. Now the edge weight w(j, k) is changed ac-
cording to a formula where wnew = wold +Label(j)−Label(k). If we associate
an arbitrary number with each vertex of the graph and change edge weights
according to the formula described then we claim that relative path lengths
between any two vertices will be the same in the new graph as compared to
the old graph. See Fig. 5.11.32. In fact the new path length from a vertex a
to a vertex b will be the old path length plus Label(a) − Label(b). Putting
arbitrary labels solves only one problem, namely the relative distances be-
tween two vertices remain the same in the new graph as compared to the old
graph. The other problem (converting all negative edge weights into posi-
tive) remains to be solved. It requires that vertices should not be labeled
arbitrariliy but with some intelligence as described below.
So now the problem is reduced to finding an appropriate number Label(j) to
be associated with a vertex j and another number Label(k) to be associated
with vertex k (assuming that there is an edge from j to k with a weight
w(j, k)) such that if w(j, k)old is negative then w(j, k)new = w(j, k)old +
Label(j) − Label(k) becomes positive.This would require that wjk (old) +
Label(j)−Label(k) is greater than or equal to zero. It means that wjk (old)+
Label(j) is not less than Label)k). In other words Label(k) should be at least
equal or more negative than wjk (old)+Label(j) assuming that wjk (old), Label(j),
and Label(k) are all negative.
Before finding a systematic scheme of providing labels to each vertex let us
try to work out a simple example. What numbers are desirable and should
be associated with two adjacent vertices j and k are indicated in Fig. 5.11.33.
Here vertex x is a vertex added to the given directed graph just like the one
shown in Fig. 5.11.30. In the top diagram we try to initially label k such that
Shortest Path Algorithms 243
b
11 -6
11-6=5
a c
-2 -2+3-4+7=4
7 -3
d 3 e -4 f b
11 -6
0 11-6=5 -4
a c
-3 -2 -2+3-4+7=4
7
b
-6-3+4
d 3 e -4 f
11+0+3
0 -4 -1 -2 -5
a 11-6+0+4=9 c
-2+0+1 -2+3-4+7+0+4=8
7-5+4
d e -4-2+5
f
3-1+2
-1 -2 -5
Summary
So now we are in a position to describe Johnson’s algorithm in the following
meaningful manner as applied on a directed graph D.
1. Check if there are any negative weight cycles in the graph D using
Bellman-Ford algorithm applied to the graph after inserting a source
vertex x as shown in Fig. 5.11.35. If there are no negative weight cycles
in graph D then move to step 2 otherwise terminate.
Dist(k)
Dist(j)
j k
w(j,k)
x x
Transform
0
b b 0
8 -4 8-2 -4+4
-2 -4 -2 -4
a 1 c a 1+0 c
-2 2 -2+2
2+4
d 3 e d
3+0
e
0 0
0 0
Figure 5.11.35: How to determine which labels to associate with each vertex.
Discussion 247
5.12 Discussion
The most important task of a teacher should enable the students to discover
and acquire experience of independent work. According to Polya [10]: If the
student is left alone with his problem without any help or with insufficient
help, he may make no progress at all. If the teacher helps too much, nothing
is left to the students. The teacher should help, but not too much and not
too little, so that the student shall have a reasonable share of the work. In
this chapter we have demonstrated how a teacher can help students discover
a number of graph algorithms with some initial help, starting with something
seemingly simplistic yet capable of being transformed into a number of pow-
erful algorithms with minor modifications. We have shown that by asking
thought provoking questions it becomes possible for the teacher to guide the
students while solving complex problems. We have also shown that making
comparisons between various techniques and solutions provides a deep in-
sight which itself is very useful in solving otherwise difficult problems [8]. At
times it is almost impossible to solve a given problem while it is easy to solve
a related problem (the shortest path problem is solvable while the longest
path problem is unsolvable). Similarly it is difficult to solve a problem in its
original form while it is easier to solve it while placing certain restrictions
(the graph isomorphism problem is solvable for trees but is difficult to solve
in general). It is extremely useful to find why a certain technique works under
certain conditions and why it fails in others (greedy methods provide optimal
solutions in finding the shortest path but fail to find the longest path). The
theory of NP-Completeness connects all problems that are NP-Complete: it
is also possible to find a useful relationship among solvable problems and this
is what we have attempted to do in this chapter. According to Hale [5]:
“There are different kinds of learning, but I refer here to the intellectual
kind. To learn means to cause your mind to function in a different way: new
memories are created and/or new connections are forged.”
248 Basics of Graph Algorithms
Acknowledgement
We are thankful to the Department of Computer Science, Lahore University
of Management Sciences for providing support for this research. We wish
to specially thank R. Khan for providing motivation as well as inspiration
for this project. We also wish to thank S. Skiena, S. Baase, S. Mahkari, H.
Mian, A. Alvi, K. Fahd, J. Ikram, M. Maud, T. Jadoon for their help and
encouragement.
Chapter 6
Network Flows, Connectivity and
Matching Problems
6.1 Introduction
6.2 Definitions & Prior Knowledge
6.3 Konig’s Theorem, Menger’s Theorem & Hall’s (Marriage) Theorem
6.4 Menger’s Theorem
6.5 Konig’s Theorem, Menger’s Theorem & Hall’s (Marriage) The-
orem Revisited
6.6 Network Flows
6.7 The Matching Problem in Bipartite Graphs
6.8 The Max-Flow Min-Cost Problem
6.9 Network flows with lower & upper bounds on flow and the
Circulation Problem
250 Network Flows, Connectivity and Matching Problems
6.1 Introduction
We shall first address the problem of vertex or edge connectivity in general
graphs. We shall also discuss the matching problem in bipartite graphs.
Specifically we shall be describing Menger’s Theorem which relates maximum
number of vertex-disjoint (or edge-disjoint) paths with minimum number of
vertices (or edges), which if removed will disconnect a special node from
another special node in a graph. We shall also discuss Konig’s Theorem
which relates the size of the vertex cover to the size of maximum matching
in a bipartite graph. The Marriage (Hall’s) theorem provides necessary and
sufficient conditions for a bipartite graph to have a perfect matching. Our
intention (and desire) in this chapter, will be to integrate concepts so that the
enabled learner is able to appreciate the bigger picture where one relationship
implies another and one theorem can be used to prove the other. That is why
we first provide a unified picture and then go deeper in order to analyze each
area in detail. In addition to making formal proofs for a number of theorems,
we shall be designing algorithms to solve a number of related problems. We
shall also be discussing the network flow problem, the maximum flow at
minimum cost problem, and last but not the least the Circulation problem.
We use a single building block in this entire chapter for designing almost
every algorithm.
connection between the vertex cover and the independent set problem?
Figure 6.2.1: A perfect binary tree is shown. The root vertex is shown at
the top of the diagram. Each leaf vertex is at a distance of 3 from the root
vertex. Any independent set will include leaf vertex u, while any vertex cover
will include non leaf vertex v. Every edge connecting a leaf vertex u and a
non leaf vertex v will always be part of the edge cover. Note that every non
leaf vertex is a cut vertex and every edge is a cut edge or a bridge in a tree
graph.
in a graph?
A Perfect Matching is a matching which covers all vertices of the graph.
That is, every vertex of the graph is incident to exactly one edge of the
matching. Every perfect matching is both maximum and hence maximal. In
some literature, the term complete matching is used for perfect matching.
A Perfect Binary Tree: We know that a tree is a connected graph where
each edge is a bridge edge. A binary tree is a tree where the degree of each
vertex is less than or equal to 3. Here we define a perfect binary tree. A
perfect binary tree (shown in Fig. 6.2.1) has the following features:
1. It has a single vertex with degree equal to 2. Let us call it the root
vertex. The root vertex is shown as the top most vertex in Fig. 6.2.1.
2. It has exactly 2h+1 − 1 vertices where h is the path length (in terms
of number of edges) between the root vertex and any leaf vertex in the
perfect binary tree. The perfect binary tree shown in Fig. 6.2.1 has h
equal to 3 and it has 15 vertices.
4. It has exactly 2h − 1 non leaf vertices including the root vertex. All
non leaf vertices other than the root vertex has a degree exactly equal
to 3. There are 7 non leaf vertices in tree shown in Fig. 6.2.1.
Problem Set 6.1. We are given a perfect binary tree G (see Fig. 6.2.1)
and we intend to solve a number of problems related to connectivity and
matching. We know that in a tree every edge is a cut edge or bridge while
every (non leaf) vertex is a cut vertex. We also know that there is always a
unique path between every two vertices in a tree. We shall observe later in
this chapter that there is indeed a relationship between graph connectivity
and number of paths in a graph (Menger’s Theorem). In a perfect binary
tree graph G, a leaf vertex u will always part of an independent set (why?).
While a non leaf vertex v which is adjacent to a leaf vertex will always be
part of a vertex cover (why?). Using this logic all leaf vertices will be part
of the independent set and will not be part of the vertex cover. Independent
set and vertex cover vertices are shown in Fig. 6.2.2 for the perfect binary
Definitions & Prior Knowledge 253
u u
Vertex Independent
Cover Set
u u v u u u v u
u u
Maximum
Matching Edge Cover
u u v u u u v u
Figure 6.2.2: The top left diagram shows the vertices belonging to the vertex
cover highlighted by double circled vertices. The rest of the vertices belong
to the independent set in this diagram as shown in the top right corner.
The bottom left diagram highlights, in brown bold, the maximum matching
edges in the given tree. The bottom right diagram shows the edges belonging
to the edge cover; these edges are shown in bold in green. You may have
noticed that the size of the maximum matching is equal to the size of the
vertex cover in this graph. The maximum matching in this graph is not a
perfect matching, as all vertices here are not matched as shown by black
circled vertices. You may have also noticed some relationship between the
size of the vertex cover and the size of the independent set?
254 Network Flows, Connectivity and Matching Problems
tree graph of Fig. 6.2.1. In a perfect binary tree graph an edge connecting
a leaf vertex u with a non leaf vertex v (see Fig. 6.2.1) will always be part
of the edge cover. Edges belonging to the edge cover are shown in bold in
the bottom right diagram of Fig. 6.2.2. We shall observe later that in a
bipartite graph (such as a tree) the size of a maximum matching is equal to
the size of vertex cover (Konig’s Theorem). It may thus be possible to find
a maximum matching given a vertex cover in a tree graph. The bottom left
diagram of Fig. 6.2.2 shows a maximum matching in the perfect binary tree
graph of Fig. 6.2.1. It also indicates matched and unmatched vertices and
edges. Based upon the above observations it is possible to design efficient
algorithms to solve the following problems in a tree graph. Please note that
a perfect binary tree graph is a fairly restricted structure and our existing
prior knowledge of graph theory and algorithms is sufficient to solve these
problems.
Problem 6.1.1. How can we efficiently find an edge cover in a perfect binary
tree?
Problem 6.1.2. How can we efficiently find a vertex cover in a perfect binary
tree?
Problem 6.1.6. For a perfect matching to exist in a tree graph, what con-
ditions are necessary and which are sufficient?
Figure 6.2.3: There are several paths between vertices s and t in this graph.
By removing certain edges (or vertices) it is possible to remove all these paths
between the two vertices; the resulting graph will be a disconnected graph.
Problem 6.1.8. What are the necessary and sufficient conditions for a near
perfect matching to exist in a tree graph?
Problem Set 6.2. We are given a graph G with two special vertices s and
t as shown in Fig. 6.2.3. We intend to explore different paths between these
two vertices and see how we can destroy these paths in G. There is only one
bridge edge in this graph, if we remove this edge {s, a}, then the graph G
is disconnected but vertex s is still accessible to vertex t. There is no cut
vertex in this graph. We have to remove a lot more than one vertex in order
to disconnect G so as to destroy all paths between vertex s and t.
Problem 6.2.1. Draw all 4-edge paths between vertex s and t. See Fig. 6.2.4.
Problem 6.2.2. Draw all 5- and 6- edge paths between the two vertices.
See Fig. 6.2.4.
Problem 6.2.3. Out of all paths that you have listed in Problem 6.2.1 and
Problem 6.2.2 shortlist the ones that are edge-disjoint. What is the maximum
number of such paths?
Problem 6.2.4. Out of all paths that you have listed in Problem 6.2.1
and Problem 6.2.2 short list the ones that are vertex-disjoint. What is the
maximum number of such paths?
Problem 6.2.5. Draw a 4-edge path between the two vertices such that
not more than one additional edge-disjoint path is possible between the two
vertices.
Problem 6.2.6. Draw more than one edge-disjoint paths between the two
vertices such that at least one path should be of length six.
258 Network Flows, Connectivity and Matching Problems
b e h b e h
s t s t
c f i c f i
s
d j d j
Ca
n b th
e d is?
b c d
ra
wn
l
ike
e f f
g h i i j i j
t t t t t t t
Figure 6.2.4: 4-edge and 6-edge paths from vertex s to vertex t in graph G.
b c d
b e h
s t e f f
c f i
g i h i j i j
d j
t t t t t t t
Figure 6.2.5: Some edge disjoint paths; these paths are not vertex disjoint?
Definitions & Prior Knowledge 259
Problem 6.2.7. Draw a 6-edge path between the two vertices such that no
additional edge-disjoint (or vertex-disjoint) path is possible between the two
vertices.
Problem 6.2.8. The left diagram of Fig. 6.2.7 shows a 6-edge path (shown
in bold) between vertices s and t in the same graph. Note that if we remove
all edges in this 6-edge path, then all the paths between vertex s and t are
destroyed. We know that there existed more than one path between the two
vertices, but only because we have selected one wrong path initially, that
other possible paths have disappeared. Why? What are its ramifications in
designing an algorithm to find all possible edge-disjoint paths in a graph?
Problem 6.2.9. In fact it is not necessary to remove all the edges in the
single 6-edge path as shown in Fig. 6.2.7 in order to destroy connectivity
between s and t. A minimal subset of these edges shown by a cut is sufficient
to do the job. List down these edges. Do these three edges corresponds to a
minimum number of edges which if removed will destroy all paths between
vertices s and t. Can you find another subset of three edges which when
removed will disconnect s and t? What is λ(s, t) for this graph?
Problem 6.2.10. The right diagram of Fig. 6.2.7 shows two edge disjoint
paths of length 4 (shown in bold) between the vertices s and t. Once these
paths are selected, it will no longer be possible to find an extra edge-disjoint
path between the two vertices. You can remove all edges belonging to the
260 Network Flows, Connectivity and Matching Problems
a g a g
A Cut A Cut
b e h b e h
s t s t
c f i c f i
d j d j
Figure 6.2.7: The left diagram shows a 6-edge path (shown in bold) between
vertices s and t in the graph. The right diagram shows two 4 edge disjoint
paths (shown in bold) between vertices s and t. The blue (green) cut cuts
those edges which, if removed will disconnect s from t.
paths and see for yourself that the connectivity between s and t is destroyed.
What is a minimal subset of these edges which will do the same job of de-
stroying the connectivity between s and t? One such subset is shown in the
right diagram of Fig. 6.2.7. This subset consists of four edges unlike three in
the last part?
Problem 6.2.11. The left diagram of Fig. 6.2.8 shows a minimum sized sub-
set of edges, which if removed will destroy the connectivity between vertices
s and t. The right diagram shows a minimum sized subset of vertices which
if removed will disconnect s and t from each other. This subset consists of
two vertices, e and f ; the diagram also shows, in bold, two vertex-disjoint
paths between s and t. Is this a coincidence that the (maximum) number of
vertex-disjoint paths is exactly equal to the (minimum) number of vertices
which if removed will disconnect s and t? The left diagram shows an edge
cut of size three. Does this mean that there will be three edge-disjoint paths
in this graph? Find these three paths. Be careful as this is the same cut
shown in the left diagram of Fig. 6.2.7 where there existed a single path of
six edges (with no room for additional paths). Now we need to find three
edge-disjoint paths between the same vertices.
Konig’s Theorem, Menger’s Theorem & Hall’s (Marriage) Theorem 261
a An Edge g a g
Cut
b e h b e h
s t s t
c f i c f i
d j d A Vertex j
Cut
Figure 6.2.8: The left diagram shows a minimum sized subset of edges (shown
in bold) which, if removed, will destroy the connectivity between vertices s
and t. The right diagram shows a minimum sized subset of vertices (shown
by double circles) which if removed will disconnect s and t from each other.
in the left diagram) becomes the minimum sized subset of vertices which, if
removed will destroy all paths between vertex s and t in the right diagram
(why?). Similarly each matched edge (in the left diagram) will correspond
to a vertex-disjoint path between vertex s and t in the right diagram (why?).
A relationship between (minimum) vertex cover and maximum matching in
a bipartite graph (Konig’s Theorem) is transformed into a relationship be-
tween κ(s, t) and (maximum) number of vertex-disjoint paths between vertex
s and t in the right diagram of Fig. 6.3.1 (Menger’s Theorem).
Note that all vertices in the set A are not matched to all vertices in the
set B although the size of A is equal to size of B in the given bipartite
graph. In other words a perfect matching does not exist in this bipartite
graph. Had a perfect matching existed then the size of the vertex cover
would have been equal to the size of A (or B). What should be the size
of the neighborhood N (S) for every subset S of A which will guarantee a
perfect matching (or a vertex cover equal to the size of A) is the subject of
Marriage (Hall’s) Theorem. It states that a bipartite graph (of equal halves)
has a perfect matching if and only if |N (S)| ≥ |S| for every S ⊆ A. For a
better understanding of the Marriage Theorem a few neighborhood subsets
are indicated below for the bipartite graph of Fig. 6.3.1.
N ({a1 }, {a2 }) = {b1 , b2 }
N ({a1 }, {a2 }, {a3 }) = {b1 , b2 }
N ({a2 }, {a3 }) = {b2 }
N ({a2 }, {a3 }, {a4 }) = {b2 , b3 , b4 }
N ({a1 }, {a2 }, {a3 }, {a4 }) = {b1 , b2 , b3 , b4 }
Out of the five neighborhood subsets N (S), only three satisfy the condition
that |N (S)| ≥ |S|, while in the remaining two the said condition is violated.
It is interesting to note that if we remove vertex a3 from A and vertex b3
from B then the condition |N (S)| ≥ |S| is true for every S ⊆ A. Under
such conditions the size of the maximum matching and that of the vertex
cover will be equal to the size of remaining set A which is equal to three.
You may have realized yourself that this necessary and sufficient condition
(applicable to the bipartite graph shown on the left of Fig. 6.3.1) implies
that the (maximum) number of vertex-disjoint paths (and κ(s, t)) in the
graph shown on the right side of Fig. 6.3.1 would be equal to the size of
partition A (and B).
Konig’s Theorem, Menger’s Theorem & Hall’s (Marriage) Theorem 263
a1 b1 a1 b1
a2 b2 a2 b2
A B s A B t
a3 b3 a3 b3
a4 b4 a4 b4
a1 b1 a1 b1
a2 b2 a2 b2
A B s A B t
a3 b3 a3 b3
a4 b4 a4 b4
Figure 6.3.1: The top left diagram shows a bipartite graph with maximum
matching edges shown in bold, and vertices belonging to the vertex cover
highlighted by orange circles. The top right diagram shows the same bipartite
graph with two dummy vertices s and t.
264 Network Flows, Connectivity and Matching Problems
3. Given an undirected graph how can we find its connectivity: the mini-
mum number of edges (belonging to Edge Cutset) or minimum number
of vertices (belonging to Vertex Cutset) which if removed will discon-
nect the graph into two or more connected components?
The graph theoretic aspects of the above problems and a proof for Menger’s
Theorem will be presented after a better appreciation of the issues involved.
removed from D will disconnect t from s. How about devising the following
common sense algorithm to find the maximum number of edge-disjoint paths
from vertex s to t in a directed graph D?
Let us apply this simple algorithm to solve the problem in the graph of
Fig. 6.4.1. Using any path finding algorithm, we can find a directed path
P1 , from vertex s to t in D. Let us look at what are different possibilities
for P1 . It may be (s → a → c → t), (s → b → d → t), (s → a → d → t),
or (s → d → t). It cannot be (s → d → a → c → t) or (s → b → d →
a → c → t) because on directed edge (a, d) we can move from ‘a’ to ‘d’ but
not from ‘d’ to ‘a’. Once we have selected an initial path P1 , we can move
forward in Algorithm 36. But before finding another edge-disjoint path we
should first remove the edges of P1 (why?) and then find another path P2 . If
we are lucky, we shall be able to find the two edge-disjoint paths; one being
(s → a → c → t), and the other (s → b → d → t or s → d → t). But if
we are unlucky, we shall land in a difficult situation as depicted in Fig. 6.4.1.
If path P1 = (s → a → d → t) (as shown by bold lines in Fig. 6.4.1) then
we are blocked; it has now become impossible to find another path in this
graph. Thus an initial wrong choice will make things hard for us. We are in
dire need of some innovation?
There are potentially two ways to fix this problem:
1. Pick the initial path more intelligently - but this may not be an efficient
solution, why?
2. Do not delete the edges of the chosen path - and do something else
with these edges. What is that something else?
a c a c
s t s t
b d b d
Figure 6.4.1: If we initially select a path as shown by bold red lines (left
diagram) and remove it from the graph (right diagram) then it becomes
impossible to find another path in this graph.
a c a c
s t s t
b d b d
1. A directed edge (x, y), not already in use by a path, can be traversed
(or used) by a path from x to y and not from y to x. The status of such
an edge when traversed by a single path from x to y will be “used”.
2. A directed edge (x, y), already traversed by a path from x to y, can be
traversed by a new path from y to x and not from x to y. The status
of such an edge, which is traversed by two paths in opposite directions
will be “not used by any path” or “unused”.
Example 6.4.1. Let us execute this simple four line algorithm on the graph
of Fig. 6.4.3. We apply the above algorithms on the graph D shown in the
same figure. We first find maximum pseudo edge-disjoint paths from vertex
s to vertex t in this graph. In this process (Algorithm 37) we convert graph
270 Network Flows, Connectivity and Matching Problems
D into F . Graph F helps us identify the set P using Algorithm 38. Using
this set P we identify edges of D which belong to the minimum cut.
The paths P1 (green) and P2 (blue) that we have found during the execu-
tion of this algorithm are not edge-disjoint paths (see Fig. 6.4.4), yet this
algorithm tells us the maximum number of possible edge-disjoint paths in D
(why and how?). The most useful result of this algorithm is the Status of
every edge e in D after its termination. The diagram on the bottom right of
Fig. 6.4.4 shows the original graph D with used edges only. After deleting
unused edges (from D) we end up with this new graph; this new graph has
some very desirable properties which we shall elaborate later. The Minimum
Cut is found in Fig. 6.4.5. Before jumping to any conclusions and in order
to gain more confidence let us solve another example.
Let us again consider graph D (of Example 6.4.1) minus the unused edges
as shown in the bottom of Fig. 6.4.4. Let us also consider the graph D
(of Example 6.4.2) minus the unused edges shown in Fig. 6.4.6. Both these
graphs share something in common; they both belong to a (desirable) class?
You can apply (the very naive) Algorithm 36 to this class of graphs and you
will find maximum sized subset of edge-disjoint paths without any difficulty.
a c a c
Used
Used
s t s t
Used
b d b d
Graph D Initial Graph F
a c Used
a c
Used
Used
U
n
s t s t
U
se
Used
d
Used
b d b d
a c a c
U
n
s t t
U
s
se
d
Un Used
b d b d
Un Used
Final Graph F Graph D without Unused Edges
Figure 6.4.4: Graph D is the input to Algorithm 37. This Algorithm outputs
maximum pseudo edge-disjoint paths and the Graph F .
Menger’s Theorem 273
a c a c
s t s t
b d b d
Graph D Graph F
a c a c
s t s t
b d b d
Graph F Graph D
Figure 6.4.5: The top left diagram shows the Original Graph D, the top right
diagram shows the final graph F with a couple of paths found and reversed.
The bottom left diagram shows the final graph F where the Min Cut is found
and then it is applied to the original graph D on the bottom right diagram.
274 Network Flows, Connectivity and Matching Problems
a g a g
b e h b e h
s t s t
c f i c f i
d j d j
a g a g
b e h b e h
s t s t
c f i c f i
d j d j
Figure 6.4.6: The top left diagram shows a directed graph D with a selected
path P1 from vertex s to vertex t shown in bold. The top right diagram
shows the edges of another path P2 shown in bold. The bottom left diagram
shows the edges of the path P3 shown in bold. The original graph D is shown
in the bottom right diagram with used edges shown in bold.
Menger’s Theorem 275
vertex t). Out of many possible cuts we show only three cuts in the directed
graph shown in the top diagram of Fig. 6.4.7. Please note that it is the same
directed graph for which we have found maximum number of edge-disjoint
paths as shown in Fig. 6.4.1.
For Cut A : SA = {s, a, b, d}; TA = {c, t}; (SA , TA ) = {(a, c), (d, t)}
For Cut B : SB = {s, a, b}; TB = {c, d, t}; (SB , TB ) = {(s, d), (a, c), (a, d), (b, d)}
For Cut C : SC = {s, b, d}; TC = {a, c, t}; (SC , TC ) = {(s, a), (d, t)}
It is interesting to note that the edge (a, d) is physically cut by Cut B as
well as Cut C but it is only included in the Cut B and not in Cut C. This
is because in Cut B vertex ‘a’ belongs to SB while vertex d belongs to TB as
shown in the bottom left diagram of Fig. 6.4.7. While in Cut C, vertex ‘a’
belongs to TC and vertex d belongs to SC . Thus the edge (a, d) in the Cut
C does not belong to the set (SC , TC ) as shown in the bottom right diagram
of Fig. 6.4.7. Looking at this problem from a different angle: In Cut B you
have to remove all four edges present in this cut in order to destroy all paths
from vertex s to t in D. While in Cut C you have to remove only two edges
present in this cut and vertex s is disconnected from t; edge (a, d) is not
contributing to a path from s to t in D and is therefore not required to be
removed. The size of Cut B is larger then the size of Cut A and that of
Cut C. By a little inspection in this graph it can easily be verified that Cut
A is indeed one of the minimum cuts, (SA , TA ) is equal to U (which is the
minimum sized subset of edges which, if removed will destroy all paths from
s to t in D). Thus λ(s, t) = |(SA , TA )| = 2.
According to Menger’s Theorem the maximum number of edge-disjoint paths
in this graph will equal λ(s, t), which is 2. How can we find the minimum
cut systematically? We shall discuss it in a proof of Menger’s Theorem.
We are given a directed graph D and two special nodes s and t. We intend to
prove Menger’s Theorem in terms of edge connectivity of a directed graph.
The theorem states that the maximum number of edge-disjoint paths from
vertex s to t is equal to the minimum number of edges of D which if removed
will destroy all paths from s to t in D. We shall provide a constructive proof.
276 Network Flows, Connectivity and Matching Problems
Cut C c
a a c
SA Cut A
s t s t
Cut A b d
b d
Cut B
a c a c
Cut C
s SB
Cut B t s SC t
b d b d
Figure 6.4.7: We are given a directed graph D and we intend to find maximum
number of edge-disjoint (directed) paths from vertex s to vertex t. The top
diagram also shows three cuts. The bottom left diagram shows the subset
SB along with the Cut B edges shown in bold. The bottom right diagram
shows the subset SC along with the Cut C edges shown in bold.
Menger’s Theorem 277
Using this proof one can also find the maximum number of edge-disjoint paths
as well as the minimum cut. This proof technique can easily be adapted for
undirected graphs and can also be used to prove the vertex form of Menger’s
Theorem which states that, the minimum number of vertex-disjoint paths
from s to t is equal to the cardinality of minimum sized vertex cut-set which
will disconnect t from s in D.
We know that number of edge-disjoint paths from vertex s to t in a graph
cannot exceed the size of a cut (SX , TX ) where vertex s belongs to the subset
SX while vertex t belongs to TX as defined earlier in this section. As we
have seen before there may be different cuts possible with different sizes in
the same graph as shown in Fig. 6.4.7. If we try to maximize the number of
paths from s to t they will only be limited by the bottleneck in the graph
which will be the minimum cut. That means the maximum number of edge-
disjoint paths from s to t in D will not exceed |(S, T )|min where the minimum
is taken over all cuts (S, T ) in D. Menger’s Theorem tells us that the two
quantities (maximum number of paths and size of the minimum cut) are
exactly equal.
We shall make the following claims here:
Claim 6.4.2. The maximum number of pseudo edge-disjoint paths from ver-
tex s to t is equal to the minimum number of edges of D which if removed
will destroy all paths from s to t in D, i.e., the minimum cut.
Claim 6.4.3. The number of real (not pseudo) edge-disjoint paths from s to
t in D can not exceed the number of (pseudo) edge-disjoint paths from s to
t in D. The maximum number of the two paths will be exactly equal (given
one, we should be able to find the other).
opposite direction) as shown in the top right corner of Fig. 6.4.6. We keep
on doing this until we are no longer able to find a path from s to t in D and
then the algorithm terminates. It is obvious that if there is a (pseudo) path
left (in addition to the one’s that are already found) then Algorithm 37 will
find it before its exits.
a c a 2
c
2
1
2 Pseudo
Paths
s P t s t
1
2 1
d Min Cut
b b d
a c
Edge-Disjoint
Paths
s unused t
unused
b unused
d
Figure 6.4.8: The bottom left diagram of Fig. 6.4.5 is duplicated in the top
left corner; it shows the number of vertices (enclosed in the shaded area)
which are reachable from s in D after the termination of Algorithm 37. The
maximum number of pseudo edge-disjoint paths is shown in bold in the top
right diagram. The bottom diagram shows the modified graph D0 with used
edges shown in bold.
We have seen that the maximum number of pseudo edge-disjoint paths cannot
exceed the size of a minimum cut; what we need to show now is that there
will actually be a cut whose size will be equal to the maximum number of
paths from s to t in D. Let us assume that while executing this algorithm we
have reached a stage where we are no longer able to find a new path and the
Algorithm 37 terminates. Although no (additional) path from vertex s to t
exists anymore, but it still may be possible to reach a number of vertices in
the modified graph D from the vertex s. Let P be a nonempty set (a subset
of V (D)) containing vertex s and all other vertices which are reachable from
s. We define subset Q to be equal to V (D) − P . It is important to note that
the vertex t will belong to Q otherwise it will be in P and then we can find an
extra path from vertex s to vertex t in D. The Cut (P, Q) will consist of edges
such that each edge which is part of this cut will be occupied by a unique
Menger’s Theorem 279
path from vertex s to t in D. The number of paths cannot be larger than the
size of this cut as discussed before. Can the number of paths be smaller than
the size of this cut? If the number of paths is smaller than the size of the cut
then you can identify at least one edge (x, y) which is part of the cut but not
part of any path from s to t in D. Under such conditions vertex y will also
be reachable from vertex s which contradicts our initial assumption. Thus
each edge of the Cut (P, Q) will be part of exactly one pseudo path from
vertex s to t in D. This situation is depicted in Fig. 6.4.5. When Algorithm
37 is unable to find an extra path it terminates as shown in the bottom left
corner of Fig. 6.4.5. The set of vertices which are reachable from s (known
as P ) are shown shaded in this diagram. The Cut (P, Q) consists of edges
all of which are already occupied by existing paths as shown in the bottom
right corner of Fig. 6.4.5. The Cut (P, Q) is in fact the Minimum Cut equal
to the maximum number of edge-disjoint paths from vertex s to t in D.
Proof for Claim 6.4.3: We have seen that the Algorithm 37 not only
provides maximum number of pseudo edge-disjoint paths (see Fig. 6.4.4 &
Fig. 6.4.6.) but also the status of every edge is provided by it. If we remove
all edges of D which have a Status = unused from D then the resulting graph
D0 would have the following properties:
The above three properties guarantee that the modified graph D0 has as
many (real) edge-disjoint paths from s to t as the number of pseudo edge-
disjoint paths in the original graph D. In fact it is possible to find the (real)
edge-disjoint paths from the modified graph D0 (as shown in the bottom of
Fig. 6.4.4) using (the very simple) Algorithm 36.
number of edges (which if removed will destroy all paths from vertex s to t).
The vertex form of Menger’s Theorem equates maximum number of vertex-
disjoint paths (from vertex s to vertex t) to minimum number of vertices
which if removed will destroy all paths from vertex s to t in a directed
graph. The Fig. 6.4.9 shown below shows a directed graph H in the top left
diagram. It also shows how this graph would look like if we remove a number
of vertices. Please note that removal of certain vertices does not destroy all
paths from vertex s to vertex t in this graph (see the top right and bottom
left diagrams) while the removal of some other vertices does indeed destroy
the s − t connectivity in this graph.
a c c
s t s t
b d b d
Graph H Graph H after removing
vertex a
c c
s t s t
d b
Graph H after removing Graph H after removing
vertex a & b vertex a & d
Figure 6.4.9: Various vertices are removed to show that the graph does not
necessarily become disconnected by their removal and only specific ones make
the graph disconnected.
Interestingly both these problems can be resolved using our prior experience
provided we make a couple of transformations on a given directed graph H
(left diagram) and convert it into another directed graph D (right diagram)
as shown in Fig. 6.4.10 using Algorithm 39. We split each vertex x (excluding
vertex s and t) of directed graph H into x1 and x2 in D; each directed edge
(x, y) in H is transformed into a directed edge (x2 , y1 ) in D. We insert an
extra edge between x1 and x2 in D for every vertex x in H except for s and
t as shown here. For every edge (s, x) in H insert a directed edge from s to
x1 in D. Similarly for every edge (x, t) in H insert a directed edge from x2
to t in D. Thus for every edge in H, there will be a corresponding edge in
D as shown in brown color in the right diagram of Fig. 6.4.10. Let us call
these edges external edges. In addition to these edges we have edges of the
form (x1 , x2 ); we call these edges internal edges. Thus for the new graph D
we have V (D) = 2V (H) − 2 and E(D) = E(H) + V (H) − 2.
a1 a2 c1 c2
a c
s Graph D
s t t
b d
b1 b2 d1 d2
Graph H
Figure 6.4.10: Each vertex of the directed graph of shown in the left diagram
is split up into two vertices, as shown in the right diagram. External edges
are shown in brown color while internal edges are shown in bold orange color.
Once the directed graph H is transformed into D it has now become possible
to appreciate the following:
282 Network Flows, Connectivity and Matching Problems
a1 a2 c1 c2 a1 a2 c1 c2
s t s t
b1 b2 d1 d2 b1 b2 d1 d2
a1 a2 c1 c2 a1 a2 c1 c2
s t s t
b1 b2 d1 d2 b1 b2 d1 d2
a1 a2 c1 c2
a1 a2 c1 c2
s t
s t
b1 b2 d1 d2
b1 b2 d1 d2
Figure 6.4.11: The top left diagram shows a directed graph with a selected
a c
path P1 from vertex s to vertex t shown in pink. The top right diagram
Graph H
shows selected edges of the path P1 reversed. We again find a path (blue)
as shown in the middle left diagram. Now when we reverse selected edges
of this path then it is no longer possible to find a path from vertex s to t
as shown in the middle right diagram. This is the final Graph F , to which
we add the external edges of Graph D and find the Minimum Cut. The
minimum (vertex) cut is indicated by a bold line. The reason for the step
in the bottom right diagram is to ensure the Minimum Cut passes through
internal edges only.
284 Network Flows, Connectivity and Matching Problems
a c a c
s t s t
b d b d
a c a c
s t s t
b d b d
Graph D Initial Graph F
a c a c
Min Cut
s t s t
b d b d
a c a c
s t s t
b d b d
Figure 6.4.13: The top left diagram shows an un-directed graph with a se-
lected path P1 from vertex s to vertex t shown in bold. The top right diagram
shows the edges of the path P1 reversed. We again find a path as shown in
the middle left diagram. Now when we reverse the edges of this path then
it is no longer possible to find a path from vertex s to t as shown in the
middle right diagram. The minimum cut is indicated by a shaded region in
the bottom diagrams.
Menger’s Theorem 287
a c
s t Un-directed Graph H
b d
a1 a a2 c1 c c2
Directed Graph D s1 s s2 t1 t t2
before vertex splitting
b1 b b2 d1 d d2
a1 a2 c1 c2
s1 s s2 t1 t t2
Directed Graph D after
vertex splitting
b1 b2 d1 d2
Figure 6.4.14: The top diagram is an undirected graph G. Each edge of this
graph (is split to indicate directed edges going both ways) is replaced by two
directed edges as shown in the middle diagram. Then each vertex other than
vertex s and t is split and the graph G is transformed into a directed graph
D shown in the bottom diagram.
288 Network Flows, Connectivity and Matching Problems
a1 a2 c1 c2 a1 a2 c1 c2
s1 s2 t1 t2 s1 s2 t1 t2
b1 b2 d1 d2 b1 b2 d1 d2
Graph F
a1 a2 c1 c2 a1 a2 c1 c2
s1 s2 t1 t2 s1 s2 t1 t2
b1 b2 d1 d2 b1 b2 d1 d2
Final Graph F
a1 a2 c1 c2
a c
s1 s2 t1 t2
s t
b1 b2 d1 d2
b d
Graph H
Final Graph F with
External Edges
Figure 6.4.15: The top-left diagram shows the initial graph F , where path P1
(pink) is found and the edges of the path are reversed as shown in the top-
right diagram. The middle diagram shows another path P2 (blue) found and
its edges are reversed in the middle-right diagram, as can be seen no more
paths can be found. Hence we apply Algorithm 39 and find the minimum
cut for the vertex-disjoint paths in the bottom-left diagram and that cut is
applied to original Graph G of Fig. 6.4.14.
Menger’s Theorem 289
Edge Connectivity
a p
b f g i m
c e h j n
d k
Vertex Connectivity
Figure 6.4.16: The top diagram shows the edge connectivity of the top graph
and the bottom one shows the vertex connectivity of the bottom graph.
Our prior knowledge and expertise tells us that given an undirected graph G
and two already selected special vertices s and t we can efficiently find λ(s, t)
which is equal to minimum number of edges which if removed will disconnect
vertex s from vertex t in G. (We actually do it by finding maximum number
of edge-disjoint paths from vertex s to t in a graph G). Is it possible to
arbitrarily select s and t in the graphs below, find λ(s, t) and claim that
λ(s, t) is equal to the edge connectivity of graph G, that is, λ(s, t) = λ(G)?
The problem may not be that simple as it is evident from the diagram below
(Fig. 6.4.17). There is a possibility that a wrong choice for vertex s and
vertex t may give you an incorrect result as shown in the following diagrams.
What is then the way out? Perhaps we should consider all possible (s − t)
pairs in the graph G? As the graph G consists of p vertices, there could be
O(p2 ) distinct pairs of vertices. For each such pair (we call it an s−t pair) we
find the maximum number of edge-disjoint paths from vertex s to vertex t in
the undirected graph G. We know that the maximum number of edge disjoint
290 Network Flows, Connectivity and Matching Problems
8 8 8
t
7 6 7 6 7 6
t
2 2 2
5 5 s 5
1 3 1 3 1 3
s s t
4 4 4
paths is equal to λ(s, t). Out of all the O(p2 ) values for λ(s, t) (because of
O(p2 ) distinct pairs of s−t vertices) we select the one with the minimum value
and that will be the edge connectivity λ(G) of the graph. It will be a useful
experience to derive the overall time complexity of this simple algorithm.
The next logical step should be to find ways to reduce this complexity. You
may have realized that there is no need to consider all O(p2 ) distinct pairs of
vertices; only O(p) pairs will be sufficient for calculating edge connectivity as
shown in Fig. 6.4.18. The problem of efficiently finding vertex connectivity
is slightly more complex as we shall explain in the coming section.
We have already hinted before that in order to efficiently compute edge con-
nectivity of an undirected graph, we may fix s arbitrarily but then compute
λ(s, t) for every different value of t in the undirected graph as shown in the
figure below. We thus have to apply the maximum edge-disjoint paths find-
ing algorithm for all possible p − 1 pairs where s is fixed while t takes on
every possible value as shown in Fig. 6.4.19. This intelligent observation will
certainly cut down the time complexity of our earlier technique as described
in the algorithm given below.
We select a vertex s and keep it fixed throughout the working of the algo-
rithm. We select a different vertex t in each of the p − 1 graphs and in each
such graph we find the maximum number of edge-disjoint paths from vertex
s to vertex t in time equal to O(pq). We repeat this process as many number
of times as the number of graphs. Out of all results we select the minimum.
Menger’s Theorem 291
2 6
1 3 5 8
4 7
Edge Connectivity
8 8 s 8
7 6 7 6 7 6
s 2 2 2
5 5 5
1 3 1 3 1 3
s
4 4 4
Figure 6.4.18: When does vertex s moves from one place to another?
8 8 8
t
7 6 7 6 7 6
t
2 2 2
5 5 s 5
1 3 1 3 1 3
s s t
4 4 4
Figure 6.4.19: When is vertex s fixed and when does vertex t change its
position?
Problem 6.3.1. We now know how to find the magnitude of λ(G). How
about finding the actual edges belonging to the minimum sized set of edges
which if removed will disconnect graph G?
Of course you can use the edge-connectivity finding algorithm to solve this
problem but then it will be an overkill. So let us design an efficient algorithm
to solve this problem. Once we have solved the decision problem it becomes
almost trivial to solve the corresponding optimization problem of finding
the (minimum) edge connectivity of a graph. In fact the problem becomes
a search problem in a finite search space. You have the option of making
a linear search or a more efficient binary search in order to find the edge
connectivity of a graph. Try to derive the time complexity of this algorithm.
Let us now try to make the previous algorithm more efficient. We make p − 1
copies of graph D numbered 2 to p and then fix vertex s and t in each copy.
In the j th copy of graph G we select vertex j as t where 2 ≤ j ≤ p. In every
Menger’s Theorem 293
8 8 8
t
7 6 7 6 7 6
2 t 2 2
5 5 s 5
1 3 1 3 1 3
s s t
4 4 4
Copies of graph G with s fixed at 1 and a different t
8 8 8
7 6 7 6 7 6 t
2 t 2 2
5 5 s 5
1 3 1 3 1 3
s s t
4 4 4
8 8 8
7 6 7 6 7 6 t
2 t 2 2
5 5 s 5
1 3 1 3 1 3
s s t
4 4 4
We know how to find κ(s, t), that is the minimum number of vertices which
if removed will destroy all paths between vertex s and t in a given undirected
graph G. If we find a κ(s, t) for every possible pair of vertices in the graph
G and then select the minimum (out of all O(p2 ) possible values), we shall
get the vertex connectivity κ(G) for graph G. How about if we use the same
complexity cutting strategy used in finding the edge connectivity; select s
arbitrarily but allow t to have all possible values? Please concentrate on the
following algorithm (Algorithm 44)
Menger’s Theorem 295
Assume that the algorithm (Algorithm 44) is applied to the graph shown
below. The vertex connectivity κ(G) of this graph is also indicated here.
The outcome of the above algorithm when applied to this graph (Fig. 6.4.21)
is shown in Fig. 6.4.22.
a p
b f sg i m
e sh j n
d k
The figure shows that if s is initially selected as g (or h) in this graph then
the output of this algorithm will be 3 which is a wrong answer for vertex
connectivity. Any other choice for vertex s will always provide us the correct
answer in this graph. You can easily generalize these observations; if we select
s among the vertices which belong to the minimum sized vertex set (which if
removed will disconnect graph G) then our algorithm will provide an incorrect
estimate for vertex connectivity. The output of the above algorithm will not
be equal to the vertex connectivity of the given graph but it will still give us
an upper bound on vertex connectivity. That means we should change s and
again apply this algorithm but we need not do this repetition more than the
latest output of our algorithm. How this intelligent strategy cuts down the
296 Network Flows, Connectivity and Matching Problems
Min-Cut Min-Cut
a p a p s
s
b f g i m b f g i m
c e h j t n c e h j n
t
d k d k
a p a p
s g f
s g m
b f i m b i
c e s
h j n c e s
h j n
t
t
d k d k
If s = g and t = c then κ(s,t) If s = g and t = e then κ(s,t)
will not provide the Optimal answer will not provide the Optimal answer
a p a p
t s g s g
b f i m b f i m
c e s
h j n c e t h j n
d k d k
If s = g and t = b then κ(s,t) If s = g and t = h then κ(s,t)
will not provide the Optimal answer will not provide the Optimal answer
Figure 6.4.22: The arbitrary pair s and t are used to find the Min Cut for
vertex-disjoint paths between each pair of s and t and then the minimum
value of those p cuts is chosen to find Vertex Connectivity of the graph as a
whole.
298 Network Flows, Connectivity and Matching Problems
a1 b1 a1 b1
a2 b2 a2 b2
A B A B
a3 b3 a3 b3
a4 b4 a4 b4
Transforms
Directed Graph D with nodes s & t Maximum Paths in Graph D
a1 b1 a1 b1
a2 b2 a2 b2
s A B t s A t
a3 b3 a3 b3
Find Max-Paths
a4 b4 a4 b4
Figure 6.5.1: The top left diagram shows a bipartite graph B. The bipartite
graph is transformed into a directed graph D with additional vertices s and
t as shown in the bottom left diagram. We find maximum number of edge-
disjoint (or vertex-disjoint) paths in graph D as shown in the bottom right
diagram. Corresponding to each edge-disjoint path from vertex s to vertex
t in the directed graph D, there is a matching edge in the bipartite graph
as shown in the top right diagram. Maximizing number of paths in D is
equivalent to finding maximum matching in B.
300 Network Flows, Connectivity and Matching Problems
P
a1 b1 a1 b1
a2 b2 a2 b2
s A B t s t
a3 b3 a3 b3
A
a4 b4 a4 b4
N(X)
P
a1 b1 a1 b1
X
a2 b2 a2 b2
s t s t
a3 b3 a3 b3
Min-Cut A-X
A
a4 b4 a4 b4
a1 b1 a1 b1
X Y
a2 b2 a2 b2
s t A B
a3 b3 a3 b3
a4 b4 a4 b4
Figure 6.5.2: The top left diagram shows directed graph D where it is no
longer possible to find an additional path from vertex s to t. However it is
still possible to reach some vertices from s; these vertices form the set P .
The set X contains those vertices which are common in the set P and the
partite A. The middle right diagram shows the minimum cut. The bottom
left diagram shows the set Y containing (minimum number of) vertices which
if removed will destroy all paths from vertex s to t in the directed graph. The
set Y is in fact the vertex cover in the bipartite graph shown in the bottom
right diagram.
Konig’s Theorem, Menger’s Theorem & Hall’s (Marriage) Theorem
Revisited 301
5. Let P represent the vertices which are reachable from vertex s once we
have found maximum number of paths and it is no longer possible to
find an additional path from s to t in D as shown in the top left diagram
of Fig. 6.5.2. Obviously P will not contain t (why?). Let X represents
vertices which are common between P and the partition A as shown in
the middle left diagram of the same figure. The neighborhood N (X)
of X is shown in the middle right diagram of this figure.
8. If the set P does not contain any vertex of A then it implies that the
number of paths from s to t is equal to the degree of node s in the
graph. It also implies that every vertex belonging to A is matched to
a vertex in B in the corresponding bipartite graph.
Problem 6.4.1. The Fig. 6.5.4 below shows a bipartite graph B in the
top left diagram. It is first transformed into a directed graph D as described
earlier, we find maximum edge-disjoint paths in D, then reverse the direction
of each path, the resulting directed graph F is shown in the top right diagram
of this figure. Each matching edge in the bipartite graph corresponds to a
302 Network Flows, Connectivity and Matching Problems
a1 b1 a1 b1
a2 b2 a2 b2
A B s A B t
a3 b3 a3 b3
a4 b4 a4 b4
a1 P
b1 a1 b1
a2 b2 a2 b2
A B t
s A B
a3 b3 a3 b3
a4 b4 a4 b4
a1 b1 a1 b1
P
a2 b2 a2 b2
A B s t
a3 b3 a3 b3
Transforms
a4 b4 a4 b4
a1 b1 a1 b1
a2 b2 a2 b2
A B A B
a3 b3 a3 b3
a4 b4 a4 b4
Figure 6.5.4:
Problem 6.4.2. The figure shown above (Fig. 6.5.4) shows two different
maximum matchings of the same bipartite graph B in the bottom diagrams.
For each bipartite graph (with a maximum matching indicated) draw the
corresponding graph F . You know that in graph F , there is a directed path
from vertex t to vertex s corresponding to each matching edge in B. Find the
set P, X, A − X, and N (X). Also find the minimum cut in terms of vertices
as well as edges.
304 Network Flows, Connectivity and Matching Problems
Problem 6.4.3. The left diagram of Fig. 6.5.5 shows a bipartite graph with
maximum matching edges indicated in different colors. It is obvious that the
maximum matching in this bipartite graph is not a perfect matching – all
vertices of partition A are not matched. Under such conditions vertex s will
be part of a directed cycle, such that the number of vertices of partition A in
the cycle will always be one larger than the number of vertices of partition
B belonging to the same cycle. Can we prove this observation in general
or is this localized to this graph only? What are the implications of this
observation? Is this something to do with the proof of Hall’s Theorem?
a1 b1 a1 b1
a2 b2 a2 b2
s t
a3 b3 a3 b3
a4 b4 a4 b4
Figure 6.5.5:
Problem 6.4.5. The figure shown below (Fig. 6.5.7) shows a bipartite graph
B with maximum matching edges shown in different colors. The right dia-
gram shows the same bipartite graph with two extra vertices s and t added
to it. Note that the bipartite graph B in this problem is transformed into an
un-directed graph G instead of a directed graph D. It is obvious that corre-
sponding to every matching edge in this bipartite graph, there is a path from
Konig’s Theorem, Menger’s Theorem & Hall’s (Marriage) Theorem
Revisited 305
a1 b1 a1 b1
a2 b2 a2 b2
s t
a3 b3 a3 b3
a4 b4 a4 b4
a1 b1 a1 b1
a2 b2 a2 b2
s t
a3 b3 a3 b3
a4 b4 a4 b4
Figure 6.5.6:
306 Network Flows, Connectivity and Matching Problems
between vertex s and vertex t in the graph G (Is this one to one correspon-
dence between a matching edge in the bipartite graph and an edge-disjoint
path in graph G a general phenomena or is it restricted to this bipartite
graph? We shall address this issue in the next problem). Find the minimum
edge cut & minimum vertex cut in graph G and show that it is equal to the
size of the vertex cover in this bipartite graph B.
a1 b1 a1 b1
a2 b2 a2 b2
s t
a3 b3 a3 b3
a4 b4 a4 b4
Bipartite Graph B Graph G
Figure 6.5.7:
a c a c
s t s t
b d b d
Graph D Initial Graph F
a c a c
s t s t
b d b d
a c a c
s t s t
b d b d
a c a c
s t s t
b d b d
Figure 6.6.1: The top left diagram shows a multi-directed graph with three
selected paths from vertex s to vertex t shown in bold. The top right diagram
shows the edges of these paths reversed. We then find another path and
reverse the direction of its edges. The bottom right diagram shows a stage
when it is no longer possible to find an additional path from s to t in this
graph. The minimum cut is also indicated in the bottom diagrams.
Network Flows 309
c 4
a a c
2
3
3
s t s t
1
3
4
b d b d
1
Figure 6.6.2: The left diagram shows the un-weighted multi-directed graph
of Fig. 6.6.1. The right diagram shows how it can be represented by a simple
weighted graph; the weight of each edge (x, y) in this weighted graph corre-
sponds to the number of edges from vertex x to vertex y in the un-weighted
multi-directed shown in the left diagram.
310 Network Flows, Connectivity and Matching Problems
3. Let us assume that (S, T ) is a cut in the network such that the source
vertex s belongs to set S while the sink vertex t belongs to set T .
Then the flow taking place on the cut from S to T will be donated
by (S, T ) while the flow taking place from T to Swill be represented
by (T < S). Thus the network flow f (N ) is given by the equation:
f (N ) = f (S, T ) − f (T, S) . Hence every cut will have some flow in
the direction from s to t represented by f (S, T ) and some flow in the
direction from t to s which is represented by f (T, S); the network
flow f (N ) is the difference between the two. The maximum flow in a
Network Flows 311
5. In every network the value of the maximum flow is equal to the ca-
pacity of a minimum cut. This relationship is described by the famous
MinCut-MaxFlow Theorem.
4 1/4
a c a c
2 1/2
3 3/3
3 2/3
s t s t
1 1/1
3 1/3
4 4/4
b d b d
1 1/1
Figure 6.6.3: The left diagram shows a network flow graph with the capacity
of each directed edge shown. The right diagram shows the actual flow taking
place in an edge divided by the capacity of that edge. It also shows the
maximum flow and the minimum cut.
directed graph with just one important difference – whenever we find a path
from vertex s to vertex t, we use breadth first search – thus ensuring that
we find a shortest path in terms of number of edges. In the shortest path we
move from vertex s to 1 and then to t – thus ensuring a network flow of 3
units from the source to the sink as shown in the top right diagram of this
figure. We find another shortest path from s to 2 to t in the second step –
again a flow of 3 units is possible in this path. The maximum flow and the
minimum cut are found after applying BFS twice in this graph. Please note
that if the upper bound on flow in every edge is 3000 instead of 3 – even
then we have to apply the same number of steps to find the maximum flow.
In other words the time complexity does not depend upon the magnitude
of the upper bound on flow at least in this example. In fact, it can be
proved that application of BFS (a minor change in our algorithm) ensures
(something really big) that the time complexity of the resulting algorithm
will not depend upon the magnitudes of the upper bounds on flow – it will
only depend upon the size of the problem. Unfortunately the said proof and
the resulting time complexity calculations are beyond the scope of this book
– we, therefore rely on our earlier techniques. Let us first find out what extra
price we have to pay if we do not use BFS.
The Fig. 6.6.5 below shows the same network graph. We apply our earlier
technique of finding any path (not necessarily shortest) from vertex s to
vertex t in this network. Fig. 6.6.5 shows various stages of the working of
our algorithm while it is trying to find a maximum flow in the network. This
time we take a longer path from vertex s to vertex t. The path goes from s to
1 to 2 and then to t. We reverse its edges and then take another path – this
time it goes from vertex s to 2 to 1 to t. The algorithm finally converges and
we get the correct answer but after passing through a number of iterations
proportional to k where k is an upper bound on flow in the network. Here the
time complexity has become dependent not only on the size of the problem
but the magnitude of the numbers involved.
3 3 3/3 3/3
s 1 t s 0/1 t
3 3 0/3 0/3
s 0/1 t s 0/1 t
Figure 6.6.4: While finding a path from vertex s to vertex t we do use BFS;
we show the resulting repercussions.
314 Network Flows, Connectivity and Matching Problems
1 2 1
3 3 3
s 1 t s 1 t
3 3
3
2 2 2
2 1 2 1 2
2
s 1 t s 1 t
2
2 2 2 2 2
1 3 1
2 2 2
s 1 t s 1 t
2 2 2 3
2 2
3 1 3/3 1 3/3
3
s 1 t s 0/1 t
3 3/3
3 2 Min-Cut 3/3 2
Figure 6.6.5: While finding a path from vertex s to vertex t we do not use
BFS; it shows the resulting repercussions.
Network Flows 315
Max-Cut passes through maximum number of edges. The size of the Min-
Cut specifies the maximum number of edge-disjoint paths between s and t
in a graph (according to Menger’s Theorem). The size of the Min-Cut in
this graph is only two and thus there are only two edge-disjoint paths in this
graph (between s and t) which are also indicated in the right diagram shown
below (Fig. 6.6.6). Please note that we have already described an efficient
algorithm to find a Min-Cut or maximum edge-disjoint paths in a graph.
Min-Cut
Figure 6.6.6: The left diagram shows various cuts which disconnect graph
and the right diagram shows the Min-Cut for this graph.
If we intend to find a Max-Cut in a network flow graph then why not remove
all edges of the graph - the source vertex will certainly be separated from
the sink vertex and the size of the cut will be maximum? Please think about
this strategy and find what is wrong with this otherwise simple approach?
We now address the problem of how to find Max-Cut and minimum number
316 Network Flows, Connectivity and Matching Problems
Max-Cut
Max-Cut
Figure 6.6.7: The left diagram shows the Max-Cut for the graph and the
right diagram shows the minimum number of paths where each path passes
through every edge of the graph at least once.
4 1/4
3 2 3/3 1/2
3 Min-Cut 2/3
s t s t
1 1/1
3 4 4/4
1/3
1 1/1
Figure 6.6.8: The left diagram shows the capacity of each edge and the right
diagram shows the corresponding Maximum Flow and Min-Cut for the graph.
4 4/4
3 2 7/3 4/2
3
s t s Max-Cut
3/3
t
1
3 4 1/1
3/3 7/4
1 3/1
Figure 6.6.9: The left diagram shows the lower bound for the capacity of
each edge and the right diagram shows the Minimum Flow and Max-Cut for
the same graph.
318 Network Flows, Connectivity and Matching Problems
capacities then we can also solve the problem of finding minimum number
of paths between vertex s and vertex t in a given directed graph where each
edge is traversed at least once.
Remember when we have (only) an upper bound on flow that can take place
through any edge in a network then we start with a small acceptable flow
– so small that it can take place through every edge without violating any
bounds. A zero flow through every edge is a possible answer. Then we try to
increase and maximize the flow from vertex s to vertex t in the network. Now
when we have lower bounds on flow that can take place through any edge
then we should start with a large acceptable flow – large enough that the
lower bound (or limit) on flow through any edge is not violated. We should
be careful about one thing – the conservation of the flow taking place in the
network. It requires that the total flow coming towards a vertex should be
exactly equal to the total flow going out of that vertex (except for vertices
s and t in the network). Algorithm 46 finds an acceptable flow through the
network D. The input as well as the output networks of this algorithm is
shown in Fig. 6.6.10; we assume that the lower bound on flow through each
edge is equal to 1. Please note that an acceptable (or legal flow) shown in the
right diagram of Fig. 6.6.10 is not a minimum flow taking place from vertex
s to t in the network.
1 2
1 3 2
1
1 1
s t s t
1 2
1 1 2 5
1 2
Figure 6.6.10: The left diagram shows the input graph to Algorithm 46 and
the right diagram shows one of the possible output for the algorithm.
The resulting graph F is shown in the top right diagram of Fig. 6.6.11. We
find the maximum flow in graph F taking place from vertex t to vertex s as
shown in the middle left diagram. The maximum flow taking place through
any edge (y, x) in this graph is represented by m(y, x). The Min-Cut in
graph F from vertex t to vertex s is shown in the middle right diagram. The
320 Network Flows, Connectivity and Matching Problems
2
x y x y
1
3 2 2 1
1 s 0 t
s t
2 1
2 5 1 4
2 1
1/1 1/1
x y
1/2 1/1 1/2 1/1
Min-Cut
s 0 t s 0 t
1/1 2/4 1/ 1 2/4
1/1 1/1
1/1 1/1
1
x y
2 1
Max-Cut
1
s t s t
1
1 3
1
Figure 6.6.11: The diagram shows the entire process of finding the Max-
Cut for the graph by minimizing the flow(middle left diagram) after the
acceptable flow (left-right diagram) which eventually leads to finding the
Max-Cut for the graph (in the bottom-right diagram).
Network Flows 321
6/1 7
6/6 6
s t
6/4 6/1
4 3
Figure 6.6.12: Upper/Lower limit for flow in each edge is shown. The flow,
shown in red color, violates the upper limit.
Problem 6.5.7. We are given a network graph D with special vertices s and
t. The lower limit for flow is 1 while the upper limit is 2 for each edge of this
graph. We need to find if there is a feasible flow in this network graph? Can
we design an efficient algorithm to solve this problem? If the in-degree of
any node (other than vertex s and t) is more than two times the out-degree
of that node in this graph then it will not be possible to find a feasible flow
in the graph (why?). If, however, this condition is false in a network graph
(that means the in-degree is at most two times the out-degree of any vertex
other than vertex s and t) then a feasible flow will exist in the network graph?
Can you counter this argument? Can you now design an efficient algorithm
to find a feasible flow in the graph?
a g a g
b e h b e h
s t s t
c f i c f i
d j d j
Figure 6.6.13:
Problem 6.5.8. Please recall the directed graph D shown in top left diagram
of Fig. 6.4.6 (also shown below Fig. 6.6.13). We apply Algorithm 45 on this
graph; classify used and unused edges and then redraw the directed graph
324 Network Flows, Connectivity and Matching Problems
D without the unused edges. The two graphs are reproduced in the figure
below for ready reference. We claim that in any network graph D without
the unused edges, it is possible to push a flow of exactly one unit in every
directed edge, either prove this or give a counter example.
Problem 6.5.9. A directed network graph D without unused edges has two
important properties: (a) every edge is part of a directed path from vertex
s to vertex t in D, & (b) We can use (the so called stupid) Algorithm 36 to
find maximum edge-disjoint paths in this graph. Can you think of a directed
graph where property (a) is applicable but (b) is not? Can you think of a
graph where (b) is applicable but (a) is not?
Problem 6.5.10. Consider a directed graph D where the in-degree of every
vertex (other than s and t) is equal to the corresponding out-degree. (a) Is
it possible to push a flow of exactly one unit through every directed edge in
this graph? (b) If it is possible then does it mean that every edge in this
graph will be part of a directed path from vertex s to vertex t in this graph?
(c) If (a) is possible then can we find maximum edge-disjoint paths from s
to t in this graph using the stupid Algorithm 36?
Figure 6.6.14:
a1 b1 a1 b1
a2 b2 a2
s b2 t
A B
a3 b3 a3 b3
Directed Graph F
a1 b1 a1 b1
s a2 b2 t s a2 b2 t
a3 b3 a3 b3
a1 b1 a1 b1
s a2 b2 t a2 b2
A B
a3 b3 a3 b3
Figure 6.7.1: The top left diagram shows a maximal matching in a bipartite
graph. The top right diagram shows a path from vertex s to vertex t cor-
responding to each matching edge in the bipartite graph. This diagram is
converted into the middle left diagram by reversing every edge in each path.
The middle right diagram shows maximum number of pseudo edge-disjoint
paths from vertex s to t. The bottom left diagram shows maximum number
of edge-disjoint paths in the directed graph while the bottom right diagram
shows the corresponding maximum matching in the bipartite graph.
The Matching Problem in Bipartite Graphs 327
in every pair are compatible. This problem is also known as the Marriage
Problem, it can be modeled by an un-weighted bipartite graph as shown
below (Fig. 6.7.2). The problem can be solved by techniques similar to the
ones that we have just studied.
Problem 6.6.1. Assume that we are required to solve the marriage problem:
namely we intend to marry maximum number of boy; each boy can marry
a single girl and each girl can marry a single boy. How will you model this
problem in terms of a known graph problem? Under what conditions it will
be possible?
Problem 6.6.2. Assume that we are required to solve the marriage problem:
namely we intend to marry maximum number of girls; each girl can marry
a single boy. How will you model this problem in terms of a known graph
problem? Under what conditions it will be possible?
Problem 6.6.4. Assume that a boy can marry four girls and each girl can
marry a single boy; we intend to maximize the number of marriages taking
place. How will you model this problem in terms of a known graph problem?
Under what conditions will it be possible?
Problem 6.6.5. Assume that a boy can marry four girls; we intend to
maximize the number of girls who are married. How will you model this
problem in terms of a known graph problem? Under what conditions will it
be possible?
Problem 6.6.6. Assume that a boy can marry four girls; we intend to
maximize the number of boys who are married. How will you model this
problem in terms of a known graph problem? Under what conditions will it
be possible?
Problem 6.6.7. We intend to maximize the number of married boys and
girls subject to the condition that a boy can marry four girls while a girl can
marry a single boy.
Problem Set 6.7.
Problem 6.7.1. The maximum matching in a bipartite graph is equal to
the maximum number of edge-disjoint paths in a directed graph D. In other
words the problem of finding maximum matching in a bipartite graph is
transformed into the problem of finding maximum number of edge-disjoint
(or vertex-disjoint) paths in a directed graph D. How about if we remove all
directions from the directed graph D, thus converting it into an undirected
graph. Can we still claim that the maximum matching in the bipartite graph
is equal to the maximum number of edge-disjoint paths between vertex s and
vertex t in the undirected graph D. Discuss with the help of an example.
Problem 6.7.2. We are given a bipartite graph with edge weights equal to
either zero or one. We need to find maximum weighted matching in this
weighted bipartite graph. Discuss how you will solve this problem efficiently
using similar techniques.
diagram. It can be found using our expertise gained in the last section.
Here we shall describe another algorithm to find the maximum mathing -
this new algorithm may not be as efficient as the one described before but
it has the added advantage of being flexible. It can handle (with or without
minor modification) the more general problem of finding maximum weighted
matching in a bipartite graph. As shown in Fig. 6.7.3 we convert this bipartite
graph into a complete weighted bipartite graph - see the right diagram of
this figure. An edge in the original bipartite graph has a weight of 1 in the
complete bipartite graph while every other edge has a weight equal to zero.
Thus the complete bipartite graph has binary weights. A maximum weighted
matching in the right graph of this figure will be a maximum matching in
the original bipartite graph.
We describe in the following paragraphs a useful algorithm to solve the max-
imum weighted matching problem in a complete bipartite graph with binary
weights. The results of this algorithms can be used to find maximum match-
ing in a bipartite graph. The algorithm can also be used to find maximum
weighted in a complete bipartite graph with non binary weights as described
in the next section.
a1 a1 1
b1 b1
1
efficient but useful algorithm) which works on the directed graph D and
outputs the maximum weighted matching in the bipartite graph.
a1 b1 a1
1 b1
1
a2 1
b2 a2 1
s b2 t
A B
1 1
a3 b3 1 1
a3 b3
Directed Graph F
a1 b1 a1 b1
1 1
s a2 1 b2 t s a2 -1 b2 t
1 1 1
1
a3 b3 a3 b3
Weight of the Longest
Path = +1
a1 b1 a1 b1
1
1
s a2 -1 b2 t a2 1
s b2 t
1 1
a3 -1 -1
b3 a3 b3
Weight of the Longest
Path = +1-1+1=+1
a1 b1 a1
1
b1
1
s a2 1 b2 t a2 1
b2
-1
A B
-1
a3 1 1
b3 a3 b3
Weight of the Longest
Path = 0
Graph consisting
of First 2 Layers
13
a1 b1 a1 13
b1
6 15
6 15
A B
a2 4
b2 a2 4
b2
8
8
10 16
10 16
17
a3 17
9
b3 a3 9 b3
13 13
a1 b1 a1 b1
6 15 6 15
4 Different
a2 b2 a2 4
b2
8 8
10 16 10 16
17 17
a3 9 b3 9
a3 b3
a1 13 13
b1 a1 b1
6 15 6 15
a2 4
b2 a2 4
b2
8 Different
10 16 16
8
17 17
a3 9 b3 a3 9 b3
13 13
a1 b1 a1 b1
6 15 6 15
a2 4 Same a2 4
b2 b2
8 8
10 16 10 16
17 17
a3 9 b3 a3 9 b3
Figure 6.7.6: The left diagrams show the working of Algorithm No. 48 when
applied to a bipartite graph G. The right diagrams show the working of
Algorithm No. 49 when applied to the same bipartite graph G. Please note
that intermediate results may be different but the end result is the same for
the two algorithms.
The Matching Problem in Bipartite Graphs 335
Claim 6.7.1. The weight of the longest path from vertex s to t in D is equal
to the gain in the weight of maximum matching when the size of matching is
increased from x to x + 1 in the bipartite graph G.
336 Network Flows, Connectivity and Matching Problems
-10
Maximum Matching in First 2 layers a1 b1
6 5
a1 10 b1 Transform
-4
6 5 a2 b2
8
s 16 10 t
a2 4
b2 9
17
8 a3 b3
16 10
17
a3 b3
9 a4 b4 a1 -10 b1
6 5
a4 b4
a2 -4
b2
8
s 16 10 t
17 25
a3 b3
9
a1 10 b1
6 5
a4 b4
a1 10 b1
a2 4
b2 -6 5
8
16 10
a2 4
17 b2
a3 b3 8
9 s t
-16 10
-17 25
a3 b3
a4 9
b4
Transform
Maximum Matching in First 3 layers
a4 b4
Figure 6.7.7: The diagrams show the working of Procedure No. 1. Maximum
weighted matching in the first two layers in a bipartite graph G is converted
into a maximum weighted matching in the first three layers of the same
bipartite graph.
The Matching Problem in Bipartite Graphs 337
a2 4
s b2 t
Convert
10 16
-17
13 a3 b3
a1 b1 9
6 15 Find Longest path
13
a1 b1
4 6 -15
a2 b2 Transform
16 4
8 s a2 b2 t
17 8
a3 9 b3
10 -16
17
Maximum Matching of size Two a3 b3
9
Reverse Longest path
Figure 6.7.8: The diagrams show the working of Procedure No. 2. Maxi-
mum weighted matching of size 1 in a bipartite graph G is converted into a
maximum weighted matching of size 2 in the same bipartite graph.
338 Network Flows, Connectivity and Matching Problems
10
a1 b1
6 5
a2 4
8
b2
14
l= 10
tia 16
Ini
17
a3 9
b3
6 a1 -10
b1 16
6 5
a4 b4
10
a1 b1
-4 8 6 5
4 a2 b2
8
s 16 10 t Increase by 9
a2 4
0 9 b2
17 8
a3 9 b3
16 10
17
a3 9 b3
a4 b4
-10 16
6 a1 b1
6 5
10
a1 b1
6 5
8 -4 12
a2 b2
8
s 16 10 t Increase by 21 a2 4
b2
0 21 8
17
a3 9 b3 16 10
17
a3 9 b3
a4 b4
6 a1 -10 b1 16 a1 10
b1
6 5 6 5
a2 -4 12
8 b2 a2 4
b2
8 8
s 16 10 t Increase by 25
10
16
0 17 25
17
a3 b3 a3 9
b3
9
a4 b4 a4 b4
Figure 6.7.9: The diagrams illustrates that every path from s to t in the
directed graph D passing through the first three layers corresponds to a
matching in the first three layers of the bipartite graph G. The longest path
corresponds to a perfect matching of maximum weight.
The Matching Problem in Bipartite Graphs 339
6 a1 -10 b1 16 6 a1 -10
b1 16
6 5 6 5
-4 8
8 a2 -4
b2 12 4 a2 b2
8 8
s 16 10 t s 16 10 t
0 17 25 0 17 9
a3 a3 9 b3
9
b3
a4 b4 a4 b4
-10
a1 b1
6 5
a2 -4
b2 ge
s
8 Ed
e
s t rs
16 10 ve
Re
17
a3 -9 b3
a4 b4
Figure 6.7.10: The top left diagram shows the longest path from s to t in D
while the right diagram shows a relatively shorter path. If we reverse edges
of the shorter path then a cycle of net positive value will be formed.
340 Network Flows, Connectivity and Matching Problems
Problem 6.9.5. Assume that we have found a 3-edge longest path from
vertex s to vertex t in a directed graph consisting of the first x + 1 layers of
graph D (see right diagram of Fig. 6.7.13). Now we need to find a 5-edge
longest path from s to t in the same graph consisting of first x + 1 layers of
graph D. Design an efficient algorithm to solve this problem. Carefully derive
the time complexity of this algorithm in terms of the size of the problem.
Discuss briefly if your algorithm is a greedy algorithm or does it use dynamic
programming.
The Matching Problem in Bipartite Graphs 341
10
a1 b1 10
6 5
0
a2 4
b2
8
s 16 10 t
17 10
a3 9 b3 a1 b1
a4 b4
10
a1 b1
6 5
Finding Maximum Matching in First 2 layers
a2 4
-5
b2
5 -5 -10 5
-10 a1
a1 b1 b1
6 5 6 5
0 0
4 4 a2 4 4
a2 b2 b2
8 8
10
s 10 t s 16 10 t a1 b1
16 6 5
17 17
a3 9 a3 9 b3
b3
a2 4
b2
8
a4 a4 10
b4 b4 16
17
a3 9 b3
-10 16
4 a2 -4 8 6 a1 b1 a2 -4 12
b2 6 5 8 b2
8 8
s 16 10 t s 16 10 t
0 9 8 0 25
a3 9
17
b3
a2 -4
b2 12 a3
17
b3
8
9
s 16 10 t
0 17 21
a3 9 b3
a4 b4 a4 b4
a4 b4
13
a1 b1 15 a1 13
b1
6 15 6 15
4 8
s a2 b2 t a2 4
b2
8 8
10 16 16
10
17 17
9 17
a3 b3 a3 9 b3 a1 13
b1
6 15
a2 4
b2
Finding Maximum Matching of size 2 16
8
17
a3 9 b3
13
0 a1 b1 13 0 a1 13
b1 14
6 15 6 15
8 8
a2 4
s -1 b2 t s -1 a2 4
b2 t a1 13
b1
8 8 6 15
10 16 10 16
16 16
9 -17 -17
0 a3 b3 0 a3 9 b3 a2 4
b2
8
10 16
17
a3 9 b3
4 8 13 -5 8
s -5 a2 b2 t a1 b1 10
s a2 4
b2 t
8 6 -15 8
-4
10 -16 10 -16
17 9 8 12
4 17
a3 9 b3 s a2 b2 t a3 9 b3
8
-5
10 -16
17 12
a3 9 b3
Only 1
edge is
a1 -10 b1 First layer a1 -10 b1
6 5 Only x 6 5
reversed First x
edges are
reversed Layers
a2 4 -4
b2 a2 b2
8 8
s 16 10 t s 16 10 t
17 17
a3 b3 a3 b3
9 9
ak bk ak bk
Figure 6.7.13: The left diagram shows a directed graph D in which there is
only one edge going from a vertex b to a vertex a. The right diagram shows
another directed graph D where there are x edges going from a vertex b to a
vertex a.
Problem 6.9.9. Find the actual longest path in each of the directed graphs
shown in Fig. 6.7.14.
13 13
a1 b1 a1 b1 15
6 15 6 15
-17 8
s a2 4
b2 t s a2 b2 t
8 8
10 16 10 16
-17 4
ak bk ak bk
9 9
13 13
a1 b1 a1 b1
6 15 6 15
a2 -4 -4
s b2 t s a2 b2 t
8 8
10 16 10 16
17 17
ak bk ak bk
9 -9
Figure 6.7.14: We need to find longest path from vertex s to t in the directed
graphs shown here.
Problem 6.9.15. Suppose we need to find a best possible matching for the
first x members of partite A in a given complete balanced weighted bipartite
graph G. Discuss if we can use Algorithm No. 48 or 49 to solve this problem?
How can you design a better algorithm to solve this problem? Discuss briefly.
Problem 6.9.16. Suppose we need to find a best possible matching for the
first x members of partite B in a given complete balanced weighted bipartite
graph G. Discuss if we can use Algorithm No. 48 or 49 to solve this problem?
How can you design a better algorithm to solve this problem? Discuss briefly.
The Max-Flow Min-Cost Problem 345
1/8
2/8 2/8
a b a b 1/0
2/0 1/0 2/0
1/4 3/5 1/4 3/5
s 6/9 8/2 t s 6/9 8/2 1/2
t
1/4 1/5
2/0
1/0 2/0 2/0
c d c d
1/9 1/9
Figure 6.8.1: We show a directed graph D (left diagram) having two special
vertices s and t. The capacity/cost of an edge is shown along with each edge.
We need to find maximum flow coming out of vertex s and being absorbed
by vertex t. We also need to make sure that the maximum flow is taking
place at minimum cost. The maximum flow at minimum cost is indicated by
colored lines in the right diagram.
that this challenging problem (which we call a Category 1 problem) has two
requirements:
We know how to solve the above two problems in isolation but how can we
fulfill the two requirements simultaneously? If we start finding flows without
looking at costs then we may end up with a maximum flow but at higher
cost. If we start minimizing the cost by finding shortest paths, then how
will we be able to tackle the problem of maximizing the flow? Different
edges have different capacities; finding shortest paths without considering
capacities will create complications. It is certainly a very exciting mixture
of two important problems. Before we solve this general problem we shall
try to reflect on similar problems we have already solved and what are some
of the special cases of this general problem which can be resolved using our
prior knowledge? We shall then extend or modify specialized solutions to
solve this general problem. So let us list down the general problem (once
again) and its special cases:
Category 1: Given a network flow graph as shown in Fig. 6.8.1 how can
we find maximum flow at minimum cost from a source vertex s to a sink
vertex t? We assume that edge capacities are integers while per unit cost of
flow through any edge may be any non-negative real number.
Category 2: Given a network flow graph as shown in Fig. 6.8.2 how can
we find the maximum flow at minimum cost from a source vertex s to a
sink vertex t? We assume that the capacity of each edge is exactly 1 while
per unit cost of flow through any edge may be a real number. Please note
that this problem is equivalent to finding the maximum edge-disjoint paths
from vertex s to vertex t at minimum cost, i.e. the sum of edge costs in all
edge-disjoint (shortest) paths should be as small as possible.
Category 3: The network flow graph is derived from a complete balanced
and weighted bipartite graph. Here we add a source vertex s to the A and
The Max-Flow Min-Cost Problem 347
1/9
a b
1/0 1/0
1/4 1/5
s 1/9 1/2 t The Problem of finding
Equivalent to
Max edge-disjoint
1/0 1/0 paths at Min Cost
c d
1/3
Category 2
The Max-Flow Min-Cost Problem
Reduced to
Binary Costs
1/1
a b
1/0 1/0
1/0 1/1
bl e i n g
s 1/0 1/1 t
p r o er es t
m
1/0 1/0
c d
i nt
1/0
An
13 13
a1 b1 a1 b1
6 15 6 15
a2 4 a2 4
8
b2 s 8
b2 t
A 10 16 B 10 16
17 17
a3 9 b3 9
a3 b3
13
a1 b1 a1 13
b1
6 15 6 15
a2 4 4
b2 s a2 b2
8 t
A 10 16 B 10
8
16
17 17
a3 9 b3 9
a3 b3
Find a longest path from s to t Reverse the edges of the longest path
13 13
a1 b1 a1 b1
6 15 6 15
a2 4 a2 4
s 8
b2 t s 8
b2 t
10 16 10 16
17 -17
a3 9 a3 9 b3
b3
Find another longest path from s to t Reverse the edges of the longest path
13 13
a1 b1 a1 b1
6 15 6 -15
s a2 4
8
b2 t s a2 4
8
b2 t
10 16 10 -16
-17 17
a3 b3 a3 b3
9 9
Find another longest path from s to t Reverse the edges of the longest path
13
a1 b1 a1 13
b1
6 -15 6 -15
s a2 4
b2 t s a2 4
b2 t
8 -8
10 -16 -16
10
17 17
a3 b3 a3 b3
9 9
Figure 6.8.5: The step by step working of Algorithm 52 is shown here on the
network graph of Fig. 6.8.3. The final graph F is shown in the bottom right
corner. Please note that in this graph it is no longer possible to find another
path from the source vertex to the sink vertex.
352 Network Flows, Connectivity and Matching Problems
13
a1 b1 a1 b1
6 -15 -15
s a2 4
b2 t s a2 -16 b2 t
-8
10 -16
17 Remove +ive edges -8
a3 b3 from graph F a3 b3
9
Re
gra mov
a1 b1
ph e +i 15
F f ve
rom ed
gra ges i
ph n
D s a2 16 b2 t
8
a3 b3
Figure 6.8.6: Final graph F taken from the last figure is shown in the top
left diagram. We can recover maximum flow at maximum cost by removing
edges with positive weights in this final graph F .
to do some serious graph theoretic work in terms of claims and some hints
for their proofs.
Claim 6.8.1. We are given a directed & weighted graph D. The graph D may
contain cycles but it does not contain any negative weight edge. It contains a
source vertex s and a sink vertex t. We copy this graph in graph F . We find
a shortest path P from source vertex s to the sink vertex t in directed graph
F . We now reverse the direction of each edge in path P in graph F . We
also multiply the weight of each edge in path P by negative one in graph F .
Now we claim that as directed graph D does not contain any negative edges
therefore graph F will not contain any negative weight cycle.
Claim 6.8.2. We are given a directed & weighted graph D with no negative
weight cycles but it may contain negative weight edges. It contains a source
vertex s and a sink vertex t. We copy this graph in graph F . We find a
shortest path P from source vertex s to the sink vertex t in directed graph F .
We now reverse the direction of each edge in path P in graph F . We also
multiply the weight of each edge in path P by minus one in graph F . Now
we claim that as directed graph D does not contain any negative weight cycle
then graph F will also not contain any negative weight cycle.
Claim 6.8.3. Given a network flow graph D. We find a flow of one unit
from vertex s to vertex t. If there is a negative weight cycle in graph F with
a net weight equal to -k (graph F is derived from a network graph D after
reversing the edges in the direction of the flow and multiplying the costs in
354 Network Flows, Connectivity and Matching Problems
2/8 2/8
a b a b
2/0 1/0 2/0 1/0
3/5
1/4 3/5
s 6/9 8/2 t s 6/9 8/2 t
1/0 2/0 1/0 1/4
c d c d
1/9 1/0
1/-9
An Improvement is
One edge-disjoint path or Reverse the edges in the
a flow of 1 unit in a
possible as there is a path shown earlier
Category 1 Problem negative cycle There is a negative cycle
of value = -2
2/8
a b
2/0 1/0
1/4 3/5
s 6/9 8/2 t
1/0 2/0
c d
1/9
Figure 6.8.7: We show a flow of one unit in the network shown in the top
left diagram. If there is a negative weight cycle (after reversing the edges in
the direction of the flow) then the cost of flow can be further reduced.
The Max-Flow Min-Cost Problem 355
these edges by minus 1) then it is possible to reduce the cost of the existing
flow by k by re-adjusting the flow in the direction of the negative cycle.
Claim 6.8.4. If there is no negative weight cycle in graph F (derived from
a network graph D after reversing the edges in the direction of the flow and
multiplying the costs in these edges by negative 1) then it is not possible to
reduce the cost of the existing flow by re-adjusting the flow in any direction.
2/8 2/8
1/0
a b a b
2/0 1/0 1/0 1/0
1/4 3/5 1/-5
1/-4
s 6/9 8/2 t s 6/9
2/5
8/2 t
1/0 2/0 1/0 2/0
c d c d
1/9 1/9
2/8 2/8
1/0 a b 1/0
1/0 a b 1/0
1/0 1/0
1/-5
1/-4 1/-5 1/-4
s 6/9
2/5
8/2 t s 6/9
2/5
8/2 t
1/0
m en t o r a 1/0
c d v e ef c d
1/9 1/0
m p r o s s i bl n i t s 1/9 1/0
I o u
No t i s p o f 2
The cycle is of net
o s ow The cycle is of net
C l
positive weight
i n xed f positive weight
f i
Figure 6.8.8: We show a flow of two units in the network shown in the top left
diagram. If (after reversing the edges in the direction of the flow) there are
no negative weight cycles then the cost of flow can not be further reduced.
Claim 6.8.5. Given a graph D and a finite flow taking place from vertex
s to vertex t. We know the path taken by each unit of flow from vertex s
to vertex t in D; we keep a record of the length of each such path and call
these paths existing paths in D. If there is no improvement possible in the
cost of existing flow then there will be no negative weight cycle in graph F
(derived from a network graph D after reversing the edges in the direction of
the flow and multiplying the costs in these edges by negative 1). Please see
Fig. 6.8.9.
356 Network Flows, Connectivity and Matching Problems
2/8 2/8
1/0
a b a b
2/0 1/0 1/0 1/0
1/4 3/5 1/-5
1/-4
s 6/9 8/2 t s 6/9
2/5
8/2 t
1/0 2/0 1/0 2/0
c d Convert c d
1/9 1/9
Figure 6.8.9: We show two existing paths from s to t in graph D (top left
diagram). The paths are reversed in graph F as shown in the top right
diagram.
The Max-Flow Min-Cost Problem 357
2/8 2/
a b a b 1/
2/
2/0 1/0
1/4 3/5 1/ 3/
s 6/9 8/2 t s 6/ 8/ t
1/0 2/0 1/ 2/
c d c d
1/9 1/
2 2
2 2/ 2 2/8
a b 1/ a b
2/ 2/0 1/0
1/ 3/
s 6/ 8/ t s 6/
1/4 3/5 8/2 t
2/ 2/0
1/ 1/0
c d c d
1/ 1/9
2 2/-8
0/8
0/
2
a b a b 0/0
0/ 0/ 0/0
1/4 3/5 1/-2
s 6/
1/ 3/ 7/ t s 6/9 7/2 t
0/ 0/0
0/0
0/
c d c d
0/ 0/9
1/-9
a b 0/0 a b 0/0
0/0 0/0
1/4
3/5 1/-2 3/5
s 6/9 7/2 t s 6/9 7/2 1/-2
t
1/4
0/0 0/0
0/0 0/0
c d c d
1/-9 0/9 1/-9 0/9
There is a negative cost There is a negative cost
cycle of value = -8 in F cycle of value = -6 in F
a b 0/0 a b
0/0 0/0 0/0
3/5 3/5
1/-2
s 6/9
1/4
7/2 t s 6/9 7/2 1/-2
t
1/4
0/0 0/0
0/0 0/0
c d c d
1/-9 0/9 1/-9 0/9
1/-8 1/-8
1/8 1/8
a b 0/0
a b 0/0
0/0 0/0
0/4 3/5 0/4 3/5
s 6/9 8/2 t s 6/9 8/2 t
1/-4 1/-4
0/0 0/0
0/0 0/0
c 0/9
d c 0/9
d
1/-9
1/-9
A newly formed negative cycle Reverse the direction of the
Circulate a unit flow in this cycle edges in the Cycle in graph F
1/-8 1/8
1/8 2/8
a b 0/0
a b 1/0
0/0 2/0
0/4 2/5 1/4 3/5
s 6/9 7/2 1/-2 t s 6/9 8/2 1/2
t
1/-4 1/-5 1/4 1/5
0/0 2/0
0/0 1/0
c d c d
1/9 1/9
a b 0/0 a b
0/0 0/0 0/0
1/4 1/4
3/5 1/-2 3/5
s 6/9 7/2 t s 6/9 7/2 1/-2
t
0/0 0/0
0/0 0/0
c d c d
1/-9 0/9 1/-9 0/9
There is a negative cost Reverse the direction of
cycle of value = 8 in F the edges in the cycle
Circulate a flow of 1 unit
1/-8 1/8
1/8 2/8
a b 0/0
a b 1/0
0/0 2/0
0/4 2/5 1/4 3/5
7/2 1/-2 1/2
s 6/9
1/-5
t s 6/9 8/2 t
1/-4 1/4 1/5
0/0 2/0
0/0 2/0
c d c d
1/9 1/9
1/2
a b a b
1/0 1/0
1/9
1/8
s 1/7
t s t
1/0
1/0
c 1/5 d c d
Category 2 Category 2: Forget Capacities & ignore Costs
2
a b a b
0 0
-9
-8
s t s -7 t
0
0
c d c 5 d
Find max edge-disjoint paths Reverse the edges in the paths and
multiply edge costs in the paths by -1
2 -2
a b a b
0 0 0 0
-9 9
-8 8
s -7 t s 7 t
0 0
0 0
c 5 d c -5 d
There are -ive Reverse the edges
cost cycles in the cycles
2
a b
0 0
9
Max Edge-Disjoint
8
Paths at Min Cost
s 7 t
0
0
c 5 d
Describe this algorithm (known as Algorithm 53) in your own words and
carefully derive its time complexity. Is this possible to use this technique
(without any appreciable change) to find the minimum cost for a fixed flow-
the amount of fixed flow may not be the maximum flow in the network.
Discuss briefly.
Problem 6.10.2. We show an interesting special case of Category 4 in Fig.
6.8.14. In this special category of graphs the sum of capacities of all edges
coming out of s is equal to the sum of capacities of all edges going into
vertex t. The cost of every edge coming out of s and going into t is zero. The
capacity of every edge in the D − s − t graph is infinite while the cost is a
positive integer indicated in the diagram. We need to find maximum flow at
minimum cost using an efficient algorithm. Find if you can use any earlier
techniques (Algorithm 53) to solve this interesting problem. You may design
a better algorithm if you want and if you can?
∞/8
a b
4/0 5/0
∞/3 ∞/7
s t
3/0 2/0
c d
∞/6
1/7
a1 b1 a1
1/7
1/0 1/0
Source/sink edge
b1
1/3 1/4
1/4 1/5 costs are integers
s t s
1/4 1/5
t
1/0 1/0
a2 b2 1/2
a2
1/7
1/3 b2
1/3
Equivalent
Category 3
The Max flow Min-Cost Problem Category 3
The Max flow Min-Cost Problem
Figure 6.9.1: Left diagram shows a network flow graph with upper bound on
flow in each edge. The lower bound on flow through every edge is zero. The
right diagram shows the maximum flow and the minimum cut in the network
shown in the left diagram. Please note that the flow is conserved at every
vertex except the source and the sink vertex.
2. We are given a network flow graph with zero lower bound, a finite
Network flows with lower & upper bounds on flow and the Circulation
Problem 365
upper bound on flow in each edge. We are also given a cost per unit
flow in each edge of the network graph, and now we need to find a
maximum (or a fixed) flow at minimum cost from a source vertex to
a sink vertex in the network. For example in the following network
flow graph (Fig. 6.9.2) the minimum cost of a flow of one unit from the
source to the sink vertex is 4.
2/8
a b
2/0 1/0
1/4 3/5
s 6/9 8/2 t
1/0 2/0
c d
1/9
Figure 6.9.2: A network flow graph with upper bound on flow in each edge
as well as per unit cost of flow through that edge. The lower bound on
flow through every edge is zero. The problem is to find maximum flow at
minimum cost. Please note that the flow is conserved at every vertex except
the source and the sink vertex.
a 3,9
2,7
s 5 t
4,5 5,7
b
Figure 6.9.3: A Circulation graph with lower as well as upper bound on flow
in each edge. A feasible flow in the graph is also indicated. Please note that
the flow is conserved at every vertex without any exception.
2
a b a b a b
2
3 2
e 4 e 3 e
d c d c d c
3 2
Figure 6.9.4: A Circulation graph (left diagram) with lower bound equal to
1 and upper bound equal to five on flow in each edge. The cost of a unit
flow through every edge is equal to one in this graph. A feasible flow in
the circulation graph is indicated in the middle diagram. A minimum cost
feasible flow is indicated in the right diagram. Please note that the flow is
conserved at every vertex without any exception.
source to the sink. If, however, the lower bound on flow in any edge is
nonzero then finding a feasible flow is not a trivial problem as the zero
flow is not a correct answer.
3,7/3
w x
3,5/3
1,5/5
s 2,4/2 t
0,9/2 2,6/4
y z
2,8/2
Figure 6.9.5: A network flow graph with lower bound, upper bound, and
actual flow in each edge is indicated in the respective order. Please note that
the flow is conserved at every vertex except the source and the sink vertex.
Also note that the feasible flow taking place may not be a maximum flow (or
a minimum flow) from the source to the sink vertex.
1,2,15/2 1,2,15/1
Figure 6.9.6: Left graph is a network flow with a flow of 4 units taking place
from the source to the sink vertex. Each edge has an associated lower bound,
an upper bound, per unit cost on flow, and actual flow taking place in that
edge. A flow of 4 units is a feasible flow but it is taking place at a higher
cost in the left diagram. The right diagram shows the same amount of flow
- that is 4 units - taking place at minimum cost.
Network flows with lower & upper bounds on flow and the Circulation
Problem 369
a 0,9 a 0,9
2,2 2,2
2
No Feasible
s t s Flow t
0
0,5 0,1 0,5 0,1
b b
Figure 6.9.7: Left graph is a network flow graph with a source and sink
vertex; here a feasible flow exists. Right graph is a circulation graph; here a
feasible flow does not exist because of obvious reasons.
x
2
a 0,9 y a 0,9
2,2
2
s t s t
Figure 6.9.8: Left graph is a Circulation graph with associated lower bound
and upper bound on flow in each edge. Please note that except for one edge
all edges have lower bounds equal to zero. The edge (s, a) has a lower bound
equal to upper bound equal to 2. The Circulation graph is converted into a
network flow graph shown in the right diagram. Here the source vertex x is
trying to push a flow of 2 units in the network graph while the sink vertex y
is trying to pull 2 units from the network graph.
x
2
a 0,9 a 0,9
2,2 2,2
y a 0,9
2
s t s t
0,∞ s t
0,∞
0,5 0,1 0,5 0,1
b b 0,1 0,1
b
Figure 6.9.9: Left diagram is a network flow graph with associated lower
bound and upper bound on flow in each edge. First it is converted into a
Circulation graph (middle diagram) and then we find a feasible flow in the
circulation graph.
x
2
a 0,9
2,2
y a 0,9
2
s t
s 2 t
0,∞
0,5 0,1
b 0,1 0,1
b
a 0,9
2,2
2
s t
0
0,5 0,1
b
Figure 6.9.10: Top left diagram is a network flow graph with associated
lower bound and upper bound on flow in each edge. First it is converted
into a Circulation graph (top right diagram). We find a feasible flow in
the Circulation graph and then convert it into a feasible flow in the original
network flow graph shown in the bottom diagram.
372 Network Flows, Connectivity and Matching Problems
2 y
a a a
2,7 0,9 2,2 0,9 0,9
0,5 0,5
s t s t s 2 t
2,7
a
0,9
s 2 t
0,5 0,7
b
Figure 6.9.11: Left graph is a Circulation graph with associated lower bound
and upper bound on flow in each edge. Please note that except for one edge
all edges have lower bounds equal to zero. The edge (s, a) has a lower bound
equal to 2 and an upper bound equal to 7. This edge is split into two edges
as shown in the middle graph. The Circulation graph is converted into a
network flow graph shown in the right diagram. Here the source vertex x is
trying to push a flow of 2 units in the network graph while the sink vertex y
is trying to pull 2 units from the network graph.
Network flows with lower & upper bounds on flow and the Circulation
Problem 373
y y
2 3 4 5 1 2
a 0,5 a a
2,7 3,9 0,5
0,6 0,6
s t s t s t
0,1 0,1
4,5 5,7 0,2 0,2
b b b
4 5 2 3 2 1
x x
Figure 6.9.12: Left graph is a Circulation graph with associated lower bound
and upper bound on flow in each edge. The Circulation graph is converted
into a network flow graph shown in the middle diagram. Here the source
vertex x is trying to push a flow of number of units in the network graph
while the sink vertex y is trying to pull the same amount of units from the
network graph.
374 Network Flows, Connectivity and Matching Problems
y
1
2
a a a
2,7 3,9 2,7 3,9
0,5/3 0,6/2 2+3 3+2
s t s t s t
0,1/1 0,2/0 4+1 5+0
4,5 5,7 b 4,5 5,7
b b
2 1
Figure 6.9.13: Left graph is a Circulation graph with associated lower bound
and upper bound on flow in each edge. The right diagram shows a feasible
flow in the circulation graph. The middle diagram shows an intermediate
stage.
-1 +1
-1
+1
1
1
Upper Bound is 3 while Lower Upper Bound is 2 while
Lower bound is 0 for
bound is 1 black edges
y
Figure 6.9.14: The left diagram is a Circulation graph with associated lower
bound and upper bound on flow in each edge. The cost of per-unit flow
through each edge is equal to 1. The Circulation graph is converted into a
network flow graph shown in the right diagram. Here the source vertex x is
trying to push a flow of 2 units in the network graph while the sink vertex y
is trying to pull the same amount of units from the network graph.
If we just want to find a feasible flow (not the minimum cost feasible flow)
then we know what to do. We convert the circulation (left diagram) into a
network flow graph as shown in the right diagram. We try to push 2 units
of flow from vertex x. If we are successful then it means that a feasible flow
exists in the circulation. But we know that this feasible flow may not be the
minimum cost feasible flow. Such a feasible flow is shown in the left diagram
of Fig. 6.9.15.
376 Network Flows, Connectivity and Matching Problems
-1 +1 -1 +1
-1 -1
+1 +1
1 1
1
Upper Bound is 2 while 1
Upper Bound is 2 while
Lower bound is 0 for
Lower bound is 0 for
black edges
y black edges
y
Figure 6.9.15: The left diagram shows that it is possible to push 2 units of
flow from the source vertex x at some cost. The right diagram shows that it
is possible to push the same amount of flow from vertex x at a lower cost -
in fact at a minimum cost.
-1 +1
-1
+1
1
1
Upper Bound is 2 while Upper Bound is 3 while Lower
Lower bound is 0 for
bound is 1
black edges
y
Figure 6.9.16: The left diagram shows the source vertex x pushing two units
of flow at minimum cost. The right diagram shows the resulting solution
of the minimum cost Circulation Problem. Here the flow in each edge is 1
except for the red bold edges where the flow is 2 units.
Figure 6.9.17: Finding a feasible flow in a Circulation with non zero lower
bounds.
Figure 6.9.19: Finding feasible flow in a network with non zero lower bounds.
Network flows with lower & upper bounds on flow and the Circulation
Problem 379
Figure 6.9.20: The network flow problem is first converted into a circulation
problem.
The top figure shows that if we need to find max cut in this directed graph
(Problem 1) then we should find the min flow from vertex s to vertex t
(Problem 2). In order to find minimum flow we need to make it a circulation
as shown above and then find a minimum cost circulation (Problem 3) in
this graph - that will give us a minimum flow from s to t in the network flow
graph as shown below.
The minimum flow is 5 and thus the max cut will also have same size. Thus
we moved in the following fashion to find the max cut in a network flow
graph? In case of un-directed graphs we have the same sort of strategy - but
instead a negative result.
380 Network Flows, Connectivity and Matching Problems
5,7/5
w x
3,5/3
1,3/3
2,4/2 1,3/2
s t
0,9/2
2,6/2
y z
2,8/4
Figure 6.9.22: Solving one problem solves another. It essentially means that
we cannot find a minimum cost circulation in an un-directed graph while we
can solve this problem in a directed graph?
Chapter 7
Eulerian Graphs & the Chinese
Postman Problem
Introduction
We shall start with a special category of graphs which was earlier discov-
ered in the last chapter. Exploiting our prior knowledge about this category
we define a number of new categories of graphs which are to some extent
similar and at the same time different from the graphs we have seen previ-
ously. Eulerian graphs belong to one of these categories which we discuss
in detail. We make a number of inter-related claims about such graphs and
then show how the proof of one claim can lead to the proof of another. We
then come back to our categories of graphs and look at these in the light of
our newly acquired experience about Eulerian graphs. Thus we start with a
panoramic picture, move in depth with one category, and then come back to
the panoramic picture with new tools and techniques. We end this chapter
with a detailed study of the Chinese Postman problem for both directed as
well as undirected graphs.
Consider the directed graph D shown in Fig. 7.1.1. In this graph vertex a
is a source vertex having only out-degree while the vertex d is a sink vertex
A Special Class of Graphs 383
having only in-degree; for the rest of the vertices of D, the in-degree is equal
to the corresponding out-degree. In this special class of graphs the in-degree
of vertex d is always equal to the out-degree of vertex a (why?). All directed
graphs fulfilling the above properties are known as Class A graphs.
e f y d
a b x c
Figure 7.1.1: A directed graph D with two special nodes a and d. The in-
degree is equal to the out-degree for every node in this graph except a and
d. We can find maximum edge-disjoint paths from vertex a to d in this class
of directed graphs using Algorithm 55.
z z
e f y d e f y d
a b x c a b x c
Figure 7.1.2: We show a directed graph of Class A: Vertex a has only out-
degree while vertex d has only in-degree. For every other node in this graph
the in-degree is equal to the corresponding out-degree. There are two edge-
disjoint paths from vertex a to vertex d in the graph D as shown in the left
diagram. There are two edge-disjoint trails in this graph such that each edge
of the graph is covered exactly once by either of the two trails as shown in
the right diagram.
Concept Map 7.1. A concept map showing various classes of some special
graphs.
386 Eulerian Graphs & the Chinese Postman Problem
every graph G (having p vertices) has 2k nodes with an odd degree; the rest
of the p − 2k vertices have an even degree. Remember that the number of
vertices having odd degree in a graph can not be odd (why?).
We show a Class B directed graph in Fig. 7.1.3. The out-degree of vertex f
is 3 while the in-degree is equal to 1; the out-degree of vertex x is 1 while
its in-degree is equal to 3; for the rest of the nodes, the in-degree is equal
to the corresponding out-degree. We can use Algorithm 55 to find the two
edge-disjoint paths from vertex f to vertex x in D; the two paths are shown
in Fig. 7.1.3. Can we also find trails in this graph such that each edge of this
graph is covered exactly once by one of the trails?
We show a Class C directed graph in the left diagram of Fig. 7.1.4. There
is no special vertex in this graph; the in-degree of every vertex is equal to
the corresponding out-degree. An undirected graph belonging to the same
category is shown in the left diagram of this figure. The degree of each node
in this undirected graph is even. It is interesting to note that a directed graph
(where the in-degree of each node is equal to the corresponding out-degree)
belongs to the same class as an undirected graph where the degree of each
vertex is even (why?).
As you should discover yourself Class C directed and undirected graphs
have some special and interesting properties. One such property is shown
in Fig. 7.1.4. Both the graphs shown in this diagram can be partitioned into
edge disjoint cycles (or circuits) shown in different colors. We can use any
traversal algorithm to find a cycle in such graphs; if we now remove edges
of this cycle from the original graph then the new graph will also belong
to the same class (why?). If however we remove (all edges in) a path from
any vertex x to a vertex y in a Class C graph then the new graph will not
be a Class C graph any more; it will become a Class A or Class B graph.
Similarly if we add a path between vertex a and d (these are the only two
vertices having an odd degree) in a Class E graph then it will be transformed
into a Class C category. How other properties of a graph change (or do not
change) after such a transformation is interesting to explore.
Before moving forward, here are a few questions that you should be able to
answer by yourself:
z z
e f y d e f y d
a b x c a b x c
Figure 7.1.4: Every node in the directed graph has an in-degree equal to the
corresponding out-degree as shown in the left diagram. An un-directed graph
where the degree of each vertex is even is shown in the right diagram. Both
these graphs belong to our Class C category. The edge set of these graphs
can be partitioned into edge-disjoint cycles (shown by different colors) which
if combined together will create a circuit consisting of all edges of the graph.
388 Eulerian Graphs & the Chinese Postman Problem
3. Can we claim that in such a graph every vertex will lie on some cycle?
4. Can we make the above claim if the degree of every vertex is even?
4. The degree of each vertex in a graph G is even if and only if the edge
set of G can be decomposed into edge-disjoint cycles.
We shall start with proving Claim Number 4 and then work backwards in
order to prove earlier claims. Assume that a graph G can be decomposed
into edge-disjoint cycles; then we should be able to prove that the degree of
each vertex of G is even (you should be able to do it easily). So we assume
that the degree of each vertex is even and now we should be able to prove
that the graph G can be decomposed into edge-disjoint cycles. Let us do
Eulerian Circuits and Graphs 389
z z
e f y d e f y d
a b x c a b x c
Let us now concentrate on Claim Number 1. First assume that every edge
(m, n) in graph G is part of an odd number of cycles. It means that the degree
of node m as well as that of n will be even; thus G will be an Eulerian graph.
Now assume that the graph G is Eulerian; we have to prove that every edge
(m, n) in G will be part of an odd number of cycles as shown in Fig. 7.2.2. As
the graph G is Eulerian thus the degree of each node will be even; thus the
degree of node m as well as that of node n will be even. Proving that edge
(m, n) will be part of an odd number of cycles is equivalent to proving that
there are an odd number of paths between vertex m and vertex n (why?).
Proving that there is an odd number of paths between vertex m and vertex
n is in fact equivalent to proving that there is an odd number of trails from
vertex m to vertex n. This may not be very obvious so we shall prove this
after first finding the number of distinct trails between the two vertices.
Please remember that in a trail we may repeat vertices but we cannot repeat
edges while in a path neither vertices nor edges can be repeated.
We start from a (in Fig. 7.2.2) and arrive at the adjacent vertex e; as the
degree of every vertex in G is even so if you can enter a vertex then you
can leave it as well; in fact there will always be an odd number of ways out
(why?). This means that if you draw a tree of all possible trails then the
out-degree of every node in this tree will be an odd number as shown in Fig.
7.2.3. The total number of trails in the graph of Fig. 7.2.2 will be equal to
Eulerian Circuits and Graphs 391
z
z
e f y d
e f y d
a b x c
a a b x c
z
z
e f y d
e
e f y d
a b x c
f a b x c
z b y
y b z x
x f b f b d c
b
b d c b b c d
d x x
c
b b
x x z
b b
e f y d
a b x c
Figure 7.2.3: Consider the Eulerian graph shown in Fig. 7.2.2. We show
here (middle diagram) all possible trails starting from vertex a and ending
at vertex b. We show all possible paths between vertex a and vertex b in the
bottom diagram.
392 Eulerian Graphs & the Chinese Postman Problem
the number of leaf vertices in the tree of Fig. 7.2.3 which will be an odd
number (why?). Every cycle in the graph contributes to two trails as shown
in Fig. 7.2.2 and Fig. 7.2.3. Thus the total number of paths between vertex
m and vertex n will be an odd number (why?).
There are a number of algorithmic issues apart from the above (theoretical)
claims and their respective proofs:
There are a number of exciting theoretical problems which you should at-
tempt before moving forward:
1. How can you generalize the four claims that we have made for multi-
graphs; in multi-graphs we allow parallel edges and self loops? (Hint:
Can you convert a multi-graph into a simple graph?)
2. How can you generalize (or modify) the four claims in case of directed
graphs?
Eulerian Circuits and Graphs 393
Problem Set 7.1. Please read the following algorithm which is primarily
designed to find edge-disjoint cycles in a graph G where the degree of every
vertex is even.
e f y e f y
a b x a b x
Graph G
Graph H
c c
Figure 7.2.4:
Problem 7.1.2. Some one claims that the algorithm outputs edge disjoint
cycles. Discuss why or why not.
Problem 7.1.3. Someone claims that the algorithm outputs maximal num-
ber of edge-disjoint cycles in a graph G. Prove the claim or give a counter
example.
Problem 7.1.4. Someone claims that the algorithm outputs maximum num-
ber of edge-disjoint cycles in a graph G. Prove the claim or give a counter
example.
394 Eulerian Graphs & the Chinese Postman Problem
Problem 7.1.7. How about if we have a directed graph in which the in-
degree of every vertex is equal to its out-degree. How will the solution of
earlier problems be affected?
e f y
a b x
e f y c e f y
a b x a b x
c c
e f y e f y
a b x a b x
c c
Figure 7.2.5: A graph (with every vertex having even degree) can be split up
into edge-disjoint cycles. Several sets of edge-disjoint cycles are shown here,
some having a larger size than others. Note that every edge is part of an
edge-disjoint cycle. We can use Algorithm 56 to find out efficiently one such
set of edge-disjoint cycles.
396 Eulerian Graphs & the Chinese Postman Problem
e f f y
b x
e f
a b x
c
f a b
y
b x
x
c
e f y
a b x
Figure 7.2.6: We have seen that if the degree of every vertex is even then the
graph can be split into edge-disjoint cycles again shown in the top diagram.
On the other hand if a graph can be split into edge-disjoint cycles or if the
edge-disjoint cycles of a graph are given then we can reconstruct the original
graph as shown in the middle diagram. The original graph will have all nodes
with even degree. The reconstruction algorithm will help you reconcile with
this claim. There will be an Eulerian circuit in the reconstructed graph.
Eulerian Walk and the Chinese Postman Problem 397
Such a graph is shown in the left diagram of Fig. 7.3.1. Its counterpart in
directed graphs is shown in the right diagram of Fig. 7.3.1; here the out-
degree of one special vertex is larger than its in-degree by one, while it is the
other way round for the other special vertex; for the rest of the vertices the
in-degree is equal to the corresponding out-degree. In both these graphs it is
possible to find an Eulerian trail from vertex f to vertex x; a trail in which
every edge of the graph is covered (exactly once). How can we prove this
and how can we find an Eulerian trail? Perhaps we can find a constructive
proof which will solve both the problems. Again we should appreciate that
we can use Algorithm 55 to find maximum number of edge-disjoint paths
from a source to a sink vertex?
We have earlier presented Class B and Class A category directed graphs
in Fig. 7.1.2 and 7.1.3. These graphs are reproduced in Fig. 7.3.2 for
comparison with graphs in Fig. 7.3.1. We have made certain claims about
Class A graphs earlier in this chapter. These claims can be generalized with
some interesting modifications for Class B directed graphs and then proved
using our newly acquired experience of Eulerian Graphs?
We now present one last claim which is applicable to Class F undirected
graphs. This class comprises of undirected graphs having 2k nodes with an
odd degree; the rest of the p − 2k vertices have an even degree. The claim
for such a graph G is that the edge set of G can be partitioned into k trails
where each trail is connecting two odd vertices. Such a graph is shown in
the left diagram of Fig. 7.3.3. with odd vertices shown in bold. The right
diagram shows the same graph where we add an extra edge between two odd
vertices converting this graph into a Class C category where the degree of
each vertex is even. It will then become possible to find an Eulerian circuit
in the resulting graph which is also shown in the right diagram of Fig. 7.3.3.
This observation should lead you to design a formal proof for the above claim.
z z
e f y d e f y d
a b x c a b x c
Figure 7.3.1: An un-directed graph shown in the left diagram; the degree of
vertex f and x is odd while the degree of every other vertex is even; this is
a Class E un-directed graph. A directed graph D with two special nodes f
and x is shown in the right diagram; the in-degree is equal to the out-degree
for every node in this graph except f and x; the out-degree of vertex f is one
larger than its in-degree and it is the other way round for vertex x in this
directed graph.
z z
e ff y d e f y d
a b xx c a b x c
z
z
4 4
3 3
e f y d
e f y d
4 4
3 3
a b x c
a b x c
z z
e f y d e f y d
a b x c a b x c
Figure 7.3.3: An undirected graph of Class F shown in the left diagram; the
degree of vertex f , b, y and x is odd while the degree of every other vertex is
even. By adding two edges between odd vertices we can convert this graph
into a Class C category where the degree of every vertex is even.
400 Eulerian Graphs & the Chinese Postman Problem
that the number of edges, traversed more than once, are minimized. This
problem is faced by any post man delivering letters in houses along lanes or a
sweeper who is sweeping roads; he would certainly like to traverse each lane
at least once while making sure that the traversals of the same lane should
be minimized. This problem is formally defined as the Chinese Postman
Problem: We need to find a shortest closed walk in a graph G which passes
through every edge of G at least once. Such a walk (walk because some edges
will be traversed more than once) is also known as an Eulerian walk in the
graph G. In the next figure, we show graphs where there are two vertices of
odd degree. In the left diagram of this figure we have an un-weighted graph
while the right diagram shows a weighted graph. As you can well imagine,
in a weighted graph, we need to minimize the total sum of edge weights in
a closed walk which covers every edge of the graph at least once. There are
basically two problems that we intend to solve simultaneously but first we
need an understanding of these problems in isolation.
2. We need to make sure that the Eulerian trail that we have created in
the first part is shortest in terms of number of edges involved or in
terms of edge weights. In other words the Eulerian graph H should be
of minimum size in terms of number of edges or in terms of sum of edge
weights of H.
have different number of edges; we need to select the one with the minimum
number.
The Chinese Postman Problem for an un-weighted graph can thus be rephrased:
We need to convert a graph G (having some odd vertices) into an Eulerian
graph H (having all vertices with even degree) by duplicating minimum num-
ber of existing edges of graph G. For a weighted graph we need to convert G
into an Eulerian graph by duplicating certain edges such that the sum total
of edge weighted corresponding to duplicated edges is minimized. We show
different Eulerian graphs corresponding to a weighted graph G in Fig. 7.4.4.
It is interesting to note that optimal graph, shown in the right diagram,
consists of more edges than the graph shown in the middle diagram.
402 Eulerian Graphs & the Chinese Postman Problem
3 2 4
1 2 1 2
G H
Transform G into H
4 4
4 3 4 3
2 3
Find a path between the After Duplicating the edges in the path
two odd degree vertices in the odd vertices in G become even
graph G and even vertices remain even
Figure 7.4.2: Shows how can we convert a graph G having two odd vertices
into a graph H where the degree of each vertex is even. The duplicated edges
are shown in red color in the graph H. An Eulerian Circuit in H is equal to
an Eulerian Walk in G.
1 2 1 2 1 2
4 3 4 3 4 3
Optimal Solution
The duplicated edges are shown in The duplicated edges are shown in
Only one edge (red) is duplicated
red; the graph becomes Eulerian red; the graph becomes Eulerian
to create an Eulerian Circuit
Figure 7.4.3: It is possible to convert a graph having two odd vertices into
different Eulerian graphs with varying number of edges. The Eulerian circuit
in each graph is also indicated.
Eulerian Walk and the Chinese Postman Problem 403
2 2 2
1 2 1 2 1 2
9 9
9
4 4 6 4
6 6
4 3 4 3 4 7
3
7 7
Optimal Solution
The duplicated edges shown in red The duplicated edge shown in red
Two edges are duplicated
Cost is 6+7=13 Cost is 9
Cost is 2+4 = 6
Figure 7.4.4: It is possible to convert a graph having two odd vertices into
different Eulerian graphs with varying number of edges. The Eulerian circuit
in each graph is also indicated.
It is quite obvious now that a shortest path between the two odd vertices
provides us an optimal solution to the Chinese Postman Problem given that
the graph has only two odd vertices. What about if a graph G consists of
more than 2 odd vertices as shown in Fig. 7.4.5?
How about if we apply the same trick of finding a shortest path between
vertices belonging to different pairs of odd vertices? (Remember the total
number of odd vertices in any graph will always be even). Let us start with
an arbitrary selection of vertices in the three pairs as shown in Fig. 7.4.6. The
resulting Eulerian graph is shown in the right diagram of the same figure. The
total number of edges duplicated is also indicated in this diagram. Finding
shortest paths for a fixed set of pairs of odd vertices certainly helps to reduce
the cost of making the degree of each vertex even - but a different set of
pair of odd vertices may help us in further reducing this cost as shown in
Fig. 7.4.7. Thus the problem is reduced to finding the set of pairs of odd
vertices which minimizes the cost of duplicating the edges. Do we have to
enumerate all possible sets of pairs of odd vertices in the graph G? How to do
that systematically and estimate what is the total number of possibilities? Is
a brute force approach suitable or should we find a way to solve this problem
in a more efficient manner?
Given 2k items (corresponding to 2k odd vertices) numbered from 1 to 2k;
let us draw a completely connected graph consisting of 2k = 6 vertices (of
G) as shown in Fig. 7.4.8. A perfect matching in this graph provides the
desired set of all pairs of odd vertices. It is possible to put weights on edges
of the completely connected graph as shown in Fig. 7.4.9. A minimum weight
404 Eulerian Graphs & the Chinese Postman Problem
2 2
c 4 c 4
b b
1 a 3 1 a 3
6 6
5 e 5 e
d d
Figure 7.4.6: We find shortest paths between a fixed set of odd vertices in
graph G. The resulting Eulerian graph H is shown in the right diagram.
Eulerian Walk and the Chinese Postman Problem 405
2 2
c 4 c 4
b b
1 a 3 1 a 3
6 6
5 e 5 e
d d
Figure 7.4.7: We find shortest paths between a different set of odd vertices
in graph G. The resulting Eulerian graph H is shown in the right diagram.
2 2 2
1 2 1 2 1 2
3 1 3 1 3 1
2 3 2 2 3 2 2 2
3
4 4 4
6 1 3 6 1 3 1
6 3
2 2 2
5 4 5 4 5 4
2 2
c 4 c 4
b b
1 a 3 1 a 3
6 6
5 e 5 e
d d
(1,2), (3,4), (5,6)
2
1 2 1 2
3 3
6 3 6 3
1 1 1
5 4 5 4
Figure 7.4.9: The weight of a shortest path between two odd vertices in
G corresponds to the weight of an edge between the two corresponding odd
vertices in the graph K.
408 Eulerian Graphs & the Chinese Postman Problem
1 2 2 2 2
1 2 1 2
6 3 1
6 3 6 3
2 2
1 1
5 4 5 4 3
5 4
(1,2), (3,4), (1,2), (3,5), (1,2), (3,6),
(5,6) (4,6) (4,5)
1 2 1 2 1 2
3 3 3
1 3
6 3 6 2 3 6 3
2
1
5 4 3
5 4 5 4
1 2 1 2 1 2
2 4
1
6 4 3 1 4
6 3 6 3
1 2 2
5 4 5 4 5 4
(1,4), (2,3), (1,4), (2,5), (1,4), (2,6),
(5,6) (3,6) (3,5)
1 2 1 2 1 2
1
2 1 3
1
6 3 6 1 3 6 3
1
1
3
5 4 5 4 5 4
1 2 1 2 1 2
1 2 1 3 1
6 3 6 3 6 3
2
2 1
3
5 4 5 4 5 4
c 4
2
3 1
b
2 3 2
4 1 a 3
1 1
2 2 6
1 1
2
5 e
3
1-
8 8
9 7 1+ 9 7 1-
4 4
2+
1 6 1 6
1- 1-
2 5 2 5
0 0
3 1+
3
Graph G: All six odd vertices A Directed graph D derived from the un-directed
are shown in red while even graph G: Difference ∆ between out-degree and
vertices are shown in green In-degree for each vertex is indicated: There are
vertices with +, -, & zero ∆
8 8
9 7 9 7
4 4
1 6 1 6
2 5 2 5
3 3
Figure 7.5.1: We again show an undirected graph G in the top left diagram.
It is converted into a directed graph D as shown in the top right diagram.
It is obvious that graph G is not Eulerian. After duplicating certain edges
in graph G it is transformed into an Eulerian graph as shown in the bottom
left diagram. An Eulerian walk in G is shown in the bottom right diagram.
The Eulerian walk in G is not an Eulerian walk in the directed graph D.
412 Eulerian Graphs & the Chinese Postman Problem
0
8 8
0 9 7 1- 9 7
3+
1 4 6 1 4 6
0 0
2 5 1- 2 5
1-
0
3 3
0
8
2 0 9 7 0
0
5
5 4 1 4 6
0 0
3
7
2 5 0
0
0
3
The lengths of different shortest paths
Edges in each shortest path are
between orange vertices and the red
duplicated: The resulting graph H has
vertex in graph G
now become Eulerian
Figure 7.5.2: We show a directed graph G where there is only one vertex
(shown in red) where the out-degree is larger than the in-degree. All other
vertices have either out-degree equal to in-degree (shown in green) or less
than in-degree (shown in orange).
The Chinese Postman Problem for Directed Graphs 413
the Chinese Postman problem as shown in the same figure. Please note that
all the three shortest paths originating from vertices with ∆ equal to minus
1 are terminating at a single vertex; that single vertex has ∆ exactly equal
to +3; this will make the new directed graph Eulerian; duplicating edges on
shortest paths ensures that the number of extra edges (added) is minimized.
We show a directed graph in the top diagram of Fig. 7.5.3. There are two
vertices (4 & 10) with ∆ negative shown in orange color. Vertex 4 has ∆
equal to −3 while vertex 10 has ∆ equal to −1. Similarly there are three
vertices with ∆ positive, all shown in red color; vertex 9 has ∆ equal to +2.
Please note that there should be three paths coming out of vertex 4 in order
to increase its ∆ from −3 to zero. Similarly two paths should be terminating
at vertex 9 to convert its ∆ from +2 to zero (please see Fig. 7.5.3). In
order to make ∆ of every vertex zero (thus converting the graph G into an
Eulerian graph) we should find out which path originating from an orange
vertex should terminate at which red vertex. We create a bipartite graph B
consisting of an A partite (consisting of all orange vertices) and a B partite
(consisting of red vertices) as shown in the middle left diagram of Fig. 7.5.3.
Please note that this will be a complete bipartite graph; there will be an edge
from every orange vertex to every red vertex in B signifying that there will
be a path from every orange vertex to every red vertex in graph G.
Further adding vertices s and t and edge capacities (middle right diagram)
the problem is converted into a flow problem. Finding a maximum flow
efficiently (in polynomial time) in this (special) network graph N is by itself
an interesting problem. The maximum flow in this graph (as shown in the
bottom diagram) will provide the required information. The graph G will
become Eulerian but the cost in terms of number of edges of G that are
duplicated may be high; the problem is to minimize the cost also. The
solution is simple: we create a weighted bipartite graph B where the weight
of an edge from an orange vertex x to a red vertex y in B signifies the weight
of the shortest path between the orange vertex x to red vertex y in graph
G. Finding a maximum flow with minimum cost in a network graph N will
ensure that the graph becomes Eulerian and the number of edges (which
are) duplicated are minimized at the same time. This has been illustrated in
Fig. 7.5.4.
An efficient solution of the Chinese Postman Problem in a directed graph G
is thus reduced to the following steps:
414 Eulerian Graphs & the Chinese Postman Problem
0
8
2+ 9 7 0
4
3-
1+
1 6
1+
The Number of paths coming out of 4 &
10 should be exactly equal to the 10 1-
number of paths going in 1, 6, & 9
2 5
0 0
1 3
4 0
10
9 ∞ 1
∞ 1
4 ∞
The path requirement can be fulfilled 3
by finding maximum flow in this
network graph: edge capacities are s 6 1 t
indicated with each edge
1
10 ∞
∞
2
∞
9
∞ 1
∞ 1
4 ∞
3
Maximum flow tells us which path
coming from 4 &10 should terminate at
s 6 t which vertex (1, 6, or 9) to make the
1 graph Eulerian
1
10 ∞
∞
2
∞
9
3. Convert bipartite graph B into a network flow graph after adding ver-
tices s & t. The capacity of an edge from s to an orange vertex x is
equal to ∆(x). The capacity of an edge from a red vertex y to t will
be equal to ∆(y). The costs of these edges will be zero. The cost of
any other edge in N will be the corresponding weight in the bipartite
graph; the capacity of these edges will be infinite.
Problem 7.2.2. Show that a necessary & sufficient condition for a directed
graph D to have a solution to the Chinese Postman Problem is that graph D
should be strongly connected; it means that there should be a path between
every pair of vertices in graph D.
Problem 7.2.3. We show a directed graph in the top left diagram of Fig. 7.5.5.
Here there are four vertices with ∆ negative (shown in orange color) while
416 Eulerian Graphs & the Chinese Postman Problem
0
8
2+ 9 7 0
of
s t t hs 4
C o t Pa 3-
nd s 1+
Fi orte
Sh 1 6
1+
10 1-
1
3 2 5
4 0 0
3
3
Ma 0
6 Mi ximu
5 n im m
um Flo
Co w
10 1 4 st ∞/3 1
6 ∞/5 1/0
9 4 ∞/3
3/0
s 6 1/0 t
1/0
0 10 ∞/1
8 ∞/4
2/0
2+ 9 7 0 ∞/6
9
4
3- l
1+ 1+ ma h
1 6 O pti rap
G
An rian
le
Eu
10 1-
2 5
0 0
3
0
Figure 7.5.4: We show capacity/cost associated with each edge in the middle
network graph. A maximum flow at minimum cost in this network graph
helps us find how we can convert a directed graph into an Eulerian graph by
duplicating minimum number of existing edges.
The Chinese Postman Problem for Directed Graphs 417
1-
8 2
1 3
1+ 9 7 1- 6
7 6
7 3
4 6 4
4
2+ 4
3
1 6 2
5
1- 1- 3
7 3 4
2
2 2
5 4
0 0 8 1 9
3
1+
We find a shortest path from every
The Difference ∆ between out- vertex with negative ∆ to each vertex
degree and In-degree is indicated with a positive ∆. Note that vertex 4 is
with each vertex for the graph G duplicated in the Bipartite graph?
0
2 8
1 3
7 6
6 0
9 7 0
7 3
6 4
4
4
4 0
3 0
5
2 1 6
3 0
7 3 4
2
4 2 0
2 5
8 1 9
0
3 0
Figure 7.5.5: We show a directed graph G where there are three vertices,
shown in orange color, where the out-degree is less than the corresponding
in-degree. Vertices where the out-degree is larger than the in-degree are
shown in red color. Green vertices have out-degree equal to the in-degree.
We describe how we can convert a directed graph G into an Eulerian graph
H by duplicating minimum number of edges of G.
418 Eulerian Graphs & the Chinese Postman Problem
0 0
8 8
2+ 9 7 3+ 9 7
1+ 1+
4 4
3- 3-
1 6 1 6
1+ 0 0 0
10 1- 10 1-
2 5 2 5
0 0 0 0
3 3
0 0
There are three vertices where out-degree There are two vertices where out-degree is
is larger than in-degree; two vertices where larger than in-degree; two vertices where
in-degree is larger than out-degree in-degree is larger than out-degree
3 1
6
4 6 4 7
4 7
7
5
10 8 10 5 9
9
6
9
Figure 7.5.6: We show two directed graphs which are not Eulerian. We
need to convert these graphs into Eulerian graphs by duplicating minimum
number of existing edges of these graphs.
Problem 7.2.5. Derive the time complexity of the algorithm (that we have
described in the text) which can be used to solve the Chinese Postman prob-
The Chinese Postman Problem for Directed Graphs 419
lem for directed graphs. Compare its value with the time complexity of the
algorithm used for undirected graphs.
Problem 7.2.6. We show a directed graph in the left diagram of Fig. 7.5.7;
please note that the in-degree of every vertex in this diagram is exactly equal
to its corresponding out-degree; thus the graph is Eulerian. The Chinese
Postman is supposed to distribute mail on streets shown in red color only.
We need to find an Eulerian circuit consisting of edges corresponding to red
streets alone; if that is not possible then we should allow him (the Chinese
Postman) to traverse the red edges at least once and come back after travers-
ing a minimum number of edges in this graph. Please describe an efficient
algorithm to solve this problem; show the detailed working of this algorithm
on this graph.
Figure 7.5.7: We show two directed graphs; the graph shown in the left
diagram is Eulerian while the one shown in the right diagram is neither
Eulerian nor it is strongly connected. The Chinese Postman is supposed to
deliver mail in red streets only.
Problem 7.2.8. We show a directed graph G in the left diagram of Fig. 7.5.8.
The Chinese Postman is supposed to start delivering his mail from a specific
420 Eulerian Graphs & the Chinese Postman Problem
point and end his delivery job at another point as shown in the figure. The
postman is supposed to traverse every edge of this graph exactly once. If
this is not possible then he should traverse each edge at least once and also
minimize the number of edges which are traversed more than once. The
right diagram of this figure shows a transformation of graph G into another
graph where certain edges of G are duplicated. Show that an Euler trail is
possible from vertex 8 to vertex 3 in this graph. Design an efficient algorithm
to efficiently solve the above problem. Does the right diagram of Fig. 7.5.8
provide an optimal solution to our problem for the graph shown in the left
diagram? Discuss briefly?
5 5
7 6 7 6
4 4
1 3 1 3
End
End
here
here
10 10
2 9 2 9
8 8
Start Start
here here
Figure 7.5.8: We show a directed graph G in the left diagram; the red
vertices have in-degree smaller than the corresponding out-degree while the
orange vertices have in-degree larger than the out-degree; the green vertices
have out-degree equal to the corresponding out-degree. The right diagram
shows a transformation of graph G into another graph where it is possible to
find an Eulerian Path from vertex 8 to vertex 3.
The Chinese Postman Problem for Directed Graphs 421
0 0
8 8
2+ 9 7 0 2+ 9 7 0
4 4
3- 3-
1+ 1+
1+
1 6 1 6
1+
10 1- 10 1-
2 5 2 5
0 0 0 0
3 3
0 0
Fi
Gr a l
h
n
ap
d
ian tim
M
ler p
in
Eu An O
im
um
0
8
Fl
ow
2+ 9
7 0
0
4
0 3-
1+ 1 1+
0
s 6 t
0
10 1- 0
0
2 5
0
3
0
Figure 7.5.9:
422 Eulerian Graphs & the Chinese Postman Problem
+1
a b a b
s t +2 s t
-1
d c d c -2
F
Eu ind
l e r Op
ian tim
Gr al
ap
h
∞/3
a +1
a b
1/0
2/0 c ∞/2
x y s +2 t
∞/2 -1
1/0
t
2/0
∞/1 d c -2
s
a b
1 ow
Fl
2 1 um
i n im
M
s 1 t
1
1 3
d 1 c
Figure 7.5.10:
0
8 0
8
2+ 9
7 0
2+ 9
7 0
0
4
0 3- 1+ 0
1+ 1 4
s 6 0 3- 1+
0 t 1+ 1
0
s 6 0 t
10 1- 0 0
0 10 1- 0
2
5 0
0
2 5
3 0
0
3
0
Figure 7.5.11:
Chapter 8
Hamiltonian Graphs
8.1 Introduction
8.2 Prior Knowledge
8.3 Hamiltonian Graphs
8.4 Bipartite Hamiltonian Graphs
8.5 Some Theoretical Claims
8.6 A Categorization of Hamiltonian Graphs
424 Hamiltonian Graphs
8.1 Introduction
In this chapter, we shall address the problem of finding a Hamiltonian cycle
and a Hamiltonian Path in a graph. We shall also be considering certain
conditions (necessary and then sufficient) for a graph to be Hamiltonian. We
begin by using constructive proof techniques for certain sufficient conditions;
the same techniques can be used to find a Hamiltonian Cycle in a graph where
a Hamiltonian Cycle is guaranteed (by the sufficient conditions) to exist. Stu-
dents of this topic sometimes get confused between necessary and sufficient
conditions. In case of Eulerian Graphs where we have a neat classification,
we can easily differentiate between necessary and sufficient conditions. In
Hamiltonian Graphs however, we are unaware of such a characterization so
we shall be talking about necessary and sufficient conditions separately.
u5 u4 u5 u4
u6 u3 u6 u3
u1 u2 u1 u2
Summary
Concept Map 8.1. A concept map of certain necessary and sufficient con-
ditions for a graph to be connected, Eulerian, and Hamiltonian.
428 Hamiltonian Graphs
For each of the above two problems it will be useful if the learner actually
draws such graphs and then discovers the answer himself or herself. The k th
power Gk of a graph G is a graph with same number of vertices as in G in
which two vertices are adjacent if and only if they are at most d distance
apart from each other in G. If the diameter of a graph G is k then Gk
will be a completely connected graph and that is why G2 of a star graph is
Hamiltonian. The diameter of a line graph is proportional to the size of the
graph but still G2 is Hamiltonian. It will be interesting to explore if this is
a more general result: G2 of any tree is Hamiltonian? Either show it or find
a counter example.
Let us now try to solve a problem which belongs to the so called critical
activity section. You must solve this problem before moving forward. Please
see Fig. 8.3.1 and Fig. 8.3.2.
Hamiltonian Graphs 429
8.3.1 A Puzzle:
We are given a line graph with p nodes, the starting and end node of the
line graph are designated as u1 and up respectively. The intermediate nodes
are labeled as ui , where 2 ≥ i ≥ p − 1 as shown in Fig. 8.3.1. You are
allowed to insert new edges between u1 and any of the intermediate nodes.
Similarly you can insert edges between up and any of the intermediate nodes.
While inserting edges you should keep the degree of u1 and up exactly the
same. You can not insert an edge between the two terminal nodes otherwise
a Hamiltonian Cycle will immediately be formed (we already have a Hamilto-
nian Path between the two terminal vertices). While inserting an edge from
u1 to an intermediate vertex (and then an edge from up to an intermediate
vertex) try your best that a Hamiltonian Cycle is not formed. The problem
is to find out (by an actual trial and error drawing) the minimum degree of
the two terminal vertices when it will be impossible to resist a Hamiltonian
Cycle. That will be a sufficient condition for this graph to be Hamiltonian.
If, instead of resisting a Hamiltonian Cycle, you are adamant to form one as
soon as the first opportunity arises then what will be the minimum degree
of the two terminal vertices which will guarantee a Hamiltonian cycle?
Now come back to the previous problem where we try to resist a Hamiltonian
Cycle as far as possible. But let us relax the condition that the degree of the
two terminal vertices should be the same. Now the degree of one terminal
vertex will be larger and the other smaller. Again find what will be the
minimum sum of the two degrees (of the terminal vertices) when it will be
impossible to resist a Hamiltonian Cycle. Again this will be a sufficient
condition for the modified graph to be Hamiltonian. You may like to solve
the puzzle for the graph shown in Fig. 8.3.2
u1 up u1 up
u2 u3 u4 u5 u2 u3 u4 u5
A graph where the degree of every vertex An edge between the terminal vertices
is 2 except for two terminal vertices will instantly create a Hamiltonian Cycle
u1 up u1 up
u2 u3 u4 u5 u2 u3 u4 u5
Then
Suppose we add an edge as shown... Now we should not add an edge which
will now create a Hamiltonian Cycle
Figure 8.3.1: A line graph with six nodes. We should add extra edges in this
graph but do our best to avoid a Hamiltonian Cycle.
u v
u v
u v
u v
Figure 8.3.2: Without the red dotted edge there is no Hamiltonian Cycle in
each of these graphs.
432 Hamiltonian Graphs
u5 u4
3
u6 u3
3
u1 u2
u4
u5 u4 u6 u4
2
2
3
3
u6 u3 u5 u2
u1 u2 u1 u3
Figure 8.3.3: We are given three graphs where a Hamiltonian path exists
between two special vertices. The Hamiltonian path between the two vertices
is also indicated by shaded lines in each graph. In the top graph the degree
sum of the two special vertices is equal to the number of nodes in the graph;
in the bottom graphs the degree sum of the two special vertices is less than
the number of nodes in the graph. Please note that a Hamiltonian Cycle
exists in each of these graphs.
Hamiltonian Graphs 433
Apply your algorithm on the graphs given in Fig. 8.3.3 in order to find a
Hamiltonian Cycle in each graph; please note that each of the graphs does
indeed contain a Hamiltonian Cycle (which can easily be found by hit and
trial method). Let us see where and why your algorithm fails and where it
does find a Hamiltonian Cycle. Is this not strange that a graph contains a
Hamiltonian Cycle but your algorithm can not find it?
Let us now describe the details of the algorithm which solves the problem
outlined above. The working of the algorithm is illustrated in Fig. 8.3.4.
It is interesting to note that our algorithm also has a serious short coming:
a graph may have a Hamiltonian Cycle but we can not find it (as shown
in Fig. 8.3.5). Interestingly there may be graphs where the condition (that
deg(u) + deg(v) ≥ p for a pair of non adjacent vertices u & v in G) is not
met yet our algorithm will be able to find a Hamiltonian cycle as shown in
the same figure.
434 Hamiltonian Graphs
u 2 1
v 3 6
(1)
4 5
)
(2
2 1 2 1
3 6 3 6
x x
(3)
x+1 4 5 x+1 4 5
Degree = 2
2
u5 u u5 u
3
u6 uv3 2
u6 uv3
u1 u2 u1 u2
Figure 8.3.5: A graph G shown (left diagram) where the condition (that
deg(u) + deg(v) ≥ p for a pair of non adjacent vertices u & v in G) is not
met and the Algorithm 61 will NOT be able to find a Hamiltonian cycle; but
a Hamiltonian Cycle exists in this graph as shown by blue lines. A graph
G shown where the condition (that deg(u) + deg(v) ≥ p for a pair of non
adjacent vertices u & v in G) is not met yet the Algorithm 61 will be able to
find a Hamiltonian cycle as shown in the right diagram.
436 Hamiltonian Graphs
u1 up
u2 u3 u4 u5
u1 up u1 up
u2 u3 u4 u5 u2 u3 u4 u5
Then
u1 up u1 up
u2 u3 u4 u5 u2 u3 u4 u5
Now we remove the direct edge from u1 to up Now we remove the direct edge from u1 to up
A HAM Cycle will NOT exist in graph G? A HAM Cycle will still exist in graph G?
u1 up u1 up
u2 u3 u4 u5 u2 u3 u4 u5
1. The Hamiltonian Cycle passes through the edge uv. Then there will
certainly be a Hamiltonian Path between the vertex u and vertex v.
Under such conditions we can map this problem to the last problem that
we have discussed except that the actual Hamiltonian Path between u
and v is not provided here. Such a possibility is indicated in the left
diagram of Fig. 8.3.8. Again notice the figure of eight in this diagram.
2. The Hamiltonian Cycle does not pass through the edge uv. Under such
conditions a Hamiltonian Path between u and v may not exist. It will
be instructive to actually draw such a graph. As the edge uv is not
part of the Hamiltonian Cycle so it is possible to remove this edge and
still a Hamiltonian Cycle will exist in G. Such a possibility is shown in
the right diagram of Fig. 8.3.8.
8.3.5 Summary
Let us look at what we have really understood so far. If there is a graph G
in which there are two adjacent nodes u and v such that deg(u) + deg(v) ≥ p
then G is Hamiltonian if and only if G + uv is Hamiltonian. This means
the possibility, that the graph G is not Hamiltonian while the graph G + uv
is Hamiltonian, does not exist. This also means that if we remove the edge
uv even then there will be a Hamiltonian Cycle in G provided there was a
Hamiltonian Cycle in G + uv. The edge uv does not make any difference; its
inclusion does not convert a non Hamiltonian graph into Hamiltonian and
its removal does not convert a Hamiltonian graph into a non Hamiltonian
graph provided other conditions are also met.
u5 u4 u5 u4
u6 u3 u6 u3
u1 u2 u1 u2
2. Extra edges inserted between very pair of non adjacent vertices u and
v where deg(u) + (v) ≥ p. These edges were not part of G. We call
these edges extra edges.
The Hamiltonian Cycle in c(G) will pass through some of the original edges
of the graph. But it may also pass through some of the extra edges belonging
to the second type. How to find a Hamiltonian Cycle which passes through
E(G) alone is an interesting problem. The following algorithm can be used
to find a Hamiltonian Cycle in graph G. It can also be used constructively
440 Hamiltonian Graphs
to prove that if deg(u) + deg(u)v ≥ p for every non adjacent pair of vertices
in a graph G then G is Hamiltonian. This is certainly a tighter sufficient
condition for a graph G to be Hamiltonian as compared to the complete
connectedness of a graph. This sufficient condition was originally discovered
by Ore.
Dirac’s Theorem: A graph G where the degree of each node is more than
or equal to p/2 is Hamiltonian. A formal proof of this theorem can easily be
derived from the above discussion. A constructive proof can also be designed
on similar lines.
Example 1: We show a graph G where the degree of each node is equal
to p/2 in the left diagram of Fig. 8.3.9. The closure of this graph is a
completely connected graph as shown in the right diagram of the same figure.
The Hamiltonian Cycle is also indicated in this diagram; interestingly the
Hamiltonian Cycle passes entirely through extra edges. We apply Algorithm
62 to this graph and show a number of intermediate results in Fig. 8.3.10. We
remove extra edges one by one and each time find a new Hamiltonian Cycle;
the extra edges removed are indicated by dotted lines while a Hamiltonian
Cycle is shown by bold lines in Fig. 8.3.10. Please note that the order in
which extra edges are removed is not very important in this example. Why?
The order will, however, become important in the next example.
u5 u4 u5 u4
u6 u3 u6 u3
u1 u2 u1 u2
Figure 8.3.9: A graph G where the degree sum of each pair of non adjacent
vertices is equal to p; the graph is shown in the left diagram. The closure
of this graph is shown in the right diagram. Note that the closure is a
completely connected graph containing a Hamiltonian Cycle also shown in
the right diagram. Note that the Hamiltonian Cycles passes entirely through
extra edges.
Example 2: We show a graph G in the top left diagram of Fig. 8.3.12; the
degree of some of the nodes is also indicated in this diagram; the degree sum
of a pair of non adjacent vertices is less than 6 in this graph. The closure
of this graph is, however, a completely connected graph as shown in the
top right diagram. The closure of this graph thus contains a Hamiltonian
442 Hamiltonian Graphs
u5 u4 u5 u4
u6 u3 u6 u3
u1 u2 u1 u2
h
ap
gr
e
th
w
Find Hamiltonian cycle (blue
ra
Remove One more Extra edge
ed
R
color) in the remaining graph
u1 u4 u1 u4
u6 u3 u6 u3
u5 u2 u5 u2
h
ap
gr
e
th
w
u6 u3 u6 u3
u5 u2 u2
u5
u3 u1
w
u3 u1
ra
ed
R
u6 u4 u6 u4
u5 u2 u5 u2
Figure 8.3.10: We start with the closure of the graph shown in the last figure.
We apply Algorithm 62 to this graph; remove an extra edge and find a new
Hamiltonian Cycle. We repeat this process until all extra edges are removed.
All extra edges which are removed are shown by dotted lines.
Hamiltonian Graphs 443
u5 u4 u5 u4 u1 u4
u6 u3 u6 u3 u6 u3
u1 u2 u1 u2 u5 u2
u4 u1 u4 u1 u1 u4
u6 u3 u6 u3 u6 u3
u5 u2 u5 u2 u5 u2
Hamiltonian Cycle shown Hamiltonian Cycle shown
in blue. Another Extra Edge Redraw the graph and
remove another Extra Edge in blue. Another Extra
can NOT be removed Edge can be removed
u3 u1 u3 u1 u5 u4
u6 u4 u6 u4 u6 u3
u5 u2 u5 u2 u1 u2
Hamiltonian Cycle, shown
Redraw the graph and
in blue, passes through No
remove the last Extra Edge
Extra Edge
Figure 8.3.11: It is possible to remove more than one extra edge (in a single
step) from the graph. Initially the Hamiltonian Cycle passes through all the
extra edges as shown in the top left diagram. At the end it passes through
the graph edges as shown in the bottom right diagram.
444 Hamiltonian Graphs
4 u5 u4 2 u5 u4 u5 u4
3
u6 u3 u6 u3 u6 u3
u1 u2 4 u1 u2 u1 u2
We start with a graph where Add Extra Edges where the Add Extra Edges where the
degree sum of a few pairs of degree sum condition is satisfied. degree sum condition is recently
vertices is not p This will increase the degree? satisfied. The closure is complete
u5 u4 u5 u4 u5 u4
u6 u1 u6 u1 u6 u1
u3 u2 u3 u2 u3 u2
u5 u4 u5 u4
u6
? u3 u6 u1
u1 u2 u3 u2
Redraw the graph and try to remove Find a different Hamiltonian Cycle in
the last Extra Edge through which the remaining graph as shown in blue
the Hamiltonian Cycle passes color. It passes through one Extra
NOT Possible? Edge; remove other Extra Edges
Figure 8.3.12: While removing Extra edges, one should be very careful about
the order in which they are removed otherwise one may fall into a trap as
shown in the bottom diagrams.
Hamiltonian Graphs 445
1
1
6 2
6 2
5 3 5 3
4
4
6 2
5 3
Cycle. If we now remove the extra edges in the wrong order then we may
end up with a difficult situation as depicted in the bottom right diagram of
Fig. 8.3.12. Why this order has suddenly become important and why it was
not important in the graph of Fig. 8.3.9? It is essential for you to answer
these questions before moving forward.
u5 u4 u5 u4
4 2
4
3 4
u6 u3 u6 u3
4
u1 u2 u1 u2
u5 u4 u5 u4
u6 u3 u6 u3
u1 u2 u1 u2
Figure 8.3.14: The top left diagram shows a graph in which the degree sum
of every pair of non adjacent vertices is not equal to (or larger than) the
number of nodes in the graph. The closure of this graph is still a completely
connected graph as shown in the bottom left diagram. The bottom right
diagram shows that a difficult situation arises if we remove extra edges in
the wrong order.
vertex u and vertex v belong to different partites. You should also keep the
degree of u1 and up exactly the same while inserting new edges. You can not
insert an edge between the two terminal nodes otherwise a Hamiltonian Cycle
will immediately be formed (we already have a Hamiltonian Path between the
two terminal vertices). While inserting an edge from u1 to an intermediate
vertex (and then an edge from up to again an intermediate vertex) try your
best that a Hamiltonian Cycle is not formed. The problem is to find out (by
an actual trial and error drawing) the minimum degree of the two terminal
vertices when it will be impossible to resist a Hamiltonian Cycle. That will be
a sufficient condition for this graph to be Hamiltonian. Please see Fig. 8.4.1,
Fig. 8.4.2 and Fig. 8.4.3
u1 u2
u3 u4
u5 u6
u7 u8
u1 up
u2 u7
u3 u6
u4 u5
u1 up
u2 u7
u3 u6
u4 u5
Figure 8.4.2: Here we show a Hamiltonian Path between two vertices in the
bipartite graph shown in Fig. 8.4.1. Notice the bi-colored vertices; similar
color vertices belong to one partite while all the other color vertices are in
the second partite. In a bipartite graph, an edge uv exists provided u and
v belong to different partite. Edges not allowed in a bipartite graph are
indicated in both the diagrams in the form of dotted lines.
Some Theoretical Claims 449
u v
u v
u v
u v
Figure 8.4.3: A bipartite graph of sizes 8, 10, and 12. You can easily think
of sufficient condition for a bipartite graph to be Hamiltonian.
450 Hamiltonian Graphs
k
k+1
k u k
G
k 2
Degree of every vertex k
larger than or equal to k
x
k k
k k+1
v
If p+1 closure is
complete in graph G+x
then there will be a HAM
u cycle in the graph G+x.
k k
G
k
Then there will be a HAM cycle
passing through vertex u & v
k k
v provided u and v are adjacent in G
3 4 5
1 4 2
1 4 2
1 4 2
2 2 3
x x x
2 5 2 5 2 5
4 4 4
3 4 5
3
3 2 3 2 3
Connect x to 4 and 5. Connect 4 & 5. Degree
Degree sum of 4 and 5 Connect vertices where
sum of other pairs
becomes 6 degree sum is equal to 6
becomes 6
2 5 5
1 4 1 4 1 4 2
2 5 4
5 x x
2 5 2 5 2 5
4 5 4
2 5 5
5 4
3 2 3 3
There is a HAM path All vertices in graph G+x All vertices in G are
between vertex 4 and 5 are now completely completely connected
connected
k+1
k k+1
k
G
Degree of every k p k+1
vertex larger than
or equal to k x
k k+1
k k+1
AH
AM
pat
h in
G
Example No. 2: We show a chain graph G in Fig. 8.5.4 with end vertices
3 and 5. We know that a HAM path exists between vertex 3 and 5 in this
graph. We add a vertex x to G and connect it to vertex 3 and 5 as shown in
the top left diagram. The p + 1 closure of graph G + x (having p + 1 vertices)
is not complete; thus we can not confirm that a HAM path exists between
vertex 3 and 5 in G. Then we connect vertex x to every vertex in G; now the
p + 1 closure of G + x becomes complete as shown in the rest of the diagrams;
thus confirming that a HAM path do exist in this graph.
Claim No. 4: Assume that in a graph G, the p − 1 closure is complete then
there will be a HAM path in G.
Claim No. 5: Given a graph G (having p vertices) with three vertices u, v,
and w such that u is adjacent to v and v is adjacent to w. We add a vertex x
and a vertex y to G such that vertex x is connected to vertices u and v of G,
Some Theoretical Claims 453
2 2 2 3 3 2
1 4 5 1 4 5 1 3 4
4
2
5
2 3 4
2 x 2 2
x x
2 2 2
3 3 3
A HAM path exists between We add vertex x to G but
We connect 2 and 4 as the
3 and 5 but the 6 closure of now connect x it to every
degree sum is 6
this graph is not complete vertex of graph G
5 5 5 3 5 3
1 4 5 1 4 5
5 5
2 x 2 x
5 3
3 3
u
k+1
k 2 k
k
G x
Degree of every k k+2
vertex larger than v k
or equal to k
y
k
k 2
k
k+1
w
k
v k
G
There will be a HAM cycle
k passing through u then v
and then w in G provided u
k
w is adjacent to v and v is
adjacent to w.
p/2 p/2
u p/2 u p/2
G
Degree of every
vertex is exactly p/2
p/2
2 G
p/2
x
v p/2 v p/2
p/2 p/2
Figure 8.5.6: We show a regular graph G; the degree of every vertex is p/2.
We add a vertex x to G and connect x to vertex u and v in G as shown in
the right diagram.
Problem 8.3.6. Draw a graph which is not Hamiltonian (that means it does
not satisfy the sufficient conditions) but in which there is a Hamiltonian path
between every pair of non adjacent vertices. We call it category D graphs.
Problem 8.3.7. Draw a graph which is not Hamiltonian (that means it does
not satisfy the sufficient conditions) but in which there is a Hamiltonian path
between every pair of vertices. We call it category E graphs.
Figure 8.5.7: You may find some graphs here which you were supposed to
draw in earlier problems.
458 Hamiltonian Graphs
u5 u4
u6 u3
u1 u2
k k+1
k k+1
k
p k+1
G
k
x
k k+1
k+1
k+1
u k+1
k x u k
2
2 v
k k+1
x 2
k+1 k y k+1 k
v w
Figure 8.6.2: We show here a number of regular graphs where the degree of
each vertex is exactly p/2 and p is even.
Chapter 9
Strongly Connected Directed Graphs
and Tournaments
Introduction
We shall talk about some specific features of directed graphs in this chap-
ter. As with un-directed graphs, directed graphs can also be classified into
different categories. We shall be talking about strongly connected directed
graphs, directed acyclic graphs, unilaterally connected directed graphs and
tournaments. An undirected graph is known as strongly orient-able if it has
the potential of becoming a strongly connected directed graph. We shall
discuss and prove (again using constructive proof strategies) necessary and
sufficient conditions for an undirected graph to be strongly orient-able. We
shall also describe efficient algorithms to find if a directed graph is strongly
connected, and if not then what are its strongly connected components, and
how they are connected.
Concepts
As you can see in Concept map 1, directed graphs can also be classified
on the basis of connectedness. A directed graph where you can reach any
vertex from any other vertex is known as a strongly connected graph. A
directed graph having no cycles is called an directed acyclic graph or a DAG.
If we remove directions from a directed graph D then the resulting graph
will become undirected and is known as the underlying undirected graph.
If the underlying undirected graph of D is connected then D is known as a
weekly connected directed graph. A directed graph D is unilaterally connected
provided for every pair of vertices u and v, there is a path from u to v or
a path from v to u. In a strongly connected directed graph there is a path
from u to v and a path from v to u. That means a strongly connected graph
is always a unilaterally connected graph but not vise versa. Both strongly
connected as well as unilaterally connected directed graphs are always weakly
connected but again it may not be true the other way round. Fig. 9.1.1
shows three different directed graphs. One of them is strongly connected.
The other is unilaterally connected but not strongly connected. The third is
neither unilaterally connected nor strongly connected. One of these graphs
is acyclic while the rest are cyclic. Please try to pinpoint which one is which.
When we put directions in an un-directed (or bi-directed) graph G then
it becomes a directed graph D. Not all un-directed graphs can be con-
verted into a strongly connected directed graph. Those which can be con-
verted into a strongly connected graph are known as strongly orient-able
un-directed graphs. Un-directed graphs which can be converted into a uni-
laterally connected directed graph are known as unilaterally orient-able un-
directed graphs. Completely connected un-directed graphs are both unilater-
ally orient-able and strongly orient-able. This means that if we intelligently
put directions in a completely connected un-directed graph then the result-
ing directed graph will be strongly connected (or unilaterally connected). It
is also possible to put directions in a completely connected graph in such
a manner that the resulting directed graph is acyclic, and is therefore not
strongly connected. A directed graph formed by putting arbitrary directions
in a completely connected undirected graph has a special name that is a
tournament. It is obvious that a tournament directed graph may be acyclic
Concepts, Properties & Actions 467
2 2
1 3 1 3
4 4
6 5 6 5
2
1 3
4
6 5
Figure 9.1.1: Three different directed graphs. One of them is strongly con-
nected.
Problem Set 9.1. A graph consisting of six vertices is shown in Fig. 9.1.2.
Only two edges in this graph are directed while the rest are left un-directed
or bi-directed. You are supposed to put directions in these undirected edges
in order to fulfill some objective as defined below. After putting appropriate
directions draw the directed graph D.
Problem 9.1.1. The resulting graph should be acyclic and not unilaterally
connected.
Problem 9.1.2. The resulting graph should be acyclic and unilaterally con-
nected.
Problem 9.1.3. The resulting graph should be cyclic and not unilaterally
connected.
Problem 9.1.4. We have not asked you to draw a graph which should be
acyclic and strongly connected. Why?
468 Strongly Connected Directed Graphs and Tournaments
Problem 9.1.5. The resulting directed graph should be cyclic and strongly
connected.
Problem 9.1.6. The resulting directed graph should be cyclic but not strongly
connected.
Problem 9.1.7. The resulting graph should be unilaterally connected but
not strongly connected.
Problem 9.1.8. The resulting graph should be unilaterally connected, not
strongly connected but cyclic.
2
1 3
6
4
Figure 9.1.2: A graph in which only two edges are directed while the rest are
left un-directed. You are supposed to make it a directed graph by putting
directions on un-directed edges in order to fulfill certain objectives
Problem Set 9.2. Draw a directed graph D in order to fulfill the following
objectives:
Problem 9.2.1. A directed graph in which every node lies on a directed
cycle but the graph is not strongly connected. The directed graph may not
be weekly connected.
Problem 9.2.2. A directed graph in which every node lies on a directed
cycle but the graph is not strongly connected. The directed graph should be
weekly connected.
Problem 9.2.3. A directed graph in which if there is a path from u to v
then there is a path from v to u for every pair of vertices u and v in D, and
D is not strong. Will the resulting graph always be unilaterally connected?
Discuss briefly.
Concepts, Properties & Actions 469
Problem 9.2.4. A weekly connected directed graph in which for every pair
of vertices u and v, if there is a path from u to v then there is no path from v
to u. Will the resulting graph be strongly connected, unilaterally connected,
or acyclic? Discuss briefly.
Action Items
Let us now discuss some of the action items or so called transformations.
The square of a graph D is another graph (known as) D2 in which there is
an edge from vertex u to vertex v provided there is a two edge path from
u to v or there is an edge (that is a one edge path) from u to v in D.
This definition is almost the same as given in the last chapter for un-directed
graphs. The Reachable Relation (or the transitive closure) of a directed graph
D is another directed graph in which there is an edge from vertex u to vertex
v provided v is reachable from u in D. The Reachable Relation graph of D
can be represented by an adjacency matrix A in which A(u, v) = 1 provided
there is a directed path from u to v in D, and it is zero otherwise. The
Reachable Relation matrix of an undirected connected graph will contain all
1’s. Similarly the Reachable Relation matrix of a strongly connected directed
graph will also contain all 1’s. The transpose of a directed graph D is another
directed graph E in which there is an edge from u to v if and only if there is
an edge from v to u in D. We shall introduce more action items as the need
arises.
Properties
A directed graph D is Hamiltonian provided there is a spanning cycle in the
graph. We know that a spanning cycle passes through every vertex exactly
once. A Hamiltonian Path or a spanning path spans every vertex only once
but is not closed so you can not come back from where you have started.
An Eulerian Trail is a closed trail which spans all edges exactly once. An
Eulerian Trail may pass through a vertex several times but it should pass
through every edge exactly once. A closed spanning walk in a directed graph
D passes through every node of D and comes back from where it has started.
As we know in a walk a node as well as an edge may be traversed several times.
A directed graph D is n − cyclic if it contains a directed cycle consisting of
n nodes where n ≤ p. If n = p then the graph D is Hamiltonian. A directed
graph D is transitive if and only if there is an edge from u to v provided
470 Strongly Connected Directed Graphs and Tournaments
there is an edge from u to any vertex w and an edge from w to v for every
pair of vertices u and v in D.
Problem Set 9.3. For the directed graph D shown in Fig. 9.1.3, try to
answer the following:
Problem 9.3.3. Draw the transpose of the graph. Is the transpose also
strongly connected? Why?
2
1 3
4
6 5
1. Hamiltonian Cycle
2. Eulerian Trail
4. n-cyclic
5. Transitive.
Let me now pick one action item, that is, the Reachable Relationship of a
directed graph. When a graph is strongly connected then it is possible to
reach every vertex from every vertex in that graph. This directly implies that
the Reachable Relation matrix of a strongly connected graph will contain all
1’s (see Concept map 9.2). Does it also imply that if the Reachable Relation
matrix of a graph contains all 1’s then the graph is strongly connected? I
think it is quite obvious that the answer is yes. A directed graph which is
Hamiltonian contains a Hamiltonian Cycle; this implies that every vertex is
reachable on this cycle, and thus such a graph will be strongly connected.
Is it the other way round also, that is every strongly connected graph is
Hamiltonian? I think you should be able to find a counter example quite
easily. Please do it before moving forward. A strongly connected graph may
not be Hamiltonian but it perhaps should always contain a cycle of length
n where n < p. Why it can not be an acyclic graph? Because otherwise it
will not be possible to reach from v to u if it is possible to reach from u to v.
Thus a strongly connected graph should never be acyclic. Again it is not the
other way round that is every cyclic graph may not be strongly connected
(we have already witnessed this in a problem set).
Let us now focus on Eulerian directed graphs. In such graphs it is possible to
find a Eulerian trail; a trail passes through every edge exactly once but may
pass through a vertex several times. As every vertex is visited at least once
and we come back from where we have started thus every Eulerian graph
472 Strongly Connected Directed Graphs and Tournaments
will be strongly connected but not vise versa. After looking at Hamiltonian
or Eulerian graphs which are quite restrictive in nature, let us look at a
relatively more general graph D which contains a closed spanning walk. We
know that it is possible to traverse a node or even an edge several times in
a walk. We also know that in a strongly connected graph it is possible to
move from any vertex to any other vertex; it means that you can always
come back to a vertex from where you have started; that will in fact be a
closed walk, it may, however, be not a closed spanning walk. Let us call
this a closed walk U . As U does not span all vertices thus there will be a
vertex u in U which will be connected to a vertex v of D which is not part
of U (why?). We can start from u, go to vertex v; it will still be possible
to come back to vertex u; This will result in another closed walk W which
when merged in U gives us a closed walk with more vertices that in U . We
repeat this process until the closed walk becomes a closed spanning walk (see
Fig. 9.2.1). Let us summarize of what we have gained so far. A graph D
is strongly connected if and only if we can find a closed spanning walk in D
or D is strongly connected if and only if the Reachable Relationship of D
contains all 1’s.
u v
1 2 7 1 2 7
6 U 3 8 6 W 3 8
5 4 9 5 4 9
If the relation matrix contains all 1’s then the graph D is strongly connected
otherwise it is not.
Note that this algorithm is directly related to the (constructive) proof which
proves that a directed graph D is strongly connected if and only if D contains
Strongly Connected Directed Graphs 475
a closed spanning walk. The proof was earlier presented informally in this
section. It will again be interesting to derive the time complexity of this
algorithm and to make it more efficient if possible.
Let us discuss strongly connected graphs in terms of one more property
(transitive) and one more transformation (transpose). Is a strongly connected
graph D transitive? Should it never be transitive? If it is transitive then will
it be a special strongly connected graph? If a strongly connected graph D
is not transitive then will D2 be transitive? Under what conditions D2 be
transitive? We know that in a strongly connected graph D it is possible to
reach any vertex from any vertex in D. Now assume that we reverse the
direction of each edge in graph D, that is, we have taken the transpose of D.
Will it be still possible to reach any vertex from any vertex in this graph?
If yes then the transpose of D will always be strongly connected provided
D was strongly connected. We next consider a class of un-directed graphs
(known as strongly orient-able) where if we put a direction intelligently on
each edge then the resulting directed graph will be strongly connected. On
the other hand there are un-directed graphs where it is impossible to convert
them into strongly connected directed graphs. It is interesting (or shocking)
to note that the underlying un-directed graph of a strongly connected graph
may not be strongly orient-able (why?).
The algorithm can also be used as a constructive proof to prove that if there
are no bridge edges in an un-directed graph G then it is possible to convert
G into a strongly connected directed graph D. We have yet to prove that if
there is a bridge edge in a graph G then it is impossible to convert G into
a strongly connected graph D. That will in fact complete the proof of a
theorem which states that a connected graph G is strongly orient-able if and
only if G contains no bridge edges. We have seen in the last chapter that G2
of a line graph is Hamiltonian and is therefore strongly orient-able. We have
also seen that G2 of a star graph is also Hamiltonian. It will be worthwhile
to conjecture that if G is connected but not strongly orient-able then G2 is
certainly strongly orient-able. You should either prove this conjecture or find
a counter example.
which should trouble your mind. Answer these questions before moving
forward. There are certain nodes in a DAG which have some special features
not noticed in other directed graphs. For example every DAG will have at
least one source node and at least one sink node in addition to other ordinary
(sometimes called intermediate nodes). A source node is defined as a node
with no in degree while a sink node has no out degree. An intermediate node
has both in degree as well as out degree.
It is interesting to note that the underlying undirected graph of a DAG will
be an undirected graph. Looking from the other way round you can start
from any undirected graph G and put a direction on each edge such that
the resulting directed graph becomes a DAG (in fact it can be converted
into several different DAG’s). If the undirected graph is a tree then we may
put an arbitrary direction on each edge and the resulting directed graph
will guaranteed to be a DAG. On the other hand if an un-directed graph is
completely connected then we have to be careful in selecting the direction of
an edge in order to transform G into a DAG. Try your luck and design an
algorithm which can transform an un-directed completely connected graph
into a DAG (see Fig. 9.4.4). We shall shortly provide an efficient algorithm
to solve this problem; as this DAG is derived from a completely connected
un-directed graph so it is known as a tournament DAG. A tournament DAG
is always transitive (why?). Perhaps you may like to find a non tournament
DAG which is also transitive?
Let us look at a DAG along with an action item, the transpose of a DAG.
Will the transpose of a DAG always a DAG? Why? It will obviously contain
the same number of vertices and exactly the same number of edges, the
direction of each edge is reversed only. But then a DAG consists of a source
vertex, a sink vertex, and a number of intermediate vertices. What happens
to these vertices when we take a transform of a DAG? A source node has no
in-degree but when we reverse directions it will have no out-degree so it will
be transformed into a sink as shown in Concept map 9.3. Similarly the sink
of the original DAG will be transformed into a source in the transpose of the
DAG. How about if we take the square of a DAG? Will the resulting graph
also a DAG? How the roles of different vertices change or remain the same
in the square graph? Will the resulting graph be transitive?
If we draw a Reachable Relation matrix A for a DAG, how the matrix will
tell us that it represents the reachable relation for a directed acyclic graph?
478 Strongly Connected Directed Graphs and Tournaments
We know that a directed graph is acyclic if and only if a DFS of the directed
graph does not produce a back edge. We also know that if a directed graph
is a DAG then the vertex with maximum finishing time will always be (one
of) the source vertices of the DAG. More importantly it is beneficial to sort
the vertices of the DAG on the basis of decreasing finishing times. In this
so called topological sort, the vertices of the DAG can be so arranged on a
horizontal line such that all directed edges go from left to right. The source
vertex will always be on the extreme left while the sink will be on the extreme
right on the horizontal line (see Fig. 9.3.2). This visualization of a DAG may
encourage us in devising a scheme to convert an un-directed graph into an
acyclic directed graph; the directed edges should always go only from left to
right. It is interesting to note that the vertices of a cyclic graph can not be
so arranged on a horizontal line such that all edges go in only one direction.
Problem Set 9.4. For the directed acyclic graph D shown in Fig. 9.3.1,
do the following:
Problem 9.4.1. Perform a DFS from any vertex u in D; identify tree edges,
back edges, forward edges, and cross edges if any.
Problem 9.4.2. Find the vertex of maximum finishing time.
Problem 9.4.3. Start a DFS from a vertex other than u and again locate a
vertex with the maximum finishing time.
Directed Acyclic Graphs (DAG’s) 479
Concept Map 9.3. A concept map showing the concept of a directed acyclic
graph, a couple of properties and two action items which are Reachable
Relation and transpose of a DAG.
480 Strongly Connected Directed Graphs and Tournaments
Problem 9.4.4. Repeat the above three steps (1, 2, & 3) on the same graph
with one change; reverse the direction of the edge between vertex 2 and
vertex 4. Discuss the outcome?
2
1 3
4
6 5
Problem 9.5.2. Find the source and the sink node in a DAG.
Problem 9.5.3. Find if a given DAG has a node u such that you can reach
every other vertex of the DAG from u.
Problem 9.5.4. Find if a DAG has a node u such that you can reach u from
every other vertex of the DAG.
Problem 9.5.5. Find if a given directed graph (not necessarily a DAG) has
a node u such that you can reach every vertex from u.
Algorithm 67: Check if D is a DAG; find the source and the sink.
input : A directed graph D
output: Yes/No; if yes then the source and the sink vertices
1 Apply DFS on graph D. If no back edge then D is a DAG;
2 Source vertex will be the one with maximum finishing time;
3 Sink vertex: You find out;
You should try to solve these problems yourself before moving forward.
Algorithm 67 used DFS as a bridge to solve a couple of problems. The
source vertex can be found directly by identifying the vertex with maximum
finishing time. It is not possible to find the sink node directly. Perhaps you
should take the transpose of D and then again perform a DFS now on the
transpose of D. Look at Concept map 9.3 for further hints.
Algorithm 68: Check if D is a DAG; find the source and the sink.
input : A directed graph D
output: Yes/No; if yes then the source and the sink vertices
1 Find a Reachable Relation R for the given graph D. If all diagonal
entries in R are zero then D is a DAG;
2 Source: Vertex with maximum reachability in R?;
3 Sink: Vertex with zero reachabiliy?;
You might have noticed that Algorithm 68 is much more costly than Algo-
rithm 67. This is because the Reachable Relation Matrix R is expensive as
compared to a single DFS. But then the Reachable Relation provides much
more information about a graph. Using a Reachable Relation Matrix we can
easily find if there is a node u in D from where it is possible to reach every
vertex of D. In fact it can answer this question even if the graph D is not
acyclic. Algorithm 67 can also be slightly modified to locate a vertex u in
a special DAG from where it is possible to reach every other vertex. First
we can find the vertex with maximum finishing time; this will be a source
vertex. Now we can start a new BFS from this very vertex; if all vertices of D
are spanned by the BFS then the DAG possesses the desired characteristics
(Does it mean that D is strongly connected?). How about if instead of a
DAG we have an ordinary directed graph which may contain cycles? There
482 Strongly Connected Directed Graphs and Tournaments
3/4
8/9 2/5
7/10 1/6
Start
Node
11/12
Figure 9.3.2: A depth first search is conducted on the directed acyclic graph
D starting from a vertex labeled as start node as shown in the top diagram.
The start/finishing times of each vertex is also indicated here. Vertices of D
are topologically sorted on the basis of decreasing finishing times as shown
in the bottom diagram. The directed graph D is acyclic thus all edges move
from left to right in the bottom diagram. A direct edge between two consec-
utive vertices is missing in the bottom diagram thus a Hamiltonian path is
not possible in this graph.
Strongly Connected Components 485
4 7
1 3 5
F
E D
2 6
4 7
1 3 5
D F
E
2 6
The picture is not so rosy for a directed graph which is not independently
connected. For example if you draw the Reachable matrix of the graph shown
in the bottom of Fig. 9.4.1, you will realize the inherent complication; you
can reach all vertices belonging to component E as well as component F
from vertex 1, although vertex 1 belongs to strongly connected component E
(please see Reachable Relation Matrix A in the table shown in Fig. 9.4.2).
The Reachable relation is not symmetric; hence it is not an equivalence
relationship. It is thus no longer trivial to determine vertices in a strongly
connected component in such directed graphs? If somehow we can convert it
back into a symmetric relation without disturbing the connectivity inside any
strongly connected component and without changing the number of strongly
connected components then the problem will become much simpler? This
means that some how we should be able to remove the small 1’s in the
Reachable Relation Matrix A of table in Fig. 9.4.2. You might have noticed
that removing those small ones in the table shown in Fig. 9.4.2 is again
equivalent to removing the edge (3,5) in the bottom diagram of Fig. 9.4.1
thereby converting it again into an independently connected directed graph.
The question is again how to do it?
Let us again consider an independently connected directed D and assume
that we take a transpose of D. It is interesting to note that the graph D is
different from its transpose but the Reachable relation of the two graphs will
be exactly the same as demonstrated by bold 1’s in the two tables of Table
shown in Fig. 9.4.2. If, however, we take the transpose of a directed graph
which is not independently connected then things will be different. We show
the same directed graph D as depicted in the bottom of Fig. 9.4.1 as well
as its transpose on the top of Table in Fig. 9.4.2. The Reachable Relation
Matrix A of D and the Reachable Relation Matrix B of the transpose of
D are also shown in this table. It is quite obvious in the tables that the
reachability within a strongly connected component is not affected by the
transpose transformation (see the bold 1’s in the two tables in Fig. 9.4.2).
The reachability across strongly connected components is certainly affected
by taking the transpose of a directed graph; this is indicated by the small 1’s
in the two tables given in the figure. The problem of determining strongly
connected components in a directed graph is thus reduced to recovering the
bold 1’s (or throwing away the small 1’s) in the Reachable Relation Matrices
of Fig. 9.4.2. A careful look at the two tables will provide the required
answer.
Strongly Connected Components 489
A problem
For each of the graphs shown in Fig. 9.4.3, find the Reachable Relation
Matrix and compare this matrix with the ones given in Table in Fig. 9.4.2
(this comparison will certainly be a useful learning experience). Please note
that:
1. Graph H is the same as graph D in Fig. 9.4.2 except that the edge
(3,5) in D is reversed. It is very different from the transpose of D in
Fig. 9.4.2 except for one similar edge and that is (5,3).
4. Graph K is almost the same as graph I except that the edge (2,6) in
I is reversed in K thus creating a new cycle in the directed graph.
the coming paragraphs. Let us for the time being restrict ourselves to find
one strongly connected component of a directed graph.
Again consider an independently connected directed graph D consisting of
K strongly connected components. We know there are no edges between
strongly connected components in an independently connected graph. You
are supposed to insert extra edges between the k strongly connected com-
ponents such that the new directed graph should still have as many as k
strongly connected components. When you insert edges you will soon realize
that in order to achieve the above mentioned objective you should be care-
ful not to create a cycle otherwise all strongly connected components within
that cycle will collapse into one strongly connected component and the above
condition will be violated. Please recall graph I & K in Fig. 9.4.3. Graph
I has two strongly connected components in spite of the extra edges (2,6)
while Graph K is reduced into one strongly connected component because
of a cycle created by the edge (6,2). This immediately suggests that strongly
connected components in a directed graph are always connected in the form
of a directed acyclic graph (see Concept map 9.2). This further implies that
there will be at least one source and one sink strongly connected component.
In the top left diagram of Fig. 9.4.2, the component E was a source while the
component F was a sink. While in the top right diagram of the same figure,
component F was a source and E was a sink strongly connected component.
Before discussing this algorithm (Algorithm 72), let us describe another algo-
rithm (Algorithm 73) which will also output a strongly connected component
of F . This looks very similar but there are interesting differences between
the two algorithms. A comparison of the two will certainly be an exciting
learning experience.
Strongly Connected Components 491
Graph D Transpose
E
4 7 4 of D 7
1 3 5 1 3 5 F
F
E
2 6 2 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7
1 1 1 1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 2 1 1 1 1
3 1 1 1 1 1 1 1 3 1 1 1 1
4 1 1 1 1 1 1 1 4 1 1 1 1
5 1 1 1 5 1 1 1 1 1 1 1
6 1 1 1 6 1 1 1 1 1 1 1
7 1 1 1 7 1 1 1 1 1 1 1
Figure 9.4.2: The Reachable Relation Matrix A for D is shown on the left. A
similar matrix B for the Transpose of D is shown on the right. Reachability
within a strongly connected component is shown by a bold 1’s.
Strongly Connected Components 493
Graph H Graph I
4 7 4 7
E E
1 3 5 F 1 3 5 F
2 6 2 6
Graph J Graph K
4 7 4 7
E E
1 3 5 F 1 3 5 F
2 6 2 6
for Algorithm 63. What we need to do is to slightly modify the last line
of Algorithm 72 or 73. It should read; if BFS traverses all vertices of the
directed graph D then D is strongly connected.
Problem Set 9.6. Fig. 9.4.4 (top) shows a completely connected undirected
graph. The bottom of this figure shows two directed graph derived from the
top graph by putting a directions on edges of the un-directed complete graph
(such a directed graph is known as a tournament). It is interesting to note
that the directed graph (bottom right) is a directed acyclic graph as all edges
are going from left to right (see Algorithm 69). The bottom left diagram of
this figure is one strongly connected directed graph or component. Please
note that all edges in the bottom left diagram are not directed. You may
put any directions on these edges (indicated by thin lines); the graph still
remains a strongly connected graph. Please solve the following:
1 2 3 41 5
5 5
1 2 3 41 5 1 2 3 41 5
1
Problem 9.6.1. For the two bottom graphs in Fig. 9.4.4 determine the
corresponding strongly connected components and the underlying DAG con-
necting the strongly connected components.
496 Strongly Connected Directed Graphs and Tournaments
Problem 9.6.3. Find and draw all non isomorphic directed graphs derived
from the completely connected un-directed graph shown in the figure. Draw
the corresponding DAG of strongly connected components for each of the
tournament graph that you have drawn.
Problem Set 9.7. Fig. 9.4.5 show three non-isomorphic directed graphs
(tournaments) derived by putting directions on edges of the completely con-
nected un-directed graph of Fig. 9.4.4. The corresponding DAG of strongly
connected components of each tournament graph is drawn in front of it. Note
that the top two directed graphs are not isomorphic to each other yet the
DAG’s of strongly connected components are isomorphic to each other. The
DAG of strongly connected components of the bottom graph is not isomor-
phic to any of the two.
1 2 3 4 5 1 2,3,4 5
1 2 3 4 5 1, 2 4 5
3
1 2 3 4 5 1,2
3,4 5 5
Problem 9.7.5. Derive an exact expression for the number of non isomor-
phic DAGs of strongly connected components of tournaments of size p.
9.5 Tournaments
9.5.1 A Panoramic Picture and a Concept Map
A directed graph obtained by putting directions on edges in a completely
connected un-directed graph is known as a tournament (see Fig. 9.5.1).
The directed graphs that you have studied in Fig. 9.5.1 are in fact all tour-
naments. While playing with tournaments in previous problems, you might
have noticed that a tournament can be strongly connected while another
tournament may be a directed acyclic graph again shown in Fig. 9.5.1 .
Within these two extremes there is a lot of variety of possible tournaments
consisting of various strongly connected components (see Concept map 9.6).
There are several non-isomorphic tournaments possible which are not acyclic
(Fig. 9.5.2) but there is always a unique acyclic tournament for a fixed num-
ber of vertices (why?). Similarly a tournament is transitive if and only if it is
acyclic (why?). We shall study these and many other interesting properties
of tournaments in this section.
Let us start with some very simple properties; a tournament is always uni-
laterally connected. Why? Only because it is a tournament? Recall how a
tournament is constructed from a completely connected un-directed graph
and also recall the definition of a unilaterally connected directed graph?
5 2
1 2 3 4
1 5
5 2
1 2 3 4
1 5
4 3
1 2 3 41 5 1 2 3 41 5
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 11 2 3 4 5
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 1 2 3 4 5
1 2 3 4
1 k
1 2 3 41 k 1 2 3 41 k
1
1
u u
3 4 3 4
2 u 5 2 u 5
1 k 1 k
know that it will have a Hamiltonian path inside it) into a strongly connected
graph is to insert the extra node u is to connect it with the source node of
the DAG and then connect the sink with node u; this will certainly result
in a Hamiltonian graph (why?). We have already considered the possibility
when T is strongly connected; we have also considered the possibility when
T is a directed acyclic graph; but there is a possibility when T is neither
strongly connected nor acyclic. How to cater to that class of graph; it will
be interesting for you to explore?
1 2 3 4 5
1 2 3 4 5 1 2 3 4 5
1. You can find a vertex u such that it has at least one incoming and one
out going edge connecting u to vertices already in the given cycle C.
This possibility is quite similar to the one illustrated in Fig. 9.5.4. You
can certainly find a cycle of length K +1 in this situation by extending
the cycle length of C from k to k + 1.
2. If you can not find a vertex u with the above property then you will
certainly find a vertex u and a vertex v such that (a) all edges to u
from every vertex of C are incoming towards u, (b) all edges from v to
every vertex of the cycle C are out going, and (c) there is an edge from
u to v. Note that if (a) or (b) is not true then we shall end up with
the first possibility. If (c) is not true then the tournament T will not
be strongly connected. This scenario is depicted in Fig. 9.5.5. Now it
is possible to find a cycle of length k + 2 (see the bottom left of Fig.
9.5.5); it is possible to convert this cycle into a cycle of length k + 1
by a simple manipulation (see the bottom right of Fig. 9.5.5and also
Fig. 9.5.6 ).
This completes the constructive proof that you can extend the cycle length in
a strong tournament of p nodes from 3 to p−1. This also provides an efficient
algorithm to find a cycle from length 3 to all the way to a Hamiltonian cycle
of length p − 1.
Problem Set 9.8. It is important for you to answer the following in order
to meaningfully understand the last algorithm that we have described to find
a Hamiltonian cycle in a strong tournament.
Problem 9.8.1. Indicate which vertices are rip vertices and which vertices
are not rip vertices in Fig. 9.5.5.
Problem 9.8.3. Draw a strong tournament in which there are at least 3 rip
vertices in a tournament of size 5 or 6 .
Problem 9.8.4. Prove that there always exists at least one non rip vertex
in a strong tournament.
Problem 9.8.5. How can you efficiently find a non rip vertex in a strong
tournament?
Tournaments 509
5 5 5 5
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
5 5
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
1 is a rip vertex 1 is a rip vertex Not a rip vertex Not a rip vertex
5 5
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Not a rip vertex 2 is a rip vertex Not a rip vertex 2 is a rip vertex
5 5
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Not a rip vertex Not a rip vertex 3 is a rip vertex 3 is a rip vertex
5 5
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
4 is a rip vertex Not a rip vertex 4 is a rip vertex Not a rip vertex
1 2 3 4 5 2 3 4 5
2 3 4 5 2 4 5
2 4 5 2 3 4 5
2 3 4 5 1 2 3 4 5
Find a 4 cycle in the last graph Insert back the last vertex removed
Problem 9.8.6. What will be the overall time complexity of finding a Hamil-
tonian cycle in a strong tournament using the above algorithm.
Problem 9.8.7. Why we insist that a non rip vertex should be removed
instead of a rip vertex?
1 2 3 4
1 5 2 3 4
1 5
1 3 4
1 5 1 2 4
1 5
1
1 2 3 5 1 2 3 4
1
1
5 5
1 2 3 1
4 5 1 2 3 4
1 5
graphs with the above property and the assumption that all other entries in
the matrix are zero; we show in Fig. 9.6.2, such a unilaterally connected
graphs, one having minimum number of edges (left) and the other having
maximum number of edges as shown on the right of this diagram. We shall
soon prove that a unilaterally connected directed acyclic graph D will always
contain a Hamiltonian path inside D. But before that let us try to imagine
what will happen if there are more 1’s then are absolutely essential for a
graph to be unilaterally connected. Assume that in addition to all 1’s on
the upper side of the diagonal there are some additional 1’s as shown (in
bold) in the top diagram of Fig. 9.6.3. Under such conditions one would
expect that the resulting directed graph will be unilateral, as well as cyclic
containing a Hamiltonian path (as before) as shown in the middle diagram
of Fig. 9.6.3. What is quite unexpected is that the Hamiltonian path may
disappear because of the extra 1’s that we have added in the Reachable
Relation matrix as shown in the bottom diagram of Fig. 9.6.3.
Let us summarize our observations before making a number of formal proofs.
We have observed that a unilaterally connected graph contains a Hamiltonian
path if it is acyclic (see Fig. 9.6.2). If there are cycles in the unilaterally
connected graph then there may or may not be a Hamiltonian path inside
the graph (see Fig. 9.6.3). What we have not observed until now is that
whether a unilaterally connected directed graph contains a Hamiltonian path
or not it will certainly contain an open spanning walk. In fact we shall prove
that a directed graph is unilaterally connected if and only if it contains an
open spanning walk.
Let us first find a constructive proof that a unilaterally connected directed
acyclic graph will always contain a Hamiltonian path inside it. We are dealing
with a directed acyclic graph D; thus if we do a topological sort on vertices
of D then the ordering of the vertices of the graph D along a horizontal
line is such that all directed edges will be going from left to right as shown
in Fig. 9.6.2. There will not be any edge going from right to left because
otherwise graph D will be cyclic. We are dealing with a unilaterally connected
directed graph D; thus for every consecutive vertex pair {u, v} in D either u is
reachable to v or v is reachable to u; assume that u is on the left of v in linear
ordering then there must be a direct edge from u to v otherwise the condition
stated above will be violated or there will be an edge going from right to left.
This means there will be a direct edge between every two consecutive vertices
(again from left to right); this would imply a Hamiltonian path in D (it will
516 Strongly Connected Directed Graphs and Tournaments
1 2 3 4 5
1 1 1 1 1
2 1 1 1 1
3 1 1 1 1
4 1 1 1 1
5
1 2 3 1
4 5
1 2 3 14 5
1 2 3 4 5
walk inside D.
3. Some edges are non bridge edges while some are bridges.
We have already considered the first possibility when G is a tree and every
edge of G is a bridge edge. Let us consider the second possibility when every
edge of G is a non bridge edge. If you recall this is a sufficient condition for a
graph to be strongly orient-able. If G is strongly orient-able then it will also
be unilaterally orient-able. Now we shall consider the third possibility when
some edges of G are bridge edges while some other edges are non bridges
as shown in Fig. 9.6.5. If the graph G contains K bridge edges and if we
remove all these K edges then G will be decomposed into k + 1 connected
components. Consider each connected component as a single (super) vertex.
If we put back the bridge edges then these (super) vertices will be connected
in the form of a tree (why?). A tree of ordinary vertices can be unilaterally
Unilaterally Connected Directed Graphs: 519
1 4 5
1 2 4 5
orient-able if and only if the tree is a line graph. A tree of super vertices can
be unilaterally orient-able provided: (1) The tree is a line graph and (2) We
can find a closed spanning walk inside each connected component (why?). So
once again concentrate on a single connected component; no edge inside the
component will be a bridge edge (why?). Thus each connected component
is strongly orient-able (and will subsequently become a strongly connected
component in the directed graph); there will be a closed spanning walk inside
each component. This completes the proof that if a graph G is unilaterally
orient-able then it will contain an open spanning walk. You may prove the
converse as an exercise?
Bibliography
[2] Sara Baase and Allen Van Gelder. Computer Algorithms: Introduction
to Design and Analysis. Addison-Wesley, 2000.
[3] Donald R. Chalice. How to teach a class by the modified moore method.
Amer. Math. Monthly, 102:317–321, 1995.
[8] M. A. Iqbal and Atif Alvi. The magic of dynamic programming. In 2003
International Conference on Engineering Education, 2003.
[13] David Rine. Lectures in patterns - directed design, george mason uni-
versity, 2002.