Proofs: Greedy Algorithms
A greedy algorithm is only useful if we can prove that its choices lead to an optimal solution. This page proves the optimality of the greedy algorithms that the lessons call optimal, mostly with exchange arguments: take any optimal solution and show that it can be changed, step by step, into the greedy one without getting worse.
The greedy change is optimal for powers of a base
The change lesson states that the greedy algorithm is optimal when the coins are . We prove a slightly more general result.
Claim. If every coin value divides the next one (, and is a multiple of ), and there is an unlimited supply of each coin, the greedy algorithm uses the minimum number of coins for every amount.
Proof. Take an optimal solution and let be the number of coins of value it uses. For every , : otherwise, the coins of value would add up to at least , and since is an integer, of them could be replaced by a single coin of value , using fewer coins. So the coins smaller than add up to
(using , which holds because is a multiple of smaller than ). This means that, in an optimal solution, the number of coins of the largest value is exactly , the maximum that fits, because the rest of the coins add up to less than . That is the greedy choice. Repeating the argument with the remaining amount and the smaller coins, the optimal solution coincides with the greedy one.
The condition is sufficient but not necessary: the euro system (1, 2, 5, 10, ...) does not satisfy it, since 2 does not divide 5, and the greedy algorithm is still optimal for it.
The fractional knapsack: highest value per weight first
Claim. If objects can be split, taking them in decreasing order of (and a fraction of the last one) maximizes the value.
Proof (exchange argument). Sort the objects so that , and let be the greedy solution and any optimal solution, both filling the knapsack completely (if the objects do not fill it, both take everything). Let be the first object that takes in a smaller amount than . Since takes as much as possible of each object in order, must use the missing weight on objects . Replace in a weight of those objects by the same weight of object : the value changes by
so the new solution is still optimal and agrees with on one more object. Repeating, we transform into without ever decreasing its value, so is optimal.
The argument fails for the 0/1 knapsack because there we cannot move an arbitrary weight : objects are taken whole.
The plumber: shortest tasks first
Claim. Doing the tasks in increasing order of duration minimizes the total waiting time of the plumber's customers.
Proof. If the tasks are done in the order , the task in position is counted in its own waiting time and in that of the customers after it:
The coefficients decrease. Suppose that in some order a longer task comes immediately before a shorter one, . Swapping them changes by : the total decreases. So an optimal order has no such pair, which means it is sorted by increasing duration.
Files on a disk: smallest files first
Claim. To store the largest number of files on a disk of capacity , taking the files from smallest to largest while they fit is optimal.
Proof. Let the files sorted by size be , and suppose the greedy algorithm stores the first . Any set of files has total size at least , since those are the smallest. If , this sum is at least (the greedy algorithm stopped because file did not fit). So no set of more than files fits.
(The same rule does not minimize the free space; that problem is the 0/1 knapsack with .)
Prim and Kruskal are optimal
Both algorithms for minimum spanning trees rely on one property. A cut of a graph is a division of its nodes into two non-empty groups and ; an edge crosses the cut if it has one end in each group.
Cut property. Let be a set of edges contained in some minimum spanning tree, and let be a cut that no edge of crosses. If is a cheapest edge crossing the cut, then is also contained in some minimum spanning tree.
Proof. Let be a minimum spanning tree that contains . If , we are done. Otherwise, adding to creates a cycle, because already connects and . The path of from (in ) to (outside ) must cross the cut at some edge . Replace by : the result is still connected (any path that used can go around the cycle through ) and has edges, so it is a spanning tree. Its weight is , because is a cheapest crossing edge. So is also a minimum spanning tree, and it contains ( is not in , because no edge of crosses the cut).
Prim. At each step, let be the set of nodes of the current tree. No edge of the tree crosses the cut , and Prim adds a cheapest crossing edge. By induction on the steps, using the cut property, the tree is always contained in a minimum spanning tree; when it has edges, it is one.
Kruskal. When Kruskal accepts the edge , let be the set of nodes of the current tree that contains . No accepted edge crosses the cut , and every lighter edge that crosses it was considered earlier and rejected, which only happens if both its ends were already connected, contradicting that it crosses the cut. So is a cheapest crossing edge, and the cut property applies again.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.