How ‘>’ instead of ‘>=’ can destroy everything

Lately, I’ve been working on a simple maximization algorithm which trivially looks simple. A simple method is supposed to choose a _server_ to be used to run some job on the cloud. Very simple, each server gets a score and the highest score is chosen:

for (Server s : servers) {
  double fitness = computeFitness(s);
  if (fitness > bestFitness) {
    // update the best server
  }
}

Happily, I committed the code and the product shipped to the live environment. BAM! The first observation was that only one server is being selected! There are abundant number of servers that can be chosen but why only that one is always selected?

The explanation was simple. In any how, consider that the first server in the collection is the one that gets the highest score in certain circumstances although there can be servers afterwards with at least the same score. Since “>” is used so the best server never gets updated although there can be the same servers afterwards. This algorithm should guarantee that in the worst case it works as Round-Robin could work. That’s how “>=” comes into the picture that when having ties in server scores, the last highest will be selected to guarantee uniform selection over servers. Quite an experience about something that was trivially studied durig bachelors in all those algorithm courses!

Leave a Reply

Your email address will not be published. Required fields are marked *