Chapter 2
Chapter 2
A graph is acyclic if it has no cycles. A tree is a connected acyclic graph. Any graph without
cycles is a forest, thus the components of a forest are trees.
Example of trees :
Theorem 8
Let T be a graph with n vertices. Then the following statements are equivalent.
CHAPTER 3 : MATCHING AND FACTORS
(i) T is a tree;
(ii) T contains no cycles, and has n – 1 edges;
(iii) T is connected, and has n – 1 edges;
(iv) T is connected, and each edge is a bridge;
(v) any two vertices of T are connected by exactly one path;
(vi) T contains no cycles, but the addition of any new edge creates exactly one
cycle.
Spanning trees
A spanning tree of a connected graph G is a subgraph which includes all the vertices of G and
is a tree.
To get a spanning tree of a graph,
1. Choose a cycle.
2. Remove any one of its edges and the remaining graph is still connected.
3. Repeat 1 and 2 until there are no cycles left.
The total number of edges removed from G to obtain a spanning tree is the cycle rank of G,
denoted by (G). Note that (G) = m – n + 1.
Example of a graph G with five of its spanning trees :
v w v w
x y z x y z
G T1
v w
v w
x y z
x y z
T3
T2
v w v w
x y z x y z
T4 T5
Counting trees
Theorem (Cayley)
There are nn-2 distinct labelled trees with n vertices.
Example
Every tree with four vertices is either a path or a star.
For the path, there are 4 ways to label the first vertex, 3 ways to label the second, 2 ways to
label the third and 1 way to label the fourth; reversing the labelling is regarded the same. Thus,
4!
the number of ways of labelling the path is = 12. For the star, the central vertex can be
2
labelled in four different ways and each one determines the labelling. Thus, the number of ways
of labelling the star is 4.
Hence, the total number of distinct labelled trees with four vertices is 12 + 4 = 16 = 42.
To each labelled tree on n vertices there corresponds a unique spanning tree of Kn. Conversely,
each spanning tree of Kn gives rise to a unique labelled tree on n vertices. Thus, we have the
following corollary.
Corollary
The number of spanning trees of Kn is nn-2.
Theorem (Matrix-tree theorem) or Kirchoff’s Theorem
Let G be a connected simple graph with vertex set {v1, v2, …, vn}, and let M = (mij) be the n
n matrix in which mii = deg(vi), mij = −1 if vi and vj are adjacent, and mij = 0 otherwise. Then the
number of spanning trees of G is equal to the cofactor of any element of M.
Example v1 v2
3 −1 −1 −1
−1 2 0 −1
M=
−1 0 2 −1
v4 v3 −1 −1 −1 3
G
2 0 −1
Cofactor of m11 = 0 2 −1 = 2(6 – 1) – 1(0 + 2) = 8
−1 −1 3
v1 v2 v1 v2 v1 v2 v1 v2
v4 v3 v4 v3 v v3 v4 v3
4
T1 T2 T3 T4
v1 v1 v2 v1
v2 v2 v1 v2
v4 v3 v3 v4 v3 v4 v3
v4
T5 T6 T7 T8
Minimum Spanning Tree (MST)
A minimum spanning tree is a spanning tree such that the total length of its edges is as small
as possible. It is sometimes called a minimum connector. Kruskal’s algorithm and Prim’s
algorithm find a minimum spanning tree uses the greedy approach.
Kruskal’s Algorithm
Kruskal’s algorithm finds the shortest, cheapest or fastest way of linking all the vertices into one
system. The algorithm is as follows.
1. Sort all the edges into ascending order of weight.
2. Select the edge of least weight to start the tree.
3. Consider the next edge of least weight
• If it would form a cycle with edges already selected, reject it.
• If it does not form a cycle, add it to the tree.
If there is a choice of equal weight, consider each in turn.
4. Repeat step 3 until all vertices are connected.
Example
Find a minimum spanning tree using the Kruskal’s algorithm for the network given below.
a 6 b
7 5
f 3 4 c
2
8 2
e 3 d
Step 1 :
Edge Weight
bd 2
cd 2
ae 3
de 3
be 4
bc 5
ab 6
af 7
ef 8
Step 2 :
a b
7
f 3 c
2
2
e 3 d
Prim’s Algorithm
Steps : 1. Choose any vertex to start the tree.
2. Select an edge of least weight that joins a vertex that is already in the tree to a vertex
that is not yet in the tree. If there is a choice of edges of equal weight, choose
randomly.
3. Repeat step 2 until all vertices are connected.
Example
Find a minimum spanning tree using the Prim’s algorithm for the network given below
a 6 b
7 5
f 3 4 c
2
8 2
e 3 d
a b
7
f 3 c
2
2
e 3 d
Prim’s Algorithm to a Distance Matrix
Steps : 1. Choose any vertex to start the tree.
2. Delete the row in the matrix for the chosen vertex.
3. Number the column in the matrix for the chosen vertex.
4. Put a ring round the lowest undeleted entry in the numbered column. If there is an
equal choice, choose randomly.
5. The ringed entry becomes the next edge to be added to the tree.
6. Repeats steps 2, 3, 4, and 5 until all rows are deleted.
Example
Find a minimum spanning tree using the Prim’s algorithm for the network given the distance
matrix below.
A B C D E
A - 27 12 23 74
B 27 - 47 15 71
C 12 47 - 28 87
D 23 15 28 - 75
E 74 71 87 75 -
Choose vertex A; delete row A; number column A as 1. Put a ring around “12”, the smallest
entry in column A. The first edge is AC of weight 12.
The new vertex is C; delete row C; number column C as 2. The smallest undeleted entry in
column A and column C is 23. Put a ring around “23”. Edge AD is added to the tree.
Delete row D; number column D as 3. The smallest undeleted entry in columns A, C and D is
15. Put a ring around it and add edge BD to the tree.
Delete row B; number column B as 4. The smallest undeleted entry in columns A, C, D and B is
71. Put a ring around it and add edge BE to the tree.
Finally delete row E and number column E as 5. All rows have been deleted and an MST is
obtained.
Minimum spanning tree :
C
12
A B 71 E
15
23
D
A 4 5
5 4
D 5 8
5 8
4 3
S 1 0 6 B 3 4 8 T 6 11
6 4 14 12 11
2
2
12
C 2 2
2
Give vertex S a final value of 0 and indicate it as the first vertex to receive the final value.
Give working values to A(5), B(6) and C(2). Among these, 2 is the smallest working value.
Vertex C is now completed and it gets final value of 2. It is the second vertex to be completed.
Add working values to each vertex that is directly connected to C; T(2+12=14), B(2+2=4, which
is smaller than the previous working value 6)
4 is now the smallest working value, hence vertex B is completed. It gets the final value 4 and it
is the third vertex completed.
Add working values to each vertex that is directly connected to B; D(4+4=8), T(4+8=12, which is
smaller than the previous working value 14)
5 is now the smallest working value, hence vertex A is completed. It gets the final value 5 and it
is the fourth vertex completed.
Vertex D is the only vertex directly connected to A. The new working value is 5+4=9 which is
bigger than the previous working value. So, it is not recorded.
8 is the smallest working value, hence vertex D is completed. It gets the final value 8 and it is
the fifth vertex completed.
Finally, vertex T is the vertex directly connected to D. Its new working value is 8+3=11 and this
the sixth vertex completed. The final value is 11.
The shortest distance from S to T is 11.
Back tracking, 11−3=8, 8−4=4, 4−2=2, 2−2=0 hence the shortest path is SCBDT.