Topic09 Graphs
Topic09 Graphs
Graph Theory
249
250 CHAPTER 11. GRAPH THEORY
Definitions
A graph G = (V,E) is a structure consisting of a set V of
vertices (nodes) and a set E of edges (lines joining vertices).
e={u,v}
u v
Simple Graphs
A simple graph is a graph that has no loop (=edge {u,v} with
u=v) and no parallel edges between any pair of vertices.
London
Beijing
From\To Hong Kong Singapore Beijing Taipei London Hong Kong
Hong Kong 4 Flights
Taipei
Singapore 2 Flights 3 Flights 1 Flight 1 Flight
Taipei
Take for example Hong Kong: there will be one edge between Hong Kong
and Singapore (this is read in the first row), and and one edge from Hong
Kong to Beijing (this is read in the first column). This graph is simple, there
is no loop, and no parallel edge.
Definition 75. A multigraph G is a graph that has no loop and at least two
parallel edges between some pair of vertices.
Example 107. We continue with our fictitious example of flights. Suppose
now we want to know how many flights are there that operate between two
cities (we are still not interested in the direction). In this case, we will need
parallel edges to represent multiple flights. For example, let us consider Hong
Kong and Singapore: there are 4 flights from Hong Kong to Singapore, and
2 flights from Singapore to Hong Kong, thus a total of 6 edges between the
two vertices representing these cities.
Very often, a relation from a vertex A to B does not yield one from B to
A, in this case, edges become arrows.
Definition 76. A directed graph G, also called digraph for short, is a graph
where edges {u, v} are ordered ({u, v} and {v, u} are not the same), that is
edges have a direction. The graph is called undirected otherwise. Parallel
edges are allowed in directed multigraph. Loops are allowed for both directed
and directed multigraphs.
Example 108. On our example of ficticious flights, a directed graph corre-
sponds to put arrows for flights going in a given direction, for example, there
is one arrow from London to Beijing, but none from Beijing to London.
People attribute the origin of graph theory to the Königsberg bridge ques-
tion, solved by the mathematician Euler in 1736. The question was, given
the map of Königsberg, which contains 7 bridges, is it possible to find a walk
that goes through the 7 bridges without crossing a bridge twice? You may
look at the map and give it a try yourself, to convince yourself of the answer.
The answer turns out to be no, it is not possible. We will see why next. But
first, we will give a name to such walks, in honour of Euler:
Definition 77. A Euler path/trail is a walk on the edges of a graph which
uses each edge in the graph exactly once. A Euler circuit/cycle is a walk on
the edges of a graph which starts and ends at the same vertex, and uses each
edge in the graph exactly once.
252 CHAPTER 11. GRAPH THEORY
Multigraphs
A multigraph is a graph that has no loop and at least 2
parallel edges between some pair of vertices.
London
Beijing
From\To Hong Kong Singapore Beijing Taipei London Hong Kong
Hong Kong 4 Flights
Taipei
Singapore 2 Flights 3 Flights 1 Flight 1 Flight
Taipei
Directed (Multi)graphs
A directed graph is a graph where edges {u,v} are ordered,
that is, edges have a direction. Parallel edges are allowed
in directed multigraphs. Loops are allowed for both.
From\To Hong Kong Singapore Beijing Taipei London
London
Hong Kong 4 Flights
Beijing
Hong Kong
Singapore 2 Flights 3 Flights 1 Flight 1 Flight
a Leonhard Euler
introduced
Graphs in 1736
to solve the
b d Königsberg
• People tried (without success) Bridge problem
(no solution and
to find a way to walk all 7 bridges why).
without crossing a bridge twice. c
Euler Circuit
• Suppose the beginning and end are the same
a
node u.
• The graph must be connected.
• At every vertex v≠ u, we reach v along one
edge and go out along another, thus the
b d number of edges incident at v (called the
degree of v) is even.
• The node u is visited once the first time we
leave, and once the last time we arrive, and
c
possibly in between (back and forth), thus the
degree of u is even.
• Since the Königsberg Bridges graph has odd
degrees, no solution!
8/15
Euler Theorem
The degree of a vertex is the number of edges
incident with it.
The Euler circuit/cycle is simply an Euler path/trail whose start and end
are the same vertex.
Definition 78. The degree of a node is the number of edges incident with
it.
With this definition of degree, we next answer the question of the bridges
of Kn̈igsberg. We start with the more constrained case of starting and finish-
ing at the same vertex. We assume the graph G is connected, which means
that there is always a way to walk from any vertex to any other (possibly
using several times the same vertex or the same edge).
Proof. We prove only that if G contains an Euler circuit, then all nodes have
an even degree. We start the walk at vertex u. Now for any vertex v which is
not u, we need to walk in v using some edge, and walk out of u using another
edge. We may come back to v, but for every come back, we still need one
edge to come in, and one to walk out. Therefore the degree of v must be
even! As for the starting point u, it is visited once the first time we leave,
and the last time we arrive (2 edges), and any possible back and forth counts
for 2 edges as well, which shows that indeed, it must be that all nodes have
an even degree!
Since the Königsberg bridge graph has odd degrees, it has no Euler cycle.
We next extend the argument to an Euler path.
Proof. We only prove that if G has an Euler path, then exactly two nodes of
G have an odd degree. Suppose thus that G has an Euler path, which starts
at v and finishes at w. Create a new graph G0 , which is formed from G by
adding one edge between v and w. Now G0 has an Euler cycle, and so we
know by the previoust theorem that G0 has the property that all its vertices
have an even degree. Therefore the degrees of v and w in G are odd, while
all the others are even and we are done. The other direction is left as an
exercise (see Exercise 96).
256 CHAPTER 11. GRAPH THEORY
Examples
Note: Euler Theorem actually states an if and only if.
Here are two more types of graphs which are important, in that you are
very likely to encounter them.
Definition 79. A complete graph with n vertices is a simple graph that has
every vertex connected to every other distinct vertex.
w/cfg wgf/c
c/wfg cfg/w
Proof.
Choose an e ∈ E(G) with endpoints v, w ∈ V.
e contributes 1 to deg(v) and 1 to deg(w). True
v3 v4
even when v = w . Thus each edge contributes 2
to the total degree. deg(v1) = deg(v2) = deg(v3) = deg(v4) = 4
2e = deg(v) = 4 4 = 16 and e = 8
260 CHAPTER 11. GRAPH THEORY
Adjacency Matrix
v1 v2 v3
v1 1 0 0
v1 v2
A= v2 1 1 2
v3 1 0 0
v3
Hamiltonian Circuit
• The Icosian game (1857):
Along the edges of a dodecahedron, find a
path such that every vertex is visited a single
time, and the ending point is the same as the
starting point.
• Hamilton sold it to a London game dealer in
1859 for 25 pounds.
Copyright: http://mathworld.wolfram.com/IcosianGame.html
261
Proof. The proof follows the name of the theorem. The idea of handshaking
is that if two people shake hands, there must be...well...two persons involved.
In a graph G, this becomes, if there is an edge e between v and w, then e
contributes to 1 to the degree of v and to 1 to the degree of s. This is also
true when v = w. Therefore each edge contributes 2 to the total degree.
To represent a graph, a useful way to do so is to use a matrix.
Definition 82. The adjacency matrix of a graph G is a matrix A whose
coefficients are denoted aij , where aij , the coefficient in the ith row and jth
column, counts the number of arrows from vi to vj .
You may replace the term arrow by edge in this definition if you graph is
undirected.
Example 110. The adjacency of a complete graph with 4 vertices is
0 1 1 1
1 0 1 1
.
1 1 0 1
1 1 1 0
The first row reads that v1 is connected to v2 , v3 , v4 , but not to itself (since
it is a simple graph by definition).
We saw earlier Euler paths as walks going through exactly every edge of
a graph. If you replace ”exactly every edge” by ”exactly every vertex”, this
becomes a Hamiltonian path!
Definition 83. A Hamiltonian path of a graph G is a walk such that every
vertex is visited exactly one. A Hamiltonian circuit of a graph G is a closed
walk such that every vertex is visited exactly one, except the same start/end
vertex.
Hamiltonian paths are harder to characterize than Euler paths. In par-
ticular, finding an algorithm that will identify Hamiltonian paths in a graph
is hard!
262 CHAPTER 11. GRAPH THEORY
Hamiltonian vs Eulerian
Examples
Euler circuit
Hamiltonian path
No Hamiltonian circuit
Graph Isomorphism
v1 v1
0 1 0 0 1
1 0 1 0 0
v3 v4 v5 v2
0 1 0 1 0
0 0 1 0 1
1 0 0 1 0
v5 v2 v4 v3
Exercise 98. Show that in every graph G, the number of vertices of odd
degree is even.
Exercise 99. Show that in very simple graph (with at least two vertices),
there must be two vertices that have the same degree.
Exercise 100. Decide whether the following graphs contain a Euler path/cycle.