Table Of ContentIntroduction to Algorithms
Second Edition
This page intentionally left blank
ThomasH.Cormen
Charles E. Leiserson
RonaldL. Rivest
CliffordStein
Introduction to Algorithms
Second Edition
TheMITPress
Cambridge,Massachusetts London, England
McGraw-HillBookCompany
Boston BurrRidge,IL Dubuque, IA Madison,WI
NewYork SanFrancisco St.Louis Montre´al Toronto
This book is one of a series of texts written by faculty of the Electrical Engineering and Computer Science
DepartmentattheMassachusettsInstituteofTechnology. ItwaseditedandproducedbyTheMITPressundera
jointproduction-distributionagreementwiththeMcGraw-HillBookCompany.
OrderingInformation:
NorthAmerica
TextordersshouldbeaddressedtotheMcGraw-HillBookCompany.AllotherordersshouldbeaddressedtoThe
MITPress.
OutsideNorthAmerica
AllordersshouldbeaddressedtoTheMITPressoritslocaldistributor.
Thirdprinting,2002
(cid:1)c 2001byTheMassachusettsInstituteofTechnology
Firstedition1990
Allrightsreserved.Nopartofthisbookmaybereproducedinanyformorbyanyelectronicormechanicalmeans
(includingphotocopying,recording,orinformationstorageandretrieval)withoutpermissioninwritingfromthe
publisher.
ThisbookwasprintedandboundintheUnitedStatesofAmerica.
LibraryofCongressCataloging-in-PublicationData
Introductiontoalgorithms/ThomasH.Cormen...[etal.].—2nded.
p. cm.
Includesbibliographicalreferencesandindex.
ISBN0-262-03293-7(hc.:alk.paper,MITPress).—ISBN0-07-013151-1(McGraw-Hill)
1.Computerprogramming. 2.Computeralgorithms. I.Title:
Algorithms.II.Cormen,ThomasH.
QA76.6I5858 2001
005.1—dc21
2001031277
Contents
Preface xiii
I Foundations
Introduction 3
1 TheRoleofAlgorithmsinComputing 5
1.1 Algorithms 5
1.2 Algorithmsasatechnology 10
2 GettingStarted 15
2.1 Insertion sort 15
2.2 Analyzing algorithms 21
2.3 Designing algorithms 27
3 GrowthofFunctions 41
3.1 Asymptoticnotation 41
3.2 Standardnotations andcommonfunctions 51
4 Recurrences 62
4.1 Thesubstitution method 63
4.2 Therecursion-tree method 67
4.3 Themastermethod 73
(cid:1)
4.4 Proofofthemastertheorem 76
5 ProbabilisticAnalysisandRandomizedAlgorithms 91
5.1 Thehiringproblem 91
5.2 Indicator randomvariables 94
5.3 Randomizedalgorithms 99
(cid:1)
5.4 Probabilistic analysis andfurtherusesofindicator randomvariables
106
vi Contents
II Sorting and OrderStatistics
Introduction 123
6 Heapsort 127
6.1 Heaps 127
6.2 Maintaining theheapproperty 130
6.3 Buildingaheap 132
6.4 Theheapsort algorithm 135
6.5 Priorityqueues 138
7 Quicksort 145
7.1 Description ofquicksort 145
7.2 Performanceofquicksort 149
7.3 Arandomized versionofquicksort 153
7.4 Analysisofquicksort 155
8 SortinginLinearTime 165
8.1 Lowerbounds forsorting 165
8.2 Countingsort 168
8.3 Radixsort 170
8.4 Bucketsort 174
9 MediansandOrderStatistics 183
9.1 Minimumandmaximum 184
9.2 Selectioninexpected lineartime 185
9.3 Selectioninworst-case lineartime 189
III DataStructures
Introduction 197
10 ElementaryDataStructures 200
10.1 Stacksandqueues 200
10.2 Linkedlists 204
10.3 Implementing pointersandobjects 209
10.4 Representing rootedtrees 214
11 HashTables 221
11.1 Direct-address tables 222
11.2 Hashtables 224
11.3 Hashfunctions 229
11.4 Openaddressing 237
(cid:1)
11.5 Perfecthashing 245
Contents vii
12 BinarySearchTrees 253
12.1 Whatisabinarysearchtree? 253
12.2 Queryingabinarysearchtree 256
12.3 Insertion anddeletion 261
(cid:1)
12.4 Randomlybuiltbinarysearchtrees 265
13 Red-BlackTrees 273
13.1 Propertiesofred-black trees 273
13.2 Rotations 277
13.3 Insertion 280
13.4 Deletion 288
14 AugmentingDataStructures 302
14.1 Dynamicorderstatistics 302
14.2 Howtoaugmentadatastructure 308
14.3 Intervaltrees 311
IV Advanced Designand Analysis Techniques
Introduction 321
15 DynamicProgramming 323
15.1 Assembly-line scheduling 324
15.2 Matrix-chain multiplication 331
15.3 Elementsofdynamicprogramming 339
15.4 Longestcommonsubsequence 350
15.5 Optimalbinarysearchtrees 356
16 GreedyAlgorithms 370
16.1 Anactivity-selection problem 371
16.2 Elementsofthegreedy strategy 379
16.3 Huffmancodes 385
(cid:1)
16.4 Theoretical foundations forgreedymethods 393
(cid:1)
16.5 Atask-scheduling problem 399
17 AmortizedAnalysis 405
17.1 Aggregateanalysis 406
17.2 Theaccounting method 410
17.3 Thepotential method 412
17.4 Dynamictables 416
viii Contents
V Advanced DataStructures
Introduction 431
18 B-Trees 434
18.1 DefinitionofB-trees 438
18.2 Basicoperations onB-trees 441
18.3 DeletingakeyfromaB-tree 449
19 BinomialHeaps 455
19.1 Binomialtreesandbinomialheaps 457
19.2 Operationsonbinomialheaps 461
20 FibonacciHeaps 476
20.1 StructureofFibonacciheaps 477
20.2 Mergeable-heap operations 479
20.3 Decreasingakeyanddeleting anode 489
20.4 Boundingthemaximumdegree 493
21 DataStructuresforDisjointSets 498
21.1 Disjoint-set operations 498
21.2 Linked-list representation ofdisjointsets 501
21.3 Disjoint-set forests 505
(cid:1)
21.4 Analysisofunionbyrankwithpathcompression 509
VI GraphAlgorithms
Introduction 525
22 ElementaryGraphAlgorithms 527
22.1 Representations ofgraphs 527
22.2 Breadth-firstsearch 531
22.3 Depth-firstsearch 540
22.4 Topological sort 549
22.5 Stronglyconnected components 552
23 MinimumSpanningTrees 561
23.1 Growingaminimumspanning tree 562
23.2 ThealgorithmsofKruskalandPrim 567
24 Single-SourceShortestPaths 580
24.1 TheBellman-Fordalgorithm 588
24.2 Single-source shortest pathsindirected acyclicgraphs 592
24.3 Dijkstra’salgorithm 595
24.4 Differenceconstraints andshortest paths 601
24.5 Proofsofshortest-paths properties 607
Contents ix
25 All-PairsShortestPaths 620
25.1 Shortestpathsandmatrixmultiplication 622
25.2 TheFloyd-Warshall algorithm 629
25.3 Johnson’s algorithm forsparsegraphs 636
26 MaximumFlow 643
26.1 Flownetworks 644
26.2 TheFord-Fulkerson method 651
26.3 Maximumbipartitematching 664
(cid:1)
26.4 Push-relabel algorithms 669
(cid:1)
26.5 Therelabel-to-front algorithm 681
VII Selected Topics
Introduction 701
27 SortingNetworks 704
27.1 Comparisonnetworks 704
27.2 Thezero-one principle 709
27.3 Abitonic sortingnetwork 712
27.4 Amerging network 716
27.5 Asorting network 719
28 MatrixOperations 725
28.1 Propertiesofmatrices 725
28.2 Strassen’salgorithm formatrixmultiplication 735
28.3 Solvingsystemsoflinearequations 742
28.4 Inverting matrices 755
28.5 Symmetricpositive-definite matricesandleast-squares approximation
760
29 LinearProgramming 770
29.1 Standardandslackforms 777
29.2 Formulatingproblemsaslinearprograms 785
29.3 Thesimplexalgorithm 790
29.4 Duality 804
29.5 Theinitialbasicfeasiblesolution 811
30 PolynomialsandtheFFT 822
30.1 Representation ofpolynomials 824
30.2 TheDFTandFFT 830
30.3 EfficientFFTimplementations 839
Description:The updated new edition of the classic Introduction to Algorithms is intended primarily for use in undergraduate or graduate courses in algorithms or data structures. Like the first edition, this text can also be used for self-study by technical professionals since it discusses engineering issues in