Module 5 TREES
Module 5 TREES
Module 5 TREES
T1 T2 T3
› If there are two paths between a pair of vertices of T, the union two
paths become a cycle, and T cannot be tree.
› Thus, between every pair of vertices in a tree there must exist one
and only one path.
› ∴ In a tree, there is one and only one path between every pair
of vertices Dr G Manjula Associate Professor,DSATM 3
Theorem 2
If in a graph G there is one and only one path between every pair
of vertices, then G is a tree.
Proof:
› There is the existence of a path between every pair of vertices so
we assume that graph G is connected.
› A cycle in a graph implies that there is at least one pair of vertices a
and b, such that there are two distinct paths between a and b.
› Since G has one and only one path between every pair of vertices.
› G cannot have any cycle. Hence graph G is a tree.
› Let the statement be true for n=m. Now we want to prove that it is
true for n=m+1.
6
Dr G Manjula Associate Professor,DSATM
Theorem 4
A connected graph is a tree if and only if it is minimally connected.
Proof:
› Let the graph G is minimally connected, i.e; removal of one edge make it
disconnected.
› Therefore, there is no circuit. Hence graph G is a tree.
› Conversely, let the graph G is a tree i.e; there exists one and only one path
between every pair of vertices and we know that removal of one edge from
the path makes the graph disconnected.
(a)If v1, v2, v3 are any three vertices of Kn where n > 2, then the
closed walk v1, v2, v3, v1 is a cycle in Kn. Since Kn has a cycle, it
cannot be tree.
(b)Let v1 and v2 be any two vertices in the first bipartite and v1’
and v2’ in other bipartite of Kr,s with s ≥ r ≥ 2. then, the closed
walk v1 v1’ v2 v2’ v1 is a cycle in Kr,s . Since Kr, s has a cycle, it
cannot be tree.
Dr G Manjula Associate Professor,DSATM 8
Problems
Prove that a tree with two or more vertices contains at least two
leaves (pendant vertices)
› Proof: Let T be tree with n vertices where n ≥ 2. then it has n -1 edges.
› The sum of the degrees of n vertices must be equal to 2(n – 1).
› Thus, if d1, d2 d3, …..,dn are the degrees of vertices of T, we have
d1+ d2 + d3 ....,dn = 2(n -1) = 2n - 2
› If each of d1, d2 d3, …..,dn is ≥ 2, then their sum must be at least 2n. Since, this is
not true, at least one of the d’s is less than 2.
› Thus, there is d which is equal to 1, let us take d1 = 1
› 1+ d2 + d3 ....,dn -1 = 2 (n -1) = 2n -2
› d2 + d3 ....,dn -1 = 2n -2 – 1
› d2 + d3 ....,dn -1 = 2n – 3
› This is possible only if at least one of d2 d3, …..,dn is equal to 1.
› Thus in T, there are at least two vertices with degree 1.
› That is there are at least two pendant vertices (leaves)
› i.e. k + 3 (n - k) = 2 (n -1)
› n = 2k -2 = 2 (k -1)
› This shows that n is even number
3. Let T1 = (V1, E1) and T2 = (V2, E2) be two trees. If |E1|=19 and
|V2| = 3|V1|, determine |V1|, |V2| and |E2|.
• a, b, d are ancestors of f
• c, d, e, f, g are descendants of b
{-1, 7} {4} {5, 11} {-8} { -3, 15} { -2} { 6, 10} {3}
The sorted version of the given list is -8, -3, -2, -1, 3, 4, 5, 6, 7, 10, 11, 15
Optimal Prefix Code: The edges of the optimal tree are symbolically labelled
0(left child) and 1(right child) appropriately. This will facilitate to identify all
the vertices and leaves of the tree by binary sequences. These sequences
generates a prefix code for the symbols representing the leaves and the prefix
code is referred to as an optimal prefix code.
Example:
2. Obtain the optimal prefix code for the message ROAD IS GOOD.
Indicate the code.
Step 1: Arrange the symbols (vertices) such that their frequencies(weights) are in
increasing order.
q z y u a o
(4) (7) (12) (17) (20) (28)
Step 2: Two smallest weights (q, z) are added and the resulting weight (4+7=11) be
associated with new vertex say v1. A tree having v1 as root and (q, z) as its children is
drawn along with the other vertices in the increasing order of weights.
v1 (11)
y u a o
(12) (17) (20) (28)
u a o
v2(23)
(17) (20) (28)
v1 (11) y(12)
q (4) z(7)
v3(37)
v2(23)
o (28)
v1 (11) y(12)
u (17) a(20)
q (4) z(7)
v4 (51)
v3(37)
v2(23) o (28)
u (17) a(20)
v1 (11) y(12)
q Z
(4) (7)
Dr G Manjula Associate Professor,DSATM 33
Problems on optimal prefix code
Step 6: w(v3) + w(v4) = 37 + 51 = 88 be associated with new vertex v5.
This step being final one, this new vertex becomes the root of the tree be
denoted as v5 which will have v3 and v4 as its children.
v5 (88)
v4 (51)
v3(37)
v2(23)
u (17) o (28)
a(20)
v1 (11) y(12)
q(4) z(7)
Dr G Manjula Associate Professor,DSATM 34
Problems on optimal prefix code
Huffmann optimal tree as follows
v5 (88)
0 1
v4 (51)
v3(37)
0 1 0 1
v2(23)
u (17) a(20) o (28)
0 1
v1 (11) y(12)
1
0
q(4) z(7)
The optimal prefix code for the symbols are tabulated Dr G Manjula
Associate
a o q u y z Professor,DSATM
01 11 1000 00 101 1001 35
Problems on optimal prefix code
Huffmann optimal tree as follows
v5 (88)
0 1
v4 (51)
v3(37)
0 1 0 1
v2(23)
u (17) a(20) o (28)
0 1
v1 (11) y(12)
1
0
q(4)
z(7)