Data Structures and Algorithms (II) Spring 2007
Voluntary Exercise 2
Tue, Mar 27 due Mon, Apr 09
Problem 1. (See CLRS Problem 23-1 ) Let G = (V, E) be an undirected, connected graph with weight function w : E → R, and suppose that |E| ≥ |V | and all edge weights are distinct.
A second-best minimum spanning tree is any minimum-weight spanning tree among all spanning trees except the one(s) of minimum weight. ♣
Solution.
a. We’ll show that the minimum spanning tree (MST) be unique. First we assume by contradiction that T and T0 are two different minimum spanning trees. And there is at least one edge (u, v) ∈ T0 and (u, v) /∈ T . Then, T ∪ (u, v) must form a cycle including (u, v). Let (x, y) be the heavist edge in the cycle. According to the cycle property, (x, y) can’t be contained in any MST. If (x, y) ∈ T (in T0 resp.), then T (T0 resp.) is not a MST, which contradicts to our assumption. Therefore the uniqueness of MST is proved.
The second-best MST is not unique as shown by the following example:
4 3
2
5 6
In the above graph, the best MST consists of edges of weights 2, 3, 5. There are two 2nd-best MSTs, one having edges of weights 2, 4, 5 and the other one having edges of weights 2, 3, 6.
b. Let T2 be a 2-nd best MST. Let (u, v) ∈ T − T2. Then, T2 ∪ (u, v) contains a cycle where one of the edges in the cycle is not in T (cycle property). Let this edge by (x, y).
Then, we must have w(x, y) > w(u, v), for otherwise, we could replace (x, y) in T2 by (u, v) to get a MST better than T2 . Now, we note that S = T2− (x, y) ∪ (u, v) is also a spanning tree since (u, v) and (x, y) are in the same cycle.
1
2
that S = T , therefore T and T2 differ with only one edge.
c. The main observation is that the path between two vertices u and v in the MST is unique. The algorithm is as follows:
Algorithm 1 For each u ∈ V , perform a BFS or DFS to find the maximum weight between u and every other v ∈ V . Since T is a spanning tree, the BFS Tree or DFS Tree has only tree edges. When we visit edge (x, y)(from x to y), the max-weight edge in the path from root u to y is found as following:
max[x, y] =
(max[u, x] if w(max[u, x]) > w[x, y];
(x, y) otherwise.
(1) (2)
For each u, the time for BFS or DFS is O(|V |) because a tree has only |V | − 1 edges.
So, the total time is O(|V |2).
d. From part b, we know that we can replace one edge (u, v) in the MST by another edge (x, y) to get a 2nd-best MST. How do we determine these two edges?
Note that if we replace (u, v) by (x, y), then
w(T2) = w(T ) − w(u, v) + w(x, y) = w(T ) + [w(x, y) − w(u, v)] (3) If we know which (x, y) to add to T2, then (u, v) must be the max-weight edge in the path from x to y (which can minimize the second part of the equation above), which can be found by part c. So, we get the following algorithm:
Algorithm 2 (1) Find MST by using Prim’s Algorithm.
(2) Find max-weight edges as in part c. Let max[x, y] denote the max-weight edge in the path from x to y in tree T.
(3) Find an edge (x, y) ∈ E − T that minimizes w(x, y) − w(max[x, y]).
(4) Output T2 = (T − max[x, y]) ∪ (x, y).
The step (1) can be done in time O(|V |2lg |V |) by using min-heap, and step (2) can be done in time O(|V |2). There are E = O(|V |2) edges to be considered in step (3), and so step (3) takes time O(|V |2). The total runtime is O(|V |2lg |V |).
If we use Fibonacci heap to realize the priority queue in the Prim’s Algorithm, the running-time will be guaranteed in O(|V |2).
2
Problem 2
(From 守壹, 愷陽)
a. 題目是:Let T be a spanning tree of G whose largest edge weight is minimum over all spanning trees of G Ù T is a minimum spanning tree.
=> 這個方向是錯的。如左 圖,雖然兩個 tree 的 largest edge weight 都是 100,但是 左邊那顆不是 minimum spanning tree
<= 這個方向是對的。假設有一個 MST T,他的 largest edge weight w(e)不是 minimum over all spanning trees。 表示說有另一個 spanning tree T*, 他的 largest edge weight y < w(e)。所以 w(e)比在 T*中的每一個邊的 weight 都還要 重。而當我們把 e 放到 T*,會產生一個 cycle,而 e 在此 cycle 中是 weight 最重的 edge,但是這個和 cycle property 一個 cycle 最重的 edge 不可能在 MST 中矛盾,所以若 T 是 MST,則 T is a spanning tree of G whose largest edge weight is minimum over all spanning trees of G.
b. We claim that 如果在原本的 graph 上的 MST 是 T,加了新的點以後,在考慮 新的 graph 的 MST T’時,那些原本在舊的 graph 上的 non tree edge 不可能是 T’的一個邊。因為那些 non tree edge,在舊的 graph 上是某一個 cycle 的最重 邊,但是因為新的 graph 我們只有加入新的點和邊,所以那些 cycle 仍然存在 在新的 graph 中,所以那些 non tree edge,由 cycle property,不可能在 T’上。
所以我們現在需要考慮新的 graph 中的 edge 就變少了。m = (n – 1) + n = O(n)。其中 n – 1 是在 T 上的那些邊,而 n 是指加入的新的點,他最多會新 加 n 個 edge 到新的 graph 裡面。而 Kruskal algorithm 是 O(mlogn),所以要 update 到新的 MST 需要花 O(nlogn)。
100 2
3
4 2 100
3 4
undirected, connected graph G = (V, E) in which all edge weights are non-negative.
Analyze its running time.
Solution.
For each edge (u,v) in G, remove edge (u,v) from G but keep the end vertices. Run Dijkstra’s algorithm to find shortest path from u to v. If the shortest path from u to v exists, add (u,v) to this path will form a cycle. Then we can find the cycle with minimum weight among them. This algorithm runs in O(E(E + VlgV)).
Another approach is running Dijkstra’s algorithm for each vertex v, and for each edge (x,y) not in the shortest path tree, add (x,y) to the tree will form a cycle. Since we have the distance from v to all vertices, we can find the cycle with minimum weight by adding non-tree edges in O(V2 + E) time. So the total running time of this algorithm is O(V(E + VlgV + V2)).
Problem 4.
Solution:
Assume we choose u to be the next vertex, then d[u] must be equal to d(u), the
shortest distance from r to u. By contradiction, there might be some negative edges on the path from another vertex y to u, then we will evaluate new distance of u, d’[u].
The negative edges may cause d’[u] smaller than d[u], so d[u] is not the shortest distance from r to u.
But if the negative edges all incident to r, it’s impossible that any negative edge on the path from y to u. We always evaluate d[u] correctly.
r
x
u y
Negative edge?