DM Trees1
DM Trees1
Trees: Chapter 11
Definition
i) A simple undirected graph is a tree if it is connected and has
no cycles.
i) In a tree, a vertex with degree 1 is called a leaf. A node is not
a leaf is called an internal node.
iii) A forest is a set of trees, i.e. is a simple graph with no circuits.
Terminologies
Examples:
Properties I
Lemma
Let G be a simple graph.
i) If every vertex in G has degree at least two, then G contains a
cycle.
ii) Every tree with at least 2 vertices has at least one leaf.
Removing it yields again a tree.
Theorem
Let T = (V , E ) be a tree. Then |V | = |E | + 1.
Properties II
Theorem (Equivalent statements for trees)
Let G = (V , E ) be a simple graph. The following statements are
equivalent:
1. G is a tree.
2. G is connected and |V | = |E | + 1.
3. G is acyclic and |V | = |E | + 1.
4. For any two vertices u, v ∈ V , there exists exactly one simple
path from u to v .
5. G is minimally connected, i.e., G is connected and removing
any edge from G disconnects it (every edge of G is a cut
edge).
6. G is maximally acyclic, i.e., G is acyclic and adding any
additional edge creates a cycle.
Examples
On the whiteboard!
Rooted Trees
I Root: 2
2
I 4, 7, 9 are siblings
I 6 is the parent of 5 and a
3 8
child of 8
I Ancestors of 1 are 4, 3, 2
4 7 9 6 10
I Descendants of 8 are 5, 6, 10
I 1, 7, 9, 5, 10 are leaves
1 5
I 2, 3, 4, 8, 6 are internal
vertices
m-ary trees
Definition
1. A rooted tree is called an m-ary tree if each vertex has at
most m children.
2. A rooted tree is called a full m-ary tree if each internal vertex
has exactly m children.
3. A 2-ary tree is called a binary tree.
4. A full 2-ary tree is called a full binary tree.
5. A complete m-ary tree is a full m-ary tree in which all levels
except possibly the last are completely filled, and the last level
has all its nodes to the left side.
Examples I
3 8
4 7 9 6 10
1 5
Theorem
If n is the number of vertices and i is the number of internal
vertices of a full m-ary tree then
1. n = mi + 1;
2. the tree has n − i = (m − 1)i + 1 leaves.
Corollary
A full binary tree has an odd number 2n + 1 of vertices with n
internal nodes and n + 1 leaves for some n ≥ 0.
Examples
How many persons received the letter (including the one who
starts the game)?
Definition
1. The level of a vertex in a rooted tree is the length of the
unique path from the root to that vertex.
2. The height of a rooted tree is the maximum of the levels of
the vertices.
3. An m-ary tree of height h is called a balanced tree if each
leave has level h or h − 1.
Examples I
I Root: d
d I Vertices with level 1:
a, c, o, i
a c o i I Vertices with level 2:
b, k, g , f
b k g f I Vertices with level 3:
h, l, e, m, n
h l e m n I Vertices with level 4: p, q
I Height of the tree is 4
p q
I The tree is not balanced.
Examples II
I Root: d
a c I Vertices with level 1:
I Vertices with level 2:
i b k g I Vertices with level 3:
I Vertices with level 4:
f l h o I Height of the tree is
I Is the tree balanced?
e m n p q
Properties
Theorem
1. In an m-ary tree with height h and l leaves we have l ≤ mh .
Therefore h ≥ dlogm le.
2. In a balanced full m-ary tree we have h = dlogm le.
Example. Does there exist a full m-ary tree with height 4 and 100
leaves?
Ordered rooted trees
Definition
An ordered rooted tree is a tree with a root where the children of
each internal node are ordered. Ordered rooted trees are drawn
such that the children are shown in order from left to right.
Figure: Initiating with the root being the first item Mathematics
..
.
Theorem
The least number of comparisons to locate an element in a list of
length n is dlog(n + 1)e.
Proof.
I The tree obtained by locating items using lexicographic order
of keys is a binary tree, denoted by T ;
I We can form a full binary tree U from T by adding unlabeled
vertices, called ”null” nodes, whenever necessary so that every
vertex with a key has two children.
I The full binary tree U has n internal nodes.
Figure: Binary search tree T and its corresponding full binary tree U
Decision Trees
Note.
Proof.
I There are 2 possible cases for each comparison of the two
numbers;
I Given a sequence of n distinct numbers, there are n!
possibilities arranging linearly;
I If there is decision tree for sorting problem, then it is a binary
tree with at least n! leaves.
I The complexity, which is counted as the number of binary
comparisons, of an sorting algorithm is the height of a
decision tree. In this case, it is at least dlog(n!)e.
Prefix Codes
To encode the message ”good morning” by a bit string we can
represent each letter by a bit string of length 3. In this case the
total number of bits used is 33.
Read textbook!
Tree Traversal
Post-order traversal
c o i
b k g f
h l e m n
I Pre-order traversal: d, c, b, h, l, k, g , o, i, f , e, m, n;
I In-order traversal: h, b, l, c, k, g , d, o, e, f , m, n, i;
I Post-order traversal: h, l, b, k, g , c, o, e, m, n, f , i, d.
Example.
723 ∗ −4 ↑ 93/+
+ − ∗235/ ↑ 234
References:
Definition
A labeled tree is a tree with n vertices whose vertices are assigned
unique numbers from 1 to n. Two labeled trees are identical if the
identity bijection from [n] to [n] preserves their adjacent relations.
Proof.
1. By induction: Homework
2. A combinatorial proof using Prüfer code will be presented
next pages.
One-to-one correspondence between labeled trees and
Prüfer sequences I
4 2 6
5 3 7 8
10 9
One-to-one correspondence between labeled trees and
Prüfer sequences IV
4
I Choose the smallest leaf: leaf
4;
I Write successively its unique
5 neighbor (node 5) in the
sequence
(1, 4, 6, 8, 6, 1, 4, 5, · · · )
I Remove the smallest leaf from
10
the tree.
One-to-one correspondence between labeled trees and
Prüfer sequences XII
5
10
Stop when the tree remains two leaves. We finally obtain the
sequence (1, 4, 6, 8, 6, 1, 4, 5). Therefore, we have the
correspondence:
1
4 2 6
←→ (1, 4, 6, 8, 6, 1, 4, 5).
5 3 7 8
10 9
One-to-one correspondence between labeled trees and
Prüfer sequences XIII
Lemma (Exercise)
1) If node i in a labeled tree T has degree di , then the occurrence of i
in its corresponding Prüfer code is di − 1.
2) Leaves in a labeled tree do not appear in the Prüfer code of that
tree.
One-to-one correspondence between labeled trees and
Prüfer sequences XIV
Corollary
There are nn−1 rooted labeled trees with n vertices.
Corollary
There are
(n − 2)!
(d1 − 1)!(d2 − 1)! · · · (dn − 1)!
labeled trees with n vertices and a given degree sequence
(d1 , d2 , . . . , dn ), where di is the degree of vertex labeled by i in
trees.
One-to-one correspondence between labeled trees and
Prüfer sequences XVII
Exercises:
9 5 3 8
7 2 3 8 4 7 9 6 10
10 6 4 1 5
One-to-one correspondence between labeled trees and
Prüfer sequences XVIII
References:
Definition
Let G = (V , E ) be an undirected simple graph. A subgraph
T = (V , E 0 ) of G is called a spanning tree if it is a tree that
contains every vertex in V .
Examples I
a c
e d
1. The subgraph
a c
e d
e d
4. The graph
a c
e d
is a spanning tree.
Examples IV
Given the graph
b d
a a a
b d b d b d
c c c
How to generate efficiently a spanning tree I
Theorem
A graph G is connected if and only if it has a spanning tree.
Proof.
On the white board!
Generating a spanning tree I
Figure: DFS of G
Remark
Examples:
d c
Its complexity is κ = 8.
Enumerating Spanning Trees II
All its spanning trees are
a b a b a b a b
d c d c d c d c
a b a b a b a b
d c d c d c d c
3. The complexity of
a c
e d
is ...
4. Could we know κ(G ) for a general graph G ?
Laplacian matrix I
b d
κ(G ) = det L̃1 (G )
3 −1 −1 0
= 2 det − (−1) det
c
−1 1 −1 1
= 3.
a c
e d
Spanning forest
Definition
A spanning forest of a graph G is a forest that contains every
vertex of G such that two vertices are in the same tree of the
forest when there is path in G between these two vertices.
I K2,2 I Kn
I K1,n I Km,n
I K2,3 I W4
I Cn I Q3
a
a key(b)=4, key(h)=8 +ab
a,b key(c)=8, key(h)=8 + bc
a,b,c key(i)=2, key(f)=4, key(d)=7, key(h)=8 +ci
a,b,c,i key(f)=4, key(g)=6, key(d)=7, key(h)=7 +cf
a,b,c,i,f key(g)=2, key(d)=7, key(h)=7, key(e)=10 +fg
a,b,c,i,f,g key(h)=1, key(d)=7, key(e)=10 +gh
a,b,c,i,f,g,h key(d)=7, key(e)=10 +cd
a,b,c,i,f,g,h,d key(e)=9 +de
a,b,c,i,f,g,h,d,e
Remark
I Initiate: T = ∅
I for each v ∈ V , makeset(v );
I Sort the edges E of G in nondecreasing order by their weights.
I for e = uv ∈ E taken in the nondecreasing order above, check
if findset(u) 6= findset(v ), then add e to T , and union(u, v )
return T .
Remark