-3

Suppose you have to drive from Islamabad to Lahore. At the start, your gas tank is full. Your gas tank, when full, holds enough gas to travel m miles, and you have a map that gives distances between gas stations along the route. Let d1 < d2 < … < dn be the locations of all the gas stations along the route, where di is the distance from Islamabad to the gas station. The distance between neighboring gas stations is at most m miles. Also, the distance between the last gas station and Lahore is at most m miles.

Your goal is to make as few gas stops as possible along the way. Give a greedy algorithm (in pseudo-code form) to determine at which gas stations you should stop.

Is your solution optimal? What is the time complexity of your solution?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2617096
  • 11
  • 1
  • 3
  • 1
    The question reads like a copy-and-paste job from some homework assignment. Anyway, it's **off-topic** as *"Questions asking for code must demonstrate **a minimal understanding of the problem being solved**. Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)"* – e-sushi Jul 25 '13 at 04:39
  • i am unable to map types of greedy algorithm on it please help!! – user2617096 Jul 25 '13 at 04:41
  • Much easier than TSP. I'd guess it's order N, where N is number of gas stations between the two cities. – Jiminion Jul 25 '13 at 04:42
  • according to the statement what i understand is that di to dn these all stations will come in the route and gas tank will finish after every m miles , according to this we have to stop on every gas station how to greedy algorithm on it :S – user2617096 Jul 25 '13 at 04:44
  • Find all gas stations in range. Pick the one closest to destination. Repeat. It is not optimal O(n). – Jiminion Jul 25 '13 at 04:46
  • but how to write it in pseudocode? – user2617096 Jul 25 '13 at 04:49
  • Are the maps on a 2D or 1D plane? It's not really clear from the question. – Bernhard Barker Jul 25 '13 at 12:03

1 Answers1

3

This algorithm begins at Islamabad, and repeatedly tries to drive as far as possible without running out of gas.

current_distance = 0
current_stop = 0
stops = []
while current != lahore:
  next_stop = 0
  while distance(next_stop) - current_distance <= m:
    next_stop = next_stop + 1
  next_stop = next_stop - 1

  current_stop = next_stop
  current_distance = distance(current_stop)
  add next_stop to stops
return stops

This is an optimal solution. To see this, we note that any sequence of stops that took less stops then the greedy algorithm would have to 'pass' the greedy algorithm at some point along the route.

Using induction, we can see that if the greedy algorithm is the farthest it can be after the first stop, and after the nth stop it is the farthest it could be given stop n - 1, then the greedy algorithm must be the farthest it can be for all stops along the route.

Although this algorithm has complexity O(n) and returns an optimal solution computationally, the route it returns may not be a very 'even' or 'smooth' route. In order to produce routes for actual use by people, more routes that space their stops more evenly would want to be considered.

cptroot
  • 321
  • 2
  • 6