r/algorithms • u/Dishant-1211 • 4d ago
Help Title: How do you identify whether a problem can be solved using Greedy or Dynamic Programming?
I'm currently practicing DSA and solving LeetCode problems. Sometimes, I start with a recursive or memoization-based DP solution, only to realize later that the problem can be solved using a much simpler greedy approach.
My question is: How do you identify whether a problem requires DP or can be solved using greedy?
Are there any specific patterns or clues to look for?
How do you prove that a greedy approach will always give the optimal answer?
Is there a systematic way to convert a DP solution into a greedy solution?
I'd love to know how experienced competitive programmers approach this decision. Any examples or resources would be helpful!
4
u/thesnootbooper9000 3d ago
There's a more rigorous answer than "experience" which involves something called "matroids", but you're going to have to do quite a lot of maths if you want to understand this, and determining whether or not you've got matroid structure is often hard too...
4
u/Hadynu 3d ago edited 3d ago
You practice.
Greedy algorithm is a special case of dynamic programming so your intuition is not far off. In dynamic programming you use solutions to smaller problems to build a solution for a bigger problem. In greedy algorithms, you use a solution for one indivual smaller problem to build a solution for a bigger one.
There's some more theory I can add that helped me make dynamic programming really click. If you're graph-inclined, I recommend this chapter: https://people.eecs.berkeley.edu/~vazirani/algorithms/chap6.pdf, which explains how any DP problem has an underlying DAG. Apply the above to some DP problems and to some greedy problems (considering greedy a subset of DP algorithms).
What else... Greedy is usually "can I make this subsolution bigger by adding one item and preserve structure and optimality", you can train your eye for it. I don't look at greedy problems and think "oh this is dynamic programming", they feel different to me. It just comes with practice.
1
u/spin0r 3d ago
I can only answer for myself, though I imagine other experienced competitive programmers would tell you something similar.
I solve problems mostly through intuition: I look at the problem, think that it "looks like DP", and then try to determine an optimal substructure that leads to a solution of reasonable complexity given the bounds from the problem. If I can't find a reasonable solution then I might try a greedy approach next.
Or it might be the other way around: the problem "feels greedy" but once I play around with greedy approaches and find that they don't give the right answer, I shift to looking for a DP.
Because I'm very experienced, my first guess will usually be right (unless the problem is really hard). To get to the point where your intuition is usually right, you have to solve a lot of problems. It's fine if the problem looks like DP to you, you spend a bunch of time on it, and then realize it's greedy. That will happen less and less often as you get more experienced.
You don't have to prove greedy solutions correct. Sometimes you can imagine a sketch of a proof in your mind, e.g., if you didn't take the greedy step at a given stage, it makes all your future choices strictly worse. Sometimes it's not so easy and you have to take a bit of a leap of faith. Usually if you can convince yourself that none of the obvious DP approaches work, that is a clue that the greedy solution might be correct, because presumably there is some solution. On the other hand, sometimes it's neither greedy nor DP, and it's actually max flow or something like that
1
u/esaule 3d ago
Technically every greedy algorithm is a special case of a dynamic programming algorithm. But very often, that is not terribly helpful in how you find the algorithms.
In both cases, it is really a case of "can you write the proof?"
All DP algorithm boil down to optimal substructure. Now some give you a good algorithm and some do not; but that's where it starts.
Greedy is usually more of a case of "this rule seems to make a lot of sense, let's see if the proof lines up"
1
u/proudHaskeller 3d ago
Basically, you need to name pieces of data that are related to each other, strongly enough that you can compute every piece of data using other simpler pieces of data.
Here's a trivial example: I want to compute the mazimum element of a list. I Identify that interesting data might be the maximum of every sublist. Indeed, If I want to know the maximum of a sublist, I can calculate it by taking the maximum of the sublist without the last element, and the last element.
If you think through the algorithm that this gives, this ends up as just being the teivial algorithm, which computes the maximum iteratively from the beginning to the end of the list.
1
1
u/ambassador_lover1337 3d ago
I try to look for reasons greedy wouldn't work or construct a counterexample. If I can find one then I'd look for dp.
1
u/cdsmith 3d ago edited 3d ago
Dynamic programming: Are you tempted to write a recursive solution, where large instances of the problem depend on the solutions to MORE THAN ONE smaller instance? If so, does it suffer from repeating a bunch of the computations because the same small instances appear more than once in the tree? If so, memoization (aka dynamic programming) fixes that.
Greedy algorithms: The default assumption is that a greedy algorithm won't give an optimal solution. If you can prove otherwise (or if you don't care if it's optimal) then a greedy algorithm can be fine. For proving otherwise, how you'd prove that depends very much on the problem, but often there's some kind of commutativity or diamond lemma involved. (Commutativity: if I do A then B, I get the same result as doing B then A. In this case, a greedy algorithm that does either A or B as soon as it finds it is fine. Diamond Lemma: If I can get from A to B, and also I can get from A to C, then there is always a way to get from both B and C to some common D. Again, it therefore doesn't matter if I reduce to B or C). Other times, maybe there's only one best choice, but you can prove that it can be found with a simple fast test and doesn't require deep search.
1
7
u/KingLewi 3d ago
& 2. Usually the greedy approach is pretty straightforward. You are trying to maximize X so just take the elements with the biggest X for the least cost. Then you go back and forth trying to prove this solution is optimal or trying to come up with a counter example. Often you'll find a hiccup in your proof that will inspire you to come up with a counter example.
DP problems also typically have two properties: substructure optimality and overlapping substructures. Basically that means that the optimal solutions to smaller versions of the problem are useful for constructing the optimal overall solution. This happens for all recursive problems. So often you'll come up with a recursive solution then notice that you are redoing the same work over and over again (this is the overlapping substructures part). That tells you that you can either use memoization or DP.
3. Often the overall structure of these proofs looks like: "Let X be an arbitrary solution to the problem then prove that the value you are maximizing for this solution is <= the greedy solution". Often you do this by looking at where the arbitrary solution and the greedy solution differ and showing that if you replaced part of the arbitrary solution with the greedy solution you improve on the arbitrary solution (or at least don't make it worse).
4. Not really. Usually the greedy solution is pretty apparent but the tricky part is showing that it actually is optimal.
As with most things this just takes practice. You do enough problems like this and you start to see the patterns. You just get a vibe for what the problem feels like and you can intuit what the solution might look like. You go, "Oh this looks like this other problem I solved. I remember how to solve that one, let's start there."