10 Unit10

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 177


Unit 10 Trees and Algorithms
Structure
10.1 Introduction
Objectives
10.2 Characterization of Trees
10.3 Rooted Trees and Applications
10.4 Spanning Trees
10.5 Algorithms for Spanning Trees
Kruskal Algorithm
Prims Algorithm
10.6 Summary
10.7 Terminal Questions
10.8 Answers
10.1 Introduction
After studying the concept of graphs in the previous unit, now, you will study
the concept of trees. Trees are extensively used as models in area like
computer science, chemistry and in search procedures. These are also
useful in design of wide range of algorithms. In this unit, we will provide
some characterizations of trees with suitable illustrations. We will also
introduce a special type of trees namely rooted trees and binary trees. We
will give few applications of these trees. The spanning trees will be
introduced and the algorithms for spanning trees will also be obtained.
Objectives
After studying this unit, you should be able to:
describe the connected graphs without circuits (called trees) with their
properties
characterize a tree from a connected graph
apply the concept of trees in real life situations
analyze and write algorithms for spanning trees.

10.2 Characterization of Trees
The concept of a tree plays a vital role in the theory of graphs. First, we
introduce the concept of tree, study its properties and some of its
applications. Later, we introduce the concept of spanning tree, and study
the relationships among circuits and trees.
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 178
Definition: A connected graph without circuits is called a tree. A collection
of trees is called a forest.
Example:
Trees with one, two, three and four vertices are given in the Fig. 10.1



Fig. 10.1
Observations
i) A tree contains at least one vertex.
ii) A tree without any edge is referred to as a null tree.
iii) The trees considered are finite and a tree is always a simple graph.
Some natural examples
i) The list of the ancestors of a family may be represented by a tree. This
tree referred to as a family tree.
ii) A river with its tributaries and sub tributaries may be represented by a
tree. This tree is referred to as a river tree.
iii) The sorting of mail according to zip code are done according to a tree.
This tree is called decision tree (or) sorting tree.
Theorem: In a tree T, there is one and only one path between every pair of
vertices.
Proof: Suppose T is a tree.
By definition, T is a connected graph and contains no circuits.
Since T is connected, there exists at least one path between every pair of
vertices in T. Suppose that between two vertices a and b of T, there are two
distinct paths. Now, the union of these two paths will contain a circuit in T, a
contradiction (since T contains no circuits).
This shows that there exists one and only one path between a given pair of
vertices in T.
Theorem: If there is one and only one path between every pair of vertices in
G, then G is a tree.

Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 179
Proof: Assume that there is one and only one path between every pair of
vertices in a graph G. Clearly G is connected.
If possible, suppose that G contains a circuit. Then, there is at least one pair
of vertices a, b such that there are two distinct paths between a and b,
which is a contradiction to our assumption. So G contains no circuits. Thus
G is a tree.
Theorem: A tree G with n vertices has (n - 1) edges.
Proof: (Use induction on n):
Step (i): If n = 1, then G contains only one vertex and no edge.
So the number of edges in G is n -1 = 1 - 1 = 0.
Step (ii): Induction hypothesis: The statement is true for all trees with less
than n vertices.
Step (iii): Let us consider a tree with n vertices.
Let e
k
be any edge in T whose end vertices are v
i
and v
j.
Since T is a tree, there is no other path between v
i
and v
j.
So by removing

e
k
from T, we get a disconnected graph.
Also, T- e
k
consists of exactly two components(say T
1
and T
2
).
Since T is a tree, there were no circuits in T and so there were no circuits in
T
1
and T
2.
Therefore, T
1
and T
2
are also trees.
It is clear that |V(T
1
)| + |V(T
2
)| = |V(T)| where V(T) denotes the set of
vertices in T.

Also, |V(T
1
)| and |V(T
2
)| are less than n.
Therefore, by the induction hypothesis, we have
|E(T
1
)| = |V(T
1
)| - 1 and |E(T
2
)| = |V(T
2
)| - 1.
Now |E(T)| - 1 = |E(T
1
)| + |E(T
2
)| = |V(T
1
)| - 1 + |V(T
2
)| - 1
Thus |E(T)| = |V(T
1
)| + |V(T
2
)| - 1 = |V(T)| - 1 = n-1.
Theorem: Any connected graph with n vertices and n -1 edges is a tree.
Proof: Let G be a connected graph with n vertices and n - 1 edges.
It suffices to show that G contains no circuits.
If possible, suppose that G contains a circuit. Let e be an edge in that
circuit.
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 180
Since e in a circuit, we have that G - e is still connected.
Now G - e is connected with n vertices, and so |G - e| n -1 edges, a
contradiction (to the fact that G - e contain only (n-2) edges, and so G
contains no circuits. Therefore, G is a tree.
Definition: A connected graph is said to be minimally connected if the
removal of any one edge from the graph provides a disconnected graph.
Example:
i) Graph given in Fig. 10.2 A is not minimally connected.
ii) Graph given in Fig10.2 B is minimally connected.
iii) Every circuit is not minimally connected.
iv) Every tree is minimally connected.





