[問題] directed graph找shortest path

看板Prob_Solve作者 (無法顯示)時間12年前 (2011/12/22 19:17), 編輯推噓4(4012)
留言16則, 3人參與, 最新討論串1/1
We have a directed graph G = (V, E) represented using adjacency lists. The edge costs are integers in the range {1, 2, 3, 4, 5}. Assume that G has no self-loops or multiple edges. Design an algorithm that solves the single-source shortest path problem on G in O(|V|+|E|). 請問這怎麼把這個directed graph轉成undirected graph再套BFS解呢? ================== 還想請問一個multigraph的shortest跟longest path的問題.. http://ppt.cc/R1,B 請問這兩題應該怎麼解呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.118.110.186

12/22 19:52, , 1F
BFS 可以直接在 directed graph 上做,不必轉
12/22 19:52, 1F

12/22 19:52, , 2F
將長度為 i 的 edge 轉成 i 條 edges 可能就可以
12/22 19:52, 2F
可是轉成i條edges 這些edges還是directed edge嗎? 轉了之後要怎麼在O(V+E)內解single shortest path呢?

12/22 19:54, , 3F
是 multistage graph 不是 multigraph ...
12/22 19:54, 3F

12/22 19:54, , 4F
看起來 shortest path 與 longest path 的做法類似
12/22 19:54, 4F

12/22 19:55, , 5F
有長度為負數的邊時應該也可以,因為沒有 cycle
12/22 19:55, 5F

12/22 19:59, , 6F
這不就是DAG 用DP就好了呀
12/22 19:59, 6F
可以請問shortest path跟longest path的遞迴式要怎麼寫嗎@@?

12/22 20:15, , 7F
DP(v1) = max/min(DP(v1), DP(v2)+weight) //v1->v2
12/22 20:15, 7F

12/22 20:16, , 8F
max 是longest path ,min是shortest path
12/22 20:16, 8F
謝謝 因為我看解答好像不是很懂 http://ppt.cc/cVIa 紅字框起來的地方好像說要backwardly 照解答這樣..是不是應該這樣寫 L[1,j] = wi + L[i,j] ?

12/23 05:32, , 9F
因為每條邊的長度最多是 5,所以將長度為 i 的邊
12/23 05:32, 9F

12/23 05:32, , 10F
轉成 i 條邊之後,邊數最多是 5|E| 條
12/23 05:32, 10F

12/23 05:33, , 11F
代入 BFS 一樣是 O(V+E) 的時間內可以做完
12/23 05:33, 11F

12/23 11:21, , 12F
1. 一條邊,權重是5,就直接拆成4個點+5條邊。再跑BFS即可。
12/23 11:21, 12F

12/23 11:21, , 13F
就算每條邊都拆,最後也才5V點 5E邊,時間複雜度仍是O(V+E)
12/23 11:21, 13F
第一題 好像是I2A二版的習題24.3-5跟24.3-6的改版 網路上的解法 http://ppt.cc/uxY9 可是我看不太懂它的G'是什麼@@

12/23 11:22, , 14F
2. 請參考 activity on edge network,用DP解。
12/23 11:22, 14F

12/23 11:28, , 15F
對了忘記說 directed/undirected graph本來就都可以執行BFS
12/23 11:28, 15F
※ 編輯: mqazz1 來自: 140.118.110.186 (12/25 21:51)

12/28 19:34, , 16F
它的G'就是一條邊長度L變成L條邊長度一 只是寫的很文言而已
12/28 19:34, 16F
文章代碼(AID): #1Eyn7air (Prob_Solve)