You are on page 1of 19

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 46


Unit 3 Trees and Binary Trees
Structure
3.1 Introduction
Objectives
3.2 Graphs
3.3 Trees
3.4 Binary Trees
3.5 Walks and Paths
3.6 Directed Graphs
3.7 Summary
3.8 Terminal Questions
3.9 Answers

3.1 Introduction
The last three decades have witnessed more interest in Graph Theory,
particularly among applied mathematicians and engineers. Graph Theory
has a surprising number of applications in many developing areas. The
Graph Theory is also intimately related to many branches of mathematics
including Group Theory, Matrix Theory, Automata and Combinatorics.
Graph Theory serves as a mathematical model for any system involving a
binary relation.
Objectives:
At the end of the unit you would be able to
- learn the concept, graph and illustration.
- know the Trees and Binary Trees.
- know the weighed graphs and applications.
- find the spanning tree of weighted graphs
- define a directed graph
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 47
3.2 Graphs
3.2.1 Definitions
- A graph G = (V, E) consists of a nonempty set of objects,
V = {v
1
, v
2
, } called vertices and another set, E = {e
1
, e
2
, } of
elements called edges such that each edge e
k
is identified with an
unordered pair {v
i
, v
j
} of vertices.
- The vertices v
i
, v
j
associated with edge e
k
are called the end vertices of
e
k
. An edge associated with a vertex pair {v
i
, v
i
} is called a loop (or) self-
loop.
- If there is more than one edge associated with a given pair of vertices,
then these edges are called parallel edges (or) multiple edges.
3.2.2 Example
Consider the graph given here.






This is a graph with five vertices and seven edges. Here G = (V, E) where
V = {v
1
, v
2
, v
3
, v
4
, v
5
} and E = {e
1
, e
2
, e
3
, e
4
, e
5
, e
6
, e
7
}.
The identification of edges with the unordered pairs of vertices is given by
e
1
{v
2
, v
2
}, e
2
{v
2
, v
4
}, e
3
{v
1
, v
2
},
e
4
{v
1
, v
3
}, e
5
{v
1
, v
3
}, e
6
{v
3
, v
4
}.
Here e
1
is a loop and e
4
, e
5
are parallel edges.
3.2.3 Definition
A graph that does not have self-loop nor even parallel edges is called a
v
1
e
1
e
6
e
7
e
2
e
3
e
4
v
3 v
4
v
5
e
5
v
2
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 48
simple graph. Graph containing either parallel edges or loops is referred as
general graph.
3.2.4 Example
It can be observed that the two graphs given in Figures A and B are one and
the same.







3.2.5 Definition
i) If a vertex v is an end vertex of some edge e, then v and e are said to
be incident with (or on, or to) each other.
ii) Two non-parallel edges are said to be adjacent if they are incident on a
common vertex. Two vertices are said to be adjacent if they are the
end vertices of the same edge.
iii) The number of edges incident on a vertex v is called the degree of v.
The degree of a vertex v is denoted by d(v). It is to be noted that a self-
loop contributes two to the degree of the vertex.
3.2.6 Theorem
The sum of the degrees of the vertices of a graph G is twice the number of
edges. That is,

eV
i
v
i
) v ( d = 2e. (Here e is the number of edges).
Proof: (The proof is by induction on the number of edges e).
Case-(i): Suppose e = 1. Suppose f is the edge in G with f = uv.
Then d(v) = 1, d(u) = 1. Therefore,
4
3
2
1
Fig-A
2
4
3
1
Fig-B
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 49

eV x
) x ( d =
=

e } v , u { \ V x
) x ( d + d(u) + d(v)
= 0 + 1 + 1 = 2 = 2
= 2 (number of edges).
Hence the given statement is true for n = 1.
Now we can assume that the result is true for e = k - 1.
Take a graph G with k edges.
Now consider an edge f in G whose end points are u and v.
Remove f from G.
Then we get a new graph G
*
= G - {f}.
Suppose d
*
(v) denotes the degree of vertices v in G
*
.
Now for any x e {u, v}, we have d(x) = d
*
(x), and
d
*
(v) = d(v) - 1, d
*
(u) = d(u) - 1.
Now G
*
has k - 1 edges. So by induction hypothesis

eV
i
v
i
*
) v ( d = 2(k - 1).
Now
2(k - 1) =

eV
i
v
i
*
) v ( d =

e } v , u {
i
v
i
*
) v ( d + d
*
(u) + d
*
(v)
=

e } v , u {
i
v
i
) v ( d + (d(u) - 1) + (d(v) - 1)
=

e } v , u {
i
v
i
) v ( d + d(u) + d(v) - 2 =

eV
i
v
i
*
) v ( d - 2
2(k - 1) + 2 =

eV
i
v
i
*
) v ( d 2k =

eV
i
v
i
) v ( d .
Hence, by induction we get that the sum of the degrees of the vertices of
the graph G is twice the numbers of edges.
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 50
3.2.7 Problem
The number of vertices of odd degrees is always even.
Solution: We know that the sum of degrees of all the n vertices (say, v
i
,
1s i s n) of a graph G is twice the number of edges (e) of G. So we have

=
n
1 i
i
) v ( d = 2e --------- (i)
If we consider the vertices of odd degree and even degree separately, then

=
n
1 i
i
) v ( d =

iseven
j
v
j
) v ( d +

isodd
k
v
k
) v ( d -------- (ii)
Since the L.H.S of (ii) is even (from (i)) and the first expression on the RHS
side is even, we have that the second expression on RHS is always even.
Therefore,

odd is
k
v
k
) v ( d --------- (iii)
is an even number.
In (iii), each d(v
k
) is odd. The number of terms in the sum must be even to
make the sum an even number. Hence the number of vertices of odd
degree is even.
3.2.8 Definition
A graph H is said to be a sub-graph of a graph G if all the vertices and all
the edges of H are in G, and each edge of H has the same end vertices in
H as in G.
3.2.9 Example
The graphs H and K are subgraphs of graph G.



Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 51
1
6
f











Self Assessment Questions
1. Every graph is a ______of itself;
2. A subgraph of a subgraph of G is a _______of G;
3. A single vertex in a graph G is a _____of G
4. A single edge in G together with its end vertices is a ____of G.

3.3 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.
3.3.1 Definition
A connected graph without circuits is called a tree.
3.3.2 Example
Trees with one, two, three and four vertices are given.


1
2
3
6
i
b
d
j

Graph K
3

Graph H
4
1
b
d
c
a
e
2
g
4
2
3
a
5
b
c
d
e
h
j
i

Graph G
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 52
3.3.3 Note
- Since a tree is a graph, we have that a tree contains at least one vertex.
- A tree without any edge is referred to as a null tree.
- Since we are considering only finite graphs, we have that the trees
considered are also finite.
- A tree is always a simple graph.
3.3.4 Definition
A connected graph is said to be minimally connected if the removal of any
one edge from the graph provides a disconnected graph.
Observation:
- A graph G is a tree it is minimally connected.
- If a graph G contains n vertices, n -1 edges and no circuits, then G is a
connected graph.
- If G is a tree (with two or more vertices), then there exists at least two
pendant vertices.
3.3.5 Theorem
For a given graph G, with n vertices the following conditions are equivalent:
i) G is connected and is circuitless;
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 tree.

3.4 Binary Trees
3.4.1 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.
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 53
3.4.2 Example
Distinct rooted trees with four vertices were given in Fig.






Generally, the term tree means trees without any root. However, they are
sometimes called free trees or non-rooted trees. A verity of rooted trees
(called the Binary rooted trees) is of particular interest (since they are
extensively used in the computer search methods, binary identification
problems, and variable length binary codes).
3.4.3 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.
i) Clearly, the following graph represents a binary tree (because the only
vertex v
1
is of degree 2, and all other vertices are of degree either 1 or 3).











v
1
v
3
v
2
v
4
v
6
v
8
v
5
v
11
v
7
v10
v
1
3
v
9
v
1
2
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 54
ii) Since 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.
3.4.4 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
.
3.4.5 Example
In the above graph, 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.
3.4.6 Definition
A non-pendent vertex in a tree is called an internal vertex.
Note:
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 above binary tree, 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.


Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 55
3.4.7 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.
3.4.8 Example
i) A 13-vertex, 4-level binary tree was given in Fig.












Here the number of vertices at levels 0, 1, 2, 3, 4 is 1, 2, 2, 4 and 4
respectively.

level 1
level 4
level 0
level 2
level 3
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 56
Self Assessment Question
5. Draw the 11-vertex binary trees and also find its path length.

