A greedy algorithm is an approach for solving a problem by selecting the best option available at the moment, without worrying about the future result it would bring. In other words, the locally best choices aim at producing globally best results.

This algorithm may not be the best option for all the problems. It may produce wrong results in some cases.

This algorithm never goes back to reverse the decision made. This algorithm works in a top-down approach.

The main advantage of this algorithm is:

- The algorithm is
**easier to describe**. - This algorithm can
**perform better**than other algorithms (but, not in all cases).

## Feasible Solution

A feasible solution is the one that provides the optimal solution to the problem.

## Greedy Algorithm

- To begin with, the solution set (containing answers) is empty.
- At each step, an item is added into the solution set.
- If the solution set is feasible, the current item is kept.
- Else, the item is rejected and never considered again.

## Example - Greedy Approach

Problem:You have to make a change of an amount using the smallest possible number of coins. Amount: $28 Available coins: $5 coin $2 coin $1 coin

**Solution:**

- Create an empty
`solution-set = { }`. `coins = {5, 2, 1}``sum = 0`- While
`sum ≠ 28`, do the following. - Select a coin
`C`from`coins`such that`sum + C < 28`. - If
`C + sum > 28`, return no solution. - Else,
`sum = sum + C`. - Add
`C`to`solution-set`.

Up to the first 5 iterations, the solution set contains `5` $5 coins. After that, we get `1` $2 coin and finally, `1` $1 coin.