SUNY Geneseo Department of Computer Science


Trip Planning via Dynamic Programming

Tuesday, February 26

CSci 242, Spring 2013
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Misc

Hour exam one is Thursday (Feb. 28)

I’m out of town next Thursday (Mar. 7)

Questions?

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

        tripPlanning( D, P, n )              // p[i] = price per gallon at station i
            let r[0..n] be a new array       // r[j] = min cost to station j
            let s[0..n] be a new array       // s[j] = stop previous to j
            r[0] = 0
            s[0] = 0
            for j = 1 to n                   // j is station arriving at
                q = ∞
                i = j - 1
                while d[j] - d[i] <= GM && i >= 0        //i is station coming from
                    if p[j]*(d[j]-d[i])/M + r[i] < q
                        q = p[j]*(d[j]-d[i])/M + r[i]
                        s[j] = i
                    i = i - 1
                r[j] = q
            return r[n] and s

Some key steps in turning what we had last class into this

Next

(After exam)

More dynamic programming—matrix chain multiplication

Section 15.2


Next Lecture