Docsity

Dynamic Programming and the Manhattan Tourist Problem - Prof. Wei Wang, Study notes of Computer Science

Toggle sidebar
bg1
9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 1
Lecture 8:
Lecture 8:
Dynamic Programming
Dynamic Programming
Preliminaries
Preliminaries
Study Chapter 6.1
Study Chapter 6.1-
-6.3
6.3
9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 2
Revisit Coin
Revisit Coin-
-Change Problem
Change Problem
•So far we’ve tried:
–A greedy algorithm that does not work for all inputs
(it is incorrect)
–An exhaustive search algorithm that is correct, but
can take a long time
–A branch-and-bound algorithm that is correct, and
works well for most cases, but might be as slow as an
exhaustive search for some inputs.
•Is there anything
else we can try?
Coin-change problem
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Dynamic Programming and the Manhattan Tourist Problem - Prof. Wei Wang and more Study notes Computer Science in PDF only on Docsity!

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 1

Dynamic ProgrammingDynamic ProgrammingLecture 8:Lecture 8:

PreliminariesPreliminaries

Study Chapter 6.1Study Chapter 6.1--6.36.

Revisit Coin- Revisit Coin-Change ProblemChange Problem

• So far we’ve tried: – A greedy algorithm that does not work for all inputs

– (it is incorrect)An exhaustive search algorithm that is correct, but

– can take a long timeA branch-and-bound algorithm that is correct, and

works well forexhaustive search for some inputs. most cases, but might be as slow as an

• Is there anythingelse we can try? Coin-change problem

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 3

Tabulating the Answers Tabulating the Answers

• If it is costly to compute the answer for a giveninput, then there may be advantages to caching

• This trades-off time-complexity for space^ the result of the calculation in a table

• How could we fill inthe table in the first

place? – Run our best correct

– algorithmCan the table help? 11¢10¢9¢8¢7¢6¢^11111114321 52¢51¢50¢49¢48¢47¢^22222211121432

5¢4¢3¢2¢1¢ 1 4321 46¢45¢44¢43¢42¢ 22222 11 1432

Amt 2520105 1 Amt 2520105 1

Filling in a Missing Table Entry Filling in a Missing Table Entry

• Suppose you are asked to fill-in the unknowntable entry for 65¢

• It must differ from previous known optimalresult by at most one coin…

• So what are the possibilities? – Change(65¢) = 25¢ + Change(40¢), or

– – Change(65¢) = 20¢ + Change(45¢), orChange(65¢) = 10¢ + Change(55¢), or

– – Change(65¢) = 5¢ + Change(60¢), orChange(65¢) = 1¢ + Change(64¢)

Lookslike arecursive definition

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 7

Table Evaluation Table Evaluation

• When do we fill in the values of the table?• We could do it lazily as needed… as each call to

• Or we could do it from the bottom-up tabulating allChange() progresses from M down to 1

• Thus, instead of just trying to find the minimal numbervalues from 1 up to M

of coins to change M cents, we attempt the solve thesuperficially harder problem of solving for the optimal

change for all values from 1 to M

1¢ = [0,0,0,0,1] 2¢ = [0,0,0,0,2] 3¢ = [0,0,0,0,3] … M¢ = [?,?,?,?,?]

Change via Dynamic Programming Change via Dynamic Programming

• Recall, BruteForceChange( ) was O(M• DPChange( ) is O(Md) d^ )

def DPChange(M, c):change = [[0 for i in xrange(len(c))]]for m in xrange(1,M+1):

bestNumCoins = m+1for i in xrange(len(c)):

if (m >= c[i]):thisChange = [x for x in change[m - c[i]]]thisChange[i] += 1

if (sum(thisChange) < bestNumCoins):change[m:m] = [thisChange]bestNumCoins = sum(thisChange)

return change[M]

While computing best-change solutions for allvalues from 1 to M seems like a lot ofwasted work, wefrequently reuse results

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 9

Dynamic Programming Dynamic Programming

• Dynamic Programming computing recurrence relations efficiently by is a technique for

storing partial results

• Three keys to constructing a dynamicprogramming solution:

1.2. Formulate the answer as a recurrence relationConsider all instances of the recurrence at each step

