- xian
A DFS forest can be generated by perform DFS on a directed graph. There are 4 types of edges in a DFS forest: tree edge
, forward edge
, back edge
and cross edge
. Modify DFS so that it can determine the type of each edge.
【edge的定義】
【解題思路】
利用塗色的方法解決問題,根據DFS Property我們可以將遇到不同顏色看做是edge的分類:
因此我們可以在DFS Algorithm中加入判斷式,獲得我們要的答案。
【變數定義】
【蘇都扣】
- LXS
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.
Vertices | Discover d[u] | Finish f[u] |
---|---|---|
q | 1 | 16 |
r | 17 | 20 |
s | 2 | 7 |
t | 8 | 15 |
u | 18 | 19 |
v | 3 | 6 |
w | 4 | 5 |
x | 9 | 12 |
y | 13 | 14 |
z | 10 | 11 |
Call but consider the vertices in order of decreasing f[u] computed in line 1.
Vertices | Discover d[u] | Finish f[u] |
---|---|---|
r | 17 | 20 |
u | 18 | 19 |
q | 1 | 16 |
t | 8 | 15 |
y | 13 | 14 |
x | 9 | 12 |
z | 10 | 11 |
s | 2 | 7 |
v | 3 | 6 |
w | 4 | 5 |
Vertices | Discover d[u] | Finish f[u] |
---|---|---|
r | 1 | 2 |
u | 3 | 4 |
q | 5 | 10 |
t | 7 | 8 |
y | 6 | 9 |
x | 11 | 14 |
z | 12 | 13 |
s | 15 | 20 |
v | 17 | 18 |
w | 16 | 19 |
由此可得到五棵樹:
- Mark
前情提要
G = (V, E) is semi-connected => for all pairs of vertices u, v in V, we have u->v or v->u.
思路
graph
做求有向圖的SCC的Tarjan's Algorith
得出SCC,graph
(已變成DAG)->gN
gN
做DFS()
檢查是否為semi-connected
,,查看是否為線性鏈蘇都扣
【step 1,2】
【step 3】
ans:
證明
如果這個圖是 semi-connected,對於任意的,都會有 path ,假設 是他們的SCCs,如果有 path 從 ,那麼一定也會有 path 從 ,因為 裡的 node 都是 strongly-connected。
- songchiu
題意:張貴雲教授教的「用stack解DFS」可否拿來處理Topo Sorting、Strongly-Connected-Component
【Topological Sorting】
結論:可以,但要修改。
原本是沒辦法的,因為順序會有差,以ppt裡的圖為例:
何老師的作法,DFS會求出:AFEGDCB
張老師的作法,DFS會求出:AGEDBCF
很明顯的,E不應該在F的前面,因此不適用
會發生這樣的狀況因為較後面被走訪的點,先被pop出來了
因此我們要將該DFS修改一下,加入stack的點先不要pop出來,待它的子小孩都過且離開stack後,再將點pop出來
當每個子小孩離開stack時,順便將它的離開時間紀下來。待全部完成後,把紀下來的時間倒過來,即為topo sort的時間
附上蘇都扣來參考一下:
【Strongly Connected Components】
結論:可以,但也要修改。
在此說明常用的SCC解法:
假設從開始找,的時間為1;接著向下找,依序找到、;
接著就會走回,但發現走過了,就會將的時間傳給,接著再依序往上傳給 (利用遞迴式回傳值的方式)
因此我們在一張圖中,看到有幾種不同的時間,就是有幾個
但是第二種解法,它用stack來處理
假設有一點,在準備被走訪前,先被丟到stack
走訪時被從stack取出,走訪後就沒辦法再回頭改它的值了
由於我們沒辦法回頭更新,因此這個方法不適合拿來解
但還是有辦法解的,只要稍微修改一下就行了
跟剛剛的很像,但多拿一個stack 來紀錄一下當時走訪過哪些點
之後再透過一個vector 來存放,擁有相同counter值的,會被放到同個vector元素裡
這樣就可以找出它們是不是SCC的關係了
- songchiu
假設原本的圖形是這樣,紫色的邊構成
接著將剪掉,此時是兩個分開的connected component
我們需要再找個權重較低的邊,把圖重新組回
假定是權重較小的邊 ()
它能使圖形再次回到(因為已經是當前最小)
會得出這樣的結論,是依據Theorem 23.1
當把剪掉後,在它的crossing edge當中,可以找到一個最輕邊
這個最輕邊也會是MST裡的一條邊
用數學式來證就是:
因為
所以
因此若是,也會是
- chung
Let e be a maximum-weight edge on some cycle of connected graph G = (V, E). Prove that there is a minimum spanning tree of G’ = (V, E – {e}) that is also a minimum spanning tree tree of G. That is, there is a minimum spanning tree of G that does not include e.
【Proof】
利用反證法,假設 T 是一顆隨意的生成樹,不包含上述被剪掉的最大weight的邊 e・由於樹的特性,可以得知點之間一定有路徑互相連接,假設這條路徑為P,P 一定會穿過 S 與 V − S 兩個子集,假設連接兩個 set 的邊為 e',e' 連接的兩個點分別為 V' 與 W'・將 e' 替換成 e,生成新的樹 T',由於 e > e',因此 T' 的權重勢必會比較大,而 T' 的每個點也都會有路徑相連,同時 T' 是以 e' 替換 e 形成,並沒有引進新的邊,因此也不會出現 cycle,故得證 T' 的權重大於 T,T'不是最小生成樹,故最小生成樹不會包含e。
- yee
Show that a graph has a unique minimum spanning tree if, for every cut of the graph, there is a unique light edge crossing the cut. Show that the converse is not true by giving a counterexample.
【解題思路】
透過反證法,先假設有兩棵,分別為
先從 中隨機移除一條邊 ,產生兩個的子集
因為 ,由 可知 為 中的唯一最輕邊
再假設 為 且穿過
由於穿過 的最輕邊是唯一
即代表 與 為同一條邊,所以 也屬於
又因 為隨機選擇,代表所有於 上的邊也於 上
即可證明 是唯一
【Counterexample】
若一 存在唯一 , 中未必存在唯一最輕邊
由上圖可知, 皆為最輕邊