Chapter 3 Tree

Graph Theory
Chapter 3
Tree
大葉大學(Da-Yeh Univ.)
資訊工程系(Dept. CSIE)
黃鈴玲(Lingling Huang)
Outline
3.1
3.2
3.3
3.5
3.6
Properties of trees
Rooted trees
Depth-first search
Breadth-first search
The minimum spanning tree
problem
Ch3-2
Copyright  黃鈴玲
3.1 Properties of trees
Definition:
tree: a connected graph without cycles.
( every edge is a bridge)
forest: a graph (connected or disconnected)
without cycles .
A spanning tree of a graph G is a spanning
subgraph of G that is a tree .
See Figure 3.1.
Ch3-3
Copyright  黃鈴玲
Thm 3.1:
Let T be a tree of order p and size q,
then q=p-1 .
Pf.
By strong from of induction on p.
p = 1 : k1 is the only tree of order 1, |E(k1)| = 0.
ok!
Assume the result is true for all trees of order p < k
(k  2 , k  ).
consider p = k :
Let T be a tree of order p = k, and e be an edge of T.
Let T1, T2 be the two components of T-{e}, and
|V(T1)| = p1, |V(T2)| = p2 (p1 +p2 = p)
Ch3-4
Copyright  黃鈴玲
∵p1, p2 < k
∴|E(T1)| = p1-1, |E(T2)| = p2-1 by assumption.
 |E(T)| = p1-1 + p2-1 +1 = p1+p2 -1 = p -1.
By induction, the result is true for all trees. #
Ch3-5
Copyright  黃鈴玲
Cor 3.1:
Let G be a graph of order p, the following are
equivalent :
(i) G is a tree.
(ii) G is connected and has size p-1.
(iii) G has size p-1 and no cycles.
Pf.
(i)  (ii) (by Thm 3.1)
(ii)  (iii) (證明 connected + size p-1  no cycle)
反證,若有cycle可扣掉邊後製造tree,
邊數不對
(iii)  (i) (證明 size p-1 + no cycle  connected)
反證,每一個component本身都是
connected + no cycle  tree
邊數總和不對
Ch3-6
Copyright  黃鈴玲
Thm 3.2:
Every nontrivial tree contains at least
two end-vertices.
Pf.
Suppose T is a tree of order p and size q,
∵ T is connected and nontrivial.
∴ deg(v)  1,  v  V(T).


