the manhattan tourist problem shane wood 4/29/08 cs 329e

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

Upload: shanon-price

Post on 19-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

The Manhattan Tourist Problem

Shane Wood

4/29/08

CS 329E

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

Page 3: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

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 A

ve

7th A

ve

6th A

ve

5th A

ve

Madison A

ve

Park A

ve

Lexington

Ave

Third A

ve

59th St

57th St

55th St

53rd St

51st St

49th St

47th St

45th St

43rd St

42nd St

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

4

43rd St

42nd St

45th St

47th St

49th St

51st St

53rd St

55th St

57th St

59th St

Third

Ave

Lexingto

n Ave

Park

Ave

Madison

Ave

5th A

ve

6th A

ve

7th A

ve

8th A

ve

4

1

32

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.

Page 5: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

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

Page 6: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

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

Page 7: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

Exhaustive Solution

• Generate ALL possible paths in grid G

• Output the longest path

• Not feasible for even a moderately sized graph

Page 8: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

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!

3 4

3

31

21

2

2

32 6

1 1

74

5

1

7

3

3

9

3

2

Source

Sink12 v 28

Page 9: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

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,j

Si,03 4

3

31

21

2

2

32 6

1 1

74

5

1

7

3

3

9

3

2

Page 10: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

• 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 s0,1 + weight of edge between (0,1) and (1,1)

s1,0 + weight of edge between (1,0) and (1,1)

Page 11: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

• 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 + weight of edge between (i-1,j) and (i,j)

si,j-1 + weight of edge between (i,j-1) and (i,j)

Page 12: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

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,0 si-1,0 + wi,0 • 4 for j 1 to n• 5 s0,j s0,j-1 + w0,j • 6 for i 1 to n• 7 for j 1 to m• 8 si,j max

• 9 return sn,m

si-1,j + wi,j

si,j-1 + wi,j Running Time: O(nxm) for an nxm grid

Page 13: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

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!

Page 14: The Manhattan Tourist Problem Shane Wood 4/29/08 CS 329E

Questions??

Thanks!