hw2_sol

# hw2_sol - we argue that S’ is legal(i.e you never run out...

This preview shows pages 1–4. Sign up to view the full content.

CS570 Homework #2 solution 1. The greedy algorithm we use is to go as far as possible before stopping for gas. Let ci be the city with distance di from USC. Here is the pseudo-code. S = 0 ; last = 0 for i = 1 to n if (di last) > P S = S U {c i 1 } last = t i 1 Clearly the above is an O(n) algorithm. We now prove it is correct. Greedy Choice Property: Let S be an optimal solution. Suppose that its sequence of stops is s1, s2, . . . , sk where si is the stop corresponding to distance ti. Suppose that g is the first stop made by the above greedy algorithm. We now show that there is an optimal solution with a first stop at g. If s1 = g then S is such a solution. Now suppose that s1 g. Since the greedy algorithm stops at the latest possible city then it follows that s1 is before g. We now argue that S’=<g, s2, s3, . . . , sk > is an optimal solution. First note that |S’| = |S|. Second,

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: we argue that S’ is legal (i.e. you never run out of gas). By definition of the greedy choice you can reach g. Finally, since S is optimal and the distance between g and s2 is no more than the distance between s1 and s2, there is enough gas to get from g to s2. The rest of S’ is like S and thus legal. Optimal Substructure Property: Let P be the original problem with an optimal solution S. Then after stopping at the station g at distance di the subproblem P’ that remains is given by di+1, . . . , dn (i.e. you start at the current city instead of USC). Let S’ be an optimal solution to P’. Since, cost(S) = cost(S’) + 1, clearly an optimal solution to P includes within it an optimal solution to P’....
View Full Document

## This note was uploaded on 02/15/2011 for the course CS 570 taught by Professor Shahriarshamsian during the Spring '08 term at USC.

### Page1 / 4

hw2_sol - we argue that S’ is legal(i.e you never run out...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online