Top Banner
DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™ MARK ALLEN WEISS Florida International University An imprint of Addison Wesley Longman, Inc. Reading, Massachusetts • Harlow, England • Menlo Park, California Berkeley, California • Don Mills, Ontario • Sydney • Bonn • Amsterdam Tokyo • Mexico City
11

DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

Mar 18, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

DATA STRUCTURES & ALGORITHM ANALYSIS IN

JAVA™

MARK ALLEN WEISS Florida International University

An imprint of Addison Wesley Longman, Inc.

Reading, Massachusetts • Harlow, England • Menlo Park, California

Berkeley, California • Don Mills, Ontario • Sydney • Bonn • Amsterdam

Tokyo • Mexico City

Page 2: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

Contents

Chapter 1 Introduction 1

1.1. What's the Book About? 1

1.2. Mathematics Review 2

1.2.1. Exponents 2

1.2.2. Logarithms 3

1.2.3. Series 3

1.2.4. Modular Arithmetic 5

1.2.5. The P Word 5

1.3. A Brief Introduction to Recursion 7

1.4. Generic Objects in Java 11

1.4.1. The IntCell Class 11

1.4.2. The MemoryCell Class 13

1.4.3. Implementing Generic findMax 15

1.5. Exceptions 16

1.6. Input and Output 19

1.6.1. Basic Stream Operations 19

1.6.2. The StringTokenizer Object 20

1.6.3. Sequential Files 20

1.7. Code Organization 24

1.7.1. Packages 24

1.7.2. The Mylnteger Class 24

1.7.3. Efficiency Considerations 24

Summary 25

Page 3: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

CONTENTS

Exercises 25

References 27

Chapter 2 Algorithm Analysis 29

2.1. Mathematical Background 29

2.2. Model 32

2.3. What to Analyze 32

2.4. Running Time Calculations 35

2.4.1. A Simple Example 35

2.4.2. General Rules 35

2.4.3. Solutions for the Maximum Subsequence Sum Problem 37

2.4.4. Logarithms in the Running Time 43

2.4.5. Checking Your Analysis 4 7

2.4.6. A Grain of Salt 48

Summary 48

Exercises 49

References 54

Chapter 3 Lists, Stacks, and Queues 55

3.1. Abstract Data Types (ADTS) 55

3.2. The List ADT 56

3.2.1. Simple Array Implementation of Lists

3.2.2. Linked Lists 57

3.2.3. Programming Details 58

3.2.4. Doubly Linked Lists 63

3.2.5. Circular Linked Lists 63

3.2.6. Examples 64

3.2.7. Cursor Implementation of Linked Lists

3.3. The Stack ADT 75

3.3.1. Stack Model 75

3.3.2. Implementation of Stacks 76

Page 4: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

CONTENTS xi

3.3.3. Applications 81

3.4. The Queue ADT 88

3.4.1. Queue Model 88

3.4.2. Array Implementation ol Queues 89

3.4.3. Applications of Queues 92

Summary 94

Exercises 94

Chapter 4 Trees 99

4.1. Preliminaries 99

4.1.1. Implementation of Trees 100

4.1.2. Tree Traversals with an Application 101

4.2. Binary Trees 105

4.2.1. Implementation 105

4.2.2. An Example: Expression Trees 106

4.3. The Search Tree ADT—Binary Search Trees 109

4.3.1. find 110

4.3.2. findMin and findMax 111

4.3.3. insert 112

4.3.4. remove 113

4.3.5. Average-Case Analysis 116

4.4. AVL Trees 118

4.4.1. Single Rotation 12 0

4.4.2. Double Rotation 123

4.5. Splay Trees 130

4.5.1. A Simple Idea (That Does Not Work) 130

4.5.2. Splaying 132

4.6. Tree Traversals (Revisited) 138

4.7. B-Trees 139

Summary 144

Exercises 145

References 152

Page 5: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

xi i CONTENTS

Chapter 5 Hashing 155

5.1. General Idea 155

5.2. Hash Function 156

5.3. Separate Chaining 158

5.4. Open Addressing 162

