Professional Documents
Culture Documents
Trees
Trees
H
A
P Trees
T
E
R
Example 11.1.1 List the cycles in the graph shown in Figure 11.1.
200
Trees
Figure 11.1
Figure 11.2
containing a cycle.
Removing any edge from a tree produces a disconnected graph.
▼
It is easily checked that the theorem is true for each of the trees shown in
Figure 11.2. For example, the rightmost tree has 13 vertices and 12 edges.
We will prove the theorem using induction on n. The proof is an
interesting example of induction because it shows that the method can be
applied more widely than just to the algebraic problems we dealt with in
Chapter 7.
201
Discrete mathematics for computing
In the course of the proof, we will need to use the following fact, which
should be obvious: in any tree with at least two vertices, it is always
possible to find a vertex with degree 1.
For the base step of the proof, we simply note that a tree with one
vertex must have no edges, because a tree cannot contain loops.
We begin the inductive step by assuming that any tree with k vertices
has k – 1 edges. Let T be a tree with k + 1 vertices. ‘Prune’ the tree T by
removing a vertex v with degree 1 and the edge e incident to v. The
resulting graph is a tree with k vertices, and by the inductive hypothesis it
has k – 1 edges. Now restore T by replacing v and e. In the process, the
number of edges increases by 1, so T has k edges. This completes the
inductive step.
It follows by induction that a tree with n vertices has n – 1 edges, for all
values of n. This concludes the proof.
We will use this result in the minimal spanning tree algorithm in the
next section.
Table 11.1
A B C D
A –
B 120 –
C 100 105 –
D 140 125 90 –
202
Trees
Figure 11.3
Definitions A weighted graph is a simple graph in which each edge has a positive
number attached to it. The number attached to an edge in a weighted
graph is called the weight of the edge.
In the present example, the weights are the costs of installing the links. In
other problems in which weighted graphs are used, the weights might
represent road distances, signal travelling times, or link capacities.
We do not need to install all of the links in our example; it is sufficient
to install just the three cheapest: CD, BC and AC. With these links in
place, any two laboratories can communicate with one another.
This example was small enough that the solution could be obtained
easily by inspection. In a problem involving a large number of sites, it is
not practical to determine the solution by trial and error in this way. For
this reason, we now turn our attention to developing an algorithm for
finding the cheapest network linking the sites.
As a first step towards solving the problem, we can look for some
properties that the solution must have. The solution is itself a graph,
consisting of all of the vertices but only some of the edges of the original
weighted graph. Clearly the solution must be a connected graph, in order to
ensure that any two sites can communicate with each other. Furthermore,
since the solution represents the cheapest possible network connecting the
sites, it cannot contain any cycles because if a cycle were present, then one
of the edges in the cycle could be removed to produce a connected graph
with a smaller total weight.
We can sum up these observations as follows. The solution to the
problem must be a tree. The vertices of the tree must be the vertices of the
original graph, and the edges of the tree must be selected from the set of
edges in the original graph. A tree that satisfies these requirements is
called a spanning tree of the graph.
Typically, a connected graph has many spanning trees. In Figure 11.3,
not only do the edges CD, BC and AC form a spanning tree, but so do the
edges AB, AC and AD, and the edges AB, BC and CD. The problem is to
find a spanning tree with the smallest possible total weight, since this will
represent the cheapest possible network. A spanning tree with this
property is called a minimal spanning tree.
Here, then, is the problem stated in the terminology of graph theory.
203
Discrete mathematics for computing
Example 11.2.1 Use Prim’s algorithm to find a minimal spanning tree for the weighted
graph shown in Figure 11.4.
Figure 11.4
Solution Starting at vertex A, we find that the edge with least weight incident to A
is the edge AD with weight 20, so this is the first edge in the minimal
spanning tree T.
We look next at the edges incident to both A and D and not yet in T. Of
these, DE has the least weight, so we add it to T.
204
Trees
Figure 11.5
Table 11.2
Step e v T unused_edges Output
2 – – {A} {AB, AD, AF, BC, BE, –
BF, CD, CE, DE, EF}
3.1–3.4 AD D {A, AD, D} {AB, AF, BC, BE, BF, –
CD, CE, DE, EF}
3.1–3.4 DE E {A, AD, D, DE, {AB, AF, BC, BE, BF, –
E} CD, CE, EF}
3.1–3.4 BE B {A, AD, D, DE, {AB, AF, BC, BF, CD, –
E, BE, B} CE, EF}
3.1–3.4 CE C {A, AD, D, DE, {AB, AF, BC, BF, CD, –
E, BE, B, CE, C} EF}
3.1–3.4 BF F {A, AD, D, DE, {AB, AF, BC, CD, EF} –
E, BE, B, CE, C,
BF, F}
4 BF F {A, AD, D, DE, {AB, AF, BC, CD, EF} {A, AD, D, DE,
E, BE, B, CE, C, E, BE, B, CE, C,
BF, F} BF, F}
205
Discrete mathematics for computing
206
Trees
tree problem and the Travelling sales representative problem are of this
type. In the second type, the weights represent something we would want
to maximise, such as the capacity of a communications channel. In a
graph of the first type, a pair of vertices with no connecting edge is even
‘worse’ than a pair joined by an edge with a very large weight. This is
indicated in the weight matrix by treating two non-adjacent vertices as if
they were joined by an edge of infinite weight. For weighted graphs of the
second type, a weight of zero is appropriate for non-adjacent vertices. The
entries on the diagonal of the weight matrix are zero for both types of
weighted graph. We consider only the first type here.
Definition Let G be a weighted graph with vertices v1, v2, ..., vn, in which the weights
represent a penalty of some kind. The weight matrix of G is the n × n
matrix for which the entry wij in the ith row and the jth column is given
by the rule:
Ï0 if i = j
ÔÔ
wij = Ìthe weight of the edge from i to j if vi and v j are adjacent
Ô
ÔÓ• if i π j and vi and v j are not adjacent
Example 11.2.2 Write down the weight matrix for the weighted graph in Example 11.2.1.
Solution A B C D E F
A %0 22 $ 20 $ 25 (
B ' 22 0 10 $ 5 12 *
' *
C '$ 10 0 18 8 $ *
D ' 20 $ 18 0 13 $ *
' *
E '$ 5 8 13 0 15 *
F '
& 25 12 $ $ 15 0 *)
207
Discrete mathematics for computing
We will use the notation d(u, v) to denote the distance from u to v. It will
also be convenient to use the notation weight(e) to denote the weight of
an edge e.
Notice that the definition of the length of a path in a weighted graph is
different from the definition in a graph without weights, which we
encountered in Chapter 10. (Recall that in the latter case the length of a
path is simply the number of edges in the path.)
In the application we have in mind, the weights actually represent
times rather than distances. However, we can imagine that a weighted
graph could also represent a road network in which the weights are the
road distances.
The problem can now be stated in the terminology of graph theory as
follows.
208
Trees
Example 11.3.1 Apply Dijkstra’s algorithm to find the minimum distance from A to C in
the weighted graph in Example 11.2.1, and the path that achieves this
distance.
Table 11.3
Step w e v T d(v1, v) Output
2 – – – {A} – –
3.1–3.5 A AD D {A, AD, D} 20 –
3.1–3.5 A AB B {A, AD, D, AB, B} 22 –
3.1–3.5 A AF F {A, AD, D, AB, B, 25 –
AF, F}
3.1–3.5 B BE E {A, AD, D, AB, B, 27 –
AF, F, BE, E}
3.1–3.5 B BC C {A, AD, D, AB, B, 32 –
AF, F, BE, E, BC, C}
4 B BC C {A, AD, D, AB, B, 32 {A, AD, D, AB, B, AF, F,
AF, F, BE, E, BC, C} BE, E, BC, C}
209
Discrete mathematics for computing
Figure 11.6
Figure 11.7
210
Trees
The rooted tree in this example has the property that every vertex that
is not a leaf has exactly two children. A rooted tree with this property is
called a binary rooted tree. In a binary rooted tree, the two children of
each parent are identified as the left child and the right child, according to
where they are placed when the tree is drawn. For any given parent
vertex, the left child is the root of the left subtree, the binary rooted tree
consisting of the left child and its descendants. Similarly, the right child is
the root of the right subtree.
There is also a recursive definition of a binary rooted tree:
1. A single vertex is a binary rooted tree.
2. If T is a binary rooted tree, and v is a leaf of T, then T "{ v1 , e 1 , v 2 , e 2 }
is a binary rooted tree, where v1 and v2 are vertices not in T, e1 is an
edge from v to v1, and e2 is an edge from v to v2.
We now show how a binary rooted tree can be used to represent the
way in which an algebraic expression is evaluated.
Suppose we have an algebraic expression containing a number of
variables and the binary operations of addition (+), subtraction (–),
multiplication (×) and division (/). An example of such an expression is
shown below:
(a – (b/c)) × (d + e)
For the sake of clarity, we have used brackets explicitly to indicate the
order in which the operations are to be carried out, including those
around b/c where brackets are not strictly necessary because of the
convention in algebra about precedence of operators.
We now imagine that this expression is being evaluated by a computer
with particular values substituted for the variables. The principal
operation in this example is the multiplication, because this is the
operation that would be executed last, after (a – (b/c)) and (d + e) have
both been evaluated. (This is exactly analogous to the principal
connective of a logical expression, which we met in Chapter 4.) The
principal operations of the subexpressions (a – (b/c)) and (d + e) are the
subtraction and the addition respectively. In turn, (a – (b/c)) contains the
subexpression (b/c) with division as its principal operation.
The structure of the expression can be depicted using a binary rooted
tree known as an expression tree (Figure 11.8).
Figure 11.8
The root of the tree is labelled with the principal operation. The left
subexpression (a – (b/c)) is represented by the left subtree, whose root is
the left child of the root of the expression tree. Similarly, the right
211
Discrete mathematics for computing
212
Trees
Example 11.4.1 Carry out a pre-order traversal on the expression tree in Figure 11.8.
213
Discrete mathematics for computing
Example 11.4.2 Carry out a post-order traversal on the expression tree in Figure 11.8.
Solution The details are similar to those in the previous example, so we just give
the output:
abc/–de+×
EXERCISES
1 List the cycles in the graphs shown in Figure 11.9.
Figure 11.9
214
Trees
Figure 11.10
A B C D E
B 85
C – 95
D 65 50 –
E 55 – – 70
F 45 – 80 – –
Find the network with the least possible cost that could be built
to link all the nodes in the network.
8 Use Dijkstra’s algorithm to find:
(a) the minimum distance path from C to F in the weighted
graph in Exercise 5(a) (Figure 11.10(a));
(b) the minimum distance path from A to C in the weighted
graph in Exercise 5(b) (Figure 11.10(b)).
9 Construct an expression tree for each of the following
expressions:
(a) a + ((b – c) × d)
(b) ((a × b)/(c × d)) – (e/f)
(c) (a – (b + (c + d))) – ((e × f) × g)
10 By applying a pre-order traversal to the expression tree, write
each of the expressions in Exercise 9 in Polish prefix notation.
11 By applying a post-order traversal to the expression tree, write
each of the expressions in Exercise 9 in reverse Polish notation.
12 Write down (in infix notation) the expressions corresponding to
the trees shown in Figure 11.11 (overleaf).
13 The binary rooted tree shown in Figure 11.12 can be used to
encode and decode English text according to a Huffman code.
The sequence of edges from the root to any letter yields the
binary code for that letter. Note that the number of bits varies
from one letter to another.
215
Discrete mathematics for computing
Figure 11.11
Figure 11.12
216