Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

# Difference Between Greedy and Dynamic Programming

## Introduction

Greedy programming and dynamic programming are two popular techniques used in computer science to solve optimization problems. Both methods are used to find the optimal solution for a given situation. However, there are some critical differences between these two techniques.

In this article, we will discuss about the difference between greedy and dynamic programming, their examples, and when to use them.

## What is Greedy Programming?

Greedy programming is a technique that follows the "greedy" approach, which means that it makes the locally optimal choice at each step with the hope of finding a global optimum. In other words, it chooses the best option at each stage without considering the future consequences. Greedy algorithms are often used to solve optimization problems with a set of choices, each with a value and a weight. The goal is to find the maximum value obtained by selecting a subset of options with a total weight less than or equal to a given limit.

## When to use Greedy Programming?

We can apply Greedy programming when:

1. The problem has a set of choices.

2. The goal is to find an optimal solution.

3. The locally optimal choices lead to a globally optimal solution.

4. The problem has subproblems that can be solved independently of each other.

## Characteristics of Greedy Programming:

The main characteristics of greedy programming are as follows:

### Greedy Choice Property

The greedy algorithm selects the locally optimal solution at each step, which is the option that yields the best result for the particular step at hand.

### Optimal Substructure Property

The overall problem can be solved optimally by integrating the best answers to each subproblem.

### Efficient

Greedy algorithms frequently require little implementation effort and perform well. They are appropriate for various optimisation issues because they often have a lower time complexity than more complicated methods.

### Elimination of Backtracking

Greedy algorithms avoid going back and reconsidering choices made in earlier steps. A choice is never changed once it has been made.

### Applicability

Greedy algorithms help resolve optimisation issues when several decisions must be taken to maximise or minimise a particular objective function.

## Examples of Greedy Programming

Greedy algorithms are often used in optimization problems where a solution must be found quickly. Examples of issues that can be solved using greedy programming:

1. Coin Change Problem: Given a set of coins with different denominations and a total amount of money, the goal is to find the minimum number of coins needed to make up the amount. In this problem, the greedy approach always chooses the coin with the largest denomination that is less than or equal to the remaining amount.

2. Activity Selection Problem: Given a set of activities with start and end times, the goal is to select the maximum number of non-overlapping activities. The greedy approach is always choosing the activity that ends first and then recursively applying the same strategy to the remaining activities.

3. Huffman Encoding: Given a set of characters and their frequencies, the goal is to find a binary code for each character to minimize the number of bits used. The greedy approach repeatedly merges the two characters with the lowest frequencies into a single node until a binary tree represents all characters.

4. Minimum Spanning Tree: Given a weighted graph, the goal is to find a spanning tree with the minimum total weight. The greedy approach starts with any vertex and repeatedly adds the edge with the most negligible weight that connects a vertex in the tree to a vertex outside the tree until all vertices are included.

5. Knapsack Problem: Given a set of items with weights and values, and a knapsack with a limited weight capacity, the goal is to maximize the total value of the things that can be put into the knapsack. The greedy approach is to sort the things by their value-to-weight ratios and add them to the knapsack in that order until the knapsack is full.

## What is Dynamic Programming?

Dynamic programming is a technique that breaks down a problem into smaller subproblems and solves each subproblem only once. It involves breaking a problem into subproblems and solving each problem only once. It uses the concept of memoization, where solutions to subproblems are stored in memory for later use. It can solve problems in various domains, such as computer science, engineering, economics, and finance. Dynamic Programming is often used to solve optimization problems, where the goal is to find the best solution among possible solutions. Its goal is to find the optimal solution by solving the subproblems in a bottom-up manner.

Also see, C Programming and Application of Graph in Data Structure

## When to use Dynamic Programming?

1. Dynamic programming is best suited for optimization problems that have overlapping subproblems.

2. It is also useful when the problem can be broken down into smaller subproblems, and the solutions can be combined to solve the more significant problem.

3. Dynamic programming is often used to solve complex problems, such as the shortest path problem, the knapsack problem, and the traveling salesperson problem.

## Characteristics of Dynamic Programming:

The characteristics of dynamic programming are as follows:

### Optimal Substructure Property

By integrating the best answers to each of the subproblems, the overall problem can be solved optimally.

### Overlapping Subproblems

Dynamic programming problems frequently have overlapping subproblems that can be solved separately from one another. As a result, when we need to answer the same subproblem repeatedly, we can reuse the solutions by storing them in a table.

### Memoization (Top-Down Approach)

In dynamic programming, the outcomes of solved subproblems are frequently saved in a data structure (such as an array or hash table). By retrieving previously computed solutions as needed, avoids needless computations.

### Tabulation (Bottom-Up Approach)

It is a different technique to dynamic programming, where the subproblems are solved iteratively, from the simplest subproblems all the way up to the solution of the main problem. An array or matrix is frequently used in tabulation to store intermediate results.

Read More,  floyd's algorithm

## Examples of Dynamic Programming:

Dynamic programming solves complex problems by breaking them down into smaller subproblems and solving each subproblem only once. This technique is often used in optimization problems, where the goal is to find a function's maximum or minimum value.

1. Fibonacci Sequence: In this problem, we need to find the nth number in the Fibonacci sequence, where each number is the sum of the two preceding ones. A naive recursive approach would lead to exponential time complexity. Dynamic programming can be used to compute the Fibonacci sequence in linear time.

2. Longest Common Subsequence: Given two series, we need to find the longest subsequence common to both. This problem can be solved using dynamic programming by breaking it down into smaller subproblems and incrementally building the solution.

3. Shortest Path in a Graph: Given a weighted graph, we must find the shortest path between two vertices. Dynamic programming can solve this problem by computing the shortest paths from the source vertex to all other vertices in the graph, then selecting the shortest path from the source vertex to the destination vertex.

4. Knapsack Problem: In this problem, we need to find the maximum value obtained by selecting a subset of items with given weights and values such that the total weight does not exceed a given limit. Dynamic programming can solve this problem by breaking it down into smaller subproblems and computing the maximum value that can be obtained using only a subset of the items.

5. Edit Distance: Given two strings, we need to find the minimum number of operations required to transform one string into the other, where each process can be either an insertion, deletion, or substitution. Dynamic programming can solve this problem by breaking it down into smaller subproblems and computing the minimum number of operations required to transform each substring of the two strings.

## Difference between Greedy and Dynamic Programming

Here are some difference between Greedy and Dynamic Programming.

Also see, Rabin Karp Algorithm

## Frequently Asked Questions

### What is a dynamic algorithm?

A dynamic algorithm solves problems by breaking them down into smaller subproblems and solving each subproblem only once.

### What is a greedy algorithm?

A greedy algorithm solves problems by making the locally optimal choice at each step in the hope of finding a global optimum.

### What are the differences between dynamic and greedy algorithms?

Dynamic algorithms are more time-consuming and require more memory space than greedy algorithms, but they are generally more accurate and can solve more complex problems. Greedy algorithms are simpler and faster than dynamic algorithms, but they may not always find the best solution to a problem.

### When should I use a dynamic algorithm and when should I use a greedy algorithm?

Use a dynamic algorithm when you need to find the best solution to a complex problem. Use a greedy algorithm when you need to find a quick and simple solution to a problem.

## Conclusion

Greedy programming and dynamic programming are two popular techniques used in computer science to solve optimization problems. In this article, we discussed the difference between greedy and dynamic programming, their examples, and when to use them. You can refer to the dial algorithm to better understand these algorithms.

Difference Between IOT and M2M

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning Ninja!

Topics covered
1.
Introduction
2.
What is Greedy Programming?
3.
When to use Greedy Programming?
4.
Characteristics of Greedy Programming:
4.1.
Greedy Choice Property
4.2.
Optimal Substructure Property
4.3.
Efficient
4.4.
Elimination of Backtracking
4.5.
Applicability
5.
Examples of Greedy Programming
6.
What is Dynamic Programming?
7.
When to use Dynamic Programming?
8.
Characteristics of Dynamic Programming:
8.1.
Optimal Substructure Property
8.2.
Overlapping Subproblems
8.3.
Memoization (Top-Down Approach)
8.4.
Tabulation (Bottom-Up Approach)
9.
Examples of Dynamic Programming:
10.
Difference between Greedy and Dynamic Programming
11.