Graph TheoryGraph Theory
Chapter 3 Chapter 3 Tree Tree
大葉大學大葉大學 (Da-Yeh Univ.)(Da-Yeh Univ.) 資訊工程系資訊工程系 (Dept. CSIE)(Dept. CSIE)黃鈴玲黃鈴玲 (Lingling Huang)(Lingling Huang)
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-22
OutlineOutline
3.1 Properties of trees3.1 Properties of trees
3.2 Rooted trees3.2 Rooted trees
3.3 Depth-first search3.3 Depth-first search
3.5 Breadth-first search3.5 Breadth-first search
3.6 The minimum spanning tree 3.6 The minimum spanning tree problem problem
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-33
3.1 Properties of trees3.1 Properties of trees
Definition:Definition:treetree:: a connected graph without cycles. a connected graph without cycles.
( ( every edge is a bridge) every edge is a bridge)forestforest:: a graph (connected or a graph (connected or
disconnected) without cycles .disconnected) without cycles .A A spanning treespanning tree of a graph of a graph GG is a is a
spanning subgraph of spanning subgraph of GG that is a tree . that is a tree .
See Figure 3.1.
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-44
Thm 3.1:Thm 3.1: Let Let TT be a tree of order be a tree of order pp and size and size qq,, then then qq==pp1 .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)
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-55
∵∵ pp11,, pp22 << kk
∴∴|E(T|E(T11)| = )| = pp111, |E(T1, |E(T22)| = )| = pp221 by assumption.1 by assumption.
|E(T)| = |E(T)| = pp111 + 1 + pp221 +1 = 1 +1 = pp11++pp22 1 = 1 = pp 1.1.
By induction, the result is true for all trees. By induction, the result is true for all trees. ##
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-66
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 p1. (iii) G has size p1 and no cycles.
Pf. (i) (ii) (by Thm 3.1) (ii) (iii) ( 證明 connected + size p1 no cycle) 反證,若有 cycle 可扣掉邊後製造 tree ,
邊數不對 (iii) (i) ( 證明 size p1 + no cycle connected) 反證,每一個 component 本身都是 connected + no cycle tree 邊數總和不對
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-77
Thm 3.2:Thm 3.2:
Every nontrivial tree contains at Every nontrivial tree contains at least two end-vertices.least two end-vertices.
Pf.
Suppose T is a tree of order p and size q, ∵ ∵ T is connected and nontrivial. is connected and nontrivial. ∴ ∴ degdeg((vv) ) 1, 1, vv VV((TT).). 222)deg( p - q v
V(T)v
∵
∴ ∴ There are at least two vertices of There are at least two vertices of degree 1.degree 1.##
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-88
Thm 3.3:Thm 3.3:
If If TT is a tree, is a tree, uu≠≠vv VV((TT), then there is ), then there is exactly one exactly one uu--vv path. 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
eE(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.
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-99
Note:Note: TT: a tree.: a tree. If If uvuv E( E(TT), then ), then TT++uvuv has exactly one has exactly one
cycle cycle CC. (which must contains the edge . (which must contains the edge uvuv))
If some edge If some edge ee ( ( 未必是未必是 uvuv) is deleted ) is deleted from from CC, a tree results again. , a tree results again.
Thm 3.4 :
Let TT be a tree of order m, and let G be a graph with δ(G) m1. Then T is isomorphic to some subgraph of G. ( 證明跳過 )
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1010
HomeworkHomework
Exercise : 2, 5, 8, 11, 12, 14, 15, Exercise : 2, 5, 8, 11, 12, 14, 15, 16, 1916, 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 verticesof degree 5 does T have?
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1111
Exercise 15. Prove or disprove:(c) Every vertex of a tree is a cut-vertex.(d) A tree of order p3 has more cut-vertices then bridges.(e) There exist exactly two regular trees.
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1212
OutlineOutline
3.1 Properties of trees3.1 Properties of trees
3.2 Rooted trees3.2 Rooted trees
3.3 Depth-first search3.3 Depth-first search
3.5 Breadth-first search3.5 Breadth-first search
3.6 The minimum spanning tree 3.6 The minimum spanning tree problem problem
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1313
3.2 Rooted Trees3.2 Rooted Trees
Definition:Definition: A A directed treedirected tree is an is an asymmetricasymmetric
digraph whose underlying graph is a digraph whose underlying graph is a tree.tree.
A directed tree A directed tree TT is called a is called a rooted treerooted tree if there exists a vertex if there exists a vertex rr of of TT, called the , called the rootroot, s. t. , s. t. vertex vertex vv of of TT, there is an , there is an rr--vv path in path in TT..
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1414
※※ 為方便起見,將為方便起見,將 treetree 的點分層畫出的點分層畫出 ::
rroot r 在 level 0level
height = 3tree 的 height 即最大的 level 值
0
1
2
3
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1515
Thm 3.5 :Thm 3.5 : A directed tree A directed tree TT is a rooted tree iff is a rooted tree iff
TT contains a vertex contains a vertex rr with id with id rr = 0, and = 0, and id id vv = 1 for all other vertices = 1 for all other vertices vv of of TT..
pf : () root 即 r, 每點往上一層只連接到一點 , 否則不是 tree, 故 id id vv = 1 = 1 vv rr. . () ( 證明 u1V(T), r-u1 path)
因 id id vv = 1 = 1 vv rr, 從 u1 開始往回走 , 最後一定停在 r, 否則表示有 cycle, 與 T 是 directed tree 矛盾 .
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1616
DefinitioDefinition:n:※※ parent
child
更上層 : ancestor
更下層 : descendant
※※r T
v maximal subtree of T rooted at v ( 由 v 及其子孫構成的 induced subtree)
※※因因 rooted tree rooted tree 中所有中所有 arcarc 都是向下都是向下 ,, 所以可省略方向性所以可省略方向性 ..
See Figure 3.8.
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1717
※ ※ leafleaf : a vertex with no children : a vertex with no children
internal vertexinternal vertex: : leaf leaf 之之 vertex.vertex.
※ ※ A rooted tree isA rooted tree is mm-ary -ary if every vertex has if every vertex has at at most most mm children. children.
mm = 2 = 2 binary binary
※ ※ complete complete mm-ary tree-ary tree: every vertex : every vertex has has exactly exactly mm children or no children. children or no children.
left child
right child
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1818
Thm 3.6 :Thm 3.6 : A complete A complete mm-ary tree with -ary tree with ii internal internal
vertices has order vertices has order m i m i +1.+1.
Pf.
每個 internal vertex 有 m 個 children , 故共有 m i m i 個 個 children ( 每個 child 只有一個 parent ,所以只會被計算一次 ) , root 沒有 parent ,還沒被計算到
∴∴共有 共有 m i m i +1 +1 個點個點
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-1919
Cor 3.6 :Cor 3.6 : Every Every complete binary treecomplete binary tree with with ii internal internal vertices has vertices has i+i+11 leaves. leaves.
pf. By Thm 3.6, m = 2, i internal vertex ∴ ∴ 點數 點數 = 2= 2ii + 1 , + 1 , ii +1 leaves. +1 leaves.
Thm 3.7 :Thm 3.7 :
If If TT is a binary tree of height is a binary tree of height hh and and order order pp,,
then then hh+1 +1 pp 2 2hh+1+1 1. 1.Pf.
(hh+1 +1 pp) ) 當每個 當每個 level level 只有一點時只有一點時 ((pp 2 2hh+1+1 –– 1) 1) 當 當 tree tree 是 是 complete complete 時時
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2020
Definition:Definition: A rooted tree A rooted tree TT of height of height hh is is balancedbalanced
if every leaf is at level if every leaf is at level hh or or hh1.1.
Cor 3.7 :Cor 3.7 :
If If TT is a binary tree of height is a binary tree of height hh and order and order pp,,
then then hh log log2 2 ((((pp+1)/2)+1)/2) . .Equality holds if Equality holds if TT is a balanced is a balanced complete binary tree.complete binary tree.
Pf.
pp 2 2hh+1+1 1 1 hh log log2 2 ((((pp+1)/2)+1)/2)
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2121
Pf of Cor 3.7: (continued)
If If TT is a balanced complete binary is a balanced complete binary tree,tree, then then pp > 1+2+2 > 1+2+222+…+2+…+2hh11 = 2 = 2hh 11 22hh 1 < 1 < p p 2 2hh+1+1 1 1 22hh11 < < ((pp+1)/2+1)/2 2 2hh
hh = = log log2 2 ((((pp+1)/2)+1)/2) ##
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2222
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.
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2323
OutlineOutline
3.1 Properties of trees3.1 Properties of trees
3.2 Rooted trees3.2 Rooted trees
3.3 Depth-first search3.3 Depth-first search
3.5 Breadth-first search3.5 Breadth-first search
3.6 The minimum spanning tree 3.6 The minimum spanning tree problem problem
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2424
GA spanning tree of a graph G is a tree that is a subgraph of G with vertex set V(G).
T1 T2T3
Some spanning trees of G :
3.3 Depth-first Search3.3 Depth-first Search
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2525
※ Many graph algorithms require a systematic method of visiting the vertices of a graph depth-first search (DFS, 深先搜尋 ) bread-first search (BFS, 廣先搜尋 )
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2626
Depth-first searchDepth-first search
Gv1 v2
v3 v5v4 v6
v7
假設 G 用其 adjacency list 來表示,而 list 中,每個點的neighbor 依下標由小排到大
v11
v3
v7
v5 v4
v2
v6
Step 1: visit v1
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2727
Gv1 v2
v3 v5v4 v6
v7
v11
v3
2
v7
v5 v4
v2
v6
Step 2: visit v3
Step 3: visit v5
v11
v3
2
v7
v5 v4
v2
v63
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2828
Gv1 v2
v3 v5v4 v6
v7
Step 4: visit v7
Step 5: visit v2
v11
v3
2
v7
v5 v4
v2
v63
4
v11
v3
2
v7
v5 v4
v2
v63
4
5
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-2929
Gv1 v2
v3 v5v4 v6
v7
Step 6: visit v4
Step 7: visit v6
v11
v3
2
v7
v5 v4
v2
v63
4
5
6
v11
v3
2
v7
v5 v4
v2
v63
4
5
6 7
depth-first search index
dfi(v5)=3
DFS forest
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3030
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15
v14
v11
DFS forest
F
v12
v5 v13
v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
1
2
3
45
6
78
9
1011
12
13
14
15
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3131
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
rooted forest
F’
v12
v5 v13
v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
1
2
3
45
6
7 8
9
1011
12
13
1415
DFS forest
F
v11
v2 2
v79
v53
v6
4v12
5
v36
v4
7v9
8
v1010v8
11
v1312
v1113
v1414 v1515
back edge
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3232
Algorithm 3.1 (DFS algorithm)1. dfi(v) 0 vV(G)2. i 1 /* 用來 assign 點的 dfi 值 */3. S /* 用來存放 DFS forest 的所有 arc */4. 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) i6. i i + 17. (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.
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3333
Thm 3.8.Thm 3.8. Every back edge Every back edge ee of a graph of a graph GG
joins an ancestor and a descendant.joins an ancestor and a descendant.
In particular, if In particular, if ee = = uvuv and and uu is visited is visited before before vv in the depth-first search of in the depth-first search of GG, , then then vv is a descendant of is a descendant of uu..
※ Time complexity of Alg. 3.1: O(max{p, q})
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3434
v6
Exercise 1.For the graph G shown below, find thedepth-first search forest. ( 不必為 rooted 形式 )
G
v5 v12
v8v7
v1
v4
v9
v10
v2
v3
v11
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3535
OutlineOutline
3.1 Properties of trees3.1 Properties of trees
3.2 Rooted trees3.2 Rooted trees
3.3 Depth-first search3.3 Depth-first search
3.5 Breadth-first search3.5 Breadth-first search
3.6 The minimum spanning tree 3.6 The minimum spanning tree problem problem
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3636
BFS: visit 過點 v 後,先 visit 完點 v 的每個neighbor , 將之存入 queue 中,再從 queue 中選取第一個 點 visit ,依此類推
3.5 Breadth-first 3.5 Breadth-first SearchSearch
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3737
Queue
v1
Step 1: visit v1
v11
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3838
Queue
v1
Step 2: visit neighbors of v1
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
v11
v22
v33v44
v65
v96
v127
v2 v3 v4 v6 v9 v12
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-3939
Queue
v1 v2 v3 v4 v6 v9 v12
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
v11
v22 v65v127
v33v44 v96
v58
Step 3: visit neighbors of v2
v5
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4040
Queue
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
v11
v22 v65v127
v33v44 v96
v58
Step 4: visit v7
v79
v7
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4141
Queue
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
v11
v22 v65v127
v33v44 v96
v58
Step 5: visit neighbors of v7
v79v7
v1311
v1010
v10 v13
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4242
Queue
Gv5 v13
v12 v6
v8
v7
v1
v4 v9
v10
v2
v3
v15v14
v11
v11
v22 v65v127
v33v44 v96
v58
Step 6: visit neighbors of v10
v79
v1311
v1010
v10 v13
v11
13
v11
v14
14
v14
v15
15
v15v8
12 v8
所有點皆已 visit
BFS forest
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4343
v6
Exercise 2.For the graph G shown below, find thebreadth-first search forest.
G
v5 v12
v8v7
v1
v4
v9
v10
v2
v3
v11
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4444
OutlineOutline
3.1 Properties of trees3.1 Properties of trees
3.2 Rooted trees3.2 Rooted trees
3.3 Depth-first search3.3 Depth-first search
3.5 Breadth-first search3.5 Breadth-first search
3.6 The minimum spanning tree 3.6 The minimum spanning tree problem problem
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4545
3.6 The minimum 3.6 The minimum spanning tree spanning tree problemproblemDefinition:Definition: (1) (1) weighted graphweighted 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 :The Minimum Spanning Tree Problem : Find a minimum spanning tree in all Find a minimum spanning tree in all
connected weighted graph.connected weighted graph.
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4646
Algorithm 3.3 (KruskalAlgorithm 3.3 (Kruskal’’s s Algorithm)Algorithm)
1. 1. SS ← ← ..
2. Let 2. Let ee be an edge of minimum be an edge of minimum weight such that weight such that eeSS and < and <SS∪{∪{ee}> }> is acyclic, and let is acyclic, and let SS ← ← SS∪{∪{ee}.}.
3. If |3. If |SS| = | = pp – 1, then output – 1, then output SS; ; otherwise, return to Step 2.otherwise, return to Step 2.
建議:在 step2 執行前先將 G 的 edge 依 weight 由小至大排列O(p2)
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4747
y
x
w
u v1
Constructing the tree by Kruskal algorithmConstructing the tree by Kruskal algorithm
y
x
w
u v1
2
y
x
w
u v
2 5
43
5
2 2
1
FIGURE 3-18FIGURE 3-18
6
G
Sort the edges of G: uv, xy, vw, uw, ux, vx, vy, uy, wx
Step 1 Step 2
S={uv} S={uv, xy}
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4848
Constructing the treeConstructing the tree
y
x
w
u v
2 5
43
5
2 2
1
FIGURE 3-18FIGURE 3-18
6
G
Sort the edges of G: uv, xy, vw, uw, ux, vx, vy, uy, wx
Step 3 y
x
w
u v1
2
2
y
x
w
u v1
2
2
3
Step 4|S|=p-1 stop!
S={uv, xy, vw} S={uv, xy, vw, ux}
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-4949
Theorem Theorem 3.12.3.12.
KruskalKruskal’’s algorithm produces a minimum s algorithm produces a minimum
spanning tree in a nontrivial connected spanning tree in a nontrivial connected
weighted graph.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, …, ep1 }where w(e1) w(e2) … w(ey1).Therefore, the weight of T is
1
1
).()(p
iiewTw
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5050
Suppose, to the contrary, that Suppose, to the contrary, that TT is not a is not a minimum spanning tree. minimum spanning tree.
Then, among the minimum spanning trees of Then, among the minimum spanning trees of GG, , choose one, call it choose one, call it HH, which has a maximum , which has a maximum number of edges in common with number of edges in common with TT..
Now Now HH TT; so ; so TT has at least one edge that has at least one edge that does not belong to does not belong to HH..
Let Let eeii(1(1 ii pp1)1) be the first edge with be the first edge with eeii EE((TT)),, eeii EE((H).H). { {e1, e2, …, ei1} EE((H).H).
Define Define GG00 = = HH++eeii. .
Then Then GG00 has exactly one cycle has exactly one cycle CC..
Since Since TT has no cycles, there is an edge has no cycles, there is an edge ee0 0 of of CC that is not in that is not in TT. . ee00 EE((T),T), ee00 EE((H).H).
The graph The graph TT00 = = GG00 ee00 is also a spanning tree of is also a spanning tree of GG and and ww((TT00) = ) = ww((HH) + ) + ww((eeii) ) ww((ee00).).
Proof of Thm 3.12 (continued):
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5151
Since Since ww((HH) ) ww((TT00) ) ww((ee00) ) ww((eeii).).
By KruskalBy Kruskal’’s algorithm, s algorithm, eeii is an edge of is an edge of minimum weight such that <{minimum weight such that <{ee11,,ee22,,……, , eeiii-i-
11}∪{}∪{eeiiii}> is acyclic.}> is acyclic.
However, <{However, <{ee11,,ee22,,……, , eeiii-1i-1, , ee00}> is a subgraph of }> is a subgraph of H H and is therefore acyclic, so and is therefore acyclic, so ww((eeii) = ) = ww((ee00).).
Thus, Thus, ww((TT00) = ) = ww((HH),), which implies that which implies that TT00 is also a is also a minimum spanning tree of minimum spanning tree of GG. .
But But TT00 has more edges in common with has more edges in common with TT than than H H has with has with TT, which contradicts our assumption., which contradicts our assumption.
Proof of Thm 3.12 (continued):
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5252
Algorithm 3.3 (PrimAlgorithm 3.3 (Prim’’s s Algorithm)Algorithm)
1. Let 1. Let uu be an arbitrary vertex of be an arbitrary vertex of GG and and TT ← ← uu..
2. Let 2. Let ee be an edge of minimum be an edge of minimum weight joining a vertex of weight joining a vertex of TT and a and a vertex not in vertex not in TT, and , and TT ← ← TT∪{∪{ee}.}.
3. If |3. If |EE((TT))| = | = pp – 1, then output – 1, then output TT; ; otherwise, return to Step 2.otherwise, return to Step 2.
O(p3)
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5353
y
x
w
u v1
Constructing the tree by Prim’s algorithmConstructing the tree by Prim’s algorithm
y
x
w
u v
2 5
43
5
2 2
1
FIGURE 3-19FIGURE 3-19
6
G
Step 2
Step 3
V(T)={u, v}, E(T)={u v}
V(T)={u, v,w}, E(T)={uv, uw}
Step 1V(T)={u}
y
x
w
u v1
2
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5454
Constructing the tree by Prim’s algorithmConstructing the tree by Prim’s algorithm
y
x
w
u v
2 5
43
5
2 2
1
FIGURE 3-19FIGURE 3-19
6
G
Step 5V(T)=V(G) E(T)={uv, uw, ux, xy}
Step 4
V(T)={u, v, w, x},
E(T)={uv, uw, ux}
y
x
w
u v1
2
3
y
x
w
u v1
3
2
2
|E(T)|=p-1 stop!
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5555
Algorithm 3.5 (BorAlgorithm 3.5 (Borůůvkavka’’s s Algorithm)Algorithm)
1. 1. FF ← ← KKpp..
2. For each component 2. For each component F’F’ of of FF, join a , join a vertex of vertex of F’F’ to a vertex of some to a vertex of some other component of other component of FF by an edge by an edge of minimum weight. Denote this of minimum weight. Denote this set of edges by set of edges by SS, and , and FF ← ← FF + + SS..
3. If 3. If ||EE((FF)| = )| = pp – 1 – 1, then output , then output EE((FF);); otherwise, return to Step 2.otherwise, return to Step 2.
If G is a connected weighted graph whose edges have distinct weights:
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5656
F1:
x
w
u v
y yF2:
x
w
u v
2
5
1
u
y
x
w
v
2 8
43
9
7 5
1
FIGURE 3-20FIGURE 3-20
Constructing the tree by Constructing the tree by BorBorůůvka’svka’s algorithm: algorithm:Step 1 Step 2
6
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5757
F3:
x
w
u v
y
2
5
1
3
y
x
w
v
2 8
43
9
7 5
1
FIGURE 3-20FIGURE 3-20
Constructing the tree by Constructing the tree by BorBorůůvka’svka’s algorithm: algorithm:Step 3
6
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5858
Spanning treeSpanning tree
Every graph can be considered a Every graph can be considered a weighted graph in which every weighted graph in which every edge has weight 1.edge has weight 1.
Kruskal, Prim, DFS, and BFSKruskal, Prim, DFS, and BFS
can be applied to producecan be applied to produce a spanning tree a spanning tree(see Figure 3-21)(see Figure 3-21)
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-5959
Theorem 3.14 Theorem 3.14 (Cayley(Cayley’’s Tree Formula)s Tree Formula)
Let Let GG be a complete labeled graph of be a complete labeled graph of
order order pp. Then there are . Then there are pppp22 unequal unequal
spanning trees of spanning trees of GG..
Note : Cayley’s Tree Formula implies that if V is a set of cardinality p1, then there are pp-2
unequal trees with vertex set V.(see Figure 3-22, 16 unequal trees with 4 vertices.)(see Figure 3-22, 16 unequal trees with 4 vertices.)
Copyright Copyright 黃鈴玲黃鈴玲Ch3-Ch3-6060
HomeworkHomework
Exercise 3.6: Exercise 3.6: 1, 9 1, 9