Greedy Algorithm

Context: FIT2004_MOC · the paradigm behind Prim, Kruskal and Dijkstra — take the best-looking option now, never reconsider, and prove the result is globally optimal Parent Framework: Algorithm

Quick Revision

  • 🎯 Objective: one irrevocable locally-optimal choice per step ➔ a globally optimal solution, when the problem has the greedy-choice property.
  • 📦 Core Components: a ranking rule (sort key) ➔ a feasibility test ➔ a proof by exchange argument or stays-ahead induction.
  • ⚡ Key Constraint: the algorithm is trivial and the proof is the deliverable — an unproved greedy is a guess, and most plausible ranking rules are wrong.

📝 How It Works

1. The Shape

  • Ranking rule ➔ sort or heap the candidates by the quantity the greed maximises — edge weight for Kruskal, finish time for interval scheduling.
  • Feasibility test ➔ accept the next candidate iff it keeps the partial solution legal (acyclic for MST, non-overlapping for scheduling).
  • Irrevocable ➔ an accepted choice is never undone; this is what separates greedy from dynamic programming, which keeps every subproblem’s answer and combines them later.
  • Cost profile ➔ almost always from the sort or scanning ⟹ the sort is the bound.

2. When Greed Is Provably Right

  • Greedy-choice property ➔ some optimal solution contains the first greedy choice; hence choosing it loses nothing.
  • Optimal substructure ➔ after committing that choice, the remaining problem is the same problem on a smaller instance.
  • Both are needed ➔ optimal substructure alone gives dynamic programming; the greedy-choice property is the extra fact that lets you skip the search.
  • The lecture’s framing ➔ choose local optimal, believe you get the global optimal — then prove it.

3. The Two Proof Templates (LO2)

  • Exchange argument ➔ assume an optimal solution that omits your greedy choice ; show can be edited to include without getting worse ⟹ an optimal solution containing exists. This is the cut-and-swap used for Prim and Kruskal.
  • Stays-ahead induction ➔ show that after choices your partial solution is at least as far along as any optimal solution’s first choices; conclude the optimum can never be strictly longer.
  • Contradiction wrapper ➔ both are usually written as “suppose a better solution exists” and terminate in an impossible inequality, exactly as Dijkstra’s proof does.
  • Termination is still owed ➔ a greedy proof needs the Invariant pair: the maintained property and the argument that the loop ends.

4. Interval Scheduling — the Worked Case (applied P7)

  • Problem ➔ requests with start and finish ; a subset is compatible if no two overlap; maximise the number accepted.
  • Rule ➔ sort by finishing time ascending; scan, accepting each request compatible with the last accepted one.
  • Intuition ➔ freeing the machine as early as possible leaves the most room for everything after it.
  • Cost ➔ sort scan; only the finish time of the last accepted request need be remembered.
  • Not a weight problem ➔ this maximises the count; weighted interval scheduling is a dynamic-programming problem, not a greedy one.

5. Ranking Rules That Look Sensible and Fail

  • Earliest start time ➔ the request that starts first may be the one that finishes last, blocking everything ➔ a single long job beats the whole schedule.
  • Shortest duration ➔ counterexample from the sheet: , , . The shortest is , and taking it excludes both others ⟹ accepted where were possible.
  • Fewest conflicts ➔ also fails in general; plausibility is not a proof.
  • The lesson (LO1) ➔ enumerate two or three candidate ranking rules, kill the wrong ones with explicit counterexamples, and only then prove the survivor. That counterexample hunt is itself a marked step.

⚖️ Core Decision Matrix

ProblemGreedy ruleProof templateBound
Minimum Spanning Tree via Kruskallightest edge joining two componentsexchange across a cut
Minimum Spanning Tree via Primlightest edge leaving the treeexchange across a cut
Single-source shortest path (Dijkstra)nearest non-finalised vertexcontradiction on the first bad serve
Interval scheduling (max count)earliest finish timestays ahead
Weighted interval schedulingnone — greedy fails—dynamic programming
Shortest path with negative edgesnone — greedy fails—Bellman-Ford

When It Flips: greed survives exactly while a locally best choice can be exchanged into some optimal solution. Add weights to the intervals, or negative edges to the graph, and that exchange stops going through — the algorithm must change, not its constant factors.

🔭 Beyond the lecture (not in the slides): the structures on which "always take the cheapest feasible element" is provably optimal are matroids — Kruskal is the graphic matroid's instance. FIT2004 asks for the exchange proof, not the theory.

📊 Exam Execution Trace & Applied Exercises

Applied Exercise — prove interval scheduling optimal (P7, stays ahead)

Problem: let the algorithm output in time order and suppose some compatible set has .

Final Extracted Output: the algorithm would have accepted instead of stopping at — contradiction, so and the output is of maximum size. The reusable move: index the two solutions in parallel and prove yours never falls behind.

Applied Exercise — kill the rival rules

Problem: show shortest duration and earliest start are not optimal. Final Extracted Output: on shortest-duration takes and accepts ; earliest-finish takes then and accepts . On the same instance earliest-start also takes first and here coincides — so exhibit a separate instance for it, e.g. one long request starting at time that overlaps every short one. One counterexample kills one rule; do not reuse it for both.

⚠️ Common Mistakes

  • 💡 Presenting the rule without the proof ➔ “sort by finish time and scan” is lines of an answer whose marks live in the stays-ahead induction. The same applies to every MST answer.
  • 💡 Assuming a plausible sort key ➔ shortest-first, most-constrained-first and cheapest-first are all wrong somewhere. Name the rule, then attack it.
  • 💡 Calling Dijkstra “just greedy” ➔ it is greedy and dynamic programming: it reuses sub-minima and finalises irrevocably. The negative-edge failure is the greedy half breaking, not the DP half.

🧠 Active Recall