Fig. 10.2 A Fig. 10.2 B

Problem: A graph G is a tree if and only if it is minimally connected.
Solution: Part (i): Assume that G is a tree. If G is not minimally connected,
then there exists an edge e such that G - e is connected. That is, e is in
some circuit, which implies G is not a tree, a contradiction. Hence G
minimally connected.
Part (ii): Suppose that G is minimally connected.
If G contains a circuit, then by removing one of the edges in the circuit, we
get a connected graph, a contradiction (to the supposition). This shows that
G contains no circuits. Thus G is a tree.
Observation: To join n given distinct points, the minimum number of line
segments needed is n -1.
Problem: If a graph G contains n vertices, n -1 edges and no circuits, then
G is a connected graph.
Proof: Let G be a graph with n vertices, n 1 edges and contains no circuits.
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 181
Suppose that (in a contrary way) G is disconnected.
G consists of two or more circuitless components (say, g
1
, g
2
, , g
k
) (here k 2).
Choose a vertex v
i
in g
i
, for 1 i k.
Add new edges e
1
, e
2
, , e
k-1
where e
i
=
1 i i
v v to get a new graph G
*
.
It is clear that G
*
contains no circuits and connected, and so G
*
is a tree.
Now G
*
contains n vertices and
(n - 1) + (k - 1) = (n + k - 2) n edges, a contradiction (since a tree
contains (n - 1) edges). This shows that G is connected.
Characterization for Tree
For a given graph G, the following conditions are equivalent:
i) G is connected and is circuit less,
ii) G is connected and has n -1 edges,
iii) G is circuitless and has n -1 edges,
iv) There is exactly one path between every pair of vertices in G,
v) G is a minimally connected graph,
vi) G is a tree.
Problem: If G is a tree (with minimum two vertices), then there exists at
least two pendant vertices.
Proof: Let G be a tree with |V| 2.
Let v
0
e
1
v
1
e
2
v
2
e
3
. . . v
n-1
e
n
v
n
be a longest path in G (since G is finite graph,
it is possible to find a longest path).
Now we show that d(v
0
) = 1 = d(v
n
).
If d(v
0
) > 1, then there exists at least one edge e with end point v
0
such
that e e
1
. If e {e
1
, e
2
, e
n
}, then e = e
i
for some i 1.
So either v
i-1
= v
0
or v
i
= v
0
v
0
repeated in the path, a contradiction.
Therefore, e {e
1,
e
2,
e
3 ,
e
n
} and so e
,
e
1,
e
2,
e
3 ,
e
n
is a path of length
n + 1, a contradiction.
Thus, d(v
0
) = 1. In a similar way, we can show that d(v
n
) = 1. Hence v
0,
v
n
are two pendant vertices.
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 182
Definition: Let G be a connected graph. The distance between two vertices
v and u

is denoted by d(v , u) and is defined as the length of the shortest
path [that is, the number of edges in the shortest path] between v

and u. In
a connected graph, we can find the distance between any two given
vertices.
Example:
i) Consider the connected graph given in Fig. 10.3A. Here some of the
paths between v
1
and v
2
are (a, e), (a, b, f), (b, c, e), (b, f), (b, g, h), (b,
g, i, j), (b, g, i, k). Here there are two shortest paths (a, e) and (b, f)
each of length 2. Hence, d(v
1
, v
2
) = 2.







Fig. 10.3 A

ii) Consider the tree given in the Fig. 10.3 B. Here d(a, b) = 1, d(a, c) =
2, d(a, d) = 2, d(b, d) = 1.



Fig. 10.3 B
Self Assessment Questions
1. Any connected graph with n vertices and n -1 edges is a .
2. A graph G is a tree if and only if it is .