5.4.1. Linear Probing 162

5.4.2. Quadratic Probing 164

5.4.3. Double Hashing 170

5.5. Rehashing 171

5.6. Extendible Hashing 174

Summary 176

Exercises 178

References 181

Chapter 6 Priority Queues (Heaps) 183

6.1. Model 183

6.2. Simple Implementations 184

6.3. Binary Heap 184

6.3.1. Structure Property 185

6.3.2. Heap Order Property 186

6.3.3. Basic Heap Operations 188

6.3.4. Other Heap Operations 192

6.4. Applications of Priority Queues 195

6.4.1. The Selection Problem 195

6.4.2. Event Simulation 196

6.5. d-Heaps 198

6.6. Leftist Heaps 198

6.6.1. Leftist Heap Property 199

6.6.2. Leftist Heap Operations 200

6.7. Skew Heaps 206

6.8. Binomial Queues 208

6.8.1. Binomial Queue Structure 208

Page 6: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

CONTENTS xiii

6.8.2. Binomial Queue Operations 208

6.8.3. Implementation of Binomial Queues 213

Summary 215

Exercises 218

References 222

Chapter 7 Sorting 225

7.1. Preliminaries 225

7.2. Insertion Sort 226

7.2.1. The Algorithm 226

7.2.2. Analysis of Insertion Sort 226

7.3. A Lower Bound for Simple Sorting Algorithms 227

7.4. Shellsort 228

7.4.1. Worst-Case Analysis of Shellsort 229

7.5. Heapsort 232

7.5.1. Analysis of Heapsort 232

7.6. Mergesort 235

7.6.1. Analysis of Mergesort 237

7.7. Quicksort 240

7.7.1. Picking the Pivot 241

7.7.2. Partitioning Strategy 243

7.7.3. Small Arrays 245

7.7.4. Actual Quicksort Routines 245

7.7.5. Analysis of Quicksort 246

7.7.6. A Linear-Expected-Time Algorithm for Selection 250

7.8. A General Lower Bound for Sorting 252

7.8.1. Decision Trees 252

7.9. Bucket Sort 254

7.10. External Sorting 255

7.10.1. Why We Need New Algorithms 255

7.10.2. Model for External Sorting 255

7.10.3. The Simple Algorithm 255

Page 7: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

x iv CONTENTS

7.10.4. Mufti way Merge 257

7.10.5. Polyphase Merge 258

7.10.6. Replacement Selection 259

Summary 260

Exercises 261

References 265

Chapter 8 The Disjoint Set ADT 269

8.1. Equivalence Relations 269

8.2. The Dynamic Equivalence Problem 270

8.3. Basic Data Structure 271

8.4. Smart Union Algorithms 274

8.5. Path Compression 276

8.6. Worst Case for Union-by-Rank and Path Compression 279

8.6.1. Analysis of the Union/Find Algorithm 2 79

8.7. An Application 285

Summary 287

Exercises 287

References 289

Chapter 9 Graph Algorithms 291

9.1. Definitions 291

9.1.1. Representation of Graphs 292

9.2. Topological Sort 294

9.3. Shortest-Path Algorithms 297

9.3.1. Unweighted Shortest Paths 299

9.3.2. Dijkstra's Algorithm 304

9.3.3. Graphs with Negative Edge Costs 310

9.3.4. Acyclic Graphs 311

9.3.5. All-Pairs Shortest Path 314

9.4. Network Flow Problems 314

9.4.1. A Simple Maximum-Flow Algorithm 315

Page 8: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

CONTENTS XV

9.5. Minimum Spanning Tree 319

9.5.1. Prim's Algorithm 320

9.5.2. Kruskal's Algorithm 323

9.6. Applications of Depth-First Search 325

9.6.1. Undirected Graphs 326

9.6.2. Biconnectivity 327

9.6.3. Euler Circuits 331

9.6.4. Directed Graphs 334

9.6.5. Finding Strong Components 336

9.7. Introduction to NP-Completeness 337

9.7.1. Easy vs. Hard 338

9.7.2. The Class NP 339

