The point is, ladies and gentleman, greed is good. Greed works, greed is right.
Greed clari�es, cuts through, and captures the essence of the evolutionary spirit.
Greed in all its forms, greed for life, money, love, knowledge has marked the
upward surge in mankind. And greed—mark my words—will save not only Teldar
Paper but the other malfunctioning corporation called the USA.
— Gordon Gekko [Michael Douglas], Wall Street (��8�)
There is always an easy solution to every human problem—
neat, plausible, and wrong.
— H. L. Mencken, “The Divine Af�atus”,
New York Evening Mail (November �6, ����)�
Greedy Algorithms
�.�

Storing Files on Tape

Suppose we have a set of n files that we want to store on magnetic tape. In the
future, users will want to read those files from the tape. Reading a file fromtape isn’t like reading a file from disk; first we have to fast-forward past all theother files, and that takes a significant amount of time. Let L[1 .. n] be an array
listing the lengths of each file; specifically, file i has length L[i]. If the files are
stored in order from 1 to n, then the cost of accessing the kth file iscost(k) =

k
X

L[i].

i=1

Readers who are tempted to object that magnetic tape has been obsolete for decades
are cordially invited to tour your nearest supercomputer facility; ask to see the tape robots.
Alternatively, consider filing a sequence of books on a library bookshelf. You know, those strange
brick-like objects made of dead trees and ink?

����. G����� A���������

The cost reflects the fact that before we read file k we must first scan past all theearlier files on the tape. If we assume for the moment that each file is equally
likely to be accessed, then the expected cost of searching for a random file isE[cost] =nX
cost(k)
k=1

n

1 XX
L[i].
n k=1 i=1n=k

If we change the order of the files on the tape, we change the cost of accessing
the files; some files become more expensive to read, but others become cheaper.
Different file orders are likely to result in different expected costs. Specifically,let ⇡(i) denote the index of the file stored at position i on the tape. Then the
expected cost of the permutation ⇡ is
1 XX
E[cost(⇡)] =L[⇡(i)].
n k=1 i=1nkWhich order should we use if we want this expected cost to be as small as
possible? The answer seems intuitively clear: Sort the files by increasing length.
But intuition is a tricky beast. The only way to be sure that this order works is
to take off and nuke the entire site from orbit actually prove that it works!
Lemma �.�. E[cost(⇡)] is minimized when L[⇡(i)]  L[⇡(i + 1)] for all i.

Proof: Suppose L[⇡(i)] > L[⇡(i + 1)] for some index i. To simplify notation,
let a = ⇡(i) and b = ⇡(i + 1). If we swap files a and b, then the cost ofaccessing a increases by L[b], and the cost of accessing b decreases by L[a].
Overall, the swap changes the expected cost by (L[b] L[a])/n. But this change
is an improvement, because L[b] < L[a]. Thus, if the files are out of order, we
can decrease the expected cost by swapping some mis-ordered pair of files. É
This is our first example of a correct greedy algorithm. To minimize the total
expected cost of accessing the files, we put the file that is cheapest to accessfirst, and then recursively write everything else; no backtracking, no dynamic
programming, just make the best local choice and blindly plow ahead. If weuse an efficient sorting algorithm, the running time is clearly O(n log n), plus
the time required to actually write the files. To show that the greedy algorithm
is actually correct, we proved that the output of any other algorithm can be
improved by some sort of exchange
Let’s generalize this idea further. Suppose we are also given an array F [1 .. n]of access frequencies for each file; file i will be accessed exactly F [i] times overthe lifetime of the tape. Now the total cost of accessing all the files on the tape is
Ç
å
n
k
n X
k
X
X
X
⌃cost(⇡) =F [⇡(k)] ·L[⇡(i)] =F [⇡(k)] · L[⇡(i)] .
k=1

�6�

i=1

k=1 i=1

�.�. Scheduling Classes

As before, reordering the files can change this total cost. So what order should
we use if we want the total cost to be as small as possible? (This question issimilar in spirit to the optimal binary search tree problem, but the target data
structure and the cost function are both different, so the algorithm must be
different, too.)
We already proved that if all the frequencies are equal, we should sort thefiles by increasing size. If the frequencies are all different but the file lengths
L[i] are all equal, then intuitively, we should sort the files by decreasing accessfrequency, with the most-accessed file first. In fact, this is not hard to prove(hint, hint) by modifying the proof of Lemma . . But what if the sizes and thefrequencies both vary? In this case, we should sort the files by the ratio L/F .
Lemma �.�. ⌃cost(⇡) is minimized whenL[⇡(i)]
L[⇡(i + 1)]
for all i.
F [⇡(i)]F [⇡(i + 1)]

Proof: Suppose L[⇡(i)]/F [⇡(i)] > L[⇡(i + 1)]/F [⇡(i + i)] for some index i.
To simplify notation, let a = ⇡(i) and b = ⇡(i +1). If we swap files a and b, thenthe cost of accessing a increases by L[b], and the cost of accessing b decreasesby L[a]. Overall, the swap changes the total cost by L[b]F [a] L[a]F [b]. But
this change is an improvement, because
L[a]
L[b]
>
() L[b]F [a]
F [a]
F [b]

L[a]F [b] < 0.

Thus, if any two adjacent files are out of order, we can improve the total cost by
swapping them.
É

�.�

Scheduling Classes

The next example is slightly more complex. Suppose you decide to drop outof computer science and change your major to Applied Chaos. The Applied
Chaos department offers all of its classes on the same day every week, called“Soberday” by the students (but interestingly, not by the faculty). Every class
has a different start time and a different ending time: AC(“Toilet Paper
Landscape Architecture”) starts at : pm and ends at : pm; AC(“Immanentizing the Eschaton”) starts at : pm and ends at : pm, and so on.
In the interest of graduating as quickly as possible, you want to register for as
many classes as possible. (Applied Chaos classes don’t require any actual work.)
The university’s registration computer won’t let you register for overlappingclasses, and no one in the department knows how to override this “feature”.
Which classes should you take?
More formally, suppose you are given two arrays S[1 .. n] and F [1 .. n] listing
the start and finish times of each class; to be concrete, we can assume that
�6��. G����� A���������

0  S[i] < F [i]  M for each i, for some value M (for example, the numberof picoseconds in Soberday). Your task is to choose the largest possible subset
X 2 {1, 2, . . . , n} so that for any pair i, j 2 X , either S[i] > F [ j] or S[ j] > F [i].
We can illustrate the problem by drawing each class as a rectangle whose left
and right x-coordinates show the start and finish times. The goal is to find alargest subset of rectangles that do not overlap vertically.

Figure �.�. A maximum con�ict-free schedule for a set of classes.