10.3 Rooted Trees and Applications
Rooted trees are extensively used in the computer search methods, binary
identification problems, and variable length binary codes.
j
b
d
c
v
2
v
1
i
h
k
g
e
a
f
a
b
c
d
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 183
Definition: A tree in which one vertex (called the root) is distinguished from
all the other vertices, is called a rooted tree. In a rooted tree, the root is
generally marked in a small triangle (or small circle).
Example:
Distinct rooted trees with four vertices, were given in Fig. 10.4






Fig. 10.4
Generally, the term tree means trees without any root. However, they are
sometimes called free trees (or) non-rooted trees. A variety of rooted trees
(called the Binary rooted trees) is of particular interest.
Definition: A tree in which there is exactly one vertex of degree 2, and all
other remaining vertices are of degree one or three, is called a binary tree.













Fig-.10.5
v
3
v
2
v
4
v
6
v
8
v
5
v
11
v
7
v
1
v
10
v
13
v
9
v
12
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 184
i) The Fig. 10.5 represents a binary tree (since the only vertex v
1
is of
degree 2, and all other vertices are of degree either 1 or 3).
ii) The vertex of degree 2 (that is, v
1
) is distinct from all other vertices, this
vertex v
1
is the root.
iii) In a binary tree, the vertex with degree 2 serves as a root. So, every
binary tree is a rooted tree.
Properties of Binary trees
Property (i): The number of vertices n, in a binary tree is always odd.
Property (ii): The number of pendent vertices is
2
1 n
.
Property (iii): Number of vertices of degree 3 is
= n - p - 1 = n - (
2
1 n
) - 1 =
2
3 n
.
Example:
In the graph given in Fig. 10.5, we have that n = 13, p =
2
1 n
=
2
1 13
=
2
14
=7. Therefore, number of vertices of degree 3 is
2
3 n
=
2
3 13
= 5.
Definition:
A non-pendent vertex in a tree is called an internal vertex.
Observation:
i) The number of internal vertices in a Binary tree is

2
1 n
= (p - 1) where p = the number of pendent vertices.
ii) In the binary tree given in Fig. 10.5, the internal vertices are v
1
, v
3
, v
4
,
v
5
, v
6
, v
9
. These are 6 (=7 - 1 = p - 1) in number.
Definition: Let v

be a vertex in a binary tree. Then v is said to be at level I if
v

is at a distance of I

from the root.
Example:
A 13-vertex, 4-level binary tree is given in Fig. 10.6




Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 185












Fig. 10.6

Here, the number of vertices at levels 0, 1, 2, 3, 4 are 1, 2, 2, 4 and
4 respectively.
Definition: The sum of path lengths from the root to all pendent vertices is
called the path length (or) external path length of a tree.
Example:
i) The path length of the binary tree given in Fig. 10.6 is:
1 + 3 + 3 + 4 + 4 + 4+ 4 = 23.
ii) In the Figures 10.7 A and B, there are two 11-vertex binary trees.








Fig. 10.7 A
The path length of graph (fig. 10.7 A): 2 + 2 + 3 + 3 + 3 + 3 = 16.
level -1
level - 0
level -2
level -3
level 1
level 4
level 0
level 2
level 3
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 186









Fig. 10.7 B
The path length of graph (fig. 10.7 B): 1 + 2 + 3 + 4 + 5 + 5 = 20.
Search procedures:
Each vertex of a binary tree represents a test with two possible outcomes.
We start at the root. The outcome of the test at the root sends us to one of
the two vertices at the next level, where further tests are made and so on.
Reaching a specified pendent vertex (that vertex which represents the goal
of the search), terminates the search.
For such search procedures, it is often important to construct a binary tree in
which, for a given number of vertices n, the vertex frothiest from the root in
as close to the root as possible.
i) There can be only one vertex (the root) at level 0. Number of vertices
at level one is at most 2. Number of vertices at level two is at most 2
2

and so on. So the maximum number of vertices possible in a k -level
binary tree is 2
0
+ 2
1
+ 2
2
+ . . . + 2
k
.
So n 2
0
+ 2
1
+ 2
2
+ . . . + 2
k

