1
votes

Is it possible that a greedy algorithm is also a dynamic programming algorithm?

I took an Analysis of Algorithms class but still, I am not sure with the two concepts.

I understand that the greedy approach uses the current optimal solution to find the global optimal solution and DP algorithm reuse the overlapping sub-results.

I believe the answer is "YES" but I couldn't find a good example which is both greedy and DP algorithm.

Could someone give me an example?

If the answer to the above question is "NO" then could someone explain to me why?

3
Is it possible that a greedy algorithm is also a dynamic programming algorithm?, why not? what keeps me from preparing the sub-result in a greedy manner?mangusta
I couldn't find the example that is both greedy and DP.Jiho Choi
Can anyone give me one algorithm which is both greedy and DP?Jiho Choi

3 Answers

2
votes

From looking at the Bellman equation:

Bellman Equation

If in the minimization we can separate the f part (current period) from the J part (optimal from previous periods) then this corresponds precisely to the greedy approach. An easy example of this is when the optimization function is the sum of the costs at each period,

J(u1,u2,...)= sum(f_i(u_i)).

2
votes

Here's my understanding

Greedy algorithm and dynamic algorithm are two different things. The greedy algorithm always makes the choice that seems to be the best at that moment. It will make choice as soon as the new option pops up regardless what is going to happen next. the dynamic algorithm is combining the solution for the subprogram to get the final solution. It makes the decision based on the results of subprogram and it usually works when there's variable that influences the final solution. So, these are two kinds of way thinking.

The dynamic algorithm always works in the problem that can be solved by greedy algorithm ,but the time cost and space cost of dynamic algorithm are much higher than those of the greedy algorithm. The greedy algorithm mostly can not solve the DP problem.

So the answer is No

2
votes

In optimization algorithms, the greedy approach and the dynamic programming approach are basically opposites. The greedy approach is to choose the locally optimal option, while the whole purpose of dynamic programming is to efficiently evaluate the whole range of options.

BUT that doesn't mean you can't have an algorithm that takes advantage of both strategies. The A* path-finding algorithm, for example, does just that, and is both a greedy algorithm and a dynamic programming algorithm. It uses the greedy approach to optimize the best cases, and the dynamic programming approach to optimize the worst cases.

See: https://en.wikipedia.org/wiki/A*_search_algorithm