### Fundamentals

### of the Average

### Case Analysis of

### Particular Algorithms

### Wiley-Teubner Series in

### Computer Science

Editorial Board

Günter Hotz
*Universität des Saarlandes. *

*Fachbereich Angewandte Mathematik und Informatik. *
*Saarbrücken. *

*Germanv *
Michael

### J.

Fischer*Department of Computer Science.*

*New Haven, Connecticut. *
*USA *

Maurice Nivat

*U.E.R. de Mathematique et lnformatique. *
*Universite Paris VII. *

*Paris. France *
David W. Barron
*Department of Computer Science. *

*Universitv of Southampton. *
*UK *

Kemp: Fundamentals of the Average Case Analysis of Particular Algorithms

Loeckx/Sieber/Stansifer: The Foundations of Program Verification

## Fundamentals

## of the Average

## Case Analysis of

## Particular Algorithms

### Rainer Kemp

*Fachbereich Informatik *

*Universität Frankfurt *

*Germany *

Copyright© 1984 by Springer Fachmedien Wiesbaden Originally published by B.G. Teubner, Stuttgart in 1984 Ali rights reserved.

No part of this book may be reproduced by any means, nor transmitted, nor translated into a machine language without the written permission of the publisher.

**Library of Congress Cataloging in Publication Data: **

Kemp, Rainer.

Fundamentals of the average case analysis of particular algorithms.

(Wiley-Teubner series in computer science) lncludes bibliographical references and index. 1. Electronic digital computers-Programming. 2. Algorithms. 1. Title. 11. Series.

QA76.6.K53 1984 519.4 83-16663

ISBN 978-3-519-02100-1 ISBN 978-3-663-12191-6 (eBook) DOI 10.1007/978-3-663-12191-6

**British Library Cataloguing in Publication Data: **

Kemp, Rainer.

Fundamentals of the average case analysis of particular algorithms.-(Wiley-Teubner series in computer science)

1. Algorithms.

1. Title.

511 '.6 QA9.58 ISBN 978-3-519-02100-1

**CIP-Kurztitelaufnahme der Deutschen Bibliothek: **

Kemp, Rainer.

Fundamentals of the average case analysis of particular algorithms/ Rai ner Kemp.

(Wiley-Teubner series in computer science) ISBN 978-3-519-02100-1

**Preface **

This book provides an introduction to the analysis of particular algorithms. lt has its origin in lecture courses given at the Universität des Saarlandes, Saarbrücken in 1980 and attheJohann Wolfgang Goethe-Universität, Frankfurt a.M. in 1982. The material can be covered in a one-semester course.

ln preparing the notes for publication as a book, I have added a considerable amount of material additional to the lecture notes, with the intention of making the book more useful. My prime consideration has been to produce a textbook whose scope is selective; some of the omitted material is outlined in various exercises and should be useful in indicating possible approaches to certain problems. Moreover, problems are provided to furnish examples, to expand on the material or to indicate related results, and occasionally to guide the reader through the steps of lengthy proofs and derivations. I have referred, in various places, to those books and original papers which have been of particular assistance to me.

I wish to take this opportunity to thank all those who have had apart in this work, and who have made this book possible. I am particularly indebted to Professor Dr. Günter Hotz for his encouragement in the writing of this textbook. Special thanks are due to Ute Schürfeld for careful reading of the text. Dr. P. Spuhler from Teubner-Verlag provided co-operative and competent support in all editorial problems. Finally, I wish to thank Teubner-Verlag and John Wiley

### &

Sons for very good and timely editorial work.Frankfurt a.M., West Germany December 1982

V

**Contents **

Preface 1. lntroduction

Exercises

2. A Simple Class of Algorithms

2.1 Definitior1 *ot *a Random Algorithm
2.2 Analysis of Random Algorithms
Exercises

3. Permutationsand Their Applications 3.1 Motivation

3.2 Average Values of Characteristic Quantities of Permutations

3.2.1 AverageNumber of Cycles of a Permutation

3.2.2 Average Length of a Cycle

3.2.3 A General Approach

3.2.4 Average Number of Falls and Rises of a Permutation

3.2.5 Average Length of a Run

3.2.6 Average Length of the k-th Run

3.2.7 Average Number of Inversions of a Permutation Exercises

4. Random Walks, Trees, Lists 4.1 Random Walks

4.2 Enumeration of Random Walks

4.2.1 Uniform Random Walks

4.2.2 *(d, *

*e. *

u) -Random Walks
4.2.3 Non-negative Closed Random Walks 4.3 One-to-one Correspondences

4.3.1 Random Walks-Ordered Trees

4.3.2 Random Walks-Dynamic Data Structures

### vii

V 1 10 12 12 15### 19

21 21 26 28### 29

30 34 37 41 42### 45

50 50 52 52### 60

### 72

### 81

### 81

84### viii

4.4 Exact and Asymptotical Enumeration Results 92

4.4.1 Useful Techniques for Determining Asymptotics From

Generating Functions 92

4.4.2 Ordered Trees 101

4.4.3 Derivation Trees of Context-free Grammars 111

4.4.4 Dynamic Data Structures 114

Exercises 121

5. Applications 130

5.1 Reduction of Binary Trees 130

5.1 .1 The Reduction of Binary Trees by a Stack 132 5.1.2 The Reduction of Binary Trees by an Optimal Algorithm 150 5.1.3 The Reduction of Binary Trees by an lnput-restricted Deque 158 5.2 Two Algorithms for the Recognition of Dycklanguages 173

5.2.1 The Recognition by a Stack 174

5.2.2 The Recognition by a Two-Way-One-Counter Automaten 176

5.3 Batcher's Algorithm 183

5.3.1 Networks for Sorting 183

5.3.2 The Number of Exchanges in the Warst Case 188 5.3.3 The Number of Exchanges in the Average Case 190

Exercises 201

APPENDIX A 204

1. Basic Definitions of Probability Theory 204

2. Grammars-Formal Power Series 205

3. Generating Functions 206

4. Linear Recurrences 207

5. Dirichlet Series 207

6. Laurent Series-Residues-Cauchy Integral Formula 207

7. Euler's Summation Formula 208

APPENDIX B 209

1. Numbers in Combinatorial Analysis 209

2. Special Functions 214

References 221