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:


Previous Article
Next Article

Similar Reads

Comparison among Greedy, Divide and Conquer and Dynamic Programming algorithm
Greedy algorithm, divide and conquer algorithm, and dynamic programming algorithm are three common algorithmic paradigms used to solve problems. Here's a comparison among these algorithms: Approach:Greedy algorithm: Makes locally optimal choices at each step with the hope of finding a global optimum.Divide and conquer algorithm: Breaks down a probl
4 min read
Coin game of two corners (Greedy Approach)
Consider a two-player coin game where each player gets turned one by one. There is a row of even a number of coins, and a player on his/her turn can pick a coin from any of the two corners of the row. The player that collects coins with more value wins the game. Develop a strategy for the player making the first turn, such that he/she never loses t
9 min read
Maximum profit by buying and selling a share at most K times | Greedy Approach
In share trading, a buyer buys shares and sells on a future date. Given the stock price of N days, the trader is allowed to make at most K transactions, where a new transaction can only start after the previous transaction is complete. The task is to find out the maximum profit that a share trader could have made. Examples: Input: prices[] = {10, 2
11 min read
Travelling Salesman Problem | Greedy Approach
Given a 2D matrix tsp[][], where each row has the array of distances from that indexed city to all the other cities and -1 denotes that there doesn't exist a path between those two indexed cities. The task is to print minimum cost in TSP cycle.Examples: Input: tsp[][] = {{-1, 10, 15, 20}, {10, -1, 35, 25}, {15, 35, -1, 30}, {20, 25, 30, -1}}; Below
10 min read
Longest subsequence with a given OR value : Dynamic Programming Approach
Given an array arr[], the task is to find the longest subsequence with a given OR value M. If there is no such sub-sequence then print 0.Examples: Input: arr[] = {3, 7, 2, 3}, M = 3 Output: 3 {3, 2, 3} is the required subsequence 3 | 2 | 3 = 3Input: arr[] = {2, 2}, M = 3 Output: 0 Approach: A simple solution is to generate all the possible sub-sequ
6 min read
Difference Between Pessimistic Approach and Optimistic Approach in DBMS
1. Pessimistic Approach : A Pessimistic approach is an approach of concurrency control algorithms in which the transaction is delayed if there is a conflict with each other at some point of time in the future. It locks the database’s record for update access and other users can only access record as read-only or have to wait for a record to be ‘unl
4 min read
Difference between Interlingua Approach and Transfer Approach
Interlingua Approach: It is a machine translation approach used by systems in Natural Language Processing to translate one language to another and vice versa. This approach can also be used to translate one language to many languages. The credit of this approach went to Descartes and Leibniz who were among the first people to give the idea of Inter
2 min read
Dynamic Programming in Game Theory for Competitive Programming
In the fast-paced world of competitive programming, mastering dynamic programming in game theory is the key to solving complex strategic challenges. This article explores how dynamic programming in game theory can enhance your problem-solving skills and strategic insights, giving you a competitive edge. Whether you're a seasoned coder or a newcomer
17 min read
Extendible Hashing (Dynamic approach to DBMS)
Extendible Hashing is a dynamic hashing method wherein directories, and buckets are used to hash data. It is an aggressively flexible method in which the hash function also experiences dynamic changes. Main features of Extendible Hashing: The main features in this hashing technique are: Directories: The directories store addresses of the buckets in
7 min read
FIFO (First-In-First-Out) approach in Programming
FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last. Real-life example: In this example, following things are to be considered: There is a ticket counter where people come, take tickets and go.People enter a line (queue) to g
5 min read