This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Counter Example for Greedy Algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Counter Example for Greedy Algorithm" Watch "Counter Example for Greedy Algorithm" New topic
Author

Counter Example for Greedy Algorithm

William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
There is only enough room for one person in the pool at a time but during this race unlimited people can bike and run at the same time. We want the race to be completed as early as possible. The order is Swim, Bike, Run. You have records that let you calculate approximate times for each person for each event.

My greedy strategy would to have the slowest swimmer go in the pool first, followed by the next fastest, and so on so the fastest swimmer would go last. You also know their run and bike times though, so I am having trouble coming up with a counter example that proves this strategy won't work. Can anyone provide me with one. Your example can have any number of racers involved in it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7696
    
  20

William Koch wrote:I am having trouble coming up with a counter example that proves this strategy won't work.

Me too, and mainly because you haven't described the problem very well. The fact is that you are looking for the best triathlon time with the same person doing all three sports (I assumed this was some sort of "team" problem). You also haven't said what exactly this business of the pool only being able to take "one person at a time" is, and how it affects the rules of the race. My first thought is that it simply makes swimming time irrelevant; but I could well be wrong.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
My conjecture is the problem is to organize the competition in the least total time with the only restriction: the swimming pool (or its timing machine) is restricted to be used by at most 1 swimmer at a time.



William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
correct but you also need to assume you know everyones estimated times for all three events. So how do you organize it so the entire event takes the least amount of time?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7696
    
  20

Ivan Jozsef Balazs wrote:My conjecture is the problem is to organize the competition in the least total time with the only restriction: the swimming pool (or its timing machine) is restricted to be used by at most 1 swimmer at a time.

Ah. Total time. OK, now it makes sense....

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7696
    
  20

William Koch wrote:correct but you also need to assume you know everyones estimated times for all three events. So how do you organize it so the entire event takes the least amount of time?

Well, I think my first cut would be to rank them by the difference in the amount of time it takes them to swim from the time it takes them to complete the cycling and running, ie, rank them by x where x = (c + r) - s, and then have them enter the pool in descending order of x.

That ensures that the overall time will be close to sum(s) (which is a constant) + min(x). The only thing I'm not sure about (because I haven't really thought it through ) is whether the final competitors can be shuffled around if there is a gap between values of x which is greater than one of the swimmer's times. I'm pretty sure they should be - but exactly how you go about it I'm not sure.

HIH

Winston
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Let us presume the first competitor swims in 2 hours and bikes+runs in hour, whereas the second swims in 1 hour, and bikes+runs in 2 hours.

If the slower swimmer, that is, number 1 begins swimming, (s)he finises at time unit 2, goes on (b+r)-ing until 3 hour.
Number 2, having waited for number 1 to finish swimming, starts at hour 2, swims until hour 3, and finishes at hour 5.

However if number 2 starts, (s)he finishes at 3 hour, number 1 waits until 1 hour, and starts then and finishes at 4.

The point is that in this case not the swimming time counts but the combined time for biking and running.
 
Consider Paul's rocket mass heater.
 
subject: Counter Example for Greedy Algorithm