Analysis o f A lgorithms

~~ I_ND EDIIIQN ~

I ntroduction t o

*

The Design &

Analysis o f Algorithms

I ND EDITION "

I ntroduction t o

~

~

~

Anany Levitin

Villanova University

B oston San Francisco N ew York

L ondon T oronto S ydney Tokyo Singapore Madrid

M exico City M unich Paris C ape Town H ong K ong M ontreal

Publisher

Acquisitions E ditor

P roject E ditor

P roduction Supervisor

M arketing Manager

P roject Management & Composition

Technical A rt

C opyeditor

C over Design

C over Illustration

S enior P re press Supervisor

Greg Tobin

Matt Goldstein

Katherine Harutunian

Marilyn L loyd

Michelle Brown

Windfall Software, using Z zT£X

George Nichols

Richard Camp

Joyce Cosentino Wells

Jennifer M. K ohnke

Caroline Fell

Access the latest information a bout Addison-Wesley titles from o ur World Wide Web site: http://www.aw-bc.com/cornputing

Many o f t he designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, a nd AddisonWesley was aware o f a t rademark claim, the designations have been printed in initial caps o r all caps.

I f y ou purchased this book within the United S tates o r C anada you should b e a ware that

i t has been wrongfully imported without the approval o f t he Publisher o r t he Author. C opyright© 2007 by P earson E ducation, Inc.

All rights reserved. N o p art o f this publication may b e r eproduced, stored in a retrieval system, o r t ransmitted, in any form o r by any means, electronic, mechanical, photocopying, recording, o r otherwise, without the prior written permission of the publisher. P rinted i n t he U nited States o f America.

ISBN 0-321-36413-9

3 4 5 6 7 8 9 1 0-DOH-09 08 07

B rief C ontents

Preface

1 I ntroduction

2 F undamentals o f t he A nalysis o f A lgorithm E fficiency 3 B rute Force

4 D ivide-and-Conquer

5 D ecrease-and-Conquer

6 T ransform-and-Conquer

1 Space a nd T ime Tradeoffs

8 D ynamic P rogramming

9 G reedy T echnique

1 0 I terative I mprovement

1 1 l imitations o f A lgorithm P ower

1 2 C oping w ith t he L imitations o f A lgorithm P ower

E pilogue

x vii

1

41

97

123

157

197

249

279

307

335

379

415

465

APPENDIX A

U seful F ormulas f or t he A nalysis o f A lgorithms

469

APPENDIX B

S hort T utorial o n Recurrence Relations

473

B ibliography

487

497

541

H ints t o Exercises

I ndex

v

.

.

;

!

C ontents

Preface

1 Introduction

1.1 W hat i s a n A lgorithm?

E xercises 1.1

1.2 F undamentals o f A lgorithmic P roblem S olving

xvii

1

3

8

Understanding the Problem

Ascertaining the Capabilities o f a Computational Device

Choosing between Exact and Approximate Problem Solving

Deciding on Appropriate Data Structures

Algorithm Design Techniques

Methods of Specifying an Algorithm

Proving an Algorithm's Correctness

Analyzing an Algorithm

Coding an Algorithm

9

9

11

11

12

12

12

13

14

15

E xercises 1.2

17

1.3 I mportant P roblem T ypes

Sorting

Searching

String Processing

Graph Problems

Combinatorial Problems

Geometric Problems

Numerical Problems

19

19

20

21

21

22

22

23

E xercises 1.3

23

vii

---~----·-·

I

viii

Contents

j li

;!'

rll

S ummary

!:!

26

26

28

32

36

38

39

f undamentals o f t he A nalysis o f A lgorithm

E fficiency

41

1 .4 F undamental D ata S tructures

II:,

.

Linear Data Structures

Graphs

Trees

Sets and Dictionaries

il

,'.II

I!

1:1

E xercises 1.4

f:i

III:'

1\,

I'-!,,

i!i

if!

li1

II

I

I

!

'

'

2

2.1 A nalysis F ramework

Measuring an Input's Size

Units for Measuring Running Time

Orders of Growth

Worst-Case. Best-Case, and Average-Case Efficiencies

Recapitulation of the Analysis Framework

Exercises 2.1

2.2 A symptotic N otations a nd B asic E...