Skip to main content

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 1,p,p2,…,pm1, p, p^2, \dots, p^m. We prove a slightly more general result.

Claim. If every coin value divides the next one (c1=1c_1 = 1, and ci+1c_{i+1} is a multiple of cic_i), 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 xix_i be the number of coins of value cic_i it uses. For every i<mi \lt m, xi⋅ci<ci+1x_i \cdot c_i \lt c_{i+1}: otherwise, the coins of value cic_i would add up to at least ci+1c_{i+1}, and since ci+1/cic_{i+1} / c_i is an integer, ci+1/ci≥2c_{i+1} / c_i \ge 2 of them could be replaced by a single coin of value ci+1c_{i+1}, using fewer coins. So the coins smaller than ci+1c_{i+1} add up to

∑j≤ixjcj≤∑j≤i(cj+1−cj)=ci+1−1<ci+1\sum_{j \le i} x_j c_j \le \sum_{j \le i} (c_{j+1} - c_j) = c_{i+1} - 1 \lt c_{i+1}

(using xjcj≤cj+1−cjx_j c_j \le c_{j+1} - c_j, which holds because xjcjx_j c_j is a multiple of cjc_j smaller than cj+1c_{j+1}). This means that, in an optimal solution, the number of coins of the largest value cmc_m is exactly ⌊A/cm⌋\lfloor A / c_m \rfloor, the maximum that fits, because the rest of the coins add up to less than cmc_m. That is the greedy choice. Repeating the argument with the remaining amount and the smaller coins, the optimal solution coincides with the greedy one. ■\blacksquare

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 vi/wiv_i / w_i (and a fraction of the last one) maximizes the value.

Proof (exchange argument). Sort the objects so that v1w1≥v2w2≥…\frac{v_1}{w_1} \ge \frac{v_2}{w_2} \ge \dots, and let GG be the greedy solution and OO any optimal solution, both filling the knapsack completely (if the objects do not fill it, both take everything). Let ii be the first object that OO takes in a smaller amount than GG. Since GG takes as much as possible of each object in order, OO must use the missing weight δ\delta on objects j>ij \gt i. Replace in OO a weight δ\delta of those objects by the same weight of object ii: the value changes by

δ⋅viwi−δ⋅vjwj≥0\delta \cdot \frac{v_i}{w_i} - \delta \cdot \frac{v_j}{w_j} \ge 0

so the new solution is still optimal and agrees with GG on one more object. Repeating, we transform OO into GG without ever decreasing its value, so GG is optimal. ■\blacksquare

The argument fails for the 0/1 knapsack because there we cannot move an arbitrary weight δ\delta: objects are taken whole.


The plumber: shortest tasks first​

Claim. Doing the tasks in increasing order of duration minimizes the total waiting time W=∑wiW = \sum w_i of the plumber's customers.

Proof. If the tasks are done in the order t(1),t(2),…,t(n)t_{(1)}, t_{(2)}, \dots, t_{(n)}, the task in position kk is counted in its own waiting time and in that of the n−kn - k customers after it:

W=∑k=1n(n−k+1) t(k)W = \sum_{k=1}^{n} (n - k + 1) \, t_{(k)}

The coefficients n,n−1,…,1n, n-1, \dots, 1 decrease. Suppose that in some order a longer task comes immediately before a shorter one, t(k)>t(k+1)t_{(k)} \gt t_{(k+1)}. Swapping them changes WW by (n−k+1)t(k+1)+(n−k)t(k)−(n−k+1)t(k)−(n−k)t(k+1)=t(k+1)−t(k)<0(n - k + 1) t_{(k+1)} + (n - k) t_{(k)} - (n - k + 1) t_{(k)} - (n - k) t_{(k+1)} = t_{(k+1)} - t_{(k)} \lt 0: the total decreases. So an optimal order has no such pair, which means it is sorted by increasing duration. ■\blacksquare


Files on a disk: smallest files first​

Claim. To store the largest number of files on a disk of capacity DD, taking the files from smallest to largest while they fit is optimal.

Proof. Let the files sorted by size be s1≤s2≤⋯≤sns_1 \le s_2 \le \dots \le s_n, and suppose the greedy algorithm stores the first gg. Any set of mm files has total size at least s1+⋯+sms_1 + \dots + s_m, since those are the mm smallest. If m>gm \gt g, this sum is at least s1+⋯+sg+1>Ds_1 + \dots + s_{g+1} \gt D (the greedy algorithm stopped because file g+1g + 1 did not fit). So no set of more than gg files fits. ■\blacksquare

(The same rule does not minimize the free space; that problem is the 0/1 knapsack with vi=wiv_i = w_i.)


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 SS and V∖SV \setminus S; an edge crosses the cut if it has one end in each group.

Cut property. Let FF be a set of edges contained in some minimum spanning tree, and let (S,V∖S)(S, V \setminus S) be a cut that no edge of FF crosses. If ee is a cheapest edge crossing the cut, then F∪{e}F \cup \{e\} is also contained in some minimum spanning tree.

Proof. Let TT be a minimum spanning tree that contains FF. If e∈Te \in T, we are done. Otherwise, adding e=(u,v)e = (u, v) to TT creates a cycle, because TT already connects uu and vv. The path of TT from uu (in SS) to vv (outside SS) must cross the cut at some edge e′e'. Replace e′e' by ee: the result T′=T−e′+eT' = T - e' + e is still connected (any path that used e′e' can go around the cycle through ee) and has n−1n - 1 edges, so it is a spanning tree. Its weight is w(T)−w(e′)+w(e)≤w(T)w(T) - w(e') + w(e) \le w(T), because ee is a cheapest crossing edge. So T′T' is also a minimum spanning tree, and it contains F∪{e}F \cup \{e\} (e′e' is not in FF, because no edge of FF crosses the cut). ■\blacksquare

Prim. At each step, let SS be the set of nodes of the current tree. No edge of the tree crosses the cut (S,V∖S)(S, V \setminus S), 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 n−1n - 1 edges, it is one.

Kruskal. When Kruskal accepts the edge e=(u,v)e = (u, v), let SS be the set of nodes of the current tree that contains uu. No accepted edge crosses the cut (S,V∖S)(S, V \setminus S), 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 ee is a cheapest crossing edge, and the cut property applies again. ■\blacksquare

Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.