Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E.

Similar presentations


Presentation on theme: "The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E."— Presentation transcript:

1 The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

2 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?

3 Summary (cont.) Imagine the map as a graph with a source (59 th St. and 8 th Ave.) and a sink ( 42 nd St. and Lexington Ave.: 8 th Ave 7 th Ave 6 th Ave 5 th Ave MadisonAve Park Ave LexingtonAve ThirdAve 59 th St 57 th St 55 th St 53 rd St 51 st St 49 th St 47 th St 45 th St 43 rd St 42 nd St

4 4 43 rd St 42 nd St 45 th St 47 th St 49 th St 51 st St 53 rd St 55 th St 57 th St 59 th St ThirdAve LexingtonAve Park Ave MadisonAve 5 th Ave 6 th Ave 7 th Ave 8 th Ave 4 1 3 2 2 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.

5 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

6 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 n x m 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

7 Exhaustive Solution Generate ALL possible paths in grid G Output the longest path Not feasible for even a moderately sized graph

8 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! 34 3 31 2 1 2 2 3 26 11 74 5 1 7 3 3 9 3 2 Source Sink 12 v 28

9 DP Approach For every vertex, we want to find s i,j, the weight of the longest path from (0,0) to that vertex Base Case: –Finding s 0,j and s i,0 for all i and j is easy S 0,j S i,0 34 3 31 2 1 2 2 3 26 11 74 5 1 7 3 3 9 3 2

10 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 s 0,j and s i,0 are computed, we can determine s 1,1 by comparing these two possibilities and determining the max –s 1,1 = max s 0,1 + weight of edge between (0,1) and (1,1) s 1,0 + weight of edge between (1,0) and (1,1)

11 This same logic applies more generally: –s i,j = max We can thus compute every value for s i,j recursively with one run through the grid s i-1,j + weight of edge between (i-1,j) and (i,j) s i,j-1 + weight of edge between (i,j-1) and (i,j)

12 Algorithm used for DP solution Let w i,j represent the weight of a southerly move (the weight of the edge between (i,j-1) and (i,j) ) and w i,j represent the weight of an easterly move (the weight of the edge between (i-1, j) and (i,j) ) 1 s 0,0 0 2 for i 1 to n 3 s i,0 s i-1,0 + w i,0 4 for j 1 to n 5 s 0,j s 0,j-1 + w 0,j 6 for i 1 to n 7 for j 1 to m 8 s i,j max 9 return s n,m s i-1,j + w i,j s i,j-1 + w i,j Running Time: O(n x m) for an n x m grid

13 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 s n,m !

14 Questions?? Thanks!


Download ppt "The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E."

Similar presentations


Ads by Google