3. Order evaluations so you will always have theneeded partial results

Yet Another DP Example Yet Another DP Example

Imagine seeking a pathfrom source to sink in a

Manhattan-like city gridthat maximizes the number

of attractions (*) passed.With the following caveat–

at every step you must makeprogress towards the goal.

We treat the city map as agraph, with a “vertices” at

each corner, and weighted edges along each block. Theweights are the number of attractions along each block.^ Sink

Source

Manhattan Tourist Problem (MTP)Manhattan Tourist Problem (MTP)

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 13

MTP Strategy MTP Strategy

• Instead of solving the Manhattan Touristproblem directly, (i.e. the path from (0,0) to

(n,m)) we will solve a more general problem:find the longest path from (0,0) to any arbitrary

• If the longest path from (0,0) to (n,m) passesvertex (i,j).

through some vertex (i,j), then the path from(0,0) to (i,j) must be the longest. Otherwise, you

could increase your path by changing it.

MTP: Greedy Algorithm Is Not Optimal MTP: Greedy Algorithm Is Not Optimal

promising start,but leads tobad choices! 2

source

18 22 sink

Greedy Algorithm: At each step select

the maximumweight block.

Greed has a shorthorizon

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 15

MTP: Simple Recursive Program MTP: Simple Recursive Program

MT(if n = 0 and m = 0n,m)

if n = 0return 0

if m = 0returnMT(0,m-1) + len(edge) from(0,m-1) to (0,m)

x returnÅ MT(n-1,m) + len(edge) fromMT(n-1, 0) + len(edge) from(n- 1,m) to (n,m)(n-1,0) to (n,0)

yreturn Å MT(n,m-1) + len(edge) frommax{x,y} (n,m-1) to (n,m)

We saw this in ourrecursive changealgorithm. It computes thesame pathsmultiple times

What’s wrong with this approach?

i

source

5 S1,0 = 5

S0,1 = 1

• Calculate optimal path score for each vertex in the graph• Each vertex’s score is the maximum of the prior vertices

score plus the weight of the respective edge in between

MTP: Dynamic ProgrammingMTP: Dynamic Programming j

First, fill in the easy ones!

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 19

MTP: Dynamic Programming MTP: Dynamic Programming (cont(cont’’d)d)

This is wherethe greedy alg.failed!

i

source

S3,1 = 9

S2,2 = 12

S1,3 = 8

j

And growing…

MTP: Dynamic Programming MTP: Dynamic Programming (cont(cont’’d)d)

i

source

j

S3,2 = 9

S2,3 = 15

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 21

MTP: Dynamic Programming MTP: Dynamic Programming (cont(cont’’d)d)

i

source

j

01 16 S

(showing all back-traces)

Done!

MTP: Recurrence MTP: Recurrence

Computing the score for a pointrecurrence relation: (i,j) by the

s i, j = max^ s s^ i-1, j i, j-1^ + weight of the edge between (+ weight of the edge between ( i-1, ji, j-1 ) and () and ( i, ji, j ))

The running time is n x m for a n by m grid

( n = # of rows, m = # of columns)

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 25

Traveling in the Grid Traveling in the Grid

•The only hitch is that one must decide on theorder in which visit the vertices

•By the time the vertex s y for all its predecessors x y is analyzed, the values 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

DAG: Directed Acyclic Graph DAG: Directed Acyclic Graph

• Since Manhattan is not a perfect regular grid, werepresent it as a DAG

• DAG for Dressing in the morning problem

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 27

Topological Ordering Topological 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 toa vertex with a larger label

• In other words, if vertices are positioned on aline in an increasing order of labels then all edges

go from left to right.

Topological Ordering Topological Ordering

• 2 different topological orderings of the DAG

9/11/2008 Comp 590-90/Comp 790-90 Fall 2008 31

Traversing the Manhattan Grid Traversing the Manhattan Grid

• 3 different strategies: – a) Column by column

– – b) Row by rowc) Along diagonals

a) b)

c)

Midterm #1 Midterm

• Quiz on Sep. 30 – Chapters 1-

– Open book, open notes, closed computer, nodiscussion

– – Bring your book!Book/notes sharing during the exam is not allowed

– – 1 hrLocation: FB 007