9.7.3. NP-Complete Problems 339

Summary 341

Exercises 341

References 349

Chapter 10 Algorithm Design Techniques 353

10.1. Greedy Algorithms 353

10.1.1. A Simple Scheduling Problem 354

10.1.2. Huffman Codes 357

10.1.3. Approximate Bin Packing 362

10.2. Divide and Conquer 370

10.2.1. Running Time of Divide and Conquer Algorithms 371

10.2.2. Closest-Points Problem 373

10.2.3. The Selection Problem 377

10.2.4. Theoretical Improvements for Arithmetic Problems 380

10.3. Dynamic Programming 384

10.3.1. Using a Table Instead of Recursion 384

10.3.2. Ordering Matrix Multiplications 387

Page 9: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

xy i CONTENTS

10.3.3. Optimal Binary Search Tree 389

10.3.4. All-Pairs Shortest Path 393

10.4. Randomized Algorithms 395

10.4.1. Random Number Generators 396

10.4.2. Skip Lists 399

10.4.3. Primality Testing 402

10.5. Backtracking Algorithms 403

10.5.1. The Turnpike Reconstruction Problem 405

10.5.2. Games 409

Summary 415

Exercises 415

References 423

Chapter 11 Amortized Analysis 429

11.1. An Unrelated Puzzle 430

11.2. Binomial Queues 430

11.3. Skew Heaps 435

11.4. Fibonacci Heaps 437

11.4.1. Cutting Nodes in Leftist Heaps 438

11.4.2. Lazy Merging for Binomial Queues 440

11.4.3. The Fibonacci Heap Operations 443

11.4.4. Proof of the Time Bound 443

11.5. Splay Trees 446

Summary 449

Exercises 450

References 452

Chapter 12 Advanced Data Structures and Implementation 453

12.1. Top-Down Splay Trees 453

12.2. Red-Black Trees 460

12.2.1. Bottom-Up Insertion 461

Page 10: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

CONTENTS xvii

12.2.2. Top-Down Red-Black Trees 462

12.2.3. Top-Down Deletion 467

12.3. Deterministic Skip Lists 468

12.4. AA-Trees 474

12.5. Treaps 481

12.6. fe-d Trees 483

12.7. Pairing Heaps 486

Summary 492

Exercises 492

References 495

Appendix A Some Library Routines 499

A.l. Classes in Package Java.lang 499

A. 1.1. Character 499

A.l.2. Integer 500

A.l .3. Object 501

A. 1.4. String 502

A. l .5 . StringBuffer 503

A. 1.6. System 505

A.l.7. Throwable 506

A.2. Classes in Package java.io 506

A.2.1. BufferedReader 506

A.2.2. BufferedWriter 507

A.2.3. File 507

A.2.4. FileReader 508

A.2.5. FileWriter 508

A.2.6. InputStreamReader 509

A.2.7. PrintWriter 509

A.2.8. PushbackReader 509

A.3. Classes in Package java.util 510

A.3.1. Random 510

A.3.2. StringTokenizer 511

Page 11: DATA STRUCTURES & ALGORITHM ANALYSIS IN JAVA™

xviii CONTENTS

Appendix B The Collections Library 513

B.l. Introduction 513

B.2. Basic Classes 514

B.2.1. Collection 514

B.2.2. I terator 514

B.2.3. Comparable 515

B.2.4. Comparator 515

B.3. Lists 516

B.3.1. ArrayListvs. LinkedList 516

B. 3.2. Stacks and Queues 517

B.3.3. Listlterator 517

B.4. Sets 517

B.5. Maps 517

B.5.1. put, get, remove, and contains 518

B.5.2. Getting a Collection from the Map 518

B.5.3. Map.Entry Pairs 518

B.6. Example: Generating a Concordance 519

B.6.1. Collections API Version 519

B.6.2. Version Using Package DataStructures 519

B.7. Example: Shortest-Path Calculation 523

B.7.1. Collections API Implementation 523

B.7.2. Version Using Package DataStructures 526

B.8. Priority Queues 530

Summary 530

Index 531