ii) The maximum number among the levels of the vertices in a binary tree
is called height of the tree. So height = max { level of a vertex v /
v V}.
This height is denoted by l
max
.
iii) To construct a binary tree for a given n such that the farthest vertex
is as far as possible from the root, we must have exactly two vertices at
each level, except at the 0 level. So max l
max
=
2
1 n
.
level 0
level 5
level 1
level 2
level 4
level 3
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 187
An Application: (Coke Machine Problem): The machine is said to have a
sequence of tests (for example, it should be capable of identifying the coin
that is put into the machine). We suppose that five rupees coin, two rupees
coin, one rupee coin and fifty paise coin can go through the slot. So the
machine can identify only these four coins. Every coin put in, is to be tested
by the machine. Each test got the effect of partitioning the coins into two
complementary sets. [Suppose a coin is put into the machine. It should test
whether the coin is five rupee coin. If it is not a five rupees coin, then it
should test whether it is a two rupees coin and so on]. We suppose the time
taken for each test is
Test Pattern-1: One type of testing pattern was shown in Graph-(i), given
in Fig. 10.8 A.
Suppose the statistical data tells that
w
1
= probability of putting a Rs 5 coin = 0.5
w
2
= probability of putting a Rs 2 coin = 0.2
w
3
= probability of putting a Rs 1 coin = 0.2
w
4
= probability of putting a Rs 0.5 coin = 0.1
Now
) v ( l w
i i
= ) v ( l w
1 1
+ ) v ( l w
2 2
+ ) v ( l w
3 3
+ ) v ( l w
4 4

= (0.5) (1) + (0.2) (2) + (0.2) (3) + (0.1) (4) = 1.9












Fig. 10.8 A
not Rs 0.5 coin
Rs 0.5 coin
Rs 1 coin
Rs 5 coin
v
3

0.2
v
1

v
2

v
4

0.1
0.2
0.5
not Rs 1 coin
not Rs 2 coin
not Rs 5 coin
Rs 2 coin
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 188
So expected time to be taken by the machine for testing one coin is 1.9t.
Thus, if the machine follows (for its testing pattern) the binary tree given in
Graph-(a), then the expected time for testing one coin is equal to 1.9t.
Test Pattern-2: Another type of testing pattern was given in the
Fig. 10.8 B.
) v ( l w
i i
= ) v ( l w
1 1
+ ) v ( l w
2 2
+ ) v ( l w
3 3
+ ) v ( l w
4 4

= (0.5) (2) + (0.2) (2) + (0.2) (2) + (0.1) (2) = 2.0











Fig. 10.8 B
So, here, the expected time to be taken by the machine for testing one coin
is 2t.
Thus, if the machine follows (for its testing pattern) the binary tree given in
graph 10.8 B then the expected time for testing one coin is 2t.
Definition: A graph G in which every vertex is assigned a unique name or
label (that is, no two vertices have the same label) is called a labeled graph.
Otherwise is said to be an unlabeled graph.
Example:
If n = 4, then there are 16 trees, shown below.
Take the vertex set V = {A, B, C, D}.


v
1
v
2

v
3

v
4

0.1
0.2
0.2
0.5
Rs 5 or Rs 2
Rs 5
Rs 2
Rs 1
Rs 0.50
Rs 1 or Rs 0.50
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 189
The following are the 16 trees of four labeled vertices




























B
C
A
D
A
D C
B
A
C D
B
C
B A
D
A B
D C D
C
B A
A
B
C D
A B
C D
A
C
B
D
B
D
A
C
A B
C
D
A B
C
D
A B
C
D
A
B
C
D
A
B
C D
A
B
C
D
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 190
Observations:
i) When we count the number of different graphs, the distinction between
labeled and unlabeled graphs is important.
ii) Consider the graphs in above example the 5
th
, 6
th
, 7
th
and 8
th
, are
counted as four different trees (even through they are isomorphic),
because they are labeled. If there is no distinction between A, B, C,
D, then these four trees counted as one.
iii) A careful inspection of the 16 graphs will reveal that the number of
unlabeled trees with four vertices (no distinction made between A, B,
C, D), is two. These two graphs were.



Cayleys Theorem:
The number of labeled trees with n vertices (n 2) is n
(n-2)
.
Example:
Part-(i): Suppose the given tree is T
1
(Fig. 10.9 A). a
1
= the pendent vertex
with smallest label. So, a
1
is the vertex 2. Now b
1
= 1. After removing a
1
and the edge (a
1
, b
1
), in the remaining graph, a
2
= the pendent vertex with
smallest index = 4.











Fig. 10.9 A

a
1

