Open In App

Greedy Approach vs Dynamic programming

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • The greedy approach makes the best choice at each step with the hope of finding a global optimum solution.
  • It selects the locally optimal solution at each stage without considering the overall effect on the solution.
  • Greedy algorithms are usually simple, easy to implement, and efficient, but they may not always lead to the best solution.

Dynamic Programming:

  • Dynamic programming breaks down a problem into smaller subproblems and solves each subproblem only once, storing its solution.
  • It uses the results of solved subproblems to build up a solution to the larger problem.
  • Dynamic programming is typically used when the same subproblems are being solved multiple times, leading to inefficient recursive algorithms. By storing the results of subproblems, dynamic programming avoids redundant computations and can be more efficient.

Difference between Greedy Approach and Dynamic Programming

Feature Greedy Approach Dynamic Programming
Optimality May not always provide an optimal solution. Guarantees an optimal solution if the problem exhibits the principle of optimality.
Subproblem Reuse Does not reuse solutions to subproblems. Reuses solutions to overlapping subproblems.
Backtracking Does not involve backtracking. May involve backtracking, especially in top-down implementations.
Complexity Typically simpler and faster to implement. May be more complex and slower to implement.
Application Suitable for problems where local optimization leads to global optimization. Suitable for problems with overlapping subproblems and optimal substructure.
Examples Minimum Spanning Tree, Shortest Path algorithms. Fibonacci sequence, Longest Common Subsequence.

Related Articles:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads