kdn251 / interviews

Everything you need to know to get the job.
https://www.youtube.com/channel/UCKvwPt6BifPP54yzH99ff1g?view_as=subscriber
MIT License
62.76k stars 12.83k forks source link

Misleading example for the Greedy Algorithms section #200

Open efeberkeevci opened 2 years ago

efeberkeevci commented 2 years ago

In the Greedy Algorithms section, coin change problem is provided as an example. This creates an impression that every coin change problem can be solved with greedy approach. This is incorrect since using greedy approach in some coin sets gives the incorrect result and needs to be solved with dynamic programming.

More info: https://stackoverflow.com/questions/13557979/why-does-the-greedy-coin-change-algorithm-not-work-for-some-coin-sets

Ziniddoug commented 1 year ago

A deceptive example for the Greedy Algorithms section is the Traveling Salesman Problem.

In this example, let's consider a set of cities that need to be visited by a salesperson. The goal is to find the shortest possible route that passes through all cities and returns to the starting point.

A naive greedy algorithm to solve this problem would always select the closest available city at each step. In other words, at each stage, the salesperson would choose the nearest city from the current city and add it to the route.

However, the Traveling Salesman Problem is an NP-complete problem, which means that there is no known efficient solution to solve it in polynomial time. The naive greedy algorithm does not take into account the global optimality of the solution and can therefore provide suboptimal solutions.

While the greedy algorithm may provide reasonable solutions for smaller instances of the problem, it does not guarantee the optimal solution in all cases. Therefore, this deceptive example of the Traveling Salesman Problem highlights the need for more sophisticated approaches, such as dynamic programming or exhaustive search algorithms, to efficiently solve NP-complete problems.