1. 程式人生 > >算法導論 Exercises 22.5(轉載)

算法導論 Exercises 22.5(轉載)

each ecif 內部 森林 ber lan 情況 incr cif

Exercises 22.5 - 算法導論.英文第3版

最近看書的同時, 感覺一些練習缺少參考, 所以按部分總結了自己的解答, 也能夠強化學習過程.
如有不足或疑問, 歡迎指正.

Exercises 22.5-1 How can the number of strongly connected components of a graph change if a new edge is added? 可以將每個強連通組件當作一個頂點, 組成強連通圖, 圖內頂點數量即強連通組件數量. 如果新增加的邊在頂點內部(即指向自己), 或者重復已有頂點間的邊, 則數量沒變化. 如果新增加的邊使圖形成環, 這樣環內頂點組成一個新的強連通組件, 所以數量減少N, N = (環內頂點數量 - 1).
Exercises 22.5-2 Show how the procedure STRONGLY-CONNECTED-COMPONENTS works on the graph of Figure 22.6. Specifically, show the finishing times computed in line 1 and the forest produced in line 3. Assume that the loop of lines 5–7 of DFS considers vertices in alphabetical order and that the adjacency lists are in alphabetical order. 技術分享
按照字母排序的循環和鄰接表, 可得出第一次DFS訪問頂點順序, 如下 (q (s (v (w, w) v) s) (t (x (z, z) x) (y, y) t) q) (r (u, u) r) finishing time: f(r) 20, f(u) 19, r(q) 16, f(t) 15, f(y) 14, f(x) 12, f(z) 11, f(s) 7, f(v) 6, f(w) 5. 根據 finishing time 降序排列, 並倒轉上圖中的邊, 第二次DFS訪問頂點順序, 如下 (r, r) * (u, u) * (q (y (t, t) y) q) * (x (z, z) x) * (s (w (v, v) w) s) 每個不相關括號(用*間隔)內的頂點, 代表 line 3 生成的樹(即強連通組件)

ANSWER:DFS(G)森林:(其中紅色為樹邊)

技術分享

強連通分量結果:

技術分享

Exercises 22.5-3 Professor Bacon claims that the algorithm for strongly connected components would be simpler if it used the original (instead of the transpose) graph in the second depth-first search and scanned the vertices in order of increasing finishing times. Does this simpler algorithm always produce correct results? 技術分享 設想對上圖應用指定的升序式算法, 圖中有A, B, C 三個頂點, A->B, B->A, B->C 三條邊. 如果首次 DFS 從 B 開始, 先向 A 遍歷, 則訪問頂點順序為 (B (A, A) (C, C) B) 升序排列 finishing time 是 A, C, B. 這樣第二次進行 DFS 則應從 A 開始, 該算法 結果顯示 A, B, C 是一個強連通組件, 而實際情況不是如此. 該算法不能保證通用性, 只有在選擇特殊頂點作為起始點, 以及規定鄰接表順序的情況下, 結果才有可能是正確的. Exercises 22.5-4 Prove that for any directed graph G, we have (( GT)SCC)T = (G)SCC. That is, the transpose of the component graph of GT is the same as the component graph of G. 在圖 G 中, 同一組件中的頂點可以互相連通. 而圖 GT 中, 組件中的邊雖然反轉, 但其頂點仍可以互相連通, 這方面是不受影響的, 組件之間的邊全部反轉, 同樣不會形成 新的組件. 所以 ((GT)SCC)T = (G)SCC, 即 GT 和 G 的強連通組件相同. Exercises 22.5-5 Give an O (V + E)-time algorithm to compute the component graph of a directed graph G = (V, E). Make sure that there is at most one edge between two vertices in the component graph your algorithm produces. (1) first DFS (G) if v finish orderList.push_front(v) // orderList 是 finishing time 降序隊列 時間 O(V + E) (2) for each v in V for each x in v.adj x.adj.push_back(v) 即求 GT 時間 O(V + E) (3) second DFS (GT) for v in order of orderList map v to scc // scc 是強連通組件 for each x in v.adj if x.color = COLOR_BLACK // 代表這條邊(v, x)是強連通組件間的邊 if x non-exist in scc.adj // 在DFS(GT)期間總搜索時間 O(E) scc.adj.push_back(x) 即求 scc 和 scc.adj 時間 O(V + E), 所以算法時間復雜度 O(V + E). Exercises 22.5-6 Given a directed graph G = (V, E), explain how to create another graph G‘ = (V, E‘) such that (a) G‘ has the same strongly connected components as G, (b) G‘ has the same component graph as G, and (c) E‘ is as small as possible. Describe a fast algorithm to compute G‘ (1) 確定每個SCC頂點集合, 以及SCC間所有邊集合 Es(不包含SCC內的邊), 時間 O (V + E) (2) 每個SCC內假設有點 v1, v2, ..., vn, 使其形成環 v1-> v2, v2->v3, ..., vn->v1, 時間 O (V) (3) 這裏假設 SCC 數量是 N, 生成 N * N 矩陣. 對 Es 中邊 (vi , vj), 假設 vi 屬於 SCCi , vj 屬於 SCCj. 如果 N[SCCi][SCCj] == 0, 則將 (vi, vj) 保存到 E‘ 中, 如果 N[SCCi][SCCj] == 1, 則跳過該邊. 依次計算 Es 中所有邊, 此處時間 O(E‘), 而 O(E‘) < O(E), 所以算法時間復雜度 O(V + E) Exercises 22.5-7 A directed graph G = (V, E) is semiconnected if, for all pairs of vertices u, v ∈ V, we have u ~> v or v ~> u. Give an efficient algorithm to determine whether or not G is semiconnected. Prove that your algorithm is correct, and analyze its running time. 強連通組件算法, 按第 2 次 DFS(GT) 強連通組件生成順序編號, 假設是 SCC1, SCC2, ..., SCCn 如果存在邊 (SCC1, SCC2), (SCC2, SCC3), ..., (SCCn-1, SCCn) , 即組件間邊形成線性的鏈, 則圖 G 是 semiconnected. 稍後詳細解釋. (1) 強連通組件圖算法, 時間 O(V+E) (2) 按強連通組件 SCC 順序編號, 假設是SCC1, SCC2, ..., SCCn, 並求出組件 SCC 的鄰接表, 這步驟可以在 (1) 中完成, 所以時間 O(V+E) (3) 遍歷 SCC 鄰接表, 如果對於範圍 i = 1, 2, ..., n-1, 強連通組件SCCi.adj 中存在 SCCi+1 , 既含有邊 (SCCi, SCCi+1), 強連通組件圖是線性鏈, 則 G = (V, E) 是 semiconnected, 時間 O(V+E). 詳細解釋, 按算法理論的支持, SCCi+1 不存在到SCCi 的邊, 如果沒有 (SCCi, SCCi+1), 則兩者均無法到達對方, 即不符合 G 是 semiconnected 的概念. 算法時間復雜度 O(V+E)

算法導論 Exercises 22.5(轉載)