Analysis of Algorithms

Similar documents
2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Mathematics for Algorithm and System Analysis

Programming Using Python

CS5310 Algorithms 3 credit hours 2 hours lecture and 2 hours recitation every week

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Univariate and Multivariate Methods PEARSON. Addison Wesley

ML for the Working Programmer

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

PARALLEL PROGRAMMING

QUANTITATIVE METHODS. for Decision Makers. Mik Wisniewski. Fifth Edition. FT Prentice Hall

Data Structures and Algorithms Written Examination

Computer Science III

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Exam study sheet for CS2711. List of topics

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

CS 2302 Data Structures Spring 2015

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Numerical Methods for Engineers

How To Understand And Solve A Linear Programming Problem

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Dynamic Programming. Lecture Overview Introduction

AP Computer Science AB Syllabus 1

24. The Branch and Bound Method

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Warshall s Algorithm: Transitive Closure

A Fast Algorithm For Finding Hamilton Cycles

DATA STRUCTURES USING C

Computer Organization

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

NEURAL NETWORK FUNDAMENTALS WITH GRAPHS, ALGORITHMS, AND APPLICATIONS

Data Structure [Question Bank]

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

Applied Computational Economics and Finance

MATHEMATICS Unit Decision 1

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Winning the Hardware-Software Game

Please consult the Department of Engineering about the Computer Engineering Emphasis.

Introduction to Algorithms. Part 3: P, NP Hard Problems

Ant Colony Optimization and Constraint Programming

Basic Programming and PC Skills: Basic Programming and PC Skills:

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

FOPR-I1O23 - Fundamentals of Programming

Section IV.1: Recursive Algorithms and Recursion Trees

CS711008Z Algorithm Design and Analysis

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

An Introduction to Programming and Computer Science

Sample Questions Csci 1112 A. Bellaachia

NEW YORK STATE TEACHER CERTIFICATION EXAMINATIONS

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Dynamic programming. Doctoral course Optimization on graphs - Lecture 4.1. Giovanni Righini. January 17 th, 2013

Discuss the size of the instance for the minimum spanning tree problem.

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

A Note for Students: How to Use This Book

FINITE AUTOMATA AND FORMAL LANGUAGES. Subject Code : 11CS42 Credits: 04. C.I.E : 50 Marks S.E.E: 50 Marks S.E.E: 3 hrs

Data Structures and Algorithms

INTEGER PROGRAMMING. Integer Programming. Prototype example. BIP model. BIP models

Computer Science MS Course Descriptions

Converting a Number from Decimal to Binary

AN INTRODUCTION TO NUMERICAL METHODS AND ANALYSIS

Data Structures Fibonacci Heaps, Amortized Analysis

MATHEMATICAL METHODS OF STATISTICS

Alabama Department of Postsecondary Education

Discrete Mathematics Problems

Catalan Numbers. Thomas A. Dowling, Department of Mathematics, Ohio State Uni- versity.

Financial Statement Analysis

Collatz Sequence. Fibbonacci Sequence. n is even; Recurrence Relation: a n+1 = a n + a n 1.

High Performance Computing for Operation Research

Numerical Analysis An Introduction

Software Configuration Management Patterns

Analysis of Algorithms I: Binary Search Trees

Algorithms and data structures

KEANSBURG SCHOOL DISTRICT KEANSBURG HIGH SCHOOL Mathematics Department. HSPA 10 Curriculum. September 2007

Linear Codes. Chapter Basics

Memory Allocation Technique for Segregated Free List Based on Genetic Algorithm

Complexity Classes P and NP

Approximation Algorithms

PES Institute of Technology-BSC QUESTION BANK

WOLLONGONG COLLEGE AUSTRALIA. Diploma in Information Technology

Florida Math for College Readiness

Classification - Examples

On the k-path cover problem for cacti

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Mathematical Induction. Lecture 10-11

Numerical Recipes in C++

Analysis of Binary Search algorithm and Selection Sort algorithm

International Journal of Software and Web Sciences (IJSWS)

Lecture Notes for Algorithm Analysis and Design

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Fairness in Routing and Load Balancing

Transcription:

Introdoction toxng DCSIStl 2ND EDITION 5 & Analysis of Algorithms Anany Levitin Villanova University PEARSON Addison Wesley Boston San Francisco New York London 'Jbronto Sydney Tokyo Siiigapore Madrid Mexico City Munich Paris CapeTown Hong Kong Montreal