1
2
6
5
3
7
9
4
8
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 191
Now b
2
= 1. a
3
= 1, b
3
= 3, a
4
= 3, b
4
= 5, a
5
= 6, b
5
= 5, a
6
= 7, b
6
= 5, a
7
= 5,
b
7
= 9.
Therefore, we have the sequence (b
1
, b
2
, b
3
, b
4
, b
5
, b
6
, b
7
)=(1, 1, 3, 5, 5, 5, 9).
Part (ii): (converse of part (i)): we have to construct a tree with n = 9 vertices.
Consider 1, 2, 3, 4, 5, 6, 7, 8, 9 . . . . . . . . . (i)
Given (n - 2)-tuple is (1, 1, 3, 5, 5, 5, 9) . . . . . . . (ii)




Fig. 10.9 B

Observe the sequence in (ii). First join 1 and 3 , 3 and 5, 5 and 9.
Then, we get the graph given in Fig. 10.9 B. Now the least number in (i)
which is not in (ii), is 2. So we join 2 and 1. Next least in (i) which is not in (ii)
is 4. So we join 4 and 1. Cancel 3 in (i) and 3 in (ii). Also cancel 5 in (i) and 5
in (ii) (in (ii), canceling of only one 5 is allowed). The next least which is not
in (ii) is 6. So we join 5 and 6. The next least which is not in (ii) is 7. So we
join 7 and 5. The next least which is not in (ii) is 8. The remaining number
available in (ii) is 9. So, we join 8 and 9.











Fig. 10.9 C
1
3
5
9
8
1
2
6
5
3
7
9
4
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 192
Now, we will study the tree as a subgraph of another graph. A given graph
may have numerous subgraphs. If e is the number of edges in G, then
there are 2
e
distinct subgraphs possible. Obviously, some of these
subgraphs will be trees. Out of these trees, we are particularly interested in
certain type of trees, called spanning trees.

Self Assessment Questions
3. A tree in which one vertex (called the root) is distinguished from all the
other vertices, is called a .
4. The number of pendent vertices is .

10.4 Spanning Trees
Definition: A tree T is said to be a spanning tree of a connected graph G
if T is a subgraph of G and T contains all the vertices of G.
Example:
Consider the graph G given in Fig. 10.10 A. Graph T (given in Fig. 10.10B)
is a spanning tree of G.














Fig. 10.10 A: Graph-G Fig. 10.10 B: Graph-T
v
3

v
1

v
4

v
2

v
5

v
6

v
7

b
2

b
3

b
1

b
6

b
4

c
5

c
6

c
8

c
7

c
1

b
5

c
2

c
3

c
4

v
7

v
1

v
2

v
3

v
4

v
5

v
6


Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 193
Observations:
i) Spanning trees are the largest (with the maximum number of edges)
trees among all trees in G. Spanning tree is also called a maximal tree
subgraph or maximal tree of G.
ii) Spanning is defined only for a connected graph (since a tree is always
connected).
iii) Each component of a disconnected graph, does have a spanning tree.
Thus, a disconnected graph with k components contains a spanning
forest consisting of k spanning trees.
Theorem: Every connected graph has at least one spanning tree.
Definition:
i) An edge in a spanning tree T is called a branch of T.
ii) An edge of G that is not in a given spanning tree T is called a
chord. In electrical engineering, chord some times referred to as tie or a
link.
iii) Let T be any spanning tree of a connected graph G, and T
1
is the
complement of T in G. Then, each edge in T is called a branch (with
respect to T), and the set of all edges in T is called the branch set.
Each edge in T
1
is called a chord (with respect to T), and the set of
edges in T
1
is called the chord set (or) Tie set. T
1
is called as the
cotree. We may write T instead of T
1
.
Observations:
i) Branches and chords are defined only with respect to a given spanning
tree.
ii) An edge that is a branch with respect to one spanning tree T
1
(of G)
may be a chord with respect to another spanning tree T
2
.
Theorem: With respect to any of its spanning trees, a connected graph of
n vertices and e edges has n-1 tree branches and e - n + 1 chords.
Proof: Let G be any connected graph on n vertices and e edges. Let T be
any spanning tree in G. Since every spanning tree of G contains all vertices
of G, we have that |V(T)| = n and so |E(T)| = |V(T)| - 1 = n - 1. Since
every edge of a spanning tree T is called a branch of T, we have that G
contains n - 1 branches. Since the number of edges in G is e , we have
that the number of chords of T is e - (n - 1) = e - n + 1.
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 194
Example:
There is a form consisting of six walled plots of land as shown in the fig and
these plots are full of water. We find the minimum number of walls are to be
broken so that all the water can be drained out.
Consider the wall joints as vertices, and walls as edges. Then, we can
consider it as a graph. In this graph, the number of vertices is n = 10, and
the number of edges is e = 15.






