SUNY Geneseo Department of Computer Science


Introduction to Dynamic Programming

Thursday, February 21

CSci 242, Spring 2013
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Misc

Hour exam one is next Thursday (Feb. 28)

Questions?

Master method problem set, problem 1, worst case?

Past lecture notes?

Dynamic Programming and Rod Cutting

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.

Size n rod involves size n-1 and n-2 subproblems, size n-1 subproblem also involves size n-2 sub-subproblem

Dynamic Programming Trip Planning Example

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).

Gas stations along trip

        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]

Hand out dynamic programming problem set

Next

Finish the trip planning problem

Time permitting, look at a more sophisticated dynamic programming application, “matrix chain multiplication”


Next Lecture