This book is to examine the most important algorithms in use on today's computers and to teach the basic techniques with the increasing number who was interested in computer users becoming increasingly serious. It is appropriate for use as a textbook for a course Monday, Tuesday or Wednesday in the computer Science: After students have had some programming skills and familiarity computer system, but before they have advanced specialized courses field of computer science or computer applications. In addition, the book may be useful as a reference for those already familiar with material, as it contains some implementations of computer useful algorithms. The book consists of forty chapters which are. | ALGORITHMS ROBERT SEDGEWICK BROWN UNIVERSITY ADDISON-WESLEY PUBLISHING COMpANy Reading Massachusetts Menlo Park California London Amsterdam Don Mills Ontario Sydney To Adam Brett Robbie and especially Linda This book is in the Addison-Wesley Series in Computer Science Consulting Editor Michael A. Harrison Sponsoring Editor James T. DeWolfe Library of Congress Cataloging in Publication Data Sedgewick Robert 1946- Algorithms. 1. Algorithms. I. Title. 1983 ISBN O-201 -06672-6 82-11672 Reproduced by Addison-Wesley from camera-ready copy supplied by the author. Reprinted with corrections August 1984 Copyright 1983 by Addison-Wesley Publishing Company Inc. All rights reserved. No part of this 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. Printed in the United States of America. ISBN o-201-06672-6 FGHIJ-HA-8987654 Preface This book is intended to survey the most important algorithms in use on computers today and to teach fundamental techniques to the growing number of people who are interested in becoming serious computer users. It is appropriate for use as a textbook for a second third or fourth course in computer science after students have acquired some programming skills and familiarity with computer systems but before they have specialized courses in advanced areas of computer science or computer applications. Additionally the book may be useful as a reference for those who already have some familiarity with the material since it contains a number of computer implementations of useful algorithms. The book consists of forty chapters which are grouped into seven major parts mathematical algorithms sorting searching string processing geometric algorithms graph algorithms and advanced topics. A major goal in the development of this book has been to bring together the fundamental methods from