Question

Gas stations optimization. There are n gas station S1, …, Sn along I-80 from San Francisco...

Gas stations optimization. There are n gas station S1, …, Sn along I-80 from San Francisco to New York. On a full tank of gas your car goes for D miles. Gas station S1 is in San Francisco, each gas station Si, for 2 ≤ i ≤ n, is di ≤ D miles after the previous gas station Si-1, and gas Sn is in New York. What is the minimum number of gas stops you must make when driving from San Francisco to New York? Give the pseudo-code for a greedy algorithm that solves the above optimization problem. Justify briefly why your algorithm finds the optimum solution. What is the asymptotic running time of your algorithm in term of n?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There are n gas stations S1,S2,... , Sn.

Total distance to be travelled is 80.

On full tank, the car goes for D miles.

For the greedy algorithm, we go as far as we can before refuelling the vehicle.

The pseudo-code for the given algorithm is as follows:

countOfStops = 0

x = 0 //current distance

while(x!=Sn)

let p be the largest integer where Sp <= x+D

x=Sp

countOfStops++;  

return countOfStops;

Since at any steps we go as far as we can before refuelling, it gives us the optimal solution.

Here we scan the entire S array once. So the complexity is O(n).

Add a comment
Know the answer?
Add Answer to:
Gas stations optimization. There are n gas station S1, …, Sn along I-80 from San Francisco...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT