






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







A greedy algorithm:
Stanford describes the idea as: “Make choices one-at-a-time. Never look back.”
For scheduling jobs, a greedy algorithm might choose the job with the best immediate ratio of importance to duration. For activity selection, it chooses the compatible activity that finishes earliest.
The challenge is not making the greedy choice—it is proving that the choice is safe.
A greedy algorithm is guaranteed to be correct when the problem has:
Property Meaning
Optimal substructure
An optimal solution contains optimal solutions to subproblems
Greedy-choice property
A locally optimal choice can be extended to a globally optimal solution
Stanford’s notes state that proving a greedy algorithm correct requires identifying optimal substructure, then showing that at each step only one subproblem needs to be considered.
Not every problem has a greedy solution. A greedy rule that works for one optimization problem may fail for another, even if the problems look similar. Stanford’s practice materials explicitly note that greedy algorithms may fail and require counterexamples.
The standard proof technique is an exchange argument.
activities.
Stanford’s slides give exactly these bounds.
The goal is to order the jobs to minimize total delay cost.
Schedule jobs in decreasing order of:
That is, prioritize jobs with the largest cost-of-delay-to-duration ratio.
front without loss of optimality.
Sorting by ratio takes:
After sorting, the schedule is produced directly.
Given characters and their frequencies, construct a prefix-free binary encoding that minimizes the total encoded length.
A prefix-free code ensures that no codeword is a prefix of another codeword. This allows unambiguous decoding.
Repeatedly merge the two subtrees with the smallest total frequencies.
text HUFFMAN(frequencies): create one leaf node for each character
while more than one node remains: x = node with smallest frequency y = node with second-smallest frequency
create parent z left child of z = x right child of z = y frequency(z) = frequency(x) + frequency(y)
insert z into the collection
return the remaining node
Kruskal’s algorithm grows a forest of disjoint trees.
text KRUSKAL(G): T = empty set sort edges by increasing weight
for each edge (u, v) in sorted order: if u and v are in different components: add (u, v) to T union their components
return T
Kruskal considers edges in increasing weight order and adds an edge only if it does not create a cycle.
Both algorithms rely on the cut property :
For Prim, the chosen edge is the lightest edge crossing the current tree-to-rest cut. For Kruskal, each accepted edge is the lightest edge crossing the cut between its two connected components.
Stanford’s notes state that the correctness proofs for both Prim and Kruskal use exchange arguments.
Feature Greedy algorithm Dynamic programming
Decision
strategy Makes one locally optimal choice
Considers multiple subproblem outcomes
Backtracking Never revisits choices May combine many
Feature Greedy algorithm Dynamic programming
subproblem solutions
Proof requirement
Greedy-choice property and exchange argument
Optimal substructure and recurrence
Typical runtime
Often polynomial in table size
Example Activity selection 0/1 Knapsack
Stanford describes greedy algorithms as a refinement of dynamic programming: a greedy algorithm is correct when only one prior table entry needs to be considered at each step.
Activity selection has a correct greedy rule: choose the earliest-finishing compatible activity.
0/1 Knapsack generally requires dynamic programming because choosing the highest- value or best-ratio item first can prevent an optimal solution.
A greedy algorithm must be proved correct. Many plausible greedy rules fail.
For example, choosing the shortest job first, the highest-value job first, or the best-ratio job first may fail for certain optimization problems. Stanford’s practice materials emphasize constructing counterexamples when greedy fails.
A locally optimal choice is not automatically part of a globally optimal solution. The greedy-choice property must be proved.
Topic
Official Stanford resource
Topic
Official Stanford resource
slides
Problem Greedy rule
Correctness tool Runtime
Activity
selection
Choose earliest- finishing compatible activity
Exchange
Job
scheduling
Choose largest cost/time ratio first
Exchange
Huffman
coding
Merge two least- frequent subtrees
Greedy-choice
Prim’s MST
Add lightest edge
Kruskal’s
MST
Add lightest non- cycle edge
Cut property and