Greedy algorithms are algorithms that always make the choice that looks best at the moment — it makes a locally optimal choice hoping that leads to a globally optimal solution. They’re generally fast but often don’t get the optimal solution. Smarter greedy algorithms use a guess of what may happen in the future to make a better decision. Notably, greedy algorithms are good approximators for NP-complete problems.

Like dynamic programming, greedy algorithms rely on optimal substructure proofs. This is achievable primarily via contradiction — and the premise is that greedy solutions are built on top of other greedy solutions to smaller problems.

Principles

Greedy solutions rely on three principles:

  • Greedy choice — the optimal solution agrees with the first greedy choice.
  • Smaller subproblems — after making the greedy choice, the resulting subproblem reduces in size.
  • Optimal substructure — an optimal solution contains optimal solutions to subproblems.

In particular, for problems where greedy is optimal, we must be able to prove optimality of the greedy approach, i.e., that if we made the greedy decision first, we’d be able to reach an optimal point.

The intuition behind when to pick greedy versus DP largely comes down to whether a greedy solution can “overshoot” or “undershoot”. DP solutions are viable for greedy problems, but we can get a more optimal solution with greedy.

Approach

When we’ve found a suitable greedy choice, the proof of correctness has a few steps we follow:

  • Let , where denotes a greedy solution.
  • First greedy choice — there exists some optimal solution such that but past 1.
  • Smaller subproblems — after making the choice , then we should be left with a smaller subproblem.
  • Optimal substructure — we must solve the subproblem optimally for the original solution to be optimal.
  • Recurse the argument to conclude that .
  • Therefore there exists an optimal solution such that .

Greedy choice property

This is the most important, and most arcane proof in our procedure. We note a few key ideas:

  • For greedy problems, there can be multiple optimal solutions. In fact, there can be multiple greedy solutions to a single problem. This makes contradiction a difficult proof to pull off.
  • Thus, it’s very easy to “come” to a contradiction when one might not exist.

In our proof, we first define as an optimal and as a greedy solution. If , “then we’re done”. We still need to prove the case where . Depending on the problem, we either have 1) every element in the set show up in our final answer (car building), or 2) only a subset of input elements (scheduling).

  • In the first case, this implies that must appear somewhere in . By the definition of the greedy choice (i.e., by how we constructed it), we should get an inequality. Like for some .
  • In the second case, the order of selection doesn’t matter. Assume , then , by construction of . If we construct a solution

Problems

Extra notes

one pitfall:

  • different optimal solutions, not every optimal solution agrees with the greedy choice
  • common mistake: they say is any optimal solution. suppose .
  • they “come” to a contradiction, where there might not exist one

final 2015:

  • note . not <. first slide.
    • not a contradiction! because there can be multiple greedy solutions to a single problem
  • we instead found another optimal solution that agrees with the first greedy choice
  • optimal substructure: done via contradiction
    • strictly non-optimal, so strict inequality <
    • basically depends on original greedy choice we proved before being true

scheduling deadlines:

  • again not actually a contradiction. we’re proving that an optimal solution that agrees with the greedy choice