 GeeksforGeeks App
Open App Browser
Continue

# 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:

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:

My Personal Notes arrow_drop_up