algorithms (4th ed ) sedgewick wayne 2011 03 19 Cấu trúc dữ liệu và giải thuật

969 92 0
algorithms (4th ed ) sedgewick   wayne 2011 03 19 Cấu trúc dữ liệu và giải thuật

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

m Algorithms FOURTH EDITION CuuDuongThanCong.com This page intentionally left blank CuuDuongThanCong.com Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City CuuDuongThanCong.com Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests For more information, please contact: U.S Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw Cataloging-in-Publication Data is on file with the Library of Congress Copyright © 2011 Pearson Education, Inc All rights reserved Printed in the United States of America This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise For information regarding permissions, write to: Pearson Education, Inc Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671-3447 ISBN-13: 978-0-321-57351-3 ISBN-10: 0-321-57351-X Text printed in the United States on recycled paper at Courier in Westford, Massachusetts First printing, March 2011 CuuDuongThanCong.com To Adam, Andrew, Brett, Robbie and especially Linda _ To Jackie and Alex _ CuuDuongThanCong.com CONTENTS Preface viii Fundamentals 1.1 Basic Programming Model 1.2 Data Abstraction 1.3 Bags, Queues, and Stacks 64 120 1.4 Analysis of Algorithms 172 1.5 216 Case Study: Union-Find Sorting 243 2.1 Elementary Sorts 244 2.2 Mergesort 270 2.3 Quicksort 288 2.4 308 Priority Queues 2.5 Applications 336 Searching 361 3.1 Symbol Tables 362 3.2 Binary Search Trees 396 3.3 Balanced Search Trees 424 3.4 Hash Tables 458 3.5 Applications 486 vi From CuuDuongThanCong.com Graphs 515 4.1 Undirected Graphs 518 4.2 Directed Graphs 566 4.3 Minimum Spanning Trees 604 4.4 Shortest Paths 638 Strings 695 5.1 String Sorts 702 5.2 Tries 730 5.3 Substring Search 758 5.4 Regular Expressions 788 5.5 Data Compression 810 Context 853 Index 933 Algorithms 954 Clients 955 vii From CuuDuongThanCong.com PREFACE T his book is intended to survey the most important computer algorithms in use today, and to teach fundamental techniques to the growing number of people in need of knowing them It is intended for use as a textbook for a second course in computer science, after students have acquired basic programming skills and familiarity with computer systems The book also may be useful for self-study or as a reference for people engaged in the development of computer systems or applications programs, since it contains implementations of useful algorithms and detailed information on performance characteristics and clients The broad perspective taken makes the book an appropriate introduction to the field the study of algorithms and data structures is fundamental to any computerscience curriculum, but it is not just for programmers and computer-science students Everyone who uses a computer wants it to run faster or to solve larger problems The algorithms in this book represent a body of knowledge developed over the last 50 years that has become indispensable From N-body simulation problems in physics to genetic-sequencing problems in molecular biology, the basic methods described here have become essential in scientific research; from architectural modeling systems to aircraft simulation, they have become essential tools in engineering; and from database systems to internet search engines, they have become essential parts of modern software systems And these are but a few examples—as the scope of computer applications continues to grow, so grows the impact of the basic methods covered here Before developing our fundamental approach to studying algorithms, we develop data types for stacks, queues, and other low-level abstractions that we use throughout the book Then we survey fundamental algorithms for sorting, searching, graphs, and strings The last chapter is an overview placing the rest of the material in the book in a larger context viii From CuuDuongThanCong.com Distinctive features The orientation of the book is to study algorithms likely to be of practical use The book teaches a broad variety of algorithms and data structures and provides sufficient information about them that readers can confidently implement, debug, and put them to work in any computational environment The approach involves: Algorithms Our descriptions of algorithms are based on complete implementations and on a discussion of the operations of these programs on a consistent set of examples Instead of presenting pseudo-code, we work with real code, so that the programs can quickly be put to practical use Our programs are written in Java, but in a style such that most of our code can be reused to develop implementations in other modern programming languages Data types We use a modern programming style based on data abstraction, so that algorithms and their data structures are encapsulated together Applications Each chapter has a detailed description of applications where the algorithms described play a critical role These range from applications in physics and molecular biology, to engineering computers and systems, to familiar tasks such as data compression and searching on the web A scientific approach We emphasize developing mathematical models for describing the performance of algorithms, using the models to develop hypotheses about performance, and then testing the hypotheses by running the algorithms in realistic contexts Breadth of coverage We cover basic abstract data types, sorting algorithms, searching algorithms, graph processing, and string processing We keep the material in algorithmic context, describing data structures, algorithm design paradigms, reduction, and problem-solving models We cover classic methods that have been taught since the 1960s and new methods that have been invented in recent years Our primary goal is to introduce the most important algorithms in use today to as wide an audience as possible These algorithms are generally ingenious creations that, remarkably, can each be expressed in just a dozen or two lines of code As a group, they represent problemsolving power of amazing scope They have enabled the construction of computational artifacts, the solution of scientific problems, and the development of commercial applications that would not have been feasible without them ix From CuuDuongThanCong.com 942 INDEX 489 100, 138–141, 154 LinkedList 160 NoSuchElementException 139 PriorityQueue 352 Stack 159 TreeMap 489 Job-scheduling problem See Scheduling Josephus problem 168 Just-in-time compiler 195 HashMap using objects 69 variable 11 visibility modifier 84 while loop 15 wrapper type 122 Java system sort 306 Java virtual machine 51 Iterator java.awt Color Font 75 75 java.io File 75 K java.lang 107 ArrayIndexOutOfBounds 107 Boolean 102 Byte 102 Character 102 ClassCastException 387 Comparable 100 Double 34, 102 Float 102 Integer 102 Iterable 100, 123, 138, 154 Long 102 Math 28 NullPointer 107, 113, 159 Object 101 OutOfMemoryError 107 RuntimeException 107 Short 102 StackOverflowError 57, 107 StringBuilder 27, 105, 697 UnsupportedOperation 139 ArithmeticException java.net URL 75 java.util ArrayList Arrays 160 100, 339 ConcurrentModification Date L 29 Comparator 113 Karp, R 901 Karp, R M 759 Kendall tau distance 286, 345, 356 Kevin Bacon number 553–554 Key 244 Key equality ordered symbol table 368 symbol table 365 Key-indexed counting 703–705 Key type parameter priority queue 309 symbol table 361 Keyword in context 879 Khachian, L G 909 Kleene’s theorem 794 Knuth, D E 178, 205, 759 Knuth-Morris-Pratt 762–769 Knuth shuffle 32 Kosaraju’s algorithm 586–590 Kruskal, J 628 Kruskal’s algorithm 624–627 KWIC See Keyword-in-context 160 Last-in-first-out policy 127 Las Vegas algorithm 778 Leading-term approximation See Tilde notation Least-significant digit See LSD string sort Leipzig Corpora Collection 371 Lempel, A 839 less() method 245, 315 Level-order traversal binary heap 313 binary search tree 420 Levin, L 918 LIFO See Last-in first-out policy LIFO stack See Stack data type Linear equation satisfiability 913 Linear inequality satisfiability 913 Linear probing 469–474 Linear programming 907–909 ellipsoid algorithm 909 interior point method 909 reductions 907–909 simplex algorithm 909 Linear running time 186 Linearithmic running time 186 Linked allocation 156 Linked list 142–146 building 143 circular 165 defined 142 deletion 145 deletion from beginning 145 garbage collection 145 insertion 145 insertion at beginning 144 insertion at end 145 iterator 154–155 memory usage of 201 Node data type 142 queue 150 reverse a 165 sequential search 374 shuffle a 286 sort a 286 From CuuDuongThanCong.com INDEX stack 147–149 traversal 146 Literal null 112–113 primitive type 11 string 80 Load-balancing 349, 909 Load factor 471 Local minimum 210 Logarithm function binary 185 integer binary 185 natural 185 Logarithmic running time 186 Log-log plot 176 Loitering 137 Longest common prefix 875 Longest paths 661, 911 Longest prefix match 842 Longest-processing-time first rule 349 Longest repeated substring 875 long primitive data type 13 Loop for 16 foreach 138 inner 180 while 15 Lossless data compression 811 Lossy data compression 811 Lower bound priority queue 332 sorting 279–282 3-sum problem 190 union-find 231 Lowest common ancestor 598 Loyd, S 358 LSD string sort 706–709 LZW algorithm 839–845 compression 840 expansion 841 trie representation 840 M Manber, U 884 Mark-and-sweep garbage collection 573 Maslow, A 904 Maslow’s hammer 904 Matrix data type 60 Maxflow-mincut theorem 894 Maxflow problem 886–902 See also Mincut problem Ford-Fulkerson 891–893 integrality property 894 maxflow-mincut theorem 892–894 max bipartite matching 906 preflow-push algorithm 902 reductions 905–907 residual network 895–897 Maximum in array 30 in binary heap 313 in binary search tree 406 in ordered symbol table 367 Maximum st-flow problem See Maxflow problem Max bipartite matching 906 Maze 530 McIlroy, D 298, 306 McKellar, A 306 Median 332, 345–347 Median-of-3 partitioning 305 Memory management 104 linked allocation 156 loitering 137 orphan 137 Sequential allocation 156 Memory usage 200–204 array 202 hash table 476 linked list 201 nested class 201 object 67, 201 943 primitive types 200 R-way trie 744 stack 213 string 202 substring 202–204 Mergesort 270–288 abstract in-place merge 270 analysis of 272 bottom-up 277 linked list 279, 286 multiway 287 natural 285 optimality 282 stability 341 top-down 272 Merging 270–271 Method inherited 100–101 instance 68–69, 86–87 static 22–25 Mincut problem 893 See also Maxflow problem Minimum in array 30 in binary search tree 406 in ordered symbol table 367 Min spanning forest 605 Min spanning tree 604–637 Boruvka’s algorithm 636 bottleneck shortest paths 690 critical edge 633 crossing edge 606 cut 606 cut optimality conditions 634 cut property 606 defined 604 greedy algorithm 607 Kruskal’s algorithm 624–627 Prim’s algorithm 616–623 reverse-delete algorithm 633 Vyssotsky’s algorithm 633 Minimum st-cut problem From CuuDuongThanCong.com 944 INDEX See Mincut problem Minotaur 530 Mismatched character rule 770 M L Fredman 628 Modular hash function 459, 774 Modular programming 26 Monte Carlo algorithm 776 Moore, J S 759 Moore’s law 194–195 Morris, J H 759 Most-significant-digit sort See MSD string sort Move-to-front 169 MSD string sort 710–718 Multidimensional sort 356 Multigraph 518 Multiple-source reachability problem 570, 797 Multiset 509 Multiway mergesort 287 Multiway trie See R-way trie Myers, E 884 N Natural logarithm function 185 Natural mergesort 285 Natural order 337 Negative cost cycle See Negative cycle Negative cycle 668–670, 677–681 Nested class 159 Network flow See Maxflow problem new() 67 Newton’s method 23 NFA See Nondeterministic finite-state automata Node data type 159 bag 155 binary search tree 398 Huffman trie 828 linked list 142 queue 151 red-black BST 433 R-way trie 734 stack 149 ternary search trie 747 Nondeterminism 794 Turing machine 914 Nondeterministic finite-state automata 794–799 NP 912 NP-complete 917–918 Null link 396 null literal 112–113 O Object 67–74 See also Objectoriented programming behavior 67, 73 identity 67, 73 memory usage of 201 state 67, 73 Object-oriented programming 64–119 arrays of objects 72 creating an object 67 declaring an object 67 encapsulation 96 inheritance 100 instance 73 instantiate an object 67 invoke instance method 68 objects 67–74 objects as arguments 71 objects as return values 71 reference 67 subtyping 100 using objects 69 Odd-length cycle in a graph 562 OOP See Object-oriented programming Operations research 349 Optimization problem 913 Ordered symbol table 366–369 floor and ceiling 367 minimum and maximum 367 ordered array 378 range query 368 rank and selection 367 red-black BST 446 Order of growth 179 Order-of-growth classifications 186–188 Order-of-growth hypothesis 180 Order statistic 345 binary search tree 406 ordered symbol table 367 quickselect 345–347 Orphaned object 104, 137 Out data type 41, 83 Outdegree of a vertex 566 Output See Input and output Overflow 51 Overloading constructor 84 static method 24 Overriding a method 66, 101 P P complexity class 914 P= NP question 916 Page data type 870 Palindrome 81, 783 Parallel arrays linear probing 471 ordered symbol table 378 sorting 357 Parallel edge 518, 566, 612, 640 Parallel job scheduling 663–667 Parallel precedence-constrained scheduling 663, 904 Parameterized type See Generics Parent-link representation breadth-first search tree 539 From CuuDuongThanCong.com INDEX depth-first search tree 535 minimum spanning tree 620 shortest-paths tree 640 union-find 225 Parsing an arithmetic expression 128 a regular expression 800–804 Particle data type 860 Partitioning algorithm 290 2-way 288 3-way (Bentley-McIlroy) 306 3-way (Dijkstra) 298 median-of-3 296, 305 median-of-5 305 selection 346–347 Partitioning item 290 Pass by reference 71 Pass by value 24, 71 Path See Longest paths; See also Shortest paths augmenting 891 Hamiltonian 913, 920 in a digraph 567 in a graph 519 length of 519, 567 simple 519, 567 Path compression 231 Pattern matching See Regular expression Perfect hash function 480 Performance See Propositions Permutation Kendall-tau distance 356 random 168 ranking 345 sorting 354 Phone book 492 Picture data type 814 Piping 40 Point data type 77 Pointer 111 See also Reference safe 112 Pointer sort 338 Poisson approximation 466 Poisson distribution 466 Polar angle 356 Polar coordinate 77 Polar sort 356 Poly-time reduction 916 Pop operation 127 Postfix notation 162 Postorder traversal of a digraph 578 reverse 578 Power law 178 Pratt, V R 759 Precedence-constrainted scheduling 574–575 Precedence order arithmetic expressions 13 regular expressions 789 Prefix-free code 826–827 compression 829 expansion 828 Huffman 833 optimal 833 reading and writing 834–835 trie representation 827 Preorder traversal of a digraph 578 of a trie 834 Prime number 23, 774, 785 Primitive data type 11–12 memory usage of 200 reason for 51 wrapper type 102 Primitive type versus reference type 110 Prim, R 628 Prim’s algorithm 350, 616–623 eager 620–623 lazy 616–619 Priority queue 308–335 binary heap 313–322 945 change priority 321 delete 321 Dijkstra’s algorithm 652 Fibonacci heap 628 Huffman compression 830 index priority queue 320–321 linked-list 312 multiway heap 319 ordered array 312 Prim's algorithm 616 reductions 345 remove the minimum 321 soft heap 629 stability 356 unordered array 310 private access modifier 84 Probabilistic algorithm See Randomized algorithm Probe 471 Problem size 173 Programs Accumulator 93 AcyclicLP 661 AcyclicSP 660 Arbitrage 680 Average 39 Bag 155 BellmanFordSP 674 BinaryDump 814 BinarySearch 47 BinarySearchST 379, 381, 382 BlackFilter 491 BoyerMoore 772 BreadthFirstPaths 540 BST 398, 399, 407, 409, 411 BTreeSET 872 Cat 82 CC 544 CollisionSystem 863–864 Count 699 Counter 89 CPM 665 From CuuDuongThanCong.com 946 INDEX 547 91, 103, 247 DeDup 490 499 880 LSD 707 LZW 842, 844 MaxPQ 318 Merge 271, 273 MergeBU 278 MSD 712 Multiway 322 NFA 799, 802 PictureDump 814 PrimMST 622 Queue 151 Quick 289, 291 Quick3string 720 Quick3way 299 RabinKarp 777 RedBlackBST 439 Cycle LookupIndex TST Date LRS TwoColor 555 580 DepthFirstPaths 536 DepthFirstSearch 531 Digraph 569 DijkstraAllPairsSP 656 DijkstraSP 655 DirectedCycle 577 DirectedDFS 571 DirectedEdge 642 DoublingTest 177 Edge 610 EdgeWeightedDigraph 643 EdgeWeightedGraph 611 Evaluate 129 Event 861 Example 245 FileIndex 501 FixedCapacityStack 135 DegreesOfSeparation DepthFirstOrder FixedCapacityStackOfStrings Flips 133 70 FlipsMax FlowEdge 898 FrequencyCounter 372 Genome 819–820 Graph 526 GREP 804 Heap 324 HexDump 814 Huffman 836 Insertion 251 KMP 768 KosarajuSCC 587 KruskalMST 627 KWIC 881 LazyPrimMST 619 LinearProbingHashST 470 LookupCSV 495 FordFulkerson UF 140 141 127 824 Rolls 72 Selection 249 RLE 375 SequentialSearchST 489 259 Shell Stack 256 503 149 StaticSETofInts Stats 99 125 175 883 SymbolGraph 552 ThreeSum 173 ThreeSumFast 190 TopM 311 Topological 581 Transaction 340 Stopwatch SuffixArray TransitiveClosure TrieST 737–741 228 491 Whitelist 99 Properties 180 3-sum 180 Boyer-Moore algorithm 773 insertion sort 255 quicksort 343 Rabin-Karp algorithm 778 red-black BST 445 selection sort 255 separate-chaining 467 shellsort 262 versus proposition 183 Propositions 182 2-3 search tree 429 3-sum 182 3-way quicksort 301 3-way string quicksort 723 arbitrage 681 B-tree 871 Bellman-Ford 671, 673 binary heap 319 binary search 383 BST 403–404, 412 breadth-first search 541 brute substring search 761 complete binary tree 314 connected components 546 Cook-Levin theorem 918 critical path method 666 cut property 606 DFS 531, 537, 570 Dijkstra’s algorithm 652, 654 flow conservation 893 Ford-Fulkerson 900–901 generic shortest-paths 651 WhiteFilter Reverse SparseVector 95 WeightedQuickUnionUF ResizingArrayStack SortCompare 221 VisualAccumulator ResizingArrayQueue SET 547 189 TwoSumFast SeparateChainingHashST 71 896 747 593 465 From CuuDuongThanCong.com INDEX greedy MST algorithm 607 heapsort 323, 326 Huffman algorithm 833 index priority queue 321 insertion sort 250, 252 integer programming 917 key-indexed counting 705 Knuth-Morris-Pratt 769 Kosaraju’s algorithm 588, 590 Kruskal’s algorithm 624, 625 linear-probing hash table 475 linear programming 908 longest paths in DAG 661 longest repeated substring 885 LSD string sort 706, 709 maxflow-mincut theorem 894 maxflow reductions 906 mergesort 272, 279, 282 MSD string sort 717, 718 negative cycles 669 parallel job scheduling with relative deadlines 667 particle collision 865 Prim’s algorithm 616, 618, 623 quick-find algorithm 223 quickselect 347 quicksort 293–295 quick-union algorithm 226 red-black BST 444, 447 regular expression 799, 804 resizing-array stack 199 R-way trie 742, 743, 744 selection sort 248 separate-chaining 466, 475 sequential search 376 shortest paths in DAG 658 shortest-paths optimality 650 shortest paths reductions 905 sorting lower bound 280, 300 sorting reductions 903 suffix array 882 ternary search trie 749, 751 topological order 578, 582 universal compression 816 weighted quick-union 229 protected modifier 110 Protein folding 920 public access modifier 110 Pushdown stack 127 See also Stack data type Push operation 127 Q Quadratic running time 186 Quantum computer 911 Queue data type analysis of 198 API 126 circular linked list 165 linked-list 150–151 resizing-array 140 Quick-find algorithm 222–223 Quickselect 345–347 Quicksort 288–307 2-way partitioning 290 3-way partitioning 298–301 3-way string 719 analysis of 293–295 and binary search trees 403 duplicate keys 292 function-call stack size 304 median-of-2 296, 305 median-of-5 305 nonrecursive 306 random shuffle 292 Quick-union 224–227 path compression 231 weighted 227–230 R Rabin-Karp algorithm 774–778 Rabin, M O 759 Radius of a graph 559 947 Radix 700 Radix sorting See String sorting Random bag data type 167 Randomized algorithm 198 Las Vegas 778 Monte Carlo 776 quickselect 345–347 quicksort 290, 307 Rabin-Karp algorithm 776 3-way string quicksort 722 Random number 30–32 Random queue data type 168 Random string model 716–717 Range query binary search tree 412 ordered symbol table 368 Rank binary search 25, 378–381 binary search tree 408, 415 ordered symbol table 367 suffix array 879 Reachability 570–572, 590 Reachable vertex 567 Recurrence relation binary search 383 mergesort 272 quicksort 293 Recursion 25 See also Base case; See also Recursion binary search 25, 380 binary search tree 401 depth-first search 531 Euclid’s algorithm Fibonacci numbers 57 mergesort 272 quicksort 289 Red-black BST 432–447 and 2-3 search tree 432 analysis of 444–447 color flip 436 color representation 433 From CuuDuongThanCong.com 948 INDEX defined 432 delete the maximum 454 delete the minimum 453 deletion 441–443, 455 implementation 439 insertion 437–439 left-leaning 432 perfect black balance 432 rotation 433–434 search 432 Redirection 40 Reduction 903–909 defined 903 polynomial-time 916 linear programming 907–909 maxflow 905–907 priority queue 345 shortest-paths 904–905 sorting 344–347, 903–904 Reference 67 Reference type 64 Reflexive relation 102, 216, 247, 584 Regular expression 82, 788 building an NFA 800–804 closure operation 789 concatenation operation 789 defined 790 epsilon-transition 795 match transition 795 nondeterministic finite-state automaton 794–799 or operation 789 parentheses 789 \\s+ 82 shortcuts 791 simulating an NFA 797–799 Rehashing 474 Relation antisymmetric 247 equivalence 102, 216, 584 reflexive 102, 216, 247, 584 symmetric 102, 216, 584 total order 247 transitive 102, 216, 247, 584 Residual network 895–897 Resizing array 136–137 binary heap 320 hash table 474–475 queue 140 stack 136 Return value 22 Reverese postorder traversal 578 Reverse a linked list 165–166 an array 21 array iterator 139 with a stack 127 Reverse-delete algorithm 633 Reverse graph 586 Reverse Polish notation See Postfix notation Reverse postorder 578 Ring buffer data type 169 RLE See Run-length encoding Robson, J 412 Rooted tree 640 Rotation in a BST 433–434, 452 Run-length encoding 822–825 Running time 172–173 analysis of 176 constant 186 cubic 186 doubling ratio 192 exponential 186 inner loop 180 linear 186 logarithmic 186 measuring 174 order of growth 179 quadratic 186 tilde approximation 178–179 Run-time error.See Error; See also Exception R-way trie 730–744 Alphabet 741 analysis of 742–743 collecting keys 738 deletion 740 insertion 734 longest prefix 739 memory usage of 744 one-way branching 744–745, representation 734 search 732–733 wildcard match 739 S Safe pointer 112 Sample mean 30 Samplesort 306 Sample standard deviation 30 Sample variance 30 Scheduling critical-path method 664–666 load-balancing problem 349 LPT first 349 parallel precedence-constrained 663–667 precedence constraint 574– 575 relative deadlines 666 SPT first 349 Scientific method 172 Scope of a variable 14, 87 Search hit 376 Searching 360–513 See also Symbol table Search miss 376 Search problem 912 Sedgewick, R 298 Selection 345 binary search tree 406 ordered symbol table 367 quickselect 346–347 suffix array 879 From CuuDuongThanCong.com INDEX Selection client 249 Selection sort 248–249 Self-loop 518, 566, 612, 640 Separate-chaining 464–468 Sequential allocation 156 Sequential search 374–377 Set data type 489–491 Shannon entropy 300–301 Shellsort 258–262 Shortest ancestral path 598 Shortest augmenting path 897 Shortest path 638 Shortest paths problem 638–693 all-pairs 656 arbitrage detection 679–681 Bellman-Ford 668–678 bitonic 689 bottleneck 690 certification 651 critical edge 690 Dijkstra’s algorithm 652–657 edge relaxation 646–647 edge-weighted DAG 658–667 generic algorithm 651 ineligible edge 646 in Euclidean graphs 656 monotonic 689 negative cycle 669 Negative cycle detection 670 negative weights 668–681 optimality conditions 650 parent-link 640 reduction 904–905 shortest-paths tree 640 single-source 639, 654 source-sink 656 undirected graph 654 vertex relaxation 648 Shortest-processing-time-first rule 349, 355 short primitive data type 13 Shuffling a linked list 286 an array 32 quicksort 292 Side effect 22, 108 Signature instance method 86 static method 22 Simple digraph 567 Simple graph 518 Simplex algorithm 909 Single-source problems connectivity 556 directed paths 573 longest paths in DAG 661 paths 534 reachability 570 shortest directed paths 573 shortest paths in undirected graphs 654, 904 shortest paths 538, 639 Social network 517 Soft heap 629 Software cache 391, 451, 462 Sollin, M 628 Sorting 242–359 See also String sorting 3-way quicksort 298–301 binary search tree 412 certification 246, 265 Comparable 246–247 compare-based 279 complexity of 279–282 cost model 246 entropy-optimal 296–301 extra memory 246 heapsort 323–327 indirect 286 in-place 246 insertion sort 250–252 inversion 252 lower bound 279–282, 306 mergesort 270–288 949 partially-sorted array 252 pointer 338 primitive types 343 quicksort 288–307 reduction 903–904 reductions 344–347 selection sort 248–250 shellsort 258–262 stability 341 suffix array 875–885 system sort 343 Source-sink shortest paths 656 Spanning forest 520 Spanning tree 520, 604 Sparse graph 520 Sparse matrix 510 Sparse vector 502–505 Specification problem 97 SPT See Shortest paths tree; See also Shortest-processing-time-first rule st-cut 892 st-flow 888 st-flow network 888 Stability 341, 355 insertion sort 341 key-indexed counting 705 LSD string sort 706 mergesort 341 priority queue 356 Stack data type 127 analysis of 198, 199 array implementation 132 fixed-capacity 132–133 generic 134 iteration 138–140 linked-list 147–149 resizing array 136 Standard deviation 30 Standard drawing 36, 42–45 Standard input 36, 39 Standard libraries 30 From CuuDuongThanCong.com 950 INDEX 82–83 In 41, 82–83 Out 41, 82–83 StdDraw 43 StdIn 39 StdOut 37 StdRandom 30 StdStats 30 Stopwatch 174–175 Standard output 36, 37–38 Static method 22–25 argument 22 defining a 22 invoking a 22 overloaded 24 pass by value 24 recursive 25 return statement 24 return value 22 side effect 22, 24 signature 22 Static variable 113 Statistics chi-square 483 median 345 minimum and maximum 30 order 345 sample mean 30, 125 sample standard deviation 30 sample variance 30, 125 StdDraw library 43 StdIn library 39 StdOut library 37 StdRandom library 30 StdStats library 30 Steque data type 167, 212 Stirling's approximation 185 Stopwatch data type 174–175 String data type 34, 80–81 API 80 characters 696 charAt() method 696 Draw concatenation 34, 697 conversion 102 immutability 696 indexing 696 indexOf() method 779 length 696 length() method 696 literal 34 memory usage of 202 + operator 80, 697 substring extraction 696 substring() method 696 String processing 80–81, 694–851 data compression 810–851 regular expression 788 sorting 702–729 substring search 758–785 suffix array 875–885 tries 730–757 String search See Substring search; See also Trie String sorting 702–729 3-way quicksort 719–723 key-indexed counting 703 LSD string sort 706–709 MSD string sort 710–718 Strong component 584 Strong connectivity 584–591 Strongly connected component See Strong component Strongly connected relation 584 Strongly typed language 14 Subclass 101 Subgraph 519 Sublinear running time 716, 779 Substring extraction memory usage of 202–204 substring() method 696 Substring search 758–785 Boyer-Moore 770–773 brute-force 760–761 indexOf() method 779 Knuth-Morris-Pratt 762–769 Rabin-Karp 774–778 Subtyping 100 Suffix array 875–885 Suffix array data type 879 Suffix-free code 847 Superclass 101 Symbol digraph 581 Symbol graph 548–555 Symbol table 360–513 2-3 search tree 424–431 API 363, 366 associative array 363 balanced search tree 424–457 binary search 378–384 binary search tree 396–423 B-tree 866–874 cost model 369 defined 362 duplicate key policy 363 floor and ceiling 367 hash table 458–485 insertion 362 key equality 365 lazy deletion 364 linear-probing 469–474 minimum and maximum 367 null value 364 ordered 366–369 ordered array 378 range query 368 rank and selection 367 red-black BST 432–447 R-way trie 732–745 search 362 separate-chaining 464–468 sequential search 374 string keys 730–757 ternary search trie 746–751 trie 730–757 unordered linked list 374 Symmetric order 396 From CuuDuongThanCong.com INDEX Symmetric relation 102, 216, 584 Szpankowski, W 882 T Tail vertex 566 Tale of Two Cities 371 Tandem repeat 784 Tarjan, R E 590, 628 Terminal window 10, 36 Ternary search trie 746–751 alphabet 750 analysis of 749 collecting keys 750 deletion 750 insertion 746 one-way branching 751, 755 prefix match 750 search 746 wildcard match 750 Theseus 530 this reference 87 Threading 420 Tilde notation 178, 206 Time-driven simulation 856 Timing a program 174–175 Top-down 2-3-4 tree 441 Top-down mergesort 272 Topological sort 574–583 depth-first search 578 queue-based algorithm 599 toString() method 66, 102 Total order 247 Transaction data type 78–79 compare() 340 compareTo() 266, 337 hashCode() 462 Transitive closure 592 Transitive relation 102, 216, 247, 584 Transpose a matrix 56 Tree 2-3 search tree See 2-3 search tree binary See Binary tree binary search tree See Binary search tree balanced search tree See Balanced search tree binomial 237 depth of a node 226 height of 226 inorder traversal 412 spanning tree See Minimum spanning tree parent-link 535, 539 preorder traversal 834 rooted 640 size 226 spanning tree See Spanning tree undirected graph 520 union-find 224–226 Tremaux exploration 530 Triangular sum 185 Trie 730–757 See also R-way trie; See also Ternary search trie collecting keys 731 Lempel-Ziv-Welch 840 longest prefix match 731, 842 one-way branching 744–745, 751, 755 prefix-free code 827 preorder traversal 834 reading and writing 834–835 wildcard match 731 Tufte plot 456 Tukey ninther 306 Turing, A 910 Turing machine 910 Church-Turing thesis 910 computability 910 951 nondeterministic 914 universality 910 Type conversion 13 Type erasure 158 Type parameter 122, 134 U Undecidability 97, 817 Undirected graph acyclic 520 adjacency-lists 524 adjacency-matrix 524 adjacency-sets 527 adjacent vertex 519 articulation point 562 biconnected 562 bipartite 521, 546–547, 562 breadth-first search 538–542 bridge 562 center 559 connected 519 connected component 519 connected to relation 519 connectivity 534, 543–546 cycle 519 cycle detection 546–547 defined 518 degree 519 dense 520 depth-first search 530–533 diameter 559 edge 518 edge-connected 562 edge-weighted See Edge-weighted graph Euler tour 562 forest 520 girth 559 Hamilton tour 562 interval graph 564 isomorphism 561 multigraph 518 From CuuDuongThanCong.com 952 INDEX odd cycle detection 562 parallel edge 518 path 519 radius 559 self-loop 518 simple 518 simple cycle 519, 567 simple path 519 single-source connectivity 556 single-source paths 534 single-source shortest paths 538 spanning forest 520 spanning tree 520 sparse 520 subgraph 519 tree 520 two-colorability 546–547, 562 vertex 518 weighted See Edge-weighted graph Unicode 696 Uniform hashing 463 Union-find 216–241 and depth-first search 546 binomial tree 237 Boruvka’s algorithm 636 dynamic connectivity 216 forest-of-trees 225 Kruskal's algorithm 625 parent-link 225 path compression 231, 237 quick-find 222–223 quick-union 224–227 weighted quick-find 236 weighted quick-union 227– 231 weighted quick-union by height 237 weighted quick-union with path compression 237 Uniquely decodable code 826 Unit testing 26 Universal data compression 816 Universality 910 Upper bound 206, 207, 281 V Value type parameter symbol table 361 trie 730 Variable 10 Variable-length code 826 Variance 30 Vector data type 106 Vertex adjacent 519 connected to relation 519 degree of 519 eccentricity 559 head and tail 566 indegree and outdegree 566 reachable 567 source 528 Vertex cover problem 920 Vertex relaxation 648 Virtual terminal 10 Vyssotsky’s algorithm 633 while loop 15 Whitelist filter 8, 48–49, 99, 491 Wide interface 160, 557 Wildcard character 791 Wildcard match 750 Worst-case guarantee 197 Wrapper type 102, 122 Z Zev, J 839 Zero-based indexing 53 Zipf’s law 393 W Web search 496 Weighted digraph See Edgeweighted digraph Weighted edge 604, 638 Weighted external path length 832 Weighted graph See Edgeweighted graph Weighted quick-union 227–231 Weighted quick-union with path compression 237 Weiner, P 884 Welch, T 839 From CuuDuongThanCong.com This page intentionally left blank From CuuDuongThanCong.com ALGORITHMS Fundamentals Graphs 1.1 Pushdown stack (resizing array) 4.1 Depth-first search 1.2 Pushdown stack (linked-list) 4.2 Breadth-first search 1.3 FIFO queue 4.3 Connected components 1.4 Bag 4.4 Reachability 1.5 Union-find 4.5 Topological sort 4.6 Strong componenets (Kosaraju) Sorting 4.7 Minimum spanning tree (Prim) 2.1 Selection sort 4.8 Minimum spanning tree (Kruskal) 2.2 Insertion sort 4.9 Shortest paths (Dijkstra) 2.3 Shellsort 4.10 Shortest paths in DAGs 2.4 Top-down mergesort 4.11 Shortest paths (Bellman-Ford) Bottom-up mergesort 2.5 Quicksort Strings Quicksort with 3-way partitioning 5.1 LSD string sort 2.6 Heap priority queue 5.2 MSD string sort 2.7 Heapsort 5.3 Three-way string quicksort 5.4 Trie symbol table Symbol Tables 5.5 TST symbol table 3.1 Sequential search 5.6 Substring search (Knuth-Morris-Pratt) 3.2 Binary search 5.7 Substring search (Boyer-Moore) 3.3 Binary tree search 5.8 Substring search (Rabin-Karp) 3.4 Red-black BST search 5.9 Regular expression pattern matching 3.5 Hashing with separate chaining 5.10 Huffman compression/expansion 3.6 Hashing with linear probing 5.11 LZW compression/expansion 954 From CuuDuongThanCong.com CLIENTS Fundamentals Strings Whitelisting Regular expression pattern matching Expression evaluation Huffman compression Connectivity Lempel-Ziv-Welch compression Sorting Context Comparing two algorithms Colliding particle simulation Top M B-tree set Multiway merge Suffix array (elementary) Longest repeated substring Symbol Tables Dedup Keyword in context Maxflow (Ford-Fulkerson) Frequency count Dictionary lookup Index lookup File indexing Sparse vector with dot product Graphs Symbol graph data type Degrees of separation PERT Arbitrage 955 From CuuDuongThanCong.com This page intentionally left blank From CuuDuongThanCong.com ... (divide) % (remainder) double-precision real numbers (64-bit IEEE 754 standard) + (add) (subtract) * (multiply) / (divide) true char or false characters (16-bit) - && (and) || (or) ! (not) ^ (xor)... library; need to download code (see page 2 7) while (!StdIn.isEmpty ()) { int key = StdIn.readInt (); if (rank(key, whitelist) == - 1) StdOut.println(key); } conditional statement (see page 1 5) call... theta) cosine function static double tan(double theta) tangent function Note 2: Angles are expressed in radians Use toDegrees () and toRadians () to convert Note 3: Use asin (), acos (), and atan()

Ngày đăng: 29/08/2020, 22:05

Mục lục

    CONTENTS (with direct linking)

    1.3 Bags, Queues, and Stacks

    INDEX (with direct linking)

Tài liệu cùng người dùng

Tài liệu liên quan