Open in App
Not now

# Greedy approach vs Dynamic programming

• Difficulty Level : Medium
• Last Updated : 13 Mar, 2023

Greedy approach and dynamic programming are two different algorithmic approaches that can be used to solve optimization problems. Here are the main differences between these two approaches:

### Greedy approach:

1. The greedy approach makes locally optimal choices at each step with the hope of finding a global optimum.
2. The greedy approach does not necessarily consider the future consequences of the current choice.
3. The greedy approach is useful for solving problems where making locally optimal choices at each step leads to a global optimum.
4. The greedy approach is generally faster and simpler than dynamic programming.

### Dynamic programming:

1. Dynamic programming is a bottom-up algorithmic approach that builds up the solution to a problem by solving its subproblems recursively.
2. Dynamic programming stores the solutions to subproblems and reuses them when necessary to avoid solving the same subproblems multiple times.
3. Dynamic programming is useful for solving problems where the optimal solution can be obtained by combining optimal solutions to subproblems.
4. Dynamic programming is generally slower and more complex than the greedy approach, but it guarantees the optimal solution.
5. In summary, the main difference between the greedy approach and dynamic programming is that the greedy approach makes locally optimal choices at each step without considering the future consequences, while dynamic programming solves subproblems recursively and reuses their solutions to avoid repeated calculations. The greedy approach is generally faster and simpler, but may not always provide the optimal solution, while dynamic programming guarantees the optimal solution but is slower and more complex.

## Greedy approach:

A Greedy algorithm is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. So the problems where choosing locally optimal also leads to a global solution is the best fit for Greedy.

Example: In Fractional Knapsack Problem the local optimal strategy is to choose the item that has maximum value vs weight ratio. This strategy also leads to global optimal solution because we allowed taking fractions of an item.

### Characteristics of Greedy approach:

A problem that can be solved using the Greedy approach follows the below-mentioned properties:

• Optimal substructure property.
• Minimization or Maximization of quantity is required.
• Ordered data is available such as data on increasing profit, decreasing cost, etc.
• Non-overlapping subproblems.

Standard problems on Greedy Approach:

## Dynamic Programming:

Dynamic programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of subproblems so that we do not have to re-compute them when needed later. This simple optimization reduces time complexities from exponential to polynomial.

Example: If we write a simple recursive solution for Fibonacci Numbers, we get exponential time complexity and to optimize it by storing solutions of subproblems, time complexity reduces to linear this can be achieved by Tabulation or Memoization method of Dynamic programming.

### Characteristics of Dynamic Programming:

A problem that can be solved using Dynamic Programming must follow the below mentioned properties:

• Optimal substructure property.
• Overlapping subproblems.

Standard problems on Dynamic Programming:

### Below are some major differences between Greedy method and Dynamic programming:

Related Articles:

My Personal Notes arrow_drop_up
Related Articles