Introduction To Graph Theory: C H A P T E R
Introduction To Graph Theory: C H A P T E R
Introduction To Graph Theory: C H A P T E R
H
A
P Introduction to graph
T
E theory
R
10
10.1 What is a graph?
The objects that we study in the branch of mathematics known as graph
theory are not graphs drawn with x and y axes. In this chapter, the word
‘graph’ refers to a structure consisting of points (called ‘vertices’), some
of which may be joined to other vertices by lines (called ‘edges’) to form a
network. Structures of this type abound in computing. The computers on
a site may be connected into a local area network, which in turn may be
linked to wider networks, including the Internet. The circuitry inside a
computer (which we represented schematically by digital circuit diagrams
in Chapter 8) provides another example of a graph or network structure.
At a more abstract level, we saw in Chapter 5 how a relation on a set can
be depicted using a diagram that takes the form of a graph.
There is a particular type of graph called a tree, which we will study in
Chapter 11. Trees are used in computing to represent the structure of
expressions; we saw an example of a logical expression tree in Chapter 4.
Trees can also be used to represent decision processes, in which we are
faced with two or more choices at each step of a procedure; the tree
diagrams in Chapter 9 were of this type.
In this chapter, we introduce graphs, study their basic properties, and
investigate some practical problems in which they can be applied. In
Chapter 11, we will study trees and investigate some of their applications
to computing.
174
Introduction to graph theory
Figure 10.1
Figure 10.2
Figure 10.3
A graph may also have parallel edges (two or more edges linking the
same pair of vertices), as in Figure 10.4.
175
Discrete mathematics for computing
Figure 10.4
Figure 10.5
176
Introduction to graph theory
Figure 10.6
If we flip the edge AB over into the triangle BCD, and bend DEF up a bit,
we now obtain the graph shown in Figure 10.5(b) without breaking or
rejoining edges or creating new ones.
We will have more to say in Section 10.4 about graphs being
isomorphic.
A useful way to get a feel for graphs is to look for all the simple graphs
with a small number of vertices. We do this now, by finding all the simple
graphs with up to three vertices.
If a simple graph has one vertex, then it must have no edges, because a
simple graph is not permitted to have loops. Therefore there is only one
simple graph with one vertex; it is shown in Figure 10.7(a).
Figure 10.7(a)
If a simple graph has two vertices, then either there is an edge joining
the vertices or there is not. The two possibilities are shown in Figure
10.7(b).
Figure 10.7(b)
With three vertices, things start to get more complicated. It doesn’t
matter whether we draw the vertices in a straight line or in a triangle,
because we may move them around however we like and the graph will
still be essentially the same graph. Suppose we arrange the vertices in a
triangle. We find that there are four different simple graphs with three
vertices; see Figure 10.7(c).
Figure 10.7(c)
These four graphs are all different (they must be, because they have
different numbers of edges), and any simple graph with three vertices is
isomorphic to one of these four. (For example, the graph with three
vertices ‘in line’ can be obtained by ‘straightening out’ the third graph in
Figure 10.7(c).)
You are asked in Exercise 2 to draw all 11 simple graphs with four
vertices. This can be tricky, but it is a useful exercise for becoming
177
Discrete mathematics for computing
familiar with graphs. (If you think you have found more than 11, you will
need to look carefully to see whether you have included graphs that
appear to be different but are actually isomorphic.)
There are 34 simple graphs with five vertices. The number of simple
graphs increases rapidly as the number of vertices increases. There is a
known method for calculating the number of simple graphs with n
vertices, but as it is very complicated we will not give it here.
We now return to graphs in general (not necessarily simple graphs). In
order to proceed further, we need the following definitions.
For example, the vertices of the graph shown in Figure 10.8 have the
degrees indicated.
Figure 10.8
Theorem In any graph, the sum of the degrees of the vertices equals twice the
number of edges.
If V and E denote respectively the set of vertices and the set of edges of a
graph, then the theorem can be written in symbols as follows:
 deg(v) = 2 | E |