Fig. 10.11
If there exists a circuit, then the water inside the circuit cannot be drained
out. So, we have to remove minimum number of edges so that the graph do
not contain circuits. To have this, we should have a spanning tree with
(n - 1) edges. Hence, we have to break e - (n - 1) = e - n + 1 = 15 - 10
+ 1 = 6 edges (walls) so that all the water can be drained out.
If we add an edge between any two vertices of a tree, then a circuit is
created. This is because, there already exists one path between any two
vertices of a tree, adding an edge in between, creates an additional path,
and hence a circuit. The concept of a fundamental circuit has an enormous
significance in electrical network analysis.
Definition:
Let T be any spanning tree of a connected graph G. Adding any one chord
to T will create exactly one circuit. Such a circuit formed by adding a chord
to a spanning tree, is called a fundamental circuit.
Example:
Consider the graph G (given in Fig. 10.12A), and its spanning tree T (given
in Fig. 10.12 B) of G. Now, if we add the chord c
1
to T, we get a circuit
b
1
b
2
b
3
b
5
c
1
which is called as fundamental circuit (given in Fig. 10.12C).

Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 195








Fig. 10.12 A Fig. 10.12 B Fig. 10.13 C
Graph-G Spanning tree-T Fundamental circuit-F

Note: A connected graph G is a tree if and only if adding an edge
between any two vertices in G creates exactly one circuit.
Note: For a tree G is a tree, by adding a new edge between any two
vertices in G creates exactly one fundamental circuit.

Self Assessment Question
5. Every connected graph has at least one .

10.5 Algorithms for Spanning Trees
Definition:
i) A graph G is said to be a weighted graph if all the edges e of G were
assigned by a corresponding real number (e) (read as the weight of e).
ii) Let T be any spanning tree of a connected graph G. then the weight of
T, that is, (T) is defined as the sum of weights of all branches in T.
iii) A spanning tree with the smallest weight in a weighted graph is called a
shortest spanning tree (or) minimal spanning tree (or) shortest-distance
spanning tree.
Observation:
i) Different spanning trees of G may have different weights. Among all the
spanning trees of G, one with the smallest weight is of practical
significance.
c
6

c
5
c
7
c
8
c
4
c
3

Fi
g.
1
0.
1
0
B
G
ra
p
h-
T

c
1
b
2
b
1
b
4
b
5
b
3
b
6
c
2
b
1

b
2

b
3

b4
b
6

b
5

c
1

b
1

b
2

b
3

b4
b
6

