Graph Theory For B.Sc. CSIT: Prajwal Kansakar
Graph Theory For B.Sc. CSIT: Prajwal Kansakar
CSIT
Prajwal Kansakar
September 6, 2015
Unit 4
Graph Theory
An undirected graph G is an ordered pair (V, E) where V is a set of vertices and E is a set of
undirected edges each of which joins a pair of vertices.
For example, G = (V, E) where V = {a, b, c, d, e} and E = {e1 , e2 , e3 , e4 , e5 , e6 , e7 } is an
undirected graph as given below:
e2
a e1 b
e7 e6 e3
e
e4
e5
c d
The pair of vertices {a, b} that an edge joins are called its endpoints and the vertices a and b are
said to be adjacent.
Loop: An edge that has the same vertex as both its endpoints, is called a loop. For example,
e2 is a loop in the above graph.
Multiple/Parallel Edges: Two or more edges are said to be multiple or parallel edges if
they join the same pair of vertices. For example, e6 and e7 are multiple edges.
Types of Undirected Graphs:
1. Simple Graph: An undirected graph that has neither loops not multiple edges is called a
simple graph. For example, the following graph is a simple graph.
1
2 UNIT 4. GRAPH THEORY
a b
c d
2. Multigraph: An undirected graph that can have multiple edges but not loops, is called
a multigraph. For example, the following graph is a multigraph.
a b
c d
3. Pseudograph: An undirected graph that can have both loops as well as multiple edges
is called a pseudograph. For example, the following graph is a pseudograph.
a b
c d
a b
f
c d
Regular Graph: A simple graph in which every vertex has the same degree is called a
regular graph. If that degree is n, then it is called an n-regular graph.
Isolated Vertex and Pendant Vertex: A vertex of degree zero is called an isolated vertex.
A vertex of degree one is called a pendant vertex. For example in the above graph, f is an
isolated vertex and e is a pendant vertex.
4.1. UNDIRECTED AND DIRECTED GRAPHS 3
Theorem 1 (Handshaking
X Theorem): Let G = (V, E) be an undirected graph with m
edges. Then 2m = deg(v).
v∈V
Proof: Since one edge contributes exactly two to the sum of the degrees of vertices, so the total
1X
number of edges is exactly half the sum of the degrees of all the vertices i.e., m = deg(v)
2 v∈V
X
i.e., 2m = deg(v).
v∈V
Example:
In the graph below, m = 9, deg(a) = 3, deg(b) = deg(e) = 5, deg(c) = 1, deg(d) = 4.
Therefore,
X
deg(v) = 3 + 5 + 5 + 1 + 4 = 18 = 2m.
v∈V
a b
c d
X X X
2m = deg(v) = deg(v) + deg(v).
v∈V v∈V1 v∈V2
X
But deg(v) is even for all vertices v ∈ V1 , so deg(v) is an even number. Therefore, 2m −
v∈V1
X X
deg(v) = deg(v) is an even number, being the difference of two even numbers. Since
v∈V1 v∈V2
X
deg(v) is an odd number for all v ∈ V2 , so deg(v) is an even number which is a sum of
v∈V2
X
odd numbers. Hence there must be an even number of terms in deg(v) (because only the
v∈V2
sum of an even number of odd numbers is even) i.e., the number of vertices in V2 must be even.
Therefore the number of vertices of odd degree is even.
Example:
Here in the above graph, d is the only vertex of even degree and all the other four vertices are
of odd degree. So the number of vertices of odd degree is even.
4 UNIT 4. GRAPH THEORY
A directed graph (or digraph) G is an ordered pair (V, E) where V is a set of vertices and E is
a set of directed edges each of which is associated with an ordered pair of vertices.
For example, G = (V, E) where V = {a, b, c, d, e, f } and E = {e1 , e2 , e3 , e4 , e5 , e6 , e7 , e8 , e9 , e10 }
is a directed graph as below:
a e1
b
e2
e10 e3
e7
c
d
e4
e8 e9 f
e6
e e5
If a directed edge is associated with an ordered pair (a, b), then a is called the initial vertex
and b is called the final/terminal vertex of that edge. Also, we say that a is adjacent to b or b is
adjacent from a.
Loop: A directed edge that has the same initial and terminal vertices is called a loop. For
example, e5 in the above graph is a loop.
Multiple directed edges: Two or more directed edges are called multiple directed edges
if they have the same pair of initial and terminal vertices. For example, e6 and e7 are multiple
directed edges.
Types of Directed Graphs:
1. Simple Directed Graph: A directed graph that has neither loops nor multiple directed edges
is called a simple directed graph. For example, the following is a simple directed graph.
a b
c d
2. Directed Multigraphs: A directed graph that can have loops as well as multiple di-
rected edges is called a directed multigraph. For example, the following graph is a directed
multigraph.
4.1. UNDIRECTED AND DIRECTED GRAPHS 5
a b
c d
Indegree and Outdegree of a vertex: Let x be a vertex of a directed graph G. Then the
indegree of x, denoted by deg − (x), is the number of edges with x as their terminal vertex. The
outdegree of x, denoted by deg + (x), is the number of edges with x as their initial vertex.
For example, in the following directed graph, the indegree and outdegree of each vertex is listed
below:
a b
c
d
Alternate Proof: Since each directed edge has exactly one initial vertex, so each directed
X contributes exactly one to the sum of the indegrees of the vertices. Therefore, |E| =
edge
deg − (v). Similarly, each directed edge has exactly one terminal vertex, so each directed
v∈V
6 UNIT 4. GRAPH THEORY
X contributes exactly one to the sum of the outdegrees of the vertices. Therefore |E| =
edge
deg + (v).
v∈V
1. Complete Graph: The complete graph on n vertices, denoted by Kn , is the simple graph
that has exactly one edge between each pair of distinct vertices. For example, the following
graphs are K3 , K4 and K5 .
K3 K4 K5
n(n − 1)
A complete graph of n vertices has edges.
2
2. Cycle: For n ≥ 3, the cycle Cn is a simple graph consisting of n vertices v1 , v2 , · · · , vn
and n edges {v1 , v2 }, {v2 , v3 }, · · · , {vn−1 , vn } and {vn , v1 }. For example,
C3 C4 C5
3. Wheel: For n ≥ 3, the wheel Wn is the simple graph obtained from the cycle Cn by
adding one new vertex and n new edges connecting this new vertex to all the other n vertices in
Cn . For example,
W3 W4 W5
4.1. UNDIRECTED AND DIRECTED GRAPHS 7
110 111
10 11
100 101
0 1
Q1 011
010
00 01
Q2
000 001
Q3
v1 v2 v3
v7
v4 v5 v6
v1 v5 v3 v7
v4 v2 v6
v1
v6 v2
v5 v3
v4
The set V = {v1 , v2 , v3 , v4 , v5 , v6 } can be partitioned into two subsets V1 = {v1 , v3 , v5 } and
V2 = {v2 , v4 , v5 } such that each edge connects a vertex in V1 with a vertex in V2 as below:
v1 v3 v5
v2 v4 v6
v1 v2 v3 v1 v2 v1 v2 v3
v4 v5 v3 v4 v4 v5 v6
K3, 2 K2, 2 K3, 3
v2 v3
v2 v3
v1
v1 v4 v5
For example is a subgraph of the graph .
Union of graphs: The union of two simple graphs G1 = (V1 , E1 ) and G2 = (V2 , E2 ) is
the simple graph G = (V, E) where V = V1 ∪ V2 and E = E1 ∪ E2 . For example, the union of
the graphs G1 and G2 below is the graph G.
a a a
c b c b c
d G1 e d G2 d G1 U G2 e
Adjacency Lists: A graph with no multiple edges can be represented by using adjacency lists
which specify the vertices that are adjacent to each vertex of the graph.
For example, the undirected graph below is represented using the adjacency list as follows:
a c
f d
e
10 UNIT 4. GRAPH THEORY
b
a
d c
a b
d e
is
0 1 0 1 0
1 0 1 0 1
AG =
0 1 0 1 1
1 0 1 0 0
0 1 1 0 0
4.1. UNDIRECTED AND DIRECTED GRAPHS 11
d
c
is
0 2 1 0
2 0 1 0
AG =
1
1 0 2
0 0 2 1
0 1 0 0 0
0 0 1 0 0
AG1 =
0 0 0 1 0
0 1 0 0 0
1 0 1 0 0
0 0 1 0
2 0 0 0
AG2 =
0
1 0 1
0 0 1 1
12 UNIT 4. GRAPH THEORY
(
1 if vertex vi is an end vertex of edge ej
aij =
0 otherwise
For example, the incidence matrices M1 and M2 of the graphs G1 and G2 are written below:
1 0 0 0 0 0 0 1
1 0 0 1 1
1 1
1 1 1 0 0 0 0 0
0 0 0
M1 =
0 0
M2 = 0 0 1 1 0 1 1 1
1 1 1
0 1 0 1 1 0 0 0
0 1 1 0 0
0 0 0 0 1 1 0 0
Two simple graphs G1 = (V1 , E1 ) and G2 = (V2 , E2 ) are called isomorphic if there exists a
one-to-one and onto function f from V1 to V2 with the property that a and b are adjacent in G1
if and only if f (a) and f (b) are adjacent in G2 for all vertices a and b in V1 . Such a function f
is called an isomorphism.
For example, consider the following graphs G1 and G2 .
G1 and G2 are isomorphic graphs because there exists a one-to-one and onto function f
with f (v1 ) = u2 , f (v2 ) = u4 , f (v3 ) = u3 , f (v4 ) = u1 and f (v5 ) = u5 such that the vertices vi
and vj are adjacent in G1 if and only if the vertices f (vi ) and f (vj ) are adjacent in G2 .
Invariant Property of a Graph: A property of a graph G is said to be an invariant property
if every other graph isomorphic to G also has that property. Some invariant properties of graphs
are:
4.1. UNDIRECTED AND DIRECTED GRAPHS 13
(i) The number of vertices: If two graphs G1 and G2 are isomorphic, then by definition of
the graph isomorphism, we can conclude that the number of vertices in G1 and G2 must
be same.
(ii) The number of edges: If two graphs G1 and G2 are isomorphic, then again by definition
of the graph isomorphism, we can say that the number of edges in G1 and G2 must be
equal.
(iii) The number of vertices of a given degree: If two graphs G1 and G2 are isomorphic,
then the number of vertices of any given degree on both graphs must be equal e.g., if
graph G1 has 2 vertices of degree 3, then G2 must also have exactly 2 vertices of degree
3.
Note: If two graphs G1 and G2 are isomorphic, then by definition of the invariant property,
G1 and G2 both must have the same invariant properties. Hence, by contrapositive argument,
we can say that if G1 and G2 differ in any one of the invariant properties, then G1 and G2 cannot
be isomorphic.
Problems:
Show that the following graphs are not isomorphic.
a.
Solution: The graphs G1 and G2 given above are not isomorphic because the number of
vertices in G1 is 4 whereas the number of vertices in G2 is 5. Therefore, G1 and G2 cannot be
isomorphic.
b.
Solution: The graphs G1 and G2 cannot be isomorphic because the number of edges in G1
is 4 whereas the number of edges in G2 is 5. Thus, G1 and G2 cannot be isomorphic.
c.
14 UNIT 4. GRAPH THEORY
Solution: The graphs G1 and G2 cannot be isomorphic because the graph G1 has 1 vertex
of degree 2 whereas the graph G2 has 2 vertices of degree 2.
d.
Solution: The graphs G1 and G2 cannot be isomorphic because G1 has no vertex of degree
1 but G2 has 1 vertex of degree 1.
Path: Let G be an undirected graph with vertices u and v. Then a path from u to v is a
sequence of edges e1 , e2 , · · · , en of G such that e1 is associated with {x0 , x1 }, e2 is associated
with {x1 , x2 }, · · · , en is associated with {xn−1 , xn }, where x0 = u and xn = v. The length of a
path is defined to be the number of edges in that path.
Circuit: A circuit is a path of length greater than zero that begins and ends at the same
vertex.
Simple Path: A path is called a simple path if it does not contain the same edge more than
once.
Simple Circuit: A circuit is called a simple circuit if it does not contain the same edge
more than once.
Examples:
1. Let G be the following graph.
in the vertex v1 .
Note: When the graph G is simple, then paths or circuits in G can be specified by a
sequence of vertices rather than edges as in the example below. This is because, in a simple
graph, there can be at most one edge between any pair of vertices.
2. Let G be the simple graph as below:
Path: Let G be a directed graph with vertices u and v. Then a path from u to v is a sequence of
edges e1 , e2 , · · · , en of G such that e1 is associated with (x0 , x1 ), e2 is associated with (x1 , x2 ),
· · · , en is associated with (xn−1 , xn ) where x0 = u and xn = v. As for paths in undirected
graphs, the number of edges in a path is called its length.
Circuit: A circuit is a path of length greater than zero that begins and ends at the same
vertex.
Simple Path: A path is called a simple path if it does not contain the same edge more than
once.
Simple Circuit: A circuit is called a simple circuit if it does not contain the same edge
16 UNIT 4. GRAPH THEORY
Theorem 1: There is a simple path between every pair of distinct vertices of a connected
undirected graph.
Proof: Let u and v be two distinct vertices of the connected undirected graph G. Since G
is connected, there is at least one path between u and v. Let x0 , x1 , · · · , xn where x0 = u and
xn = v be a path of least length. We claim that this path of least length is a simple path. To
prove this, suppose that this path is not simple. Then xi = xj for some i and j with 0 ≤ i < j.
Then x0 , x1 , · · · , xi−1 , xj , · · · , xn is a path from u to v obtained from the path x0 , x1 , · · · , xn
by removing the vertices xi , xi+1 , · · · , xj−1 . So the path x0 , x1 , · · · , xi−1 , xj , · · · , xn is a path
4.3. CONNECTEDNESS IN GRAPHS 17
of shorter length than the path x0 , x1 , · · · , xn i.e., x0 , x1 , · · · , xn is not a path of shortest length.
Hence (by indirect proof method) the path x0 , x1 , · · · , xn from u to v must be a simple path.
Connected Components: A connected component of a graph G is a connected subgraph
of G that is not a proper subgraph of another connected subgraph of G. In other words, a
maximal connected subgraph of G is called its connected component.
For example, the graph below has three connected components:
Cut Vertices and Cut Edges: A vertex in G is said to be a cut vertex if removing that ver-
tex and all the edges incident on it produces a subgraph of G with more connected components
than in G.
An edge in G is said to be a cut edge or bridge if removing that edge produces a disconnected
subgraph of G
For example, in the graph below, d and e are cut vertices and {d, e} is a cut edge.
This graph is not strongly connected because there is no path from a to b but it is a weakly
connected graph because the underlying undirected graph is connected.
Strongly Connected Component: The subgraphs of a directed graph G that are strongly
connected but not contained in a larger strongly connected subgraphs are called the strongly
connected components of G. In other words, the maximal strongly connected subgraphs of G
are called its strongly connected components.
For example in the graph below,
Euler Path: An Euler path in a graph G is a simple path that contains every edge of G. For
example, in the graph G below, e, d, b, a, c, d is an Euler path.
Euler Circuit: An Euler circuit in a graph G is a simple circuit that contains every edge
of G. For example, the graph below has an Euler circuit a, b, c, d, b, e, c, a.
4.4. EULER PATHS AND CIRCUITS 19
Theorem 1: A connected multigraph has an Euler circuit if and only if each of its vertices
has even degree.
Proof: Suppose that a connected multigraph G has an Euler circuit. We need to prove that
each of the vertices of G has even degree. Now suppose that the Euler circuit in G start and
ends at a vertex u. If v is any vertex of G different from u then v must be on the Euler circuit
because G is connected and the Euler circuit contains every edge of G. Moreover, each time
v occurs in the Euler circuit, it enters and leaves v by different edges because each edge of G
occurs only once in the Euler circuit. Thus each occurrence of v in the Euler circuit adds two to
deg(v) i.e., deg(v) is even. Finally, since the Euler circuit must end at u, the first and last edges
add two to deg(u) as will any intermediate occurrence if u in the Euler circuit. So deg(u) is
also even.
Conversely suppose that a connected multigraph G has all its vertices of even degree. We
need to show that G has an Euler circuit. So let u be any arbitrary vertex of G. Starting from
u, we construct a simple path that is as long as possible. Since every vertex is of even degree,
we can exit from every vertex we enter so the path can only stop at vertex u. We then have a
simple circuit in G. If this simple circuit contains all the edges of G, then this is an Euler circuit
as required. If not, then consider a subgraph H of G obtained by removing all the edges in the
circuit and the resulting isolated vertices if any. Since both G and the simple circuit have all
their vertices of even degree, so the degrees of the vertices of H are also even. Also, since G is
connected, H has at least one vertex in common with the simple circuit that was removed. Let
v be that vertex. Starting at the vertex v, we can again construct a new simple path. Since all
the vertices of H are of even degree, this path must terminate at vertex v thus forming a simple
circuit. Now this simple circuit can be combined with the previous simple circuit to obtain a
larger simple circuit that starts and ends at vertex u. This process is continued until one obtains
a simple circuit that contains all the edges of G. We would then have an Euler circuit in G.
Example for the 2nd part of above theorem:
20 UNIT 4. GRAPH THEORY
Problems:
Which of the following graphs have an Euler circuit?
(a)
Solution: Here deg(a) = deg(b) = deg(c) = deg(d) = 4 and deg(e) = 6. So the degree
of all the vertices are even and hence the graph must have an Euler circuit. (a, b, c, d, e, a, e, b,
d, c, e, a)
(b)
BEGIN
Subcircuit:= a circuit in H beginning at a vertex in H that also
is an endpoint of an edge of Circuit
H:= H with edges of Subcircuit and all isolated vertices re-
moved
Circuit:= Circuit with Subcircuit inserted at the appropriate
vertex
END
Theorem 2: A connected multigraph has an Euler path but not an Euler circuit if and only
if it has exactly two vertices of odd degree.
Proof: Let G be a connected multigraph that doesn’t have an Euler circuit but has an
Euler path from a to b. If we join the vertices a and b with the edge {a, b}, then this Euler
path becomes an Euler circuit and the degrees of a and b increases by one whereas the degrees
of other vertices remains unchanged. By Theorem 1, the degree of all vertices of this newly
formed graph must be even i.e., the degree of a and b in this new graph must be even i.e., the
degree of a and b in the original graph must have been odd and the degree of all other vertices
must be even. So G has exactly two verices a and b of odd degree.
Conversely suppose that a connected multigraph G has exactly two vertices a and b of odd
degree. If the vertices a and b are joined by another edge {a, b} then this new graph has all the
vertices of even degree and so by Theorem 1, there must be an Euler circuit in this new graph.
If the newly added edge {a, b} is removed from this Euler circuit, then it becomes an Euler path
from vertex a to vertex b in the original graph G.
Theorem 3: A connected multigraph has an Euler path if and only if it has either no
vertices of odd degree or exactly two vertices of odd degree.
Proof: Let G be a connected multigraph which has an Euler path. If this Euler path is an
Euler circuit as well, then by Theorem 1, G has no vertices of odd degree. If this Euler path is
not an Euler circuit, then by Theorem 2, G has exactly 2 vertices of odd degree.
Conversely suppose that a connected multigraph G has no vertices of odd degree. Then all
the vertices of G has even degree and so by Theorem 1, G must have an Euler circuit and hence
an Euler path. If G has exactly two vertices of odd degree, then G must have an Euler path by
Theorem 2.
Problems:
Which of the following graphs has an Euler path?
Solution: (i) Here deg(a) = deg(b) = deg(c) = deg(e) = 3. So this graph doesn’t have
22 UNIT 4. GRAPH THEORY
an Euler path because it has more that two vertices of odd degree.
(ii) This graph has exactly two vertices, a and d, of odd degree. So it has an Euler path.
(iii) Here, deg(a) = deg(c) = deg(i) = deg(g) = 2 and deg(b) = deg(f ) = deg(h) =
deg(d) = deg(e) = 4. So this graph has no vertices of odd degree and therefore it has an Euler
path.
Hamiltonian Path: A Hamiltonian path in a graph G is a simple path that passes through every
vertex of G exactly once.
For example, v1 , v2 , v3 , v4 , v5 , v6 is a Hamiltonian path in the graph below.
Dirac’s Theorem: (Statement only) If Gl isma simple graph with n vertices, n ≥ 3, such
n
that the degree of every vertex in G is at least , then G has a Hamiltonian circuit.
2
Ore’s Theorem: (Statement only) If G is a simple graph with n vertices, n ≥ 3, such
that deg(u) + deg(v) ≥ n for every pair of nonadjacent vertices u and v in G, then G has a
Hamiltonian circuit.
Problems:
Determine whether the following graphs have Hamiltonian circuits:
lnm
Solution: (i) In this graph, the number of vertices n = 6 and so = 3. Now deg(a) =
2
deg(c)l n=m deg(f ) = deg(d) = 3 and deg(b) = deg(e) = 4. So the degree of all the vertices is at
least = 3 and so by Dirac’s Theorem, this graph must have a Hamiltonian circuit.
2
(ii) Here, the number of vertices n = 5. Now the nonadjacent pairs of vertices in this graph
are {a, d} and {a, e}. So deg(a) + deg(d) = 2 + 3 = 5, deg(a) + deg(e) = 2 + 3 = 5 i.e.,
deg(u) + deg(v) ≥ n for all nonadjacent pairs of vertices u and v in G. So by Ore’s Theorem,
G must have a Hamiltonian circuit.
Weighted graph: Graphs that have a number assigned to each edge are called weighted graphs.
For example,
Length of a path: The length of a path in a weighted graph is the sum of all the weights
of the edges in that path. For example, in the previous weighted graph, the length of the path
a, d, b, c is 6 + 5 + 10 = 21.
Weighted graphs can arise in many situations while modeling real-world problems such as
finding the path of shortest distance from one city to another while traveling or finding the best
route to transfer data from one computer to another in a computer network.
Shortest-Path Problem: Given a weighted graph G, the problem of finding a path of least
length between two of its vertices is called the shortest-path problem.
24 UNIT 4. GRAPH THEORY
For example, in the graph below, we can see that the shortest path from a to f is a, b, e, d, f
whose length is 7.
FOR i := 1 TO n
L(vi ) := ∞
L(a) := 0
S := ∅
{The labels are now initialized so that the label of a is 0 and all other labels are ∞
and S is the empty set.}
WHILE z ∈ /S
BEGIN
u := a vertex not in S with L(u) minimal.
S := S ∪ {u}
FOR all vertices v not in S and adjacent to u
IF L(u) + w(u, v) < L(v) THEN L(v) = L(u) +
w(u, v)
{This adds a vertex to S with minimal label and updates the
labels of the vertices not in S.}
END
Dijkstra’s algorithm to find this path of shortest length proceeds iteratively by first finding the
length of a shortest path from a to the first vertex, then the length of a shortest path from a to
the second vertex, and so on, until the length of a shortest path from a to z is found. For this,
the algorithm labels each vertex v of G by L(v) which denotes the length of path from a to v. It
also maintains a set S of vertices whose final shortest path length from the vertex a have already
been determined.
The algorithm proceeds stepwise as follows:
STEP 2: Set S = ∅.
STEP 4: Let u be a vertex not in S such that L(u) is the minimum. Set S := S ∪ {u} and for each
vertex v adjacent to u and not in S, if L(u) + w(u, v) < L(v), then change the value of
L(v) to L(u) + w(u, v), otherwise don’t change the value of L(v). Proceed to STEP 3.
The algorithm terminates when z is a member of S and the value of L(z) is then the length of a
shortest-path from a to z.
Examples:
Find the path of shortest length from a to z in the following weighted connected simple graphs:
(i)
Solution:
(I) Set L(a) = 0 and L(v) = ∞ for all other vertices.
(II) Since S = ∅ and L(a) = 0 is the minimum among all the vertex labels, set S =
S ∪ {a} = ∅ ∪ {a} = {a}. Now the vertices adjacent to a and not in S are b and c. We
have L(a) + w(a, b) = 0 + 4 = 4 < L(b) = ∞, so we change the value of L(b) to 4. Also,
L(a) + w(a, c) = 0 + 2 = 2 < L(c) = ∞, so we change the value of L(c) to 2.
26 UNIT 4. GRAPH THEORY
(III) For vertices other than a, since L(c) is the minimum, set S = S ∪ {c} = {a} ∪ {c} =
{a, c}. Now, the vertices adjacent to c and not in S is e. We have L(c) + w(c, e) = 2 + 3 = 5 <
L(e) = ∞, so we change the value of L(e) to 5.
(IV) For vertices other than a and c, since L(b) is the minimum, set S = S∪{b} = {a, c, b}.
The vertices adjacent to b and not in S are e and d. We have L(b)+w(b, e) = 4+3 = 7 > L(e) =
5, so we do not change the value of L(e). Also, L(b) + w(b, d) = 4 + 3 = 7 < L(d) = ∞, so
we change the value of L(d) to 7.
(V) For vertices other than a, b and c, since L(e) is the minimum, set S = S ∪ {e} =
{a, c, b, e}. The vertices adjacent to e and not in S is z only. We have L(e) + w(e, z) = 5 + 1 =
6 < L(z) = ∞. So we update the value of L(z) to 6.
(VI) For vertices other than a, b, c and e, since L(z) is the minimum, set S = S ∪ {z} =
{a, c, b, e, z}. Since z ∈ S, so the algorithm terminates with the path of shortest length from a
to z as a, c, e, z and its length 6.
4.6. SHORTEST-PATH PROBLEMS 27
(ii)
Solution:
28 UNIT 4. GRAPH THEORY
Traveling Salesman Problem (TSP): Given n number of cities and the distance between
each pair of those cities, the traveling salesman problem is to find a path that the salesman should
travel so as to visit every city precisely once and return home, with the minimum distance
traveled. Therefore in graph-theoretic terms, the traveling salesman problem is equivalent to
finding a Hamiltonian circuit that has minimum total weight in a weighted complete undirected
graph.
Solving TSP: The total number of different Hamiltonian circuits in a complete undirected
(n − 1)!
graph of n vertices is . So, theoretically, the TSP can always be solved by finding these
2
(n − 1)!
different Hamiltonian circuits, finding the total weight of each of those circuits and
2
then choosing a circuit with the least weight.
(n − 1)!
But practically, this method is very time consuming because for large n, finding dif-
2
(n − 1)! 24!
ferent Hamiltonian circuits is very inefficient. For example, if n = 25, then = ≈
2 2
23
3.1 × 10 . Assuming that it take just one nanosecond (10−9 second) to examine each Hamil-
tonian circuit, a total of approximately ten million years would be required to find a minimum-
length Hamiltonian circuit.
Therefore TSP are practically solved using approximation algorithms which do not necessarily
produce the exact solution to the problem but instead produce a solution that is close to the exact
solution in a reasonable period of time.
Planar Graph: A graph is said to be planar if it can be drawn on a plane in such a way
that no edges cross one another except at common vertices. Such a drawing is called a plane
representation of graph.
For example, the complete graph K4 in figure 1 below is a planar graph because it can be drawn
as in figure 2 such that no edges intersect.
However, the complete graph K5 is not a planar graph. The complete bipartite graph K3,3
is also not a planar graph.
Theorem 1: Let G be a connected planar simple graph with e edges and v vertices. Let r
be the number of regions in a planar representation of G. Then r = e − v + 2. (Euler’s Formula)
30 UNIT 4. GRAPH THEORY
Elementary Subdivision: The process of removing an edge {u, v} and adding a new vertex w
together with edges {u, w} and {w, v} in a graph G is called an elementary subdivision. For
example, G2 is a graph obtained by elementary subdivision of G1 .
4.7. PLANAR GRAPHS 31
Homeomorphic Graphs: Two graphs G1 and G2 are said to be homeomorphic if they can
be obtained from the same graph by performing a finite number of elementary subdivisions.
For example, the graphs G1 and G2 below are homeomorphic.
Map Coloring: The coloring of a map in which any two regions with a common boundary are
assigned different colors is called a map coloring.
For example, the following are map colorings in which different number of colors are used.
Problem: What is the minimum number of colors required for map coloring?
Dual Graph: The planar graph obtained from a map by representing each region of the
map by vertices and joining the vertices of the graph by an edge if and only if their correspond-
ing regions have a common border, is called a dual graph.
For example, the dual graph of the above map is
32 UNIT 4. GRAPH THEORY
Graph Coloring: The assignment of a color to each vertex of a simple graph so that no
two adjacent vertices are assigned the same color is called the graph coloring or coloring of the
graph.
For example, in the figures below, (ii), (iii) and (iv) are the graph colorings of the graph is figure
(i).
Chromatic Number: The chromatic number of a graph is the least number of colors
needed for a coloring of the graph.
For example, the chromatic number of the above graph is 3.
Four-Color Theorem: The chromatic number of a planar graph is less than or equal to
four.
Scheduling Exams:
Graph coloring can be used to schedule the exams so that no student has two exams at the same
time as follows:
(2) If there is a common student in the courses, then join the corresponding vertices by an
edge.
(4) Schedule the exam such that each time slot for the exam is represented by a different
color.
For example, suppose that there are seven courses which are numbered as 1, 2, 3, 4, 5, 6, 7 and
suppose further that the following courses have common students:
{1, 2}, {1, 3}, {1, 4}, {1, 7}, {2, 3}, {2, 4}, {2, 5}, {2, 7}, {3, 4}, {3, 6}, {3, 7}, {4, 5}, {4, 6},
{5, 6}, {5, 7}, {6, 7}.
Then the associated graph is as in figure (i) below:
4.8. TREES AND SPANNING TREES 33
The coloring of the graph with minimum number of colors is shown in figure (ii).
Since 4 different colors are used, the exam can be scheduled using 4 time slots as follows:
Tree: A tree is a connected undirected graph which has no simple circuits. For example, the
following graph G1 is a tree and G2 is not a tree.
its vertices. We need to show that T is a tree. Since there is a path between any two vertices
of T , so T is connected. Also, T cannot have simple circuits because if it had a simple circuit
containing the vertices, say x and y, then there would be two simple paths between x and y
formed by dividing the simple circuit at x and y. This contradicts the fact that T has a unique
simple path between any two of its vertices. Hence T is a tree.
Rooted Tree: A rooted tree is a tree in which one vertex has been designated as the root
and every edge is directed away from the root. For example, in the graph G1 above, the rooted
tree obtained by designating the vertex a as the root is
Note: Rooted trees are usually drawn with the root at the top of the graph. So the arrows
indicating the directions of the edges in a rooted tree can be omitted, because the edges are
always directed “away” or “downward” from the root.
Some definitions:
Let T be a rooted tree.
(a) For a vertex T other than the root, the parent of v is the unique vertex u such that there is
a directed edge from u to v, e.g., parent of h is e.
(b) A vertex v is called a child of vertex u if there is a directed edge from u to v, e.g., h is a
child of e.
(c) Vertices with the same parent are called siblings, e.g., h, g and f are siblings.
(d) The ancestors of a vertex other than the root are the vertices in the path from root to this
vertex, excluding the vertex itself and including the root, e.g., ancestors of i are h, e and
a.
(e) The descendants of a vertex v are those vertices that have v as an ancestor, e.g., descen-
dants of e are h, g, f, i.
(f) A vertex of a tree is called a leaf if it has no children, e.g., b, c, d, i, g, f are leafs.
(g) A vertex of a tree is called an internal vertex if it has children. The root is an internal
vertex unless it is the only vertex in the graph, in which case it is a leaf, e.g., a, e, h are
internal vertices.
4.8. TREES AND SPANNING TREES 35
(h) If v is a vertex in a tree, the subtree with v as its root is the subgraph of the tree consisting
of v and its descendants and all edges incident to these descendants, e.g., the subtree with
e as root is
m-ary tree: A rooted tree is called an m-ary tree if every internal vertex has no more than
m children.
full m-ary tree: A rooted tree is called a full m-ary tree if every internal vertex has exactly
m children.
Binary tree: An m-ary tree with m = 2 is called a binary tree.
Ordered rooted tree: A rooted tree where the children of each internal vertex are ordered
is called an ordered rooted tree.
The first child of an internal vertex in an ordered binary tree is called the left child and the
second child is called the right child.
Note: Tree as models of
(k − 1) + 1 = k edges. Therefore the induction step is true as well and so the theorem is proved.
Theorem 3: A full m-ary tree with i internal vertices has n = mi + 1 vertices.
Level of a vertex: The level of a vertex v in a rooted tree is the length of the unique path
from the root to this vertex.
Height of a rooted tree: The height of a rooted tree is the maximum of the levels of its
vertices.
For example, the height of the following rooted tree is 4.
level-0 vertices: a
level-1 vertices: b, c, d
level-2 vertices: e, f, g, h, i
level-3 vertices: j, k, l
level-4 vertices: m, n
Simplest way to store data is in linear data structures such as arrays but searching for data stored
in linear data structure can be time consuming. For example, given a sequence of numbers as
17, 23, 4, 7, 9, 19, 45, 6, 2, 37, 99, we can store them in an array as:
17 23 4 7 9 19 45 6 2 37 99
If one wants to search for the number, say 57, then we have to make 11 comparisons before
knowing that this number is not in the sequence.
Another way to store data in, is to use hierarchical data structures such as binary search tree
as below. The data stored in binary search trees is much simpler and less time-consuming to
search.
Algorithm for locating and inserting items in a BST:
PROCEDURE Insertion (T : binary search tree; x: item)
v:= root of T
{a vertex not present in T has the value null}
WHILE v 6= null and label(v) 6= x
4.8. TREES AND SPANNING TREES 37
BEGIN
END
IF root of T = null THEN add a vertex v to the tree and label it with x
ELSE IF v is null or label(v) 6= x THEN label new vertex with x and let v be this
new vertex
{v =location of x}
Problems:
Construct binary search trees for the following data:
(i) 17, 23, 4, 7, 9, 19, 45, 6, 2, 37, 99
Solution:
Spanning tree: A spanning tree of a simple graph G is a subgraph of G which is also a tree
containing all the vertices of G.
For example, given a simple graph G below, some of its spanning trees are T1 , T2 , T3 , T4 etc.
T := empty graph
FOR i := 1 TO n − 1
BEGIN
e := any edge in G with smallest weight that does not form a
simple circuit when added to T .
T := T with e added
END
Solution: The following edges are chosen to form MST using Kruskal’s algorithm.
(b)
40 UNIT 4. GRAPH THEORY
Solution: The following edges are chosen to form MST using Kruskal’s algorithm.
T := a minimum-weight edge
FOR i := 1 TO n − 2
BEGIN
END
4.8. TREES AND SPANNING TREES 41
Problems:
Find the minimum spanning tree of the following graphs using Prims’s algorithm:
(a)
Solution:
(b)
Solution:
42 UNIT 4. GRAPH THEORY
Transport Network: Let G = (V, E) be a weakly connected directed graph that does not
contain any loops such that
(i) there are exactly two distinguished vertices S and D called the source and sink of G
respectively and
(ii) there is a nonnegative real-valued function k defined on E called the capacity function of
G.
Then (G, k) is called a transport network. Also, for an edge e ∈ E, the number k(e) is called
the capacity of e.
(ii) if x is any vertex of G other that the source or the sink, then the sum of all values of
F (x, y) where y ∈ A(x) is equal to the sum of all values F (z, x) where z ∈ B(x) i.e.,
X X
F (x, y) = F (z, x)
y∈A(x) z∈B(x)
(iii) F (e) = 0 for any edge e incident to the source S or incident from the sink D.
For example, the following diagram shows a flow in the transport network above
4.9. NETWORK FLOWS 43
Saturated and Unsaturated Edge: The edges for which the flow and capacity are equal
are called saturated. Otherwise they are called unsaturated edges.
If e is an unsaturated edge, then the slack of e denoted by s(e) is defined as s(e) = k(e) − F (e).
Notation: If X ⊂ V and Y ⊂ V , then (X, Y ) denotes the set of all edges which go from a
vertex in X to a vertex in Y . For example, if X = {a, b} and Y = {c, d, D} in previous graph,
then (X, Y ) = {(a, d), (b, D)}. X
If g is any real-valued function on E, then we define g(X, Y ) = g(e). For example,
e∈(X,Y )
X
if (X, Y ) is as above, then k(X, Y ) = k(e) = k(a, d) + k(b, D) = 6 + 4 = 10 and
e∈(X,Y )
X
F (X, Y ) = F (e) = F (a, d) + F (b, D) = 1 + 3 = 4.
e∈(X,Y )
Value of the flow: Let F be a flow defined on the transport network (G, k). Let A(S) =
{a1 , a2 , · · · , am } and B(D) = {b1 , b2 , · · · , bt }. Then the value of the flow F , denoted by |F |,
is defined as
|F | = F (S, a1 ) + F (S, a2 ) + · · · + F (S, am ) = F (b1 , D) + F (b2 , D) + · · · + F (bt , D).
For example, in the previous example, |F | = F (S, a) + F (S, c) = F (b, D) + F (d, D) = 5.
Maximal Flow: A flow F in a network (G, k) is called a maximal flow if |F 0 | ≤ |F | for
any flow F 0 in (G, k).
Cut: Let (G, k) be a transport network with source S and sink D. Let X ⊂ V and
X = V − X such that S ∈ X and D ∈ X. Then the set (X, X) of all the edges from a vertex
in X to a vertex in X is called an S − D cut.
For example, in our previous network, (X, X) is an S − D cut where X = {S, a, d} (so that
X = {b, c, D}) and (X, X) = {(S, c), (a, b), (d, D)}.
Minimal Cut: An S − D cut (X, X) is called a minimal cut in a transport network (G, k)
if there is no S − D cut (Y, Y ) such that k(Y, Y ) < k(X, X).
Max Flow-Min Cut Theorem: In any transport network, the value of any maximal flow
is equal to the capacity of a minimal cut.
Forward and Backward Edges: Let (G, k) be a transport network with source S, sink D
and a flow F . A nondirected path P from S to D is a sequence of edges e1 , e2 , · · · , en and a
sequence of vertices S = v0 , v1 , · · · , vn = D where edge ei is directed from either
(a) vi−1 to vi
(b) vi to vi−1
44 UNIT 4. GRAPH THEORY
Then the edge ei is called a forward edge of P if (a) is true and backward edge if (b) is true.
For example, in our previous transport network, SacdD is a nondirected path from S to D. In
this path, (S, a), (c, d) and (d, D) are forward edges whereas (c, a) is a backward edge.
F-augmenting Path: Let P be a path from S to D in a transport network (G, k) with flow
F . Then P is called an F -augmenting path if
(i) each forward edge of P is unsaturated i.e., F (e) < k(e) and hence s(e) > 0 for any
forward edge e of P
(ii) each backward edge of P , if it exists, has positive flow i.e., F (e) > 0 for any backward
edge e of P
For example, in the previous transport network, SacdD is an F -augmenting path. Its forward
edges (S, a), (c, d) and (d, D) are all unsaturated and its backward edge (c, a) has a positive
flow. Another F -augmenting path in the same transport network is SadD which consists en-
tirely of three forward edges, all of which are unsaturated.
Notation: For an F -augmenting path P , let (P ) = min i (P ) where
(
s(ei ) = k(ei ) − F (ei ) if ei is a forward edge of P
i (P )=
F (ei ) if ei is a backward edge of P
2. Form a new flow of higher value equal to |F | + (P ) using F and the F -augmenting path
P as follows:
(a) Add (P ) to the value of the flow along all the forward edges on P .
(b) Subtract (P ) from the value of the flow along all the backward edges on P .
Problems:
Find a maximum flow for the network in the figures below:
(i)
4.9. NETWORK FLOWS 45
Solution: The path SadD is an F -augmenting path from S to D. The slack of the edges
(S, a), (a, d) and (d, D) are 5 − 3 = 2, 6 − 0 = 6, 6 − 3 = 3 respectively, the minimum of which
is 2. So the flow can be increased along the path SadD by 2 as follows:
The resulting network has the path SbcD as an F -augmenting path from S to D. The slack
of the edges (S, b), (b, c) and (c, D) are 5 − 3 = 2, 1 − 0 = 1, 6 − 3 = 3 respectively, the
minimum of which is 1. So the flow along the path SbcD can be increased by 1 as follows:
Now there are no more F -augmenting paths in this network and so we have obtained a
maximal flow whose value is 5 + 4 = 9.
(Note: The minimal cut in this network is (X, X) where X = {S, b} and X = {a, c, d, D}.)
(ii)
Solution: The flow along the F -augmenting path SacD can be increased by min{3, 2, 4} =
2 as follows:
46 UNIT 4. GRAPH THEORY
The flow along the F -augmenting path SbdD can be increased by min{5, 2, 4} = 2 as
follows:
The flow along the F -augmenting path SbcD can be increased by min{3, 2, 2} = 2 as
follows:
Now there are no more F -augmenting paths in this network and so we have reached a
maximal flow whose value is 4 + 2 = 6.
(Note: The minimal cut in this network is (X, X) where X = {S, a, b} and X = {c, d, D}
OR X = {S, a, b, c} and X = {d, D}.)
(iii)
Solution: The flow along the F -augmenting path SacD can be increased by min{2, 1, 5} =
1 as follows:
4.9. NETWORK FLOWS 47
The resulting network has an F -augmenting path SbcD where (S, b) and (c, D) are forward
edges with slack 4 − 2 = 2 and 7 − 3 = 4 respectively and the backward edge (c, b) has flow 2.
So the flow along the path SbcD can be increased by min{2, 4, 2} = 2 as follows:
Now there are no more F -augmenting paths in this network and so we have reached a
maximal flow whose value is 5 + 4 = 9.
(Note: The minimal cut in this network is (X, X) where X = {S, a} and X = {b, c, d, D}.)
(iv)
Solution: The flow along the F -augmenting path ScdD can be increased by min{15 −
10, 7 − 3, 10 − 7} = 3 as follows:
The flow along the F -augmenting path Saf eD can be increased by min{15 − 11, 9 −
4, 2, 10 − 4} = 2 as follows:
48 UNIT 4. GRAPH THEORY
The flow along the F -augmenting path Saf beD can be increased by min{15 − 13, 9 −
6, 4, 15 − 6, 10 − 6} = 2 as follows:
The flow along the F -augmenting path ScdbeD can be increased by min{15 − 13, 7 −
6, 4, 15 − 8, 10 − 8} = 1 as follows:
There are no more F -augmenting paths. So we have reached a maximal flow whose value
is 15 + 14 = 29.
(Note: The minimal cut in this network is (X, X) where X = {S, c} and X = {a, b, d, e, f, D}.)
Problems: Find the maximal flow and minimal cuts for the following networks: