Table Of ContentInstructor’s Manual
by ThomasH. Cormen
to Accompany
Introduction to Algorithms
Third Edition
by ThomasH. Cormen
Charles E. Leiserson
Ronald L. Rivest
Clifford Stein
TheMITPress
Cambridge,Massachusetts London, England
Instructor’sManualtoAccompanyIntroductiontoAlgorithms,ThirdEdition
byThomasH.Cormen,CharlesE.Leiserson,RonaldL.Rivest,andCliffordStein
Published by the MIT Press. Copyright c 2009 by The Massachusetts Institute of Technology. All rights
reserved.
Nopartofthispublicationmaybereproducedordistributedinanyformorbyanymeans,orstoredinadatabase
orretrievalsystem,withoutthepriorwrittenconsentofTheMITPress,including,butnotlimitedto,networkor
otherelectronicstorageortransmission,orbroadcastfordistancelearning.
Contents
RevisionHistory R-1
Preface P-1
Chapter2: GettingStarted
LectureNotes 2-1
Solutions 2-17
Chapter3: GrowthofFunctions
LectureNotes 3-1
Solutions 3-7
Chapter4: Divide-and-Conquer
LectureNotes 4-1
Solutions 4-17
Chapter5: ProbabilisticAnalysisandRandomizedAlgorithms
LectureNotes 5-1
Solutions 5-9
Chapter6: Heapsort
LectureNotes 6-1
Solutions 6-10
Chapter7: Quicksort
LectureNotes 7-1
Solutions 7-9
Chapter8: SortinginLinearTime
LectureNotes 8-1
Solutions 8-10
Chapter9: MediansandOrderStatistics
LectureNotes 9-1
Solutions 9-10
Chapter11: HashTables
LectureNotes 11-1
Solutions 11-16
Chapter12: BinarySearchTrees
LectureNotes 12-1
Solutions 12-15
Chapter13: Red-BlackTrees
LectureNotes 13-1
Solutions 13-13
Chapter14: AugmentingDataStructures
LectureNotes 14-1
Solutions 14-9
iv Contents
Chapter15: DynamicProgramming
LectureNotes 15-1
Solutions 15-21
Chapter16: GreedyAlgorithms
LectureNotes 16-1
Solutions 16-9
Chapter17: AmortizedAnalysis
LectureNotes 17-1
Solutions 17-14
Chapter21: DataStructuresforDisjointSets
LectureNotes 21-1
Solutions 21-6
Chapter22: ElementaryGraphAlgorithms
LectureNotes 22-1
Solutions 22-13
Chapter23: MinimumSpanningTrees
LectureNotes 23-1
Solutions 23-8
Chapter24: Single-SourceShortestPaths
LectureNotes 24-1
Solutions 24-13
Chapter25: All-PairsShortestPaths
LectureNotes 25-1
Solutions 25-9
Chapter26: MaximumFlow
LectureNotes 26-1
Solutions 26-12
Chapter27: MultithreadedAlgorithms
Solutions 27-1
Index I-1
Revision History
Revisionsarelistedbydateratherthanbeingnumbered.
22February2014. CorrectedanerrorinthesolutiontoExercise4.3-7,courtesy
ofDanSuthers. Corrected anerror inthesolution toExercise23.1-6, courtesy
ofRachelGinzberg. UpdatedthePreface.
3January 2012. Added solutions toChapter 27. Added analternative solution
to Exercise 2.3-7, courtesy of Viktor Korsun and Crystal Peng. Corrected a
minorerrorintheChapter15notesintherecurrence forT.n/fortherecursive
CUT-ROD procedure. Updated the solution to Problem 24-3. Corrected an
error intheproof about the Edmonds-Karp algorithm performing O.VE/flow
augmentations. Thebodiesofallpseudocode procedures areindented slightly.
28 January 2011. Corrected an error in the solution to Problem 2-4(c), and
removedunnecessary codeinthesolution toProblem2-4(d). Addedamissing
parameter to recursive calls of REC-MAT-MULT on page 4-7. Changed the
pseudocode for HEAP-EXTRACT-MAX on page 6-8 and MAX-HEAP-INSERT
onpage6-9toassumethattheparameternispassedbyreference.
7 May 2010. Changed the solutions to Exercises 22.2-3 and 22.3-4 because
theseexerciseschanged.
17February2010. Correctedaminorerrorinthesolution toExercise4.3-7.
16 December 2009. Added an alternative solution to Exercise 6.3-3, courtesy
ofEyalMashiach.
7December2009. AddedsolutionstoExercises16.3-1,26.1-1,26.1-3,26.1-7,
26.2-1,26.2-8, 26.2-9,26.2-12, 26.2-13,and26.4-1andtoProblem26-3. Cor-
rected spelling in the solution to Exercise 16.2-4. Several corrections to the
solution to Exercise 16.4-3, courtesy of Zhixiang Zhu. Minor changes to the
solutions toExercises24.3-3and24.4-7andProblem24-1.
7August2009. Initialrelease.
Preface
Thisdocumentisaninstructor’smanualtoaccompanyIntroduction toAlgorithms,
ThirdEdition,byThomasH.Cormen,CharlesE.Leiserson,RonaldL.Rivest,and
CliffordStein. Itisintendedforuseinacourseonalgorithms. Youmightalsofind
someofthematerialhereintobeusefulforaCS2-stylecourseindatastructures.
Unliketheinstructor’smanualforthefirsteditionofthetext—whichwasorganized
around the undergraduate algorithms course taught by Charles Leiserson at MIT
in Spring 1991—but like the instructor’s manual for the second edition, we have
chosen to organize the manual for the third edition according to chapters of the
text. Thatis,formostchapters wehaveprovided asetoflecturenotesandasetof
exercise andproblem solutions pertaining tothechapter. Thisorganization allows
youtodecidehowtobestusethematerialinthemanualinyourowncourse.
We have not included lecture notes and solutions for every chapter, nor have we
includedsolutionsforeveryexerciseandproblemwithinthechaptersthatwehave
selected. We felt that Chapter 1 is too nontechnical to include here, and Chap-
ter10consistsofbackground materialthatoftenfallsoutsidealgorithmsanddata-
structures courses. We have also omitted the chapters that are not covered in the
courses that we teach: Chapters 18–20 and 27–35 (though we do include some
solutionsforChapter27),aswellasAppendicesA–D;futureeditionsofthisman-
ual may include some of these chapters. There are two reasons that we have not
included solutions to all exercises and problems in the selected chapters. First,
writingupallthesesolutionswouldtakealongtime,andwefeltitmoreimportant
to release this manual in as timely a fashion as possible. Second, if we were to
includeallsolutions, thismanualwouldbemuchlongerthanthetextitself.
We have numbered the pages in this manual using the format CC-PP, where CC
is a chapter number of the text and PP is the page number within that chapter’s
lecturenotesandsolutions. ThePPnumbersrestartfrom1atthebeginningofeach
chapter’s lecture notes. We chose this form of page numbering so that if we add
orchangesolutions toexercisesandproblems, theonlypageswhosenumberingis
affected are those for the solutions for that chapter. Moreover, if we add material
for currently uncovered chapters, the numbers of the existing pages will remain
unchanged.
Thelecturenotes
Thelecturenotesarebasedonthreesources:
P-2 Preface
Somearefromthefirst-edition manual;theycorrespond toCharlesLeiserson’s
lecturesinMIT’sundergraduate algorithmscourse, 6.046.
Some are from Tom Cormen’s lectures in Dartmouth College’s undergraduate
algorithmscourse, CS25.
Somearewrittenjustforthismanual.
You will find that the lecture notes are more informal than the text, as is appro-
priate for a lecture situation. In some places, we have simplified the material for
lecture presentation or even omitted certain considerations. Some sections of the
text—usually starred—are omitted from the lecture notes. (Wehave included lec-
ture notes for one starred section: 12.4, on randomly built binary search trees,
whichwecoveredinanoptionalCS25lecture.)
In several places in the lecture notes, we have included “asides” to the instruc-
tor. The asides are typeset in a slanted font and are enclosed in square brack-
ets. [Hereisanaside.] Someofthe asides suggest leaving certain material onthe
board, since you will be coming back to it later. If you are projecting a presenta-
tionratherthanwritingonablackboardorwhiteboard,youmightwanttoreplicate
slides containing this material so that you can easily reprise them later in the lec-
ture.
Wehavechosennottoindicatehowlongittakestocovermaterial,asthetimenec-
essary to cover a topic depends on the instructor, the students, the class schedule,
andothervariables.
Therearetwodifferences inhowwewritepseudocode inthelecturenotesandthe
text:
Lines are not numbered in the lecture notes. We find them inconvenient to
numberwhenwritingpseudocode ontheboard.
We avoid using the length attribute of an array. Instead, we pass the array
length as a parameter to the procedure. This change makes the pseudocode
moreconcise, aswellasmatchingbetterwiththedescription ofwhatitdoes.
We have also minimized the use of shading in figures within lecture notes, since
drawingafigurewithshadingonablackboard orwhiteboard isdifficult.
Thesolutions
Thesolutions arebasedonthesamesources asthelecture notes. Theyarewritten
abitmoreformally than thelecture notes, though abitlessformally thanthetext.
We do not number lines of pseudocode, but we do use the length attribute (on the
assumption that you will want your students to write pseudocode as it appears in
thetext).
Asofthethirdedition, wehavepublicly posted afewsolutions onthebook’s web-
site. These solutions also appear in this manual, with the notation “This solution
is also posted publicly” after the exercise or problem number. The set of pub-
licly posted solutions mightincrease overtime, andsoweencourage youtocheck
whetheraparticularsolutionispostedonthewebsitebeforeyouassignanexercise
orproblemtoyourstudents.
Preface P-3
Theindexlistsalltheexercisesandproblemsforwhichthismanualprovidessolu-
tions,alongwiththenumberofthepageonwhicheachsolution starts.
Asides appear in a handful of places throughout the solutions. Also, we are less
reluctant to use shading in figures within solutions, since these figures are more
likelytobereproduced thantobedrawnonaboard.
Sourcefiles
Forseveral reasons, weareunable topublish ortransmitsource filesforthisman-
ual. Weapologize forthisinconvenience.
Youcanusetheclrscode3epackageforLATEX2"totypesetpseudocodeinthesame
way that we do. You can find this package at http://www.cs.dartmouth.edu/ thc/
clrscode/. Thatsite alsoincludes documentation. Makesure tousethe clrscode3e
package, nottheclrscodepackage; clrscode isforthesecondeditionofthebook.
Reportingerrors andsuggestions
Undoubtedly, instructors will find errors in this manual. Please report errors by
sendingemailtoclrs-manual-bugs@mitpress.mit.edu.
If you have a suggestion for an improvement to this manual, please feel free to
submititviaemailtoclrs-manual-suggestions@mitpress.mit.edu.
Asusual, if you findanerror inthe text itself, please verify that ithas not already
been posted on the errata web page before you submit it. You can use the MIT
Press website for the text, http://mitpress.mit.edu/algorithms/, to locate the errata
webpageandtosubmitanerrorreport.
Wethankyouinadvanceforyourassistanceincorrectingerrorsinboththismanual
andthetext.
Howweproducedthismanual
Like the third edition of Introduction to Algorithms, this manual was produced in
LATEX2". We used the Times font with mathematics typeset using the MathTime
Pro 2 fonts. As in all three editions of the textbook, we compiled the index using
Windex, a C program that we wrote. We drew the illustrations using MacDraw
Pro,1 with some of the mathematical expressions in illustrations laid in with the
psfrag package for LATEX2". We created the PDF files for this manual on a
MacBookProrunning OS10.5.
Acknowledgments
This manual borrows heavily from the manuals for the first two editions. Julie
Sussman, P.P.A.,wrotethefirst-edition manual. Juliedidsuchasuperb jobonthe
1SeeourpleaintheprefaceforthethirdeditiontoApple,askingthattheyupdateMacDrawProfor
OSX.
P-4 Preface
first-editionmanual,findingnumerouserrorsinthefirst-editiontextintheprocess,
that wewerethrilled tohaveherserve astechnical copyeditor for boththe second
andthirdeditionsofthebook. CharlesLeisersonalsoputinlargeamountsoftime
workingwithJulieonthefirst-edition manual.
The manual for the second edition was written by Tom Cormen, Clara Lee, and
Erica Lin. Clara and Erica were undergraduate computer science majors at Dart-
mouthatthetime,andtheydidasuperbjob.
The other three Introduction to Algorithms authors—Charles Leiserson, Ron
Rivest, andCliffStein—provided helpful commentsand suggestions forsolutions
toexercisesandproblems. Someofthesolutionsaremodificationsofthosewritten
overtheyearsbyteachingassistantsforalgorithmscoursesatMITandDartmouth.
Atthispoint,wedonotknowwhichTAswrotewhichsolutions, andsowesimply
thank them collectively. Several of the solutions to new exercises and problems
in the third edition were written by Sharath Gururaj of Columbia University; we
thankSharathforhisfinework. ThesolutionsforChapter27werewrittenbyPriya
Natarajan.
We also thank the MIT Press and our editors—Ada Brunstein, Jim DeWolf, and
Marie Lee— for moral and financial support. Tim Tregubov and Wayne Cripps
provided computersupportatDartmouth.
THOMAS H. CORMEN
Hanover, NewHampshire
August2009