b
5

Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 196
ii) Let G be a graph on n vertices in which every edge has a unit weight .
Then all the spanning trees have the same weight of (n - 1) units.
Applications:
i) Suppose that we are to connect n cities v
1
, v
2
, , v
n
through a network
of roads.
ii) The cost C
ij
of building a direct road between v
i
and v
j
is given for
pairs of cities v
i
and v
j
where roads can be built. (Note that there may
be pairs of cities between which no direct road can be built).
iii) Now the problem is: To find the least expensive network that connects
all n cities together. That is, to find a shortest spanning tree in a
connected weighted graph of n vertices.
iv) Here the connected network must be a tree. Otherwise, we can always
remove some edges and get a connected graph with smaller weight.
Thus the problem of connecting n cities with a least expensive
network is the problem of finding a shortest spanning tree in a
connected weighted graph of n vertices.
10.5.1 Kruskal Algorithm:
This algorithm is used in finding shortest spanning tree
Step-(i): List all the edges of G in order of non-decreasing weight. Now we
select an edge e
1
of G such that (e
1
) is as small as possible and e
1
is not
a loop.
Step-(ii): Select next smallest edge from the set of all remaining edges of
G such that the selected edge do not form a circuit with the edges that
have already been chosen.
Step-(iii): We continue this process of taking smallest edges among those
not already chosen, provided no circuit is formed with those, that have been
chosen already.
[If edges e
1
, e
2
, , e
i
have been chosen, then chose e
i+1
from E -
{e
1
, e
2
, , e
i
} in such way that graph with {e
1
, e
2
, , e
i+1
} is acyclic and
(e
i+1
) is as small as possible].
Step-(iv): If a graph G has n vertices, then we will stop this process
after choosing n - 1 edges. These edges form a subgraph T, which is not
cyclic. (Thus T is a shortest spanning tree of G).
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 197
Example: Consider the connected weighted graph G. Listing:
) 1 ( AB , ) 1 ( AD , ) 2 ( BC , ) 2 ( CD , ) 3 ( AE , ) 3 ( FE , ) 3 ( AF , ) 4 ( EB ,
) 4 ( AC , ) 4 ( ED , ) 5 ( FD .
Step-(i): Consider a spanning subgraph of G without edges.
Select the edge with minimum weight. For this graph the edge AB is of
minimum weight.
Add this edge e
1
= AB to the spanning null graph. Then, we get the
subgraph-G
1
.
Step(ii): Select the edge e
2
such that G
1
+ e
2
contains no circuits and e
2

got minimum weight among such edges. For this graph e
2
= AD. Now, add
e
2
= AD to G
1
. Then we get the subgraph-G
2
.
Step-(iii): We continue this process. For this example, we add e
3
= BC to
G
2
to get the subgraph-G
3
.









Graph-G Subgraph-G
1

Next, we add e
4
= AE to G
3
to get the subgraph-G
4
. Next, we add
e
5
= EF to get the subgraph-G
5
. If we add any edge to G
5
, we get a circuit.
So, the process is to be stopped here.





C
2
5
1
D
4
A
B
E
F
1
2 3 4
3
3
4
A
B
1
F
E
D
C
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 198







Subgraph-G
2
Subgraph-G
3











Subgraph-G
4
Graph G
5
or T
The final graph G
5
is a subgraph of G and contains no cycles. Thus G
5
is
a shortest spanning tree of G. Write T = G
5
. Now (T) = sum of the weights
of the branches in T = 1 + 2 + 1 + 3 + 3 = 10
10.5.2 Prim's Algorithm:
This algorithm is used to find the shortest spanning tree
Step- (i): Choose any vertex v
1
in G.
Step-(ii): Choose an edge e
1
= v
1
v
2
of G such that v
1
v
2
and e
1
has
the smallest weight among the edges of G incident with v
1
. [For
convenience, we can form a table indicating the weights].
Step-(iii): If the edges e
1
, e
2
, , e
i
have been already chosen involving
end vertices v
1
, v
2
, , v
i+1
, choose an edge e
i+1
, where e
i+1
= v
j
v
k
with
v
j
{ v
1
, v
2
, ,v
i+1
}, and v
k
{v
1
, , v
i+1
} such that e
i+1
has the smallest
weight among the edges of G with precisely one end in {v
1
, v
2
, , v
i+1
}.
A
B
E
1
F
1
2
D
C
3
3
F
A
B
E
1
1
2
D
C
3
F
E
A
B
1
1
2
D
C
C
A
B
1
1
D
F
E
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 199
Note that after adding e
i+1
,

the graph should be a cyclic.
Step-(iv): The process will stop after choosing the n-1 edges (otherwise,
repeat the step (iii)).
Illustration: Consider the connected weighted graph G. Let us start with C.











Step-(i): Take the spanning null subgraph G
0
of G.
Let us choose v
1
= A. The edge AD is incident on v
1
and has the smallest
weight among the edges incident on v
1
= A. Write G
1
= (G
0
+ AD)
Step-(ii): In this step we select DC and write G
2
= (G
1
+ DC).
Step-(iii): We select AB and write G
3
= (G
2
+ AB).
Step-(iv): We select AF and write G
4
= (G
3
+ AF).
Step-(v): We select FE and write G
5
= (G
4
+ FE).
Step-(vi): Since G
5
is a spanning tree, the process stops here.
Write T = G
5.




5
1
D
4
2
A
C
B
E
F
1
2 3 4
3
3
4

Given graph G Graph-G
1

D
A
F
E
B
C
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 200







Graph-G
2
Graph-G
3
Graph-G
4
Graph-G
5
= T
Hence, the shortest spanning tree of G is G
5
= T.
Now, (T) = 1 + 1 + 2 + 3 + 3 = 10.
Self Assessment Questions
6. Draw trees with 5 vertices.
7. Find all spanning trees of the graph.



8. Find all spanning trees of the following grpah.



9. The complete graph on n vertices K
n
: has
2 n
n different spanning
trees. Give all the spanning trees of K
4
.







A
A
B
E
1
F
1
2
D
C
3
3
B
1
F
1
2
D
C
3
E
A
B
1
1
2
D
C
F
E
A
1
1
D
C
F
E
B
o o o
o
o
o o o
o o
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 201
10. Find a spanning tree for each of the following graphs











11. Which connected simple graphs have exactly one spanning tree ?
12. Use Prims Algorithm to find a minimal spanning tree for the following
weighted graph.







10.6 Summary
This unit dealt with a special type of graphs namely trees, which are
extensively used in search procedures and design of computer algorithms.
You got an idea to find the different spanning trees from a given connected
graph.
Different characterizations and properties of trees were given. Also, some
applications of binary trees were discussed.
This unit highlights that spanning trees play a vital role in multicasting over
internet protocol net works.
(i)
o
o
o
o o
o o
o o
(ii)
o
o
o
o
(iii)
o
o
o o
o
(iv)
o
o
o o o
o
o o
o
8
A
3
6
4
7
2
4
6
8
6
C
B
E
D
o
o
o
o
o
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 202
10.7 Terminal Questions
1. Draw two different binary trees with five vertices having maximum
number of leaves.
2. Draw a graph with the given specification. In case, if not possible, then
explain why no such graphs exist.
i) full binary tree, five internal vertices
ii) full binary tree, five internal vertices, seven terminal vertices
iii) full binary tree, twelve vertices
iv) full binary tree, nine vertices.
3. If T
1
and T
2
are two spanning trees in a graph, then show that
i) The number of edges in T
1
not in T
2
is equal to the number of
edges in T
2
not in T
1
. (ii) d(T
1
, T
2
) = d(T
2
, T
1
).
4. Let N(g) = the number of edges in a graph g. Then N(T
i
T
j
) = the
number of edges in T
i
T
j
, and d(T
i
, T
j
) = The distance between T
i
and T
j
. Show that d(T
1
, T
2
) =
2
1
N(T
1
T
2
).
5. Consider the table of airline distances in miles between six of largest cities
in the world: London, Mexico, New York, Paris, Peking and Tokyo.
L MC NY Pa Pe T
L - 5558 3469 214 5074 5959
MC - 2090 5725 7753 7035
NY - 3636 6844 6757
Pa - 5120 6053
Pe - 1307
T -
Write the spanning tree for the above

