*** 基礎圖論, Sec.
11.7 More on Trees***
1
by 陳宏賓
1
Constructions of spanning trees
由任意一點u開始, 令U = {u}. 每次選取 an edge that joins a vertex in
U and a vertex not in U , 直到沒有這種 edges 存在. The algorithm is as
follows.
Algorit hm t o grow a spa nning t re e
Let G (V , E ) be a connected graph and let u be any vertex.
(1) Let U = {u}, F = { }.
(2) While there exists an edge D {x, y} such that x U and y U ,
(i) Put y into U.
(ii) Put D into F.
Let T (U , F ) ˁʳ
依據不同的需求, 有時我們想要建構具有某特殊性質的 spanning trees,
以下介紹的 algorithms 在演算法或 Optimization(最佳化) 等領域扮演相
當重要的角色.
2
Breadth-first spanning trees
由任意一點u開始, 用Breadth-first search(先廣) 的原則, 來建構 a spanning tree. 用這個原則所得到的 spanning tree 叫做 a BFS-tree (rooted
at u). 用到 2 個符號: breadth-first number bf (x), x是指第幾個被放入
BFS-tree.
Figure 1: Examples of BFS-trees.
3 DEPTH-FIRST SPANNING TREES
2
Algorit hm t o grow a BFS-spa nning t re e root e d at u
Let G (V , E ) be a connected graph and let u be any vertex.
(1) Let i = 1, U = {u}, D(u) = 0, bf (u ) = 1, F = { }, and T = (U, F).
(2) While there exists an edge D {x, y} in G such that x U and y U ,
࠷ x ਢࢬڶຍጟ x խ, bf(x)່՛ऱ߷ଡ, Հ٨೯܂:
(i)
Let bf ( y ) = i + 1, D(y) = D(x) + 1.
(ii)
Put y into U.
(iii) Put D into F.
(iv)
Let T (U , F ) .
(v)
Increase i by 1.
Figure 2: D(x) = distance between u and x in the BFS-tree. d(u, x) = distance
between u and x in G.
Remark.
3
BFS-tree 的特性: D(x) = d(u, x).
Depth-first spanning trees
由任意一點u開始, 用Depth-first search(先深) 的原則, 來建構 a spanning tree. 用這個原則所得的 spanning tree 叫做 a DFS-tree (rooted
at u). 用到 1 個符號: depth-first number df (x), x是指第幾個被放入 DFStree.
Figure 3: An example of DFS-trees.
一個圖的 edges 可以被加上 weights (表示要花的時間、 過路費等). 一般
來說, weights 都是 non-negative, 畢竟天下沒有白吃的午餐, non-negative
weights 是很合理的假設. 之前 4 個 spanning trees 的建構方法, 考慮的是
4 DIJKSTRA’S ALGORITHM
3
Algorit hm t o grow a DFS-spa nning t re e root e d at u
Let G (V , E ) be a connected graph and let u be any vertex.
(1) Let i = 1, U = {u}, df (u ) = 1, F = { }, and T = (U, F).
(2) While G խ ڇژan edge D {x, y} in G such that x U and y U ,
࠷ x ਢࢬڶຍጟ x խ df(x)່Օऱ߷ଡ, Հ٨೯܂:
(i)
Let df ( y ) = i + 1.
(ii)
Put y into U.
(iii) Put D into F.
(iv)
Let T (U , F ) .
(v)
Increase i by 1.
unweighted case. 以下 3 個 spanning trees 的建構方法, 考慮的是 weighted
case.
4
Dijkstra’s algorithm
Problem. Given a connected weighted graph G, for any fixed vertex
u ∈ G construct a spanning tree rooted at u such that the weighteddistance between u and each vertex x is smallest. (建構u與所有點的
shortest-path tree)
Remark. Weights we consider here are all nonnegative. 書上稱 shortestpath tree 為distance-tree.
Figure 4: The graph on the right is a shortest-path tree rooted at a.
We start with some notations. First, the weight of a walk is the sum
of all weights on the edges of the walk.
dc (x, y) denotes the weighted-distance between x and y in G, i.e., the
4 DIJKSTRA’S ALGORITHM
4
smallest weight of all the walks joining x and y. Let dc (x, x) = 0 and
let dc (x, y) = ∞ if there is no walk joining x and y.
d(x, y) denotes the distance between x and y in G, i.e., the smallest
number of edges of all walks joining x and y. For example, in Figure
11.36, dc (a, e) = 4, d(a, e) = 2.
Given a weighted graph G = (V, E) with non-negative weight c(α) =
c{x, y} associated with each edge α = {x, y} of G and given a vertex
u of G, the following algorithm constructs a spanning tree T such that
for each vertex x, dc (u, x) in T equals dc (u, x) in G.
Dijk st ra ’s Algorit hm t o grow a shor t e st -pat h t re e root e d at u
Let G (V , E ) be a weighted graph with weight function c and let u be any vertex.
(3) Let U = {u}, D(u) = 0, and F = { }.
(4) While there exists an edge D {x, y} in G such that x U and y U ,
࠷ y ਢࢬڶຍጟ y խ, D(x) + c{x, y} ່՛ऱ߷ଡ, Հ٨೯܂:
(i)
(ii)
(iii)
Put y into U.
Put D into F.
Let D( y ) D( x) c{x, y} .
(3) Let T = (U, F).
Proof. It is sufficient to prove that when the algorithm terminates,
for every vertex y, we have D(y) = dc (u, y). Clearly, D(u) = 0 =
dc (u, u). Suppose that there exists some vertex y such that D(y) 6=
dc (u, y).(如果演算法不正確)
Let y be the first vertex put into U with the property. (常用的技巧: 選
第一個在 algorithm 中不正確的y)
Then there is a u − x1 − x2 − · · · − y path P in G joining u and y whose
weight is d and d < D(y). (y 的 shortest path 是P )
Let xj ∈ P be the last vertex putting into U before y. (常用的技巧:
選P 上在 y 之前最後一個放在U 中的) Note that xj exists because of the
fact that u is the first vertex put into U .
The subpath P 0 : u = x0 − x1 − · · · − xj − xj+1 of P has weight
D(xj ) + c{xj , xj+1 } ≤ d < D(y).
Hence, by the algorithm, xj+1 put into U before y, contradicting the
5 KRUSKAL’S ALGORITHM
5
choice of xj . Thus, for every vertex y, D(y) = dc (u, y) and the algorithm
is correct.
5
Kruskal’s algorithm
Kruskal’s algorithm constructs a minimum-weight spanning tree.
Kruskal’s algorithm is a greedy algorithm and works as follows.
K rusk a l’s Algorit hm t o grow a m inim um -w e ight spa nning t re e
Let G (V , E ) be a connected n-vertex weighted graph with weight function c.
(5) Let F = { }.
(6) While | F | n 1 , Հ٨೯܂:
࠷ D {x, y} ਢࢬآࡸڶەᐞመऱᢰխ, c{x, y} ່՛ऱ߷ଡ.
If D does not create a cycle with the edges already in F,
then put x and y into U, and put D into F
else discard D .
(7) Let T (V , F ) .
Proof. When the algorithm terminates, T has no cycles and has exactly n − 1 edges; hence the resulting T is a spanning tree. Let the
n − 1 edges of F are α1 , α2 , · · · , αn−1 in the order that they are putting
into T . Suppose T is not a minimum-weight spanning tree, and T ∗
be a minimum-weight spanning tree that has the largest number of
edges in common with T . Let αk be the first edge not in T ∗ , then
α1 , α2 , · · · , αk−1 are in T ∗ . By Theorem 11.5.8, there is a β ∈ T ∗ such
that there is a spanning tree T ∗∗ = T ∗ + αk − β of G. Since T ∗∗ is a
spanning tree, we have
c(αk ) ≥ c(β).
Observe that α1 , α2 , · · · , αk−1 , β ∈ T ∗ does not create a cycle. Thus,
by the choice of the algorithm, we have
c(αk ) ≤ c(β).
Hence
c(αk ) = c(β).
6 PRIM’S ALGORITHM
6
Therefore, T ∗∗ is also a minimum-weight spanning tree of G and has
one more edge in common with T than T ∗ has, contradicting our choice
of T ∗ . The proof is complete.
Figure 5: Kruskal’s algorithm is to choose the edges ab, cd, ef, dg, eg, ag in the order
they are chosen. The weight of the resulting spanning tree is 15.
6
Prim’s algorithm
Prim’s algorithm constructs a minimum-weight spanning tree as
well.
Prim ’s Algorit hm t o grow a m inim um -w e ight spa nning t re e
Let G (V , E ) be a connected n-vertex weighted graph with weight function c.
(1) Let u be an any vertex of G and let U = {u}, F = { }.
(2) While | F | n 1 , Հ٨೯܂:
࠷ D {x, y} ਢࢬآࡸڶەᐞመऱᢰխ, x U , y U , and c{x, y}່՛ऱ߷ଡ.
Հ٨೯܂:
(i)
(ii)
Put y into U.
Put D into F.
(3) Let T = (U, F).
Proof. When the algorithm terminates, T has no cycles and has exactly n − 1 edges; hence the resulting T is a spanning tree. Let the
n − 1 edges of F are α1 , α2 , · · · , αn−1 in the order that they are putting
6 PRIM’S ALGORITHM
7
into T . Suppose T is not a minimum-weight spanning tree, and T ∗
be a minimum-weight spanning tree that has the largest number of
edges in common with T . Let αk be the first edge not in T ∗ , then
α1 , α2 , · · · , αk−1 are in T ∗ . Note that αk joins a vertex in Uk and
a vertex in its complement Uk . Since T ∗ is a spanning tree,
there is an edge β of T ∗ that joins a vertex in Uk and a vertex
in its complement Uk such that T ∗∗ = T ∗ + αk − β is a spanning
tree of G. We have
c(αk ) ≥ c(β).
Since αk has the smallest weight of all edges with one vertex
in Uk and the other in Uk . Hence
c(αk ) = c(β).
Therefore, T ∗∗ is also a minimum-weight spanning tree of G and has
one more edge in common with T than T ∗ has, contradicting our choice
of T ∗ . The proof is complete.
Figure 6: Prim’s algorithm is to choose the edges ab, af, f e, eg, gd, dc in the order
they are chosen. The weight of the resulting spanning tree is 15.
The advantage of Prim’s algorithm over the greedy algorithm is clear
in that, at each stage, we have only to determine an edge of smallest
weight which joins a vertex that has already been reached to a vertex
not yet reached. 好處就是 Prim’s algorithm 在每個 stage 所列入考慮的
edges 比較少, 一般來說, 如此可大大提高 algorithm 執行的效率.
© Copyright 2026 Paperzz