Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 496 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
496
Dung lượng
17,59 MB
Nội dung
CuuDuongThanCong.com INTRODUCTION TO ALGORITHMS A Creative Approach CuuDuongThanCong.com CuuDuongThanCong.com INTRODUCTION TO ALGORITHMS A Creative Approach UDIMANBER University of Arizona \342\226\274\342\226\274 ADDISON-WESLEY PUBLISHING Menlo Reading, Massachusetts \342\200\242 Don Mills, Ontario \342\200\242 Wokingham, Bonn \342\200\242 Sydney CuuDuongThanCong.com \342\200\242 Singapore COMPANY Park, California England \342\200\242 Tokyo \342\200\242 New York \342\200\242 Amsterdam \342\200\242 Madrid \342\200\242 San Juan Cataloging-in-PublicationData of Congress Library Udi Manber, to algorithms Introduction Includes bibliographiesand index Data structures (Computer science) Algorithms I Title ' 005.7 1989 QA76.9.D35M36 88-2186 0-201-12037-2 ISBN by Addison-Wesley Reproduced from camera-ready copysuppliedby the author programs and applications presented in this book have been included for their value but are not guaranteed for any care, They have been tested with The publisher does not offer or warranties purpose any representation, nor does it accept liabilities with to or the any respect programs applications The instructional Reprinted with Copyright All rights system, corrections \302\251 1989 No part transmitted, photocopying, recording, Printed in the United States EFGHIJ-DO-943210 CuuDuongThanCong.com Publishing Company by Addison-Wesley reserved or 1989 October, in of this any publication form or otherwise, of America or without Inc be reproduced, stored in a retrieval means, electronic, mechanical, by any written of the publisher permission prior may Published simultaneously in Canada To my CuuDuongThanCong.com parents Eva and Meshulam CuuDuongThanCong.com PREFACE book grew Like many other This out of my teachers, able to explain algorithms clearly hard for some students to solve for them to but it is also hard not being with frustrations that not I discovered is it only seemed to me) simple problems by themselves, these two parts \342\200\224 the that are given to them I believe that understand the solutions \342\200\224 and should not be separated are related and the explanation It is essential to creation in order to understand it fully to It is not sufficient follow the steps leading to a solution look at the finished product (what book emphasizes the creative how to design a new This show of sequence Instead, ' the 'problem but rather Our goals in provides depth use intellectual looks more problems when an algorithm is not they arise as important how only in the as teaching as finished to solve future program, principles algorithm's than presentan into a to translate the drawbacks, The goal is to so on particular Teaching the details a improvements, turns), wrong in so on X, the \"problem these overcoming through a creativeprocess,rather are to show like and P\\\" of the The products problems, the thinking solution involved in creating algorithms, an process in this book This is used designing algorithms for and it also known covers algorithms, many techniques designing in more an elegant intuitive for explaining framework the design of algorithms we It does not, however, of designing and cover all possible ways algorithms, it exclusively between the The heart of the methodology lies in an analogy of proving mathematical theorems by induction and that of designing process further To methodology program is to purpose described are not P, program (including possible algorithm algorithms in designing \"old-new\" A\\ (although not always) its drawbacks, the difficulties explained teaching how to solve new involved algorithm better a thus are but also A, algorithm design Its main Algorithms and relation to other methods and algorithms,\" it easier for a programmer not in a way that makes in a way makes that it easier to understand algorithms not at of algorithm algorithm usually algorithm, first attempts algorithm X, sequence straightforward analysis, side reader the help methodology combinatorial the thinking for two processes serve different and purposes similar than they may appear to be This has been observed by many people The novelty of this book is the to analogy degree We show that the analogy which this analogy is exploited encompasses many known in the process of algorithm creation algorithm-design techniques,and helps considerably and is introduced in Chapter The methodology is discussed briefly more in formally achieve algorithms Although different types of results, they Chapter CuuDuongThanCong.com these are more vi Preface a car, and the following analogy Consider to get directions want to your Suppose that you at an unfamiliar arrive would be You hotel impatient quite rent city, were if you and so on You of the city, its general layout, the traffic patterns, for two turn right, go of the form directions blocks, \"go straight would would outlook so on and three However, miles,\" for change if you your straight around for a while You could for a time that in to live probably get city long planned who those of the second form (if you find someone directions), directions gives you with the city This book is not a source of but eventually you will need to know more about easy directions It does contain explanations of how to solve many particular problems, methods As a result, the is book is on general principles and the but emphasis I the well believe that extra effort is and involvement demands It thinking challenging told the history about rather have worthwhile nonnumeric The design of efficient diverse fields, including mathematics, This book can serve as an introduction general is becoming important molecular biology, and in algorithms statistics, and to to algorithms Many professionals, and even scientists not nonnumeric deeply many engineering computations involved with in computers, more than nonintellectual work It grungy programming is nothing But such a belief may lead to straightforward, solutions, trivial, inefficient of to convince solutions exist One this book is more efficient where goal elegant, is an elegant discipline, as well as an important one readersthat algorithm design The bookis self-contained The is mostly intuitive, and technicalities presentation are either kept to a minimum In particular, or are separated from the main discussion from the algorithm-design ideas as much as implementation details are separated There are of that were possible many algorithms designed especially to examples illustrate the principles in the book The in this book is not material emphasized as to be series of mastered and memorized It is as a presented something presented and so on ideas, examples, counterexamples, modifications, improvements, Pseudocodesfor most the descriptions Numerous exercises and algorithms are given following a discussionof further reading, with a relevant bibliography, follow In each chapter most chapters, the exercises are divided into two drill exercises and creative classes, exercises Drill exercises are meant to test the reader's of the specific understanding believe that is sometimes examplesand the reader's particular (those ability algorithms, whose presented algorithms numbers to use to solve new problems are underlined) also include a summary of the main The book is organized as material Chapter is Creative exercisesare meant to test developed in that chapter, in addition to the Sketches of solutions to selected exercises in that chapter the techniques an ideas follows introduction are at given the end of the book The chapters introduced Chapters to through mathematical present induction introductory Mathematical to algorithm with is, as we will see, very important design Experience induction proofs is thereforevery helpful few computer-science students Unfortunately, may be quite for some difficult get proofs enough exposure to induction Chapter the more difficult examples at first reading, students We suggestskipping and returning to them later Chapter is an introduction to the analysis of algorithms It describes the and gives the basic tools one needs to be able to perform process of analyzing algorithms, induction CuuDuongThanCong.com Preface of the algorithms analysis simple Readers the book Chapter is with basic data structures presented in vii a brief introduction and who have a from is a good idea background directly Chapter (it always the basic ideas behind the though) Chapter presents approach the analogy to induction It gives several of designing through algorithms proofs and describes their creation If you read only one chapter algorithms, examples of simple structures data to basic mathematical to read the introduction in There are book the can start read Chapter book, this ways to organize a book on to the subject of the algorithms, basic two according geometric algorithms Even Another Chapters organization way of the emphasis though familiar are who is to divide the this book is on through present for book graph algorithms, example, to design according design techniques, I have algorithms techniques chosenthe areas: four in divide way is to One algorithms former for algorithms sequence comparisons, data compression),graph trees, shortest paths, matching), geometric algorithms (e.g., spanning (e.g., and numerical and matrix intersection problems), algebraic algorithms (e.g., Fourier I believe that this organization is clearer and fast transform) sequences sets and algorithms convex hull, multiplication, sorting, (e.g., easier to follow 10 Chapter earlier chapters, to reductions Although the subject is unique and important is devoted chapter also serves as an opening of This of NP-completeness aspect subject own This of algorithm part theory and completeness the algorithms of parallel computation The material which leaves include parts book is this of Chapters for the more parts course, of these and can appear of its a chapter theory has become should know algorithms the with deals in essential an NP- about this property Chapter 12is an introduction different models interesting algorithms under than instructor 3, 5, 6, 7, and advanced for a first several contains choices many them The more are optional in It enough to warrant to Chapter 11, which complexity designs for proving techniques to parallel who Anyone act of reductions examples in can A be covered first some depth, chapters, along not although with be used as a basisfor a one-semester in in algorithm course Chapters a more course, design should all of necessarily 9, 10, advanced 11, and course 12, Acknowledgments and First here foremost on which methodology \342\200\224 more I thank this throughout important my wife Rachel for helping She was instrumental adventure the book is based Shecontributed else \342\200\224 sound advice than anything me in more in the suggestions, I could ways than corrections, not have list I can development done it of the and without her thanks are due to Jan van Leeuwen for an excellent and thorough of review of this book His detailed comments, numerous and suggestions, many the book enormously I also thank corrections have improved Eric Bach, Darrah Chavey, Kirk read and Sun Wu, who and made Pruhs, parts of the manuscript many helpful T Almes H Chan comments, and the reviewers (Rice Guy University), Agnes Dan Gusfield of David Harel California, (Northeastern University), Davis), (University of Israel), Daniel Hirschberg (University California, (Weizmann Institute, Irvine), Special a large portion CuuDuongThanCong.com INDEX O 347-349, 351-354,356-363,369, 41 O 39 414-415,426-434 370-374, 00 41-42,174,316,410 greedy 210-211,235,237,363 41 for ft networks interconnection 378, 389-403 41 co 41 for notation numeric 293-320,379-380,398-^01, Aanderaa, S O data Abstract Ackerman's 404-407 113 types 402-403 collection of sets 372 see Directed acyclic graph graph, Acyclic Acyclic Addition, 84 Adjacency matrix representation Adleman,L M 295,316 A V 85, 174, 175,305,316,336 Aho, Ajtai, Angluin, R 411 293-320,436-438 37-60 of definition distributed Arborescence 357, 363-367 approximation 208, 376 158, 169, 172, 176, 183,264,278-281,286, 287, 289, 299-304, 311-314, 316-319, 394-396, 409-410, 431 of 341 efficient, definition geometric 102-104, 265-291,331-333, 434-436 graph 95-96,98-101, CuuDuongThanCong.com 357, 363-367 186 66, 24-26, 35-36 divide-and-conquer 103-104,113-114, 379-380,383, 289 Arithmetic geometric mean theorem 131,137,141,144, 175 A Approximate convex hull Approximation algorithms 293-320, 436-438 of 369 D Apostolico, analysis 322 Anti-Gray codes 33 Algorithm algebraic 119-183 Amortized complexity 55, 83,86 of algorithms 37-60 Analysis Ancestor (in a tree) 193 M 410 Algebraic algorithms 42 236 path Alternating Akl, S G 409 Aleliunas, sets systolic 404-409,411,416 All shortest paths problem 212-214, G S 409 Almasi, 84 list representation Adjacency sequencesand G M 75 Aderson-VePskii, of an time running 379-380,411 parallel 158-164, 175,287,320, probabilistic 86 function 214,216,304,375-416 parallel 62 185-264,325-326, Arithmetic sum 11-12,31,53 V L 317 Arlazarov, of 63 Array, definition Articulation point 218,223,260,430 ASCII Assembly Assignment 146 line 404 problem 330-331 Asymptotic complexity, definition Atkinson, M D 317 of 38 466 Index Booleanmatrix 410 M.J Attalah, Augmenting path L 248 Auslander, A Borodin, 130 AVLtree 75-77, 104,114-115, 174 Avni,H multiplication 316,410 338 Bach, E 297 Breadth-first-search 369, 373 358-363, Baer, J -L 86 75, 104-106,248 Balance factor R 369 Bar-Yehuda, Batcher, K E 394,410 L 369 Beame,P W 410 begin Blelloch, G 409 Bentley, Berge, 31, 114 217 Binary 357, 363-365, conversion numbers, Binary search 370, 371, 374 to 26-28, 36 120-125,285,342,431 molecular 149,155 matching 117,187,372 235-238, Bisection 125 R Blum, M Bohr, N 55 Bolzano 175,247,248 method Bondy,J A 247 CuuDuongThanCong.com 125 409 K M Chandy, 247,432 31 Chen,CW 86 D 247 Cheriton, Chiba, 50 equation Chavey, D Christofides, 174 79 Chand,D.R.286 N Choueka,Y 369 Bollobds, B 337, 357 98-101,113-114 Chaining, separate Chartrand,G 71-77, 86,285 Bipartite graph 111-112, 248 problem Characteristic 34,263-264 Biology, 24 A L Celebrity 34, 115 search tree L 86 Cederbaum, I edge-biconnected259 packing 175 L Carter, J Cauchy, Binary 411 Capobianco,M 247 259 complete 113 Capacity 238 search component 217-223,249,259-260,430 definition of 142,164 127-129, network Butterfly Carrol, Binary tree 55,369 Bussey,W.H.30 Biconnected Bitner,J sort Bucket 409 BFS,seeBreadth-first Bipartite C A Burge,W.H Bertsekas, D P Bin network 256 Brualdi,R.A 55,336 174,440 extension 317 396-398,406,412 Brown, J L 55, 106,287 C 247 graph, Briggs, Bre1az,D 369 E 31 R E in a F A Broadcast Beckenbach, Bellman, graph 34, 220 409 Bridge, Broadcast Bavel,Z 55 Bent,S W D 369 Brent's lemma 381,383,384,441 Brigham, E.O Bates,J L 106 Baumert, 246, 249,426,427 Brebner,G.J.410 Brelaz, 87 155,174 358-363, 370 95, 189, 198-199,238, Branch-and-bound b-Matching Bag 317, Bottleneck problems 254,257, 261,338 155, 174 Boyer,R.S Boyer-Moore algorithm Backtracking 304-309, 319-320,325-326 239 247 175 N 248,370 Circle 34,270,271,290,332 186 in a graph Circuit, Eulerian 187; see also Eulerian graph Hamiltonian 243-244; see also Hamiltonian cycle 467 Index Circuit, model of parallel 378, 410 computing 356, 361, 370, 348, 351-352, Clique problem Conversion binary to decimal 371,372 Closed Convex hull in the plane 266 278-281, path, Closestpair 287, 289 problem Coalesced hashing J 304,317 Cook,S A 78-80 (in hashing) 175, 252, 14, 161-164, Cooley,J M 317 262,432 Cover edge 262 146, 256, 329 graph 348 set 355 network of a Complement Completebinary tree Complete graph 96, Complexnumbers CREW 378,382,410,412,415 Critical node (in an AVL tree) 75 Crossedge, in depth-first search 194 38 complexity 344 Crossproduct 413 Cryptography 295-297 Cube-connected-cycles 390 42 Component Culberson, 217-223, 249, 259-260,430 biconnected 187, 192,415 connected strongly connected 86 241 Cycle(in 186 226-233,247, 250, a graph) even-cycle problem Hamiltonian 230 248, 372 odd-cycleproblem Compression data 145-148, path 82,86 DAG, see Directed acyclic graph B 247,336 Dantzig,G Data compression 145-148,174 344 complexity 186 security Concurrent-read 88 see CRCW concurrent-write, Concurrent-read exclusive-write, Condensation graph 226 form normal Conjunctive see 346 187, 192,415 Connected graph 186 376, 410 machine co-NP 368 R L structures 317 61-90 62 252 Deadlock 31 representation Decimal to binary conversion 26-28, 36 Decision tree 141-143, 174, 177,332-333, 335, 424, 426,438-439 Decoding 106 Context-free language CuuDuongThanCong.com CREW Data Data types, abstract de Bruijn sequence Decimal Connected component Connection 246 233-234, 174, 177, 180,334-335 Concatenate operation Constable, J Cut (in a network) 251,260 Computational 363, CRCW 378,383,410,412,413 space 42 Computer 348, 355-356,358, 371,373,432-433 55, 83 computational 372 3-sets vertex 262-263, 318 asymptotic by 34, 263-264 244, 263, 356 Complexity amortized 414 circuit Counter 14 352-354,370,371,373 3-coloring Communication 316 D Corasick, M J 174 369 plane coloring time 344,346,368,410 Coppersmith, k-coloring 266 theorem 347 Cook's 369 edgecoloring of polygon, definition Convolution 405-407 Cole,R 410-411 Coloring problems 266,276 path Convex 86 Cohen, Collision 289 a 273 stretching Convex 346 286-289 273-277, approximate 80 Clustering CNF 36 26-28 to binary decimal 147 Decreasing DFS numbers 223 Index 468 Decreasing first 364 fit Decryption Domino principle 186 sequence 252, 262 Dekel,E 410 binary search tree heap 69-70 in a in a hashing 80 Double induction Deo,N for 234-235, 95, 189-197,221-225, Dynamic Dershowitz, N Deterministic coloring 262,432 cover 262 4, 113 193,388 definition algorithm, DFS, see Depth-first of in 158 search Diameter of a graph Edit 113,247 E A 247, 254 204-208, algorithm 317 Directed acyclic graph 197, 199-203,226, 246, 249, 253, 255,258, 260, 338, 208, 376 113-114,131,137, 141,144, 158, 169, 172, 176, 183,264, 278-281, 286, 287, 289, 299-304,311-314, 316-319, 379-380, 383, 394-396, 50-51 382 Divide-and-crush 149 Dobkin, Dodecahedron D P 175 CuuDuongThanCong.com graph relations 259 paths 23-24 155 steps 149 of parallel Efficiency Efficient algorithms Election, 1988Presidential definition of 62 376 of 341 definition algorithm, Element, D F 317 336 trees 263 Embedding 377 Emulation 146 Encoding 295 Encryption end Eppinger, 409-410,431 Divide-and-conquer recurrence 186 Ellipsoid algorithm algorithms 103-104, Divide-and-conquer map 17 Elliott, 371,378,430-431 Distributed algorithms of a planar Editing a text file Edmonds, J 243 316 Dijkstra, E W 83, Edge-disjoint 340 71 Dictionary 266 the plane of a graph Edge-biconnected 115,389 of a set of points Dijkstra's 286 H Edelsbrunner, Edge Descendant (in a tree) W 247 192,223 192-196 tree 114, 34 decomposition Ebert,J 194 108-111, 155-158,407 Ear nonrecursive implementation 410 programming 429 194 edge 66 S 174 Dvorak, Dwork, C crossedge 194 numbers S E variable Durian, B 174 227-233, 246-250,388,426-427, back edge 194 forward 409, 411, 123, 386-389, 114 Dreyfus, Dummy 378 search Depth-first tree) 75 an AVL 440 247,410 Depth of a circuit DNA 18 technique Doubling 114 E V Denardo, 73-74 Dense graphs, algorithms 244-246 Dinic, 91 Double Double rotation (in Deletion Diffie, of points Domination Degree of a vertex Degree set 348-349 289 Dominating 295 J L Equations, system Erdos, P 175 EREW 86 of 315 378,380-381,383-389,410, 412-415 Errors in design by induction 111-112 Index in induction in reductions proofs 28-29 334-336 Franco, J 369 Euclidean graph 254, 365 traveling 294, 297-298,316,318 Euclid's algorithm 187,247 Euler,L formula 17-18,30 387-389,409-411,414 187-190, 247, 251-252, 373, graph 230, 369 372 problem Even-cycle 3-sets Full-history see EREW 96-98 one-to-one N 247, of polynomials 317 transform Fourier Fast 287 M.G Feedback-edge set 257, Girth Goldberg, 309-317,443 numbers 34-35,46, 62 155 comparisons Golovina, 174 First fit 364, 370, 374 M J 86, 175, 317, 369,380, 410 Fischer, Flajolet, P 55 network 238-243, 246, 248, 327, 329, Flow, 338 Flynn, M.J 410 Forest 174,248 Four-Russian H Gonnet,G 248 247,248 85,86, 175 Goodrich, M T 410 416 problem 409 R L 55,286 95-96,98-101,185-264, algorithms 356-363,369, (in DFS) 194 algorithm 306-109, A S R E Gondran, M 325-326, 347-349, Fox,G C 409 Fraenkel, 31 Graph 178 186-187 edge 369 Graham's scan 275-277,287-288 Fortune, S 287 Forward Gomory, Graham, Johnson sorting L I Gottlieb, A 113, 174,247 Folding principle 377 Ford and S Golumbic, M 247 Gossip R W Ford,L.R Goldstine, H H 113 Golomb, S 369 Fingerprinting Floyd, of a graph 255 A V 248 Goldwasser, FIFO (first-in-first-out) File 49, 60,318 410 E 274-275, 286 Gillis,J 30 373, 429 FFT Fibonacci 265-291, 247,409 Gift-wrapping algorithm Feng,T 411 Fich,F A Gibbons, 321 Federal Express 102-104, 331-333,434-436 Geometric series 53 inverse 314-316 Faust, 318 Geometricalgorithms 309-317,443 363, 369 356, 409 E F Gehringer, Faradzev, LA 431 86 B A Garey, M.R 344,355, GCD 297-298,318 86 R 248, 366, Galil,Z 174,175,247,248 Galler, 294-297 Extendible hashing 86 Fagin, 51-53, 57, relations 137 Gabow, H Exclusive-read exclusive-write, Exponentiation 248 recurrence Function, Exact coverby 409 D R 336, 369 247, 248, S 146 R M Fulkerson, 432 Even, Freetrees 83 Fujimoto, tour technique Eulerian 410 247 Frequency constant 53 Euler's G Fredman, M L Frederickson, 365-367 salesman 410 N Francez, 351-354, 370-374, 414-415, 426-434 316, 317 biconnected 217;seealso Biconnected component 175 CuuDuongThanCong.com 469 bipartite 111-112, 117,187,372 470 Index 252, 369, 352-354, problems coloring code Gray 370,371,373 Greatest common divisor 356 226 186 directed definition edge biconnected, 415-416 embedding Euclidean of 259 365 254, 255 Hamiltonian 369 243-246, induced 95-96, 114, 187,251 Haken, D 55 Half 291 plane 336 Hall, M 323, 336 P theorem Hamilton's 89 234-238,248,251,260-261, 291, 338, 342, 366,431;seealso Matching problems by induction, involving 18-19, representation in a strongly subgraph subgraph traversal connected 206,428 260 CuuDuongThanCong.com 443 tree 104 E 316 Height of a node M Hellman, 174 443 S T Hedetniemi, undirected 186 unipathic 137-141, Hedetniemi, S M induced 187 189-199 weighted, definition 86 Heap 68-70, 87, 138-141,148,178,179, Heapsort of a 260 80 double 226-233, 247, 250, 86 86 extendible 186 unilateral 78-80, coalesced 186 251,260 86 universal 263 53-54 79 functions Hashing simple 186 split 85 247 Harmonic series of 83-84 residual 242 selfloops F 373 31 R W Harary, Hash 372 regular 356, 372, 258, Hansen, W J 34, 35 248, 261, 356, 369 path 338 248, 319 243-246, cycle Hamming, 186 multigraph 23-24, puzzle Hamiltonian proofs 6, 243 Hamilton's quaternions 255 matching 336 324, Hamilton, W R isomorphism 248, 369 planar 287 H Hall's 263,433 kernel 369 Guting,R Hall, 253 injective join 174 Guibas,L.J 187-190,247,251-252,373, of a interval M 336 Grotschel, Gusfield, D 432 girth 290 Gries,D 4,31,113,175 Guessing 11-14,43-44,47-49, 58,345 graph Eulerian 55 Grid polygon 115,389 definition of 186 see Directed acyclic acyclic, directed, 235, 237, 363 Mesh see Grid, 318 398,410 Greene, D H 186-187 diameter A Greenberg, 234-235, 244-246 210-211, algorithms Greedy 297-298, 318 of polynomials of connected, definition 217-234 decomposition definitions 33-34 ami complete 96, 244,263, condensation 320 31, 33-34, 20-23, open 22,320 348 of a complement dense 31 Gray,F (continued) Graph in a Heuristic 211,254,369 of 187,201 HEX 369 471 Index Hibbard,T W Hillis, 102-104, 282 Hinrichs, K 287 Hoare,C A R Hoey, D 287 Inner 174 248, House, design to algorithm 174 Huffman's 174, 177, 180, 334-335 Hwang, K 409 Incident J 174 325 matrix Increasing subsequence problem 174,336 Iyengar, S S (and virtually choosing the base of choosing the order of everywhere) 137,167, 172 17, 30, 92-94, 96-101,112-113, 128, 200, 411 John, J 174 247 D B Johnson, Johnson, D J 344, Join graphs 28-29, reversed 111-112 86 217 k-connectivity element *th-smallest 143-145, 174,179, 396-399 114 25,244-246 22-24, 93, strengthening the hypothesis 105-109, 113, 145,162, 167-169, 281, 282, 379, 418, 423,428, 435, 437, 442 strong 10, 109,157 CuuDuongThanCong.com 369, 370, 202-203, usage 14 generalization 356, 363, 89 Jones, D W 18 double double 355, 174 S M Johnson, 221,282-283 common errors definition 10 314-316 410 H V Jagadish, of 186 definition Indegree of a vertex, Independent set 19,262-263,356, 371, 433 Induced subgraph 95-96, 114,187,251 3, 9-36 113 248, 369 graph Itai,A 134, 113-114, 167-169, 175 Induction 24, paradox Inverse fast Fourier transform Isomorphism, Incerpi, 281-285, 263,433 graph Inventor's 389-390,401-403,415 Hypercube Interval Invariant, loop 26-28, 31,36, 176-177 175 J W Hunt, 267-270, problems 287,289-291 145-148, encoding 125-127 search Intersection 148, 174 D A 378, 310 Interpolation Interpolation B.C Huffman, network, algorithms 389-403 Hu,T C 248 Huang, Interconnection 369,410 analogy 331,336, 54 Integral 114, 128, 310,412 92-94, 71-72 361-363,371 261, 287, 305,316,336,410 E 178 69-70 linear programming Integer 85, 86, 175, Hopcroft, J E rule 174, 130 sort 410 H.J Horowitz, binary search tree in a heap Hong,J.-W 409 Horner's 305 product in a Hofri, M 55 Hoover, 142 bound Insertion 248 CM Hoffman, 10-11,25 Information-theoretic lower Injective graph 253 127, 131,135,137, In-placesort Input size 38,294 175 D S Hirschberg, of variations 86 N elimination D 410 Hidden-line 411 Kailath,T Kapur, S S Karatsuba, 286 A Karlin,A R Karmarkar, 316 86,411 N 336,370 M 174,175,243,261,346, 255 Kernel of a graph B W 370 Kernighan, Karp, R Khachian, L G 336 368, 370 472 Index Killian, J 369 Linesin Klee,V 336,369 175 S T Klein, KMP algorithm 174, 323, 148-155, Knuth, 55, 77, 85, 86,94, 155, 357, 160, 173, 174,175,316,369,436 Knuth-Morris-Pratt 148-155, algorithm 323,427 R J Litwin, W B Kruskal,J 411 86 Load balancing 380 Localreduction 356 54 rules of subsequence increasing 187 Loopinvariant 31, 36, 26-28, 175,247,410 411 D Lopresti, 31, 175,248,336 174, 178, 180, 331-334, 339 Lovdsz,L Lowerbound 86 142 i nformation-theoretic 75 M A Langston, Lower triangular Lucas, E 247 30 Landis, Y M Language-recognition 342 Las Vegas 344 A.M 248, 369 174 of a path 370 164-166,175 409 G K Manacher, 174 Manber, U 4, 55,86, 318 113, Manna, Z 4, 113, 173 Masek,W.J.317 247,432 problems Matching 198 234-238,248,251, 260-261, 291, 338, 342,366, b 338 bipartite 235-238, Lexicographic sort 128 Liestman, A L 443 35,234 Lin,S 370 string 148-155 Line 3-dimensional 356 14,265 266 Linear Linear Linear congruential method 160 (in hashing) 79 probing 322, 327-331, 336, programming 338-339 CuuDuongThanCong.com induction, Mathematical 281-287,435 431 324,337, 357 perfect Line-sweep technique 398, 369 R Manber, 201 Level (in breadth-first search) L A 368 Levin, Line segment 175, 248, 410,440 Lenstra, J K 370 L 336 problem Malek, M 114 Lempel,A 174,248 Lesniak, T.J Majority Lawler, E L 248,366, Least common multiple Leiserson, C E 411 Length E M Lynch, Maggs, B M algorithms 33 Lattice point Law, Luks, algorithms 159;seealso Probabilistic 339 matrix Lueker,G.S 55 174 nondeterministic 141-143 for sorting E 369,380,410 I Lakatos, 113-114, 134, 176-177 Kung, H.T 411 Ladner,R 167-169, 175 317 D T 174, Lipton, Longest Kbnigsberg bridge problem Kronrod, M A 174,317 Kurtz, 409 Lipson,J.D 309,316 Logarithms, Komlos,J 410 Kbnig, 64 Lipovski, G J 427 8, 108-111,114-115, 370 D E in the plane 13 position general lists Linked problem Knapsack of 328 form standard Linear search 173 King, K.N 113 see Induction Matrix adjacency 84, 99,325, 339 Boolean 304-309,317, multiplication 319,320,325-326 and graphs 246, 325-326, 336, 339-340 Index Molecular biology 149,155 J C 247 Molluzzo, 325 incident 336 inversion lower triangular Monotonically increasing Monte Carlo algorithms Moore, J S 155, 174 339 301-309,316,318, multiplication 325-326, 333-334,336,339, 398-400,410,412,416 square of a 319, 334 Multiple-instruction multiple-data upper triangular 339 vector multiplication, of Booleanmatrices 404-405 by of general 242 Maximum-consecutive subsequence in problem 6, 247 Median spanning tree see also fan- Megiddo, N 411 217-218 Menger,K Murty, U S Myers, E W (of a interconnection 378, 389-403 389,398-400,414 A R Meyer, 317,369 Miller, R 297 MIMD Minimum-cost spanning tree 247, 208-212, 249, 255,257, 338,340, 365, 428, 429 Minimum in a set 143-144, 169-171, element 176,380-382 247-248 Minoux,M Minty, G L 336 A Misra,J 175,409 Modeof a multiset A 247 A 410 CuuDuongThanCong.com 246, 248, I 94 Nil pointer 86,287 66 Nishizeki, T 247 definition of Node, Nonconvex 186; see also Vertex 267-270 polygon Nondeterministic algorithm 344-346, language recognition 344 model of computation Notation 171-173 324, 327, 329,338 Nonuniform 173 Mirzaian, 238-243, Nievergelt, J E 248 Miniekas, Network flow Newton, 376,410 393-396 on a sorting of trees 390 329 241 cut 393 split in 256 broadcast communication 146,208,256, parallel 394-396,413 Moitra, paths 255, 258 vertex) 19 shortest Network 393-395,410 Moffat, 410 cost, Negative 104,130-131,174,281, mergesort Mesh 164,171 4,175,317 Neighborhood Merge element 175,316,336 R 247 Munro,I Nassimi, D 286 K Mehlhorn, 309, 316-317, 171-173 Multiset 174, 179; element 144-145, smallest of an Multiplicity McCreight, E M 175 MCST,seeMinimum-cost McVoy, L 4, 248 339, 319 176,380-382 Mazeproblem 316, 318, 301-309, 398-400,410,412,416 of polynomials 298-300, 143-144, 169-171 a set matrices 325-326,333-334,336, 106-107 Maximum element 317, 304-309, 319-320 theorem min-cut Max-flow 410 376, Multiplication F 30 Maurolycus, 378 186,259,262 Multigraph 333 transpose 155,174 Multicomputer 333-334 symmetric 48 function 159 174 B M E Moret, Morris, J H 319 permutation 473 algorithms graphs 83-84, geometry 186-187 265-266 370 143 Index 474 NP-completeness 341-374 problems NP-complete 345-357,368-374,429, Parter, S V 246, Partition 439-440 398-401,404-407 248 133-135, 144-145, (for quicksort) 176 293-320, 379-380, algorithms Numeric I 409 Parberry, 175, 244, Partition 357 problem Pascal o Notation Pascal'striangle 41 39 O Notation 41-42, OONotation Objectivefunction Octal tree 174, 316, 327,361 Odd-eventransposition 246 sort convex 266, 276 411, 390-393, Hamiltonian 174 see shortest, Pattern 96-98 22-23, 320 function Open Gray code Order statistics 143-145, Ore,0 247,248 174, 179,396-399 287 Outdegree of a vertex, definition 186 of M C Pearl, J 369 Perfect matching Perfect shuffle 376 174-175 Pigeonhole 316 V Pan, C H Papadimitriou, 24 inventor Paradox, Parallel algorithms 214, 216,304, addition 379-380,411 of efficiency 398-401,404-405, 410,416 of 375-379, routing 24,30,31,113,169 290 simple 266,270-272,288,331-333 sequence comparisons 407-409, 376 division Polynomial 411, 416 sorting 390-396,410,411,413,414 CuuDuongThanCong.com 287-290 regular 291 401-403 tree operations 33, 266-277, grid 385-387 of 165 public-opinion convex 266, 273-277,286-289 selection 396-399,410,414 speedup Poll, Polygon 409-411 prefix 382-385,410,411,413,415 ranking 17 map Polya,G merge 394-396,410,413 models Pivot 133, 136-137,144,396 Planar 248, 338 graph Planar 377 multiplication 174 Plotkin, S A 336 Plummer, M D 248 376 folding principle matrix 375-416 319 problem 330, 339 34, 175, 395 principle N 86, Pippenger, L 369 Pitt, 248, 336 182 matrix 396-397,409 Pipeline 247 Pang,C 142, Philanthropist Packing, bin 363-365,370-371,374 35,234 390 Perfect speedup Permutation 345 matching Permutation 341 see String matching, Paull, Perl,Y P (class of problems) problem problems Shortest-paths simple 186 l-in-3SAT371 P=NP 372-373 258, 201 length of a Ofman,Yu 316 T 82, 86 compression 414 Ottmann, 239 augmenting 393-396,410,411 Odd-even mergesort One-to-one 236 alternating 233-234, A M 174,317 186 Path 390 Odd-cycleproblem Odlyzko, M S Paterson, 410 31 O 55 Patashnik, evaluation greatest multiplication 387-389, 319 318 92-94, 114, 128,310,412 common divisor 298-300, 318 309, 316-317, Index Polynomial-time Reed, D A 369 hierarchy Pool 87 155, 174 V R Pratt, 146 constraint Prefix Prim,R.C 247 testing 369 root of unity Primitive Priority queue 62, 68, Priority search 207,246 Reingold, E M Repeated squaring problems Pruhs,K 175 algorithm A H Kan, 160 287 C Ronse, Rooted tree, definition 295-297,316 165 poll Purdom, P W 55, 369 line Rotation (in AVL trees) 68, 86 62, Queue, priority Quicksort 131-137,144,174,176 problem Routing in Raghavan, P 175 search 285,435 in linked number Sankoff,D 175 lists 385-387 SAT 317 46-53, 55,56, divide-and-conquer 50-51 with full history unfolding Reduction see SAT, l-in-3 371 175,183 Recurrence relations 57, 58, 51-53, 137 194 215, 239, 246,317,321-340,442; also NP-completeness CuuDuongThanCong.com problem (satisfiability) 346-347, 350-354,357,370,371,440 186 sequence 317 N Santoro, Reachable vertex 175 S L Salzberg, Rao,K.R 317 Recursion, 369,410 Sahni,S Saks,M 175 Rank 143 Realizable 317,409 Rytter,W Random access machine 344 number 160, 175,415 Rational 42 411 A.G Ranking of an algorithm Ryser,H.J 175 Random Range scheme 410 time Running 127-129 sort Ranade, 174,287,369 401-403 encryption 317 Runge,C Rabin, M O 329-330 a hypercube L Rudolph, 183 tournament Routing RSA public-key 316 409-410 Quinn,M.J 75 304,317 390 319 186 270 Rotating Round-robin Quaternions 66, 83, of 113 A L Rosenberg, Roth,M Radix 55 F S Rodeh,M.175,336,410 357 encryption scheme Quadtree G 370 149 RNA 368 Public-opinion 244-246 155 410 D Roberts, Pseudorandom numbers 83-84 Rivest,R.L 174,295,316 278-281 Pseudopolynomial 85, 369 297, 317 Ring 305 Rinnooy Proximity 291 410 Richards, 158-164,175,287, 175 proof Public-key polygon Revision maintenance 320,402-403 PSPACE Regular Representation of graphs Residual graph 242 Reversed induction 25, 313,315 86 Probabilistic algorithms Probabilistic graph 372 Reif,J.H Prefix, parallel 382-385,410, 411,413, 415 F P 286, 287, 365 Preparata, Primality 409 Regular 59 SAT,2-in-4371 368 Savitch,W.J Saxe, J B 55 Schaback, Scheduling Schbnhage, 174 R 185 A 174 295-297, Index 476 Schorn,P 287 Simplex algorithm 336 A Schrijver, Schwab, B 86 Single-source Sink 99 Search Sizeof an 120-125,285,342,431 binary search trees 71 in binary depth-first 227-233, 246-250, Smit,G.V 388,426-427,429 125-127 interpolation linear 173 80 Seed 160 143-145, 174, 179, problem in a graph Separate chaining 186 337, 407-409 119-183,380-383; seealso Shiloach, Shmoys, 295,316 365 Minimum-cost spanning Spencer,J 175,247 Y 410 D B 370 263 Square of a matrix Square root 429 Siegel,H.J 411 319, 334 272 304 Stable algorithm 255, 428, 247 P M Split graph 185, 201-208, Shortest-paths problems 212-214, 247, 249, 253,254, 257, 258, 322, 325, 338,427, Shrira, L 410 Stack 194 of linear Standard form Standish, T A Stanton, D 173 409 Steele,G.L 410 Simple graph Steiglitz, path 186 186 Simple polygon CuuDuongThanCong.com programming 328 85 SIMD 376,410 Simple K 248,336 Stirling's approximation 266, see also tree Speedup 376 378-389 Spira, D P 187 tree 35,186,371,429; Spanning 270-272, 430 173 E H Spanning forest 174 Siewiorek, 130 selection Spafford, 297 Shared-memory algorithms Shell, D L 174 Shellsort 131-137, 144, 174, 176 topological 199-201,249,427, Space complexity 42 algorithms M I 286-287, 390-393, 411,414 127-129 radix cover 355 Shamos, transposition 390-396,413-414 quicksort 155-158,175, 181-182,317,324-325, Sequence 141-143 393-396,410 parallel 407-409 337, Set algorithms 128, 178 for bound network 106-107, 119-183,317, Sequence comparisons Shamir, A 178 130 odd-even algorithms J 137-141, 174 mergesort 130-131 79 324-325, Shallit, heapsort lower Self-adjusting trees 86 Sequence Johnson lexicographic 130 sort Selfloops 142, 164 127-129, Ford and insertion 396-399 Selection 31 in-place 127,131,174 Segall,Z.409 Selection 369 I S bucket R 174 Sedgewick, B 113 Sorting 186 Security 86 Smith-Thomas, M 410 Snir, Sominskii, collision (in hashing) Secondary 102-104,115 174 Solovay,R 207,246 priority paths 328 D D Sleator, 189-197,221-225, 376 201-208 38,294 input Slackvariable 249,426, 427 95, shortest Skyline problem 189, 198-199,238,246, 95, breadth-first 336 multiple-data Single-instruction 288, 331-333 Stockmeyer, L 369 54, 142 477 Index Q F 410 Strassen, V 369 Stout, Strassen's 301-305, 316, 318,319, algorithm the induction hypothesis 10, Strong Strongly connected component 250,251,260 binary 34, 263-264 decision 141-143,174,177,332-333, 247, 226-233, complete 124 problem subsequence Subsequence, longest increasing Summation problems 11,15-16 Summation Supporting line System 378, 404-409, 323-324 411,416 T G node Tarry, 86 369, 352-354,370,371,373 3-Dimensional complexity 42 Timetable Towers of Hanoi Tractable problem 249,427, 430 116 248, 317,322, matrix 339 Tremaux, 247 W T of a matrix CuuDuongThanCong.com 333,414 114 346 assignment Tsitsiklis, J N 409 TSP, see Traveling 317 Tuckey,J.W, Turing machine Turner, J S W T 344, 369 247 2-in-4SAT 371 Uhr, L 396 see also tree 365 inequality 339 Transpose spanning Triangular Tutte, 341 closure 214-216, Transitive Triangle Truth 357,370, 371,439 374 Topological sort 199-201, Toussaint, G 286 Minimum-cost Trotter, 356 Matching 3SAT 350-354, Time 247, 248, 174, 247 G 35,186,371,429; spanning 55, 85,86, 410,431 247, 365,428, 208 shortest-path R E 3-Coloring 208-212, 66,83, 186 rooted 30 67-68 390 quad 175 Takaoka,T 247,426 Tarjan, 67 104 spanning self-adjusting Talmud a in 249, 255,257, 338,340, octal 390 Szemeredi, E 410 Szymanski, representation of a minimum-cost 175 A 263 for implicit representation of a insertion into a 71-72 machine 396-398 Szego,G 31, 169 Szekeres, a 73-74 from of a height 376 algorithms Systolic 192-196 193 a free 83 of equations 315 of distinct representatives System depth-first explicit matrix 333-334 Symmetric in embedding 274 Synchronization descendant deletion 54-55 43-46, technique 167-169 438-439 426, 335,424, 114, 187 95-96, 198-199 breadth-first Subgraph 186-187 induced 71-77, 86 binary search 109, 157 induction Stuttering 115 34, binary 86 H R 221 biconnected 180-181,323,427 Strong, 75 balancing 177, 174-175, 148-155, 189-199 ancestor in a 193 AVL 75-77,104, 114-115 442 435,437, 428, matching 365-367 Euclidean Traversal (of a graph) Tree 18,34,66-77,186 113,145, 162, 22-24, 93, 105-109, 167-169, 281, 282, 379,418,423, String 390-393,411,414 Traveling salesman 356, 365-367,374 326 Strengthening sort Transposition salesman 346-347 problem 429 Index 478 Voronoi diagram 175 UkkonenE 85, 86,175,305, J D Ullman, 316, 336, 409, 287 164 Voting 411 definition graph, Undirected Union of of 83, 186 260 graph Unilateral 290 polygons problem 80-83, 86, 256,429 graph 260 Unipathic Universal hash functions 86 Union-find Upfal,E 86,411 matrix triangular Upper Wagner, R A 175 Waldinger,R 173 248 Warren, H S 248 S Warshall, Wegman, M N 86 Weighted 187 graph Weiner,P 175 339 173 D White, Whitney, H 217 Vacca,G 30 A Wigderson, Vaidya, P M 366 Wildcard Valiant, L G 369,402,410 Wilf, Nat, M 174 J 86, 247, Leeuwen, van Vertex 83, 336 186 Wirth, N 85 cover 262-263,348, 355-356, 371,373,432-433 degreeof 186 outdegree of of a planar 17 Vishkin, U Neumann, CuuDuongThanCong.com 38-39 X3Cproblem 372 I M Yaglom, 410 55,86,174 Ziv,J J 31 Yao,A 175,247 VLSI 263 von 173 186 Virus 173 Vitter,J.S Worm Wrap-around 398 266 of reachability Wood, D 287 186 map of a polygon 358, 363, Worst-case 186 of 369 algorithm Winograd's basis 260,431 indegree H S Williams, J W J 174 Winograd, S 316 der van 175,369,411 174 113, 174 174 Zorat, A 410 301,316,319 I \342\200\242 Creative A Ap i roach i of Arizona University This book emphasizes the creative aspects of algorithm design It teaches the thinking behind the of algorithms Algorithms are describedby following the steps leading to their development (sometimes including wrong steps) The heart of this creative process lies in an analogy between theorems combinatorial The book contains provingmathematical algorithms by induction and designing creation hundreds understanding of problems and examples It is designed to enhance of the principles behind algorithm design the reader's abilities and problem-solving HIGHLIGHTS coverage of algorithms known many techniques for designing over 500 mostly nonroutine exercises, \342\200\242 Current, up-to-date \342\200\242 Covers \342\200\242 Contains \342\200\242 Discusses \342\200\242 Covers implementation parallel \342\200\242 Includes The of efficient introduction ABOUT details separately from algorithm and unifies many of them solutions to about a quarter design of them ideas examples for most algorithms is becoming an important tool in many fields algorithms is self-contained, and can serve as biology, engineering This book,which computation algorithms and non-numeric non-numerical statistics, mathematics, including an and gives algorithms pseudocode design algorithms, to THE AUTHOR has been an associate professor of computer science at the University of Arizona since received his Ph.D in computer science from the University of Washington in 1982 He was an assistant professor and later an associate professor at the University of Wisconsin, Madison His researchareas include design of algorithms, distributed and parallel computing, and computer He received networks the prestigious Presidential Award in 1985, and the Best Paper \\bung Investigator Award of the seventh International Conferenceon Distributed 1987 Computing Systems, Udi Manber 1987 He 90000 9\"780201\"120370 ISBN Theory Addison-Wesley Publishing CuuDuongThanCong.com Company 0-201-12037-E ... n 12 13 14 15 - - - 8 - ? - - - - - - - - - 1 - - - - ? - - - - - 1 - - 10 - - - 12 - 13 - 14 - 15 11 - - - - - 8 - 1 - - - - - - - - - - - - - - 1 - - - - - - - - - - 1 - - - - - Figure 1.1 The... ^ Ul* 2- "*i.-l*)" was to induction and a?, n-\ forjC!,Jc2, ;X34220023 6- *z- we an arbitrary for The theorem is assumedto (z use reversed to ready true of this side right-hand = Z- expression.)... now have to prove that xn - is divisible by x- numbers x- for all natural The idea is to try to write the expression xn - using xn~{ -1 , which, by the induction - 1: x divisible is by hypothesis,