Dynamic programming vs Greedy approachBefore 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:
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:
Differences between Dynamic programming and Greedy method
Next TopicRegular Expression Matching
