I have some general question about algorithms, when You have some problem and You want to write some algorithm, how do You approach the problem, how do You decide which algorithm to use greedy one or dynamic programming? thanks in advance
I have some general question about algorithms, when You have some problem and You
Share
In general I try to transform the new problem into a well known problem that has a well known solution. Then selecting the right algorithm is trivial. This covers most cases in the wild in my experience.
If step one fails I try a greedy approach and try to prove that it doesn’t work. The proving part can be tricky but basically you have to show that the local best choice in some intermediate step isn’t going to yield an overall best result. From there I branch out and usually dynamic is one of the first alternatives I try.
If all else fails, I start looking at good approximation algorithms which are close enough for the problem at hand. Many problems can be solved “well enough” with approximation in a fraction of the time and resources making it the clear winner.