deg( v)  2q  2 p -2
vV(T)
∴ There are at least two vertices of degree 1.#
Ch3-7
Copyright  黃鈴玲
Thm 3.3:
If T is a tree, u≠v V(T), then there is
exactly one u-v path.
Pf. (Bondy)(跟書上不同)
Suppose T has two u-v paths, say P and Q.
Since P≠Q, there is an edge e=xy  E(P) but
eE(Q).
Clearly the graph (P∪Q) – e is connected.
It therefore contains an x-y path P’.
But then P’ + e is a cycle in T, a contradiction.
Ch3-8
Copyright  黃鈴玲
Note:
T: a tree.
If uv E(T), then T+uv has exactly one
cycle C. (which must contains the edge uv)
If some edge e (未必是uv) is deleted from C,
a tree results again.
Thm 3.4 :
Let T be a tree of order m, and let G be a
graph with δ(G) m-1. Then T is isomorphic
to some subgraph of G. (證明跳過)
Ch3-9
Copyright  黃鈴玲
Homework
Exercise : 2, 5, 8, 11, 12, 14, 15, 16, 19
Exercise 8.
Let T be a tree of order 21 having degree set
{1, 3, 5, 6}. Suppose that T has 15 end-vertices
and one vertex of degree 6. How many vertices
of degree 5 does T have?
Ch3-10
Copyright  黃鈴玲
Exercise 15.
Prove or disprove:
(c) Every vertex of a tree is a cut-vertex.
(d) A tree of order p3 has more cut-vertices
then bridges.
(e) There exist exactly two regular trees.
Ch3-11
Copyright  黃鈴玲
Outline
3.1
3.2
3.3
3.5
3.6
Properties of trees
Rooted trees
Depth-first search
Breadth-first search
The minimum spanning tree
problem
Ch3-12
Copyright  黃鈴玲
3.2 Rooted Trees
Definition:
A directed tree is an asymmetric digraph
whose underlying graph is a tree.
A directed tree T is called a rooted tree if
there exists a vertex r of T, called the root,
s. t.  vertex v of T, there is an r-v path in
T.
Ch3-13
Copyright  黃鈴玲
※為方便起見,將tree的點分層畫出:
root r 在 level 0
r
level
0
1
2
3 height = 3
tree 的 height 即
最大的level 值
Ch3-14
Copyright  黃鈴玲
Thm 3.5 :
A directed tree T is a rooted tree iff
T contains a vertex r with id r = 0, and
id v = 1 for all other vertices v of T.
pf :
() root 即 r, 每點往上一層只連接到一點,
否則不是tree, 故 id v = 1  v  r.
() (證明 u1V(T),  r-u1 path)
因 id v = 1  v  r,
從u1開始往回走, 最後一定停在r,
否則表示有 cycle,
與 T 是directed tree 矛盾.
Ch3-15
Copyright  黃鈴玲
※因rooted tree 中所有arc都是向下,
所以可省略方向性.
Definition:
※
※
r
parent
更上層: ancestor
child
更下層: descendant
T
v
maximal subtree of T rooted at v
(由v及其子孫構成的induced subtree)
See Figure 3.8.
Ch3-16
Copyright  黃鈴玲
※ leaf : a vertex with no children
internal vertex:  leaf 之vertex.
※ A rooted tree is m-ary if every vertex has at
most m children.
m = 2  binary
left
child
right
child
※ complete m-ary tree: every vertex has
exactly m children or no children.
Ch3-17
Copyright  黃鈴玲
Thm 3.6 :
A complete m-ary tree with i internal
vertices has order m i +1.
Pf.
每個 internal vertex 有 m 個children,
故共有 m i 個 children (每個child只有一個
parent,所以只會被計算一次),
root 沒有parent,還沒被計算到
∴共有 m i +1 個點
Ch3-18
Copyright  黃鈴玲
Cor 3.6 :
Every complete binary tree with i internal
vertices has i+1 leaves.
pf.
By Thm 3.6, m = 2, i internal vertex
∴ 點數 = 2i + 1 ,  i +1 leaves.
Thm 3.7 :
If T is a binary tree of height h and order p,
then h+1  p  2h+1 - 1.
Pf.
(h+1  p) 當每個 level 只有一點時
(p  2h+1 – 1)當 tree 是 complete 時
Ch3-19
Copyright  黃鈴玲
Definition:
A rooted tree T of height h is balanced if
every leaf is at level h or h-1.
Cor 3.7 :
If T is a binary tree of height h and order p,
then h   log2 ((p+1)/2) .
Equality holds if T is a balanced complete
binary tree.
Pf.
p  2h+1 - 1

h   log2 ((p+1)/2)
Ch3-20
Copyright  黃鈴玲
Pf of Cor 3.7: (continued)
If T is a balanced complete binary tree,
then p > 1+2+22+…+2h-1 = 2h -1
 2h -1 < p  2h+1 - 1
 2h-1 < (p+1)/2  2h
 h =  log2 ((p+1)/2) #
Ch3-21
Copyright  黃鈴玲
Homework: 4, 5
Exercise 5:
A complete m-ary tree T of height h is called
a full m-ary tree if every leaf is at the level h.
If T has l leaves, show that h = logm l.
Ch3-22
Copyright  黃鈴玲
Outline
3.1
3.2
3.3
3.5
3.6
Properties of trees
Rooted trees
Depth-first search
Breadth-first search
The minimum spanning tree
problem
Ch3-23
Copyright  黃鈴玲
3.3 Depth-first Search
G
A spanning tree of a graph G
is a tree that is a subgraph of
G with vertex set V(G).
Some spanning trees of G :
T1
T2
T3
Ch3-24
Copyright  黃鈴玲
※ Many graph algorithms require a systematic
method of visiting the vertices of a graph
 depth-first search (DFS, 深先搜尋)