Brief Contents Preface xvii 1 Introduction 1 2 Fundamentals of the Analvsis of Algorithm Efficiency 41 3 Brüte Force 97 4 Divide-and-Conquer 123 5 Decrease-and-Conquer 157 6 Transform-and-Conquer 197 7 Space and Time Tradeoffs 249 8 Dynamic Programming 279 9 Greedy Technique 307 10 Iterative Improvement 335 11 Limitations of Algorithm Power 379 12 Coping with the Limitations of Algorithm Power 415 Epilogue 465 APPENDIX A UsefuI Formulas for the Analysis of Algorithms 469 APPENDIX B Short Tutorial on Recurrence Relations 473 Bibliography 487 Hints to Exercises 497 Index 541

Preface xvii Introduction 1.1 What is an Algorithm? 3 Exercises 1.1 8 1.2 Fundamentals of Algorithmic Problem Solving 9 Understanding the Problem 9 Ascertaining the Capabilities of a Computational Device 11 Choosing between Exact and Approximate Problem Solving 11 Deciding on Appropriate Data Structures 12 Algorithm Design Techniques 12 Methods of Specifying an Algorithm 12 Proving an Algonthm's Correctness 13 Analyzing an Algorithm 14 Coding an Aigorithm 15 Exercises 1.2 17 1.3 Important Problem Types 19 Sorting 19 Searching 20 String Processing 21 Graph Problems 21 Combinatorial Problems 22 Geometrie Problems 22 Numencal Problems 23 Exercises 1.3 23

1.4 Fundamental Data Structures 26 Linear Data Structures 26 Graphs 28 Trees 32 Sets and Dictionaries 36 Exercises 1.4 38 Summary 39 öl Fundamentals of the Analysis of Algorithm Efficiency 41 2.1 Analysis Framework 42 Measuring an Input's Size 43 Units for Measuring Running Time 44 Orders of Growth 45 Worst-Case, Best-Case, and Average-Case Efficiencies 47 Recapitulation of the Analysis Framework 50 Exercises 2.1 50 2.2 Asymptotic Notations and Basic Efficiency Classes 52 Informal Introduction 52 Onotation 53 J2-notation 54 -notation 55 Useful Property Involving the Asymptotic Notations 56 Using Limits for Comparing Orders of Growth 57 Basic Efficiency Classes 58 Exercises 2.2 59 2.3 Mathematical Analysis of Nonrecursive Aigorithms 61 Exercises 2.3 67 2.4 Mathematical Analysis of Recursive Aigorithms 69 Exercises 2.4 76 2.5 Example: Fibonacci Numbers 78 Explicit Formula for the rrth Fibonacci Number 79 Aigorithms for Computing Fibonacci Numbers 80 Exercises 2.5 83

ix 2.6 Empirical Analysis of Algorithms 84 Exercises 2.6 90 2.7 Algorithm Visualization 91 Summary 95 #3» Brüte Force 97 3.1 Selection Sort and Bubble Sort 98 Selection Sort 98 Bubble Sort 100 Exercises 3.1 102 3.2 Sequential Search and Brute-Force String Matching 103 Sequential Search 103 Brute-Force String Matching 104 Exercises 3.2 105 3.3 Closest-Pair and Convex-Hull Problems by Brüte Force 107 Closest-Pair Problem 107 Convex-Hull Problem 108 Exercises 3.3 112 3.4 Exhaustive Search 114 Traveling Salesman Problem 114 Knapsack Problem 115 Assignment Problem 116 Exercises 3.4 119 Summary 120 m Jr Divide-and-Conquer 123 4.1 Mergesort 125 Exercises 4.1 128 4.2 Quicksort 129 Exercises 4.2 134 4.3 Binary Search 135 Exercises 4.3 138

4.4 Binary Tree Traversais and Related Properttes 139 Exercises 4.4 142 4.5 Multiplication of Large Integers and Strassen's Matrix Multiplication 144 Multiplication of Large Integers 144 Strassen's Matrix Multiplication 146 Exercises 4.5 148 4.6 Closest-Pair and Convex-Hull Problems by Divide-and-Conquer 149 Closest-Pair Problem 149 Convex-Hull Problem 150 Exercises 4.6 154 Summary 155 &*"$$? Decrease-and-Conquer 157 5.1 Insertion Sort 160 Exercises 5.1 163 5.2 Depth-First Search and Breadth-First Search 164 Depth-First Search 165 Breadth-First Search 167 Exercises 5.2 170 5.3 Topological Sorting 172 Exercises 5.3 176 5.4 Algorithms for Generating Combinatorial Objects 177 Generating Permutations 178 Generating Subsets 180 Exercises 5.4 181 5.5 Decrease-by-a-Constant-Factor Algorithms 183 Fake-Com Problem 183 Multiplication ä la Russe 184 Josephus Problem 185 Exercises 5.5 187 5.6 Variable-Size-Decrease Algorithms 188 Computing a Median and the Selection Problem 188

