-4

I see people biased towards DP approach over the greedy approach because it can solve optimization problems. What you guys think which one of them is preferable? I need to collect arguments in favor of preferable technique to argue with my mates. LOL. Ok, DP is used to solve the problems that have optimal substructure and principle of optimality applies on them. But is it enough for DP to be better than greedy approach?

John Tota
  • 61
  • 1
  • 1
  • 3

2 Answers2

4

Your question is meaningless without knowing what problem you are trying to solve.

Dynamic Programming is a tool. It is useful for solving a certain class of problems.

Greedy Algorithms are another tools. They are useful in other situations.

It's like asking "Which is better - a hammer or a saw"?

The answer will be very different depending on what you are trying to do.
sdtom
  • 890
  • 4
  • 8
  • Hmmm.. Means both are useful in their own way depending upon the nature of the problem. Thanks for clarification sdtom. – John Tota Jul 10 '12 at 06:36
2

Let's take the Coin change example. If you take the Greedy approach you might not get to the correct result most of the times but if you take the DP approach you will always get the right result. In fact, it's the only way to solve the problem which is by using DP.

To answer your question, forget about optimality but using Greedy you might not get a correct solution in the first place for certain kind of problems.

user1168577
  • 1,863
  • 11
  • 14
  • Seriously? "Use Greedy if you want to get the wrong answer" is worse than useless advice. Greedy offers you approximate answers, DP is one of many ways to get exact answers. Which one you *need* is entirely up to you, the programmer; lots of problems are fine with inexact answers, since programs rarely perfectly model the real world anyway. – SilverbackNet Jul 10 '12 at 06:34
  • I didn't say that but if you want to interpret it that way, go ahead. And BTW Greedy doesn't offer you approximate answers always. It offers exact answers for certain kind of problems. – user1168577 Jul 10 '12 at 06:53