Discrete Mathematics: Trees
Discrete Mathematics: Trees
Mathematics
Trees
1. Introduction to Trees
Def 1 A Tree is a connected undirected graph with no circuits.
Example
Definitions: a is the parent of b, b is the child of a,
a c, d, e are siblings,
a, b, d are ancestors of f
b c, d, e, f, g are descendants of b
c, e, f, g are leaves of the tree (deg=1)
c d e a, b, d are internal vertices of the tree
(at least one child)
f g
d
subtree with d as its root:
f g
Def 3: A rooted tree is called an m-ary tree if every internal vertex
has no more than m children. The tree is called a full m-ary tree if
every internal vertex has exactly m children. An m-ary tree with m=2
is called a binary tree.
Example 3
full binary tree full 3-ary tree full 5-ary tree not full 3-ary tree
Def:
a
b c
left child of a
d e f
right child of c
left subtree of a
right subtree of a
Theorem 2. A tree with n vertices has exactly n-1 edges.
Example level
0
1
Height of the tree = 4
2
3
4
Def: A rooted m-ary tree of height h is balanced if all
leaves are at levels h or h-1.
Sol. T1, T3
Ex How many vertices and how many leaves does a complete m-ary
tree of height h have?
Sol.
# of vertices = 1+m+m2+…+mh = (mh+1-1)/(m-1)
# of leaves = mh
Cut-edge, Cut-vertex
12 Graph Theory
Cut-edge, Cut-vertex
Cut-edge
Cut-vertex
13 Graph Theory
Trivial Tree: A Tree T with only one vertex ;other wise T is a nontrivial tree .
Directed Tree: A rooted tree is a directed tree if there is root from which there is a
directed path to each vertex.
Example:
Properties of Trees:
Sol.
Remove an edge from any circuit.
(repeat until no circuit exists)
Four spanning trees of G:
Ch10-19
Breadth First Search
Algorithm:
Input: A connected graph G with vertices labeled V1,V2,-----Vn.
Output: A spanning tree T for G.
Method:
1.Let V1 be the root of T. Form the set V={v1}.
2.(Add new edges) Consider the vertices of v in order consistent with the
original labelling. Then for each vertex xєV, add the edge {x,vk} to T where k
is the minimum index such that adding the edge {x,vk} to T does not produce
a cycle. If no edge can be added, then stop; T is a spanning tree for G. After
all the vertices of V have been considered in order, go to step3.
3.(Update V) Replace V by all the children v in T of the vertices x of V
where the edges {x,v} were added in step2.Go back and repeat step2 for the
new set v.
Ch10-20
BFS Example:
i
a d f j
e
h
b c g k
Solution:
a d a d a d a d f
e
b b c b c e
b c
g
i
a d f d f j
a
e h e
h
b c b c k
g g
Ch10-21
•
BFS:
BFS partitions the edges of the graph G into two sets i.e, tree
edges and cross edges.
Tree edges: The edges that were included in the Breadth First
search spanning tree.
Cross edges: The edges that were rejected in the breadth first
search spanning tree.
Ch10-22
Breadth-First Search (BFS)
Ch10-24
DFS Example:
a d f i j
e
h
b c g k
a d
a a f
a d a d
c e
b
b b c b c b
c e i
j
a d f a d f
a d f h
h
k
b e b c e c e
c g g b g
Ch10-25
DFS:
Tree edges: The edges selected by DFS of a graph.
Back edges: The edges which are rejected in the DFS.
Example:
a b c d
e f g
Ch10-27
Minimal Spanning Trees
Let G be the graph of all possible links between the cities with the non negative
cost of construction C(e) assigned to each edge e in G. Then if H is any sub
graph of G with edges e1,e2----,em the total cost of constructing the network H is
C(H)=Σi=1 to m C(ei).
A spanning tree T where C(T) is minimal is called a minimal spanning tree of G.
Ch10-29
Kruskal’s Algorithm for finding Minimum Cost
Spanning Tree
Input: A connected graph G with non negative values assigned to each edge.
Output: A minimum cost spanning tree for G.
Method:
1.Select any edge of minimal value that is not a loop. This is the first edge of
T.(If there is more than one edge of minimal value, arbitrarily choose one of
these edges).
2.Select any remaining edge of G having minimal value that does not form a
circuit with the edges already included in T.
3.Continue step2 until T contains n-1 edges, where n is the number of vertices
of G.
Ch10-30
Q) For the given graph, find the minimum cost spanning tree using Kruskal’s
algorithm.
Collect the lengths of edges in ascending order.
Edge Cost
{b,c} 3 Add the first edge in
a 15 b {d,f} 4 the spanning tree with
{a,g} 5 the least cost
5 15 3
5 {c,d} 5 a 15 b
g d c {c,e} 5
15 18 4 5 {a,b} 15 5
3
h {a,d} 15 g 5
15 f e {f,h} 15
15 c
{g,h} 15 d
{e,f} 15 4 5
{f,g} 18
h 15 f e
The minimum cost for the construction of the spanning tree is = 3+4+5+5+5+15+15
= 52
Ch10-31
Example: Use Kruskal’s algorithm to find a minimum
spanning tree of G.
Sol.
The minimum cost for the construction of the spanning tree is=24
Find the minimum cost spanning tree for the following
graphs using Kruskal’s algorithm
Ch10-33
Prim’s Algorithm
Ch10-34
Prim’s Algorithm
Example:
a b
a 15 b
5 15 3
5 15 3 g
5 d c
g c
d 5
15 18 4 5 15 4
h e h e
15 f 15 f 15
The minimum cost for the construction of the spanning tree is=3+5+15+4+15+5+15
= 62
Ch10-35
Example Use Prim’s algorithm to find a minimum
spanning tree of G.
Sol.
The minimum cost for the construction of the spanning tree is=24