SUNY Geneseo Department of Computer Science
Thursday, February 21
CSci 242, Spring 2013
Prof. Doug Baldwin
Hour exam one is next Thursday (Feb. 28)
Master method problem set, problem 1, worst case?
Past lecture notes?
Dynamic programming is good when a recursive algorithm solves the same subproblem multiple times. But it would not, for example, be good in something like Quicksort or Mergesort where the subproblems don’t overlap.
You want to drive from home to some remote destination. Car has a G gallon gas tank and gets M miles per gallon; trip is long enough that you’ll need to stop for gas, but you want to minimize how much you pay for gas on the trip. There are gas stations along the route, the ith station charges pi dollars per gallon of gas, and is at distance di miles from your home. You leave home with a full tank of gas, and will need to top up at the destination (distance dn with price pn).
tripPlanning( D, P, n )
let r[0..n] be a new array
r[0] = 0
for j = 1 to n
q = ∞
for i = 1 to j
q = min( q, p[i] + r[j-i] )
r[j] = q
return r[n]
n
is the crucial parameter, and indicates which gas station you’re trying to reach; the return value of tripPlanning
will be the minimum cost of a trip to that gas stationr
is indexed by gas station, r[j]
contains the minimum cost of a trip to gas station j
for i
loop is stepping through gas stations you could arrive at station j
frommin
operation in tripPlanning
Hand out dynamic programming problem set
Finish the trip planning problem
Time permitting, look at a more sophisticated dynamic programming application, “matrix chain multiplication”