This problem has a fairly simple recursive solution, based on the observationthat either you take class 1 or you don’t. Let B denote the set of classes that end
before class 1 starts, and let A denote the set of classes that start after class 1
ends:
B := {i | 2  i  n and F [i] < S[1]}A := {i | 2  i  n and S[i] > F [1]}If class 1 is in the optimal schedule, then so are the optimal schedules for B
and A, which we can find recursively. If not, we can find the optimal schedulefor {2, 3, . . . , n} recursively. So we should try both choices and take whichever
one gives the better schedule. Evaluating this recursive algorithm from thebottom up gives us a dynamic programming algorithm that runs in O(n3 ) time.
I won’t bother to go through the details, because we can do better.
Intuitively, we’d like the first class to finish as early as possible, because thatleaves us with the largest number of remaining classes. This intuition suggests
the following simple greedy algorithm. Scan through the classes in order offinish time; whenever you encounter a class that doesn’t conflict with your latestclass so far, take it! See Figure . for a visualization of the resulting greedy
schedule.
We can write the greedy algorithm somewhat more formally as shown in in
Figure . . (Hopefully the first line is understandable.) After the initial sort, thealgorithm is a simple linear-time loop, so the entire algorithm runs in O(n log n)time.
But you should still work out the details yourself. The dynamic programming algorithm
can be used to find the “best” schedule for several different definitions of “best”, but the greedy
algorithm I’m describing here only works when “best” means “biggest”. Also, you can improve
the running time to O(n2 ) using a different recurrence.

�6��.�. Scheduling Classes

Figure �.�. The same classes sorted by �nish times and the greedy schedule.

G

S(S[1 .. n], F [1 .. n]):sort F and permute S to match
count 1
X [count] 1
for i
2 to nif S[i] > F [X [count]]
count count + 1
X [count]ireturn X [1 .. count]

Figure �.�. A greedy algorithm for �nding a maximum set of non-overlapping classes