3.5 Walks and Paths
3.5.1 Definition
- A finite alternating sequence of vertices and edges (no repetition of edge
allowed) beginning and ending with vertices such that each edge is
incident with the vertices preceding and following it, is called a walk.
- Vertices with which a walk begins and ends are called the terminal
vertices of the given walk.
- The remaining vertices in the walk are called intermediate vertices of the
walk.
- A walk is said to be a closed walk if the terminal points are same.
3.5.2 Example
i) From the above definition, it is clear that no edge appears more than
once in a walk.
ii) Consider the graph given. We can observe that v
1
av
2
bv
3
cv
3
d v
4
e v
2
f v
5

is a walk.










c
v
4
v
3
v
5
v
2
v
1
g
b
d
e
h
f
a
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 57
The set of vertices and edges constituting a given walk in a graph G forms a
subgraph of G. A walk which is not closed, is called an open walk. [In other
words, a walk is said to be an open walk if the terminal points are different].
In the above example, the walk v
1
av
2
bv
3
cv
1
is a closed walk and the walk
v
1
av
2
bv
3
dv
4
ev
2
fv
5
is an open walk.
3.5.3 Definition
An open walk, in which no vertex appears more than once, is called a path
or simple path.
(In other words, an open walk is said to be a path if it does not intersects
itself. The number of edges in a path is called the length of the path).
3.5.4 Example
We can observe that v
1
av
2
bv
3
dv
4
is a path, and v
1
av
2
bv
3
cv
3
dv
4
ev
2
f v
5
is a
walk but not a path (because this walk contains repeated vertex v
2
).
i) Consider the graph given above, the path v
1
av
2
bv
3
dv
4


is of length 3.
ii) An edge that is not a self-loop is a path of length one.
iii) A self-loop is a walk but not a path.
iv) Consider a path as a subgraph. With respect to the subgraph, we have
that terminal vertices of the path are of degree one and the rest of the
vertices (these are called intermediate vertices) are of degree two. For
example, consider the path v
1
av
2
bv
3
dv
4
(refer the graph given). With
respect to this path d(v
1
) = 1, d(v
2
) = 2, d(v
3
) = 2, d(v
4
) = 1.
3.5.5 Note
Consider the graph given below Observe the following.
i) If there is no repetition of vertices in a walk, then the walk is a non-
intersecting walk.



Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 58






ii) Observe that a walk is intersecting there lies repetition of vertices.
iii) v
1
v
5
, v
5
v
3
, v
3
v
4
, v
4
v
5
, v
5
v
2
is a walk.
3.5.6 Definition
- A collection of trees is called a forest.
- 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.
3.5.7 Example
Consider the graph G given in Fig A. Graph T (given in Fig B) is a spanning
tree of G.














v
4
v
3
v
2
v
1
v
5
v
6

v
3

v
1

v
4

v
2

v
5

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

Fig- A Graph-G
v
7

v
1

v
2

v
3

v
4

v
5

v
6


Fig B
Graph-T
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 59
3.5.8 Note
i) Since spanning trees are the largest (with the maximum number of
edges) trees among all trees in G, we have that a 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). However, 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.
3.5.9 Theorem
Every connected graph has at least one spanning tree.
Proof: Let G be a connected graph. If G has no circuit, then G is a spanning
tree. If G has a circuit, then delete an edge from this circuit. If till leaves the
graph connected. If there are more circuits, repeat the operation till an edge
from the last circuit is deleted, leaving the graph connected, circuitless, and
contains all the vertices of G. Thus the subgraph obtained is a spanning tree
of G. Hence every connected graph has at least one spanning tree.
3.5.10 Definitions
- 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(e).
- Let T be any spanning tree of a connected graph G. Then the weight of
T, that is, e(T) is defined as the sum of weights of all branches in T.
- A spanning tree with the smallest weight in a weighted graph is called a
minimal spanning tree.
3.5.11 Note
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.

Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 60
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.
3.5.12 Kruskal algorithm for 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(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 edges 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(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).

Self Assessment Questions
7. Consider the following graph: Write the paths, lengths of path, cycles,
and also specify whether or not its simple path/closed path.

Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 61
3.6 Directed Graphs
3.6.1 Definition
A directed graph (or) a digraph G consists of a non-empty set V (the
elements of V are normally denoted by v
1
, v
2
, ) and a set E (the elements
of E are normally denoted by e
1
, e
2
, .) and a mapping that maps every
element of E onto an ordered pair (v
i
, v
j
) of elements from V.

The elements of V are called as vertices or nodes or points. The elements
of E are called as edges or arcs or lines. If e e E and v
i
, v
j
e V such that
(e) = (v
i
, v
j
), then we write e =
j
v
i
v . In this case, we say that e is
an edge between v
i
and v
j
. (We also say that e is an edge from v
i
to v
j
).
(We also say that e originates at v
i
and terminates at v
j
). (An edge from v
i

to v
j
is denoted by a line segment with an arrow directed from v
i
to v
j
).
3.6.2 Note
i) A directed graph is also refereed to as an oriented graph.
ii) Let G be a directed graph and e = vu.
3.6.3 Example
The graph given in figure is a digraph with 5 vertices and ten edges.







