Graphs and networks 2
Graphs and networks 2
Term 2
Week Topics
10 8A,8B,8C
11 8D,8E,8F
Term 3
Week Topics
1 8G,8H
2 Revision
3 SAC
A graph is a set of points and line segments:
G1
A B
C D
Each point is called a vertex (plural vertices) and the line segments are called edges. Edges that
connect the same pair of vertices are called multiple edges, and an edge that connects a vertex to
itself is called a loop.
The vertices are denoted by letters and the edges by the letters of the two vertices they connect. The
graph G1 above has 4 vertices, 6 edges, 2 multiple edges and 1 loop. The multiple edges are denoted by
BD1 and BD2 .
The number of edges connected to a vertex is called the degree of the vertex. For G1 , the degree of
vertex A is 2, written deg(A) = 2. Similarly, deg(B) = 3 and deg(D) = 4. A loop contributes two de-
grees to a vertex. Hence deg(C) = 3. We call A and D even vertices since deg(A) = 2 and deg(D) = 4.
Similarly B and C are odd vertices.
The sum of the degrees of the vertices for G1 is 2 + 3 + 4 + 3 = 12. Note that this is twice the number
of edges, 6. This is true for any graph:
Theorem 1
The sum of the degrees of the vertices of a graph is equal to twice the number of edges in
the graph.
A graph can be represented by an adjacency matrix, which shows the number of edges connecting
each pair of vertices in the graph. There is one row and one column for each vertex in the graph. The
adjacency matrix for G1 is:
A B C D
0 1 0 1 A
1 0 0 2
B
0 0 1 1 C
1 2 1 0 D
If it is possible to travel between any two vertices of a graph along its edges then the graph is con-
nected. Otherwise the graph is disconnected. A bridge is an edge in a connected graph that, if
removed, leaves the graph disconnected. G1 is connected and edge CD is a bridge.
1
Two graphs are called isomorphic if it is possible to relabel the vertices of one of the graphs so that
the adjacency matrices of the graphs are identical.
G2 G3
A A
E B D
E
D C C B
A B C D E A B C D E
0 1 0 1 1 A 0 1 1 1 0 A
1 0 1 0 1 B 1 0 1 0 1 B
0 1 0 1 0 C 1 1 0 0 1 C
1 0 1 0 1 D 1 0 0 0 1 D
1 1 0 1 0 E 0 1 1 1 0 E
The adjacency matrices are not the same. However it is possible to relabel the vertices of G3 so that
the adjacency matrices are the same:
G2 G3
A B
E B C
D
D C A E
A B C D E A B C D E
0 1 0 1 1 A 0 1 0 1 1 A
1 0 1 0 1 B 1 0 1 0 1 B
0 1 0 1 0 C 0 1 0 1 0 C
1 0 1 0 1 D 1 0 1 0 1 D
1 1 0 1 0 E 1 1 0 1 0 E
Hence the two graphs are isomorphic. The following result can be used to show that two graphs are not
isomorphic:
Theorem 2
If two graphs are isomorphic then they have the same number of vertices and edges, and
the degrees of the vertices are the same.
2
For example, consider the following two graphs:
G5
G4 B
A B C A C
E F D E
Graphs G4 and G5 have the same number of vertices and edges, but G4 has two vertices of degree 3
(vertex A and F ) whereas G5 has none. Hence the graphs are not isomorphic.
The opposite of Theorem 2 is not true, i.e. two graphs can have the same number of vertices and edges,
and have vertices with the same degrees, but not be isomorphic. For example, consider the two graphs
below:
G6 G7
E F
A B C D A B C D E
F
Graphs G6 and G7 have the same number of vertices and edges, as well as three vertices each of degree
1, two vertices each of degree 2 and one vertex each of degree 3. However the adjacency matrices will
never be the same, regardless of how the graphs are labelled. Hence the graphs are not isomorphic.
A plane graph is a graph with edges that meet only at the vertices. All of the graphs above except G5
are plane graphs. G5 is not a plane graph because it has two edges that cross at a point that isn’t a vertex.
The faces of a plane graph are the regions enclosed by the edges of the graph. The region outside the
graph is also considered a face.
Euler’s formula describes the relationship between the number of vertices v, edges e and faces f of a
plane graph:
Euler’s formula
v−e+f =2
3
Consider G1 and G4 :
G1 G4
A B A B C
C D D
E F
For G1 v = 4, e = 6 and f = 4. So
v−e+f =4−6+4
=2
For G4 v = 6, e = 10 and f = 6. So
v − e + f = 6 − 10 + 6
=2
A planar graph is a graph that is isomorphic to a plane graph. We saw that G5 is not a plane graph.
However G5 is isomorphic to the plane graph G8 :
G5 G8
B B
A C A C
D E D E
F
F
Put simply, a planar graph is a graph that can be redrawn so that none of its edges cross.
4
A walk is a sequence of edges in a graph that takes us from vertex to another. Below is a walk from
vertex A to vertex E for G5 :
G5
A C
D E
For a graph with no multiple edges, the walk can be specified by listing the vertices:
G5
A C
D E
A path is a walk with no repeated edges and no repeated vertices, except possibly for the start and end
vertices.
The walk in the first example is not a path since vertex C is repeated, and the trail above is also not a
path (vertex E is repeated).
5
However the following is a path:
G5
D E
A circuit is a trail that starts and ends at the same vertex. The path above is a circuit.
A cycle is a path that starts and ends at the same vertex. The path above is also a cycle.
G4
A B C
E F
Eulerian trail
If a graph has exactly two vertices of odd degree, then it has an Eulerian trail. The
Eulerian trail starts at either one of the odd vertices and ends at the other odd vertex.
In the example above, the Eulerian trail starts at vertex A and ends at vertex F , since these are the
vertices of odd degree.
6
An Eulerian circuit is a circuit that traverses every edge of a graph:
G5
A C
D E
Eulerian circuit
If a graph has no vertices of odd degree (i.e. all of the vertices are even), then it has an
Eulerian ciruit. The Eulerian circuit can start and end at any vertex.
G1
A B
C D
G5
A C
D E
Unlike Eulerian trails and Eulerian circuits, there are no simple theorems for determining when a graph
has a Hamiltonian path or a Hamiltonian cycle.
7
A graph S is called a subgraph of a graph G if
G8
A C
D E
S1 S2 S3
B B B
A C A C A C
D E D E
8
A tree is a connected graph with no cycles.
Notice that the tree S2 has 4 edges, which is one less than the number of vertices in S2 . This is always
the case for trees.
Theorem 3
The number of edges in a tree is one less than the number of vertices.
A weighted graph (or network ) is a graph which has a number (called a weight) associated with each
edge of the graph. For example
G9
B
5 4
7
A 10 C
3 15
G
6 2 18
8 H
5 10
F 8 D
9 8
E
A graph T is called a spanning tree of a graph G if
The sum of the weights of the edges of a spanning tree is called the length of the spanning tree.
9
Graphs T1 , T2 and T3 below are all spanning trees of G9
T1 T2 T3
B B B
4 5 4
7 7
A C A C A 10 C
3 G 3 G G 15
2 2 6 18
H H 8 H
5 10
F 8 F F
D D D
8 9 8 9
E E E
The length of T1 is 4 + 7 + 3 + 2 + 5 + 8 + 8 = 37. The lengths of T2 and T3 are 41 and 73, respectively.
A minimum spanning tree is a spanning tree with the smallest possible length. To find a minimum
spanning tree of a graph G we can use either Kruskal’s algorithm or Prim’s algorithm.
Kruskal’s algorithm
a) Draw all of the vertices of G (but don’t draw any edges yet). Call this graph M .
b) Choose an edge of G with least weight, cross it out and add it to M . If there is more than one edge
with least weight choose any one of them.
c) Consider the edges of G that haven’t been crossed out yet. Choose the one with least weight and
cross it out (again, if there is more than one such edge choose any one). Add the edge to M provided
that no cycle is formed in M . If a cycle is formed in M do not add the edge to M .
d) If the number of edges in M is one less than the number of vertices, stop. Otherwise go to step c).
Applying Kruskal’s algorithm to G9 gives the following minimum spanning tree, which has a length of
4 + 5 + 3 + 2 + 5 + 8 + 8 = 35
M
B
5 4
A C
3
G
2
H
5
F 8 D
8
E
10
Prim’s algorithm
b) Consider the edges of G that are connected to the vertex in M and choose the one with least weight.
Cross it out and add it to M . If there is more than one such edge choose any one. Draw the other
vertex that the edge is connected to.
c) Consider the edges of G that haven’t been crossed out yet and that are connected to any of the
vertices in M . Choose the one with least weight and cross it out (again, if there is more than one such
edge choose any one). Add the edge to M provided that no cycle is formed in M , and draw the
other vertex that the edge is connected to. If a cycle is formed in M do not add the edge to M .
d) If the number of edges in M is one less than the number of vertices, stop. Otherwise go to step c).
Applying Prim’s algorithm to G9 gives the same minimum spanning tree as before
B
5 4
A C
3
G
2
H
5
F 8 D
8
E
Sometimes a graph has more than one minimum spanning tree. The length of each of these spanning
trees is the same.
11
Two vertices in a graph are adjacent if there is an edge that joins them.
Many problems require us to find the shortest path between two vertices in a network. For example,
suppose we want to find the shortest path from vertex A to vertex H in the following network
2
B C
2 4 1
2 3
A D E H
4
3 7
1 6
F G
5
2) The value of each vertex adjacent to A is equal to the weight of the edge that joins the vertex to A.
3) Circle the uncircled vertex with the smallest value. If there is more than one such vertex, circle any
one of them. Call this vertex V . If V is the destination vertex, go to step 5).
4) For each uncircled vertex adjacent to V , add together the value of V and the weight of the edge that
joins the vertex to V . Call this sum s. If:
b) the vertex has a value and s is less than this value, cross out the value and replace it with s. Go to
step 3).
c) the vertex has a value and s is greater than or equal to this value, go to step 3).
5) The length of the shortest path is the value of the destination vertex. Call this value l.
6) For each vertex adjacent to the destination vertex, subtract the weight of the edge that joins the
vertex to the destination vertex from l. If the result is equal to the value of the vertex, then the vertex
is the second last vertex in the shortest path. Then, for each vertex adjacent to the second last vertex,
subtract the weight of the edge that joins the vertex to the second last vertex from the value of the
second last vertex. If the result is equal to the value of the vertex, then the vertex is the third last
vertex in the shortest path. Repeat to find the remaining vertices in the shortest path.
Applying Dijkstra’s algorithm to the network above gives a shortest path from vertex A to vertex H of
length 5. The path is A − B − C − H.
12
Questions
Q1 Complete the following tables:
Graph 1 Graph 2
Vertex degree A B A B
Vertex degree
A
A
B
C D C D B
C
C
D
D
E E
Graph 3 Graph 4
C C
Vertex degree
A A B A B Vertex degree
B A
C B
D D
D C
E D
E
13
Q3 Suppose a graph has 5 vertices A, B, C, D,E and that:
deg(A) = 4
deg(B) = 3
deg(C) = 4
deg(D) = 6
deg(E) = 1
deg(A) = 3
deg(B) = 1
deg(C) = 4
a Find deg(D).
14
Q5 A graph has the following adjacency matrix:
A B C D
0 1 0 1 A
1 0 0 2
B
0 0 1 1 C
1 2 1 0 D
15