To prove that G
S
actually computes the largest conflict-freeschedule, we use an exchange argument, similar to the one we used for tapesorting. We are not claiming that the greedy schedule is the only maximal
schedule; there could be others. (Compare Figures . and . !) All we canclaim is that at least one of the optimal schedules is the one produced by thegreedy algorithm.
Lemma �.�. At least one maximal conflict-free schedule includes the class thatfinishes first.
Proof: Let f be the class that finishes first. Suppose we have a maximal
conflict-free schedule X that does not include f . Let g be the first class in X tofinish. Since f finishes before g does, f cannot conflict with any class in the set
X \ {g}. Thus, the schedule X 0 = X [ { f } \ {g} is also conflict-free. Since X 0 has
the same size as X , it is also maximal.
É
To finish the proof, we call on our old friend induction.
�6��. G����� A���������

Theorem �.�. The greedy schedule is an optimal schedule.
Proof: Let f be the class that finishes first, and let A be the subset of classes thatstart after f finishes. The previous lemma implies that some optimal schedule
contains f , so the best schedule that contains f is an optimal schedule. The bestschedule that includes f must contain an optimal schedule for the classes that
do not conflict with f , that is, an optimal schedule for A. The greedy algorithmchooses f and then, by the inductive hypothesis, computes an optimal scheduleof classes from A.
É
The proof might be easier to understand if we unroll the induction slightly.
Proof: Let hg1 , g2 , . . . , g k i be the sequence of classes chosen by the greedy
algorithm, sorted by starting time. Suppose we have a maximal conflict-freeschedule
S = hg1 , g2 , . . . , g j 1 , c j , c j+1 , . . . , cm i,again sorted by starting time, where c j is different from the class g j chosen by
the greedy algorithm. (We could have j = 1, in which case this schedule starts
with a non-greedy choice c1 .) By construction, the jth greedy choice g j does
not conflict with any earlier class g1 , g2 , . . . , g j 1 , and because our schedule S isconflict-free, neither does c j . Moreover, g j has the earliest finish time among
all classes that don’t conflict with the earlier classes; in particular, g j finishes
before c j . It follows that g j does not conflict with any of the later classes
c j+1 , . . . , cm . Thus, the modified scheduleS 0 = hg1 , g2 , . . . , g j 1 , g j , c j+1 , . . . , cm i,

is also conflict-free. (This argument is a direct generalization of Lemma . ,which considers the case j = 1.)
By induction, it now follows that there is an optimal schedule hg1 , g2 , . . . , g k ,ck+1 , . . . , cm i that includes every class chosen by the greedy algorithm. But thisis impossible unless k = m; if some class ck+1 does not conflict with any of thefirst k greedy classes, then the greedy algorithm would choose more than kclasses!
É�.�

General Pattern

The basic structure of this correctness proof is exactly the same as for thetape-sorting problem: an inductive exchange argument.
• Assume that there is an optimal solution that is different from the greedy
solution.
• Find the “first” difference between the two solutions.
�6��.�. Huffman Codes• Argue that we can exchange the optimal choice for the greedy choice without
making the solution worse (although the exchange might not make it better).
This argument implies by induction that some optimal solution contains theentire greedy solution, and therefore equals the greedy solution. Sometimes, asin the scheduling problem, an additional step is required to show no optimal
solution strictly improves the greedy solution.

�.�

Huffman Codes

A binary code assigns a string of s and s to each character in the alphabet. A
binary code is prefix-free if no code is a prefix of any other. (Confusingly, prefixfree codes are also commonly called prefix codes.) -bit ASCII and Unicode’s
UTF- are both prefix-free binary codes. Morse code is a binary code withsymbols • and —, but it is not prefix-free, because the code for E (•) is a prefixof the codes for I (••), S (•••), and H (••••).
Any prefix-free binary code can be visualized as a binary tree with theencoded characters stored at the leaves. The code word for any symbol is givenby the path from the root to the corresponding leaf; 0 for left, 1 for right. Thus,
the length of any symbol’s codeword is the depth of the corresponding leaf inthe code tree. Although they are superficially similar, binary code trees are notbinary search trees; we don’t care at all about the order of symbols at the leaves.
Suppose we want to encode a message written in an n-character alphabet
so that the encoded message is as short as possible. Specifically, given an arrayof frequency counts f [1 .. n], we want to compute a prefix-free binary code thatminimizes the total encoded length of the message:nX
i=1

f [i] · depth(i).

This is exactly the same cost function we considered for optimizing binary
search trees, but the optimization problem is different, because code trees are
not required to keep the keys in any particular order.
For this reason, Morse code is arguably better described as a prefix-free ternary code, withthree symbols: •, —, and pause. Alternatively, Morse code can be considered a prefix-freebinary code, with one beat of sound/light/current/high voltage/smoke/gas (Ñ) and one beatof silence/darkness/ground/low voltage/air/liquid (É) as the two symbols. Then each “dit” isencoded as ÑÉ, each “dah” as ÑÑÑÉ, and each pause as ÉÉ. In standard Morse code, eachletter is followed by one pause, and each word is followed by two additional pauses; however,És at the end of the entire coded message are omitted. For example, the string “MORSE CODE” isunambiguously encoded as the following bit string:ÑÑÑÉÑÑÑÉÉÉÑÑÑÉÑÑÑÉÑÑÑÉÉÉÑÉÑÑÑÉÉÉÑÉÑÉÑÉÉÉÑÉÉÉÉÉÉÉÑÑÑÉÑÉÑÑÑÉÑÉÉÉÑÑÑÉÑÑÑÉÑÑÑÉÉÉÑÑÑÉÑÉÑÉÉÉÑ.

�6�

�. G����� A���������

In, as a PhD student at MIT, David Huffman developed the following
greedy algorithm to produce such an optimal code:H: Merge the two least frequent letters and recurse.

Huffman’s algorithm is best illustrated through an example. Suppose we wantto encode the following helpfully self-descriptive sentence, discovered by Lee
Sallows:
This sentence contains three a’s, three c’s, two d’s, twenty-six e’s, �ve f’s, threeg’s, eight h’s, thirteen i’s, two l’s, sixteen n’s, nine o’s, six r’s, twenty-seven s’s,
twenty-two t’s, two u’s, �ve v’s, eight w’s, four x’s, �ve y’s, and only one z.

To keep things simple, let’s ignore the forty-four spaces, nineteen apostrophes,
nineteen commas, three hyphens, and only one period, and encode only theletters, as though the message were written in scriptio continua:
THISSENTENCECONTAINSTHREEASTHREECSTWODSTWENTYSIXESFIVEFSTHREEGSEIGHTHSTHIRTEENISTWOLSSIXTEENNSNINEOSSIXRSTWENTYSEVENSSTWENTYTWOTSTWOUSFIVEVSEIGHTWSFOURXSFIVEYSANDONLYONEZ

Here is the frequency table for Sallows’ sentence:A C D E F G H I L N O R S T U V W X Y Z
3 3 2 26 5 3 8 13 2 16 9 6 27 22 2 5 8 4 5 1
Huffman’s algorithm picks out the two least frequent letters, breaking ties
arbitrarily—in this case, say, Z and D—and merges them together into a single
Huffman was a student in an information theory class taught by Robert Fano, who wasa close colleague of Claude Shannon, the father of information theory. Fano and Shannon
had previously developed a different greedy algorithm for producing prefix codes—split thefrequency array into two subarrays as evenly as possible, and then recursively build a code foreach subarray—but these Fano-Shannon codes were known not to be optimal. Fano posed the
problem of finding an optimal prefix code to his class. Huffman decided to solve the problem asa class project, instead of taking a final exam, not realizing that the problem was open, or that
Fano and Shannon had already tried and failed to solve it. After several months of fruitless effort,
Huffman eventually gave up and decided to take the final exam after all. As he was throwing hisnotes in the trash, the solution dawned on him. Huffman would later describe the epiphany as“the absolute lightning of sudden realization”.
This sentence was first reported by Alexander Dewdney in his October“Computer
Recreations” column in Scientific American. Sallows himself published the remarkable story ofits discovery in, along with several other self-descriptive sentences; you can find Sallows’
paper on his web site. Frustrated with the slow progress of his code running on a VAX /,Sallows designed and built dedicated hardware to perform a brute-force search for self-descriptive
sentences with various flavor text (“This pangram has. . . ”, “This sentence contains exactly. . . ”,
and so on). Careful theoretical analysis limited the search space to just over six billion possibilities,
which his -MHz Pangram Machine enumerated in just under two hours.
. . . and he talked for forty-five minutes, and nobody understood a word that he said, but we
had fun fillin’ out the forms and playin’ with the pencils on the bench there.

�66

�.�. Huffman Codes

new character DZ with frequency 3. This new character becomes an internal
node in the code tree we are constructing, with Z and D as its children; it
doesn’t matter which child is which. The algorithm then recursively constructs
a Huffman code for the new frequency tableA C E F G H I L N O R S T U V W X Y DZ
3 3 26 5 3 8 13 2 16 9 6 27 22 2 5 8 4 5 3After merges, allletters have been merged together. The record of merges
gives us our code tree. The algorithm makes a number of arbitrary choices; as aresult, there are actually several different Huffman codes. One such Huffman
code is shown below; numbers in non-leaf nodes are frequencies for merged
characters. For example, the code for A is 101000, and the code for S is 111.
170
70

100

38

32

17
O

8
X

F

L

H

11
V

5

Y

5

A

I

6
C

3

S

27

13

6

6

2

E

26

12

8

R

5

53
25

22

W

8

U

2

T

16

16

10

9

4

4

N

21

47

G

3

3

3

D

Z

2

1

Encoding Sallows’ sentence with this particular Huffman code would yield a
bit string that starts like so:
100 0110 1011 111 111 110 010 100 110 010 101001 110 101001 0001 010 100···
T H
I S S E N T E N
C
E
C
O N T

Here is the list of costs for encoding each character in Sallows’ sentence, alongwith that character’s contribution to the total length of the encoded sentence:
char

A

C

D

E

F

G

H

I

L

N

O

R

S

T

U

V

W

X

Y

Z

freq

3

3

2

26

5

3

8

13

2

16

9

6

27 22

2

5

8

4

5

1

depth

6

6

7

3

5

6

4

4

6

3

4

5

3

6

5

4

5

5

7

total

18 18 14 78 25 18 32 52 12 48 36 30 81 66 12 25 32 20 25

7

3

Altogether, the encoded message isbits long. Different Huffman codes
encode the same characters differently, possibly with code words of different
length, but the overall length of the encoded message is the same for every
Huffman code:
bits.
�6�

�. G����� A���������Given the simple structure of Huffman’s algorithm, it’s rather surprising thatit produces an optimal prefix-free binary code. Encoding Sallows’ sentence
using any prefix-free code requires at least
bits! Fortunately, the recursive
structure makes this claim easy to prove using an exchange argument, similar
to our earlier optimality proofs. We start by proving that the algorithm’s very
first choice is correct.
Lemma �.�. Let x and y be the two least frequent characters (breaking ties
between equally frequent characters arbitrarily). There is an optimal code tree
in which x and y are siblings.
Proof: I’ll actually prove a stronger statement: There is an optimal code inwhich x and y are siblings and have the largest depth of any leaf.
Let T be an optimal code tree, and suppose this tree has depth d. Because T
is a full binary tree, it has at least two leaves at depth d that are siblings. (Verify
this claim by induction!) Suppose those two leaves are not x and y, but someother characters a and b.
Let T 0 be the code tree obtained by swapping x and a, and let=
d depth T (x). This swap increases the depth of x by and decreases the depthof a by , socost(T 0 ) = cost(T ) + · ( f [x] f [a]).
Our assumption that x is one of the two least frequent characters but a is not
implies f [x]  f [a], and our assumption that a has maximum depth implies
0. It follows that cost(T 0 )  cost(T ). On the other hand, T is an optimalcode tree, so we must also have cost(T 0 ) cost(T ). We conclude that T 0 is also
an optimal code tree.
Similarly, swapping y and b must give yet another optimal code tree. In thisfinal optimal code tree, x and y are maximum-depth siblings, as required. É

Now optimality is guaranteed by our dear friend the Recursion Fairy! Our
recursive argument relies on the following non-standard recursive definition:
A full binary tree is either a single node, or a full binary tree where some leaf has
been replaced by an internal node with two leaf children.
Theorem �.�. Every Huffman code is an optimal prefix-free binary code.
Proof: If the message has only one or two distinct characters, the theorem istrivial, so assume otherwise.
Let f [1 .. n] be the original input frequencies, and assume without loss ofgenerality that f [1] and f [2] are the two smallest frequencies. To set up theIt was certainly surprising to both Huffman and Fano!

�68�.�. Huffman Codes

recursive subproblem, define f [n + 1] = f [1] + f [2]. Our earlier exchange
argument implies that 1 and 2 are (deepest) siblings in some optimal code forf [1 .. n].
Let T 0 be the Huffman tree for f [3 .. n + 1]; the inductive hypothesis implies
that T 0 is an optimal code tree for the smaller set of frequencies. To obtainthe final code tree T , we replace the leaf labeled n + 1 with an internal nodewith two children, labelled 1 and 2. I claim that T is optimal for the original
frequency array f [1 .. n].
To prove this claim, we can express the cost of T in terms of the cost of T 0
as follows. (In these equations, depth(i) denotes the depth of the leaf labelled i
in either T or T 0 ; each leaf that appears in both T and T 0 has the same depth inboth trees.)cost(T ) =
=

n
X
i=1
n+1
X
i=3

f [i] · depth(i)f [i] · depth(i) + f [1] · depth(1) + f [2] · depth(2)f [n + 1] · depth(n + 1)

= cost(T 0 ) + ( f [1] + f [2]) · depth(T )0f [n + 1] · (depth(T )= cost(T ) + f [1] + f [2] + ( f [1] + f [2]= cost(T 0 ) + f [1] + f [2]1)

f [n + 1]) · (depth(T )1)This equation implies that minimizing the cost of T is equivalent to minimizing
the cost of T 0 ; in particular, attaching leaves labeled 1 and 2 to the leaf in T 0labeled n + 1 gives an optimal code tree for the original frequencies.
É
To efficiently construct a Huffman code, we keep the characters in a priority
queue, using the character frequencies as priorities. We can represent the code
tree as three arrays of indices, listing the Left and Right children and the Parentof each node. The leaves of the final code tree are nodes at indices 1 through n,
and the root is the node with index 2n 1. Pseudocode for the algorithm isshown in Figure . . B
Hperforms O(n) priority-queue operations:exactly 2n 1 I
s and 2n 2 E
M s. If we implement the priority
queue as a standard binary heap, each of these operations requires O(log n)time, and thus the entire algorithm runs in O(n log n) time.
Finally, simple algorithms to encode and decode messages using a fixedHuffman code are shown in Figure . ; both algorithms run in O(m) time,
where m is the length of the encoded message.

�6��. G����� A���������

BH( f [1 .. n]):for i
1 to n
L[i] 0; R[i] 0I(i, f [i])for in to 2n 1
x
E
M ()
y
E
M ()
f [i]
f [x] + f [ y]I(i, f [i])L[i]x; P[x]iR[i]y; P[ y]iP[2n 1] 0

hh�nd two rarest symbolsii
hhmerge into a new symbolii
hhupdate tree pointersii

Figure �.�. Building a Huffman code.

H

E(A[1 .. k]):
m 1
for i
1 to kHE
O (A[i])H

E
O (x):
if x < 2n 1
HEO
if x = L[P[x]]B[m] 0else
B[m] 1mm+1(P[x])

H

D(B[1 .. m]):
k
1
v
2n 1
for i
1 to mif B[i] = 0
v
L[v]
else
v
R[v]
if L[v] = 0
A[k]
v
k
k+1
v
2n 1

Figure �.�. Encoding and decoding algorithms for Huffman codes

�.�

Stable Matching

Every year, thousands of new doctors must obtain internships at hospitals aroundthe United States. During the first half of the th century, competition amonghospitals for the best doctors led to earlier and earlier offers of internships,
sometimes as early as the second year of medical school, along with tighter
deadlines for acceptance. In thes, medical schools agreed not to release
information until a common date during their students’ fourth year. In response,
hospitals began demanding faster decisions. By, hospitals would regularly
call doctors, offer them internships, and demand immediate responses. Interns
were forced to gamble if their third-choice hospital called first—accept and risk
losing a better opportunity later, or reject and risk having no position at all.
The American academic job market involves similar gambles, at least in computer science.
Some departments start making offers in February with two-week decision deadlines; otherdepartments don’t even start interviewing until March; MIT notoriously waits until May, when all����.�. Stable Matching

Finally, a central clearinghouse for internship assignments, now called the
National Resident Matching Program (NRMP), was established in the earlys.
Each year, doctors submit a ranked list of all hospitals where they would accept
an internship, and each hospital submits a ranked list of doctors they would
accept as interns. The NRMP then computes an matching between doctors
and hospitals that satisfies the following stability requirement. A matching isunstable if there is a doctor ↵ and hospital B that would be both happier witheach other than with their current match; that is,• ↵ is matched with some other hospital A, even though she prefers B.
• B is matched with some other doctor , even though they prefer ↵.
In this case, we call (↵, B) an unstable pair for the matching. The goal of the
Resident Match is a stable matching, which is a matching with no unstablepairs.
For simplicity, I’ll assume from now on that there are exactly the same
number of doctors and hospitals; each hospital offers exactly one internship;each doctor ranks all hospitals and vice versa; and finally, there are no ties inthe doctors’ or hospitals’ rankings.

Some Bad Ideas
At first glance, it is not even clear that a stable matching always exists! Certainly
not every matching of doctors and hospitals is stable. Suppose there are three
doctors (Dr. Quincy, Dr. Rotwang, Dr. Shephard, represented by lower-caseletters) and three hospitals (Arkham Asylum, Bethlem Royal Hospital, andCounty General Hospital, represented by upper-case letters), who rank eachother as follows:
q r s
A B C
A C A
r s q
C A B
q q r
B B C
s r s
The matching {Aq, Br, Cs} is unstable, because Arkham would rather hire
Dr. Rotwang than Dr. Quincy, and Dr. Rotwang would rather work at Arkham
than at Bedlam. (A, r) is an unstable pair for this matching.
its interviews are over, before making any faculty offers. Needless to say, the mishmash of offer
dates and decision deadlines causes tremendous stress, for candidates and departments alike.
For similar reasons, since, most American universities have agreed to a common April
deadline for prospective graduate students to accept offers of financial support (and by extension,offers of admission).
In reality, most hospitals offer multiple internships, each doctor ranks only a subset of thehospitals and vice versa, and there are typically more internships than interested doctors. And
then it starts getting complicated.

����. G����� A���������

One might imagine using an incremental algorithm that starts with anarbitrary matching, and then greedily performs exchanges to resolve instabilities.
Unfortunately, resolving one instability can create new ones; in fact, thisincremental “improvement” can lead to an infinite loop. For example, if we startwith our earlier unstable matching {Aq, Br, Cs}, each of the following exchanges
resolves one unstable pair (indicated over the arrow), but the sequence of
exchanges leads back to the original matching:
Ar

Cq

Cr

Aq

{Aq, Br, Cs} ! {Ar, Bq, Cs} ! {As, Bq, C r} ! {As, Br, Cq} ! {Aq, Br, Cs}

Alternatively, we might try the following multi-round greedy protocol. In
each round, every unmatched hospital makes an offer to their favorite unmatched
doctor, then every unmatched doctor with an offer accepts their favorite offer.
It’s not hard to prove that at least one new doctor-hospital pair is matched ineach round, so the algorithm always ends with a matching. For the previous
example input, we already have a stable matching {Ar, Bs, Cq} at the end of thefirst round! But consider the following input instead:
q
C
B
A

r
A
C
B

s
A
B
C

A B
q q
s r
r s

C
s
r
q

In the first round, Dr. Shephard accepts an offer from County, and Dr. Quincy
accepts an offer from Bedlam (rejecting Arkham’s offer), leaving only Dr.
Rotwang and Arkham unmatched. Thus, the protocol ends with the matching{Ar, Bq, Cs} after two rounds. Unfortunately, this matching is unstable; Arkham
and Dr. Shephard prefer each other to their matches.

The Boston Pool and Gale-Shapley Algorithms
In, the NRMP adopted the “Boston Pool” algorithm to assign interns, so
named because it had been previously used by a regional clearinghouse in the
Boston area. Ten years later, David Gale and Lloyd Shapley described andformally analyzed a generalization of the Boston Pool algorithm and proved thatit computes a stable matching. Gale and Shapley used the metaphor of college
admissions. Essentially the same algorithm was independently developed by
Elliott Peranson infor use in medical school admissions. Similar algorithms
have since been adopted for many other matching markets, including faculty
hiring in France, hiring of new economics PhDs in the United States, university
admission in Germany, public school admission in New York and Boston, billet
assignments for US Navy sailors, and kidney-matching programs.
This example was discovered by Donald Knuth.

����.�. Stable Matching

Shapley was awarded theNobel Prize in Economics for his research on
stable matchings, together with Alvin Roth, who significantly extended Shapley’s
work and used it to develop several real-world exchanges. (Gale did not share
the prize, because he died in.)
Like our last failed greedy algorithm, the Gale-Shapley algorithm proceeds
in rounds until every position has been accepted. Each round has two stages:. An arbitrary unmatched hospital A offers its position to the best doctor ↵(according to A’s preference list) who has not already rejected it.
. If ↵ is unmatched, she (tentatively) accepts A’s offer. If ↵ already has
a match but prefers A, she rejects her current match and (tentatively)
accepts the new offer from A. Otherwise, ↵ rejects the new offer.
Each doctor ultimately accepts the best offer that she receives, according to her
preference list. In short, hospitals make offers greedily, and doctors accept
offers greedily. The doctors’ ability to reject their current matches in favor of
better offers is the key to making this mutual greedy strategy work.
For example, suppose that there are four doctors (Dr. Quincy, Dr. Rotwang,
Dr. Shephard, and Dr. Tam) and four hospitals (Arkham Asylum, Bethlem Royal
Hospital, County General Hospital, and The Dharma Initiative), who rank eachother as follows:q r s tA B C DA A B Dt r t sB D A Bs t r rC C C Cr q s qD B D Aq s q tGiven these preference lists as input, the Gale-Shapley algorithm might proceed
as follows:. Arkham makes an offer to Dr. Tam.
. Bedlam makes an offer to Dr. Rotwang.
. County makes an offer to Dr. Tam, who rejects her earlier offer from
Arkham.
. Dharma makes an offer to Dr. Shephard. (From this point on, there is
only one unmatched hospital, so the algorithm has no more choices.). Arkham makes an offer to Dr. Shephard, who rejects her earlier offerfrom Dharma.
The
Boston Pool algorithm is a special case of the Gale-Shapley algorithm that executes
offers in a particular order. Roughly speaking, each offer is made by a hospital X whose favorite
doctor (among those who haven’t rejected X already) ranks X highest. Because the order ofoffers depends on the entire set of preference lists, this algorithm must be executed by a central
authority; in contrast, the Gale-Shapley algorithm does not even require each participant to know
their own preferences in advance, as long as they behave consistently with some fixed rankings.

����. G����� A���������

. Dharma makes an offer to Dr. Rotwang, who rejects her earlier offer from
Bedlam.
. Bedlam makes an offer to Dr. Tam, who rejects her earlier offer fromCounty.
. County makes an offer to Dr. Rotwang, who rejects it.
. County makes an offer to Dr. Shephard, who rejects it.
. County makes an offer to Dr. Quincy.
After the tenth round, all pending offers are accepted, and the algorithm returns
the matching {As, B t, Cq, Dr}. You can (and should) verify by brute force that
this matching is stable, even though no doctor was hired by her favorite hospital,and no hospital hired their favorite doctor; in fact, County ended up hiring theirleast favorite doctor. This is not the only stable matching for these preference
lists; the matching {Ar, Bs, Cq, Dt} is also stable.

Running Time
Analyzing the number of offers performed by the algorithm is relatively straightforward (which is why we’re doing it first). Each hospital makes an offer toeach doctor at most once, so the algorithm makes at most n2 offers.
To analyze the actual running time, however, we need to specify the algorithm
in more detail. How are the preference lists given to the algorithm? How does
the algorithm decide whether any hospital is unmatched, and if so, how does
it find an unmatched hospital? How does the algorithm store the tentative
matchings? How does the algorithm decide whether a doctor prefers her new
offer to her current match? Most fundamentally: How does the algorithm
actually represent doctors and hospitals?
One possibility is to represent each doctor and hospital by a unique integer
between 1 and n, and to represent preferences as two arrays Dpref[1 .. n, 1 .. n]and Hpref[1 .. n, 1 .. n], where Dpref[i, r] represents the rth hospital in doctor
i’s preference list, and HPref[ j, r] represents the rth doctor in hospital j’s
preference list. With the input in this form, the Boston Pool algorithm can
execute each offer in constant time, after some initial preprocessing; the overall
implementation runs in O(n 2 ) time. We leave the remaining details as astraightforward exercise.
A somewhat harder exercise is to prove that there are inputs (and choices of
who makes offers when) that force ⌦(n2 ) offers to be made before the algorithm
halts. Thus, our O(n2 ) upper bound on the worst-case running time is tight.

Correctness
But why is the algorithm correct at all? How do we know that it always computes
a stable matching, or any complete matching for that matter?
����.�. Stable Matching

Once a doctor receives an offer, she has at least a tentative match for the
rest of time. Equivalently, if any doctor is unmatched, then no hospital has
offered that doctor a job, which implies that the hospitals have not exhausted
their preference lists. It follows that when the algorithm terminates (after at
most n2 rounds), every doctor is matched, and therefore every position is filled.
In other words, the algorithm always computes a perfect matching between
doctors and hospitals. (Whew!) It remains only to prove that the resulting
matching is stable.
Suppose the algorithm matches some doctor ↵ to some hospital A, eventhough she prefers another hospital B. Because every doctor accepts the best
offer she receives, ↵ received no offer she liked more than A; in particular, B
never made an offer to ↵. On the other hand, B made offers to every doctor
they prefer over their final match . It follows that B prefers over ↵, whichmeans (↵, B) is not an unstable pair. We conclude that there are no unstablepairs; the matching is stable!

Optimality!
Surprisingly, the correctness of the Gale-Shapley algorithm does not depend
on which hospital makes its offer in each round. In fact, no matter whichunassigned hospital makes an offer in each round, the algorithm always computes
the same matching! Let’s say that ↵ is a feasible doctor for A if there is a stablematching that assigns doctor ↵ to hospital A.
Lemma �.�. During the Gale-Shapley algorithm, each hospital A is rejected
only by doctors that are infeasible for A.
Proof: We prove the lemma by induction on the number of rounds. Consider
an arbitrary round of the algorithm, in which doctor ↵ rejects one hospital A
for another hospital B. The rejection implies that ↵ prefers B to A. Each doctorthat appears higher than ↵ in B’s preference list already rejected B in an earlier
round and therefore, by the inductive hypothesis, is infeasible for B.
Now consider an arbitrary matching (of the same doctors and hospitals)
that assigns ↵ to A. We already established that ↵ prefers B to A. If B prefers ↵to its partner, the matching is unstable. On the other hand, if B prefers its
partner to ↵, then (by our earlier argument) its partner is infeasible, and again
the matching is unstable. We conclude that there is no stable matching thatassigns ↵ to A.
É
Now let best(A) denote the highest-ranked feasible doctor on A’s preference
list. Lemma . implies that every doctor that A prefers to its final match isinfeasible for A. On the other hand, the final matching is stable, so the doctor
assigned to A must be feasible for A. The following result is now immediate:����. G����� A���������

Corollary �.�. The Gale-Shapley algorithm matches best(A) with A, for every
hospital A.
In other words, the Gale-Shapley algorithm computes the best possible stablematching from the hospitals’ point of view. It turns out that this matching is
also the worst possible from the doctors’ point of view! Let worst(↵) denote thelowest-ranked feasible hospital on doctor ↵’s preference list.
Corollary �.�. The Gale-Shapley algorithm matches ↵ with worst(↵), for every
doctor ↵.
Proof: Suppose Gale and Shapley assign doctor ↵ to hospital A; we need toshow that A = worst(↵). Consider an arbitrary stable matching where A is notmatched with ↵ but with another doctor . The previous corollary implies that
A prefers ↵ = best(A) to . Because the matching is stable, ↵ must therefore
prefer her assigned hospital to A. This argument works for any stable matching,so ↵ prefers every other feasible match to A; in other words, A = worst(↵). É
A subtle consequence of these two corollaries, discovered by Lester Dubins
and David Freedman in, is that a doctor can potentially improve her matchby lying about her preferences, but a hospital cannot. (However, a set of hospitals
can collude so that some of their matches improve.) Partly for this reason, theNational Residency Matching Program reversed its matching algorithm in, so that potential residents offer to work for hospitals, according to theirpreference orders, and each hospital accepts its best offer. Thus, the newalgorithm computes the best possible stable matching for the doctors, and theworst possible stable matching for the hospitals. In practice, however, thisreversal altered less than % of the residents’ matches. As far as I know, theprecise effect of this change on the patients is an open problem.

Exercises
Caveat lector: Some of these exercises cannot be solved using greedy
algorithms! Whenever you describe and analyze a greedy algorithm, you mustalso include a proof that your algorithm is correct; this proof will typically takethe form of an exchange argument. These proofs are especially important inclasses (like mine) that do not normally require proofs of correctness.
. The GS
algorithm we described for the class schedulingproblem is not the only greedy strategy we could have tried. For each of
the following alternative greedy strategies, either prove that the resulting
algorithm always constructs an optimal schedule, or describe a small input
��6

Exercises

example for which the algorithm does not produce an optimal schedule.
Assume that all algorithms break ties arbitrarily (that is, in a manner thatis completely out of your control). [Hint: Three of these algorithms are
actually correct.](a) Choose the course x that ends last, discard classes that conflict with x,
and recurse.
(b) Choose the course x that starts first, discard all classes that conflictwith x, and recurse.
(c) Choose the course x that starts last, discard all classes that conflictwith x, and recurse.
(d) Choose the course x with shortest duration, discard all classes thatconflict with x, and recurse.
(e) Choose a course x that conflicts with the fewest other courses, discard allclasses that conflict with x, and recurse.
(f) If no classes conflict, choose them all. Otherwise, discard the coursewith longest duration and recurse.
(g) If no classes conflict, choose them all. Otherwise, discard a course thatconflicts with the most other courses and recurse.
(h) Let x be the class with the earliest start time, and let y be the class withthe second earliest start time.
• If x and y are disjoint, choose x and recurse on everything but x.
• If x completely contains y, discard x and recurse.
• Otherwise, discard y and recurse.
(i) If any course x completely contains another course, discard x andrecurse. Otherwise, choose the course y that ends last, discard all classes
that conflict with y, and recurse.
. Now consider a weighted version of the class scheduling problem, where
different classes offer different number of credit hours (totally unrelated
to the duration of the class lectures). Your goal is now to choose a set of
non-conflicting classes that give you the largest possible number of credit
hours, given arrays of start times, end times, and credit hours as input.
(a) Prove that the greedy algorithm described at the beginning of thischapter—Choose the class that ends first and recurse—does not always
return an optimal schedule.
(b) Prove that none of the greedy algorithms described in Exercise always
return an optimal schedule. [Hint: Solve Exercise first; the algorithms
that don’t work there don’t work here, either.]����. G����� A���������

(c) Describe and analyze an algorithm that always computes an optimalschedule. [Hint: Your algorithm will not be greedy.]. Let X be a set of n intervals on the real line. We say that a subset of intervals
Y ✓ X covers X if the union of all intervals in Y is equal to the union of allintervals in X . The size of a cover is just the number of intervals.
Describe and analyze an efficient algorithm to compute the smallest
cover of X . Assume that your input consists of two arrays L[1 .. n] andR[1 .. n], representing the left and right endpoints of the intervals in X . If
you use a greedy algorithm, you must prove that it is correct.

A set of intervals, with a cover (shaded) of size 7.

. Let X be a set of n intervals on the real line. We say that a set P of points
stabs X if every interval in X contains at least one point in P. Describe andanalyze an efficient algorithm to compute the smallest set of points thatstabs X . Assume that your input consists of two arrays L[1 .. n] and R[1 .. n],
representing the left and right endpoints of the intervals in X . As usual, If
you use a greedy algorithm, you must prove that it is correct.
1
1

2

3

4
3

2
1

3

1

4

2
1

3

2

4

A set of intervals stabbed by four points (shown here as vertical segments). Let X be a set of n intervals on the real line. A proper coloring of X assigns acolor to each interval, so that any two overlapping intervals are assigned
different colors. Describe and analyze an efficient algorithm to compute theminimum number of colors needed to properly color X . Assume that yourinput consists of two arrays L[1 .. n] and R[1 .. n], representing the left andright endpoints of the intervals in X . As usual, if you use a greedy algorithm,
you must prove that it is correct.
1
2

2

3

1

4
5

4

4

5

3

5
3

3

1
2

A proper coloring of a set of intervals using �ve colors.

��8

Exercises. (a) For every integer n, find a frequency array f [1 .. n] whose Huffman
code tree has depth n 1, such that the largest frequency is as small as
possible.
(b) Suppose the total length N of the unencoded message is bounded by apolynomial in the alphabet size n. Prove that the any Huffman tree forthe frequencies f [1 .. n] has depth O(log n).
™

. Call a frequency array f [1 .. n] ↵-heavy if it satisfies two conditions:• f [1] > f [i] for all i > 1; that is, 1 is the unique most frequent symbol.
Pn• f [1] ↵ i=1 f [i]; that is, at least an ↵ fraction of the symbols are 1s.
Find the largest real number ↵ such that in every Huffman code for every↵-heavy frequency array, symbol 1 is represented by a single bit. [Hint:
First prove that 1/3  ↵  1/2.]. Describe and analyze an algorithm to compute an optimal ternary prefix-freecode for a given array of frequencies f [1 .. n]. Don’t forget to prove thatyour algorithm is correct for all n.
. Describe in detail how to implement the Gale-Shapley stable matchingalgorithm, so that the worst-case running time is O(n2 ), as claimed earlier
in this chapter.
. (a) Prove that it is possible for the Gale-Shapley algorithm to perform ⌦(n2 )offers before termination. (You need to describe both a suitable input
and a sequence of ⌦(n2 ) valid offers.)(b) Describe for any integer n a set of preferences for n doctors and nhospitals that forces the Gale-Shapley algorithm to execute ⌦(n2 ) rounds,no matter which valid proposal is made in each round. [Hint: Part (b)implies part (a).]. Describe and analyze an efficient algorithm to determine whether a given
set of hospital and doctor preferences has to a unique stable matching.
. Consider a generalization of the stable matching problem, where some
doctors do not rank all hospitals and some hospitals do not rank all doctors,
and a doctor can be assigned to a hospital only if each appears in the other’s
preference list. In this case, there are three additional unstable situations:• A matched hospital prefers an unmatched doctor to its assigned match.
• A matched doctor prefers an unmatched hospital to her assigned match.
• An unmatched doctor and an unmatched hospital appear in each other’s
preference lists.
����. G����� A���������

A stable matching in this setting may leave some doctors and/or hospitals
unmatched, even though their preference lists are non-empty. For example,
if every doctor lists Harvard as their only acceptable hospital, and every
hospital lists Dr. House as their only acceptable intern, then only House andHarvard will be matched.
Describe and analyze an efficient algorithm that computes a stablematching in this more general setting. [Hint: Reduce to an instance
where every doctor ranks every hospital and vice versa, and then invoke
Gale-Shapley.]. The Scandinavian furniture company Fürni has hired n drivers to deliver n
identical orders to n different addresses in Wilmington, Delaware. Eachdriver has their own well-established delivery route through Wilmington
that visits all n addresses. Assuming they follow their routes as they always
do, two drivers never visit the same addresses at the same time.
In principle, each of the n drivers can deliver their furniture to any of
the n addresses, but there’s a complication. One of the drivers has secretly
wired proximity sensors and explosives to the Johannshamn sofas (with theStrinne green stripe pattern). If two sofas are ever at the same address atthe same time, both will explode, destroying both the delivery truck andthe building at that address. This can only happen if one driver delivers anorder to that address, and then later another driver visits that same address
while the furniture is still on their truck.
Your job as the Fürni dispatcher is to assign each driver to a delivery
address. Describe an algorithm to assign addresses to drivers so that each of
the n addresses receives their furniture order and there are no explosions.
For example, suppose Jack’s route visits
Paper Street at pm andFranklin Street at pm, and Marla’s route visits
Paper at pm andFranklin at pm. Then Jack should deliver toFranklin, and Marlashould deliver toPaper; otherwise, there would be an explosion at
Franklin at pm. (Cue the Pixies.) [Hint: Jack and Marla are a bit unstable.]. Suppose you are a simple shopkeeper living in a country with n different
types of coins, with values 1 = c[1] < c[2] < · · · < c[n]. (In the U.S.,
for example, n = 6 and the values are 1, 5, 10, 25, 50 and 100 cents.)
Your beloved and benevolent dictator, El Generalissimo, has decreed thatwhenever you give a customer change, you must use the smallest possible
number of coins, so as not to wear out the image of El Generalissimo lovingly
engraved on each coin by servants of the Royal Treasury.
(a) In the United States, there is a simple greedy algorithm that always
results in the smallest number of coins: subtract the largest coin and�8�

Exercises

recursively give change for the remainder. El Generalissimo does not
approve of American capitalist greed. Show that there is a set of coinvalues for which the greedy algorithm does not always give the smallest
possible of coins.
(b) Now suppose El Generalissimo decides to impose a currency system
where the coin denominations are consecutive powers b0 , b1 , b2 , . . . , b kof some integer b 2. Prove that despite El Generalissimo’s disapproval,
the greedy algorithm described in part (a) does make optimal change inthis currency system.
(c) Describe and analyze an efficient algorithm to determine, given a target
amount T and a sorted array c[1 .. n] of coin denominations, the smallest
number of coins needed to make T cents in change. Assume thatc[1] = 1, so that it is possible to make change for any amount T .
. Suppose you are given an array A[1 .. n] of integers, each of which may bepositive, negative, or zero. A contiguous subarray A[i .. j] is called a positive
interval if the sum of its entries is greater than zero. Describe and analyze
an algorithm to compute the minimum number of positive intervals thatcover every positive entry in A. For example, given the following array as
input, your algorithm should output 3. If every entry in the input array isnegative, your algorithm should output 0.
z
+3

sum=2

}|
5 +7

