greedy

The point is made with a counterexample Starting at vertex one, the nearest neighbour rule moves to three, then four, then two, then five The path length is ten A shorter path from one to five exists with length six

The greedy choice was locally reasonable and globally wrong That is the whole chapter in one picture Assembly programmers would call it a bad branch prediction The rest of us call it a wrong answer

The pattern

  • sort candidates by a rule
  • take the best current candidate
  • reject what conflicts
  • repeat

The hard part is not the loop The hard part is knowing whether the rule is valid

The interval case

#include <algorithm>
#include <utility>
#include <vector>

std::vector<std::pair<int, int>> greedy_intervals(
        std::vector<std::pair<int, int>> intervals) {
    std::ranges::sort(intervals,
        [](const auto &a, const auto &b) { return a.second < b.second; });
    std::vector<std::pair<int, int>> chosen;
    int last = -1;
    for (const auto &iv : intervals) {
        if (chosen.empty() || iv.first >= last) {
            chosen.push_back(iv);
            last = iv.second;
        }
    }
    return chosen;
}

For [(1,3), (2,4), (3,5)] it returns two intervals Sorting by end time is the rule that makes it work Sorting by start time would not guarantee the same result

Examples

Huffman coding combines the two least weight trees at each stage That greedy rule produces a tree with minimum weighted external path length

LPT scheduling orders jobs by length and puts each job on the machine that finishes first It does not guarantee an optimal schedule That is a heuristic, and it has a bound relating it to the optimal finish time

Greedy versus heuristic

A greedy algorithm is a procedure A heuristic is a procedure without an optimality guarantee Some greedy procedures are provably optimal, some are heuristics Do not use the words as if they mean the same thing

When it fits

  • the choice is irrevocable
  • a local rule can be proved safe
  • the problem has structure that rewards it

When those are not true, try dynamic programming or backtracking

Worked failure

Choosing scan windows to cover the most hosts Picking the window with the most hosts right now may leave two hosts uncovered that a different first choice would have included If you cannot prove the rule, test it against small cases where you can enumerate the truth

Mistakes

  • assuming intuition is a proof
  • quoting an optimal claim without the condition that makes it true
  • using greedy on a problem with future dependencies
  • forgetting tie breaking can change the result

Exercises

  1. Run the interval selector by start time instead of end time and find a failing case
  2. Explain why Huffman’s two least weight rule is safe
  3. Give an example where nearest neighbour gives a longer path
  4. Name a place in your work where a heuristic is acceptable

What to remember

Greedy is fast and fragile Prove the rule or call it a heuristic An unproved greedy algorithm is just optimism with a loop

Posted