Greedy Notes
Greedy Notes
2 Proof By Contradiction
A proof is a sequence S1 , . . . , Sn of statements where every statement is either
an axiom, which is something that we’ve assumed to be true, or follows logically
from the precedding statements.
To prove a statement p by contradiction we start with the first statement of the
proof as p̄, that is not p. A proof by contradiction then has the following form
p̄, . . . , q, . . . , q̄
Hence, by establishing that p̄ logically implies both a statement q and its nega-
tion q̄, the only way to avoid logical inconsistency in your system is if p is
true.
Almost all proofs of correctness use proof by contradiction in one way or another.
3 Exchange Argument
Here we explain what an exchange argument is. Exchange arguments are the
most common and simpliest way to prove that a greedy algorithm is optimal
for some optimization problem. However, there are cases where an exchange
argument will not work.
Let A be the greedy algorithm that we are trying to prove correct, and A(I)
the output of A on some input I. Let O be an optimal solution on input I that
is not equal to A(I).
The goal in exchange argument is to show how to modify O to create a new
solution O0 with the following properties:
1
1. O0 is at least as good of solution as O (or equivalently O0 is also optimal),
and
2. O0 is “more like” A(I) than O.
Note that the creative part, that is different for each algorithm/problem, is
determininig how to modify O to create O0 . One good heuristic to think of
A constructing A(I) over time, and then to look to made the modification at
the first point where A makes a choice that is different than what is in O. In
most of the problem that we examine, this modification involves changing just
a few elements of O. Also, what “more like” means can change from problem
to problem. Once again, while this frequently works, there’s no guarantee.
2
personal preference.
3
X
A(I)
Y
O
X
O’
X doesn’t overlap with any intervals to its right in O0 because of the key point
above and the fact that O was feasible.),
2. O0 has as many intervals as O (and is hence also optimal), and
3. O0 has more intervals in common with A(I) than O.
Hence, we reach a contradiction.
End of Proof.
(a1 , p1 , b1 ) . . . (an , pn , bn )
4
of disagreement of OP T (X) and G(X), G(X) hadn’t finished job i by time t
(and thus neither had OP T (X)), and OP T (X) is an acceptable output.
5
End of Proof.
6
Figure 3: The instances G(X), Opt(X) and Opt0 (X)
7
9 Kruskal’s Minimum Spanning Tree Algorithm
We show that the standard greedy algorithm that considers the jobs from short-
est to longest is optimal. See section 4.1.2 from the text.
Lemma: If Kruskal’s algorithm does not included an edge e = (x, y) then at the
time that the algorithm considered e, there was already a path from x to y in
the algorithm’s partial solution.
Theorem: Kruskal’s algorithm is correct.
Proof: We use an exchange argument. Let K be a nonoptimal spanning tree
constructed by Kruskal’s algorithm on some input, and let O be an optimal tree
that agrees with the algorithms choices the longest (as we following the choices
made by Kruskal’s algorithm). Consider the edge e on which they first disagree.
We first claim that e ∈ K. Otherwise, by the lemma there was previously a path
between the endpoints of e in the K, and since optimal and Kruskal’s algorithm
have agreed to date, O could not include e, which is a contradiction to the fact
that O and K disagree on e. Hence, it must be the case that e ∈ K and e ∈ / O.
8
Let x and y be the endpoints of e. Let C = x = z1 , z2 , . . . , zk be the unique cycle
in O ∪ {e}. We now claim that there must be an edge (zp , zp+1 )inC − {e} with
weight not smaller than e’s weight. To reach a contradiction assume otherwise,
that is, that each edge (zi , zi+1 have weight less than the weight of (x, y). But
then Kruskal’s considered each (zi , zi+1 before (x, y), and by the choice of (x, y)
as being the first point of disagreement, each (zi , zi+1 ) must be in K. But
this is then a contradiction to K being feasible (obviously Kruskal’s algorithm
produces a feasible solution).
We then let O0 = O+e−(zp , zp+1 ). Clearly O0 agrees with K longer than O does
(note that since the weight of (zp , zp+1 ) is greater than weight of e, Kruskal’s
considers (zp , zp+1 ) after e) and O0 has weight no larger than O’s weight (and
hence O0 is still optimal) since the weight of edge (zp , zp+1 ) is not smaller than
the weight of e.
EndProof
10 Huffman’s Algorithm
We consider the following problem.
Input: Positive weights p1 , . . . , pn
Output: APbinary tree with n leaves and a permutaton s on {1, . . . , n} that
n
minimizes i=1 ps(i) di , where di is the depth of the ith leaf.
Huffman’s algorithm picks the two smallest weights, say pi and pj , and gives
then a common parent in the tree. The algorithm then replaces pi and pj by
a single number pi + pj and recurses. Hence, every node in the final tree is
label with a probability. The probability of each internal node is the sum of the
probabilities of its children.
Lemma: Every leaf in the optimal tree has a sibling.
Proof: Otherwise you could move the leaf up one, decreasing it’s depth and
contradicting optimality.
Theorem: Huffman’s algorithm is correct.
Proof: We use an exchange argument. Let consider the first time where the
optimal solution O differs from the tree H produced by Huffman’s algorithm.
Let pi and pj be the siblings that Huffman’s algorithm creates at this time.
Hence, pi and pj are not siblings in O. Let pa be sibling of pi in O, and pb be
the sibling of pj in O. Assume without loss of generality that di = da ≤ db = dj .
Let s = db − da . Then let O0 be equal to O with the subtrees rooted at pi and
pb swapped. The net change in the average depth is kpi − kpb .
Hence in order to show that the average depth does not increase and that O0
is still optimal, we need to show that pi ≤ pb . Assume to reach a contradiction
that indeed it is the case that pb < pi . Then Huffman’s considered pb before it
paired pi and pj . Hence pa ’s partner in H is not pi . This contradicts the choice
9
of pi and pj as being the first point where they differ.
Using similar arguments it also follows that pj ≤ pb , pi ≤ pb , and pj ≤ pa .
Hence, O0 agrees with H for one more step than O did (note that O and H
could no.
EndProof.
10