CONTENTS
PART I PRELIMINARIES
CHAPTER I PROGRAMMING IN C++
1.1Introduction 3
1.2Functions and Parameters 3
1.2.1 Value Parameters 3
1.2.2 Template Functions 4
1.2.3 Reference Parameters 5
1.2.4 Const Reference Parameters6
1.2.5 Return Values 7
1.2.6Recursive Functions 8
Fibonacci numbers
Factorial
Permutations
1.3 Dynamic Memory Allocation14
1.3.1 The Operator new 14
1.3.2 One-Dimensional Arrays15
1.3.3Exception Handling 15
1.3.4The Operator d e 1 e t e16
1.3.5Two-Dimensional Arrays17
1.4Classes20
1.4.1The Class Currency20
1.4.2Using a Different Representation28
1.4.3Operator Overloading29
1.4.4Throwing Exceptions32
1.4.5Friends and Protected Class Members33
1.4.6Addition of #ifndef, #define, and #endif Statements 36
1.5 Testing and Debugging 37
1.5.1What Is Testing? 37
Roots of a quadratic
1.5.2Designing Test Data 40
Finding the maximum element
1.5.3Debugging 43
1.6 References and Selected Readings 44
CHAPTER 2 PROGRAM PERFORMANCE 45
2.1 Introduction 47
2.2 Space Complexity 48
2.2.1Components of Space Complexity 48
2.2.2Examples 54
2.3 Time Complexity 57
2.3.1Components of Time Complexity 57
2.3.2Operation Counts 58
Polynomial evaluation
Rank sort
Selection sort
Bubble sort
Insertion sort
Sequential search
2.3.3Step Counts 68
Matrix add, multiply, and transpose
Minimum and maximum
2.4 Asymptotic Notation (0, ? q, o) 83
2.4.1 Big Oh Notation (0)84
2.4.2 Omega Notation (?88
2.4.3 Theta Notation (q) 89
2.4.4Little Oh (o) 90
2.4.5Properties 91
2.4.6Complexity Analysis Examples 91 Binary search
2.5 Practical Complexities 98
2.6 Performance Measurement. 102
2.6.1Choosing Instance Size 102
2.6.2Developing the Test Data 103
2.6.3Setting Up the Experiment 104
2.7 References and Selected Readings110
PART Ⅱ DATA STRUCT URES
CHAPTER 3 DATA REPRESENTA TION 111
3.1 Introduction 113
3.2 Linear Lists 114
3.3 Formula-Based Representation 116
3.3.1 Representation 116
3.3.2 The Exception Class NoMem117
3.3.3Operations118
3.3.4Evaluation122
3.4Linked Representation129
3.4.1The Classes ChainNode and Chain129
3.4.2Operations130
3.4.3Extensions to the Class Chain136
3.4.4A Chain Iterator Class137
3.4.5Circular List Representation 138
3.4.6 Comparison with Formula-Based Representation139
3.4.7Doubly Linked List Representation140
3.4.8Summary142
3.5Indirect Addressing146
3.5.1Representation146
3.5.2Operations147
3.6Simulating Pointers 152
3.6.1 SimSpace Operations153
3.6.2 Chains Using Simulated Pointers1 7
3.7 A Comparison 163
3.8 Applications 164
3.8.1 Bin Sort 164
3.8.2 Radix Sort 170
3.8.3Equivalence Classes 173
Machine scheduling
Electrical nets
3.8.4Convex Hull 180
3.9 References and Selected Readings 188
CHAPTER 4 ARRAYS AND MATRICES 189
4.1 Arrays 191
4. 1.1 The Abstract Data Type 191
4.1.2Indexing a C++ Array 192
4.1.3Row- and Column-Major Mappings 192
4.1.4The Class ArraylD 194
4.1.5The Class Array2D 197
4.2 Matrices 204
4.2.1Definitions and Operations 204
4 . 2.2 The Class Ma t r i x 206
4.3 Special Matrices 210
4.3.1Definitions and Applications 210
4.3.2Diagonal Matrices212
4.3.3Tridiagonal Matrix214
4.3.4Triangular Matrices216
4.3.5Symmetric Matrices218
4.4Sparse Matrices 221
4.4.1Motivation 221
4.4.2Array Representation222
4.4.3Linked Representation229
CHAPTER 5 STACKS 239
5.1 The Abstract Data Type 241
5.2 Derived Classes and Inheritance 241
5.3 Formula-Based Representation 243
5.4 Linked Representation 248
5 . 5 Applications 252
5.5.1Parenthesis Matching 252
5.5.2Towers of Hanoi 254
5.5.3Rearranging Railroad Cars 256
5.5.4Switch Box Routing 263
5.5.5Offline Equivalence Problem 264
5.5.6Rat in a Maze 268
5.6 References and Selected Readings 281
CHAPTER 6 QUEUES 283
6.1The Abstract Data Type 285
6.2Formula-Based Representation 286
6.3Linked Representation 292
6.4Applications 297
6.4.1 Railroad Car Rearrangement 297
6.4.2 Wire Routing 299
6.4.3 Image Component Labeling 306
6.4.4 Machine Shop Simulation309
6.5 References and Selected Readings324
CHAPTER 7 SKIP LISTS AND HASHING 325
7.1Dictionaries 327
7.2Linear List Representation 328
7.3Skip List Representation 332
7.3.1 The Ideal Case 332
7.3.2 Insertions and Deletions 334
7.3.3 Assigning Levels 335
7.3.4 The Class SkipNode 336
7.3.5 The Class SkipList 337
7.3.6 Complexity 342
7.4Hash Table Representation 343
7.4.1 Ideal Hashing 343
7.4.2 Hashing with Linear Open Addressing 344
7.4.3 Hashing with Chains 350
7.5An Application -Text Compression 357
7.5.1 LZW Compression358
7.5.2 Implementation of LZW Compression 359
7.5.3LZW Decompression 364
7.5.4Implementation of LZW Decompression 365
7.6 References and Selected Readings 370
CHAPTER 8 BINARY AND OTHER TREES 371
8.1 Trees 373
8.2 Binary Trees 378
8.3 Properties of Binary Trees 379
8.4 Representation of Binary Trees 382
8.4.1Formula-Based Representation 382
8.4.2 Linked Representation383
8.5Common Binary Tree Operations 385
8.6 Binary Tree Traversal386
8.7 The ADT BinaryTree 391
8.8 The Class BinaryTree 391
8.9 ADT and Class Extensions 392
8.9. 1 Output397
8.9.2 Delete397
8.9.3 Height397
8.9.4 Size398
8.10 Applications400
8.10.1 Placement of Signal Boosters400
8.10.2 Online Equivalence Classes 405
8.11References and Selected Readings416
CHAPTER 9 PRIORITY QUEUES 417
9.1 Introduction 419
9.2 Linear Lists 421
9.3 Heaps 421
9.3.1Definitions 421
9.3.2Insertion into a Max Heap423
9.3.3Deletion from a Max Heap424
9.3.4Max Heap Initialization424
9.3.5The Class MaxHeap 425
9.4 Leftist Trees 432
9.4.1Height- and Weight-Biased Min and Max Leftist Trees 432
9.4.2Insertion into a Max HBLT435
9.4.3Deletion from a Max HBLT435
9.4.4Melding Two Max HBLTs435
9.4.5Initialization 437
9.4.6The Class MaxHBLT 438
9.5 Applications 444
9.5.1 Heap Sort
9.5.2 Machine Scheduling 444
9.5.3 Huffinan Codes450
9.6References and Selected Readings457
CHAPTER 10 TOURNAMENT TREES459
10. 1 Introduction 461
10.2 The ADT WinnerTree 466
10.3 The Class WinnerTree 466
10.3.1Representation466
10.3.2Class Specification467
10.3.3Constructor, Destructor, and Winner 467
10.3.4Initializing a Winner Tree 468
10.3.5Replaying Matches 471
10.4 Loser Trees 472
10.5Applications 474
10.5.1 Bin Packing Using First Fit 472
10.5.2 Bin Packing Using Next Fit 480
CHAPTER 11 SEARCH TREES 485
11.1Binary Search Trees 488
11. 1. 1 Definition 488
11.1.2 The ADTs BSTree and IndexedBSTree 490
11.1.3 The Class BSTree 491
11.1.4Searching 492
11.1.5Inserting an Element493
11.1.6Deleting an Element493
11.1.7The Class DBSTree 496
11.1.8Height of a Binary Search Tree496
11.2 AVL Trees 500
11.2.1Definition 500
11.2.2Height of an AVL Tree501
11.2.3Representation of an AVL Tree501
11.2.4Searching art AVL Search Tree502
11.2.5Inserting into an AVL Search Tree502
11.2.6Deletion from an AVL Search Tree506
11.3 Red-Black Trees 510
11.3.1Definition 510
11.3.2 Representation of a Red-Black Tree512
11.3.3 Searching a Red-Black Tree512
11.3.4Inserting into a Red-Black Tree513
11.3.5Deletion from a Red-Black Tree518
11.3.6Implementation Considerations and Complexity521
11.4B-Trees 524
11.4.1 Indexed Sequential Access Method 524
11.4.2 m-way Search Trees 525
11.4.3 B-Trees of Order m 528
11.4.4 Height of a B-tree 529
11.4.5 Searching a B-tree 530
11.4.6Inserting into a B-tree530
11.4.7Deletion from a B-tree533
11.4.8Node Structure537
11.5 Applications 539
11.5.1Histogramming539
11.5.2Best-Fit Bin Packing543
11.5.3Crossing Distribution546
11.6References and Selected Readinas 553
CHAPTER 12 GRAPHS 555
12.1Definitions 557
12.2Applications 558
12.3Properties 562
12.4The ADTs Graph and Digraph 565
12.5Representation of Graphs and Digraphs 567
12.5.1 Adjacency Matrix 567
12.5.2 Packed-Adjacency Lists 569
12.5.3 Linked-Adjacency Lists 570
12.6Representation of Networks 573
12.7Class Definitions 575
12.7.1The Different Classes575
12.7.2Adjacency-Matrix Classes576
12.7.3An Extension to the Class Chain 580
12.7.4The Class LinkedBase580
12.7.5Linked Classes 581
12.8Graph Iterators 588
12.8.1 Specification 588
12.8.2 Iterator Functions for Adjacency-Matrix Representations 589
12.8.3 Iterator Functions for Linked-Adjacency Lists589
12.9 Language Features 592
12.9.1 Virtual Functions and Polymorphism 592
12.9.2 Pure Virtual Functions and Abstract Classes 595
12.9.3 Virtual Base Classes 596
12.9.4 Abstract Classes and Abstract Data Types598.
12.10 Graph Search Methods 600
12.10.1 Breadth-First Search 600,
12.10.2 The Class Network 602
12.10.3 Implementation of Network: : BFS602
12.10.4 Complexity Analysis of Network: : BFS602
12.10.5 Depth-First Search 605
12.11Applications Revisited 607
12.11.1 Finding a Path 607
12.11.2 Connected Graphs and Components609
12.11.3 Spanning Trees 611
PART III ALGORITHM-DESIGN METHODS
CHAPTER 13 THE GREEDY METHOD 615
13.1 Optimization Problems 617
13.2 The Greedy Method 618
13.3 Applications 623
13.3.1Container Loading 623
13.3.20/1 Knapsack Problem 624
13.3.3Topological Sorting 628
13.3.4Bipartite Cover 633
13.3.5Single-Source Shortest Paths 642
13.3.6Minimum-Cost Spanning Trees 646
Kruskal’s Algorithm
Prim’s Algorithm
Sollin’s Algorithm
13.4 References and Selected Readings 659
CHAPTER 14 DIVIDE AND CONQUER 661
14.1 The Method 662
Minimum and maximum
Strassen’s matrix multiply
14.2 Applications 672
14.2.1Defective Chessboard672
14.2.2Merge Sort677
14.2.3Quick Sort682
14-2.4Selection688
14.2.5Closest Pair of Points691
14.3 Solving Recurrence Equations703
14.4 Lower Bounds on Complexity705
14.4.1 Lower Bound for the Minmax Problem706
14.4.2 Lower Bound for Sorting 708
CHAPTER 15 DYNAMIC PROGRAMMING 711
15.1The Method712
15.2Applications 715
15.2.10/1 Knapsack Problem715
15.2.2Image Compression719
15.2.3Matrix Multiplication Chains 724
15.2.4All Pairs Shortest Paths 731
15.2.5 Noncrossing Subset of Nets 735
15.2.6 Component Folding 740
15.3 References and Selected Readings 749
CHAPTER 16 BACKTRACKING 751
16.1 The Method 753
16.2 Applications 760
16.2.1 Container Loading760
16.2.20/1 Knapsack Problem769
16.2.3Max Clique 772
16.2.4Traveling Salesperson775
16.2.5Board Permutation 778
CHAPTER 17 BRANCH AND BOUND 787
17.1 The Method 788
17.2 Applications 792
17.2.1 Container Loading 792
17.2.2 0/1 Knapsack Problem802
17.2.3 Max Clique 805
17.2.4 Traveling Salesperson807
17.2.5 Board Permutation 810
INDEX817