-3

Assume you have written a hill climbing algorithm which is very slow. Your smallest data unit is a Node. You have another class called NodeList which contains a list of Nodes plus some other data. You have a list of NodeLists, and their number or order doesn’t change. Your algorithm is trying to decide which Node should go in which NodeList, so that we end up with the best score. After profiling the algorithm, you find out the score calculation is consuming 95% of the CPU time. Can you think of general ways to speed up the algorithm?

I have tried to google it and learn about the basic concept of hill climbing algorithm. But still can't figure out what should I do to improve the algorithm. Any help will be appreciated. Thanks.

  • 2
    The answer is yes, maybe, depends on the code. You haven't shown any, and this is not really the best place to ask for a code review. In short id say there are probably many ways you can increase your performance – TheGeneral Aug 02 '19 at 03:14
  • It's not nice to copy and paste interview questions in stack overflow. Your potential employer might have access to stackoverflow too. – Pouya Abadi Sep 09 '19 at 00:15
  • Thanks Sonny....we're going to have to write a new interview question now that this one has a publicly available answer. – Tim Cooper Sep 09 '19 at 00:16

1 Answers1

1

There is no all-purpose improvement because if you already have the best possible code it cannot be improved further.

If the paths along which your code would be climbing are very long (probably rare) then you can save time by trying a number of different starts and then only climbing from the best of them - these will tend to be further along their paths.

If you can find a faster but perhaps less accurate version of your score you could check out a lot of neighbors with the less accurate score and try the best of them with the full score. This should make it quicker to find improvements, which should help if you accept the first improvement you come across.

You talk about a score calculation rather than a score update. Hill-climbing tends to make small changes from the existing answer. If you are calculating the score from scratch for each possibility, can you find a way to save time by updating a score you have already calculated for the existing answer, based on the fact that the neighbor of that answer that you are checking out is very similar to it?

You could look again to see if there are other ways of solving the problem than hill-climbing.

mcdowella
  • 19,301
  • 2
  • 19
  • 25