Table Of ContentHandbook
of Algorithms
and
Data Structures
In Pascal and C
Second Edition
http://avaxhome.ws/blogs/ChrisRedfield
INTERNATIONAL COMPUTER SCIENCE SERIES
Consulting editors AD McGettrick University of Strathc1yde
J van Leeuwen University of Utrecht
SELECTED TITLES IN THE SERIES
Programming Language Translation: A Practical Approach P D Terry
Data Abstraction in Programming Languages J M Bishop
The Specification of Computer Programs W M Turski and T S E Maibaum
Syntax Analysis and Software Tools K J Gough
Functional Programming A J Field and P G Harrison
The Theory of Computability: Programs, Machines, Effectiveness and Feasibility
R Sommerhalder and S C van Westrhenen
An Introduction to Functional Programming through Lambda Calculus G Michaelson
High-Level Languages and their Compilers D Watson
Programming in Ada (3rd Edn) J G P Barnes
Elements of Functional Programming C Reade
Software Development with Modula-2 D Budgen
Program Derivation: The Development of Programs from Specifications R G Dromey
Object-Oriented Programming with Simula B Kirkerud
Program Design with Modula-2 S Eisenbach and C Sadler
Real Time Systems and Their Programming Languages A Burns and A Wellings
Fortran 77 Programming (2nd Edn) T M R Ellis
Prolog Programming for Artificial Intelligence (2nd Edn) I Bratko
Logic for Computer Science S Reeves and M Clarke
Computer Architecture M De Blasi
The Programming Process J T Latham, V J Bush and I D Cottam
Handbook
of Algorithms
and
Data Structures
In Pascal and C
Second Edition
G.H. Gonnet
ETH, Zurich
R. Baeza-Yates
University of Chile, Santiago
•
~~
ADDISON -WESLEY
PUBLISHING
COl\IPANY
Wokingham, England. Reading, Massachusetts. Menlo Park, California. New York
Don Mills, Ontario. Amsterdam. Bonn. Sydney. Singapore
Tokyo. l\ladrid • San Juan. l\liIan • Paris. l\lexico City. Seoul. Taipei
© 1991 Addison-Wesley Publishers Ltd.
© 1991 Addison-Wesley Publishing Company Inc.
All rights reserved. No part ofthis publication may be reproduced, stored in a retrieval system,
or transmitted in any form or by any means, electronic, mechanical, photocopying, recording
or otherwise, without prior written permission of the publisher.
The programs in this book have been included for their instructional value. They have been
tested with care but are not guaranteed for any particular purpose. The publisher does not offer
any warranties or representations, nor does it accept any liabilities with respect to the
programs.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Addison-Wesley has made every attempt to supply trademark
information about manufacturers and their products mentioned in this book. A list of the
trademark designations and their owners appears on p. xiv.
Cover designed by Crayon Design of Henley-on-Thames and
printed by The Riverside Printing Co. (Reading) Ltd.
Printed in Great Britain by Mackays of Chatham pic, Chatham, Kent.
First edition published 1984. Reprinted 1985.
Second edition printed 1991.
British Library Cataloguing in Publication Data
Gonnet, G. H. (Gaston H.)
Handbook of algorithms and data structures: in Pascal and
C.-2nd. ed.
1. Programming. Algorithms
I. Title II. Baeza-Yates, R. (Ricardo)
005.1
ISBN 0-201-41607-7
Library of Congress Cataloging in Publication Data
Gonnet, G. H. (Gaston H.)
Handbook of algorithms and data structures : in Pascal and C I
G.H. Gonnet, R. Baeza-Yates. --2nd ed.
p. cm. --(International computer science series)
Includes bibliographical references (p. ) and index.
ISBN 0-201-41607-7
1. Pascal (Computer program language) 2. (Computer program
language) 3. Algorithms. 4. Data structures (Computer science)
I. Baeza-Yates, R. (Ricardo) II. Title. III. Series.
QA76.73.P2G66 1991 90-26318
005. 13'3--dc20 CIP
To my boys: Miguel, Pedro Julio and Ignacio
and my girls: Ariana and Marta
Preface
Preface to the first edition
Computer Science has been, throughout its evolution, more an art than a sci
ence. My favourite example which illustrates this point is to compare a major
software project (like the writing of a compiler) with any other major project
(like the construction of the CN tower in Toronto). It would be absolutely
unthinkable to let the tower fall down a few times while its design was being
debugged: even worse would be to open it to the public before discovering
some other fatal flaw. Yet this mode of operation is being used everyday by
almost everybody in software production.
Presently it is very difficult to 'stand on your predecessor's shoulders',
most of the time we stand on our predecessor's toes, at best. This handbook
was written with the intention of making available to the computer scien
tist, instructor or programmer the wealth of information which the field has
generated in the last 20 years.
Most of the results are extracted from the given references. In some cases
the author has completed or generalized some of these results. Accuracy is
certainly one of our goals, and consequently the author will cheerfully pay
$2.00 for each first report of any type of error appearing in this handbook.
Many people helped me directly or indirectly to complete this project.
Firstly lowe my family hundreds of hours of attention. All my students
and colleagues had some impact. In particular I would like to thank Maria
Carolina Monard, Nivio Ziviani, J. Ian 1I.Iunro, Per-Ake Larson, Doron Rotem
and Derick Wood. Very special thanks go to Frank W. Tompa who is also the
coauthor of chapter 2. The source material for this chapter appears in a joint
paper in the November 1983 issue of Communications of the A CM.
Montevideo G.II. Gonnet
December 1983
VII
viii PREFACE
Preface to the second edition
The first edition of this handbook has been very well received by the com
munity, and this has given us the necessary momentum for writing a second
edition. In doing so, R. A. Baeza-Yates has joined me as a coauthor. Without
his help this version would have never appeared.
This second edition incorporates many new results and a new chapter on
text searching. The area of text managing, in particular searching, has risen in
importance and matured in recent times. The entire subject of the handbook
has matured too; our citations section has more than doubled in size. Table
searching algorithms account for a significant part of this growth.
Finally we would like to thank the over one hundred readers who notified us
about errors and misprints, they have helped us tremendously in correcting
all sorts of blemishes. We are especially grateful for the meticulous, even
amazing, work of Lynne Balfe, the proofreader. We will continue cheerfully
to pay $4.00 (increased due to inflation) for each first report of an error.
Ziirich G.B. Gonnet
December 1990
Santiago de Chile R.A. Baeza-Yates
December 1990
Contents
Preface vii
1 Introduction 1
1.1 Structure of the chapters 1
1.2 Naming of variables 3
1.3 Probabilities 4
1.4 Asymptotic notation 5
1.5 About the programming languages 5
1.6 On the code for the algorithms 6
1.7 Complexity measures and real timings 7
2 Basic Concepts 9
2.1 Data structure description 9
2.1.1 Grammar for data objects 9
2.1.2 Constraints for data objects 12
2.1.2.1 Sequential order 13
2.1.2.2 Uniqueness 13
2.1.2.3 Hierarchical order 13
2.1.2.4 IIierarchical balance 13
2.1.2.5 Optimality 14
2.2 Algorithm descriptions 14
2.2.1 Basic (or atomic) operations 15
2.2.2 Building procedures 17
2.2.2.1 Composition 17
2.2.2.2 Alternation 21
2.2.2.3 Conformation 22
2.2.2.4 Self-organization 23
2.2.3 Interchangeability 23
ix
Description:This second edition brings together many useful algorithms and their associated data structures in a single, handy reference, featuring a new section on text manipulation algorithms and expanded coverage of arithmetical algorithms. Each algorithm is coded in both C and Pascal.