Javatpoint Logo
Javatpoint Logo

Dynamic programming vs Greedy approach

Before understanding the differences between the dynamic programming and greedy approach, we should know about the dynamic programming and greedy approach separately.

What is Greedy method?

A Greedy method is one of the techniques used to solve problems. This method is used for solving optimization problems. The optimization problems are the problems that require either maximum or minimum results.

Let's understand through an example.

Suppose we want to travel from location A to location B. We can travel from A to B through various ways like walk, bike, car, bus, train, flight, etc. So, we have various solutions to achieve the goal. But there is one constraint in this problem that we have to reach B within 12 hr. We can achieve this goal either by using flight or train. So, we have two solutions in this case, i.e., flight or train. These types of solutions are feasible solutions. Feasible solutions are those solutions that satisfy the constraint given in the problem.

Suppose we want to cover this distance at the minimum cost, then it is known as a minimization problem. The minimization problem is a problem that demands a minimum cost. In the above problem, if we go by train then it would incur a minimum cost to provide an optimal solution. The optimal solution is the solution that provides the feasible solution and incurs the minimum cost. It means that there can be only one optimal solution, i.e., multiple solutions are not possible.

This problem requires a minimum result but some problems require a maximum result. The problems that require either maximum or minimum results are known as optimization problems.

A Greedy method says that a problem should be solved in stages. In each stage, we consider the input from the given problem and if the input is feasible then we include it. In this way, we include all the feasible inputs to find an optimal solution.

Algorithm of Greedy method

In the above greedy algorithm, we have passed two parameters, i.e., a and n where 'a' is the array and n is the size of the array. In the for loop, we select input values from 'a' and then we check whether it is feasible or not. It the selected input is feasible then add that input to the solution.

What is dynamic programming?

Dynamic programming is one of the techniques used to solve the optimization problems. The following is the procedures that dynamic programming follows to achieve the optimal solution:

  • It breaks down the complex problem into simpler sub-problems.
  • We will find the optimal solution to these sub-problems.
  • Storing the results of these sub-problems.
  • We will reuse the solution of these sub-problems so that we do not need to re-calculate them.
  • Finally, we will calculate the result of the complex problem.

The above are the five steps used in dynamic programming to solve the optimization problem. The dynamic programming is applicable to those problems that have certain properties given as below:

  • Overlapping subproblems: The overlapping problems are those that have common solutions.
  • Optimal substructure: The optimal substructure is a substructure that can be obtained by the combination of all the optimal solution of subproblems.

Differences between Dynamic programming and Greedy method

Dynamic programming vs Greedy approach
Dynamic programming Greedy method
Definition It is used to obtain the optimum solution. It is also used to obtain the optimum solution.
Feasibility There is no special set of feasible solutions in this method. In a greedy method, the optimum solution is obtained from the feasible set of solutions.
Recursion Dynamic programming considers all the possible sequences in order to obtain the optimum solution. In the greedy method, the optimum solution is obtained without revising the previously generated solutions.
Principle of optimality It guarantees that it will generate the optimum solution using the principle of optimality. It does not guarantee that it will generate the optimum solution.
Memoization It creates the lookup table to store the results of the subproblems that occupy the memory space. It is more efficient in terms of memory as it does not create any table to store the previous states.
Time Complexity Dynamic programming is slower than the greedy method, like Bellman-Ford algorithm takes O(VE) time. Greedy methods are faster than dynamic programming like Dijkstra's shortest path algorithm takes (ElogV + VlogV) time.
Method The dynamic programming uses the bottom-up or top-down approach by breaking down a complex problem into simpler problems. The greedy method always computes the solution in a sequence manner, and it does not look at the previous states.
Example 0/1 knapsack problem Fractional knapsack

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA