个 AN INTRODUCTION TO THE ANALYSIS OF ALGORITHMS SECOND E DIT I O N ROBERT SEDGEWICK PHILIPPE FLAJOLET www.it-ebooks.info
www.it-ebooks.info
FOREWORD P EOPLE who analyze algorithms have double happiness.First of all they experience the sheer beauty of elegant mathematical patterns that sur- round elegant computational procedures.Then they receive a practical payoff when their theories make it possible to get other jobs done more quickly and more economically. Mathematical models have been a crucial inspiration for all scientific activity,even though they are only approximate idealizations of real-world phenomena.Inside a computer,such models are more relevant than ever be- fore,because computer programs create artificial worlds in which mathemat- ical models often apply precisely.I think that's why I got hooked on analysis of algorithms when I was a graduate student,and why the subject has been my main life's work ever since. Until recently,however,analysis of algorithms has largely remained the preserve of graduate students and post-graduate researchers.Its concepts are not really esoteric or difficult,but they are relatively new,so it has taken awhile to sort out the best ways of learning them and using them. Now,after more than 40 years of development,algorithmic analysis has matured to the point where it is ready to take its place in the standard com- puter science curriculum.The appearance of this long-awaited textbook by Sedgewick and Flajolet is therefore most welcome.Its authors are not only worldwide leaders of the field,they also are masters of exposition.I am sure that every serious computer scientist will find this book rewarding in many ways. D.E.Knuth www.it-ebooks.info
F O R EW O R D P EOPLE who analyze algorithms have double happiness. First of all they experience the sheer beauty of elegant mathematical patterns that surround elegant computational procedures. Ļen they receive a practical payoff when their theories make it possible to get other jobs done more quickly and more economically. Mathematical models have been a crucial inspiration for all scientiŀc activity, even though they are only approximate idealizations of real-world phenomena. Inside a computer, such models are more relevant than ever before, because computer programs create artiŀcial worlds in which mathematical models often apply precisely. I think that’s why I got hooked on analysis of algorithms when I was a graduate student, and why the subject has been my main life’s work ever since. Until recently, however, analysis of algorithms has largely remained the preserve of graduate students and post-graduate researchers. Its concepts are not really esoteric or difficult, but they are relatively new, so it has taken awhile to sort out the best ways of learning them and using them. Now, after more than 40 years of development, algorithmic analysis has matured to the point where it is ready to take its place in the standard computer science curriculum. Ļe appearance of this long-awaited textbook by Sedgewick and Flajolet is therefore most welcome. Its authors are not only worldwide leaders of the ŀeld, they also are masters of exposition. I am sure that every serious computer scientist will ŀnd this book rewarding in many ways. D. E. Knuth www.it-ebooks.info
PREFACE ookbe thoughvvof th niques used in the mathematical analysis of algorithms.The material covered draws from classical mathematical topics,including discrete mathe- matics,elementary real analysis,and combinatorics,as well as from classical computer science topics,including algorithms and data structures.The focus ison“average-case”or“probabilistic”"analysis,though the basic mathematical tools required for“worst-case”or“complexity”analysis are covered as well.. We assume that the reader has some familiarity with basic concepts in both computer science and real analysis.In a nutshell,the reader should be able to both write programs and prove theorems.Otherwise,the book is intended to be self-contained. The book is meant to be used as a textbook in an upper-level course on analysis of algorithms.It can also be used in a course in discrete mathematics for computer scientists,since it covers basic techniques in discrete mathemat- ics as well as combinatorics and basic properties of important discrete struc- tures within a familiar context for computer science students.It is traditional to have somewhat broader coverage in such courses,but many instructors may find the approach here to be a useful way to engage students in a substantial portion of the material.The book also can be used to introduce students in mathematics and applied mathematics to principles from computer science related to algorithms and data structures. Despite the large amount of literature on the mathematical analysis of algorithms,basic information on methods and models in widespread use has not been directly accessible to students and researchers in the field.This book aims to address this situation,bringing together a body of material intended to provide readers with both an appreciation for the challenges of the field and the background needed to learn the advanced tools being developed to meet these challenges.Supplemented by papers from the literature,the book can serve as the basis for an introductory graduate course on the analysis of algo- rithms,or as a reference or basis for self-study by researchers in mathematics or computer science who want access to the literature in this field. Preparation.Mathematical maturity equivalent to one or two years'study at the college level is assumed.Basic courses in combinatorics and discrete mathematics may provide useful background(and may overlap with some www.it-ebooks.info
P R E F A C E T HIS book is intended to be a thorough overview of the primary techniques used in the mathematical analysis of algorithms. Ļe material covered draws from classical mathematical topics, including discrete mathematics, elementary real analysis, and combinatorics, as well as from classical computer science topics, including algorithms and data structures. Ļe focus is on “average-case” or “probabilistic” analysis, though the basic mathematical tools required for “worst-case” or “complexity” analysis are covered as well. We assume that the reader has some familiarity with basic concepts in both computer science and real analysis. In a nutshell, the reader should be able to both write programs and prove theorems. Otherwise, the book is intended to be self-contained. Ļe book is meant to be used as a textbook in an upper-level course on analysis of algorithms. It can also be used in a course in discrete mathematics for computer scientists, since it covers basic techniques in discrete mathematics as well as combinatorics and basic properties of important discrete structures within a familiar context for computer science students. It is traditional to have somewhat broader coverage in such courses, but many instructors may ŀnd the approach here to be a useful way to engage students in a substantial portion of the material. Ļe book also can be used to introduce students in mathematics and applied mathematics to principles from computer science related to algorithms and data structures. Despite the large amount of literature on the mathematical analysis of algorithms, basic information on methods and models in widespread use has not been directly accessible to students and researchers in the ŀeld. Ļis book aims to address this situation, bringing together a body of material intended to provide readers with both an appreciation for the challenges of the ŀeld and the background needed to learn the advanced tools being developed to meet these challenges. Supplemented by papers from the literature, the book can serve as the basis for an introductory graduate course on the analysis of algorithms, or as a reference or basis for self-study by researchers in mathematics or computer science who want access to the literature in this ŀeld. Preparation. Mathematical maturity equivalent to one or two years’ study at the college level is assumed. Basic courses in combinatorics and discrete mathematics may provide useful background (and may overlap with some www.it-ebooks.info
viii PREFACE material in the book),as would courses in real analysis,numerical methods, or elementary number theory.We draw on all of these areas,but summarize the necessary material here,with reference to standard texts for people who want more information. Programming experience equivalent to one or two semesters'study at the college level,including elementary data structures,is assumed.We do not dwell on programming and implementation issues,but algorithms and data structures are the central object of our studies.Again,our treatment is complete in the sense that we summarize basic information,with reference to standard texts and primary sources. Related books.Related texts include The Art of Computer Programming by Knuth;Algorithms,Fourth Edition,by Sedgewick and Wayne;Introduction to Algorithms by Cormen,Leiserson,Rivest,and Stein;and our own Analytic Combinatorics.This book could be considered supplementary to each of these. In spirit,this book is closest to the pioneering books by Knuth.Our fo- cus is on mathematical techniques of analysis,though,whereas Knuth's books are broad and encyclopedic in scope,with properties of algorithms playing a primary role and methods of analysis a secondary role.This book can serve as basic preparation for the advanced results covered and referred to in Knuth's books.We also cover approaches and results in the analysis of algorithms that have been developed since publication of Knuth's books. We also strive to keep the focus on covering algorithms of fundamen- tal importance and interest,such as those described in Sedgewick's Algorithms (now in its fourth edition,coauthored by K.Wayne).That book surveys classic algorithms for sorting and searching,and for processing graphs and strings. Our emphasis is on mathematics needed to support scientific studies that can serve as the basis of predicting performance of such algorithms and for com- paring different algorithms on the basis of performance. Cormen,Leiserson,Rivest,and Stein's Introduction to Algorithms has emerged as the standard textbook that provides access to the research litera- ture on algorithm design.The book (and related literature)focuses on design and the theory of algorithms,usually on the basis of worst-case performance bounds.In this book,we complement this approach by focusing on the aral- ysis of algorithms,especially on techniques that can be used as the basis for scientific studies (as opposed to theoretical studies).Chapter 1 is devoted entirely to developing this context. www.it-ebooks.info
viii P Ş ő Œ ō ŏ ő material in the book), as would courses in real analysis, numerical methods, or elementary number theory. We draw on all of these areas, but summarize the necessary material here, with reference to standard texts for people who want more information. Programming experience equivalent to one or two semesters’ study at the college level, including elementary data structures, is assumed. We do not dwell on programming and implementation issues, but algorithms and data structures are the central object of our studies. Again, our treatment is complete in the sense that we summarize basic information, with reference to standard texts and primary sources. Related books. Related texts include Ļe Art of Computer Programming by Knuth; Algorithms, Fourth Edition, by Sedgewick and Wayne; Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein; and our own Analytic Combinatorics. Ļis book could be considered supplementary to each of these. In spirit, this book is closest to the pioneering books by Knuth. Our focus is on mathematical techniques of analysis, though, whereas Knuth’s books are broad and encyclopedic in scope, with properties of algorithms playing a primary role and methods of analysis a secondary role. Ļis book can serve as basic preparation for the advanced results covered and referred to in Knuth’s books. We also cover approaches and results in the analysis of algorithms that have been developed since publication of Knuth’s books. We also strive to keep the focus on covering algorithms of fundamental importance and interest, such as those described in Sedgewick’s Algorithms (now in its fourth edition, coauthored by K.Wayne). Ļat book surveys classic algorithms for sorting and searching, and for processing graphs and strings. Our emphasis is on mathematics needed to support scientiŀc studies that can serve as the basis of predicting performance of such algorithms and for comparing different algorithms on the basis of performance. Cormen, Leiserson, Rivest, and Stein’s Introduction to Algorithms has emerged as the standard textbook that provides access to the research literature on algorithm design. Ļe book (and related literature) focuses on design and the theory of algorithms, usually on the basis of worst-case performance bounds. In this book, we complement this approach by focusing on the analysis of algorithms, especially on techniques that can be used as the basis for scientiŀc studies (as opposed to theoretical studies). Chapter 1 is devoted entirely to developing this context. www.it-ebooks.info
PREFACE ix This book also lays the groundwork for our Analytic Combinatorics,a general treatment that places the material here in a broader perspective and develops advanced methods and models that can serve as the basis for new research,not only in the analysis of algorithms but also in combinatorics and scientific applications more broadly.A higher level of mathematical matu- rity is assumed for that volume,perhaps at the senior or beginning graduate student level.Of course,careful study of this book is adequate preparation. It certainly has been our goal to make it sufficiently interesting that some readers will be inspired to tackle more advanced material! How to use this book.Readers of this book are likely to have rather diverse backgrounds in discrete mathematics and computer science.With this in mind,it is useful to be aware of the implicit structure of the book:nine chap- ters in all,an introductory chapter followed by four chapters emphasizing mathematical methods,then four chapters emphasizing combinatorial struc- tures with applications in the analysis of algorithms,as follows: INTRODUCTION ONE ANALYSIS OF ALGORITHMS DISCRETE MATHEMATICAL METHODS TWO RECURRENCE RELATIONS THREE GENERATING FUNCTIONS FOUR ASYMPTOTIC APPROXIMATIONS FIVE ANALYTIC COMBINATORICS ALGORITHMS AND COMBINATORIAL STRUCTURES SIX TREES SEVEN PERMUTATIONS EIGHT STRINGS AND TRIES NINE WORDS AND MAPPINGS Chapter 1 puts the material in the book into perspective,and will help all readers understand the basic objectives of the book and the role of the re- maining chapters in meeting those objectives.Chapters 2 through 4 cover www.it-ebooks.info
P Ş ő Œ ō ŏ ő ix Ļis book also lays the groundwork for our Analytic Combinatorics, a general treatment that places the material here in a broader perspective and develops advanced methods and models that can serve as the basis for new research, not only in the analysis of algorithms but also in combinatorics and scientiŀc applications more broadly. A higher level of mathematical maturity is assumed for that volume, perhaps at the senior or beginning graduate student level. Of course, careful study of this book is adequate preparation. It certainly has been our goal to make it sufficiently interesting that some readers will be inspired to tackle more advanced material! How to use this book. Readers of this book are likely to have rather diverse backgrounds in discrete mathematics and computer science. With this in mind, it is useful to be aware of the implicit structure of the book: nine chapters in all, an introductory chapter followed by four chapters emphasizing mathematical methods, then four chapters emphasizing combinatorial structures with applications in the analysis of algorithms, as follows: ANALYSIS OF ALGORITHMS RECURRENCE RELATIONS GENERATING FUNCTIONS ASYMPTOTIC APPROXIMATIONS ANALYTIC COMBINATORICS TREES PERMUTATIONS STRINGS AND TRIES WORDS AND MAPPINGS INTRODUCTION DISCRETE MATHEMATICAL METHODS ALGORITHMS AND COMBINATORIAL STRUCTURES ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE Chapter 1 puts the material in the book into perspective, and will help all readers understand the basic objectives of the book and the role of the remaining chapters in meeting those objectives. Chapters 2 through 4 cover www.it-ebooks.info