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. 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
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
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
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
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
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
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. 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
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
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
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
Longest Increasing Subsequence
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
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 .
Shortest Paths with Negative Edge Weights
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
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
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
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
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
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
Recommend
More recommend