Greedy approach vs Dynamic programming - GeeksforGeeks (2024)

Improve

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:

S.No.ArticlePractice
1.Fractional Knapsack Problemlink
2.Activity Selection Problemlink
3.

Water Connection Problem

link
4.

Dijkstra’s Shortest Path Algorithm

link

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:

S.No.ArticlePractice
1.Coin Changelink
2.Edit Distancelink
3.Longest Common Subsequencelink
4.Count ways to reach the n’th stairlink

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

Feature

Greedy methodDynamic programming

Feasibility

In a greedy Algorithm, we make whatever choice seems best at the moment in the hope that it will lead to global optimal solution.In Dynamic Programming we make decision at each step considering current problem and solution to previously solved sub problem to calculate optimal solution .

Optimality

In Greedy Method, sometimes there is no such guarantee of getting Optimal Solution.It is guaranteed that Dynamic Programming will generate an optimal solution as it generally considers all possible cases and then choose the best.

Recursion

A greedy method follows the problem solving heuristic of making the locally optimal choice at each stage.A Dynamic programming is an algorithmic technique which is usually based on a recurrent formula that uses some previously calculated states.

Memoization

It is more efficient in terms of memory as it never look back or revise previous choicesIt requires Dynamic Programming table for Memoization and it increases it’s memory complexity.

Time complexity

Greedy methods are generally faster. For example, Dijkstra’s shortest path algorithm takes O(ELogV + VLogV) time.Dynamic Programming is generally slower. For example, Bellman Ford algorithm takes O(VE) time.

Fashion

The greedy method computes its solution by making its choices in a serial forward fashion, never looking back or revising previous choices.Dynamic programming computes its solution bottom up or top down by synthesizing them from smaller optimal sub solutions.

Example

Fractional knapsack .
0/1 knapsack problem

Related Articles:

  • Greedy Algorithm
  • Dynamic programming
  • Optimal substructure
  • Overlapping subproblem


Last Updated : 13 Mar, 2023

Like Article

Save Article

Previous

Difference between Greedy Algorithm and Divide and Conquer Algorithm

Next

Comparison among Greedy, Divide and Conquer and Dynamic Programming algorithm

Share your thoughts in the comments

Please Login to comment...

Greedy approach vs Dynamic programming - GeeksforGeeks (2024)

FAQs

Is greedy algorithm better than dynamic programming? ›

In the application of solving the backpack problem, greedy algorithm is faster, but the resulting solution is not always optimal; dynamic programming results in an optimal solution, but the solving speed is slower.

Can every greedy problem be solved using dynamic programming? ›

1 Answer. The explanation is: A greedy algorithm gives optimal solution for all subproblems, but when these locally optimal solutions are combined it may NOT result into a globally optimal solution. Hence, a greedy algorithm CANNOT be used to solve all the dynamic programming problems.

Which strategy provides optimal solution greedy or dynamic programming? ›

Greedy algorithm and divide and conquer algorithm are generally faster and simpler, but may not always provide the optimal solution, while dynamic programming algorithm guarantees the optimal solution but is slower and more complex.

Is greedy algorithm an efficient way to solve the problem? ›

Greedy algorithms are quite successful in some problems, such as Huffman encoding which is used to compress data, or Dijkstra's algorithm, which is used to find the shortest path through a graph. However, in many problems, a greedy strategy does not produce an optimal solution.

Why dynamic approach is preferred over greedy technique? ›

It is guaranteed that Dynamic Programming will generate an optimal solution as it generally considers all possible cases and then choose the best. A greedy method follows the problem solving heuristic of making the locally optimal choice at each stage.

What is the main disadvantage of using a greedy algorithm? ›

The main disadvantage of using a greedy algorithm is that it may not find the optimal solution to a problem. In other words, it may not produce the best possible outcome.

What problems Cannot be solved by dynamic programming? ›

However, not all problems that use recursion can be solved by dynamic programming. Unless solutions to the subproblems overlap, a recursion solution can only be arrived at using a divide-and-conquer method. For example, problems like merge, sort, and quick sort are not considered dynamic programming problems.

What are the real world problems solved by dynamic programming? ›

The following computer problems can be solved using dynamic programming approach −Fibonacci number series, Knapsack problem, Tower of Hanoi, All pair shortest path by Floyd-Warshall, Shortest path by Dijkstra and Project scheduling. Dynamic programming can be used in both top-down and bottom-up manner.

Which of the following problem cannot be solved using greedy approach? ›

0-1 knapsack problem cannot be solved by the greedy method because it is enabled to fill the knapsack to full capacity so here greedy algorithm is not optimal. 0-1 knapsack problem is solved by Dynamic programming approach.

Does dynamic programming always give optimal solution? ›

The main benefit of using dynamic programming is that we move to polynomial time complexity, instead of the exponential time complexity in the backtracking version. Also, dynamic programming, if implemented correctly, guarantees that we get an optimal solution.

What are the similarities between greedy and dynamic programming? ›

Similarities Between Greedy and Dynamic Programming

Both are used to solve optimization problems. They can both operate on substructures of a problem. Highly efficient for their suitable problem types. Utilized in scenarios where an optimal solution is sought.

What are the advantages and limitations of using dynamic programming? ›

In general, dynamic programming is more efficient for problems that have many overlapping subproblems, as it avoids redundant computations and saves time. However, it also requires more memory and space, as it stores all the results of the subproblems, even if some of them are not needed.

What is the problem with greedy algorithms? ›

Hard Problems on Greedy Algorithm:

Minimum cost to process m tasks where switching costs. Minimum time to finish all jobs with given constraints. Minimize the maximum difference between the heights of towers. Minimum edges to reverse to make path from a source to a destination.

Why is greedy algorithm efficient? ›

Space Efficiency: In addition to being time-efficient, greedy algorithms often require less memory space, making them suitable for environments with limited resources. Intuitive Solutions: The locally optimal choices made by greedy algorithms often result in solutions that are intuitively appealing and easy to grasp.

What is an example of a greedy algorithm in real life? ›

Some examples include: The Knapsack Problem: Imagine packing a knapsack for a hike, limited in weight but eager to bring the most valuable loot. A greedy algorithm might grab the heaviest, most valuable items first, only to discover at the trailhead that it can't fit the tent — a classic greedy trap!

Is dynamic programming a greedy algorithm? ›

Greedy algorithm have a local choice of the sub-problems whereas Dynamic programming would solve the all sub-problems and then select one that would lead to an optimal solution. Greedy algorithm take decision in one time whereas Dynamic programming take decision at every stage.

What are the advantages of greedy algorithm? ›

Advantages of Greedy Algorithm

They follow a step-by-step approach where the best possible decision is made at each stage based on the current information. This simplicity makes them easier to design and analyze compared to more complex algorithms.

Which type of algorithm is more efficient? ›

Yes, a linear algorithm is generally more efficient than an exponential algorithm. In the context of algorithms, efficiency is often measured by comparing the growth rates of their time complexity. A linear algorithm has a time complexity of O(n), where 'n' represents the size of the input data.

Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 5837

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.