v ŒV
The theorem follows immediately from the fact that if we add the degrees
of all the vertices, every edge will be counted twice, since each edge is
incident to two vertices.
Example 10.2.1 Verify for the graph shown in Figure 10.8 that the sum of the degrees of
the vertices equals twice the number of edges.
178
Introduction to graph theory
Handshaking In any graph, the number of vertices with odd degree is even.
lemma
For example, the graph in Example 10.2.1 (Figure 10.8) has two vertices
with odd degree (b and e), and two is an even number. In general, the
Handshaking lemma asserts that if we count the number of vertices with
odd degree in a graph, we must obtain an even number as the answer.
The name of the lemma arises from an amusing way of interpreting this
result. Imagine a room full of people, some of whom shake hands with
some of the other people in the room. The situation can be represented as
a graph, in which the vertices represent the people in the room, and two
vertices are adjacent if the corresponding people shake hands. Even if we
do not know how many people there are, or how many times and with
whom each person shakes hands, we can nevertheless make one assertion
with confidence: the number of people who shake hands an odd number of
times is even.
The Handshaking lemma can be proved in the following way. We begin
with the result of the previous theorem:
 deg(v) = 2 | E |
v ŒV
The left-hand side of this equation can be split into two terms: the sum
taken over the set Vodd of vertices with odd degree, and the sum taken
over the set Veven of vertices with even degree:
 deg(v) +  deg(v) = 2 | E |
v ŒVodd v ŒVeven
179
Discrete mathematics for computing
The second term on the left-hand side is a sum of even numbers, and is
therefore equal to an even number. The right-hand side is clearly an even
number. Thus the first term on the left-hand side is the difference
between two even numbers, and so it must be an even number. Now, this
term is a sum of odd numbers; but an even result can be obtained by
adding odd numbers together only if there is an even number of them.
Hence the number of vertices with odd degree is even. This completes the
proof.
Definition A simple graph is complete if each vertex of the graph is adjacent to every
other vertex.
There is essentially only one complete graph with any given number of
vertices. The complete graph with five vertices is shown in Figure 10.9.
Figure 10.9
We can obtain a formula for the number of edges in the complete graph
with n vertices, using the following reasoning. Each edge in the graph
corresponds to a selection of two distinct vertices from the set of n vertices,
without taking order into account. This selection can be carried out in (n2 )
ways. Hence the number of edges is (n2 ), which equals [n(n – 1)]/2.
Definition The complement G of a simple graph G is the graph with the same vertices
as G, such that any two vertices are adjacent in G if and only if they are
not adjacent in G.
180
Introduction to graph theory
If G is a graph with n vertices, labelled v1, v2, ..., vn, then the adjacency
matrix of G is an n × n matrix whose entries are given by the following
rule:
The entry in the ith row and the jth column is the number of edges
from vi to vj.
In particular, if G is a simple graph, then the entry in the ith row and the
jth column of the adjacency matrix is 1 if vi and vj are adjacent, and 0 if
they are not. In addition, the elements on the principal diagonal of the
matrix (from the top left to the bottom right corner) are all 0, because
simple graphs do not have loops.
Example 10.3.1 Construct the adjacency matrix for the graph shown in Figure 10.10,
assuming the vertices are given in the order a, b, c, d, e.
Figure 10.10
Solution a b c d e
a !0 1 0 1 1$
b #1 0 1 1 0&
# &
c #0 1 0 1 1&
d #1 1 1 0 0&
# &
e "
#1 0 1 0 0&
%
Example 10.3.2 Draw the graph with the following adjacency matrix:
a b c d
a !0 0 0 1$
b #0 0 2 0&
# &
c #0 2 0 1&
d #1 0 1 1&
" %
181
Discrete mathematics for computing
Figure 10.11
The entry in the ith row and the jth column of an adjacency matrix must
be the same as the entry in the jth row and the ith column, because both
entries represent the number of edges between vi and vj. We express this
fact by saying that the adjacency matrix of any graph is symmetric. (Recall
that we are dealing only with undirected graphs; the adjacency matrix of a
directed graph need not be symmetric.) It follows that it is sufficient to
give the entries on and below the principal diagonal of the matrix; there is
no need to write down the entire matrix. This representation is called the
lower triangular matrix representation of the graph. For example, the
lower triangular matrix representation of the graph in Example 10.3.2 is:
a b c d
a !0 $
b #0 0 &
# &
c #0 2 0 &
d #1 0 1 1&
" %
182
Introduction to graph theory
Suppose we have two simple graphs G and H. Let V(G) and V(H)
denote the vertex sets of G and H respectively. If G and H are isomorphic,
then we should be able to show this by associating with each element of
V(G) the corresponding element of V(H).
We have seen something like this before. Recall that in Chapter 6, we
encountered the idea of associating each element of a set with an element
of another set. In a word, what we have here is a function, with domain
V(G) and codomain V(H):
f : V (G) ' V (H )
There are some conditions that the function f must satisfy. We cannot
associate two different vertices of G with the same vertex of H, nor can
there be any vertices of H that are not associated with vertices of G. This
means that f must be one-to-one and onto. There is a further requirement
– if two vertices are adjacent in G, then the corresponding vertices must
be adjacent in H, while if two vertices are not adjacent in G, then the
corresponding vertices must not be adjacent in H. A function that
satisfies all of these conditions is called an isomorphism from G to H.
Definitions Let G and H be two simple graphs, with vertex sets V(G) and V(H)
respectively. An isomorphism from G to H is a function f : V (G) ' V (H )
with the following properties:
f is one-to-one and onto.
▼ ▼
For any two vertices u and v of G, if u and v are adjacent in G, then f(u)
and f(v) are adjacent in H, and if u and v are not adjacent in G, then
f(u) and f(v) are not adjacent in H.
If there exists an isomorphism from G to H, then G and H are isomorphic.
Example 10.4.1 Find an isomorphism between the two graphs shown in Figure 10.12.
Figure 10.12
183
Discrete mathematics for computing
Example 10.4.2 Show that the pairs of graphs shown in Figures 10.13(a) and 10.13(b) are
not isomorphic.
Figure 10.13
Solution (a) Both graphs in Figure 10.13(a) have four vertices and three edges.
However, the second graph has a vertex with degree 3, while the first
does not, so the graphs are not isomorphic. (Note that it would be
incorrect to say that they are not isomorphic because vertex a has
degree 2 in the first graph and degree 1 in the second. An
isomorphism from one graph to another does not necessarily
associate vertices that happen to be labelled with the same letter.)
(b) Both graphs in Figure 10.13(b) have six vertices and seven edges.
Also, both graphs have four vertices with degree 2 and two vertices
184
Introduction to graph theory
with degree 3. However, the first graph has a sequence def of three
vertices with degree 2, with d adjacent to e and e adjacent to f,
whereas there is no such sequence of three vertices with degree 2 in
the second graph. Therefore the graphs are not isomorphic.
In the spirit of the earlier chapters, you might imagine that we would
provide an algorithm for testing whether two graphs are isomorphic.
Unfortunately, even the best known graph isomorphism algorithms are
inefficient when applied to moderately large graphs. It is not known
whether it is possible to construct a graph isomorphism algorithm that
works ‘efficiently’ (in a sense that will be made precise in Chapter 13).
The idea that two mathematical objects (whether they are graphs or
something else) can be fundamentally the same, even if they ‘look different’,
is very powerful. We met a related idea in Chapter 8, where we saw that
propositional logic and sets obey the ‘same’ laws of Boolean algebra. In this
more general sense, isomorphism is one of the most important concepts in
mathematics, because it allows us to ignore the superficial aspects of a
problem and study its underlying mathematical structure.
1 Pronounced ‘oiler’.
185
Discrete mathematics for computing
Figure 10.14
island, then e to the south bank. After returning to the island via f, we
could then cross d, followed by c and b. But now we’re in trouble; bridge g
remains uncrossed, but we have no way of reaching it without crossing
one of the bridges a second time.
At this point, you are encouraged to explore the problem yourself
before reading further. We will obtain the answer later in this section. For
now, we just make the observation that the Königsberg bridge problem
can be treated as a problem in graph theory.
In order to see this, notice that in order to explore the problem, it is not
necessary to know the precise geography of the pieces of land, the shape
of the river, or the lengths of the bridges. In fact, we could collapse each
of the four pieces of land down to a single point, without changing the
problem in any essential way. All that matters is which pieces of land are
joined by bridges, and by how many. If we represent the pieces of land by
vertices and the bridges by edges joining those vertices, we obtain the
graph shown in Figure 10.15.
Figure 10.15
186
Introduction to graph theory
Figure 10.16
Definition A graph G is connected if, for any pair of vertices u and v of G, there is a
path from u to v.
187
Discrete mathematics for computing
Figure 10.17
Definitions Let G be a connected graph. An Eulerian path is a path that includes every
edge of G exactly once. An Eulerian circuit is an Eulerian path for which
the first and the last vertices coincide (that is, an Eulerian circuit is an
Eulerian path that is also a circuit).
A connected graph that has an Eulerian circuit is called Eulerian. A
connected graph that has an Eulerian path but no Eulerian circuit is
called semi-Eulerian.
188
Introduction to graph theory
Figure 10.18
of the three edges incident to A, and leave via another one. That leaves
just one unused edge incident to A. It would be impossible to pass
through A a second time without repeating an edge that has already been
used, so the remaining edge can be used only if the path either starts or
ends at A. This rules out any possibility of an Eulerian circuit, but there
could still be an Eulerian path.
Now consider vertex C. Like vertex A, this vertex also has degree 3, so
the reasoning that we have just applied to A applies to C too. We deduce
that any Eulerian path would have to start or end at C.
So far, it appears that there could be an Eulerian path, provided that it
starts at A and ends at C (or vice versa). But now, if we apply the same
reasoning to vertex D (which also has degree 3), we see that any Eulerian
path would have to start or end at D. We conclude that no Eulerian path
exists, and therefore that it is impossible to find a route around
Königsberg that crosses every bridge exactly once.
The solution to the Königsberg bridge problem suggests a more general
result about Eulerian paths and circuits in connected graphs. The key to the
reasoning we used was to look at the degrees of the vertices. If a vertex has
even degree, say 2n, then an Eulerian path will use all the edges incident to
the vertex by entering and leaving the vertex n times. If a vertex has odd
degree, on the other hand, then there will be one edge left over which can be
used only if the path starts or ends there. Therefore, any connected graph
with more than two vertices with odd degree cannot have an Eulerian path.
This conclusion forms part of the following theorem.
Example 10.5.1 Classify each of the graphs in Figure 10.19 as Eulerian, semi-Eulerian, or
neither, and find an Eulerian path or an Eulerian circuit if one exists.
189
Discrete mathematics for computing
Figure 10.19
Solution (a) All the vertices have even degree, so the graph is Eulerian. An
Eulerian circuit is Aabcdefgihj.
(b) Two vertices, B and C, have odd degree, so it is possible to find an
Eulerian path from B to C. One such path is Baedcbgfh.
(c) There are four vertices with odd degree, so this graph does not have
an Eulerian path.
We have not yet proved Parts 1 and 2 of the theorem. We will establish
Part 1 by devising an algorithm for finding an Eulerian circuit in a
connected graph in which all the vertices have even degree. The fact that
the algorithm always works amounts to a proof of Part 1. Part 2 will
follow as a corollary.
Consider again the graph in part (a) of Example 10.5.1. Suppose we
decide to begin our Eulerian circuit at vertex F. (Because an Eulerian
circuit is a circuit, we should be able to start at any vertex.) If we follow
the circuit Ffge, we then find that we cannot go any further without
repeating edges, yet we have not used all the edges in the graph.
The key to the algorithm for finding an Eulerian circuit is that we can
extend the circuit we already have, by inserting extra edges and vertices at
any vertex where there are unused edges. A is such a vertex. Starting at A,
and following unused edges until we can go no further, we produce the
circuit Aahj. This new circuit can be inserted into the original one to
produce the extended circuit Ffahjge.
190
Introduction to graph theory
The circuit we have obtained so far still doesn’t include all of the edges
of the graph, so we repeat the extension process. There is a vertex, B, on
the circuit, where there are still some unused edges. Starting at B, and
using only edges that have not previously been used, we obtain the circuit
Bbcdi. Inserting the new circuit into the existing one, we obtain
Ffabcdihjge. This circuit uses all of the edges, so it is an Eulerian circuit.
Why does this procedure work? To answer this question, we need to explain
why, when a path is being created for insertion into a circuit that has already
been obtained, we can be sure that the new path will always be a circuit.
The explanation runs as follows. Since every vertex of the graph has
even degree, the new path can always leave any vertex it has entered, with
the exception only of the vertex where it started. This guarantees that
each new path will always be a circuit, and so can be inserted into the
circuit previously obtained.
We will now write this procedure as an algorithm in a form suited to hand
computation. (In a computer-oriented version of the algorithm, the graph
would be input as an adjacency matrix, and some of the steps would need
to be specified in greater detail.)
1. Input an Eulerian graph G, with vertex set V(G) = {v1, v2, ..., vm} and
edge set E(G) = {e1, e2, ..., en}.3
2. circuit ) v1 ; unused _ edges ) E(G)
{circuit is a circuit (expressed as a sequence of vertices and edges) that
is built up piece by piece until it forms an Eulerian circuit. Initially it
consists of a single vertex. The set unused_edges keeps track of the
edges that have not yet been used.}
{Step 3 is the main part of the algorithm. Each time the While-do loop
is executed, a new circuit is formed and inserted into circuit in place of
the vertex insertion_point.}
3. While unused _ edges * + do
3.1. insertion_ point ) first vertex in circuit with unused edges incident
to it
3.2. v ) insertion_ point; new _ circuit ) v
{Each time the Repeat-until loop in Step 3.3 is executed, an edge and a
vertex are appended to new_circuit.}
3.3. Repeat
3.3.1. e ) first element of unused_edges incident to v
3.3.2. v ) vertex adjacent to v via edge e
3.3.3. new _ circuit ) new _ circuit , e, v
3.3.4. unused _ edges ) unused _ edges , {e}
until no element of unused_edges is incident to v
3.4. circuit ) (circuit before insertion_point), new_circuit, (circuit after
insertion_point)
4. Output circuit
191
Discrete mathematics for computing
Example 10.5.2 Trace the algorithm using the graph shown in Figure 10.20 as input, with
vertex set {A, B, C, D} and edge set {p, q, r, s, t, u}.
Figure 10.20
Solution A full trace would be rather tedious here, so we will take a short-cut and
treat the four steps within Step 3.3 as a single step (see Table 10.1).
Table 10.1
Step circuit insertion_ e v new_ unused_ Output
point circuit edges
2 A – – – – {p, q, r, s, t, u} –
3 A – – – – {p, q, r, s, t, u} –
3.1 A A – – – {p, q, r, s, t, u} –
3.2 A A – A A {p, q, r, s, t, u} –
3.3.1–4 A A p B ApB {q, r, s, t, u} –
3.3.1–4 A A q C ApBqC {r, s, t, u} –
3.3.1–4 A A s D ApBqCsD {r, t, u} –
3.3.1–4 A A r A ApBqCsDrA {t, u} –
3.4 ApBqCsDrA A r A ApBqCsDrA {t, u} –
3 ApBqCsDrA A r A ApBqCsDrA {t, u} –
3.1 ApBqCsDrA B r A ApBqCsDrA {t, u} –
3.2 ApBqCsDrA B r B B {t, u} –
3.3.1–4 ApBqCsDrA B t D BtD {u} –
3.3.1–4 ApBqCsDrA B u B BtDuB + –
3.4 ApBtDuBqCs B u B BtDuB + –
DrA
3 ApBtDuBqCs B u B BtDuB + –
DrA
4 ApBtDuBqCs B u B BtDuB + ApBtDuBqCsDrA
DrA
192
Introduction to graph theory
Note that a Hamiltonian path or a Hamiltonian circuit need not use all of
the edges of the graph.
For example, consider the two graphs shown in Figure 10.21.
Figure 10.21
4 Hamiltonian paths and circuits are named after the Irish mathematician Sir
William Hamilton (1805–1865). In 1859, Hamilton produced a puzzle
consisting of a dodecahedron (a regular solid with 12 faces) on which each
vertex was labelled with the name of a city. The problem was to find a way of
travelling along the edges of the dodecahedron, visiting each city exactly once.
193
Discrete mathematics for computing
EXERCISES
1 Explain how each of the following situations could be modelled
as a graph. In each case, state what meaning (if any) can be
attached to loops and parallel edges, and whether it would make
sense to use directed edges.
(a) An electronic circuit.
(b) A chemical molecule.
(c) A group of people and the relationship of friendship
between two people.
(d) The management structure of a company.
(e) The modules of a computer program, and the relationship of
one module calling another.
2 Draw all 11 simple graphs with four vertices.
3 Verify for each of the graphs shown in Figure 10.22 (opposite)
that the sum of the degrees of the vertices equals twice the
number of edges.
4 Draw a graph whose vertices have the following degrees, or
explain why no such graph exists:
(a) 2, 3, 3, 4, 5 (b) 2, 3, 3, 3, 3
5 Obtain an alternative proof that the complete graph with n
vertices has [n(n – 1)]/2 edges, using the result that the sum of
the degrees of the vertices equals twice the number of edges.
6 If a simple graph G has n vertices and m edges, how many edges
does G have?
194
Introduction to graph theory
Figure 10.22
Figure 10.23
8 Draw the graphs with the following adjacency matrices:
!0 0 1 2 1$
!0 1 1 0$ #0
#1 1 1 1 0&
0 0 1& # &
(a) # & (b) # 1 1 0 0 1&
#1 0 0 1& #2
#0 1 0 0 1&
" 1 1 0&
% # &
#1
" 0 1 1 2&
%
195
Discrete mathematics for computing
(Answer the question first for the simpler case where the graph
has no loops.)
11 Show that the pairs of graphs in Figure 10.24 are isomorphic, by
finding an isomorphism from one graph to the other.
Figure 10.24
12 Show that the pairs of graphs in Figure 10.25 are not isomorphic.
Figure 10.25
196
Introduction to graph theory
Figure 10.26
Figure 10.27
(a) and (b)
(continued overleaf)
197
Discrete mathematics for computing
Figure 10.27
(c) and (d)
198
Introduction to graph theory
n
. (number of edges from vi to v k )
k-1
/ (number of edges from v k to v j )
199