10.8 Answers
Self Assessment Questions
1. Tree
2. Minimally connected
3. Rooted tree
4.
2
1 n

5. Spanning tree
L
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 203
6.





7. The given graph has four vertices and so each spanning tree must
have 4 1 = 3 edges. They are









8.


9. K
4
has
2 4
4 = 16 different spanning trees. Each having three edges.

10. 100.













o
o
o
o
o
o o
o
o
o
o
o
o
o
o
o o
o
o
o
o o o o o o
o
o o
o
o
o
o
o o o o o
o o
o o o o o
o
o
o o
o
o
o o o o o
o o o o
o
o o o
o
(i)
o
o o
o
o
o
o
o
o
(ii)
o
o
o o
(iii)
o
o
o
o
o
(iv)
o
o o
o
o
o o
o
o
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 204
11. Tree.
12. Choose edges AE, AC, DC, AB.

Terminal Questions

1.



2. i.










ii. Not possible, since any full binary tree with five internal vertices have
six terminal vertices, but not seven.
iii. No, a full binary tree has 2k + 1 vertices where k is the number of
internal vertices but 2k + 1 is odd.
iv.






3. i) Let T
1
, T
2
be two spanning trees. Now |E(T
1
)| = n - 1 = |E(T
2
)|.
Suppose T
1
contains m edges which are not in T
2
. Now the number
of edges in T
2
not in T
1
is
(n - 1) - [( n - 1) - m] = m. follows from (i).
b
c
a
e
f
g
j k
o
o
o
o
o
o
o
d
h
i
o o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o o
o
o
o o
o
Fundamentals of Algorithms Unit 10

Sikkim Manipal University Page No: 205
4. N(T
1
T
2
) = number of edges in T
1
T
2
= (number of edges in T
1
but
not in T
2
) + (number of edges in T
1
but not in T
2
) = d(T
1
, T
2
) + d(T
2
, T
1
)
= 2 d(T
1
, T
2
) d(T
1
, T
2
) =
2
1
N(T
1
T
2
)
5.














Fig. A Fig.B: Shortest spanning tree

6757
5074
130
7
T
P
e
Pa
214
2090
3469
5074
1307
M
C
NY
T
L
Pa
Pe
5725
6053
7753
3469
5120
3636
5558
2090
595
9
MC
NY
7035
684
4
214

You might also like