e
9
v
2
e
10
e
5
e
7
v
1
e
8
e
6
v
4
v
3
v
5
e
1
e
4
e
3 e
2
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 62
Here v
5
is the initial vertex and v
4
is the terminal vertex for the edge e
7
. The
edge e
5
is a self-loop.
3.6.4 Definition
- The number of edges incident out of a vertex v is called the out degree
of v. The out degree of a vertex v is denoted by d
+
(v).
- The number of edges incident into v is called the in-degree of v. The in-
degree of a vertex v is denoted by d
-
(v).
Observation: The degree of v is equal to the sum of in-degree and out
degree of v, for any vertex v in a graph.
(In symbols, we can write as d(v) = d
+
(v) + d
-
(v) for all vertices v).
Self Assessment Questions
7. Consider the graph given in Example: 3.6.3. Write the indegrees and
outdegrees of the graph.

3.7 Summary
This Unit is meant for beginning your process of learning Graph Theory. It
started with the definition of Graph and illustrated the concepts like
incidence, degree, isolated vertex, pendent vertex and null graph. We also
discussed the isomorphism between graphs and subgraphs of a given graph
with appropriate illustrations. We dealt with a special type of graphs called
trees and studied some properties. We considered the binary search tree
and their properties. We presented some illustrations on binary search tree,
complete binary search trees and directed graphs.

3.8 Terminal Questions
1. Define the terms: Graph, finite graph, infinite graph, incidence, degree,
isolated vertex, pendent vertex, null graph.
2. Show that the sum of the degrees of the vertices of a finite graph G is
twice the number of edges.
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 63
3. Show that the number of vertices of odd degree is always even.
4. Show that an infinite graph with finite number of edges must have an
infinite number of isolated vertices.
5. Show that the maximum degree of any vertex in a simple graph is (n - 1).
6. Draw all regular binary trees
a) with exactly 7 vertices
b) with exactly 9 vertices
7. Draw all distinct binary tree with (i) 3 vertices, (ii) 4 vertices.
8. Draw binary search trees for the following lists.
i) 18, 44, 2, 5, 73, 45, 14, 6, 8, 10, 20, 11
ii) 2, 1, 5, 6, 8, 9, 7, 3, 4.
7. Define a binary tree, complete binary tree and give examples of each.

3.9 Answers
Self Assessment Questions
1. Every graph is a sub graph of itself;
2. A subgraph of a subgraph of G is a subgraph of G;
3. A single vertex in a graph G is a subgraph of G; and
4. A single edge in G together with its end vertices is a subgraph of G.
5. (i)










level -1
level - 0
level -2
level -3
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 64
The path length: 2 + 2 + 3 + 3 + 3 + 3 = 16.
(ii)










The path length: 1 + 2 + 3 + 4 + 5 + 5 = 20.
6.
Path Length Simple
(yes/no)
Closed
(yes/no)
Circuit
(yes/no)
Cycle
(yes/no)
a-d-c-e-g-j-d-a 7 no yes no no
b-c-e-f-g-j-f-b 7 no yes yes no
a-b-a 2 No Yes No No
a-d-c-b-a 4 Yes Yes Yes Yes
i-i 1 Yes Yes Yes Yes
a 0 Yes Yes No No
e-f-g-j-f-b 5 No No No No
d-b-c-d 3 yes yes yes Yes

7.
i) Here d
+
(v
1
) = 3, d
+
(v
2
) = 1, d
+
(v
3
) = 1, d
+
(v
4
) = 1, d
+
(v
5
) = 4. d
-
(v
1
) = 1,
d
-
(v
2
) = 2, d
-
(v
3
) = 4, d
-
(v
4
) = 3, d
-
(v
5
) = 0.
ii) d(v
1
) = 4 = 3 + 1 = d
+
(v
1
) + d
-
(v
1
) ; d(v
2
) = 3 = 1 + 2 = d
+
(v
2
) + d
-
(v
2
),
and so on.

level 5
level 0
level 1
level 2
level 4
level 3

You might also like