XI nterpolation Search 190 Searching and Insertion in a Binary Search Tree 191 The Game of Nim 192 Exercises 5.6 194 Summary 195 Transform-and-Conquer 197 6.1 Presorting 198 Exercises 6.1 201 6.2 Gaussian Elimination 203 LU Decomposition and Other Applications 208 Computing a Matrix Inverse 209 Computing a Determinant 210 Exercises 6.2 212 6.3 Balanced Search Trees 214 AVLTrees 214 2-3 Trees 218 Exercises 6.3 222 6.4 Heaps and Heapsort 223 Notion of the Heap 223 Heapsort 227 Exercises 6.4 229 6.5 Horner's Ruie and Binary Exponentiation 230 Horner's Ruie 231 Binary Exponentiation 233 Exercises 6.5 236 6.6 Problem Reduction 237 Computing the Least Common Multiple 238 Counting Paths in a Graph 239 Reduction of Optimization Problems 240 Linear Programming 241 Reduction to Graph Problems 244 Exercises 6.6 245 Summary 247

xii Contents Space and Time Tradeoffs 249 7.1 Sorting by Counting 250 Exercises7.1 253 7.2 Input Enhancement in String Matching 255 Horspool's Algorithm 255 Boyer-Moore Algorithm 259 Exercises 7.2 264 7.3 Hashing 265 Open Hashing (Separate Chaining) 266 Closed Hashing (Open Addressing} 268 Exercises 7.3 270 7.4 B-Trees 272 Exercises 7.4 275 Summary 276 tf Dynamic Programming 279 8.1 Computing a Binomial Coefficient 280 Exercises 8.1 282 8.2 Warshall's and Floyd's Algorithms 284 Warshall's Algorithm 284 Floyd's Algorithm for the All-Pairs Shortest-Paths Problem 288 Exercises 8.2 292 8.3 Optimal Binary Search Trees 293 Exercises 8.3 298 8.4 The Knapsack Problem and Memory Functions 299 Memory Functions 301 Exercises 8.4 303 Summary 304

XIII»^jj^yä* 'J Greedy Technique 307 9.1 Prim's Algorithm 308 Exercises9.1 313 9.2 Kruskal's Algorithm 315 Disjoint Subsets and Union-Find Algorithms 317 Exercises9.2 321 9.3 Dijkstra's Algorithm 323 Exercises9.3 327 9.4 Huffman Trees 328 Exercises 9.4 332 Summary 333 / Iterative Improvement 335 10.1 The Simplex Method 336 Geometrie Interpretation of Linear Programming 337 An Outline of the Simplex Method 341 Further Notes on the Simplex Method 347 Exercises 10.1 349 10.2 The Maximum-Flow Problem 351 Exercises 10.2 361 10.3 Maximum Matching in Bipartite Graphs 363 Exercises 10.3 369 10.4 The Stable Marriage Problem 371 Exercises 10.4 375 Summary 376 * Limitations of Algorithm Power 379 11.1 Lower-Bound Arguments 380 Trivial Lower Bounds 381 Information-Theoretic Arguments 382

nts Adversary Arguments 382 Problem Reduction 383 Exercises 11.1 385 11.2 Decision Trees 386 Decision Trees for Sorting Algorithms 387 Decision Trees for Searching a Sorted Array 390 Exercises 11.2 392 11.3 P, NP, and /VP-complete Problems 393 P and NP Problems 394 /VP-Complete Problems 398 Exercises 11.3 401 11.4 Challenges of Numerical Algorithms 404 Exercises 11.4 411 Summary 413 '*?', Coping with the Limitations of Algorithm Power 415 12.1 Backtracking 416 n-queens Problem 417 Hamiltonian Circuit Problem 418 Subset-Sum Problem 419 General Remarks 421 Exercises 12.1 422 12.2 Branch-and-Bound 424 Assignment Problem 425 Knapsack Problem 428 Traveling Salesman Problem 430 Exercises 12.2 432 12.3 Approximation Algorithms for A/P-hard Problems 434 Approximation Algorithms for the Traveling Salesman Problem 436 Approximation Algorithms for the Knapsack Problem 446 Exercises 12.3 451 12.4 Algorithms for Solving Nonlinear Equations 452 Bisection Method 454 Method of False Position 457

xv Newton's Method 458 Exercises 12.4 461 Summary 462 Epilogue 465 NDIX Ä UsefuI Formulas for the Analysis of Algorithms 469 Properties of Loganthms 469 Combinatoncs 469 mportant Summation Formulas 470 Sum Manipulation Rules 470 Approximation of a Sum by a Definite Integral 471 Floor and Ceiling Formulas 471 Miscellaneous 471 Short Tutorial on Recurrence Relations 473 Sequences and Recurrence Retations 473 Methods for Solving Recurrence Relations 474 Common Recurrence Types in Algorithm Analysis 479 Bibliography 487 Hints to Exercises 497 Index 541