Open In App
Related Articles

Comparison among Greedy, Divide and Conquer and Dynamic Programming algorithm

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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:

  1. Greedy algorithm: Makes locally optimal choices at each step with the hope of finding a global optimum.
  2. Divide and conquer algorithm: Breaks down a problem into smaller subproblems, solves each subproblem recursively, and then combines the solutions to the
  3. subproblems to solve the original problem.
  4. Dynamic programming algorithm: Solves subproblems recursively and stores their solutions to avoid repeated calculations.


Goal:

  1. Greedy algorithm: Finds the best solution among a set of possible solutions.
  2. Divide and conquer algorithm: Solves a problem by dividing it into smaller subproblems, solving each subproblem independently, and then combining the
  3. solutions to the subproblems to solve the original problem.
  4. Dynamic programming algorithm: Solves a problem by breaking it down into smaller subproblems and solving each subproblem recursively.


Time complexity:

  1. Greedy algorithm: O(nlogn) or O(n) depending on the problem.
  2. Divide and conquer algorithm: O(nlogn) or O(n^2) depending on the problem.
  3. Dynamic programming algorithm: O(n^2) or O(n^3) depending on the problem

.
Space complexity:

  1. Greedy algorithm: O(1) or O(n) depending on the problem.
  2. Divide and conquer algorithm: O(nlogn) or O(n^2) depending on the problem.
  3. Dynamic programming algorithm: O(n^2) or O(n^3) depending on the problem.


Optimal solution:

  1. Greedy algorithm: May or may not provide the optimal solution.
  2. Divide and conquer algorithm: May or may not provide the optimal solution.
  3. Dynamic programming algorithm: Guarantees the optimal solution.


Examples:

  1. Greedy algorithm: Huffman coding, Kruskal’s algorithm, Dijkstra’s algorithm, etc.
  2. Divide and conquer algorithm: Merge sort, Quick sort, binary search, etc.
  3. Dynamic programming algorithm: Fibonacci series, Longest common subsequence, Knapsack problem, etc.
  4. In summary, the main differences among these algorithms are their approach, goal, time and space complexity, and their ability to provide the optimal solution. 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.

 

Greedy Algorithm:

Greedy algorithm is defined as a method for solving optimization problems by taking decisions that result in the most evident and immediate benefit irrespective of the final outcome. It is a simple, intuitive algorithm that is used in optimization problems.

Divide and conquer Algorithm:

Divide and conquer is an algorithmic paradigm in which the problem is solved using the Divide, Conquer, and Combine strategy. A typical Divide and Conquer algorithm solve a problem using the following three steps:

Divide: This involves dividing the problem into smaller sub-problems.
Conquer: Solve sub-problems by calling recursively until solved.
Combine: Combine the sub-problems to get the final solution of the whole problem.

Dynamic Programming:

Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has sometimes repeated calls for the same input states, 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.

Greedy Algorithm vs Divide and Conquer Algorithm vs Dynamic Algorithm:

Sl.NoGreedy AlgorithmDivide and conquerDynamic Programming 
1Follows Top-down approachFollows Top-down approachFollows bottom-up approach
2Used to solve optimization problemUsed to solve decision problemUsed to solve optimization problem
3The optimal solution is generated without revisiting previously generated solutions; thus, it avoids the re-computationSolution of subproblem is computed recursively more than once.The solution of subproblems is computed once and stored in a table for later use.
4It may or may not generate an optimal solution. It is used to obtain a solution to the given problem, it does not aim for the optimal solutionIt always generates optimal solution.
5Iterative in nature.Recursive in nature.Recursive in nature.
6 efficient and fast than divide and conquer. For instance, single source shortest path finding using Dijkstra’s Algo takes O(ElogV) timeless efficient and slower.more efficient but slower than greedy. For instance, single source shortest path finding using Bellman Ford Algo takes O(VE) time.
7extra memory is not required.some memory is required.more memory is required to store subproblems for later use.
8Examples: Fractional Knapsack problem
Activity selection problem
Job sequencing problem.
Examples: Merge sort
Quick sort
Strassen’s matrix multiplication.
Examples: 0/1 Knapsack
All pair shortest path
Matrix-chain multiplication.

Last Updated : 17 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials