Two more Dynamic Programming Problems Problem 1 Shortestpath
Two more Dynamic Programming Problems • Problem 1: Shortest-path in a graph with negative weights • Problem 2: Longest increasing subsequence problem
Example 1: single-source shortest-path problem with negative weight edges Input: directed graph G = <V, w, E> and source s. Output: d[v] = length of the shortest path from s to v. (assume that there is no negative weight cycle). Dijkstra’s algorithm does not work in this case. Dynamic programming formulation: D[v, j] = length of the shortest path from s to v that uses at most j edges. Write a recursive equation for D[v, j] in terms of D[u, j-1] (for various u’s) 2
Example 1: single-source shortest-path problem with negative weight edges Input: directed graph G = <V, w, E> and source s. Output: d[v] = length of the shortest path from s to v. (assume that there is no negative weight cycle). Dijkstra’s algorithm does not work in this case. Dynamic programming formulation: D[v, j] = length of the shortest path from s to v that uses at most j edges. Write a recursive equation for D[v, j] in terms of D[u, j-1] (for various u’s) D[v, j] = min { D[v, j-1] , min { D[u, j-1] + w(u, v)} } (u, v) in E 3
Example 2: longest increasing subsequence problem Input: Array A[1: n] containing n integers. Output: largest value of k such that there is a sequence of integers j 1, …, jk where j 1 < … < jk and A[j 1] < … < A[jk]. 4
Example 2: longest increasing subsequence problem Input: Array A[1: n] containing n integers. Output: largest value of k such that there is a sequence of integers j 1, …, jk where j 1 < … < jk and A[j 1] < … < A[jk]. Solution: Let s(j) = length of the longest sub-sequence of A[1: j] that ends with A[j]. Then, s(j) = 1 + max { s(k) } k<j A[k] < A[j] 5
- Slides: 5