Greedy Algorithms

A greedy algorithm constructs a solution to the problem by always making a choice that looks the best at the moment.

  • Greedy algorithms are usually very efficient.
  • The difficulty is find a greedy strategy that always produces a global optimal solution to the problem
  • Often difficult to argue that a greedy algorithm works

Applications

Example where greedy algorithm fails

Coin problem (use the least amount of coins to create a certain amount)

  • Naive solution would be to try all combinations. However, we can act greedily
  • Always try to use the largest coin until you are no longer able to. Then, switch to the next coin. This works with European coins.
  • Counter example: You have coins and the target sum is . The Greedy Algorithm will produce the solution , but the correct solution is

Proving Greedy Algorithms

I’ve figured out a small template for proving greedy algorithms.

Method #1

Assume the set generated by the optimal solution, and show that the greedy algorithm is as good as the optimal solution (prove this contradiction, assuming that greedy algorithm is worse than the optimal solution).

Example: see Interval Scheduling.

Method #2

Assume that the greedy algorithm uses things. Show that there is no other way for it to uses things. Thus, the greedy algorithm is optimal.