Title: Dynamic Programming: Manhattan Tourist Problem Lecture 17
1Dynamic ProgrammingManhattan Tourist
ProblemLecture 17
2ManhattanTouristProblem(MTP)
3Manhattan Tourist Problem (MTP)
Imagine seeking a path (from source to sink) to
travel (only eastward and southward) with the
most number of attractions () in the Manhattan
grid
Source
Sink
4Manhattan Tourist Problem (MTP)
Imagine seeking a path (from source to sink) to
travel (only eastward and southward) with the
most number of attractions () in the Manhattan
grid
Source
Sink
5Manhattan Tourist Problem Formulation
Goal Find the longest path in a weighted grid.
Input A weighted grid G with two distinct
vertices, one labeled source and the other
labeled sink
Output A longest path in G from source to
sink
6MTP An Example
0
1
2
3
4
j coordinate
source
3
2
4
0
9
5
3
0
0
1
0
4
3
2
2
3
2
4
13
1
1
6
5
4
2
0
7
3
4
19
15
2
i coordinate
4
5
2
4
1
3
3
0
2
3
20
3
8
5
6
5
sink
2
1
3
2
23
4
7MTP Greedy Algorithm Is Not Optimal
1
2
5
source
3
10
5
5
2
5
1
3
5
3
1
4
2
3
promising start, but leads to bad choices!
5
0
2
0
22
0
0
0
sink
18
8MTP Simple Recursive Program
- MT(n,m)
- if n0 or m0
- return MT(n,m)
- x ? MT(n-1,m)
- length of the edge from (n-
1,m) to (n,m) - y ? MT(n,m-1)
- length of the edge from
(n,m-1) to (n,m) - return maxx,y
9MTP Simple Recursive Program
- MT(n,m)
- x ? MT(n-1,m)
- length of the edge from (n-
1,m) to (n,m) - y ? MT(n,m-1)
- length of the edge from
(n,m-1) to (n,m) - return minx,y
- Whats wrong with this approach?
10MTP Dynamic Programming
j
0
1
source
1
0
1
S0,1 1
i
5
1
5
S1,0 5
- Calculate optimal path score for each vertex in
the graph - Each vertexs score is the maximum of the prior
vertices score plus the weight of the respective
edge in between
11MTP Dynamic Programming (contd)
j
0
1
2
source
1
2
0
1
3
S0,2 3
i
5
3
-5
1
5
4
S1,1 4
3
2
8
S2,0 8
12MTP Dynamic Programming (contd)
j
0
1
2
3
source
1
2
5
0
1
3
8
S3,0 8
i
5
10
3
1
-5
1
5
4
13
S1,2 13
3
5
-5
2
8
9
S2,1 9
0
3
8
S3,0 8
13MTP Dynamic Programming (contd)
j
0
1
2
3
source
1
2
5
0
1
3
8
i
5
3
10
-5
-5
1
-5
1
5
4
13
8
S1,3 8
3
5
-3
3
-5
2
8
9
12
S2,2 12
0
0
0
3
8
9
S3,1 9
greedy alg. fails!
14MTP Dynamic Programming (contd)
j
0
1
2
3
source
1
2
5
0
1
3
8
i
5
3
10
-5
-5
1
-5
1
5
4
13
8
3
5
-3
2
3
3
-5
2
8
9
12
15
S2,3 15
0
0
-5
0
0
3
8
9
9
S3,2 9
15MTP Dynamic Programming (contd)
j
0
1
2
3
source
1
2
5
0
1
3
8
Done!
i
5
3
10
-5
-5
1
-5
1
5
4
13
8
(showing all back-traces)
3
5
-3
2
3
3
-5
2
8
9
12
15
0
0
-5
1
0
0
0
3
8
9
9
16
S3,3 16
16MTP Recurrence
Computing the score for a point (i,j) by the
recurrence relation
The running time is n x m for a n by m grid (n
of rows, m of columns)
17Manhattan Is Not A Perfect Grid
What about diagonals?
- The score at point B is given by
18Manhattan Is Not A Perfect Grid (contd)
Computing the score for point x is given by the
recurrence relation
- Predecessors (x) set of vertices that have
edges leading to x - The running time for a graph G(V, E)
(V is the set of all vertices and E is
the set of all edges) is O(E) since each
edge is evaluated once
19Traveling in the Grid
- The only hitch is that one must decide on the
order in which visit the vertices - By the time the vertex x is analyzed, the values
sy for all its predecessors y should be computed
otherwise we are in trouble. - We need to traverse the vertices in some order
- Try to find such order for a directed cycle
- ???
-
20DAG Directed Acyclic Graph
- Since Manhattan is not a perfect regular grid, we
represent it as a DAG - DAG for Dressing in the morning problem
21Topological Ordering
- A numbering of vertices of the graph is called
topological ordering of the DAG if every edge of
the DAG connects a vertex with a smaller label to
a vertex with a larger label - In other words, if vertices are positioned on a
line in an increasing order of labels then all
edges go from left to right.
22Topological ordering
- 2 different topological orderings of the DAG
23Longest Path in DAG Problem
- Goal Find a longest path between two vertices in
a weighted DAG - Input A weighted DAG G with source and sink
vertices - Output A longest path in G from source to sink
24Longest Path in DAG Dynamic Programming
- Suppose vertex v has indegree 3 and predecessors
u1, u2, u3 - Longest path to v from source is
- In General
- sv maxu (su weight of edge from u to v)
su1 weight of edge from u1 to v su2 weight
of edge from u2 to v su3 weight of edge from
u3 to v
25Traversing the Manhattan Grid
a)
b)
- 3 different strategies
- a) Column by column
- b) Row by row
- c) Along diagonals
c)