160 likes | 428 Views
The Manhattan Tourist Problem. Shane Wood 4/29/08 CS 329E. Problem Summary. A tourist group in Manhattan wishes to see as many sights moving south-east from the corner of 59 th St. and 8 th Ave. to 42 nd Street and Lexington Ave. How can we achieve this using dynamic programming?.
E N D
The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E
Problem Summary • A tourist group in Manhattan wishes to see as many sights moving south-east from the corner of 59th St. and 8th Ave. to 42nd Street and Lexington Ave. • How can we achieve this using dynamic programming?
Summary (cont.) • Imagine the map as a graph with a source (59th St. and 8th Ave.) and a sink ( 42nd St. and Lexington Ave.:
8th Ave 7th Ave 6th Ave 5th Ave Madison Ave Park Ave Lexington Ave Third Ave 59th St 57th St 55th St 53rd St 51st St 49th St 47th St 45th St 43rd St 42nd St 4 1 3 2 2 4 4 4 1 By imagining vertices representing intersections and weighted edges representing street blocks, we can reduce the Manhattan Tourist Problem to what is known as the Longest Path Problem.
Manhattan Tourist Problem: • Input: A weighted grid G with a starting source vertex and an ending sink vertex • Output: A longest path in G from source to sink
Strategy • It is better to solve a generalized version of the MTP • Rather than solving the longest path from (0,0) (source) to (n,m)(sink for an nxm grid), we will solve (0,0) to (i,j) for 0 ≤ i ≤ n and 0 ≤ j ≤ m (an arbitrary vertex) • If (i,j)is a vertex in the longest path to the sink, the longest path to (i,j)must be contained in the longest path to the sink
Exhaustive Solution • Generate ALL possible paths in grid G • Output the longest path • Not feasible for even a moderately sized graph
Source 3 1 4 2 2 3 3 3 1 3 4 2 7 2 3 2 6 3 5 7 9 1 1 1 12 v 28 Sink A Greedy Algorithm • At every vertex, choose the adjacent edge with the highest weight • Easily achievable in polynomial time, but is unlikely to give the optimal solution, especially for larger graphs!
S0,j 1 3 4 Si,0 2 3 2 3 3 1 3 4 2 7 2 3 2 6 3 5 7 9 1 1 1 DP Approach • For every vertex, we want to find si,j, the weight of the longest path from (0,0) to that vertex • Base Case: • Finding s0,j and si,0 for all i and j is easy
s0,1 + weight of edge between (0,1) and (1,1) s1,0 + weight of edge between (1,0) and (1,1) • The tourist can now arrive at (1,1) in one of two ways: • Traveling south from (1,0), or • Traveling east from (0,1) • Once s0,j and si,0 are computed, we can determine s1,1 by comparing these two possibilities and determining the max • s1,1 = max
si-1,j + weight of edge between (i-1,j) and (i,j) si,j-1 + weight of edge between (i,j-1) and (i,j) • This same logic applies more generally: • si,j = max We can thus compute every value for si,j recursively with one run through the grid
si-1,j + wi,j si,j-1 + wi,j Algorithm used for DP solution • Let wi,j represent the weight of a southerly move (the weight of the edge between (i,j-1) and (i,j) ) and wi,j represent the weight of an easterly move (the weight of the edge between (i-1, j) and (i,j) ) • 1 s0,0 0 • 2 for i 1 to n • 3 si,0si-1,0 + wi,0 • 4 for j 1 to n • 5 s0,j s0,j-1 + w0,j • 6 for i 1 to n • 7for j 1 to m • 8 si,j max • 9 return sn,m Running Time: O(nxm) for an nxm grid
Further analysis • Note that lines 1-5 in the algorithm are generating the base cases we use to develop later recurrence relations • We can generate the longest path by keeping track of which paths are used to generate sn,m!
Questions?? Thanks!