cse 421
play

CSE 421 Longest Path in a DAG, LIS, Shortest Path with Negative - PowerPoint PPT Presentation

CSE 421 Longest Path in a DAG, LIS, Shortest Path with Negative Weights Shayan Oveis Gharan 1 Longest Path in a DAG Longest Path in a DAG Goal: Given a DAG G, find the longest path. Recall: A directed graph G is a DAG if it has no cycle.


  1. CSE 421 Longest Path in a DAG, LIS, Shortest Path with Negative Weights Shayan Oveis Gharan 1

  2. Longest Path in a DAG

  3. Longest Path in a DAG Goal: Given a DAG G, find the longest path. Recall: A directed graph G is a DAG if it has no cycle. This problem is NP-hard for general 2 3 directed graphs: - It has the Hamiltonian Path as a 6 5 4 special case 7 1 3

  4. DP for Longest Path in a DAG Q: What is the right ordering? Remember, we have to use that G is a DAG, ideally in defining the ordering We saw that every DAG has a topological sorting So, let’s use that as an ordering. 2 3 1 2 3 4 5 6 7 6 5 4 7 1 4

  5. DP for Longest Path in a DAG Suppose we have labelled the vertices such that (𝑗, π‘˜) is a directed edge only if 𝑗 < π‘˜. 1 2 3 4 5 6 7 Let π‘ƒπ‘„π‘ˆ(π‘˜) = length of the longest path ending at π‘˜ Suppose in the longest path ending at π‘˜, last edge is (𝑗, π‘˜) . Then, none of the 𝑗 + 1, … , π‘˜ βˆ’ 1 are in this path since topological ordering. So, π‘ƒπ‘„π‘ˆ π‘˜ = π‘ƒπ‘„π‘ˆ 𝑗 + 1. 5

  6. DP for Longest Path in a DAG Suppose we have labelled the vertices such that (𝑗, π‘˜) is a directed edge only if 𝑗 < π‘˜. Let π‘ƒπ‘„π‘ˆ(π‘˜) = length of the longest path ending at π‘˜ 0 If π‘˜ is a source π‘ƒπ‘„π‘ˆ π‘˜ = 0 1 + 5: 5,7 89 :;<: π‘ƒπ‘„π‘ˆ(𝑗) max o.w. 6

  7. DP for Longest Path in a DAG Let G be a DAG given with a topological sorting: For all edges (𝒋, π’Œ) we have i<j. Compute-OPT(j){ if (in-degree(j)==0) return 0 if (M[j]==empty) M[j]=0; for all edges (i,j) M[j] = max(M[j],1+Compute-OPT(i)) return M[j] } Output max(M[1],…,M[n]) Running Time: 𝑃 π‘œ + 𝑛 Memory: 𝑃 π‘œ Can we output the longest path? 7

  8. Outputting the Longest Path Let G be a DAG given with a topological sorting: For all edges (𝒋, π’Œ) we have i<j. Initialize Parent[j]=-1 for all j. Compute-OPT(j){ if (in-degree(j)==0) return 0 if (M[j]==empty) M[j]=0; Record the entry that for all edges (i,j) we used to compute OPT(j) if (M[j] < 1+Compute-OPT(i)) M[j]=1+Compute-OPT(i) Parent[j]=i return M[j] } Let M[k] be the maximum of M[1],…,M[n] While (Parent[k]!=-1) Print k k=Parent[k] 8

  9. Longest Path in a DAG

  10. Longest Path in a DAG Goal: Given a DAG G, find the longest path. Recall: A directed graph G is a DAG if it has no cycle. This problem is NP-hard for general 2 3 directed graphs: - It has the Hamiltonian Path as a 6 5 4 special case 7 1 10

  11. DP for Longest Path in a DAG Suppose we have labelled the vertices such that (𝑗, π‘˜) is a directed edge only if 𝑗 < π‘˜. 1 2 3 4 5 6 7 Let π‘ƒπ‘„π‘ˆ(π‘˜) = length of the longest path ending at π‘˜ Suppose OPT(j) is 𝑗 A , 𝑗 B , 𝑗 B , 𝑗 C , … , 𝑗 DEA , 𝑗 D , 𝑗 D , π‘˜ , then Obs 1: 𝑗 A ≀ 𝑗 B ≀ β‹― ≀ 𝑗 D ≀ π‘˜. Obs 2: 𝑗 A , 𝑗 B , 𝑗 B , 𝑗 C , … , 𝑗 DEA , 𝑗 D is the longest path ending at 𝑗 D . π‘ƒπ‘„π‘ˆ π‘˜ = 1 + π‘ƒπ‘„π‘ˆ 𝑗 D . 11

  12. DP for Longest Path in a DAG Suppose we have labelled the vertices such that (𝑗, π‘˜) is a directed edge only if 𝑗 < π‘˜. Let π‘ƒπ‘„π‘ˆ(π‘˜) = length of the longest path ending at π‘˜ 0 If π‘˜ is a source π‘ƒπ‘„π‘ˆ π‘˜ = 0 1 + 5: 5,7 89 :;<: π‘ƒπ‘„π‘ˆ(𝑗) max o.w. 12

  13. Outputting the Longest Path Let G be a DAG given with a topological sorting: For all edges (𝒋, π’Œ) we have i<j. Initialize Parent[j]=-1 for all j. Compute-OPT(j){ if (in-degree(j)==0) return 0 if (M[j]==empty) M[j]=0; Record the entry that for all edges (i,j) we used to compute OPT(j) if (M[j] < 1+Compute-OPT(i)) M[j]=1+Compute-OPT(i) Parent[j]=i return M[j] } Let M[k] be the maximum of M[1],…,M[n] While (Parent[k]!=-1) Print k k=Parent[k] 13

  14. Longest Increasing Subsequence

  15. Longest Increasing Subsequence Given a sequence of numbers Find the longest increasing subsequence 41, 22, 9, 15, 23, 39, 21, 56, 24, 34, 59, 23, 60, 39, 87, 23, 90 41, 22, 9 , 15 , 23 , 39, 21, 56, 24 , 34 , 59 , 23, 60 , 39, 87 , 23, 90 15

  16. DP for LIS Let OPT(j) be the longest increasing subsequence ending at j. Observation: Suppose the OPT(j) is the sequence 𝑦 5 I , 𝑦 5 J , … , 𝑦 5 K , 𝑦 7 Then, 𝑦 5 I , 𝑦 5 J , … , 𝑦 5 K is the longest increasing subsequence ending at 𝑦 5 K , i.e., π‘ƒπ‘„π‘ˆ π‘˜ = 1 + π‘ƒπ‘„π‘ˆ(𝑗 D ) If 𝑦 7 > 𝑦 5 for all 𝑗 < π‘˜ 1 o.w. π‘ƒπ‘„π‘ˆ π‘˜ = L 1 + max 5:M N OM P π‘ƒπ‘„π‘ˆ(𝑗) Remark: This is a special case of Longest path in a DAG: Construct a 16 graph 1,…n where (𝑗, π‘˜) is an edge if 𝑗 < π‘˜ and 𝑦 5 < 𝑦 7 .

  17. Shortest Paths with Negative Edge Weights

  18. Shortest Paths with Neg Edge Weights Given a weighted directed graph 𝐻 = π‘Š, 𝐹 and a source vertex 𝑑, where the weight of edge (u,v) is 𝑑 W,X Goal: Find the shortest path from s to all vertices of G. Recall that Dikjstra’s Algorithm fails when weights are negative 1 1 -2 -2 3 3 3 source 3 2 2 s s -1 2 -1 2 4 4 18

  19. Impossibility on Graphs with Neg Cycles Observation: No solution exists if G has a negative cycle. This is because we can minimize the length by going over the cycle again and again. So, suppose G does not have a negative cycle. 1 3 -2 2 3 s 2 -1 4 19

  20. DP for Shortest Path Def: Let π‘ƒπ‘„π‘ˆ(𝑀, 𝑗) be the length of the shortest 𝑑 - 𝑀 path with at most 𝑗 edges. Let us characterize π‘ƒπ‘„π‘ˆ(𝑀, 𝑗). Case 1: π‘ƒπ‘„π‘ˆ(𝑀, 𝑗) path has less than 𝑗 edges. Then, π‘ƒπ‘„π‘ˆ 𝑀, 𝑗 = π‘ƒπ‘„π‘ˆ 𝑀, 𝑗 βˆ’ 1 . β€’ Case 2: π‘ƒπ‘„π‘ˆ(𝑀, 𝑗) path has exactly 𝑗 edges. Let 𝑑, 𝑀 A , 𝑀 B , … , 𝑀 5EA , 𝑀 be the π‘ƒπ‘„π‘ˆ(𝑀, 𝑗) path with 𝑗 edges. β€’ Then, 𝑑, 𝑀 A , … , 𝑀 5EA must be the shortest 𝑑 - 𝑀 5EA path with at β€’ most 𝑗 βˆ’ 1 edges. So, π‘ƒπ‘„π‘ˆ 𝑀, 𝑗 = π‘ƒπ‘„π‘ˆ 𝑀 5EA , 𝑗 βˆ’ 1 + 𝑑 X NZI ,X 20

  21. DP for Shortest Path Def: Let π‘ƒπ‘„π‘ˆ(𝑀, 𝑗) be the length of the shortest 𝑑 - 𝑀 path with at most 𝑗 edges. 0 if 𝑀 = 𝑑 ∞ if 𝑀 β‰  𝑑, 𝑗 = 0 π‘ƒπ‘„π‘ˆ 𝑀, 𝑗 = [ min(π‘ƒπ‘„π‘ˆ 𝑀, 𝑗 βˆ’ 1 , W: W,X 89 :;<: π‘ƒπ‘„π‘ˆ 𝑣, 𝑗 βˆ’ 1 + 𝑑 W,X ) min So, for every v, π‘ƒπ‘„π‘ˆ 𝑀, ? is the shortest path from s to v. But how long do we have to run? Since G has no negative cycle, it has at most π‘œ βˆ’ 1 edges. So, π‘ƒπ‘„π‘ˆ(𝑀, π‘œ βˆ’ 1) is the answer. 21

  22. Bellman Ford Algorithm for v=1 to n if π’˜ β‰  𝒕 then M[v,0]= ∞ M[s,0]=0. for i=1 to n-1 for v=1 to n M[v,i]=M[v,i-1] for every edge (u,v) M[v,i]=min(M[v,i], M[u,i-1]+c u,v ) Running Time: 𝑃 π‘œπ‘› Can we test if G has negative cycles? Yes, run for i=1…3n and see if the M[v,n-1] is different from M[v,3n] 22

  23. DP Techniques Summary Recipe: β€’ Follow the natural induction proof. β€’ Find out additional assumptions/variables/subproblems that you need to do the induction β€’ Strengthen the hypothesis and define w.r.t. new subproblems Dynamic programming techniques. β€’ Whenever a problem is a special case of an NP-hard problem an ordering is important: β€’ Adding a new variable: knapsack. β€’ Dynamic programming over intervals: RNA secondary structure. Top-down vs. bottom-up: β€’ Different people have different intuitions β€’ Bottom-up is useful to optimize the memory 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend