The Wayback Machine - https://web.archive.org/web/20250114163256/https://www.geeksforgeeks.org/greedy-approach-vs-dynamic-programming/
Open In App

Greedy Approach vs Dynamic programming

Last Updated : 23 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. With comprehensive lessons and practical exercises, this course will set you up for success in technical interviews and beyond.

And here's the challenge – join the Three 90 Challenge! Complete 90% of the course in 90 days, and you’ll earn a 90% refund. It's a great way to stay motivated, track your progress, and reward yourself for your dedication. Start the challenge today, and push your limits to achieve mastery in DSA!


Next Article

Similar Reads

three90RightbarBannerImg