{
4 +1

sum=1

z
}|
{
8 +3 7 +5

sum=7

z
}|
{
9 +5 2 +4. Consider the following process. At all times you have a single positive
integer x, which is initially equal to 1. In each step, you can either
increment x or double x. Your goal is to produce a target value n. For
example, you can produce the integer 10 in four steps as follows:
+1

⇥2

+1

⇥2

1 ! 2 ! 4 ! 5 ! 10
Obviously you can produce any integer n using exactly n 1 increments, but
for almost all values of n, this is horribly inefficient. Describe and analyze
an algorithm to compute the minimum number of steps required to produce
any given integer n.
. Suppose we have n skiers with heights given in an array P[1 .. n], and n skis
with heights given in an array S[1 .. n]. Describe an efficient algorithm toassign a ski to each skier, so that the average difference between the heightof a skier and her assigned ski is as small as possible. The algorithm should
�8��. G����� A���������

compute a permutationsuch that the expression
1X
P[i]
n i=1
n

S[ (i)]

is as small as possible.
. Alice wants to throw a party and she is trying to decide who to invite. She
has n people to choose from, and she knows which pairs of these people
know each other. She wants to invite as many people as possible, subject totwo constraints:• For each guest, there should be at least five other guests that they alreadyknow.
• For each guest, there should be at least five other guests that they don’t
already know.
Describe and analyze an algorithm that computes the largest possible numberof guests Alice can invite, given a list of n people and the list of pairs who
know each other.
. Suppose we are given two arrays C[1 .. n] and R[1 .. n] of positive integers.
An n ⇥ n matrix of 0s and 1s agrees with R and C if, for every index i, theith row contains R[i] 1s, and the ith column contains C[i] 1s. Describe
and analyze an algorithm that either constructs a matrix that agrees with R
and C, or correctly reports that no such matrix exists.
. You’ve just accepted a job from Elon Musk, delivering burritos from San
Francisco to New York City. You get to drive a Burrito-Delivery Vehicle
through Elon’s new Transcontinental Underground Burrito-Delivery Tube,
which runs in a direct line between these two cities.
Your Burrito-Delivery Vehicle runs on single-use batteries, which must
be replaced after at mostmiles. The actual fuel is virtually free, but
the batteries are expensive and fragile, and therefore must be installed only
by official members of the Transcontinental Underground Burrito-Delivery
Vehicle Battery-Replacement Technicians’ Union. Thus, even if you replace
your battery early, you must still pay full price for each new battery to beinstalled. Moreover, your Vehicle is too small to carry more than one battery
at a time.
. . . and which was clearly modeled after Maciej Ceg owski’s fictional “Alameda-Weehauken
Burrito Tunnel”
or as they call themselves in German, Die Transkontinentaluntergrundburritolieferfahrzeugbatteriewechseltechnikervereinigung.

�8�

Exercises

There are several fueling stations along the Tube; each station charges adifferent price for installing a new battery. Before you start your trip, you
carefully print the Wikipedia page listing the locations and prices of every
fueling station along the Tube. Given this information, how do you decide
the best places to stop for fuel?
More formally, suppose you are given two arrays D[1 .. n] and C[1 .. n],
where D[i] is the distance from the start of the Tube to the ith station, andC[i] is the cost to replace your battery at the ith station. Assume that yourtrip starts and ends at fueling stations (so D[1] = 0 and D[n] is the total
length of your trip), and that your car starts with an empty battery (so you
must install a new battery at station 1).
(a) Describe and analyze a greedy algorithm to find the minimum numberof refueling stops needed to complete your trip. Don’t forget to provethat your algorithm is correct.
(b) But what you really want to minimize is the total cost of travel. Show
that your greedy algorithm in part (a) does not produce an optimal
solution when extended to this setting.
(c) Describe an efficient algorithm to compute the locations of the fuelstations you should stop at to minimize the total cost of travel.
. You’ve been hired to store a sequence of n books on shelves in a library. The
order of the books is fixed by the cataloging system and cannot be changed;
each shelf must store a contiguous interval of the given sequence of books.
You are given two arrays H[1 .. n] and T [1 .. n], where H[i] and T [i] arerespectively the height and thickness of the ith book in the sequence. All
shelves in this library have the same length L; the total thickness of all books
on any single shelf cannot exceed L.
(a) Suppose all the books have the same height h and the shelves have heightlarger than h, so every book fits on every shelf. Describe and analyze agreedy algorithm to store the books in as few shelves as possible. [Hint:
The algorithm is obvious, but why is it correct?](b) That was a nice warmup, but now here’s the real problem. In fact thebooks have different heights, but you can adjust the height of each shelf
to match the tallest book on that shelf. (In particular, you can change
the height of any empty shelf to zero.) Now your task is to store thebooks so that the sum of the heights of the shelves is as small as possible.
Show that your greedy algorithm from part (a) does not always give thebest solution to this problem.
(c) Describe and analyze an algorithm to find the best matching between
books and shelves as described in part (b).
�8��. G����� A���������

. A string w of parentheses ( and ) is balanced if it satisfies one of thefollowing conditions:• w is the empty string.
• w = ( x ) for some balanced string x• w = x y for some balanced strings x and y
For example, the stringw = ((())()()) (()()) ()is balanced, because w = x y, wherex = ( (()) () () )andy = ( () () ) ().

(a) Describe and analyze an algorithm to determine whether a given stringof parentheses is balanced.
(b) Describe and analyze a greedy algorithm to compute the length of alongest balanced subsequence of a given string of parentheses. As usual,
don’t forget to prove your algorithm is correct.
For both problems, your input is an array w[1 .. n], where for each i, either
w[i] = ( or w[i] = ). Both of your algorithms should run in O(n) time.
. One day Alex got tired of climbing in a gym and decided to take a large
group of climber friends outside to climb. They went to a climbing areawith a huge wide boulder, not very tall, with several marked hand and footholds. Alex quickly determined an “allowed” set of moves that her group offriends can perform to get from one hold to another.
The overall system of holds can be described by a rooted tree T with nvertices, where each vertex corresponds to a hold and each edge corresponds
to an allowed move between holds. The climbing paths converge as they go
up the boulder, leading to a unique hold at the summit, represented by theroot of T .
Alex and her friends (who are all excellent climbers) decided to play agame, where as many climbers as possible are simultaneously on the boulder
and each climber needs to perform a sequence of exactly k moves. Each
climber can choose an arbitrary hold to start from, and all moves must moveaway from the ground. Thus, each climber traces out a path of k edges
in the tree T , all directed toward the root. However, no two climbers areallowed to touch the same hold; the paths followed by different climbers
cannot intersect at all.
(a) Describe and analyze a greedy algorithm to compute the maximum
number of climbers that can play this game. Your algorithm is given
�8�

Exercisesa rooted tree T and an integer k as input, and it should compute thelargest possible number of disjoint paths in T , where each path has
length k. Do not assume that T is a binary tree. For example, given thetree below as input, your algorithm should return the integer 8.

Figure �.6. Seven disjoint paths of length k = 3. This is not the largest such set of paths in this tree.

(b) Now suppose each vertex in T has an associated reward, and your goal
is to maximize the total reward of the vertices in your paths, instead of
the total number of paths. Show that your greedy algorithm does notalways return the optimal reward.
(c) Describe an efficient algorithm to compute the maximum possible reward,
as described in part (b).
. Congratulations! You have successfully conquered Camelot, transforming
the former battle-scarred hereditary monarchy into an anarcho-syndicalist
commune, where citizens take turns to act as a sort of executive-officerfor-the-week, but with all the decisions of that officer ratified at a special
bi-weekly meeting, by a simple majority in the case of purely internal affairs,
but by a two-thirds majority in the case of more major. . . .
As a final symbolic act, you order the Round Table (surprisingly, an
actual circular table) to be split into pizza-like wedges and distributed to thecitizens of Camelot as trophies. Each citizen has submitted a request for anangular wedge of the table, specified by two angles—for example: Sir Robinthe Brave might request the wedge from 17.23 to 42 , and Sir Lancelot
the Pure might request the 2 wedge from 359 to 1 . Each citizen will behappy if and only if they receive precisely the wedge that they requested.
Unfortunately, some of these ranges overlap, so satisfying all the citizens’
requests is simply impossible. Welcome to politics.
Describe and analyze an algorithm to find the maximum number ofrequests that can be satisfied. [Hint: The output of your algorithm should
not change if you rotate the table. Do not assume that angles are integers.]
�8��. G����� A���������

. Suppose you are standing in a field surrounded by several large balloons.
You want to use your brand new Acme Brand Zap-O-MaticTM to pop all theballoons, without moving from your current location. The Zap-O-MaticTM
shoots a high-powered laser beam, which pops all the balloons it hits. Since
each shot requires enough energy to power a small country for a year, youwant to fire as few shots as possible.
2
2
1

1

3

1

3

3

4
4

Figure �.�. Nine balloons popped by four shots of the Zap-O-MaticTM

The minimum zap problem can be stated more formally as follows. Givena set C of n circles in the plane, each specified by its radius and the (x, y)
coordinates of its center, compute the minimum number of rays from theorigin that intersect every circle in C. Your goal is to find an efficientalgorithm for this problem.
(a) Suppose it is possible to shoot a ray that does not intersect any balloons.
Describe and analyze a greedy algorithm that solves the minimum zap
problem in this special case. [Hint: See Exercise .](b) Describe and analyze a greedy algorithm whose output is within 1 of
optimal. That is, if m is the minimum number of rays required to hit
every balloon, then your greedy algorithm must output either m or m + 1.
(Of course, you must prove this fact.)(c) Describe an algorithm that solves the minimum zap problem in O(n2 )time.
™

(d) Describe an algorithm that solves the minimum zap problem in O(n log n)time.
Assume you have a subroutine that tells you the range of angles of rays thatintersects an arbitrary circle c in O(1) time. This subroutine is not difficult
to write, but it’s not the interesting part of the problem.

�86