bread-first search (BFS, 廣先搜尋)
Ch3-25
Copyright  黃鈴玲
Depth-first search
G
v1
v3
v2
v5 v 4
v6
假設G用其adjacency list來
表示,而list中,每個點的
neighbor依下標由小排到大
v7
Step 1:
visit v1
v2
1 v1
v5
v3
v4
v6
v7
Ch3-26
Copyright  黃鈴玲
G
v1
v3
v2
v5 v 4
v6
v7
Step 2:
visit v3
v2
1 v1
2
v5
v3
v6
v4
v7
Step 3:
visit v5
v2
1 v1
2
3 v5
v3
v7
v4
v6
Ch3-27
Copyright  黃鈴玲
G
v1
v3
v2
v5 v 4
v6
v7
Step 4:
visit v7
v2
1 v1
2
3 v5
v3
v6
v4
4 v7
Step 5:
visit v2
5 v2
1 v1
2
3 v5
v3
4 v7
v4
v6
Ch3-28
Copyright  黃鈴玲
G
v1
v3
v2
v5 v 4
v6
v7
Step 6:
visit v4
5 v2
1 v1
2
3 v5
v3
4 v7
Step 7:
visit v6
depth-first search index
5 v2
1 v1
2
3 v5
v3
4 v7
v6
6 v4
dfi(v5)=3
6
v4
DFS forest
7 v6
Ch3-29
Copyright  黃鈴玲
DFS forest
G
F
v5
v13
v8
v2
v12
3v
5
v7
v6
v1
v10
v2 2
5
v12
v1
v15
v4
v14
v9
v3
v8
4v
9
11
6
10
1
v15
7
8
v4
v11
12 v
13
v3
6
v9
v7
v10
15
14
v14
13 v11
Ch3-30
Copyright  黃鈴玲
rooted forest
G
v12
v5
v13
v9
v2 2
v1
v8
4 v 11
6
8
v4
v3
10
6
v9
v4
8
v9
5
v6
v12
9 v7
9
1
7
4
12 v
13
v15
7
3 v5
v11
DFS forest
3v
5
v12
v2 2
v14
v3
5
6 v3
v10
v15
v4
1 v1
v7
v6
v1
F
F’
v8
v2
v7
v8
11
v10
15
14
13 v11
back edge
10 v10
13 v11
v14
12 v13
14 v14
15 v15
Ch3-31
Copyright  黃鈴玲
Algorithm 3.1 (DFS algorithm)
dfi(v)  0  vV(G)
i1
/* 用來assign 點的 dfi 值 */
S   /* 用來存放 DFS forest 的所有 arc */
If dfi(r)  0  r V(G), then output S and stop;
otherwise, let r be the first vertex s. t. dfi(r) = 0
and let w  r. /* r是這個 component 的tree 的 root */
5. dfi(w)  i
6. i  i + 1
7. (search)
7.1. If dfi(v) = 0 for some v in the adjacency list of w,
then continue; otherwise, go to Step 7.4.
7.2 S  S ∪ {(w, v)} and assign parent(v)  w.
7.3 w  v and return to Step 5.
7.4 If w  r, then w  parent(w) and return to Step 7.1;
otherwise, return to Step 4.
1.
2.
3.
4.
Ch3-32
Copyright  黃鈴玲
※ Time complexity of Alg. 3.1: O(max{p, q})
Thm 3.8.
Every back edge e of a graph G joins an
ancestor and a descendant.
In particular, if e = uv and u is visited before
v in the depth-first search of G, then v is a
descendant of u.
Ch3-33
Copyright  黃鈴玲
Exercise 1.
For the graph G shown below, find the
depth-first search forest. (不必為rooted形式)
G
v4
v1
v3
v5
v9
v12
v2
v6
v8
v11
v10
v7
Ch3-34
Copyright  黃鈴玲
Outline
3.1
3.2
3.3
3.5
3.6
Properties of trees
Rooted trees
Depth-first search
Breadth-first search
The minimum spanning tree
problem
Ch3-35
Copyright  黃鈴玲
3.5 Breadth-first Search
BFS: visit過點v後,先visit完點v的每個neighbor,
將之存入queue中,再從queue中選取第一個
點visit,依此類推
Ch3-36
Copyright  黃鈴玲
v5
G
v12
v13
v8
v2
v7
v6
v1
v15
v4
v10
v14
v9
v3
v11
Step 1:
visit v1
Queue
1 v1
v1
Ch3-37
Copyright  黃鈴玲
v5
G
v12
v13
v8
v2
v7
v6
v1
v15
v4
v14
v9
v3
v11
Step 2: visit neighbors of v1
7 v12
v10
2 v2
Queue
5 v6
v1 v2 v3 v4 v6 v9 v12
1 v1
6 v9
4 v4
3 v3
Ch3-38
Copyright  黃鈴玲
v5
G
v12
v13
v8
v2
v7
v6
v1
v15
v4
v14
v9
v3
v11
Step 3: visit neighbors of v2
8 v5
7 v12
v10
2 v2
5 v6
Queue
v1 v2 v3 v4 v6 v9 v12 v5
1 v1
4 v4
3 v3
6 v9
Ch3-39
Copyright  黃鈴玲
v5
G
v12
v13
v8
v2
v7
v6
v1
v15
v4
v10
v14
v9
v3
v11
Step 4: visit v7
8 v5
Queue
v7
7 v12
2 v2
5 v6
9 v7
1 v1
4 v4
3 v3
6 v9
Ch3-40
Copyright  黃鈴玲
v5
G
v12
v13
v8
v2
v7
v6
v1
v15
v4
v14
v9
v3
v11
Step 5: visit neighbors of v7
8 v5
7 v12
v10
2 v2
Queue
11 v13
v7 v10 v13
5 v6
9 v7
1 v1
4 v4
3 v3
6 v9
10 v10
Ch3-41
Copyright  黃鈴玲
v5
G
v12
v13
v8
v2
v7
v6
v1
v15
v4
v10
v14
v9
v3
v11
Step 6: visit neighbors of v10
8 v5
7 v12
2 v2
5 v6
Queue
11 v13
v8
12
10 v10
1 v1
4 v4
3 v3
9 v7
6 v9
15
v15
13
v11
v10 v13 v8 v11 v14 v15
所有點皆已visit
BFS forest
14
v14
Ch3-42
Copyright  黃鈴玲
Exercise 2.
For the graph G shown below, find the
breadth-first search forest.
G
v4
v1
v3
v5
v9
v12
v2
v6
v8
v11
v10
v7
Ch3-43
Copyright  黃鈴玲
Outline
3.1
3.2
3.3
3.5
3.6
Properties of trees
Rooted trees
Depth-first search
Breadth-first search
The minimum spanning tree
problem
Ch3-44
Copyright  黃鈴玲
3.6 The minimum
spanning tree problem
Definition:
(1) weighted graph:
graph 中每個 edge 上有 weight
(2) weight w(H) of a subgraph H of a
weighted graph:
the sum of the weights of the edges of H
The Minimum Spanning Tree Problem :
Find a minimum spanning tree in all
connected weighted graph.
Ch3-45
Copyright  黃鈴玲
Algorithm 3.3 (Kruskal’s Algorithm)
1. S ← .
2. Let e be an edge of minimum weight
such that eS and <S∪{e}> is acyclic,
and let S ← S∪{e}.
3. If |S| = p – 1, then output S;
otherwise, return to Step 2.
建議:在 step2 執行前先將 G 的 edge 依 weight 由小至大排列
O(p2)
Ch3-46
Copyright  黃鈴玲
y
FIGURE 3-18
G
2
5
3
5
x
4
6
w
2
2
1
u
v
Sort the edges of G: uv, xy, vw, uw, ux, vx, vy, uy, wx
Constructing the tree by Kruskal algorithm
Step 2
y
Step 1
y
2
x
x
w
1
u
S={uv}
w
v
u
S={uv, xy}
1
v
Ch3-47
Copyright  黃鈴玲
y
FIGURE 3-18
G
2
5
3
5
x
4
6
w
2
2
1
u
v
Sort the edges of G: uv, xy, vw, uw, ux, vx, vy, uy, wx
Constructing the tree
Step 3
y
2
2
x
w
u
y
Step 4
1
S={uv, xy, vw}
x
3
w
2
v
u
|S|=p-1
stop!
1
S={uv, xy, vw, ux}
2
v
Ch3-48
Copyright  黃鈴玲
Theorem 3.12.
Kruskal’s algorithm produces a minimum
spanning tree in a nontrivial connected
weighted graph.
Pf : Let G be a nontrivial connected weighted graph of
order p, and let T be a subgraph produced by
Kruskal’s algorithm.
Certainly, T is a spanning tree of G (and so has
size p – 1), say
E(T) = { e1, e2, …, ep-1 }
where w(e1) w(e2)  …  w(ey-1).
Therefore, the weight of T is
p -1
w(T )   w(ei ).
i 1
Ch3-49
Copyright  黃鈴玲
Proof of Thm 3.12 (continued):
Suppose, to the contrary, that T is not a
minimum spanning tree.
Then, among the minimum spanning trees of G,
choose one, call it H, which has a maximum
number of edges in common with T.
Now H  T; so T has at least one edge that does
not belong to H.
Let ei(1 i  p-1) be the first edge with ei  E(T),
ei  E(H).  {e1, e2, …, ei-1} E(H).
Define G0 = H+ei.
Then G0 has exactly one cycle C.
Since T has no cycles, there is an edge e0 of C that
is not in T.  e0  E(T), e0  E(H).
The graph T0 = G0 - e0 is also a spanning tree of G
and w(T0) = w(H) + w(ei) - w(e0).
Ch3-50
Copyright  黃鈴玲
Proof of Thm 3.12 (continued):
Since w(H)  w(T0)  w(e0)  w(ei).
By Kruskal’s algorithm, ei is an edge of minimum
weight such that <{e1,e2,…, eii-1}∪{eii}> is acyclic.
However, <{e1,e2,…, eii-1, e0}> is a subgraph of H
and is therefore acyclic, so w(ei) = w(e0).
Thus, w(T0) = w(H), which implies that T0 is also a
minimum spanning tree of G.
But T0 has more edges in common with T than H
has with T, which contradicts our assumption.
Ch3-51
Copyright  黃鈴玲
Algorithm 3.3 (Prim’s Algorithm)
1. Let u be an arbitrary vertex of G and
T ← u.
2. Let e be an edge of minimum weight
joining a vertex of T and a vertex not
in T, and T ← T∪{e}.
3. If |E(T)| = p – 1, then output T;
otherwise, return to Step 2.
O(p3)
Ch3-52
Copyright  黃鈴玲
y
FIGURE 3-19
G
2
5
3
5
x
4
6
w
2
2
1
u
v
Constructing the tree by Prim’s algorithm
Step 1
y
y
Step 3
V(T)={u}
x
Step 2
x
w
u
1
w
2
v
V(T)={u, v}, E(T)={u v}
u
V(T)={u, v,w},
E(T)={uv, uw}
1
v
Ch3-53
Copyright  黃鈴玲
y
FIGURE 3-19
G
2
5
3
5
x
4
6
w
2
2
1
u
v
Constructing the tree by Prim’s algorithm
Step 4
y
y
Step 5
2
x
V(T)=
{u, v, w, x},
3
3
w
2
u
x
w
V(T)=V(G)
E(T)=
{uv, uw, ux, xy}
2
1
E(T)={uv, uw, ux}
v
u
1
|E(T)|=p-1
v
Ch3-54
stop! Copyright  黃鈴玲
If G is a connected weighted graph whose
edges have distinct weights:
Algorithm 3.5 (Borůvka’s Algorithm)
1. F ← Kp.
2. For each component F’ of F, join a
vertex of F’ to a vertex of some other
component of F by an edge of minimum
weight. Denote this set of edges by S,
and F ← F + S.
3. If |E(F)| = p – 1, then output E(F);
otherwise, return to Step 2.
Ch3-55
Copyright  黃鈴玲
y
FIGURE 3-20
2
9
3
8
x
4
6
w
7
5
1
u
v
Constructing the tree by Borůvka’s algorithm:
Step 1
Step 2
F1 :
F2:
y
y
2
x
x
w
u
w
v
u
1
5
v
Ch3-56
Copyright  黃鈴玲
y
FIGURE 3-20
2
9
3
8
x
4
6
w
7
5
1
v
Constructing the tree by Borůvka’s algorithm:
Step 3
F3:
y
2
x
3
u
w
1
5
v
Ch3-57
Copyright  黃鈴玲
Spanning tree
Every graph can be considered a
weighted graph in which every edge
has weight 1.
 Kruskal, Prim, DFS, and BFS
can be applied to produce
a spanning tree
(see Figure 3-21)

Ch3-58
Copyright  黃鈴玲
Theorem 3.14
(Cayley’s Tree Formula)
Let G be a complete labeled graph of
order p. Then there are pp-2 unequal
spanning trees of G.
Note :
Cayley’s Tree Formula implies that if V is
a set of cardinality p1, then there are pp-2
unequal trees with vertex set V.
(see Figure 3-22, 16 unequal trees with 4 vertices.)
Ch3-59
Copyright  黃鈴玲
Homework

Exercise 3.6:
1, 9
Ch3-60
Copyright  黃鈴玲